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