]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/cam/scsi/scsi_all.h
MFC r249352:
[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 #include <machine/stdarg.h>
29
30 #ifdef _KERNEL
31 /*
32  * This is the number of seconds we wait for devices to settle after a SCSI
33  * bus reset.
34  */
35 extern int scsi_delay;
36 #endif /* _KERNEL */
37
38 /*
39  * SCSI command format
40  */
41
42 /*
43  * Define dome bits that are in ALL (or a lot of) scsi commands
44  */
45 #define SCSI_CTL_LINK           0x01
46 #define SCSI_CTL_FLAG           0x02
47 #define SCSI_CTL_VENDOR         0xC0
48 #define SCSI_CMD_LUN            0xA0    /* these two should not be needed */
49 #define SCSI_CMD_LUN_SHIFT      5       /* LUN in the cmd is no longer SCSI */
50
51 #define SCSI_MAX_CDBLEN         16      /* 
52                                          * 16 byte commands are in the 
53                                          * SCSI-3 spec 
54                                          */
55 #if defined(CAM_MAX_CDBLEN) && (CAM_MAX_CDBLEN < SCSI_MAX_CDBLEN)
56 #error "CAM_MAX_CDBLEN cannot be less than SCSI_MAX_CDBLEN"
57 #endif
58
59 /* 6byte CDBs special case 0 length to be 256 */
60 #define SCSI_CDB6_LEN(len)      ((len) == 0 ? 256 : len)
61
62 /*
63  * This type defines actions to be taken when a particular sense code is
64  * received.  Right now, these flags are only defined to take up 16 bits,
65  * but can be expanded in the future if necessary.
66  */
67 typedef enum {
68         SS_NOP      = 0x000000, /* Do nothing */
69         SS_RETRY    = 0x010000, /* Retry the command */
70         SS_FAIL     = 0x020000, /* Bail out */
71         SS_START    = 0x030000, /* Send a Start Unit command to the device,
72                                  * then retry the original command.
73                                  */
74         SS_TUR      = 0x040000, /* Send a Test Unit Ready command to the
75                                  * device, then retry the original command.
76                                  */
77         SS_MASK     = 0xff0000
78 } scsi_sense_action;
79
80 typedef enum {
81         SSQ_NONE                = 0x0000,
82         SSQ_DECREMENT_COUNT     = 0x0100,  /* Decrement the retry count */
83         SSQ_MANY                = 0x0200,  /* send lots of recovery commands */
84         SSQ_RANGE               = 0x0400,  /*
85                                             * This table entry represents the
86                                             * end of a range of ASCQs that
87                                             * have identical error actions
88                                             * and text.
89                                             */
90         SSQ_PRINT_SENSE         = 0x0800,
91         SSQ_MASK                = 0xff00
92 } scsi_sense_action_qualifier;
93
94 /* Mask for error status values */
95 #define SS_ERRMASK      0xff
96
97 /* The default, retyable, error action */
98 #define SS_RDEF         SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE|EIO
99
100 /* The retyable, error action, with table specified error code */
101 #define SS_RET          SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE
102
103 /* Fatal error action, with table specified error code */
104 #define SS_FATAL        SS_FAIL|SSQ_PRINT_SENSE
105
106 struct scsi_generic
107 {
108         u_int8_t opcode;
109         u_int8_t bytes[11];
110 };
111
112 struct scsi_request_sense
113 {
114         u_int8_t opcode;
115         u_int8_t byte2;
116 #define SRS_DESC        0x01
117         u_int8_t unused[2];
118         u_int8_t length;
119         u_int8_t control;
120 };
121
122 struct scsi_test_unit_ready
123 {
124         u_int8_t opcode;
125         u_int8_t byte2;
126         u_int8_t unused[3];
127         u_int8_t control;
128 };
129
130 struct scsi_receive_diag {
131         uint8_t opcode;
132         uint8_t byte2;
133 #define SRD_PCV         0x01
134         uint8_t page_code;
135         uint8_t length[2]; 
136         uint8_t control;
137 };
138
139 struct scsi_send_diag {
140         uint8_t opcode;
141         uint8_t byte2;
142 #define SSD_UNITOFFL                            0x01
143 #define SSD_DEVOFFL                             0x02
144 #define SSD_SELFTEST                            0x04
145 #define SSD_PF                                  0x10
146 #define SSD_SELF_TEST_CODE_MASK                 0xE0
147 #define SSD_SELF_TEST_CODE_SHIFT                5
148 #define         SSD_SELF_TEST_CODE_NONE         0x00
149 #define         SSD_SELF_TEST_CODE_BG_SHORT     0x01
150 #define         SSD_SELF_TEST_CODE_BG_EXTENDED  0x02
151 #define         SSD_SELF_TEST_CODE_BG_ABORT     0x04
152 #define         SSD_SELF_TEST_CODE_FG_SHORT     0x05
153 #define         SSD_SELF_TEST_CODE_FG_EXTENDED  0x06
154         uint8_t reserved;
155         uint8_t length[2];
156         uint8_t control;
157 };
158
159 struct scsi_sense
160 {
161         u_int8_t opcode;
162         u_int8_t byte2;
163         u_int8_t unused[2];
164         u_int8_t length;
165         u_int8_t control;
166 };
167
168 struct scsi_inquiry
169 {
170         u_int8_t opcode;
171         u_int8_t byte2;
172 #define SI_EVPD         0x01
173 #define SI_CMDDT        0x02
174         u_int8_t page_code;
175         u_int8_t length[2];
176         u_int8_t control;
177 };
178
179 struct scsi_mode_sense_6
180 {
181         u_int8_t opcode;
182         u_int8_t byte2;
183 #define SMS_DBD                         0x08
184         u_int8_t page;
185 #define SMS_PAGE_CODE                   0x3F
186 #define SMS_VENDOR_SPECIFIC_PAGE        0x00
187 #define SMS_DISCONNECT_RECONNECT_PAGE   0x02
188 #define SMS_FORMAT_DEVICE_PAGE          0x03
189 #define SMS_GEOMETRY_PAGE               0x04
190 #define SMS_CACHE_PAGE                  0x08
191 #define SMS_PERIPHERAL_DEVICE_PAGE      0x09
192 #define SMS_CONTROL_MODE_PAGE           0x0A
193 #define SMS_PROTO_SPECIFIC_PAGE         0x19
194 #define SMS_INFO_EXCEPTIONS_PAGE        0x1C
195 #define SMS_ALL_PAGES_PAGE              0x3F
196 #define SMS_PAGE_CTRL_MASK              0xC0
197 #define SMS_PAGE_CTRL_CURRENT           0x00
198 #define SMS_PAGE_CTRL_CHANGEABLE        0x40
199 #define SMS_PAGE_CTRL_DEFAULT           0x80
200 #define SMS_PAGE_CTRL_SAVED             0xC0
201         u_int8_t subpage;
202 #define SMS_SUBPAGE_PAGE_0              0x00
203 #define SMS_SUBPAGE_ALL                 0xff
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 #define SMS10_LLBAA                     0x10
213         u_int8_t page;          /* same bits as small version */
214         u_int8_t subpage;
215         u_int8_t unused[3];
216         u_int8_t length[2];
217         u_int8_t control;
218 };
219
220 struct scsi_mode_select_6
221 {
222         u_int8_t opcode;
223         u_int8_t byte2;
224 #define SMS_SP  0x01
225 #define SMS_PF  0x10
226         u_int8_t unused[2];
227         u_int8_t length;
228         u_int8_t control;
229 };
230
231 struct scsi_mode_select_10
232 {
233         u_int8_t opcode;
234         u_int8_t byte2;         /* same bits as small version */
235         u_int8_t unused[5];
236         u_int8_t length[2];
237         u_int8_t control;
238 };
239
240 /*
241  * When sending a mode select to a tape drive, the medium type must be 0.
242  */
243 struct scsi_mode_hdr_6
244 {
245         u_int8_t datalen;
246         u_int8_t medium_type;
247         u_int8_t dev_specific;
248         u_int8_t block_descr_len;
249 };
250
251 struct scsi_mode_hdr_10
252 {
253         u_int8_t datalen[2];
254         u_int8_t medium_type;
255         u_int8_t dev_specific;
256         u_int8_t reserved[2];
257         u_int8_t block_descr_len[2];
258 };
259
260 struct scsi_mode_block_descr
261 {
262         u_int8_t density_code;
263         u_int8_t num_blocks[3];
264         u_int8_t reserved;
265         u_int8_t block_len[3];
266 };
267
268 struct scsi_per_res_in
269 {
270         u_int8_t opcode;
271         u_int8_t action;
272 #define SPRI_RK 0x00
273 #define SPRI_RR 0x01
274 #define SPRI_RC 0x02
275 #define SPRI_RS 0x03
276         u_int8_t reserved[5];
277         u_int8_t length[2];
278         u_int8_t control;
279 };
280
281 struct scsi_per_res_in_header
282 {
283         u_int8_t generation[4];
284         u_int8_t length[4];
285 };
286
287 struct scsi_per_res_key
288 {
289         u_int8_t key[8];
290 };
291
292 struct scsi_per_res_in_keys
293 {
294         struct scsi_per_res_in_header header;
295         struct scsi_per_res_key keys[0];
296 };
297
298 struct scsi_per_res_cap
299 {
300         uint8_t length[2];
301         uint8_t flags1;
302 #define SPRI_CRH        0x10
303 #define SPRI_SIP_C      0x08
304 #define SPRI_ATP_C      0x04
305 #define SPRI_PTPL_C     0x01
306         uint8_t flags2;
307 #define SPRI_TMV        0x80
308 #define SPRI_PTPL_A     0x01
309         uint8_t type_mask[2];
310 #define SPRI_TM_WR_EX_AR        0x8000
311 #define SPRI_TM_EX_AC_RO        0x4000
312 #define SPRI_TM_WR_EX_RO        0x2000
313 #define SPRI_TM_EX_AC           0x0800
314 #define SPRI_TM_WR_EX           0x0200
315 #define SPRI_TM_EX_AC_AR        0x0001
316         uint8_t reserved[2];
317 };
318
319 struct scsi_per_res_in_rsrv_data
320 {
321         uint8_t reservation[8];
322         uint8_t obsolete1[4];
323         uint8_t reserved;
324         uint8_t scopetype;
325 #define SPRT_WE    0x01
326 #define SPRT_EA    0x03
327 #define SPRT_WERO  0x05
328 #define SPRT_EARO  0x06
329 #define SPRT_WEAR  0x07
330 #define SPRT_EAAR  0x08
331         uint8_t obsolete2[2];
332 };
333
334 struct scsi_per_res_in_rsrv
335 {
336         struct scsi_per_res_in_header header;
337         struct scsi_per_res_in_rsrv_data data;
338 };
339
340 struct scsi_per_res_out
341 {
342         u_int8_t opcode;
343         u_int8_t action;
344 #define SPRO_REGISTER           0x00
345 #define SPRO_RESERVE            0x01
346 #define SPRO_RELEASE            0x02
347 #define SPRO_CLEAR              0x03
348 #define SPRO_PREEMPT            0x04
349 #define SPRO_PRE_ABO            0x05
350 #define SPRO_REG_IGNO           0x06
351 #define SPRO_REG_MOVE           0x07
352 #define SPRO_ACTION_MASK        0x1f
353         u_int8_t scope_type;
354 #define SPR_SCOPE_MASK          0xf0
355 #define SPR_LU_SCOPE            0x00
356 #define SPR_TYPE_MASK           0x0f
357 #define SPR_TYPE_WR_EX          0x01
358 #define SPR_TYPE_EX_AC          0x03
359 #define SPR_TYPE_WR_EX_RO       0x05
360 #define SPR_TYPE_EX_AC_RO       0x06
361 #define SPR_TYPE_WR_EX_AR       0x07
362 #define SPR_TYPE_EX_AC_AR       0x08
363         u_int8_t reserved[2];
364         u_int8_t length[4];
365         u_int8_t control;
366 };
367
368 struct scsi_per_res_out_parms
369 {
370         struct scsi_per_res_key res_key;
371         u_int8_t serv_act_res_key[8];
372         u_int8_t obsolete1[4];
373         u_int8_t flags;
374 #define SPR_SPEC_I_PT           0x08
375 #define SPR_ALL_TG_PT           0x04
376 #define SPR_APTPL               0x01
377         u_int8_t reserved1;
378         u_int8_t obsolete2[2];
379 };
380
381
382 struct scsi_log_sense
383 {
384         u_int8_t opcode;
385         u_int8_t byte2;
386 #define SLS_SP                          0x01
387 #define SLS_PPC                         0x02
388         u_int8_t page;
389 #define SLS_PAGE_CODE                   0x3F
390 #define SLS_ALL_PAGES_PAGE              0x00
391 #define SLS_OVERRUN_PAGE                0x01
392 #define SLS_ERROR_WRITE_PAGE            0x02
393 #define SLS_ERROR_READ_PAGE             0x03
394 #define SLS_ERROR_READREVERSE_PAGE      0x04
395 #define SLS_ERROR_VERIFY_PAGE           0x05
396 #define SLS_ERROR_NONMEDIUM_PAGE        0x06
397 #define SLS_ERROR_LASTN_PAGE            0x07
398 #define SLS_SELF_TEST_PAGE              0x10
399 #define SLS_IE_PAGE                     0x2f
400 #define SLS_PAGE_CTRL_MASK              0xC0
401 #define SLS_PAGE_CTRL_THRESHOLD         0x00
402 #define SLS_PAGE_CTRL_CUMULATIVE        0x40
403 #define SLS_PAGE_CTRL_THRESH_DEFAULT    0x80
404 #define SLS_PAGE_CTRL_CUMUL_DEFAULT     0xC0
405         u_int8_t reserved[2];
406         u_int8_t paramptr[2];
407         u_int8_t length[2];
408         u_int8_t control;
409 };
410
411 struct scsi_log_select
412 {
413         u_int8_t opcode;
414         u_int8_t byte2;
415 /*      SLS_SP                          0x01 */
416 #define SLS_PCR                         0x02
417         u_int8_t page;
418 /*      SLS_PAGE_CTRL_MASK              0xC0 */
419 /*      SLS_PAGE_CTRL_THRESHOLD         0x00 */
420 /*      SLS_PAGE_CTRL_CUMULATIVE        0x40 */
421 /*      SLS_PAGE_CTRL_THRESH_DEFAULT    0x80 */
422 /*      SLS_PAGE_CTRL_CUMUL_DEFAULT     0xC0 */
423         u_int8_t reserved[4];
424         u_int8_t length[2];
425         u_int8_t control;
426 };
427
428 struct scsi_log_header
429 {
430         u_int8_t page;
431         u_int8_t reserved;
432         u_int8_t datalen[2];
433 };
434
435 struct scsi_log_param_header {
436         u_int8_t param_code[2];
437         u_int8_t param_control;
438 #define SLP_LP                          0x01
439 #define SLP_LBIN                        0x02
440 #define SLP_TMC_MASK                    0x0C
441 #define SLP_TMC_ALWAYS                  0x00
442 #define SLP_TMC_EQUAL                   0x04
443 #define SLP_TMC_NOTEQUAL                0x08
444 #define SLP_TMC_GREATER                 0x0C
445 #define SLP_ETC                         0x10
446 #define SLP_TSD                         0x20
447 #define SLP_DS                          0x40
448 #define SLP_DU                          0x80
449         u_int8_t param_len;
450 };
451
452 struct scsi_control_page {
453         u_int8_t page_code;
454         u_int8_t page_length;
455         u_int8_t rlec;
456 #define SCP_RLEC                        0x01    /*Report Log Exception Cond*/
457 #define SCP_GLTSD                       0x02    /*Global Logging target
458                                                   save disable */
459 #define SCP_DSENSE                      0x04    /*Descriptor Sense */
460 #define SCP_DPICZ                       0x08    /*Disable Prot. Info Check
461                                                   if Prot. Field is Zero */
462 #define SCP_TMF_ONLY                    0x10    /*TM Functions Only*/
463 #define SCP_TST_MASK                    0xE0    /*Task Set Type Mask*/
464 #define SCP_TST_ONE                     0x00    /*One Task Set*/
465 #define SCP_TST_SEPARATE                0x20    /*Separate Task Sets*/
466         u_int8_t queue_flags;
467 #define SCP_QUEUE_ALG_MASK              0xF0
468 #define SCP_QUEUE_ALG_RESTRICTED        0x00
469 #define SCP_QUEUE_ALG_UNRESTRICTED      0x10
470 #define SCP_QUEUE_ERR                   0x02    /*Queued I/O aborted for CACs*/
471 #define SCP_QUEUE_DQUE                  0x01    /*Queued I/O disabled*/
472         u_int8_t eca_and_aen;
473 #define SCP_EECA                        0x80    /*Enable Extended CA*/
474 #define SCP_RAENP                       0x04    /*Ready AEN Permission*/
475 #define SCP_UAAENP                      0x02    /*UA AEN Permission*/
476 #define SCP_EAENP                       0x01    /*Error AEN Permission*/
477         u_int8_t reserved;
478         u_int8_t aen_holdoff_period[2];
479 };
480
481 struct scsi_cache_page {
482         u_int8_t page_code;
483 #define SCHP_PAGE_SAVABLE               0x80    /* Page is savable */
484         u_int8_t page_length;
485         u_int8_t cache_flags;
486 #define SCHP_FLAGS_WCE                  0x04    /* Write Cache Enable */
487 #define SCHP_FLAGS_MF                   0x02    /* Multiplication factor */
488 #define SCHP_FLAGS_RCD                  0x01    /* Read Cache Disable */
489         u_int8_t rw_cache_policy;
490         u_int8_t dis_prefetch[2];
491         u_int8_t min_prefetch[2];
492         u_int8_t max_prefetch[2];
493         u_int8_t max_prefetch_ceil[2];
494 };
495
496 /*
497  * XXX KDM
498  * Updated version of the cache page, as of SBC.  Update this to SBC-3 and
499  * rationalize the two.
500  */
501 struct scsi_caching_page {
502         uint8_t page_code;
503 #define SMS_CACHING_PAGE                0x08
504         uint8_t page_length;
505         uint8_t flags1;
506 #define SCP_IC          0x80
507 #define SCP_ABPF        0x40
508 #define SCP_CAP         0x20
509 #define SCP_DISC        0x10
510 #define SCP_SIZE        0x08
511 #define SCP_WCE         0x04
512 #define SCP_MF          0x02
513 #define SCP_RCD         0x01
514         uint8_t ret_priority;
515         uint8_t disable_pf_transfer_len[2];
516         uint8_t min_prefetch[2];
517         uint8_t max_prefetch[2];
518         uint8_t max_pf_ceiling[2];
519         uint8_t flags2;
520 #define SCP_FSW         0x80
521 #define SCP_LBCSS       0x40
522 #define SCP_DRA         0x20
523 #define SCP_VS1         0x10
524 #define SCP_VS2         0x08
525         uint8_t cache_segments;
526         uint8_t cache_seg_size[2];
527         uint8_t reserved;
528         uint8_t non_cache_seg_size[3];
529 };
530
531 /*
532  * XXX KDM move this off to a vendor shim.
533  */
534 struct copan_power_subpage {
535         uint8_t page_code;
536 #define PWR_PAGE_CODE           0x00
537         uint8_t subpage;
538 #define PWR_SUBPAGE_CODE        0x02
539         uint8_t page_length[2];
540         uint8_t page_version;
541 #define PWR_VERSION                 0x01
542         uint8_t total_luns;
543         uint8_t max_active_luns;
544 #define PWR_DFLT_MAX_LUNS           0x07
545         uint8_t reserved[25];
546 };
547
548 /*
549  * XXX KDM move this off to a vendor shim.
550  */
551 struct copan_aps_subpage {
552         uint8_t page_code;
553 #define APS_PAGE_CODE           0x00
554         uint8_t subpage;
555 #define APS_SUBPAGE_CODE        0x03
556         uint8_t page_length[2];
557         uint8_t page_version;
558 #define APS_VERSION                 0x00
559         uint8_t lock_active;
560 #define APS_LOCK_ACTIVE     0x01
561 #define APS_LOCK_INACTIVE       0x00
562         uint8_t reserved[26];
563 };
564
565 /*
566  * XXX KDM move this off to a vendor shim.
567  */
568 struct copan_debugconf_subpage {
569         uint8_t page_code;
570 #define DBGCNF_PAGE_CODE                0x00
571         uint8_t subpage;
572 #define DBGCNF_SUBPAGE_CODE     0xF0
573         uint8_t page_length[2];
574         uint8_t page_version;
575 #define DBGCNF_VERSION                  0x00
576         uint8_t ctl_time_io_secs[2];
577 };
578
579
580 struct scsi_info_exceptions_page {
581         u_int8_t page_code;
582 #define SIEP_PAGE_SAVABLE               0x80    /* Page is savable */
583         u_int8_t page_length;
584         u_int8_t info_flags;
585 #define SIEP_FLAGS_PERF                 0x80
586 #define SIEP_FLAGS_EBF                  0x20
587 #define SIEP_FLAGS_EWASC                0x10
588 #define SIEP_FLAGS_DEXCPT               0x08
589 #define SIEP_FLAGS_TEST                 0x04
590 #define SIEP_FLAGS_EBACKERR             0x02
591 #define SIEP_FLAGS_LOGERR               0x01
592         u_int8_t mrie;
593         u_int8_t interval_timer[4];
594         u_int8_t report_count[4];
595 };
596
597 struct scsi_proto_specific_page {
598         u_int8_t page_code;
599 #define SPSP_PAGE_SAVABLE               0x80    /* Page is savable */
600         u_int8_t page_length;
601         u_int8_t protocol;
602 #define SPSP_PROTO_FC                   0x00
603 #define SPSP_PROTO_SPI                  0x01
604 #define SPSP_PROTO_SSA                  0x02
605 #define SPSP_PROTO_1394                 0x03
606 #define SPSP_PROTO_RDMA                 0x04
607 #define SPSP_PROTO_ISCSI                0x05
608 #define SPSP_PROTO_SAS                  0x06
609 #define SPSP_PROTO_ADT                  0x07
610 #define SPSP_PROTO_ATA                  0x08
611 #define SPSP_PROTO_NONE                 0x0f
612 };
613
614 struct scsi_reserve
615 {
616         u_int8_t opcode;
617         u_int8_t byte2;
618 #define SR_EXTENT       0x01
619 #define SR_ID_MASK      0x0e
620 #define SR_3RDPTY       0x10
621 #define SR_LUN_MASK     0xe0
622         u_int8_t resv_id;
623         u_int8_t length[2];
624         u_int8_t control;
625 };
626
627 struct scsi_reserve_10 {
628         uint8_t opcode;
629         uint8_t byte2;
630 #define SR10_3RDPTY     0x10
631 #define SR10_LONGID     0x02
632 #define SR10_EXTENT     0x01
633         uint8_t resv_id;
634         uint8_t thirdparty_id;
635         uint8_t reserved[3];
636         uint8_t length[2];
637         uint8_t control;
638 };
639
640
641 struct scsi_release
642 {
643         u_int8_t opcode;
644         u_int8_t byte2;
645         u_int8_t resv_id;
646         u_int8_t unused[1];
647         u_int8_t length;
648         u_int8_t control;
649 };
650
651 struct scsi_release_10 {
652         uint8_t opcode;
653         uint8_t byte2;
654         uint8_t resv_id;
655         uint8_t thirdparty_id;
656         uint8_t reserved[3];
657         uint8_t length[2];
658         uint8_t control;
659 };
660
661 struct scsi_prevent
662 {
663         u_int8_t opcode;
664         u_int8_t byte2;
665         u_int8_t unused[2];
666         u_int8_t how;
667         u_int8_t control;
668 };
669 #define PR_PREVENT 0x01
670 #define PR_ALLOW   0x00
671
672 struct scsi_sync_cache
673 {
674         u_int8_t opcode;
675         u_int8_t byte2;
676 #define SSC_IMMED       0x02
677 #define SSC_RELADR      0x01
678         u_int8_t begin_lba[4];
679         u_int8_t reserved;
680         u_int8_t lb_count[2];
681         u_int8_t control;       
682 };
683
684 struct scsi_sync_cache_16
685 {
686         uint8_t opcode;
687         uint8_t byte2;
688         uint8_t begin_lba[8];
689         uint8_t lb_count[4];
690         uint8_t reserved;
691         uint8_t control;
692 };
693
694 struct scsi_format {
695         uint8_t opcode;
696         uint8_t byte2;
697 #define SF_LONGLIST             0x20
698 #define SF_FMTDATA              0x10
699 #define SF_CMPLIST              0x08
700 #define SF_FORMAT_MASK          0x07
701 #define SF_FORMAT_BLOCK         0x00
702 #define SF_FORMAT_LONG_BLOCK    0x03
703 #define SF_FORMAT_BFI           0x04
704 #define SF_FORMAT_PHYS          0x05
705         uint8_t vendor;
706         uint8_t interleave[2];
707         uint8_t control;
708 };
709
710 struct scsi_format_header_short {
711         uint8_t reserved;
712 #define SF_DATA_FOV     0x80
713 #define SF_DATA_DPRY    0x40
714 #define SF_DATA_DCRT    0x20
715 #define SF_DATA_STPF    0x10
716 #define SF_DATA_IP      0x08
717 #define SF_DATA_DSP     0x04
718 #define SF_DATA_IMMED   0x02
719 #define SF_DATA_VS      0x01
720         uint8_t byte2;
721         uint8_t defect_list_len[2];
722 };
723
724 struct scsi_format_header_long {
725         uint8_t reserved;
726         uint8_t byte2;
727         uint8_t reserved2[2];
728         uint8_t defect_list_len[4];
729 };
730
731 struct scsi_changedef
732 {
733         u_int8_t opcode;
734         u_int8_t byte2;
735         u_int8_t unused1;
736         u_int8_t how;
737         u_int8_t unused[4];
738         u_int8_t datalen;
739         u_int8_t control;
740 };
741
742 struct scsi_read_buffer
743 {
744         u_int8_t opcode;
745         u_int8_t byte2;
746 #define RWB_MODE                0x07
747 #define RWB_MODE_HDR_DATA       0x00
748 #define RWB_MODE_VENDOR         0x01
749 #define RWB_MODE_DATA           0x02
750 #define RWB_MODE_DOWNLOAD       0x04
751 #define RWB_MODE_DOWNLOAD_SAVE  0x05
752         u_int8_t buffer_id;
753         u_int8_t offset[3];
754         u_int8_t length[3];
755         u_int8_t control;
756 };
757
758 struct scsi_write_buffer
759 {
760         u_int8_t opcode;
761         u_int8_t byte2;
762         u_int8_t buffer_id;
763         u_int8_t offset[3];
764         u_int8_t length[3];
765         u_int8_t control;
766 };
767
768 struct scsi_rw_6
769 {
770         u_int8_t opcode;
771         u_int8_t addr[3];
772 /* only 5 bits are valid in the MSB address byte */
773 #define SRW_TOPADDR     0x1F
774         u_int8_t length;
775         u_int8_t control;
776 };
777
778 struct scsi_rw_10
779 {
780         u_int8_t opcode;
781 #define SRW10_RELADDR   0x01
782 /* EBP defined for WRITE(10) only */
783 #define SRW10_EBP       0x04
784 #define SRW10_FUA       0x08
785 #define SRW10_DPO       0x10
786         u_int8_t byte2;
787         u_int8_t addr[4];
788         u_int8_t reserved;
789         u_int8_t length[2];
790         u_int8_t control;
791 };
792
793 struct scsi_rw_12
794 {
795         u_int8_t opcode;
796 #define SRW12_RELADDR   0x01
797 #define SRW12_FUA       0x08
798 #define SRW12_DPO       0x10
799         u_int8_t byte2;
800         u_int8_t addr[4];
801         u_int8_t length[4];
802         u_int8_t reserved;
803         u_int8_t control;
804 };
805
806 struct scsi_rw_16
807 {
808         u_int8_t opcode;
809 #define SRW16_RELADDR   0x01
810 #define SRW16_FUA       0x08
811 #define SRW16_DPO       0x10
812         u_int8_t byte2;
813         u_int8_t addr[8];
814         u_int8_t length[4];
815         u_int8_t reserved;
816         u_int8_t control;
817 };
818
819 struct scsi_write_same_10
820 {
821         uint8_t opcode;
822         uint8_t byte2;
823 #define SWS_LBDATA      0x02
824 #define SWS_PBDATA      0x04
825 #define SWS_UNMAP       0x08
826 #define SWS_ANCHOR      0x10
827         uint8_t addr[4];
828         uint8_t group;
829         uint8_t length[2];
830         uint8_t control;
831 };
832
833 struct scsi_write_same_16
834 {
835         uint8_t opcode;
836         uint8_t byte2;
837         uint8_t addr[8];
838         uint8_t length[4];
839         uint8_t group;
840         uint8_t control;
841 };
842
843 struct scsi_unmap
844 {
845         uint8_t opcode;
846         uint8_t byte2;
847 #define SU_ANCHOR       0x01
848         uint8_t reserved[4];
849         uint8_t group;
850         uint8_t length[2];
851         uint8_t control;
852 };
853
854 struct scsi_write_verify_10
855 {
856         uint8_t opcode;
857         uint8_t byte2;
858 #define SWV_BYTCHK              0x02
859 #define SWV_DPO                 0x10
860 #define SWV_WRPROECT_MASK       0xe0
861         uint8_t addr[4];
862         uint8_t group;
863         uint8_t length[2];
864         uint8_t control;
865 };
866
867 struct scsi_write_verify_12
868 {
869         uint8_t opcode;
870         uint8_t byte2;
871         uint8_t addr[4];
872         uint8_t length[4];
873         uint8_t group;
874         uint8_t control;
875 };
876
877 struct scsi_write_verify_16
878 {
879         uint8_t opcode;
880         uint8_t byte2;
881         uint8_t addr[8];
882         uint8_t length[4];
883         uint8_t group;
884         uint8_t control;
885 };
886
887
888 struct scsi_start_stop_unit
889 {
890         u_int8_t opcode;
891         u_int8_t byte2;
892 #define SSS_IMMED               0x01
893         u_int8_t reserved[2];
894         u_int8_t how;
895 #define SSS_START               0x01
896 #define SSS_LOEJ                0x02
897 #define SSS_PC_MASK             0xf0
898 #define SSS_PC_START_VALID      0x00
899 #define SSS_PC_ACTIVE           0x10
900 #define SSS_PC_IDLE             0x20
901 #define SSS_PC_STANDBY          0x30
902 #define SSS_PC_LU_CONTROL       0x70
903 #define SSS_PC_FORCE_IDLE_0     0xa0
904 #define SSS_PC_FORCE_STANDBY_0  0xb0
905         u_int8_t control;
906 };
907
908 struct ata_pass_12 {
909         u_int8_t opcode;
910         u_int8_t protocol;
911 #define AP_MULTI        0xe0
912         u_int8_t flags;
913 #define AP_T_LEN        0x03
914 #define AP_BB           0x04
915 #define AP_T_DIR        0x08
916 #define AP_CK_COND      0x20
917 #define AP_OFFLINE      0x60
918         u_int8_t features;
919         u_int8_t sector_count;
920         u_int8_t lba_low;
921         u_int8_t lba_mid;
922         u_int8_t lba_high;
923         u_int8_t device;
924         u_int8_t command;
925         u_int8_t reserved;
926         u_int8_t control;
927 };
928
929 struct scsi_maintenance_in
930 {
931         uint8_t  opcode;
932         uint8_t  byte2;
933 #define SERVICE_ACTION_MASK  0x1f
934 #define SA_RPRT_TRGT_GRP     0x0a
935         uint8_t  reserved[4];
936         uint8_t  length[4];
937         uint8_t  reserved1;
938         uint8_t  control;
939 };
940
941 struct ata_pass_16 {
942         u_int8_t opcode;
943         u_int8_t protocol;
944 #define AP_EXTEND       0x01
945         u_int8_t flags;
946         u_int8_t features_ext;
947         u_int8_t features;
948         u_int8_t sector_count_ext;
949         u_int8_t sector_count;
950         u_int8_t lba_low_ext;
951         u_int8_t lba_low;
952         u_int8_t lba_mid_ext;
953         u_int8_t lba_mid;
954         u_int8_t lba_high_ext;
955         u_int8_t lba_high;
956         u_int8_t device;
957         u_int8_t command;
958         u_int8_t control;
959 };
960
961 #define SC_SCSI_1 0x01
962 #define SC_SCSI_2 0x03
963
964 /*
965  * Opcodes
966  */
967
968 #define TEST_UNIT_READY         0x00
969 #define REQUEST_SENSE           0x03
970 #define READ_6                  0x08
971 #define WRITE_6                 0x0A
972 #define INQUIRY                 0x12
973 #define MODE_SELECT_6           0x15
974 #define MODE_SENSE_6            0x1A
975 #define START_STOP_UNIT         0x1B
976 #define START_STOP              0x1B
977 #define RESERVE                 0x16
978 #define RELEASE                 0x17
979 #define RECEIVE_DIAGNOSTIC      0x1C
980 #define SEND_DIAGNOSTIC         0x1D
981 #define PREVENT_ALLOW           0x1E
982 #define READ_CAPACITY           0x25
983 #define READ_10                 0x28
984 #define WRITE_10                0x2A
985 #define POSITION_TO_ELEMENT     0x2B
986 #define WRITE_VERIFY_10         0x2E
987 #define VERIFY_10               0x2F
988 #define SYNCHRONIZE_CACHE       0x35
989 #define READ_DEFECT_DATA_10     0x37
990 #define WRITE_BUFFER            0x3B
991 #define READ_BUFFER             0x3C
992 #define CHANGE_DEFINITION       0x40
993 #define WRITE_SAME_10           0x41
994 #define UNMAP                   0x42
995 #define LOG_SELECT              0x4C
996 #define LOG_SENSE               0x4D
997 #define MODE_SELECT_10          0x55
998 #define RESERVE_10              0x56
999 #define RELEASE_10              0x57
1000 #define MODE_SENSE_10           0x5A
1001 #define PERSISTENT_RES_IN       0x5E
1002 #define PERSISTENT_RES_OUT      0x5F
1003 #define ATA_PASS_16             0x85
1004 #define READ_16                 0x88
1005 #define WRITE_16                0x8A
1006 #define WRITE_VERIFY_16         0x8E
1007 #define SYNCHRONIZE_CACHE_16    0x91
1008 #define WRITE_SAME_16           0x93
1009 #define SERVICE_ACTION_IN       0x9E
1010 #define REPORT_LUNS             0xA0
1011 #define ATA_PASS_12             0xA1
1012 #define MAINTENANCE_IN          0xA3
1013 #define MAINTENANCE_OUT         0xA4
1014 #define MOVE_MEDIUM             0xA5
1015 #define READ_12                 0xA8
1016 #define WRITE_12                0xAA
1017 #define WRITE_VERIFY_12         0xAE
1018 #define READ_ELEMENT_STATUS     0xB8
1019 #define READ_CD                 0xBE
1020
1021 /* Maintenance In Service Action Codes */
1022 #define REPORT_IDENTIFYING_INFRMATION           0x05
1023 #define REPORT_TARGET_PORT_GROUPS               0x0A
1024 #define REPORT_ALIASES                          0x0B
1025 #define REPORT_SUPPORTED_OPERATION_CODES        0x0C
1026 #define REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS      0x0D
1027 #define REPORT_PRIORITY                         0x0E
1028 #define REPORT_TIMESTAMP                        0x0F
1029 #define MANAGEMENT_PROTOCOL_IN                  0x10
1030 /* Maintenance Out Service Action Codes */
1031 #define SET_IDENTIFY_INFORMATION                0x06
1032 #define SET_TARGET_PORT_GROUPS                  0x0A
1033 #define CHANGE_ALIASES                          0x0B
1034 #define SET_PRIORITY                            0x0E
1035 #define SET_TIMESTAMP                           0x0F
1036 #define MANGAEMENT_PROTOCOL_OUT                 0x10
1037
1038 /*
1039  * Device Types
1040  */
1041 #define T_DIRECT        0x00
1042 #define T_SEQUENTIAL    0x01
1043 #define T_PRINTER       0x02
1044 #define T_PROCESSOR     0x03
1045 #define T_WORM          0x04
1046 #define T_CDROM         0x05
1047 #define T_SCANNER       0x06
1048 #define T_OPTICAL       0x07
1049 #define T_CHANGER       0x08
1050 #define T_COMM          0x09
1051 #define T_ASC0          0x0a
1052 #define T_ASC1          0x0b
1053 #define T_STORARRAY     0x0c
1054 #define T_ENCLOSURE     0x0d
1055 #define T_RBC           0x0e
1056 #define T_OCRW          0x0f
1057 #define T_OSD           0x11
1058 #define T_ADC           0x12
1059 #define T_NODEVICE      0x1f
1060 #define T_ANY           0xff    /* Used in Quirk table matches */
1061
1062 #define T_REMOV         1
1063 #define T_FIXED         0
1064
1065 /*
1066  * This length is the initial inquiry length used by the probe code, as    
1067  * well as the legnth necessary for scsi_print_inquiry() to function 
1068  * correctly.  If either use requires a different length in the future, 
1069  * the two values should be de-coupled.
1070  */
1071 #define SHORT_INQUIRY_LENGTH    36
1072
1073 struct scsi_inquiry_data
1074 {
1075         u_int8_t device;
1076 #define SID_TYPE(inq_data) ((inq_data)->device & 0x1f)
1077 #define SID_QUAL(inq_data) (((inq_data)->device & 0xE0) >> 5)
1078 #define SID_QUAL_LU_CONNECTED   0x00    /*
1079                                          * The specified peripheral device
1080                                          * type is currently connected to
1081                                          * logical unit.  If the target cannot
1082                                          * determine whether or not a physical
1083                                          * device is currently connected, it
1084                                          * shall also use this peripheral
1085                                          * qualifier when returning the INQUIRY
1086                                          * data.  This peripheral qualifier
1087                                          * does not mean that the device is
1088                                          * ready for access by the initiator.
1089                                          */
1090 #define SID_QUAL_LU_OFFLINE     0x01    /*
1091                                          * The target is capable of supporting
1092                                          * the specified peripheral device type
1093                                          * on this logical unit; however, the
1094                                          * physical device is not currently
1095                                          * connected to this logical unit.
1096                                          */
1097 #define SID_QUAL_RSVD           0x02
1098 #define SID_QUAL_BAD_LU         0x03    /*
1099                                          * The target is not capable of
1100                                          * supporting a physical device on
1101                                          * this logical unit. For this
1102                                          * peripheral qualifier the peripheral
1103                                          * device type shall be set to 1Fh to
1104                                          * provide compatibility with previous
1105                                          * versions of SCSI. All other
1106                                          * peripheral device type values are
1107                                          * reserved for this peripheral
1108                                          * qualifier.
1109                                          */
1110 #define SID_QUAL_IS_VENDOR_UNIQUE(inq_data) ((SID_QUAL(inq_data) & 0x08) != 0)
1111         u_int8_t dev_qual2;
1112 #define SID_QUAL2       0x7F
1113 #define SID_IS_REMOVABLE(inq_data) (((inq_data)->dev_qual2 & 0x80) != 0)
1114         u_int8_t version;
1115 #define SID_ANSI_REV(inq_data) ((inq_data)->version & 0x07)
1116 #define         SCSI_REV_0              0
1117 #define         SCSI_REV_CCS            1
1118 #define         SCSI_REV_2              2
1119 #define         SCSI_REV_SPC            3
1120 #define         SCSI_REV_SPC2           4
1121 #define         SCSI_REV_SPC3           5
1122 #define         SCSI_REV_SPC4           6
1123
1124 #define SID_ECMA        0x38
1125 #define SID_ISO         0xC0
1126         u_int8_t response_format;
1127 #define SID_AENC        0x80
1128 #define SID_TrmIOP      0x40
1129 #define SID_NormACA     0x20
1130 #define SID_HiSup       0x10
1131         u_int8_t additional_length;
1132 #define SID_ADDITIONAL_LENGTH(iqd)                                      \
1133         ((iqd)->additional_length +                                     \
1134         __offsetof(struct scsi_inquiry_data, additional_length) + 1)
1135         u_int8_t spc3_flags;
1136 #define SPC3_SID_PROTECT        0x01
1137 #define SPC3_SID_3PC            0x08
1138 #define SPC3_SID_TPGS_MASK      0x30
1139 #define SPC3_SID_TPGS_IMPLICIT  0x10
1140 #define SPC3_SID_TPGS_EXPLICIT  0x20
1141 #define SPC3_SID_ACC            0x40
1142 #define SPC3_SID_SCCS           0x80
1143         u_int8_t spc2_flags;
1144 #define SPC2_SID_ADDR16         0x01
1145 #define SPC2_SID_MChngr         0x08
1146 #define SPC2_SID_MultiP         0x10
1147 #define SPC2_SID_EncServ        0x40
1148 #define SPC2_SID_BQueue         0x80
1149
1150 #define INQ_DATA_TQ_ENABLED(iqd)                                \
1151     ((SID_ANSI_REV(iqd) < SCSI_REV_SPC2)? ((iqd)->flags & SID_CmdQue) : \
1152     (((iqd)->flags & SID_CmdQue) && !((iqd)->spc2_flags & SPC2_SID_BQueue)) || \
1153     (!((iqd)->flags & SID_CmdQue) && ((iqd)->spc2_flags & SPC2_SID_BQueue)))
1154
1155         u_int8_t flags;
1156 #define SID_SftRe       0x01
1157 #define SID_CmdQue      0x02
1158 #define SID_Linked      0x08
1159 #define SID_Sync        0x10
1160 #define SID_WBus16      0x20
1161 #define SID_WBus32      0x40
1162 #define SID_RelAdr      0x80
1163 #define SID_VENDOR_SIZE   8
1164         char     vendor[SID_VENDOR_SIZE];
1165 #define SID_PRODUCT_SIZE  16
1166         char     product[SID_PRODUCT_SIZE];
1167 #define SID_REVISION_SIZE 4
1168         char     revision[SID_REVISION_SIZE];
1169         /*
1170          * The following fields were taken from SCSI Primary Commands - 2
1171          * (SPC-2) Revision 14, Dated 11 November 1999
1172          */
1173 #define SID_VENDOR_SPECIFIC_0_SIZE      20
1174         u_int8_t vendor_specific0[SID_VENDOR_SPECIFIC_0_SIZE];
1175         /*
1176          * An extension of SCSI Parallel Specific Values
1177          */
1178 #define SID_SPI_IUS             0x01
1179 #define SID_SPI_QAS             0x02
1180 #define SID_SPI_CLOCK_ST        0x00
1181 #define SID_SPI_CLOCK_DT        0x04
1182 #define SID_SPI_CLOCK_DT_ST     0x0C
1183 #define SID_SPI_MASK            0x0F
1184         u_int8_t spi3data;
1185         u_int8_t reserved2;
1186         /*
1187          * Version Descriptors, stored 2 byte values.
1188          */
1189         u_int8_t version1[2];
1190         u_int8_t version2[2];
1191         u_int8_t version3[2];
1192         u_int8_t version4[2];
1193         u_int8_t version5[2];
1194         u_int8_t version6[2];
1195         u_int8_t version7[2];
1196         u_int8_t version8[2];
1197
1198         u_int8_t reserved3[22];
1199
1200 #define SID_VENDOR_SPECIFIC_1_SIZE      160
1201         u_int8_t vendor_specific1[SID_VENDOR_SPECIFIC_1_SIZE];
1202 };
1203
1204 /*
1205  * This structure is more suited to initiator operation, because the
1206  * maximum number of supported pages is already allocated.
1207  */
1208 struct scsi_vpd_supported_page_list
1209 {
1210         u_int8_t device;
1211         u_int8_t page_code;
1212 #define SVPD_SUPPORTED_PAGE_LIST        0x00
1213 #define SVPD_SUPPORTED_PAGES_HDR_LEN    4
1214         u_int8_t reserved;
1215         u_int8_t length;        /* number of VPD entries */
1216 #define SVPD_SUPPORTED_PAGES_SIZE       251
1217         u_int8_t list[SVPD_SUPPORTED_PAGES_SIZE];
1218 };
1219
1220 /*
1221  * This structure is more suited to target operation, because the
1222  * number of supported pages is left to the user to allocate.
1223  */
1224 struct scsi_vpd_supported_pages
1225 {
1226         u_int8_t device;
1227         u_int8_t page_code;
1228         u_int8_t reserved;
1229 #define SVPD_SUPPORTED_PAGES    0x00
1230         u_int8_t length;
1231         u_int8_t page_list[0];
1232 };
1233
1234
1235 struct scsi_vpd_unit_serial_number
1236 {
1237         u_int8_t device;
1238         u_int8_t page_code;
1239 #define SVPD_UNIT_SERIAL_NUMBER 0x80
1240         u_int8_t reserved;
1241         u_int8_t length; /* serial number length */
1242 #define SVPD_SERIAL_NUM_SIZE 251
1243         u_int8_t serial_num[SVPD_SERIAL_NUM_SIZE];
1244 };
1245
1246 struct scsi_vpd_device_id
1247 {
1248         u_int8_t device;
1249         u_int8_t page_code;
1250 #define SVPD_DEVICE_ID                  0x83
1251 #define SVPD_DEVICE_ID_MAX_SIZE         252
1252 #define SVPD_DEVICE_ID_HDR_LEN \
1253     __offsetof(struct scsi_vpd_device_id, desc_list)
1254         u_int8_t length[2];
1255         u_int8_t desc_list[];
1256 };
1257
1258 struct scsi_vpd_id_descriptor
1259 {
1260         u_int8_t        proto_codeset;
1261 #define SCSI_PROTO_FC           0x00
1262 #define SCSI_PROTO_SPI          0x01
1263 #define SCSI_PROTO_SSA          0x02
1264 #define SCSI_PROTO_1394         0x03
1265 #define SCSI_PROTO_RDMA         0x04
1266 #define SCSI_PROTO_iSCSI        0x05
1267 #define SCSI_PROTO_SAS          0x06
1268 #define SCSI_PROTO_ADT          0x07
1269 #define SCSI_PROTO_ATA          0x08
1270 #define SVPD_ID_PROTO_SHIFT     4
1271 #define SVPD_ID_CODESET_BINARY  0x01
1272 #define SVPD_ID_CODESET_ASCII   0x02
1273 #define SVPD_ID_CODESET_MASK    0x0f
1274         u_int8_t        id_type;
1275 #define SVPD_ID_PIV             0x80
1276 #define SVPD_ID_ASSOC_LUN       0x00
1277 #define SVPD_ID_ASSOC_PORT      0x10
1278 #define SVPD_ID_ASSOC_TARGET    0x20
1279 #define SVPD_ID_ASSOC_MASK      0x30
1280 #define SVPD_ID_TYPE_VENDOR     0x00
1281 #define SVPD_ID_TYPE_T10        0x01
1282 #define SVPD_ID_TYPE_EUI64      0x02
1283 #define SVPD_ID_TYPE_NAA        0x03
1284 #define SVPD_ID_TYPE_RELTARG    0x04
1285 #define SVPD_ID_TYPE_TPORTGRP   0x05
1286 #define SVPD_ID_TYPE_LUNGRP     0x06
1287 #define SVPD_ID_TYPE_MD5_LUN_ID 0x07
1288 #define SVPD_ID_TYPE_SCSI_NAME  0x08
1289 #define SVPD_ID_TYPE_MASK       0x0f
1290         u_int8_t        reserved;
1291         u_int8_t        length;
1292 #define SVPD_DEVICE_ID_DESC_HDR_LEN \
1293     __offsetof(struct scsi_vpd_id_descriptor, identifier) 
1294         u_int8_t        identifier[];
1295 };
1296
1297 struct scsi_vpd_id_t10
1298 {
1299         u_int8_t        vendor[8];
1300         u_int8_t        vendor_spec_id[0];
1301 };
1302
1303 struct scsi_vpd_id_eui64
1304 {
1305         u_int8_t        ieee_company_id[3];
1306         u_int8_t        extension_id[5];
1307 };
1308
1309 struct scsi_vpd_id_naa_basic
1310 {
1311         uint8_t naa;
1312         /* big endian, packed:
1313         uint8_t naa : 4;
1314         uint8_t naa_desig : 4;
1315         */
1316 #define SVPD_ID_NAA_NAA_SHIFT           4
1317 #define SVPD_ID_NAA_IEEE_EXT            0x02
1318 #define SVPD_ID_NAA_LOCAL_REG           0x03
1319 #define SVPD_ID_NAA_IEEE_REG            0x05
1320 #define SVPD_ID_NAA_IEEE_REG_EXT        0x06
1321         uint8_t naa_data[];
1322 };
1323
1324 struct scsi_vpd_id_naa_ieee_extended_id
1325 {
1326         uint8_t naa;
1327         uint8_t vendor_specific_id_a;
1328         uint8_t ieee_company_id[3];
1329         uint8_t vendor_specific_id_b[4];
1330 };
1331
1332 struct scsi_vpd_id_naa_local_reg
1333 {
1334         uint8_t naa;
1335         uint8_t local_value[7];
1336 };
1337
1338 struct scsi_vpd_id_naa_ieee_reg
1339 {
1340         uint8_t naa;
1341         uint8_t reg_value[7];
1342         /* big endian, packed:
1343         uint8_t naa_basic : 4;
1344         uint8_t ieee_company_id_0 : 4;
1345         uint8_t ieee_company_id_1[2];
1346         uint8_t ieee_company_id_2 : 4;
1347         uint8_t vendor_specific_id_0 : 4;
1348         uint8_t vendor_specific_id_1[4];
1349         */
1350 };
1351
1352 struct scsi_vpd_id_naa_ieee_reg_extended
1353 {
1354         uint8_t naa;
1355         uint8_t reg_value[15];
1356         /* big endian, packed:
1357         uint8_t naa_basic : 4;
1358         uint8_t ieee_company_id_0 : 4;
1359         uint8_t ieee_company_id_1[2];
1360         uint8_t ieee_company_id_2 : 4;
1361         uint8_t vendor_specific_id_0 : 4;
1362         uint8_t vendor_specific_id_1[4];
1363         uint8_t vendor_specific_id_ext[8];
1364         */
1365 };
1366
1367 struct scsi_vpd_id_rel_trgt_port_id
1368 {
1369         uint8_t obsolete[2];
1370         uint8_t rel_trgt_port_id[2];
1371 };
1372
1373 struct scsi_vpd_id_trgt_port_grp_id
1374 {
1375         uint8_t reserved[2];
1376         uint8_t trgt_port_grp[2];
1377 };
1378
1379 struct scsi_vpd_id_lun_grp_id
1380 {
1381         uint8_t reserved[2];
1382         uint8_t log_unit_grp[2];
1383 };
1384
1385 struct scsi_vpd_id_md5_lun_id
1386 {
1387         uint8_t lun_id[16];
1388 };
1389
1390 struct scsi_vpd_id_scsi_name
1391 {
1392         uint8_t name_string[256];
1393 };
1394
1395 struct scsi_service_action_in
1396 {
1397         uint8_t opcode;
1398         uint8_t service_action;
1399         uint8_t action_dependent[13];
1400         uint8_t control;
1401 };
1402
1403 struct scsi_diag_page {
1404         uint8_t page_code;
1405         uint8_t page_specific_flags;
1406         uint8_t length[2];
1407         uint8_t params[0];
1408 };
1409
1410 struct scsi_read_capacity
1411 {
1412         u_int8_t opcode;
1413         u_int8_t byte2;
1414 #define SRC_RELADR      0x01
1415         u_int8_t addr[4];
1416         u_int8_t unused[2];
1417         u_int8_t pmi;
1418 #define SRC_PMI         0x01
1419         u_int8_t control;
1420 };
1421
1422 struct scsi_read_capacity_16
1423 {
1424         uint8_t opcode;
1425 #define SRC16_SERVICE_ACTION    0x10
1426         uint8_t service_action;
1427         uint8_t addr[8];
1428         uint8_t alloc_len[4];
1429 #define SRC16_PMI               0x01
1430 #define SRC16_RELADR            0x02
1431         uint8_t reladr;
1432         uint8_t control;
1433 };
1434
1435 struct scsi_read_capacity_data
1436 {
1437         u_int8_t addr[4];
1438         u_int8_t length[4];
1439 };
1440
1441 struct scsi_read_capacity_data_long
1442 {
1443         uint8_t addr[8];
1444         uint8_t length[4];
1445 #define SRC16_PROT_EN           0x01
1446 #define SRC16_P_TYPE            0x0e
1447 #define SRC16_PTYPE_1           0x00
1448 #define SRC16_PTYPE_2           0x02
1449 #define SRC16_PTYPE_3           0x04
1450         uint8_t prot;
1451 #define SRC16_LBPPBE            0x0f
1452 #define SRC16_PI_EXPONENT       0xf0
1453 #define SRC16_PI_EXPONENT_SHIFT 4
1454         uint8_t prot_lbppbe;
1455 #define SRC16_LALBA             0x3f
1456 #define SRC16_LBPRZ             0x40
1457 #define SRC16_LBPME             0x80
1458 /*
1459  * Alternate versions of these macros that are intended for use on a 16-bit
1460  * version of the lalba_lbp field instead of the array of 2 8 bit numbers.
1461  */
1462 #define SRC16_LALBA_A           0x3fff
1463 #define SRC16_LBPRZ_A           0x4000
1464 #define SRC16_LBPME_A           0x8000
1465         uint8_t lalba_lbp[2];
1466 };
1467
1468 struct scsi_report_luns
1469 {
1470         uint8_t opcode;
1471         uint8_t reserved1;
1472 #define RPL_REPORT_DEFAULT      0x00
1473 #define RPL_REPORT_WELLKNOWN    0x01
1474 #define RPL_REPORT_ALL          0x02
1475         uint8_t select_report;
1476         uint8_t reserved2[3];
1477         uint8_t length[4];
1478         uint8_t reserved3;
1479         uint8_t control;
1480 };
1481
1482 struct scsi_report_luns_lundata {
1483         uint8_t lundata[8];
1484 #define RPL_LUNDATA_PERIPH_BUS_MASK     0x3f
1485 #define RPL_LUNDATA_FLAT_LUN_MASK       0x3f
1486 #define RPL_LUNDATA_FLAT_LUN_BITS       0x06
1487 #define RPL_LUNDATA_LUN_TARG_MASK       0x3f
1488 #define RPL_LUNDATA_LUN_BUS_MASK        0xe0
1489 #define RPL_LUNDATA_LUN_LUN_MASK        0x1f
1490 #define RPL_LUNDATA_EXT_LEN_MASK        0x30
1491 #define RPL_LUNDATA_EXT_EAM_MASK        0x0f
1492 #define RPL_LUNDATA_EXT_EAM_WK          0x01
1493 #define RPL_LUNDATA_EXT_EAM_NOT_SPEC    0x0f
1494 #define RPL_LUNDATA_ATYP_MASK   0xc0    /* MBZ for type 0 lun */
1495 #define RPL_LUNDATA_ATYP_PERIPH 0x00
1496 #define RPL_LUNDATA_ATYP_FLAT   0x40
1497 #define RPL_LUNDATA_ATYP_LUN    0x80
1498 #define RPL_LUNDATA_ATYP_EXTLUN 0xc0
1499 };
1500
1501 struct scsi_report_luns_data {
1502         u_int8_t length[4];     /* length of LUN inventory, in bytes */
1503         u_int8_t reserved[4];   /* unused */
1504         /*
1505          * LUN inventory- we only support the type zero form for now.
1506          */
1507         struct scsi_report_luns_lundata luns[0];
1508 };
1509
1510 struct scsi_target_group
1511 {
1512         uint8_t opcode;
1513         uint8_t service_action;
1514 #define STG_PDF_LENGTH          0x00
1515 #define RPL_PDF_EXTENDED        0x20
1516         uint8_t reserved1[4];
1517         uint8_t length[4];
1518         uint8_t reserved2;
1519         uint8_t control;
1520 };
1521
1522 struct scsi_target_port_descriptor {
1523         uint8_t reserved[2];
1524         uint8_t relative_target_port_identifier[2];
1525         uint8_t desc_list[];
1526 };
1527
1528 struct scsi_target_port_group_descriptor {
1529         uint8_t pref_state;
1530 #define TPG_PRIMARY                             0x80
1531 #define TPG_ASYMMETRIC_ACCESS_STATE_MASK        0xf
1532 #define TPG_ASYMMETRIC_ACCESS_OPTIMIZED         0x0
1533 #define TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED      0x1
1534 #define TPG_ASYMMETRIC_ACCESS_STANDBY           0x2
1535 #define TPG_ASYMMETRIC_ACCESS_UNAVAILABLE       0x3
1536 #define TPG_ASYMMETRIC_ACCESS_LBA_DEPENDENT     0x4
1537 #define TPG_ASYMMETRIC_ACCESS_OFFLINE           0xE
1538 #define TPG_ASYMMETRIC_ACCESS_TRANSITIONING     0xF
1539         uint8_t support;
1540 #define TPG_AO_SUP      0x01
1541 #define TPG_AN_SUP      0x02
1542 #define TPG_S_SUP       0x04
1543 #define TPG_U_SUP       0x08
1544 #define TPG_LBD_SUP     0x10
1545 #define TPG_O_SUP       0x40
1546 #define TPG_T_SUP       0x80
1547         uint8_t target_port_group[2];
1548         uint8_t reserved;
1549         uint8_t status;
1550 #define TPG_UNAVLBL      0
1551 #define TPG_SET_BY_STPG  0x01
1552 #define TPG_IMPLICIT     0x02
1553         uint8_t vendor_specific;
1554         uint8_t target_port_count;
1555         struct scsi_target_port_descriptor descriptors[];
1556 };
1557
1558 struct scsi_target_group_data {
1559         uint8_t length[4];      /* length of returned data, in bytes */
1560         struct scsi_target_port_group_descriptor groups[];
1561 };
1562
1563 struct scsi_target_group_data_extended {
1564         uint8_t length[4];      /* length of returned data, in bytes */
1565         uint8_t format_type;    /* STG_PDF_LENGTH or RPL_PDF_EXTENDED */
1566         uint8_t implicit_transition_time;
1567         uint8_t reserved[2];
1568         struct scsi_target_port_group_descriptor groups[];
1569 };
1570
1571
1572 typedef enum {
1573         SSD_TYPE_NONE,
1574         SSD_TYPE_FIXED,
1575         SSD_TYPE_DESC
1576 } scsi_sense_data_type;
1577
1578 typedef enum {
1579         SSD_ELEM_NONE,
1580         SSD_ELEM_SKIP,
1581         SSD_ELEM_DESC,
1582         SSD_ELEM_SKS,
1583         SSD_ELEM_COMMAND,
1584         SSD_ELEM_INFO,
1585         SSD_ELEM_FRU,
1586         SSD_ELEM_STREAM,
1587         SSD_ELEM_MAX
1588 } scsi_sense_elem_type;
1589
1590
1591 struct scsi_sense_data
1592 {
1593         uint8_t error_code;
1594         /*
1595          * SPC-4 says that the maximum length of sense data is 252 bytes.
1596          * So this structure is exactly 252 bytes log.
1597          */
1598 #define SSD_FULL_SIZE 252
1599         uint8_t sense_buf[SSD_FULL_SIZE - 1];
1600         /*
1601          * XXX KDM is this still a reasonable minimum size?
1602          */
1603 #define SSD_MIN_SIZE 18
1604         /*
1605          * Maximum value for the extra_len field in the sense data.
1606          */
1607 #define SSD_EXTRA_MAX 244
1608 };
1609
1610 /*
1611  * Fixed format sense data.
1612  */
1613 struct scsi_sense_data_fixed
1614 {
1615         u_int8_t error_code;
1616 #define SSD_ERRCODE                     0x7F
1617 #define         SSD_CURRENT_ERROR       0x70
1618 #define         SSD_DEFERRED_ERROR      0x71
1619 #define SSD_ERRCODE_VALID       0x80    
1620         u_int8_t segment;
1621         u_int8_t flags;
1622 #define SSD_KEY                         0x0F
1623 #define         SSD_KEY_NO_SENSE        0x00
1624 #define         SSD_KEY_RECOVERED_ERROR 0x01
1625 #define         SSD_KEY_NOT_READY       0x02
1626 #define         SSD_KEY_MEDIUM_ERROR    0x03
1627 #define         SSD_KEY_HARDWARE_ERROR  0x04
1628 #define         SSD_KEY_ILLEGAL_REQUEST 0x05
1629 #define         SSD_KEY_UNIT_ATTENTION  0x06
1630 #define         SSD_KEY_DATA_PROTECT    0x07
1631 #define         SSD_KEY_BLANK_CHECK     0x08
1632 #define         SSD_KEY_Vendor_Specific 0x09
1633 #define         SSD_KEY_COPY_ABORTED    0x0a
1634 #define         SSD_KEY_ABORTED_COMMAND 0x0b            
1635 #define         SSD_KEY_EQUAL           0x0c
1636 #define         SSD_KEY_VOLUME_OVERFLOW 0x0d
1637 #define         SSD_KEY_MISCOMPARE      0x0e
1638 #define         SSD_KEY_COMPLETED       0x0f                    
1639 #define SSD_ILI         0x20
1640 #define SSD_EOM         0x40
1641 #define SSD_FILEMARK    0x80
1642         u_int8_t info[4];
1643         u_int8_t extra_len;
1644         u_int8_t cmd_spec_info[4];
1645         u_int8_t add_sense_code;
1646         u_int8_t add_sense_code_qual;
1647         u_int8_t fru;
1648         u_int8_t sense_key_spec[3];
1649 #define SSD_SCS_VALID           0x80
1650 #define SSD_FIELDPTR_CMD        0x40
1651 #define SSD_BITPTR_VALID        0x08
1652 #define SSD_BITPTR_VALUE        0x07
1653         u_int8_t extra_bytes[14];
1654 #define SSD_FIXED_IS_PRESENT(sense, length, field)                      \
1655         ((length >= (offsetof(struct scsi_sense_data_fixed, field) +    \
1656         sizeof(sense->field))) ? 1 :0)
1657 #define SSD_FIXED_IS_FILLED(sense, field)                               \
1658         ((((offsetof(struct scsi_sense_data_fixed, field) +             \
1659         sizeof(sense->field)) -                                         \
1660         (offsetof(struct scsi_sense_data_fixed, extra_len) +            \
1661         sizeof(sense->extra_len))) <= sense->extra_len) ? 1 : 0)
1662 };
1663
1664 /*
1665  * Descriptor format sense data definitions.
1666  * Introduced in SPC-3.
1667  */
1668 struct scsi_sense_data_desc 
1669 {
1670         uint8_t error_code;
1671 #define SSD_DESC_CURRENT_ERROR  0x72
1672 #define SSD_DESC_DEFERRED_ERROR 0x73
1673         uint8_t sense_key;
1674         uint8_t add_sense_code;
1675         uint8_t add_sense_code_qual;
1676         uint8_t reserved[3];
1677         /*
1678          * Note that SPC-4, section 4.5.2.1 says that the extra_len field
1679          * must be less than or equal to 244.
1680          */
1681         uint8_t extra_len;
1682         uint8_t sense_desc[0];
1683 #define SSD_DESC_IS_PRESENT(sense, length, field)                       \
1684         ((length >= (offsetof(struct scsi_sense_data_desc, field) +     \
1685         sizeof(sense->field))) ? 1 :0)
1686 };
1687
1688 struct scsi_sense_desc_header
1689 {
1690         uint8_t desc_type;
1691         uint8_t length;
1692 };
1693 /*
1694  * The information provide in the Information descriptor is device type or
1695  * command specific information, and defined in a command standard.
1696  *
1697  * Note that any changes to the field names or positions in this structure,
1698  * even reserved fields, should be accompanied by an examination of the
1699  * code in ctl_set_sense() that uses them.
1700  *
1701  * Maximum descriptors allowed: 1 (as of SPC-4)
1702  */
1703 struct scsi_sense_info
1704 {
1705         uint8_t desc_type;
1706 #define SSD_DESC_INFO   0x00
1707         uint8_t length;
1708         uint8_t byte2;
1709 #define SSD_INFO_VALID  0x80
1710         uint8_t reserved;
1711         uint8_t info[8];
1712 };
1713
1714 /*
1715  * Command-specific information depends on the command for which the
1716  * reported condition occured.
1717  *
1718  * Note that any changes to the field names or positions in this structure,
1719  * even reserved fields, should be accompanied by an examination of the
1720  * code in ctl_set_sense() that uses them.
1721  *
1722  * Maximum descriptors allowed: 1 (as of SPC-4)
1723  */
1724 struct scsi_sense_command
1725 {
1726         uint8_t desc_type;
1727 #define SSD_DESC_COMMAND        0x01
1728         uint8_t length;
1729         uint8_t reserved[2];
1730         uint8_t command_info[8];
1731 };
1732
1733 /*
1734  * Sense key specific descriptor.  The sense key specific data format
1735  * depends on the sense key in question.
1736  *
1737  * Maximum descriptors allowed: 1 (as of SPC-4)
1738  */
1739 struct scsi_sense_sks
1740 {
1741         uint8_t desc_type;
1742 #define SSD_DESC_SKS            0x02
1743         uint8_t length;
1744         uint8_t reserved1[2];
1745         uint8_t sense_key_spec[3];
1746 #define SSD_SKS_VALID           0x80
1747         uint8_t reserved2;
1748 };
1749
1750 /*
1751  * This is used for the Illegal Request sense key (0x05) only.
1752  */
1753 struct scsi_sense_sks_field
1754 {
1755         uint8_t byte0;
1756 #define SSD_SKS_FIELD_VALID     0x80
1757 #define SSD_SKS_FIELD_CMD       0x40
1758 #define SSD_SKS_BPV             0x08
1759 #define SSD_SKS_BIT_VALUE       0x07
1760         uint8_t field[2];
1761 };
1762
1763
1764 /* 
1765  * This is used for the Hardware Error (0x04), Medium Error (0x03) and
1766  * Recovered Error (0x01) sense keys.
1767  */
1768 struct scsi_sense_sks_retry
1769 {
1770         uint8_t byte0;
1771 #define SSD_SKS_RETRY_VALID     0x80
1772         uint8_t actual_retry_count[2];
1773 };
1774
1775 /*
1776  * Used with the NO Sense (0x00) or Not Ready (0x02) sense keys.
1777  */
1778 struct scsi_sense_sks_progress
1779 {
1780         uint8_t byte0;
1781 #define SSD_SKS_PROGRESS_VALID  0x80
1782         uint8_t progress[2];
1783 #define SSD_SKS_PROGRESS_DENOM  0x10000
1784 };
1785
1786 /*
1787  * Used with the Copy Aborted (0x0a) sense key.
1788  */
1789 struct scsi_sense_sks_segment
1790 {
1791         uint8_t byte0;
1792 #define SSD_SKS_SEGMENT_VALID   0x80
1793 #define SSD_SKS_SEGMENT_SD      0x20
1794 #define SSD_SKS_SEGMENT_BPV     0x08
1795 #define SSD_SKS_SEGMENT_BITPTR  0x07
1796         uint8_t field[2];
1797 };
1798
1799 /*
1800  * Used with the Unit Attention (0x06) sense key.
1801  *
1802  * This is currently used to indicate that the unit attention condition
1803  * queue has overflowed (when the overflow bit is set).
1804  */
1805 struct scsi_sense_sks_overflow
1806 {
1807         uint8_t byte0;
1808 #define SSD_SKS_OVERFLOW_VALID  0x80
1809 #define SSD_SKS_OVERFLOW_SET    0x01
1810         uint8_t reserved[2];
1811 };
1812
1813 /*
1814  * This specifies which component is associated with the sense data.  There
1815  * is no standard meaning for the fru value.
1816  *
1817  * Maximum descriptors allowed: 1 (as of SPC-4)
1818  */
1819 struct scsi_sense_fru
1820 {
1821         uint8_t desc_type;
1822 #define SSD_DESC_FRU            0x03
1823         uint8_t length;
1824         uint8_t reserved;
1825         uint8_t fru;
1826 };
1827
1828 /*
1829  * Used for Stream commands, defined in SSC-4.
1830  *
1831  * Maximum descriptors allowed: 1 (as of SPC-4)
1832  */
1833  
1834 struct scsi_sense_stream
1835 {
1836         uint8_t desc_type;
1837 #define SSD_DESC_STREAM         0x04
1838         uint8_t length;
1839         uint8_t reserved;
1840         uint8_t byte3;
1841 #define SSD_DESC_STREAM_FM      0x80
1842 #define SSD_DESC_STREAM_EOM     0x40
1843 #define SSD_DESC_STREAM_ILI     0x20
1844 };
1845
1846 /*
1847  * Used for Block commands, defined in SBC-3.
1848  *
1849  * This is currently (as of SBC-3) only used for the Incorrect Length
1850  * Indication (ILI) bit, which says that the data length requested in the
1851  * READ LONG or WRITE LONG command did not match the length of the logical
1852  * block.
1853  *
1854  * Maximum descriptors allowed: 1 (as of SPC-4)
1855  */
1856 struct scsi_sense_block
1857 {
1858         uint8_t desc_type;
1859 #define SSD_DESC_BLOCK          0x05
1860         uint8_t length;
1861         uint8_t reserved;
1862         uint8_t byte3;
1863 #define SSD_DESC_BLOCK_ILI      0x20
1864 };
1865
1866 /*
1867  * Used for Object-Based Storage Devices (OSD-3).
1868  *
1869  * Maximum descriptors allowed: 1 (as of SPC-4)
1870  */
1871 struct scsi_sense_osd_objid
1872 {
1873         uint8_t desc_type;
1874 #define SSD_DESC_OSD_OBJID      0x06
1875         uint8_t length;
1876         uint8_t reserved[6];
1877         /*
1878          * XXX KDM provide the bit definitions here?  There are a lot of
1879          * them, and we don't have an OSD driver yet.
1880          */
1881         uint8_t not_init_cmds[4];
1882         uint8_t completed_cmds[4];
1883         uint8_t partition_id[8];
1884         uint8_t object_id[8];
1885 };
1886
1887 /*
1888  * Used for Object-Based Storage Devices (OSD-3).
1889  *
1890  * Maximum descriptors allowed: 1 (as of SPC-4)
1891  */
1892 struct scsi_sense_osd_integrity
1893 {
1894         uint8_t desc_type;
1895 #define SSD_DESC_OSD_INTEGRITY  0x07
1896         uint8_t length;
1897         uint8_t integ_check_val[32];
1898 };
1899
1900 /*
1901  * Used for Object-Based Storage Devices (OSD-3).
1902  *
1903  * Maximum descriptors allowed: 1 (as of SPC-4)
1904  */
1905 struct scsi_sense_osd_attr_id
1906 {
1907         uint8_t desc_type;
1908 #define SSD_DESC_OSD_ATTR_ID    0x08
1909         uint8_t length;
1910         uint8_t reserved[2];
1911         uint8_t attr_desc[0];
1912 };
1913
1914 /*
1915  * Used with Sense keys No Sense (0x00) and Not Ready (0x02).
1916  *
1917  * Maximum descriptors allowed: 32 (as of SPC-4)
1918  */
1919 struct scsi_sense_progress
1920 {
1921         uint8_t desc_type;
1922 #define SSD_DESC_PROGRESS       0x0a
1923         uint8_t length;
1924         uint8_t sense_key;
1925         uint8_t add_sense_code;
1926         uint8_t add_sense_code_qual;
1927         uint8_t reserved;
1928         uint8_t progress[2];
1929 };
1930
1931 /*
1932  * This is typically forwarded as the result of an EXTENDED COPY command.
1933  *
1934  * Maximum descriptors allowed: 2 (as of SPC-4)
1935  */
1936 struct scsi_sense_forwarded
1937 {
1938         uint8_t desc_type;
1939 #define SSD_DESC_FORWARDED      0x0c
1940         uint8_t length;
1941         uint8_t byte2;
1942 #define SSD_FORWARDED_FSDT      0x80
1943 #define SSD_FORWARDED_SDS_MASK  0x0f
1944 #define SSD_FORWARDED_SDS_UNK   0x00
1945 #define SSD_FORWARDED_SDS_EXSRC 0x01
1946 #define SSD_FORWARDED_SDS_EXDST 0x02
1947 };
1948
1949 /*
1950  * Vendor-specific sense descriptor.  The desc_type field will be in the
1951  * range bewteen MIN and MAX inclusive.
1952  */
1953 struct scsi_sense_vendor
1954 {
1955         uint8_t desc_type;
1956 #define SSD_DESC_VENDOR_MIN     0x80
1957 #define SSD_DESC_VENDOR_MAX     0xff
1958         uint8_t length;
1959         uint8_t data[0];
1960 };
1961
1962 struct scsi_mode_header_6
1963 {
1964         u_int8_t data_length;   /* Sense data length */
1965         u_int8_t medium_type;
1966         u_int8_t dev_spec;
1967         u_int8_t blk_desc_len;
1968 };
1969
1970 struct scsi_mode_header_10
1971 {
1972         u_int8_t data_length[2];/* Sense data length */
1973         u_int8_t medium_type;
1974         u_int8_t dev_spec;
1975         u_int8_t unused[2];
1976         u_int8_t blk_desc_len[2];
1977 };
1978
1979 struct scsi_mode_page_header
1980 {
1981         u_int8_t page_code;
1982 #define SMPH_PS         0x80
1983 #define SMPH_SPF        0x40
1984 #define SMPH_PC_MASK    0x3f
1985         u_int8_t page_length;
1986 };
1987
1988 struct scsi_mode_page_header_sp
1989 {
1990         uint8_t page_code;
1991         uint8_t subpage;
1992         uint8_t page_length[2];
1993 };
1994
1995
1996 struct scsi_mode_blk_desc
1997 {
1998         u_int8_t density;
1999         u_int8_t nblocks[3];
2000         u_int8_t reserved;
2001         u_int8_t blklen[3];
2002 };
2003
2004 #define SCSI_DEFAULT_DENSITY    0x00    /* use 'default' density */
2005 #define SCSI_SAME_DENSITY       0x7f    /* use 'same' density- >= SCSI-2 only */
2006
2007
2008 /*
2009  * Status Byte
2010  */
2011 #define SCSI_STATUS_OK                  0x00
2012 #define SCSI_STATUS_CHECK_COND          0x02
2013 #define SCSI_STATUS_COND_MET            0x04
2014 #define SCSI_STATUS_BUSY                0x08
2015 #define SCSI_STATUS_INTERMED            0x10
2016 #define SCSI_STATUS_INTERMED_COND_MET   0x14
2017 #define SCSI_STATUS_RESERV_CONFLICT     0x18
2018 #define SCSI_STATUS_CMD_TERMINATED      0x22    /* Obsolete in SAM-2 */
2019 #define SCSI_STATUS_QUEUE_FULL          0x28
2020 #define SCSI_STATUS_ACA_ACTIVE          0x30
2021 #define SCSI_STATUS_TASK_ABORTED        0x40
2022
2023 struct scsi_inquiry_pattern {
2024         u_int8_t   type;
2025         u_int8_t   media_type;
2026 #define SIP_MEDIA_REMOVABLE     0x01
2027 #define SIP_MEDIA_FIXED         0x02
2028         const char *vendor;
2029         const char *product;
2030         const char *revision;
2031 }; 
2032
2033 struct scsi_static_inquiry_pattern {
2034         u_int8_t   type;
2035         u_int8_t   media_type;
2036         char       vendor[SID_VENDOR_SIZE+1];
2037         char       product[SID_PRODUCT_SIZE+1];
2038         char       revision[SID_REVISION_SIZE+1];
2039 };
2040
2041 struct scsi_sense_quirk_entry {
2042         struct scsi_inquiry_pattern     inq_pat;
2043         int                             num_sense_keys;
2044         int                             num_ascs;
2045         struct sense_key_table_entry    *sense_key_info;
2046         struct asc_table_entry          *asc_info;
2047 };
2048
2049 struct sense_key_table_entry {
2050         u_int8_t    sense_key;
2051         u_int32_t   action;
2052         const char *desc;
2053 };
2054
2055 struct asc_table_entry {
2056         u_int8_t    asc;
2057         u_int8_t    ascq;
2058         u_int32_t   action;
2059         const char *desc;
2060 };
2061
2062 struct op_table_entry {
2063         u_int8_t    opcode;
2064         u_int32_t   opmask;
2065         const char  *desc;
2066 };
2067
2068 struct scsi_op_quirk_entry {
2069         struct scsi_inquiry_pattern     inq_pat;
2070         int                             num_ops;
2071         struct op_table_entry           *op_table;
2072 };
2073
2074 typedef enum {
2075         SSS_FLAG_NONE           = 0x00,
2076         SSS_FLAG_PRINT_COMMAND  = 0x01
2077 } scsi_sense_string_flags;
2078
2079 struct ccb_scsiio;
2080 struct cam_periph;
2081 union  ccb;
2082 #ifndef _KERNEL
2083 struct cam_device;
2084 #endif
2085
2086 extern const char *scsi_sense_key_text[];
2087
2088 struct sbuf;
2089
2090 __BEGIN_DECLS
2091 void scsi_sense_desc(int sense_key, int asc, int ascq,
2092                      struct scsi_inquiry_data *inq_data,
2093                      const char **sense_key_desc, const char **asc_desc);
2094 scsi_sense_action scsi_error_action(struct ccb_scsiio* csio,
2095                                     struct scsi_inquiry_data *inq_data,
2096                                     u_int32_t sense_flags);
2097 const char *    scsi_status_string(struct ccb_scsiio *csio);
2098
2099 void scsi_desc_iterate(struct scsi_sense_data_desc *sense, u_int sense_len,
2100                        int (*iter_func)(struct scsi_sense_data_desc *sense,
2101                                         u_int, struct scsi_sense_desc_header *,
2102                                         void *), void *arg);
2103 uint8_t *scsi_find_desc(struct scsi_sense_data_desc *sense, u_int sense_len,
2104                         uint8_t desc_type);
2105 void scsi_set_sense_data(struct scsi_sense_data *sense_data, 
2106                          scsi_sense_data_type sense_format, int current_error,
2107                          int sense_key, int asc, int ascq, ...) ;
2108 void scsi_set_sense_data_va(struct scsi_sense_data *sense_data,
2109                             scsi_sense_data_type sense_format,
2110                             int current_error, int sense_key, int asc,
2111                             int ascq, va_list ap);
2112 int scsi_get_sense_info(struct scsi_sense_data *sense_data, u_int sense_len,
2113                         uint8_t info_type, uint64_t *info,
2114                         int64_t *signed_info);
2115 int scsi_get_sks(struct scsi_sense_data *sense_data, u_int sense_len,
2116                  uint8_t *sks);
2117 int scsi_get_block_info(struct scsi_sense_data *sense_data, u_int sense_len,
2118                         struct scsi_inquiry_data *inq_data,
2119                         uint8_t *block_bits);
2120 int scsi_get_stream_info(struct scsi_sense_data *sense_data, u_int sense_len,
2121                          struct scsi_inquiry_data *inq_data,
2122                          uint8_t *stream_bits);
2123 void scsi_info_sbuf(struct sbuf *sb, uint8_t *cdb, int cdb_len,
2124                     struct scsi_inquiry_data *inq_data, uint64_t info);
2125 void scsi_command_sbuf(struct sbuf *sb, uint8_t *cdb, int cdb_len,
2126                        struct scsi_inquiry_data *inq_data, uint64_t csi);
2127 void scsi_progress_sbuf(struct sbuf *sb, uint16_t progress);
2128 int scsi_sks_sbuf(struct sbuf *sb, int sense_key, uint8_t *sks);
2129 void scsi_fru_sbuf(struct sbuf *sb, uint64_t fru);
2130 void scsi_stream_sbuf(struct sbuf *sb, uint8_t stream_bits, uint64_t info);
2131 void scsi_block_sbuf(struct sbuf *sb, uint8_t block_bits, uint64_t info);
2132 void scsi_sense_info_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
2133                           u_int sense_len, uint8_t *cdb, int cdb_len,
2134                           struct scsi_inquiry_data *inq_data,
2135                           struct scsi_sense_desc_header *header);
2136
2137 void scsi_sense_command_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
2138                              u_int sense_len, uint8_t *cdb, int cdb_len,
2139                              struct scsi_inquiry_data *inq_data,
2140                              struct scsi_sense_desc_header *header);
2141 void scsi_sense_sks_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
2142                          u_int sense_len, uint8_t *cdb, int cdb_len,
2143                          struct scsi_inquiry_data *inq_data,
2144                          struct scsi_sense_desc_header *header);
2145 void scsi_sense_fru_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
2146                          u_int sense_len, uint8_t *cdb, int cdb_len,
2147                          struct scsi_inquiry_data *inq_data,
2148                          struct scsi_sense_desc_header *header);
2149 void scsi_sense_stream_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
2150                             u_int sense_len, uint8_t *cdb, int cdb_len,
2151                             struct scsi_inquiry_data *inq_data,
2152                             struct scsi_sense_desc_header *header);
2153 void scsi_sense_block_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
2154                            u_int sense_len, uint8_t *cdb, int cdb_len,
2155                            struct scsi_inquiry_data *inq_data,
2156                            struct scsi_sense_desc_header *header);
2157 void scsi_sense_progress_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
2158                               u_int sense_len, uint8_t *cdb, int cdb_len,
2159                               struct scsi_inquiry_data *inq_data,
2160                               struct scsi_sense_desc_header *header);
2161 void scsi_sense_generic_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
2162                              u_int sense_len, uint8_t *cdb, int cdb_len,
2163                              struct scsi_inquiry_data *inq_data,
2164                              struct scsi_sense_desc_header *header);
2165 void scsi_sense_desc_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
2166                           u_int sense_len, uint8_t *cdb, int cdb_len,
2167                           struct scsi_inquiry_data *inq_data,
2168                           struct scsi_sense_desc_header *header);
2169 scsi_sense_data_type scsi_sense_type(struct scsi_sense_data *sense_data);
2170
2171 void scsi_sense_only_sbuf(struct scsi_sense_data *sense, u_int sense_len,
2172                           struct sbuf *sb, char *path_str,
2173                           struct scsi_inquiry_data *inq_data, uint8_t *cdb,
2174                           int cdb_len);
2175
2176 #ifdef _KERNEL
2177 int             scsi_command_string(struct ccb_scsiio *csio, struct sbuf *sb);
2178 int             scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
2179                                 scsi_sense_string_flags flags);
2180 char *          scsi_sense_string(struct ccb_scsiio *csio,
2181                                   char *str, int str_len);
2182 void            scsi_sense_print(struct ccb_scsiio *csio);
2183 #else /* _KERNEL */
2184 int             scsi_command_string(struct cam_device *device,
2185                                     struct ccb_scsiio *csio, struct sbuf *sb);
2186 int             scsi_sense_sbuf(struct cam_device *device, 
2187                                 struct ccb_scsiio *csio, struct sbuf *sb,
2188                                 scsi_sense_string_flags flags);
2189 char *          scsi_sense_string(struct cam_device *device, 
2190                                   struct ccb_scsiio *csio,
2191                                   char *str, int str_len);
2192 void            scsi_sense_print(struct cam_device *device, 
2193                                  struct ccb_scsiio *csio, FILE *ofile);
2194 #endif /* _KERNEL */
2195
2196 const char *    scsi_op_desc(u_int16_t opcode, 
2197                              struct scsi_inquiry_data *inq_data);
2198 char *          scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string,
2199                                 size_t len);
2200
2201 void            scsi_print_inquiry(struct scsi_inquiry_data *inq_data);
2202
2203 u_int           scsi_calc_syncsrate(u_int period_factor);
2204 u_int           scsi_calc_syncparam(u_int period);
2205
2206 typedef int     (*scsi_devid_checkfn_t)(uint8_t *);
2207 int             scsi_devid_is_naa_ieee_reg(uint8_t *bufp);
2208 int             scsi_devid_is_sas_target(uint8_t *bufp);
2209 uint8_t *       scsi_get_devid(struct scsi_vpd_device_id *id, uint32_t len,
2210                                scsi_devid_checkfn_t ck_fn);
2211
2212 void            scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries,
2213                                      void (*cbfcnp)(struct cam_periph *, 
2214                                                     union ccb *),
2215                                      u_int8_t tag_action, 
2216                                      u_int8_t sense_len, u_int32_t timeout);
2217
2218 void            scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
2219                                    void (*cbfcnp)(struct cam_periph *, 
2220                                                   union ccb *),
2221                                    void *data_ptr, u_int8_t dxfer_len,
2222                                    u_int8_t tag_action, u_int8_t sense_len,
2223                                    u_int32_t timeout);
2224
2225 void            scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
2226                              void (*cbfcnp)(struct cam_periph *, union ccb *),
2227                              u_int8_t tag_action, u_int8_t *inq_buf, 
2228                              u_int32_t inq_len, int evpd, u_int8_t page_code,
2229                              u_int8_t sense_len, u_int32_t timeout);
2230
2231 void            scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
2232                                 void (*cbfcnp)(struct cam_periph *,
2233                                                union ccb *),
2234                                 u_int8_t tag_action, int dbd,
2235                                 u_int8_t page_code, u_int8_t page,
2236                                 u_int8_t *param_buf, u_int32_t param_len,
2237                                 u_int8_t sense_len, u_int32_t timeout);
2238
2239 void            scsi_mode_sense_len(struct ccb_scsiio *csio, u_int32_t retries,
2240                                     void (*cbfcnp)(struct cam_periph *,
2241                                                    union ccb *),
2242                                     u_int8_t tag_action, int dbd,
2243                                     u_int8_t page_code, u_int8_t page,
2244                                     u_int8_t *param_buf, u_int32_t param_len,
2245                                     int minimum_cmd_size, u_int8_t sense_len,
2246                                     u_int32_t timeout);
2247
2248 void            scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
2249                                  void (*cbfcnp)(struct cam_periph *,
2250                                                 union ccb *),
2251                                  u_int8_t tag_action, int scsi_page_fmt,
2252                                  int save_pages, u_int8_t *param_buf,
2253                                  u_int32_t param_len, u_int8_t sense_len,
2254                                  u_int32_t timeout);
2255
2256 void            scsi_mode_select_len(struct ccb_scsiio *csio, u_int32_t retries,
2257                                      void (*cbfcnp)(struct cam_periph *,
2258                                                     union ccb *),
2259                                      u_int8_t tag_action, int scsi_page_fmt,
2260                                      int save_pages, u_int8_t *param_buf,
2261                                      u_int32_t param_len, int minimum_cmd_size,
2262                                      u_int8_t sense_len, u_int32_t timeout);
2263
2264 void            scsi_log_sense(struct ccb_scsiio *csio, u_int32_t retries,
2265                                void (*cbfcnp)(struct cam_periph *, union ccb *),
2266                                u_int8_t tag_action, u_int8_t page_code,
2267                                u_int8_t page, int save_pages, int ppc,
2268                                u_int32_t paramptr, u_int8_t *param_buf,
2269                                u_int32_t param_len, u_int8_t sense_len,
2270                                u_int32_t timeout);
2271
2272 void            scsi_log_select(struct ccb_scsiio *csio, u_int32_t retries,
2273                                 void (*cbfcnp)(struct cam_periph *,
2274                                 union ccb *), u_int8_t tag_action,
2275                                 u_int8_t page_code, int save_pages,
2276                                 int pc_reset, u_int8_t *param_buf,
2277                                 u_int32_t param_len, u_int8_t sense_len,
2278                                 u_int32_t timeout);
2279
2280 void            scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
2281                              void (*cbfcnp)(struct cam_periph *, union ccb *),
2282                              u_int8_t tag_action, u_int8_t action,
2283                              u_int8_t sense_len, u_int32_t timeout);
2284
2285 void            scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
2286                                    void (*cbfcnp)(struct cam_periph *, 
2287                                    union ccb *), u_int8_t tag_action, 
2288                                    struct scsi_read_capacity_data *,
2289                                    u_int8_t sense_len, u_int32_t timeout);
2290 void            scsi_read_capacity_16(struct ccb_scsiio *csio, uint32_t retries,
2291                                       void (*cbfcnp)(struct cam_periph *,
2292                                       union ccb *), uint8_t tag_action,
2293                                       uint64_t lba, int reladr, int pmi,
2294                                       struct scsi_read_capacity_data_long
2295                                       *rcap_buf, uint8_t sense_len,
2296                                       uint32_t timeout);
2297
2298 void            scsi_report_luns(struct ccb_scsiio *csio, u_int32_t retries,
2299                                  void (*cbfcnp)(struct cam_periph *, 
2300                                  union ccb *), u_int8_t tag_action, 
2301                                  u_int8_t select_report,
2302                                  struct scsi_report_luns_data *rpl_buf,
2303                                  u_int32_t alloc_len, u_int8_t sense_len,
2304                                  u_int32_t timeout);
2305
2306 void            scsi_report_target_group(struct ccb_scsiio *csio, u_int32_t retries,
2307                                  void (*cbfcnp)(struct cam_periph *, 
2308                                  union ccb *), u_int8_t tag_action, 
2309                                  u_int8_t pdf,
2310                                  void *buf,
2311                                  u_int32_t alloc_len, u_int8_t sense_len,
2312                                  u_int32_t timeout);
2313
2314 void            scsi_set_target_group(struct ccb_scsiio *csio, u_int32_t retries,
2315                                  void (*cbfcnp)(struct cam_periph *, 
2316                                  union ccb *), u_int8_t tag_action, void *buf,
2317                                  u_int32_t alloc_len, u_int8_t sense_len,
2318                                  u_int32_t timeout);
2319
2320 void            scsi_synchronize_cache(struct ccb_scsiio *csio, 
2321                                        u_int32_t retries,
2322                                        void (*cbfcnp)(struct cam_periph *, 
2323                                        union ccb *), u_int8_t tag_action, 
2324                                        u_int32_t begin_lba, u_int16_t lb_count,
2325                                        u_int8_t sense_len, u_int32_t timeout);
2326
2327 void scsi_receive_diagnostic_results(struct ccb_scsiio *csio, u_int32_t retries,
2328                                      void (*cbfcnp)(struct cam_periph *,
2329                                                     union ccb*),
2330                                      uint8_t tag_action, int pcv,
2331                                      uint8_t page_code, uint8_t *data_ptr,
2332                                      uint16_t allocation_length,
2333                                      uint8_t sense_len, uint32_t timeout);
2334
2335 void scsi_send_diagnostic(struct ccb_scsiio *csio, u_int32_t retries,
2336                           void (*cbfcnp)(struct cam_periph *, union ccb *),
2337                           uint8_t tag_action, int unit_offline,
2338                           int device_offline, int self_test, int page_format,
2339                           int self_test_code, uint8_t *data_ptr,
2340                           uint16_t param_list_length, uint8_t sense_len,
2341                           uint32_t timeout);
2342
2343 void scsi_read_buffer(struct ccb_scsiio *csio, u_int32_t retries,
2344                         void (*cbfcnp)(struct cam_periph *, union ccb*),
2345                         uint8_t tag_action, int mode,
2346                         uint8_t buffer_id, u_int32_t offset,
2347                         uint8_t *data_ptr, uint32_t allocation_length,
2348                         uint8_t sense_len, uint32_t timeout);
2349
2350 void scsi_write_buffer(struct ccb_scsiio *csio, u_int32_t retries,
2351                         void (*cbfcnp)(struct cam_periph *, union ccb *),
2352                         uint8_t tag_action, int mode,
2353                         uint8_t buffer_id, u_int32_t offset,
2354                         uint8_t *data_ptr, uint32_t param_list_length,
2355                         uint8_t sense_len, uint32_t timeout);
2356
2357 void scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
2358                      void (*cbfcnp)(struct cam_periph *, union ccb *),
2359                      u_int8_t tag_action, int readop, u_int8_t byte2, 
2360                      int minimum_cmd_size, u_int64_t lba,
2361                      u_int32_t block_count, u_int8_t *data_ptr,
2362                      u_int32_t dxfer_len, u_int8_t sense_len,
2363                      u_int32_t timeout);
2364
2365 void scsi_write_same(struct ccb_scsiio *csio, u_int32_t retries,
2366                      void (*cbfcnp)(struct cam_periph *, union ccb *),
2367                      u_int8_t tag_action, u_int8_t byte2, 
2368                      int minimum_cmd_size, u_int64_t lba,
2369                      u_int32_t block_count, u_int8_t *data_ptr,
2370                      u_int32_t dxfer_len, u_int8_t sense_len,
2371                      u_int32_t timeout);
2372
2373 void scsi_unmap(struct ccb_scsiio *csio, u_int32_t retries,
2374                 void (*cbfcnp)(struct cam_periph *, union ccb *),
2375                 u_int8_t tag_action, u_int8_t byte2,
2376                 u_int8_t *data_ptr, u_int16_t dxfer_len,
2377                 u_int8_t sense_len, u_int32_t timeout);
2378
2379 void scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
2380                      void (*cbfcnp)(struct cam_periph *, union ccb *),
2381                      u_int8_t tag_action, int start, int load_eject,
2382                      int immediate, u_int8_t sense_len, u_int32_t timeout);
2383
2384 int             scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry);
2385 int             scsi_static_inquiry_match(caddr_t inqbuffer,
2386                                           caddr_t table_entry);
2387 int             scsi_devid_match(uint8_t *rhs, size_t rhs_len,
2388                                  uint8_t *lhs, size_t lhs_len);
2389
2390 void scsi_extract_sense(struct scsi_sense_data *sense, int *error_code,
2391                         int *sense_key, int *asc, int *ascq);
2392 int scsi_extract_sense_ccb(union ccb *ccb, int *error_code, int *sense_key,
2393                            int *asc, int *ascq);
2394 void scsi_extract_sense_len(struct scsi_sense_data *sense,
2395                             u_int sense_len, int *error_code, int *sense_key,
2396                             int *asc, int *ascq, int show_errors);
2397 int scsi_get_sense_key(struct scsi_sense_data *sense, u_int sense_len,
2398                        int show_errors);
2399 int scsi_get_asc(struct scsi_sense_data *sense, u_int sense_len,
2400                  int show_errors);
2401 int scsi_get_ascq(struct scsi_sense_data *sense, u_int sense_len,
2402                   int show_errors);
2403 static __inline void scsi_ulto2b(u_int32_t val, u_int8_t *bytes);
2404 static __inline void scsi_ulto3b(u_int32_t val, u_int8_t *bytes);
2405 static __inline void scsi_ulto4b(u_int32_t val, u_int8_t *bytes);
2406 static __inline void scsi_u64to8b(u_int64_t val, u_int8_t *bytes);
2407 static __inline uint32_t scsi_2btoul(const uint8_t *bytes);
2408 static __inline uint32_t scsi_3btoul(const uint8_t *bytes);
2409 static __inline int32_t scsi_3btol(const uint8_t *bytes);
2410 static __inline uint32_t scsi_4btoul(const uint8_t *bytes);
2411 static __inline uint64_t scsi_8btou64(const uint8_t *bytes);
2412 static __inline void *find_mode_page_6(struct scsi_mode_header_6 *mode_header);
2413 static __inline void *find_mode_page_10(struct scsi_mode_header_10 *mode_header);
2414
2415 static __inline void
2416 scsi_ulto2b(u_int32_t val, u_int8_t *bytes)
2417 {
2418
2419         bytes[0] = (val >> 8) & 0xff;
2420         bytes[1] = val & 0xff;
2421 }
2422
2423 static __inline void
2424 scsi_ulto3b(u_int32_t val, u_int8_t *bytes)
2425 {
2426
2427         bytes[0] = (val >> 16) & 0xff;
2428         bytes[1] = (val >> 8) & 0xff;
2429         bytes[2] = val & 0xff;
2430 }
2431
2432 static __inline void
2433 scsi_ulto4b(u_int32_t val, u_int8_t *bytes)
2434 {
2435
2436         bytes[0] = (val >> 24) & 0xff;
2437         bytes[1] = (val >> 16) & 0xff;
2438         bytes[2] = (val >> 8) & 0xff;
2439         bytes[3] = val & 0xff;
2440 }
2441
2442 static __inline void
2443 scsi_u64to8b(u_int64_t val, u_int8_t *bytes)
2444 {
2445
2446         bytes[0] = (val >> 56) & 0xff;
2447         bytes[1] = (val >> 48) & 0xff;
2448         bytes[2] = (val >> 40) & 0xff;
2449         bytes[3] = (val >> 32) & 0xff;
2450         bytes[4] = (val >> 24) & 0xff;
2451         bytes[5] = (val >> 16) & 0xff;
2452         bytes[6] = (val >> 8) & 0xff;
2453         bytes[7] = val & 0xff;
2454 }
2455
2456 static __inline uint32_t
2457 scsi_2btoul(const uint8_t *bytes)
2458 {
2459         uint32_t rv;
2460
2461         rv = (bytes[0] << 8) |
2462              bytes[1];
2463         return (rv);
2464 }
2465
2466 static __inline uint32_t
2467 scsi_3btoul(const uint8_t *bytes)
2468 {
2469         uint32_t rv;
2470
2471         rv = (bytes[0] << 16) |
2472              (bytes[1] << 8) |
2473              bytes[2];
2474         return (rv);
2475 }
2476
2477 static __inline int32_t 
2478 scsi_3btol(const uint8_t *bytes)
2479 {
2480         uint32_t rc = scsi_3btoul(bytes);
2481  
2482         if (rc & 0x00800000)
2483                 rc |= 0xff000000;
2484
2485         return (int32_t) rc;
2486 }
2487
2488 static __inline uint32_t
2489 scsi_4btoul(const uint8_t *bytes)
2490 {
2491         uint32_t rv;
2492
2493         rv = (bytes[0] << 24) |
2494              (bytes[1] << 16) |
2495              (bytes[2] << 8) |
2496              bytes[3];
2497         return (rv);
2498 }
2499
2500 static __inline uint64_t
2501 scsi_8btou64(const uint8_t *bytes)
2502 {
2503         uint64_t rv;
2504  
2505         rv = (((uint64_t)bytes[0]) << 56) |
2506              (((uint64_t)bytes[1]) << 48) |
2507              (((uint64_t)bytes[2]) << 40) |
2508              (((uint64_t)bytes[3]) << 32) |
2509              (((uint64_t)bytes[4]) << 24) |
2510              (((uint64_t)bytes[5]) << 16) |
2511              (((uint64_t)bytes[6]) << 8) |
2512              bytes[7];
2513         return (rv);
2514 }
2515
2516 /*
2517  * Given the pointer to a returned mode sense buffer, return a pointer to
2518  * the start of the first mode page.
2519  */
2520 static __inline void *
2521 find_mode_page_6(struct scsi_mode_header_6 *mode_header)
2522 {
2523         void *page_start;
2524
2525         page_start = (void *)((u_int8_t *)&mode_header[1] +
2526                               mode_header->blk_desc_len);
2527
2528         return(page_start);
2529 }
2530
2531 static __inline void *
2532 find_mode_page_10(struct scsi_mode_header_10 *mode_header)
2533 {
2534         void *page_start;
2535
2536         page_start = (void *)((u_int8_t *)&mode_header[1] +
2537                                scsi_2btoul(mode_header->blk_desc_len));
2538
2539         return(page_start);
2540 }
2541
2542 __END_DECLS
2543
2544 #endif /*_SCSI_SCSI_ALL_H*/