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