1/*
2 * This header file contains public constants and structures used by
3 * the scsi code for linux.
4 *
5 * For documentation on the OPCODES, MESSAGES, and SENSE values,
6 * please consult the SCSI standard.
7 */
8#ifndef _SCSI_SCSI_H
9#define _SCSI_SCSI_H
10
11#include <linux/types.h>
12
13/*
14 *	The maximum sg list length SCSI can cope with
15 *	(currently must be a power of 2 between 32 and 256)
16 */
17#define SCSI_MAX_PHYS_SEGMENTS	MAX_PHYS_SEGMENTS
18
19
20/*
21 *	SCSI command lengths
22 */
23
24extern const unsigned char scsi_command_size[8];
25#define COMMAND_SIZE(opcode) scsi_command_size[((opcode) >> 5) & 7]
26
27/*
28 * Special value for scanning to specify scanning or rescanning of all
29 * possible channels, (target) ids, or luns on a given shost.
30 */
31#define SCAN_WILD_CARD	~0
32
33/*
34 *      SCSI opcodes
35 */
36
37#define TEST_UNIT_READY       0x00
38#define REZERO_UNIT           0x01
39#define REQUEST_SENSE         0x03
40#define FORMAT_UNIT           0x04
41#define READ_BLOCK_LIMITS     0x05
42#define REASSIGN_BLOCKS       0x07
43#define INITIALIZE_ELEMENT_STATUS 0x07
44#define READ_6                0x08
45#define WRITE_6               0x0a
46#define SEEK_6                0x0b
47#define READ_REVERSE          0x0f
48#define WRITE_FILEMARKS       0x10
49#define SPACE                 0x11
50#define INQUIRY               0x12
51#define RECOVER_BUFFERED_DATA 0x14
52#define MODE_SELECT           0x15
53#define RESERVE               0x16
54#define RELEASE               0x17
55#define COPY                  0x18
56#define ERASE                 0x19
57#define MODE_SENSE            0x1a
58#define START_STOP            0x1b
59#define RECEIVE_DIAGNOSTIC    0x1c
60#define SEND_DIAGNOSTIC       0x1d
61#define ALLOW_MEDIUM_REMOVAL  0x1e
62
63#define SET_WINDOW            0x24
64#define READ_CAPACITY         0x25
65#define READ_10               0x28
66#define WRITE_10              0x2a
67#define SEEK_10               0x2b
68#define POSITION_TO_ELEMENT   0x2b
69#define WRITE_VERIFY          0x2e
70#define VERIFY                0x2f
71#define SEARCH_HIGH           0x30
72#define SEARCH_EQUAL          0x31
73#define SEARCH_LOW            0x32
74#define SET_LIMITS            0x33
75#define PRE_FETCH             0x34
76#define READ_POSITION         0x34
77#define SYNCHRONIZE_CACHE     0x35
78#define LOCK_UNLOCK_CACHE     0x36
79#define READ_DEFECT_DATA      0x37
80#define MEDIUM_SCAN           0x38
81#define COMPARE               0x39
82#define COPY_VERIFY           0x3a
83#define WRITE_BUFFER          0x3b
84#define READ_BUFFER           0x3c
85#define UPDATE_BLOCK          0x3d
86#define READ_LONG             0x3e
87#define WRITE_LONG            0x3f
88#define CHANGE_DEFINITION     0x40
89#define WRITE_SAME            0x41
90#define READ_TOC              0x43
91#define LOG_SELECT            0x4c
92#define LOG_SENSE             0x4d
93#define MODE_SELECT_10        0x55
94#define RESERVE_10            0x56
95#define RELEASE_10            0x57
96#define MODE_SENSE_10         0x5a
97#define PERSISTENT_RESERVE_IN 0x5e
98#define PERSISTENT_RESERVE_OUT 0x5f
99#define REPORT_LUNS           0xa0
100#define MAINTENANCE_IN        0xa3
101#define MOVE_MEDIUM           0xa5
102#define EXCHANGE_MEDIUM       0xa6
103#define READ_12               0xa8
104#define WRITE_12              0xaa
105#define WRITE_VERIFY_12       0xae
106#define SEARCH_HIGH_12        0xb0
107#define SEARCH_EQUAL_12       0xb1
108#define SEARCH_LOW_12         0xb2
109#define READ_ELEMENT_STATUS   0xb8
110#define SEND_VOLUME_TAG       0xb6
111#define WRITE_LONG_2          0xea
112#define READ_16               0x88
113#define WRITE_16              0x8a
114#define VERIFY_16	      0x8f
115#define SERVICE_ACTION_IN     0x9e
116/* values for service action in */
117#define	SAI_READ_CAPACITY_16  0x10
118/* values for maintenance in */
119#define MI_REPORT_TARGET_PGS  0x0a
120
121/* Values for T10/04-262r7 */
122#define	ATA_16		      0x85	/* 16-byte pass-thru */
123#define	ATA_12		      0xa1	/* 12-byte pass-thru */
124
125/*
126 *  SCSI Architecture Model (SAM) Status codes. Taken from SAM-3 draft
127 *  T10/1561-D Revision 4 Draft dated 7th November 2002.
128 */
129#define SAM_STAT_GOOD            0x00
130#define SAM_STAT_CHECK_CONDITION 0x02
131#define SAM_STAT_CONDITION_MET   0x04
132#define SAM_STAT_BUSY            0x08
133#define SAM_STAT_INTERMEDIATE    0x10
134#define SAM_STAT_INTERMEDIATE_CONDITION_MET 0x14
135#define SAM_STAT_RESERVATION_CONFLICT 0x18
136#define SAM_STAT_COMMAND_TERMINATED 0x22	/* obsolete in SAM-3 */
137#define SAM_STAT_TASK_SET_FULL   0x28
138#define SAM_STAT_ACA_ACTIVE      0x30
139#define SAM_STAT_TASK_ABORTED    0x40
140
141/** scsi_status_is_good - check the status return.
142 *
143 * @status: the status passed up from the driver (including host and
144 *          driver components)
145 *
146 * This returns true for known good conditions that may be treated as
147 * command completed normally
148 */
149static inline int scsi_status_is_good(int status)
150{
151	status &= 0xfe;
152	return ((status == SAM_STAT_GOOD) ||
153		(status == SAM_STAT_INTERMEDIATE) ||
154		(status == SAM_STAT_INTERMEDIATE_CONDITION_MET) ||
155		(status == SAM_STAT_COMMAND_TERMINATED));
156}
157
158/*
159 *  Status codes. These are deprecated as they are shifted 1 bit right
160 *  from those found in the SCSI standards. This causes confusion for
161 *  applications that are ported to several OSes. Prefer SAM Status codes
162 *  above.
163 */
164
165#define GOOD                 0x00
166#define CHECK_CONDITION      0x01
167#define CONDITION_GOOD       0x02
168#define BUSY                 0x04
169#define INTERMEDIATE_GOOD    0x08
170#define INTERMEDIATE_C_GOOD  0x0a
171#define RESERVATION_CONFLICT 0x0c
172#define COMMAND_TERMINATED   0x11
173#define QUEUE_FULL           0x14
174#define ACA_ACTIVE           0x18
175#define TASK_ABORTED         0x20
176
177#define STATUS_MASK          0xfe
178
179/*
180 *  SENSE KEYS
181 */
182
183#define NO_SENSE            0x00
184#define RECOVERED_ERROR     0x01
185#define NOT_READY           0x02
186#define MEDIUM_ERROR        0x03
187#define HARDWARE_ERROR      0x04
188#define ILLEGAL_REQUEST     0x05
189#define UNIT_ATTENTION      0x06
190#define DATA_PROTECT        0x07
191#define BLANK_CHECK         0x08
192#define COPY_ABORTED        0x0a
193#define ABORTED_COMMAND     0x0b
194#define VOLUME_OVERFLOW     0x0d
195#define MISCOMPARE          0x0e
196
197
198/*
199 *  DEVICE TYPES
200 *  Please keep them in 0x%02x format for $MODALIAS to work
201 */
202
203#define TYPE_DISK           0x00
204#define TYPE_TAPE           0x01
205#define TYPE_PRINTER        0x02
206#define TYPE_PROCESSOR      0x03    /* HP scanners use this */
207#define TYPE_WORM           0x04    /* Treated as ROM by our system */
208#define TYPE_ROM            0x05
209#define TYPE_SCANNER        0x06
210#define TYPE_MOD            0x07    /* Magneto-optical disk -
211				     * - treated as TYPE_DISK */
212#define TYPE_MEDIUM_CHANGER 0x08
213#define TYPE_COMM           0x09    /* Communications device */
214#define TYPE_RAID           0x0c
215#define TYPE_ENCLOSURE      0x0d    /* Enclosure Services Device */
216#define TYPE_RBC	    0x0e
217#define TYPE_NO_LUN         0x7f
218
219/* Returns a human-readable name for the device */
220extern const char * scsi_device_type(unsigned type);
221
222/*
223 * standard mode-select header prepended to all mode-select commands
224 */
225
226struct ccs_modesel_head {
227	__u8 _r1;			/* reserved */
228	__u8 medium;		/* device-specific medium type */
229	__u8 _r2;			/* reserved */
230	__u8 block_desc_length;	/* block descriptor length */
231	__u8 density;		/* device-specific density code */
232	__u8 number_blocks_hi;	/* number of blocks in this block desc */
233	__u8 number_blocks_med;
234	__u8 number_blocks_lo;
235	__u8 _r3;
236	__u8 block_length_hi;	/* block length for blocks in this desc */
237	__u8 block_length_med;
238	__u8 block_length_lo;
239};
240
241/*
242 * ScsiLun: 8 byte LUN.
243 */
244struct scsi_lun {
245	__u8 scsi_lun[8];
246};
247
248/*
249 *  MESSAGE CODES
250 */
251
252#define COMMAND_COMPLETE    0x00
253#define EXTENDED_MESSAGE    0x01
254#define     EXTENDED_MODIFY_DATA_POINTER    0x00
255#define     EXTENDED_SDTR                   0x01
256#define     EXTENDED_EXTENDED_IDENTIFY      0x02    /* SCSI-I only */
257#define     EXTENDED_WDTR                   0x03
258#define     EXTENDED_PPR                    0x04
259#define     EXTENDED_MODIFY_BIDI_DATA_PTR   0x05
260#define SAVE_POINTERS       0x02
261#define RESTORE_POINTERS    0x03
262#define DISCONNECT          0x04
263#define INITIATOR_ERROR     0x05
264#define ABORT_TASK_SET      0x06
265#define MESSAGE_REJECT      0x07
266#define NOP                 0x08
267#define MSG_PARITY_ERROR    0x09
268#define LINKED_CMD_COMPLETE 0x0a
269#define LINKED_FLG_CMD_COMPLETE 0x0b
270#define TARGET_RESET        0x0c
271#define ABORT_TASK          0x0d
272#define CLEAR_TASK_SET      0x0e
273#define INITIATE_RECOVERY   0x0f            /* SCSI-II only */
274#define RELEASE_RECOVERY    0x10            /* SCSI-II only */
275#define CLEAR_ACA           0x16
276#define LOGICAL_UNIT_RESET  0x17
277#define SIMPLE_QUEUE_TAG    0x20
278#define HEAD_OF_QUEUE_TAG   0x21
279#define ORDERED_QUEUE_TAG   0x22
280#define IGNORE_WIDE_RESIDUE 0x23
281#define ACA                 0x24
282#define QAS_REQUEST         0x55
283
284/* Old SCSI2 names, don't use in new code */
285#define BUS_DEVICE_RESET    TARGET_RESET
286#define ABORT               ABORT_TASK_SET
287
288/*
289 * Host byte codes
290 */
291
292#define DID_OK          0x00	/* NO error                                */
293#define DID_NO_CONNECT  0x01	/* Couldn't connect before timeout period  */
294#define DID_BUS_BUSY    0x02	/* BUS stayed busy through time out period */
295#define DID_TIME_OUT    0x03	/* TIMED OUT for other reason              */
296#define DID_BAD_TARGET  0x04	/* BAD target.                             */
297#define DID_ABORT       0x05	/* Told to abort for some other reason     */
298#define DID_PARITY      0x06	/* Parity error                            */
299#define DID_ERROR       0x07	/* Internal error                          */
300#define DID_RESET       0x08	/* Reset by somebody.                      */
301#define DID_BAD_INTR    0x09	/* Got an interrupt we weren't expecting.  */
302#define DID_PASSTHROUGH 0x0a	/* Force command past mid-layer            */
303#define DID_SOFT_ERROR  0x0b	/* The low level driver just wish a retry  */
304#define DID_IMM_RETRY   0x0c	/* Retry without decrementing retry count  */
305#define DID_REQUEUE	0x0d	/* Requeue command (no immediate retry) also
306				 * without decrementing the retry count	   */
307#define DRIVER_OK       0x00	/* Driver status                           */
308
309/*
310 *  These indicate the error that occurred, and what is available.
311 */
312
313#define DRIVER_BUSY         0x01
314#define DRIVER_SOFT         0x02
315#define DRIVER_MEDIA        0x03
316#define DRIVER_ERROR        0x04
317
318#define DRIVER_INVALID      0x05
319#define DRIVER_TIMEOUT      0x06
320#define DRIVER_HARD         0x07
321#define DRIVER_SENSE	    0x08
322
323#define SUGGEST_RETRY       0x10
324#define SUGGEST_ABORT       0x20
325#define SUGGEST_REMAP       0x30
326#define SUGGEST_DIE         0x40
327#define SUGGEST_SENSE       0x80
328#define SUGGEST_IS_OK       0xff
329
330#define DRIVER_MASK         0x0f
331#define SUGGEST_MASK        0xf0
332
333/*
334 * Internal return values.
335 */
336
337#define NEEDS_RETRY     0x2001
338#define SUCCESS         0x2002
339#define FAILED          0x2003
340#define QUEUED          0x2004
341#define SOFT_ERROR      0x2005
342#define ADD_TO_MLQUEUE  0x2006
343#define TIMEOUT_ERROR   0x2007
344
345/*
346 * Midlevel queue return values.
347 */
348#define SCSI_MLQUEUE_HOST_BUSY   0x1055
349#define SCSI_MLQUEUE_DEVICE_BUSY 0x1056
350#define SCSI_MLQUEUE_EH_RETRY    0x1057
351
352/*
353 *  Use these to separate status msg and our bytes
354 *
355 *  These are set by:
356 *
357 *      status byte = set from target device
358 *      msg_byte    = return status from host adapter itself.
359 *      host_byte   = set by low-level driver to indicate status.
360 *      driver_byte = set by mid-level.
361 */
362#define status_byte(result) (((result) >> 1) & 0x7f)
363#define msg_byte(result)    (((result) >> 8) & 0xff)
364#define host_byte(result)   (((result) >> 16) & 0xff)
365#define driver_byte(result) (((result) >> 24) & 0xff)
366#define suggestion(result)  (driver_byte(result) & SUGGEST_MASK)
367
368#define sense_class(sense)  (((sense) >> 4) & 0x7)
369#define sense_error(sense)  ((sense) & 0xf)
370#define sense_valid(sense)  ((sense) & 0x80);
371
372/*
373 * default timeouts
374*/
375#define FORMAT_UNIT_TIMEOUT		(2 * 60 * 60 * HZ)
376#define START_STOP_TIMEOUT		(60 * HZ)
377#define MOVE_MEDIUM_TIMEOUT		(5 * 60 * HZ)
378#define READ_ELEMENT_STATUS_TIMEOUT	(5 * 60 * HZ)
379#define READ_DEFECT_DATA_TIMEOUT	(60 * HZ )
380
381
382#define IDENTIFY_BASE       0x80
383#define IDENTIFY(can_disconnect, lun)   (IDENTIFY_BASE |\
384		     ((can_disconnect) ?  0x40 : 0) |\
385		     ((lun) & 0x07))
386
387/*
388 *  struct scsi_device::scsi_level values. For SCSI devices other than those
389 *  prior to SCSI-2 (i.e. over 12 years old) this value is (resp[2] + 1)
390 *  where "resp" is a byte array of the response to an INQUIRY. The scsi_level
391 *  variable is visible to the user via sysfs.
392 */
393
394#define SCSI_UNKNOWN    0
395#define SCSI_1          1
396#define SCSI_1_CCS      2
397#define SCSI_2          3
398#define SCSI_3          4        /* SPC */
399#define SCSI_SPC_2      5
400#define SCSI_SPC_3      6
401
402/*
403 * INQ PERIPHERAL QUALIFIERS
404 */
405#define SCSI_INQ_PQ_CON         0x00
406#define SCSI_INQ_PQ_NOT_CON     0x01
407#define SCSI_INQ_PQ_NOT_CAP     0x03
408
409
410/*
411 * Here are some scsi specific ioctl commands which are sometimes useful.
412 *
413 * Note that include/linux/cdrom.h also defines IOCTL 0x5300 - 0x5395
414 */
415
416/* Used to obtain PUN and LUN info.  Conflicts with CDROMAUDIOBUFSIZ */
417#define SCSI_IOCTL_GET_IDLUN		0x5382
418
419/* 0x5383 and 0x5384 were used for SCSI_IOCTL_TAGGED_{ENABLE,DISABLE} */
420
421/* Used to obtain the host number of a device. */
422#define SCSI_IOCTL_PROBE_HOST		0x5385
423
424/* Used to obtain the bus number for a device */
425#define SCSI_IOCTL_GET_BUS_NUMBER	0x5386
426
427/* Used to obtain the PCI location of a device */
428#define SCSI_IOCTL_GET_PCI		0x5387
429
430/* Pull a u32 out of a SCSI message (using BE SCSI conventions) */
431static inline __u32 scsi_to_u32(__u8 *ptr)
432{
433	return (ptr[0]<<24) + (ptr[1]<<16) + (ptr[2]<<8) + ptr[3];
434}
435
436#endif /* _SCSI_SCSI_H */
437