xref: /freebsd/sys/cam/scsi/scsi_all.h (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
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_control_page {
264 	u_int8_t page_code;
265 	u_int8_t page_length;
266 	u_int8_t rlec;
267 #define SCB_RLEC			0x01	/*Report Log Exception Cond*/
268 	u_int8_t queue_flags;
269 #define SCP_QUEUE_ALG_MASK		0xF0
270 #define SCP_QUEUE_ALG_RESTRICTED	0x00
271 #define SCP_QUEUE_ALG_UNRESTRICTED	0x10
272 #define SCP_QUEUE_ERR			0x02	/*Queued I/O aborted for CACs*/
273 #define SCP_QUEUE_DQUE			0x01	/*Queued I/O disabled*/
274 	u_int8_t eca_and_aen;
275 #define SCP_EECA			0x80	/*Enable Extended CA*/
276 #define SCP_RAENP			0x04	/*Ready AEN Permission*/
277 #define SCP_UAAENP			0x02	/*UA AEN Permission*/
278 #define SCP_EAENP			0x01	/*Error AEN Permission*/
279 	u_int8_t reserved;
280 	u_int8_t aen_holdoff_period[2];
281 };
282 
283 struct scsi_reserve
284 {
285 	u_int8_t opcode;
286 	u_int8_t byte2;
287 	u_int8_t unused[2];
288 	u_int8_t length;
289 	u_int8_t control;
290 };
291 
292 struct scsi_release
293 {
294 	u_int8_t opcode;
295 	u_int8_t byte2;
296 	u_int8_t unused[2];
297 	u_int8_t length;
298 	u_int8_t control;
299 };
300 
301 struct scsi_prevent
302 {
303 	u_int8_t opcode;
304 	u_int8_t byte2;
305 	u_int8_t unused[2];
306 	u_int8_t how;
307 	u_int8_t control;
308 };
309 #define	PR_PREVENT 0x01
310 #define PR_ALLOW   0x00
311 
312 struct scsi_sync_cache
313 {
314 	u_int8_t opcode;
315 	u_int8_t byte2;
316 	u_int8_t begin_lba[4];
317 	u_int8_t reserved;
318 	u_int8_t lb_count[2];
319 	u_int8_t control;
320 };
321 
322 
323 struct scsi_changedef
324 {
325 	u_int8_t opcode;
326 	u_int8_t byte2;
327 	u_int8_t unused1;
328 	u_int8_t how;
329 	u_int8_t unused[4];
330 	u_int8_t datalen;
331 	u_int8_t control;
332 };
333 
334 struct scsi_read_buffer
335 {
336 	u_int8_t opcode;
337 	u_int8_t byte2;
338 #define	RWB_MODE		0x07
339 #define	RWB_MODE_HDR_DATA	0x00
340 #define	RWB_MODE_DATA		0x02
341 #define	RWB_MODE_DOWNLOAD	0x04
342 #define	RWB_MODE_DOWNLOAD_SAVE	0x05
343         u_int8_t buffer_id;
344         u_int8_t offset[3];
345         u_int8_t length[3];
346         u_int8_t control;
347 };
348 
349 struct scsi_write_buffer
350 {
351 	u_int8_t opcode;
352 	u_int8_t byte2;
353 	u_int8_t buffer_id;
354 	u_int8_t offset[3];
355 	u_int8_t length[3];
356 	u_int8_t control;
357 };
358 
359 struct scsi_rw_6
360 {
361 	u_int8_t opcode;
362 	u_int8_t addr[3];
363 /* only 5 bits are valid in the MSB address byte */
364 #define	SRW_TOPADDR	0x1F
365 	u_int8_t length;
366 	u_int8_t control;
367 };
368 
369 struct scsi_rw_10
370 {
371 	u_int8_t opcode;
372 #define	SRW10_RELADDR	0x01
373 #define SRW10_FUA	0x08
374 #define	SRW10_DPO	0x10
375 	u_int8_t byte2;
376 	u_int8_t addr[4];
377 	u_int8_t reserved;
378 	u_int8_t length[2];
379 	u_int8_t control;
380 };
381 
382 struct scsi_rw_12
383 {
384 	u_int8_t opcode;
385 #define	SRW12_RELADDR	0x01
386 #define SRW12_FUA	0x08
387 #define	SRW12_DPO	0x10
388 	u_int8_t byte2;
389 	u_int8_t addr[4];
390 	u_int8_t reserved;
391 	u_int8_t length[4];
392 	u_int8_t control;
393 };
394 
395 struct scsi_start_stop_unit
396 {
397 	u_int8_t opcode;
398 	u_int8_t byte2;
399 #define	SSS_IMMED		0x01
400 	u_int8_t reserved[2];
401 	u_int8_t how;
402 #define	SSS_START		0x01
403 #define	SSS_LOEJ		0x02
404 	u_int8_t control;
405 };
406 
407 #define SC_SCSI_1 0x01
408 #define SC_SCSI_2 0x03
409 
410 /*
411  * Opcodes
412  */
413 
414 #define	TEST_UNIT_READY		0x00
415 #define REQUEST_SENSE		0x03
416 #define	READ_6			0x08
417 #define WRITE_6			0x0a
418 #define INQUIRY			0x12
419 #define MODE_SELECT_6		0x15
420 #define MODE_SENSE_6		0x1a
421 #define START_STOP_UNIT		0x1b
422 #define START_STOP		0x1b
423 #define RESERVE      		0x16
424 #define RELEASE      		0x17
425 #define	RECEIVE_DIAGNOSTIC	0x1c
426 #define	SEND_DIAGNOSTIC		0x1d
427 #define PREVENT_ALLOW		0x1e
428 #define	READ_CAPACITY		0x25
429 #define	READ_10			0x28
430 #define WRITE_10		0x2a
431 #define POSITION_TO_ELEMENT	0x2b
432 #define	SYNCHRONIZE_CACHE	0x35
433 #define	WRITE_BUFFER            0x3b
434 #define	READ_BUFFER             0x3c
435 #define	CHANGE_DEFINITION	0x40
436 #define	MODE_SELECT_10		0x55
437 #define	MODE_SENSE_10		0x5A
438 #define MOVE_MEDIUM     	0xa5
439 #define READ_12			0xa8
440 #define WRITE_12		0xaa
441 #define READ_ELEMENT_STATUS	0xb8
442 
443 
444 /*
445  * Device Types
446  */
447 #define T_DIRECT	0x00
448 #define T_SEQUENTIAL	0x01
449 #define T_PRINTER	0x02
450 #define T_PROCESSOR	0x03
451 #define T_WORM		0x04
452 #define T_CDROM		0x05
453 #define T_SCANNER 	0x06
454 #define T_OPTICAL 	0x07
455 #define T_CHANGER	0x08
456 #define T_COMM		0x09
457 #define T_ASC0		0x0a
458 #define T_ASC1		0x0b
459 #define	T_STORARRAY	0x0c
460 #define	T_ENCLOSURE	0x0d
461 #define	T_RBC		0x0e
462 #define	T_OCRW		0x0f
463 #define T_NODEVICE	0x1F
464 #define	T_ANY		0xFF	/* Used in Quirk table matches */
465 
466 #define T_REMOV		1
467 #define	T_FIXED		0
468 
469 /*
470  * This length is the initial inquiry length used by the probe code, as
471  * well as the legnth necessary for scsi_print_inquiry() to function
472  * correctly.  If either use requires a different length in the future,
473  * the two values should be de-coupled.
474  */
475 #define	SHORT_INQUIRY_LENGTH	36
476 
477 struct scsi_inquiry_data
478 {
479 	u_int8_t device;
480 #define	SID_TYPE(inq_data) ((inq_data)->device & 0x1f)
481 #define	SID_QUAL(inq_data) (((inq_data)->device & 0xE0) >> 5)
482 #define	SID_QUAL_LU_CONNECTED	0x00	/*
483 					 * The specified peripheral device
484 					 * type is currently connected to
485 					 * logical unit.  If the target cannot
486 					 * determine whether or not a physical
487 					 * device is currently connected, it
488 					 * shall also use this peripheral
489 					 * qualifier when returning the INQUIRY
490 					 * data.  This peripheral qualifier
491 					 * does not mean that the device is
492 					 * ready for access by the initiator.
493 					 */
494 #define	SID_QUAL_LU_OFFLINE	0x01	/*
495 					 * The target is capable of supporting
496 					 * the specified peripheral device type
497 					 * on this logical unit; however, the
498 					 * physical device is not currently
499 					 * connected to this logical unit.
500 					 */
501 #define SID_QUAL_RSVD		0x02
502 #define	SID_QUAL_BAD_LU		0x03	/*
503 					 * The target is not capable of
504 					 * supporting a physical device on
505 					 * this logical unit. For this
506 					 * peripheral qualifier the peripheral
507 					 * device type shall be set to 1Fh to
508 					 * provide compatibility with previous
509 					 * versions of SCSI. All other
510 					 * peripheral device type values are
511 					 * reserved for this peripheral
512 					 * qualifier.
513 					 */
514 #define	SID_QUAL_IS_VENDOR_UNIQUE(inq_data) ((SID_QUAL(inq_data) & 0x08) != 0)
515 	u_int8_t dev_qual2;
516 #define	SID_QUAL2	0x7F
517 #define	SID_IS_REMOVABLE(inq_data) (((inq_data)->dev_qual2 & 0x80) != 0)
518 	u_int8_t version;
519 #define SID_ANSI_REV(inq_data) ((inq_data)->version & 0x07)
520 #define		SCSI_REV_0		0
521 #define		SCSI_REV_CCS		1
522 #define		SCSI_REV_2		2
523 #define		SCSI_REV_SPC		3
524 #define		SCSI_REV_SPC2		4
525 
526 #define SID_ECMA	0x38
527 #define SID_ISO		0xC0
528 	u_int8_t response_format;
529 #define SID_AENC	0x80
530 #define SID_TrmIOP	0x40
531 	u_int8_t additional_length;
532 	u_int8_t reserved[2];
533 	u_int8_t flags;
534 #define	SID_SftRe	0x01
535 #define	SID_CmdQue	0x02
536 #define	SID_Linked	0x08
537 #define	SID_Sync	0x10
538 #define	SID_WBus16	0x20
539 #define	SID_WBus32	0x40
540 #define	SID_RelAdr	0x80
541 #define SID_VENDOR_SIZE   8
542 	char	 vendor[SID_VENDOR_SIZE];
543 #define SID_PRODUCT_SIZE  16
544 	char	 product[SID_PRODUCT_SIZE];
545 #define SID_REVISION_SIZE 4
546 	char	 revision[SID_REVISION_SIZE];
547 	/*
548 	 * The following fields were taken from SCSI Primary Commands - 2
549 	 * (SPC-2) Revision 14, Dated 11 November 1999
550 	 */
551 #define	SID_VENDOR_SPECIFIC_0_SIZE	20
552 	u_int8_t vendor_specific0[SID_VENDOR_SPECIFIC_0_SIZE];
553 	/*
554 	 * An extension of SCSI Parallel Specific Values
555 	 */
556 #define	SID_SPI_IUS		0x01
557 #define	SID_SPI_QAS		0x02
558 #define	SID_SPI_CLOCK_ST	0x00
559 #define	SID_SPI_CLOCK_DT	0x04
560 #define	SID_SPI_CLOCK_DT_ST	0x0C
561 #define	SID_SPI_MASK		0x0F
562 	u_int8_t spi3data;
563 	u_int8_t reserved2;
564 	/*
565 	 * Version Descriptors, stored 2 byte values.
566 	 */
567 	u_int8_t version1[2];
568 	u_int8_t version2[2];
569 	u_int8_t version3[2];
570 	u_int8_t version4[2];
571 	u_int8_t version5[2];
572 	u_int8_t version6[2];
573 	u_int8_t version7[2];
574 	u_int8_t version8[2];
575 
576 	u_int8_t reserved3[22];
577 
578 #define	SID_VENDOR_SPECIFIC_1_SIZE	160
579 	u_int8_t vendor_specific1[SID_VENDOR_SPECIFIC_1_SIZE];
580 };
581 
582 struct scsi_vpd_unit_serial_number
583 {
584 	u_int8_t device;
585 	u_int8_t page_code;
586 #define SVPD_UNIT_SERIAL_NUMBER	0x80
587 	u_int8_t reserved;
588 	u_int8_t length; /* serial number length */
589 #define SVPD_SERIAL_NUM_SIZE 251
590 	u_int8_t serial_num[SVPD_SERIAL_NUM_SIZE];
591 };
592 
593 struct scsi_read_capacity
594 {
595 	u_int8_t opcode;
596 	u_int8_t byte2;
597 	u_int8_t addr[4];
598 	u_int8_t unused[3];
599 	u_int8_t control;
600 };
601 
602 struct scsi_read_capacity_data
603 {
604 	u_int8_t addr[4];
605 	u_int8_t length[4];
606 };
607 
608 struct scsi_sense_data
609 {
610 	u_int8_t error_code;
611 #define	SSD_ERRCODE			0x7F
612 #define		SSD_CURRENT_ERROR	0x70
613 #define		SSD_DEFERRED_ERROR	0x71
614 #define	SSD_ERRCODE_VALID	0x80
615 	u_int8_t segment;
616 	u_int8_t flags;
617 #define	SSD_KEY				0x0F
618 #define		SSD_KEY_NO_SENSE	0x00
619 #define		SSD_KEY_RECOVERED_ERROR	0x01
620 #define		SSD_KEY_NOT_READY	0x02
621 #define		SSD_KEY_MEDIUM_ERROR	0x03
622 #define		SSD_KEY_HARDWARE_ERROR	0x04
623 #define		SSD_KEY_ILLEGAL_REQUEST	0x05
624 #define		SSD_KEY_UNIT_ATTENTION	0x06
625 #define		SSD_KEY_DATA_PROTECT	0x07
626 #define		SSD_KEY_BLANK_CHECK	0x08
627 #define		SSD_KEY_Vendor_Specific	0x09
628 #define		SSD_KEY_COPY_ABORTED	0x0a
629 #define		SSD_KEY_ABORTED_COMMAND	0x0b
630 #define		SSD_KEY_EQUAL		0x0c
631 #define		SSD_KEY_VOLUME_OVERFLOW	0x0d
632 #define		SSD_KEY_MISCOMPARE	0x0e
633 #define		SSD_KEY_RESERVED	0x0f
634 #define	SSD_ILI		0x20
635 #define	SSD_EOM		0x40
636 #define	SSD_FILEMARK	0x80
637 	u_int8_t info[4];
638 	u_int8_t extra_len;
639 	u_int8_t cmd_spec_info[4];
640 	u_int8_t add_sense_code;
641 	u_int8_t add_sense_code_qual;
642 	u_int8_t fru;
643 	u_int8_t sense_key_spec[3];
644 #define	SSD_SCS_VALID		0x80
645 #define SSD_FIELDPTR_CMD	0x40
646 #define SSD_BITPTR_VALID	0x08
647 #define SSD_BITPTR_VALUE	0x07
648 #define SSD_MIN_SIZE 18
649 	u_int8_t extra_bytes[14];
650 #define SSD_FULL_SIZE sizeof(struct scsi_sense_data)
651 };
652 
653 struct scsi_mode_header_6
654 {
655 	u_int8_t data_length;	/* Sense data length */
656 	u_int8_t medium_type;
657 	u_int8_t dev_spec;
658 	u_int8_t blk_desc_len;
659 };
660 
661 struct scsi_mode_header_10
662 {
663 	u_int8_t data_length[2];/* Sense data length */
664 	u_int8_t medium_type;
665 	u_int8_t dev_spec;
666 	u_int8_t unused[2];
667 	u_int8_t blk_desc_len[2];
668 };
669 
670 struct scsi_mode_page_header
671 {
672 	u_int8_t page_code;
673 	u_int8_t page_length;
674 };
675 
676 struct scsi_mode_blk_desc
677 {
678 	u_int8_t density;
679 	u_int8_t nblocks[3];
680 	u_int8_t reserved;
681 	u_int8_t blklen[3];
682 };
683 
684 #define	SCSI_DEFAULT_DENSITY	0x00	/* use 'default' density */
685 #define	SCSI_SAME_DENSITY	0x7f	/* use 'same' density- >= SCSI-2 only */
686 /*
687  * Status Byte
688  */
689 #define	SCSI_STATUS_OK			0x00
690 #define	SCSI_STATUS_CHECK_COND		0x02
691 #define	SCSI_STATUS_COND_MET		0x04
692 #define	SCSI_STATUS_BUSY		0x08
693 #define SCSI_STATUS_INTERMED		0x10
694 #define SCSI_STATUS_INTERMED_COND_MET	0x14
695 #define SCSI_STATUS_RESERV_CONFLICT	0x18
696 #define SCSI_STATUS_CMD_TERMINATED	0x22	/* Obsolete in SAM-2 */
697 #define SCSI_STATUS_QUEUE_FULL		0x28
698 #define SCSI_STATUS_ACA_ACTIVE		0x30
699 #define SCSI_STATUS_TASK_ABORTED	0x40
700 
701 struct scsi_inquiry_pattern {
702 	u_int8_t   type;
703 	u_int8_t   media_type;
704 #define	SIP_MEDIA_REMOVABLE	0x01
705 #define	SIP_MEDIA_FIXED		0x02
706 	const char *vendor;
707 	const char *product;
708 	const char *revision;
709 };
710 
711 struct scsi_static_inquiry_pattern {
712 	u_int8_t   type;
713 	u_int8_t   media_type;
714 	char       vendor[SID_VENDOR_SIZE+1];
715 	char       product[SID_PRODUCT_SIZE+1];
716 	char       revision[SID_REVISION_SIZE+1];
717 };
718 
719 struct scsi_sense_quirk_entry {
720 	struct scsi_inquiry_pattern	inq_pat;
721 	int				num_sense_keys;
722 	int				num_ascs;
723 	struct sense_key_table_entry	*sense_key_info;
724 	struct asc_table_entry		*asc_info;
725 };
726 
727 struct sense_key_table_entry {
728 	u_int8_t    sense_key;
729 	u_int32_t   action;
730 	const char *desc;
731 };
732 
733 struct asc_table_entry {
734 	u_int8_t    asc;
735 	u_int8_t    ascq;
736 	u_int32_t   action;
737 	const char *desc;
738 };
739 
740 struct op_table_entry {
741 	u_int8_t    opcode;
742 	u_int16_t   opmask;
743 	const char  *desc;
744 };
745 
746 struct scsi_op_quirk_entry {
747 	struct scsi_inquiry_pattern	inq_pat;
748 	int				num_ops;
749 	struct op_table_entry		*op_table;
750 };
751 
752 typedef enum {
753 	SSS_FLAG_NONE		= 0x00,
754 	SSS_FLAG_PRINT_COMMAND	= 0x01
755 } scsi_sense_string_flags;
756 
757 struct ccb_scsiio;
758 struct cam_periph;
759 union  ccb;
760 #ifndef _KERNEL
761 struct cam_device;
762 #endif
763 
764 extern const char *scsi_sense_key_text[];
765 
766 struct sbuf;
767 
768 __BEGIN_DECLS
769 void scsi_sense_desc(int sense_key, int asc, int ascq,
770 		     struct scsi_inquiry_data *inq_data,
771 		     const char **sense_key_desc, const char **asc_desc);
772 scsi_sense_action scsi_error_action(struct ccb_scsiio* csio,
773 				    struct scsi_inquiry_data *inq_data,
774 				    u_int32_t sense_flags);
775 const char *	scsi_status_string(struct ccb_scsiio *csio);
776 #ifdef _KERNEL
777 int		scsi_command_string(struct ccb_scsiio *csio, struct sbuf *sb);
778 int		scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
779 				scsi_sense_string_flags flags);
780 char *		scsi_sense_string(struct ccb_scsiio *csio,
781 				  char *str, int str_len);
782 void		scsi_sense_print(struct ccb_scsiio *csio);
783 int		scsi_interpret_sense(union ccb *ccb,
784 				     u_int32_t sense_flags,
785 				     u_int32_t *relsim_flags,
786 				     u_int32_t *reduction,
787 				     u_int32_t *timeout,
788 				     scsi_sense_action error_action);
789 #else /* _KERNEL */
790 int		scsi_command_string(struct cam_device *device,
791 				    struct ccb_scsiio *csio, struct sbuf *sb);
792 int		scsi_sense_sbuf(struct cam_device *device,
793 				struct ccb_scsiio *csio, struct sbuf *sb,
794 				scsi_sense_string_flags flags);
795 char *		scsi_sense_string(struct cam_device *device,
796 				  struct ccb_scsiio *csio,
797 				  char *str, int str_len);
798 void		scsi_sense_print(struct cam_device *device,
799 				 struct ccb_scsiio *csio, FILE *ofile);
800 int		scsi_interpret_sense(struct cam_device *device,
801 				     union ccb *ccb,
802 				     u_int32_t sense_flags,
803 				     u_int32_t *relsim_flags,
804 				     u_int32_t *reduction,
805 				     u_int32_t *timeout,
806 				     scsi_sense_action error_action);
807 #endif /* _KERNEL */
808 
809 #define	SF_RETRY_UA	0x01
810 #define SF_NO_PRINT	0x02
811 #define SF_QUIET_IR	0x04	/* Be quiet about Illegal Request reponses */
812 #define SF_PRINT_ALWAYS	0x08
813 
814 
815 const char *	scsi_op_desc(u_int16_t opcode,
816 			     struct scsi_inquiry_data *inq_data);
817 char *		scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string,
818 				size_t len);
819 
820 void		scsi_print_inquiry(struct scsi_inquiry_data *inq_data);
821 
822 u_int		scsi_calc_syncsrate(u_int period_factor);
823 u_int		scsi_calc_syncparam(u_int period);
824 
825 void		scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries,
826 				     void (*cbfcnp)(struct cam_periph *,
827 						    union ccb *),
828 				     u_int8_t tag_action,
829 				     u_int8_t sense_len, u_int32_t timeout);
830 
831 void		scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
832 				   void (*cbfcnp)(struct cam_periph *,
833 						  union ccb *),
834 				   void *data_ptr, u_int8_t dxfer_len,
835 				   u_int8_t tag_action, u_int8_t sense_len,
836 				   u_int32_t timeout);
837 
838 void		scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
839 			     void (*cbfcnp)(struct cam_periph *, union ccb *),
840 			     u_int8_t tag_action, u_int8_t *inq_buf,
841 			     u_int32_t inq_len, int evpd, u_int8_t page_code,
842 			     u_int8_t sense_len, u_int32_t timeout);
843 
844 void		scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
845 				void (*cbfcnp)(struct cam_periph *,
846 					       union ccb *),
847 				u_int8_t tag_action, int dbd,
848 				u_int8_t page_code, u_int8_t page,
849 				u_int8_t *param_buf, u_int32_t param_len,
850 				u_int8_t sense_len, u_int32_t timeout);
851 
852 void		scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
853 				 void (*cbfcnp)(struct cam_periph *,
854 						union ccb *),
855 				 u_int8_t tag_action, int scsi_page_fmt,
856 				 int save_pages, u_int8_t *param_buf,
857 				 u_int32_t param_len, u_int8_t sense_len,
858 				 u_int32_t timeout);
859 
860 void		scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
861 				   void (*cbfcnp)(struct cam_periph *,
862 				   union ccb *), u_int8_t tag_action,
863 				   struct scsi_read_capacity_data *rcap_buf,
864 				   u_int8_t sense_len, u_int32_t timeout);
865 
866 void		scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
867 			     void (*cbfcnp)(struct cam_periph *, union ccb *),
868 			     u_int8_t tag_action, u_int8_t action,
869 			     u_int8_t sense_len, u_int32_t timeout);
870 
871 void		scsi_synchronize_cache(struct ccb_scsiio *csio,
872 				       u_int32_t retries,
873 				       void (*cbfcnp)(struct cam_periph *,
874 				       union ccb *), u_int8_t tag_action,
875 				       u_int32_t begin_lba, u_int16_t lb_count,
876 				       u_int8_t sense_len, u_int32_t timeout);
877 
878 void scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
879 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
880 		     u_int8_t tag_action, int readop, u_int8_t byte2,
881 		     int minimum_cmd_size, u_int32_t lba,
882 		     u_int32_t block_count, u_int8_t *data_ptr,
883 		     u_int32_t dxfer_len, u_int8_t sense_len,
884 		     u_int32_t timeout);
885 
886 void scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
887 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
888 		     u_int8_t tag_action, int start, int load_eject,
889 		     int immediate, u_int8_t sense_len, u_int32_t timeout);
890 
891 int		scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry);
892 int		scsi_static_inquiry_match(caddr_t inqbuffer,
893 					  caddr_t table_entry);
894 
895 static __inline void scsi_extract_sense(struct scsi_sense_data *sense,
896 					int *error_code, int *sense_key,
897 					int *asc, int *ascq);
898 static __inline void scsi_ulto2b(u_int32_t val, u_int8_t *bytes);
899 static __inline void scsi_ulto3b(u_int32_t val, u_int8_t *bytes);
900 static __inline void scsi_ulto4b(u_int32_t val, u_int8_t *bytes);
901 static __inline u_int32_t scsi_2btoul(u_int8_t *bytes);
902 static __inline u_int32_t scsi_3btoul(u_int8_t *bytes);
903 static __inline int32_t scsi_3btol(u_int8_t *bytes);
904 static __inline u_int32_t scsi_4btoul(u_int8_t *bytes);
905 static __inline void *find_mode_page_6(struct scsi_mode_header_6 *mode_header);
906 static __inline void *find_mode_page_10(struct scsi_mode_header_10 *mode_header);
907 
908 static __inline void scsi_extract_sense(struct scsi_sense_data *sense,
909 					int *error_code, int *sense_key,
910 					int *asc, int *ascq)
911 {
912 	*error_code = sense->error_code & SSD_ERRCODE;
913 	*sense_key = sense->flags & SSD_KEY;
914 	*asc = (sense->extra_len >= 5) ? sense->add_sense_code : 0;
915 	*ascq = (sense->extra_len >= 6) ? sense->add_sense_code_qual : 0;
916 }
917 
918 static __inline void
919 scsi_ulto2b(u_int32_t val, u_int8_t *bytes)
920 {
921 
922 	bytes[0] = (val >> 8) & 0xff;
923 	bytes[1] = val & 0xff;
924 }
925 
926 static __inline void
927 scsi_ulto3b(u_int32_t val, u_int8_t *bytes)
928 {
929 
930 	bytes[0] = (val >> 16) & 0xff;
931 	bytes[1] = (val >> 8) & 0xff;
932 	bytes[2] = val & 0xff;
933 }
934 
935 static __inline void
936 scsi_ulto4b(u_int32_t val, u_int8_t *bytes)
937 {
938 
939 	bytes[0] = (val >> 24) & 0xff;
940 	bytes[1] = (val >> 16) & 0xff;
941 	bytes[2] = (val >> 8) & 0xff;
942 	bytes[3] = val & 0xff;
943 }
944 
945 static __inline u_int32_t
946 scsi_2btoul(u_int8_t *bytes)
947 {
948 	u_int32_t rv;
949 
950 	rv = (bytes[0] << 8) |
951 	     bytes[1];
952 	return (rv);
953 }
954 
955 static __inline u_int32_t
956 scsi_3btoul(u_int8_t *bytes)
957 {
958 	u_int32_t rv;
959 
960 	rv = (bytes[0] << 16) |
961 	     (bytes[1] << 8) |
962 	     bytes[2];
963 	return (rv);
964 }
965 
966 static __inline int32_t
967 scsi_3btol(u_int8_t *bytes)
968 {
969 	u_int32_t rc = scsi_3btoul(bytes);
970 
971 	if (rc & 0x00800000)
972 		rc |= 0xff000000;
973 
974 	return (int32_t) rc;
975 }
976 
977 static __inline u_int32_t
978 scsi_4btoul(u_int8_t *bytes)
979 {
980 	u_int32_t rv;
981 
982 	rv = (bytes[0] << 24) |
983 	     (bytes[1] << 16) |
984 	     (bytes[2] << 8) |
985 	     bytes[3];
986 	return (rv);
987 }
988 
989 /*
990  * Given the pointer to a returned mode sense buffer, return a pointer to
991  * the start of the first mode page.
992  */
993 static __inline void *
994 find_mode_page_6(struct scsi_mode_header_6 *mode_header)
995 {
996 	void *page_start;
997 
998 	page_start = (void *)((u_int8_t *)&mode_header[1] +
999 			      mode_header->blk_desc_len);
1000 
1001 	return(page_start);
1002 }
1003 
1004 static __inline void *
1005 find_mode_page_10(struct scsi_mode_header_10 *mode_header)
1006 {
1007 	void *page_start;
1008 
1009 	page_start = (void *)((u_int8_t *)&mode_header[1] +
1010 			       scsi_2btoul(mode_header->blk_desc_len));
1011 
1012 	return(page_start);
1013 }
1014 
1015 __END_DECLS
1016 
1017 #endif /*_SCSI_SCSI_ALL_H*/
1018