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