Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _SCSI_GENERIC_H
0003 #define _SCSI_GENERIC_H
0004 
0005 #include <linux/compiler.h>
0006 
0007 /*
0008  * History:
0009  *  Started: Aug 9 by Lawrence Foard (entropy@world.std.com), to allow user
0010  *   process control of SCSI devices.
0011  *  Development Sponsored by Killy Corp. NY NY
0012  *
0013  * Original driver (sg.h):
0014  *       Copyright (C) 1992 Lawrence Foard
0015  * Version 2 and 3 extensions to driver:
0016  *  Copyright (C) 1998 - 2014 Douglas Gilbert
0017  *
0018  *  Version: 3.5.36 (20140603)
0019  *  This version is for 2.6 and 3 series kernels.
0020  *
0021  * Documentation
0022  * =============
0023  * A web site for the SG device driver can be found at:
0024  *  http://sg.danny.cz/sg  [alternatively check the MAINTAINERS file]
0025  * The documentation for the sg version 3 driver can be found at:
0026  *  http://sg.danny.cz/sg/p/sg_v3_ho.html
0027  * Also see: <kernel_source>/Documentation/scsi/scsi-generic.rst
0028  *
0029  * For utility and test programs see: http://sg.danny.cz/sg/sg3_utils.html
0030  */
0031 
0032 
0033 typedef struct sg_iovec /* same structure as used by readv() Linux system */
0034 {                       /* call. It defines one scatter-gather element. */
0035     void __user *iov_base;      /* Starting address  */
0036     size_t iov_len;             /* Length in bytes  */
0037 } sg_iovec_t;
0038 
0039 
0040 typedef struct sg_io_hdr
0041 {
0042     int interface_id;           /* [i] 'S' for SCSI generic (required) */
0043     int dxfer_direction;        /* [i] data transfer direction  */
0044     unsigned char cmd_len;      /* [i] SCSI command length */
0045     unsigned char mx_sb_len;    /* [i] max length to write to sbp */
0046     unsigned short iovec_count; /* [i] 0 implies no scatter gather */
0047     unsigned int dxfer_len;     /* [i] byte count of data transfer */
0048     void __user *dxferp;    /* [i], [*io] points to data transfer memory
0049                           or scatter gather list */
0050     unsigned char __user *cmdp; /* [i], [*i] points to command to perform */
0051     void __user *sbp;       /* [i], [*o] points to sense_buffer memory */
0052     unsigned int timeout;       /* [i] MAX_UINT->no timeout (unit: millisec) */
0053     unsigned int flags;         /* [i] 0 -> default, see SG_FLAG... */
0054     int pack_id;                /* [i->o] unused internally (normally) */
0055     void __user * usr_ptr;      /* [i->o] unused internally */
0056     unsigned char status;       /* [o] scsi status */
0057     unsigned char masked_status;/* [o] shifted, masked scsi status */
0058     unsigned char msg_status;   /* [o] messaging level data (optional) */
0059     unsigned char sb_len_wr;    /* [o] byte count actually written to sbp */
0060     unsigned short host_status; /* [o] errors from host adapter */
0061     unsigned short driver_status;/* [o] errors from software driver */
0062     int resid;                  /* [o] dxfer_len - actual_transferred */
0063     unsigned int duration;      /* [o] time taken by cmd (unit: millisec) */
0064     unsigned int info;          /* [o] auxiliary information */
0065 } sg_io_hdr_t;  /* 64 bytes long (on i386) */
0066 
0067 #if defined(__KERNEL__)
0068 #include <linux/compat.h>
0069 
0070 struct compat_sg_io_hdr {
0071     compat_int_t interface_id;  /* [i] 'S' for SCSI generic (required) */
0072     compat_int_t dxfer_direction;   /* [i] data transfer direction  */
0073     unsigned char cmd_len;      /* [i] SCSI command length ( <= 16 bytes) */
0074     unsigned char mx_sb_len;    /* [i] max length to write to sbp */
0075     unsigned short iovec_count; /* [i] 0 implies no scatter gather */
0076     compat_uint_t dxfer_len;    /* [i] byte count of data transfer */
0077     compat_uint_t dxferp;       /* [i], [*io] points to data transfer memory
0078                         or scatter gather list */
0079     compat_uptr_t cmdp;     /* [i], [*i] points to command to perform */
0080     compat_uptr_t sbp;      /* [i], [*o] points to sense_buffer memory */
0081     compat_uint_t timeout;      /* [i] MAX_UINT->no timeout (unit: millisec) */
0082     compat_uint_t flags;        /* [i] 0 -> default, see SG_FLAG... */
0083     compat_int_t pack_id;       /* [i->o] unused internally (normally) */
0084     compat_uptr_t usr_ptr;      /* [i->o] unused internally */
0085     unsigned char status;       /* [o] scsi status */
0086     unsigned char masked_status;    /* [o] shifted, masked scsi status */
0087     unsigned char msg_status;   /* [o] messaging level data (optional) */
0088     unsigned char sb_len_wr;    /* [o] byte count actually written to sbp */
0089     unsigned short host_status; /* [o] errors from host adapter */
0090     unsigned short driver_status;   /* [o] errors from software driver */
0091     compat_int_t resid;     /* [o] dxfer_len - actual_transferred */
0092     compat_uint_t duration;     /* [o] time taken by cmd (unit: millisec) */
0093     compat_uint_t info;     /* [o] auxiliary information */
0094 };
0095 #endif
0096 
0097 #define SG_INTERFACE_ID_ORIG 'S'
0098 
0099 /* Use negative values to flag difference from original sg_header structure */
0100 #define SG_DXFER_NONE (-1)      /* e.g. a SCSI Test Unit Ready command */
0101 #define SG_DXFER_TO_DEV (-2)    /* e.g. a SCSI WRITE command */
0102 #define SG_DXFER_FROM_DEV (-3)  /* e.g. a SCSI READ command */
0103 #define SG_DXFER_TO_FROM_DEV (-4) /* treated like SG_DXFER_FROM_DEV with the
0104                    additional property than during indirect
0105                    IO the user buffer is copied into the
0106                    kernel buffers before the transfer */
0107 #define SG_DXFER_UNKNOWN (-5)   /* Unknown data direction */
0108 
0109 /* following flag values can be "or"-ed together */
0110 #define SG_FLAG_DIRECT_IO 1     /* default is indirect IO */
0111 #define SG_FLAG_UNUSED_LUN_INHIBIT 2   /* default is overwrite lun in SCSI */
0112                 /* command block (when <= SCSI_2) */
0113 #define SG_FLAG_MMAP_IO 4       /* request memory mapped IO */
0114 #define SG_FLAG_NO_DXFER 0x10000 /* no transfer of kernel buffers to/from */
0115                 /* user space (debug indirect IO) */
0116 /* defaults:: for sg driver: Q_AT_HEAD; for block layer: Q_AT_TAIL */
0117 #define SG_FLAG_Q_AT_TAIL 0x10
0118 #define SG_FLAG_Q_AT_HEAD 0x20
0119 
0120 /* following 'info' values are "or"-ed together */
0121 #define SG_INFO_OK_MASK 0x1
0122 #define SG_INFO_OK 0x0          /* no sense, host nor driver "noise" */
0123 #define SG_INFO_CHECK 0x1       /* something abnormal happened */
0124 
0125 #define SG_INFO_DIRECT_IO_MASK 0x6
0126 #define SG_INFO_INDIRECT_IO 0x0 /* data xfer via kernel buffers (or no xfer) */
0127 #define SG_INFO_DIRECT_IO 0x2   /* direct IO requested and performed */
0128 #define SG_INFO_MIXED_IO 0x4    /* part direct, part indirect IO */
0129 
0130 /*
0131  * Obsolete DRIVER_SENSE driver byte
0132  *
0133  * Originally the SCSI midlayer would set the DRIVER_SENSE driver byte when
0134  * a sense code was generated and a sense buffer was allocated.
0135  * However, as nowadays every scsi command has a sense code allocated this
0136  * distinction became moot as one could check the sense buffer directly.
0137  * Consequently this byte is not set anymore from the midlayer, but SG will
0138  * keep setting this byte to be compatible with previous releases.
0139  */
0140 #define DRIVER_SENSE 0x08
0141 /* Obsolete driver_byte() declaration */
0142 #define driver_byte(result) (((result) >> 24) & 0xff)
0143 
0144 /*
0145  *  Original linux SCSI Status codes. They are shifted 1 bit right
0146  *  from those found in the SCSI standards.
0147  */
0148 
0149 #define GOOD                 0x00
0150 #define CHECK_CONDITION      0x01
0151 #define CONDITION_GOOD       0x02
0152 #define BUSY                 0x04
0153 #define INTERMEDIATE_GOOD    0x08
0154 #define INTERMEDIATE_C_GOOD  0x0a
0155 #define RESERVATION_CONFLICT 0x0c
0156 #define COMMAND_TERMINATED   0x11
0157 #define QUEUE_FULL           0x14
0158 #define ACA_ACTIVE           0x18
0159 #define TASK_ABORTED         0x20
0160 
0161 /* Obsolete status_byte() declaration */
0162 #define status_byte(result) (((result) >> 1) & 0x7f)
0163 
0164 typedef struct sg_scsi_id { /* used by SG_GET_SCSI_ID ioctl() */
0165     int host_no;        /* as in "scsi<n>" where 'n' is one of 0, 1, 2 etc */
0166     int channel;
0167     int scsi_id;        /* scsi id of target device */
0168     int lun;
0169     int scsi_type;      /* TYPE_... defined in scsi/scsi.h */
0170     short h_cmd_per_lun;/* host (adapter) maximum commands per lun */
0171     short d_queue_depth;/* device (or adapter) maximum queue length */
0172     int unused[2];      /* probably find a good use, set 0 for now */
0173 } sg_scsi_id_t; /* 32 bytes long on i386 */
0174 
0175 typedef struct sg_req_info { /* used by SG_GET_REQUEST_TABLE ioctl() */
0176     char req_state;     /* 0 -> not used, 1 -> written, 2 -> ready to read */
0177     char orphan;        /* 0 -> normal request, 1 -> from interrupted SG_IO */
0178     char sg_io_owned;   /* 0 -> complete with read(), 1 -> owned by SG_IO */
0179     char problem;       /* 0 -> no problem detected, 1 -> error to report */
0180     int pack_id;        /* pack_id associated with request */
0181     void __user *usr_ptr;     /* user provided pointer (in new interface) */
0182     unsigned int duration; /* millisecs elapsed since written (req_state==1)
0183                   or request duration (req_state==2) */
0184     int unused;
0185 } sg_req_info_t; /* 20 bytes long on i386 */
0186 
0187 
0188 /* IOCTLs: Those ioctls that are relevant to the SG 3.x drivers follow.
0189  [Those that only apply to the SG 2.x drivers are at the end of the file.]
0190  (_GET_s yield result via 'int *' 3rd argument unless otherwise indicated) */
0191 
0192 #define SG_EMULATED_HOST 0x2203 /* true for emulated host adapter (ATAPI) */
0193 
0194 /* Used to configure SCSI command transformation layer for ATAPI devices */
0195 /* Only supported by the ide-scsi driver */
0196 #define SG_SET_TRANSFORM 0x2204 /* N.B. 3rd arg is not pointer but value: */
0197               /* 3rd arg = 0 to disable transform, 1 to enable it */
0198 #define SG_GET_TRANSFORM 0x2205
0199 
0200 #define SG_SET_RESERVED_SIZE 0x2275  /* request a new reserved buffer size */
0201 #define SG_GET_RESERVED_SIZE 0x2272  /* actual size of reserved buffer */
0202 
0203 /* The following ioctl has a 'sg_scsi_id_t *' object as its 3rd argument. */
0204 #define SG_GET_SCSI_ID 0x2276   /* Yields fd's bus, chan, dev, lun + type */
0205 /* SCSI id information can also be obtained from SCSI_IOCTL_GET_IDLUN */
0206 
0207 /* Override host setting and always DMA using low memory ( <16MB on i386) */
0208 #define SG_SET_FORCE_LOW_DMA 0x2279  /* 0-> use adapter setting, 1-> force */
0209 #define SG_GET_LOW_DMA 0x227a   /* 0-> use all ram for dma; 1-> low dma ram */
0210 
0211 /* When SG_SET_FORCE_PACK_ID set to 1, pack_id is input to read() which
0212    tries to fetch a packet with a matching pack_id, waits, or returns EAGAIN.
0213    If pack_id is -1 then read oldest waiting. When ...FORCE_PACK_ID set to 0
0214    then pack_id ignored by read() and oldest readable fetched. */
0215 #define SG_SET_FORCE_PACK_ID 0x227b
0216 #define SG_GET_PACK_ID 0x227c /* Yields oldest readable pack_id (or -1) */
0217 
0218 #define SG_GET_NUM_WAITING 0x227d /* Number of commands awaiting read() */
0219 
0220 /* Yields max scatter gather tablesize allowed by current host adapter */
0221 #define SG_GET_SG_TABLESIZE 0x227F  /* 0 implies can't do scatter gather */
0222 
0223 #define SG_GET_VERSION_NUM 0x2282 /* Example: version 2.1.34 yields 20134 */
0224 
0225 /* Returns -EBUSY if occupied. 3rd argument pointer to int (see next) */
0226 #define SG_SCSI_RESET 0x2284
0227 /* Associated values that can be given to SG_SCSI_RESET follow.
0228  * SG_SCSI_RESET_NO_ESCALATE may be OR-ed to the _DEVICE, _TARGET, _BUS
0229  * or _HOST reset value so only that action is attempted. */
0230 #define     SG_SCSI_RESET_NOTHING   0
0231 #define     SG_SCSI_RESET_DEVICE    1
0232 #define     SG_SCSI_RESET_BUS   2
0233 #define     SG_SCSI_RESET_HOST  3
0234 #define     SG_SCSI_RESET_TARGET    4
0235 #define     SG_SCSI_RESET_NO_ESCALATE   0x100
0236 
0237 /* synchronous SCSI command ioctl, (only in version 3 interface) */
0238 #define SG_IO 0x2285   /* similar effect as write() followed by read() */
0239 
0240 #define SG_GET_REQUEST_TABLE 0x2286   /* yields table of active requests */
0241 
0242 /* How to treat EINTR during SG_IO ioctl(), only in SG 3.x series */
0243 #define SG_SET_KEEP_ORPHAN 0x2287 /* 1 -> hold for read(), 0 -> drop (def) */
0244 #define SG_GET_KEEP_ORPHAN 0x2288
0245 
0246 /* yields scsi midlevel's access_count for this SCSI device */
0247 #define SG_GET_ACCESS_COUNT 0x2289  
0248 
0249 
0250 #define SG_SCATTER_SZ (8 * 4096)
0251 /* Largest size (in bytes) a single scatter-gather list element can have.
0252    The value used by the driver is 'max(SG_SCATTER_SZ, PAGE_SIZE)'.
0253    This value should be a power of 2 (and may be rounded up internally).
0254    If scatter-gather is not supported by adapter then this value is the
0255    largest data block that can be read/written by a single scsi command. */
0256 
0257 #define SG_DEFAULT_RETRIES 0
0258 
0259 /* Defaults, commented if they differ from original sg driver */
0260 #define SG_DEF_FORCE_PACK_ID 0
0261 #define SG_DEF_KEEP_ORPHAN 0
0262 #define SG_DEF_RESERVED_SIZE SG_SCATTER_SZ /* load time option */
0263 
0264 /* maximum outstanding requests, write() yields EDOM if exceeded */
0265 #define SG_MAX_QUEUE 16
0266 
0267 #define SG_BIG_BUFF SG_DEF_RESERVED_SIZE    /* for backward compatibility */
0268 
0269 /* Alternate style type names, "..._t" variants preferred */
0270 typedef struct sg_io_hdr Sg_io_hdr;
0271 typedef struct sg_io_vec Sg_io_vec;
0272 typedef struct sg_scsi_id Sg_scsi_id;
0273 typedef struct sg_req_info Sg_req_info;
0274 
0275 
0276 /* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */
0277 /*   The older SG interface based on the 'sg_header' structure follows.   */
0278 /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
0279 
0280 #define SG_MAX_SENSE 16   /* this only applies to the sg_header interface */
0281 
0282 struct sg_header
0283 {
0284     int pack_len;    /* [o] reply_len (ie useless), ignored as input */
0285     int reply_len;   /* [i] max length of expected reply (inc. sg_header) */
0286     int pack_id;     /* [io] id number of packet (use ints >= 0) */
0287     int result;      /* [o] 0==ok, else (+ve) Unix errno (best ignored) */
0288     unsigned int twelve_byte:1;
0289     /* [i] Force 12 byte command length for group 6 & 7 commands  */
0290     unsigned int target_status:5;   /* [o] scsi status from target */
0291     unsigned int host_status:8;     /* [o] host status (see "DID" codes) */
0292     unsigned int driver_status:8;   /* [o] driver status+suggestion */
0293     unsigned int other_flags:10;    /* unused */
0294     unsigned char sense_buffer[SG_MAX_SENSE]; /* [o] Output in 3 cases:
0295        when target_status is CHECK_CONDITION or
0296        when target_status is COMMAND_TERMINATED or
0297        when (driver_status & DRIVER_SENSE) is true. */
0298 };      /* This structure is 36 bytes long on i386 */
0299 
0300 
0301 /* IOCTLs: The following are not required (or ignored) when the sg_io_hdr_t
0302        interface is used. They are kept for backward compatibility with
0303        the original and version 2 drivers. */
0304 
0305 #define SG_SET_TIMEOUT 0x2201  /* unit: jiffies (10ms on i386) */
0306 #define SG_GET_TIMEOUT 0x2202  /* yield timeout as _return_ value */
0307 
0308 /* Get/set command queuing state per fd (default is SG_DEF_COMMAND_Q.
0309    Each time a sg_io_hdr_t object is seen on this file descriptor, this
0310    command queuing flag is set on (overriding the previous setting). */
0311 #define SG_GET_COMMAND_Q 0x2270   /* Yields 0 (queuing off) or 1 (on) */
0312 #define SG_SET_COMMAND_Q 0x2271   /* Change queuing state with 0 or 1 */
0313 
0314 /* Turn on/off error sense trace (1 and 0 respectively, default is off).
0315    Try using: "# cat /proc/scsi/sg/debug" instead in the v3 driver */
0316 #define SG_SET_DEBUG 0x227e    /* 0 -> turn off debug */
0317 
0318 #define SG_NEXT_CMD_LEN 0x2283  /* override SCSI command length with given
0319            number on the next write() on this file descriptor */
0320 
0321 
0322 /* Defaults, commented if they differ from original sg driver */
0323 #ifdef __KERNEL__
0324 #define SG_DEFAULT_TIMEOUT_USER (60*USER_HZ) /* HZ == 'jiffies in 1 second' */
0325 #else
0326 #define SG_DEFAULT_TIMEOUT  (60*HZ)      /* HZ == 'jiffies in 1 second' */
0327 #endif
0328 
0329 #define SG_DEF_COMMAND_Q 0     /* command queuing is always on when
0330                   the new interface is used */
0331 #define SG_DEF_UNDERRUN_FLAG 0
0332 
0333 #endif