xref: /freebsd/sys/cam/scsi/scsi_all.h (revision 35a04710d7286aa9538917fd7f8e417dbee95b82)
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_NODEVICE	0x1F
632 #define	T_ANY		0xFF	/* Used in Quirk table matches */
633 
634 #define T_REMOV		1
635 #define	T_FIXED		0
636 
637 /*
638  * This length is the initial inquiry length used by the probe code, as
639  * well as the legnth necessary for scsi_print_inquiry() to function
640  * correctly.  If either use requires a different length in the future,
641  * the two values should be de-coupled.
642  */
643 #define	SHORT_INQUIRY_LENGTH	36
644 
645 struct scsi_inquiry_data
646 {
647 	u_int8_t device;
648 #define	SID_TYPE(inq_data) ((inq_data)->device & 0x1f)
649 #define	SID_QUAL(inq_data) (((inq_data)->device & 0xE0) >> 5)
650 #define	SID_QUAL_LU_CONNECTED	0x00	/*
651 					 * The specified peripheral device
652 					 * type is currently connected to
653 					 * logical unit.  If the target cannot
654 					 * determine whether or not a physical
655 					 * device is currently connected, it
656 					 * shall also use this peripheral
657 					 * qualifier when returning the INQUIRY
658 					 * data.  This peripheral qualifier
659 					 * does not mean that the device is
660 					 * ready for access by the initiator.
661 					 */
662 #define	SID_QUAL_LU_OFFLINE	0x01	/*
663 					 * The target is capable of supporting
664 					 * the specified peripheral device type
665 					 * on this logical unit; however, the
666 					 * physical device is not currently
667 					 * connected to this logical unit.
668 					 */
669 #define SID_QUAL_RSVD		0x02
670 #define	SID_QUAL_BAD_LU		0x03	/*
671 					 * The target is not capable of
672 					 * supporting a physical device on
673 					 * this logical unit. For this
674 					 * peripheral qualifier the peripheral
675 					 * device type shall be set to 1Fh to
676 					 * provide compatibility with previous
677 					 * versions of SCSI. All other
678 					 * peripheral device type values are
679 					 * reserved for this peripheral
680 					 * qualifier.
681 					 */
682 #define	SID_QUAL_IS_VENDOR_UNIQUE(inq_data) ((SID_QUAL(inq_data) & 0x08) != 0)
683 	u_int8_t dev_qual2;
684 #define	SID_QUAL2	0x7F
685 #define	SID_IS_REMOVABLE(inq_data) (((inq_data)->dev_qual2 & 0x80) != 0)
686 	u_int8_t version;
687 #define SID_ANSI_REV(inq_data) ((inq_data)->version & 0x07)
688 #define		SCSI_REV_0		0
689 #define		SCSI_REV_CCS		1
690 #define		SCSI_REV_2		2
691 #define		SCSI_REV_SPC		3
692 #define		SCSI_REV_SPC2		4
693 
694 #define SID_ECMA	0x38
695 #define SID_ISO		0xC0
696 	u_int8_t response_format;
697 #define SID_AENC	0x80
698 #define SID_TrmIOP	0x40
699 	u_int8_t additional_length;
700 #define	SID_ADDITIONAL_LENGTH(iqd)					\
701 	((iqd)->additional_length +					\
702 	offsetof(struct scsi_inquiry_data, additional_length) + 1)
703 	u_int8_t reserved;
704 	u_int8_t spc2_flags;
705 #define SPC2_SID_MChngr 	0x08
706 #define SPC2_SID_MultiP 	0x10
707 #define SPC2_SID_EncServ	0x40
708 #define SPC2_SID_BQueue		0x80
709 
710 #define INQ_DATA_TQ_ENABLED(iqd)				\
711     ((SID_ANSI_REV(iqd) < SCSI_REV_SPC2)? ((iqd)->flags & SID_CmdQue) :	\
712     (((iqd)->flags & SID_CmdQue) && !((iqd)->spc2_flags & SPC2_SID_BQueue)) || \
713     (!((iqd)->flags & SID_CmdQue) && ((iqd)->spc2_flags & SPC2_SID_BQueue)))
714 
715 	u_int8_t flags;
716 #define	SID_SftRe	0x01
717 #define	SID_CmdQue	0x02
718 #define	SID_Linked	0x08
719 #define	SID_Sync	0x10
720 #define	SID_WBus16	0x20
721 #define	SID_WBus32	0x40
722 #define	SID_RelAdr	0x80
723 #define SID_VENDOR_SIZE   8
724 	char	 vendor[SID_VENDOR_SIZE];
725 #define SID_PRODUCT_SIZE  16
726 	char	 product[SID_PRODUCT_SIZE];
727 #define SID_REVISION_SIZE 4
728 	char	 revision[SID_REVISION_SIZE];
729 	/*
730 	 * The following fields were taken from SCSI Primary Commands - 2
731 	 * (SPC-2) Revision 14, Dated 11 November 1999
732 	 */
733 #define	SID_VENDOR_SPECIFIC_0_SIZE	20
734 	u_int8_t vendor_specific0[SID_VENDOR_SPECIFIC_0_SIZE];
735 	/*
736 	 * An extension of SCSI Parallel Specific Values
737 	 */
738 #define	SID_SPI_IUS		0x01
739 #define	SID_SPI_QAS		0x02
740 #define	SID_SPI_CLOCK_ST	0x00
741 #define	SID_SPI_CLOCK_DT	0x04
742 #define	SID_SPI_CLOCK_DT_ST	0x0C
743 #define	SID_SPI_MASK		0x0F
744 	u_int8_t spi3data;
745 	u_int8_t reserved2;
746 	/*
747 	 * Version Descriptors, stored 2 byte values.
748 	 */
749 	u_int8_t version1[2];
750 	u_int8_t version2[2];
751 	u_int8_t version3[2];
752 	u_int8_t version4[2];
753 	u_int8_t version5[2];
754 	u_int8_t version6[2];
755 	u_int8_t version7[2];
756 	u_int8_t version8[2];
757 
758 	u_int8_t reserved3[22];
759 
760 #define	SID_VENDOR_SPECIFIC_1_SIZE	160
761 	u_int8_t vendor_specific1[SID_VENDOR_SPECIFIC_1_SIZE];
762 };
763 
764 struct scsi_vpd_supported_page_list
765 {
766 	u_int8_t device;
767 	u_int8_t page_code;
768 #define SVPD_SUPPORTED_PAGE_LIST 0x00
769 	u_int8_t reserved;
770 	u_int8_t length;	/* number of VPD entries */
771 #define SVPD_SUPPORTED_PAGES_SIZE	251
772 	u_int8_t list[SVPD_SUPPORTED_PAGES_SIZE];
773 };
774 
775 struct scsi_vpd_unit_serial_number
776 {
777 	u_int8_t device;
778 	u_int8_t page_code;
779 #define SVPD_UNIT_SERIAL_NUMBER	0x80
780 	u_int8_t reserved;
781 	u_int8_t length; /* serial number length */
782 #define SVPD_SERIAL_NUM_SIZE 251
783 	u_int8_t serial_num[SVPD_SERIAL_NUM_SIZE];
784 };
785 
786 struct scsi_read_capacity
787 {
788 	u_int8_t opcode;
789 	u_int8_t byte2;
790 	u_int8_t addr[4];
791 	u_int8_t unused[3];
792 	u_int8_t control;
793 };
794 
795 struct scsi_read_capacity_16
796 {
797 	uint8_t opcode;
798 #define	SRC16_SERVICE_ACTION	0x10
799 	uint8_t service_action;
800 	uint8_t addr[8];
801 	uint8_t alloc_len[4];
802 #define	SRC16_PMI		0x01
803 #define	SRC16_RELADR		0x02
804 	uint8_t reladr;
805 	uint8_t control;
806 };
807 
808 struct scsi_read_capacity_data
809 {
810 	u_int8_t addr[4];
811 	u_int8_t length[4];
812 };
813 
814 struct scsi_read_capacity_data_long
815 {
816 	uint8_t addr[8];
817 	uint8_t length[4];
818 };
819 
820 struct scsi_report_luns
821 {
822 	uint8_t opcode;
823 	uint8_t reserved1;
824 #define	RPL_REPORT_DEFAULT	0x00
825 #define	RPL_REPORT_WELLKNOWN	0x01
826 #define	RPL_REPORT_ALL		0x02
827 	uint8_t select_report;
828 	uint8_t reserved2[3];
829 	uint8_t length[4];
830 	uint8_t reserved3;
831 	uint8_t control;
832 };
833 
834 struct scsi_report_luns_data {
835 	u_int8_t length[4];	/* length of LUN inventory, in bytes */
836 	u_int8_t reserved[4];	/* unused */
837 	/*
838 	 * LUN inventory- we only support the type zero form for now.
839 	 */
840 	struct {
841 		u_int8_t lundata[8];
842 	} luns[0];
843 };
844 #define	RPL_LUNDATA_PERIPH_BUS_MASK	0x3f
845 #define	RPL_LUNDATA_FLAT_LUN_MASK	0x3f
846 #define	RPL_LUNDATA_LUN_TARG_MASK	0x3f
847 #define	RPL_LUNDATA_LUN_BUS_MASK	0xe0
848 #define	RPL_LUNDATA_LUN_LUN_MASK	0x1f
849 #define	RPL_LUNDATA_EXT_LEN_MASK	0x30
850 #define	RPL_LUNDATA_EXT_EAM_MASK	0x0f
851 #define	RPL_LUNDATA_EXT_EAM_WK		0x01
852 #define	RPL_LUNDATA_EXT_EAM_NOT_SPEC	0x0f
853 #define	RPL_LUNDATA_ATYP_MASK	0xc0	/* MBZ for type 0 lun */
854 #define	RPL_LUNDATA_ATYP_PERIPH	0x00
855 #define	RPL_LUNDATA_ATYP_FLAT	0x40
856 #define	RPL_LUNDATA_ATYP_LUN	0x80
857 #define	RPL_LUNDATA_ATYP_EXTLUN	0xc0
858 
859 
860 struct scsi_sense_data
861 {
862 	u_int8_t error_code;
863 #define	SSD_ERRCODE			0x7F
864 #define		SSD_CURRENT_ERROR	0x70
865 #define		SSD_DEFERRED_ERROR	0x71
866 #define	SSD_ERRCODE_VALID	0x80
867 	u_int8_t segment;
868 	u_int8_t flags;
869 #define	SSD_KEY				0x0F
870 #define		SSD_KEY_NO_SENSE	0x00
871 #define		SSD_KEY_RECOVERED_ERROR	0x01
872 #define		SSD_KEY_NOT_READY	0x02
873 #define		SSD_KEY_MEDIUM_ERROR	0x03
874 #define		SSD_KEY_HARDWARE_ERROR	0x04
875 #define		SSD_KEY_ILLEGAL_REQUEST	0x05
876 #define		SSD_KEY_UNIT_ATTENTION	0x06
877 #define		SSD_KEY_DATA_PROTECT	0x07
878 #define		SSD_KEY_BLANK_CHECK	0x08
879 #define		SSD_KEY_Vendor_Specific	0x09
880 #define		SSD_KEY_COPY_ABORTED	0x0a
881 #define		SSD_KEY_ABORTED_COMMAND	0x0b
882 #define		SSD_KEY_EQUAL		0x0c
883 #define		SSD_KEY_VOLUME_OVERFLOW	0x0d
884 #define		SSD_KEY_MISCOMPARE	0x0e
885 #define		SSD_KEY_RESERVED	0x0f
886 #define	SSD_ILI		0x20
887 #define	SSD_EOM		0x40
888 #define	SSD_FILEMARK	0x80
889 	u_int8_t info[4];
890 	u_int8_t extra_len;
891 	u_int8_t cmd_spec_info[4];
892 	u_int8_t add_sense_code;
893 	u_int8_t add_sense_code_qual;
894 	u_int8_t fru;
895 	u_int8_t sense_key_spec[3];
896 #define	SSD_SCS_VALID		0x80
897 #define SSD_FIELDPTR_CMD	0x40
898 #define SSD_BITPTR_VALID	0x08
899 #define SSD_BITPTR_VALUE	0x07
900 #define SSD_MIN_SIZE 18
901 	u_int8_t extra_bytes[14];
902 #define SSD_FULL_SIZE sizeof(struct scsi_sense_data)
903 };
904 
905 struct scsi_mode_header_6
906 {
907 	u_int8_t data_length;	/* Sense data length */
908 	u_int8_t medium_type;
909 	u_int8_t dev_spec;
910 	u_int8_t blk_desc_len;
911 };
912 
913 struct scsi_mode_header_10
914 {
915 	u_int8_t data_length[2];/* Sense data length */
916 	u_int8_t medium_type;
917 	u_int8_t dev_spec;
918 	u_int8_t unused[2];
919 	u_int8_t blk_desc_len[2];
920 };
921 
922 struct scsi_mode_page_header
923 {
924 	u_int8_t page_code;
925 	u_int8_t page_length;
926 };
927 
928 struct scsi_mode_blk_desc
929 {
930 	u_int8_t density;
931 	u_int8_t nblocks[3];
932 	u_int8_t reserved;
933 	u_int8_t blklen[3];
934 };
935 
936 #define	SCSI_DEFAULT_DENSITY	0x00	/* use 'default' density */
937 #define	SCSI_SAME_DENSITY	0x7f	/* use 'same' density- >= SCSI-2 only */
938 
939 
940 /*
941  * Status Byte
942  */
943 #define	SCSI_STATUS_OK			0x00
944 #define	SCSI_STATUS_CHECK_COND		0x02
945 #define	SCSI_STATUS_COND_MET		0x04
946 #define	SCSI_STATUS_BUSY		0x08
947 #define SCSI_STATUS_INTERMED		0x10
948 #define SCSI_STATUS_INTERMED_COND_MET	0x14
949 #define SCSI_STATUS_RESERV_CONFLICT	0x18
950 #define SCSI_STATUS_CMD_TERMINATED	0x22	/* Obsolete in SAM-2 */
951 #define SCSI_STATUS_QUEUE_FULL		0x28
952 #define SCSI_STATUS_ACA_ACTIVE		0x30
953 #define SCSI_STATUS_TASK_ABORTED	0x40
954 
955 struct scsi_inquiry_pattern {
956 	u_int8_t   type;
957 	u_int8_t   media_type;
958 #define	SIP_MEDIA_REMOVABLE	0x01
959 #define	SIP_MEDIA_FIXED		0x02
960 	const char *vendor;
961 	const char *product;
962 	const char *revision;
963 };
964 
965 struct scsi_static_inquiry_pattern {
966 	u_int8_t   type;
967 	u_int8_t   media_type;
968 	char       vendor[SID_VENDOR_SIZE+1];
969 	char       product[SID_PRODUCT_SIZE+1];
970 	char       revision[SID_REVISION_SIZE+1];
971 };
972 
973 struct scsi_sense_quirk_entry {
974 	struct scsi_inquiry_pattern	inq_pat;
975 	int				num_sense_keys;
976 	int				num_ascs;
977 	struct sense_key_table_entry	*sense_key_info;
978 	struct asc_table_entry		*asc_info;
979 };
980 
981 struct sense_key_table_entry {
982 	u_int8_t    sense_key;
983 	u_int32_t   action;
984 	const char *desc;
985 };
986 
987 struct asc_table_entry {
988 	u_int8_t    asc;
989 	u_int8_t    ascq;
990 	u_int32_t   action;
991 	const char *desc;
992 };
993 
994 struct op_table_entry {
995 	u_int8_t    opcode;
996 	u_int16_t   opmask;
997 	const char  *desc;
998 };
999 
1000 struct scsi_op_quirk_entry {
1001 	struct scsi_inquiry_pattern	inq_pat;
1002 	int				num_ops;
1003 	struct op_table_entry		*op_table;
1004 };
1005 
1006 typedef enum {
1007 	SSS_FLAG_NONE		= 0x00,
1008 	SSS_FLAG_PRINT_COMMAND	= 0x01
1009 } scsi_sense_string_flags;
1010 
1011 struct ccb_scsiio;
1012 struct cam_periph;
1013 union  ccb;
1014 #ifndef _KERNEL
1015 struct cam_device;
1016 #endif
1017 
1018 extern const char *scsi_sense_key_text[];
1019 
1020 struct sbuf;
1021 
1022 __BEGIN_DECLS
1023 void scsi_sense_desc(int sense_key, int asc, int ascq,
1024 		     struct scsi_inquiry_data *inq_data,
1025 		     const char **sense_key_desc, const char **asc_desc);
1026 scsi_sense_action scsi_error_action(struct ccb_scsiio* csio,
1027 				    struct scsi_inquiry_data *inq_data,
1028 				    u_int32_t sense_flags);
1029 const char *	scsi_status_string(struct ccb_scsiio *csio);
1030 #ifdef _KERNEL
1031 int		scsi_command_string(struct ccb_scsiio *csio, struct sbuf *sb);
1032 int		scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
1033 				scsi_sense_string_flags flags);
1034 char *		scsi_sense_string(struct ccb_scsiio *csio,
1035 				  char *str, int str_len);
1036 void		scsi_sense_print(struct ccb_scsiio *csio);
1037 int		scsi_interpret_sense(union ccb *ccb,
1038 				     u_int32_t sense_flags,
1039 				     u_int32_t *relsim_flags,
1040 				     u_int32_t *reduction,
1041 				     u_int32_t *timeout,
1042 				     scsi_sense_action error_action);
1043 #else /* _KERNEL */
1044 int		scsi_command_string(struct cam_device *device,
1045 				    struct ccb_scsiio *csio, struct sbuf *sb);
1046 int		scsi_sense_sbuf(struct cam_device *device,
1047 				struct ccb_scsiio *csio, struct sbuf *sb,
1048 				scsi_sense_string_flags flags);
1049 char *		scsi_sense_string(struct cam_device *device,
1050 				  struct ccb_scsiio *csio,
1051 				  char *str, int str_len);
1052 void		scsi_sense_print(struct cam_device *device,
1053 				 struct ccb_scsiio *csio, FILE *ofile);
1054 int		scsi_interpret_sense(struct cam_device *device,
1055 				     union ccb *ccb,
1056 				     u_int32_t sense_flags,
1057 				     u_int32_t *relsim_flags,
1058 				     u_int32_t *reduction,
1059 				     u_int32_t *timeout,
1060 				     scsi_sense_action error_action);
1061 #endif /* _KERNEL */
1062 
1063 #define	SF_RETRY_UA	0x01
1064 #define SF_NO_PRINT	0x02
1065 #define SF_QUIET_IR	0x04	/* Be quiet about Illegal Request reponses */
1066 #define SF_PRINT_ALWAYS	0x08
1067 
1068 
1069 const char *	scsi_op_desc(u_int16_t opcode,
1070 			     struct scsi_inquiry_data *inq_data);
1071 char *		scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string,
1072 				size_t len);
1073 
1074 void		scsi_print_inquiry(struct scsi_inquiry_data *inq_data);
1075 
1076 u_int		scsi_calc_syncsrate(u_int period_factor);
1077 u_int		scsi_calc_syncparam(u_int period);
1078 
1079 void		scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries,
1080 				     void (*cbfcnp)(struct cam_periph *,
1081 						    union ccb *),
1082 				     u_int8_t tag_action,
1083 				     u_int8_t sense_len, u_int32_t timeout);
1084 
1085 void		scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
1086 				   void (*cbfcnp)(struct cam_periph *,
1087 						  union ccb *),
1088 				   void *data_ptr, u_int8_t dxfer_len,
1089 				   u_int8_t tag_action, u_int8_t sense_len,
1090 				   u_int32_t timeout);
1091 
1092 void		scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
1093 			     void (*cbfcnp)(struct cam_periph *, union ccb *),
1094 			     u_int8_t tag_action, u_int8_t *inq_buf,
1095 			     u_int32_t inq_len, int evpd, u_int8_t page_code,
1096 			     u_int8_t sense_len, u_int32_t timeout);
1097 
1098 void		scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
1099 				void (*cbfcnp)(struct cam_periph *,
1100 					       union ccb *),
1101 				u_int8_t tag_action, int dbd,
1102 				u_int8_t page_code, u_int8_t page,
1103 				u_int8_t *param_buf, u_int32_t param_len,
1104 				u_int8_t sense_len, u_int32_t timeout);
1105 
1106 void		scsi_mode_sense_len(struct ccb_scsiio *csio, u_int32_t retries,
1107 				    void (*cbfcnp)(struct cam_periph *,
1108 						   union ccb *),
1109 				    u_int8_t tag_action, int dbd,
1110 				    u_int8_t page_code, u_int8_t page,
1111 				    u_int8_t *param_buf, u_int32_t param_len,
1112 				    int minimum_cmd_size, u_int8_t sense_len,
1113 				    u_int32_t timeout);
1114 
1115 void		scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
1116 				 void (*cbfcnp)(struct cam_periph *,
1117 						union ccb *),
1118 				 u_int8_t tag_action, int scsi_page_fmt,
1119 				 int save_pages, u_int8_t *param_buf,
1120 				 u_int32_t param_len, u_int8_t sense_len,
1121 				 u_int32_t timeout);
1122 
1123 void		scsi_mode_select_len(struct ccb_scsiio *csio, u_int32_t retries,
1124 				     void (*cbfcnp)(struct cam_periph *,
1125 						    union ccb *),
1126 				     u_int8_t tag_action, int scsi_page_fmt,
1127 				     int save_pages, u_int8_t *param_buf,
1128 				     u_int32_t param_len, int minimum_cmd_size,
1129 				     u_int8_t sense_len, u_int32_t timeout);
1130 
1131 void		scsi_log_sense(struct ccb_scsiio *csio, u_int32_t retries,
1132 			       void (*cbfcnp)(struct cam_periph *, union ccb *),
1133 			       u_int8_t tag_action, u_int8_t page_code,
1134 			       u_int8_t page, int save_pages, int ppc,
1135 			       u_int32_t paramptr, u_int8_t *param_buf,
1136 			       u_int32_t param_len, u_int8_t sense_len,
1137 			       u_int32_t timeout);
1138 
1139 void		scsi_log_select(struct ccb_scsiio *csio, u_int32_t retries,
1140 				void (*cbfcnp)(struct cam_periph *,
1141 				union ccb *), u_int8_t tag_action,
1142 				u_int8_t page_code, int save_pages,
1143 				int pc_reset, u_int8_t *param_buf,
1144 				u_int32_t param_len, u_int8_t sense_len,
1145 				u_int32_t timeout);
1146 
1147 void		scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
1148 			     void (*cbfcnp)(struct cam_periph *, union ccb *),
1149 			     u_int8_t tag_action, u_int8_t action,
1150 			     u_int8_t sense_len, u_int32_t timeout);
1151 
1152 void		scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
1153 				   void (*cbfcnp)(struct cam_periph *,
1154 				   union ccb *), u_int8_t tag_action,
1155 				   struct scsi_read_capacity_data *,
1156 				   u_int8_t sense_len, u_int32_t timeout);
1157 void		scsi_read_capacity_16(struct ccb_scsiio *csio, uint32_t retries,
1158 				      void (*cbfcnp)(struct cam_periph *,
1159 				      union ccb *), uint8_t tag_action,
1160 				      uint64_t lba, int reladr, int pmi,
1161 				      struct scsi_read_capacity_data_long
1162 				      *rcap_buf, uint8_t sense_len,
1163 				      uint32_t timeout);
1164 
1165 void		scsi_report_luns(struct ccb_scsiio *csio, u_int32_t retries,
1166 				 void (*cbfcnp)(struct cam_periph *,
1167 				 union ccb *), u_int8_t tag_action,
1168 				 u_int8_t select_report,
1169 				 struct scsi_report_luns_data *rpl_buf,
1170 				 u_int32_t alloc_len, u_int8_t sense_len,
1171 				 u_int32_t timeout);
1172 
1173 void		scsi_synchronize_cache(struct ccb_scsiio *csio,
1174 				       u_int32_t retries,
1175 				       void (*cbfcnp)(struct cam_periph *,
1176 				       union ccb *), u_int8_t tag_action,
1177 				       u_int32_t begin_lba, u_int16_t lb_count,
1178 				       u_int8_t sense_len, u_int32_t timeout);
1179 
1180 void scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
1181 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
1182 		     u_int8_t tag_action, int readop, u_int8_t byte2,
1183 		     int minimum_cmd_size, u_int64_t lba,
1184 		     u_int32_t block_count, u_int8_t *data_ptr,
1185 		     u_int32_t dxfer_len, u_int8_t sense_len,
1186 		     u_int32_t timeout);
1187 
1188 void scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
1189 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
1190 		     u_int8_t tag_action, int start, int load_eject,
1191 		     int immediate, u_int8_t sense_len, u_int32_t timeout);
1192 
1193 int		scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry);
1194 int		scsi_static_inquiry_match(caddr_t inqbuffer,
1195 					  caddr_t table_entry);
1196 
1197 static __inline void scsi_extract_sense(struct scsi_sense_data *sense,
1198 					int *error_code, int *sense_key,
1199 					int *asc, int *ascq);
1200 static __inline void scsi_ulto2b(u_int32_t val, u_int8_t *bytes);
1201 static __inline void scsi_ulto3b(u_int32_t val, u_int8_t *bytes);
1202 static __inline void scsi_ulto4b(u_int32_t val, u_int8_t *bytes);
1203 static __inline void scsi_u64to8b(u_int64_t val, u_int8_t *bytes);
1204 static __inline u_int32_t scsi_2btoul(u_int8_t *bytes);
1205 static __inline u_int32_t scsi_3btoul(u_int8_t *bytes);
1206 static __inline int32_t scsi_3btol(u_int8_t *bytes);
1207 static __inline u_int32_t scsi_4btoul(u_int8_t *bytes);
1208 static __inline u_int64_t scsi_8btou64(u_int8_t *bytes);
1209 static __inline void *find_mode_page_6(struct scsi_mode_header_6 *mode_header);
1210 static __inline void *find_mode_page_10(struct scsi_mode_header_10 *mode_header);
1211 
1212 static __inline void scsi_extract_sense(struct scsi_sense_data *sense,
1213 					int *error_code, int *sense_key,
1214 					int *asc, int *ascq)
1215 {
1216 	*error_code = sense->error_code & SSD_ERRCODE;
1217 	*sense_key = sense->flags & SSD_KEY;
1218 	*asc = (sense->extra_len >= 5) ? sense->add_sense_code : 0;
1219 	*ascq = (sense->extra_len >= 6) ? sense->add_sense_code_qual : 0;
1220 }
1221 
1222 static __inline void
1223 scsi_ulto2b(u_int32_t val, u_int8_t *bytes)
1224 {
1225 
1226 	bytes[0] = (val >> 8) & 0xff;
1227 	bytes[1] = val & 0xff;
1228 }
1229 
1230 static __inline void
1231 scsi_ulto3b(u_int32_t val, u_int8_t *bytes)
1232 {
1233 
1234 	bytes[0] = (val >> 16) & 0xff;
1235 	bytes[1] = (val >> 8) & 0xff;
1236 	bytes[2] = val & 0xff;
1237 }
1238 
1239 static __inline void
1240 scsi_ulto4b(u_int32_t val, u_int8_t *bytes)
1241 {
1242 
1243 	bytes[0] = (val >> 24) & 0xff;
1244 	bytes[1] = (val >> 16) & 0xff;
1245 	bytes[2] = (val >> 8) & 0xff;
1246 	bytes[3] = val & 0xff;
1247 }
1248 
1249 static __inline void
1250 scsi_u64to8b(u_int64_t val, u_int8_t *bytes)
1251 {
1252 
1253 	bytes[0] = (val >> 56) & 0xff;
1254 	bytes[1] = (val >> 48) & 0xff;
1255 	bytes[2] = (val >> 40) & 0xff;
1256 	bytes[3] = (val >> 32) & 0xff;
1257 	bytes[4] = (val >> 24) & 0xff;
1258 	bytes[5] = (val >> 16) & 0xff;
1259 	bytes[6] = (val >> 8) & 0xff;
1260 	bytes[7] = val & 0xff;
1261 }
1262 
1263 static __inline u_int32_t
1264 scsi_2btoul(u_int8_t *bytes)
1265 {
1266 	u_int32_t rv;
1267 
1268 	rv = (bytes[0] << 8) |
1269 	     bytes[1];
1270 	return (rv);
1271 }
1272 
1273 static __inline u_int32_t
1274 scsi_3btoul(u_int8_t *bytes)
1275 {
1276 	u_int32_t rv;
1277 
1278 	rv = (bytes[0] << 16) |
1279 	     (bytes[1] << 8) |
1280 	     bytes[2];
1281 	return (rv);
1282 }
1283 
1284 static __inline int32_t
1285 scsi_3btol(u_int8_t *bytes)
1286 {
1287 	u_int32_t rc = scsi_3btoul(bytes);
1288 
1289 	if (rc & 0x00800000)
1290 		rc |= 0xff000000;
1291 
1292 	return (int32_t) rc;
1293 }
1294 
1295 static __inline u_int32_t
1296 scsi_4btoul(u_int8_t *bytes)
1297 {
1298 	u_int32_t rv;
1299 
1300 	rv = (bytes[0] << 24) |
1301 	     (bytes[1] << 16) |
1302 	     (bytes[2] << 8) |
1303 	     bytes[3];
1304 	return (rv);
1305 }
1306 
1307 static __inline uint64_t
1308 scsi_8btou64(uint8_t *bytes)
1309 {
1310         uint64_t rv;
1311 
1312 	rv = (((uint64_t)bytes[0]) << 56) |
1313 	     (((uint64_t)bytes[1]) << 48) |
1314 	     (((uint64_t)bytes[2]) << 40) |
1315 	     (((uint64_t)bytes[3]) << 32) |
1316 	     (((uint64_t)bytes[4]) << 24) |
1317 	     (((uint64_t)bytes[5]) << 16) |
1318 	     (((uint64_t)bytes[6]) << 8) |
1319 	     bytes[7];
1320 	return (rv);
1321 }
1322 
1323 /*
1324  * Given the pointer to a returned mode sense buffer, return a pointer to
1325  * the start of the first mode page.
1326  */
1327 static __inline void *
1328 find_mode_page_6(struct scsi_mode_header_6 *mode_header)
1329 {
1330 	void *page_start;
1331 
1332 	page_start = (void *)((u_int8_t *)&mode_header[1] +
1333 			      mode_header->blk_desc_len);
1334 
1335 	return(page_start);
1336 }
1337 
1338 static __inline void *
1339 find_mode_page_10(struct scsi_mode_header_10 *mode_header)
1340 {
1341 	void *page_start;
1342 
1343 	page_start = (void *)((u_int8_t *)&mode_header[1] +
1344 			       scsi_2btoul(mode_header->blk_desc_len));
1345 
1346 	return(page_start);
1347 }
1348 
1349 __END_DECLS
1350 
1351 #endif /*_SCSI_SCSI_ALL_H*/
1352