0001
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
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #define MAX_COMMAND_SIZE 16
0029
0030 struct scsi_data_buffer {
0031 struct sg_table table;
0032 unsigned length;
0033 };
0034
0035
0036 struct scsi_pointer {
0037 char *ptr;
0038 int this_residual;
0039 struct scatterlist *buffer;
0040 int buffers_residual;
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
0052 #define SCMD_TAGGED (1 << 0)
0053 #define SCMD_INITIALIZED (1 << 1)
0054 #define SCMD_LAST (1 << 2)
0055
0056 #define SCMD_PRESERVED_FLAGS (SCMD_INITIALIZED)
0057
0058
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;
0071 struct delayed_work abort_work;
0072
0073 struct rcu_head rcu;
0074
0075 int eh_eflags;
0076
0077 int budget_token;
0078
0079
0080
0081
0082
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];
0098
0099
0100 struct scsi_data_buffer sdb;
0101 struct scsi_data_buffer *prot_sdb;
0102
0103 unsigned underflow;
0104
0105
0106 unsigned transfersize;
0107
0108
0109
0110
0111 unsigned resid_len;
0112 unsigned sense_len;
0113 unsigned char *sense_buffer;
0114
0115
0116
0117
0118
0119 int flags;
0120 unsigned long state;
0121
0122 unsigned int extra_len;
0123
0124
0125
0126
0127
0128
0129 unsigned char *host_scribble;
0130
0131
0132
0133
0134
0135
0136
0137 int result;
0138 };
0139
0140
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
0148
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
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
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
0238
0239
0240 enum scsi_prot_operations {
0241
0242 SCSI_PROT_NORMAL = 0,
0243
0244
0245 SCSI_PROT_READ_INSERT,
0246 SCSI_PROT_WRITE_STRIP,
0247
0248
0249 SCSI_PROT_READ_STRIP,
0250 SCSI_PROT_WRITE_INSERT,
0251
0252
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
0277
0278
0279
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
0350
0351
0352
0353
0354
0355
0356
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