18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Largely written by Julian Elischer (julian@tfs.com)
38c2ecf20Sopenharmony_ci * for TRW Financial Systems.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * TRW Financial Systems, in accordance with their agreement with Carnegie
68c2ecf20Sopenharmony_ci * Mellon University, makes this software available to CMU to distribute
78c2ecf20Sopenharmony_ci * or use in any manner that they see fit as long as this message is kept with
88c2ecf20Sopenharmony_ci * the software. For this reason TFS also grants any other persons or
98c2ecf20Sopenharmony_ci * organisations permission to use or modify this software.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * TFS supplies this software to be publicly redistributed
128c2ecf20Sopenharmony_ci * on the understanding that TFS is not responsible for the correct
138c2ecf20Sopenharmony_ci * functioning of this software in any circumstances.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * $FreeBSD: src/sys/cam/scsi/scsi_all.h,v 1.21 2002/10/08 17:12:44 ken Exp $
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * Copyright (c) 2003 Adaptec Inc.
208c2ecf20Sopenharmony_ci * All rights reserved.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
238c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
248c2ecf20Sopenharmony_ci * are met:
258c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
268c2ecf20Sopenharmony_ci *    notice, this list of conditions, and the following disclaimer,
278c2ecf20Sopenharmony_ci *    without modification.
288c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce at minimum a disclaimer
298c2ecf20Sopenharmony_ci *    substantially similar to the "NO WARRANTY" disclaimer below
308c2ecf20Sopenharmony_ci *    ("Disclaimer") and any redistribution must be conditioned upon
318c2ecf20Sopenharmony_ci *    including a substantially similar Disclaimer requirement for further
328c2ecf20Sopenharmony_ci *    binary redistribution.
338c2ecf20Sopenharmony_ci * 3. Neither the names of the above-listed copyright holders nor the names
348c2ecf20Sopenharmony_ci *    of any contributors may be used to endorse or promote products derived
358c2ecf20Sopenharmony_ci *    from this software without specific prior written permission.
368c2ecf20Sopenharmony_ci *
378c2ecf20Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the
388c2ecf20Sopenharmony_ci * GNU General Public License ("GPL") version 2 as published by the Free
398c2ecf20Sopenharmony_ci * Software Foundation.
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * NO WARRANTY
428c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
438c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
448c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
458c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
468c2ecf20Sopenharmony_ci * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
478c2ecf20Sopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
488c2ecf20Sopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
498c2ecf20Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
508c2ecf20Sopenharmony_ci * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
518c2ecf20Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
528c2ecf20Sopenharmony_ci * POSSIBILITY OF SUCH DAMAGES.
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * $Id$
558c2ecf20Sopenharmony_ci */
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#ifndef	_AICLIB_H
588c2ecf20Sopenharmony_ci#define _AICLIB_H
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistruct scsi_sense
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	uint8_t opcode;
638c2ecf20Sopenharmony_ci	uint8_t byte2;
648c2ecf20Sopenharmony_ci	uint8_t unused[2];
658c2ecf20Sopenharmony_ci	uint8_t length;
668c2ecf20Sopenharmony_ci	uint8_t control;
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define		SCSI_REV_0		0
708c2ecf20Sopenharmony_ci#define		SCSI_REV_CCS		1
718c2ecf20Sopenharmony_ci#define		SCSI_REV_2		2
728c2ecf20Sopenharmony_ci#define		SCSI_REV_SPC		3
738c2ecf20Sopenharmony_ci#define		SCSI_REV_SPC2		4
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistruct scsi_sense_data
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	uint8_t error_code;
788c2ecf20Sopenharmony_ci#define	SSD_ERRCODE			0x7F
798c2ecf20Sopenharmony_ci#define		SSD_CURRENT_ERROR	0x70
808c2ecf20Sopenharmony_ci#define		SSD_DEFERRED_ERROR	0x71
818c2ecf20Sopenharmony_ci#define	SSD_ERRCODE_VALID	0x80
828c2ecf20Sopenharmony_ci	uint8_t segment;
838c2ecf20Sopenharmony_ci	uint8_t flags;
848c2ecf20Sopenharmony_ci#define	SSD_KEY				0x0F
858c2ecf20Sopenharmony_ci#define		SSD_KEY_NO_SENSE	0x00
868c2ecf20Sopenharmony_ci#define		SSD_KEY_RECOVERED_ERROR	0x01
878c2ecf20Sopenharmony_ci#define		SSD_KEY_NOT_READY	0x02
888c2ecf20Sopenharmony_ci#define		SSD_KEY_MEDIUM_ERROR	0x03
898c2ecf20Sopenharmony_ci#define		SSD_KEY_HARDWARE_ERROR	0x04
908c2ecf20Sopenharmony_ci#define		SSD_KEY_ILLEGAL_REQUEST	0x05
918c2ecf20Sopenharmony_ci#define		SSD_KEY_UNIT_ATTENTION	0x06
928c2ecf20Sopenharmony_ci#define		SSD_KEY_DATA_PROTECT	0x07
938c2ecf20Sopenharmony_ci#define		SSD_KEY_BLANK_CHECK	0x08
948c2ecf20Sopenharmony_ci#define		SSD_KEY_Vendor_Specific	0x09
958c2ecf20Sopenharmony_ci#define		SSD_KEY_COPY_ABORTED	0x0a
968c2ecf20Sopenharmony_ci#define		SSD_KEY_ABORTED_COMMAND	0x0b
978c2ecf20Sopenharmony_ci#define		SSD_KEY_EQUAL		0x0c
988c2ecf20Sopenharmony_ci#define		SSD_KEY_VOLUME_OVERFLOW	0x0d
998c2ecf20Sopenharmony_ci#define		SSD_KEY_MISCOMPARE	0x0e
1008c2ecf20Sopenharmony_ci#define		SSD_KEY_RESERVED	0x0f
1018c2ecf20Sopenharmony_ci#define	SSD_ILI		0x20
1028c2ecf20Sopenharmony_ci#define	SSD_EOM		0x40
1038c2ecf20Sopenharmony_ci#define	SSD_FILEMARK	0x80
1048c2ecf20Sopenharmony_ci	uint8_t info[4];
1058c2ecf20Sopenharmony_ci	uint8_t extra_len;
1068c2ecf20Sopenharmony_ci	uint8_t cmd_spec_info[4];
1078c2ecf20Sopenharmony_ci	uint8_t add_sense_code;
1088c2ecf20Sopenharmony_ci	uint8_t add_sense_code_qual;
1098c2ecf20Sopenharmony_ci	uint8_t fru;
1108c2ecf20Sopenharmony_ci	uint8_t sense_key_spec[3];
1118c2ecf20Sopenharmony_ci#define	SSD_SCS_VALID		0x80
1128c2ecf20Sopenharmony_ci#define SSD_FIELDPTR_CMD	0x40
1138c2ecf20Sopenharmony_ci#define SSD_BITPTR_VALID	0x08
1148c2ecf20Sopenharmony_ci#define SSD_BITPTR_VALUE	0x07
1158c2ecf20Sopenharmony_ci#define SSD_MIN_SIZE 18
1168c2ecf20Sopenharmony_ci	uint8_t extra_bytes[14];
1178c2ecf20Sopenharmony_ci#define SSD_FULL_SIZE sizeof(struct scsi_sense_data)
1188c2ecf20Sopenharmony_ci};
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/*
1218c2ecf20Sopenharmony_ci * Status Byte
1228c2ecf20Sopenharmony_ci */
1238c2ecf20Sopenharmony_ci#define	SCSI_STATUS_OK			0x00
1248c2ecf20Sopenharmony_ci#define	SCSI_STATUS_CHECK_COND		0x02
1258c2ecf20Sopenharmony_ci#define	SCSI_STATUS_COND_MET		0x04
1268c2ecf20Sopenharmony_ci#define	SCSI_STATUS_BUSY		0x08
1278c2ecf20Sopenharmony_ci#define SCSI_STATUS_INTERMED		0x10
1288c2ecf20Sopenharmony_ci#define SCSI_STATUS_INTERMED_COND_MET	0x14
1298c2ecf20Sopenharmony_ci#define SCSI_STATUS_RESERV_CONFLICT	0x18
1308c2ecf20Sopenharmony_ci#define SCSI_STATUS_CMD_TERMINATED	0x22	/* Obsolete in SAM-2 */
1318c2ecf20Sopenharmony_ci#define SCSI_STATUS_QUEUE_FULL		0x28
1328c2ecf20Sopenharmony_ci#define SCSI_STATUS_ACA_ACTIVE		0x30
1338c2ecf20Sopenharmony_ci#define SCSI_STATUS_TASK_ABORTED	0x40
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/************************* Large Disk Handling ********************************/
1368c2ecf20Sopenharmony_cistatic inline int
1378c2ecf20Sopenharmony_ciaic_sector_div(sector_t capacity, int heads, int sectors)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	/* ugly, ugly sector_div calling convention.. */
1408c2ecf20Sopenharmony_ci	sector_div(capacity, (heads * sectors));
1418c2ecf20Sopenharmony_ci	return (int)capacity;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic inline uint32_t
1458c2ecf20Sopenharmony_ciscsi_4btoul(uint8_t *bytes)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	uint32_t rv;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	rv = (bytes[0] << 24) |
1508c2ecf20Sopenharmony_ci	     (bytes[1] << 16) |
1518c2ecf20Sopenharmony_ci	     (bytes[2] << 8) |
1528c2ecf20Sopenharmony_ci	     bytes[3];
1538c2ecf20Sopenharmony_ci	return (rv);
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci/* Macros for generating the elements of the PCI ID tables. */
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci#define GETID(v, s) (unsigned)(((v) >> (s)) & 0xFFFF ?: PCI_ANY_ID)
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci#define ID_C(x, c)						\
1618c2ecf20Sopenharmony_ci{								\
1628c2ecf20Sopenharmony_ci	GETID(x,32), GETID(x,48), GETID(x,0), GETID(x,16),	\
1638c2ecf20Sopenharmony_ci	(c) << 8, 0xFFFF00, 0					\
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci#define ID2C(x)                          \
1678c2ecf20Sopenharmony_ci	ID_C(x, PCI_CLASS_STORAGE_SCSI), \
1688c2ecf20Sopenharmony_ci	ID_C(x, PCI_CLASS_STORAGE_RAID)
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci#define IDIROC(x)  ((x) | ~ID_ALL_IROC_MASK)
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci/* Generate IDs for all 16 possibilites.
1738c2ecf20Sopenharmony_ci * The argument has already masked out
1748c2ecf20Sopenharmony_ci * the 4 least significant bits of the device id.
1758c2ecf20Sopenharmony_ci * (e.g., mask: ID_9005_GENERIC_MASK).
1768c2ecf20Sopenharmony_ci */
1778c2ecf20Sopenharmony_ci#define ID16(x)                          \
1788c2ecf20Sopenharmony_ci	ID(x),                           \
1798c2ecf20Sopenharmony_ci	ID((x) | 0x0001000000000000ull), \
1808c2ecf20Sopenharmony_ci	ID((x) | 0x0002000000000000ull), \
1818c2ecf20Sopenharmony_ci	ID((x) | 0x0003000000000000ull), \
1828c2ecf20Sopenharmony_ci	ID((x) | 0x0004000000000000ull), \
1838c2ecf20Sopenharmony_ci	ID((x) | 0x0005000000000000ull), \
1848c2ecf20Sopenharmony_ci	ID((x) | 0x0006000000000000ull), \
1858c2ecf20Sopenharmony_ci	ID((x) | 0x0007000000000000ull), \
1868c2ecf20Sopenharmony_ci	ID((x) | 0x0008000000000000ull), \
1878c2ecf20Sopenharmony_ci	ID((x) | 0x0009000000000000ull), \
1888c2ecf20Sopenharmony_ci	ID((x) | 0x000A000000000000ull), \
1898c2ecf20Sopenharmony_ci	ID((x) | 0x000B000000000000ull), \
1908c2ecf20Sopenharmony_ci	ID((x) | 0x000C000000000000ull), \
1918c2ecf20Sopenharmony_ci	ID((x) | 0x000D000000000000ull), \
1928c2ecf20Sopenharmony_ci	ID((x) | 0x000E000000000000ull), \
1938c2ecf20Sopenharmony_ci	ID((x) | 0x000F000000000000ull)
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci#endif /*_AICLIB_H */
196