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