1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * This header file contains public constants and structures used by
4  * the SCSI initiator code.
5  */
6 #ifndef _SCSI_SCSI_H
7 #define _SCSI_SCSI_H
8 
9 #include <linux/types.h>
10 
11 #ifdef __KERNEL__
12 #include <linux/scatterlist.h>
13 #include <linux/kernel.h>
14 #include <scsi/scsi_common.h>
15 #endif /* __KERNEL__ */
16 #include <scsi/scsi_proto.h>
17 
18 #ifdef __KERNEL__
19 struct scsi_cmnd;
20 
21 enum scsi_timeouts {
22 	SCSI_DEFAULT_EH_TIMEOUT		= 10 * HZ,
23 };
24 
25 /*
26  * DIX-capable adapters effectively support infinite chaining for the
27  * protection information scatterlist
28  */
29 #define SCSI_MAX_PROT_SG_SEGMENTS	0xFFFF
30 
31 /*
32  * Special value for scanning to specify scanning or rescanning of all
33  * possible channels, (target) ids, or luns on a given shost.
34  */
35 #define SCAN_WILD_CARD	~0
36 
37 /** scsi_status_is_good - check the status return.
38  *
39  * @status: the status passed up from the driver (including host and
40  *          driver components)
41  *
42  * This returns true for known good conditions that may be treated as
43  * command completed normally
44  */
scsi_status_is_good(int status)45 static inline int scsi_status_is_good(int status)
46 {
47 	/*
48 	 * FIXME: bit0 is listed as reserved in SCSI-2, but is
49 	 * significant in SCSI-3.  For now, we follow the SCSI-2
50 	 * behaviour and ignore reserved bits.
51 	 */
52 	status &= 0xfe;
53 	return ((status == SAM_STAT_GOOD) ||
54 		(status == SAM_STAT_CONDITION_MET) ||
55 		/* Next two "intermediate" statuses are obsolete in SAM-4 */
56 		(status == SAM_STAT_INTERMEDIATE) ||
57 		(status == SAM_STAT_INTERMEDIATE_CONDITION_MET) ||
58 		/* FIXME: this is obsolete in SAM-3 */
59 		(status == SAM_STAT_COMMAND_TERMINATED));
60 }
61 #endif /* __KERNEL__ */
62 
63 
64 /*
65  * standard mode-select header prepended to all mode-select commands
66  */
67 
68 struct ccs_modesel_head {
69 	__u8 _r1;			/* reserved */
70 	__u8 medium;		/* device-specific medium type */
71 	__u8 _r2;			/* reserved */
72 	__u8 block_desc_length;	/* block descriptor length */
73 	__u8 density;		/* device-specific density code */
74 	__u8 number_blocks_hi;	/* number of blocks in this block desc */
75 	__u8 number_blocks_med;
76 	__u8 number_blocks_lo;
77 	__u8 _r3;
78 	__u8 block_length_hi;	/* block length for blocks in this desc */
79 	__u8 block_length_med;
80 	__u8 block_length_lo;
81 };
82 
83 #ifdef __KERNEL__
84 /*
85  * The Well Known LUNS (SAM-3) in our int representation of a LUN
86  */
87 #define SCSI_W_LUN_BASE 0xc100
88 #define SCSI_W_LUN_REPORT_LUNS (SCSI_W_LUN_BASE + 1)
89 #define SCSI_W_LUN_ACCESS_CONTROL (SCSI_W_LUN_BASE + 2)
90 #define SCSI_W_LUN_TARGET_LOG_PAGE (SCSI_W_LUN_BASE + 3)
91 
scsi_is_wlun(u64 lun)92 static inline int scsi_is_wlun(u64 lun)
93 {
94 	return (lun & 0xff00) == SCSI_W_LUN_BASE;
95 }
96 #endif /* __KERNEL__ */
97 
98 
99 /*
100  *  MESSAGE CODES
101  */
102 
103 #define COMMAND_COMPLETE    0x00
104 #define EXTENDED_MESSAGE    0x01
105 #define     EXTENDED_MODIFY_DATA_POINTER    0x00
106 #define     EXTENDED_SDTR                   0x01
107 #define     EXTENDED_EXTENDED_IDENTIFY      0x02    /* SCSI-I only */
108 #define     EXTENDED_WDTR                   0x03
109 #define     EXTENDED_PPR                    0x04
110 #define     EXTENDED_MODIFY_BIDI_DATA_PTR   0x05
111 #define SAVE_POINTERS       0x02
112 #define RESTORE_POINTERS    0x03
113 #define DISCONNECT          0x04
114 #define INITIATOR_ERROR     0x05
115 #define ABORT_TASK_SET      0x06
116 #define MESSAGE_REJECT      0x07
117 #define NOP                 0x08
118 #define MSG_PARITY_ERROR    0x09
119 #define LINKED_CMD_COMPLETE 0x0a
120 #define LINKED_FLG_CMD_COMPLETE 0x0b
121 #define TARGET_RESET        0x0c
122 #define ABORT_TASK          0x0d
123 #define CLEAR_TASK_SET      0x0e
124 #define INITIATE_RECOVERY   0x0f            /* SCSI-II only */
125 #define RELEASE_RECOVERY    0x10            /* SCSI-II only */
126 #define TERMINATE_IO_PROC   0x11            /* SCSI-II only */
127 #define CLEAR_ACA           0x16
128 #define LOGICAL_UNIT_RESET  0x17
129 #define SIMPLE_QUEUE_TAG    0x20
130 #define HEAD_OF_QUEUE_TAG   0x21
131 #define ORDERED_QUEUE_TAG   0x22
132 #define IGNORE_WIDE_RESIDUE 0x23
133 #define ACA                 0x24
134 #define QAS_REQUEST         0x55
135 
136 /* Old SCSI2 names, don't use in new code */
137 #define BUS_DEVICE_RESET    TARGET_RESET
138 #define ABORT               ABORT_TASK_SET
139 
140 #ifdef __KERNEL__
141 /*
142  * Host byte codes
143  */
144 
145 #define DID_OK          0x00	/* NO error                                */
146 #define DID_NO_CONNECT  0x01	/* Couldn't connect before timeout period  */
147 #define DID_BUS_BUSY    0x02	/* BUS stayed busy through time out period */
148 #define DID_TIME_OUT    0x03	/* TIMED OUT for other reason              */
149 #define DID_BAD_TARGET  0x04	/* BAD target.                             */
150 #define DID_ABORT       0x05	/* Told to abort for some other reason     */
151 #define DID_PARITY      0x06	/* Parity error                            */
152 #define DID_ERROR       0x07	/* Internal error                          */
153 #define DID_RESET       0x08	/* Reset by somebody.                      */
154 #define DID_BAD_INTR    0x09	/* Got an interrupt we weren't expecting.  */
155 #define DID_PASSTHROUGH 0x0a	/* Force command past mid-layer            */
156 #define DID_SOFT_ERROR  0x0b	/* The low level driver just wish a retry  */
157 #define DID_IMM_RETRY   0x0c	/* Retry without decrementing retry count  */
158 #define DID_REQUEUE	0x0d	/* Requeue command (no immediate retry) also
159 				 * without decrementing the retry count	   */
160 #define DID_TRANSPORT_DISRUPTED 0x0e /* Transport error disrupted execution
161 				      * and the driver blocked the port to
162 				      * recover the link. Transport class will
163 				      * retry or fail IO */
164 #define DID_TRANSPORT_FAILFAST	0x0f /* Transport class fastfailed the io */
165 #define DID_TARGET_FAILURE 0x10 /* Permanent target failure, do not retry on
166 				 * other paths */
167 #define DID_NEXUS_FAILURE 0x11  /* Permanent nexus failure, retry on other
168 				 * paths might yield different results */
169 #define DID_ALLOC_FAILURE 0x12  /* Space allocation on the device failed */
170 #define DID_MEDIUM_ERROR  0x13  /* Medium error */
171 #define DID_TRANSPORT_MARGINAL 0x14 /* Transport marginal errors */
172 #define DRIVER_OK       0x00	/* Driver status                           */
173 
174 /*
175  *  These indicate the error that occurred, and what is available.
176  */
177 
178 #define DRIVER_BUSY         0x01
179 #define DRIVER_SOFT         0x02
180 #define DRIVER_MEDIA        0x03
181 #define DRIVER_ERROR        0x04
182 
183 #define DRIVER_INVALID      0x05
184 #define DRIVER_TIMEOUT      0x06
185 #define DRIVER_HARD         0x07
186 #define DRIVER_SENSE	    0x08
187 
188 /*
189  * Internal return values.
190  */
191 
192 #define NEEDS_RETRY     0x2001
193 #define SUCCESS         0x2002
194 #define FAILED          0x2003
195 #define QUEUED          0x2004
196 #define SOFT_ERROR      0x2005
197 #define ADD_TO_MLQUEUE  0x2006
198 #define TIMEOUT_ERROR   0x2007
199 #define SCSI_RETURN_NOT_HANDLED   0x2008
200 #define FAST_IO_FAIL	0x2009
201 
202 /*
203  * Midlevel queue return values.
204  */
205 #define SCSI_MLQUEUE_HOST_BUSY   0x1055
206 #define SCSI_MLQUEUE_DEVICE_BUSY 0x1056
207 #define SCSI_MLQUEUE_EH_RETRY    0x1057
208 #define SCSI_MLQUEUE_TARGET_BUSY 0x1058
209 
210 /*
211  *  Use these to separate status msg and our bytes
212  *
213  *  These are set by:
214  *
215  *      status byte = set from target device
216  *      msg_byte    = return status from host adapter itself.
217  *      host_byte   = set by low-level driver to indicate status.
218  *      driver_byte = set by mid-level.
219  */
220 #define status_byte(result) (((result) >> 1) & 0x7f)
221 #define msg_byte(result)    (((result) >> 8) & 0xff)
222 #define host_byte(result)   (((result) >> 16) & 0xff)
223 #define driver_byte(result) (((result) >> 24) & 0xff)
224 
225 #define sense_class(sense)  (((sense) >> 4) & 0x7)
226 #define sense_error(sense)  ((sense) & 0xf)
227 #define sense_valid(sense)  ((sense) & 0x80)
228 
229 /*
230  * default timeouts
231 */
232 #define FORMAT_UNIT_TIMEOUT		(2 * 60 * 60 * HZ)
233 #define START_STOP_TIMEOUT		(60 * HZ)
234 #define MOVE_MEDIUM_TIMEOUT		(5 * 60 * HZ)
235 #define READ_ELEMENT_STATUS_TIMEOUT	(5 * 60 * HZ)
236 #define READ_DEFECT_DATA_TIMEOUT	(60 * HZ )
237 
238 
239 #define IDENTIFY_BASE       0x80
240 #define IDENTIFY(can_disconnect, lun)   (IDENTIFY_BASE |\
241 		     ((can_disconnect) ?  0x40 : 0) |\
242 		     ((lun) & 0x07))
243 
244 /*
245  *  struct scsi_device::scsi_level values. For SCSI devices other than those
246  *  prior to SCSI-2 (i.e. over 12 years old) this value is (resp[2] + 1)
247  *  where "resp" is a byte array of the response to an INQUIRY. The scsi_level
248  *  variable is visible to the user via sysfs.
249  */
250 
251 #define SCSI_UNKNOWN    0
252 #define SCSI_1          1
253 #define SCSI_1_CCS      2
254 #define SCSI_2          3
255 #define SCSI_3          4        /* SPC */
256 #define SCSI_SPC_2      5
257 #define SCSI_SPC_3      6
258 
259 /*
260  * INQ PERIPHERAL QUALIFIERS
261  */
262 #define SCSI_INQ_PQ_CON         0x00
263 #define SCSI_INQ_PQ_NOT_CON     0x01
264 #define SCSI_INQ_PQ_NOT_CAP     0x03
265 #endif /* __KERNEL__ */
266 
267 
268 /*
269  * Here are some scsi specific ioctl commands which are sometimes useful.
270  *
271  * Note that include/linux/cdrom.h also defines IOCTL 0x5300 - 0x5395
272  */
273 
274 /* Used to obtain PUN and LUN info.  Conflicts with CDROMAUDIOBUFSIZ */
275 #define SCSI_IOCTL_GET_IDLUN		0x5382
276 
277 /* 0x5383 and 0x5384 were used for SCSI_IOCTL_TAGGED_{ENABLE,DISABLE} */
278 
279 /* Used to obtain the host number of a device. */
280 #define SCSI_IOCTL_PROBE_HOST		0x5385
281 
282 /* Used to obtain the bus number for a device */
283 #define SCSI_IOCTL_GET_BUS_NUMBER	0x5386
284 
285 /* Used to obtain the PCI location of a device */
286 #define SCSI_IOCTL_GET_PCI		0x5387
287 
288 #endif /* _SCSI_SCSI_H */
289