Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _SCSI_SCSI_CMND_H
0003 #define _SCSI_SCSI_CMND_H
0004 
0005 #include <linux/dma-mapping.h>
0006 #include <linux/blkdev.h>
0007 #include <linux/t10-pi.h>
0008 #include <linux/list.h>
0009 #include <linux/types.h>
0010 #include <linux/timer.h>
0011 #include <linux/scatterlist.h>
0012 #include <scsi/scsi_device.h>
0013 
0014 struct Scsi_Host;
0015 
0016 /*
0017  * MAX_COMMAND_SIZE is:
0018  * The longest fixed-length SCSI CDB as per the SCSI standard.
0019  * fixed-length means: commands that their size can be determined
0020  * by their opcode and the CDB does not carry a length specifier, (unlike
0021  * the VARIABLE_LENGTH_CMD(0x7f) command). This is actually not exactly
0022  * true and the SCSI standard also defines extended commands and
0023  * vendor specific commands that can be bigger than 16 bytes. The kernel
0024  * will support these using the same infrastructure used for VARLEN CDB's.
0025  * So in effect MAX_COMMAND_SIZE means the maximum size command scsi-ml
0026  * supports without specifying a cmd_len by ULD's
0027  */
0028 #define MAX_COMMAND_SIZE 16
0029 
0030 struct scsi_data_buffer {
0031     struct sg_table table;
0032     unsigned length;
0033 };
0034 
0035 /* embedded in scsi_cmnd */
0036 struct scsi_pointer {
0037     char *ptr;      /* data pointer */
0038     int this_residual;  /* left in this buffer */
0039     struct scatterlist *buffer; /* which buffer */
0040     int buffers_residual;   /* how many buffers left */
0041 
0042         dma_addr_t dma_handle;
0043 
0044     volatile int Status;
0045     volatile int Message;
0046     volatile int have_data_in;
0047     volatile int sent_command;
0048     volatile int phase;
0049 };
0050 
0051 /* for scmd->flags */
0052 #define SCMD_TAGGED     (1 << 0)
0053 #define SCMD_INITIALIZED    (1 << 1)
0054 #define SCMD_LAST       (1 << 2)
0055 /* flags preserved across unprep / reprep */
0056 #define SCMD_PRESERVED_FLAGS    (SCMD_INITIALIZED)
0057 
0058 /* for scmd->state */
0059 #define SCMD_STATE_COMPLETE 0
0060 #define SCMD_STATE_INFLIGHT 1
0061 
0062 enum scsi_cmnd_submitter {
0063     SUBMITTED_BY_BLOCK_LAYER = 0,
0064     SUBMITTED_BY_SCSI_ERROR_HANDLER = 1,
0065     SUBMITTED_BY_SCSI_RESET_IOCTL = 2,
0066 } __packed;
0067 
0068 struct scsi_cmnd {
0069     struct scsi_device *device;
0070     struct list_head eh_entry; /* entry for the host eh_abort_list/eh_cmd_q */
0071     struct delayed_work abort_work;
0072 
0073     struct rcu_head rcu;
0074 
0075     int eh_eflags;      /* Used by error handlr */
0076 
0077     int budget_token;
0078 
0079     /*
0080      * This is set to jiffies as it was when the command was first
0081      * allocated.  It is used to time how long the command has
0082      * been outstanding
0083      */
0084     unsigned long jiffies_at_alloc;
0085 
0086     int retries;
0087     int allowed;
0088 
0089     unsigned char prot_op;
0090     unsigned char prot_type;
0091     unsigned char prot_flags;
0092     enum scsi_cmnd_submitter submitter;
0093 
0094     unsigned short cmd_len;
0095     enum dma_data_direction sc_data_direction;
0096 
0097     unsigned char cmnd[32]; /* SCSI CDB */
0098 
0099     /* These elements define the operation we ultimately want to perform */
0100     struct scsi_data_buffer sdb;
0101     struct scsi_data_buffer *prot_sdb;
0102 
0103     unsigned underflow; /* Return error if less than
0104                    this amount is transferred */
0105 
0106     unsigned transfersize;  /* How much we are guaranteed to
0107                    transfer with each SCSI transfer
0108                    (ie, between disconnect / 
0109                    reconnects.   Probably == sector
0110                    size */
0111     unsigned resid_len; /* residual count */
0112     unsigned sense_len;
0113     unsigned char *sense_buffer;
0114                 /* obtained by REQUEST SENSE when
0115                  * CHECK CONDITION is received on original
0116                  * command (auto-sense). Length must be
0117                  * SCSI_SENSE_BUFFERSIZE bytes. */
0118 
0119     int flags;      /* Command flags */
0120     unsigned long state;    /* Command completion state */
0121 
0122     unsigned int extra_len; /* length of alignment and padding */
0123 
0124     /*
0125      * The fields below can be modified by the LLD but the fields above
0126      * must not be modified.
0127      */
0128 
0129     unsigned char *host_scribble;   /* The host adapter is allowed to
0130                      * call scsi_malloc and get some memory
0131                      * and hang it here.  The host adapter
0132                      * is also expected to call scsi_free
0133                      * to release this memory.  (The memory
0134                      * obtained by scsi_malloc is guaranteed
0135                      * to be at an address < 16Mb). */
0136 
0137     int result;     /* Status code from lower level driver */
0138 };
0139 
0140 /* Variant of blk_mq_rq_from_pdu() that verifies the type of its argument. */
0141 static inline struct request *scsi_cmd_to_rq(struct scsi_cmnd *scmd)
0142 {
0143     return blk_mq_rq_from_pdu(scmd);
0144 }
0145 
0146 /*
0147  * Return the driver private allocation behind the command.
0148  * Only works if cmd_size is set in the host template.
0149  */
0150 static inline void *scsi_cmd_priv(struct scsi_cmnd *cmd)
0151 {
0152     return cmd + 1;
0153 }
0154 
0155 void scsi_done(struct scsi_cmnd *cmd);
0156 void scsi_done_direct(struct scsi_cmnd *cmd);
0157 
0158 extern void scsi_finish_command(struct scsi_cmnd *cmd);
0159 
0160 extern void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count,
0161                  size_t *offset, size_t *len);
0162 extern void scsi_kunmap_atomic_sg(void *virt);
0163 
0164 blk_status_t scsi_alloc_sgtables(struct scsi_cmnd *cmd);
0165 void scsi_free_sgtables(struct scsi_cmnd *cmd);
0166 
0167 #ifdef CONFIG_SCSI_DMA
0168 extern int scsi_dma_map(struct scsi_cmnd *cmd);
0169 extern void scsi_dma_unmap(struct scsi_cmnd *cmd);
0170 #else /* !CONFIG_SCSI_DMA */
0171 static inline int scsi_dma_map(struct scsi_cmnd *cmd) { return -ENOSYS; }
0172 static inline void scsi_dma_unmap(struct scsi_cmnd *cmd) { }
0173 #endif /* !CONFIG_SCSI_DMA */
0174 
0175 static inline unsigned scsi_sg_count(struct scsi_cmnd *cmd)
0176 {
0177     return cmd->sdb.table.nents;
0178 }
0179 
0180 static inline struct scatterlist *scsi_sglist(struct scsi_cmnd *cmd)
0181 {
0182     return cmd->sdb.table.sgl;
0183 }
0184 
0185 static inline unsigned scsi_bufflen(struct scsi_cmnd *cmd)
0186 {
0187     return cmd->sdb.length;
0188 }
0189 
0190 static inline void scsi_set_resid(struct scsi_cmnd *cmd, unsigned int resid)
0191 {
0192     cmd->resid_len = resid;
0193 }
0194 
0195 static inline unsigned int scsi_get_resid(struct scsi_cmnd *cmd)
0196 {
0197     return cmd->resid_len;
0198 }
0199 
0200 #define scsi_for_each_sg(cmd, sg, nseg, __i)            \
0201     for_each_sg(scsi_sglist(cmd), sg, nseg, __i)
0202 
0203 static inline int scsi_sg_copy_from_buffer(struct scsi_cmnd *cmd,
0204                        void *buf, int buflen)
0205 {
0206     return sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
0207                    buf, buflen);
0208 }
0209 
0210 static inline int scsi_sg_copy_to_buffer(struct scsi_cmnd *cmd,
0211                      void *buf, int buflen)
0212 {
0213     return sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
0214                  buf, buflen);
0215 }
0216 
0217 static inline sector_t scsi_get_sector(struct scsi_cmnd *scmd)
0218 {
0219     return blk_rq_pos(scsi_cmd_to_rq(scmd));
0220 }
0221 
0222 static inline sector_t scsi_get_lba(struct scsi_cmnd *scmd)
0223 {
0224     unsigned int shift = ilog2(scmd->device->sector_size) - SECTOR_SHIFT;
0225 
0226     return blk_rq_pos(scsi_cmd_to_rq(scmd)) >> shift;
0227 }
0228 
0229 static inline unsigned int scsi_logical_block_count(struct scsi_cmnd *scmd)
0230 {
0231     unsigned int shift = ilog2(scmd->device->sector_size) - SECTOR_SHIFT;
0232 
0233     return blk_rq_bytes(scsi_cmd_to_rq(scmd)) >> shift;
0234 }
0235 
0236 /*
0237  * The operations below are hints that tell the controller driver how
0238  * to handle I/Os with DIF or similar types of protection information.
0239  */
0240 enum scsi_prot_operations {
0241     /* Normal I/O */
0242     SCSI_PROT_NORMAL = 0,
0243 
0244     /* OS-HBA: Protected, HBA-Target: Unprotected */
0245     SCSI_PROT_READ_INSERT,
0246     SCSI_PROT_WRITE_STRIP,
0247 
0248     /* OS-HBA: Unprotected, HBA-Target: Protected */
0249     SCSI_PROT_READ_STRIP,
0250     SCSI_PROT_WRITE_INSERT,
0251 
0252     /* OS-HBA: Protected, HBA-Target: Protected */
0253     SCSI_PROT_READ_PASS,
0254     SCSI_PROT_WRITE_PASS,
0255 };
0256 
0257 static inline void scsi_set_prot_op(struct scsi_cmnd *scmd, unsigned char op)
0258 {
0259     scmd->prot_op = op;
0260 }
0261 
0262 static inline unsigned char scsi_get_prot_op(struct scsi_cmnd *scmd)
0263 {
0264     return scmd->prot_op;
0265 }
0266 
0267 enum scsi_prot_flags {
0268     SCSI_PROT_TRANSFER_PI       = 1 << 0,
0269     SCSI_PROT_GUARD_CHECK       = 1 << 1,
0270     SCSI_PROT_REF_CHECK     = 1 << 2,
0271     SCSI_PROT_REF_INCREMENT     = 1 << 3,
0272     SCSI_PROT_IP_CHECKSUM       = 1 << 4,
0273 };
0274 
0275 /*
0276  * The controller usually does not know anything about the target it
0277  * is communicating with.  However, when DIX is enabled the controller
0278  * must be know target type so it can verify the protection
0279  * information passed along with the I/O.
0280  */
0281 enum scsi_prot_target_type {
0282     SCSI_PROT_DIF_TYPE0 = 0,
0283     SCSI_PROT_DIF_TYPE1,
0284     SCSI_PROT_DIF_TYPE2,
0285     SCSI_PROT_DIF_TYPE3,
0286 };
0287 
0288 static inline void scsi_set_prot_type(struct scsi_cmnd *scmd, unsigned char type)
0289 {
0290     scmd->prot_type = type;
0291 }
0292 
0293 static inline unsigned char scsi_get_prot_type(struct scsi_cmnd *scmd)
0294 {
0295     return scmd->prot_type;
0296 }
0297 
0298 static inline u32 scsi_prot_ref_tag(struct scsi_cmnd *scmd)
0299 {
0300     struct request *rq = blk_mq_rq_from_pdu(scmd);
0301 
0302     return t10_pi_ref_tag(rq);
0303 }
0304 
0305 static inline unsigned int scsi_prot_interval(struct scsi_cmnd *scmd)
0306 {
0307     return scmd->device->sector_size;
0308 }
0309 
0310 static inline unsigned scsi_prot_sg_count(struct scsi_cmnd *cmd)
0311 {
0312     return cmd->prot_sdb ? cmd->prot_sdb->table.nents : 0;
0313 }
0314 
0315 static inline struct scatterlist *scsi_prot_sglist(struct scsi_cmnd *cmd)
0316 {
0317     return cmd->prot_sdb ? cmd->prot_sdb->table.sgl : NULL;
0318 }
0319 
0320 static inline struct scsi_data_buffer *scsi_prot(struct scsi_cmnd *cmd)
0321 {
0322     return cmd->prot_sdb;
0323 }
0324 
0325 #define scsi_for_each_prot_sg(cmd, sg, nseg, __i)       \
0326     for_each_sg(scsi_prot_sglist(cmd), sg, nseg, __i)
0327 
0328 static inline void set_status_byte(struct scsi_cmnd *cmd, char status)
0329 {
0330     cmd->result = (cmd->result & 0xffffff00) | status;
0331 }
0332 
0333 static inline u8 get_status_byte(struct scsi_cmnd *cmd)
0334 {
0335     return cmd->result & 0xff;
0336 }
0337 
0338 static inline void set_host_byte(struct scsi_cmnd *cmd, char status)
0339 {
0340     cmd->result = (cmd->result & 0xff00ffff) | (status << 16);
0341 }
0342 
0343 static inline u8 get_host_byte(struct scsi_cmnd *cmd)
0344 {
0345     return (cmd->result >> 16) & 0xff;
0346 }
0347 
0348 /**
0349  * scsi_msg_to_host_byte() - translate message byte
0350  *
0351  * Translate the SCSI parallel message byte to a matching
0352  * host byte setting. A message of COMMAND_COMPLETE indicates
0353  * a successful command execution, any other message indicate
0354  * an error. As the messages themselves only have a meaning
0355  * for the SCSI parallel protocol this function translates
0356  * them into a matching host byte value for SCSI EH.
0357  */
0358 static inline void scsi_msg_to_host_byte(struct scsi_cmnd *cmd, u8 msg)
0359 {
0360     switch (msg) {
0361     case COMMAND_COMPLETE:
0362         break;
0363     case ABORT_TASK_SET:
0364         set_host_byte(cmd, DID_ABORT);
0365         break;
0366     case TARGET_RESET:
0367         set_host_byte(cmd, DID_RESET);
0368         break;
0369     default:
0370         set_host_byte(cmd, DID_ERROR);
0371         break;
0372     }
0373 }
0374 
0375 static inline unsigned scsi_transfer_length(struct scsi_cmnd *scmd)
0376 {
0377     unsigned int xfer_len = scmd->sdb.length;
0378     unsigned int prot_interval = scsi_prot_interval(scmd);
0379 
0380     if (scmd->prot_flags & SCSI_PROT_TRANSFER_PI)
0381         xfer_len += (xfer_len >> ilog2(prot_interval)) * 8;
0382 
0383     return xfer_len;
0384 }
0385 
0386 extern void scsi_build_sense(struct scsi_cmnd *scmd, int desc,
0387                  u8 key, u8 asc, u8 ascq);
0388 
0389 struct request *scsi_alloc_request(struct request_queue *q, blk_opf_t opf,
0390                    blk_mq_req_flags_t flags);
0391 
0392 #endif /* _SCSI_SCSI_CMND_H */