Add interfaces for SCSI LOG SELECT and LOG SENSE commands.
[dragonfly.git] / sys / bus / cam / scsi / scsi_all.h
1 /*
2  * Largely written by Julian Elischer (julian@tfs.com)
3  * for TRW Financial Systems.
4  *
5  * TRW Financial Systems, in accordance with their agreement with Carnegie
6  * Mellon University, makes this software available to CMU to distribute
7  * or use in any manner that they see fit as long as this message is kept with
8  * the software. For this reason TFS also grants any other persons or
9  * organisations permission to use or modify this software.
10  *
11  * TFS supplies this software to be publicly redistributed
12  * on the understanding that TFS is not responsible for the correct
13  * functioning of this software in any circumstances.
14  *
15  * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
16  *
17  * $FreeBSD: src/sys/cam/scsi/scsi_all.h,v 1.14.2.5 2003/08/24 03:26:37 ken Exp $
18  * $DragonFly: src/sys/bus/cam/scsi/scsi_all.h,v 1.9 2007/11/22 16:34:25 pavalos Exp $
19  */
20
21 /*
22  * SCSI general  interface description
23  */
24
25 #ifndef _SCSI_SCSI_ALL_H
26 #define _SCSI_SCSI_ALL_H 1
27
28 #ifndef _SYS_CDEFS_H_
29 #include <sys/cdefs.h>
30 #endif
31 #if !defined(_KERNEL) && !defined(_STDIO_H_)
32 #include <stdio.h>      /* FILE for userland protos */
33 #endif
34
35 #ifdef _KERNEL
36 #include "opt_scsi.h"
37 /*
38  * This is the number of seconds we wait for devices to settle after a SCSI
39  * bus reset.
40  */
41 #ifndef SCSI_DELAY
42 #define SCSI_DELAY 2000
43 #endif
44 /*
45  * If someone sets this to 0, we assume that they want the minimum
46  * allowable bus settle delay.  All devices need _some_ sort of bus settle
47  * delay, so we'll set it to a minimum value of 100ms.
48  */
49 #if (SCSI_DELAY == 0)
50 #undef SCSI_DELAY
51 #define SCSI_DELAY 100
52 #endif
53
54 /*
55  * Make sure the user isn't using seconds instead of milliseconds.
56  */
57 #if (SCSI_DELAY < 100)
58 #error "SCSI_DELAY is in milliseconds, not seconds!  Please use a larger value"
59 #endif
60 #endif /* _KERNEL */
61
62 /*
63  * SCSI command format
64  */
65
66 /*
67  * Define dome bits that are in ALL (or a lot of) scsi commands
68  */
69 #define SCSI_CTL_LINK           0x01
70 #define SCSI_CTL_FLAG           0x02
71 #define SCSI_CTL_VENDOR         0xC0
72 #define SCSI_CMD_LUN            0xA0    /* these two should not be needed */
73 #define SCSI_CMD_LUN_SHIFT      5       /* LUN in the cmd is no longer SCSI */
74
75 #define SCSI_MAX_CDBLEN         16      /* 
76                                          * 16 byte commands are in the 
77                                          * SCSI-3 spec 
78                                          */
79 #if defined(CAM_MAX_CDBLEN) && (CAM_MAX_CDBLEN < SCSI_MAX_CDBLEN)
80 #error "CAM_MAX_CDBLEN cannot be less than SCSI_MAX_CDBLEN"
81 #endif
82
83 /* 6byte CDBs special case 0 length to be 256 */
84 #define SCSI_CDB6_LEN(len)      ((len) == 0 ? 256 : len)
85
86 /*
87  * This type defines actions to be taken when a particular sense code is
88  * received.  Right now, these flags are only defined to take up 16 bits,
89  * but can be expanded in the future if necessary.
90  */
91 typedef enum {
92         SS_NOP      = 0x000000, /* Do nothing */
93         SS_RETRY    = 0x010000, /* Retry the command */
94         SS_FAIL     = 0x020000, /* Bail out */
95         SS_START    = 0x030000, /* Send a Start Unit command to the device,
96                                  * then retry the original command.
97                                  */
98         SS_TUR      = 0x040000, /* Send a Test Unit Ready command to the
99                                  * device, then retry the original command.
100                                  */
101         SS_REQSENSE = 0x050000, /* Send a RequestSense command to the
102                                  * device, then retry the original command.
103                                  */
104         SS_MASK     = 0xff0000
105 } scsi_sense_action;
106
107 typedef enum {
108         SSQ_NONE                = 0x0000,
109         SSQ_DECREMENT_COUNT     = 0x0100,  /* Decrement the retry count */
110         SSQ_MANY                = 0x0200,  /* send lots of recovery commands */
111         SSQ_RANGE               = 0x0400,  /*
112                                             * This table entry represents the
113                                             * end of a range of ASCQs that
114                                             * have identical error actions
115                                             * and text.
116                                             */
117         SSQ_PRINT_SENSE         = 0x0800,
118         SSQ_MASK                = 0xff00
119 } scsi_sense_action_qualifier;
120
121 /* Mask for error status values */
122 #define SS_ERRMASK      0xff
123
124 /* The default, retyable, error action */
125 #define SS_RDEF         SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE|EIO
126
127 /* The retyable, error action, with table specified error code */
128 #define SS_RET          SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE
129
130 /* Fatal error action, with table specified error code */
131 #define SS_FATAL        SS_FAIL|SSQ_PRINT_SENSE
132
133 struct scsi_generic
134 {
135         u_int8_t opcode;
136         u_int8_t bytes[11];
137 };
138
139 struct scsi_request_sense
140 {
141         u_int8_t opcode;
142         u_int8_t byte2;
143         u_int8_t unused[2];
144         u_int8_t length;
145         u_int8_t control;
146 };
147
148 struct scsi_test_unit_ready
149 {
150         u_int8_t opcode;
151         u_int8_t byte2;
152         u_int8_t unused[3];
153         u_int8_t control;
154 };
155
156 struct scsi_send_diag
157 {
158         u_int8_t opcode;
159         u_int8_t byte2;
160 #define SSD_UOL         0x01
161 #define SSD_DOL         0x02
162 #define SSD_SELFTEST    0x04
163 #define SSD_PF          0x10
164         u_int8_t unused[1];
165         u_int8_t paramlen[2];
166         u_int8_t control;
167 };
168
169 struct scsi_sense
170 {
171         u_int8_t opcode;
172         u_int8_t byte2;
173         u_int8_t unused[2];
174         u_int8_t length;
175         u_int8_t control;
176 };
177
178 struct scsi_inquiry
179 {
180         u_int8_t opcode;
181         u_int8_t byte2;
182 #define SI_EVPD 0x01
183         u_int8_t page_code;
184         u_int8_t reserved;
185         u_int8_t length;
186         u_int8_t control;
187 };
188
189 struct scsi_mode_sense_6
190 {
191         u_int8_t opcode;
192         u_int8_t byte2;
193 #define SMS_DBD                         0x08
194         u_int8_t page;
195 #define SMS_PAGE_CODE                   0x3F
196 #define SMS_VENDOR_SPECIFIC_PAGE        0x00
197 #define SMS_DISCONNECT_RECONNECT_PAGE   0x02
198 #define SMS_PERIPHERAL_DEVICE_PAGE      0x09
199 #define SMS_CONTROL_MODE_PAGE           0x0A
200 #define SMS_ALL_PAGES_PAGE              0x3F
201 #define SMS_PAGE_CTRL_MASK              0xC0
202 #define SMS_PAGE_CTRL_CURRENT           0x00
203 #define SMS_PAGE_CTRL_CHANGEABLE        0x40
204 #define SMS_PAGE_CTRL_DEFAULT           0x80
205 #define SMS_PAGE_CTRL_SAVED             0xC0
206         u_int8_t unused;
207         u_int8_t length;
208         u_int8_t control;
209 };
210
211 struct scsi_mode_sense_10
212 {
213         u_int8_t opcode;
214         u_int8_t byte2;         /* same bits as small version */
215         u_int8_t page;          /* same bits as small version */
216         u_int8_t unused[4];
217         u_int8_t length[2];
218         u_int8_t control;
219 };
220
221 struct scsi_mode_select_6
222 {
223         u_int8_t opcode;
224         u_int8_t byte2;
225 #define SMS_SP  0x01
226 #define SMS_PF  0x10
227         u_int8_t unused[2];
228         u_int8_t length;
229         u_int8_t control;
230 };
231
232 struct scsi_mode_select_10
233 {
234         u_int8_t opcode;
235         u_int8_t byte2;         /* same bits as small version */
236         u_int8_t unused[5];
237         u_int8_t length[2];
238         u_int8_t control;
239 };
240
241 /*
242  * When sending a mode select to a tape drive, the medium type must be 0.
243  */
244 struct scsi_mode_hdr_6
245 {
246         u_int8_t datalen;
247         u_int8_t medium_type;
248         u_int8_t dev_specific;
249         u_int8_t block_descr_len;
250 };
251
252 struct scsi_mode_hdr_10
253 {
254         u_int8_t datalen[2];
255         u_int8_t medium_type;
256         u_int8_t dev_specific;
257         u_int8_t reserved[2];
258         u_int8_t block_descr_len[2];
259 };
260
261 struct scsi_mode_block_descr
262 {
263         u_int8_t density_code;
264         u_int8_t num_blocks[3];
265         u_int8_t reserved;
266         u_int8_t block_len[3];
267 };
268
269 struct scsi_log_sense
270 {
271         u_int8_t opcode;
272         u_int8_t byte2;
273 #define SLS_SP                          0x01
274 #define SLS_PPC                         0x02
275         u_int8_t page;
276 #define SLS_PAGE_CODE                   0x3F
277 #define SLS_ALL_PAGES_PAGE              0x00
278 #define SLS_OVERRUN_PAGE                0x01
279 #define SLS_ERROR_WRITE_PAGE            0x02
280 #define SLS_ERROR_READ_PAGE             0x03
281 #define SLS_ERROR_READREVERSE_PAGE      0x04
282 #define SLS_ERROR_VERIFY_PAGE           0x05
283 #define SLS_ERROR_NONMEDIUM_PAGE        0x06
284 #define SLS_ERROR_LASTN_PAGE            0x07
285 #define SLS_PAGE_CTRL_MASK              0xC0
286 #define SLS_PAGE_CTRL_THRESHOLD         0x00
287 #define SLS_PAGE_CTRL_CUMULATIVE        0x40
288 #define SLS_PAGE_CTRL_THRESH_DEFAULT    0x80
289 #define SLS_PAGE_CTRL_CUMUL_DEFAULT     0xC0
290         u_int8_t reserved[2];
291         u_int8_t paramptr[2];
292         u_int8_t length[2];
293         u_int8_t control;
294 };
295
296 struct scsi_log_select
297 {
298         u_int8_t opcode;
299         u_int8_t byte2;
300 /*      SLS_SP                          0x01 */
301 #define SLS_PCR                         0x02
302         u_int8_t page;
303 /*      SLS_PAGE_CTRL_MASK              0xC0 */
304 /*      SLS_PAGE_CTRL_THRESHOLD         0x00 */
305 /*      SLS_PAGE_CTRL_CUMULATIVE        0x40 */
306 /*      SLS_PAGE_CTRL_THRESH_DEFAULT    0x80 */
307 /*      SLS_PAGE_CTRL_CUMUL_DEFAULT     0xC0 */
308         u_int8_t reserved[4];
309         u_int8_t length[2];
310         u_int8_t control;
311 };
312
313 struct scsi_log_header
314 {
315         u_int8_t page;
316         u_int8_t reserved;
317         u_int8_t datalen[2];
318 };
319
320 struct scsi_log_param_header {
321         u_int8_t param_code[2];
322         u_int8_t param_control;
323 #define SLP_LP                          0x01
324 #define SLP_LBIN                        0x02
325 #define SLP_TMC_MASK                    0x0C
326 #define SLP_TMC_ALWAYS                  0x00
327 #define SLP_TMC_EQUAL                   0x04
328 #define SLP_TMC_NOTEQUAL                0x08
329 #define SLP_TMC_GREATER                 0x0C
330 #define SLP_ETC                         0x10
331 #define SLP_TSD                         0x20
332 #define SLP_DS                          0x40
333 #define SLP_DU                          0x80
334         u_int8_t param_len;
335 };
336
337 struct scsi_control_page {
338         u_int8_t page_code;
339         u_int8_t page_length;
340         u_int8_t rlec;
341 #define SCB_RLEC                        0x01    /*Report Log Exception Cond*/
342         u_int8_t queue_flags;
343 #define SCP_QUEUE_ALG_MASK              0xF0
344 #define SCP_QUEUE_ALG_RESTRICTED        0x00
345 #define SCP_QUEUE_ALG_UNRESTRICTED      0x10
346 #define SCP_QUEUE_ERR                   0x02    /*Queued I/O aborted for CACs*/
347 #define SCP_QUEUE_DQUE                  0x01    /*Queued I/O disabled*/
348         u_int8_t eca_and_aen;
349 #define SCP_EECA                        0x80    /*Enable Extended CA*/
350 #define SCP_RAENP                       0x04    /*Ready AEN Permission*/
351 #define SCP_UAAENP                      0x02    /*UA AEN Permission*/
352 #define SCP_EAENP                       0x01    /*Error AEN Permission*/
353         u_int8_t reserved;
354         u_int8_t aen_holdoff_period[2];
355 };
356
357 struct scsi_reserve
358 {
359         u_int8_t opcode;
360         u_int8_t byte2;
361         u_int8_t unused[2];
362         u_int8_t length;
363         u_int8_t control;
364 };
365
366 struct scsi_release
367 {
368         u_int8_t opcode;
369         u_int8_t byte2;
370         u_int8_t unused[2];
371         u_int8_t length;
372         u_int8_t control;
373 };
374
375 struct scsi_prevent
376 {
377         u_int8_t opcode;
378         u_int8_t byte2;
379         u_int8_t unused[2];
380         u_int8_t how;
381         u_int8_t control;
382 };
383 #define PR_PREVENT 0x01
384 #define PR_ALLOW   0x00
385
386 struct scsi_sync_cache
387 {
388         u_int8_t opcode;
389         u_int8_t byte2;
390         u_int8_t begin_lba[4];
391         u_int8_t reserved;
392         u_int8_t lb_count[2];
393         u_int8_t control;       
394 };
395
396
397 struct scsi_changedef
398 {
399         u_int8_t opcode;
400         u_int8_t byte2;
401         u_int8_t unused1;
402         u_int8_t how;
403         u_int8_t unused[4];
404         u_int8_t datalen;
405         u_int8_t control;
406 };
407
408 struct scsi_read_buffer
409 {
410         u_int8_t opcode;
411         u_int8_t byte2;
412 #define RWB_MODE                0x07
413 #define RWB_MODE_HDR_DATA       0x00
414 #define RWB_MODE_DATA           0x02
415 #define RWB_MODE_DOWNLOAD       0x04
416 #define RWB_MODE_DOWNLOAD_SAVE  0x05
417         u_int8_t buffer_id;
418         u_int8_t offset[3];
419         u_int8_t length[3];
420         u_int8_t control;
421 };
422
423 struct scsi_write_buffer
424 {
425         u_int8_t opcode;
426         u_int8_t byte2;
427         u_int8_t buffer_id;
428         u_int8_t offset[3];
429         u_int8_t length[3];
430         u_int8_t control;
431 };
432
433 struct scsi_rw_6
434 {
435         u_int8_t opcode;
436         u_int8_t addr[3];
437 /* only 5 bits are valid in the MSB address byte */
438 #define SRW_TOPADDR     0x1F
439         u_int8_t length;
440         u_int8_t control;
441 };
442
443 struct scsi_rw_10
444 {
445         u_int8_t opcode;
446 #define SRW10_RELADDR   0x01
447 /* EBP defined for WRITE(10) only */
448 #define SRW10_EBP       0x04
449 #define SRW10_FUA       0x08
450 #define SRW10_DPO       0x10
451         u_int8_t byte2;
452         u_int8_t addr[4];
453         u_int8_t reserved;
454         u_int8_t length[2];
455         u_int8_t control;
456 };
457
458 struct scsi_rw_12
459 {
460         u_int8_t opcode;
461 #define SRW12_RELADDR   0x01
462 #define SRW12_FUA       0x08
463 #define SRW12_DPO       0x10
464         u_int8_t byte2;
465         u_int8_t addr[4];
466         u_int8_t length[4];
467         u_int8_t reserved;
468         u_int8_t control;
469 };
470
471 struct scsi_rw_16
472 {
473         u_int8_t opcode;
474 #define        SRW16_RELADDR   0x01
475 #define        SRW16_FUA       0x08
476 #define        SRW16_DPO       0x10
477         u_int8_t byte2;
478         u_int8_t addr[8];
479         u_int8_t length[4];
480         u_int8_t reserved;
481         u_int8_t control;
482 };
483
484
485 struct scsi_start_stop_unit
486 {
487         u_int8_t opcode;
488         u_int8_t byte2;
489 #define SSS_IMMED               0x01
490         u_int8_t reserved[2];
491         u_int8_t how;
492 #define SSS_START               0x01
493 #define SSS_LOEJ                0x02
494         u_int8_t control;
495 };
496
497 #define SC_SCSI_1 0x01
498 #define SC_SCSI_2 0x03
499
500 /*
501  * Opcodes
502  */
503
504 #define TEST_UNIT_READY         0x00
505 #define REQUEST_SENSE           0x03
506 #define READ_6                  0x08
507 #define WRITE_6                 0x0a
508 #define INQUIRY                 0x12
509 #define MODE_SELECT_6           0x15
510 #define MODE_SENSE_6            0x1a
511 #define START_STOP_UNIT         0x1b
512 #define START_STOP              0x1b
513 #define RESERVE                 0x16
514 #define RELEASE                 0x17
515 #define RECEIVE_DIAGNOSTIC      0x1c
516 #define SEND_DIAGNOSTIC         0x1d
517 #define PREVENT_ALLOW           0x1e
518 #define READ_CAPACITY           0x25
519 #define READ_10                 0x28
520 #define WRITE_10                0x2a
521 #define POSITION_TO_ELEMENT     0x2b
522 #define SYNCHRONIZE_CACHE       0x35
523 #define WRITE_BUFFER            0x3b
524 #define READ_BUFFER             0x3c
525 #define CHANGE_DEFINITION       0x40
526 #define LOG_SELECT              0x4c
527 #define LOG_SENSE               0x4d
528 #define MODE_SELECT_10          0x55
529 #define MODE_SENSE_10           0x5A
530 #define READ_16                 0x88
531 #define WRITE_16                0x8a
532 #define SERVICE_ACTION_IN       0x9e
533 #define MOVE_MEDIUM             0xa5
534 #define READ_12                 0xa8
535 #define WRITE_12                0xaa
536 #define READ_ELEMENT_STATUS     0xb8
537
538 /*
539  * Device Types
540  */
541 #define T_DIRECT        0x00
542 #define T_SEQUENTIAL    0x01
543 #define T_PRINTER       0x02
544 #define T_PROCESSOR     0x03
545 #define T_WORM          0x04
546 #define T_CDROM         0x05
547 #define T_SCANNER       0x06
548 #define T_OPTICAL       0x07
549 #define T_CHANGER       0x08
550 #define T_COMM          0x09
551 #define T_ASC0          0x0a
552 #define T_ASC1          0x0b
553 #define T_STORARRAY     0x0c
554 #define T_ENCLOSURE     0x0d
555 #define T_RBC           0x0e
556 #define T_OCRW          0x0f
557 #define T_NODEVICE      0x1F
558 #define T_ANY           0xFF    /* Used in Quirk table matches */
559
560 #define T_REMOV         1
561 #define T_FIXED         0
562
563 /*
564  * This length is the initial inquiry length used by the probe code, as    
565  * well as the legnth necessary for scsi_print_inquiry() to function 
566  * correctly.  If either use requires a different length in the future, 
567  * the two values should be de-coupled.
568  */
569 #define SHORT_INQUIRY_LENGTH    36
570
571 struct scsi_inquiry_data
572 {
573         u_int8_t device;
574 #define SID_TYPE(inq_data) ((inq_data)->device & 0x1f)
575 #define SID_QUAL(inq_data) (((inq_data)->device & 0xE0) >> 5)
576 #define SID_QUAL_LU_CONNECTED   0x00    /*
577                                          * The specified peripheral device
578                                          * type is currently connected to
579                                          * logical unit.  If the target cannot
580                                          * determine whether or not a physical
581                                          * device is currently connected, it
582                                          * shall also use this peripheral
583                                          * qualifier when returning the INQUIRY
584                                          * data.  This peripheral qualifier
585                                          * does not mean that the device is
586                                          * ready for access by the initiator.
587                                          */
588 #define SID_QUAL_LU_OFFLINE     0x01    /*
589                                          * The target is capable of supporting
590                                          * the specified peripheral device type
591                                          * on this logical unit; however, the
592                                          * physical device is not currently
593                                          * connected to this logical unit.
594                                          */
595 #define SID_QUAL_RSVD           0x02
596 #define SID_QUAL_BAD_LU         0x03    /*
597                                          * The target is not capable of
598                                          * supporting a physical device on
599                                          * this logical unit. For this
600                                          * peripheral qualifier the peripheral
601                                          * device type shall be set to 1Fh to
602                                          * provide compatibility with previous
603                                          * versions of SCSI. All other
604                                          * peripheral device type values are
605                                          * reserved for this peripheral
606                                          * qualifier.
607                                          */
608 #define SID_QUAL_IS_VENDOR_UNIQUE(inq_data) ((SID_QUAL(inq_data) & 0x08) != 0)
609         u_int8_t dev_qual2;
610 #define SID_QUAL2       0x7F
611 #define SID_IS_REMOVABLE(inq_data) (((inq_data)->dev_qual2 & 0x80) != 0)
612         u_int8_t version;
613 #define SID_ANSI_REV(inq_data) ((inq_data)->version & 0x07)
614 #define         SCSI_REV_0              0
615 #define         SCSI_REV_CCS            1
616 #define         SCSI_REV_2              2
617 #define         SCSI_REV_SPC            3
618 #define         SCSI_REV_SPC2           4
619
620 #define SID_ECMA        0x38
621 #define SID_ISO         0xC0
622         u_int8_t response_format;
623 #define SID_AENC        0x80
624 #define SID_TrmIOP      0x40
625         u_int8_t additional_length;
626         u_int8_t reserved[2];
627         u_int8_t flags;
628 #define SID_SftRe       0x01
629 #define SID_CmdQue      0x02
630 #define SID_Linked      0x08
631 #define SID_Sync        0x10
632 #define SID_WBus16      0x20
633 #define SID_WBus32      0x40
634 #define SID_RelAdr      0x80
635 #define SID_VENDOR_SIZE   8
636         char     vendor[SID_VENDOR_SIZE];
637 #define SID_PRODUCT_SIZE  16
638         char     product[SID_PRODUCT_SIZE];
639 #define SID_REVISION_SIZE 4
640         char     revision[SID_REVISION_SIZE];
641         /*
642          * The following fields were taken from SCSI Primary Commands - 2
643          * (SPC-2) Revision 14, Dated 11 November 1999
644          */
645 #define SID_VENDOR_SPECIFIC_0_SIZE      20
646         u_int8_t vendor_specific0[SID_VENDOR_SPECIFIC_0_SIZE];
647         /*
648          * An extension of SCSI Parallel Specific Values
649          */
650 #define SID_SPI_IUS             0x01
651 #define SID_SPI_QAS             0x02
652 #define SID_SPI_CLOCK_ST        0x00
653 #define SID_SPI_CLOCK_DT        0x04
654 #define SID_SPI_CLOCK_DT_ST     0x0C
655 #define SID_SPI_MASK            0x0F
656         u_int8_t spi3data;
657         u_int8_t reserved2;
658         /*
659          * Version Descriptors, stored 2 byte values.
660          */
661         u_int8_t version1[2];
662         u_int8_t version2[2];
663         u_int8_t version3[2];
664         u_int8_t version4[2];
665         u_int8_t version5[2];
666         u_int8_t version6[2];
667         u_int8_t version7[2];
668         u_int8_t version8[2];
669
670         u_int8_t reserved3[22];
671
672 #define SID_VENDOR_SPECIFIC_1_SIZE      160
673         u_int8_t vendor_specific1[SID_VENDOR_SPECIFIC_1_SIZE];
674 };
675
676 struct scsi_vpd_unit_serial_number
677 {
678         u_int8_t device;
679         u_int8_t page_code;
680 #define SVPD_UNIT_SERIAL_NUMBER 0x80
681         u_int8_t reserved;
682         u_int8_t length; /* serial number length */
683 #define SVPD_SERIAL_NUM_SIZE 251
684         u_int8_t serial_num[SVPD_SERIAL_NUM_SIZE];
685 };
686
687 struct scsi_read_capacity
688 {
689         u_int8_t opcode;
690         u_int8_t byte2;
691         u_int8_t addr[4];
692         u_int8_t unused[3];
693         u_int8_t control;
694 };
695
696 struct scsi_read_capacity_16
697 {
698         uint8_t opcode;
699 #define        SRC16_SERVICE_ACTION    0x10
700         uint8_t service_action;
701         uint8_t addr[8];
702         uint8_t alloc_len[4];
703 #define        SRC16_PMI               0x01
704 #define        SRC16_RELADR            0x02
705         uint8_t reladr;
706         uint8_t control;
707 };
708
709 struct scsi_read_capacity_data
710 {
711         u_int8_t addr[4];
712         u_int8_t length[4];
713 };
714
715 struct scsi_read_capacity_data_long
716 {
717         uint8_t addr[8];
718         uint8_t length[4];
719 };
720
721 struct scsi_sense_data
722 {
723         u_int8_t error_code;
724 #define SSD_ERRCODE                     0x7F
725 #define         SSD_CURRENT_ERROR       0x70
726 #define         SSD_DEFERRED_ERROR      0x71
727 #define SSD_ERRCODE_VALID       0x80    
728         u_int8_t segment;
729         u_int8_t flags;
730 #define SSD_KEY                         0x0F
731 #define         SSD_KEY_NO_SENSE        0x00
732 #define         SSD_KEY_RECOVERED_ERROR 0x01
733 #define         SSD_KEY_NOT_READY       0x02
734 #define         SSD_KEY_MEDIUM_ERROR    0x03
735 #define         SSD_KEY_HARDWARE_ERROR  0x04
736 #define         SSD_KEY_ILLEGAL_REQUEST 0x05
737 #define         SSD_KEY_UNIT_ATTENTION  0x06
738 #define         SSD_KEY_DATA_PROTECT    0x07
739 #define         SSD_KEY_BLANK_CHECK     0x08
740 #define         SSD_KEY_Vendor_Specific 0x09
741 #define         SSD_KEY_COPY_ABORTED    0x0a
742 #define         SSD_KEY_ABORTED_COMMAND 0x0b            
743 #define         SSD_KEY_EQUAL           0x0c
744 #define         SSD_KEY_VOLUME_OVERFLOW 0x0d
745 #define         SSD_KEY_MISCOMPARE      0x0e
746 #define         SSD_KEY_RESERVED        0x0f                    
747 #define SSD_ILI         0x20
748 #define SSD_EOM         0x40
749 #define SSD_FILEMARK    0x80
750         u_int8_t info[4];
751         u_int8_t extra_len;
752         u_int8_t cmd_spec_info[4];
753         u_int8_t add_sense_code;
754         u_int8_t add_sense_code_qual;
755         u_int8_t fru;
756         u_int8_t sense_key_spec[3];
757 #define SSD_SCS_VALID           0x80
758 #define SSD_FIELDPTR_CMD        0x40
759 #define SSD_BITPTR_VALID        0x08
760 #define SSD_BITPTR_VALUE        0x07
761 #define SSD_MIN_SIZE 18
762         u_int8_t extra_bytes[14];
763 #define SSD_FULL_SIZE sizeof(struct scsi_sense_data)
764 };
765
766 struct scsi_mode_header_6
767 {
768         u_int8_t data_length;   /* Sense data length */
769         u_int8_t medium_type;
770         u_int8_t dev_spec;
771         u_int8_t blk_desc_len;
772 };
773
774 struct scsi_mode_header_10
775 {
776         u_int8_t data_length[2];/* Sense data length */
777         u_int8_t medium_type;
778         u_int8_t dev_spec;
779         u_int8_t unused[2];
780         u_int8_t blk_desc_len[2];
781 };
782
783 struct scsi_mode_page_header
784 {
785         u_int8_t page_code;
786         u_int8_t page_length;
787 };
788
789 struct scsi_mode_blk_desc
790 {
791         u_int8_t density;
792         u_int8_t nblocks[3];
793         u_int8_t reserved;
794         u_int8_t blklen[3];
795 };
796
797 #define SCSI_DEFAULT_DENSITY    0x00    /* use 'default' density */
798 #define SCSI_SAME_DENSITY       0x7f    /* use 'same' density- >= SCSI-2 only */
799 /*
800  * Status Byte
801  */
802 #define SCSI_STATUS_OK                  0x00
803 #define SCSI_STATUS_CHECK_COND          0x02
804 #define SCSI_STATUS_COND_MET            0x04
805 #define SCSI_STATUS_BUSY                0x08
806 #define SCSI_STATUS_INTERMED            0x10
807 #define SCSI_STATUS_INTERMED_COND_MET   0x14
808 #define SCSI_STATUS_RESERV_CONFLICT     0x18
809 #define SCSI_STATUS_CMD_TERMINATED      0x22    /* Obsolete in SAM-2 */
810 #define SCSI_STATUS_QUEUE_FULL          0x28
811 #define SCSI_STATUS_ACA_ACTIVE          0x30
812 #define SCSI_STATUS_TASK_ABORTED        0x40
813
814 struct scsi_inquiry_pattern {
815         u_int8_t   type;
816         u_int8_t   media_type;
817 #define SIP_MEDIA_REMOVABLE     0x01
818 #define SIP_MEDIA_FIXED         0x02
819         const char *vendor;
820         const char *product;
821         const char *revision;
822 }; 
823
824 struct scsi_static_inquiry_pattern {
825         u_int8_t   type;
826         u_int8_t   media_type;
827         char       vendor[SID_VENDOR_SIZE+1];
828         char       product[SID_PRODUCT_SIZE+1];
829         char       revision[SID_REVISION_SIZE+1];
830 };
831
832 struct scsi_sense_quirk_entry {
833         struct scsi_inquiry_pattern     inq_pat;
834         int                             num_sense_keys;
835         int                             num_ascs;
836         struct sense_key_table_entry    *sense_key_info;
837         struct asc_table_entry          *asc_info;
838 };
839
840 struct sense_key_table_entry {
841         u_int8_t    sense_key;
842         u_int32_t   action;
843         const char *desc;
844 };
845
846 struct asc_table_entry {
847         u_int8_t    asc;
848         u_int8_t    ascq;
849         u_int32_t   action;
850         const char *desc;
851 };
852
853 struct op_table_entry {
854         u_int8_t    opcode;
855         u_int16_t   opmask;
856         const char  *desc;
857 };
858
859 struct scsi_op_quirk_entry {
860         struct scsi_inquiry_pattern     inq_pat;
861         int                             num_ops;
862         struct op_table_entry           *op_table;
863 };
864
865 typedef enum {
866         SSS_FLAG_NONE           = 0x00,
867         SSS_FLAG_PRINT_COMMAND  = 0x01
868 } scsi_sense_string_flags;
869
870 struct ccb_scsiio;
871 struct cam_periph;
872 union  ccb;
873 #ifndef _KERNEL
874 struct cam_device;
875 #endif
876
877 extern const char *scsi_sense_key_text[];
878
879 struct sbuf;
880
881 __BEGIN_DECLS
882 void scsi_sense_desc(int sense_key, int asc, int ascq,
883                      struct scsi_inquiry_data *inq_data,
884                      const char **sense_key_desc, const char **asc_desc);
885 scsi_sense_action scsi_error_action(struct ccb_scsiio* csio,
886                                     struct scsi_inquiry_data *inq_data,
887                                     u_int32_t sense_flags);
888 const char *    scsi_status_string(struct ccb_scsiio *csio);
889 #ifdef _KERNEL
890 int             scsi_command_string(struct ccb_scsiio *csio, struct sbuf *sb);
891 int             scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
892                                 scsi_sense_string_flags flags);
893 char *          scsi_sense_string(struct ccb_scsiio *csio,
894                                   char *str, int str_len);
895 void            scsi_sense_print(struct ccb_scsiio *csio);
896 int             scsi_interpret_sense(union ccb *ccb, 
897                                      u_int32_t sense_flags,
898                                      u_int32_t *relsim_flags, 
899                                      u_int32_t *reduction,
900                                      u_int32_t *timeout,
901                                      scsi_sense_action error_action);
902 #else
903 int             scsi_command_string(struct cam_device *device,
904                                     struct ccb_scsiio *csio, struct sbuf *sb);
905 int             scsi_sense_sbuf(struct cam_device *device,
906                                 struct ccb_scsiio *csio, struct sbuf *sb,
907                                 scsi_sense_string_flags flags);
908 char *          scsi_sense_string(struct cam_device *device, 
909                                   struct ccb_scsiio *csio,
910                                   char *str, int str_len);
911 void            scsi_sense_print(struct cam_device *device, 
912                                  struct ccb_scsiio *csio, FILE *ofile);
913 int             scsi_interpret_sense(struct cam_device *device,
914                                      union ccb *ccb,
915                                      u_int32_t sense_flags,
916                                      u_int32_t *relsim_flags, 
917                                      u_int32_t *reduction,
918                                      u_int32_t *timeout,
919                                      scsi_sense_action error_action);
920 #endif /* _KERNEL */
921
922 #define SF_RETRY_UA     0x01
923 #define SF_NO_PRINT     0x02
924 #define SF_QUIET_IR     0x04    /* Be quiet about Illegal Request reponses */
925 #define SF_PRINT_ALWAYS 0x08
926
927
928 const char *    scsi_op_desc(u_int16_t opcode, 
929                              struct scsi_inquiry_data *inq_data);
930 char *          scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string,
931                                 size_t len);
932
933 void            scsi_print_inquiry(struct scsi_inquiry_data *inq_data);
934
935 u_int           scsi_calc_syncsrate(u_int period_factor);
936 u_int           scsi_calc_syncparam(u_int period);
937         
938 void            scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries,
939                                      void (*cbfcnp)(struct cam_periph *, 
940                                                     union ccb *),
941                                      u_int8_t tag_action, 
942                                      u_int8_t sense_len, u_int32_t timeout);
943
944 void            scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
945                                    void (*cbfcnp)(struct cam_periph *, 
946                                                   union ccb *),
947                                    void *data_ptr, u_int8_t dxfer_len,
948                                    u_int8_t tag_action, u_int8_t sense_len,
949                                    u_int32_t timeout);
950
951 void            scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
952                              void (*cbfcnp)(struct cam_periph *, union ccb *),
953                              u_int8_t tag_action, u_int8_t *inq_buf, 
954                              u_int32_t inq_len, int evpd, u_int8_t page_code,
955                              u_int8_t sense_len, u_int32_t timeout);
956
957 void            scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
958                                 void (*cbfcnp)(struct cam_periph *,
959                                                union ccb *),
960                                 u_int8_t tag_action, int dbd,
961                                 u_int8_t page_code, u_int8_t page,
962                                 u_int8_t *param_buf, u_int32_t param_len,
963                                 u_int8_t sense_len, u_int32_t timeout);
964 void            scsi_mode_sense_len(struct ccb_scsiio *csio, u_int32_t retries,
965                                 void (*cbfcnp)(struct cam_periph *,
966                                                 union ccb *),
967                                 u_int8_t tag_action, int dbd,
968                                 u_int8_t page_code, u_int8_t page,
969                                 u_int8_t *param_buf, u_int32_t param_len,
970                                 int minimum_cmd_size, u_int8_t sense_len,
971                                 u_int32_t timeout);
972
973 void            scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
974                                  void (*cbfcnp)(struct cam_periph *,
975                                                 union ccb *),
976                                  u_int8_t tag_action, int scsi_page_fmt,
977                                  int save_pages, u_int8_t *param_buf,
978                                  u_int32_t param_len, u_int8_t sense_len,
979                                  u_int32_t timeout);
980
981 void            scsi_mode_select_len(struct ccb_scsiio *csio, u_int32_t retries,
982                                 void (*cbfcnp)(struct cam_periph *,
983                                                 union ccb *),
984                                 u_int8_t tag_action, int scsi_page_fmt,
985                                 int save_pages, u_int8_t *param_buf,
986                                 u_int32_t param_len, int minimum_cmd_size,
987                                 u_int8_t sense_len, u_int32_t timeout);
988
989 void            scsi_log_sense(struct ccb_scsiio *csio, u_int32_t retries,
990                                void (*cbfcnp)(struct cam_periph *, union ccb *),
991                                u_int8_t tag_action, u_int8_t page_code,
992                                u_int8_t page, int save_pages, int ppc,
993                                u_int32_t paramptr, u_int8_t *param_buf,
994                                u_int32_t param_len, u_int8_t sense_len,
995                                u_int32_t timeout);
996
997 void            scsi_log_select(struct ccb_scsiio *csio, u_int32_t retries,
998                                 void (*cbfcnp)(struct cam_periph *,
999                                 union ccb *), u_int8_t tag_action,
1000                                 u_int8_t page_code, int save_pages,
1001                                 int pc_reset, u_int8_t *param_buf,
1002                                 u_int32_t param_len, u_int8_t sense_len,
1003                                 u_int32_t timeout);
1004
1005 void            scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
1006                                    void (*cbfcnp)(struct cam_periph *, 
1007                                    union ccb *), u_int8_t tag_action, 
1008                                    struct scsi_read_capacity_data *rcap_buf,
1009                                    u_int8_t sense_len, u_int32_t timeout);
1010 void            scsi_read_capacity_16(struct ccb_scsiio *csio, uint32_t retries,
1011                                     void (*cbfcnp)(struct cam_periph *,
1012                                     union ccb *), uint8_t tag_action,
1013                                     uint64_t lba, int reladr, int pmi,
1014                                     struct scsi_read_capacity_data_long
1015                                     *rcap_buf, uint8_t sense_len,
1016                                     uint32_t timeout);
1017
1018 void            scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
1019                              void (*cbfcnp)(struct cam_periph *, union ccb *),
1020                              u_int8_t tag_action, u_int8_t action,
1021                              u_int8_t sense_len, u_int32_t timeout);
1022
1023 void            scsi_synchronize_cache(struct ccb_scsiio *csio, 
1024                                        u_int32_t retries,
1025                                        void (*cbfcnp)(struct cam_periph *, 
1026                                        union ccb *), u_int8_t tag_action, 
1027                                        u_int32_t begin_lba, u_int16_t lb_count,
1028                                        u_int8_t sense_len, u_int32_t timeout);
1029
1030 void scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
1031                      void (*cbfcnp)(struct cam_periph *, union ccb *),
1032                      u_int8_t tag_action, int readop, u_int8_t byte2, 
1033                      int minimum_cmd_size, u_int64_t lba,
1034                      u_int32_t block_count, u_int8_t *data_ptr,
1035                      u_int32_t dxfer_len, u_int8_t sense_len,
1036                      u_int32_t timeout);
1037
1038 void scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
1039                      void (*cbfcnp)(struct cam_periph *, union ccb *),
1040                      u_int8_t tag_action, int start, int load_eject,
1041                      int immediate, u_int8_t sense_len, u_int32_t timeout);
1042
1043 int             scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry);
1044 int             scsi_static_inquiry_match(caddr_t inqbuffer,
1045                                           caddr_t table_entry);
1046
1047 static __inline void scsi_extract_sense(struct scsi_sense_data *sense,
1048                                         int *error_code, int *sense_key,
1049                                         int *asc, int *ascq);
1050 static __inline void scsi_ulto2b(u_int32_t val, u_int8_t *bytes);
1051 static __inline void scsi_ulto3b(u_int32_t val, u_int8_t *bytes);
1052 static __inline void scsi_ulto4b(u_int32_t val, u_int8_t *bytes);
1053 static __inline void scsi_u64to8b(u_int64_t val, u_int8_t *bytes);
1054 static __inline u_int32_t scsi_2btoul(u_int8_t *bytes);
1055 static __inline u_int32_t scsi_3btoul(u_int8_t *bytes);
1056 static __inline int32_t scsi_3btol(u_int8_t *bytes);
1057 static __inline u_int32_t scsi_4btoul(u_int8_t *bytes);
1058 static __inline u_int64_t scsi_8btou64(u_int8_t *bytes);
1059 static __inline void *find_mode_page_6(struct scsi_mode_header_6 *mode_header);
1060 static __inline void *find_mode_page_10(struct scsi_mode_header_10 *mode_header);
1061
1062 static __inline void
1063 scsi_extract_sense(struct scsi_sense_data *sense, int *error_code,
1064                    int *sense_key, int *asc, int *ascq)
1065 {
1066         *error_code = sense->error_code & SSD_ERRCODE;
1067         *sense_key = sense->flags & SSD_KEY;
1068         *asc = (sense->extra_len >= 5) ? sense->add_sense_code : 0;
1069         *ascq = (sense->extra_len >= 6) ? sense->add_sense_code_qual : 0;
1070 }
1071
1072 static __inline void
1073 scsi_ulto2b(u_int32_t val, u_int8_t *bytes)
1074 {
1075
1076         bytes[0] = (val >> 8) & 0xff;
1077         bytes[1] = val & 0xff;
1078 }
1079
1080 static __inline void
1081 scsi_ulto3b(u_int32_t val, u_int8_t *bytes)
1082 {
1083
1084         bytes[0] = (val >> 16) & 0xff;
1085         bytes[1] = (val >> 8) & 0xff;
1086         bytes[2] = val & 0xff;
1087 }
1088
1089 static __inline void
1090 scsi_ulto4b(u_int32_t val, u_int8_t *bytes)
1091 {
1092
1093         bytes[0] = (val >> 24) & 0xff;
1094         bytes[1] = (val >> 16) & 0xff;
1095         bytes[2] = (val >> 8) & 0xff;
1096         bytes[3] = val & 0xff;
1097 }
1098
1099 static __inline void
1100 scsi_u64to8b(u_int64_t val, u_int8_t *bytes)
1101 {
1102         bytes[0] = (val >> 56) & 0xff;
1103         bytes[1] = (val >> 48) & 0xff;
1104         bytes[2] = (val >> 40) & 0xff;
1105         bytes[3] = (val >> 32) & 0xff;
1106         bytes[4] = (val >> 24) & 0xff;
1107         bytes[5] = (val >> 16) & 0xff;
1108         bytes[6] = (val >> 8) & 0xff;
1109         bytes[7] = val & 0xff;
1110 }
1111
1112 static __inline u_int32_t
1113 scsi_2btoul(u_int8_t *bytes)
1114 {
1115         u_int32_t rv;
1116
1117         rv = (bytes[0] << 8) |
1118              bytes[1];
1119         return (rv);
1120 }
1121
1122 static __inline uint64_t
1123 scsi_8btou64(uint8_t *bytes)
1124 {
1125         uint64_t rv;
1126
1127         rv = (((uint64_t)bytes[0]) << 56) |
1128              (((uint64_t)bytes[1]) << 48) |
1129              (((uint64_t)bytes[2]) << 40) |
1130              (((uint64_t)bytes[3]) << 32) |
1131              (((uint64_t)bytes[4]) << 24) |
1132              (((uint64_t)bytes[5]) << 16) |
1133              (((uint64_t)bytes[6]) << 8) |
1134              bytes[7];
1135         return (rv);
1136 }
1137
1138 static __inline u_int32_t
1139 scsi_3btoul(u_int8_t *bytes)
1140 {
1141         u_int32_t rv;
1142
1143         rv = (bytes[0] << 16) |
1144              (bytes[1] << 8) |
1145              bytes[2];
1146         return (rv);
1147 }
1148
1149 static __inline int32_t 
1150 scsi_3btol(u_int8_t *bytes)
1151 {
1152         u_int32_t rc = scsi_3btoul(bytes);
1153  
1154         if (rc & 0x00800000)
1155                 rc |= 0xff000000;
1156
1157         return (int32_t) rc;
1158 }
1159
1160 static __inline u_int32_t
1161 scsi_4btoul(u_int8_t *bytes)
1162 {
1163         u_int32_t rv;
1164
1165         rv = (bytes[0] << 24) |
1166              (bytes[1] << 16) |
1167              (bytes[2] << 8) |
1168              bytes[3];
1169         return (rv);
1170 }
1171
1172 /*
1173  * Given the pointer to a returned mode sense buffer, return a pointer to
1174  * the start of the first mode page.
1175  */
1176 static __inline void *
1177 find_mode_page_6(struct scsi_mode_header_6 *mode_header)
1178 {
1179         void *page_start;
1180
1181         page_start = (void *)((u_int8_t *)&mode_header[1] +
1182                               mode_header->blk_desc_len);
1183
1184         return(page_start);
1185 }
1186
1187 static __inline void *
1188 find_mode_page_10(struct scsi_mode_header_10 *mode_header)
1189 {
1190         void *page_start;
1191
1192         page_start = (void *)((u_int8_t *)&mode_header[1] +
1193                                scsi_2btoul(mode_header->blk_desc_len));
1194
1195         return(page_start);
1196 }
1197
1198 __END_DECLS
1199
1200 #endif /*_SCSI_SCSI_ALL_H*/