scsi_cmnd.h 8.6 KB

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