xref: /openbmc/linux/include/scsi/scsi.h (revision 633b47cb009d09dc8f4ba9cdb3a0ca138809c7c7)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * This header file contains public constants and structures used by
4c5f1ac8bSBart Van Assche  * the SCSI initiator code.
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds #ifndef _SCSI_SCSI_H
71da177e4SLinus Torvalds #define _SCSI_SCSI_H
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/types.h>
10ac61c46fSDavid Dillow #include <linux/scatterlist.h>
110816c925SMartin K. Petersen #include <linux/kernel.h>
1207e38420SBart Van Assche #include <scsi/scsi_common.h>
13c5f1ac8bSBart Van Assche #include <scsi/scsi_proto.h>
14149d0e48SBart Van Assche #include <scsi/scsi_status.h>
15f290f197SJames Bottomley 
16f290f197SJames Bottomley struct scsi_cmnd;
171da177e4SLinus Torvalds 
180816c925SMartin K. Petersen enum scsi_timeouts {
190816c925SMartin K. Petersen 	SCSI_DEFAULT_EH_TIMEOUT		= 10 * HZ,
200816c925SMartin K. Petersen };
210816c925SMartin K. Petersen 
221da177e4SLinus Torvalds /*
2313f05c8dSMartin K. Petersen  * DIX-capable adapters effectively support infinite chaining for the
2413f05c8dSMartin K. Petersen  * protection information scatterlist
2513f05c8dSMartin K. Petersen  */
2613f05c8dSMartin K. Petersen #define SCSI_MAX_PROT_SG_SEGMENTS	0xFFFF
2713f05c8dSMartin K. Petersen 
2813f05c8dSMartin K. Petersen /*
29e02f3f59SChristoph Hellwig  * Special value for scanning to specify scanning or rescanning of all
30e02f3f59SChristoph Hellwig  * possible channels, (target) ids, or luns on a given shost.
31e02f3f59SChristoph Hellwig  */
32e02f3f59SChristoph Hellwig #define SCAN_WILD_CARD	~0
33e02f3f59SChristoph Hellwig 
341da177e4SLinus Torvalds /*
351da177e4SLinus Torvalds  * standard mode-select header prepended to all mode-select commands
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds struct ccs_modesel_head {
391da177e4SLinus Torvalds 	__u8 _r1;			/* reserved */
401da177e4SLinus Torvalds 	__u8 medium;		/* device-specific medium type */
411da177e4SLinus Torvalds 	__u8 _r2;			/* reserved */
421da177e4SLinus Torvalds 	__u8 block_desc_length;	/* block descriptor length */
431da177e4SLinus Torvalds 	__u8 density;		/* device-specific density code */
441da177e4SLinus Torvalds 	__u8 number_blocks_hi;	/* number of blocks in this block desc */
451da177e4SLinus Torvalds 	__u8 number_blocks_med;
461da177e4SLinus Torvalds 	__u8 number_blocks_lo;
471da177e4SLinus Torvalds 	__u8 _r3;
481da177e4SLinus Torvalds 	__u8 block_length_hi;	/* block length for blocks in this desc */
491da177e4SLinus Torvalds 	__u8 block_length_med;
501da177e4SLinus Torvalds 	__u8 block_length_lo;
511da177e4SLinus Torvalds };
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /*
5401b291bdSJames Bottomley  * The Well Known LUNS (SAM-3) in our int representation of a LUN
5501b291bdSJames Bottomley  */
5601b291bdSJames Bottomley #define SCSI_W_LUN_BASE 0xc100
5701b291bdSJames Bottomley #define SCSI_W_LUN_REPORT_LUNS (SCSI_W_LUN_BASE + 1)
5801b291bdSJames Bottomley #define SCSI_W_LUN_ACCESS_CONTROL (SCSI_W_LUN_BASE + 2)
5901b291bdSJames Bottomley #define SCSI_W_LUN_TARGET_LOG_PAGE (SCSI_W_LUN_BASE + 3)
6001b291bdSJames Bottomley 
scsi_is_wlun(u64 lun)619cb78c16SHannes Reinecke static inline int scsi_is_wlun(u64 lun)
6201b291bdSJames Bottomley {
6301b291bdSJames Bottomley 	return (lun & 0xff00) == SCSI_W_LUN_BASE;
6401b291bdSJames Bottomley }
6501b291bdSJames Bottomley 
66d0672a03SHannes Reinecke /**
67d0672a03SHannes Reinecke  * scsi_status_is_check_condition - check the status return.
68d0672a03SHannes Reinecke  *
69d0672a03SHannes Reinecke  * @status: the status passed up from the driver (including host and
70d0672a03SHannes Reinecke  *          driver components)
71d0672a03SHannes Reinecke  *
72d0672a03SHannes Reinecke  * This returns true if the status code is SAM_STAT_CHECK_CONDITION.
73d0672a03SHannes Reinecke  */
scsi_status_is_check_condition(int status)74d0672a03SHannes Reinecke static inline int scsi_status_is_check_condition(int status)
75d0672a03SHannes Reinecke {
76d0672a03SHannes Reinecke 	if (status < 0)
77d0672a03SHannes Reinecke 		return false;
78d0672a03SHannes Reinecke 	status &= 0xfe;
79d0672a03SHannes Reinecke 	return status == SAM_STAT_CHECK_CONDITION;
80d0672a03SHannes Reinecke }
8101b291bdSJames Bottomley 
8201b291bdSJames Bottomley /*
83149d0e48SBart Van Assche  *  Extended message codes.
841da177e4SLinus Torvalds  */
851da177e4SLinus Torvalds #define     EXTENDED_MODIFY_DATA_POINTER    0x00
861da177e4SLinus Torvalds #define     EXTENDED_SDTR                   0x01
871da177e4SLinus Torvalds #define     EXTENDED_EXTENDED_IDENTIFY      0x02    /* SCSI-I only */
881da177e4SLinus Torvalds #define     EXTENDED_WDTR                   0x03
891da177e4SLinus Torvalds #define     EXTENDED_PPR                    0x04
901da177e4SLinus Torvalds #define     EXTENDED_MODIFY_BIDI_DATA_PTR   0x05
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /*
931da177e4SLinus Torvalds  * Internal return values.
941da177e4SLinus Torvalds  */
95b8e162f9SBart Van Assche enum scsi_disposition {
96b8e162f9SBart Van Assche 	NEEDS_RETRY		= 0x2001,
97b8e162f9SBart Van Assche 	SUCCESS			= 0x2002,
98b8e162f9SBart Van Assche 	FAILED			= 0x2003,
99b8e162f9SBart Van Assche 	QUEUED			= 0x2004,
100b8e162f9SBart Van Assche 	SOFT_ERROR		= 0x2005,
101b8e162f9SBart Van Assche 	ADD_TO_MLQUEUE		= 0x2006,
102b8e162f9SBart Van Assche 	TIMEOUT_ERROR		= 0x2007,
103b8e162f9SBart Van Assche 	SCSI_RETURN_NOT_HANDLED	= 0x2008,
104b8e162f9SBart Van Assche 	FAST_IO_FAIL		= 0x2009,
105b8e162f9SBart Van Assche };
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * Midlevel queue return values.
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds #define SCSI_MLQUEUE_HOST_BUSY   0x1055
1111da177e4SLinus Torvalds #define SCSI_MLQUEUE_DEVICE_BUSY 0x1056
1121da177e4SLinus Torvalds #define SCSI_MLQUEUE_EH_RETRY    0x1057
113f0c0a376SMike Christie #define SCSI_MLQUEUE_TARGET_BUSY 0x1058
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /*
1161da177e4SLinus Torvalds  *  Use these to separate status msg and our bytes
1171da177e4SLinus Torvalds  *
1181da177e4SLinus Torvalds  *  These are set by:
1191da177e4SLinus Torvalds  *
1201da177e4SLinus Torvalds  *      status byte = set from target device
12154cf31d0SHannes Reinecke  *      msg_byte    (unused)
1221da177e4SLinus Torvalds  *      host_byte   = set by low-level driver to indicate status.
1231da177e4SLinus Torvalds  */
12404b3c8c0SMike Christie #define status_byte(result) (result & 0xff)
1251da177e4SLinus Torvalds #define host_byte(result)   (((result) >> 16) & 0xff)
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds #define sense_class(sense)  (((sense) >> 4) & 0x7)
1281da177e4SLinus Torvalds #define sense_error(sense)  ((sense) & 0xf)
129497888cfSPhil Carmody #define sense_valid(sense)  ((sense) & 0x80)
1301da177e4SLinus Torvalds 
131b6651129S /*
132b6651129S  * default timeouts
133b6651129S */
134b6651129S #define FORMAT_UNIT_TIMEOUT		(2 * 60 * 60 * HZ)
135b6651129S #define START_STOP_TIMEOUT		(60 * HZ)
136b6651129S #define MOVE_MEDIUM_TIMEOUT		(5 * 60 * HZ)
137b6651129S #define READ_ELEMENT_STATUS_TIMEOUT	(5 * 60 * HZ)
138b6651129S #define READ_DEFECT_DATA_TIMEOUT	(60 * HZ )
139b6651129S 
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds #define IDENTIFY_BASE       0x80
1421da177e4SLinus Torvalds #define IDENTIFY(can_disconnect, lun)   (IDENTIFY_BASE |\
1431da177e4SLinus Torvalds 		     ((can_disconnect) ?  0x40 : 0) |\
1441da177e4SLinus Torvalds 		     ((lun) & 0x07))
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds /*
1471da177e4SLinus Torvalds  *  struct scsi_device::scsi_level values. For SCSI devices other than those
1481da177e4SLinus Torvalds  *  prior to SCSI-2 (i.e. over 12 years old) this value is (resp[2] + 1)
1491da177e4SLinus Torvalds  *  where "resp" is a byte array of the response to an INQUIRY. The scsi_level
1501da177e4SLinus Torvalds  *  variable is visible to the user via sysfs.
1511da177e4SLinus Torvalds  */
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds #define SCSI_UNKNOWN    0
1541da177e4SLinus Torvalds #define SCSI_1          1
1551da177e4SLinus Torvalds #define SCSI_1_CCS      2
1561da177e4SLinus Torvalds #define SCSI_2          3
1571da177e4SLinus Torvalds #define SCSI_3          4        /* SPC */
1581da177e4SLinus Torvalds #define SCSI_SPC_2      5
1591da177e4SLinus Torvalds #define SCSI_SPC_3      6
160*2132df16SDamien Le Moal #define SCSI_SPC_4	7
161*2132df16SDamien Le Moal #define SCSI_SPC_5	8
162*2132df16SDamien Le Moal #define SCSI_SPC_6	14
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /*
1651da177e4SLinus Torvalds  * INQ PERIPHERAL QUALIFIERS
1661da177e4SLinus Torvalds  */
1671da177e4SLinus Torvalds #define SCSI_INQ_PQ_CON         0x00
1681da177e4SLinus Torvalds #define SCSI_INQ_PQ_NOT_CON     0x01
1691da177e4SLinus Torvalds #define SCSI_INQ_PQ_NOT_CAP     0x03
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds /*
1731da177e4SLinus Torvalds  * Here are some scsi specific ioctl commands which are sometimes useful.
1741da177e4SLinus Torvalds  *
1751da177e4SLinus Torvalds  * Note that include/linux/cdrom.h also defines IOCTL 0x5300 - 0x5395
1761da177e4SLinus Torvalds  */
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* Used to obtain PUN and LUN info.  Conflicts with CDROMAUDIOBUFSIZ */
1791da177e4SLinus Torvalds #define SCSI_IOCTL_GET_IDLUN		0x5382
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds /* 0x5383 and 0x5384 were used for SCSI_IOCTL_TAGGED_{ENABLE,DISABLE} */
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /* Used to obtain the host number of a device. */
1841da177e4SLinus Torvalds #define SCSI_IOCTL_PROBE_HOST		0x5385
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds /* Used to obtain the bus number for a device */
1871da177e4SLinus Torvalds #define SCSI_IOCTL_GET_BUS_NUMBER	0x5386
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /* Used to obtain the PCI location of a device */
1901da177e4SLinus Torvalds #define SCSI_IOCTL_GET_PCI		0x5387
1911da177e4SLinus Torvalds 
1921ee27534SJason Yan /** scsi_status_is_good - check the status return.
1931ee27534SJason Yan  *
1941ee27534SJason Yan  * @status: the status passed up from the driver (including host and
1951ee27534SJason Yan  *          driver components)
1961ee27534SJason Yan  *
1971ee27534SJason Yan  * This returns true for known good conditions that may be treated as
1981ee27534SJason Yan  * command completed normally
1991ee27534SJason Yan  */
scsi_status_is_good(int status)200ced202f7SHannes Reinecke static inline bool scsi_status_is_good(int status)
2011ee27534SJason Yan {
202ced202f7SHannes Reinecke 	if (status < 0)
203ced202f7SHannes Reinecke 		return false;
204ced202f7SHannes Reinecke 
2051ee27534SJason Yan 	if (host_byte(status) == DID_NO_CONNECT)
206ced202f7SHannes Reinecke 		return false;
2071ee27534SJason Yan 
2081ee27534SJason Yan 	/*
2091ee27534SJason Yan 	 * FIXME: bit0 is listed as reserved in SCSI-2, but is
2101ee27534SJason Yan 	 * significant in SCSI-3.  For now, we follow the SCSI-2
2111ee27534SJason Yan 	 * behaviour and ignore reserved bits.
2121ee27534SJason Yan 	 */
2131ee27534SJason Yan 	status &= 0xfe;
2141ee27534SJason Yan 	return ((status == SAM_STAT_GOOD) ||
2151ee27534SJason Yan 		(status == SAM_STAT_CONDITION_MET) ||
2161ee27534SJason Yan 		/* Next two "intermediate" statuses are obsolete in SAM-4 */
2171ee27534SJason Yan 		(status == SAM_STAT_INTERMEDIATE) ||
2181ee27534SJason Yan 		(status == SAM_STAT_INTERMEDIATE_CONDITION_MET) ||
2191ee27534SJason Yan 		/* FIXME: this is obsolete in SAM-3 */
2201ee27534SJason Yan 		(status == SAM_STAT_COMMAND_TERMINATED));
2211ee27534SJason Yan }
2221ee27534SJason Yan 
2231da177e4SLinus Torvalds #endif /* _SCSI_SCSI_H */
224