scsi_cmnd.h 8.2 KB

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