]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/cam/scsi/scsi_all.h
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / sys / 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$
18  */
19
20 /*
21  * SCSI general  interface description
22  */
23
24 #ifndef _SCSI_SCSI_ALL_H
25 #define _SCSI_SCSI_ALL_H 1
26
27 #include <sys/cdefs.h>
28
29 #ifdef _KERNEL
30 /*
31  * This is the number of seconds we wait for devices to settle after a SCSI
32  * bus reset.
33  */
34 extern int scsi_delay;
35 #endif /* _KERNEL */
36
37 /*
38  * SCSI command format
39  */
40
41 /*
42  * Define dome bits that are in ALL (or a lot of) scsi commands
43  */
44 #define SCSI_CTL_LINK           0x01
45 #define SCSI_CTL_FLAG           0x02
46 #define SCSI_CTL_VENDOR         0xC0
47 #define SCSI_CMD_LUN            0xA0    /* these two should not be needed */
48 #define SCSI_CMD_LUN_SHIFT      5       /* LUN in the cmd is no longer SCSI */
49
50 #define SCSI_MAX_CDBLEN         16      /* 
51                                          * 16 byte commands are in the 
52                                          * SCSI-3 spec 
53                                          */
54 #if defined(CAM_MAX_CDBLEN) && (CAM_MAX_CDBLEN < SCSI_MAX_CDBLEN)
55 #error "CAM_MAX_CDBLEN cannot be less than SCSI_MAX_CDBLEN"
56 #endif
57
58 /* 6byte CDBs special case 0 length to be 256 */
59 #define SCSI_CDB6_LEN(len)      ((len) == 0 ? 256 : len)
60
61 /*
62  * This type defines actions to be taken when a particular sense code is
63  * received.  Right now, these flags are only defined to take up 16 bits,
64  * but can be expanded in the future if necessary.
65  */
66 typedef enum {
67         SS_NOP      = 0x000000, /* Do nothing */
68         SS_RETRY    = 0x010000, /* Retry the command */
69         SS_FAIL     = 0x020000, /* Bail out */
70         SS_START    = 0x030000, /* Send a Start Unit command to the device,
71                                  * then retry the original command.
72                                  */
73         SS_TUR      = 0x040000, /* Send a Test Unit Ready command to the
74                                  * device, then retry the original command.
75                                  */
76         SS_REQSENSE = 0x050000, /* Send a RequestSense command to the
77                                  * device, then retry the original command.
78                                  */
79         SS_MASK     = 0xff0000
80 } scsi_sense_action;
81
82 typedef enum {
83         SSQ_NONE                = 0x0000,
84         SSQ_DECREMENT_COUNT     = 0x0100,  /* Decrement the retry count */
85         SSQ_MANY                = 0x0200,  /* send lots of recovery commands */
86         SSQ_RANGE               = 0x0400,  /*
87                                             * This table entry represents the
88                                             * end of a range of ASCQs that
89                                             * have identical error actions
90                                             * and text.
91                                             */
92         SSQ_PRINT_SENSE         = 0x0800,
93         SSQ_MASK                = 0xff00
94 } scsi_sense_action_qualifier;
95
96 /* Mask for error status values */
97 #define SS_ERRMASK      0xff
98
99 /* The default, retyable, error action */
100 #define SS_RDEF         SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE|EIO
101
102 /* The retyable, error action, with table specified error code */
103 #define SS_RET          SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE
104
105 /* Fatal error action, with table specified error code */
106 #define SS_FATAL        SS_FAIL|SSQ_PRINT_SENSE
107
108 struct scsi_generic
109 {
110         u_int8_t opcode;
111         u_int8_t bytes[11];
112 };
113
114 struct scsi_request_sense
115 {
116         u_int8_t opcode;
117         u_int8_t byte2;
118 #define SRS_DESC        0x01
119         u_int8_t unused[2];
120         u_int8_t length;
121         u_int8_t control;
122 };
123
124 struct scsi_test_unit_ready
125 {
126         u_int8_t opcode;
127         u_int8_t byte2;
128         u_int8_t unused[3];
129         u_int8_t control;
130 };
131
132 struct scsi_receive_diag {
133         uint8_t opcode;
134         uint8_t byte2;
135 #define SRD_PCV         0x01
136         uint8_t page_code;
137         uint8_t length[2]; 
138         uint8_t control;
139 };
140
141 struct scsi_send_diag {
142         uint8_t opcode;
143         uint8_t byte2;
144 #define SSD_UNITOFFL                            0x01
145 #define SSD_DEVOFFL                             0x02
146 #define SSD_SELFTEST                            0x04
147 #define SSD_PF                                  0x10
148 #define SSD_SELF_TEST_CODE_MASK                 0xE0
149 #define SSD_SELF_TEST_CODE_SHIFT                5
150 #define         SSD_SELF_TEST_CODE_NONE         0x00
151 #define         SSD_SELF_TEST_CODE_BG_SHORT     0x01
152 #define         SSD_SELF_TEST_CODE_BG_EXTENDED  0x02
153 #define         SSD_SELF_TEST_CODE_BG_ABORT     0x04
154 #define         SSD_SELF_TEST_CODE_FG_SHORT     0x05
155 #define         SSD_SELF_TEST_CODE_FG_EXTENDED  0x06
156         uint8_t reserved;
157         uint8_t length[2];
158         uint8_t control;
159 };
160
161 struct scsi_sense
162 {
163         u_int8_t opcode;
164         u_int8_t byte2;
165         u_int8_t unused[2];
166         u_int8_t length;
167         u_int8_t control;
168 };
169
170 struct scsi_inquiry
171 {
172         u_int8_t opcode;
173         u_int8_t byte2;
174 #define SI_EVPD 0x01
175         u_int8_t page_code;
176         u_int8_t reserved;
177         u_int8_t length;
178         u_int8_t control;
179 };
180
181 struct scsi_mode_sense_6
182 {
183         u_int8_t opcode;
184         u_int8_t byte2;
185 #define SMS_DBD                         0x08
186         u_int8_t page;
187 #define SMS_PAGE_CODE                   0x3F
188 #define SMS_VENDOR_SPECIFIC_PAGE        0x00
189 #define SMS_DISCONNECT_RECONNECT_PAGE   0x02
190 #define SMS_FORMAT_DEVICE_PAGE          0x03
191 #define SMS_GEOMETRY_PAGE               0x04
192 #define SMS_CACHE_PAGE                  0x08
193 #define SMS_PERIPHERAL_DEVICE_PAGE      0x09
194 #define SMS_CONTROL_MODE_PAGE           0x0A
195 #define SMS_PROTO_SPECIFIC_PAGE         0x19
196 #define SMS_INFO_EXCEPTIONS_PAGE        0x1C
197 #define SMS_ALL_PAGES_PAGE              0x3F
198 #define SMS_PAGE_CTRL_MASK              0xC0
199 #define SMS_PAGE_CTRL_CURRENT           0x00
200 #define SMS_PAGE_CTRL_CHANGEABLE        0x40
201 #define SMS_PAGE_CTRL_DEFAULT           0x80
202 #define SMS_PAGE_CTRL_SAVED             0xC0
203         u_int8_t unused;
204         u_int8_t length;
205         u_int8_t control;
206 };
207
208 struct scsi_mode_sense_10
209 {
210         u_int8_t opcode;
211         u_int8_t byte2;         /* same bits as small version */
212         u_int8_t page;          /* same bits as small version */
213         u_int8_t unused[4];
214         u_int8_t length[2];
215         u_int8_t control;
216 };
217
218 struct scsi_mode_select_6
219 {
220         u_int8_t opcode;
221         u_int8_t byte2;
222 #define SMS_SP  0x01
223 #define SMS_PF  0x10
224         u_int8_t unused[2];
225         u_int8_t length;
226         u_int8_t control;
227 };
228
229 struct scsi_mode_select_10
230 {
231         u_int8_t opcode;
232         u_int8_t byte2;         /* same bits as small version */
233         u_int8_t unused[5];
234         u_int8_t length[2];
235         u_int8_t control;
236 };
237
238 /*
239  * When sending a mode select to a tape drive, the medium type must be 0.
240  */
241 struct scsi_mode_hdr_6
242 {
243         u_int8_t datalen;
244         u_int8_t medium_type;
245         u_int8_t dev_specific;
246         u_int8_t block_descr_len;
247 };
248
249 struct scsi_mode_hdr_10
250 {
251         u_int8_t datalen[2];
252         u_int8_t medium_type;
253         u_int8_t dev_specific;
254         u_int8_t reserved[2];
255         u_int8_t block_descr_len[2];
256 };
257
258 struct scsi_mode_block_descr
259 {
260         u_int8_t density_code;
261         u_int8_t num_blocks[3];
262         u_int8_t reserved;
263         u_int8_t block_len[3];
264 };
265
266 struct scsi_log_sense
267 {
268         u_int8_t opcode;
269         u_int8_t byte2;
270 #define SLS_SP                          0x01
271 #define SLS_PPC                         0x02
272         u_int8_t page;
273 #define SLS_PAGE_CODE                   0x3F
274 #define SLS_ALL_PAGES_PAGE              0x00
275 #define SLS_OVERRUN_PAGE                0x01
276 #define SLS_ERROR_WRITE_PAGE            0x02
277 #define SLS_ERROR_READ_PAGE             0x03
278 #define SLS_ERROR_READREVERSE_PAGE      0x04
279 #define SLS_ERROR_VERIFY_PAGE           0x05
280 #define SLS_ERROR_NONMEDIUM_PAGE        0x06
281 #define SLS_ERROR_LASTN_PAGE            0x07
282 #define SLS_SELF_TEST_PAGE              0x10
283 #define SLS_IE_PAGE                     0x2f
284 #define SLS_PAGE_CTRL_MASK              0xC0
285 #define SLS_PAGE_CTRL_THRESHOLD         0x00
286 #define SLS_PAGE_CTRL_CUMULATIVE        0x40
287 #define SLS_PAGE_CTRL_THRESH_DEFAULT    0x80
288 #define SLS_PAGE_CTRL_CUMUL_DEFAULT     0xC0
289         u_int8_t reserved[2];
290         u_int8_t paramptr[2];
291         u_int8_t length[2];
292         u_int8_t control;
293 };
294
295 struct scsi_log_select
296 {
297         u_int8_t opcode;
298         u_int8_t byte2;
299 /*      SLS_SP                          0x01 */
300 #define SLS_PCR                         0x02
301         u_int8_t page;
302 /*      SLS_PAGE_CTRL_MASK              0xC0 */
303 /*      SLS_PAGE_CTRL_THRESHOLD         0x00 */
304 /*      SLS_PAGE_CTRL_CUMULATIVE        0x40 */
305 /*      SLS_PAGE_CTRL_THRESH_DEFAULT    0x80 */
306 /*      SLS_PAGE_CTRL_CUMUL_DEFAULT     0xC0 */
307         u_int8_t reserved[4];
308         u_int8_t length[2];
309         u_int8_t control;
310 };
311
312 struct scsi_log_header
313 {
314         u_int8_t page;
315         u_int8_t reserved;
316         u_int8_t datalen[2];
317 };
318
319 struct scsi_log_param_header {
320         u_int8_t param_code[2];
321         u_int8_t param_control;
322 #define SLP_LP                          0x01
323 #define SLP_LBIN                        0x02
324 #define SLP_TMC_MASK                    0x0C
325 #define SLP_TMC_ALWAYS                  0x00
326 #define SLP_TMC_EQUAL                   0x04
327 #define SLP_TMC_NOTEQUAL                0x08
328 #define SLP_TMC_GREATER                 0x0C
329 #define SLP_ETC                         0x10
330 #define SLP_TSD                         0x20
331 #define SLP_DS                          0x40
332 #define SLP_DU                          0x80
333         u_int8_t param_len;
334 };
335
336 struct scsi_control_page {
337         u_int8_t page_code;
338         u_int8_t page_length;
339         u_int8_t rlec;
340 #define SCB_RLEC                        0x01    /*Report Log Exception Cond*/
341         u_int8_t queue_flags;
342 #define SCP_QUEUE_ALG_MASK              0xF0
343 #define SCP_QUEUE_ALG_RESTRICTED        0x00
344 #define SCP_QUEUE_ALG_UNRESTRICTED      0x10
345 #define SCP_QUEUE_ERR                   0x02    /*Queued I/O aborted for CACs*/
346 #define SCP_QUEUE_DQUE                  0x01    /*Queued I/O disabled*/
347         u_int8_t eca_and_aen;
348 #define SCP_EECA                        0x80    /*Enable Extended CA*/
349 #define SCP_RAENP                       0x04    /*Ready AEN Permission*/
350 #define SCP_UAAENP                      0x02    /*UA AEN Permission*/
351 #define SCP_EAENP                       0x01    /*Error AEN Permission*/
352         u_int8_t reserved;
353         u_int8_t aen_holdoff_period[2];
354 };
355
356 struct scsi_cache_page {
357         u_int8_t page_code;
358 #define SCHP_PAGE_SAVABLE               0x80    /* Page is savable */
359         u_int8_t page_length;
360         u_int8_t cache_flags;
361 #define SCHP_FLAGS_WCE                  0x04    /* Write Cache Enable */
362 #define SCHP_FLAGS_MF                   0x02    /* Multiplication factor */
363 #define SCHP_FLAGS_RCD                  0x01    /* Read Cache Disable */
364         u_int8_t rw_cache_policy;
365         u_int8_t dis_prefetch[2];
366         u_int8_t min_prefetch[2];
367         u_int8_t max_prefetch[2];
368         u_int8_t max_prefetch_ceil[2];
369 };
370
371 struct scsi_info_exceptions_page {
372         u_int8_t page_code;
373 #define SIEP_PAGE_SAVABLE               0x80    /* Page is savable */
374         u_int8_t page_length;
375         u_int8_t info_flags;
376 #define SIEP_FLAGS_PERF                 0x80
377 #define SIEP_FLAGS_EBF                  0x20
378 #define SIEP_FLAGS_EWASC                0x10
379 #define SIEP_FLAGS_DEXCPT               0x08
380 #define SIEP_FLAGS_TEST                 0x04
381 #define SIEP_FLAGS_EBACKERR             0x02
382 #define SIEP_FLAGS_LOGERR               0x01
383         u_int8_t mrie;
384         u_int8_t interval_timer[4];
385         u_int8_t report_count[4];
386 };
387
388 struct scsi_proto_specific_page {
389         u_int8_t page_code;
390 #define SPSP_PAGE_SAVABLE               0x80    /* Page is savable */
391         u_int8_t page_length;
392         u_int8_t protocol;
393 #define SPSP_PROTO_FC                   0x00
394 #define SPSP_PROTO_SPI                  0x01
395 #define SPSP_PROTO_SSA                  0x02
396 #define SPSP_PROTO_1394                 0x03
397 #define SPSP_PROTO_RDMA                 0x04
398 #define SPSP_PROTO_ISCSI                0x05
399 #define SPSP_PROTO_SAS                  0x06
400 #define SPSP_PROTO_ADT                  0x07
401 #define SPSP_PROTO_ATA                  0x08
402 #define SPSP_PROTO_NONE                 0x0f
403 };
404
405 struct scsi_reserve
406 {
407         u_int8_t opcode;
408         u_int8_t byte2;
409         u_int8_t unused[2];
410         u_int8_t length;
411         u_int8_t control;
412 };
413
414 struct scsi_release
415 {
416         u_int8_t opcode;
417         u_int8_t byte2;
418         u_int8_t unused[2];
419         u_int8_t length;
420         u_int8_t control;
421 };
422
423 struct scsi_prevent
424 {
425         u_int8_t opcode;
426         u_int8_t byte2;
427         u_int8_t unused[2];
428         u_int8_t how;
429         u_int8_t control;
430 };
431 #define PR_PREVENT 0x01
432 #define PR_ALLOW   0x00
433
434 struct scsi_sync_cache
435 {
436         u_int8_t opcode;
437         u_int8_t byte2;
438         u_int8_t begin_lba[4];
439         u_int8_t reserved;
440         u_int8_t lb_count[2];
441         u_int8_t control;       
442 };
443
444
445 struct scsi_changedef
446 {
447         u_int8_t opcode;
448         u_int8_t byte2;
449         u_int8_t unused1;
450         u_int8_t how;
451         u_int8_t unused[4];
452         u_int8_t datalen;
453         u_int8_t control;
454 };
455
456 struct scsi_read_buffer
457 {
458         u_int8_t opcode;
459         u_int8_t byte2;
460 #define RWB_MODE                0x07
461 #define RWB_MODE_HDR_DATA       0x00
462 #define RWB_MODE_DATA           0x02
463 #define RWB_MODE_DOWNLOAD       0x04
464 #define RWB_MODE_DOWNLOAD_SAVE  0x05
465         u_int8_t buffer_id;
466         u_int8_t offset[3];
467         u_int8_t length[3];
468         u_int8_t control;
469 };
470
471 struct scsi_write_buffer
472 {
473         u_int8_t opcode;
474         u_int8_t byte2;
475         u_int8_t buffer_id;
476         u_int8_t offset[3];
477         u_int8_t length[3];
478         u_int8_t control;
479 };
480
481 struct scsi_rw_6
482 {
483         u_int8_t opcode;
484         u_int8_t addr[3];
485 /* only 5 bits are valid in the MSB address byte */
486 #define SRW_TOPADDR     0x1F
487         u_int8_t length;
488         u_int8_t control;
489 };
490
491 struct scsi_rw_10
492 {
493         u_int8_t opcode;
494 #define SRW10_RELADDR   0x01
495 /* EBP defined for WRITE(10) only */
496 #define SRW10_EBP       0x04
497 #define SRW10_FUA       0x08
498 #define SRW10_DPO       0x10
499         u_int8_t byte2;
500         u_int8_t addr[4];
501         u_int8_t reserved;
502         u_int8_t length[2];
503         u_int8_t control;
504 };
505
506 struct scsi_rw_12
507 {
508         u_int8_t opcode;
509 #define SRW12_RELADDR   0x01
510 #define SRW12_FUA       0x08
511 #define SRW12_DPO       0x10
512         u_int8_t byte2;
513         u_int8_t addr[4];
514         u_int8_t length[4];
515         u_int8_t reserved;
516         u_int8_t control;
517 };
518
519 struct scsi_rw_16
520 {
521         u_int8_t opcode;
522 #define SRW16_RELADDR   0x01
523 #define SRW16_FUA       0x08
524 #define SRW16_DPO       0x10
525         u_int8_t byte2;
526         u_int8_t addr[8];
527         u_int8_t length[4];
528         u_int8_t reserved;
529         u_int8_t control;
530 };
531
532 struct scsi_start_stop_unit
533 {
534         u_int8_t opcode;
535         u_int8_t byte2;
536 #define SSS_IMMED               0x01
537         u_int8_t reserved[2];
538         u_int8_t how;
539 #define SSS_START               0x01
540 #define SSS_LOEJ                0x02
541         u_int8_t control;
542 };
543
544 struct ata_pass_12 {
545         u_int8_t opcode;
546         u_int8_t protocol;
547 #define AP_MULTI        0xe0
548         u_int8_t flags;
549 #define AP_T_LEN        0x03
550 #define AP_BB           0x04
551 #define AP_T_DIR        0x08
552 #define AP_CK_COND      0x20
553 #define AP_OFFLINE      0x60
554         u_int8_t features;
555         u_int8_t sector_count;
556         u_int8_t lba_low;
557         u_int8_t lba_mid;
558         u_int8_t lba_high;
559         u_int8_t device;
560         u_int8_t command;
561         u_int8_t reserved;
562         u_int8_t control;
563 };
564
565 struct ata_pass_16 {
566         u_int8_t opcode;
567         u_int8_t protocol;
568 #define AP_EXTEND       0x01
569         u_int8_t flags;
570         u_int8_t features_ext;
571         u_int8_t features;
572         u_int8_t sector_count_ext;
573         u_int8_t sector_count;
574         u_int8_t lba_low_ext;
575         u_int8_t lba_low;
576         u_int8_t lba_mid_ext;
577         u_int8_t lba_mid;
578         u_int8_t lba_high_ext;
579         u_int8_t lba_high;
580         u_int8_t device;
581         u_int8_t command;
582         u_int8_t control;
583 };
584
585 #define SC_SCSI_1 0x01
586 #define SC_SCSI_2 0x03
587
588 /*
589  * Opcodes
590  */
591
592 #define TEST_UNIT_READY         0x00
593 #define REQUEST_SENSE           0x03
594 #define READ_6                  0x08
595 #define WRITE_6                 0x0A
596 #define INQUIRY                 0x12
597 #define MODE_SELECT_6           0x15
598 #define MODE_SENSE_6            0x1A
599 #define START_STOP_UNIT         0x1B
600 #define START_STOP              0x1B
601 #define RESERVE                 0x16
602 #define RELEASE                 0x17
603 #define RECEIVE_DIAGNOSTIC      0x1C
604 #define SEND_DIAGNOSTIC         0x1D
605 #define PREVENT_ALLOW           0x1E
606 #define READ_CAPACITY           0x25
607 #define READ_10                 0x28
608 #define WRITE_10                0x2A
609 #define POSITION_TO_ELEMENT     0x2B
610 #define SYNCHRONIZE_CACHE       0x35
611 #define READ_DEFECT_DATA_10     0x37
612 #define WRITE_BUFFER            0x3B
613 #define READ_BUFFER             0x3C
614 #define CHANGE_DEFINITION       0x40
615 #define LOG_SELECT              0x4C
616 #define LOG_SENSE               0x4D
617 #define MODE_SELECT_10          0x55
618 #define MODE_SENSE_10           0x5A
619 #define ATA_PASS_16             0x85
620 #define READ_16                 0x88
621 #define WRITE_16                0x8A
622 #define SERVICE_ACTION_IN       0x9E
623 #define REPORT_LUNS             0xA0
624 #define ATA_PASS_12             0xA1
625 #define MAINTENANCE_IN          0xA3
626 #define MAINTENANCE_OUT         0xA4
627 #define MOVE_MEDIUM             0xA5
628 #define READ_12                 0xA8
629 #define WRITE_12                0xAA
630 #define READ_ELEMENT_STATUS     0xB8
631
632 /* Maintenance In Service Action Codes */
633 #define REPORT_IDENTIFYING_INFRMATION           0x05
634 #define REPORT_TARGET_PORT_GROUPS               0x0A
635 #define REPORT_ALIASES                          0x0B
636 #define REPORT_SUPPORTED_OPERATION_CODES        0x0C
637 #define REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS      0x0D
638 #define REPORT_PRIORITY                         0x0E
639 #define REPORT_TIMESTAMP                        0x0F
640 #define MANAGEMENT_PROTOCOL_IN                  0x10
641 /* Maintenance Out Service Action Codes */
642 #define SET_IDENTIFY_INFORMATION                0x06
643 #define SET_TARGET_PORT_GROUPS                  0x0A
644 #define CHANGE_ALIASES                          0x0B
645 #define SET_PRIORITY                            0x0E
646 #define SET_TIMESTAMP                           0x0F
647 #define MANGAEMENT_PROTOCOL_OUT                 0x10
648
649 /*
650  * Device Types
651  */
652 #define T_DIRECT        0x00
653 #define T_SEQUENTIAL    0x01
654 #define T_PRINTER       0x02
655 #define T_PROCESSOR     0x03
656 #define T_WORM          0x04
657 #define T_CDROM         0x05
658 #define T_SCANNER       0x06
659 #define T_OPTICAL       0x07
660 #define T_CHANGER       0x08
661 #define T_COMM          0x09
662 #define T_ASC0          0x0a
663 #define T_ASC1          0x0b
664 #define T_STORARRAY     0x0c
665 #define T_ENCLOSURE     0x0d
666 #define T_RBC           0x0e
667 #define T_OCRW          0x0f
668 #define T_OSD           0x11
669 #define T_ADC           0x12
670 #define T_NODEVICE      0x1f
671 #define T_ANY           0xff    /* Used in Quirk table matches */
672
673 #define T_REMOV         1
674 #define T_FIXED         0
675
676 /*
677  * This length is the initial inquiry length used by the probe code, as    
678  * well as the legnth necessary for scsi_print_inquiry() to function 
679  * correctly.  If either use requires a different length in the future, 
680  * the two values should be de-coupled.
681  */
682 #define SHORT_INQUIRY_LENGTH    36
683
684 struct scsi_inquiry_data
685 {
686         u_int8_t device;
687 #define SID_TYPE(inq_data) ((inq_data)->device & 0x1f)
688 #define SID_QUAL(inq_data) (((inq_data)->device & 0xE0) >> 5)
689 #define SID_QUAL_LU_CONNECTED   0x00    /*
690                                          * The specified peripheral device
691                                          * type is currently connected to
692                                          * logical unit.  If the target cannot
693                                          * determine whether or not a physical
694                                          * device is currently connected, it
695                                          * shall also use this peripheral
696                                          * qualifier when returning the INQUIRY
697                                          * data.  This peripheral qualifier
698                                          * does not mean that the device is
699                                          * ready for access by the initiator.
700                                          */
701 #define SID_QUAL_LU_OFFLINE     0x01    /*
702                                          * The target is capable of supporting
703                                          * the specified peripheral device type
704                                          * on this logical unit; however, the
705                                          * physical device is not currently
706                                          * connected to this logical unit.
707                                          */
708 #define SID_QUAL_RSVD           0x02
709 #define SID_QUAL_BAD_LU         0x03    /*
710                                          * The target is not capable of
711                                          * supporting a physical device on
712                                          * this logical unit. For this
713                                          * peripheral qualifier the peripheral
714                                          * device type shall be set to 1Fh to
715                                          * provide compatibility with previous
716                                          * versions of SCSI. All other
717                                          * peripheral device type values are
718                                          * reserved for this peripheral
719                                          * qualifier.
720                                          */
721 #define SID_QUAL_IS_VENDOR_UNIQUE(inq_data) ((SID_QUAL(inq_data) & 0x08) != 0)
722         u_int8_t dev_qual2;
723 #define SID_QUAL2       0x7F
724 #define SID_IS_REMOVABLE(inq_data) (((inq_data)->dev_qual2 & 0x80) != 0)
725         u_int8_t version;
726 #define SID_ANSI_REV(inq_data) ((inq_data)->version & 0x07)
727 #define         SCSI_REV_0              0
728 #define         SCSI_REV_CCS            1
729 #define         SCSI_REV_2              2
730 #define         SCSI_REV_SPC            3
731 #define         SCSI_REV_SPC2           4
732 #define         SCSI_REV_SPC3           5
733 #define         SCSI_REV_SPC4           6
734
735 #define SID_ECMA        0x38
736 #define SID_ISO         0xC0
737         u_int8_t response_format;
738 #define SID_AENC        0x80
739 #define SID_TrmIOP      0x40
740         u_int8_t additional_length;
741 #define SID_ADDITIONAL_LENGTH(iqd)                                      \
742         ((iqd)->additional_length +                                     \
743         offsetof(struct scsi_inquiry_data, additional_length) + 1)
744         u_int8_t spc3_flags;
745 #define SPC3_SID_PROTECT        0x01
746 #define SPC3_SID_3PC            0x08
747 #define SPC3_SID_TPGS_MASK      0x30
748 #define SPC3_SID_TPGS_IMPLICIT  0x10
749 #define SPC3_SID_TPGS_EXPLICIT  0x20
750 #define SPC3_SID_ACC            0x40
751 #define SPC3_SID_SCCS           0x80
752         u_int8_t spc2_flags;
753 #define SPC2_SID_MChngr         0x08
754 #define SPC2_SID_MultiP         0x10
755 #define SPC2_SID_EncServ        0x40
756 #define SPC2_SID_BQueue         0x80
757
758 #define INQ_DATA_TQ_ENABLED(iqd)                                \
759     ((SID_ANSI_REV(iqd) < SCSI_REV_SPC2)? ((iqd)->flags & SID_CmdQue) : \
760     (((iqd)->flags & SID_CmdQue) && !((iqd)->spc2_flags & SPC2_SID_BQueue)) || \
761     (!((iqd)->flags & SID_CmdQue) && ((iqd)->spc2_flags & SPC2_SID_BQueue)))
762
763         u_int8_t flags;
764 #define SID_SftRe       0x01
765 #define SID_CmdQue      0x02
766 #define SID_Linked      0x08
767 #define SID_Sync        0x10
768 #define SID_WBus16      0x20
769 #define SID_WBus32      0x40
770 #define SID_RelAdr      0x80
771 #define SID_VENDOR_SIZE   8
772         char     vendor[SID_VENDOR_SIZE];
773 #define SID_PRODUCT_SIZE  16
774         char     product[SID_PRODUCT_SIZE];
775 #define SID_REVISION_SIZE 4
776         char     revision[SID_REVISION_SIZE];
777         /*
778          * The following fields were taken from SCSI Primary Commands - 2
779          * (SPC-2) Revision 14, Dated 11 November 1999
780          */
781 #define SID_VENDOR_SPECIFIC_0_SIZE      20
782         u_int8_t vendor_specific0[SID_VENDOR_SPECIFIC_0_SIZE];
783         /*
784          * An extension of SCSI Parallel Specific Values
785          */
786 #define SID_SPI_IUS             0x01
787 #define SID_SPI_QAS             0x02
788 #define SID_SPI_CLOCK_ST        0x00
789 #define SID_SPI_CLOCK_DT        0x04
790 #define SID_SPI_CLOCK_DT_ST     0x0C
791 #define SID_SPI_MASK            0x0F
792         u_int8_t spi3data;
793         u_int8_t reserved2;
794         /*
795          * Version Descriptors, stored 2 byte values.
796          */
797         u_int8_t version1[2];
798         u_int8_t version2[2];
799         u_int8_t version3[2];
800         u_int8_t version4[2];
801         u_int8_t version5[2];
802         u_int8_t version6[2];
803         u_int8_t version7[2];
804         u_int8_t version8[2];
805
806         u_int8_t reserved3[22];
807
808 #define SID_VENDOR_SPECIFIC_1_SIZE      160
809         u_int8_t vendor_specific1[SID_VENDOR_SPECIFIC_1_SIZE];
810 };
811
812 struct scsi_vpd_supported_page_list
813 {
814         u_int8_t device;
815         u_int8_t page_code;
816 #define SVPD_SUPPORTED_PAGE_LIST        0x00
817 #define SVPD_SUPPORTED_PAGES_HDR_LEN    4
818         u_int8_t reserved;
819         u_int8_t length;        /* number of VPD entries */
820 #define SVPD_SUPPORTED_PAGES_SIZE       251
821         u_int8_t list[SVPD_SUPPORTED_PAGES_SIZE];
822 };
823
824 /*
825  * This structure is more suited to target operation, because the
826  * number of supported pages is left to the user to allocate.
827  */
828 struct scsi_vpd_supported_pages
829 {
830         u_int8_t device;
831         u_int8_t page_code;
832         u_int8_t reserved;
833 #define SVPD_SUPPORTED_PAGES    0x00
834         u_int8_t length;
835         u_int8_t page_list[0];
836 };
837
838
839 struct scsi_vpd_unit_serial_number
840 {
841         u_int8_t device;
842         u_int8_t page_code;
843 #define SVPD_UNIT_SERIAL_NUMBER 0x80
844         u_int8_t reserved;
845         u_int8_t length; /* serial number length */
846 #define SVPD_SERIAL_NUM_SIZE 251
847         u_int8_t serial_num[SVPD_SERIAL_NUM_SIZE];
848 };
849
850 struct scsi_vpd_device_id
851 {
852         u_int8_t device;
853         u_int8_t page_code;
854 #define SVPD_DEVICE_ID                  0x83
855 #define SVPD_DEVICE_ID_MAX_SIZE         0xffff
856 #define SVPD_DEVICE_ID_HDR_LEN          4
857 #define SVPD_DEVICE_ID_DESC_HDR_LEN     4
858         u_int8_t length[2];
859         u_int8_t desc_list[0];
860 };
861
862 struct scsi_vpd_id_descriptor
863 {
864         u_int8_t        proto_codeset;
865 #define SCSI_PROTO_FC           0x00
866 #define SCSI_PROTO_SPI          0x01
867 #define SCSI_PROTO_SSA          0x02
868 #define SCSI_PROTO_1394         0x03
869 #define SCSI_PROTO_RDMA         0x04
870 #define SCSI_PROTO_iSCSI        0x05
871 #define SCSI_PROTO_SAS          0x06
872 #define SVPD_ID_PROTO_SHIFT     4
873 #define SVPD_ID_CODESET_BINARY  0x01
874 #define SVPD_ID_CODESET_ASCII   0x02
875         u_int8_t        id_type;
876 #define SVPD_ID_PIV             0x80
877 #define SVPD_ID_ASSOC_LUN       0x00
878 #define SVPD_ID_ASSOC_PORT      0x10
879 #define SVPD_ID_ASSOC_TARGET    0x20
880 #define SVPD_ID_TYPE_VENDOR     0x00
881 #define SVPD_ID_TYPE_T10        0x01
882 #define SVPD_ID_TYPE_EUI64      0x02
883 #define SVPD_ID_TYPE_NAA        0x03
884 #define SVPD_ID_TYPE_RELTARG    0x04
885 #define SVPD_ID_TYPE_TPORTGRP   0x05
886 #define SVPD_ID_TYPE_LUNGRP     0x06
887 #define SVPD_ID_TYPE_MD5_LUN_ID 0x07
888 #define SVPD_ID_TYPE_SCSI_NAME  0x08
889 #define SVPD_ID_TYPE_MASK       0x0f
890         u_int8_t        reserved;
891         u_int8_t        length;
892         u_int8_t        identifier[0];
893 };
894
895 struct scsi_vpd_id_t10
896 {
897         u_int8_t        vendor[8];
898         u_int8_t        vendor_spec_id[0];
899 };
900
901 struct scsi_vpd_id_eui64
902 {
903         u_int8_t        ieee_company_id[3];
904         u_int8_t        extension_id[5];
905 };
906
907 struct scsi_vpd_id_naa_basic
908 {
909         uint8_t naa;
910         /* big endian, packed:
911         uint8_t naa : 4;
912         uint8_t naa_desig : 4;
913         */
914 #define SVPD_ID_NAA_NAA_SHIFT           4
915 #define SVPD_ID_NAA_IEEE_EXT            0x02
916 #define SVPD_ID_NAA_LOCAL_REG           0x03
917 #define SVPD_ID_NAA_IEEE_REG            0x05
918 #define SVPD_ID_NAA_IEEE_REG_EXT        0x06
919         uint8_t naa_data[];
920 };
921
922 struct scsi_vpd_id_naa_ieee_extended_id
923 {
924         uint8_t naa;
925         uint8_t vendor_specific_id_a;
926         uint8_t ieee_company_id[3];
927         uint8_t vendor_specific_id_b[4];
928 };
929
930 struct scsi_vpd_id_naa_local_reg
931 {
932         uint8_t naa;
933         uint8_t local_value[7];
934 };
935
936 struct scsi_vpd_id_naa_ieee_reg
937 {
938         uint8_t naa;
939         uint8_t reg_value[7];
940         /* big endian, packed:
941         uint8_t naa_basic : 4;
942         uint8_t ieee_company_id_0 : 4;
943         uint8_t ieee_company_id_1[2];
944         uint8_t ieee_company_id_2 : 4;
945         uint8_t vendor_specific_id_0 : 4;
946         uint8_t vendor_specific_id_1[4];
947         */
948 };
949
950 struct scsi_vpd_id_naa_ieee_reg_extended
951 {
952         uint8_t naa;
953         uint8_t reg_value[15];
954         /* big endian, packed:
955         uint8_t naa_basic : 4;
956         uint8_t ieee_company_id_0 : 4;
957         uint8_t ieee_company_id_1[2];
958         uint8_t ieee_company_id_2 : 4;
959         uint8_t vendor_specific_id_0 : 4;
960         uint8_t vendor_specific_id_1[4];
961         uint8_t vendor_specific_id_ext[8];
962         */
963 };
964
965 struct scsi_vpd_id_rel_trgt_port_id
966 {
967         uint8_t obsolete[2];
968         uint8_t rel_trgt_port_id[2];
969 };
970
971 struct scsi_vpd_id_trgt_port_grp_id
972 {
973         uint8_t reserved[2];
974         uint8_t trgt_port_grp[2];
975 };
976
977 struct scsi_vpd_id_lun_grp_id
978 {
979         uint8_t reserved[2];
980         uint8_t log_unit_grp[2];
981 };
982
983 struct scsi_vpd_id_md5_lun_id
984 {
985         uint8_t lun_id[16];
986 };
987
988 struct scsi_vpd_id_scsi_name
989 {
990         uint8_t name_string[256];
991 };
992
993 struct scsi_read_capacity
994 {
995         u_int8_t opcode;
996         u_int8_t byte2;
997         u_int8_t addr[4];
998         u_int8_t unused[3];
999         u_int8_t control;
1000 };
1001
1002 struct scsi_read_capacity_16
1003 {
1004         uint8_t opcode;
1005 #define SRC16_SERVICE_ACTION    0x10
1006         uint8_t service_action;
1007         uint8_t addr[8];
1008         uint8_t alloc_len[4];
1009 #define SRC16_PMI               0x01
1010 #define SRC16_RELADR            0x02
1011         uint8_t reladr;
1012         uint8_t control;
1013 };
1014
1015 struct scsi_read_capacity_data
1016 {
1017         u_int8_t addr[4];
1018         u_int8_t length[4];
1019 };
1020
1021 struct scsi_read_capacity_data_long
1022 {
1023         uint8_t addr[8];
1024         uint8_t length[4];
1025 };
1026
1027 struct scsi_report_luns
1028 {
1029         uint8_t opcode;
1030         uint8_t reserved1;
1031 #define RPL_REPORT_DEFAULT      0x00
1032 #define RPL_REPORT_WELLKNOWN    0x01
1033 #define RPL_REPORT_ALL          0x02
1034         uint8_t select_report;
1035         uint8_t reserved2[3];
1036         uint8_t length[4];
1037         uint8_t reserved3;
1038         uint8_t control;
1039 };
1040
1041 struct scsi_report_luns_data {
1042         u_int8_t length[4];     /* length of LUN inventory, in bytes */
1043         u_int8_t reserved[4];   /* unused */
1044         /*
1045          * LUN inventory- we only support the type zero form for now.
1046          */
1047         struct {
1048                 u_int8_t lundata[8];
1049         } luns[0];
1050 };
1051 #define RPL_LUNDATA_PERIPH_BUS_MASK     0x3f
1052 #define RPL_LUNDATA_FLAT_LUN_MASK       0x3f
1053 #define RPL_LUNDATA_LUN_TARG_MASK       0x3f
1054 #define RPL_LUNDATA_LUN_BUS_MASK        0xe0
1055 #define RPL_LUNDATA_LUN_LUN_MASK        0x1f
1056 #define RPL_LUNDATA_EXT_LEN_MASK        0x30
1057 #define RPL_LUNDATA_EXT_EAM_MASK        0x0f
1058 #define RPL_LUNDATA_EXT_EAM_WK          0x01
1059 #define RPL_LUNDATA_EXT_EAM_NOT_SPEC    0x0f
1060 #define RPL_LUNDATA_ATYP_MASK   0xc0    /* MBZ for type 0 lun */
1061 #define RPL_LUNDATA_ATYP_PERIPH 0x00
1062 #define RPL_LUNDATA_ATYP_FLAT   0x40
1063 #define RPL_LUNDATA_ATYP_LUN    0x80
1064 #define RPL_LUNDATA_ATYP_EXTLUN 0xc0
1065
1066 struct scsi_target_group
1067 {
1068         uint8_t opcode;
1069         uint8_t service_action;
1070 #define STG_PDF_LENGTH          0x00
1071 #define RPL_PDF_EXTENDED        0x20
1072         uint8_t reserved1[4];
1073         uint8_t length[4];
1074         uint8_t reserved2;
1075         uint8_t control;
1076 };
1077
1078 struct scsi_target_port_descriptor {
1079         uint8_t reserved[2];
1080         uint8_t relative_target_port_identifier[2];
1081         uint8_t desc_list[];
1082 };
1083
1084 struct scsi_target_port_group_descriptor {
1085         uint8_t pref_state;
1086 #define TPG_PRIMARY                             0x80
1087 #define TPG_ASYMMETRIC_ACCESS_STATE_MASK        0xf
1088 #define TPG_ASYMMETRIC_ACCESS_OPTIMIZED         0x0
1089 #define TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED      0x1
1090 #define TPG_ASYMMETRIC_ACCESS_STANDBY           0x2
1091 #define TPG_ASYMMETRIC_ACCESS_UNAVAILABLE       0x3
1092 #define TPG_ASYMMETRIC_ACCESS_LBA_DEPENDENT     0x4
1093 #define TPG_ASYMMETRIC_ACCESS_OFFLINE           0xE
1094 #define TPG_ASYMMETRIC_ACCESS_TRANSITIONING     0xF
1095         uint8_t support;
1096 #define TPG_AO_SUP      0x01
1097 #define TPG_AN_SUP      0x02
1098 #define TPG_S_SUP       0x04
1099 #define TPG_U_SUP       0x08
1100 #define TPG_LBD_SUP     0x10
1101 #define TPG_O_SUP       0x40
1102 #define TPG_T_SUP       0x80
1103         uint8_t target_port_group[2];
1104         uint8_t reserved;
1105         uint8_t status;
1106         uint8_t vendor_specific;
1107         uint8_t target_port_count;
1108         struct scsi_target_port_descriptor descriptors[];
1109 };
1110
1111 struct scsi_target_group_data {
1112         uint8_t length[4];      /* length of returned data, in bytes */
1113         struct scsi_target_port_group_descriptor groups[];
1114 };
1115
1116 struct scsi_target_group_data_extended {
1117         uint8_t length[4];      /* length of returned data, in bytes */
1118         uint8_t format_type;    /* STG_PDF_LENGTH or RPL_PDF_EXTENDED */
1119         uint8_t implicit_transition_time;
1120         uint8_t reserved[2];
1121         struct scsi_target_port_group_descriptor groups[];
1122 };
1123
1124
1125 struct scsi_sense_data
1126 {
1127         u_int8_t error_code;
1128 #define SSD_ERRCODE                     0x7F
1129 #define         SSD_CURRENT_ERROR       0x70
1130 #define         SSD_DEFERRED_ERROR      0x71
1131 #define SSD_ERRCODE_VALID       0x80    
1132         u_int8_t segment;
1133         u_int8_t flags;
1134 #define SSD_KEY                         0x0F
1135 #define         SSD_KEY_NO_SENSE        0x00
1136 #define         SSD_KEY_RECOVERED_ERROR 0x01
1137 #define         SSD_KEY_NOT_READY       0x02
1138 #define         SSD_KEY_MEDIUM_ERROR    0x03
1139 #define         SSD_KEY_HARDWARE_ERROR  0x04
1140 #define         SSD_KEY_ILLEGAL_REQUEST 0x05
1141 #define         SSD_KEY_UNIT_ATTENTION  0x06
1142 #define         SSD_KEY_DATA_PROTECT    0x07
1143 #define         SSD_KEY_BLANK_CHECK     0x08
1144 #define         SSD_KEY_Vendor_Specific 0x09
1145 #define         SSD_KEY_COPY_ABORTED    0x0a
1146 #define         SSD_KEY_ABORTED_COMMAND 0x0b            
1147 #define         SSD_KEY_EQUAL           0x0c
1148 #define         SSD_KEY_VOLUME_OVERFLOW 0x0d
1149 #define         SSD_KEY_MISCOMPARE      0x0e
1150 #define         SSD_KEY_RESERVED        0x0f                    
1151 #define SSD_ILI         0x20
1152 #define SSD_EOM         0x40
1153 #define SSD_FILEMARK    0x80
1154         u_int8_t info[4];
1155         u_int8_t extra_len;
1156         u_int8_t cmd_spec_info[4];
1157         u_int8_t add_sense_code;
1158         u_int8_t add_sense_code_qual;
1159         u_int8_t fru;
1160         u_int8_t sense_key_spec[3];
1161 #define SSD_SCS_VALID           0x80
1162 #define SSD_FIELDPTR_CMD        0x40
1163 #define SSD_BITPTR_VALID        0x08
1164 #define SSD_BITPTR_VALUE        0x07
1165 #define SSD_MIN_SIZE 18
1166         u_int8_t extra_bytes[14];
1167 #define SSD_FULL_SIZE sizeof(struct scsi_sense_data)
1168 };
1169
1170 struct scsi_mode_header_6
1171 {
1172         u_int8_t data_length;   /* Sense data length */
1173         u_int8_t medium_type;
1174         u_int8_t dev_spec;
1175         u_int8_t blk_desc_len;
1176 };
1177
1178 struct scsi_mode_header_10
1179 {
1180         u_int8_t data_length[2];/* Sense data length */
1181         u_int8_t medium_type;
1182         u_int8_t dev_spec;
1183         u_int8_t unused[2];
1184         u_int8_t blk_desc_len[2];
1185 };
1186
1187 struct scsi_mode_page_header
1188 {
1189         u_int8_t page_code;
1190         u_int8_t page_length;
1191 };
1192
1193 struct scsi_mode_blk_desc
1194 {
1195         u_int8_t density;
1196         u_int8_t nblocks[3];
1197         u_int8_t reserved;
1198         u_int8_t blklen[3];
1199 };
1200
1201 #define SCSI_DEFAULT_DENSITY    0x00    /* use 'default' density */
1202 #define SCSI_SAME_DENSITY       0x7f    /* use 'same' density- >= SCSI-2 only */
1203
1204
1205 /*
1206  * Status Byte
1207  */
1208 #define SCSI_STATUS_OK                  0x00
1209 #define SCSI_STATUS_CHECK_COND          0x02
1210 #define SCSI_STATUS_COND_MET            0x04
1211 #define SCSI_STATUS_BUSY                0x08
1212 #define SCSI_STATUS_INTERMED            0x10
1213 #define SCSI_STATUS_INTERMED_COND_MET   0x14
1214 #define SCSI_STATUS_RESERV_CONFLICT     0x18
1215 #define SCSI_STATUS_CMD_TERMINATED      0x22    /* Obsolete in SAM-2 */
1216 #define SCSI_STATUS_QUEUE_FULL          0x28
1217 #define SCSI_STATUS_ACA_ACTIVE          0x30
1218 #define SCSI_STATUS_TASK_ABORTED        0x40
1219
1220 struct scsi_inquiry_pattern {
1221         u_int8_t   type;
1222         u_int8_t   media_type;
1223 #define SIP_MEDIA_REMOVABLE     0x01
1224 #define SIP_MEDIA_FIXED         0x02
1225         const char *vendor;
1226         const char *product;
1227         const char *revision;
1228 }; 
1229
1230 struct scsi_static_inquiry_pattern {
1231         u_int8_t   type;
1232         u_int8_t   media_type;
1233         char       vendor[SID_VENDOR_SIZE+1];
1234         char       product[SID_PRODUCT_SIZE+1];
1235         char       revision[SID_REVISION_SIZE+1];
1236 };
1237
1238 struct scsi_sense_quirk_entry {
1239         struct scsi_inquiry_pattern     inq_pat;
1240         int                             num_sense_keys;
1241         int                             num_ascs;
1242         struct sense_key_table_entry    *sense_key_info;
1243         struct asc_table_entry          *asc_info;
1244 };
1245
1246 struct sense_key_table_entry {
1247         u_int8_t    sense_key;
1248         u_int32_t   action;
1249         const char *desc;
1250 };
1251
1252 struct asc_table_entry {
1253         u_int8_t    asc;
1254         u_int8_t    ascq;
1255         u_int32_t   action;
1256         const char *desc;
1257 };
1258
1259 struct op_table_entry {
1260         u_int8_t    opcode;
1261         u_int32_t   opmask;
1262         const char  *desc;
1263 };
1264
1265 struct scsi_op_quirk_entry {
1266         struct scsi_inquiry_pattern     inq_pat;
1267         int                             num_ops;
1268         struct op_table_entry           *op_table;
1269 };
1270
1271 typedef enum {
1272         SSS_FLAG_NONE           = 0x00,
1273         SSS_FLAG_PRINT_COMMAND  = 0x01
1274 } scsi_sense_string_flags;
1275
1276 struct ccb_scsiio;
1277 struct cam_periph;
1278 union  ccb;
1279 #ifndef _KERNEL
1280 struct cam_device;
1281 #endif
1282
1283 extern const char *scsi_sense_key_text[];
1284
1285 struct sbuf;
1286
1287 __BEGIN_DECLS
1288 void scsi_sense_desc(int sense_key, int asc, int ascq,
1289                      struct scsi_inquiry_data *inq_data,
1290                      const char **sense_key_desc, const char **asc_desc);
1291 scsi_sense_action scsi_error_action(struct ccb_scsiio* csio,
1292                                     struct scsi_inquiry_data *inq_data,
1293                                     u_int32_t sense_flags);
1294 const char *    scsi_status_string(struct ccb_scsiio *csio);
1295 #ifdef _KERNEL
1296 int             scsi_command_string(struct ccb_scsiio *csio, struct sbuf *sb);
1297 int             scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
1298                                 scsi_sense_string_flags flags);
1299 char *          scsi_sense_string(struct ccb_scsiio *csio,
1300                                   char *str, int str_len);
1301 void            scsi_sense_print(struct ccb_scsiio *csio);
1302 int             scsi_interpret_sense(union ccb *ccb, 
1303                                      u_int32_t sense_flags,
1304                                      u_int32_t *relsim_flags, 
1305                                      u_int32_t *reduction,
1306                                      u_int32_t *timeout,
1307                                      scsi_sense_action error_action);
1308 #else /* _KERNEL */
1309 int             scsi_command_string(struct cam_device *device,
1310                                     struct ccb_scsiio *csio, struct sbuf *sb);
1311 int             scsi_sense_sbuf(struct cam_device *device, 
1312                                 struct ccb_scsiio *csio, struct sbuf *sb,
1313                                 scsi_sense_string_flags flags);
1314 char *          scsi_sense_string(struct cam_device *device, 
1315                                   struct ccb_scsiio *csio,
1316                                   char *str, int str_len);
1317 void            scsi_sense_print(struct cam_device *device, 
1318                                  struct ccb_scsiio *csio, FILE *ofile);
1319 int             scsi_interpret_sense(struct cam_device *device,
1320                                      union ccb *ccb,
1321                                      u_int32_t sense_flags,
1322                                      u_int32_t *relsim_flags, 
1323                                      u_int32_t *reduction,
1324                                      u_int32_t *timeout,
1325                                      scsi_sense_action error_action);
1326 #endif /* _KERNEL */
1327
1328 #define SF_RETRY_UA     0x01
1329 #define SF_NO_PRINT     0x02
1330 #define SF_QUIET_IR     0x04    /* Be quiet about Illegal Request reponses */
1331 #define SF_PRINT_ALWAYS 0x08
1332
1333
1334 const char *    scsi_op_desc(u_int16_t opcode, 
1335                              struct scsi_inquiry_data *inq_data);
1336 char *          scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string,
1337                                 size_t len);
1338
1339 void            scsi_print_inquiry(struct scsi_inquiry_data *inq_data);
1340
1341 u_int           scsi_calc_syncsrate(u_int period_factor);
1342 u_int           scsi_calc_syncparam(u_int period);
1343
1344 typedef int     (*scsi_devid_checkfn_t)(uint8_t *);
1345 int             scsi_devid_is_naa_ieee_reg(uint8_t *bufp);
1346 int             scsi_devid_is_sas_target(uint8_t *bufp);
1347 uint8_t *       scsi_get_devid(struct scsi_vpd_device_id *id, uint32_t len,
1348                                scsi_devid_checkfn_t ck_fn);
1349
1350 void            scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries,
1351                                      void (*cbfcnp)(struct cam_periph *, 
1352                                                     union ccb *),
1353                                      u_int8_t tag_action, 
1354                                      u_int8_t sense_len, u_int32_t timeout);
1355
1356 void            scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
1357                                    void (*cbfcnp)(struct cam_periph *, 
1358                                                   union ccb *),
1359                                    void *data_ptr, u_int8_t dxfer_len,
1360                                    u_int8_t tag_action, u_int8_t sense_len,
1361                                    u_int32_t timeout);
1362
1363 void            scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
1364                              void (*cbfcnp)(struct cam_periph *, union ccb *),
1365                              u_int8_t tag_action, u_int8_t *inq_buf, 
1366                              u_int32_t inq_len, int evpd, u_int8_t page_code,
1367                              u_int8_t sense_len, u_int32_t timeout);
1368
1369 void            scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
1370                                 void (*cbfcnp)(struct cam_periph *,
1371                                                union ccb *),
1372                                 u_int8_t tag_action, int dbd,
1373                                 u_int8_t page_code, u_int8_t page,
1374                                 u_int8_t *param_buf, u_int32_t param_len,
1375                                 u_int8_t sense_len, u_int32_t timeout);
1376
1377 void            scsi_mode_sense_len(struct ccb_scsiio *csio, u_int32_t retries,
1378                                     void (*cbfcnp)(struct cam_periph *,
1379                                                    union ccb *),
1380                                     u_int8_t tag_action, int dbd,
1381                                     u_int8_t page_code, u_int8_t page,
1382                                     u_int8_t *param_buf, u_int32_t param_len,
1383                                     int minimum_cmd_size, u_int8_t sense_len,
1384                                     u_int32_t timeout);
1385
1386 void            scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
1387                                  void (*cbfcnp)(struct cam_periph *,
1388                                                 union ccb *),
1389                                  u_int8_t tag_action, int scsi_page_fmt,
1390                                  int save_pages, u_int8_t *param_buf,
1391                                  u_int32_t param_len, u_int8_t sense_len,
1392                                  u_int32_t timeout);
1393
1394 void            scsi_mode_select_len(struct ccb_scsiio *csio, u_int32_t retries,
1395                                      void (*cbfcnp)(struct cam_periph *,
1396                                                     union ccb *),
1397                                      u_int8_t tag_action, int scsi_page_fmt,
1398                                      int save_pages, u_int8_t *param_buf,
1399                                      u_int32_t param_len, int minimum_cmd_size,
1400                                      u_int8_t sense_len, u_int32_t timeout);
1401
1402 void            scsi_log_sense(struct ccb_scsiio *csio, u_int32_t retries,
1403                                void (*cbfcnp)(struct cam_periph *, union ccb *),
1404                                u_int8_t tag_action, u_int8_t page_code,
1405                                u_int8_t page, int save_pages, int ppc,
1406                                u_int32_t paramptr, u_int8_t *param_buf,
1407                                u_int32_t param_len, u_int8_t sense_len,
1408                                u_int32_t timeout);
1409
1410 void            scsi_log_select(struct ccb_scsiio *csio, u_int32_t retries,
1411                                 void (*cbfcnp)(struct cam_periph *,
1412                                 union ccb *), u_int8_t tag_action,
1413                                 u_int8_t page_code, int save_pages,
1414                                 int pc_reset, u_int8_t *param_buf,
1415                                 u_int32_t param_len, u_int8_t sense_len,
1416                                 u_int32_t timeout);
1417
1418 void            scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
1419                              void (*cbfcnp)(struct cam_periph *, union ccb *),
1420                              u_int8_t tag_action, u_int8_t action,
1421                              u_int8_t sense_len, u_int32_t timeout);
1422
1423 void            scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
1424                                    void (*cbfcnp)(struct cam_periph *, 
1425                                    union ccb *), u_int8_t tag_action, 
1426                                    struct scsi_read_capacity_data *,
1427                                    u_int8_t sense_len, u_int32_t timeout);
1428 void            scsi_read_capacity_16(struct ccb_scsiio *csio, uint32_t retries,
1429                                       void (*cbfcnp)(struct cam_periph *,
1430                                       union ccb *), uint8_t tag_action,
1431                                       uint64_t lba, int reladr, int pmi,
1432                                       struct scsi_read_capacity_data_long
1433                                       *rcap_buf, uint8_t sense_len,
1434                                       uint32_t timeout);
1435
1436 void            scsi_report_luns(struct ccb_scsiio *csio, u_int32_t retries,
1437                                  void (*cbfcnp)(struct cam_periph *, 
1438                                  union ccb *), u_int8_t tag_action, 
1439                                  u_int8_t select_report,
1440                                  struct scsi_report_luns_data *rpl_buf,
1441                                  u_int32_t alloc_len, u_int8_t sense_len,
1442                                  u_int32_t timeout);
1443
1444 void            scsi_report_target_group(struct ccb_scsiio *csio, u_int32_t retries,
1445                                  void (*cbfcnp)(struct cam_periph *, 
1446                                  union ccb *), u_int8_t tag_action, 
1447                                  u_int8_t pdf,
1448                                  void *buf,
1449                                  u_int32_t alloc_len, u_int8_t sense_len,
1450                                  u_int32_t timeout);
1451
1452 void            scsi_set_target_group(struct ccb_scsiio *csio, u_int32_t retries,
1453                                  void (*cbfcnp)(struct cam_periph *, 
1454                                  union ccb *), u_int8_t tag_action, void *buf,
1455                                  u_int32_t alloc_len, u_int8_t sense_len,
1456                                  u_int32_t timeout);
1457
1458 void            scsi_synchronize_cache(struct ccb_scsiio *csio, 
1459                                        u_int32_t retries,
1460                                        void (*cbfcnp)(struct cam_periph *, 
1461                                        union ccb *), u_int8_t tag_action, 
1462                                        u_int32_t begin_lba, u_int16_t lb_count,
1463                                        u_int8_t sense_len, u_int32_t timeout);
1464
1465 void scsi_receive_diagnostic_results(struct ccb_scsiio *csio, u_int32_t retries,
1466                                      void (*cbfcnp)(struct cam_periph *,
1467                                                     union ccb*),
1468                                      uint8_t tag_action, int pcv,
1469                                      uint8_t page_code, uint8_t *data_ptr,
1470                                      uint16_t allocation_length,
1471                                      uint8_t sense_len, uint32_t timeout);
1472
1473 void scsi_send_diagnostic(struct ccb_scsiio *csio, u_int32_t retries,
1474                           void (*cbfcnp)(struct cam_periph *, union ccb *),
1475                           uint8_t tag_action, int unit_offline,
1476                           int device_offline, int self_test, int page_format,
1477                           int self_test_code, uint8_t *data_ptr,
1478                           uint16_t param_list_length, uint8_t sense_len,
1479                           uint32_t timeout);
1480
1481 void scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
1482                      void (*cbfcnp)(struct cam_periph *, union ccb *),
1483                      u_int8_t tag_action, int readop, u_int8_t byte2, 
1484                      int minimum_cmd_size, u_int64_t lba,
1485                      u_int32_t block_count, u_int8_t *data_ptr,
1486                      u_int32_t dxfer_len, u_int8_t sense_len,
1487                      u_int32_t timeout);
1488
1489 void scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
1490                      void (*cbfcnp)(struct cam_periph *, union ccb *),
1491                      u_int8_t tag_action, int start, int load_eject,
1492                      int immediate, u_int8_t sense_len, u_int32_t timeout);
1493
1494 int             scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry);
1495 int             scsi_static_inquiry_match(caddr_t inqbuffer,
1496                                           caddr_t table_entry);
1497 int             scsi_devid_match(uint8_t *rhs, size_t rhs_len,
1498                                  uint8_t *lhs, size_t lhs_len);
1499
1500 static __inline void scsi_extract_sense(struct scsi_sense_data *sense,
1501                                         int *error_code, int *sense_key,
1502                                         int *asc, int *ascq);
1503 static __inline void scsi_ulto2b(u_int32_t val, u_int8_t *bytes);
1504 static __inline void scsi_ulto3b(u_int32_t val, u_int8_t *bytes);
1505 static __inline void scsi_ulto4b(u_int32_t val, u_int8_t *bytes);
1506 static __inline void scsi_u64to8b(u_int64_t val, u_int8_t *bytes);
1507 static __inline u_int32_t scsi_2btoul(u_int8_t *bytes);
1508 static __inline u_int32_t scsi_3btoul(u_int8_t *bytes);
1509 static __inline int32_t scsi_3btol(u_int8_t *bytes);
1510 static __inline u_int32_t scsi_4btoul(u_int8_t *bytes);
1511 static __inline u_int64_t scsi_8btou64(u_int8_t *bytes);
1512 static __inline void *find_mode_page_6(struct scsi_mode_header_6 *mode_header);
1513 static __inline void *find_mode_page_10(struct scsi_mode_header_10 *mode_header);
1514
1515 static __inline void scsi_extract_sense(struct scsi_sense_data *sense,
1516                                         int *error_code, int *sense_key,
1517                                         int *asc, int *ascq)
1518 {
1519         *error_code = sense->error_code & SSD_ERRCODE;
1520         *sense_key = sense->flags & SSD_KEY;
1521         *asc = (sense->extra_len >= 5) ? sense->add_sense_code : 0;
1522         *ascq = (sense->extra_len >= 6) ? sense->add_sense_code_qual : 0;
1523 }
1524
1525 static __inline void
1526 scsi_ulto2b(u_int32_t val, u_int8_t *bytes)
1527 {
1528
1529         bytes[0] = (val >> 8) & 0xff;
1530         bytes[1] = val & 0xff;
1531 }
1532
1533 static __inline void
1534 scsi_ulto3b(u_int32_t val, u_int8_t *bytes)
1535 {
1536
1537         bytes[0] = (val >> 16) & 0xff;
1538         bytes[1] = (val >> 8) & 0xff;
1539         bytes[2] = val & 0xff;
1540 }
1541
1542 static __inline void
1543 scsi_ulto4b(u_int32_t val, u_int8_t *bytes)
1544 {
1545
1546         bytes[0] = (val >> 24) & 0xff;
1547         bytes[1] = (val >> 16) & 0xff;
1548         bytes[2] = (val >> 8) & 0xff;
1549         bytes[3] = val & 0xff;
1550 }
1551
1552 static __inline void
1553 scsi_u64to8b(u_int64_t val, u_int8_t *bytes)
1554 {
1555
1556         bytes[0] = (val >> 56) & 0xff;
1557         bytes[1] = (val >> 48) & 0xff;
1558         bytes[2] = (val >> 40) & 0xff;
1559         bytes[3] = (val >> 32) & 0xff;
1560         bytes[4] = (val >> 24) & 0xff;
1561         bytes[5] = (val >> 16) & 0xff;
1562         bytes[6] = (val >> 8) & 0xff;
1563         bytes[7] = val & 0xff;
1564 }
1565
1566 static __inline u_int32_t
1567 scsi_2btoul(u_int8_t *bytes)
1568 {
1569         u_int32_t rv;
1570
1571         rv = (bytes[0] << 8) |
1572              bytes[1];
1573         return (rv);
1574 }
1575
1576 static __inline u_int32_t
1577 scsi_3btoul(u_int8_t *bytes)
1578 {
1579         u_int32_t rv;
1580
1581         rv = (bytes[0] << 16) |
1582              (bytes[1] << 8) |
1583              bytes[2];
1584         return (rv);
1585 }
1586
1587 static __inline int32_t 
1588 scsi_3btol(u_int8_t *bytes)
1589 {
1590         u_int32_t rc = scsi_3btoul(bytes);
1591  
1592         if (rc & 0x00800000)
1593                 rc |= 0xff000000;
1594
1595         return (int32_t) rc;
1596 }
1597
1598 static __inline u_int32_t
1599 scsi_4btoul(u_int8_t *bytes)
1600 {
1601         u_int32_t rv;
1602
1603         rv = (bytes[0] << 24) |
1604              (bytes[1] << 16) |
1605              (bytes[2] << 8) |
1606              bytes[3];
1607         return (rv);
1608 }
1609
1610 static __inline uint64_t
1611 scsi_8btou64(uint8_t *bytes)
1612 {
1613         uint64_t rv;
1614  
1615         rv = (((uint64_t)bytes[0]) << 56) |
1616              (((uint64_t)bytes[1]) << 48) |
1617              (((uint64_t)bytes[2]) << 40) |
1618              (((uint64_t)bytes[3]) << 32) |
1619              (((uint64_t)bytes[4]) << 24) |
1620              (((uint64_t)bytes[5]) << 16) |
1621              (((uint64_t)bytes[6]) << 8) |
1622              bytes[7];
1623         return (rv);
1624 }
1625
1626 /*
1627  * Given the pointer to a returned mode sense buffer, return a pointer to
1628  * the start of the first mode page.
1629  */
1630 static __inline void *
1631 find_mode_page_6(struct scsi_mode_header_6 *mode_header)
1632 {
1633         void *page_start;
1634
1635         page_start = (void *)((u_int8_t *)&mode_header[1] +
1636                               mode_header->blk_desc_len);
1637
1638         return(page_start);
1639 }
1640
1641 static __inline void *
1642 find_mode_page_10(struct scsi_mode_header_10 *mode_header)
1643 {
1644         void *page_start;
1645
1646         page_start = (void *)((u_int8_t *)&mode_header[1] +
1647                                scsi_2btoul(mode_header->blk_desc_len));
1648
1649         return(page_start);
1650 }
1651
1652 __END_DECLS
1653
1654 #endif /*_SCSI_SCSI_ALL_H*/