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