sg.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. /*
  15. History:
  16. Started: Aug 9 by Lawrence Foard (entropy@world.std.com), to allow user
  17. process control of SCSI devices.
  18. Development Sponsored by Killy Corp. NY NY
  19. */
  20. #ifndef _SCSI_SG_H
  21. #define _SCSI_SG_H 1
  22. #include <features.h>
  23. /* New interface introduced in the 3.x SG drivers follows */
  24. /* Same structure as used by readv() Linux system call. It defines one
  25. scatter-gather element. */
  26. typedef struct sg_iovec
  27. {
  28. void * iov_base; /* Starting address */
  29. size_t iov_len; /* Length in bytes */
  30. } sg_iovec_t;
  31. typedef struct sg_io_hdr
  32. {
  33. int interface_id; /* [i] 'S' for SCSI generic (required) */
  34. int dxfer_direction; /* [i] data transfer direction */
  35. unsigned char cmd_len; /* [i] SCSI command length ( <= 16 bytes) */
  36. unsigned char mx_sb_len; /* [i] max length to write to sbp */
  37. unsigned short int iovec_count; /* [i] 0 implies no scatter gather */
  38. unsigned int dxfer_len; /* [i] byte count of data transfer */
  39. void * dxferp; /* [i], [*io] points to data transfer memory
  40. or scatter gather list */
  41. unsigned char * cmdp; /* [i], [*i] points to command to perform */
  42. unsigned char * sbp; /* [i], [*o] points to sense_buffer memory */
  43. unsigned int timeout; /* [i] MAX_UINT->no timeout (unit: millisec) */
  44. unsigned int flags; /* [i] 0 -> default, see SG_FLAG... */
  45. int pack_id; /* [i->o] unused internally (normally) */
  46. void * usr_ptr; /* [i->o] unused internally */
  47. unsigned char status; /* [o] scsi status */
  48. unsigned char masked_status;/* [o] shifted, masked scsi status */
  49. unsigned char msg_status; /* [o] messaging level data (optional) */
  50. unsigned char sb_len_wr; /* [o] byte count actually written to sbp */
  51. unsigned short int host_status; /* [o] errors from host adapter */
  52. unsigned short int driver_status;/* [o] errors from software driver */
  53. int resid; /* [o] dxfer_len - actual_transferred */
  54. unsigned int duration; /* [o] time taken by cmd (unit: millisec) */
  55. unsigned int info; /* [o] auxiliary information */
  56. } sg_io_hdr_t;
  57. /* Use negative values to flag difference from original sg_header structure. */
  58. #define SG_DXFER_NONE -1 /* e.g. a SCSI Test Unit Ready command */
  59. #define SG_DXFER_TO_DEV -2 /* e.g. a SCSI WRITE command */
  60. #define SG_DXFER_FROM_DEV -3 /* e.g. a SCSI READ command */
  61. #define SG_DXFER_TO_FROM_DEV -4 /* treated like SG_DXFER_FROM_DEV with the
  62. additional property than during indirect
  63. IO the user buffer is copied into the
  64. kernel buffers before the transfer */
  65. /* following flag values can be "or"-ed together */
  66. #define SG_FLAG_DIRECT_IO 1 /* default is indirect IO */
  67. #define SG_FLAG_LUN_INHIBIT 2 /* default is to put device's lun into */
  68. /* the 2nd byte of SCSI command */
  69. #define SG_FLAG_NO_DXFER 0x10000 /* no transfer of kernel buffers to/from */
  70. /* user space (debug indirect IO) */
  71. /* The following 'info' values are "or"-ed together. */
  72. #define SG_INFO_OK_MASK 0x1
  73. #define SG_INFO_OK 0x0 /* no sense, host nor driver "noise" */
  74. #define SG_INFO_CHECK 0x1 /* something abnormal happened */
  75. #define SG_INFO_DIRECT_IO_MASK 0x6
  76. #define SG_INFO_INDIRECT_IO 0x0 /* data xfer via kernel buffers (or no xfer) */
  77. #define SG_INFO_DIRECT_IO 0x2 /* direct IO requested and performed */
  78. #define SG_INFO_MIXED_IO 0x4 /* part direct, part indirect IO */
  79. /* Request information about a specific SG device, used by
  80. SG_GET_SCSI_ID ioctl (). */
  81. struct sg_scsi_id {
  82. /* Host number as in "scsi<n>" where 'n' is one of 0, 1, 2 etc. */
  83. int host_no;
  84. int channel;
  85. /* SCSI id of target device. */
  86. int scsi_id;
  87. int lun;
  88. /* TYPE_... defined in <scsi/scsi.h>. */
  89. int scsi_type;
  90. /* Host (adapter) maximum commands per lun. */
  91. short int h_cmd_per_lun;
  92. /* Device (or adapter) maximum queue length. */
  93. short int d_queue_depth;
  94. /* Unused, set to 0 for now. */
  95. int unused[2];
  96. };
  97. /* Used by SG_GET_REQUEST_TABLE ioctl(). */
  98. typedef struct sg_req_info {
  99. char req_state; /* 0 -> not used, 1 -> written, 2 -> ready to read */
  100. char orphan; /* 0 -> normal request, 1 -> from interruped SG_IO */
  101. char sg_io_owned; /* 0 -> complete with read(), 1 -> owned by SG_IO */
  102. char problem; /* 0 -> no problem detected, 1 -> error to report */
  103. int pack_id; /* pack_id associated with request */
  104. void * usr_ptr; /* user provided pointer (in new interface) */
  105. unsigned int duration; /* millisecs elapsed since written (req_state==1)
  106. or request duration (req_state==2) */
  107. int unused;
  108. } sg_req_info_t;
  109. /* IOCTLs: Those ioctls that are relevant to the SG 3.x drivers follow.
  110. [Those that only apply to the SG 2.x drivers are at the end of the file.]
  111. (_GET_s yield result via 'int *' 3rd argument unless otherwise indicated) */
  112. #define SG_EMULATED_HOST 0x2203 /* true for emulated host adapter (ATAPI) */
  113. /* Used to configure SCSI command transformation layer for ATAPI devices */
  114. /* Only supported by the ide-scsi driver */
  115. #define SG_SET_TRANSFORM 0x2204 /* N.B. 3rd arg is not pointer but value: */
  116. /* 3rd arg = 0 to disable transform, 1 to enable it */
  117. #define SG_GET_TRANSFORM 0x2205
  118. #define SG_SET_RESERVED_SIZE 0x2275 /* request a new reserved buffer size */
  119. #define SG_GET_RESERVED_SIZE 0x2272 /* actual size of reserved buffer */
  120. /* The following ioctl has a 'sg_scsi_id_t *' object as its 3rd argument. */
  121. #define SG_GET_SCSI_ID 0x2276 /* Yields fd's bus, chan, dev, lun + type */
  122. /* SCSI id information can also be obtained from SCSI_IOCTL_GET_IDLUN */
  123. /* Override host setting and always DMA using low memory ( <16MB on i386) */
  124. #define SG_SET_FORCE_LOW_DMA 0x2279 /* 0-> use adapter setting, 1-> force */
  125. #define SG_GET_LOW_DMA 0x227a /* 0-> use all ram for dma; 1-> low dma ram */
  126. /* When SG_SET_FORCE_PACK_ID set to 1, pack_id is input to read() which
  127. tries to fetch a packet with a matching pack_id, waits, or returns EAGAIN.
  128. If pack_id is -1 then read oldest waiting. When ...FORCE_PACK_ID set to 0
  129. then pack_id ignored by read() and oldest readable fetched. */
  130. #define SG_SET_FORCE_PACK_ID 0x227b
  131. #define SG_GET_PACK_ID 0x227c /* Yields oldest readable pack_id (or -1) */
  132. #define SG_GET_NUM_WAITING 0x227d /* Number of commands awaiting read() */
  133. /* Yields max scatter gather tablesize allowed by current host adapter */
  134. #define SG_GET_SG_TABLESIZE 0x227F /* 0 implies can't do scatter gather */
  135. #define SG_GET_VERSION_NUM 0x2282 /* Example: version 2.1.34 yields 20134 */
  136. /* Returns -EBUSY if occupied. 3rd argument pointer to int (see next) */
  137. #define SG_SCSI_RESET 0x2284
  138. /* Associated values that can be given to SG_SCSI_RESET follow */
  139. #define SG_SCSI_RESET_NOTHING 0
  140. #define SG_SCSI_RESET_DEVICE 1
  141. #define SG_SCSI_RESET_BUS 2
  142. #define SG_SCSI_RESET_HOST 3
  143. /* synchronous SCSI command ioctl, (only in version 3 interface) */
  144. #define SG_IO 0x2285 /* similar effect as write() followed by read() */
  145. #define SG_GET_REQUEST_TABLE 0x2286 /* yields table of active requests */
  146. /* How to treat EINTR during SG_IO ioctl(), only in SG 3.x series */
  147. #define SG_SET_KEEP_ORPHAN 0x2287 /* 1 -> hold for read(), 0 -> drop (def) */
  148. #define SG_GET_KEEP_ORPHAN 0x2288
  149. #define SG_SCATTER_SZ (8 * 4096) /* PAGE_SIZE not available to user */
  150. /* Largest size (in bytes) a single scatter-gather list element can have.
  151. The value must be a power of 2 and <= (PAGE_SIZE * 32) [131072 bytes on
  152. i386]. The minimum value is PAGE_SIZE. If scatter-gather not supported
  153. by adapter then this value is the largest data block that can be
  154. read/written by a single scsi command. The user can find the value of
  155. PAGE_SIZE by calling getpagesize() defined in unistd.h . */
  156. #define SG_DEFAULT_RETRIES 1
  157. /* Defaults, commented if they differ from original sg driver */
  158. #define SG_DEF_FORCE_LOW_DMA 0 /* was 1 -> memory below 16MB on i386 */
  159. #define SG_DEF_FORCE_PACK_ID 0
  160. #define SG_DEF_KEEP_ORPHAN 0
  161. #define SG_DEF_RESERVED_SIZE SG_SCATTER_SZ /* load time option */
  162. /* maximum outstanding requests, write() yields EDOM if exceeded */
  163. #define SG_MAX_QUEUE 16
  164. #define SG_BIG_BUFF SG_DEF_RESERVED_SIZE /* for backward compatibility */
  165. /* Alternate style type names, "..._t" variants preferred */
  166. typedef struct sg_io_hdr Sg_io_hdr;
  167. typedef struct sg_io_vec Sg_io_vec;
  168. typedef struct sg_scsi_id Sg_scsi_id;
  169. typedef struct sg_req_info Sg_req_info;
  170. /* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */
  171. /* The older SG interface based on the 'sg_header' structure follows. */
  172. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  173. #define SG_MAX_SENSE 16 /* this only applies to the sg_header interface */
  174. struct sg_header
  175. {
  176. /* Length of incoming packet (including header). */
  177. int pack_len;
  178. /* Maximal length of expected reply. */
  179. int reply_len;
  180. /* Id number of packet. */
  181. int pack_id;
  182. /* 0==ok, otherwise error number. */
  183. int result;
  184. /* Force 12 byte command length for group 6 & 7 commands. */
  185. unsigned int twelve_byte:1;
  186. /* SCSI status from target. */
  187. unsigned int target_status:5;
  188. /* Host status (see "DID" codes). */
  189. unsigned int host_status:8;
  190. /* Driver status+suggestion. */
  191. unsigned int driver_status:8;
  192. /* Unused. */
  193. unsigned int other_flags:10;
  194. /* Output in 3 cases:
  195. when target_status is CHECK_CONDITION or
  196. when target_status is COMMAND_TERMINATED or
  197. when (driver_status & DRIVER_SENSE) is true. */
  198. unsigned char sense_buffer[SG_MAX_SENSE];
  199. };
  200. /* IOCTLs: The following are not required (or ignored) when the sg_io_hdr_t
  201. interface is used. They are kept for backward compatibility with
  202. the original and version 2 drivers. */
  203. #define SG_SET_TIMEOUT 0x2201 /* Set timeout; *(int *)arg==timeout. */
  204. #define SG_GET_TIMEOUT 0x2202 /* Get timeout; return timeout. */
  205. /* Get/set command queuing state per fd (default is SG_DEF_COMMAND_Q). */
  206. #define SG_GET_COMMAND_Q 0x2270 /* Yields 0 (queuing off) or 1 (on). */
  207. #define SG_SET_COMMAND_Q 0x2271 /* Change queuing state with 0 or 1. */
  208. /* Turn on error sense trace (1..8), dump this device to log/console (9)
  209. or dump all sg device states ( >9 ) to log/console. */
  210. #define SG_SET_DEBUG 0x227e /* 0 -> turn off debug */
  211. #define SG_NEXT_CMD_LEN 0x2283 /* Override SCSI command length with given
  212. number on the next write() on this file
  213. descriptor. */
  214. /* Defaults, commented if they differ from original sg driver */
  215. #define SG_DEFAULT_TIMEOUT (60*HZ) /* HZ == 'jiffies in 1 second' */
  216. #define SG_DEF_COMMAND_Q 0 /* command queuing is always on when
  217. the new interface is used */
  218. #define SG_DEF_UNDERRUN_FLAG 0
  219. #endif /* scsi/sg.h */