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