blob: 6ce4109efdf3365a39ab736597bdd2fa39d696fb [file] [log] [blame]
Alex Aizman7ba24712005-08-04 19:30:08 -07001/*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
Mike Christie5bb0b552006-04-06 21:26:46 -05006 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
Alex Aizman7ba24712005-08-04 19:30:08 -07008 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * See the file COPYING included with this distribution for more details.
21 *
22 * Credits:
23 * Christoph Hellwig
24 * FUJITA Tomonori
25 * Arne Redlich
26 * Zhenyu Wang
27 */
28
29#include <linux/types.h>
30#include <linux/list.h>
31#include <linux/inet.h>
Mike Christie4e7aba72007-05-30 12:57:23 -050032#include <linux/file.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070033#include <linux/blkdev.h>
34#include <linux/crypto.h>
35#include <linux/delay.h>
36#include <linux/kfifo.h>
37#include <linux/scatterlist.h>
38#include <net/tcp.h>
39#include <scsi/scsi_cmnd.h>
Mike Christied1d81c02007-05-30 12:57:21 -050040#include <scsi/scsi_device.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070041#include <scsi/scsi_host.h>
42#include <scsi/scsi.h>
43#include <scsi/scsi_transport_iscsi.h>
44
45#include "iscsi_tcp.h"
46
47MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
48 "Alex Aizman <itn780@yahoo.com>");
49MODULE_DESCRIPTION("iSCSI/TCP data-path");
50MODULE_LICENSE("GPL");
Alex Aizman7ba24712005-08-04 19:30:08 -070051/* #define DEBUG_TCP */
Alex Aizman7ba24712005-08-04 19:30:08 -070052#define DEBUG_ASSERT
53
54#ifdef DEBUG_TCP
Mike Christie5bb0b552006-04-06 21:26:46 -050055#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
Alex Aizman7ba24712005-08-04 19:30:08 -070056#else
57#define debug_tcp(fmt...)
58#endif
59
Alex Aizman7ba24712005-08-04 19:30:08 -070060#ifndef DEBUG_ASSERT
61#ifdef BUG_ON
62#undef BUG_ON
63#endif
64#define BUG_ON(expr)
65#endif
66
Alex Aizman7ba24712005-08-04 19:30:08 -070067static unsigned int iscsi_max_lun = 512;
68module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
69
Alex Aizman7ba24712005-08-04 19:30:08 -070070static inline void
Alex Aizman7ba24712005-08-04 19:30:08 -070071iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
72{
Jens Axboe45711f12007-10-22 21:19:53 +020073 sg_init_one(&ibuf->sg, vbuf, size);
Alex Aizman7ba24712005-08-04 19:30:08 -070074 ibuf->sent = 0;
Mike Christie7cae5152006-01-13 18:05:47 -060075 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070076}
77
78static inline void
79iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
80{
Jens Axboe45711f12007-10-22 21:19:53 +020081 sg_init_table(&ibuf->sg, 1);
82 sg_set_page(&ibuf->sg, sg_page(sg));
Mike Christie7cae5152006-01-13 18:05:47 -060083 ibuf->sg.offset = sg->offset;
84 ibuf->sg.length = sg->length;
Alex Aizman7ba24712005-08-04 19:30:08 -070085 /*
86 * Fastpath: sg element fits into single page
87 */
Jens Axboe45711f12007-10-22 21:19:53 +020088 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg_page(sg)))
Mike Christie7cae5152006-01-13 18:05:47 -060089 ibuf->use_sendmsg = 0;
90 else
91 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070092 ibuf->sent = 0;
93}
94
95static inline int
96iscsi_buf_left(struct iscsi_buf *ibuf)
97{
98 int rc;
99
100 rc = ibuf->sg.length - ibuf->sent;
101 BUG_ON(rc < 0);
102 return rc;
103}
104
105static inline void
Mike Christieaf973482005-09-12 21:01:32 -0500106iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
107 u8* crc)
Alex Aizman7ba24712005-08-04 19:30:08 -0700108{
Mike Christie5bb0b552006-04-06 21:26:46 -0500109 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
110
James Bottomleyc9802cd2006-09-23 15:33:43 -0500111 crypto_hash_digest(&tcp_conn->tx_hash, &buf->sg, buf->sg.length, crc);
Mike Christie218432c2007-05-30 12:57:17 -0500112 buf->sg.length += sizeof(u32);
Alex Aizman7ba24712005-08-04 19:30:08 -0700113}
114
Alex Aizman7ba24712005-08-04 19:30:08 -0700115static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500116iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700117{
Mike Christie5bb0b552006-04-06 21:26:46 -0500118 struct sk_buff *skb = tcp_conn->in.skb;
Alex Aizman7ba24712005-08-04 19:30:08 -0700119
Mike Christie5bb0b552006-04-06 21:26:46 -0500120 tcp_conn->in.zero_copy_hdr = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700121
Mike Christie5bb0b552006-04-06 21:26:46 -0500122 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
123 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700124 /*
125 * Zero-copy PDU Header: using connection context
126 * to store header pointer.
127 */
128 if (skb_shinfo(skb)->frag_list == NULL &&
Mike Christie5bb0b552006-04-06 21:26:46 -0500129 !skb_shinfo(skb)->nr_frags) {
130 tcp_conn->in.hdr = (struct iscsi_hdr *)
131 ((char*)skb->data + tcp_conn->in.offset);
132 tcp_conn->in.zero_copy_hdr = 1;
133 } else {
Alex Aizman7ba24712005-08-04 19:30:08 -0700134 /* ignoring return code since we checked
135 * in.copy before */
Mike Christie5bb0b552006-04-06 21:26:46 -0500136 skb_copy_bits(skb, tcp_conn->in.offset,
137 &tcp_conn->hdr, tcp_conn->hdr_size);
138 tcp_conn->in.hdr = &tcp_conn->hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700139 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500140 tcp_conn->in.offset += tcp_conn->hdr_size;
141 tcp_conn->in.copy -= tcp_conn->hdr_size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700142 } else {
143 int hdr_remains;
144 int copylen;
145
146 /*
147 * PDU header scattered across SKB's,
148 * copying it... This'll happen quite rarely.
149 */
150
Mike Christie5bb0b552006-04-06 21:26:46 -0500151 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
152 tcp_conn->in.hdr_offset = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700153
Mike Christie5bb0b552006-04-06 21:26:46 -0500154 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700155 BUG_ON(hdr_remains <= 0);
156
Mike Christie5bb0b552006-04-06 21:26:46 -0500157 copylen = min(tcp_conn->in.copy, hdr_remains);
158 skb_copy_bits(skb, tcp_conn->in.offset,
159 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
160 copylen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700161
162 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
Mike Christie5bb0b552006-04-06 21:26:46 -0500163 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
164 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700165
Mike Christie5bb0b552006-04-06 21:26:46 -0500166 tcp_conn->in.offset += copylen;
167 tcp_conn->in.copy -= copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700168 if (copylen < hdr_remains) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500169 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
170 tcp_conn->in.hdr_offset += copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700171 return -EAGAIN;
172 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500173 tcp_conn->in.hdr = &tcp_conn->hdr;
174 tcp_conn->discontiguous_hdr_cnt++;
175 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700176 }
177
178 return 0;
179}
180
Mike Christie30a6c652006-04-06 21:13:39 -0500181/*
182 * must be called with session lock
183 */
184static void
Mike Christieb6c395e2006-07-24 15:47:15 -0500185iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700186{
Mike Christie5bb0b552006-04-06 21:26:46 -0500187 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500188 struct iscsi_r2t_info *r2t;
Mike Christie30a6c652006-04-06 21:13:39 -0500189 struct scsi_cmnd *sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700190
Mike Christieb6c395e2006-07-24 15:47:15 -0500191 /* flush ctask's r2t queues */
192 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
193 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
194 sizeof(void*));
195 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
196 }
197
Mike Christie30a6c652006-04-06 21:13:39 -0500198 sc = ctask->sc;
199 if (unlikely(!sc))
Alex Aizman7ba24712005-08-04 19:30:08 -0700200 return;
Mike Christie30a6c652006-04-06 21:13:39 -0500201
Mike Christie5bb0b552006-04-06 21:26:46 -0500202 tcp_ctask->xmstate = XMSTATE_IDLE;
203 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700204}
205
206/**
207 * iscsi_data_rsp - SCSI Data-In Response processing
208 * @conn: iscsi connection
209 * @ctask: scsi command task
210 **/
211static int
212iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
213{
Mike Christie5bb0b552006-04-06 21:26:46 -0500214 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
215 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
216 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700217 struct iscsi_session *session = conn->session;
Mike Christie857ae0b2007-05-30 12:57:15 -0500218 struct scsi_cmnd *sc = ctask->sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700219 int datasn = be32_to_cpu(rhdr->datasn);
220
Mike Christie77a23c22007-05-30 12:57:18 -0500221 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Alex Aizman7ba24712005-08-04 19:30:08 -0700222 /*
223 * setup Data-In byte counter (gets decremented..)
224 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500225 ctask->data_count = tcp_conn->in.datalen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700226
Mike Christie5bb0b552006-04-06 21:26:46 -0500227 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700228 return 0;
229
Mike Christied473cc72007-05-30 12:57:14 -0500230 if (tcp_ctask->exp_datasn != datasn) {
231 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
232 __FUNCTION__, tcp_ctask->exp_datasn, datasn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700233 return ISCSI_ERR_DATASN;
Mike Christied473cc72007-05-30 12:57:14 -0500234 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700235
Mike Christied473cc72007-05-30 12:57:14 -0500236 tcp_ctask->exp_datasn++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700237
Mike Christie5bb0b552006-04-06 21:26:46 -0500238 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900239 if (tcp_ctask->data_offset + tcp_conn->in.datalen > scsi_bufflen(sc)) {
Mike Christie857ae0b2007-05-30 12:57:15 -0500240 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
241 __FUNCTION__, tcp_ctask->data_offset,
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900242 tcp_conn->in.datalen, scsi_bufflen(sc));
Alex Aizman7ba24712005-08-04 19:30:08 -0700243 return ISCSI_ERR_DATA_OFFSET;
Mike Christie857ae0b2007-05-30 12:57:15 -0500244 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700245
246 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700247 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600248 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700249 int res_count = be32_to_cpu(rhdr->residual_count);
250
251 if (res_count > 0 &&
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900252 res_count <= scsi_bufflen(sc)) {
253 scsi_set_resid(sc, res_count);
Alex Aizman7ba24712005-08-04 19:30:08 -0700254 sc->result = (DID_OK << 16) | rhdr->cmd_status;
255 } else
256 sc->result = (DID_BAD_TARGET << 16) |
257 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600258 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900259 scsi_set_resid(sc, be32_to_cpu(rhdr->residual_count));
Alex Aizman7ba24712005-08-04 19:30:08 -0700260 sc->result = (DID_OK << 16) | rhdr->cmd_status;
261 } else
262 sc->result = (DID_OK << 16) | rhdr->cmd_status;
263 }
264
265 conn->datain_pdus_cnt++;
266 return 0;
267}
268
269/**
270 * iscsi_solicit_data_init - initialize first Data-Out
271 * @conn: iscsi connection
272 * @ctask: scsi command task
273 * @r2t: R2T info
274 *
275 * Notes:
276 * Initialize first Data-Out within this R2T sequence and finds
277 * proper data_offset within this SCSI command.
278 *
279 * This function is called with connection lock taken.
280 **/
281static void
282iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
283 struct iscsi_r2t_info *r2t)
284{
285 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700286 struct scsi_cmnd *sc = ctask->sc;
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900287 int i, sg_count = 0;
288 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700289
Mike Christieffbfe922006-05-18 20:31:36 -0500290 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700291 memset(hdr, 0, sizeof(struct iscsi_data));
292 hdr->ttt = r2t->ttt;
293 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
294 r2t->solicit_datasn++;
295 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500296 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
297 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700298 hdr->exp_statsn = r2t->exp_statsn;
299 hdr->offset = cpu_to_be32(r2t->data_offset);
300 if (r2t->data_length > conn->max_xmit_dlength) {
301 hton24(hdr->dlength, conn->max_xmit_dlength);
302 r2t->data_count = conn->max_xmit_dlength;
303 hdr->flags = 0;
304 } else {
305 hton24(hdr->dlength, r2t->data_length);
306 r2t->data_count = r2t->data_length;
307 hdr->flags = ISCSI_FLAG_CMD_FINAL;
308 }
309 conn->dataout_pdus_cnt++;
310
311 r2t->sent = 0;
312
Mike Christie6e458cc2006-05-18 20:31:31 -0500313 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -0500314 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -0700315
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900316 sg = scsi_sglist(sc);
317 r2t->sg = NULL;
318 for (i = 0; i < scsi_sg_count(sc); i++, sg += 1) {
319 /* FIXME: prefetch ? */
320 if (sg_count + sg->length > r2t->data_offset) {
321 int page_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700322
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900323 /* sg page found! */
Alex Aizman7ba24712005-08-04 19:30:08 -0700324
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900325 /* offset within this page */
326 page_offset = r2t->data_offset - sg_count;
Alex Aizman7ba24712005-08-04 19:30:08 -0700327
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900328 /* fill in this buffer */
329 iscsi_buf_init_sg(&r2t->sendbuf, sg);
330 r2t->sendbuf.sg.offset += page_offset;
331 r2t->sendbuf.sg.length -= page_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700332
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900333 /* xmit logic will continue with next one */
334 r2t->sg = sg + 1;
335 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700336 }
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900337 sg_count += sg->length;
Mike Christie62f38302006-08-31 18:09:27 -0400338 }
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900339 BUG_ON(r2t->sg == NULL);
Alex Aizman7ba24712005-08-04 19:30:08 -0700340}
341
342/**
343 * iscsi_r2t_rsp - iSCSI R2T Response processing
344 * @conn: iscsi connection
345 * @ctask: scsi command task
346 **/
347static int
348iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
349{
350 struct iscsi_r2t_info *r2t;
351 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500352 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
353 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
354 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700355 int r2tsn = be32_to_cpu(rhdr->r2tsn);
356 int rc;
357
Mike Christie98a94162006-08-31 18:09:26 -0400358 if (tcp_conn->in.datalen) {
359 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
360 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700361 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400362 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700363
Mike Christied473cc72007-05-30 12:57:14 -0500364 if (tcp_ctask->exp_datasn != r2tsn){
365 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
366 __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700367 return ISCSI_ERR_R2TSN;
Mike Christied473cc72007-05-30 12:57:14 -0500368 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700369
Alex Aizman7ba24712005-08-04 19:30:08 -0700370 /* fill-in new R2T associated with the task */
371 spin_lock(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -0500372 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
373
Alex Aizman7ba24712005-08-04 19:30:08 -0700374 if (!ctask->sc || ctask->mtask ||
375 session->state != ISCSI_STATE_LOGGED_IN) {
376 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
377 "recovery...\n", ctask->itt);
378 spin_unlock(&session->lock);
379 return 0;
380 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500381
Mike Christie5bb0b552006-04-06 21:26:46 -0500382 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700383 BUG_ON(!rc);
384
385 r2t->exp_statsn = rhdr->statsn;
386 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400387 if (r2t->data_length == 0) {
388 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
Alex Aizman7ba24712005-08-04 19:30:08 -0700389 spin_unlock(&session->lock);
390 return ISCSI_ERR_DATALEN;
391 }
392
Mike Christie98a94162006-08-31 18:09:26 -0400393 if (r2t->data_length > session->max_burst)
394 debug_scsi("invalid R2T with data len %u and max burst %u."
395 "Attempting to execute request.\n",
396 r2t->data_length, session->max_burst);
397
Alex Aizman7ba24712005-08-04 19:30:08 -0700398 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900399 if (r2t->data_offset + r2t->data_length > scsi_bufflen(ctask->sc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700400 spin_unlock(&session->lock);
Mike Christie98a94162006-08-31 18:09:26 -0400401 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
402 "offset %u and total length %d\n", r2t->data_length,
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900403 r2t->data_offset, scsi_bufflen(ctask->sc));
Alex Aizman7ba24712005-08-04 19:30:08 -0700404 return ISCSI_ERR_DATALEN;
405 }
406
407 r2t->ttt = rhdr->ttt; /* no flip */
408 r2t->solicit_datasn = 0;
409
410 iscsi_solicit_data_init(conn, ctask, r2t);
411
Mike Christied473cc72007-05-30 12:57:14 -0500412 tcp_ctask->exp_datasn = r2tsn + 1;
Mike Christie5bb0b552006-04-06 21:26:46 -0500413 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christie218432c2007-05-30 12:57:17 -0500414 tcp_ctask->xmstate |= XMSTATE_SOL_HDR_INIT;
Mike Christieb6c395e2006-07-24 15:47:15 -0500415 list_move_tail(&ctask->running, &conn->xmitqueue);
Alex Aizman7ba24712005-08-04 19:30:08 -0700416
Mike Christie55e32992006-01-13 18:05:53 -0600417 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -0700418 conn->r2t_pdus_cnt++;
419 spin_unlock(&session->lock);
420
421 return 0;
422}
423
424static int
Mike Christie5bb0b552006-04-06 21:26:46 -0500425iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700426{
Mike Christie5bb0b552006-04-06 21:26:46 -0500427 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700428 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700429 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500430 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
431 uint32_t cdgst, rdgst = 0, itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700432
Mike Christie5bb0b552006-04-06 21:26:46 -0500433 hdr = tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700434
435 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500436 tcp_conn->in.datalen = ntoh24(hdr->dlength);
437 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700438 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500439 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700440 return ISCSI_ERR_DATALEN;
441 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500442 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700443
444 /* read AHS */
Mike Christie5bb0b552006-04-06 21:26:46 -0500445 ahslen = hdr->hlength << 2;
446 tcp_conn->in.offset += ahslen;
447 tcp_conn->in.copy -= ahslen;
448 if (tcp_conn->in.copy < 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700449 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
Mike Christie5bb0b552006-04-06 21:26:46 -0500450 "%d bytes\n", ahslen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700451 return ISCSI_ERR_AHSLEN;
452 }
453
454 /* calculate read padding */
Mike Christie5bb0b552006-04-06 21:26:46 -0500455 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
456 if (tcp_conn->in.padding) {
457 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
458 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
Alex Aizman7ba24712005-08-04 19:30:08 -0700459 }
460
461 if (conn->hdrdgst_en) {
462 struct scatterlist sg;
463
464 sg_init_one(&sg, (u8 *)hdr,
Mike Christie5bb0b552006-04-06 21:26:46 -0500465 sizeof(struct iscsi_hdr) + ahslen);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500466 crypto_hash_digest(&tcp_conn->rx_hash, &sg, sg.length,
467 (u8 *)&cdgst);
Alex Aizman7ba24712005-08-04 19:30:08 -0700468 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
Mike Christie5bb0b552006-04-06 21:26:46 -0500469 ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600470 if (cdgst != rdgst) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500471 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
472 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
Mike Christie8a47cd32005-11-30 02:27:19 -0600473 return ISCSI_ERR_HDR_DGST;
474 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700475 }
476
Mike Christie5bb0b552006-04-06 21:26:46 -0500477 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700478 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500479 rc = iscsi_verify_itt(conn, hdr, &itt);
480 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
481 tcp_conn->in.datalen = 0; /* force drop */
482 return 0;
483 } else if (rc)
484 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700485
486 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500487 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
488 ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700489
Mike Christie5bb0b552006-04-06 21:26:46 -0500490 switch(opcode) {
491 case ISCSI_OP_SCSI_DATA_IN:
492 tcp_conn->in.ctask = session->cmds[itt];
493 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
Mike Christie275fd7d2006-07-24 15:47:17 -0500494 if (rc)
495 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500496 /* fall through */
497 case ISCSI_OP_SCSI_CMD_RSP:
498 tcp_conn->in.ctask = session->cmds[itt];
499 if (tcp_conn->in.datalen)
500 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700501
Mike Christie5bb0b552006-04-06 21:26:46 -0500502 spin_lock(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500503 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
504 spin_unlock(&session->lock);
505 break;
506 case ISCSI_OP_R2T:
507 tcp_conn->in.ctask = session->cmds[itt];
508 if (ahslen)
509 rc = ISCSI_ERR_AHSLEN;
510 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
511 DMA_TO_DEVICE)
512 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
513 else
514 rc = ISCSI_ERR_PROTO;
515 break;
516 case ISCSI_OP_LOGIN_RSP:
517 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500518 case ISCSI_OP_REJECT:
519 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500520 /*
521 * It is possible that we could get a PDU with a buffer larger
522 * than 8K, but there are no targets that currently do this.
523 * For now we fail until we find a vendor that needs it
524 */
Mike Christiebf32ed32007-02-28 17:32:17 -0600525 if (ISCSI_DEF_MAX_RECV_SEG_LEN <
Mike Christiec8dc1e52006-07-24 15:47:39 -0500526 tcp_conn->in.datalen) {
527 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
528 "but conn buffer is only %u (opcode %0x)\n",
529 tcp_conn->in.datalen,
Mike Christiebf32ed32007-02-28 17:32:17 -0600530 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
Mike Christiec8dc1e52006-07-24 15:47:39 -0500531 rc = ISCSI_ERR_PROTO;
532 break;
533 }
534
Mike Christie5bb0b552006-04-06 21:26:46 -0500535 if (tcp_conn->in.datalen)
536 goto copy_hdr;
537 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500538 case ISCSI_OP_LOGOUT_RSP:
539 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500540 case ISCSI_OP_SCSI_TMFUNC_RSP:
541 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
542 break;
543 default:
544 rc = ISCSI_ERR_BAD_OPCODE;
545 break;
546 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700547
548 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500549
550copy_hdr:
551 /*
552 * if we did zero copy for the header but we will need multiple
553 * skbs to complete the command then we have to copy the header
554 * for later use
555 */
Mike Christie275fd7d2006-07-24 15:47:17 -0500556 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
Mike Christie5bb0b552006-04-06 21:26:46 -0500557 (tcp_conn->in.datalen + tcp_conn->in.padding +
558 (conn->datadgst_en ? 4 : 0))) {
559 debug_tcp("Copying header for later use. in.copy %d in.datalen"
560 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
561 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
562 sizeof(struct iscsi_hdr));
563 tcp_conn->in.hdr = &tcp_conn->hdr;
564 tcp_conn->in.zero_copy_hdr = 0;
565 }
566 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700567}
568
569/**
570 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
Mike Christie5bb0b552006-04-06 21:26:46 -0500571 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700572 * @ctask: scsi command task
573 * @buf: buffer to copy to
574 * @buf_size: size of buffer
575 * @offset: offset within the buffer
576 *
577 * Notes:
578 * The function calls skb_copy_bits() and updates per-connection and
579 * per-cmd byte counters.
580 *
581 * Read counters (in bytes):
582 *
583 * conn->in.offset offset within in progress SKB
584 * conn->in.copy left to copy from in progress SKB
585 * including padding
586 * conn->in.copied copied already from in progress SKB
587 * conn->data_copied copied already from in progress buffer
588 * ctask->sent total bytes sent up to the MidLayer
589 * ctask->data_count left to copy from in progress Data-In
590 * buf_left left to copy from in progress buffer
591 **/
592static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500593iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -0700594 void *buf, int buf_size, int offset)
595{
Mike Christie5bb0b552006-04-06 21:26:46 -0500596 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
597 int buf_left = buf_size - (tcp_conn->data_copied + offset);
Mike Christie857ae0b2007-05-30 12:57:15 -0500598 unsigned size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700599 int rc;
600
601 size = min(size, ctask->data_count);
602
603 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500604 size, tcp_conn->in.offset, tcp_conn->in.copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700605
606 BUG_ON(size <= 0);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900607 BUG_ON(tcp_ctask->sent + size > scsi_bufflen(ctask->sc));
Alex Aizman7ba24712005-08-04 19:30:08 -0700608
Mike Christie5bb0b552006-04-06 21:26:46 -0500609 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
610 (char*)buf + (offset + tcp_conn->data_copied), size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700611 /* must fit into skb->len */
612 BUG_ON(rc);
613
Mike Christie5bb0b552006-04-06 21:26:46 -0500614 tcp_conn->in.offset += size;
615 tcp_conn->in.copy -= size;
616 tcp_conn->in.copied += size;
617 tcp_conn->data_copied += size;
618 tcp_ctask->sent += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700619 ctask->data_count -= size;
620
Mike Christie5bb0b552006-04-06 21:26:46 -0500621 BUG_ON(tcp_conn->in.copy < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700622 BUG_ON(ctask->data_count < 0);
623
Mike Christie5bb0b552006-04-06 21:26:46 -0500624 if (buf_size != (tcp_conn->data_copied + offset)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700625 if (!ctask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500626 BUG_ON(buf_size - tcp_conn->data_copied < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700627 /* done with this PDU */
Mike Christie5bb0b552006-04-06 21:26:46 -0500628 return buf_size - tcp_conn->data_copied;
Alex Aizman7ba24712005-08-04 19:30:08 -0700629 }
630 return -EAGAIN;
631 }
632
633 /* done with this buffer or with both - PDU and buffer */
Mike Christie5bb0b552006-04-06 21:26:46 -0500634 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700635 return 0;
636}
637
638/**
639 * iscsi_tcp_copy - copy skb bits to the destanation buffer
Mike Christie5bb0b552006-04-06 21:26:46 -0500640 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700641 *
642 * Notes:
643 * The function calls skb_copy_bits() and updates per-connection
644 * byte counters.
645 **/
646static inline int
Mike Christieca518682006-08-31 18:09:32 -0400647iscsi_tcp_copy(struct iscsi_conn *conn, int buf_size)
Alex Aizman7ba24712005-08-04 19:30:08 -0700648{
Mike Christiec8dc1e52006-07-24 15:47:39 -0500649 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500650 int buf_left = buf_size - tcp_conn->data_copied;
651 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700652 int rc;
653
654 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500655 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700656 BUG_ON(size <= 0);
657
Mike Christie5bb0b552006-04-06 21:26:46 -0500658 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christiec8dc1e52006-07-24 15:47:39 -0500659 (char*)conn->data + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700660 BUG_ON(rc);
661
Mike Christie5bb0b552006-04-06 21:26:46 -0500662 tcp_conn->in.offset += size;
663 tcp_conn->in.copy -= size;
664 tcp_conn->in.copied += size;
665 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700666
Mike Christie5bb0b552006-04-06 21:26:46 -0500667 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700668 return -EAGAIN;
669
670 return 0;
671}
672
673static inline void
James Bottomleyc9802cd2006-09-23 15:33:43 -0500674partial_sg_digest_update(struct hash_desc *desc, struct scatterlist *sg,
Mike Christie62f38302006-08-31 18:09:27 -0400675 int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700676{
677 struct scatterlist temp;
678
679 memcpy(&temp, sg, sizeof(struct scatterlist));
680 temp.offset = offset;
681 temp.length = length;
James Bottomleyc9802cd2006-09-23 15:33:43 -0500682 crypto_hash_update(desc, &temp, length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700683}
684
Mike Christief6cfba12005-11-29 23:12:57 -0600685static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500686iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600687{
688 struct scatterlist tmp;
689
690 sg_init_one(&tmp, buf, len);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500691 crypto_hash_update(&tcp_conn->rx_hash, &tmp, len);
Mike Christief6cfba12005-11-29 23:12:57 -0600692}
693
Alex Aizman7ba24712005-08-04 19:30:08 -0700694static int iscsi_scsi_data_in(struct iscsi_conn *conn)
695{
Mike Christie5bb0b552006-04-06 21:26:46 -0500696 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
697 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
698 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700699 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600700 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700701 int i, offset, rc = 0;
702
703 BUG_ON((void*)ctask != sc->SCp.ptr);
704
Mike Christie5bb0b552006-04-06 21:26:46 -0500705 offset = tcp_ctask->data_offset;
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900706 sg = scsi_sglist(sc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700707
Mike Christie5bb0b552006-04-06 21:26:46 -0500708 if (tcp_ctask->data_offset)
709 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700710 offset -= sg[i].length;
711 /* we've passed through partial sg*/
712 if (offset < 0)
713 offset = 0;
714
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900715 for (i = tcp_ctask->sg_count; i < scsi_sg_count(sc); i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700716 char *dest;
717
Jens Axboe45711f12007-10-22 21:19:53 +0200718 dest = kmap_atomic(sg_page(&sg[i]), KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500719 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700720 sg[i].length, offset);
721 kunmap_atomic(dest, KM_SOFTIRQ0);
722 if (rc == -EAGAIN)
723 /* continue with the next SKB/PDU */
724 return rc;
725 if (!rc) {
726 if (conn->datadgst_en) {
727 if (!offset)
Herbert Xudc64ddf2006-08-24 18:45:50 +1000728 crypto_hash_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500729 &tcp_conn->rx_hash,
Arne Redlichc959e1c2006-12-17 12:10:24 -0600730 &sg[i], sg[i].length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700731 else
Mike Christie62f38302006-08-31 18:09:27 -0400732 partial_sg_digest_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500733 &tcp_conn->rx_hash,
Mike Christie5bb0b552006-04-06 21:26:46 -0500734 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700735 sg[i].offset + offset,
736 sg[i].length - offset);
737 }
738 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500739 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700740 }
741
742 if (!ctask->data_count) {
743 if (rc && conn->datadgst_en)
744 /*
745 * data-in is complete, but buffer not...
746 */
James Bottomleyc9802cd2006-09-23 15:33:43 -0500747 partial_sg_digest_update(&tcp_conn->rx_hash,
748 &sg[i],
749 sg[i].offset,
750 sg[i].length-rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700751 rc = 0;
752 break;
753 }
754
Mike Christie5bb0b552006-04-06 21:26:46 -0500755 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700756 return -EAGAIN;
757 }
758 BUG_ON(ctask->data_count);
759
Alex Aizman7ba24712005-08-04 19:30:08 -0700760 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500761 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Mike Christieb6c395e2006-07-24 15:47:15 -0500762 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
763 (long)sc, sc->result, ctask->itt,
764 tcp_conn->in.hdr->flags);
Mike Christie5bb0b552006-04-06 21:26:46 -0500765 spin_lock(&conn->session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500766 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
767 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700768 }
769
770 return rc;
771}
772
773static int
774iscsi_data_recv(struct iscsi_conn *conn)
775{
Mike Christie5bb0b552006-04-06 21:26:46 -0500776 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
777 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700778
Mike Christie5bb0b552006-04-06 21:26:46 -0500779 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
780 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700781 case ISCSI_OP_SCSI_DATA_IN:
782 rc = iscsi_scsi_data_in(conn);
783 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500784 case ISCSI_OP_SCSI_CMD_RSP:
Alex Aizman7ba24712005-08-04 19:30:08 -0700785 case ISCSI_OP_TEXT_RSP:
786 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500787 case ISCSI_OP_ASYNC_EVENT:
788 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700789 /*
790 * Collect data segment to the connection's data
791 * placeholder
792 */
Mike Christieca518682006-08-31 18:09:32 -0400793 if (iscsi_tcp_copy(conn, tcp_conn->in.datalen)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700794 rc = -EAGAIN;
795 goto exit;
796 }
797
Mike Christiec8dc1e52006-07-24 15:47:39 -0500798 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500799 tcp_conn->in.datalen);
800 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
Mike Christiec8dc1e52006-07-24 15:47:39 -0500801 iscsi_recv_digest_update(tcp_conn, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500802 tcp_conn->in.datalen);
803 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700804 default:
805 BUG_ON(1);
806 }
807exit:
808 return rc;
809}
810
811/**
812 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
813 * @rd_desc: read descriptor
814 * @skb: socket buffer
815 * @offset: offset in skb
816 * @len: skb->len - offset
817 **/
818static int
819iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
820 unsigned int offset, size_t len)
821{
822 int rc;
823 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500824 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700825 int processed;
826 char pad[ISCSI_PAD_LEN];
827 struct scatterlist sg;
828
829 /*
830 * Save current SKB and its offset in the corresponding
831 * connection context.
832 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500833 tcp_conn->in.copy = skb->len - offset;
834 tcp_conn->in.offset = offset;
835 tcp_conn->in.skb = skb;
836 tcp_conn->in.len = tcp_conn->in.copy;
837 BUG_ON(tcp_conn->in.copy <= 0);
838 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700839
840more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500841 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700842 rc = 0;
843
844 if (unlikely(conn->suspend_rx)) {
845 debug_tcp("conn %d Rx suspended!\n", conn->id);
846 return 0;
847 }
848
Mike Christie5bb0b552006-04-06 21:26:46 -0500849 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
850 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
851 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700852 if (rc) {
853 if (rc == -EAGAIN)
854 goto nomore;
855 else {
Mike Christied82967c2006-07-24 15:47:11 -0500856 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700857 return 0;
858 }
859 }
860
861 /*
862 * Verify and process incoming PDU header.
863 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500864 rc = iscsi_tcp_hdr_recv(conn);
865 if (!rc && tcp_conn->in.datalen) {
Mike Christiedd8c0d92006-08-31 18:09:28 -0400866 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -0500867 crypto_hash_init(&tcp_conn->rx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -0500868 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700869 } else if (rc) {
Mike Christie40527af2006-07-24 15:47:45 -0500870 iscsi_conn_failure(conn, rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700871 return 0;
872 }
873 }
874
Mike Christiedbdb0162007-05-30 12:57:20 -0500875 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV &&
876 tcp_conn->in.copy) {
Mike Christief6cfba12005-11-29 23:12:57 -0600877 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500878
Alex Aizman7ba24712005-08-04 19:30:08 -0700879 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500880 tcp_conn->in.offset, tcp_conn->in.copy);
Mike Christiedbdb0162007-05-30 12:57:20 -0500881
882 if (!tcp_conn->data_copied) {
883 if (tcp_conn->in.padding) {
884 debug_tcp("padding -> %d\n",
885 tcp_conn->in.padding);
886 memset(pad, 0, tcp_conn->in.padding);
887 sg_init_one(&sg, pad, tcp_conn->in.padding);
888 crypto_hash_update(&tcp_conn->rx_hash,
889 &sg, sg.length);
890 }
891 crypto_hash_final(&tcp_conn->rx_hash,
892 (u8 *) &tcp_conn->in.datadgst);
893 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
894 }
895
Mike Christieca518682006-08-31 18:09:32 -0400896 rc = iscsi_tcp_copy(conn, sizeof(uint32_t));
897 if (rc) {
898 if (rc == -EAGAIN)
899 goto again;
900 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
901 return 0;
902 }
903
904 memcpy(&recv_digest, conn->data, sizeof(uint32_t));
Mike Christie5bb0b552006-04-06 21:26:46 -0500905 if (recv_digest != tcp_conn->in.datadgst) {
Mike Christief6cfba12005-11-29 23:12:57 -0600906 debug_tcp("iscsi_tcp: data digest error!"
907 "0x%x != 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500908 tcp_conn->in.datadgst);
Mike Christief6cfba12005-11-29 23:12:57 -0600909 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
910 return 0;
911 } else {
912 debug_tcp("iscsi_tcp: data digest match!"
913 "0x%x == 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500914 tcp_conn->in.datadgst);
915 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700916 }
917 }
918
Mike Christie5bb0b552006-04-06 21:26:46 -0500919 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
Mike Christiedbdb0162007-05-30 12:57:20 -0500920 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700921 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500922 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700923
924 rc = iscsi_data_recv(conn);
925 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500926 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700927 goto again;
Mike Christied82967c2006-07-24 15:47:11 -0500928 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700929 return 0;
930 }
Mike Christiedbdb0162007-05-30 12:57:20 -0500931
932 if (tcp_conn->in.padding)
933 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
934 else if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500935 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Mike Christiedbdb0162007-05-30 12:57:20 -0500936 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500937 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Mike Christiedbdb0162007-05-30 12:57:20 -0500938 tcp_conn->data_copied = 0;
939 }
940
941 if (tcp_conn->in_progress == IN_PROGRESS_PAD_RECV &&
942 tcp_conn->in.copy) {
943 int copylen = min(tcp_conn->in.padding - tcp_conn->data_copied,
944 tcp_conn->in.copy);
945
946 tcp_conn->in.copy -= copylen;
947 tcp_conn->in.offset += copylen;
948 tcp_conn->data_copied += copylen;
949
950 if (tcp_conn->data_copied != tcp_conn->in.padding)
951 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
952 else if (conn->datadgst_en)
953 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
954 else
955 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
956 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700957 }
958
959 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500960 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
961 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700962
Mike Christie5bb0b552006-04-06 21:26:46 -0500963 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700964 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500965 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700966 goto more;
967 }
968
969nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500970 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700971 BUG_ON(processed == 0);
972 return processed;
973
974again:
Mike Christie5bb0b552006-04-06 21:26:46 -0500975 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700976 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
977 processed, (int)len, (int)rd_desc->count);
978 BUG_ON(processed == 0);
979 BUG_ON(processed > len);
980
981 conn->rxdata_octets += processed;
982 return processed;
983}
984
985static void
986iscsi_tcp_data_ready(struct sock *sk, int flag)
987{
988 struct iscsi_conn *conn = sk->sk_user_data;
989 read_descriptor_t rd_desc;
990
991 read_lock(&sk->sk_callback_lock);
992
Mike Christie665b44a2006-05-02 19:46:49 -0500993 /*
994 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
995 * We set count to 1 because we want the network layer to
996 * hand us all the skbs that are available. iscsi_tcp_data_recv
997 * handled pdus that cross buffers or pdus that still need data.
998 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700999 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -05001000 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001001 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
1002
1003 read_unlock(&sk->sk_callback_lock);
1004}
1005
1006static void
1007iscsi_tcp_state_change(struct sock *sk)
1008{
Mike Christie5bb0b552006-04-06 21:26:46 -05001009 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001010 struct iscsi_conn *conn;
1011 struct iscsi_session *session;
1012 void (*old_state_change)(struct sock *);
1013
1014 read_lock(&sk->sk_callback_lock);
1015
1016 conn = (struct iscsi_conn*)sk->sk_user_data;
1017 session = conn->session;
1018
Mike Christiee6273992005-11-29 23:12:49 -06001019 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1020 sk->sk_state == TCP_CLOSE) &&
1021 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001022 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1023 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1024 }
1025
Mike Christie5bb0b552006-04-06 21:26:46 -05001026 tcp_conn = conn->dd_data;
1027 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001028
1029 read_unlock(&sk->sk_callback_lock);
1030
1031 old_state_change(sk);
1032}
1033
1034/**
1035 * iscsi_write_space - Called when more output buffer space is available
1036 * @sk: socket space is available for
1037 **/
1038static void
1039iscsi_write_space(struct sock *sk)
1040{
1041 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001042 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1043
1044 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001045 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001046 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001047}
1048
1049static void
1050iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1051{
Mike Christie5bb0b552006-04-06 21:26:46 -05001052 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1053 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001054
1055 /* assign new callbacks */
1056 write_lock_bh(&sk->sk_callback_lock);
1057 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001058 tcp_conn->old_data_ready = sk->sk_data_ready;
1059 tcp_conn->old_state_change = sk->sk_state_change;
1060 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001061 sk->sk_data_ready = iscsi_tcp_data_ready;
1062 sk->sk_state_change = iscsi_tcp_state_change;
1063 sk->sk_write_space = iscsi_write_space;
1064 write_unlock_bh(&sk->sk_callback_lock);
1065}
1066
1067static void
Mike Christie1c834692006-07-24 15:47:26 -05001068iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001069{
Mike Christie5bb0b552006-04-06 21:26:46 -05001070 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001071
1072 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1073 write_lock_bh(&sk->sk_callback_lock);
1074 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001075 sk->sk_data_ready = tcp_conn->old_data_ready;
1076 sk->sk_state_change = tcp_conn->old_state_change;
1077 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001078 sk->sk_no_check = 0;
1079 write_unlock_bh(&sk->sk_callback_lock);
1080}
1081
1082/**
1083 * iscsi_send - generic send routine
1084 * @sk: kernel's socket
1085 * @buf: buffer to write from
1086 * @size: actual size to write
1087 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001088 */
1089static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001090iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001091{
Mike Christie5bb0b552006-04-06 21:26:46 -05001092 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1093 struct socket *sk = tcp_conn->sock;
Mike Christie3219e522006-05-30 00:37:28 -05001094 int offset = buf->sg.offset + buf->sent, res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001095
Mike Christie7cae5152006-01-13 18:05:47 -06001096 /*
1097 * if we got use_sg=0 or are sending something we kmallocd
1098 * then we did not have to do kmap (kmap returns page_address)
1099 *
1100 * if we got use_sg > 0, but had to drop down, we do not
1101 * set clustering so this should only happen for that
1102 * slab case.
1103 */
1104 if (buf->use_sendmsg)
Jens Axboe45711f12007-10-22 21:19:53 +02001105 res = sock_no_sendpage(sk, sg_page(&buf->sg), offset, size, flags);
Mike Christie7cae5152006-01-13 18:05:47 -06001106 else
Jens Axboe45711f12007-10-22 21:19:53 +02001107 res = tcp_conn->sendpage(sk, sg_page(&buf->sg), offset, size, flags);
Mike Christie3219e522006-05-30 00:37:28 -05001108
1109 if (res >= 0) {
1110 conn->txdata_octets += res;
1111 buf->sent += res;
1112 return res;
1113 }
1114
1115 tcp_conn->sendpage_failures_cnt++;
1116 if (res == -EAGAIN)
1117 res = -ENOBUFS;
1118 else
1119 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1120 return res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001121}
1122
1123/**
1124 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1125 * @conn: iscsi connection
1126 * @buf: buffer to write from
1127 * @datalen: lenght of data to be sent after the header
1128 *
1129 * Notes:
1130 * (Tx, Fast Path)
1131 **/
1132static inline int
1133iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1134{
Alex Aizman7ba24712005-08-04 19:30:08 -07001135 int flags = 0; /* MSG_DONTWAIT; */
1136 int res, size;
1137
1138 size = buf->sg.length - buf->sent;
1139 BUG_ON(buf->sent + size > buf->sg.length);
1140 if (buf->sent + size != buf->sg.length || datalen)
1141 flags |= MSG_MORE;
1142
FUJITA Tomonori56851692006-01-13 18:05:44 -06001143 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001144 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1145 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001146 if (size != res)
1147 return -EAGAIN;
1148 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001149 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001150
1151 return res;
1152}
1153
1154/**
1155 * iscsi_sendpage - send one page of iSCSI Data-Out.
1156 * @conn: iscsi connection
1157 * @buf: buffer to write from
1158 * @count: remaining data
1159 * @sent: number of bytes sent
1160 *
1161 * Notes:
1162 * (Tx, Fast Path)
1163 **/
1164static inline int
1165iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1166 int *count, int *sent)
1167{
Alex Aizman7ba24712005-08-04 19:30:08 -07001168 int flags = 0; /* MSG_DONTWAIT; */
1169 int res, size;
1170
1171 size = buf->sg.length - buf->sent;
1172 BUG_ON(buf->sent + size > buf->sg.length);
1173 if (size > *count)
1174 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001175 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001176 flags |= MSG_MORE;
1177
FUJITA Tomonori56851692006-01-13 18:05:44 -06001178 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001179 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1180 size, buf->sent, *count, *sent, res);
1181 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001182 *count -= res;
1183 *sent += res;
1184 if (size != res)
1185 return -EAGAIN;
1186 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001187 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001188
1189 return res;
1190}
1191
1192static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001193iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
Mike Christie62f38302006-08-31 18:09:27 -04001194 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001195{
James Bottomleyc9802cd2006-09-23 15:33:43 -05001196 crypto_hash_init(&tcp_conn->tx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -05001197 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001198}
1199
Alex Aizman7ba24712005-08-04 19:30:08 -07001200/**
1201 * iscsi_solicit_data_cont - initialize next Data-Out
1202 * @conn: iscsi connection
1203 * @ctask: scsi command task
1204 * @r2t: R2T info
1205 * @left: bytes left to transfer
1206 *
1207 * Notes:
1208 * Initialize next Data-Out within this R2T sequence and continue
1209 * to process next Scatter-Gather element(if any) of this SCSI command.
1210 *
1211 * Called under connection lock.
1212 **/
1213static void
1214iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1215 struct iscsi_r2t_info *r2t, int left)
1216{
1217 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001218 int new_offset;
1219
Mike Christieffbfe922006-05-18 20:31:36 -05001220 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001221 memset(hdr, 0, sizeof(struct iscsi_data));
1222 hdr->ttt = r2t->ttt;
1223 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1224 r2t->solicit_datasn++;
1225 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001226 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1227 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001228 hdr->exp_statsn = r2t->exp_statsn;
1229 new_offset = r2t->data_offset + r2t->sent;
1230 hdr->offset = cpu_to_be32(new_offset);
1231 if (left > conn->max_xmit_dlength) {
1232 hton24(hdr->dlength, conn->max_xmit_dlength);
1233 r2t->data_count = conn->max_xmit_dlength;
1234 } else {
1235 hton24(hdr->dlength, left);
1236 r2t->data_count = left;
1237 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1238 }
1239 conn->dataout_pdus_cnt++;
1240
Mike Christie6e458cc2006-05-18 20:31:31 -05001241 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001242 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001243
Mike Christie62f38302006-08-31 18:09:27 -04001244 if (iscsi_buf_left(&r2t->sendbuf))
1245 return;
1246
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001247 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1248 r2t->sg += 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001249}
1250
Mike Christie62f38302006-08-31 18:09:27 -04001251static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask,
1252 unsigned long len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001253{
Mike Christie62f38302006-08-31 18:09:27 -04001254 tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1);
1255 if (!tcp_ctask->pad_count)
1256 return;
Alex Aizman7ba24712005-08-04 19:30:08 -07001257
Mike Christie62f38302006-08-31 18:09:27 -04001258 tcp_ctask->pad_count = ISCSI_PAD_LEN - tcp_ctask->pad_count;
1259 debug_scsi("write padding %d bytes\n", tcp_ctask->pad_count);
1260 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Alex Aizman7ba24712005-08-04 19:30:08 -07001261}
1262
1263/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001264 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001265 * @conn: iscsi connection
1266 * @ctask: scsi command task
1267 * @sc: scsi command
1268 **/
1269static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001270iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001271{
Mike Christie5bb0b552006-04-06 21:26:46 -05001272 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001273
Mike Christie5bb0b552006-04-06 21:26:46 -05001274 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Mike Christie218432c2007-05-30 12:57:17 -05001275 tcp_ctask->xmstate = XMSTATE_CMD_HDR_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001276}
1277
1278/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001279 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001280 * @conn: iscsi connection
1281 * @mtask: task management task
1282 *
1283 * Notes:
1284 * The function can return -EAGAIN in which case caller must
1285 * call it again later, or recover. '0' return code means successful
1286 * xmit.
1287 *
Mike Christie218432c2007-05-30 12:57:17 -05001288 * Management xmit state machine consists of these states:
1289 * XMSTATE_IMM_HDR_INIT - calculate digest of PDU Header
1290 * XMSTATE_IMM_HDR - PDU Header xmit in progress
1291 * XMSTATE_IMM_DATA - PDU Data xmit in progress
1292 * XMSTATE_IDLE - management PDU is done
Alex Aizman7ba24712005-08-04 19:30:08 -07001293 **/
1294static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001295iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001296{
Mike Christie5bb0b552006-04-06 21:26:46 -05001297 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001298 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001299
1300 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001301 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001302
Mike Christie218432c2007-05-30 12:57:17 -05001303 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR_INIT) {
1304 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
1305 sizeof(struct iscsi_hdr));
1306
1307 if (mtask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001308 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie218432c2007-05-30 12:57:17 -05001309 iscsi_buf_init_iov(&tcp_mtask->sendbuf,
1310 (char*)mtask->data,
1311 mtask->data_count);
1312 }
1313
Mike Christieaf973482005-09-12 21:01:32 -05001314 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001315 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001316 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001317 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1318 (u8*)tcp_mtask->hdrext);
Mike Christie218432c2007-05-30 12:57:17 -05001319
1320 tcp_mtask->sent = 0;
1321 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR_INIT;
1322 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
1323 }
1324
1325 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
Mike Christie3219e522006-05-30 00:37:28 -05001326 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1327 mtask->data_count);
Mike Christie218432c2007-05-30 12:57:17 -05001328 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001329 return rc;
Mike Christie218432c2007-05-30 12:57:17 -05001330 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001331 }
1332
Mike Christie5bb0b552006-04-06 21:26:46 -05001333 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001334 BUG_ON(!mtask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001335 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001336 /* FIXME: implement.
1337 * Virtual buffer could be spreaded across multiple pages...
1338 */
1339 do {
Mike Christie3219e522006-05-30 00:37:28 -05001340 int rc;
1341
1342 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1343 &mtask->data_count, &tcp_mtask->sent);
1344 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001345 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001346 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001347 }
1348 } while (mtask->data_count);
1349 }
1350
Mike Christie5bb0b552006-04-06 21:26:46 -05001351 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
Al Virob4377352007-02-09 16:39:40 +00001352 if (mtask->hdr->itt == RESERVED_ITT) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001353 struct iscsi_session *session = conn->session;
1354
1355 spin_lock_bh(&session->lock);
1356 list_del(&conn->mtask->running);
1357 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1358 sizeof(void*));
1359 spin_unlock_bh(&session->lock);
1360 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001361 return 0;
1362}
1363
Mike Christie218432c2007-05-30 12:57:17 -05001364static int
1365iscsi_send_cmd_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001366{
Mike Christie218432c2007-05-30 12:57:17 -05001367 struct scsi_cmnd *sc = ctask->sc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001368 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie218432c2007-05-30 12:57:17 -05001369 int rc = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -05001370
Mike Christie218432c2007-05-30 12:57:17 -05001371 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_INIT) {
1372 tcp_ctask->sent = 0;
1373 tcp_ctask->sg_count = 0;
1374 tcp_ctask->exp_datasn = 0;
Mike Christie3219e522006-05-30 00:37:28 -05001375
Mike Christie218432c2007-05-30 12:57:17 -05001376 if (sc->sc_data_direction == DMA_TO_DEVICE) {
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001377 struct scatterlist *sg = scsi_sglist(sc);
Alex Aizman7ba24712005-08-04 19:30:08 -07001378
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001379 iscsi_buf_init_sg(&tcp_ctask->sendbuf, sg);
1380 tcp_ctask->sg = sg + 1;
1381 tcp_ctask->bad_sg = sg + scsi_sg_count(sc);
Mike Christie218432c2007-05-30 12:57:17 -05001382
1383 debug_scsi("cmd [itt 0x%x total %d imm_data %d "
1384 "unsol count %d, unsol offset %d]\n",
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001385 ctask->itt, scsi_bufflen(sc),
Mike Christie218432c2007-05-30 12:57:17 -05001386 ctask->imm_count, ctask->unsol_count,
1387 ctask->unsol_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -07001388 }
Mike Christie218432c2007-05-30 12:57:17 -05001389
1390 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
1391 sizeof(struct iscsi_hdr));
1392
1393 if (conn->hdrdgst_en)
1394 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1395 (u8*)tcp_ctask->hdrext);
1396 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_INIT;
1397 tcp_ctask->xmstate |= XMSTATE_CMD_HDR_XMIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001398 }
1399
Mike Christie218432c2007-05-30 12:57:17 -05001400 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_XMIT) {
1401 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1402 if (rc)
1403 return rc;
1404 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_XMIT;
1405
1406 if (sc->sc_data_direction != DMA_TO_DEVICE)
1407 return 0;
1408
1409 if (ctask->imm_count) {
1410 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1411 iscsi_set_padding(tcp_ctask, ctask->imm_count);
1412
1413 if (ctask->conn->datadgst_en) {
1414 iscsi_data_digest_init(ctask->conn->dd_data,
1415 tcp_ctask);
1416 tcp_ctask->immdigest = 0;
1417 }
1418 }
1419
1420 if (ctask->unsol_count)
1421 tcp_ctask->xmstate |=
1422 XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
1423 }
1424 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001425}
1426
Mike Christie62f38302006-08-31 18:09:27 -04001427static int
1428iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1429{
1430 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1431 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1432 int sent = 0, rc;
1433
1434 if (tcp_ctask->xmstate & XMSTATE_W_PAD) {
1435 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
1436 tcp_ctask->pad_count);
1437 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001438 crypto_hash_update(&tcp_conn->tx_hash,
1439 &tcp_ctask->sendbuf.sg,
1440 tcp_ctask->sendbuf.sg.length);
Mike Christie62f38302006-08-31 18:09:27 -04001441 } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD))
1442 return 0;
1443
1444 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
1445 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_PAD;
1446 debug_scsi("sending %d pad bytes for itt 0x%x\n",
1447 tcp_ctask->pad_count, ctask->itt);
1448 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1449 &sent);
1450 if (rc) {
1451 debug_scsi("padding send failed %d\n", rc);
1452 tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD;
1453 }
1454 return rc;
1455}
1456
1457static int
1458iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1459 struct iscsi_buf *buf, uint32_t *digest)
1460{
1461 struct iscsi_tcp_cmd_task *tcp_ctask;
1462 struct iscsi_tcp_conn *tcp_conn;
1463 int rc, sent = 0;
1464
1465 if (!conn->datadgst_en)
1466 return 0;
1467
1468 tcp_ctask = ctask->dd_data;
1469 tcp_conn = conn->dd_data;
1470
1471 if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) {
James Bottomleyc9802cd2006-09-23 15:33:43 -05001472 crypto_hash_final(&tcp_conn->tx_hash, (u8*)digest);
Mike Christie62f38302006-08-31 18:09:27 -04001473 iscsi_buf_init_iov(buf, (char*)digest, 4);
1474 }
1475 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST;
1476
1477 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
1478 if (!rc)
1479 debug_scsi("sent digest 0x%x for itt 0x%x\n", *digest,
1480 ctask->itt);
1481 else {
1482 debug_scsi("sending digest 0x%x failed for itt 0x%x!\n",
1483 *digest, ctask->itt);
1484 tcp_ctask->xmstate |= XMSTATE_W_RESEND_DATA_DIGEST;
1485 }
1486 return rc;
1487}
1488
1489static int
1490iscsi_send_data(struct iscsi_cmd_task *ctask, struct iscsi_buf *sendbuf,
1491 struct scatterlist **sg, int *sent, int *count,
1492 struct iscsi_buf *digestbuf, uint32_t *digest)
1493{
1494 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1495 struct iscsi_conn *conn = ctask->conn;
1496 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1497 int rc, buf_sent, offset;
1498
1499 while (*count) {
1500 buf_sent = 0;
1501 offset = sendbuf->sent;
1502
1503 rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent);
1504 *sent = *sent + buf_sent;
1505 if (buf_sent && conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001506 partial_sg_digest_update(&tcp_conn->tx_hash,
Mike Christie62f38302006-08-31 18:09:27 -04001507 &sendbuf->sg, sendbuf->sg.offset + offset,
1508 buf_sent);
1509 if (!iscsi_buf_left(sendbuf) && *sg != tcp_ctask->bad_sg) {
1510 iscsi_buf_init_sg(sendbuf, *sg);
1511 *sg = *sg + 1;
1512 }
1513
1514 if (rc)
1515 return rc;
1516 }
1517
1518 rc = iscsi_send_padding(conn, ctask);
1519 if (rc)
1520 return rc;
1521
1522 return iscsi_send_digest(conn, ctask, digestbuf, digest);
1523}
1524
1525static int
1526iscsi_send_unsol_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001527{
Mike Christie5bb0b552006-04-06 21:26:46 -05001528 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001529 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001530 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001531
Mike Christie5bb0b552006-04-06 21:26:46 -05001532 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1533 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Mike Christie62f38302006-08-31 18:09:27 -04001534 dtask = &tcp_ctask->unsol_dtask;
1535
Mike Christieffd04362006-08-31 18:09:24 -04001536 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
1537 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
1538 sizeof(struct iscsi_hdr));
Mike Christieaf973482005-09-12 21:01:32 -05001539 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001540 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001541 (u8*)dtask->hdrext);
Mike Christie62f38302006-08-31 18:09:27 -04001542
Mike Christie5bb0b552006-04-06 21:26:46 -05001543 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Mike Christie62f38302006-08-31 18:09:27 -04001544 iscsi_set_padding(tcp_ctask, ctask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001545 }
Mike Christie3219e522006-05-30 00:37:28 -05001546
1547 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1548 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001549 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1550 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001551 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001552 }
1553
Mike Christiedd8c0d92006-08-31 18:09:28 -04001554 if (conn->datadgst_en) {
1555 dtask = &tcp_ctask->unsol_dtask;
1556 iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
1557 dtask->digest = 0;
1558 }
1559
Alex Aizman7ba24712005-08-04 19:30:08 -07001560 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001561 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001562 return 0;
1563}
1564
Mike Christie62f38302006-08-31 18:09:27 -04001565static int
1566iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001567{
Mike Christie5bb0b552006-04-06 21:26:46 -05001568 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001569 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001570
Mike Christie62f38302006-08-31 18:09:27 -04001571 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
1572 BUG_ON(!ctask->unsol_count);
1573 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
1574send_hdr:
1575 rc = iscsi_send_unsol_hdr(conn, ctask);
1576 if (rc)
1577 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001578 }
1579
Mike Christie62f38302006-08-31 18:09:27 -04001580 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
1581 struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask;
Mike Christie5bb0b552006-04-06 21:26:46 -05001582 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001583
Mike Christie62f38302006-08-31 18:09:27 -04001584 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1585 &tcp_ctask->sent, &ctask->data_count,
1586 &dtask->digestbuf, &dtask->digest);
Mike Christie5bb0b552006-04-06 21:26:46 -05001587 ctask->unsol_count -= tcp_ctask->sent - start;
Mike Christie62f38302006-08-31 18:09:27 -04001588 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001589 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001590 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1591 /*
1592 * Done with the Data-Out. Next, check if we need
1593 * to send another unsolicited Data-Out.
1594 */
1595 if (ctask->unsol_count) {
1596 debug_scsi("sending more uns\n");
1597 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
1598 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001599 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001600 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001601 return 0;
1602}
1603
Mike Christie62f38302006-08-31 18:09:27 -04001604static int iscsi_send_sol_pdu(struct iscsi_conn *conn,
1605 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001606{
Mike Christie5bb0b552006-04-06 21:26:46 -05001607 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie62f38302006-08-31 18:09:27 -04001608 struct iscsi_session *session = conn->session;
1609 struct iscsi_r2t_info *r2t;
1610 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001611 int left, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001612
Mike Christie218432c2007-05-30 12:57:17 -05001613 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR_INIT) {
Mike Christiedb37c502006-11-08 15:58:33 -06001614 if (!tcp_ctask->r2t) {
1615 spin_lock_bh(&session->lock);
Mike Christie62f38302006-08-31 18:09:27 -04001616 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1617 sizeof(void*));
Mike Christiedb37c502006-11-08 15:58:33 -06001618 spin_unlock_bh(&session->lock);
1619 }
Mike Christie62f38302006-08-31 18:09:27 -04001620send_hdr:
1621 r2t = tcp_ctask->r2t;
1622 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001623
Mike Christie62f38302006-08-31 18:09:27 -04001624 if (conn->hdrdgst_en)
1625 iscsi_hdr_digest(conn, &r2t->headbuf,
1626 (u8*)dtask->hdrext);
Mike Christie218432c2007-05-30 12:57:17 -05001627 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR_INIT;
1628 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
1629 }
1630
1631 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
1632 r2t = tcp_ctask->r2t;
1633 dtask = &r2t->dtask;
1634
Mike Christie62f38302006-08-31 18:09:27 -04001635 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
Mike Christie218432c2007-05-30 12:57:17 -05001636 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001637 return rc;
Mike Christie218432c2007-05-30 12:57:17 -05001638 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1639 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001640
Mike Christiedd8c0d92006-08-31 18:09:28 -04001641 if (conn->datadgst_en) {
1642 iscsi_data_digest_init(conn->dd_data, tcp_ctask);
1643 dtask->digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001644 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001645
Mike Christie62f38302006-08-31 18:09:27 -04001646 iscsi_set_padding(tcp_ctask, r2t->data_count);
1647 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1648 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1649 r2t->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001650 }
1651
Mike Christie62f38302006-08-31 18:09:27 -04001652 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
1653 r2t = tcp_ctask->r2t;
1654 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001655
Mike Christie62f38302006-08-31 18:09:27 -04001656 rc = iscsi_send_data(ctask, &r2t->sendbuf, &r2t->sg,
1657 &r2t->sent, &r2t->data_count,
1658 &dtask->digestbuf, &dtask->digest);
1659 if (rc)
1660 return rc;
1661 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001662
Mike Christie62f38302006-08-31 18:09:27 -04001663 /*
1664 * Done with this Data-Out. Next, check if we have
1665 * to send another Data-Out for this R2T.
1666 */
1667 BUG_ON(r2t->data_length - r2t->sent < 0);
1668 left = r2t->data_length - r2t->sent;
1669 if (left) {
1670 iscsi_solicit_data_cont(conn, ctask, r2t, left);
Mike Christie62f38302006-08-31 18:09:27 -04001671 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001672 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001673
Mike Christie62f38302006-08-31 18:09:27 -04001674 /*
1675 * Done with this R2T. Check if there are more
1676 * outstanding R2Ts ready to be processed.
1677 */
1678 spin_lock_bh(&session->lock);
1679 tcp_ctask->r2t = NULL;
1680 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
1681 sizeof(void*));
1682 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t,
1683 sizeof(void*))) {
1684 tcp_ctask->r2t = r2t;
Mike Christie62f38302006-08-31 18:09:27 -04001685 spin_unlock_bh(&session->lock);
1686 goto send_hdr;
1687 }
1688 spin_unlock_bh(&session->lock);
1689 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001690 return 0;
1691}
1692
Mike Christie218432c2007-05-30 12:57:17 -05001693/**
1694 * iscsi_tcp_ctask_xmit - xmit normal PDU task
1695 * @conn: iscsi connection
1696 * @ctask: iscsi command task
1697 *
1698 * Notes:
1699 * The function can return -EAGAIN in which case caller must
1700 * call it again later, or recover. '0' return code means successful
1701 * xmit.
1702 * The function is devided to logical helpers (above) for the different
1703 * xmit stages.
1704 *
1705 *iscsi_send_cmd_hdr()
1706 * XMSTATE_CMD_HDR_INIT - prepare Header and Data buffers Calculate
1707 * Header Digest
1708 * XMSTATE_CMD_HDR_XMIT - Transmit header in progress
1709 *
1710 *iscsi_send_padding
1711 * XMSTATE_W_PAD - Prepare and send pading
1712 * XMSTATE_W_RESEND_PAD - retry send pading
1713 *
1714 *iscsi_send_digest
1715 * XMSTATE_W_RESEND_DATA_DIGEST - Finalize and send Data Digest
1716 * XMSTATE_W_RESEND_DATA_DIGEST - retry sending digest
1717 *
1718 *iscsi_send_unsol_hdr
1719 * XMSTATE_UNS_INIT - prepare un-solicit data header and digest
1720 * XMSTATE_UNS_HDR - send un-solicit header
1721 *
1722 *iscsi_send_unsol_pdu
1723 * XMSTATE_UNS_DATA - send un-solicit data in progress
1724 *
1725 *iscsi_send_sol_pdu
1726 * XMSTATE_SOL_HDR_INIT - solicit data header and digest initialize
1727 * XMSTATE_SOL_HDR - send solicit header
1728 * XMSTATE_SOL_DATA - send solicit data
1729 *
1730 *iscsi_tcp_ctask_xmit
1731 * XMSTATE_IMM_DATA - xmit managment data (??)
1732 **/
Alex Aizman7ba24712005-08-04 19:30:08 -07001733static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001734iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001735{
Mike Christie5bb0b552006-04-06 21:26:46 -05001736 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001737 int rc = 0;
1738
1739 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001740 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001741
Mike Christie218432c2007-05-30 12:57:17 -05001742 rc = iscsi_send_cmd_hdr(conn, ctask);
1743 if (rc)
1744 return rc;
1745 if (ctask->sc->sc_data_direction != DMA_TO_DEVICE)
1746 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001747
Mike Christie5bb0b552006-04-06 21:26:46 -05001748 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Mike Christie62f38302006-08-31 18:09:27 -04001749 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1750 &tcp_ctask->sent, &ctask->imm_count,
1751 &tcp_ctask->immbuf, &tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001752 if (rc)
1753 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001754 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001755 }
1756
Mike Christie62f38302006-08-31 18:09:27 -04001757 rc = iscsi_send_unsol_pdu(conn, ctask);
1758 if (rc)
1759 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001760
Mike Christie62f38302006-08-31 18:09:27 -04001761 rc = iscsi_send_sol_pdu(conn, ctask);
1762 if (rc)
1763 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001764
1765 return rc;
1766}
1767
Mike Christie7b8631b2006-01-13 18:05:50 -06001768static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001769iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001770{
Mike Christie7b8631b2006-01-13 18:05:50 -06001771 struct iscsi_conn *conn;
1772 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001773 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001774
Mike Christie5bb0b552006-04-06 21:26:46 -05001775 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001776 if (!cls_conn)
1777 return NULL;
1778 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001779 /*
1780 * due to strange issues with iser these are not set
1781 * in iscsi_conn_setup
1782 */
Mike Christiebf32ed32007-02-28 17:32:17 -06001783 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
Alex Aizman7ba24712005-08-04 19:30:08 -07001784
Mike Christie5bb0b552006-04-06 21:26:46 -05001785 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1786 if (!tcp_conn)
1787 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001788
Mike Christie5bb0b552006-04-06 21:26:46 -05001789 conn->dd_data = tcp_conn;
1790 tcp_conn->iscsi_conn = conn;
1791 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1792 /* initial operational parameters */
1793 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07001794
James Bottomleyc9802cd2006-09-23 15:33:43 -05001795 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1796 CRYPTO_ALG_ASYNC);
1797 tcp_conn->tx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001798 if (IS_ERR(tcp_conn->tx_hash.tfm)) {
1799 printk(KERN_ERR "Could not create connection due to crc32c "
1800 "loading error %ld. Make sure the crc32c module is "
1801 "built as a module or into the kernel\n",
1802 PTR_ERR(tcp_conn->tx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001803 goto free_tcp_conn;
Mike Christie0f238412007-02-28 17:32:21 -06001804 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001805
James Bottomleyc9802cd2006-09-23 15:33:43 -05001806 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1807 CRYPTO_ALG_ASYNC);
1808 tcp_conn->rx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001809 if (IS_ERR(tcp_conn->rx_hash.tfm)) {
1810 printk(KERN_ERR "Could not create connection due to crc32c "
1811 "loading error %ld. Make sure the crc32c module is "
1812 "built as a module or into the kernel\n",
1813 PTR_ERR(tcp_conn->rx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001814 goto free_tx_tfm;
Mike Christie0f238412007-02-28 17:32:21 -06001815 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001816
Mike Christie7b8631b2006-01-13 18:05:50 -06001817 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001818
Mike Christiedd8c0d92006-08-31 18:09:28 -04001819free_tx_tfm:
James Bottomleyc9802cd2006-09-23 15:33:43 -05001820 crypto_free_hash(tcp_conn->tx_hash.tfm);
Mike Christiedd8c0d92006-08-31 18:09:28 -04001821free_tcp_conn:
1822 kfree(tcp_conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001823tcp_conn_alloc_fail:
1824 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001825 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001826}
1827
1828static void
Mike Christie1c834692006-07-24 15:47:26 -05001829iscsi_tcp_release_conn(struct iscsi_conn *conn)
1830{
Mike Christie22236962007-05-30 12:57:24 -05001831 struct iscsi_session *session = conn->session;
Mike Christie1c834692006-07-24 15:47:26 -05001832 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie22236962007-05-30 12:57:24 -05001833 struct socket *sock = tcp_conn->sock;
Mike Christie1c834692006-07-24 15:47:26 -05001834
Mike Christie22236962007-05-30 12:57:24 -05001835 if (!sock)
Mike Christie1c834692006-07-24 15:47:26 -05001836 return;
1837
Mike Christie22236962007-05-30 12:57:24 -05001838 sock_hold(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001839 iscsi_conn_restore_callbacks(tcp_conn);
Mike Christie22236962007-05-30 12:57:24 -05001840 sock_put(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001841
Mike Christie22236962007-05-30 12:57:24 -05001842 spin_lock_bh(&session->lock);
Mike Christie1c834692006-07-24 15:47:26 -05001843 tcp_conn->sock = NULL;
1844 conn->recv_lock = NULL;
Mike Christie22236962007-05-30 12:57:24 -05001845 spin_unlock_bh(&session->lock);
1846 sockfd_put(sock);
Mike Christie1c834692006-07-24 15:47:26 -05001847}
1848
1849static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001850iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001851{
Mike Christie7b8631b2006-01-13 18:05:50 -06001852 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001853 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001854
Mike Christie1c834692006-07-24 15:47:26 -05001855 iscsi_tcp_release_conn(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001856 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001857
Pete Wyckoff534284a2006-11-08 15:58:31 -06001858 if (tcp_conn->tx_hash.tfm)
1859 crypto_free_hash(tcp_conn->tx_hash.tfm);
1860 if (tcp_conn->rx_hash.tfm)
1861 crypto_free_hash(tcp_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001862
Mike Christie5bb0b552006-04-06 21:26:46 -05001863 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001864}
1865
Mike Christie1c834692006-07-24 15:47:26 -05001866static void
1867iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1868{
1869 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christied5390f52006-08-31 18:09:30 -04001870 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie1c834692006-07-24 15:47:26 -05001871
1872 iscsi_conn_stop(cls_conn, flag);
1873 iscsi_tcp_release_conn(conn);
Mike Christied5390f52006-08-31 18:09:30 -04001874 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christie1c834692006-07-24 15:47:26 -05001875}
1876
Mike Christie22236962007-05-30 12:57:24 -05001877static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
1878 char *buf, int *port,
1879 int (*getname)(struct socket *, struct sockaddr *,
1880 int *addrlen))
1881{
1882 struct sockaddr_storage *addr;
1883 struct sockaddr_in6 *sin6;
1884 struct sockaddr_in *sin;
1885 int rc = 0, len;
1886
Al Viroa9204872007-07-20 16:03:40 +01001887 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
Mike Christie22236962007-05-30 12:57:24 -05001888 if (!addr)
1889 return -ENOMEM;
1890
1891 if (getname(sock, (struct sockaddr *) addr, &len)) {
1892 rc = -ENODEV;
1893 goto free_addr;
1894 }
1895
1896 switch (addr->ss_family) {
1897 case AF_INET:
1898 sin = (struct sockaddr_in *)addr;
1899 spin_lock_bh(&conn->session->lock);
1900 sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
1901 *port = be16_to_cpu(sin->sin_port);
1902 spin_unlock_bh(&conn->session->lock);
1903 break;
1904 case AF_INET6:
1905 sin6 = (struct sockaddr_in6 *)addr;
1906 spin_lock_bh(&conn->session->lock);
1907 sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
1908 *port = be16_to_cpu(sin6->sin6_port);
1909 spin_unlock_bh(&conn->session->lock);
1910 break;
1911 }
1912free_addr:
1913 kfree(addr);
1914 return rc;
1915}
1916
Alex Aizman7ba24712005-08-04 19:30:08 -07001917static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001918iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001919 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001920 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001921{
Mike Christie5bb0b552006-04-06 21:26:46 -05001922 struct iscsi_conn *conn = cls_conn->dd_data;
1923 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001924 struct sock *sk;
1925 struct socket *sock;
1926 int err;
1927
1928 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001929 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001930 if (!sock) {
1931 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1932 return -EEXIST;
1933 }
Mike Christie22236962007-05-30 12:57:24 -05001934 /*
1935 * copy these values now because if we drop the session
1936 * userspace may still want to query the values since we will
1937 * be using them for the reconnect
1938 */
1939 err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1940 &conn->portal_port, kernel_getpeername);
1941 if (err)
1942 goto free_socket;
1943
1944 err = iscsi_tcp_get_addr(conn, sock, conn->local_address,
1945 &conn->local_port, kernel_getsockname);
1946 if (err)
1947 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001948
Mike Christie5bb0b552006-04-06 21:26:46 -05001949 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1950 if (err)
Mike Christie22236962007-05-30 12:57:24 -05001951 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001952
Mike Christie67a61112006-05-30 00:37:20 -05001953 /* bind iSCSI connection and socket */
1954 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001955
Mike Christie67a61112006-05-30 00:37:20 -05001956 /* setup Socket parameters */
1957 sk = sock->sk;
1958 sk->sk_reuse = 1;
1959 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1960 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001961
Mike Christie67a61112006-05-30 00:37:20 -05001962 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07001963
Mike Christie67a61112006-05-30 00:37:20 -05001964 /*
1965 * Intercept TCP callbacks for sendfile like receive
1966 * processing.
1967 */
1968 conn->recv_lock = &sk->sk_callback_lock;
1969 iscsi_conn_set_callbacks(conn);
1970 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1971 /*
1972 * set receive state machine into initial state
1973 */
1974 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07001975 return 0;
Mike Christie22236962007-05-30 12:57:24 -05001976
1977free_socket:
1978 sockfd_put(sock);
1979 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001980}
1981
Mike Christie5bb0b552006-04-06 21:26:46 -05001982/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05001983static void
Mike Christie77a23c22007-05-30 12:57:18 -05001984iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Mike Christie30a6c652006-04-06 21:13:39 -05001985{
Mike Christie5bb0b552006-04-06 21:26:46 -05001986 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie218432c2007-05-30 12:57:17 -05001987 tcp_mtask->xmstate = XMSTATE_IMM_HDR_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001988}
1989
1990static int
1991iscsi_r2tpool_alloc(struct iscsi_session *session)
1992{
1993 int i;
1994 int cmd_i;
1995
1996 /*
1997 * initialize per-task: R2T pool and xmit queue
1998 */
1999 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2000 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05002001 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002002
2003 /*
2004 * pre-allocated x4 as much r2ts to handle race when
2005 * target acks DataOut faster than we data_xmit() queues
2006 * could replenish r2tqueue.
2007 */
2008
2009 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05002010 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
2011 (void***)&tcp_ctask->r2ts,
2012 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002013 goto r2t_alloc_fail;
2014 }
2015
2016 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05002017 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07002018 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05002019 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2020 iscsi_pool_free(&tcp_ctask->r2tpool,
2021 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002022 goto r2t_alloc_fail;
2023 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002024 }
2025
2026 return 0;
2027
2028r2t_alloc_fail:
2029 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002030 struct iscsi_cmd_task *ctask = session->cmds[i];
2031 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2032
Mike Christie5bb0b552006-04-06 21:26:46 -05002033 kfifo_free(tcp_ctask->r2tqueue);
2034 iscsi_pool_free(&tcp_ctask->r2tpool,
2035 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002036 }
2037 return -ENOMEM;
2038}
2039
2040static void
2041iscsi_r2tpool_free(struct iscsi_session *session)
2042{
2043 int i;
2044
2045 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002046 struct iscsi_cmd_task *ctask = session->cmds[i];
2047 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2048
Mike Christie5bb0b552006-04-06 21:26:46 -05002049 kfifo_free(tcp_ctask->r2tqueue);
2050 iscsi_pool_free(&tcp_ctask->r2tpool,
2051 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002052 }
2053}
2054
Alex Aizman7ba24712005-08-04 19:30:08 -07002055static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002056iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002057 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07002058{
Mike Christie7b7232f2006-02-01 21:06:49 -06002059 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002060 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05002061 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002062 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07002063
Alex Aizman7ba24712005-08-04 19:30:08 -07002064 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002065 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002066 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002067 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christiedd8c0d92006-08-31 18:09:28 -04002068 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05002069 tcp_conn->hdr_size += sizeof(__u32);
Alex Aizman7ba24712005-08-04 19:30:08 -07002070 break;
2071 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002072 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002073 tcp_conn->sendpage = conn->datadgst_en ?
2074 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07002075 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002076 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002077 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07002078 if (session->max_r2t == roundup_pow_of_two(value))
2079 break;
2080 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002081 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002082 if (session->max_r2t & (session->max_r2t - 1))
2083 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2084 if (iscsi_r2tpool_alloc(session))
2085 return -ENOMEM;
2086 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002087 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002088 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002089 }
2090
2091 return 0;
2092}
2093
2094static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05002095iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2096 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06002097{
Mike Christie7b7232f2006-02-01 21:06:49 -06002098 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002099 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06002100
2101 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002102 case ISCSI_PARAM_CONN_PORT:
Mike Christie22236962007-05-30 12:57:24 -05002103 spin_lock_bh(&conn->session->lock);
2104 len = sprintf(buf, "%hu\n", conn->portal_port);
2105 spin_unlock_bh(&conn->session->lock);
Mike Christie8d2860b2006-05-02 19:46:47 -05002106 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002107 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie22236962007-05-30 12:57:24 -05002108 spin_lock_bh(&conn->session->lock);
2109 len = sprintf(buf, "%s\n", conn->portal_address);
2110 spin_unlock_bh(&conn->session->lock);
Mike Christiefd7255f2006-04-06 21:13:36 -05002111 break;
2112 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002113 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05002114 }
2115
2116 return len;
2117}
2118
Mike Christie22236962007-05-30 12:57:24 -05002119static int
2120iscsi_tcp_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2121 char *buf)
2122{
2123 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2124 int len;
2125
2126 switch (param) {
2127 case ISCSI_HOST_PARAM_IPADDRESS:
2128 spin_lock_bh(&session->lock);
2129 if (!session->leadconn)
2130 len = -ENODEV;
2131 else
2132 len = sprintf(buf, "%s\n",
2133 session->leadconn->local_address);
2134 spin_unlock_bh(&session->lock);
2135 break;
2136 default:
2137 return iscsi_host_get_param(shost, param, buf);
2138 }
2139 return len;
2140}
2141
Alex Aizman7ba24712005-08-04 19:30:08 -07002142static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002143iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002144{
Mike Christie7b7232f2006-02-01 21:06:49 -06002145 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002146 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002147
2148 stats->txdata_octets = conn->txdata_octets;
2149 stats->rxdata_octets = conn->rxdata_octets;
2150 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2151 stats->dataout_pdus = conn->dataout_pdus_cnt;
2152 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2153 stats->datain_pdus = conn->datain_pdus_cnt;
2154 stats->r2t_pdus = conn->r2t_pdus_cnt;
2155 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2156 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2157 stats->custom_length = 3;
2158 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002159 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002160 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002161 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002162 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2163 stats->custom[2].value = conn->eh_abort_cnt;
2164}
2165
Mike Christie5bb0b552006-04-06 21:26:46 -05002166static struct iscsi_cls_session *
2167iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2168 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05002169 uint16_t cmds_max, uint16_t qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05002170 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002171{
Mike Christie5bb0b552006-04-06 21:26:46 -05002172 struct iscsi_cls_session *cls_session;
2173 struct iscsi_session *session;
2174 uint32_t hn;
2175 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002176
Mike Christie15482712007-05-30 12:57:19 -05002177 cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05002178 sizeof(struct iscsi_tcp_cmd_task),
2179 sizeof(struct iscsi_tcp_mgmt_task),
2180 initial_cmdsn, &hn);
2181 if (!cls_session)
2182 return NULL;
2183 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002184
Mike Christie5bb0b552006-04-06 21:26:46 -05002185 session = class_to_transport_session(cls_session);
2186 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2187 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2188 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2189
2190 ctask->hdr = &tcp_ctask->hdr;
2191 }
2192
2193 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2194 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2195 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2196
2197 mtask->hdr = &tcp_mtask->hdr;
2198 }
2199
2200 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2201 goto r2tpool_alloc_fail;
2202
2203 return cls_session;
2204
2205r2tpool_alloc_fail:
2206 iscsi_session_teardown(cls_session);
2207 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002208}
2209
Mike Christie5bb0b552006-04-06 21:26:46 -05002210static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2211{
Mike Christie5bb0b552006-04-06 21:26:46 -05002212 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2213 iscsi_session_teardown(cls_session);
2214}
2215
Mike Christied1d81c02007-05-30 12:57:21 -05002216static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
2217{
Mike Christieb6d44fe2007-07-26 12:46:47 -05002218 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
Mike Christied1d81c02007-05-30 12:57:21 -05002219 blk_queue_dma_alignment(sdev->request_queue, 0);
2220 return 0;
2221}
2222
Mike Christie5bb0b552006-04-06 21:26:46 -05002223static struct scsi_host_template iscsi_sht = {
Mike Christie79743922007-07-26 12:46:46 -05002224 .module = THIS_MODULE,
Mike Christief4246b32006-07-24 15:47:54 -05002225 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05002226 .queuecommand = iscsi_queuecommand,
2227 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -05002228 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie5bb0b552006-04-06 21:26:46 -05002229 .sg_tablesize = ISCSI_SG_TABLESIZE,
Mike Christie8231f0e2007-02-28 17:32:20 -06002230 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -05002231 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2232 .eh_abort_handler = iscsi_eh_abort,
2233 .eh_host_reset_handler = iscsi_eh_host_reset,
2234 .use_clustering = DISABLE_CLUSTERING,
Mike Christied1d81c02007-05-30 12:57:21 -05002235 .slave_configure = iscsi_tcp_slave_configure,
Mike Christie5bb0b552006-04-06 21:26:46 -05002236 .proc_name = "iscsi_tcp",
2237 .this_id = -1,
2238};
2239
Alex Aizman7ba24712005-08-04 19:30:08 -07002240static struct iscsi_transport iscsi_tcp_transport = {
2241 .owner = THIS_MODULE,
2242 .name = "tcp",
2243 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2244 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002245 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2246 ISCSI_MAX_XMIT_DLENGTH |
2247 ISCSI_HDRDGST_EN |
2248 ISCSI_DATADGST_EN |
2249 ISCSI_INITIAL_R2T_EN |
2250 ISCSI_MAX_R2T |
2251 ISCSI_IMM_DATA_EN |
2252 ISCSI_FIRST_BURST |
2253 ISCSI_MAX_BURST |
2254 ISCSI_PDU_INORDER_EN |
2255 ISCSI_DATASEQ_INORDER_EN |
2256 ISCSI_ERL |
2257 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002258 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05002259 ISCSI_EXP_STATSN |
2260 ISCSI_PERSISTENT_PORT |
2261 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -05002262 ISCSI_TARGET_NAME | ISCSI_TPGT |
2263 ISCSI_USERNAME | ISCSI_PASSWORD |
2264 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN,
Mike Christie22236962007-05-30 12:57:24 -05002265 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
Mike Christied8196ed2007-05-30 12:57:25 -05002266 ISCSI_HOST_INITIATOR_NAME |
2267 ISCSI_HOST_NETDEV_NAME,
Alex Aizman7ba24712005-08-04 19:30:08 -07002268 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002269 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002270 .max_conn = 1,
2271 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002272 /* session management */
2273 .create_session = iscsi_tcp_session_create,
2274 .destroy_session = iscsi_tcp_session_destroy,
2275 /* connection management */
2276 .create_conn = iscsi_tcp_conn_create,
2277 .bind_conn = iscsi_tcp_conn_bind,
2278 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002279 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002280 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002281 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002282 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05002283 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -05002284 /* iscsi host params */
Mike Christie22236962007-05-30 12:57:24 -05002285 .get_host_param = iscsi_tcp_host_get_param,
Mike Christie0801c242007-05-30 12:57:12 -05002286 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -05002287 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002288 .send_pdu = iscsi_conn_send_pdu,
2289 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002290 .init_cmd_task = iscsi_tcp_cmd_init,
2291 .init_mgmt_task = iscsi_tcp_mgmt_init,
2292 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2293 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2294 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2295 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002296 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002297};
2298
2299static int __init
2300iscsi_tcp_init(void)
2301{
Alex Aizman7ba24712005-08-04 19:30:08 -07002302 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002303 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2304 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002305 return -EINVAL;
2306 }
2307 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2308
Mike Christie7b8631b2006-01-13 18:05:50 -06002309 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002310 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002311
Mike Christie7b8631b2006-01-13 18:05:50 -06002312 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002313}
2314
2315static void __exit
2316iscsi_tcp_exit(void)
2317{
2318 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002319}
2320
2321module_init(iscsi_tcp_init);
2322module_exit(iscsi_tcp_exit);