scsi_cmnd.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. #include <scsi/scsi_request.h>
  13. struct Scsi_Host;
  14. struct scsi_driver;
  15. #include <scsi/scsi_device.h>
  16. /*
  17. * MAX_COMMAND_SIZE is:
  18. * The longest fixed-length SCSI CDB as per the SCSI standard.
  19. * fixed-length means: commands that their size can be determined
  20. * by their opcode and the CDB does not carry a length specifier, (unlike
  21. * the VARIABLE_LENGTH_CMD(0x7f) command). This is actually not exactly
  22. * true and the SCSI standard also defines extended commands and
  23. * vendor specific commands that can be bigger than 16 bytes. The kernel
  24. * will support these using the same infrastructure used for VARLEN CDB's.
  25. * So in effect MAX_COMMAND_SIZE means the maximum size command scsi-ml
  26. * supports without specifying a cmd_len by ULD's
  27. */
  28. #define MAX_COMMAND_SIZE 16
  29. #if (MAX_COMMAND_SIZE > BLK_MAX_CDB)
  30. # error MAX_COMMAND_SIZE can not be bigger than BLK_MAX_CDB
  31. #endif
  32. struct scsi_data_buffer {
  33. struct sg_table table;
  34. unsigned length;
  35. int resid;
  36. };
  37. /* embedded in scsi_cmnd */
  38. struct scsi_pointer {
  39. char *ptr; /* data pointer */
  40. int this_residual; /* left in this buffer */
  41. struct scatterlist *buffer; /* which buffer */
  42. int buffers_residual; /* how many buffers left */
  43. dma_addr_t dma_handle;
  44. volatile int Status;
  45. volatile int Message;
  46. volatile int have_data_in;
  47. volatile int sent_command;
  48. volatile int phase;
  49. };
  50. /* for scmd->flags */
  51. #define SCMD_TAGGED (1 << 0)
  52. #define SCMD_UNCHECKED_ISA_DMA (1 << 1)
  53. #define SCMD_INITIALIZED (1 << 2)
  54. /* flags preserved across unprep / reprep */
  55. #define SCMD_PRESERVED_FLAGS (SCMD_UNCHECKED_ISA_DMA | SCMD_INITIALIZED)
  56. struct scsi_cmnd {
  57. struct scsi_request req;
  58. struct scsi_device *device;
  59. struct list_head list; /* scsi_cmnd participates in queue lists */
  60. struct list_head eh_entry; /* entry for the host eh_cmd_q */
  61. struct delayed_work abort_work;
  62. struct rcu_head rcu;
  63. int eh_eflags; /* Used by error handlr */
  64. /*
  65. * A SCSI Command is assigned a nonzero serial_number before passed
  66. * to the driver's queue command function. The serial_number is
  67. * cleared when scsi_done is entered indicating that the command
  68. * has been completed. It is a bug for LLDDs to use this number
  69. * for purposes other than printk (and even that is only useful
  70. * for debugging).
  71. */
  72. unsigned long serial_number;
  73. /*
  74. * This is set to jiffies as it was when the command was first
  75. * allocated. It is used to time how long the command has
  76. * been outstanding
  77. */
  78. unsigned long jiffies_at_alloc;
  79. int retries;
  80. int allowed;
  81. unsigned char prot_op;
  82. unsigned char prot_type;
  83. unsigned char prot_flags;
  84. unsigned short cmd_len;
  85. enum dma_data_direction sc_data_direction;
  86. /* These elements define the operation we are about to perform */
  87. unsigned char *cmnd;
  88. /* These elements define the operation we ultimately want to perform */
  89. struct scsi_data_buffer sdb;
  90. struct scsi_data_buffer *prot_sdb;
  91. unsigned underflow; /* Return error if less than
  92. this amount is transferred */
  93. unsigned transfersize; /* How much we are guaranteed to
  94. transfer with each SCSI transfer
  95. (ie, between disconnect /
  96. reconnects. Probably == sector
  97. size */
  98. struct request *request; /* The command we are
  99. working on */
  100. #define SCSI_SENSE_BUFFERSIZE 96
  101. unsigned char *sense_buffer;
  102. /* obtained by REQUEST SENSE when
  103. * CHECK CONDITION is received on original
  104. * command (auto-sense) */
  105. /* Low-level done function - can be used by low-level driver to point
  106. * to completion function. Not used by mid/upper level code. */
  107. void (*scsi_done) (struct scsi_cmnd *);
  108. /*
  109. * The following fields can be written to by the host specific code.
  110. * Everything else should be left alone.
  111. */
  112. struct scsi_pointer SCp; /* Scratchpad used by some host adapters */
  113. unsigned char *host_scribble; /* The host adapter is allowed to
  114. * call scsi_malloc and get some memory
  115. * and hang it here. The host adapter
  116. * is also expected to call scsi_free
  117. * to release this memory. (The memory
  118. * obtained by scsi_malloc is guaranteed
  119. * to be at an address < 16Mb). */
  120. int result; /* Status code from lower level driver */
  121. int flags; /* Command flags */
  122. unsigned char tag; /* SCSI-II queued command tag */
  123. };
  124. /*
  125. * Return the driver private allocation behind the command.
  126. * Only works if cmd_size is set in the host template.
  127. */
  128. static inline void *scsi_cmd_priv(struct scsi_cmnd *cmd)
  129. {
  130. return cmd + 1;
  131. }
  132. /* make sure not to use it with passthrough commands */
  133. static inline struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd)
  134. {
  135. return *(struct scsi_driver **)cmd->request->rq_disk->private_data;
  136. }
  137. extern void scsi_put_command(struct scsi_cmnd *);
  138. extern void scsi_finish_command(struct scsi_cmnd *cmd);
  139. extern void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count,
  140. size_t *offset, size_t *len);
  141. extern void scsi_kunmap_atomic_sg(void *virt);
  142. extern int scsi_init_io(struct scsi_cmnd *cmd);
  143. #ifdef CONFIG_SCSI_DMA
  144. extern int scsi_dma_map(struct scsi_cmnd *cmd);
  145. extern void scsi_dma_unmap(struct scsi_cmnd *cmd);
  146. #else /* !CONFIG_SCSI_DMA */
  147. static inline int scsi_dma_map(struct scsi_cmnd *cmd) { return -ENOSYS; }
  148. static inline void scsi_dma_unmap(struct scsi_cmnd *cmd) { }
  149. #endif /* !CONFIG_SCSI_DMA */
  150. static inline unsigned scsi_sg_count(struct scsi_cmnd *cmd)
  151. {
  152. return cmd->sdb.table.nents;
  153. }
  154. static inline struct scatterlist *scsi_sglist(struct scsi_cmnd *cmd)
  155. {
  156. return cmd->sdb.table.sgl;
  157. }
  158. static inline unsigned scsi_bufflen(struct scsi_cmnd *cmd)
  159. {
  160. return cmd->sdb.length;
  161. }
  162. static inline void scsi_set_resid(struct scsi_cmnd *cmd, int resid)
  163. {
  164. cmd->sdb.resid = resid;
  165. }
  166. static inline int scsi_get_resid(struct scsi_cmnd *cmd)
  167. {
  168. return cmd->sdb.resid;
  169. }
  170. #define scsi_for_each_sg(cmd, sg, nseg, __i) \
  171. for_each_sg(scsi_sglist(cmd), sg, nseg, __i)
  172. static inline int scsi_bidi_cmnd(struct scsi_cmnd *cmd)
  173. {
  174. return blk_bidi_rq(cmd->request) &&
  175. (cmd->request->next_rq->special != NULL);
  176. }
  177. static inline struct scsi_data_buffer *scsi_in(struct scsi_cmnd *cmd)
  178. {
  179. return scsi_bidi_cmnd(cmd) ?
  180. cmd->request->next_rq->special : &cmd->sdb;
  181. }
  182. static inline struct scsi_data_buffer *scsi_out(struct scsi_cmnd *cmd)
  183. {
  184. return &cmd->sdb;
  185. }
  186. static inline int scsi_sg_copy_from_buffer(struct scsi_cmnd *cmd,
  187. void *buf, int buflen)
  188. {
  189. return sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  190. buf, buflen);
  191. }
  192. static inline int scsi_sg_copy_to_buffer(struct scsi_cmnd *cmd,
  193. void *buf, int buflen)
  194. {
  195. return sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  196. buf, buflen);
  197. }
  198. /*
  199. * The operations below are hints that tell the controller driver how
  200. * to handle I/Os with DIF or similar types of protection information.
  201. */
  202. enum scsi_prot_operations {
  203. /* Normal I/O */
  204. SCSI_PROT_NORMAL = 0,
  205. /* OS-HBA: Protected, HBA-Target: Unprotected */
  206. SCSI_PROT_READ_INSERT,
  207. SCSI_PROT_WRITE_STRIP,
  208. /* OS-HBA: Unprotected, HBA-Target: Protected */
  209. SCSI_PROT_READ_STRIP,
  210. SCSI_PROT_WRITE_INSERT,
  211. /* OS-HBA: Protected, HBA-Target: Protected */
  212. SCSI_PROT_READ_PASS,
  213. SCSI_PROT_WRITE_PASS,
  214. };
  215. static inline void scsi_set_prot_op(struct scsi_cmnd *scmd, unsigned char op)
  216. {
  217. scmd->prot_op = op;
  218. }
  219. static inline unsigned char scsi_get_prot_op(struct scsi_cmnd *scmd)
  220. {
  221. return scmd->prot_op;
  222. }
  223. enum scsi_prot_flags {
  224. SCSI_PROT_TRANSFER_PI = 1 << 0,
  225. SCSI_PROT_GUARD_CHECK = 1 << 1,
  226. SCSI_PROT_REF_CHECK = 1 << 2,
  227. SCSI_PROT_REF_INCREMENT = 1 << 3,
  228. SCSI_PROT_IP_CHECKSUM = 1 << 4,
  229. };
  230. /*
  231. * The controller usually does not know anything about the target it
  232. * is communicating with. However, when DIX is enabled the controller
  233. * must be know target type so it can verify the protection
  234. * information passed along with the I/O.
  235. */
  236. enum scsi_prot_target_type {
  237. SCSI_PROT_DIF_TYPE0 = 0,
  238. SCSI_PROT_DIF_TYPE1,
  239. SCSI_PROT_DIF_TYPE2,
  240. SCSI_PROT_DIF_TYPE3,
  241. };
  242. static inline void scsi_set_prot_type(struct scsi_cmnd *scmd, unsigned char type)
  243. {
  244. scmd->prot_type = type;
  245. }
  246. static inline unsigned char scsi_get_prot_type(struct scsi_cmnd *scmd)
  247. {
  248. return scmd->prot_type;
  249. }
  250. static inline sector_t scsi_get_lba(struct scsi_cmnd *scmd)
  251. {
  252. return blk_rq_pos(scmd->request);
  253. }
  254. static inline unsigned int scsi_prot_interval(struct scsi_cmnd *scmd)
  255. {
  256. return scmd->device->sector_size;
  257. }
  258. static inline unsigned scsi_prot_sg_count(struct scsi_cmnd *cmd)
  259. {
  260. return cmd->prot_sdb ? cmd->prot_sdb->table.nents : 0;
  261. }
  262. static inline struct scatterlist *scsi_prot_sglist(struct scsi_cmnd *cmd)
  263. {
  264. return cmd->prot_sdb ? cmd->prot_sdb->table.sgl : NULL;
  265. }
  266. static inline struct scsi_data_buffer *scsi_prot(struct scsi_cmnd *cmd)
  267. {
  268. return cmd->prot_sdb;
  269. }
  270. #define scsi_for_each_prot_sg(cmd, sg, nseg, __i) \
  271. for_each_sg(scsi_prot_sglist(cmd), sg, nseg, __i)
  272. static inline void set_msg_byte(struct scsi_cmnd *cmd, char status)
  273. {
  274. cmd->result = (cmd->result & 0xffff00ff) | (status << 8);
  275. }
  276. static inline void set_host_byte(struct scsi_cmnd *cmd, char status)
  277. {
  278. cmd->result = (cmd->result & 0xff00ffff) | (status << 16);
  279. }
  280. static inline void set_driver_byte(struct scsi_cmnd *cmd, char status)
  281. {
  282. cmd->result = (cmd->result & 0x00ffffff) | (status << 24);
  283. }
  284. static inline unsigned scsi_transfer_length(struct scsi_cmnd *scmd)
  285. {
  286. unsigned int xfer_len = scsi_out(scmd)->length;
  287. unsigned int prot_interval = scsi_prot_interval(scmd);
  288. if (scmd->prot_flags & SCSI_PROT_TRANSFER_PI)
  289. xfer_len += (xfer_len >> ilog2(prot_interval)) * 8;
  290. return xfer_len;
  291. }
  292. #endif /* _SCSI_SCSI_CMND_H */