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