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