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.21 2002/10/08 17:12:44 ken Exp $
18  *
19  * Copyright (c) 2003 Adaptec Inc.
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the above copyright
26  *    notice, this list of conditions, and the following disclaimer,
27  *    without modification.
28  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
29  *    substantially similar to the "NO WARRANTY" disclaimer below
30  *    ("Disclaimer") and any redistribution must be conditioned upon
31  *    including a substantially similar Disclaimer requirement for further
32  *    binary redistribution.
33  * 3. Neither the names of the above-listed copyright holders nor the names
34  *    of any contributors may be used to endorse or promote products derived
35  *    from this software without specific prior written permission.
36  *
37  * Alternatively, this software may be distributed under the terms of the
38  * GNU General Public License ("GPL") version 2 as published by the Free
39  * Software Foundation.
40  *
41  * NO WARRANTY
42  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
45  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
51  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52  * POSSIBILITY OF SUCH DAMAGES.
53  *
54  * $Id$
55  */
56 
57 #ifndef	_AICLIB_H
58 #define _AICLIB_H
59 
60 /*
61  * Linux Interrupt Support.
62  */
63 #ifndef IRQ_RETVAL
64 typedef void irqreturn_t;
65 #define	IRQ_RETVAL(x)
66 #endif
67 
68 /*
69  * SCSI command format
70  */
71 
72 /*
73  * Define dome bits that are in ALL (or a lot of) scsi commands
74  */
75 #define SCSI_CTL_LINK		0x01
76 #define SCSI_CTL_FLAG		0x02
77 #define SCSI_CTL_VENDOR		0xC0
78 #define	SCSI_CMD_LUN		0xA0	/* these two should not be needed */
79 #define	SCSI_CMD_LUN_SHIFT	5	/* LUN in the cmd is no longer SCSI */
80 
81 #define SCSI_MAX_CDBLEN		16	/*
82 					 * 16 byte commands are in the
83 					 * SCSI-3 spec
84 					 */
85 /* 6byte CDBs special case 0 length to be 256 */
86 #define SCSI_CDB6_LEN(len)	((len) == 0 ? 256 : len)
87 
88 /*
89  * This type defines actions to be taken when a particular sense code is
90  * received.  Right now, these flags are only defined to take up 16 bits,
91  * but can be expanded in the future if necessary.
92  */
93 typedef enum {
94 	SS_NOP		= 0x000000, /* Do nothing */
95 	SS_RETRY	= 0x010000, /* Retry the command */
96 	SS_FAIL		= 0x020000, /* Bail out */
97 	SS_START	= 0x030000, /* Send a Start Unit command to the device,
98 				     * then retry the original command.
99 				     */
100 	SS_TUR		= 0x040000, /* Send a Test Unit Ready command to the
101 				     * device, then retry the original command.
102 				     */
103 	SS_REQSENSE	= 0x050000, /* Send a RequestSense command to the
104 				     * device, then retry the original command.
105 				     */
106 	SS_INQ_REFRESH	= 0x060000,
107 	SS_MASK		= 0xff0000
108 } aic_sense_action;
109 
110 typedef enum {
111 	SSQ_NONE		= 0x0000,
112 	SSQ_DECREMENT_COUNT	= 0x0100,  /* Decrement the retry count */
113 	SSQ_MANY		= 0x0200,  /* send lots of recovery commands */
114 	SSQ_RANGE		= 0x0400,  /*
115 					    * This table entry represents the
116 					    * end of a range of ASCQs that
117 					    * have identical error actions
118 					    * and text.
119 					    */
120 	SSQ_PRINT_SENSE		= 0x0800,
121 	SSQ_DELAY		= 0x1000,  /* Delay before retry. */
122 	SSQ_DELAY_RANDOM	= 0x2000,  /* Randomized delay before retry. */
123 	SSQ_FALLBACK		= 0x4000,  /* Do a speed fallback to recover */
124 	SSQ_MASK		= 0xff00
125 } aic_sense_action_qualifier;
126 
127 /* Mask for error status values */
128 #define SS_ERRMASK	0xff
129 
130 /* The default, retyable, error action */
131 #define SS_RDEF		SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE|EIO
132 
133 /* The retyable, error action, with table specified error code */
134 #define SS_RET		SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE
135 
136 /* Fatal error action, with table specified error code */
137 #define SS_FATAL	SS_FAIL|SSQ_PRINT_SENSE
138 
139 struct scsi_generic
140 {
141 	uint8_t opcode;
142 	uint8_t bytes[11];
143 };
144 
145 struct scsi_request_sense
146 {
147 	uint8_t opcode;
148 	uint8_t byte2;
149 	uint8_t unused[2];
150 	uint8_t length;
151 	uint8_t control;
152 };
153 
154 struct scsi_test_unit_ready
155 {
156 	uint8_t opcode;
157 	uint8_t byte2;
158 	uint8_t unused[3];
159 	uint8_t control;
160 };
161 
162 struct scsi_send_diag
163 {
164 	uint8_t opcode;
165 	uint8_t byte2;
166 #define	SSD_UOL		0x01
167 #define	SSD_DOL		0x02
168 #define	SSD_SELFTEST	0x04
169 #define	SSD_PF		0x10
170 	uint8_t unused[1];
171 	uint8_t paramlen[2];
172 	uint8_t control;
173 };
174 
175 struct scsi_sense
176 {
177 	uint8_t opcode;
178 	uint8_t byte2;
179 	uint8_t unused[2];
180 	uint8_t length;
181 	uint8_t control;
182 };
183 
184 struct scsi_inquiry
185 {
186 	uint8_t opcode;
187 	uint8_t byte2;
188 #define	SI_EVPD 0x01
189 	uint8_t page_code;
190 	uint8_t reserved;
191 	uint8_t length;
192 	uint8_t control;
193 };
194 
195 struct scsi_mode_sense_6
196 {
197 	uint8_t opcode;
198 	uint8_t byte2;
199 #define	SMS_DBD				0x08
200 	uint8_t page;
201 #define	SMS_PAGE_CODE 			0x3F
202 #define SMS_VENDOR_SPECIFIC_PAGE	0x00
203 #define SMS_DISCONNECT_RECONNECT_PAGE	0x02
204 #define SMS_PERIPHERAL_DEVICE_PAGE	0x09
205 #define SMS_CONTROL_MODE_PAGE		0x0A
206 #define SMS_ALL_PAGES_PAGE		0x3F
207 #define	SMS_PAGE_CTRL_MASK		0xC0
208 #define	SMS_PAGE_CTRL_CURRENT 		0x00
209 #define	SMS_PAGE_CTRL_CHANGEABLE 	0x40
210 #define	SMS_PAGE_CTRL_DEFAULT 		0x80
211 #define	SMS_PAGE_CTRL_SAVED 		0xC0
212 	uint8_t unused;
213 	uint8_t length;
214 	uint8_t control;
215 };
216 
217 struct scsi_mode_sense_10
218 {
219 	uint8_t opcode;
220 	uint8_t byte2;		/* same bits as small version */
221 	uint8_t page; 		/* same bits as small version */
222 	uint8_t unused[4];
223 	uint8_t length[2];
224 	uint8_t control;
225 };
226 
227 struct scsi_mode_select_6
228 {
229 	uint8_t opcode;
230 	uint8_t byte2;
231 #define	SMS_SP	0x01
232 #define	SMS_PF	0x10
233 	uint8_t unused[2];
234 	uint8_t length;
235 	uint8_t control;
236 };
237 
238 struct scsi_mode_select_10
239 {
240 	uint8_t opcode;
241 	uint8_t byte2;		/* same bits as small version */
242 	uint8_t unused[5];
243 	uint8_t length[2];
244 	uint8_t control;
245 };
246 
247 /*
248  * When sending a mode select to a tape drive, the medium type must be 0.
249  */
250 struct scsi_mode_hdr_6
251 {
252 	uint8_t datalen;
253 	uint8_t medium_type;
254 	uint8_t dev_specific;
255 	uint8_t block_descr_len;
256 };
257 
258 struct scsi_mode_hdr_10
259 {
260 	uint8_t datalen[2];
261 	uint8_t medium_type;
262 	uint8_t dev_specific;
263 	uint8_t reserved[2];
264 	uint8_t block_descr_len[2];
265 };
266 
267 struct scsi_mode_block_descr
268 {
269 	uint8_t density_code;
270 	uint8_t num_blocks[3];
271 	uint8_t reserved;
272 	uint8_t block_len[3];
273 };
274 
275 struct scsi_log_sense
276 {
277 	uint8_t opcode;
278 	uint8_t byte2;
279 #define	SLS_SP				0x01
280 #define	SLS_PPC				0x02
281 	uint8_t page;
282 #define	SLS_PAGE_CODE 			0x3F
283 #define	SLS_ALL_PAGES_PAGE		0x00
284 #define	SLS_OVERRUN_PAGE		0x01
285 #define	SLS_ERROR_WRITE_PAGE		0x02
286 #define	SLS_ERROR_READ_PAGE		0x03
287 #define	SLS_ERROR_READREVERSE_PAGE	0x04
288 #define	SLS_ERROR_VERIFY_PAGE		0x05
289 #define	SLS_ERROR_NONMEDIUM_PAGE	0x06
290 #define	SLS_ERROR_LASTN_PAGE		0x07
291 #define	SLS_PAGE_CTRL_MASK		0xC0
292 #define	SLS_PAGE_CTRL_THRESHOLD		0x00
293 #define	SLS_PAGE_CTRL_CUMULATIVE	0x40
294 #define	SLS_PAGE_CTRL_THRESH_DEFAULT	0x80
295 #define	SLS_PAGE_CTRL_CUMUL_DEFAULT	0xC0
296 	uint8_t reserved[2];
297 	uint8_t paramptr[2];
298 	uint8_t length[2];
299 	uint8_t control;
300 };
301 
302 struct scsi_log_select
303 {
304 	uint8_t opcode;
305 	uint8_t byte2;
306 /*	SLS_SP				0x01 */
307 #define	SLS_PCR				0x02
308 	uint8_t page;
309 /*	SLS_PAGE_CTRL_MASK		0xC0 */
310 /*	SLS_PAGE_CTRL_THRESHOLD		0x00 */
311 /*	SLS_PAGE_CTRL_CUMULATIVE	0x40 */
312 /*	SLS_PAGE_CTRL_THRESH_DEFAULT	0x80 */
313 /*	SLS_PAGE_CTRL_CUMUL_DEFAULT	0xC0 */
314 	uint8_t reserved[4];
315 	uint8_t length[2];
316 	uint8_t control;
317 };
318 
319 struct scsi_log_header
320 {
321 	uint8_t page;
322 	uint8_t reserved;
323 	uint8_t datalen[2];
324 };
325 
326 struct scsi_log_param_header {
327 	uint8_t param_code[2];
328 	uint8_t param_control;
329 #define	SLP_LP				0x01
330 #define	SLP_LBIN			0x02
331 #define	SLP_TMC_MASK			0x0C
332 #define	SLP_TMC_ALWAYS			0x00
333 #define	SLP_TMC_EQUAL			0x04
334 #define	SLP_TMC_NOTEQUAL		0x08
335 #define	SLP_TMC_GREATER			0x0C
336 #define	SLP_ETC				0x10
337 #define	SLP_TSD				0x20
338 #define	SLP_DS				0x40
339 #define	SLP_DU				0x80
340 	uint8_t param_len;
341 };
342 
343 struct scsi_control_page {
344 	uint8_t page_code;
345 	uint8_t page_length;
346 	uint8_t rlec;
347 #define SCB_RLEC			0x01	/*Report Log Exception Cond*/
348 	uint8_t queue_flags;
349 #define SCP_QUEUE_ALG_MASK		0xF0
350 #define SCP_QUEUE_ALG_RESTRICTED	0x00
351 #define SCP_QUEUE_ALG_UNRESTRICTED	0x10
352 #define SCP_QUEUE_ERR			0x02	/*Queued I/O aborted for CACs*/
353 #define SCP_QUEUE_DQUE			0x01	/*Queued I/O disabled*/
354 	uint8_t eca_and_aen;
355 #define SCP_EECA			0x80	/*Enable Extended CA*/
356 #define SCP_RAENP			0x04	/*Ready AEN Permission*/
357 #define SCP_UAAENP			0x02	/*UA AEN Permission*/
358 #define SCP_EAENP			0x01	/*Error AEN Permission*/
359 	uint8_t reserved;
360 	uint8_t aen_holdoff_period[2];
361 };
362 
363 struct scsi_reserve
364 {
365 	uint8_t opcode;
366 	uint8_t byte2;
367 	uint8_t unused[2];
368 	uint8_t length;
369 	uint8_t control;
370 };
371 
372 struct scsi_release
373 {
374 	uint8_t opcode;
375 	uint8_t byte2;
376 	uint8_t unused[2];
377 	uint8_t length;
378 	uint8_t control;
379 };
380 
381 struct scsi_prevent
382 {
383 	uint8_t opcode;
384 	uint8_t byte2;
385 	uint8_t unused[2];
386 	uint8_t how;
387 	uint8_t control;
388 };
389 #define	PR_PREVENT 0x01
390 #define PR_ALLOW   0x00
391 
392 struct scsi_sync_cache
393 {
394 	uint8_t opcode;
395 	uint8_t byte2;
396 	uint8_t begin_lba[4];
397 	uint8_t reserved;
398 	uint8_t lb_count[2];
399 	uint8_t control;
400 };
401 
402 
403 struct scsi_changedef
404 {
405 	uint8_t opcode;
406 	uint8_t byte2;
407 	uint8_t unused1;
408 	uint8_t how;
409 	uint8_t unused[4];
410 	uint8_t datalen;
411 	uint8_t control;
412 };
413 
414 struct scsi_read_buffer
415 {
416 	uint8_t opcode;
417 	uint8_t byte2;
418 #define	RWB_MODE		0x07
419 #define	RWB_MODE_HDR_DATA	0x00
420 #define	RWB_MODE_DATA		0x02
421 #define	RWB_MODE_DOWNLOAD	0x04
422 #define	RWB_MODE_DOWNLOAD_SAVE	0x05
423         uint8_t buffer_id;
424         uint8_t offset[3];
425         uint8_t length[3];
426         uint8_t control;
427 };
428 
429 struct scsi_write_buffer
430 {
431 	uint8_t opcode;
432 	uint8_t byte2;
433 	uint8_t buffer_id;
434 	uint8_t offset[3];
435 	uint8_t length[3];
436 	uint8_t control;
437 };
438 
439 struct scsi_rw_6
440 {
441 	uint8_t opcode;
442 	uint8_t addr[3];
443 /* only 5 bits are valid in the MSB address byte */
444 #define	SRW_TOPADDR	0x1F
445 	uint8_t length;
446 	uint8_t control;
447 };
448 
449 struct scsi_rw_10
450 {
451 	uint8_t opcode;
452 #define	SRW10_RELADDR	0x01
453 #define SRW10_FUA	0x08
454 #define	SRW10_DPO	0x10
455 	uint8_t byte2;
456 	uint8_t addr[4];
457 	uint8_t reserved;
458 	uint8_t length[2];
459 	uint8_t control;
460 };
461 
462 struct scsi_rw_12
463 {
464 	uint8_t opcode;
465 #define	SRW12_RELADDR	0x01
466 #define SRW12_FUA	0x08
467 #define	SRW12_DPO	0x10
468 	uint8_t byte2;
469 	uint8_t addr[4];
470 	uint8_t length[4];
471 	uint8_t reserved;
472 	uint8_t control;
473 };
474 
475 struct scsi_start_stop_unit
476 {
477 	uint8_t opcode;
478 	uint8_t byte2;
479 #define	SSS_IMMED		0x01
480 	uint8_t reserved[2];
481 	uint8_t how;
482 #define	SSS_START		0x01
483 #define	SSS_LOEJ		0x02
484 	uint8_t control;
485 };
486 
487 #define SC_SCSI_1 0x01
488 #define SC_SCSI_2 0x03
489 
490 /*
491  * Opcodes
492  */
493 
494 #define	TEST_UNIT_READY		0x00
495 #define REQUEST_SENSE		0x03
496 #define	READ_6			0x08
497 #define WRITE_6			0x0a
498 #define INQUIRY			0x12
499 #define MODE_SELECT_6		0x15
500 #define MODE_SENSE_6		0x1a
501 #define START_STOP_UNIT		0x1b
502 #define START_STOP		0x1b
503 #define RESERVE      		0x16
504 #define RELEASE      		0x17
505 #define	RECEIVE_DIAGNOSTIC	0x1c
506 #define	SEND_DIAGNOSTIC		0x1d
507 #define PREVENT_ALLOW		0x1e
508 #define	READ_CAPACITY		0x25
509 #define	READ_10			0x28
510 #define WRITE_10		0x2a
511 #define POSITION_TO_ELEMENT	0x2b
512 #define	SYNCHRONIZE_CACHE	0x35
513 #define	WRITE_BUFFER            0x3b
514 #define	READ_BUFFER             0x3c
515 #define	CHANGE_DEFINITION	0x40
516 #define	LOG_SELECT		0x4c
517 #define	LOG_SENSE		0x4d
518 #ifdef XXXCAM
519 #define	MODE_SENSE_10		0x5A
520 #endif
521 #define	MODE_SELECT_10		0x55
522 #define MOVE_MEDIUM     	0xa5
523 #define READ_12			0xa8
524 #define WRITE_12		0xaa
525 #define READ_ELEMENT_STATUS	0xb8
526 
527 
528 /*
529  * Device Types
530  */
531 #define T_DIRECT	0x00
532 #define T_SEQUENTIAL	0x01
533 #define T_PRINTER	0x02
534 #define T_PROCESSOR	0x03
535 #define T_WORM		0x04
536 #define T_CDROM		0x05
537 #define T_SCANNER 	0x06
538 #define T_OPTICAL 	0x07
539 #define T_CHANGER	0x08
540 #define T_COMM		0x09
541 #define T_ASC0		0x0a
542 #define T_ASC1		0x0b
543 #define	T_STORARRAY	0x0c
544 #define	T_ENCLOSURE	0x0d
545 #define	T_RBC		0x0e
546 #define	T_OCRW		0x0f
547 #define T_NODEVICE	0x1F
548 #define	T_ANY		0xFF	/* Used in Quirk table matches */
549 
550 #define T_REMOV		1
551 #define	T_FIXED		0
552 
553 /*
554  * This length is the initial inquiry length used by the probe code, as
555  * well as the legnth necessary for aic_print_inquiry() to function
556  * correctly.  If either use requires a different length in the future,
557  * the two values should be de-coupled.
558  */
559 #define	SHORT_INQUIRY_LENGTH	36
560 
561 struct scsi_inquiry_data
562 {
563 	uint8_t device;
564 #define	SID_TYPE(inq_data) ((inq_data)->device & 0x1f)
565 #define	SID_QUAL(inq_data) (((inq_data)->device & 0xE0) >> 5)
566 #define	SID_QUAL_LU_CONNECTED	0x00	/*
567 					 * The specified peripheral device
568 					 * type is currently connected to
569 					 * logical unit.  If the target cannot
570 					 * determine whether or not a physical
571 					 * device is currently connected, it
572 					 * shall also use this peripheral
573 					 * qualifier when returning the INQUIRY
574 					 * data.  This peripheral qualifier
575 					 * does not mean that the device is
576 					 * ready for access by the initiator.
577 					 */
578 #define	SID_QUAL_LU_OFFLINE	0x01	/*
579 					 * The target is capable of supporting
580 					 * the specified peripheral device type
581 					 * on this logical unit; however, the
582 					 * physical device is not currently
583 					 * connected to this logical unit.
584 					 */
585 #define SID_QUAL_RSVD		0x02
586 #define	SID_QUAL_BAD_LU		0x03	/*
587 					 * The target is not capable of
588 					 * supporting a physical device on
589 					 * this logical unit. For this
590 					 * peripheral qualifier the peripheral
591 					 * device type shall be set to 1Fh to
592 					 * provide compatibility with previous
593 					 * versions of SCSI. All other
594 					 * peripheral device type values are
595 					 * reserved for this peripheral
596 					 * qualifier.
597 					 */
598 #define	SID_QUAL_IS_VENDOR_UNIQUE(inq_data) ((SID_QUAL(inq_data) & 0x08) != 0)
599 	uint8_t dev_qual2;
600 #define	SID_QUAL2	0x7F
601 #define	SID_IS_REMOVABLE(inq_data) (((inq_data)->dev_qual2 & 0x80) != 0)
602 	uint8_t version;
603 #define SID_ANSI_REV(inq_data) ((inq_data)->version & 0x07)
604 #define		SCSI_REV_0		0
605 #define		SCSI_REV_CCS		1
606 #define		SCSI_REV_2		2
607 #define		SCSI_REV_SPC		3
608 #define		SCSI_REV_SPC2		4
609 
610 #define SID_ECMA	0x38
611 #define SID_ISO		0xC0
612 	uint8_t response_format;
613 #define SID_AENC	0x80
614 #define SID_TrmIOP	0x40
615 	uint8_t additional_length;
616 	uint8_t reserved[2];
617 	uint8_t flags;
618 #define	SID_SftRe	0x01
619 #define	SID_CmdQue	0x02
620 #define	SID_Linked	0x08
621 #define	SID_Sync	0x10
622 #define	SID_WBus16	0x20
623 #define	SID_WBus32	0x40
624 #define	SID_RelAdr	0x80
625 #define SID_VENDOR_SIZE   8
626 	char	 vendor[SID_VENDOR_SIZE];
627 #define SID_PRODUCT_SIZE  16
628 	char	 product[SID_PRODUCT_SIZE];
629 #define SID_REVISION_SIZE 4
630 	char	 revision[SID_REVISION_SIZE];
631 	/*
632 	 * The following fields were taken from SCSI Primary Commands - 2
633 	 * (SPC-2) Revision 14, Dated 11 November 1999
634 	 */
635 #define	SID_VENDOR_SPECIFIC_0_SIZE	20
636 	uint8_t vendor_specific0[SID_VENDOR_SPECIFIC_0_SIZE];
637 	/*
638 	 * An extension of SCSI Parallel Specific Values
639 	 */
640 #define	SID_SPI_IUS		0x01
641 #define	SID_SPI_QAS		0x02
642 #define	SID_SPI_CLOCK_ST	0x00
643 #define	SID_SPI_CLOCK_DT	0x04
644 #define	SID_SPI_CLOCK_DT_ST	0x0C
645 #define	SID_SPI_MASK		0x0F
646 	uint8_t spi3data;
647 	uint8_t reserved2;
648 	/*
649 	 * Version Descriptors, stored 2 byte values.
650 	 */
651 	uint8_t version1[2];
652 	uint8_t version2[2];
653 	uint8_t version3[2];
654 	uint8_t version4[2];
655 	uint8_t version5[2];
656 	uint8_t version6[2];
657 	uint8_t version7[2];
658 	uint8_t version8[2];
659 
660 	uint8_t reserved3[22];
661 
662 #define	SID_VENDOR_SPECIFIC_1_SIZE	160
663 	uint8_t vendor_specific1[SID_VENDOR_SPECIFIC_1_SIZE];
664 };
665 
666 struct scsi_vpd_unit_serial_number
667 {
668 	uint8_t device;
669 	uint8_t page_code;
670 #define SVPD_UNIT_SERIAL_NUMBER	0x80
671 	uint8_t reserved;
672 	uint8_t length; /* serial number length */
673 #define SVPD_SERIAL_NUM_SIZE 251
674 	uint8_t serial_num[SVPD_SERIAL_NUM_SIZE];
675 };
676 
677 struct scsi_read_capacity
678 {
679 	uint8_t opcode;
680 	uint8_t byte2;
681 	uint8_t addr[4];
682 	uint8_t unused[3];
683 	uint8_t control;
684 };
685 
686 struct scsi_read_capacity_data
687 {
688 	uint8_t addr[4];
689 	uint8_t length[4];
690 };
691 
692 struct scsi_report_luns
693 {
694 	uint8_t opcode;
695 	uint8_t byte2;
696 	uint8_t unused[3];
697 	uint8_t addr[4];
698 	uint8_t control;
699 };
700 
701 struct scsi_report_luns_data {
702 	uint8_t length[4];	/* length of LUN inventory, in bytes */
703 	uint8_t reserved[4];	/* unused */
704 	/*
705 	 * LUN inventory- we only support the type zero form for now.
706 	 */
707 	struct {
708 		uint8_t lundata[8];
709 	} luns[1];
710 };
711 #define	RPL_LUNDATA_ATYP_MASK	0xc0	/* MBZ for type 0 lun */
712 #define	RPL_LUNDATA_T0LUN	1	/* @ lundata[1] */
713 
714 
715 struct scsi_sense_data
716 {
717 	uint8_t error_code;
718 #define	SSD_ERRCODE			0x7F
719 #define		SSD_CURRENT_ERROR	0x70
720 #define		SSD_DEFERRED_ERROR	0x71
721 #define	SSD_ERRCODE_VALID	0x80
722 	uint8_t segment;
723 	uint8_t flags;
724 #define	SSD_KEY				0x0F
725 #define		SSD_KEY_NO_SENSE	0x00
726 #define		SSD_KEY_RECOVERED_ERROR	0x01
727 #define		SSD_KEY_NOT_READY	0x02
728 #define		SSD_KEY_MEDIUM_ERROR	0x03
729 #define		SSD_KEY_HARDWARE_ERROR	0x04
730 #define		SSD_KEY_ILLEGAL_REQUEST	0x05
731 #define		SSD_KEY_UNIT_ATTENTION	0x06
732 #define		SSD_KEY_DATA_PROTECT	0x07
733 #define		SSD_KEY_BLANK_CHECK	0x08
734 #define		SSD_KEY_Vendor_Specific	0x09
735 #define		SSD_KEY_COPY_ABORTED	0x0a
736 #define		SSD_KEY_ABORTED_COMMAND	0x0b
737 #define		SSD_KEY_EQUAL		0x0c
738 #define		SSD_KEY_VOLUME_OVERFLOW	0x0d
739 #define		SSD_KEY_MISCOMPARE	0x0e
740 #define		SSD_KEY_RESERVED	0x0f
741 #define	SSD_ILI		0x20
742 #define	SSD_EOM		0x40
743 #define	SSD_FILEMARK	0x80
744 	uint8_t info[4];
745 	uint8_t extra_len;
746 	uint8_t cmd_spec_info[4];
747 	uint8_t add_sense_code;
748 	uint8_t add_sense_code_qual;
749 	uint8_t fru;
750 	uint8_t sense_key_spec[3];
751 #define	SSD_SCS_VALID		0x80
752 #define SSD_FIELDPTR_CMD	0x40
753 #define SSD_BITPTR_VALID	0x08
754 #define SSD_BITPTR_VALUE	0x07
755 #define SSD_MIN_SIZE 18
756 	uint8_t extra_bytes[14];
757 #define SSD_FULL_SIZE sizeof(struct scsi_sense_data)
758 };
759 
760 struct scsi_mode_header_6
761 {
762 	uint8_t data_length;	/* Sense data length */
763 	uint8_t medium_type;
764 	uint8_t dev_spec;
765 	uint8_t blk_desc_len;
766 };
767 
768 struct scsi_mode_header_10
769 {
770 	uint8_t data_length[2];/* Sense data length */
771 	uint8_t medium_type;
772 	uint8_t dev_spec;
773 	uint8_t unused[2];
774 	uint8_t blk_desc_len[2];
775 };
776 
777 struct scsi_mode_page_header
778 {
779 	uint8_t page_code;
780 	uint8_t page_length;
781 };
782 
783 struct scsi_mode_blk_desc
784 {
785 	uint8_t density;
786 	uint8_t nblocks[3];
787 	uint8_t reserved;
788 	uint8_t blklen[3];
789 };
790 
791 #define	SCSI_DEFAULT_DENSITY	0x00	/* use 'default' density */
792 #define	SCSI_SAME_DENSITY	0x7f	/* use 'same' density- >= SCSI-2 only */
793 
794 
795 /*
796  * Status Byte
797  */
798 #define	SCSI_STATUS_OK			0x00
799 #define	SCSI_STATUS_CHECK_COND		0x02
800 #define	SCSI_STATUS_COND_MET		0x04
801 #define	SCSI_STATUS_BUSY		0x08
802 #define SCSI_STATUS_INTERMED		0x10
803 #define SCSI_STATUS_INTERMED_COND_MET	0x14
804 #define SCSI_STATUS_RESERV_CONFLICT	0x18
805 #define SCSI_STATUS_CMD_TERMINATED	0x22	/* Obsolete in SAM-2 */
806 #define SCSI_STATUS_QUEUE_FULL		0x28
807 #define SCSI_STATUS_ACA_ACTIVE		0x30
808 #define SCSI_STATUS_TASK_ABORTED	0x40
809 
810 struct scsi_inquiry_pattern {
811 	uint8_t   type;
812 	uint8_t   media_type;
813 #define	SIP_MEDIA_REMOVABLE	0x01
814 #define	SIP_MEDIA_FIXED		0x02
815 	const char *vendor;
816 	const char *product;
817 	const char *revision;
818 };
819 
820 struct scsi_static_inquiry_pattern {
821 	uint8_t   type;
822 	uint8_t   media_type;
823 	char       vendor[SID_VENDOR_SIZE+1];
824 	char       product[SID_PRODUCT_SIZE+1];
825 	char       revision[SID_REVISION_SIZE+1];
826 };
827 
828 struct scsi_sense_quirk_entry {
829 	struct scsi_inquiry_pattern	inq_pat;
830 	int				num_sense_keys;
831 	int				num_ascs;
832 	struct sense_key_table_entry	*sense_key_info;
833 	struct asc_table_entry		*asc_info;
834 };
835 
836 struct sense_key_table_entry {
837 	uint8_t    sense_key;
838 	uint32_t   action;
839 	const char *desc;
840 };
841 
842 struct asc_table_entry {
843 	uint8_t    asc;
844 	uint8_t    ascq;
845 	uint32_t   action;
846 	const char *desc;
847 };
848 
849 struct op_table_entry {
850 	uint8_t    opcode;
851 	uint16_t   opmask;
852 	const char  *desc;
853 };
854 
855 struct scsi_op_quirk_entry {
856 	struct scsi_inquiry_pattern	inq_pat;
857 	int				num_ops;
858 	struct op_table_entry		*op_table;
859 };
860 
861 typedef enum {
862 	SSS_FLAG_NONE		= 0x00,
863 	SSS_FLAG_PRINT_COMMAND	= 0x01
864 } scsi_sense_string_flags;
865 
866 extern const char *scsi_sense_key_text[];
867 
868 /************************* Large Disk Handling ********************************/
869 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
870 static __inline int aic_sector_div(u_long capacity, int heads, int sectors);
871 
872 static __inline int
aic_sector_div(u_long capacity,int heads,int sectors)873 aic_sector_div(u_long capacity, int heads, int sectors)
874 {
875 	return (capacity / (heads * sectors));
876 }
877 #else
878 static __inline int aic_sector_div(sector_t capacity, int heads, int sectors);
879 
880 static __inline int
aic_sector_div(sector_t capacity,int heads,int sectors)881 aic_sector_div(sector_t capacity, int heads, int sectors)
882 {
883 	/* ugly, ugly sector_div calling convention.. */
884 	sector_div(capacity, (heads * sectors));
885 	return (int)capacity;
886 }
887 #endif
888 
889 /**************************** Module Library Hack *****************************/
890 /*
891  * What we'd like to do is have a single "scsi library" module that both the
892  * aic7xxx and aic79xx drivers could load and depend on.  A cursory examination
893  * of implementing module dependencies in Linux (handling the install and
894  * initrd cases) does not look promissing.  For now, we just duplicate this
895  * code in both drivers using a simple symbol renaming scheme that hides this
896  * hack from the drivers.
897  */
898 #define AIC_LIB_ENTRY_CONCAT(x, prefix)	prefix ## x
899 #define	AIC_LIB_ENTRY_EXPAND(x, prefix) AIC_LIB_ENTRY_CONCAT(x, prefix)
900 #define AIC_LIB_ENTRY(x)		AIC_LIB_ENTRY_EXPAND(x, AIC_LIB_PREFIX)
901 
902 #define	aic_sense_desc			AIC_LIB_ENTRY(_sense_desc)
903 #define	aic_sense_error_action		AIC_LIB_ENTRY(_sense_error_action)
904 #define	aic_error_action		AIC_LIB_ENTRY(_error_action)
905 #define	aic_op_desc			AIC_LIB_ENTRY(_op_desc)
906 #define	aic_cdb_string			AIC_LIB_ENTRY(_cdb_string)
907 #define aic_print_inquiry		AIC_LIB_ENTRY(_print_inquiry)
908 #define aic_calc_syncsrate		AIC_LIB_ENTRY(_calc_syncrate)
909 #define	aic_calc_syncparam		AIC_LIB_ENTRY(_calc_syncparam)
910 #define	aic_calc_speed			AIC_LIB_ENTRY(_calc_speed)
911 #define	aic_inquiry_match		AIC_LIB_ENTRY(_inquiry_match)
912 #define	aic_static_inquiry_match	AIC_LIB_ENTRY(_static_inquiry_match)
913 #define	aic_parse_brace_option		AIC_LIB_ENTRY(_parse_brace_option)
914 
915 /******************************************************************************/
916 
917 void			aic_sense_desc(int /*sense_key*/, int /*asc*/,
918 				       int /*ascq*/, struct scsi_inquiry_data*,
919 				       const char** /*sense_key_desc*/,
920 				       const char** /*asc_desc*/);
921 aic_sense_action	aic_sense_error_action(struct scsi_sense_data*,
922 					       struct scsi_inquiry_data*,
923 					       uint32_t /*sense_flags*/);
924 uint32_t		aic_error_action(struct scsi_cmnd *,
925 					 struct scsi_inquiry_data *,
926 					 cam_status, u_int);
927 
928 #define	SF_RETRY_UA	0x01
929 #define SF_NO_PRINT	0x02
930 #define SF_QUIET_IR	0x04	/* Be quiet about Illegal Request reponses */
931 #define SF_PRINT_ALWAYS	0x08
932 
933 
934 const char *	aic_op_desc(uint16_t /*opcode*/, struct scsi_inquiry_data*);
935 char *		aic_cdb_string(uint8_t* /*cdb_ptr*/, char* /*cdb_string*/,
936 			       size_t /*len*/);
937 void		aic_print_inquiry(struct scsi_inquiry_data*);
938 
939 u_int		aic_calc_syncsrate(u_int /*period_factor*/);
940 u_int		aic_calc_syncparam(u_int /*period*/);
941 u_int		aic_calc_speed(u_int width, u_int period, u_int offset,
942 			       u_int min_rate);
943 
944 int		aic_inquiry_match(caddr_t /*inqbuffer*/,
945 				  caddr_t /*table_entry*/);
946 int		aic_static_inquiry_match(caddr_t /*inqbuffer*/,
947 					 caddr_t /*table_entry*/);
948 
949 typedef void aic_option_callback_t(u_long, int, int, int32_t);
950 char *		aic_parse_brace_option(char *opt_name, char *opt_arg,
951 				       char *end, int depth,
952 				       aic_option_callback_t *, u_long);
953 
954 static __inline void	 scsi_extract_sense(struct scsi_sense_data *sense,
955 					    int *error_code, int *sense_key,
956 					    int *asc, int *ascq);
957 static __inline void	 scsi_ulto2b(uint32_t val, uint8_t *bytes);
958 static __inline void	 scsi_ulto3b(uint32_t val, uint8_t *bytes);
959 static __inline void	 scsi_ulto4b(uint32_t val, uint8_t *bytes);
960 static __inline uint32_t scsi_2btoul(uint8_t *bytes);
961 static __inline uint32_t scsi_3btoul(uint8_t *bytes);
962 static __inline int32_t	 scsi_3btol(uint8_t *bytes);
963 static __inline uint32_t scsi_4btoul(uint8_t *bytes);
964 
scsi_extract_sense(struct scsi_sense_data * sense,int * error_code,int * sense_key,int * asc,int * ascq)965 static __inline void scsi_extract_sense(struct scsi_sense_data *sense,
966 				       int *error_code, int *sense_key,
967 				       int *asc, int *ascq)
968 {
969 	*error_code = sense->error_code & SSD_ERRCODE;
970 	*sense_key = sense->flags & SSD_KEY;
971 	*asc = (sense->extra_len >= 5) ? sense->add_sense_code : 0;
972 	*ascq = (sense->extra_len >= 6) ? sense->add_sense_code_qual : 0;
973 }
974 
975 static __inline void
scsi_ulto2b(uint32_t val,uint8_t * bytes)976 scsi_ulto2b(uint32_t val, uint8_t *bytes)
977 {
978 
979 	bytes[0] = (val >> 8) & 0xff;
980 	bytes[1] = val & 0xff;
981 }
982 
983 static __inline void
scsi_ulto3b(uint32_t val,uint8_t * bytes)984 scsi_ulto3b(uint32_t val, uint8_t *bytes)
985 {
986 
987 	bytes[0] = (val >> 16) & 0xff;
988 	bytes[1] = (val >> 8) & 0xff;
989 	bytes[2] = val & 0xff;
990 }
991 
992 static __inline void
scsi_ulto4b(uint32_t val,uint8_t * bytes)993 scsi_ulto4b(uint32_t val, uint8_t *bytes)
994 {
995 
996 	bytes[0] = (val >> 24) & 0xff;
997 	bytes[1] = (val >> 16) & 0xff;
998 	bytes[2] = (val >> 8) & 0xff;
999 	bytes[3] = val & 0xff;
1000 }
1001 
1002 static __inline uint32_t
scsi_2btoul(uint8_t * bytes)1003 scsi_2btoul(uint8_t *bytes)
1004 {
1005 	uint32_t rv;
1006 
1007 	rv = (bytes[0] << 8) |
1008 	     bytes[1];
1009 	return (rv);
1010 }
1011 
1012 static __inline uint32_t
scsi_3btoul(uint8_t * bytes)1013 scsi_3btoul(uint8_t *bytes)
1014 {
1015 	uint32_t rv;
1016 
1017 	rv = (bytes[0] << 16) |
1018 	     (bytes[1] << 8) |
1019 	     bytes[2];
1020 	return (rv);
1021 }
1022 
1023 static __inline int32_t
scsi_3btol(uint8_t * bytes)1024 scsi_3btol(uint8_t *bytes)
1025 {
1026 	uint32_t rc = scsi_3btoul(bytes);
1027 
1028 	if (rc & 0x00800000)
1029 		rc |= 0xff000000;
1030 
1031 	return (int32_t) rc;
1032 }
1033 
1034 static __inline uint32_t
scsi_4btoul(uint8_t * bytes)1035 scsi_4btoul(uint8_t *bytes)
1036 {
1037 	uint32_t rv;
1038 
1039 	rv = (bytes[0] << 24) |
1040 	     (bytes[1] << 16) |
1041 	     (bytes[2] << 8) |
1042 	     bytes[3];
1043 	return (rv);
1044 }
1045 
1046 #endif /*_AICLIB_H */
1047