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