[SCSI] iscsi_tcp: rewrite recv path
[safe/jmp/linux-2.6] / include / scsi / libiscsi.h
1 /*
2  * iSCSI lib definitions
3  *
4  * Copyright (C) 2006 Red Hat, Inc.  All rights reserved.
5  * Copyright (C) 2004 - 2006 Mike Christie
6  * Copyright (C) 2004 - 2005 Dmitry Yusupov
7  * Copyright (C) 2004 - 2005 Alex Aizman
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */
23 #ifndef LIBISCSI_H
24 #define LIBISCSI_H
25
26 #include <linux/types.h>
27 #include <linux/mutex.h>
28 #include <linux/timer.h>
29 #include <linux/workqueue.h>
30 #include <scsi/iscsi_proto.h>
31 #include <scsi/iscsi_if.h>
32
33 struct scsi_transport_template;
34 struct scsi_device;
35 struct Scsi_Host;
36 struct scsi_cmnd;
37 struct socket;
38 struct iscsi_transport;
39 struct iscsi_cls_session;
40 struct iscsi_cls_conn;
41 struct iscsi_session;
42 struct iscsi_nopin;
43
44 /* #define DEBUG_SCSI */
45 #ifdef DEBUG_SCSI
46 #define debug_scsi(fmt...) printk(KERN_INFO "iscsi: " fmt)
47 #else
48 #define debug_scsi(fmt...)
49 #endif
50
51 #define ISCSI_DEF_XMIT_CMDS_MAX 128     /* must be power of 2 */
52 #define ISCSI_MGMT_CMDS_MAX     16      /* must be power of 2 */
53
54 #define ISCSI_MGMT_ITT_OFFSET   0xa00
55
56 #define ISCSI_DEF_CMD_PER_LUN           32
57 #define ISCSI_MAX_CMD_PER_LUN           128
58
59 /* Task Mgmt states */
60 enum {
61         TMF_INITIAL,
62         TMF_QUEUED,
63         TMF_SUCCESS,
64         TMF_FAILED,
65         TMF_TIMEDOUT,
66         TMF_NOT_FOUND,
67 };
68
69 /* Connection suspend "bit" */
70 #define ISCSI_SUSPEND_BIT               1
71
72 #define ISCSI_ITT_MASK                  (0xfff)
73 #define ISCSI_CID_SHIFT                 12
74 #define ISCSI_CID_MASK                  (0xffff << ISCSI_CID_SHIFT)
75 #define ISCSI_AGE_SHIFT                 28
76 #define ISCSI_AGE_MASK                  (0xf << ISCSI_AGE_SHIFT)
77
78 #define ISCSI_ADDRESS_BUF_LEN           64
79
80 enum {
81         ISCSI_DIGEST_SIZE = sizeof(__u32),
82 };
83
84 struct iscsi_mgmt_task {
85         /*
86          * Becuae LLDs allocate their hdr differently, this is a pointer to
87          * that storage. It must be setup at session creation time.
88          */
89         struct iscsi_hdr        *hdr;
90         char                    *data;          /* mgmt payload */
91         unsigned                data_count;     /* counts data to be sent */
92         uint32_t                itt;            /* this ITT */
93         void                    *dd_data;       /* driver/transport data */
94         struct list_head        running;
95 };
96
97 enum {
98         ISCSI_TASK_COMPLETED,
99         ISCSI_TASK_PENDING,
100         ISCSI_TASK_RUNNING,
101 };
102
103 struct iscsi_cmd_task {
104         /*
105          * Becuae LLDs allocate their hdr differently, this is a pointer to
106          * that storage. It must be setup at session creation time.
107          */
108         struct iscsi_cmd        *hdr;
109         int                     itt;            /* this ITT */
110
111         uint32_t                unsol_datasn;
112         unsigned                imm_count;      /* imm-data (bytes)   */
113         unsigned                unsol_count;    /* unsolicited (bytes)*/
114         /* offset in unsolicited stream (bytes); */
115         unsigned                unsol_offset;
116         unsigned                data_count;     /* remaining Data-Out */
117         struct scsi_cmnd        *sc;            /* associated SCSI cmd*/
118         struct iscsi_conn       *conn;          /* used connection    */
119
120         /* state set/tested under session->lock */
121         int                     state;
122         atomic_t                refcount;
123         struct list_head        running;        /* running cmd list */
124         void                    *dd_data;       /* driver/transport data */
125 };
126
127 struct iscsi_conn {
128         struct iscsi_cls_conn   *cls_conn;      /* ptr to class connection */
129         void                    *dd_data;       /* iscsi_transport data */
130         struct iscsi_session    *session;       /* parent session */
131         /*
132          * LLDs should set this lock. It protects the transport recv
133          * code
134          */
135         rwlock_t                *recv_lock;
136         /*
137          * conn_stop() flag: stop to recover, stop to terminate
138          */
139         int                     stop_stage;
140
141         /* iSCSI connection-wide sequencing */
142         uint32_t                exp_statsn;
143
144         /* control data */
145         int                     id;             /* CID */
146         int                     c_stage;        /* connection state */
147         /*
148          * Preallocated buffer for pdus that have data but do not
149          * originate from scsi-ml. We never have two pdus using the
150          * buffer at the same time. It is only allocated to
151          * the default max recv size because the pdus we support
152          * should always fit in this buffer
153          */
154         char                    *data;
155         struct iscsi_mgmt_task  *login_mtask;   /* mtask used for login/text */
156         struct iscsi_mgmt_task  *mtask;         /* xmit mtask in progress */
157         struct iscsi_cmd_task   *ctask;         /* xmit ctask in progress */
158
159         /* xmit */
160         struct list_head        mgmtqueue;      /* mgmt (control) xmit queue */
161         struct list_head        mgmt_run_list;  /* list of control tasks */
162         struct list_head        xmitqueue;      /* data-path cmd queue */
163         struct list_head        run_list;       /* list of cmds in progress */
164         struct list_head        requeue;        /* tasks needing another run */
165         struct work_struct      xmitwork;       /* per-conn. xmit workqueue */
166         unsigned long           suspend_tx;     /* suspend Tx */
167         unsigned long           suspend_rx;     /* suspend Rx */
168
169         /* abort */
170         wait_queue_head_t       ehwait;         /* used in eh_abort() */
171         struct iscsi_tm         tmhdr;
172         struct timer_list       tmf_timer;
173         int                     tmf_state;      /* see TMF_INITIAL, etc.*/
174
175         /* negotiated params */
176         unsigned                max_recv_dlength; /* initiator_max_recv_dsl*/
177         unsigned                max_xmit_dlength; /* target_max_recv_dsl */
178         int                     hdrdgst_en;
179         int                     datadgst_en;
180         int                     ifmarker_en;
181         int                     ofmarker_en;
182         /* values userspace uses to id a conn */
183         int                     persistent_port;
184         char                    *persistent_address;
185         /* remote portal currently connected to */
186         int                     portal_port;
187         char                    portal_address[ISCSI_ADDRESS_BUF_LEN];
188         /* local address */
189         int                     local_port;
190         char                    local_address[ISCSI_ADDRESS_BUF_LEN];
191
192         /* MIB-statistics */
193         uint64_t                txdata_octets;
194         uint64_t                rxdata_octets;
195         uint32_t                scsicmd_pdus_cnt;
196         uint32_t                dataout_pdus_cnt;
197         uint32_t                scsirsp_pdus_cnt;
198         uint32_t                datain_pdus_cnt;
199         uint32_t                r2t_pdus_cnt;
200         uint32_t                tmfcmd_pdus_cnt;
201         int32_t                 tmfrsp_pdus_cnt;
202
203         /* custom statistics */
204         uint32_t                eh_abort_cnt;
205 };
206
207 struct iscsi_queue {
208         struct kfifo            *queue;         /* FIFO Queue */
209         void                    **pool;         /* Pool of elements */
210         int                     max;            /* Max number of elements */
211 };
212
213 struct iscsi_session {
214         /*
215          * Syncs up the scsi eh thread with the iscsi eh thread when sending
216          * task management functions. This must be taken before the session
217          * and recv lock.
218          */
219         struct mutex            eh_mutex;
220
221         /* iSCSI session-wide sequencing */
222         uint32_t                cmdsn;
223         uint32_t                exp_cmdsn;
224         uint32_t                max_cmdsn;
225
226         /* This tracks the reqs queued into the initiator */
227         uint32_t                queued_cmdsn;
228
229         /* configuration */
230         int                     initial_r2t_en;
231         unsigned                max_r2t;
232         int                     imm_data_en;
233         unsigned                first_burst;
234         unsigned                max_burst;
235         int                     time2wait;
236         int                     time2retain;
237         int                     pdu_inorder_en;
238         int                     dataseq_inorder_en;
239         int                     erl;
240         int                     fast_abort;
241         int                     tpgt;
242         char                    *username;
243         char                    *username_in;
244         char                    *password;
245         char                    *password_in;
246         char                    *targetname;
247         char                    *initiatorname;
248         /* hw address or netdev iscsi connection is bound to */
249         char                    *hwaddress;
250         char                    *netdev;
251         /* control data */
252         struct iscsi_transport  *tt;
253         struct Scsi_Host        *host;
254         struct iscsi_conn       *leadconn;      /* leading connection */
255         spinlock_t              lock;           /* protects session state, *
256                                                  * sequence numbers,       *
257                                                  * session resources:      *
258                                                  * - cmdpool,              *
259                                                  * - mgmtpool,             *
260                                                  * - r2tpool               */
261         int                     state;          /* session state           */
262         int                     age;            /* counts session re-opens */
263
264         int                     cmds_max;       /* size of cmds array */
265         struct iscsi_cmd_task   **cmds;         /* Original Cmds arr */
266         struct iscsi_queue      cmdpool;        /* PDU's pool */
267         int                     mgmtpool_max;   /* size of mgmt array */
268         struct iscsi_mgmt_task  **mgmt_cmds;    /* Original mgmt arr */
269         struct iscsi_queue      mgmtpool;       /* Mgmt PDU's pool */
270 };
271
272 /*
273  * scsi host template
274  */
275 extern int iscsi_change_queue_depth(struct scsi_device *sdev, int depth);
276 extern int iscsi_eh_abort(struct scsi_cmnd *sc);
277 extern int iscsi_eh_host_reset(struct scsi_cmnd *sc);
278 extern int iscsi_eh_device_reset(struct scsi_cmnd *sc);
279 extern int iscsi_queuecommand(struct scsi_cmnd *sc,
280                               void (*done)(struct scsi_cmnd *));
281
282
283 /*
284  * iSCSI host helpers.
285  */
286 extern int iscsi_host_set_param(struct Scsi_Host *shost,
287                                 enum iscsi_host_param param, char *buf,
288                                 int buflen);
289 extern int iscsi_host_get_param(struct Scsi_Host *shost,
290                                 enum iscsi_host_param param, char *buf);
291
292 /*
293  * session management
294  */
295 extern struct iscsi_cls_session *
296 iscsi_session_setup(struct iscsi_transport *, struct scsi_transport_template *,
297                     uint16_t, uint16_t, int, int, uint32_t, uint32_t *);
298 extern void iscsi_session_teardown(struct iscsi_cls_session *);
299 extern struct iscsi_session *class_to_transport_session(struct iscsi_cls_session *);
300 extern void iscsi_session_recovery_timedout(struct iscsi_cls_session *);
301 extern int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
302                            enum iscsi_param param, char *buf, int buflen);
303 extern int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
304                                    enum iscsi_param param, char *buf);
305
306 #define session_to_cls(_sess) \
307         hostdata_session(_sess->host->hostdata)
308
309 /*
310  * connection management
311  */
312 extern struct iscsi_cls_conn *iscsi_conn_setup(struct iscsi_cls_session *,
313                                                uint32_t);
314 extern void iscsi_conn_teardown(struct iscsi_cls_conn *);
315 extern int iscsi_conn_start(struct iscsi_cls_conn *);
316 extern void iscsi_conn_stop(struct iscsi_cls_conn *, int);
317 extern int iscsi_conn_bind(struct iscsi_cls_session *, struct iscsi_cls_conn *,
318                            int);
319 extern void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err);
320 extern int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
321                                 enum iscsi_param param, char *buf);
322
323 /*
324  * pdu and task processing
325  */
326 extern void iscsi_update_cmdsn(struct iscsi_session *, struct iscsi_nopin *);
327 extern void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *,
328                                         struct iscsi_data *hdr);
329 extern int iscsi_conn_send_pdu(struct iscsi_cls_conn *, struct iscsi_hdr *,
330                                 char *, uint32_t);
331 extern int iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
332                               char *, int);
333 extern int __iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
334                                 char *, int);
335 extern int iscsi_verify_itt(struct iscsi_conn *, struct iscsi_hdr *,
336                             uint32_t *);
337 extern void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask);
338
339 /*
340  * generic helpers
341  */
342 extern void iscsi_pool_free(struct iscsi_queue *, void **);
343 extern int iscsi_pool_init(struct iscsi_queue *, int, void ***, int);
344
345 #endif