1 #ifndef _SCSI_GENERIC_H
2 #define _SCSI_GENERIC_H
6 Started: Aug 9 by Lawrence Foard (entropy@world.std.com), to allow user
7 process control of SCSI devices.
8 Development Sponsored by Killy Corp. NY NY
9 Original driver (sg.h):
10 * Copyright (C) 1992 Lawrence Foard
11 Version 2 and 3 extensions to driver:
12 * Copyright (C) 1998 - 2000 Douglas Gilbert
14 Version: 3.1.12 (20000222)
15 This version is for 2.3/2.4 series kernels.
17 Changes since 3.1.10 (20000123)
18 - make device allocation dynamic (get rid of SG_EXTRA_DEVS)
19 - move to sg0,sg1,sg2 rather than sga,sgb,sgc
20 - make sg_io_hdr_t safer across architectures
21 Changes since 2.1.34 (990603) and 2.3.35 (990708)
22 - add new interface structure: sg_io_hdr_t
23 - supports larger sense buffer, DMA residual count + direct IO
24 - add SG_IO ioctl (combines function of write() + read() )
25 - remove SG_SET_MERGE_FD, UNDERRUN_FLAG + _GET_ ioctls + logic
26 - add proc_fs support in /proc/scsi/sg/ directory
27 - add queuing info into struct sg_scsi_id
28 - def_reserved_size can be given at driver or module load time
29 Changes since 2.1.33 (990521)
30 - implement SG_SET_RESERVED_SIZE and associated memory re-org.
31 - add SG_NEXT_CMD_LEN to override SCSI command lengths
32 - add SG_GET_VERSION_NUM to get version expressed as an integer
33 Changes since 2.1.32 (990501)
34 - fix race condition in sg_read() and sg_open()
35 Changes since 2.1.31 (990327)
36 - add ioctls SG_GET_UNDERRUN_FLAG and _SET_. Change the default
37 to _not_ flag underruns (affects aic7xxx driver)
38 - clean up logging of pointers to use %p (for 64 bit architectures)
39 - rework usage of get_user/copy_to_user family of kernel calls
40 - "disown" scsi_command blocks before releasing them
42 Map of SG verions to the Linux kernels in which they appear:
43 ---------- ----------------------------------
44 original all kernels < 2.2.6
45 2.1.31 2.2.6 and 2.2.7
46 2.1.32 2.2.8 and 2.2.9
47 2.1.34 2.2.10 to 2.2.13
49 2.3.35 2.3.x development series kernels (starting 2.3.20)
50 3.0.x optional version 3 sg driver for 2.2 series
51 3.1.x candidate version 3 sg driver for 2.3 series
53 Major new features in SG 3.x driver (cf SG 2.x drivers)
54 - SG_IO ioctl() combines function if write() and read()
55 - new interface (sg_io_hdr_t) but still supports old interface
56 - scatter/gather in user space and direct IO supported
58 Major features in SG 2.x driver (cf original SG driver)
59 - per file descriptor (fd) write-read sequencing
60 - command queuing supported
61 - scatter-gather supported at kernel level allowing potentially
63 - more SCSI status information returned
64 - asynchronous notification support added (SIGPOLL, SIGIO)
65 - read() can fetch by given pack_id
66 - uses kernel memory as appropriate for SCSI adapter being used
67 - single SG_BIG_BUFF replaced by per file descriptor "reserve
68 buffer" whose size can be manipulated by ioctls()
70 The term "indirect IO" refers a method by which data is DMAed into kernel
71 buffers from the hardware and afterwards is transferred into the user
72 space (or vice versa if you are writing). Transfer speeds of up to 20 to
73 30MBytes/sec have been measured using indirect IO. For faster throughputs
74 "direct IO" which cuts out the double handling of data is required.
75 Direct IO is supported by the SG 3.x drivers on 2.3 series Linux kernels
76 (or later) and requires the use of the new interface.
78 Requests for direct IO with the new interface will automatically fall back
79 to indirect IO mode if they cannot be fulfilled. An example of such a case
80 is an ISA SCSI adapter which is only capable of DMAing to the lower 16MB of
81 memory due to the architecture of ISA. The 'info' field in the new
82 interface indicates whether a direct or indirect data transfer took place.
84 Obtaining memory for the kernel buffers used in indirect IO is done by
85 first checking if the "reserved buffer" for the current file descriptor
86 is available and large enough. If these conditions are _not_ met then
87 kernel memory is obtained on a per SCSI command basis. This corresponds
88 to a write(), read() sequence or a SG_IO ioctl() call. Further, the
89 kernel memory that is suitable for DMA may be constrained by the
90 architecture of the SCSI adapter (e.g. ISA adapters).
94 A web site for SG device drivers can be found at:
95 http://www.torque.net/sg [alternatively check the MAINTAINERS file]
96 The main documents are still based on 2.x versions:
97 http://www.torque.net/sg/p/scsi-generic.txt
98 http://www.torque.net/sg/p/scsi-generic_long.txt
99 The first document can also be found in the kernel source tree, probably at:
100 /usr/src/linux/Documentation/scsi-generic.txt .
101 Documentation on the changes and additions in 3.x version of the sg driver
102 can be found at: http://www.torque.net/sg/p/scsi-generic_v3.txt
103 Utility and test programs are also available at that web site.
106 /* New interface introduced in the 3.x SG drivers follows */
108 typedef struct sg_iovec
/* same structure as used by readv() Linux system */
109 { /* call. It defines one scatter-gather element. */
110 void * iov_base
; /* Starting address */
111 size_t iov_len
; /* Length in bytes */
115 typedef struct sg_io_hdr
117 int interface_id
; /* [i] 'S' for SCSI generic (required) */
118 int dxfer_direction
; /* [i] data transfer direction */
119 unsigned char cmd_len
; /* [i] SCSI command length ( <= 16 bytes) */
120 unsigned char mx_sb_len
; /* [i] max length to write to sbp */
121 unsigned short iovec_count
; /* [i] 0 implies no scatter gather */
122 unsigned int dxfer_len
; /* [i] byte count of data transfer */
123 void * dxferp
; /* [i], [*io] points to data transfer memory
124 or scatter gather list */
125 unsigned char * cmdp
; /* [i], [*i] points to command to perform */
126 unsigned char * sbp
; /* [i], [*o] points to sense_buffer memory */
127 unsigned int timeout
; /* [i] MAX_UINT->no timeout (unit: millisec) */
128 unsigned int flags
; /* [i] 0 -> default, see SG_FLAG... */
129 int pack_id
; /* [i->o] unused internally (normally) */
130 void * usr_ptr
; /* [i->o] unused internally */
131 unsigned char status
; /* [o] scsi status */
132 unsigned char masked_status
;/* [o] shifted, masked scsi status */
133 unsigned char msg_status
; /* [o] messaging level data (optional) */
134 unsigned char sb_len_wr
; /* [o] byte count actually written to sbp */
135 unsigned short host_status
; /* [o] errors from host adapter */
136 unsigned short driver_status
;/* [o] errors from software driver */
137 int resid
; /* [o] dxfer_len - actual_transferred */
138 unsigned int duration
; /* [o] time taken by cmd (unit: millisec) */
139 unsigned int info
; /* [o] auxiliary information */
140 } sg_io_hdr_t
; /* 64 bytes long (on i386) */
142 /* Use negative values to flag difference from original sg_header structure */
143 #define SG_DXFER_NONE -1 /* e.g. a SCSI Test Unit Ready command */
144 #define SG_DXFER_TO_DEV -2 /* e.g. a SCSI WRITE command */
145 #define SG_DXFER_FROM_DEV -3 /* e.g. a SCSI READ command */
146 #define SG_DXFER_TO_FROM_DEV -4 /* treated like SG_DXFER_FROM_DEV with the
147 additional property than during indirect
148 IO the user buffer is copied into the
149 kernel buffers before the transfer */
151 /* following flag values can be "or"-ed together */
152 #define SG_FLAG_DIRECT_IO 1 /* default is indirect IO */
153 #define SG_FLAG_LUN_INHIBIT 2 /* default is to put device's lun into */
154 /* the 2nd byte of SCSI command */
155 #define SG_FLAG_NO_DXFER 0x10000 /* no transfer of kernel buffers to/from */
156 /* user space (debug indirect IO) */
158 /* following 'info' values are "or"-ed together */
159 #define SG_INFO_OK_MASK 0x1
160 #define SG_INFO_OK 0x0 /* no sense, host nor driver "noise" */
161 #define SG_INFO_CHECK 0x1 /* something abnormal happened */
163 #define SG_INFO_DIRECT_IO_MASK 0x6
164 #define SG_INFO_INDIRECT_IO 0x0 /* data xfer via kernel buffers (or no xfer) */
165 #define SG_INFO_DIRECT_IO 0x2 /* direct IO requested and performed */
166 #define SG_INFO_MIXED_IO 0x4 /* part direct, part indirect IO */
169 typedef struct sg_scsi_id
{ /* used by SG_GET_SCSI_ID ioctl() */
170 int host_no
; /* as in "scsi<n>" where 'n' is one of 0, 1, 2 etc */
172 int scsi_id
; /* scsi id of target device */
174 int scsi_type
; /* TYPE_... defined in scsi/scsi.h */
175 short h_cmd_per_lun
;/* host (adapter) maximum commands per lun */
176 short d_queue_depth
;/* device (or adapter) maximum queue length */
177 int unused
[2]; /* probably find a good use, set 0 for now */
178 } sg_scsi_id_t
; /* 32 bytes long on i386 */
180 typedef struct sg_req_info
{ /* used by SG_GET_REQUEST_TABLE ioctl() */
181 char req_state
; /* 0 -> not used, 1 -> written, 2 -> ready to read */
182 char orphan
; /* 0 -> normal request, 1 -> from interruped SG_IO */
183 char sg_io_owned
; /* 0 -> complete with read(), 1 -> owned by SG_IO */
184 char problem
; /* 0 -> no problem detected, 1 -> error to report */
185 int pack_id
; /* pack_id associated with request */
186 void * usr_ptr
; /* user provided pointer (in new interface) */
187 unsigned int duration
; /* millisecs elapsed since written (req_state==1)
188 or request duration (req_state==2) */
190 } sg_req_info_t
; /* 20 bytes long on i386 */
193 /* IOCTLs: Those ioctls that are relevant to the SG 3.x drivers follow.
194 [Those that only apply to the SG 2.x drivers are at the end of the file.]
195 (_GET_s yield result via 'int *' 3rd argument unless otherwise indicated) */
197 #define SG_EMULATED_HOST 0x2203 /* true for emulated host adapter (ATAPI) */
199 /* Used to configure SCSI command transformation layer for ATAPI devices */
200 /* Only supported by the ide-scsi driver */
201 #define SG_SET_TRANSFORM 0x2204 /* N.B. 3rd arg is not pointer but value: */
202 /* 3rd arg = 0 to disable transform, 1 to enable it */
203 #define SG_GET_TRANSFORM 0x2205
205 #define SG_SET_RESERVED_SIZE 0x2275 /* request a new reserved buffer size */
206 #define SG_GET_RESERVED_SIZE 0x2272 /* actual size of reserved buffer */
208 /* The following ioctl has a 'sg_scsi_id_t *' object as its 3rd argument. */
209 #define SG_GET_SCSI_ID 0x2276 /* Yields fd's bus, chan, dev, lun + type */
210 /* SCSI id information can also be obtained from SCSI_IOCTL_GET_IDLUN */
212 /* Override host setting and always DMA using low memory ( <16MB on i386) */
213 #define SG_SET_FORCE_LOW_DMA 0x2279 /* 0-> use adapter setting, 1-> force */
214 #define SG_GET_LOW_DMA 0x227a /* 0-> use all ram for dma; 1-> low dma ram */
216 /* When SG_SET_FORCE_PACK_ID set to 1, pack_id is input to read() which
217 tries to fetch a packet with a matching pack_id, waits, or returns EAGAIN.
218 If pack_id is -1 then read oldest waiting. When ...FORCE_PACK_ID set to 0
219 then pack_id ignored by read() and oldest readable fetched. */
220 #define SG_SET_FORCE_PACK_ID 0x227b
221 #define SG_GET_PACK_ID 0x227c /* Yields oldest readable pack_id (or -1) */
223 #define SG_GET_NUM_WAITING 0x227d /* Number of commands awaiting read() */
225 /* Yields max scatter gather tablesize allowed by current host adapter */
226 #define SG_GET_SG_TABLESIZE 0x227F /* 0 implies can't do scatter gather */
228 #define SG_GET_VERSION_NUM 0x2282 /* Example: version 2.1.34 yields 20134 */
230 /* Returns -EBUSY if occupied. 3rd argument pointer to int (see next) */
231 #define SG_SCSI_RESET 0x2284
232 /* Associated values that can be given to SG_SCSI_RESET follow */
233 #define SG_SCSI_RESET_NOTHING 0
234 #define SG_SCSI_RESET_DEVICE 1
235 #define SG_SCSI_RESET_BUS 2
236 #define SG_SCSI_RESET_HOST 3
238 /* synchronous SCSI command ioctl, (only in version 3 interface) */
239 #define SG_IO 0x2285 /* similar effect as write() followed by read() */
241 #define SG_GET_REQUEST_TABLE 0x2286 /* yields table of active requests */
243 /* How to treat EINTR during SG_IO ioctl(), only in SG 3.x series */
244 #define SG_SET_KEEP_ORPHAN 0x2287 /* 1 -> hold for read(), 0 -> drop (def) */
245 #define SG_GET_KEEP_ORPHAN 0x2288
248 #define SG_SCATTER_SZ (8 * 4096) /* PAGE_SIZE not available to user */
249 /* Largest size (in bytes) a single scatter-gather list element can have.
250 The value must be a power of 2 and <= (PAGE_SIZE * 32) [131072 bytes on
251 i386]. The minimum value is PAGE_SIZE. If scatter-gather not supported
252 by adapter then this value is the largest data block that can be
253 read/written by a single scsi command. The user can find the value of
254 PAGE_SIZE by calling getpagesize() defined in unistd.h . */
256 #define SG_DEFAULT_RETRIES 1
258 /* Defaults, commented if they differ from original sg driver */
259 #define SG_DEF_FORCE_LOW_DMA 0 /* was 1 -> memory below 16MB on i386 */
260 #define SG_DEF_FORCE_PACK_ID 0
261 #define SG_DEF_KEEP_ORPHAN 0
262 #define SG_DEF_RESERVED_SIZE SG_SCATTER_SZ /* load time option */
264 /* maximum outstanding requests, write() yields EDOM if exceeded */
265 #define SG_MAX_QUEUE 16
267 #define SG_BIG_BUFF SG_DEF_RESERVED_SIZE /* for backward compatibility */
269 /* Alternate style type names, "..._t" variants preferred */
270 typedef struct sg_io_hdr Sg_io_hdr
;
271 typedef struct sg_io_vec Sg_io_vec
;
272 typedef struct sg_scsi_id Sg_scsi_id
;
273 typedef struct sg_req_info Sg_req_info
;
276 /* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */
277 /* The older SG interface based on the 'sg_header' structure follows. */
278 /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
280 #define SG_MAX_SENSE 16 /* this only applies to the sg_header interface */
284 int pack_len
; /* [o] reply_len (ie useless), ignored as input */
285 int reply_len
; /* [i] max length of expected reply (inc. sg_header) */
286 int pack_id
; /* [io] id number of packet (use ints >= 0) */
287 int result
; /* [o] 0==ok, else (+ve) Unix errno (best ignored) */
288 unsigned int twelve_byte
:1;
289 /* [i] Force 12 byte command length for group 6 & 7 commands */
290 unsigned int target_status
:5; /* [o] scsi status from target */
291 unsigned int host_status
:8; /* [o] host status (see "DID" codes) */
292 unsigned int driver_status
:8; /* [o] driver status+suggestion */
293 unsigned int other_flags
:10; /* unused */
294 unsigned char sense_buffer
[SG_MAX_SENSE
]; /* [o] Output in 3 cases:
295 when target_status is CHECK_CONDITION or
296 when target_status is COMMAND_TERMINATED or
297 when (driver_status & DRIVER_SENSE) is true. */
298 }; /* This structure is 36 bytes long on i386 */
301 /* IOCTLs: The following are not required (or ignored) when the sg_io_hdr_t
302 interface is used. They are kept for backward compatibility with
303 the original and version 2 drivers. */
305 #define SG_SET_TIMEOUT 0x2201 /* unit: jiffies (10ms on i386) */
306 #define SG_GET_TIMEOUT 0x2202 /* yield timeout as _return_ value */
308 /* Get/set command queuing state per fd (default is SG_DEF_COMMAND_Q.
309 Each time a sg_io_hdr_t object is seen on this file descriptor, this
310 command queuing flag is set on (overriding the previous setting). */
311 #define SG_GET_COMMAND_Q 0x2270 /* Yields 0 (queuing off) or 1 (on) */
312 #define SG_SET_COMMAND_Q 0x2271 /* Change queuing state with 0 or 1 */
314 /* Turn on/off error sense trace (1 and 0 respectively, default is off).
315 Try using: "# cat /proc/scsi/sg/debug" instead in the v3 driver */
316 #define SG_SET_DEBUG 0x227e /* 0 -> turn off debug */
318 #define SG_NEXT_CMD_LEN 0x2283 /* override SCSI command length with given
319 number on the next write() on this file descriptor */
322 /* Defaults, commented if they differ from original sg driver */
323 #define SG_DEFAULT_TIMEOUT (60*HZ) /* HZ == 'jiffies in 1 second' */
324 #define SG_DEF_COMMAND_Q 0 /* command queuing is always on when
325 the new interface is used */
326 #define SG_DEF_UNDERRUN_FLAG 0