scsi_cmnd.h 9.6 KB

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