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