Remove unused variable.
[dragonfly.git] / sys / bus / cam / scsi / scsi_all.c
1 /*
2  * Implementation of Utility functions for all SCSI device types.
3  *
4  * Copyright (c) 1997, 1998 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  * $FreeBSD: src/sys/cam/scsi/scsi_all.c,v 1.14.2.11 2003/10/30 15:06:35 thomas Exp $
30  * $DragonFly: src/sys/bus/cam/scsi/scsi_all.c,v 1.18 2007/11/24 03:09:57 pavalos Exp $
31  */
32
33 #include <sys/param.h>
34
35 #ifdef _KERNEL
36
37 #include <opt_scsi.h>
38 #include <sys/systm.h>
39 #include <sys/libkern.h>
40
41 #else
42
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 #define ksnprintf       snprintf        /* ick, userland uses us too */
49 #define kprintf         printf
50 #endif
51
52 #include "../cam.h"
53 #include "../cam_ccb.h"
54 #include "../cam_xpt.h"
55 #include "scsi_all.h"
56 #include <sys/sbuf.h>
57 #ifndef _KERNEL
58 #include <sys/camlib.h>
59
60 #ifndef FALSE
61 #define FALSE   0
62 #endif /* FALSE */
63 #ifndef TRUE
64 #define TRUE    1
65 #endif /* TRUE */
66 #define ERESTART        -1              /* restart syscall */
67 #define EJUSTRETURN     -2              /* don't modify regs, just return */
68 #endif /* !_KERNEL */
69
70 static int      ascentrycomp(const void *key, const void *member);
71 static int      senseentrycomp(const void *key, const void *member);
72 static void     fetchtableentries(int sense_key, int asc, int ascq,
73                                   struct scsi_inquiry_data *,
74                                   const struct sense_key_table_entry **,
75                                   const struct asc_table_entry **);
76
77 #if !defined(SCSI_NO_OP_STRINGS)
78
79 #define D 0x001
80 #define T 0x002
81 #define L 0x004
82 #define P 0x008
83 #define W 0x010
84 #define R 0x020
85 #define S 0x040
86 #define O 0x080
87 #define M 0x100
88 #define C 0x200
89 #define A 0x400
90 #define E 0x800
91
92 #define ALL 0xFFF
93
94 static struct op_table_entry plextor_cd_ops[] = {
95         {0xD8, R, "CD-DA READ"}
96 };
97
98 static struct scsi_op_quirk_entry scsi_op_quirk_table[] = {
99         {
100                 /*
101                  * I believe that 0xD8 is the Plextor proprietary command
102                  * to read CD-DA data.  I'm not sure which Plextor CDROM
103                  * models support the command, though.  I know for sure
104                  * that the 4X, 8X, and 12X models do, and presumably the
105                  * 12-20X does.  I don't know about any earlier models,
106                  * though.  If anyone has any more complete information,
107                  * feel free to change this quirk entry.
108                  */
109                 {T_CDROM, SIP_MEDIA_REMOVABLE, "PLEXTOR", "CD-ROM PX*", "*"},
110                 sizeof(plextor_cd_ops)/sizeof(struct op_table_entry),
111                 plextor_cd_ops
112         }
113 };
114
115 static struct op_table_entry scsi_op_codes[] = {
116 /*
117  * From: ftp://ftp.symbios.com/pub/standards/io/t10/drafts/spc/op-num.txt
118  * Modifications by Kenneth Merry (ken@FreeBSD.ORG)
119  *
120  * Note:  order is important in this table, scsi_op_desc() currently
121  * depends on the opcodes in the table being in order to save search time.
122  */
123 /*  
124  * File: OP-NUM.TXT
125  *
126  * SCSI Operation Codes
127  * Numeric Sorted Listing
128  * as of 11/13/96
129  * 
130  *     D - DIRECT ACCESS DEVICE (SBC)                    device column key
131  *     .T - SEQUENTIAL ACCESS DEVICE (SSC)              -------------------
132  *     . L - PRINTER DEVICE (SSC)                       M = Mandatory
133  *     .  P - PROCESSOR DEVICE (SPC)                    O = Optional
134  *     .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC)    V = Vendor specific
135  *     .  . R - CD DEVICE (MMC)                         R = Reserved
136  *     .  .  S - SCANNER DEVICE (SGC)                   Z = Obsolete
137  *     .  .  .O - OPTICAL MEMORY DEVICE (SBC)
138  *     .  .  . M - MEDIA CHANGER DEVICE (SMC)
139  *     .  .  .  C - COMMUNICATION DEVICE (SSC)
140  *     .  .  .  .A - STORAGE ARRAY DEVICE (SCC)
141  *     .  .  .  . E - ENCLOSURE SERVICES DEVICE (SES)
142  * OP  DTLPWRSOMCAE  Description
143  * --  ------------  ---------------------------------------------------- */
144 /* 00  MMMMMMMMMMMM  TEST UNIT READY */
145 {0x00, ALL,             "TEST UNIT READY"},
146
147 /* 01   M            REWIND */
148 {0x01, T,           "REWIND"},
149 /* 01  Z V ZO ZO     REZERO UNIT */
150 {0x01, D|L|W|O|M,   "REZERO UNIT"},
151
152 /* 02  VVVVVV  V   */
153
154 /* 03  MMMMMMMMMMMM  REQUEST SENSE */
155 {0x03, ALL,         "REQUEST SENSE"},
156
157 /* 04  M    O O      FORMAT UNIT */
158 {0x04, D|R|O,       "FORMAT UNIT"},
159 /* 04   O            FORMAT MEDIUM */
160 {0x04, T,           "FORMAT MEDIUM"},
161 /* 04    O           FORMAT */
162 {0x04, L,           "FORMAT"},
163
164 /* 05  VMVVVV  V     READ BLOCK LIMITS */
165 {0x05, T,           "READ BLOCK LIMITS"},
166
167 /* 06  VVVVVV  V   */
168
169 /* 07  OVV O  OV     REASSIGN BLOCKS */
170 {0x07, D|W|O,       "REASSIGN BLOCKS"},
171 /* 07          O     INITIALIZE ELEMENT STATUS */
172 {0x07, M,           "INITIALIZE ELEMENT STATUS"},
173
174 /* 08  OMV OO OV     READ(06) */
175 {0x08, D|T|W|R|O,   "READ(06)"},
176 /* 08     O          RECEIVE */
177 {0x08, P,           "RECEIVE"},
178 /* 08           M    GET MESSAGE(06) */
179 {0x08, C,           "GET MESSAGE(06)"},
180
181 /* 09  VVVVVV  V   */
182
183 /* 0A  OM  O  OV     WRITE(06) */
184 {0x0A, D|T|W|O, "WRITE(06)"},
185 /* 0A     M          SEND(06) */
186 {0x0A, P,           "SEND(06)"},
187 /* 0A           M    SEND MESSAGE(06) */
188 {0x0A, C,           "SEND MESSAGE(06)"},
189 /* 0A    M           PRINT */
190 {0x0A, L,           "PRINT"},
191
192 /* 0B  Z   ZO ZV     SEEK(06) */
193 {0x0B, D|W|R|O,     "SEEK(06)"},
194 /* 0B    O           SLEW AND PRINT */
195 {0x0B, L,           "SLEW AND PRINT"},
196
197 /* 0C  VVVVVV  V   */
198 /* 0D  VVVVVV  V   */
199 /* 0E  VVVVVV  V   */
200 /* 0F  VOVVVV  V     READ REVERSE */
201 {0x0F, T,           "READ REVERSE"},
202
203 /* 10  VM VVV        WRITE FILEMARKS */
204 {0x10, T,           "WRITE FILEMARKS"},
205 /* 10    O O         SYNCHRONIZE BUFFER */
206 {0x10, L|W,         "SYNCHRONIZE BUFFER"},
207
208 /* 11  VMVVVV        SPACE */
209 {0x11, T,           "SPACE"},
210
211 /* 12  MMMMMMMMMMMM  INQUIRY */
212 {0x12, ALL,         "INQUIRY"},
213
214 /* 13  VOVVVV        VERIFY(06) */
215 {0x13, T,           "VERIFY(06)"},
216
217 /* 14  VOOVVV        RECOVER BUFFERED DATA */
218 {0x14, T|L,         "RECOVER BUFFERED DATA"},
219
220 /* 15  OMO OOOOOOOO  MODE SELECT(06) */
221 {0x15, ALL & ~(P),    "MODE SELECT(06)"},
222
223 /* 16  MMMOMMMM   O  RESERVE(06) */
224 {0x16, D|T|L|P|W|R|S|O|E, "RESERVE(06)"},
225 /* 16          M     RESERVE ELEMENT(06) */
226 {0x16, M,           "RESERVE ELEMENT(06)"},
227
228 /* 17  MMMOMMMM   O  RELEASE(06) */
229 {0x17, ALL & ~(M|C|A), "RELEASE(06)"},
230 /* 17          M     RELEASE ELEMENT(06) */
231 {0x17, M,           "RELEASE ELEMENT(06)"},
232
233 /* 18  OOOOOOOO      COPY */
234 {0x18, ALL & ~(M|C|A|E), "COPY"},
235
236 /* 19  VMVVVV        ERASE */
237 {0x19, T,           "ERASE"},
238
239 /* 1A  OMO OOOOOOOO  MODE SENSE(06) */
240 {0x1A, ALL & ~(P),  "MODE SENSE(06)"},
241
242 /* 1B  O   OM O      STOP START UNIT */
243 {0x1B, D|W|R|O,     "STOP START UNIT"},
244 /* 1B   O            LOAD UNLOAD */
245 {0x1B, T,           "LOAD UNLOAD"},
246 /* 1B        O       SCAN */
247 {0x1B, S,           "SCAN"},
248 /* 1B    O           STOP PRINT */
249 {0x1B, L,           "STOP PRINT"},
250
251 /* 1C  OOOOOOOOOO M  RECEIVE DIAGNOSTIC RESULTS */
252 {0x1C, ALL & ~(A),  "RECEIVE DIAGNOSTIC RESULTS"},
253
254 /* 1D  MMMMMMMMMMMM  SEND DIAGNOSTIC */
255 {0x1D, ALL,         "SEND DIAGNOSTIC"},
256
257 /* 1E  OO  OM OO     PREVENT ALLOW MEDIUM REMOVAL */
258 {0x1E, D|T|W|R|O|M, "PREVENT ALLOW MEDIUM REMOVAL"},
259
260 /* 1F */
261 /* 20  V   VV V */
262 /* 21  V   VV V */
263 /* 22  V   VV V */
264 /* 23  V   VV V */
265
266 /* 24  V   VVM       SET WINDOW */
267 {0x24, S,           "SET WINDOW"},
268
269 /* 25  M   M  M      READ CAPACITY */
270 {0x25, D|W|O,       "READ CAPACITY"},
271 /* 25       M        READ CD RECORDED CAPACITY */
272 {0x25, R,           "READ CD RECORDED CAPACITY"},
273 /* 25        O       GET WINDOW */
274 {0x25, S,           "GET WINDOW"},
275
276 /* 26  V   VV */
277 /* 27  V   VV */
278
279 /* 28  M   MMMM      READ(10) */
280 {0x28, D|W|R|S|O,   "READ(10)"},
281 /* 28           O    GET MESSAGE(10) */
282 {0x28, C,           "GET MESSAGE(10)"},
283
284 /* 29  V   VV O      READ GENERATION */
285 {0x29, O,           "READ GENERATION"},
286
287 /* 2A  M   MM M      WRITE(10) */
288 {0x2A, D|W|R|O,     "WRITE(10)"},
289 /* 2A        O       SEND(10) */
290 {0x2A, S,           "SEND(10)"},
291 /* 2A           O    SEND MESSAGE(10) */
292 {0x2A, C,           "SEND MESSAGE(10)"},
293
294 /* 2B  O   OM O      SEEK(10) */
295 {0x2B, D|W|R|O,     "SEEK(10)"},
296 /* 2B   O            LOCATE */
297 {0x2B, T,           "LOCATE"},
298 /* 2B          O     POSITION TO ELEMENT */
299 {0x2B, M,           "POSITION TO ELEMENT"},
300
301 /* 2C  V      O      ERASE(10) */
302 {0x2C, O,           "ERASE(10)"},
303
304 /* 2D  V   O  O      READ UPDATED BLOCK */
305 {0x2D, W|O,         "READ UPDATED BLOCK"},
306
307 /* 2E  O   O  O      WRITE AND VERIFY(10) */
308 {0x2E, D|W|O,       "WRITE AND VERIFY(10)"},
309
310 /* 2F  O   OO O      VERIFY(10) */
311 {0x2F, D|W|R|O,     "VERIFY(10)"},
312
313 /* 30  Z   ZO Z      SEARCH DATA HIGH(10) */
314 {0x30, D|W|R|O,     "SEARCH DATA HIGH(10)"},
315
316 /* 31  Z   ZO Z      SEARCH DATA EQUAL(10) */
317 {0x31, D|W|R|O,     "SEARCH DATA EQUAL(10)"},
318 /* 31        O       OBJECT POSITION */
319 {0x31, S,           "OBJECT POSITION"},
320
321 /* 32  Z   ZO Z      SEARCH DATA LOW(10) */
322 {0x32, D|W|R|O,     "SEARCH DATA LOW(10"},
323
324 /* 33  O   OO O      SET LIMITS(10) */
325 {0x33, D|W|R|O,     "SET LIMITS(10)"},
326
327 /* 34  O   OO O      PRE-FETCH */
328 {0x34, D|W|R|O,     "PRE-FETCH"},
329 /* 34   O            READ POSITION */
330 {0x34, T,           "READ POSITION"},
331 /* 34        O       GET DATA BUFFER STATUS */
332 {0x34, S,           "GET DATA BUFFER STATUS"},
333
334 /* 35  O   OM O      SYNCHRONIZE CACHE */
335 {0x35, D|W|R|O,     "SYNCHRONIZE CACHE"},
336
337 /* 36  O   OO O      LOCK UNLOCK CACHE */
338 {0x36, D|W|R|O,     "LOCK UNLOCK CACHE"},
339
340 /* 37  O      O      READ DEFECT DATA(10) */
341 {0x37, D|O,         "READ DEFECT DATA(10)"},
342
343 /* 38      O  O      MEDIUM SCAN */
344 {0x38, W|O,         "MEDIUM SCAN"},
345
346 /* 39  OOOOOOOO      COMPARE */
347 {0x39, ALL & ~(M|C|A|E), "COMPARE"},
348
349 /* 3A  OOOOOOOO      COPY AND VERIFY */
350 {0x3A, ALL & ~(M|C|A|E), "COPY AND VERIFY"},
351
352 /* 3B  OOOOOOOOOO O  WRITE BUFFER */
353 {0x3B, ALL & ~(A),  "WRITE BUFFER"},
354
355 /* 3C  OOOOOOOOOO    READ BUFFER */
356 {0x3C, ALL & ~(A|E),"READ BUFFER"},
357
358 /* 3D      O  O      UPDATE BLOCK */
359 {0x3D, W|O,         "UPDATE BLOCK"},
360
361 /* 3E  O   OO O      READ LONG */
362 {0x3E, D|W|R|O,     "READ LONG"},
363
364 /* 3F  O   O  O      WRITE LONG */
365 {0x3F, D|W|O,       "WRITE LONG"},
366
367 /* 40  OOOOOOOOOO    CHANGE DEFINITION */
368 {0x40, ALL & ~(A|E),"CHANGE DEFINITION"},
369
370 /* 41  O             WRITE SAME */
371 {0x41, D,           "WRITE SAME"},
372
373 /* 42       M        READ SUB-CHANNEL */
374 {0x42, R,           "READ SUB-CHANNEL"}, 
375
376 /* 43       M        READ TOC/PMA/ATIP {MMC Proposed} */
377 {0x43, R,           "READ TOC/PMA/ATIP {MMC Proposed}"},
378
379 /* 44   M            REPORT DENSITY SUPPORT */
380 {0x44, T,           "REPORT DENSITY SUPPORT"},
381 /* 44       M        READ HEADER */
382 {0x44, R,           "READ HEADER"},
383
384 /* 45       O        PLAY AUDIO(10) */
385 {0x45, R,           "PLAY AUDIO(10)"},
386
387 /* 46 */
388
389 /* 47       O        PLAY AUDIO MSF */
390 {0x47, R,           "PLAY AUDIO MSF"},
391
392 /* 48       O        PLAY AUDIO TRACK INDEX */
393 {0x48, R,           "PLAY AUDIO TRACK INDEX"},
394
395 /* 49       O        PLAY TRACK RELATIVE(10) */
396 {0x49, R,           "PLAY TRACK RELATIVE(10)"},
397
398 /* 4A */
399
400 /* 4B       O        PAUSE/RESUME */
401 {0x4B, R,           "PAUSE/RESUME"},
402
403 /* 4C  OOOOOOOOOOO   LOG SELECT */
404 {0x4C, ALL & ~(E),  "LOG SELECT"},
405
406 /* 4D  OOOOOOOOOOO   LOG SENSE */
407 {0x4D, ALL & ~(E),  "LOG SENSE"},
408
409 /* 4E       O        STOP PLAY/SCAN {MMC Proposed} */
410 {0x4E, R,           "STOP PLAY/SCAN {MMC Proposed}"},
411
412 /* 4F */
413
414 /* 50  O             XDWRITE(10) */
415 {0x50, D,           "XDWRITE(10)"},
416
417 /* 51  O             XPWRITE(10) */
418 {0x51, D,           "XPWRITE(10)"},
419 /* 51       M        READ DISC INFORMATION {MMC Proposed} */
420 {0x51, R,           "READ DISC INFORMATION {MMC Proposed}"},
421
422 /* 52  O             XDREAD(10) */
423 {0x52, D,           "XDREAD(10)"},
424 /* 52       M        READ TRACK INFORMATION {MMC Proposed} */
425 {0x52, R,           "READ TRACK INFORMATION {MMC Proposed}"},
426
427 /* 53       M        RESERVE TRACK {MMC Proposed} */
428 {0x53, R,           "RESERVE TRACK {MMC Proposed}"},
429
430 /* 54       O        SEND OPC INFORMATION {MMC Proposed} */
431 {0x54, R,           "SEND OPC INFORMATION {MMC Proposed}"},
432
433 /* 55  OOO OOOOOOOO  MODE SELECT(10) */
434 {0x55, ALL & ~(P),  "MODE SELECT(10)"},
435
436 /* 56  MMMOMMMM   O  RESERVE(10) */
437 {0x56, ALL & ~(M|C|A), "RESERVE(10)"},
438 /* 56          M     RESERVE ELEMENT(10) */
439 {0x56, M,           "RESERVE ELEMENT(10)"},
440
441 /* 57  MMMOMMMM   O  RELEASE(10) */
442 {0x57, ALL & ~(M|C|A), "RELEASE(10"},
443 /* 57          M     RELEASE ELEMENT(10) */
444 {0x57, M,           "RELEASE ELEMENT(10)"},
445
446 /* 58       O        REPAIR TRACK {MMC Proposed} */
447 {0x58, R,           "REPAIR TRACK {MMC Proposed}"},
448
449 /* 59       O        READ MASTER CUE {MMC Proposed} */
450 {0x59, R,           "READ MASTER CUE {MMC Proposed}"},
451
452 /* 5A  OOO OOOOOOOO  MODE SENSE(10) */
453 {0x5A, ALL & ~(P),  "MODE SENSE(10)"},
454
455 /* 5B       M        CLOSE TRACK/SESSION {MMC Proposed} */
456 {0x5B, R,           "CLOSE TRACK/SESSION {MMC Proposed}"},
457
458 /* 5C       O        READ BUFFER CAPACITY {MMC Proposed} */
459 {0x5C, R,           "READ BUFFER CAPACITY {MMC Proposed}"},
460
461 /* 5D       O        SEND CUE SHEET {MMC Proposed} */
462 {0x5D, R,           "SEND CUE SHEET {MMC Proposed}"},
463
464 /* 5E  OOOOOOOOO  O  PERSISTENT RESERVE IN */
465 {0x5E, ALL & ~(C|A),"PERSISTENT RESERVE IN"},
466
467 /* 5F  OOOOOOOOO  O  PERSISTENT RESERVE OUT */
468 {0x5F, ALL & ~(C|A),"PERSISTENT RESERVE OUT"},
469
470 /* 80  O             XDWRITE EXTENDED(16) */
471 {0x80, D,           "XDWRITE EXTENDED(16)"},
472
473 /* 81  O             REBUILD(16) */
474 {0x81, D,           "REBUILD(16)"},
475
476 /* 82  O             REGENERATE(16) */
477 {0x82, D,           "REGENERATE(16)"},
478
479 /* 83 */
480 /* 84 */
481 /* 85 */
482 /* 86 */
483 /* 87 */
484 /* 88  MM  OO O    O   READ(16) */
485 {0x88, D|T|W|R|O,     "READ(16)"},
486 /* 89 */
487 /* 8A  OM  O  O    O   WRITE(16) */
488 {0x8A, D|T|W|R|O,     "WRITE(16)"},
489 /* 8B */
490 /* 8C */
491 /* 8D */
492 /* 8E */
493 /* 8F */
494 /* 90 */
495 /* 91 */
496 /* 92 */
497 /* 93 */
498 /* 94 */
499 /* 95 */
500 /* 96 */
501 /* 97 */
502 /* 98 */
503 /* 99 */
504 /* 9A */
505 /* 9B */
506 /* 9C */
507 /* 9D */
508 /* XXX KDM ALL for these?  op-num.txt defines them for none.. */
509 /* 9E                  SERVICE ACTION IN(16) */
510 {0x9E, ALL,           "SERVICE ACTION IN(16)"},
511 /* 9F                  SERVICE ACTION OUT(16) */
512 {0x9F, ALL,           "SERVICE ACTION OUT(16)"},
513
514 /* A0  OOOOOOOOOOO   REPORT LUNS */
515 {0xA0, ALL & ~(E),  "REPORT LUNS"},
516
517 /* A1       O        BLANK {MMC Proposed} */
518 {0xA1, R,           "BLANK {MMC Proposed}"},
519
520 /* A2       O        WRITE CD MSF {MMC Proposed} */
521 {0xA2, R,           "WRITE CD MSF {MMC Proposed}"},
522
523 /* A3            M   MAINTENANCE (IN) */
524 {0xA3, A,           "MAINTENANCE (IN)"},
525
526 /* A4            O   MAINTENANCE (OUT) */
527 {0xA4, A,           "MAINTENANCE (OUT)"},
528
529 /* A5   O      M     MOVE MEDIUM */
530 {0xA5, T|M,         "MOVE MEDIUM"},
531 /* A5       O        PLAY AUDIO(12) */
532 {0xA5, R,           "PLAY AUDIO(12)"},
533
534 /* A6          O     EXCHANGE MEDIUM */
535 {0xA6, M,           "EXCHANGE MEDIUM"},
536 /* A6       O        LOAD/UNLOAD CD {MMC Proposed} */
537 {0xA6, R,           "LOAD/UNLOAD CD {MMC Proposed}"},
538
539 /* A7  OO  OO OO     MOVE MEDIUM ATTACHED */
540 {0xA7, D|T|W|R|O|M, "MOVE MEDIUM ATTACHED"},
541
542 /* A8  O   OM O      READ(12) */
543 {0xA8,D|W|R|O,      "READ(12)"},
544 /* A8           O    GET MESSAGE(12) */
545 {0xA8, C,           "GET MESSAGE(12)"},
546
547 /* A9       O        PLAY TRACK RELATIVE(12) */
548 {0xA9, R,           "PLAY TRACK RELATIVE(12)"},
549
550 /* AA  O   O  O      WRITE(12) */
551 {0xAA,D|W|O,        "WRITE(12)"},
552 /* AA       O        WRITE CD(12) {MMC Proposed} */
553 {0xAA, R,           "WRITE CD(12) {MMC Proposed}"},
554 /* AA           O    SEND MESSAGE(12) */
555 {0xAA, C,           "SEND MESSAGE(12)"},
556
557 /* AB */
558
559 /* AC         O      ERASE(12) */
560 {0xAC, O,           "ERASE(12)"},
561
562 /* AD */
563
564 /* AE      O  O      WRITE AND VERIFY(12) */
565 {0xAE, W|O,         "WRITE AND VERIFY(12)"},
566
567 /* AF      OO O      VERIFY(12) */
568 {0xAF, W|R|O,       "VERIFY(12)"},
569
570 /* B0      ZO Z      SEARCH DATA HIGH(12) */
571 {0xB0, W|R|O,       "SEARCH DATA HIGH(12)"},
572
573 /* B1      ZO Z      SEARCH DATA EQUAL(12) */
574 {0xB1, W|R|O,       "SEARCH DATA EQUAL(12)"},
575
576 /* B2      ZO Z      SEARCH DATA LOW(12) */
577 {0xB2, W|R|O,       "SEARCH DATA LOW(12)"},
578
579 /* B3      OO O      SET LIMITS(12) */
580 {0xB3, W|R|O,       "SET LIMITS(12)"},
581
582 /* B4  OO  OO OO     READ ELEMENT STATUS ATTACHED */
583 {0xB4, D|T|W|R|O|M, "READ ELEMENT STATUS ATTACHED"},
584
585 /* B5          O     REQUEST VOLUME ELEMENT ADDRESS */
586 {0xB5, M,           "REQUEST VOLUME ELEMENT ADDRESS"},
587
588 /* B6          O     SEND VOLUME TAG */
589 {0xB6, M,           "SEND VOLUME TAG"},
590
591 /* B7         O      READ DEFECT DATA(12) */
592 {0xB7, O,           "READ DEFECT DATA(12)"},
593
594 /* B8   O      M     READ ELEMENT STATUS */
595 {0xB8, T|M,         "READ ELEMENT STATUS"},
596 /* B8       O        SET CD SPEED {MMC Proposed} */
597 {0xB8, R,           "SET CD SPEED {MMC Proposed}"},
598
599 /* B9       M        READ CD MSF {MMC Proposed} */
600 {0xB9, R,           "READ CD MSF {MMC Proposed}"},
601
602 /* BA       O        SCAN {MMC Proposed} */
603 {0xBA, R,           "SCAN {MMC Proposed}"},
604 /* BA            M   REDUNDANCY GROUP (IN) */
605 {0xBA, A,           "REDUNDANCY GROUP (IN)"},
606
607 /* BB       O        SET CD-ROM SPEED {proposed} */
608 {0xBB, R,           "SET CD-ROM SPEED {proposed}"},
609 /* BB            O   REDUNDANCY GROUP (OUT) */
610 {0xBB, A,           "REDUNDANCY GROUP (OUT)"},
611
612 /* BC       O        PLAY CD {MMC Proposed} */
613 {0xBC, R,           "PLAY CD {MMC Proposed}"},
614 /* BC            M   SPARE (IN) */
615 {0xBC, A,           "SPARE (IN)"},
616
617 /* BD       M        MECHANISM STATUS {MMC Proposed} */
618 {0xBD, R,           "MECHANISM STATUS {MMC Proposed}"},
619 /* BD            O   SPARE (OUT) */
620 {0xBD, A,           "SPARE (OUT)"},
621
622 /* BE       O        READ CD {MMC Proposed} */
623 {0xBE, R,           "READ CD {MMC Proposed}"},
624 /* BE            M   VOLUME SET (IN) */
625 {0xBE, A,           "VOLUME SET (IN)"},
626
627 /* BF            O   VOLUME SET (OUT) */
628 {0xBF, A,           "VOLUME SET (OUT)"}
629 };
630
631 const char *
632 scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
633 {
634         caddr_t match;
635         int i, j;
636         u_int16_t opmask;
637         u_int16_t pd_type;
638         int       num_ops[2];
639         struct op_table_entry *table[2];
640         int num_tables;
641
642         pd_type = SID_TYPE(inq_data);
643
644         match = cam_quirkmatch((caddr_t)inq_data,
645                                (caddr_t)scsi_op_quirk_table,
646                                sizeof(scsi_op_quirk_table)/
647                                sizeof(*scsi_op_quirk_table),
648                                sizeof(*scsi_op_quirk_table),
649                                scsi_inquiry_match);
650
651         if (match != NULL) {
652                 table[0] = ((struct scsi_op_quirk_entry *)match)->op_table;
653                 num_ops[0] = ((struct scsi_op_quirk_entry *)match)->num_ops;
654                 table[1] = scsi_op_codes;
655                 num_ops[1] = sizeof(scsi_op_codes)/sizeof(scsi_op_codes[0]);
656                 num_tables = 2;
657         } else {
658                 /*      
659                  * If this is true, we have a vendor specific opcode that
660                  * wasn't covered in the quirk table.
661                  */
662                 if ((opcode > 0xBF) || ((opcode > 0x5F) && (opcode < 0x80)))
663                         return("Vendor Specific Command");
664
665                 table[0] = scsi_op_codes;
666                 num_ops[0] = sizeof(scsi_op_codes)/sizeof(scsi_op_codes[0]);
667                 num_tables = 1;
668         }
669
670         /* RBC is 'Simplified' Direct Access Device */
671         if (pd_type == T_RBC)
672                 pd_type = T_DIRECT;
673
674         opmask = 1 << pd_type;
675
676         for (j = 0; j < num_tables; j++) {
677                 for (i = 0;i < num_ops[j] && table[j][i].opcode <= opcode; i++){
678                         if ((table[j][i].opcode == opcode) 
679                          && ((table[j][i].opmask & opmask) != 0))
680                                 return(table[j][i].desc);
681                 }
682         }
683         
684         /*
685          * If we can't find a match for the command in the table, we just
686          * assume it's a vendor specifc command.
687          */
688         return("Vendor Specific Command");
689
690 }
691
692 #else /* SCSI_NO_OP_STRINGS */
693
694 const char *
695 scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
696 {
697         return("");
698 }
699
700 #endif
701
702
703 #include <sys/param.h>
704
705 #if !defined(SCSI_NO_SENSE_STRINGS)
706 #define SST(asc, ascq, action, desc) \
707         asc, ascq, action, desc
708 #else 
709 const char empty_string[] = "";
710
711 #define SST(asc, ascq, action, desc) \
712         asc, ascq, action, empty_string
713 #endif 
714
715 const struct sense_key_table_entry sense_key_table[] =
716 {
717         { SSD_KEY_NO_SENSE, SS_NOP, "NO SENSE" },
718         { SSD_KEY_RECOVERED_ERROR, SS_NOP|SSQ_PRINT_SENSE, "RECOVERED ERROR" },
719         {
720           SSD_KEY_NOT_READY, SS_TUR|SSQ_MANY|SSQ_DECREMENT_COUNT|EBUSY,
721           "NOT READY"
722         },
723         { SSD_KEY_MEDIUM_ERROR, SS_RDEF, "MEDIUM ERROR" },
724         { SSD_KEY_HARDWARE_ERROR, SS_RDEF, "HARDWARE FAILURE" },
725         { SSD_KEY_ILLEGAL_REQUEST, SS_FATAL|EINVAL, "ILLEGAL REQUEST" },
726         { SSD_KEY_UNIT_ATTENTION, SS_FATAL|ENXIO, "UNIT ATTENTION" },
727         { SSD_KEY_DATA_PROTECT, SS_FATAL|EACCES, "DATA PROTECT" },
728         { SSD_KEY_BLANK_CHECK, SS_FATAL|ENOSPC, "BLANK CHECK" },
729         { SSD_KEY_Vendor_Specific, SS_FATAL|EIO, "Vendor Specific" },
730         { SSD_KEY_COPY_ABORTED, SS_FATAL|EIO, "COPY ABORTED" },
731         { SSD_KEY_ABORTED_COMMAND, SS_RDEF, "ABORTED COMMAND" },
732         { SSD_KEY_EQUAL, SS_NOP, "EQUAL" },
733         { SSD_KEY_VOLUME_OVERFLOW, SS_FATAL|EIO, "VOLUME OVERFLOW" },
734         { SSD_KEY_MISCOMPARE, SS_NOP, "MISCOMPARE" },
735         { SSD_KEY_RESERVED, SS_FATAL|EIO, "RESERVED" }
736 };
737
738 const int sense_key_table_size =
739     sizeof(sense_key_table)/sizeof(sense_key_table[0]);
740
741 static struct asc_table_entry quantum_fireball_entries[] = {
742         {SST(0x04, 0x0b, SS_START|SSQ_DECREMENT_COUNT|ENXIO, 
743              "Logical unit not ready, initializing cmd. required")}
744 };
745
746 static struct asc_table_entry sony_mo_entries[] = {
747         {SST(0x04, 0x00, SS_START|SSQ_DECREMENT_COUNT|ENXIO,
748              "Logical unit not ready, cause not reportable")}
749 };
750
751 static struct scsi_sense_quirk_entry sense_quirk_table[] = {
752         {
753                 /*
754                  * The Quantum Fireball ST and SE like to return 0x04 0x0b when
755                  * they really should return 0x04 0x02.  0x04,0x0b isn't
756                  * defined in any SCSI spec, and it isn't mentioned in the
757                  * hardware manual for these drives.
758                  */
759                 {T_DIRECT, SIP_MEDIA_FIXED, "QUANTUM", "FIREBALL S*", "*"},
760                 /*num_sense_keys*/0,
761                 sizeof(quantum_fireball_entries)/sizeof(struct asc_table_entry),
762                 /*sense key entries*/NULL,
763                 quantum_fireball_entries
764         },
765         {
766                 /*
767                  * This Sony MO drive likes to return 0x04, 0x00 when it
768                  * isn't spun up.
769                  */
770                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SONY", "SMO-*", "*"},
771                 /*num_sense_keys*/0,
772                 sizeof(sony_mo_entries)/sizeof(struct asc_table_entry),
773                 /*sense key entries*/NULL,
774                 sony_mo_entries
775         }
776 };
777
778 const int sense_quirk_table_size =
779     sizeof(sense_quirk_table)/sizeof(sense_quirk_table[0]);
780
781 static struct asc_table_entry asc_table[] = {
782 /*
783  * From File: ASC-NUM.TXT
784  * SCSI ASC/ASCQ Assignments
785  * Numeric Sorted Listing
786  * as of  5/12/97
787  *
788  * D - DIRECT ACCESS DEVICE (SBC)                     device column key
789  * .T - SEQUENTIAL ACCESS DEVICE (SSC)               -------------------
790  * . L - PRINTER DEVICE (SSC)                           blank = reserved
791  * .  P - PROCESSOR DEVICE (SPC)                     not blank = allowed
792  * .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC)
793  * .  . R - CD DEVICE (MMC)
794  * .  .  S - SCANNER DEVICE (SGC)
795  * .  .  .O - OPTICAL MEMORY DEVICE (SBC)
796  * .  .  . M - MEDIA CHANGER DEVICE (SMC)
797  * .  .  .  C - COMMUNICATION DEVICE (SSC)
798  * .  .  .  .A - STORAGE ARRAY DEVICE (SCC)
799  * .  .  .  . E - ENCLOSURE SERVICES DEVICE (SES)
800  * DTLPWRSOMCAE        ASC   ASCQ  Action  Description
801  * ------------        ----  ----  ------  -----------------------------------*/
802 /* DTLPWRSOMCAE */{SST(0x00, 0x00, SS_NOP,
803                         "No additional sense information") },
804 /*  T    S      */{SST(0x00, 0x01, SS_RDEF,
805                         "Filemark detected") },
806 /*  T    S      */{SST(0x00, 0x02, SS_RDEF,
807                         "End-of-partition/medium detected") },
808 /*  T           */{SST(0x00, 0x03, SS_RDEF,
809                         "Setmark detected") },
810 /*  T    S      */{SST(0x00, 0x04, SS_RDEF,
811                         "Beginning-of-partition/medium detected") },
812 /*  T    S      */{SST(0x00, 0x05, SS_RDEF,
813                         "End-of-data detected") },
814 /* DTLPWRSOMCAE */{SST(0x00, 0x06, SS_RDEF,
815                         "I/O process terminated") },
816 /*      R       */{SST(0x00, 0x11, SS_FATAL|EBUSY,
817                         "Audio play operation in progress") },
818 /*      R       */{SST(0x00, 0x12, SS_NOP,
819                         "Audio play operation paused") },
820 /*      R       */{SST(0x00, 0x13, SS_NOP,
821                         "Audio play operation successfully completed") },
822 /*      R       */{SST(0x00, 0x14, SS_RDEF,
823                         "Audio play operation stopped due to error") },
824 /*      R       */{SST(0x00, 0x15, SS_NOP,
825                         "No current audio status to return") },
826 /* DTLPWRSOMCAE */{SST(0x00, 0x16, SS_FATAL|EBUSY,
827                         "Operation in progress") },
828 /* DTL WRSOM AE */{SST(0x00, 0x17, SS_RDEF,
829                         "Cleaning requested") },
830 /* D   W  O     */{SST(0x01, 0x00, SS_RDEF,
831                         "No index/sector signal") },
832 /* D   WR OM    */{SST(0x02, 0x00, SS_RDEF,
833                         "No seek complete") },
834 /* DTL W SO     */{SST(0x03, 0x00, SS_RDEF,
835                         "Peripheral device write fault") },
836 /*  T           */{SST(0x03, 0x01, SS_RDEF,
837                         "No write current") },
838 /*  T           */{SST(0x03, 0x02, SS_RDEF,
839                         "Excessive write errors") },
840 /* DTLPWRSOMCAE */{SST(0x04, 0x00, SS_TUR|SSQ_MANY|SSQ_DECREMENT_COUNT|EIO,
841                         "Logical unit not ready, cause not reportable") },
842 /* DTLPWRSOMCAE */{SST(0x04, 0x01, SS_TUR|SSQ_MANY|SSQ_DECREMENT_COUNT|EBUSY,
843                         "Logical unit is in process of becoming ready") },
844 /* DTLPWRSOMCAE */{SST(0x04, 0x02, SS_START|SSQ_DECREMENT_COUNT|ENXIO,
845                         "Logical unit not ready, initializing cmd. required") },
846 /* DTLPWRSOMCAE */{SST(0x04, 0x03, SS_FATAL|ENXIO,
847                         "Logical unit not ready, manual intervention required")},
848 /* DTL    O     */{SST(0x04, 0x04, SS_FATAL|EBUSY,
849                         "Logical unit not ready, format in progress") },
850 /* DT  W  OMCA  */{SST(0x04, 0x05, SS_FATAL|EBUSY,
851                         "Logical unit not ready, rebuild in progress") },
852 /* DT  W  OMCA  */{SST(0x04, 0x06, SS_FATAL|EBUSY,
853                         "Logical unit not ready, recalculation in progress") },
854 /* DTLPWRSOMCAE */{SST(0x04, 0x07, SS_FATAL|EBUSY,
855                         "Logical unit not ready, operation in progress") },
856 /*      R       */{SST(0x04, 0x08, SS_FATAL|EBUSY,
857                         "Logical unit not ready, long write in progress") },
858 /* DTL WRSOMCAE */{SST(0x05, 0x00, SS_RDEF,
859                         "Logical unit does not respond to selection") },
860 /* D   WR OM    */{SST(0x06, 0x00, SS_RDEF,
861                         "No reference position found") },
862 /* DTL WRSOM    */{SST(0x07, 0x00, SS_RDEF,
863                         "Multiple peripheral devices selected") },
864 /* DTL WRSOMCAE */{SST(0x08, 0x00, SS_RDEF,
865                         "Logical unit communication failure") },
866 /* DTL WRSOMCAE */{SST(0x08, 0x01, SS_RDEF,
867                         "Logical unit communication time-out") },
868 /* DTL WRSOMCAE */{SST(0x08, 0x02, SS_RDEF,
869                         "Logical unit communication parity error") },
870 /* DT   R OM    */{SST(0x08, 0x03, SS_RDEF,
871                         "Logical unit communication crc error (ultra-dma/32)")},
872 /* DT  WR O     */{SST(0x09, 0x00, SS_RDEF,
873                         "Track following error") },
874 /*     WR O     */{SST(0x09, 0x01, SS_RDEF,
875                         "Tracking servo failure") },
876 /*     WR O     */{SST(0x09, 0x02, SS_RDEF,
877                         "Focus servo failure") },
878 /*     WR O     */{SST(0x09, 0x03, SS_RDEF,
879                         "Spindle servo failure") },
880 /* DT  WR O     */{SST(0x09, 0x04, SS_RDEF,
881                         "Head select fault") },
882 /* DTLPWRSOMCAE */{SST(0x0A, 0x00, SS_FATAL|ENOSPC,
883                         "Error log overflow") },
884 /* DTLPWRSOMCAE */{SST(0x0B, 0x00, SS_RDEF,
885                         "Warning") },
886 /* DTLPWRSOMCAE */{SST(0x0B, 0x01, SS_RDEF,
887                         "Specified temperature exceeded") },
888 /* DTLPWRSOMCAE */{SST(0x0B, 0x02, SS_RDEF,
889                         "Enclosure degraded") },
890 /*  T   RS      */{SST(0x0C, 0x00, SS_RDEF,
891                         "Write error") },
892 /* D   W  O     */{SST(0x0C, 0x01, SS_NOP|SSQ_PRINT_SENSE,
893                         "Write error - recovered with auto reallocation") },
894 /* D   W  O     */{SST(0x0C, 0x02, SS_RDEF,
895                         "Write error - auto reallocation failed") },
896 /* D   W  O     */{SST(0x0C, 0x03, SS_RDEF,
897                         "Write error - recommend reassignment") },
898 /* DT  W  O     */{SST(0x0C, 0x04, SS_RDEF,
899                         "Compression check miscompare error") },
900 /* DT  W  O     */{SST(0x0C, 0x05, SS_RDEF,
901                         "Data expansion occurred during compression") },
902 /* DT  W  O     */{SST(0x0C, 0x06, SS_RDEF,
903                         "Block not compressible") },
904 /*      R       */{SST(0x0C, 0x07, SS_RDEF,
905                         "Write error - recovery needed") },
906 /*      R       */{SST(0x0C, 0x08, SS_RDEF,
907                         "Write error - recovery failed") },
908 /*      R       */{SST(0x0C, 0x09, SS_RDEF,
909                         "Write error - loss of streaming") },
910 /*      R       */{SST(0x0C, 0x0A, SS_RDEF,
911                         "Write error - padding blocks added") },
912 /* D   W  O     */{SST(0x10, 0x00, SS_RDEF,
913                         "ID CRC or ECC error") },
914 /* DT  WRSO     */{SST(0x11, 0x00, SS_RDEF,
915                         "Unrecovered read error") },
916 /* DT  W SO     */{SST(0x11, 0x01, SS_RDEF,
917                         "Read retries exhausted") },
918 /* DT  W SO     */{SST(0x11, 0x02, SS_RDEF,
919                         "Error too long to correct") },
920 /* DT  W SO     */{SST(0x11, 0x03, SS_RDEF,
921                         "Multiple read errors") },
922 /* D   W  O     */{SST(0x11, 0x04, SS_RDEF,
923                         "Unrecovered read error - auto reallocate failed") },
924 /*     WR O     */{SST(0x11, 0x05, SS_RDEF,
925                         "L-EC uncorrectable error") },
926 /*     WR O     */{SST(0x11, 0x06, SS_RDEF,
927                         "CIRC unrecovered error") },
928 /*     W  O     */{SST(0x11, 0x07, SS_RDEF,
929                         "Data re-synchronization error") },
930 /*  T           */{SST(0x11, 0x08, SS_RDEF,
931                         "Incomplete block read") },
932 /*  T           */{SST(0x11, 0x09, SS_RDEF,
933                         "No gap found") },
934 /* DT     O     */{SST(0x11, 0x0A, SS_RDEF,
935                         "Miscorrected error") },
936 /* D   W  O     */{SST(0x11, 0x0B, SS_RDEF,
937                         "Unrecovered read error - recommend reassignment") },
938 /* D   W  O     */{SST(0x11, 0x0C, SS_RDEF,
939                         "Unrecovered read error - recommend rewrite the data")},
940 /* DT  WR O     */{SST(0x11, 0x0D, SS_RDEF,
941                         "De-compression CRC error") },
942 /* DT  WR O     */{SST(0x11, 0x0E, SS_RDEF,
943                         "Cannot decompress using declared algorithm") },
944 /*      R       */{SST(0x11, 0x0F, SS_RDEF,
945                         "Error reading UPC/EAN number") },
946 /*      R       */{SST(0x11, 0x10, SS_RDEF,
947                         "Error reading ISRC number") },
948 /*      R       */{SST(0x11, 0x11, SS_RDEF,
949                         "Read error - loss of streaming") },
950 /* D   W  O     */{SST(0x12, 0x00, SS_RDEF,
951                         "Address mark not found for id field") },
952 /* D   W  O     */{SST(0x13, 0x00, SS_RDEF,
953                         "Address mark not found for data field") },
954 /* DTL WRSO     */{SST(0x14, 0x00, SS_RDEF,
955                         "Recorded entity not found") },
956 /* DT  WR O     */{SST(0x14, 0x01, SS_RDEF,
957                         "Record not found") },
958 /*  T           */{SST(0x14, 0x02, SS_RDEF,
959                         "Filemark or setmark not found") },
960 /*  T           */{SST(0x14, 0x03, SS_RDEF,
961                         "End-of-data not found") },
962 /*  T           */{SST(0x14, 0x04, SS_RDEF,
963                         "Block sequence error") },
964 /* DT  W  O     */{SST(0x14, 0x05, SS_RDEF,
965                         "Record not found - recommend reassignment") },
966 /* DT  W  O     */{SST(0x14, 0x06, SS_RDEF,
967                         "Record not found - data auto-reallocated") },
968 /* DTL WRSOM    */{SST(0x15, 0x00, SS_RDEF,
969                         "Random positioning error") },
970 /* DTL WRSOM    */{SST(0x15, 0x01, SS_RDEF,
971                         "Mechanical positioning error") },
972 /* DT  WR O     */{SST(0x15, 0x02, SS_RDEF,
973                         "Positioning error detected by read of medium") },
974 /* D   W  O     */{SST(0x16, 0x00, SS_RDEF,
975                         "Data synchronization mark error") },
976 /* D   W  O     */{SST(0x16, 0x01, SS_RDEF,
977                         "Data sync error - data rewritten") },
978 /* D   W  O     */{SST(0x16, 0x02, SS_RDEF,
979                         "Data sync error - recommend rewrite") },
980 /* D   W  O     */{SST(0x16, 0x03, SS_NOP|SSQ_PRINT_SENSE,
981                         "Data sync error - data auto-reallocated") },
982 /* D   W  O     */{SST(0x16, 0x04, SS_RDEF,
983                         "Data sync error - recommend reassignment") },
984 /* DT  WRSO     */{SST(0x17, 0x00, SS_NOP|SSQ_PRINT_SENSE,
985                         "Recovered data with no error correction applied") },
986 /* DT  WRSO     */{SST(0x17, 0x01, SS_NOP|SSQ_PRINT_SENSE,
987                         "Recovered data with retries") },
988 /* DT  WR O     */{SST(0x17, 0x02, SS_NOP|SSQ_PRINT_SENSE,
989                         "Recovered data with positive head offset") },
990 /* DT  WR O     */{SST(0x17, 0x03, SS_NOP|SSQ_PRINT_SENSE,
991                         "Recovered data with negative head offset") },
992 /*     WR O     */{SST(0x17, 0x04, SS_NOP|SSQ_PRINT_SENSE,
993                         "Recovered data with retries and/or CIRC applied") },
994 /* D   WR O     */{SST(0x17, 0x05, SS_NOP|SSQ_PRINT_SENSE,
995                         "Recovered data using previous sector id") },
996 /* D   W  O     */{SST(0x17, 0x06, SS_NOP|SSQ_PRINT_SENSE,
997                         "Recovered data without ECC - data auto-reallocated") },
998 /* D   W  O     */{SST(0x17, 0x07, SS_NOP|SSQ_PRINT_SENSE,
999                         "Recovered data without ECC - recommend reassignment")},
1000 /* D   W  O     */{SST(0x17, 0x08, SS_NOP|SSQ_PRINT_SENSE,
1001                         "Recovered data without ECC - recommend rewrite") },
1002 /* D   W  O     */{SST(0x17, 0x09, SS_NOP|SSQ_PRINT_SENSE,
1003                         "Recovered data without ECC - data rewritten") },
1004 /* D   W  O     */{SST(0x18, 0x00, SS_NOP|SSQ_PRINT_SENSE,
1005                         "Recovered data with error correction applied") },
1006 /* D   WR O     */{SST(0x18, 0x01, SS_NOP|SSQ_PRINT_SENSE,
1007                         "Recovered data with error corr. & retries applied") },
1008 /* D   WR O     */{SST(0x18, 0x02, SS_NOP|SSQ_PRINT_SENSE,
1009                         "Recovered data - data auto-reallocated") },
1010 /*      R       */{SST(0x18, 0x03, SS_NOP|SSQ_PRINT_SENSE,
1011                         "Recovered data with CIRC") },
1012 /*      R       */{SST(0x18, 0x04, SS_NOP|SSQ_PRINT_SENSE,
1013                         "Recovered data with L-EC") },
1014 /* D   WR O     */{SST(0x18, 0x05, SS_NOP|SSQ_PRINT_SENSE,
1015                         "Recovered data - recommend reassignment") },
1016 /* D   WR O     */{SST(0x18, 0x06, SS_NOP|SSQ_PRINT_SENSE,
1017                         "Recovered data - recommend rewrite") },
1018 /* D   W  O     */{SST(0x18, 0x07, SS_NOP|SSQ_PRINT_SENSE,
1019                         "Recovered data with ECC - data rewritten") },
1020 /* D      O     */{SST(0x19, 0x00, SS_RDEF,
1021                         "Defect list error") },
1022 /* D      O     */{SST(0x19, 0x01, SS_RDEF,
1023                         "Defect list not available") },
1024 /* D      O     */{SST(0x19, 0x02, SS_RDEF,
1025                         "Defect list error in primary list") },
1026 /* D      O     */{SST(0x19, 0x03, SS_RDEF,
1027                         "Defect list error in grown list") },
1028 /* DTLPWRSOMCAE */{SST(0x1A, 0x00, SS_RDEF,
1029                         "Parameter list length error") },
1030 /* DTLPWRSOMCAE */{SST(0x1B, 0x00, SS_RDEF,
1031                         "Synchronous data transfer error") },
1032 /* D      O     */{SST(0x1C, 0x00, SS_RDEF,
1033                         "Defect list not found") },
1034 /* D      O     */{SST(0x1C, 0x01, SS_RDEF,
1035                         "Primary defect list not found") },
1036 /* D      O     */{SST(0x1C, 0x02, SS_RDEF,
1037                         "Grown defect list not found") },
1038 /* D   W  O     */{SST(0x1D, 0x00, SS_FATAL,
1039                         "Miscompare during verify operation" )},
1040 /* D   W  O     */{SST(0x1E, 0x00, SS_NOP|SSQ_PRINT_SENSE,
1041                         "Recovered id with ecc correction") },
1042 /* D      O     */{SST(0x1F, 0x00, SS_RDEF,
1043                         "Partial defect list transfer") },
1044 /* DTLPWRSOMCAE */{SST(0x20, 0x00, SS_FATAL|EINVAL,
1045                         "Invalid command operation code") },
1046 /* DT  WR OM    */{SST(0x21, 0x00, SS_FATAL|EINVAL,
1047                         "Logical block address out of range" )},
1048 /* DT  WR OM    */{SST(0x21, 0x01, SS_FATAL|EINVAL,
1049                         "Invalid element address") },
1050 /* D            */{SST(0x22, 0x00, SS_FATAL|EINVAL,
1051                         "Illegal function") }, /* Deprecated. Use 20 00, 24 00, or 26 00 instead */
1052 /* DTLPWRSOMCAE */{SST(0x24, 0x00, SS_FATAL|EINVAL,
1053                         "Invalid field in CDB") },
1054 /* DTLPWRSOMCAE */{SST(0x25, 0x00, SS_FATAL|ENXIO,
1055                         "Logical unit not supported") },
1056 /* DTLPWRSOMCAE */{SST(0x26, 0x00, SS_FATAL|EINVAL,
1057                         "Invalid field in parameter list") },
1058 /* DTLPWRSOMCAE */{SST(0x26, 0x01, SS_FATAL|EINVAL,
1059                         "Parameter not supported") },
1060 /* DTLPWRSOMCAE */{SST(0x26, 0x02, SS_FATAL|EINVAL,
1061                         "Parameter value invalid") },
1062 /* DTLPWRSOMCAE */{SST(0x26, 0x03, SS_FATAL|EINVAL,
1063                         "Threshold parameters not supported") },
1064 /* DTLPWRSOMCAE */{SST(0x26, 0x04, SS_FATAL|EINVAL,
1065                         "Invalid release of active persistent reservation") },
1066 /* DT  W  O     */{SST(0x27, 0x00, SS_FATAL|EACCES,
1067                         "Write protected") },
1068 /* DT  W  O     */{SST(0x27, 0x01, SS_FATAL|EACCES,
1069                         "Hardware write protected") },
1070 /* DT  W  O     */{SST(0x27, 0x02, SS_FATAL|EACCES,
1071                         "Logical unit software write protected") },
1072 /*  T           */{SST(0x27, 0x03, SS_FATAL|EACCES,
1073                         "Associated write protect") },
1074 /*  T           */{SST(0x27, 0x04, SS_FATAL|EACCES,
1075                         "Persistent write protect") },
1076 /*  T           */{SST(0x27, 0x05, SS_FATAL|EACCES,
1077                         "Permanent write protect") },
1078 /* DTLPWRSOMCAE */{SST(0x28, 0x00, SS_FATAL|ENXIO,
1079                         "Not ready to ready change, medium may have changed") },
1080 /* DTLPWRSOMCAE */{SST(0x28, 0x01, SS_FATAL|ENXIO,
1081                         "Import or export element accessed") },
1082 /*
1083  * XXX JGibbs - All of these should use the same errno, but I don't think
1084  * ENXIO is the correct choice.  Should we borrow from the networking
1085  * errnos?  ECONNRESET anyone?
1086  */
1087 /* DTLPWRSOMCAE */{SST(0x29, 0x00, SS_FATAL|ENXIO,
1088                         "Power on, reset, or bus device reset occurred") },
1089 /* DTLPWRSOMCAE */{SST(0x29, 0x01, SS_RDEF,
1090                         "Power on occurred") },
1091 /* DTLPWRSOMCAE */{SST(0x29, 0x02, SS_RDEF,
1092                         "Scsi bus reset occurred") },
1093 /* DTLPWRSOMCAE */{SST(0x29, 0x03, SS_RDEF,
1094                         "Bus device reset function occurred") },
1095 /* DTLPWRSOMCAE */{SST(0x29, 0x04, SS_RDEF,
1096                         "Device internal reset") },
1097 /* DTLPWRSOMCAE */{SST(0x29, 0x05, SS_RDEF,
1098                         "Transceiver mode changed to single-ended") },
1099 /* DTLPWRSOMCAE */{SST(0x29, 0x06, SS_RDEF,
1100                         "Transceiver mode changed to LVD") },
1101 /* DTL WRSOMCAE */{SST(0x2A, 0x00, SS_RDEF,
1102                         "Parameters changed") },
1103 /* DTL WRSOMCAE */{SST(0x2A, 0x01, SS_RDEF,
1104                         "Mode parameters changed") },
1105 /* DTL WRSOMCAE */{SST(0x2A, 0x02, SS_RDEF,
1106                         "Log parameters changed") },
1107 /* DTLPWRSOMCAE */{SST(0x2A, 0x03, SS_RDEF,
1108                         "Reservations preempted") },
1109 /* DTLPWRSO C   */{SST(0x2B, 0x00, SS_RDEF,
1110                         "Copy cannot execute since host cannot disconnect") },
1111 /* DTLPWRSOMCAE */{SST(0x2C, 0x00, SS_RDEF,
1112                         "Command sequence error") },
1113 /*       S      */{SST(0x2C, 0x01, SS_RDEF,
1114                         "Too many windows specified") },
1115 /*       S      */{SST(0x2C, 0x02, SS_RDEF,
1116                         "Invalid combination of windows specified") },
1117 /*      R       */{SST(0x2C, 0x03, SS_RDEF,
1118                         "Current program area is not empty") },
1119 /*      R       */{SST(0x2C, 0x04, SS_RDEF,
1120                         "Current program area is empty") },
1121 /*  T           */{SST(0x2D, 0x00, SS_RDEF,
1122                         "Overwrite error on update in place") },
1123 /* DTLPWRSOMCAE */{SST(0x2F, 0x00, SS_RDEF,
1124                         "Commands cleared by another initiator") },
1125 /* DT  WR OM    */{SST(0x30, 0x00, SS_RDEF,
1126                         "Incompatible medium installed") },
1127 /* DT  WR O     */{SST(0x30, 0x01, SS_RDEF,
1128                         "Cannot read medium - unknown format") },
1129 /* DT  WR O     */{SST(0x30, 0x02, SS_RDEF,
1130                         "Cannot read medium - incompatible format") },
1131 /* DT           */{SST(0x30, 0x03, SS_RDEF,
1132                         "Cleaning cartridge installed") },
1133 /* DT  WR O     */{SST(0x30, 0x04, SS_RDEF,
1134                         "Cannot write medium - unknown format") },
1135 /* DT  WR O     */{SST(0x30, 0x05, SS_RDEF,
1136                         "Cannot write medium - incompatible format") },
1137 /* DT  W  O     */{SST(0x30, 0x06, SS_RDEF,
1138                         "Cannot format medium - incompatible medium") },
1139 /* DTL WRSOM AE */{SST(0x30, 0x07, SS_RDEF,
1140                         "Cleaning failure") },
1141 /*      R       */{SST(0x30, 0x08, SS_RDEF,
1142                         "Cannot write - application code mismatch") },
1143 /*      R       */{SST(0x30, 0x09, SS_RDEF,
1144                         "Current session not fixated for append") },
1145 /* DT  WR O     */{SST(0x31, 0x00, SS_RDEF,
1146                         "Medium format corrupted") },
1147 /* D L  R O     */{SST(0x31, 0x01, SS_RDEF,
1148                         "Format command failed") },
1149 /* D   W  O     */{SST(0x32, 0x00, SS_RDEF,
1150                         "No defect spare location available") },
1151 /* D   W  O     */{SST(0x32, 0x01, SS_RDEF,
1152                         "Defect list update failure") },
1153 /*  T           */{SST(0x33, 0x00, SS_RDEF,
1154                         "Tape length error") },
1155 /* DTLPWRSOMCAE */{SST(0x34, 0x00, SS_RDEF,
1156                         "Enclosure failure") },
1157 /* DTLPWRSOMCAE */{SST(0x35, 0x00, SS_RDEF,
1158                         "Enclosure services failure") },
1159 /* DTLPWRSOMCAE */{SST(0x35, 0x01, SS_RDEF,
1160                         "Unsupported enclosure function") },
1161 /* DTLPWRSOMCAE */{SST(0x35, 0x02, SS_RDEF,
1162                         "Enclosure services unavailable") },
1163 /* DTLPWRSOMCAE */{SST(0x35, 0x03, SS_RDEF,
1164                         "Enclosure services transfer failure") },
1165 /* DTLPWRSOMCAE */{SST(0x35, 0x04, SS_RDEF,
1166                         "Enclosure services transfer refused") },
1167 /*   L          */{SST(0x36, 0x00, SS_RDEF,
1168                         "Ribbon, ink, or toner failure") },
1169 /* DTL WRSOMCAE */{SST(0x37, 0x00, SS_RDEF,
1170                         "Rounded parameter") },
1171 /* DTL WRSOMCAE */{SST(0x39, 0x00, SS_RDEF,
1172                         "Saving parameters not supported") },
1173 /* DTL WRSOM    */{SST(0x3A, 0x00, SS_FATAL|ENXIO,
1174                         "Medium not present") },
1175 /* DT  WR OM    */{SST(0x3A, 0x01, SS_FATAL|ENXIO,
1176                         "Medium not present - tray closed") },
1177 /* DT  WR OM    */{SST(0x3A, 0x02, SS_FATAL|ENXIO,
1178                         "Medium not present - tray open") },
1179 /*  TL          */{SST(0x3B, 0x00, SS_RDEF,
1180                         "Sequential positioning error") },
1181 /*  T           */{SST(0x3B, 0x01, SS_RDEF,
1182                         "Tape position error at beginning-of-medium") },
1183 /*  T           */{SST(0x3B, 0x02, SS_RDEF,
1184                         "Tape position error at end-of-medium") },
1185 /*   L          */{SST(0x3B, 0x03, SS_RDEF,
1186                         "Tape or electronic vertical forms unit not ready") },
1187 /*   L          */{SST(0x3B, 0x04, SS_RDEF,
1188                         "Slew failure") },
1189 /*   L          */{SST(0x3B, 0x05, SS_RDEF,
1190                         "Paper jam") },
1191 /*   L          */{SST(0x3B, 0x06, SS_RDEF,
1192                         "Failed to sense top-of-form") },
1193 /*   L          */{SST(0x3B, 0x07, SS_RDEF,
1194                         "Failed to sense bottom-of-form") },
1195 /*  T           */{SST(0x3B, 0x08, SS_RDEF,
1196                         "Reposition error") },
1197 /*       S      */{SST(0x3B, 0x09, SS_RDEF,
1198                         "Read past end of medium") },
1199 /*       S      */{SST(0x3B, 0x0A, SS_RDEF,
1200                         "Read past beginning of medium") },
1201 /*       S      */{SST(0x3B, 0x0B, SS_RDEF,
1202                         "Position past end of medium") },
1203 /*  T    S      */{SST(0x3B, 0x0C, SS_RDEF,
1204                         "Position past beginning of medium") },
1205 /* DT  WR OM    */{SST(0x3B, 0x0D, SS_FATAL|ENOSPC,
1206                         "Medium destination element full") },
1207 /* DT  WR OM    */{SST(0x3B, 0x0E, SS_RDEF,
1208                         "Medium source element empty") },
1209 /*      R       */{SST(0x3B, 0x0F, SS_RDEF,
1210                         "End of medium reached") },
1211 /* DT  WR OM    */{SST(0x3B, 0x11, SS_RDEF,
1212                         "Medium magazine not accessible") },
1213 /* DT  WR OM    */{SST(0x3B, 0x12, SS_RDEF,
1214                         "Medium magazine removed") },
1215 /* DT  WR OM    */{SST(0x3B, 0x13, SS_RDEF,
1216                         "Medium magazine inserted") },
1217 /* DT  WR OM    */{SST(0x3B, 0x14, SS_RDEF,
1218                         "Medium magazine locked") },
1219 /* DT  WR OM    */{SST(0x3B, 0x15, SS_RDEF,
1220                         "Medium magazine unlocked") },
1221 /* DTLPWRSOMCAE */{SST(0x3D, 0x00, SS_RDEF,
1222                         "Invalid bits in identify message") },
1223 /* DTLPWRSOMCAE */{SST(0x3E, 0x00, SS_RDEF,
1224                         "Logical unit has not self-configured yet") },
1225 /* DTLPWRSOMCAE */{SST(0x3E, 0x01, SS_RDEF,
1226                         "Logical unit failure") },
1227 /* DTLPWRSOMCAE */{SST(0x3E, 0x02, SS_RDEF,
1228                         "Timeout on logical unit") },
1229 /* DTLPWRSOMCAE */{SST(0x3F, 0x00, SS_RDEF,
1230                         "Target operating conditions have changed") },
1231 /* DTLPWRSOMCAE */{SST(0x3F, 0x01, SS_RDEF,
1232                         "Microcode has been changed") },
1233 /* DTLPWRSOMC   */{SST(0x3F, 0x02, SS_RDEF,
1234                         "Changed operating definition") },
1235 /* DTLPWRSOMCAE */{SST(0x3F, 0x03, SS_RDEF,
1236                         "Inquiry data has changed") },
1237 /* DT  WR OMCAE */{SST(0x3F, 0x04, SS_RDEF,
1238                         "Component device attached") },
1239 /* DT  WR OMCAE */{SST(0x3F, 0x05, SS_RDEF,
1240                         "Device identifier changed") },
1241 /* DT  WR OMCAE */{SST(0x3F, 0x06, SS_RDEF,
1242                         "Redundancy group created or modified") },
1243 /* DT  WR OMCAE */{SST(0x3F, 0x07, SS_RDEF,
1244                         "Redundancy group deleted") },
1245 /* DT  WR OMCAE */{SST(0x3F, 0x08, SS_RDEF,
1246                         "Spare created or modified") },
1247 /* DT  WR OMCAE */{SST(0x3F, 0x09, SS_RDEF,
1248                         "Spare deleted") },
1249 /* DT  WR OMCAE */{SST(0x3F, 0x0A, SS_RDEF,
1250                         "Volume set created or modified") },
1251 /* DT  WR OMCAE */{SST(0x3F, 0x0B, SS_RDEF,
1252                         "Volume set deleted") },
1253 /* DT  WR OMCAE */{SST(0x3F, 0x0C, SS_RDEF,
1254                         "Volume set deassigned") },
1255 /* DT  WR OMCAE */{SST(0x3F, 0x0D, SS_RDEF,
1256                         "Volume set reassigned") },
1257 /* D            */{SST(0x40, 0x00, SS_RDEF,
1258                         "Ram failure") }, /* deprecated - use 40 NN instead */
1259 /* DTLPWRSOMCAE */{SST(0x40, 0x80, SS_RDEF,
1260                         "Diagnostic failure: ASCQ = Component ID") },
1261 /* DTLPWRSOMCAE */{SST(0x40, 0xFF, SS_RDEF|SSQ_RANGE,
1262                         NULL) },/* Range 0x80->0xFF */
1263 /* D            */{SST(0x41, 0x00, SS_RDEF,
1264                         "Data path failure") }, /* deprecated - use 40 NN instead */
1265 /* D            */{SST(0x42, 0x00, SS_RDEF,
1266                         "Power-on or self-test failure") }, /* deprecated - use 40 NN instead */
1267 /* DTLPWRSOMCAE */{SST(0x43, 0x00, SS_RDEF,
1268                         "Message error") },
1269 /* DTLPWRSOMCAE */{SST(0x44, 0x00, SS_RDEF,
1270                         "Internal target failure") },
1271 /* DTLPWRSOMCAE */{SST(0x45, 0x00, SS_RDEF,
1272                         "Select or reselect failure") },
1273 /* DTLPWRSOMC   */{SST(0x46, 0x00, SS_RDEF,
1274                         "Unsuccessful soft reset") },
1275 /* DTLPWRSOMCAE */{SST(0x47, 0x00, SS_RDEF,
1276                         "SCSI parity error") },
1277 /* DTLPWRSOMCAE */{SST(0x48, 0x00, SS_RDEF,
1278                         "Initiator detected error message received") },
1279 /* DTLPWRSOMCAE */{SST(0x49, 0x00, SS_RDEF,
1280                         "Invalid message error") },
1281 /* DTLPWRSOMCAE */{SST(0x4A, 0x00, SS_RDEF,
1282                         "Command phase error") },
1283 /* DTLPWRSOMCAE */{SST(0x4B, 0x00, SS_RDEF,
1284                         "Data phase error") },
1285 /* DTLPWRSOMCAE */{SST(0x4C, 0x00, SS_RDEF,
1286                         "Logical unit failed self-configuration") },
1287 /* DTLPWRSOMCAE */{SST(0x4D, 0x00, SS_RDEF,
1288                         "Tagged overlapped commands: ASCQ = Queue tag ID") },
1289 /* DTLPWRSOMCAE */{SST(0x4D, 0xFF, SS_RDEF|SSQ_RANGE,
1290                         NULL)}, /* Range 0x00->0xFF */
1291 /* DTLPWRSOMCAE */{SST(0x4E, 0x00, SS_RDEF,
1292                         "Overlapped commands attempted") },
1293 /*  T           */{SST(0x50, 0x00, SS_RDEF,
1294                         "Write append error") },
1295 /*  T           */{SST(0x50, 0x01, SS_RDEF,
1296                         "Write append position error") },
1297 /*  T           */{SST(0x50, 0x02, SS_RDEF,
1298                         "Position error related to timing") },
1299 /*  T     O     */{SST(0x51, 0x00, SS_RDEF,
1300                         "Erase failure") },
1301 /*  T           */{SST(0x52, 0x00, SS_RDEF,
1302                         "Cartridge fault") },
1303 /* DTL WRSOM    */{SST(0x53, 0x00, SS_RDEF,
1304                         "Media load or eject failed") },
1305 /*  T           */{SST(0x53, 0x01, SS_RDEF,
1306                         "Unload tape failure") },
1307 /* DT  WR OM    */{SST(0x53, 0x02, SS_RDEF,
1308                         "Medium removal prevented") },
1309 /*    P         */{SST(0x54, 0x00, SS_RDEF,
1310                         "Scsi to host system interface failure") },
1311 /*    P         */{SST(0x55, 0x00, SS_RDEF,
1312                         "System resource failure") },
1313 /* D      O     */{SST(0x55, 0x01, SS_FATAL|ENOSPC,
1314                         "System buffer full") },
1315 /*      R       */{SST(0x57, 0x00, SS_RDEF,
1316                         "Unable to recover table-of-contents") },
1317 /*        O     */{SST(0x58, 0x00, SS_RDEF,
1318                         "Generation does not exist") },
1319 /*        O     */{SST(0x59, 0x00, SS_RDEF,
1320                         "Updated block read") },
1321 /* DTLPWRSOM    */{SST(0x5A, 0x00, SS_RDEF,
1322                         "Operator request or state change input") },
1323 /* DT  WR OM    */{SST(0x5A, 0x01, SS_RDEF,
1324                         "Operator medium removal request") },
1325 /* DT  W  O     */{SST(0x5A, 0x02, SS_RDEF,
1326                         "Operator selected write protect") },
1327 /* DT  W  O     */{SST(0x5A, 0x03, SS_RDEF,
1328                         "Operator selected write permit") },
1329 /* DTLPWRSOM    */{SST(0x5B, 0x00, SS_RDEF,
1330                         "Log exception") },
1331 /* DTLPWRSOM    */{SST(0x5B, 0x01, SS_RDEF,
1332                         "Threshold condition met") },
1333 /* DTLPWRSOM    */{SST(0x5B, 0x02, SS_RDEF,
1334                         "Log counter at maximum") },
1335 /* DTLPWRSOM    */{SST(0x5B, 0x03, SS_RDEF,
1336                         "Log list codes exhausted") },
1337 /* D      O     */{SST(0x5C, 0x00, SS_RDEF,
1338                         "RPL status change") },
1339 /* D      O     */{SST(0x5C, 0x01, SS_NOP|SSQ_PRINT_SENSE,
1340                         "Spindles synchronized") },
1341 /* D      O     */{SST(0x5C, 0x02, SS_RDEF,
1342                         "Spindles not synchronized") },
1343 /* DTLPWRSOMCAE */{SST(0x5D, 0x00, SS_RDEF,
1344                         "Failure prediction threshold exceeded") },
1345 /* DTLPWRSOMCAE */{SST(0x5D, 0xFF, SS_RDEF,
1346                         "Failure prediction threshold exceeded (false)") },
1347 /* DTLPWRSO CA  */{SST(0x5E, 0x00, SS_RDEF,
1348                         "Low power condition on") },
1349 /* DTLPWRSO CA  */{SST(0x5E, 0x01, SS_RDEF,
1350                         "Idle condition activated by timer") },
1351 /* DTLPWRSO CA  */{SST(0x5E, 0x02, SS_RDEF,
1352                         "Standby condition activated by timer") },
1353 /* DTLPWRSO CA  */{SST(0x5E, 0x03, SS_RDEF,
1354                         "Idle condition activated by command") },
1355 /* DTLPWRSO CA  */{SST(0x5E, 0x04, SS_RDEF,
1356                         "Standby condition activated by command") },
1357 /*       S      */{SST(0x60, 0x00, SS_RDEF,
1358                         "Lamp failure") },
1359 /*       S      */{SST(0x61, 0x00, SS_RDEF,
1360                         "Video acquisition error") },
1361 /*       S      */{SST(0x61, 0x01, SS_RDEF,
1362                         "Unable to acquire video") },
1363 /*       S      */{SST(0x61, 0x02, SS_RDEF,
1364                         "Out of focus") },
1365 /*       S      */{SST(0x62, 0x00, SS_RDEF,
1366                         "Scan head positioning error") },
1367 /*      R       */{SST(0x63, 0x00, SS_RDEF,
1368                         "End of user area encountered on this track") },
1369 /*      R       */{SST(0x63, 0x01, SS_FATAL|ENOSPC,
1370                         "Packet does not fit in available space") },
1371 /*      R       */{SST(0x64, 0x00, SS_RDEF,
1372                         "Illegal mode for this track") },
1373 /*      R       */{SST(0x64, 0x01, SS_RDEF,
1374                         "Invalid packet size") },
1375 /* DTLPWRSOMCAE */{SST(0x65, 0x00, SS_RDEF,
1376                         "Voltage fault") },
1377 /*       S      */{SST(0x66, 0x00, SS_RDEF,
1378                         "Automatic document feeder cover up") },
1379 /*       S      */{SST(0x66, 0x01, SS_RDEF,
1380                         "Automatic document feeder lift up") },
1381 /*       S      */{SST(0x66, 0x02, SS_RDEF,
1382                         "Document jam in automatic document feeder") },
1383 /*       S      */{SST(0x66, 0x03, SS_RDEF,
1384                         "Document miss feed automatic in document feeder") },
1385 /*           A  */{SST(0x67, 0x00, SS_RDEF,
1386                         "Configuration failure") },
1387 /*           A  */{SST(0x67, 0x01, SS_RDEF,
1388                         "Configuration of incapable logical units failed") },
1389 /*           A  */{SST(0x67, 0x02, SS_RDEF,
1390                         "Add logical unit failed") },
1391 /*           A  */{SST(0x67, 0x03, SS_RDEF,
1392                         "Modification of logical unit failed") },
1393 /*           A  */{SST(0x67, 0x04, SS_RDEF,
1394                         "Exchange of logical unit failed") },
1395 /*           A  */{SST(0x67, 0x05, SS_RDEF,
1396                         "Remove of logical unit failed") },
1397 /*           A  */{SST(0x67, 0x06, SS_RDEF,
1398                         "Attachment of logical unit failed") },
1399 /*           A  */{SST(0x67, 0x07, SS_RDEF,
1400                         "Creation of logical unit failed") },
1401 /*           A  */{SST(0x68, 0x00, SS_RDEF,
1402                         "Logical unit not configured") },
1403 /*           A  */{SST(0x69, 0x00, SS_RDEF,
1404                         "Data loss on logical unit") },
1405 /*           A  */{SST(0x69, 0x01, SS_RDEF,
1406                         "Multiple logical unit failures") },
1407 /*           A  */{SST(0x69, 0x02, SS_RDEF,
1408                         "Parity/data mismatch") },
1409 /*           A  */{SST(0x6A, 0x00, SS_RDEF,
1410                         "Informational, refer to log") },
1411 /*           A  */{SST(0x6B, 0x00, SS_RDEF,
1412                         "State change has occurred") },
1413 /*           A  */{SST(0x6B, 0x01, SS_RDEF,
1414                         "Redundancy level got better") },
1415 /*           A  */{SST(0x6B, 0x02, SS_RDEF,
1416                         "Redundancy level got worse") },
1417 /*           A  */{SST(0x6C, 0x00, SS_RDEF,
1418                         "Rebuild failure occurred") },
1419 /*           A  */{SST(0x6D, 0x00, SS_RDEF,
1420                         "Recalculate failure occurred") },
1421 /*           A  */{SST(0x6E, 0x00, SS_RDEF,
1422                         "Command to logical unit failed") },
1423 /*  T           */{SST(0x70, 0x00, SS_RDEF,
1424                         "Decompression exception short: ASCQ = Algorithm ID") },
1425 /*  T           */{SST(0x70, 0xFF, SS_RDEF|SSQ_RANGE,
1426                         NULL) }, /* Range 0x00 -> 0xFF */
1427 /*  T           */{SST(0x71, 0x00, SS_RDEF,
1428                         "Decompression exception long: ASCQ = Algorithm ID") },
1429 /*  T           */{SST(0x71, 0xFF, SS_RDEF|SSQ_RANGE,
1430                         NULL) }, /* Range 0x00 -> 0xFF */       
1431 /*      R       */{SST(0x72, 0x00, SS_RDEF,
1432                         "Session fixation error") },
1433 /*      R       */{SST(0x72, 0x01, SS_RDEF,
1434                         "Session fixation error writing lead-in") },
1435 /*      R       */{SST(0x72, 0x02, SS_RDEF,
1436                         "Session fixation error writing lead-out") },
1437 /*      R       */{SST(0x72, 0x03, SS_RDEF,
1438                         "Session fixation error - incomplete track in session") },
1439 /*      R       */{SST(0x72, 0x04, SS_RDEF,
1440                         "Empty or partially written reserved track") },
1441 /*      R       */{SST(0x73, 0x00, SS_RDEF,
1442                         "CD control error") },
1443 /*      R       */{SST(0x73, 0x01, SS_RDEF,
1444                         "Power calibration area almost full") },
1445 /*      R       */{SST(0x73, 0x02, SS_FATAL|ENOSPC,
1446                         "Power calibration area is full") },
1447 /*      R       */{SST(0x73, 0x03, SS_RDEF,
1448                         "Power calibration area error") },
1449 /*      R       */{SST(0x73, 0x04, SS_RDEF,
1450                         "Program memory area update failure") },
1451 /*      R       */{SST(0x73, 0x05, SS_RDEF,
1452                         "program memory area is full") }
1453 };
1454
1455 const int asc_table_size = sizeof(asc_table)/sizeof(asc_table[0]);
1456
1457 struct asc_key
1458 {
1459         int asc;
1460         int ascq;
1461 };
1462
1463 static int
1464 ascentrycomp(const void *key, const void *member)
1465 {
1466         int asc;
1467         int ascq;
1468         const struct asc_table_entry *table_entry;
1469
1470         asc = ((const struct asc_key *)key)->asc;
1471         ascq = ((const struct asc_key *)key)->ascq;
1472         table_entry = (const struct asc_table_entry *)member;
1473
1474         if (asc >= table_entry->asc) {
1475
1476                 if (asc > table_entry->asc)
1477                         return (1);
1478
1479                 if (ascq <= table_entry->ascq) {
1480                         /* Check for ranges */
1481                         if (ascq == table_entry->ascq
1482                          || ((table_entry->action & SSQ_RANGE) != 0
1483                            && ascq >= (table_entry - 1)->ascq))
1484                                 return (0);
1485                         return (-1);
1486                 }
1487                 return (1);
1488         }
1489         return (-1);
1490 }
1491
1492 static int
1493 senseentrycomp(const void *key, const void *member)
1494 {
1495         int sense_key;
1496         const struct sense_key_table_entry *table_entry;
1497
1498         sense_key = *((const int *)key);
1499         table_entry = (const struct sense_key_table_entry *)member;
1500
1501         if (sense_key >= table_entry->sense_key) {
1502                 if (sense_key == table_entry->sense_key)
1503                         return (0);
1504                 return (1);
1505         }
1506         return (-1);
1507 }
1508
1509 static void
1510 fetchtableentries(int sense_key, int asc, int ascq,
1511                   struct scsi_inquiry_data *inq_data,
1512                   const struct sense_key_table_entry **sense_entry,
1513                   const struct asc_table_entry **asc_entry)
1514 {
1515         caddr_t match;
1516         const struct asc_table_entry *asc_tables[2];
1517         const struct sense_key_table_entry *sense_tables[2];
1518         struct asc_key asc_ascq;
1519         size_t asc_tables_size[2];
1520         size_t sense_tables_size[2];
1521         int num_asc_tables;
1522         int num_sense_tables;
1523         int i;
1524
1525         /* Default to failure */
1526         *sense_entry = NULL;
1527         *asc_entry = NULL;
1528         match = NULL;
1529         if (inq_data != NULL)
1530                 match = cam_quirkmatch((caddr_t)inq_data,
1531                                        (caddr_t)sense_quirk_table,
1532                                        sense_quirk_table_size,
1533                                        sizeof(*sense_quirk_table),
1534                                        scsi_inquiry_match);
1535
1536         if (match != NULL) {
1537                 struct scsi_sense_quirk_entry *quirk;
1538
1539                 quirk = (struct scsi_sense_quirk_entry *)match;
1540                 asc_tables[0] = quirk->asc_info;
1541                 asc_tables_size[0] = quirk->num_ascs;
1542                 asc_tables[1] = asc_table;
1543                 asc_tables_size[1] = asc_table_size;
1544                 num_asc_tables = 2;
1545                 sense_tables[0] = quirk->sense_key_info;
1546                 sense_tables_size[0] = quirk->num_sense_keys;
1547                 sense_tables[1] = sense_key_table;
1548                 sense_tables_size[1] = sense_key_table_size;
1549                 num_sense_tables = 2;
1550         } else {
1551                 asc_tables[0] = asc_table;
1552                 asc_tables_size[0] = asc_table_size;
1553                 num_asc_tables = 1;
1554                 sense_tables[0] = sense_key_table;
1555                 sense_tables_size[0] = sense_key_table_size;
1556                 num_sense_tables = 1;
1557         }
1558
1559         asc_ascq.asc = asc;
1560         asc_ascq.ascq = ascq;
1561         for (i = 0; i < num_asc_tables; i++) {
1562                 void *found_entry;
1563
1564                 found_entry = bsearch(&asc_ascq, asc_tables[i],
1565                                       asc_tables_size[i],
1566                                       sizeof(**asc_tables),
1567                                       ascentrycomp);
1568
1569                 if (found_entry) {
1570                         *asc_entry = (struct asc_table_entry *)found_entry;
1571                         break;
1572                 }
1573         }
1574
1575         for (i = 0; i < num_sense_tables; i++) {
1576                 void *found_entry;
1577
1578                 found_entry = bsearch(&sense_key, sense_tables[i],
1579                                       sense_tables_size[i],
1580                                       sizeof(**sense_tables),
1581                                       senseentrycomp);
1582
1583                 if (found_entry) {
1584                         *sense_entry =
1585                             (struct sense_key_table_entry *)found_entry;
1586                         break;
1587                 }
1588         }
1589 }
1590
1591 void
1592 scsi_sense_desc(int sense_key, int asc, int ascq,
1593                 struct scsi_inquiry_data *inq_data,
1594                 const char **sense_key_desc, const char **asc_desc)
1595 {
1596         const struct asc_table_entry *asc_entry;
1597         const struct sense_key_table_entry *sense_entry;
1598
1599         fetchtableentries(sense_key, asc, ascq,
1600                           inq_data,
1601                           &sense_entry,
1602                           &asc_entry);
1603
1604         *sense_key_desc = sense_entry->desc;
1605
1606         if (asc_entry != NULL)
1607                 *asc_desc = asc_entry->desc;
1608         else if (asc >= 0x80 && asc <= 0xff)
1609                 *asc_desc = "Vendor Specific ASC";
1610         else if (ascq >= 0x80 && ascq <= 0xff)
1611                 *asc_desc = "Vendor Specific ASCQ";
1612         else
1613                 *asc_desc = "Reserved ASC/ASCQ pair";
1614 }
1615
1616 /*
1617  * Given sense and device type information, return the appropriate action.
1618  * If we do not understand the specific error as identified by the ASC/ASCQ
1619  * pair, fall back on the more generic actions derived from the sense key.
1620  */
1621 scsi_sense_action
1622 scsi_error_action(struct ccb_scsiio *csio, struct scsi_inquiry_data *inq_data,
1623                   u_int32_t sense_flags)
1624 {
1625         const struct asc_table_entry *asc_entry;
1626         const struct sense_key_table_entry *sense_entry;
1627         int error_code, sense_key, asc, ascq;
1628         scsi_sense_action action;
1629
1630         scsi_extract_sense(&csio->sense_data, &error_code,
1631                            &sense_key, &asc, &ascq);
1632
1633         if (error_code == SSD_DEFERRED_ERROR) {
1634                 /*
1635                  * XXX dufault@FreeBSD.org
1636                  * This error doesn't relate to the command associated
1637                  * with this request sense.  A deferred error is an error
1638                  * for a command that has already returned GOOD status
1639                  * (see SCSI2 8.2.14.2).
1640                  *
1641                  * By my reading of that section, it looks like the current
1642                  * command has been cancelled, we should now clean things up
1643                  * (hopefully recovering any lost data) and then retry the
1644                  * current command.  There are two easy choices, both wrong:
1645                  *
1646                  * 1. Drop through (like we had been doing), thus treating
1647                  *    this as if the error were for the current command and
1648                  *    return and stop the current command.
1649                  *
1650                  * 2. Issue a retry (like I made it do) thus hopefully
1651                  *    recovering the current transfer, and ignoring the
1652                  *    fact that we've dropped a command.
1653                  *
1654                  * These should probably be handled in a device specific
1655                  * sense handler or punted back up to a user mode daemon
1656                  */
1657                 action = SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE;
1658         } else {
1659                 fetchtableentries(sense_key, asc, ascq,
1660                                   inq_data,
1661                                   &sense_entry,
1662                                   &asc_entry);
1663
1664                 /*
1665                  * Override the 'No additional Sense' entry (0,0)
1666                  * with the error action of the sense key.
1667                  */
1668                 if (asc_entry != NULL
1669                  && (asc != 0 || ascq != 0))
1670                         action = asc_entry->action;
1671                 else
1672                         action = sense_entry->action;
1673
1674                 if (sense_key == SSD_KEY_RECOVERED_ERROR) {
1675                         /*
1676                          * The action succeeded but the device wants
1677                          * the user to know that some recovery action
1678                          * was required.
1679                          */
1680                         action &= ~(SS_MASK|SSQ_MASK|SS_ERRMASK);
1681                         action |= SS_NOP|SSQ_PRINT_SENSE;
1682                 } else if (sense_key == SSD_KEY_ILLEGAL_REQUEST) {
1683                         if ((sense_flags & SF_QUIET_IR) != 0)
1684                                 action &= ~SSQ_PRINT_SENSE;
1685                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION) {
1686                         if ((sense_flags & SF_RETRY_UA) != 0
1687                          && (action & SS_MASK) == SS_FAIL) {
1688                                 action &= ~(SS_MASK|SSQ_MASK);
1689                                 action |= SS_RETRY|SSQ_DECREMENT_COUNT|
1690                                           SSQ_PRINT_SENSE;
1691                         }
1692                 }
1693         }
1694 #ifdef KERNEL
1695         if (bootverbose)
1696                 sense_flags |= SF_PRINT_ALWAYS;
1697 #endif
1698         if ((sense_flags & SF_PRINT_ALWAYS) != 0)
1699                 action |= SSQ_PRINT_SENSE;
1700         else if ((sense_flags & SF_NO_PRINT) != 0)
1701                 action &= ~SSQ_PRINT_SENSE;
1702
1703         return (action);
1704 }
1705
1706 char *
1707 scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string, size_t len)
1708 {
1709         u_int8_t cdb_len;
1710         int i;
1711
1712         if (cdb_ptr == NULL)
1713                 return("");
1714
1715         /* Silence warnings */
1716         cdb_len = 0;
1717
1718         /*
1719          * This is taken from the SCSI-3 draft spec.
1720          * (T10/1157D revision 0.3)
1721          * The top 3 bits of an opcode are the group code.  The next 5 bits
1722          * are the command code.
1723          * Group 0:  six byte commands
1724          * Group 1:  ten byte commands
1725          * Group 2:  ten byte commands
1726          * Group 3:  reserved
1727          * Group 4:  sixteen byte commands
1728          * Group 5:  twelve byte commands
1729          * Group 6:  vendor specific
1730          * Group 7:  vendor specific
1731          */
1732         switch((*cdb_ptr >> 5) & 0x7) {
1733                 case 0:
1734                         cdb_len = 6;
1735                         break;
1736                 case 1:
1737                 case 2:
1738                         cdb_len = 10;
1739                         break;
1740                 case 3:
1741                 case 6:
1742                 case 7:
1743                         /* in this case, just print out the opcode */
1744                         cdb_len = 1;
1745                         break;
1746                 case 4:
1747                         cdb_len = 16;
1748                         break;
1749                 case 5:
1750                         cdb_len = 12;
1751                         break;
1752         }
1753         *cdb_string = '\0';
1754         for (i = 0; i < cdb_len; i++)
1755                 ksnprintf(cdb_string + strlen(cdb_string),
1756                           len - strlen(cdb_string), "%x ", cdb_ptr[i]);
1757
1758         return(cdb_string);
1759 }
1760
1761 const char *
1762 scsi_status_string(struct ccb_scsiio *csio)
1763 {
1764         switch(csio->scsi_status) {
1765         case SCSI_STATUS_OK:
1766                 return("OK");
1767         case SCSI_STATUS_CHECK_COND:
1768                 return("Check Condition");
1769         case SCSI_STATUS_BUSY:
1770                 return("Busy");
1771         case SCSI_STATUS_INTERMED:
1772                 return("Intermediate");
1773         case SCSI_STATUS_INTERMED_COND_MET:
1774                 return("Intermediate-Condition Met");
1775         case SCSI_STATUS_RESERV_CONFLICT:
1776                 return("Reservation Conflict");
1777         case SCSI_STATUS_CMD_TERMINATED:
1778                 return("Command Terminated");
1779         case SCSI_STATUS_QUEUE_FULL:
1780                 return("Queue Full");
1781         case SCSI_STATUS_ACA_ACTIVE:
1782                 return("ACA Active");
1783         case SCSI_STATUS_TASK_ABORTED:
1784                 return("Task Aborted");
1785         default: {
1786                 static char unkstr[64];
1787                 ksnprintf(unkstr, sizeof(unkstr), "Unknown %#x",
1788                           csio->scsi_status);
1789                 return(unkstr);
1790         }
1791         }
1792 }
1793
1794 /*
1795  * scsi_command_string() returns 0 for success and -1 for failure.
1796  */
1797 #ifdef _KERNEL
1798 int
1799 scsi_command_string(struct ccb_scsiio *csio, struct sbuf *sb)
1800 #else
1801 int
1802 scsi_command_string(struct cam_device *device, struct ccb_scsiio *csio,
1803                     struct sbuf *sb)
1804 #endif
1805 {
1806         struct scsi_inquiry_data *inq_data;
1807         char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
1808 #ifdef _KERNEL
1809         struct    ccb_getdev cgd;
1810 #endif
1811
1812 #ifdef _KERNEL
1813         /*
1814          * Get the device information.
1815          */
1816         xpt_setup_ccb(&cgd.ccb_h,
1817                       csio->ccb_h.path,
1818                       /*priority*/ 1);
1819         cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1820         xpt_action((union ccb *)&cgd);
1821
1822         /*
1823          * If the device is unconfigured, just pretend that it is a hard
1824          * drive.  scsi_op_desc() needs this.
1825          */
1826         if (cgd.ccb_h.status == CAM_DEV_NOT_THERE)
1827                 cgd.inq_data.device = T_DIRECT;
1828
1829         inq_data = &cgd.inq_data;
1830
1831 #else /* !_KERNEL */
1832
1833         inq_data = &device->inq_data;
1834
1835 #endif /* _KERNEL/!_KERNEL */
1836
1837         if ((csio->ccb_h.flags & CAM_CDB_POINTER) != 0) {
1838                 sbuf_printf(sb, "%s. CDB: %s",
1839                             scsi_op_desc(csio->cdb_io.cdb_ptr[0], inq_data),
1840                             scsi_cdb_string(csio->cdb_io.cdb_ptr, cdb_str,
1841                                             sizeof(cdb_str)));
1842         } else {
1843                 sbuf_printf(sb, "%s. CDB: %s",
1844                             scsi_op_desc(csio->cdb_io.cdb_bytes[0], inq_data),
1845                             scsi_cdb_string(csio->cdb_io.cdb_bytes, cdb_str,
1846                                             sizeof(cdb_str)));
1847         }
1848
1849         return(0);
1850 }
1851
1852 /*
1853  * scsi_sense_sbuf() returns 0 for success and -1 for failure.
1854  */
1855 #ifdef _KERNEL
1856 int
1857 scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
1858                 scsi_sense_string_flags flags)
1859 #else /* !_KERNEL */
1860 int
1861 scsi_sense_sbuf(struct cam_device *device, struct ccb_scsiio *csio,
1862                 struct sbuf *sb, scsi_sense_string_flags flags)
1863 #endif /* _KERNEL/!_KERNEL */
1864 {
1865         struct    scsi_sense_data *sense;
1866         struct    scsi_inquiry_data *inq_data;
1867 #ifdef _KERNEL
1868         struct    ccb_getdev cgd;
1869 #endif /* _KERNEL */
1870         u_int32_t info;
1871         int       error_code;
1872         int       sense_key;
1873         int       asc, ascq;
1874         char      path_str[64];
1875
1876 #ifndef _KERNEL
1877         if (device == NULL)
1878                 return(-1);
1879 #endif /* !_KERNEL */
1880         if ((csio == NULL) || (sb == NULL))
1881                 return(-1);
1882
1883         /*
1884          * If the CDB is a physical address, we can't deal with it..
1885          */
1886         if ((csio->ccb_h.flags & CAM_CDB_PHYS) != 0)
1887                 flags &= ~SSS_FLAG_PRINT_COMMAND;
1888
1889 #ifdef _KERNEL
1890         xpt_path_string(csio->ccb_h.path, path_str, sizeof(path_str));
1891 #else /* !_KERNEL */
1892         cam_path_string(device, path_str, sizeof(path_str));
1893 #endif /* _KERNEL/!_KERNEL */
1894
1895 #ifdef _KERNEL
1896         /*
1897          * Get the device information.
1898          */
1899         xpt_setup_ccb(&cgd.ccb_h,
1900                       csio->ccb_h.path,
1901                       /*priority*/ 1);
1902         cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1903         xpt_action((union ccb *)&cgd);
1904
1905         /*
1906          * If the device is unconfigured, just pretend that it is a hard
1907          * drive.  scsi_op_desc() needs this.
1908          */
1909         if (cgd.ccb_h.status == CAM_DEV_NOT_THERE)
1910                 cgd.inq_data.device = T_DIRECT;
1911
1912         inq_data = &cgd.inq_data;
1913
1914 #else /* !_KERNEL */
1915
1916         inq_data = &device->inq_data;
1917
1918 #endif /* _KERNEL/!_KERNEL */
1919
1920         sense = NULL;
1921
1922         if (flags & SSS_FLAG_PRINT_COMMAND) {
1923
1924                 sbuf_cat(sb, path_str);
1925
1926 #ifdef _KERNEL
1927                 scsi_command_string(csio, sb);
1928 #else /* !_KERNEL */
1929                 scsi_command_string(device, csio, sb);
1930 #endif /* _KERNEL/!_KERNEL */
1931         }
1932
1933         /*
1934          * If the sense data is a physical pointer, forget it.
1935          */
1936         if (csio->ccb_h.flags & CAM_SENSE_PTR) {
1937                 if (csio->ccb_h.flags & CAM_SENSE_PHYS)
1938                         return(-1);
1939                 else {
1940                         /* 
1941                          * bcopy the pointer to avoid unaligned access
1942                          * errors on finicky architectures.  We don't
1943                          * ensure that the sense data is pointer aligned.
1944                          */
1945                         bcopy(&csio->sense_data, &sense, 
1946                               sizeof(struct scsi_sense_data *));
1947                 }
1948         } else {
1949                 /*
1950                  * If the physical sense flag is set, but the sense pointer
1951                  * is not also set, we assume that the user is an idiot and
1952                  * return.  (Well, okay, it could be that somehow, the
1953                  * entire csio is physical, but we would have probably core
1954                  * dumped on one of the bogus pointer deferences above
1955                  * already.)
1956                  */
1957                 if (csio->ccb_h.flags & CAM_SENSE_PHYS) 
1958                         return(-1);
1959                 else
1960                         sense = &csio->sense_data;
1961         }
1962
1963
1964         sbuf_cat(sb, path_str);
1965
1966         error_code = sense->error_code & SSD_ERRCODE;
1967         sense_key = sense->flags & SSD_KEY;
1968
1969         switch (error_code) {
1970         case SSD_DEFERRED_ERROR:
1971                 sbuf_printf(sb, "Deferred Error: ");
1972
1973                 /* FALLTHROUGH */
1974         case SSD_CURRENT_ERROR:
1975         {
1976                 const char *sense_key_desc;
1977                 const char *asc_desc;
1978
1979                 asc = (sense->extra_len >= 5) ? sense->add_sense_code : 0;
1980                 ascq = (sense->extra_len >= 6) ? sense->add_sense_code_qual : 0;
1981                 scsi_sense_desc(sense_key, asc, ascq, inq_data,
1982                                 &sense_key_desc, &asc_desc);
1983                 sbuf_cat(sb, sense_key_desc);
1984
1985                 info = scsi_4btoul(sense->info);
1986                 
1987                 if (sense->error_code & SSD_ERRCODE_VALID) {
1988
1989                         switch (sense_key) {
1990                         case SSD_KEY_NOT_READY:
1991                         case SSD_KEY_ILLEGAL_REQUEST:
1992                         case SSD_KEY_UNIT_ATTENTION:
1993                         case SSD_KEY_DATA_PROTECT:
1994                                 break;
1995                         case SSD_KEY_BLANK_CHECK:
1996                                 sbuf_printf(sb, " req sz: %d (decimal)", info);
1997                                 break;
1998                         default:
1999                                 if (info) {
2000                                         if (sense->flags & SSD_ILI) {
2001                                                 sbuf_printf(sb, " ILI (length "
2002                                                         "mismatch): %d", info);
2003                         
2004                                         } else {
2005                                                 sbuf_printf(sb, " info:%x",
2006                                                             info);
2007                                         }
2008                                 }
2009                         }
2010                 } else if (info) {
2011                         sbuf_printf(sb, " info?:%x", info);
2012                 }
2013
2014                 if (sense->extra_len >= 4) {
2015                         if (bcmp(sense->cmd_spec_info, "\0\0\0\0", 4)) {
2016                                 sbuf_printf(sb, " csi:%x,%x,%x,%x",
2017                                             sense->cmd_spec_info[0],
2018                                             sense->cmd_spec_info[1],
2019                                             sense->cmd_spec_info[2],
2020                                             sense->cmd_spec_info[3]);
2021                         }
2022                 }
2023
2024                 sbuf_printf(sb, " asc:%x,%x\n%s%s", asc, ascq,
2025                             path_str, asc_desc);
2026
2027                 if (sense->extra_len >= 7 && sense->fru) {
2028                         sbuf_printf(sb, " field replaceable unit: %x",
2029                                     sense->fru);
2030                 }
2031
2032                 if ((sense->extra_len >= 10)
2033                  && (sense->sense_key_spec[0] & SSD_SCS_VALID) != 0) {
2034                         switch(sense_key) {
2035                         case SSD_KEY_ILLEGAL_REQUEST: {
2036                                 int bad_command;
2037                                 char tmpstr2[40];
2038
2039                                 if (sense->sense_key_spec[0] & 0x40)
2040                                         bad_command = 1;
2041                                 else
2042                                         bad_command = 0;
2043
2044                                 tmpstr2[0] = '\0';
2045
2046                                 /* Bit pointer is valid */
2047                                 if (sense->sense_key_spec[0] & 0x08)
2048                                         ksnprintf(tmpstr2, sizeof(tmpstr2),
2049                                                  "bit %d",
2050                                                 sense->sense_key_spec[0] & 0x7);
2051                                         sbuf_printf(sb,
2052                                                    ": %s byte %d %s is invalid",
2053                                                     bad_command ?
2054                                                     "Command" : "Data",
2055                                                     scsi_2btoul(
2056                                                     &sense->sense_key_spec[1]),
2057                                                     tmpstr2);
2058                                 break;
2059                         }
2060                         case SSD_KEY_RECOVERED_ERROR:
2061                         case SSD_KEY_HARDWARE_ERROR:
2062                         case SSD_KEY_MEDIUM_ERROR:
2063                                 sbuf_printf(sb, " actual retry count: %d",
2064                                             scsi_2btoul(
2065                                             &sense->sense_key_spec[1]));
2066                                 break;
2067                         default:
2068                                 sbuf_printf(sb, " sks:%#x,%#x",
2069                                             sense->sense_key_spec[0],
2070                                             scsi_2btoul(
2071                                             &sense->sense_key_spec[1]));
2072                                 break;
2073                         }
2074                 }
2075                 break;
2076
2077         }
2078         default:
2079                 sbuf_printf(sb, "error code %d",
2080                             sense->error_code & SSD_ERRCODE);
2081
2082                 if (sense->error_code & SSD_ERRCODE_VALID) {
2083                         sbuf_printf(sb, " at block no. %d (decimal)",
2084                                     info = scsi_4btoul(sense->info));
2085                 }
2086         }
2087
2088         sbuf_printf(sb, "\n");
2089
2090         return(0);
2091 }
2092
2093 #ifdef _KERNEL
2094 char *
2095 scsi_sense_string(struct ccb_scsiio *csio, char *str, int str_len)
2096 #else /* !_KERNEL */
2097 char *
2098 scsi_sense_string(struct cam_device *device, struct ccb_scsiio *csio,
2099                   char *str, int str_len)
2100 #endif /* _KERNEL/!_KERNEL */
2101 {
2102         struct sbuf sb;
2103
2104         sbuf_new(&sb, str, str_len, 0);
2105
2106 #ifdef _KERNEL
2107         scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
2108 #else /* !_KERNEL */
2109         scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
2110 #endif /* _KERNEL/!_KERNEL */
2111
2112         sbuf_finish(&sb);
2113
2114         return(sbuf_data(&sb));
2115 }
2116
2117 #ifdef _KERNEL
2118 void
2119 scsi_sense_print(struct ccb_scsiio *csio)
2120 {
2121         struct sbuf sb;
2122         char str[512];
2123
2124         sbuf_new(&sb, str, sizeof(str), 0);
2125
2126         scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
2127
2128         sbuf_finish(&sb);
2129
2130         kprintf("%s", sbuf_data(&sb));
2131 }
2132
2133 #else /* !_KERNEL */
2134 void
2135 scsi_sense_print(struct cam_device *device, struct ccb_scsiio *csio, 
2136                  FILE *ofile)
2137 {
2138         struct sbuf sb;
2139         char str[512];
2140
2141         if ((device == NULL) || (csio == NULL) || (ofile == NULL))
2142                 return;
2143
2144         sbuf_new(&sb, str, sizeof(str), 0);
2145
2146         scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
2147
2148         sbuf_finish(&sb);
2149
2150         fprintf(ofile, "%s", sbuf_data(&sb));
2151 }
2152
2153 #endif /* _KERNEL/!_KERNEL */
2154
2155 /*
2156  * This function currently requires at least 36 bytes, or
2157  * SHORT_INQUIRY_LENGTH, worth of data to function properly.  If this
2158  * function needs more or less data in the future, another length should be
2159  * defined in scsi_all.h to indicate the minimum amount of data necessary
2160  * for this routine to function properly.
2161  */
2162 void
2163 scsi_print_inquiry(struct scsi_inquiry_data *inq_data)
2164 {
2165         u_int8_t type;
2166         char *dtype, *qtype;
2167         char vendor[16], product[48], revision[16], rstr[4];
2168
2169         type = SID_TYPE(inq_data);
2170
2171         /*
2172          * Figure out basic device type and qualifier.
2173          */
2174         if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data)) {
2175                 qtype = "(vendor-unique qualifier)";
2176         } else {
2177                 switch (SID_QUAL(inq_data)) {
2178                 case SID_QUAL_LU_CONNECTED:
2179                         qtype = "";
2180                         break;
2181
2182                 case SID_QUAL_LU_OFFLINE:
2183                         qtype = "(offline)";
2184                         break;
2185
2186                 case SID_QUAL_RSVD:
2187                         qtype = "(reserved qualifier)";
2188                         break;
2189                 default:
2190                 case SID_QUAL_BAD_LU:
2191                         qtype = "(lun not supported)";
2192                         break;
2193                 }
2194         }
2195
2196         switch (type) {
2197         case T_DIRECT:
2198                 dtype = "Direct Access";
2199                 break;
2200         case T_SEQUENTIAL:
2201                 dtype = "Sequential Access";
2202                 break;
2203         case T_PRINTER:
2204                 dtype = "Printer";
2205                 break;
2206         case T_PROCESSOR:
2207                 dtype = "Processor";
2208                 break;
2209         case T_CDROM:
2210                 dtype = "CD-ROM";
2211                 break;
2212         case T_WORM:
2213                 dtype = "Worm";
2214                 break;
2215         case T_SCANNER:
2216                 dtype = "Scanner";
2217                 break;
2218         case T_OPTICAL:
2219                 dtype = "Optical";
2220                 break;
2221         case T_CHANGER:
2222                 dtype = "Changer";
2223                 break;
2224         case T_COMM:
2225                 dtype = "Communication";
2226                 break;
2227         case T_STORARRAY:
2228                 dtype = "Storage Array";
2229                 break;
2230         case T_ENCLOSURE:
2231                 dtype = "Enclosure Services";
2232                 break;
2233         case T_RBC:
2234                 dtype = "Simplified Direct Access";
2235                 break;
2236         case T_OCRW:
2237                 dtype = "Optical Card Read/Write";
2238                 break;
2239         case T_NODEVICE:
2240                 dtype = "Uninstalled";
2241         default:
2242                 dtype = "unknown";
2243                 break;
2244         }
2245
2246         cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
2247                    sizeof(vendor));
2248         cam_strvis(product, inq_data->product, sizeof(inq_data->product),
2249                    sizeof(product));
2250         cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
2251                    sizeof(revision));
2252
2253         if (SID_ANSI_REV(inq_data) == SCSI_REV_CCS)
2254                 bcopy("CCS", rstr, 4);
2255         else
2256                 ksnprintf(rstr, sizeof (rstr), "%d", SID_ANSI_REV(inq_data));
2257         kprintf("<%s %s %s> %s %s SCSI-%s device %s\n",
2258                vendor, product, revision,
2259                SID_IS_REMOVABLE(inq_data) ? "Removable" : "Fixed",
2260                dtype, rstr, qtype);
2261 }
2262
2263 /*
2264  * Table of syncrates that don't follow the "divisible by 4"
2265  * rule. This table will be expanded in future SCSI specs.
2266  */
2267 static struct {
2268         u_int period_factor;
2269         u_int period;   /* in 100ths of ns */
2270 } scsi_syncrates[] = {
2271         { 0x08, 625 },  /* FAST-160 */
2272         { 0x09, 1250 }, /* FAST-80 */
2273         { 0x0a, 2500 }, /* FAST-40 40MHz */
2274         { 0x0b, 3030 }, /* FAST-40 33MHz */
2275         { 0x0c, 5000 }  /* FAST-20 */
2276 };
2277
2278 /*
2279  * Return the frequency in kHz corresponding to the given
2280  * sync period factor.
2281  */
2282 u_int
2283 scsi_calc_syncsrate(u_int period_factor)
2284 {
2285         int i;
2286         int num_syncrates;
2287
2288         num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
2289         /* See if the period is in the "exception" table */
2290         for (i = 0; i < num_syncrates; i++) {
2291
2292                 if (period_factor == scsi_syncrates[i].period_factor) {
2293                         /* Period in kHz */
2294                         return (100000000 / scsi_syncrates[i].period);
2295                 }
2296         }
2297
2298         /*
2299          * Wasn't in the table, so use the standard
2300          * 4 times conversion.
2301          */
2302         return (10000000 / (period_factor * 4 * 10));
2303 }
2304
2305 /*
2306  * Return the SCSI sync parameter that corresponsd to
2307  * the passed in period in 10ths of ns.
2308  */
2309 u_int
2310 scsi_calc_syncparam(u_int period)
2311 {
2312         int i;
2313         int num_syncrates;
2314
2315         if (period == 0)
2316                 return (~0);    /* Async */
2317
2318         /* Adjust for exception table being in 100ths. */
2319         period *= 10;
2320         num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
2321         /* See if the period is in the "exception" table */
2322         for (i = 0; i < num_syncrates; i++) {
2323
2324                 if (period <= scsi_syncrates[i].period) {
2325                         /* Period in 100ths of ns */
2326                         return (scsi_syncrates[i].period_factor);
2327                 }
2328         }
2329
2330         /*
2331          * Wasn't in the table, so use the standard
2332          * 1/4 period in ns conversion.
2333          */
2334         return (period/400);
2335 }
2336
2337 void
2338 scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries,
2339                      void (*cbfcnp)(struct cam_periph *, union ccb *),
2340                      u_int8_t tag_action, u_int8_t sense_len, u_int32_t timeout)
2341 {
2342         struct scsi_test_unit_ready *scsi_cmd;
2343
2344         cam_fill_csio(csio,
2345                       retries,
2346                       cbfcnp,
2347                       CAM_DIR_NONE,
2348                       tag_action,
2349                       /*data_ptr*/NULL,
2350                       /*dxfer_len*/0,
2351                       sense_len,
2352                       sizeof(*scsi_cmd),
2353                       timeout);
2354
2355         scsi_cmd = (struct scsi_test_unit_ready *)&csio->cdb_io.cdb_bytes;
2356         bzero(scsi_cmd, sizeof(*scsi_cmd));
2357         scsi_cmd->opcode = TEST_UNIT_READY;
2358 }
2359
2360 void
2361 scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
2362                    void (*cbfcnp)(struct cam_periph *, union ccb *),
2363                    void *data_ptr, u_int8_t dxfer_len, u_int8_t tag_action,
2364                    u_int8_t sense_len, u_int32_t timeout)
2365 {
2366         struct scsi_request_sense *scsi_cmd;
2367
2368         cam_fill_csio(csio,
2369                       retries,
2370                       cbfcnp,
2371                       CAM_DIR_IN,
2372                       tag_action,
2373                       data_ptr,
2374                       dxfer_len,
2375                       sense_len,
2376                       sizeof(*scsi_cmd),
2377                       timeout);
2378
2379         scsi_cmd = (struct scsi_request_sense *)&csio->cdb_io.cdb_bytes;
2380         bzero(scsi_cmd, sizeof(*scsi_cmd));
2381         scsi_cmd->opcode = REQUEST_SENSE;
2382         scsi_cmd->length = dxfer_len;
2383 }
2384
2385 void
2386 scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
2387              void (*cbfcnp)(struct cam_periph *, union ccb *),
2388              u_int8_t tag_action, u_int8_t *inq_buf, u_int32_t inq_len,
2389              int evpd, u_int8_t page_code, u_int8_t sense_len,
2390              u_int32_t timeout)
2391 {
2392         struct scsi_inquiry *scsi_cmd;
2393
2394         cam_fill_csio(csio,
2395                       retries,
2396                       cbfcnp,
2397                       /*flags*/CAM_DIR_IN,
2398                       tag_action,
2399                       /*data_ptr*/inq_buf,
2400                       /*dxfer_len*/inq_len,
2401                       sense_len,
2402                       sizeof(*scsi_cmd),
2403                       timeout);
2404
2405         scsi_cmd = (struct scsi_inquiry *)&csio->cdb_io.cdb_bytes;
2406         bzero(scsi_cmd, sizeof(*scsi_cmd));
2407         scsi_cmd->opcode = INQUIRY;
2408         if (evpd) {
2409                 scsi_cmd->byte2 |= SI_EVPD;
2410                 scsi_cmd->page_code = page_code;                
2411         }
2412         /*
2413          * A 'transfer units' count of 256 is coded as
2414          * zero for all commands with a single byte count
2415          * field. 
2416          */
2417         if (inq_len == 256)
2418                 inq_len = 0;
2419         scsi_cmd->length = inq_len;
2420 }
2421
2422 void
2423 scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
2424                 void (*cbfcnp)(struct cam_periph *, union ccb *),
2425                 u_int8_t tag_action, int dbd, u_int8_t page_code,
2426                 u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
2427                 u_int8_t sense_len, u_int32_t timeout)
2428 {
2429         return(scsi_mode_sense_len(csio, retries, cbfcnp, tag_action, dbd,
2430                 page_code, page, param_buf, param_len, 0,
2431                 sense_len, timeout));
2432 }
2433
2434 void
2435 scsi_mode_sense_len(struct ccb_scsiio *csio, u_int32_t retries,
2436                     void (*cbfcnp)(struct cam_periph *, union ccb *),
2437                     u_int8_t tag_action, int dbd, u_int8_t page_code,
2438                     u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
2439                     int minimum_cmd_size, u_int8_t sense_len, u_int32_t timeout)
2440 {
2441         u_int8_t cdb_len;
2442
2443         /*
2444          * Use the smallest possible command to perform the operation.
2445          */
2446         if ((param_len < 256) && (minimum_cmd_size < 10)) {
2447                 /*
2448                  * We can fit in a 6 byte cdb.
2449                  */
2450                 struct scsi_mode_sense_6 *scsi_cmd;
2451
2452                 scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes;
2453                 bzero(scsi_cmd, sizeof(*scsi_cmd));
2454                 scsi_cmd->opcode = MODE_SENSE_6;
2455                 if (dbd != 0)
2456                         scsi_cmd->byte2 |= SMS_DBD;
2457                 scsi_cmd->page = page_code | page;
2458                 scsi_cmd->length = param_len;
2459                 cdb_len = sizeof(*scsi_cmd);
2460         } else {
2461                 /*
2462                  * Need a 10 byte cdb.
2463                  */
2464                 struct scsi_mode_sense_10 *scsi_cmd;
2465
2466                 scsi_cmd = (struct scsi_mode_sense_10 *)&csio->cdb_io.cdb_bytes;
2467                 bzero(scsi_cmd, sizeof(*scsi_cmd));
2468                 scsi_cmd->opcode = MODE_SENSE_10;
2469                 if (dbd != 0)
2470                         scsi_cmd->byte2 |= SMS_DBD;
2471                 scsi_cmd->page = page_code | page;
2472                 scsi_ulto2b(param_len, scsi_cmd->length);
2473                 cdb_len = sizeof(*scsi_cmd);
2474         }
2475         cam_fill_csio(csio,
2476                       retries,
2477                       cbfcnp,
2478                       CAM_DIR_IN,
2479                       tag_action,
2480                       param_buf,
2481                       param_len,
2482                       sense_len,
2483                       cdb_len,
2484                       timeout);
2485 }
2486
2487 void
2488 scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
2489                  void (*cbfcnp)(struct cam_periph *, union ccb *),
2490                  u_int8_t tag_action, int scsi_page_fmt, int save_pages,
2491                  u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
2492                  u_int32_t timeout)
2493 {
2494         return(scsi_mode_select_len(csio, retries, cbfcnp, tag_action,
2495                 scsi_page_fmt, save_pages, param_buf,
2496                 param_len, 0, sense_len, timeout));
2497 }
2498
2499 void
2500 scsi_mode_select_len(struct ccb_scsiio *csio, u_int32_t retries,
2501                     void (*cbfcnp)(struct cam_periph *, union ccb *),
2502                     u_int8_t tag_action, int scsi_page_fmt, int save_pages,
2503                     u_int8_t *param_buf, u_int32_t param_len,
2504                     int minimum_cmd_size, u_int8_t sense_len,
2505                     u_int32_t timeout)
2506 {
2507         u_int8_t cdb_len;
2508
2509         /*
2510          * Use the smallest possible command to perform the operation.
2511          */
2512         if ((param_len < 256) && (minimum_cmd_size < 10)) {
2513                 /*
2514                  * We can fit in a 6 byte cdb.
2515                  */
2516                 struct scsi_mode_select_6 *scsi_cmd;
2517
2518                 scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes;
2519                 bzero(scsi_cmd, sizeof(*scsi_cmd));
2520                 scsi_cmd->opcode = MODE_SELECT_6;
2521                 if (scsi_page_fmt != 0)
2522                         scsi_cmd->byte2 |= SMS_PF;
2523                 if (save_pages != 0)
2524                         scsi_cmd->byte2 |= SMS_SP;
2525                 scsi_cmd->length = param_len;
2526                 cdb_len = sizeof(*scsi_cmd);
2527         } else {
2528                 /*
2529                  * Need a 10 byte cdb.
2530                  */
2531                 struct scsi_mode_select_10 *scsi_cmd;
2532
2533                 scsi_cmd =
2534                     (struct scsi_mode_select_10 *)&csio->cdb_io.cdb_bytes;
2535                 bzero(scsi_cmd, sizeof(*scsi_cmd));
2536                 scsi_cmd->opcode = MODE_SELECT_10;
2537                 if (scsi_page_fmt != 0)
2538                         scsi_cmd->byte2 |= SMS_PF;
2539                 if (save_pages != 0)
2540                         scsi_cmd->byte2 |= SMS_SP;
2541                 scsi_ulto2b(param_len, scsi_cmd->length);
2542                 cdb_len = sizeof(*scsi_cmd);
2543         }
2544         cam_fill_csio(csio,
2545                       retries,
2546                       cbfcnp,
2547                       CAM_DIR_OUT,
2548                       tag_action,
2549                       param_buf,
2550                       param_len,
2551                       sense_len,
2552                       cdb_len,
2553                       timeout);
2554 }
2555
2556 void
2557 scsi_read_capacity_16(struct ccb_scsiio *csio, uint32_t retries,
2558                       void (*cbfcnp)(struct cam_periph *, union ccb *),
2559                       uint8_t tag_action, uint64_t lba, int reladr, int pmi,
2560                       struct scsi_read_capacity_data_long *rcap_buf,
2561                       uint8_t sense_len, uint32_t timeout)
2562 {
2563         struct scsi_read_capacity_16 *scsi_cmd;
2564
2565         cam_fill_csio(csio,
2566                       retries,
2567                       cbfcnp,
2568                       /*flags*/CAM_DIR_IN,
2569                       tag_action,
2570                       /*data_ptr*/(u_int8_t *)rcap_buf,
2571                       /*dxfer_len*/sizeof(*rcap_buf),
2572                       sense_len,
2573                       sizeof(*scsi_cmd),
2574                       timeout);
2575         scsi_cmd = (struct scsi_read_capacity_16 *)&csio->cdb_io.cdb_bytes;
2576         bzero(scsi_cmd, sizeof(*scsi_cmd));
2577         scsi_cmd->opcode = SERVICE_ACTION_IN;
2578         scsi_cmd->service_action = SRC16_SERVICE_ACTION;
2579         scsi_u64to8b(lba, scsi_cmd->addr);
2580         scsi_ulto4b(sizeof(*rcap_buf), scsi_cmd->alloc_len);
2581         if (pmi)
2582                reladr |= SRC16_PMI;
2583         if (reladr)
2584                reladr |= SRC16_RELADR;
2585 }
2586
2587 /* XXX allow specification of address and PMI bit and LBA */
2588 void
2589 scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
2590                    void (*cbfcnp)(struct cam_periph *, union ccb *),
2591                    u_int8_t tag_action,
2592                    struct scsi_read_capacity_data *rcap_buf,
2593                    u_int8_t sense_len, u_int32_t timeout)
2594 {
2595         struct scsi_read_capacity *scsi_cmd;
2596
2597         cam_fill_csio(csio,
2598                       retries,
2599                       cbfcnp,
2600                       /*flags*/CAM_DIR_IN,
2601                       tag_action,
2602                       /*data_ptr*/(u_int8_t *)rcap_buf,
2603                       /*dxfer_len*/sizeof(*rcap_buf),
2604                       sense_len,
2605                       sizeof(*scsi_cmd),
2606                       timeout);
2607
2608         scsi_cmd = (struct scsi_read_capacity *)&csio->cdb_io.cdb_bytes;
2609         bzero(scsi_cmd, sizeof(*scsi_cmd));
2610         scsi_cmd->opcode = READ_CAPACITY;
2611 }
2612
2613 /*
2614  * Prevent or allow the user to remove the media
2615  */
2616 void
2617 scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
2618              void (*cbfcnp)(struct cam_periph *, union ccb *),
2619              u_int8_t tag_action, u_int8_t action,
2620              u_int8_t sense_len, u_int32_t timeout)
2621 {
2622         struct scsi_prevent *scsi_cmd;
2623
2624         cam_fill_csio(csio,
2625                       retries,
2626                       cbfcnp,
2627                       /*flags*/CAM_DIR_NONE,
2628                       tag_action,
2629                       /*data_ptr*/NULL,
2630                       /*dxfer_len*/0,
2631                       sense_len,
2632                       sizeof(*scsi_cmd),
2633                       timeout);
2634
2635         scsi_cmd = (struct scsi_prevent *)&csio->cdb_io.cdb_bytes;
2636         bzero(scsi_cmd, sizeof(*scsi_cmd));
2637         scsi_cmd->opcode = PREVENT_ALLOW;
2638         scsi_cmd->how = action;
2639 }
2640
2641 /*
2642  * Syncronize the media to the contents of the cache for
2643  * the given lba/count pair.  Specifying 0/0 means sync
2644  * the whole cache.
2645  */
2646 void
2647 scsi_synchronize_cache(struct ccb_scsiio *csio, u_int32_t retries,
2648                        void (*cbfcnp)(struct cam_periph *, union ccb *),
2649                        u_int8_t tag_action, u_int32_t begin_lba,
2650                        u_int16_t lb_count, u_int8_t sense_len,
2651                        u_int32_t timeout)
2652 {
2653         struct scsi_sync_cache *scsi_cmd;
2654
2655         cam_fill_csio(csio,
2656                       retries,
2657                       cbfcnp,
2658                       /*flags*/CAM_DIR_NONE,
2659                       tag_action,
2660                       /*data_ptr*/NULL,
2661                       /*dxfer_len*/0,
2662                       sense_len,
2663                       sizeof(*scsi_cmd),
2664                       timeout);
2665
2666         scsi_cmd = (struct scsi_sync_cache *)&csio->cdb_io.cdb_bytes;
2667         bzero(scsi_cmd, sizeof(*scsi_cmd));
2668         scsi_cmd->opcode = SYNCHRONIZE_CACHE;
2669         scsi_ulto4b(begin_lba, scsi_cmd->begin_lba);
2670         scsi_ulto2b(lb_count, scsi_cmd->lb_count);
2671 }
2672
2673 void
2674 scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
2675                 void (*cbfcnp)(struct cam_periph *, union ccb *),
2676                 u_int8_t tag_action, int readop, u_int8_t byte2,
2677                 int minimum_cmd_size, u_int64_t lba, u_int32_t block_count,
2678                 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2679                 u_int32_t timeout)
2680 {
2681         u_int8_t cdb_len;
2682
2683         /*
2684          * Use the smallest possible command to perform the operation
2685          * as some legacy hardware does not support the 10 byte commands.
2686          * If any of the bits in byte2 is set, we have to go with a larger
2687          * command.
2688          */
2689         if ((minimum_cmd_size < 10)
2690          && ((lba & 0x1fffff) == lba)
2691          && ((block_count & 0xff) == block_count)
2692          && (byte2 == 0)) {
2693                 /*
2694                  * We can fit in a 6 byte cdb.
2695                  */
2696                 struct scsi_rw_6 *scsi_cmd;
2697
2698                 scsi_cmd = (struct scsi_rw_6 *)&csio->cdb_io.cdb_bytes;
2699                 scsi_cmd->opcode = readop ? READ_6 : WRITE_6;
2700                 scsi_ulto3b(lba, scsi_cmd->addr);
2701                 scsi_cmd->length = block_count & 0xff;
2702                 scsi_cmd->control = 0;
2703                 cdb_len = sizeof(*scsi_cmd);
2704
2705                 CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
2706                           ("6byte: %x%x%x:%d:%d\n", scsi_cmd->addr[0],
2707                            scsi_cmd->addr[1], scsi_cmd->addr[2],
2708                            scsi_cmd->length, dxfer_len));
2709         } else if ((minimum_cmd_size < 12)
2710                 && ((block_count & 0xffff) == block_count)
2711                 && ((lba & 0xffffffffU) == lba)) {
2712                 /*
2713                  * Need a 10 byte cdb.
2714                  */
2715                 struct scsi_rw_10 *scsi_cmd;
2716
2717                 scsi_cmd = (struct scsi_rw_10 *)&csio->cdb_io.cdb_bytes;
2718                 scsi_cmd->opcode = readop ? READ_10 : WRITE_10;
2719                 scsi_cmd->byte2 = byte2;
2720                 scsi_ulto4b(lba, scsi_cmd->addr);
2721                 scsi_cmd->reserved = 0;
2722                 scsi_ulto2b(block_count, scsi_cmd->length);
2723                 scsi_cmd->control = 0;
2724                 cdb_len = sizeof(*scsi_cmd);
2725
2726                 CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
2727                           ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
2728                            scsi_cmd->addr[1], scsi_cmd->addr[2],
2729                            scsi_cmd->addr[3], scsi_cmd->length[0],
2730                            scsi_cmd->length[1], dxfer_len));
2731         } else if ((minimum_cmd_size < 16)
2732                 && ((block_count & 0xffffffffU) == block_count)
2733                 && ((lba & 0xffffffffU) == lba)) {
2734                 /* 
2735                  * The block count is too big for a 10 byte CDB, use a 12
2736                  * byte CDB.
2737                  */
2738                 struct scsi_rw_12 *scsi_cmd;
2739
2740                 scsi_cmd = (struct scsi_rw_12 *)&csio->cdb_io.cdb_bytes;
2741                 scsi_cmd->opcode = readop ? READ_12 : WRITE_12;
2742                 scsi_cmd->byte2 = byte2;
2743                 scsi_ulto4b(lba, scsi_cmd->addr);
2744                 scsi_cmd->reserved = 0;
2745                 scsi_ulto4b(block_count, scsi_cmd->length);
2746                 scsi_cmd->control = 0;
2747                 cdb_len = sizeof(*scsi_cmd);
2748
2749                 CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
2750                           ("12byte: %x%x%x%x:%x%x%x%x: %d\n", scsi_cmd->addr[0],
2751                            scsi_cmd->addr[1], scsi_cmd->addr[2],
2752                            scsi_cmd->addr[3], scsi_cmd->length[0],
2753                            scsi_cmd->length[1], scsi_cmd->length[2],
2754                            scsi_cmd->length[3], dxfer_len));
2755         } else {
2756                 /*
2757                  * 16 byte CDB.  We'll only get here if the LBA is larger
2758                  * than 2^32, or if the user asks for a 16 byte command.
2759                  */
2760                 struct scsi_rw_16 *scsi_cmd;
2761
2762                 scsi_cmd = (struct scsi_rw_16 *)&csio->cdb_io.cdb_bytes;
2763                 scsi_cmd->opcode = readop ? READ_16 : WRITE_16;
2764                 scsi_cmd->byte2 = byte2;
2765                 scsi_u64to8b(lba, scsi_cmd->addr);
2766                 scsi_cmd->reserved = 0;
2767                 scsi_ulto4b(block_count, scsi_cmd->length);
2768                 scsi_cmd->control = 0;
2769                 cdb_len = sizeof(*scsi_cmd);
2770         }
2771         cam_fill_csio(csio,
2772                       retries,
2773                       cbfcnp,
2774                       /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,
2775                       tag_action,
2776                       data_ptr,
2777                       dxfer_len,
2778                       sense_len,
2779                       cdb_len,
2780                       timeout);
2781 }
2782
2783 void 
2784 scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
2785                 void (*cbfcnp)(struct cam_periph *, union ccb *),
2786                 u_int8_t tag_action, int start, int load_eject,
2787                 int immediate, u_int8_t sense_len, u_int32_t timeout)
2788 {
2789         struct scsi_start_stop_unit *scsi_cmd;
2790         int extra_flags = 0;
2791
2792         scsi_cmd = (struct scsi_start_stop_unit *)&csio->cdb_io.cdb_bytes;
2793         bzero(scsi_cmd, sizeof(*scsi_cmd));
2794         scsi_cmd->opcode = START_STOP_UNIT;
2795         if (start != 0) {
2796                 scsi_cmd->how |= SSS_START;
2797                 /* it takes a lot of power to start a drive */
2798                 extra_flags |= CAM_HIGH_POWER;
2799         }
2800         if (load_eject != 0)
2801                 scsi_cmd->how |= SSS_LOEJ;
2802         if (immediate != 0)
2803                 scsi_cmd->byte2 |= SSS_IMMED;
2804
2805         cam_fill_csio(csio,
2806                       retries,
2807                       cbfcnp,
2808                       /*flags*/CAM_DIR_NONE | extra_flags,
2809                       tag_action,
2810                       /*data_ptr*/NULL,
2811                       /*dxfer_len*/0,
2812                       sense_len,
2813                       sizeof(*scsi_cmd),
2814                       timeout);
2815
2816 }
2817
2818 void
2819 scsi_log_sense(struct ccb_scsiio *csio, u_int32_t retries,
2820                void (*cbfcnp)(struct cam_periph *, union ccb *),
2821                u_int8_t tag_action, u_int8_t page_code, u_int8_t page,
2822                int save_pages, int ppc, u_int32_t paramptr,
2823                u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
2824                u_int32_t timeout)
2825 {
2826         struct scsi_log_sense *scsi_cmd;
2827         u_int8_t cdb_len;
2828
2829         scsi_cmd = (struct scsi_log_sense *)&csio->cdb_io.cdb_bytes;
2830         bzero(scsi_cmd, sizeof(*scsi_cmd));
2831         scsi_cmd->opcode = LOG_SENSE;
2832         scsi_cmd->page = page_code | page;
2833         if (save_pages != 0)
2834                 scsi_cmd->byte2 |= SLS_SP;
2835         if (ppc != 0)
2836                 scsi_cmd->byte2 |= SLS_PPC;
2837         scsi_ulto2b(paramptr, scsi_cmd->paramptr);
2838         scsi_ulto2b(param_len, scsi_cmd->length);
2839         cdb_len = sizeof(*scsi_cmd);
2840
2841         cam_fill_csio(csio,
2842                       retries,
2843                       cbfcnp,
2844                       /*flags*/CAM_DIR_IN,
2845                       tag_action,
2846                       /*data_ptr*/param_buf,
2847                       /*dxfer_len*/param_len,
2848                       sense_len,
2849                       cdb_len,
2850                       timeout);
2851 }
2852
2853 void
2854 scsi_log_select(struct ccb_scsiio *csio, u_int32_t retries,
2855                 void (*cbfcnp)(struct cam_periph *, union ccb *),
2856                 u_int8_t tag_action, u_int8_t page_code, int save_pages,
2857                 int pc_reset, u_int8_t *param_buf, u_int32_t param_len,
2858                 u_int8_t sense_len, u_int32_t timeout)
2859 {
2860         struct scsi_log_select *scsi_cmd;
2861         u_int8_t cdb_len;
2862
2863         scsi_cmd = (struct scsi_log_select *)&csio->cdb_io.cdb_bytes;
2864         bzero(scsi_cmd, sizeof(*scsi_cmd));
2865         scsi_cmd->opcode = LOG_SELECT;
2866         scsi_cmd->page = page_code & SLS_PAGE_CODE;
2867         if (save_pages != 0)
2868                 scsi_cmd->byte2 |= SLS_SP;
2869         if (pc_reset != 0)
2870                 scsi_cmd->byte2 |= SLS_PCR;
2871         scsi_ulto2b(param_len, scsi_cmd->length);
2872         cdb_len = sizeof(*scsi_cmd);
2873
2874         cam_fill_csio(csio,
2875                       retries,
2876                       cbfcnp,
2877                       /*flags*/CAM_DIR_OUT,
2878                       tag_action,
2879                       /*data_ptr*/param_buf,
2880                       /*dxfer_len*/param_len,
2881                       sense_len,
2882                       cdb_len,
2883                       timeout);
2884 }
2885
2886 /*      
2887  * Try make as good a match as possible with
2888  * available sub drivers
2889  */
2890 int
2891 scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
2892 {
2893         struct scsi_inquiry_pattern *entry;
2894         struct scsi_inquiry_data *inq;
2895  
2896         entry = (struct scsi_inquiry_pattern *)table_entry;
2897         inq = (struct scsi_inquiry_data *)inqbuffer;
2898
2899         if (((SID_TYPE(inq) == entry->type)
2900           || (entry->type == T_ANY))
2901          && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
2902                                    : entry->media_type & SIP_MEDIA_FIXED)
2903          && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
2904          && (cam_strmatch(inq->product, entry->product,
2905                           sizeof(inq->product)) == 0)
2906          && (cam_strmatch(inq->revision, entry->revision,
2907                           sizeof(inq->revision)) == 0)) {
2908                 return (0);
2909         }
2910         return (-1);
2911 }
2912
2913 /*      
2914  * Try make as good a match as possible with
2915  * available sub drivers
2916  */
2917 int
2918 scsi_static_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
2919 {
2920         struct scsi_static_inquiry_pattern *entry;
2921         struct scsi_inquiry_data *inq;
2922  
2923         entry = (struct scsi_static_inquiry_pattern *)table_entry;
2924         inq = (struct scsi_inquiry_data *)inqbuffer;
2925
2926         if (((SID_TYPE(inq) == entry->type)
2927           || (entry->type == T_ANY))
2928          && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
2929                                    : entry->media_type & SIP_MEDIA_FIXED)
2930          && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
2931          && (cam_strmatch(inq->product, entry->product,
2932                           sizeof(inq->product)) == 0)
2933          && (cam_strmatch(inq->revision, entry->revision,
2934                           sizeof(inq->revision)) == 0)) {
2935                 return (0);
2936         }
2937         return (-1);
2938 }