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