scsi_cmnd.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _SCSI_SCSI_CMND_H
  3. #define _SCSI_SCSI_CMND_H
  4. #include <linux/dma-mapping.h>
  5. #include <linux/blkdev.h>
  6. #include <linux/t10-pi.h>
  7. #include <linux/list.h>
  8. #include <linux/types.h>
  9. #include <linux/timer.h>
  10. #include <linux/scatterlist.h>
  11. #include <scsi/scsi_device.h>
  12. struct Scsi_Host;
  13. /*
  14. * MAX_COMMAND_SIZE is:
  15. * The longest fixed-length SCSI CDB as per the SCSI standard.
  16. * fixed-length means: commands that their size can be determined
  17. * by their opcode and the CDB does not carry a length specifier, (unlike
  18. * the VARIABLE_LENGTH_CMD(0x7f) command). This is actually not exactly
  19. * true and the SCSI standard also defines extended commands and
  20. * vendor specific commands that can be bigger than 16 bytes. The kernel
  21. * will support these using the same infrastructure used for VARLEN CDB's.
  22. * So in effect MAX_COMMAND_SIZE means the maximum size command scsi-ml
  23. * supports without specifying a cmd_len by ULD's
  24. */
  25. #define MAX_COMMAND_SIZE 16
  26. struct scsi_data_buffer {
  27. struct sg_table table;
  28. unsigned length;
  29. };
  30. /* embedded in scsi_cmnd */
  31. struct scsi_pointer {
  32. char *ptr; /* data pointer */
  33. int this_residual; /* left in this buffer */
  34. struct scatterlist *buffer; /* which buffer */
  35. int buffers_residual; /* how many buffers left */
  36. dma_addr_t dma_handle;
  37. volatile int Status;
  38. volatile int Message;
  39. volatile int have_data_in;
  40. volatile int sent_command;
  41. volatile int phase;
  42. };
  43. /* for scmd->flags */
  44. #define SCMD_TAGGED (1 << 0)
  45. #define SCMD_INITIALIZED (1 << 1)
  46. #define SCMD_LAST (1 << 2)
  47. /*
  48. * libata uses SCSI EH to fetch sense data for successful commands.
  49. * SCSI EH should not overwrite scmd->result when SCMD_FORCE_EH_SUCCESS is set.
  50. */
  51. #define SCMD_FORCE_EH_SUCCESS (1 << 3)
  52. #define SCMD_FAIL_IF_RECOVERING (1 << 4)
  53. /* flags preserved across unprep / reprep */
  54. #define SCMD_PRESERVED_FLAGS (SCMD_INITIALIZED | SCMD_FAIL_IF_RECOVERING)
  55. /* for scmd->state */
  56. #define SCMD_STATE_COMPLETE 0
  57. #define SCMD_STATE_INFLIGHT 1
  58. enum scsi_cmnd_submitter {
  59. SUBMITTED_BY_BLOCK_LAYER = 0,
  60. SUBMITTED_BY_SCSI_ERROR_HANDLER = 1,
  61. SUBMITTED_BY_SCSI_RESET_IOCTL = 2,
  62. } __packed;
  63. struct scsi_cmnd {
  64. struct scsi_device *device;
  65. struct list_head eh_entry; /* entry for the host eh_abort_list/eh_cmd_q */
  66. struct delayed_work abort_work;
  67. struct rcu_head rcu;
  68. int eh_eflags; /* Used by error handlr */
  69. int budget_token;
  70. /*
  71. * This is set to jiffies as it was when the command was first
  72. * allocated. It is used to time how long the command has
  73. * been outstanding
  74. */
  75. unsigned long jiffies_at_alloc;
  76. int retries;
  77. int allowed;
  78. unsigned char prot_op;
  79. unsigned char prot_type;
  80. unsigned char prot_flags;
  81. enum scsi_cmnd_submitter submitter;
  82. unsigned short cmd_len;
  83. enum dma_data_direction sc_data_direction;
  84. unsigned char cmnd[32]; /* SCSI CDB */
  85. /* These elements define the operation we ultimately want to perform */
  86. struct scsi_data_buffer sdb;
  87. struct scsi_data_buffer *prot_sdb;
  88. unsigned underflow; /* Return error if less than
  89. this amount is transferred */
  90. unsigned transfersize; /* How much we are guaranteed to
  91. transfer with each SCSI transfer
  92. (ie, between disconnect /
  93. reconnects. Probably == sector
  94. size */
  95. unsigned resid_len; /* residual count */
  96. unsigned sense_len;
  97. unsigned char *sense_buffer;
  98. /* obtained by REQUEST SENSE when
  99. * CHECK CONDITION is received on original
  100. * command (auto-sense). Length must be
  101. * SCSI_SENSE_BUFFERSIZE bytes. */
  102. int flags; /* Command flags */
  103. unsigned long state; /* Command completion state */
  104. unsigned int extra_len; /* length of alignment and padding */
  105. /*
  106. * The fields below can be modified by the LLD but the fields above
  107. * must not be modified.
  108. */
  109. unsigned char *host_scribble; /* The host adapter is allowed to
  110. * call scsi_malloc and get some memory
  111. * and hang it here. The host adapter
  112. * is also expected to call scsi_free
  113. * to release this memory. (The memory
  114. * obtained by scsi_malloc is guaranteed
  115. * to be at an address < 16Mb). */
  116. int result; /* Status code from lower level driver */
  117. };
  118. /* Variant of blk_mq_rq_from_pdu() that verifies the type of its argument. */
  119. static inline struct request *scsi_cmd_to_rq(struct scsi_cmnd *scmd)
  120. {
  121. return blk_mq_rq_from_pdu(scmd);
  122. }
  123. /*
  124. * Return the driver private allocation behind the command.
  125. * Only works if cmd_size is set in the host template.
  126. */
  127. static inline void *scsi_cmd_priv(struct scsi_cmnd *cmd)
  128. {
  129. return cmd + 1;
  130. }
  131. void scsi_done(struct scsi_cmnd *cmd);
  132. void scsi_done_direct(struct scsi_cmnd *cmd);
  133. extern void scsi_finish_command(struct scsi_cmnd *cmd);
  134. extern void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count,
  135. size_t *offset, size_t *len);
  136. extern void scsi_kunmap_atomic_sg(void *virt);
  137. blk_status_t scsi_alloc_sgtables(struct scsi_cmnd *cmd);
  138. void scsi_free_sgtables(struct scsi_cmnd *cmd);
  139. #ifdef CONFIG_SCSI_DMA
  140. extern int scsi_dma_map(struct scsi_cmnd *cmd);
  141. extern void scsi_dma_unmap(struct scsi_cmnd *cmd);
  142. #else /* !CONFIG_SCSI_DMA */
  143. static inline int scsi_dma_map(struct scsi_cmnd *cmd) { return -ENOSYS; }
  144. static inline void scsi_dma_unmap(struct scsi_cmnd *cmd) { }
  145. #endif /* !CONFIG_SCSI_DMA */
  146. static inline unsigned scsi_sg_count(struct scsi_cmnd *cmd)
  147. {
  148. return cmd->sdb.table.nents;
  149. }
  150. static inline struct scatterlist *scsi_sglist(struct scsi_cmnd *cmd)
  151. {
  152. return cmd->sdb.table.sgl;
  153. }
  154. static inline unsigned scsi_bufflen(struct scsi_cmnd *cmd)
  155. {
  156. return cmd->sdb.length;
  157. }
  158. static inline void scsi_set_resid(struct scsi_cmnd *cmd, unsigned int resid)
  159. {
  160. cmd->resid_len = resid;
  161. }
  162. static inline unsigned int scsi_get_resid(struct scsi_cmnd *cmd)
  163. {
  164. return cmd->resid_len;
  165. }
  166. #define scsi_for_each_sg(cmd, sg, nseg, __i) \
  167. for_each_sg(scsi_sglist(cmd), sg, nseg, __i)
  168. static inline int scsi_sg_copy_from_buffer(struct scsi_cmnd *cmd,
  169. const void *buf, int buflen)
  170. {
  171. return sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  172. buf, buflen);
  173. }
  174. static inline int scsi_sg_copy_to_buffer(struct scsi_cmnd *cmd,
  175. void *buf, int buflen)
  176. {
  177. return sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  178. buf, buflen);
  179. }
  180. static inline sector_t scsi_get_sector(struct scsi_cmnd *scmd)
  181. {
  182. return blk_rq_pos(scsi_cmd_to_rq(scmd));
  183. }
  184. static inline sector_t scsi_get_lba(struct scsi_cmnd *scmd)
  185. {
  186. unsigned int shift = ilog2(scmd->device->sector_size) - SECTOR_SHIFT;
  187. return blk_rq_pos(scsi_cmd_to_rq(scmd)) >> shift;
  188. }
  189. static inline unsigned int scsi_logical_block_count(struct scsi_cmnd *scmd)
  190. {
  191. unsigned int shift = ilog2(scmd->device->sector_size);
  192. return blk_rq_bytes(scsi_cmd_to_rq(scmd)) >> shift;
  193. }
  194. /*
  195. * The operations below are hints that tell the controller driver how
  196. * to handle I/Os with DIF or similar types of protection information.
  197. */
  198. enum scsi_prot_operations {
  199. /* Normal I/O */
  200. SCSI_PROT_NORMAL = 0,
  201. /* OS-HBA: Protected, HBA-Target: Unprotected */
  202. SCSI_PROT_READ_INSERT,
  203. SCSI_PROT_WRITE_STRIP,
  204. /* OS-HBA: Unprotected, HBA-Target: Protected */
  205. SCSI_PROT_READ_STRIP,
  206. SCSI_PROT_WRITE_INSERT,
  207. /* OS-HBA: Protected, HBA-Target: Protected */
  208. SCSI_PROT_READ_PASS,
  209. SCSI_PROT_WRITE_PASS,
  210. };
  211. static inline void scsi_set_prot_op(struct scsi_cmnd *scmd, unsigned char op)
  212. {
  213. scmd->prot_op = op;
  214. }
  215. static inline unsigned char scsi_get_prot_op(struct scsi_cmnd *scmd)
  216. {
  217. return scmd->prot_op;
  218. }
  219. enum scsi_prot_flags {
  220. SCSI_PROT_TRANSFER_PI = 1 << 0,
  221. SCSI_PROT_GUARD_CHECK = 1 << 1,
  222. SCSI_PROT_REF_CHECK = 1 << 2,
  223. SCSI_PROT_REF_INCREMENT = 1 << 3,
  224. SCSI_PROT_IP_CHECKSUM = 1 << 4,
  225. };
  226. /*
  227. * The controller usually does not know anything about the target it
  228. * is communicating with. However, when DIX is enabled the controller
  229. * must be know target type so it can verify the protection
  230. * information passed along with the I/O.
  231. */
  232. enum scsi_prot_target_type {
  233. SCSI_PROT_DIF_TYPE0 = 0,
  234. SCSI_PROT_DIF_TYPE1,
  235. SCSI_PROT_DIF_TYPE2,
  236. SCSI_PROT_DIF_TYPE3,
  237. };
  238. static inline void scsi_set_prot_type(struct scsi_cmnd *scmd, unsigned char type)
  239. {
  240. scmd->prot_type = type;
  241. }
  242. static inline unsigned char scsi_get_prot_type(struct scsi_cmnd *scmd)
  243. {
  244. return scmd->prot_type;
  245. }
  246. static inline u32 scsi_prot_ref_tag(struct scsi_cmnd *scmd)
  247. {
  248. struct request *rq = blk_mq_rq_from_pdu(scmd);
  249. return t10_pi_ref_tag(rq);
  250. }
  251. static inline unsigned int scsi_prot_interval(struct scsi_cmnd *scmd)
  252. {
  253. return scmd->device->sector_size;
  254. }
  255. static inline unsigned scsi_prot_sg_count(struct scsi_cmnd *cmd)
  256. {
  257. return cmd->prot_sdb ? cmd->prot_sdb->table.nents : 0;
  258. }
  259. static inline struct scatterlist *scsi_prot_sglist(struct scsi_cmnd *cmd)
  260. {
  261. return cmd->prot_sdb ? cmd->prot_sdb->table.sgl : NULL;
  262. }
  263. static inline struct scsi_data_buffer *scsi_prot(struct scsi_cmnd *cmd)
  264. {
  265. return cmd->prot_sdb;
  266. }
  267. #define scsi_for_each_prot_sg(cmd, sg, nseg, __i) \
  268. for_each_sg(scsi_prot_sglist(cmd), sg, nseg, __i)
  269. static inline void set_status_byte(struct scsi_cmnd *cmd, char status)
  270. {
  271. cmd->result = (cmd->result & 0xffffff00) | status;
  272. }
  273. static inline u8 get_status_byte(struct scsi_cmnd *cmd)
  274. {
  275. return cmd->result & 0xff;
  276. }
  277. static inline void set_host_byte(struct scsi_cmnd *cmd, char status)
  278. {
  279. cmd->result = (cmd->result & 0xff00ffff) | (status << 16);
  280. }
  281. static inline u8 get_host_byte(struct scsi_cmnd *cmd)
  282. {
  283. return (cmd->result >> 16) & 0xff;
  284. }
  285. /**
  286. * scsi_msg_to_host_byte() - translate message byte
  287. * @cmd: the SCSI command
  288. * @msg: the SCSI parallel message byte to translate
  289. *
  290. * Translate the SCSI parallel message byte to a matching
  291. * host byte setting. A message of COMMAND_COMPLETE indicates
  292. * a successful command execution, any other message indicate
  293. * an error. As the messages themselves only have a meaning
  294. * for the SCSI parallel protocol this function translates
  295. * them into a matching host byte value for SCSI EH.
  296. */
  297. static inline void scsi_msg_to_host_byte(struct scsi_cmnd *cmd, u8 msg)
  298. {
  299. switch (msg) {
  300. case COMMAND_COMPLETE:
  301. break;
  302. case ABORT_TASK_SET:
  303. set_host_byte(cmd, DID_ABORT);
  304. break;
  305. case TARGET_RESET:
  306. set_host_byte(cmd, DID_RESET);
  307. break;
  308. default:
  309. set_host_byte(cmd, DID_ERROR);
  310. break;
  311. }
  312. }
  313. static inline unsigned scsi_transfer_length(struct scsi_cmnd *scmd)
  314. {
  315. unsigned int xfer_len = scmd->sdb.length;
  316. unsigned int prot_interval = scsi_prot_interval(scmd);
  317. if (scmd->prot_flags & SCSI_PROT_TRANSFER_PI)
  318. xfer_len += (xfer_len >> ilog2(prot_interval)) * 8;
  319. return xfer_len;
  320. }
  321. extern void scsi_build_sense(struct scsi_cmnd *scmd, int desc,
  322. u8 key, u8 asc, u8 ascq);
  323. struct request *scsi_alloc_request(struct request_queue *q, blk_opf_t opf,
  324. blk_mq_req_flags_t flags);
  325. #endif /* _SCSI_SCSI_CMND_H */