blob: 66a1ae1d6982b209596cdf75e6ce6616b634f099 [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
Herbert Xudc64ddf2006-08-24 18:45:50 +100029#include <linux/err.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070030#include <linux/types.h>
31#include <linux/list.h>
32#include <linux/inet.h>
33#include <linux/blkdev.h>
34#include <linux/crypto.h>
35#include <linux/delay.h>
36#include <linux/kfifo.h>
37#include <linux/scatterlist.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010038#include <linux/mutex.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070039#include <net/tcp.h>
40#include <scsi/scsi_cmnd.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{
Mike Christie7cae5152006-01-13 18:05:47 -060073 ibuf->sg.page = virt_to_page(vbuf);
74 ibuf->sg.offset = offset_in_page(vbuf);
Alex Aizman7ba24712005-08-04 19:30:08 -070075 ibuf->sg.length = size;
76 ibuf->sent = 0;
Mike Christie7cae5152006-01-13 18:05:47 -060077 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070078}
79
80static inline void
81iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
82{
Mike Christie7cae5152006-01-13 18:05:47 -060083 ibuf->sg.page = sg->page;
84 ibuf->sg.offset = sg->offset;
85 ibuf->sg.length = sg->length;
Alex Aizman7ba24712005-08-04 19:30:08 -070086 /*
87 * Fastpath: sg element fits into single page
88 */
Mike Christiea1e80c22006-01-13 18:05:56 -060089 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
Mike Christie7cae5152006-01-13 18:05:47 -060090 ibuf->use_sendmsg = 0;
91 else
92 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070093 ibuf->sent = 0;
94}
95
96static inline int
97iscsi_buf_left(struct iscsi_buf *ibuf)
98{
99 int rc;
100
101 rc = ibuf->sg.length - ibuf->sent;
102 BUG_ON(rc < 0);
103 return rc;
104}
105
106static inline void
Mike Christieaf973482005-09-12 21:01:32 -0500107iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
108 u8* crc)
Alex Aizman7ba24712005-08-04 19:30:08 -0700109{
Mike Christie5bb0b552006-04-06 21:26:46 -0500110 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Herbert Xudc64ddf2006-08-24 18:45:50 +1000111 struct hash_desc desc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500112
Herbert Xudc64ddf2006-08-24 18:45:50 +1000113 desc.tfm = tcp_conn->tx_tfm;
114 desc.flags = 0;
115 crypto_hash_digest(&desc, &buf->sg, buf->sg.length, crc);
Mike Christieaf973482005-09-12 21:01:32 -0500116 buf->sg.length += sizeof(uint32_t);
Alex Aizman7ba24712005-08-04 19:30:08 -0700117}
118
Alex Aizman7ba24712005-08-04 19:30:08 -0700119static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500120iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700121{
Mike Christie5bb0b552006-04-06 21:26:46 -0500122 struct sk_buff *skb = tcp_conn->in.skb;
Alex Aizman7ba24712005-08-04 19:30:08 -0700123
Mike Christie5bb0b552006-04-06 21:26:46 -0500124 tcp_conn->in.zero_copy_hdr = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700125
Mike Christie5bb0b552006-04-06 21:26:46 -0500126 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
127 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700128 /*
129 * Zero-copy PDU Header: using connection context
130 * to store header pointer.
131 */
132 if (skb_shinfo(skb)->frag_list == NULL &&
Mike Christie5bb0b552006-04-06 21:26:46 -0500133 !skb_shinfo(skb)->nr_frags) {
134 tcp_conn->in.hdr = (struct iscsi_hdr *)
135 ((char*)skb->data + tcp_conn->in.offset);
136 tcp_conn->in.zero_copy_hdr = 1;
137 } else {
Alex Aizman7ba24712005-08-04 19:30:08 -0700138 /* ignoring return code since we checked
139 * in.copy before */
Mike Christie5bb0b552006-04-06 21:26:46 -0500140 skb_copy_bits(skb, tcp_conn->in.offset,
141 &tcp_conn->hdr, tcp_conn->hdr_size);
142 tcp_conn->in.hdr = &tcp_conn->hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700143 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500144 tcp_conn->in.offset += tcp_conn->hdr_size;
145 tcp_conn->in.copy -= tcp_conn->hdr_size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700146 } else {
147 int hdr_remains;
148 int copylen;
149
150 /*
151 * PDU header scattered across SKB's,
152 * copying it... This'll happen quite rarely.
153 */
154
Mike Christie5bb0b552006-04-06 21:26:46 -0500155 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
156 tcp_conn->in.hdr_offset = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700157
Mike Christie5bb0b552006-04-06 21:26:46 -0500158 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700159 BUG_ON(hdr_remains <= 0);
160
Mike Christie5bb0b552006-04-06 21:26:46 -0500161 copylen = min(tcp_conn->in.copy, hdr_remains);
162 skb_copy_bits(skb, tcp_conn->in.offset,
163 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
164 copylen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700165
166 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
Mike Christie5bb0b552006-04-06 21:26:46 -0500167 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
168 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700169
Mike Christie5bb0b552006-04-06 21:26:46 -0500170 tcp_conn->in.offset += copylen;
171 tcp_conn->in.copy -= copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700172 if (copylen < hdr_remains) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500173 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
174 tcp_conn->in.hdr_offset += copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700175 return -EAGAIN;
176 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500177 tcp_conn->in.hdr = &tcp_conn->hdr;
178 tcp_conn->discontiguous_hdr_cnt++;
179 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700180 }
181
182 return 0;
183}
184
Mike Christie30a6c652006-04-06 21:13:39 -0500185/*
186 * must be called with session lock
187 */
188static void
Mike Christieb6c395e2006-07-24 15:47:15 -0500189iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700190{
Mike Christie5bb0b552006-04-06 21:26:46 -0500191 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500192 struct iscsi_r2t_info *r2t;
Mike Christie30a6c652006-04-06 21:13:39 -0500193 struct scsi_cmnd *sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700194
Mike Christieb6c395e2006-07-24 15:47:15 -0500195 /* flush ctask's r2t queues */
196 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
197 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
198 sizeof(void*));
199 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
200 }
201
Mike Christie30a6c652006-04-06 21:13:39 -0500202 sc = ctask->sc;
203 if (unlikely(!sc))
Alex Aizman7ba24712005-08-04 19:30:08 -0700204 return;
Mike Christie30a6c652006-04-06 21:13:39 -0500205
Mike Christie5bb0b552006-04-06 21:26:46 -0500206 tcp_ctask->xmstate = XMSTATE_IDLE;
207 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700208}
209
210/**
211 * iscsi_data_rsp - SCSI Data-In Response processing
212 * @conn: iscsi connection
213 * @ctask: scsi command task
214 **/
215static int
216iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
217{
218 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500219 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
220 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
221 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700222 struct iscsi_session *session = conn->session;
223 int datasn = be32_to_cpu(rhdr->datasn);
224
225 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
226 if (rc)
227 return rc;
228 /*
229 * setup Data-In byte counter (gets decremented..)
230 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500231 ctask->data_count = tcp_conn->in.datalen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700232
Mike Christie5bb0b552006-04-06 21:26:46 -0500233 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700234 return 0;
235
236 if (ctask->datasn != datasn)
237 return ISCSI_ERR_DATASN;
238
239 ctask->datasn++;
240
Mike Christie5bb0b552006-04-06 21:26:46 -0500241 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
242 if (tcp_ctask->data_offset + tcp_conn->in.datalen > ctask->total_length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700243 return ISCSI_ERR_DATA_OFFSET;
244
245 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
246 struct scsi_cmnd *sc = ctask->sc;
247
248 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600249 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700250 int res_count = be32_to_cpu(rhdr->residual_count);
251
252 if (res_count > 0 &&
253 res_count <= sc->request_bufflen) {
254 sc->resid = res_count;
255 sc->result = (DID_OK << 16) | rhdr->cmd_status;
256 } else
257 sc->result = (DID_BAD_TARGET << 16) |
258 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600259 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700260 sc->resid = be32_to_cpu(rhdr->residual_count);
261 sc->result = (DID_OK << 16) | rhdr->cmd_status;
262 } else
263 sc->result = (DID_OK << 16) | rhdr->cmd_status;
264 }
265
266 conn->datain_pdus_cnt++;
267 return 0;
268}
269
270/**
271 * iscsi_solicit_data_init - initialize first Data-Out
272 * @conn: iscsi connection
273 * @ctask: scsi command task
274 * @r2t: R2T info
275 *
276 * Notes:
277 * Initialize first Data-Out within this R2T sequence and finds
278 * proper data_offset within this SCSI command.
279 *
280 * This function is called with connection lock taken.
281 **/
282static void
283iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
284 struct iscsi_r2t_info *r2t)
285{
286 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700287 struct scsi_cmnd *sc = ctask->sc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500288 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
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
Alex Aizman7ba24712005-08-04 19:30:08 -0700316 if (sc->use_sg) {
317 int i, sg_count = 0;
318 struct scatterlist *sg = sc->request_buffer;
319
320 r2t->sg = NULL;
321 for (i = 0; i < sc->use_sg; i++, sg += 1) {
322 /* FIXME: prefetch ? */
323 if (sg_count + sg->length > r2t->data_offset) {
324 int page_offset;
325
326 /* sg page found! */
327
328 /* offset within this page */
329 page_offset = r2t->data_offset - sg_count;
330
331 /* fill in this buffer */
332 iscsi_buf_init_sg(&r2t->sendbuf, sg);
333 r2t->sendbuf.sg.offset += page_offset;
334 r2t->sendbuf.sg.length -= page_offset;
335
336 /* xmit logic will continue with next one */
337 r2t->sg = sg + 1;
338 break;
339 }
340 sg_count += sg->length;
341 }
342 BUG_ON(r2t->sg == NULL);
343 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500344 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -0700345 (char*)sc->request_buffer + r2t->data_offset,
346 r2t->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -0700347}
348
349/**
350 * iscsi_r2t_rsp - iSCSI R2T Response processing
351 * @conn: iscsi connection
352 * @ctask: scsi command task
353 **/
354static int
355iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
356{
357 struct iscsi_r2t_info *r2t;
358 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500359 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
360 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
361 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700362 int r2tsn = be32_to_cpu(rhdr->r2tsn);
363 int rc;
364
Mike Christie5bb0b552006-04-06 21:26:46 -0500365 if (tcp_conn->in.datalen)
Alex Aizman7ba24712005-08-04 19:30:08 -0700366 return ISCSI_ERR_DATALEN;
367
Mike Christie5bb0b552006-04-06 21:26:46 -0500368 if (tcp_ctask->exp_r2tsn && tcp_ctask->exp_r2tsn != r2tsn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700369 return ISCSI_ERR_R2TSN;
370
371 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
372 if (rc)
373 return rc;
374
375 /* FIXME: use R2TSN to detect missing R2T */
376
377 /* fill-in new R2T associated with the task */
378 spin_lock(&session->lock);
379 if (!ctask->sc || ctask->mtask ||
380 session->state != ISCSI_STATE_LOGGED_IN) {
381 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
382 "recovery...\n", ctask->itt);
383 spin_unlock(&session->lock);
384 return 0;
385 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500386
Mike Christie5bb0b552006-04-06 21:26:46 -0500387 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700388 BUG_ON(!rc);
389
390 r2t->exp_statsn = rhdr->statsn;
391 r2t->data_length = be32_to_cpu(rhdr->data_length);
392 if (r2t->data_length == 0 ||
393 r2t->data_length > session->max_burst) {
394 spin_unlock(&session->lock);
395 return ISCSI_ERR_DATALEN;
396 }
397
398 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
399 if (r2t->data_offset + r2t->data_length > ctask->total_length) {
400 spin_unlock(&session->lock);
401 return ISCSI_ERR_DATALEN;
402 }
403
404 r2t->ttt = rhdr->ttt; /* no flip */
405 r2t->solicit_datasn = 0;
406
407 iscsi_solicit_data_init(conn, ctask, r2t);
408
Mike Christie5bb0b552006-04-06 21:26:46 -0500409 tcp_ctask->exp_r2tsn = r2tsn + 1;
410 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
411 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christieb6c395e2006-07-24 15:47:15 -0500412 list_move_tail(&ctask->running, &conn->xmitqueue);
Alex Aizman7ba24712005-08-04 19:30:08 -0700413
Mike Christie55e32992006-01-13 18:05:53 -0600414 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -0700415 conn->r2t_pdus_cnt++;
416 spin_unlock(&session->lock);
417
418 return 0;
419}
420
421static int
Mike Christie5bb0b552006-04-06 21:26:46 -0500422iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700423{
Mike Christie5bb0b552006-04-06 21:26:46 -0500424 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700425 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700426 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500427 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
428 uint32_t cdgst, rdgst = 0, itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700429
Mike Christie5bb0b552006-04-06 21:26:46 -0500430 hdr = tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700431
432 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500433 tcp_conn->in.datalen = ntoh24(hdr->dlength);
434 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700435 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500436 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700437 return ISCSI_ERR_DATALEN;
438 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500439 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700440
441 /* read AHS */
Mike Christie5bb0b552006-04-06 21:26:46 -0500442 ahslen = hdr->hlength << 2;
443 tcp_conn->in.offset += ahslen;
444 tcp_conn->in.copy -= ahslen;
445 if (tcp_conn->in.copy < 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700446 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
Mike Christie5bb0b552006-04-06 21:26:46 -0500447 "%d bytes\n", ahslen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700448 return ISCSI_ERR_AHSLEN;
449 }
450
451 /* calculate read padding */
Mike Christie5bb0b552006-04-06 21:26:46 -0500452 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
453 if (tcp_conn->in.padding) {
454 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
455 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
Alex Aizman7ba24712005-08-04 19:30:08 -0700456 }
457
458 if (conn->hdrdgst_en) {
Herbert Xudc64ddf2006-08-24 18:45:50 +1000459 struct hash_desc desc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700460 struct scatterlist sg;
461
462 sg_init_one(&sg, (u8 *)hdr,
Mike Christie5bb0b552006-04-06 21:26:46 -0500463 sizeof(struct iscsi_hdr) + ahslen);
Herbert Xudc64ddf2006-08-24 18:45:50 +1000464 desc.tfm = tcp_conn->rx_tfm;
465 desc.flags = 0;
466 crypto_hash_digest(&desc, &sg, sg.length, (u8 *)&cdgst);
Alex Aizman7ba24712005-08-04 19:30:08 -0700467 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
Mike Christie5bb0b552006-04-06 21:26:46 -0500468 ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600469 if (cdgst != rdgst) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500470 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
471 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
Mike Christie8a47cd32005-11-30 02:27:19 -0600472 return ISCSI_ERR_HDR_DGST;
473 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700474 }
475
Mike Christie5bb0b552006-04-06 21:26:46 -0500476 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700477 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500478 rc = iscsi_verify_itt(conn, hdr, &itt);
479 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
480 tcp_conn->in.datalen = 0; /* force drop */
481 return 0;
482 } else if (rc)
483 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700484
485 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500486 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
487 ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700488
Mike Christie5bb0b552006-04-06 21:26:46 -0500489 switch(opcode) {
490 case ISCSI_OP_SCSI_DATA_IN:
491 tcp_conn->in.ctask = session->cmds[itt];
492 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
Mike Christie275fd7d2006-07-24 15:47:17 -0500493 if (rc)
494 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500495 /* fall through */
496 case ISCSI_OP_SCSI_CMD_RSP:
497 tcp_conn->in.ctask = session->cmds[itt];
498 if (tcp_conn->in.datalen)
499 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700500
Mike Christie5bb0b552006-04-06 21:26:46 -0500501 spin_lock(&session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500502 iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
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 */
525 if (DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH <
526 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,
530 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, opcode);
531 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);
598 int 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);
Mike Christie5bb0b552006-04-06 21:26:46 -0500607 BUG_ON(tcp_ctask->sent + size > ctask->total_length);
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 Christiec8dc1e52006-07-24 15:47:39 -0500647iscsi_tcp_copy(struct iscsi_conn *conn)
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_size = tcp_conn->in.datalen;
651 int buf_left = buf_size - tcp_conn->data_copied;
652 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700653 int rc;
654
655 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500656 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700657 BUG_ON(size <= 0);
658
Mike Christie5bb0b552006-04-06 21:26:46 -0500659 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christiec8dc1e52006-07-24 15:47:39 -0500660 (char*)conn->data + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700661 BUG_ON(rc);
662
Mike Christie5bb0b552006-04-06 21:26:46 -0500663 tcp_conn->in.offset += size;
664 tcp_conn->in.copy -= size;
665 tcp_conn->in.copied += size;
666 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700667
Mike Christie5bb0b552006-04-06 21:26:46 -0500668 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700669 return -EAGAIN;
670
671 return 0;
672}
673
674static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -0500675partial_sg_digest_update(struct iscsi_tcp_conn *tcp_conn,
676 struct scatterlist *sg, int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700677{
678 struct scatterlist temp;
679
680 memcpy(&temp, sg, sizeof(struct scatterlist));
681 temp.offset = offset;
682 temp.length = length;
Herbert Xudc64ddf2006-08-24 18:45:50 +1000683 crypto_hash_update(&tcp_conn->data_rx_hash, &temp, length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700684}
685
Mike Christief6cfba12005-11-29 23:12:57 -0600686static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500687iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600688{
689 struct scatterlist tmp;
690
691 sg_init_one(&tmp, buf, len);
Herbert Xudc64ddf2006-08-24 18:45:50 +1000692 crypto_hash_update(&tcp_conn->data_rx_hash, &tmp, len);
Mike Christief6cfba12005-11-29 23:12:57 -0600693}
694
Alex Aizman7ba24712005-08-04 19:30:08 -0700695static int iscsi_scsi_data_in(struct iscsi_conn *conn)
696{
Mike Christie5bb0b552006-04-06 21:26:46 -0500697 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
698 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
699 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700700 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600701 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700702 int i, offset, rc = 0;
703
704 BUG_ON((void*)ctask != sc->SCp.ptr);
705
706 /*
707 * copying Data-In into the Scsi_Cmnd
708 */
709 if (!sc->use_sg) {
710 i = ctask->data_count;
Mike Christie5bb0b552006-04-06 21:26:46 -0500711 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
712 sc->request_bufflen,
713 tcp_ctask->data_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700714 if (rc == -EAGAIN)
715 return rc;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600716 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500717 iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
718 i);
Alex Aizman7ba24712005-08-04 19:30:08 -0700719 rc = 0;
720 goto done;
721 }
722
Mike Christie5bb0b552006-04-06 21:26:46 -0500723 offset = tcp_ctask->data_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700724 sg = sc->request_buffer;
725
Mike Christie5bb0b552006-04-06 21:26:46 -0500726 if (tcp_ctask->data_offset)
727 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700728 offset -= sg[i].length;
729 /* we've passed through partial sg*/
730 if (offset < 0)
731 offset = 0;
732
Mike Christie5bb0b552006-04-06 21:26:46 -0500733 for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700734 char *dest;
735
736 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500737 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700738 sg[i].length, offset);
739 kunmap_atomic(dest, KM_SOFTIRQ0);
740 if (rc == -EAGAIN)
741 /* continue with the next SKB/PDU */
742 return rc;
743 if (!rc) {
744 if (conn->datadgst_en) {
745 if (!offset)
Herbert Xudc64ddf2006-08-24 18:45:50 +1000746 crypto_hash_update(
747 &tcp_conn->data_rx_hash,
748 &sg[i], sg[i].length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700749 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500750 partial_sg_digest_update(tcp_conn,
751 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700752 sg[i].offset + offset,
753 sg[i].length - offset);
754 }
755 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500756 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700757 }
758
759 if (!ctask->data_count) {
760 if (rc && conn->datadgst_en)
761 /*
762 * data-in is complete, but buffer not...
763 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500764 partial_sg_digest_update(tcp_conn, &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700765 sg[i].offset, sg[i].length-rc);
766 rc = 0;
767 break;
768 }
769
Mike Christie5bb0b552006-04-06 21:26:46 -0500770 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700771 return -EAGAIN;
772 }
773 BUG_ON(ctask->data_count);
774
775done:
776 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500777 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Mike Christieb6c395e2006-07-24 15:47:15 -0500778 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
779 (long)sc, sc->result, ctask->itt,
780 tcp_conn->in.hdr->flags);
Mike Christie5bb0b552006-04-06 21:26:46 -0500781 spin_lock(&conn->session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500782 iscsi_tcp_cleanup_ctask(conn, ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500783 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
784 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700785 }
786
787 return rc;
788}
789
790static int
791iscsi_data_recv(struct iscsi_conn *conn)
792{
Mike Christie5bb0b552006-04-06 21:26:46 -0500793 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
794 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700795
Mike Christie5bb0b552006-04-06 21:26:46 -0500796 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
797 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700798 case ISCSI_OP_SCSI_DATA_IN:
799 rc = iscsi_scsi_data_in(conn);
800 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500801 case ISCSI_OP_SCSI_CMD_RSP:
802 spin_lock(&conn->session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500803 iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500804 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700805 case ISCSI_OP_TEXT_RSP:
806 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500807 case ISCSI_OP_ASYNC_EVENT:
808 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700809 /*
810 * Collect data segment to the connection's data
811 * placeholder
812 */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500813 if (iscsi_tcp_copy(conn)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700814 rc = -EAGAIN;
815 goto exit;
816 }
817
Mike Christiec8dc1e52006-07-24 15:47:39 -0500818 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500819 tcp_conn->in.datalen);
820 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
Mike Christiec8dc1e52006-07-24 15:47:39 -0500821 iscsi_recv_digest_update(tcp_conn, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500822 tcp_conn->in.datalen);
823 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700824 default:
825 BUG_ON(1);
826 }
827exit:
828 return rc;
829}
830
831/**
832 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
833 * @rd_desc: read descriptor
834 * @skb: socket buffer
835 * @offset: offset in skb
836 * @len: skb->len - offset
837 **/
838static int
839iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
840 unsigned int offset, size_t len)
841{
842 int rc;
843 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500844 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700845 int processed;
846 char pad[ISCSI_PAD_LEN];
847 struct scatterlist sg;
848
849 /*
850 * Save current SKB and its offset in the corresponding
851 * connection context.
852 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500853 tcp_conn->in.copy = skb->len - offset;
854 tcp_conn->in.offset = offset;
855 tcp_conn->in.skb = skb;
856 tcp_conn->in.len = tcp_conn->in.copy;
857 BUG_ON(tcp_conn->in.copy <= 0);
858 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700859
860more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500861 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700862 rc = 0;
863
864 if (unlikely(conn->suspend_rx)) {
865 debug_tcp("conn %d Rx suspended!\n", conn->id);
866 return 0;
867 }
868
Mike Christie5bb0b552006-04-06 21:26:46 -0500869 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
870 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
871 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700872 if (rc) {
873 if (rc == -EAGAIN)
874 goto nomore;
875 else {
Mike Christied82967c2006-07-24 15:47:11 -0500876 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700877 return 0;
878 }
879 }
880
881 /*
882 * Verify and process incoming PDU header.
883 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500884 rc = iscsi_tcp_hdr_recv(conn);
885 if (!rc && tcp_conn->in.datalen) {
Mike Christie8a47cd32005-11-30 02:27:19 -0600886 if (conn->datadgst_en) {
Herbert Xudc64ddf2006-08-24 18:45:50 +1000887 crypto_hash_init(&tcp_conn->data_rx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -0700888 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500889 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700890 } else if (rc) {
Mike Christie40527af2006-07-24 15:47:45 -0500891 iscsi_conn_failure(conn, rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700892 return 0;
893 }
894 }
895
Mike Christie5bb0b552006-04-06 21:26:46 -0500896 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
Mike Christief6cfba12005-11-29 23:12:57 -0600897 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500898
Alex Aizman7ba24712005-08-04 19:30:08 -0700899 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500900 tcp_conn->in.offset, tcp_conn->in.copy);
901 skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christief6cfba12005-11-29 23:12:57 -0600902 &recv_digest, 4);
Mike Christie5bb0b552006-04-06 21:26:46 -0500903 tcp_conn->in.offset += 4;
904 tcp_conn->in.copy -= 4;
905 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 &&
920 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700921
922 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500923 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700924
925 rc = iscsi_data_recv(conn);
926 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500927 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700928 goto again;
Mike Christied82967c2006-07-24 15:47:11 -0500929 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700930 return 0;
931 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500932 tcp_conn->in.copy -= tcp_conn->in.padding;
933 tcp_conn->in.offset += tcp_conn->in.padding;
Mike Christie8a47cd32005-11-30 02:27:19 -0600934 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500935 if (tcp_conn->in.padding) {
936 debug_tcp("padding -> %d\n",
937 tcp_conn->in.padding);
938 memset(pad, 0, tcp_conn->in.padding);
939 sg_init_one(&sg, pad, tcp_conn->in.padding);
Herbert Xudc64ddf2006-08-24 18:45:50 +1000940 crypto_hash_update(&tcp_conn->data_rx_hash,
941 &sg, sg.length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700942 }
Herbert Xudc64ddf2006-08-24 18:45:50 +1000943 crypto_hash_final(&tcp_conn->data_rx_hash,
944 (u8 *)&tcp_conn->in.datadgst);
Mike Christie5bb0b552006-04-06 21:26:46 -0500945 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
946 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700947 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500948 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700949 }
950
951 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500952 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
953 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700954
Mike Christie5bb0b552006-04-06 21:26:46 -0500955 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700956 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500957 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700958 goto more;
959 }
960
961nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500962 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700963 BUG_ON(processed == 0);
964 return processed;
965
966again:
Mike Christie5bb0b552006-04-06 21:26:46 -0500967 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700968 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
969 processed, (int)len, (int)rd_desc->count);
970 BUG_ON(processed == 0);
971 BUG_ON(processed > len);
972
973 conn->rxdata_octets += processed;
974 return processed;
975}
976
977static void
978iscsi_tcp_data_ready(struct sock *sk, int flag)
979{
980 struct iscsi_conn *conn = sk->sk_user_data;
981 read_descriptor_t rd_desc;
982
983 read_lock(&sk->sk_callback_lock);
984
Mike Christie665b44a2006-05-02 19:46:49 -0500985 /*
986 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
987 * We set count to 1 because we want the network layer to
988 * hand us all the skbs that are available. iscsi_tcp_data_recv
989 * handled pdus that cross buffers or pdus that still need data.
990 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700991 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -0500992 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -0700993 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
994
995 read_unlock(&sk->sk_callback_lock);
996}
997
998static void
999iscsi_tcp_state_change(struct sock *sk)
1000{
Mike Christie5bb0b552006-04-06 21:26:46 -05001001 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001002 struct iscsi_conn *conn;
1003 struct iscsi_session *session;
1004 void (*old_state_change)(struct sock *);
1005
1006 read_lock(&sk->sk_callback_lock);
1007
1008 conn = (struct iscsi_conn*)sk->sk_user_data;
1009 session = conn->session;
1010
Mike Christiee6273992005-11-29 23:12:49 -06001011 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1012 sk->sk_state == TCP_CLOSE) &&
1013 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001014 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1015 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1016 }
1017
Mike Christie5bb0b552006-04-06 21:26:46 -05001018 tcp_conn = conn->dd_data;
1019 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001020
1021 read_unlock(&sk->sk_callback_lock);
1022
1023 old_state_change(sk);
1024}
1025
1026/**
1027 * iscsi_write_space - Called when more output buffer space is available
1028 * @sk: socket space is available for
1029 **/
1030static void
1031iscsi_write_space(struct sock *sk)
1032{
1033 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001034 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1035
1036 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001037 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001038 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001039}
1040
1041static void
1042iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1043{
Mike Christie5bb0b552006-04-06 21:26:46 -05001044 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1045 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001046
1047 /* assign new callbacks */
1048 write_lock_bh(&sk->sk_callback_lock);
1049 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001050 tcp_conn->old_data_ready = sk->sk_data_ready;
1051 tcp_conn->old_state_change = sk->sk_state_change;
1052 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001053 sk->sk_data_ready = iscsi_tcp_data_ready;
1054 sk->sk_state_change = iscsi_tcp_state_change;
1055 sk->sk_write_space = iscsi_write_space;
1056 write_unlock_bh(&sk->sk_callback_lock);
1057}
1058
1059static void
Mike Christie1c834692006-07-24 15:47:26 -05001060iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001061{
Mike Christie5bb0b552006-04-06 21:26:46 -05001062 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001063
1064 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1065 write_lock_bh(&sk->sk_callback_lock);
1066 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001067 sk->sk_data_ready = tcp_conn->old_data_ready;
1068 sk->sk_state_change = tcp_conn->old_state_change;
1069 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001070 sk->sk_no_check = 0;
1071 write_unlock_bh(&sk->sk_callback_lock);
1072}
1073
1074/**
1075 * iscsi_send - generic send routine
1076 * @sk: kernel's socket
1077 * @buf: buffer to write from
1078 * @size: actual size to write
1079 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001080 */
1081static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001082iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001083{
Mike Christie5bb0b552006-04-06 21:26:46 -05001084 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1085 struct socket *sk = tcp_conn->sock;
Mike Christie3219e522006-05-30 00:37:28 -05001086 int offset = buf->sg.offset + buf->sent, res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001087
Mike Christie7cae5152006-01-13 18:05:47 -06001088 /*
1089 * if we got use_sg=0 or are sending something we kmallocd
1090 * then we did not have to do kmap (kmap returns page_address)
1091 *
1092 * if we got use_sg > 0, but had to drop down, we do not
1093 * set clustering so this should only happen for that
1094 * slab case.
1095 */
1096 if (buf->use_sendmsg)
Mike Christie3219e522006-05-30 00:37:28 -05001097 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
Mike Christie7cae5152006-01-13 18:05:47 -06001098 else
Mike Christie3219e522006-05-30 00:37:28 -05001099 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1100
1101 if (res >= 0) {
1102 conn->txdata_octets += res;
1103 buf->sent += res;
1104 return res;
1105 }
1106
1107 tcp_conn->sendpage_failures_cnt++;
1108 if (res == -EAGAIN)
1109 res = -ENOBUFS;
1110 else
1111 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1112 return res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001113}
1114
1115/**
1116 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1117 * @conn: iscsi connection
1118 * @buf: buffer to write from
1119 * @datalen: lenght of data to be sent after the header
1120 *
1121 * Notes:
1122 * (Tx, Fast Path)
1123 **/
1124static inline int
1125iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1126{
Alex Aizman7ba24712005-08-04 19:30:08 -07001127 int flags = 0; /* MSG_DONTWAIT; */
1128 int res, size;
1129
1130 size = buf->sg.length - buf->sent;
1131 BUG_ON(buf->sent + size > buf->sg.length);
1132 if (buf->sent + size != buf->sg.length || datalen)
1133 flags |= MSG_MORE;
1134
FUJITA Tomonori56851692006-01-13 18:05:44 -06001135 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001136 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1137 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001138 if (size != res)
1139 return -EAGAIN;
1140 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001141 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001142
1143 return res;
1144}
1145
1146/**
1147 * iscsi_sendpage - send one page of iSCSI Data-Out.
1148 * @conn: iscsi connection
1149 * @buf: buffer to write from
1150 * @count: remaining data
1151 * @sent: number of bytes sent
1152 *
1153 * Notes:
1154 * (Tx, Fast Path)
1155 **/
1156static inline int
1157iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1158 int *count, int *sent)
1159{
Alex Aizman7ba24712005-08-04 19:30:08 -07001160 int flags = 0; /* MSG_DONTWAIT; */
1161 int res, size;
1162
1163 size = buf->sg.length - buf->sent;
1164 BUG_ON(buf->sent + size > buf->sg.length);
1165 if (size > *count)
1166 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001167 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001168 flags |= MSG_MORE;
1169
FUJITA Tomonori56851692006-01-13 18:05:44 -06001170 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001171 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1172 size, buf->sent, *count, *sent, res);
1173 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001174 *count -= res;
1175 *sent += res;
1176 if (size != res)
1177 return -EAGAIN;
1178 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001179 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001180
1181 return res;
1182}
1183
1184static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001185iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
1186 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001187{
Mike Christie5bb0b552006-04-06 21:26:46 -05001188 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1189
Herbert Xudc64ddf2006-08-24 18:45:50 +10001190 crypto_hash_init(&tcp_conn->data_tx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -05001191 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001192}
1193
Arjan van de Ven858119e2006-01-14 13:20:43 -08001194static int
Alex Aizman7ba24712005-08-04 19:30:08 -07001195iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1196 struct iscsi_buf *buf, uint32_t *digest, int final)
1197{
Mike Christie5bb0b552006-04-06 21:26:46 -05001198 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1199 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001200 int rc = 0;
1201 int sent = 0;
1202
1203 if (final)
Herbert Xudc64ddf2006-08-24 18:45:50 +10001204 crypto_hash_final(&tcp_conn->data_tx_hash, (u8 *)digest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001205
Mike Christie6e458cc2006-05-18 20:31:31 -05001206 iscsi_buf_init_iov(buf, (char*)digest, 4);
Mike Christie5bb0b552006-04-06 21:26:46 -05001207 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001208 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001209 tcp_ctask->datadigest = *digest;
1210 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
Alex Aizman7ba24712005-08-04 19:30:08 -07001211 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001212 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001213 return rc;
1214}
1215
1216/**
1217 * iscsi_solicit_data_cont - initialize next Data-Out
1218 * @conn: iscsi connection
1219 * @ctask: scsi command task
1220 * @r2t: R2T info
1221 * @left: bytes left to transfer
1222 *
1223 * Notes:
1224 * Initialize next Data-Out within this R2T sequence and continue
1225 * to process next Scatter-Gather element(if any) of this SCSI command.
1226 *
1227 * Called under connection lock.
1228 **/
1229static void
1230iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1231 struct iscsi_r2t_info *r2t, int left)
1232{
Mike Christie5bb0b552006-04-06 21:26:46 -05001233 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001234 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001235 struct scsi_cmnd *sc = ctask->sc;
1236 int new_offset;
1237
Mike Christieffbfe922006-05-18 20:31:36 -05001238 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001239 memset(hdr, 0, sizeof(struct iscsi_data));
1240 hdr->ttt = r2t->ttt;
1241 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1242 r2t->solicit_datasn++;
1243 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001244 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1245 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001246 hdr->exp_statsn = r2t->exp_statsn;
1247 new_offset = r2t->data_offset + r2t->sent;
1248 hdr->offset = cpu_to_be32(new_offset);
1249 if (left > conn->max_xmit_dlength) {
1250 hton24(hdr->dlength, conn->max_xmit_dlength);
1251 r2t->data_count = conn->max_xmit_dlength;
1252 } else {
1253 hton24(hdr->dlength, left);
1254 r2t->data_count = left;
1255 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1256 }
1257 conn->dataout_pdus_cnt++;
1258
Mike Christie6e458cc2006-05-18 20:31:31 -05001259 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001260 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001261
Alex Aizman7ba24712005-08-04 19:30:08 -07001262 if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001263 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
Alex Aizman7ba24712005-08-04 19:30:08 -07001264 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1265 r2t->sg += 1;
1266 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001267 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -07001268 (char*)sc->request_buffer + new_offset,
1269 r2t->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001270}
1271
1272static void
1273iscsi_unsolicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1274{
Mike Christie5bb0b552006-04-06 21:26:46 -05001275 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001276 struct iscsi_data_task *dtask;
1277
Mike Christieffbfe922006-05-18 20:31:36 -05001278 dtask = tcp_ctask->dtask = &tcp_ctask->unsol_dtask;
Mike Christie5bb0b552006-04-06 21:26:46 -05001279 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr,
1280 tcp_ctask->r2t_data_count);
Mike Christie6e458cc2006-05-18 20:31:31 -05001281 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001282 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001283}
1284
1285/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001286 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001287 * @conn: iscsi connection
1288 * @ctask: scsi command task
1289 * @sc: scsi command
1290 **/
1291static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001292iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001293{
Mike Christie5bb0b552006-04-06 21:26:46 -05001294 struct scsi_cmnd *sc = ctask->sc;
1295 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001296
Mike Christie5bb0b552006-04-06 21:26:46 -05001297 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Alex Aizman7ba24712005-08-04 19:30:08 -07001298
Mike Christie5bb0b552006-04-06 21:26:46 -05001299 tcp_ctask->sent = 0;
1300 tcp_ctask->sg_count = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001301
1302 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001303 tcp_ctask->xmstate = XMSTATE_W_HDR;
1304 tcp_ctask->exp_r2tsn = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001305 BUG_ON(ctask->total_length == 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05001306
Alex Aizman7ba24712005-08-04 19:30:08 -07001307 if (sc->use_sg) {
1308 struct scatterlist *sg = sc->request_buffer;
1309
Mike Christie5bb0b552006-04-06 21:26:46 -05001310 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1311 &sg[tcp_ctask->sg_count++]);
1312 tcp_ctask->sg = sg;
1313 tcp_ctask->bad_sg = sg + sc->use_sg;
Alex Aizman7ba24712005-08-04 19:30:08 -07001314 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001315 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1316 sc->request_buffer,
1317 sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001318
Mike Christie5bb0b552006-04-06 21:26:46 -05001319 if (ctask->imm_count)
1320 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001321
Mike Christie5bb0b552006-04-06 21:26:46 -05001322 tcp_ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
1323 if (tcp_ctask->pad_count) {
1324 tcp_ctask->pad_count = ISCSI_PAD_LEN -
1325 tcp_ctask->pad_count;
1326 debug_scsi("write padding %d bytes\n",
1327 tcp_ctask->pad_count);
1328 tcp_ctask->xmstate |= XMSTATE_W_PAD;
1329 }
1330
1331 if (ctask->unsol_count)
1332 tcp_ctask->xmstate |= XMSTATE_UNS_HDR |
1333 XMSTATE_UNS_INIT;
1334 tcp_ctask->r2t_data_count = ctask->total_length -
Alex Aizman7ba24712005-08-04 19:30:08 -07001335 ctask->imm_count -
1336 ctask->unsol_count;
1337
Mike Christieb6c395e2006-07-24 15:47:15 -05001338 debug_scsi("cmd [itt 0x%x total %d imm %d imm_data %d "
Alex Aizman7ba24712005-08-04 19:30:08 -07001339 "r2t_data %d]\n",
1340 ctask->itt, ctask->total_length, ctask->imm_count,
Mike Christie5bb0b552006-04-06 21:26:46 -05001341 ctask->unsol_count, tcp_ctask->r2t_data_count);
1342 } else
1343 tcp_ctask->xmstate = XMSTATE_R_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001344
Mike Christie6e458cc2006-05-18 20:31:31 -05001345 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001346 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001347}
1348
1349/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001350 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001351 * @conn: iscsi connection
1352 * @mtask: task management task
1353 *
1354 * Notes:
1355 * The function can return -EAGAIN in which case caller must
1356 * call it again later, or recover. '0' return code means successful
1357 * xmit.
1358 *
1359 * Management xmit state machine consists of two states:
1360 * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
1361 * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1362 **/
1363static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001364iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001365{
Mike Christie5bb0b552006-04-06 21:26:46 -05001366 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001367 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001368
1369 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001370 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001371
Mike Christie5bb0b552006-04-06 21:26:46 -05001372 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
1373 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001374 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001375 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christieaf973482005-09-12 21:01:32 -05001376 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001377 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001378 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001379 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1380 (u8*)tcp_mtask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001381 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1382 mtask->data_count);
1383 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001384 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001385 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001386 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001387 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001388 }
1389 }
1390
Mike Christie5bb0b552006-04-06 21:26:46 -05001391 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001392 BUG_ON(!mtask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001393 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001394 /* FIXME: implement.
1395 * Virtual buffer could be spreaded across multiple pages...
1396 */
1397 do {
Mike Christie3219e522006-05-30 00:37:28 -05001398 int rc;
1399
1400 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1401 &mtask->data_count, &tcp_mtask->sent);
1402 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001403 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001404 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001405 }
1406 } while (mtask->data_count);
1407 }
1408
Mike Christie5bb0b552006-04-06 21:26:46 -05001409 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
1410 if (mtask->hdr->itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1411 struct iscsi_session *session = conn->session;
1412
1413 spin_lock_bh(&session->lock);
1414 list_del(&conn->mtask->running);
1415 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1416 sizeof(void*));
1417 spin_unlock_bh(&session->lock);
1418 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001419 return 0;
1420}
1421
1422static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -05001423handle_xmstate_r_hdr(struct iscsi_conn *conn,
1424 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001425{
Mike Christie3219e522006-05-30 00:37:28 -05001426 int rc;
1427
Mike Christie5bb0b552006-04-06 21:26:46 -05001428 tcp_ctask->xmstate &= ~XMSTATE_R_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001429 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001430 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1431 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001432 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, 0);
1433 if (!rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001434 BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE);
Alex Aizman7ba24712005-08-04 19:30:08 -07001435 return 0; /* wait for Data-In */
1436 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001437 tcp_ctask->xmstate |= XMSTATE_R_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001438 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001439}
1440
1441static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -05001442handle_xmstate_w_hdr(struct iscsi_conn *conn,
1443 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001444{
Mike Christie5bb0b552006-04-06 21:26:46 -05001445 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001446 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001447
1448 tcp_ctask->xmstate &= ~XMSTATE_W_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001449 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001450 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1451 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001452 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1453 if (rc)
Mike Christie5bb0b552006-04-06 21:26:46 -05001454 tcp_ctask->xmstate |= XMSTATE_W_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001455 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001456}
1457
1458static inline int
1459handle_xmstate_data_digest(struct iscsi_conn *conn,
1460 struct iscsi_cmd_task *ctask)
1461{
Mike Christie5bb0b552006-04-06 21:26:46 -05001462 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001463 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001464
1465 tcp_ctask->xmstate &= ~XMSTATE_DATA_DIGEST;
1466 debug_tcp("resent data digest 0x%x\n", tcp_ctask->datadigest);
Mike Christie3219e522006-05-30 00:37:28 -05001467 rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1468 &tcp_ctask->datadigest, 0);
1469 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001470 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
Alex Aizman7ba24712005-08-04 19:30:08 -07001471 debug_tcp("resent data digest 0x%x fail!\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001472 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001473 }
Mike Christie3219e522006-05-30 00:37:28 -05001474
1475 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001476}
1477
1478static inline int
1479handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1480{
Mike Christie5bb0b552006-04-06 21:26:46 -05001481 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1482 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001483 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001484
Alex Aizman7ba24712005-08-04 19:30:08 -07001485 BUG_ON(!ctask->imm_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001486 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001487
1488 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001489 iscsi_data_digest_init(tcp_conn, ctask);
1490 tcp_ctask->immdigest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001491 }
1492
1493 for (;;) {
Mike Christie3219e522006-05-30 00:37:28 -05001494 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1495 &ctask->imm_count, &tcp_ctask->sent);
1496 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001497 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001498 if (conn->datadgst_en) {
Herbert Xudc64ddf2006-08-24 18:45:50 +10001499 crypto_hash_final(&tcp_conn->data_tx_hash,
1500 (u8 *)&tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001501 debug_tcp("tx imm sendpage fail 0x%x\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001502 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001503 }
Mike Christie3219e522006-05-30 00:37:28 -05001504 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001505 }
1506 if (conn->datadgst_en)
Herbert Xudc64ddf2006-08-24 18:45:50 +10001507 crypto_hash_update(&tcp_conn->data_tx_hash,
1508 &tcp_ctask->sendbuf.sg,
1509 tcp_ctask->sendbuf.sg.length);
Alex Aizman7ba24712005-08-04 19:30:08 -07001510
1511 if (!ctask->imm_count)
1512 break;
Mike Christie5bb0b552006-04-06 21:26:46 -05001513 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1514 &tcp_ctask->sg[tcp_ctask->sg_count++]);
Alex Aizman7ba24712005-08-04 19:30:08 -07001515 }
1516
Mike Christie5bb0b552006-04-06 21:26:46 -05001517 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
Mike Christie3219e522006-05-30 00:37:28 -05001518 rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1519 &tcp_ctask->immdigest, 1);
1520 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001521 debug_tcp("sending imm digest 0x%x fail!\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001522 tcp_ctask->immdigest);
Mike Christie3219e522006-05-30 00:37:28 -05001523 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001524 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001525 debug_tcp("sending imm digest 0x%x\n", tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001526 }
1527
1528 return 0;
1529}
1530
1531static inline int
1532handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1533{
Mike Christie5bb0b552006-04-06 21:26:46 -05001534 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001535 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001536 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001537
Mike Christie5bb0b552006-04-06 21:26:46 -05001538 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1539 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001540 iscsi_unsolicit_data_init(conn, ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -05001541 dtask = tcp_ctask->dtask;
Mike Christieaf973482005-09-12 21:01:32 -05001542 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001543 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001544 (u8*)dtask->hdrext);
Mike Christie5bb0b552006-04-06 21:26:46 -05001545 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001546 }
Mike Christie3219e522006-05-30 00:37:28 -05001547
1548 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1549 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001550 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1551 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001552 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001553 }
1554
1555 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001556 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001557 return 0;
1558}
1559
1560static inline int
1561handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1562{
Mike Christie5bb0b552006-04-06 21:26:46 -05001563 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1564 struct iscsi_data_task *dtask = tcp_ctask->dtask;
1565 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001566 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001567
1568 BUG_ON(!ctask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001569 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001570
1571 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001572 iscsi_data_digest_init(tcp_conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001573 dtask->digest = 0;
1574 }
1575
1576 for (;;) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001577 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001578
Mike Christie3219e522006-05-30 00:37:28 -05001579 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1580 &ctask->data_count, &tcp_ctask->sent);
1581 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001582 ctask->unsol_count -= tcp_ctask->sent - start;
1583 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001584 /* will continue with this ctask later.. */
1585 if (conn->datadgst_en) {
Herbert Xudc64ddf2006-08-24 18:45:50 +10001586 crypto_hash_final(&tcp_conn->data_tx_hash,
1587 (u8 *)&dtask->digest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001588 debug_tcp("tx uns data fail 0x%x\n",
1589 dtask->digest);
1590 }
Mike Christie3219e522006-05-30 00:37:28 -05001591 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001592 }
1593
Mike Christie5bb0b552006-04-06 21:26:46 -05001594 BUG_ON(tcp_ctask->sent > ctask->total_length);
1595 ctask->unsol_count -= tcp_ctask->sent - start;
Alex Aizman7ba24712005-08-04 19:30:08 -07001596
1597 /*
1598 * XXX:we may run here with un-initial sendbuf.
1599 * so pass it
1600 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001601 if (conn->datadgst_en && tcp_ctask->sent - start > 0)
Herbert Xudc64ddf2006-08-24 18:45:50 +10001602 crypto_hash_update(&tcp_conn->data_tx_hash,
1603 &tcp_ctask->sendbuf.sg,
1604 tcp_ctask->sendbuf.sg.length);
Alex Aizman7ba24712005-08-04 19:30:08 -07001605
1606 if (!ctask->data_count)
1607 break;
Mike Christie5bb0b552006-04-06 21:26:46 -05001608 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1609 &tcp_ctask->sg[tcp_ctask->sg_count++]);
Alex Aizman7ba24712005-08-04 19:30:08 -07001610 }
1611 BUG_ON(ctask->unsol_count < 0);
1612
1613 /*
1614 * Done with the Data-Out. Next, check if we need
1615 * to send another unsolicited Data-Out.
1616 */
1617 if (ctask->unsol_count) {
1618 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001619 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001620 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001621 &dtask->digest, 1);
1622 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001623 debug_tcp("send uns digest 0x%x fail\n",
1624 dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001625 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001626 }
1627 debug_tcp("sending uns digest 0x%x, more uns\n",
1628 dtask->digest);
1629 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001630 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001631 return 1;
1632 }
1633
Mike Christie5bb0b552006-04-06 21:26:46 -05001634 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
Mike Christie3219e522006-05-30 00:37:28 -05001635 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001636 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001637 &dtask->digest, 1);
1638 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001639 debug_tcp("send last uns digest 0x%x fail\n",
1640 dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001641 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001642 }
1643 debug_tcp("sending uns digest 0x%x\n",dtask->digest);
1644 }
1645
1646 return 0;
1647}
1648
1649static inline int
1650handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1651{
1652 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001653 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1654 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1655 struct iscsi_r2t_info *r2t = tcp_ctask->r2t;
Mike Christieffbfe922006-05-18 20:31:36 -05001656 struct iscsi_data_task *dtask = &r2t->dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001657 int left, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001658
Mike Christie5bb0b552006-04-06 21:26:46 -05001659 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1660 tcp_ctask->dtask = dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001661
1662 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001663 iscsi_data_digest_init(tcp_conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001664 dtask->digest = 0;
1665 }
1666solicit_again:
1667 /*
Mike Christieb6c395e2006-07-24 15:47:15 -05001668 * send Data-Out within this R2T sequence.
Alex Aizman7ba24712005-08-04 19:30:08 -07001669 */
1670 if (!r2t->data_count)
1671 goto data_out_done;
1672
Mike Christie3219e522006-05-30 00:37:28 -05001673 rc = iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent);
1674 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001675 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001676 /* will continue with this ctask later.. */
1677 if (conn->datadgst_en) {
Herbert Xudc64ddf2006-08-24 18:45:50 +10001678 crypto_hash_final(&tcp_conn->data_tx_hash,
Alex Aizman7ba24712005-08-04 19:30:08 -07001679 (u8 *)&dtask->digest);
1680 debug_tcp("r2t data send fail 0x%x\n", dtask->digest);
1681 }
Mike Christie3219e522006-05-30 00:37:28 -05001682 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001683 }
1684
1685 BUG_ON(r2t->data_count < 0);
1686 if (conn->datadgst_en)
Herbert Xudc64ddf2006-08-24 18:45:50 +10001687 crypto_hash_update(&tcp_conn->data_tx_hash, &r2t->sendbuf.sg,
1688 r2t->sendbuf.sg.length);
Alex Aizman7ba24712005-08-04 19:30:08 -07001689
1690 if (r2t->data_count) {
1691 BUG_ON(ctask->sc->use_sg == 0);
1692 if (!iscsi_buf_left(&r2t->sendbuf)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001693 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
Alex Aizman7ba24712005-08-04 19:30:08 -07001694 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1695 r2t->sg += 1;
1696 }
1697 goto solicit_again;
1698 }
1699
1700data_out_done:
1701 /*
1702 * Done with this Data-Out. Next, check if we have
1703 * to send another Data-Out for this R2T.
1704 */
1705 BUG_ON(r2t->data_length - r2t->sent < 0);
1706 left = r2t->data_length - r2t->sent;
1707 if (left) {
1708 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001709 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001710 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001711 &dtask->digest, 1);
1712 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001713 debug_tcp("send r2t data digest 0x%x"
1714 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001715 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001716 }
1717 debug_tcp("r2t data send digest 0x%x\n",
1718 dtask->digest);
1719 }
1720 iscsi_solicit_data_cont(conn, ctask, r2t, left);
Mike Christie5bb0b552006-04-06 21:26:46 -05001721 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1722 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001723 return 1;
1724 }
1725
1726 /*
1727 * Done with this R2T. Check if there are more
1728 * outstanding R2Ts ready to be processed.
1729 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001730 BUG_ON(tcp_ctask->r2t_data_count - r2t->data_length < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -07001731 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001732 rc = iscsi_digest_final_send(conn, ctask, &dtask->digestbuf,
1733 &dtask->digest, 1);
1734 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001735 debug_tcp("send last r2t data digest 0x%x"
1736 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001737 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001738 }
1739 debug_tcp("r2t done dout digest 0x%x\n", dtask->digest);
1740 }
1741
Mike Christie5bb0b552006-04-06 21:26:46 -05001742 tcp_ctask->r2t_data_count -= r2t->data_length;
1743 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001744 spin_lock_bh(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05001745 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -07001746 spin_unlock_bh(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05001747 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
1748 tcp_ctask->r2t = r2t;
1749 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1750 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001751 return 1;
1752 }
1753
1754 return 0;
1755}
1756
1757static inline int
1758handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1759{
Mike Christie5bb0b552006-04-06 21:26:46 -05001760 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1761 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1762 struct iscsi_data_task *dtask = tcp_ctask->dtask;
Mike Christieb6c395e2006-07-24 15:47:15 -05001763 int sent = 0, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001764
Mike Christie5bb0b552006-04-06 21:26:46 -05001765 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
Mike Christie6e458cc2006-05-18 20:31:31 -05001766 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
Mike Christie5bb0b552006-04-06 21:26:46 -05001767 tcp_ctask->pad_count);
Mike Christie3219e522006-05-30 00:37:28 -05001768 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1769 &sent);
1770 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001771 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Mike Christie3219e522006-05-30 00:37:28 -05001772 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001773 }
1774
1775 if (conn->datadgst_en) {
Herbert Xudc64ddf2006-08-24 18:45:50 +10001776 crypto_hash_update(&tcp_conn->data_tx_hash,
1777 &tcp_ctask->sendbuf.sg,
1778 tcp_ctask->sendbuf.sg.length);
Alex Aizman7ba24712005-08-04 19:30:08 -07001779 /* imm data? */
1780 if (!dtask) {
Mike Christie3219e522006-05-30 00:37:28 -05001781 rc = iscsi_digest_final_send(conn, ctask,
Mike Christie5bb0b552006-04-06 21:26:46 -05001782 &tcp_ctask->immbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001783 &tcp_ctask->immdigest, 1);
1784 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001785 debug_tcp("send padding digest 0x%x"
Mike Christie5bb0b552006-04-06 21:26:46 -05001786 "fail!\n", tcp_ctask->immdigest);
Mike Christie3219e522006-05-30 00:37:28 -05001787 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001788 }
1789 debug_tcp("done with padding, digest 0x%x\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001790 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001791 } else {
Mike Christie3219e522006-05-30 00:37:28 -05001792 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001793 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001794 &dtask->digest, 1);
1795 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001796 debug_tcp("send padding digest 0x%x"
1797 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001798 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001799 }
1800 debug_tcp("done with padding, digest 0x%x\n",
1801 dtask->digest);
1802 }
1803 }
1804
1805 return 0;
1806}
1807
1808static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001809iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001810{
Mike Christie5bb0b552006-04-06 21:26:46 -05001811 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001812 int rc = 0;
1813
1814 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001815 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001816
1817 /*
1818 * serialize with TMF AbortTask
1819 */
1820 if (ctask->mtask)
1821 return rc;
1822
Mike Christie3219e522006-05-30 00:37:28 -05001823 if (tcp_ctask->xmstate & XMSTATE_R_HDR)
1824 return handle_xmstate_r_hdr(conn, tcp_ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001825
Mike Christie5bb0b552006-04-06 21:26:46 -05001826 if (tcp_ctask->xmstate & XMSTATE_W_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001827 rc = handle_xmstate_w_hdr(conn, ctask);
1828 if (rc)
1829 return rc;
1830 }
1831
1832 /* XXX: for data digest xmit recover */
Mike Christie5bb0b552006-04-06 21:26:46 -05001833 if (tcp_ctask->xmstate & XMSTATE_DATA_DIGEST) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001834 rc = handle_xmstate_data_digest(conn, ctask);
1835 if (rc)
1836 return rc;
1837 }
1838
Mike Christie5bb0b552006-04-06 21:26:46 -05001839 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001840 rc = handle_xmstate_imm_data(conn, ctask);
1841 if (rc)
1842 return rc;
1843 }
1844
Mike Christie5bb0b552006-04-06 21:26:46 -05001845 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001846 BUG_ON(!ctask->unsol_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001847 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001848unsolicit_head_again:
1849 rc = handle_xmstate_uns_hdr(conn, ctask);
1850 if (rc)
1851 return rc;
1852 }
1853
Mike Christie5bb0b552006-04-06 21:26:46 -05001854 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001855 rc = handle_xmstate_uns_data(conn, ctask);
1856 if (rc == 1)
1857 goto unsolicit_head_again;
1858 else if (rc)
1859 return rc;
1860 goto done;
1861 }
1862
Mike Christie5bb0b552006-04-06 21:26:46 -05001863 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001864 struct iscsi_r2t_info *r2t;
1865
Mike Christie5bb0b552006-04-06 21:26:46 -05001866 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1867 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1868 if (!tcp_ctask->r2t)
1869 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
Alex Aizman7ba24712005-08-04 19:30:08 -07001870 sizeof(void*));
1871solicit_head_again:
Mike Christie5bb0b552006-04-06 21:26:46 -05001872 r2t = tcp_ctask->r2t;
Mike Christieaf973482005-09-12 21:01:32 -05001873 if (conn->hdrdgst_en)
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001874 iscsi_hdr_digest(conn, &r2t->headbuf,
Mike Christieffbfe922006-05-18 20:31:36 -05001875 (u8*)r2t->dtask.hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001876 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
1877 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001878 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1879 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001880 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001881 }
1882
1883 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1884 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1885 r2t->sent);
1886 }
1887
Mike Christie5bb0b552006-04-06 21:26:46 -05001888 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001889 rc = handle_xmstate_sol_data(conn, ctask);
1890 if (rc == 1)
1891 goto solicit_head_again;
1892 if (rc)
1893 return rc;
1894 }
1895
1896done:
1897 /*
1898 * Last thing to check is whether we need to send write
1899 * padding. Note that we check for xmstate equality, not just the bit.
1900 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001901 if (tcp_ctask->xmstate == XMSTATE_W_PAD)
Alex Aizman7ba24712005-08-04 19:30:08 -07001902 rc = handle_xmstate_w_pad(conn, ctask);
1903
1904 return rc;
1905}
1906
Mike Christie7b8631b2006-01-13 18:05:50 -06001907static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001908iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001909{
Mike Christie7b8631b2006-01-13 18:05:50 -06001910 struct iscsi_conn *conn;
1911 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001912 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001913
Mike Christie5bb0b552006-04-06 21:26:46 -05001914 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001915 if (!cls_conn)
1916 return NULL;
1917 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001918 /*
1919 * due to strange issues with iser these are not set
1920 * in iscsi_conn_setup
1921 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001922 conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
1923
Mike Christie5bb0b552006-04-06 21:26:46 -05001924 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1925 if (!tcp_conn)
1926 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001927
Mike Christie5bb0b552006-04-06 21:26:46 -05001928 conn->dd_data = tcp_conn;
1929 tcp_conn->iscsi_conn = conn;
1930 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1931 /* initial operational parameters */
1932 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07001933
Mike Christie7b8631b2006-01-13 18:05:50 -06001934 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001935
Mike Christie5bb0b552006-04-06 21:26:46 -05001936tcp_conn_alloc_fail:
1937 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001938 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001939}
1940
1941static void
Mike Christie1c834692006-07-24 15:47:26 -05001942iscsi_tcp_release_conn(struct iscsi_conn *conn)
1943{
1944 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1945
1946 if (!tcp_conn->sock)
1947 return;
1948
1949 sock_hold(tcp_conn->sock->sk);
1950 iscsi_conn_restore_callbacks(tcp_conn);
1951 sock_put(tcp_conn->sock->sk);
1952
1953 sock_release(tcp_conn->sock);
1954 tcp_conn->sock = NULL;
1955 conn->recv_lock = NULL;
1956}
1957
1958static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001959iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001960{
Mike Christie7b8631b2006-01-13 18:05:50 -06001961 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001962 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1963 int digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001964
Mike Christie5bb0b552006-04-06 21:26:46 -05001965 if (conn->hdrdgst_en || conn->datadgst_en)
1966 digest = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001967
Mike Christie1c834692006-07-24 15:47:26 -05001968 iscsi_tcp_release_conn(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001969 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001970
Mike Christie5bb0b552006-04-06 21:26:46 -05001971 /* now free tcp_conn */
1972 if (digest) {
1973 if (tcp_conn->tx_tfm)
Herbert Xudc64ddf2006-08-24 18:45:50 +10001974 crypto_free_hash(tcp_conn->tx_tfm);
Mike Christie5bb0b552006-04-06 21:26:46 -05001975 if (tcp_conn->rx_tfm)
Herbert Xudc64ddf2006-08-24 18:45:50 +10001976 crypto_free_hash(tcp_conn->rx_tfm);
1977 if (tcp_conn->data_tx_hash.tfm)
1978 crypto_free_hash(tcp_conn->data_tx_hash.tfm);
1979 if (tcp_conn->data_rx_hash.tfm)
1980 crypto_free_hash(tcp_conn->data_rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001981 }
1982
Mike Christie5bb0b552006-04-06 21:26:46 -05001983 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001984}
1985
Mike Christie1c834692006-07-24 15:47:26 -05001986static void
1987iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1988{
1989 struct iscsi_conn *conn = cls_conn->dd_data;
1990
1991 iscsi_conn_stop(cls_conn, flag);
1992 iscsi_tcp_release_conn(conn);
1993}
1994
Alex Aizman7ba24712005-08-04 19:30:08 -07001995static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001996iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001997 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001998 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001999{
Mike Christie5bb0b552006-04-06 21:26:46 -05002000 struct iscsi_conn *conn = cls_conn->dd_data;
2001 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002002 struct sock *sk;
2003 struct socket *sock;
2004 int err;
2005
2006 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05002007 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07002008 if (!sock) {
2009 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
2010 return -EEXIST;
2011 }
2012
Mike Christie5bb0b552006-04-06 21:26:46 -05002013 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
2014 if (err)
2015 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07002016
Mike Christie67a61112006-05-30 00:37:20 -05002017 /* bind iSCSI connection and socket */
2018 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07002019
Mike Christie67a61112006-05-30 00:37:20 -05002020 /* setup Socket parameters */
2021 sk = sock->sk;
2022 sk->sk_reuse = 1;
2023 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
2024 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07002025
Mike Christie67a61112006-05-30 00:37:20 -05002026 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07002027
Mike Christie67a61112006-05-30 00:37:20 -05002028 /*
2029 * Intercept TCP callbacks for sendfile like receive
2030 * processing.
2031 */
2032 conn->recv_lock = &sk->sk_callback_lock;
2033 iscsi_conn_set_callbacks(conn);
2034 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
2035 /*
2036 * set receive state machine into initial state
2037 */
2038 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07002039
Alex Aizman7ba24712005-08-04 19:30:08 -07002040 return 0;
2041}
2042
Mike Christie5bb0b552006-04-06 21:26:46 -05002043/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05002044static void
Mike Christie5bb0b552006-04-06 21:26:46 -05002045iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask,
2046 char *data, uint32_t data_size)
Mike Christie30a6c652006-04-06 21:13:39 -05002047{
Mike Christie5bb0b552006-04-06 21:26:46 -05002048 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie30a6c652006-04-06 21:13:39 -05002049
Mike Christie6e458cc2006-05-18 20:31:31 -05002050 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
2051 sizeof(struct iscsi_hdr));
Mike Christie5bb0b552006-04-06 21:26:46 -05002052 tcp_mtask->xmstate = XMSTATE_IMM_HDR;
Mike Christieb6c395e2006-07-24 15:47:15 -05002053 tcp_mtask->sent = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002054
Mike Christie5bb0b552006-04-06 21:26:46 -05002055 if (mtask->data_count)
2056 iscsi_buf_init_iov(&tcp_mtask->sendbuf, (char*)mtask->data,
Alex Aizman7ba24712005-08-04 19:30:08 -07002057 mtask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07002058}
2059
2060static int
2061iscsi_r2tpool_alloc(struct iscsi_session *session)
2062{
2063 int i;
2064 int cmd_i;
2065
2066 /*
2067 * initialize per-task: R2T pool and xmit queue
2068 */
2069 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2070 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05002071 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002072
2073 /*
2074 * pre-allocated x4 as much r2ts to handle race when
2075 * target acks DataOut faster than we data_xmit() queues
2076 * could replenish r2tqueue.
2077 */
2078
2079 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05002080 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
2081 (void***)&tcp_ctask->r2ts,
2082 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002083 goto r2t_alloc_fail;
2084 }
2085
2086 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05002087 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07002088 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05002089 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2090 iscsi_pool_free(&tcp_ctask->r2tpool,
2091 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002092 goto r2t_alloc_fail;
2093 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002094 }
2095
2096 return 0;
2097
2098r2t_alloc_fail:
2099 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002100 struct iscsi_cmd_task *ctask = session->cmds[i];
2101 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2102
Mike Christie5bb0b552006-04-06 21:26:46 -05002103 kfifo_free(tcp_ctask->r2tqueue);
2104 iscsi_pool_free(&tcp_ctask->r2tpool,
2105 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002106 }
2107 return -ENOMEM;
2108}
2109
2110static void
2111iscsi_r2tpool_free(struct iscsi_session *session)
2112{
2113 int i;
2114
2115 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002116 struct iscsi_cmd_task *ctask = session->cmds[i];
2117 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2118
Mike Christie5bb0b552006-04-06 21:26:46 -05002119 kfifo_free(tcp_ctask->r2tqueue);
2120 iscsi_pool_free(&tcp_ctask->r2tpool,
2121 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002122 }
2123}
2124
Alex Aizman7ba24712005-08-04 19:30:08 -07002125static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002126iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002127 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07002128{
Mike Christie7b7232f2006-02-01 21:06:49 -06002129 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002130 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05002131 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002132 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07002133
Alex Aizman7ba24712005-08-04 19:30:08 -07002134 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002135 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002136 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002137 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07002138 if (conn->hdrdgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002139 tcp_conn->hdr_size += sizeof(__u32);
2140 if (!tcp_conn->tx_tfm)
Herbert Xudc64ddf2006-08-24 18:45:50 +10002141 tcp_conn->tx_tfm =
2142 crypto_alloc_hash("crc32c", 0,
2143 CRYPTO_ALG_ASYNC);
2144 if (IS_ERR(tcp_conn->tx_tfm))
2145 return PTR_ERR(tcp_conn->tx_tfm);
Mike Christie5bb0b552006-04-06 21:26:46 -05002146 if (!tcp_conn->rx_tfm)
Herbert Xudc64ddf2006-08-24 18:45:50 +10002147 tcp_conn->rx_tfm =
2148 crypto_alloc_hash("crc32c", 0,
2149 CRYPTO_ALG_ASYNC);
2150 if (IS_ERR(tcp_conn->rx_tfm)) {
2151 crypto_free_hash(tcp_conn->tx_tfm);
2152 return PTR_ERR(tcp_conn->rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002153 }
2154 } else {
Mike Christie5bb0b552006-04-06 21:26:46 -05002155 if (tcp_conn->tx_tfm)
Herbert Xudc64ddf2006-08-24 18:45:50 +10002156 crypto_free_hash(tcp_conn->tx_tfm);
Mike Christie5bb0b552006-04-06 21:26:46 -05002157 if (tcp_conn->rx_tfm)
Herbert Xudc64ddf2006-08-24 18:45:50 +10002158 crypto_free_hash(tcp_conn->rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002159 }
2160 break;
2161 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002162 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002163 if (conn->datadgst_en) {
Herbert Xudc64ddf2006-08-24 18:45:50 +10002164 if (!tcp_conn->data_tx_hash.tfm)
2165 tcp_conn->data_tx_hash.tfm =
2166 crypto_alloc_hash("crc32c", 0,
2167 CRYPTO_ALG_ASYNC);
2168 if (IS_ERR(tcp_conn->data_tx_hash.tfm))
2169 return PTR_ERR(tcp_conn->data_tx_hash.tfm);
2170 if (!tcp_conn->data_rx_hash.tfm)
2171 tcp_conn->data_rx_hash.tfm =
2172 crypto_alloc_hash("crc32c", 0,
2173 CRYPTO_ALG_ASYNC);
2174 if (IS_ERR(tcp_conn->data_rx_hash.tfm)) {
2175 crypto_free_hash(tcp_conn->data_tx_hash.tfm);
2176 return PTR_ERR(tcp_conn->data_rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002177 }
2178 } else {
Herbert Xudc64ddf2006-08-24 18:45:50 +10002179 if (tcp_conn->data_tx_hash.tfm)
2180 crypto_free_hash(tcp_conn->data_tx_hash.tfm);
2181 if (tcp_conn->data_rx_hash.tfm)
2182 crypto_free_hash(tcp_conn->data_rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002183 }
Mike Christie5bb0b552006-04-06 21:26:46 -05002184 tcp_conn->sendpage = conn->datadgst_en ?
2185 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07002186 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002187 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002188 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07002189 if (session->max_r2t == roundup_pow_of_two(value))
2190 break;
2191 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002192 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002193 if (session->max_r2t & (session->max_r2t - 1))
2194 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2195 if (iscsi_r2tpool_alloc(session))
2196 return -ENOMEM;
2197 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002198 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002199 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002200 }
2201
2202 return 0;
2203}
2204
2205static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05002206iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2207 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06002208{
Mike Christie7b7232f2006-02-01 21:06:49 -06002209 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002210 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christiefd7255f2006-04-06 21:13:36 -05002211 struct inet_sock *inet;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002212 struct ipv6_pinfo *np;
2213 struct sock *sk;
2214 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06002215
2216 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002217 case ISCSI_PARAM_CONN_PORT:
2218 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002219 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002220 mutex_unlock(&conn->xmitmutex);
2221 return -EINVAL;
2222 }
2223
Mike Christie5bb0b552006-04-06 21:26:46 -05002224 inet = inet_sk(tcp_conn->sock->sk);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002225 len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
Mike Christiefd7255f2006-04-06 21:13:36 -05002226 mutex_unlock(&conn->xmitmutex);
Mike Christie8d2860b2006-05-02 19:46:47 -05002227 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002228 case ISCSI_PARAM_CONN_ADDRESS:
2229 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002230 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002231 mutex_unlock(&conn->xmitmutex);
2232 return -EINVAL;
2233 }
2234
Mike Christie5bb0b552006-04-06 21:26:46 -05002235 sk = tcp_conn->sock->sk;
Mike Christiefd7255f2006-04-06 21:13:36 -05002236 if (sk->sk_family == PF_INET) {
2237 inet = inet_sk(sk);
2238 len = sprintf(buf, "%u.%u.%u.%u\n",
2239 NIPQUAD(inet->daddr));
2240 } else {
2241 np = inet6_sk(sk);
2242 len = sprintf(buf,
2243 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
2244 NIP6(np->daddr));
2245 }
2246 mutex_unlock(&conn->xmitmutex);
2247 break;
2248 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002249 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05002250 }
2251
2252 return len;
2253}
2254
Alex Aizman7ba24712005-08-04 19:30:08 -07002255static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002256iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002257{
Mike Christie7b7232f2006-02-01 21:06:49 -06002258 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002259 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002260
2261 stats->txdata_octets = conn->txdata_octets;
2262 stats->rxdata_octets = conn->rxdata_octets;
2263 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2264 stats->dataout_pdus = conn->dataout_pdus_cnt;
2265 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2266 stats->datain_pdus = conn->datain_pdus_cnt;
2267 stats->r2t_pdus = conn->r2t_pdus_cnt;
2268 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2269 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2270 stats->custom_length = 3;
2271 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002272 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002273 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002274 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002275 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2276 stats->custom[2].value = conn->eh_abort_cnt;
2277}
2278
Mike Christie5bb0b552006-04-06 21:26:46 -05002279static struct iscsi_cls_session *
2280iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2281 struct scsi_transport_template *scsit,
2282 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002283{
Mike Christie5bb0b552006-04-06 21:26:46 -05002284 struct iscsi_cls_session *cls_session;
2285 struct iscsi_session *session;
2286 uint32_t hn;
2287 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002288
Mike Christie5bb0b552006-04-06 21:26:46 -05002289 cls_session = iscsi_session_setup(iscsit, scsit,
2290 sizeof(struct iscsi_tcp_cmd_task),
2291 sizeof(struct iscsi_tcp_mgmt_task),
2292 initial_cmdsn, &hn);
2293 if (!cls_session)
2294 return NULL;
2295 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002296
Mike Christie5bb0b552006-04-06 21:26:46 -05002297 session = class_to_transport_session(cls_session);
2298 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2299 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2300 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2301
2302 ctask->hdr = &tcp_ctask->hdr;
2303 }
2304
2305 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2306 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2307 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2308
2309 mtask->hdr = &tcp_mtask->hdr;
2310 }
2311
2312 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2313 goto r2tpool_alloc_fail;
2314
2315 return cls_session;
2316
2317r2tpool_alloc_fail:
2318 iscsi_session_teardown(cls_session);
2319 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002320}
2321
Mike Christie5bb0b552006-04-06 21:26:46 -05002322static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2323{
Mike Christie5bb0b552006-04-06 21:26:46 -05002324 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2325 iscsi_session_teardown(cls_session);
2326}
2327
2328static struct scsi_host_template iscsi_sht = {
Mike Christief4246b32006-07-24 15:47:54 -05002329 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05002330 .queuecommand = iscsi_queuecommand,
2331 .change_queue_depth = iscsi_change_queue_depth,
2332 .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
2333 .sg_tablesize = ISCSI_SG_TABLESIZE,
2334 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2335 .eh_abort_handler = iscsi_eh_abort,
2336 .eh_host_reset_handler = iscsi_eh_host_reset,
2337 .use_clustering = DISABLE_CLUSTERING,
2338 .proc_name = "iscsi_tcp",
2339 .this_id = -1,
2340};
2341
Alex Aizman7ba24712005-08-04 19:30:08 -07002342static struct iscsi_transport iscsi_tcp_transport = {
2343 .owner = THIS_MODULE,
2344 .name = "tcp",
2345 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2346 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002347 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2348 ISCSI_MAX_XMIT_DLENGTH |
2349 ISCSI_HDRDGST_EN |
2350 ISCSI_DATADGST_EN |
2351 ISCSI_INITIAL_R2T_EN |
2352 ISCSI_MAX_R2T |
2353 ISCSI_IMM_DATA_EN |
2354 ISCSI_FIRST_BURST |
2355 ISCSI_MAX_BURST |
2356 ISCSI_PDU_INORDER_EN |
2357 ISCSI_DATASEQ_INORDER_EN |
2358 ISCSI_ERL |
2359 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002360 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05002361 ISCSI_EXP_STATSN |
2362 ISCSI_PERSISTENT_PORT |
2363 ISCSI_PERSISTENT_ADDRESS |
2364 ISCSI_TARGET_NAME |
2365 ISCSI_TPGT,
Alex Aizman7ba24712005-08-04 19:30:08 -07002366 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002367 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002368 .max_conn = 1,
2369 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002370 /* session management */
2371 .create_session = iscsi_tcp_session_create,
2372 .destroy_session = iscsi_tcp_session_destroy,
2373 /* connection management */
2374 .create_conn = iscsi_tcp_conn_create,
2375 .bind_conn = iscsi_tcp_conn_bind,
2376 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002377 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002378 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002379 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002380 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05002381 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie5bb0b552006-04-06 21:26:46 -05002382 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002383 .send_pdu = iscsi_conn_send_pdu,
2384 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002385 .init_cmd_task = iscsi_tcp_cmd_init,
2386 .init_mgmt_task = iscsi_tcp_mgmt_init,
2387 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2388 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2389 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2390 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002391 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002392};
2393
2394static int __init
2395iscsi_tcp_init(void)
2396{
Alex Aizman7ba24712005-08-04 19:30:08 -07002397 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002398 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2399 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002400 return -EINVAL;
2401 }
2402 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2403
Mike Christie7b8631b2006-01-13 18:05:50 -06002404 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002405 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002406
Mike Christie7b8631b2006-01-13 18:05:50 -06002407 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002408}
2409
2410static void __exit
2411iscsi_tcp_exit(void)
2412{
2413 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002414}
2415
2416module_init(iscsi_tcp_init);
2417module_exit(iscsi_tcp_exit);