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