scsi_cmnd.h 8.9 KB

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