iscsi_tcp.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_cls_conn;
  103. struct iscsi_conn {
  104. struct iscsi_cls_conn *cls_conn; /* ptr to class connection */
  105. struct iscsi_hdr hdr; /* header placeholder */
  106. char hdrext[4*sizeof(__u16) +
  107. sizeof(__u32)];
  108. int data_copied;
  109. char *data; /* data placeholder */
  110. struct socket *sock; /* TCP socket */
  111. int data_size; /* actual recv_dlength */
  112. int stop_stage; /* conn_stop() flag: *
  113. * stop to recover, *
  114. * stop to terminate */
  115. /* iSCSI connection-wide sequencing */
  116. uint32_t exp_statsn;
  117. int hdr_size; /* PDU header size */
  118. unsigned long suspend_rx; /* suspend Rx */
  119. struct crypto_tfm *rx_tfm; /* CRC32C (Rx) */
  120. struct crypto_tfm *data_rx_tfm; /* CRC32C (Rx) for data */
  121. /* control data */
  122. int senselen; /* scsi sense length */
  123. int id; /* CID */
  124. struct iscsi_tcp_recv in; /* TCP receive context */
  125. struct iscsi_session *session; /* parent session */
  126. struct list_head item; /* maintains list of conns */
  127. int in_progress; /* connection state machine */
  128. int c_stage; /* connection state */
  129. struct iscsi_mgmt_task *login_mtask; /* mtask used for login/text */
  130. struct iscsi_mgmt_task *mtask; /* xmit mtask in progress */
  131. struct iscsi_cmd_task *ctask; /* xmit ctask in progress */
  132. /* old values for socket callbacks */
  133. void (*old_data_ready)(struct sock *, int);
  134. void (*old_state_change)(struct sock *);
  135. void (*old_write_space)(struct sock *);
  136. /* xmit */
  137. struct crypto_tfm *tx_tfm; /* CRC32C (Tx) */
  138. struct crypto_tfm *data_tx_tfm; /* CRC32C (Tx) for data */
  139. struct kfifo *writequeue; /* write cmds for Data-Outs */
  140. struct kfifo *immqueue; /* immediate xmit queue */
  141. struct kfifo *mgmtqueue; /* mgmt (control) xmit queue */
  142. struct kfifo *xmitqueue; /* data-path cmd queue */
  143. struct work_struct xmitwork; /* per-conn. xmit workqueue */
  144. struct mutex xmitmutex; /* serializes connection xmit,
  145. * access to kfifos: *
  146. * xmitqueue, writequeue, *
  147. * immqueue, mgmtqueue */
  148. unsigned long suspend_tx; /* suspend Tx */
  149. /* abort */
  150. wait_queue_head_t ehwait; /* used in eh_abort() */
  151. struct iscsi_tm tmhdr;
  152. struct timer_list tmabort_timer; /* abort timer */
  153. int tmabort_state; /* see TMABORT_INITIAL, etc.*/
  154. /* negotiated params */
  155. int max_recv_dlength;
  156. int max_xmit_dlength;
  157. int hdrdgst_en;
  158. int datadgst_en;
  159. /* MIB-statistics */
  160. uint64_t txdata_octets;
  161. uint64_t rxdata_octets;
  162. uint32_t scsicmd_pdus_cnt;
  163. uint32_t dataout_pdus_cnt;
  164. uint32_t scsirsp_pdus_cnt;
  165. uint32_t datain_pdus_cnt;
  166. uint32_t r2t_pdus_cnt;
  167. uint32_t tmfcmd_pdus_cnt;
  168. int32_t tmfrsp_pdus_cnt;
  169. /* custom statistics */
  170. uint32_t sendpage_failures_cnt;
  171. uint32_t discontiguous_hdr_cnt;
  172. uint32_t eh_abort_cnt;
  173. ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int);
  174. };
  175. struct iscsi_session {
  176. /* iSCSI session-wide sequencing */
  177. uint32_t cmdsn;
  178. uint32_t exp_cmdsn;
  179. uint32_t max_cmdsn;
  180. /* configuration */
  181. int initial_r2t_en;
  182. int max_r2t;
  183. int imm_data_en;
  184. int first_burst;
  185. int max_burst;
  186. int time2wait;
  187. int time2retain;
  188. int pdu_inorder_en;
  189. int dataseq_inorder_en;
  190. int erl;
  191. int ifmarker_en;
  192. int ofmarker_en;
  193. /* control data */
  194. struct Scsi_Host *host;
  195. int id;
  196. struct iscsi_conn *leadconn; /* leading connection */
  197. spinlock_t lock; /* protects session state, *
  198. * sequence numbers, *
  199. * session resources: *
  200. * - cmdpool, *
  201. * - mgmtpool, *
  202. * - r2tpool */
  203. int state; /* session state */
  204. struct list_head item;
  205. void *auth_client;
  206. int conn_cnt;
  207. int age; /* counts session re-opens */
  208. struct list_head connections; /* list of connections */
  209. int cmds_max; /* size of cmds array */
  210. struct iscsi_cmd_task **cmds; /* Original Cmds arr */
  211. struct iscsi_queue cmdpool; /* PDU's pool */
  212. int mgmtpool_max; /* size of mgmt array */
  213. struct iscsi_mgmt_task **mgmt_cmds; /* Original mgmt arr */
  214. struct iscsi_queue mgmtpool; /* Mgmt PDU's pool */
  215. };
  216. struct iscsi_buf {
  217. struct scatterlist sg;
  218. unsigned int sent;
  219. char use_sendmsg;
  220. };
  221. struct iscsi_data_task {
  222. struct iscsi_data hdr; /* PDU */
  223. char hdrext[sizeof(__u32)]; /* Header-Digest */
  224. struct list_head item; /* data queue item */
  225. struct iscsi_buf digestbuf; /* digest buffer */
  226. uint32_t digest; /* data digest */
  227. };
  228. #define ISCSI_DTASK_DEFAULT_MAX ISCSI_SG_TABLESIZE * PAGE_SIZE / 512
  229. struct iscsi_mgmt_task {
  230. struct iscsi_hdr hdr; /* mgmt. PDU */
  231. char hdrext[sizeof(__u32)]; /* Header-Digest */
  232. char *data; /* mgmt payload */
  233. int xmstate; /* mgmt xmit progress */
  234. int data_count; /* counts data to be sent */
  235. struct iscsi_buf headbuf; /* header buffer */
  236. struct iscsi_buf sendbuf; /* in progress buffer */
  237. int sent;
  238. uint32_t itt; /* this ITT */
  239. };
  240. struct iscsi_r2t_info {
  241. __be32 ttt; /* copied from R2T */
  242. __be32 exp_statsn; /* copied from R2T */
  243. uint32_t data_length; /* copied from R2T */
  244. uint32_t data_offset; /* copied from R2T */
  245. struct iscsi_buf headbuf; /* Data-Out Header Buffer */
  246. struct iscsi_buf sendbuf; /* Data-Out in progress buffer*/
  247. int sent; /* R2T sequence progress */
  248. int data_count; /* DATA-Out payload progress */
  249. struct scatterlist *sg; /* per-R2T SG list */
  250. int solicit_datasn;
  251. struct iscsi_data_task *dtask; /* which data task */
  252. };
  253. struct iscsi_cmd_task {
  254. struct iscsi_cmd hdr; /* iSCSI PDU header */
  255. char hdrext[4*sizeof(__u16)+ /* AHS */
  256. sizeof(__u32)]; /* HeaderDigest */
  257. char pad[ISCSI_PAD_LEN];
  258. int itt; /* this ITT */
  259. int datasn; /* DataSN */
  260. struct iscsi_buf headbuf; /* header buf (xmit) */
  261. struct iscsi_buf sendbuf; /* in progress buffer*/
  262. int sent;
  263. struct scatterlist *sg; /* per-cmd SG list */
  264. struct scatterlist *bad_sg; /* assert statement */
  265. int sg_count; /* SG's to process */
  266. uint32_t unsol_datasn;
  267. uint32_t exp_r2tsn;
  268. int xmstate; /* xmit xtate machine */
  269. int imm_count; /* imm-data (bytes) */
  270. int unsol_count; /* unsolicited (bytes)*/
  271. int r2t_data_count; /* R2T Data-Out bytes */
  272. int data_count; /* remaining Data-Out */
  273. int pad_count; /* padded bytes */
  274. struct scsi_cmnd *sc; /* associated SCSI cmd*/
  275. int total_length;
  276. int data_offset;
  277. struct iscsi_conn *conn; /* used connection */
  278. struct iscsi_mgmt_task *mtask; /* tmf mtask in progr */
  279. struct iscsi_r2t_info *r2t; /* in progress R2T */
  280. struct iscsi_queue r2tpool;
  281. struct kfifo *r2tqueue;
  282. struct iscsi_r2t_info **r2ts;
  283. struct list_head dataqueue; /* Data-Out dataqueue */
  284. mempool_t *datapool;
  285. uint32_t datadigest; /* for recover digest */
  286. int digest_count;
  287. uint32_t immdigest; /* for imm data */
  288. struct iscsi_buf immbuf; /* for imm data digest */
  289. struct iscsi_data_task *dtask; /* data task in progress*/
  290. int digest_offset; /* for partial buff digest */
  291. };
  292. #endif /* ISCSI_H */