xref: /freebsd/sys/cam/scsi/scsi_all.h (revision 9207b4cff7b8d483f4dd3c62266c2b58819eb7f9)
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 MOVE_MEDIUM     	0xa5
509 #define READ_12			0xa8
510 #define WRITE_12		0xaa
511 #define READ_ELEMENT_STATUS	0xb8
512 
513 
514 /*
515  * Device Types
516  */
517 #define T_DIRECT	0x00
518 #define T_SEQUENTIAL	0x01
519 #define T_PRINTER	0x02
520 #define T_PROCESSOR	0x03
521 #define T_WORM		0x04
522 #define T_CDROM		0x05
523 #define T_SCANNER 	0x06
524 #define T_OPTICAL 	0x07
525 #define T_CHANGER	0x08
526 #define T_COMM		0x09
527 #define T_ASC0		0x0a
528 #define T_ASC1		0x0b
529 #define	T_STORARRAY	0x0c
530 #define	T_ENCLOSURE	0x0d
531 #define	T_RBC		0x0e
532 #define	T_OCRW		0x0f
533 #define T_NODEVICE	0x1F
534 #define	T_ANY		0xFF	/* Used in Quirk table matches */
535 
536 #define T_REMOV		1
537 #define	T_FIXED		0
538 
539 /*
540  * This length is the initial inquiry length used by the probe code, as
541  * well as the legnth necessary for scsi_print_inquiry() to function
542  * correctly.  If either use requires a different length in the future,
543  * the two values should be de-coupled.
544  */
545 #define	SHORT_INQUIRY_LENGTH	36
546 
547 struct scsi_inquiry_data
548 {
549 	u_int8_t device;
550 #define	SID_TYPE(inq_data) ((inq_data)->device & 0x1f)
551 #define	SID_QUAL(inq_data) (((inq_data)->device & 0xE0) >> 5)
552 #define	SID_QUAL_LU_CONNECTED	0x00	/*
553 					 * The specified peripheral device
554 					 * type is currently connected to
555 					 * logical unit.  If the target cannot
556 					 * determine whether or not a physical
557 					 * device is currently connected, it
558 					 * shall also use this peripheral
559 					 * qualifier when returning the INQUIRY
560 					 * data.  This peripheral qualifier
561 					 * does not mean that the device is
562 					 * ready for access by the initiator.
563 					 */
564 #define	SID_QUAL_LU_OFFLINE	0x01	/*
565 					 * The target is capable of supporting
566 					 * the specified peripheral device type
567 					 * on this logical unit; however, the
568 					 * physical device is not currently
569 					 * connected to this logical unit.
570 					 */
571 #define SID_QUAL_RSVD		0x02
572 #define	SID_QUAL_BAD_LU		0x03	/*
573 					 * The target is not capable of
574 					 * supporting a physical device on
575 					 * this logical unit. For this
576 					 * peripheral qualifier the peripheral
577 					 * device type shall be set to 1Fh to
578 					 * provide compatibility with previous
579 					 * versions of SCSI. All other
580 					 * peripheral device type values are
581 					 * reserved for this peripheral
582 					 * qualifier.
583 					 */
584 #define	SID_QUAL_IS_VENDOR_UNIQUE(inq_data) ((SID_QUAL(inq_data) & 0x08) != 0)
585 	u_int8_t dev_qual2;
586 #define	SID_QUAL2	0x7F
587 #define	SID_IS_REMOVABLE(inq_data) (((inq_data)->dev_qual2 & 0x80) != 0)
588 	u_int8_t version;
589 #define SID_ANSI_REV(inq_data) ((inq_data)->version & 0x07)
590 #define		SCSI_REV_0		0
591 #define		SCSI_REV_CCS		1
592 #define		SCSI_REV_2		2
593 #define		SCSI_REV_SPC		3
594 #define		SCSI_REV_SPC2		4
595 
596 #define SID_ECMA	0x38
597 #define SID_ISO		0xC0
598 	u_int8_t response_format;
599 #define SID_AENC	0x80
600 #define SID_TrmIOP	0x40
601 	u_int8_t additional_length;
602 	u_int8_t reserved[2];
603 	u_int8_t flags;
604 #define	SID_SftRe	0x01
605 #define	SID_CmdQue	0x02
606 #define	SID_Linked	0x08
607 #define	SID_Sync	0x10
608 #define	SID_WBus16	0x20
609 #define	SID_WBus32	0x40
610 #define	SID_RelAdr	0x80
611 #define SID_VENDOR_SIZE   8
612 	char	 vendor[SID_VENDOR_SIZE];
613 #define SID_PRODUCT_SIZE  16
614 	char	 product[SID_PRODUCT_SIZE];
615 #define SID_REVISION_SIZE 4
616 	char	 revision[SID_REVISION_SIZE];
617 	/*
618 	 * The following fields were taken from SCSI Primary Commands - 2
619 	 * (SPC-2) Revision 14, Dated 11 November 1999
620 	 */
621 #define	SID_VENDOR_SPECIFIC_0_SIZE	20
622 	u_int8_t vendor_specific0[SID_VENDOR_SPECIFIC_0_SIZE];
623 	/*
624 	 * An extension of SCSI Parallel Specific Values
625 	 */
626 #define	SID_SPI_IUS		0x01
627 #define	SID_SPI_QAS		0x02
628 #define	SID_SPI_CLOCK_ST	0x00
629 #define	SID_SPI_CLOCK_DT	0x04
630 #define	SID_SPI_CLOCK_DT_ST	0x0C
631 #define	SID_SPI_MASK		0x0F
632 	u_int8_t spi3data;
633 	u_int8_t reserved2;
634 	/*
635 	 * Version Descriptors, stored 2 byte values.
636 	 */
637 	u_int8_t version1[2];
638 	u_int8_t version2[2];
639 	u_int8_t version3[2];
640 	u_int8_t version4[2];
641 	u_int8_t version5[2];
642 	u_int8_t version6[2];
643 	u_int8_t version7[2];
644 	u_int8_t version8[2];
645 
646 	u_int8_t reserved3[22];
647 
648 #define	SID_VENDOR_SPECIFIC_1_SIZE	160
649 	u_int8_t vendor_specific1[SID_VENDOR_SPECIFIC_1_SIZE];
650 };
651 
652 struct scsi_vpd_unit_serial_number
653 {
654 	u_int8_t device;
655 	u_int8_t page_code;
656 #define SVPD_UNIT_SERIAL_NUMBER	0x80
657 	u_int8_t reserved;
658 	u_int8_t length; /* serial number length */
659 #define SVPD_SERIAL_NUM_SIZE 251
660 	u_int8_t serial_num[SVPD_SERIAL_NUM_SIZE];
661 };
662 
663 struct scsi_read_capacity
664 {
665 	u_int8_t opcode;
666 	u_int8_t byte2;
667 	u_int8_t addr[4];
668 	u_int8_t unused[3];
669 	u_int8_t control;
670 };
671 
672 struct scsi_read_capacity_data
673 {
674 	u_int8_t addr[4];
675 	u_int8_t length[4];
676 };
677 
678 struct scsi_sense_data
679 {
680 	u_int8_t error_code;
681 #define	SSD_ERRCODE			0x7F
682 #define		SSD_CURRENT_ERROR	0x70
683 #define		SSD_DEFERRED_ERROR	0x71
684 #define	SSD_ERRCODE_VALID	0x80
685 	u_int8_t segment;
686 	u_int8_t flags;
687 #define	SSD_KEY				0x0F
688 #define		SSD_KEY_NO_SENSE	0x00
689 #define		SSD_KEY_RECOVERED_ERROR	0x01
690 #define		SSD_KEY_NOT_READY	0x02
691 #define		SSD_KEY_MEDIUM_ERROR	0x03
692 #define		SSD_KEY_HARDWARE_ERROR	0x04
693 #define		SSD_KEY_ILLEGAL_REQUEST	0x05
694 #define		SSD_KEY_UNIT_ATTENTION	0x06
695 #define		SSD_KEY_DATA_PROTECT	0x07
696 #define		SSD_KEY_BLANK_CHECK	0x08
697 #define		SSD_KEY_Vendor_Specific	0x09
698 #define		SSD_KEY_COPY_ABORTED	0x0a
699 #define		SSD_KEY_ABORTED_COMMAND	0x0b
700 #define		SSD_KEY_EQUAL		0x0c
701 #define		SSD_KEY_VOLUME_OVERFLOW	0x0d
702 #define		SSD_KEY_MISCOMPARE	0x0e
703 #define		SSD_KEY_RESERVED	0x0f
704 #define	SSD_ILI		0x20
705 #define	SSD_EOM		0x40
706 #define	SSD_FILEMARK	0x80
707 	u_int8_t info[4];
708 	u_int8_t extra_len;
709 	u_int8_t cmd_spec_info[4];
710 	u_int8_t add_sense_code;
711 	u_int8_t add_sense_code_qual;
712 	u_int8_t fru;
713 	u_int8_t sense_key_spec[3];
714 #define	SSD_SCS_VALID		0x80
715 #define SSD_FIELDPTR_CMD	0x40
716 #define SSD_BITPTR_VALID	0x08
717 #define SSD_BITPTR_VALUE	0x07
718 #define SSD_MIN_SIZE 18
719 	u_int8_t extra_bytes[14];
720 #define SSD_FULL_SIZE sizeof(struct scsi_sense_data)
721 };
722 
723 struct scsi_mode_header_6
724 {
725 	u_int8_t data_length;	/* Sense data length */
726 	u_int8_t medium_type;
727 	u_int8_t dev_spec;
728 	u_int8_t blk_desc_len;
729 };
730 
731 struct scsi_mode_header_10
732 {
733 	u_int8_t data_length[2];/* Sense data length */
734 	u_int8_t medium_type;
735 	u_int8_t dev_spec;
736 	u_int8_t unused[2];
737 	u_int8_t blk_desc_len[2];
738 };
739 
740 struct scsi_mode_page_header
741 {
742 	u_int8_t page_code;
743 	u_int8_t page_length;
744 };
745 
746 struct scsi_mode_blk_desc
747 {
748 	u_int8_t density;
749 	u_int8_t nblocks[3];
750 	u_int8_t reserved;
751 	u_int8_t blklen[3];
752 };
753 
754 #define	SCSI_DEFAULT_DENSITY	0x00	/* use 'default' density */
755 #define	SCSI_SAME_DENSITY	0x7f	/* use 'same' density- >= SCSI-2 only */
756 /*
757  * Status Byte
758  */
759 #define	SCSI_STATUS_OK			0x00
760 #define	SCSI_STATUS_CHECK_COND		0x02
761 #define	SCSI_STATUS_COND_MET		0x04
762 #define	SCSI_STATUS_BUSY		0x08
763 #define SCSI_STATUS_INTERMED		0x10
764 #define SCSI_STATUS_INTERMED_COND_MET	0x14
765 #define SCSI_STATUS_RESERV_CONFLICT	0x18
766 #define SCSI_STATUS_CMD_TERMINATED	0x22	/* Obsolete in SAM-2 */
767 #define SCSI_STATUS_QUEUE_FULL		0x28
768 #define SCSI_STATUS_ACA_ACTIVE		0x30
769 #define SCSI_STATUS_TASK_ABORTED	0x40
770 
771 struct scsi_inquiry_pattern {
772 	u_int8_t   type;
773 	u_int8_t   media_type;
774 #define	SIP_MEDIA_REMOVABLE	0x01
775 #define	SIP_MEDIA_FIXED		0x02
776 	const char *vendor;
777 	const char *product;
778 	const char *revision;
779 };
780 
781 struct scsi_static_inquiry_pattern {
782 	u_int8_t   type;
783 	u_int8_t   media_type;
784 	char       vendor[SID_VENDOR_SIZE+1];
785 	char       product[SID_PRODUCT_SIZE+1];
786 	char       revision[SID_REVISION_SIZE+1];
787 };
788 
789 struct scsi_sense_quirk_entry {
790 	struct scsi_inquiry_pattern	inq_pat;
791 	int				num_sense_keys;
792 	int				num_ascs;
793 	struct sense_key_table_entry	*sense_key_info;
794 	struct asc_table_entry		*asc_info;
795 };
796 
797 struct sense_key_table_entry {
798 	u_int8_t    sense_key;
799 	u_int32_t   action;
800 	const char *desc;
801 };
802 
803 struct asc_table_entry {
804 	u_int8_t    asc;
805 	u_int8_t    ascq;
806 	u_int32_t   action;
807 	const char *desc;
808 };
809 
810 struct op_table_entry {
811 	u_int8_t    opcode;
812 	u_int16_t   opmask;
813 	const char  *desc;
814 };
815 
816 struct scsi_op_quirk_entry {
817 	struct scsi_inquiry_pattern	inq_pat;
818 	int				num_ops;
819 	struct op_table_entry		*op_table;
820 };
821 
822 typedef enum {
823 	SSS_FLAG_NONE		= 0x00,
824 	SSS_FLAG_PRINT_COMMAND	= 0x01
825 } scsi_sense_string_flags;
826 
827 struct ccb_scsiio;
828 struct cam_periph;
829 union  ccb;
830 #ifndef _KERNEL
831 struct cam_device;
832 #endif
833 
834 extern const char *scsi_sense_key_text[];
835 
836 struct sbuf;
837 
838 __BEGIN_DECLS
839 void scsi_sense_desc(int sense_key, int asc, int ascq,
840 		     struct scsi_inquiry_data *inq_data,
841 		     const char **sense_key_desc, const char **asc_desc);
842 scsi_sense_action scsi_error_action(struct ccb_scsiio* csio,
843 				    struct scsi_inquiry_data *inq_data,
844 				    u_int32_t sense_flags);
845 const char *	scsi_status_string(struct ccb_scsiio *csio);
846 #ifdef _KERNEL
847 int		scsi_command_string(struct ccb_scsiio *csio, struct sbuf *sb);
848 int		scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
849 				scsi_sense_string_flags flags);
850 char *		scsi_sense_string(struct ccb_scsiio *csio,
851 				  char *str, int str_len);
852 void		scsi_sense_print(struct ccb_scsiio *csio);
853 int		scsi_interpret_sense(union ccb *ccb,
854 				     u_int32_t sense_flags,
855 				     u_int32_t *relsim_flags,
856 				     u_int32_t *reduction,
857 				     u_int32_t *timeout,
858 				     scsi_sense_action error_action);
859 #else /* _KERNEL */
860 int		scsi_command_string(struct cam_device *device,
861 				    struct ccb_scsiio *csio, struct sbuf *sb);
862 int		scsi_sense_sbuf(struct cam_device *device,
863 				struct ccb_scsiio *csio, struct sbuf *sb,
864 				scsi_sense_string_flags flags);
865 char *		scsi_sense_string(struct cam_device *device,
866 				  struct ccb_scsiio *csio,
867 				  char *str, int str_len);
868 void		scsi_sense_print(struct cam_device *device,
869 				 struct ccb_scsiio *csio, FILE *ofile);
870 int		scsi_interpret_sense(struct cam_device *device,
871 				     union ccb *ccb,
872 				     u_int32_t sense_flags,
873 				     u_int32_t *relsim_flags,
874 				     u_int32_t *reduction,
875 				     u_int32_t *timeout,
876 				     scsi_sense_action error_action);
877 #endif /* _KERNEL */
878 
879 #define	SF_RETRY_UA	0x01
880 #define SF_NO_PRINT	0x02
881 #define SF_QUIET_IR	0x04	/* Be quiet about Illegal Request reponses */
882 #define SF_PRINT_ALWAYS	0x08
883 
884 
885 const char *	scsi_op_desc(u_int16_t opcode,
886 			     struct scsi_inquiry_data *inq_data);
887 char *		scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string,
888 				size_t len);
889 
890 void		scsi_print_inquiry(struct scsi_inquiry_data *inq_data);
891 
892 u_int		scsi_calc_syncsrate(u_int period_factor);
893 u_int		scsi_calc_syncparam(u_int period);
894 
895 void		scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries,
896 				     void (*cbfcnp)(struct cam_periph *,
897 						    union ccb *),
898 				     u_int8_t tag_action,
899 				     u_int8_t sense_len, u_int32_t timeout);
900 
901 void		scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
902 				   void (*cbfcnp)(struct cam_periph *,
903 						  union ccb *),
904 				   void *data_ptr, u_int8_t dxfer_len,
905 				   u_int8_t tag_action, u_int8_t sense_len,
906 				   u_int32_t timeout);
907 
908 void		scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
909 			     void (*cbfcnp)(struct cam_periph *, union ccb *),
910 			     u_int8_t tag_action, u_int8_t *inq_buf,
911 			     u_int32_t inq_len, int evpd, u_int8_t page_code,
912 			     u_int8_t sense_len, u_int32_t timeout);
913 
914 void		scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
915 				void (*cbfcnp)(struct cam_periph *,
916 					       union ccb *),
917 				u_int8_t tag_action, int dbd,
918 				u_int8_t page_code, u_int8_t page,
919 				u_int8_t *param_buf, u_int32_t param_len,
920 				u_int8_t sense_len, u_int32_t timeout);
921 
922 void		scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
923 				 void (*cbfcnp)(struct cam_periph *,
924 						union ccb *),
925 				 u_int8_t tag_action, int scsi_page_fmt,
926 				 int save_pages, u_int8_t *param_buf,
927 				 u_int32_t param_len, u_int8_t sense_len,
928 				 u_int32_t timeout);
929 
930 void		scsi_log_sense(struct ccb_scsiio *csio, u_int32_t retries,
931 			       void (*cbfcnp)(struct cam_periph *, union ccb *),
932 			       u_int8_t tag_action, u_int8_t page_code,
933 			       u_int8_t page, int save_pages, int ppc,
934 			       u_int32_t paramptr, u_int8_t *param_buf,
935 			       u_int32_t param_len, u_int8_t sense_len,
936 			       u_int32_t timeout);
937 
938 void		scsi_log_select(struct ccb_scsiio *csio, u_int32_t retries,
939 				void (*cbfcnp)(struct cam_periph *,
940 				union ccb *), u_int8_t tag_action,
941 				u_int8_t page_code, int save_pages,
942 				int pc_reset, u_int8_t *param_buf,
943 				u_int32_t param_len, u_int8_t sense_len,
944 				u_int32_t timeout);
945 
946 void		scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
947 				   void (*cbfcnp)(struct cam_periph *,
948 				   union ccb *), u_int8_t tag_action,
949 				   struct scsi_read_capacity_data *rcap_buf,
950 				   u_int8_t sense_len, u_int32_t timeout);
951 
952 void		scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
953 			     void (*cbfcnp)(struct cam_periph *, union ccb *),
954 			     u_int8_t tag_action, u_int8_t action,
955 			     u_int8_t sense_len, u_int32_t timeout);
956 
957 void		scsi_synchronize_cache(struct ccb_scsiio *csio,
958 				       u_int32_t retries,
959 				       void (*cbfcnp)(struct cam_periph *,
960 				       union ccb *), u_int8_t tag_action,
961 				       u_int32_t begin_lba, u_int16_t lb_count,
962 				       u_int8_t sense_len, u_int32_t timeout);
963 
964 void scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
965 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
966 		     u_int8_t tag_action, int readop, u_int8_t byte2,
967 		     int minimum_cmd_size, u_int32_t lba,
968 		     u_int32_t block_count, u_int8_t *data_ptr,
969 		     u_int32_t dxfer_len, u_int8_t sense_len,
970 		     u_int32_t timeout);
971 
972 void scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
973 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
974 		     u_int8_t tag_action, int start, int load_eject,
975 		     int immediate, u_int8_t sense_len, u_int32_t timeout);
976 
977 int		scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry);
978 int		scsi_static_inquiry_match(caddr_t inqbuffer,
979 					  caddr_t table_entry);
980 
981 static __inline void scsi_extract_sense(struct scsi_sense_data *sense,
982 					int *error_code, int *sense_key,
983 					int *asc, int *ascq);
984 static __inline void scsi_ulto2b(u_int32_t val, u_int8_t *bytes);
985 static __inline void scsi_ulto3b(u_int32_t val, u_int8_t *bytes);
986 static __inline void scsi_ulto4b(u_int32_t val, u_int8_t *bytes);
987 static __inline u_int32_t scsi_2btoul(u_int8_t *bytes);
988 static __inline u_int32_t scsi_3btoul(u_int8_t *bytes);
989 static __inline int32_t scsi_3btol(u_int8_t *bytes);
990 static __inline u_int32_t scsi_4btoul(u_int8_t *bytes);
991 static __inline void *find_mode_page_6(struct scsi_mode_header_6 *mode_header);
992 static __inline void *find_mode_page_10(struct scsi_mode_header_10 *mode_header);
993 
994 static __inline void scsi_extract_sense(struct scsi_sense_data *sense,
995 					int *error_code, int *sense_key,
996 					int *asc, int *ascq)
997 {
998 	*error_code = sense->error_code & SSD_ERRCODE;
999 	*sense_key = sense->flags & SSD_KEY;
1000 	*asc = (sense->extra_len >= 5) ? sense->add_sense_code : 0;
1001 	*ascq = (sense->extra_len >= 6) ? sense->add_sense_code_qual : 0;
1002 }
1003 
1004 static __inline void
1005 scsi_ulto2b(u_int32_t val, u_int8_t *bytes)
1006 {
1007 
1008 	bytes[0] = (val >> 8) & 0xff;
1009 	bytes[1] = val & 0xff;
1010 }
1011 
1012 static __inline void
1013 scsi_ulto3b(u_int32_t val, u_int8_t *bytes)
1014 {
1015 
1016 	bytes[0] = (val >> 16) & 0xff;
1017 	bytes[1] = (val >> 8) & 0xff;
1018 	bytes[2] = val & 0xff;
1019 }
1020 
1021 static __inline void
1022 scsi_ulto4b(u_int32_t val, u_int8_t *bytes)
1023 {
1024 
1025 	bytes[0] = (val >> 24) & 0xff;
1026 	bytes[1] = (val >> 16) & 0xff;
1027 	bytes[2] = (val >> 8) & 0xff;
1028 	bytes[3] = val & 0xff;
1029 }
1030 
1031 static __inline u_int32_t
1032 scsi_2btoul(u_int8_t *bytes)
1033 {
1034 	u_int32_t rv;
1035 
1036 	rv = (bytes[0] << 8) |
1037 	     bytes[1];
1038 	return (rv);
1039 }
1040 
1041 static __inline u_int32_t
1042 scsi_3btoul(u_int8_t *bytes)
1043 {
1044 	u_int32_t rv;
1045 
1046 	rv = (bytes[0] << 16) |
1047 	     (bytes[1] << 8) |
1048 	     bytes[2];
1049 	return (rv);
1050 }
1051 
1052 static __inline int32_t
1053 scsi_3btol(u_int8_t *bytes)
1054 {
1055 	u_int32_t rc = scsi_3btoul(bytes);
1056 
1057 	if (rc & 0x00800000)
1058 		rc |= 0xff000000;
1059 
1060 	return (int32_t) rc;
1061 }
1062 
1063 static __inline u_int32_t
1064 scsi_4btoul(u_int8_t *bytes)
1065 {
1066 	u_int32_t rv;
1067 
1068 	rv = (bytes[0] << 24) |
1069 	     (bytes[1] << 16) |
1070 	     (bytes[2] << 8) |
1071 	     bytes[3];
1072 	return (rv);
1073 }
1074 
1075 /*
1076  * Given the pointer to a returned mode sense buffer, return a pointer to
1077  * the start of the first mode page.
1078  */
1079 static __inline void *
1080 find_mode_page_6(struct scsi_mode_header_6 *mode_header)
1081 {
1082 	void *page_start;
1083 
1084 	page_start = (void *)((u_int8_t *)&mode_header[1] +
1085 			      mode_header->blk_desc_len);
1086 
1087 	return(page_start);
1088 }
1089 
1090 static __inline void *
1091 find_mode_page_10(struct scsi_mode_header_10 *mode_header)
1092 {
1093 	void *page_start;
1094 
1095 	page_start = (void *)((u_int8_t *)&mode_header[1] +
1096 			       scsi_2btoul(mode_header->blk_desc_len));
1097 
1098 	return(page_start);
1099 }
1100 
1101 __END_DECLS
1102 
1103 #endif /*_SCSI_SCSI_ALL_H*/
1104