iscsi_tcp.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * iSCSI Initiator TCP Transport
  3. * Copyright (C) 2004 Dmitry Yusupov
  4. * Copyright (C) 2004 Alex Aizman
  5. * Copyright (C) 2005 Mike Christie
  6. * maintained by open-iscsi@googlegroups.com
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published
  10. * by the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * See the file COPYING included with this distribution for more details.
  19. */
  20. #ifndef ISCSI_TCP_H
  21. #define ISCSI_TCP_H
  22. /* Session's states */
  23. #define ISCSI_STATE_FREE 1
  24. #define ISCSI_STATE_LOGGED_IN 2
  25. #define ISCSI_STATE_FAILED 3
  26. #define ISCSI_STATE_TERMINATE 4
  27. /* Connection's states */
  28. #define ISCSI_CONN_INITIAL_STAGE 0
  29. #define ISCSI_CONN_STARTED 1
  30. #define ISCSI_CONN_STOPPED 2
  31. #define ISCSI_CONN_CLEANUP_WAIT 3
  32. /* Connection suspend "bit" */
  33. #define SUSPEND_BIT 1
  34. /* Socket's Receive state machine */
  35. #define IN_PROGRESS_WAIT_HEADER 0x0
  36. #define IN_PROGRESS_HEADER_GATHER 0x1
  37. #define IN_PROGRESS_DATA_RECV 0x2
  38. #define IN_PROGRESS_DDIGEST_RECV 0x3
  39. /* Task Mgmt states */
  40. #define TMABORT_INITIAL 0x0
  41. #define TMABORT_SUCCESS 0x1
  42. #define TMABORT_FAILED 0x2
  43. #define TMABORT_TIMEDOUT 0x3
  44. /* xmit state machine */
  45. #define XMSTATE_IDLE 0x0
  46. #define XMSTATE_R_HDR 0x1
  47. #define XMSTATE_W_HDR 0x2
  48. #define XMSTATE_IMM_HDR 0x4
  49. #define XMSTATE_IMM_DATA 0x8
  50. #define XMSTATE_UNS_INIT 0x10
  51. #define XMSTATE_UNS_HDR 0x20
  52. #define XMSTATE_UNS_DATA 0x40
  53. #define XMSTATE_SOL_HDR 0x80
  54. #define XMSTATE_SOL_DATA 0x100
  55. #define XMSTATE_W_PAD 0x200
  56. #define XMSTATE_DATA_DIGEST 0x400
  57. #define ISCSI_CONN_MAX 1
  58. #define ISCSI_CONN_RCVBUF_MIN 262144
  59. #define ISCSI_CONN_SNDBUF_MIN 262144
  60. #define ISCSI_PAD_LEN 4
  61. #define ISCSI_R2T_MAX 16
  62. #define ISCSI_XMIT_CMDS_MAX 128 /* must be power of 2 */
  63. #define ISCSI_MGMT_CMDS_MAX 32 /* must be power of 2 */
  64. #define ISCSI_MGMT_ITT_OFFSET 0xa00
  65. #define ISCSI_SG_TABLESIZE SG_ALL
  66. #define ISCSI_DEF_CMD_PER_LUN 32
  67. #define ISCSI_MAX_CMD_PER_LUN 128
  68. #define ISCSI_TCP_MAX_CMD_LEN 16
  69. #define ITT_MASK (0xfff)
  70. #define CID_SHIFT 12
  71. #define CID_MASK (0xffff<<CID_SHIFT)
  72. #define AGE_SHIFT 28
  73. #define AGE_MASK (0xf<<AGE_SHIFT)
  74. struct iscsi_queue {
  75. struct kfifo *queue; /* FIFO Queue */
  76. void **pool; /* Pool of elements */
  77. int max; /* Max number of elements */
  78. };
  79. struct iscsi_session;
  80. struct iscsi_cmd_task;
  81. struct iscsi_mgmt_task;
  82. /* Socket connection recieve helper */
  83. struct iscsi_tcp_recv {
  84. struct iscsi_hdr *hdr;
  85. struct sk_buff *skb;
  86. int offset;
  87. int len;
  88. int hdr_offset;
  89. int copy;
  90. int copied;
  91. int padding;
  92. struct iscsi_cmd_task *ctask; /* current cmd in progress */
  93. /* copied and flipped values */
  94. int opcode;
  95. int flags;
  96. int cmd_status;
  97. int ahslen;
  98. int datalen;
  99. uint32_t itt;
  100. int datadgst;
  101. };
  102. struct iscsi_conn {
  103. struct iscsi_hdr hdr; /* header placeholder */
  104. char hdrext[4*sizeof(__u16) +
  105. sizeof(__u32)];
  106. int data_copied;
  107. char *data; /* data placeholder */
  108. struct socket *sock; /* TCP socket */
  109. int data_size; /* actual recv_dlength */
  110. int stop_stage; /* conn_stop() flag: *
  111. * stop to recover, *
  112. * stop to terminate */
  113. /* iSCSI connection-wide sequencing */
  114. uint32_t exp_statsn;
  115. int hdr_size; /* PDU header size */
  116. unsigned long suspend_rx; /* suspend Rx */
  117. struct crypto_tfm *rx_tfm; /* CRC32C (Rx) */
  118. struct crypto_tfm *data_rx_tfm; /* CRC32C (Rx) for data */
  119. /* control data */
  120. int senselen; /* scsi sense length */
  121. int id; /* CID */
  122. struct iscsi_tcp_recv in; /* TCP receive context */
  123. struct iscsi_session *session; /* parent session */
  124. struct list_head item; /* maintains list of conns */
  125. int in_progress; /* connection state machine */
  126. int c_stage; /* connection state */
  127. struct iscsi_mgmt_task *login_mtask; /* mtask used for login/text */
  128. struct iscsi_mgmt_task *mtask; /* xmit mtask in progress */
  129. struct iscsi_cmd_task *ctask; /* xmit ctask in progress */
  130. spinlock_t lock; /* FIXME: to be removed */
  131. /* old values for socket callbacks */
  132. void (*old_data_ready)(struct sock *, int);
  133. void (*old_state_change)(struct sock *);
  134. void (*old_write_space)(struct sock *);
  135. /* xmit */
  136. struct crypto_tfm *tx_tfm; /* CRC32C (Tx) */
  137. struct crypto_tfm *data_tx_tfm; /* CRC32C (Tx) for data */
  138. struct kfifo *writequeue; /* write cmds for Data-Outs */
  139. struct kfifo *immqueue; /* immediate xmit queue */
  140. struct kfifo *mgmtqueue; /* mgmt (control) xmit queue */
  141. struct kfifo *xmitqueue; /* data-path cmd queue */
  142. struct work_struct xmitwork; /* per-conn. xmit workqueue */
  143. struct mutex xmitmutex; /* serializes connection xmit,
  144. * access to kfifos: *
  145. * xmitqueue, writequeue, *
  146. * immqueue, mgmtqueue */
  147. unsigned long suspend_tx; /* suspend Tx */
  148. /* abort */
  149. wait_queue_head_t ehwait; /* used in eh_abort() */
  150. struct iscsi_tm tmhdr;
  151. struct timer_list tmabort_timer; /* abort timer */
  152. int tmabort_state; /* see TMABORT_INITIAL, etc.*/
  153. /* negotiated params */
  154. int max_recv_dlength;
  155. int max_xmit_dlength;
  156. int hdrdgst_en;
  157. int datadgst_en;
  158. /* MIB-statistics */
  159. uint64_t txdata_octets;
  160. uint64_t rxdata_octets;
  161. uint32_t scsicmd_pdus_cnt;
  162. uint32_t dataout_pdus_cnt;
  163. uint32_t scsirsp_pdus_cnt;
  164. uint32_t datain_pdus_cnt;
  165. uint32_t r2t_pdus_cnt;
  166. uint32_t tmfcmd_pdus_cnt;
  167. int32_t tmfrsp_pdus_cnt;
  168. /* custom statistics */
  169. uint32_t sendpage_failures_cnt;
  170. uint32_t discontiguous_hdr_cnt;
  171. uint32_t eh_abort_cnt;
  172. ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int);
  173. };
  174. struct iscsi_session {
  175. /* iSCSI session-wide sequencing */
  176. uint32_t cmdsn;
  177. uint32_t exp_cmdsn;
  178. uint32_t max_cmdsn;
  179. /* configuration */
  180. int initial_r2t_en;
  181. int max_r2t;
  182. int imm_data_en;
  183. int first_burst;
  184. int max_burst;
  185. int time2wait;
  186. int time2retain;
  187. int pdu_inorder_en;
  188. int dataseq_inorder_en;
  189. int erl;
  190. int ifmarker_en;
  191. int ofmarker_en;
  192. /* control data */
  193. struct Scsi_Host *host;
  194. int id;
  195. struct iscsi_conn *leadconn; /* leading connection */
  196. spinlock_t lock; /* protects session state, *
  197. * sequence numbers, *
  198. * session resources: *
  199. * - cmdpool, *
  200. * - mgmtpool, *
  201. * - r2tpool */
  202. int state; /* session state */
  203. struct list_head item;
  204. void *auth_client;
  205. int conn_cnt;
  206. int age; /* counts session re-opens */
  207. struct list_head connections; /* list of connections */
  208. int cmds_max; /* size of cmds array */
  209. struct iscsi_cmd_task **cmds; /* Original Cmds arr */
  210. struct iscsi_queue cmdpool; /* PDU's pool */
  211. int mgmtpool_max; /* size of mgmt array */
  212. struct iscsi_mgmt_task **mgmt_cmds; /* Original mgmt arr */
  213. struct iscsi_queue mgmtpool; /* Mgmt PDU's pool */
  214. };
  215. struct iscsi_buf {
  216. struct scatterlist sg;
  217. unsigned int sent;
  218. char use_sendmsg;
  219. };
  220. struct iscsi_data_task {
  221. struct iscsi_data hdr; /* PDU */
  222. char hdrext[sizeof(__u32)]; /* Header-Digest */
  223. struct list_head item; /* data queue item */
  224. struct iscsi_buf digestbuf; /* digest buffer */
  225. uint32_t digest; /* data digest */
  226. };
  227. #define ISCSI_DTASK_DEFAULT_MAX ISCSI_SG_TABLESIZE * PAGE_SIZE / 512
  228. struct iscsi_mgmt_task {
  229. struct iscsi_hdr hdr; /* mgmt. PDU */
  230. char hdrext[sizeof(__u32)]; /* Header-Digest */
  231. char *data; /* mgmt payload */
  232. int xmstate; /* mgmt xmit progress */
  233. int data_count; /* counts data to be sent */
  234. struct iscsi_buf headbuf; /* header buffer */
  235. struct iscsi_buf sendbuf; /* in progress buffer */
  236. int sent;
  237. uint32_t itt; /* this ITT */
  238. };
  239. struct iscsi_r2t_info {
  240. __be32 ttt; /* copied from R2T */
  241. __be32 exp_statsn; /* copied from R2T */
  242. uint32_t data_length; /* copied from R2T */
  243. uint32_t data_offset; /* copied from R2T */
  244. struct iscsi_buf headbuf; /* Data-Out Header Buffer */
  245. struct iscsi_buf sendbuf; /* Data-Out in progress buffer*/
  246. int sent; /* R2T sequence progress */
  247. int data_count; /* DATA-Out payload progress */
  248. struct scatterlist *sg; /* per-R2T SG list */
  249. int solicit_datasn;
  250. struct iscsi_data_task *dtask; /* which data task */
  251. };
  252. struct iscsi_cmd_task {
  253. struct iscsi_cmd hdr; /* iSCSI PDU header */
  254. char hdrext[4*sizeof(__u16)+ /* AHS */
  255. sizeof(__u32)]; /* HeaderDigest */
  256. char pad[ISCSI_PAD_LEN];
  257. int itt; /* this ITT */
  258. int datasn; /* DataSN */
  259. struct iscsi_buf headbuf; /* header buf (xmit) */
  260. struct iscsi_buf sendbuf; /* in progress buffer*/
  261. int sent;
  262. struct scatterlist *sg; /* per-cmd SG list */
  263. struct scatterlist *bad_sg; /* assert statement */
  264. int sg_count; /* SG's to process */
  265. uint32_t unsol_datasn;
  266. uint32_t exp_r2tsn;
  267. int xmstate; /* xmit xtate machine */
  268. int imm_count; /* imm-data (bytes) */
  269. int unsol_count; /* unsolicited (bytes)*/
  270. int r2t_data_count; /* R2T Data-Out bytes */
  271. int data_count; /* remaining Data-Out */
  272. int pad_count; /* padded bytes */
  273. struct scsi_cmnd *sc; /* associated SCSI cmd*/
  274. int total_length;
  275. int data_offset;
  276. struct iscsi_conn *conn; /* used connection */
  277. struct iscsi_mgmt_task *mtask; /* tmf mtask in progr */
  278. struct iscsi_r2t_info *r2t; /* in progress R2T */
  279. struct iscsi_queue r2tpool;
  280. struct kfifo *r2tqueue;
  281. struct iscsi_r2t_info **r2ts;
  282. struct list_head dataqueue; /* Data-Out dataqueue */
  283. mempool_t *datapool;
  284. uint32_t datadigest; /* for recover digest */
  285. int digest_count;
  286. uint32_t immdigest; /* for imm data */
  287. struct iscsi_buf immbuf; /* for imm data digest */
  288. struct iscsi_data_task *dtask; /* data task in progress*/
  289. int digest_offset; /* for partial buff digest */
  290. };
  291. #endif /* ISCSI_H */