Back to index

glibc  2.9
sg.h
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 1998, 1999, 2000  Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 /*
00020    History:
00021     Started: Aug 9 by Lawrence Foard (entropy@world.std.com), to allow user
00022      process control of SCSI devices.
00023     Development Sponsored by Killy Corp. NY NY
00024 */
00025 
00026 #ifndef _SCSI_SG_H
00027 #define _SCSI_SG_H   1
00028 
00029 #include <features.h>
00030 
00031 
00032 /* New interface introduced in the 3.x SG drivers follows */
00033 
00034 /* Same structure as used by readv() Linux system call. It defines one
00035    scatter-gather element. */
00036 typedef struct sg_iovec
00037 {
00038   void * iov_base;            /* Starting address  */
00039   size_t iov_len;             /* Length in bytes  */
00040 } sg_iovec_t;
00041 
00042 
00043 typedef struct sg_io_hdr
00044 {
00045   int interface_id;           /* [i] 'S' for SCSI generic (required) */
00046   int dxfer_direction;        /* [i] data transfer direction  */
00047   unsigned char cmd_len;      /* [i] SCSI command length ( <= 16 bytes) */
00048   unsigned char mx_sb_len;    /* [i] max length to write to sbp */
00049   unsigned short int iovec_count; /* [i] 0 implies no scatter gather */
00050   unsigned int dxfer_len;     /* [i] byte count of data transfer */
00051   void * dxferp;              /* [i], [*io] points to data transfer memory
00052                              or scatter gather list */
00053   unsigned char * cmdp;       /* [i], [*i] points to command to perform */
00054   unsigned char * sbp;        /* [i], [*o] points to sense_buffer memory */
00055   unsigned int timeout;       /* [i] MAX_UINT->no timeout (unit: millisec) */
00056   unsigned int flags;         /* [i] 0 -> default, see SG_FLAG... */
00057   int pack_id;                /* [i->o] unused internally (normally) */
00058   void * usr_ptr;             /* [i->o] unused internally */
00059   unsigned char status;       /* [o] scsi status */
00060   unsigned char masked_status;/* [o] shifted, masked scsi status */
00061   unsigned char msg_status;   /* [o] messaging level data (optional) */
00062   unsigned char sb_len_wr;    /* [o] byte count actually written to sbp */
00063   unsigned short int host_status; /* [o] errors from host adapter */
00064   unsigned short int driver_status;/* [o] errors from software driver */
00065   int resid;                  /* [o] dxfer_len - actual_transferred */
00066   unsigned int duration;      /* [o] time taken by cmd (unit: millisec) */
00067   unsigned int info;          /* [o] auxiliary information */
00068 } sg_io_hdr_t;
00069 
00070 
00071 /* Use negative values to flag difference from original sg_header structure.  */
00072 #define SG_DXFER_NONE -1        /* e.g. a SCSI Test Unit Ready command */
00073 #define SG_DXFER_TO_DEV -2      /* e.g. a SCSI WRITE command */
00074 #define SG_DXFER_FROM_DEV -3    /* e.g. a SCSI READ command */
00075 #define SG_DXFER_TO_FROM_DEV -4 /* treated like SG_DXFER_FROM_DEV with the
00076                                additional property than during indirect
00077                                IO the user buffer is copied into the
00078                                kernel buffers before the transfer */
00079 
00080 
00081 /* following flag values can be "or"-ed together */
00082 #define SG_FLAG_DIRECT_IO 1     /* default is indirect IO */
00083 #define SG_FLAG_LUN_INHIBIT 2   /* default is to put device's lun into */
00084                             /* the 2nd byte of SCSI command */
00085 #define SG_FLAG_NO_DXFER 0x10000 /* no transfer of kernel buffers to/from */
00086                             /* user space (debug indirect IO) */
00087 
00088 /* The following 'info' values are "or"-ed together.  */
00089 #define SG_INFO_OK_MASK     0x1
00090 #define SG_INFO_OK   0x0    /* no sense, host nor driver "noise" */
00091 #define SG_INFO_CHECK       0x1     /* something abnormal happened */
00092 
00093 #define SG_INFO_DIRECT_IO_MASK     0x6
00094 #define SG_INFO_INDIRECT_IO        0x0    /* data xfer via kernel buffers (or no xfer) */
00095 #define SG_INFO_DIRECT_IO   0x2    /* direct IO requested and performed */
00096 #define SG_INFO_MIXED_IO    0x4    /* part direct, part indirect IO */
00097 
00098 
00099 /* Request information about a specific SG device, used by
00100    SG_GET_SCSI_ID ioctl ().  */
00101 struct sg_scsi_id {
00102   /* Host number as in "scsi<n>" where 'n' is one of 0, 1, 2 etc.  */
00103   int host_no;
00104   int channel;
00105   /* SCSI id of target device.  */
00106   int scsi_id;
00107   int lun;
00108   /* TYPE_... defined in <scsi/scsi.h>.  */
00109   int scsi_type;
00110   /* Host (adapter) maximum commands per lun.  */
00111   short int h_cmd_per_lun;
00112   /* Device (or adapter) maximum queue length.  */
00113   short int d_queue_depth;
00114   /* Unused, set to 0 for now.  */
00115   int unused[2];
00116 };
00117 
00118 /* Used by SG_GET_REQUEST_TABLE ioctl().  */
00119 typedef struct sg_req_info {
00120     char req_state;     /* 0 -> not used, 1 -> written, 2 -> ready to read */
00121     char orphan;        /* 0 -> normal request, 1 -> from interruped SG_IO */
00122     char sg_io_owned;   /* 0 -> complete with read(), 1 -> owned by SG_IO */
00123     char problem;       /* 0 -> no problem detected, 1 -> error to report */
00124     int pack_id;        /* pack_id associated with request */
00125     void * usr_ptr;     /* user provided pointer (in new interface) */
00126     unsigned int duration; /* millisecs elapsed since written (req_state==1)
00127                            or request duration (req_state==2) */
00128     int unused;
00129 } sg_req_info_t;
00130 
00131 
00132 /* IOCTLs: Those ioctls that are relevant to the SG 3.x drivers follow.
00133  [Those that only apply to the SG 2.x drivers are at the end of the file.]
00134  (_GET_s yield result via 'int *' 3rd argument unless otherwise indicated) */
00135 
00136 #define SG_EMULATED_HOST 0x2203 /* true for emulated host adapter (ATAPI) */
00137 
00138 /* Used to configure SCSI command transformation layer for ATAPI devices */
00139 /* Only supported by the ide-scsi driver */
00140 #define SG_SET_TRANSFORM 0x2204 /* N.B. 3rd arg is not pointer but value: */
00141                     /* 3rd arg = 0 to disable transform, 1 to enable it */
00142 #define SG_GET_TRANSFORM 0x2205
00143 
00144 #define SG_SET_RESERVED_SIZE 0x2275  /* request a new reserved buffer size */
00145 #define SG_GET_RESERVED_SIZE 0x2272  /* actual size of reserved buffer */
00146 
00147 /* The following ioctl has a 'sg_scsi_id_t *' object as its 3rd argument. */
00148 #define SG_GET_SCSI_ID 0x2276   /* Yields fd's bus, chan, dev, lun + type */
00149 /* SCSI id information can also be obtained from SCSI_IOCTL_GET_IDLUN */
00150 
00151 /* Override host setting and always DMA using low memory ( <16MB on i386) */
00152 #define SG_SET_FORCE_LOW_DMA 0x2279  /* 0-> use adapter setting, 1-> force */
00153 #define SG_GET_LOW_DMA 0x227a   /* 0-> use all ram for dma; 1-> low dma ram */
00154 
00155 /* When SG_SET_FORCE_PACK_ID set to 1, pack_id is input to read() which
00156    tries to fetch a packet with a matching pack_id, waits, or returns EAGAIN.
00157    If pack_id is -1 then read oldest waiting. When ...FORCE_PACK_ID set to 0
00158    then pack_id ignored by read() and oldest readable fetched. */
00159 #define SG_SET_FORCE_PACK_ID 0x227b
00160 #define SG_GET_PACK_ID 0x227c /* Yields oldest readable pack_id (or -1) */
00161 
00162 #define SG_GET_NUM_WAITING 0x227d /* Number of commands awaiting read() */
00163 
00164 /* Yields max scatter gather tablesize allowed by current host adapter */
00165 #define SG_GET_SG_TABLESIZE 0x227F  /* 0 implies can't do scatter gather */
00166 
00167 #define SG_GET_VERSION_NUM 0x2282 /* Example: version 2.1.34 yields 20134 */
00168 
00169 /* Returns -EBUSY if occupied. 3rd argument pointer to int (see next) */
00170 #define SG_SCSI_RESET 0x2284
00171 /* Associated values that can be given to SG_SCSI_RESET follow */
00172 #define SG_SCSI_RESET_NOTHING      0
00173 #define SG_SCSI_RESET_DEVICE       1
00174 #define SG_SCSI_RESET_BUS   2
00175 #define SG_SCSI_RESET_HOST  3
00176 
00177 /* synchronous SCSI command ioctl, (only in version 3 interface) */
00178 #define SG_IO 0x2285   /* similar effect as write() followed by read() */
00179 
00180 #define SG_GET_REQUEST_TABLE 0x2286   /* yields table of active requests */
00181 
00182 /* How to treat EINTR during SG_IO ioctl(), only in SG 3.x series */
00183 #define SG_SET_KEEP_ORPHAN 0x2287 /* 1 -> hold for read(), 0 -> drop (def) */
00184 #define SG_GET_KEEP_ORPHAN 0x2288
00185 
00186 
00187 #define SG_SCATTER_SZ (8 * 4096)  /* PAGE_SIZE not available to user */
00188 /* Largest size (in bytes) a single scatter-gather list element can have.
00189    The value must be a power of 2 and <= (PAGE_SIZE * 32) [131072 bytes on
00190    i386]. The minimum value is PAGE_SIZE. If scatter-gather not supported
00191    by adapter then this value is the largest data block that can be
00192    read/written by a single scsi command. The user can find the value of
00193    PAGE_SIZE by calling getpagesize() defined in unistd.h . */
00194 
00195 #define SG_DEFAULT_RETRIES 1
00196 
00197 /* Defaults, commented if they differ from original sg driver */
00198 #define SG_DEF_FORCE_LOW_DMA 0  /* was 1 -> memory below 16MB on i386 */
00199 #define SG_DEF_FORCE_PACK_ID 0
00200 #define SG_DEF_KEEP_ORPHAN 0
00201 #define SG_DEF_RESERVED_SIZE SG_SCATTER_SZ /* load time option */
00202 
00203 /* maximum outstanding requests, write() yields EDOM if exceeded */
00204 #define SG_MAX_QUEUE 16
00205 
00206 #define SG_BIG_BUFF SG_DEF_RESERVED_SIZE    /* for backward compatibility */
00207 
00208 /* Alternate style type names, "..._t" variants preferred */
00209 typedef struct sg_io_hdr Sg_io_hdr;
00210 typedef struct sg_io_vec Sg_io_vec;
00211 typedef struct sg_scsi_id Sg_scsi_id;
00212 typedef struct sg_req_info Sg_req_info;
00213 
00214 
00215 /* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */
00216 /*   The older SG interface based on the 'sg_header' structure follows.   */
00217 /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
00218 
00219 #define SG_MAX_SENSE 16   /* this only applies to the sg_header interface */
00220 
00221 struct sg_header
00222  {
00223    /* Length of incoming packet (including header).  */
00224    int pack_len;
00225    /* Maximal length of expected reply.  */
00226    int reply_len;
00227    /* Id number of packet.  */
00228    int pack_id;
00229    /* 0==ok, otherwise error number.  */
00230    int result;
00231    /* Force 12 byte command length for group 6 & 7 commands.  */
00232    unsigned int twelve_byte:1;
00233    /* SCSI status from target.  */
00234    unsigned int target_status:5;
00235    /* Host status (see "DID" codes).  */
00236    unsigned int host_status:8;
00237    /* Driver status+suggestion.  */
00238    unsigned int driver_status:8;
00239    /* Unused.  */
00240    unsigned int other_flags:10;
00241    /* Output in 3 cases:
00242       when target_status is CHECK_CONDITION or
00243       when target_status is COMMAND_TERMINATED or
00244       when (driver_status & DRIVER_SENSE) is true.  */
00245    unsigned char sense_buffer[SG_MAX_SENSE];
00246  };
00247 
00248 
00249 /* IOCTLs: The following are not required (or ignored) when the sg_io_hdr_t
00250           interface is used. They are kept for backward compatibility with
00251           the original and version 2 drivers. */
00252 
00253 #define SG_SET_TIMEOUT             0x2201 /* Set timeout; *(int *)arg==timeout.  */
00254 #define SG_GET_TIMEOUT             0x2202 /* Get timeout; return timeout.  */
00255 
00256 /* Get/set command queuing state per fd (default is SG_DEF_COMMAND_Q). */
00257 #define SG_GET_COMMAND_Q    0x2270 /* Yields 0 (queuing off) or 1 (on).  */
00258 #define SG_SET_COMMAND_Q    0x2271 /* Change queuing state with 0 or 1.  */
00259 
00260 /* Turn on error sense trace (1..8), dump this device to log/console (9)
00261    or dump all sg device states ( >9 ) to log/console.  */
00262 #define SG_SET_DEBUG        0x227e /* 0 -> turn off debug */
00263 
00264 #define SG_NEXT_CMD_LEN            0x2283 /* Override SCSI command length with given
00265                                       number on the next write() on this file
00266                                       descriptor.  */
00267 
00268 /* Defaults, commented if they differ from original sg driver */
00269 #define SG_DEFAULT_TIMEOUT (60*HZ) /* HZ == 'jiffies in 1 second' */
00270 #define SG_DEF_COMMAND_Q 0     /* command queuing is always on when
00271                               the new interface is used */
00272 #define SG_DEF_UNDERRUN_FLAG 0
00273 
00274 
00275 #endif /* scsi/sg.h */