blob: d6927f1a6b65de26eece4e099bb07525c7345d3b [file] [log] [blame]
Alex Aizman7ba24712005-08-04 19:30:08 -07001/*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
Mike Christie5bb0b552006-04-06 21:26:46 -05006 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
Alex Aizman7ba24712005-08-04 19:30:08 -07008 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * See the file COPYING included with this distribution for more details.
21 *
22 * Credits:
23 * Christoph Hellwig
24 * FUJITA Tomonori
25 * Arne Redlich
26 * Zhenyu Wang
27 */
28
29#include <linux/types.h>
30#include <linux/list.h>
31#include <linux/inet.h>
32#include <linux/blkdev.h>
33#include <linux/crypto.h>
34#include <linux/delay.h>
35#include <linux/kfifo.h>
36#include <linux/scatterlist.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010037#include <linux/mutex.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070038#include <net/tcp.h>
39#include <scsi/scsi_cmnd.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070040#include <scsi/scsi_host.h>
41#include <scsi/scsi.h>
42#include <scsi/scsi_transport_iscsi.h>
43
44#include "iscsi_tcp.h"
45
46MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
47 "Alex Aizman <itn780@yahoo.com>");
48MODULE_DESCRIPTION("iSCSI/TCP data-path");
49MODULE_LICENSE("GPL");
Alex Aizman7ba24712005-08-04 19:30:08 -070050/* #define DEBUG_TCP */
Alex Aizman7ba24712005-08-04 19:30:08 -070051#define DEBUG_ASSERT
52
53#ifdef DEBUG_TCP
Mike Christie5bb0b552006-04-06 21:26:46 -050054#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
Alex Aizman7ba24712005-08-04 19:30:08 -070055#else
56#define debug_tcp(fmt...)
57#endif
58
Alex Aizman7ba24712005-08-04 19:30:08 -070059#ifndef DEBUG_ASSERT
60#ifdef BUG_ON
61#undef BUG_ON
62#endif
63#define BUG_ON(expr)
64#endif
65
Alex Aizman7ba24712005-08-04 19:30:08 -070066static unsigned int iscsi_max_lun = 512;
67module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
68
Alex Aizman7ba24712005-08-04 19:30:08 -070069static inline void
Alex Aizman7ba24712005-08-04 19:30:08 -070070iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
71{
Mike Christie7cae5152006-01-13 18:05:47 -060072 ibuf->sg.page = virt_to_page(vbuf);
73 ibuf->sg.offset = offset_in_page(vbuf);
Alex Aizman7ba24712005-08-04 19:30:08 -070074 ibuf->sg.length = size;
75 ibuf->sent = 0;
Mike Christie7cae5152006-01-13 18:05:47 -060076 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070077}
78
79static inline void
80iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
81{
Mike Christie7cae5152006-01-13 18:05:47 -060082 ibuf->sg.page = sg->page;
83 ibuf->sg.offset = sg->offset;
84 ibuf->sg.length = sg->length;
Alex Aizman7ba24712005-08-04 19:30:08 -070085 /*
86 * Fastpath: sg element fits into single page
87 */
Mike Christiea1e80c22006-01-13 18:05:56 -060088 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
Mike Christie7cae5152006-01-13 18:05:47 -060089 ibuf->use_sendmsg = 0;
90 else
91 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070092 ibuf->sent = 0;
93}
94
95static inline int
96iscsi_buf_left(struct iscsi_buf *ibuf)
97{
98 int rc;
99
100 rc = ibuf->sg.length - ibuf->sent;
101 BUG_ON(rc < 0);
102 return rc;
103}
104
105static inline void
Mike Christieaf973482005-09-12 21:01:32 -0500106iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
107 u8* crc)
Alex Aizman7ba24712005-08-04 19:30:08 -0700108{
Mike Christie5bb0b552006-04-06 21:26:46 -0500109 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
110
111 crypto_digest_digest(tcp_conn->tx_tfm, &buf->sg, 1, crc);
Mike Christieaf973482005-09-12 21:01:32 -0500112 buf->sg.length += sizeof(uint32_t);
Alex Aizman7ba24712005-08-04 19:30:08 -0700113}
114
Alex Aizman7ba24712005-08-04 19:30:08 -0700115static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500116iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700117{
Mike Christie5bb0b552006-04-06 21:26:46 -0500118 struct sk_buff *skb = tcp_conn->in.skb;
Alex Aizman7ba24712005-08-04 19:30:08 -0700119
Mike Christie5bb0b552006-04-06 21:26:46 -0500120 tcp_conn->in.zero_copy_hdr = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700121
Mike Christie5bb0b552006-04-06 21:26:46 -0500122 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
123 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700124 /*
125 * Zero-copy PDU Header: using connection context
126 * to store header pointer.
127 */
128 if (skb_shinfo(skb)->frag_list == NULL &&
Mike Christie5bb0b552006-04-06 21:26:46 -0500129 !skb_shinfo(skb)->nr_frags) {
130 tcp_conn->in.hdr = (struct iscsi_hdr *)
131 ((char*)skb->data + tcp_conn->in.offset);
132 tcp_conn->in.zero_copy_hdr = 1;
133 } else {
Alex Aizman7ba24712005-08-04 19:30:08 -0700134 /* ignoring return code since we checked
135 * in.copy before */
Mike Christie5bb0b552006-04-06 21:26:46 -0500136 skb_copy_bits(skb, tcp_conn->in.offset,
137 &tcp_conn->hdr, tcp_conn->hdr_size);
138 tcp_conn->in.hdr = &tcp_conn->hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700139 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500140 tcp_conn->in.offset += tcp_conn->hdr_size;
141 tcp_conn->in.copy -= tcp_conn->hdr_size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700142 } else {
143 int hdr_remains;
144 int copylen;
145
146 /*
147 * PDU header scattered across SKB's,
148 * copying it... This'll happen quite rarely.
149 */
150
Mike Christie5bb0b552006-04-06 21:26:46 -0500151 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
152 tcp_conn->in.hdr_offset = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700153
Mike Christie5bb0b552006-04-06 21:26:46 -0500154 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700155 BUG_ON(hdr_remains <= 0);
156
Mike Christie5bb0b552006-04-06 21:26:46 -0500157 copylen = min(tcp_conn->in.copy, hdr_remains);
158 skb_copy_bits(skb, tcp_conn->in.offset,
159 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
160 copylen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700161
162 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
Mike Christie5bb0b552006-04-06 21:26:46 -0500163 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
164 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700165
Mike Christie5bb0b552006-04-06 21:26:46 -0500166 tcp_conn->in.offset += copylen;
167 tcp_conn->in.copy -= copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700168 if (copylen < hdr_remains) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500169 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
170 tcp_conn->in.hdr_offset += copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700171 return -EAGAIN;
172 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500173 tcp_conn->in.hdr = &tcp_conn->hdr;
174 tcp_conn->discontiguous_hdr_cnt++;
175 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700176 }
177
178 return 0;
179}
180
Mike Christie30a6c652006-04-06 21:13:39 -0500181/*
182 * must be called with session lock
183 */
184static void
Mike Christieb6c395e2006-07-24 15:47:15 -0500185iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700186{
Mike Christie5bb0b552006-04-06 21:26:46 -0500187 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500188 struct iscsi_r2t_info *r2t;
Mike Christie30a6c652006-04-06 21:13:39 -0500189 struct scsi_cmnd *sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700190
Mike Christieb6c395e2006-07-24 15:47:15 -0500191 /* flush ctask's r2t queues */
192 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
193 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
194 sizeof(void*));
195 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
196 }
197
Mike Christie30a6c652006-04-06 21:13:39 -0500198 sc = ctask->sc;
199 if (unlikely(!sc))
Alex Aizman7ba24712005-08-04 19:30:08 -0700200 return;
Mike Christie30a6c652006-04-06 21:13:39 -0500201
Mike Christie5bb0b552006-04-06 21:26:46 -0500202 tcp_ctask->xmstate = XMSTATE_IDLE;
203 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700204}
205
206/**
207 * iscsi_data_rsp - SCSI Data-In Response processing
208 * @conn: iscsi connection
209 * @ctask: scsi command task
210 **/
211static int
212iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
213{
214 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500215 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
216 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
217 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700218 struct iscsi_session *session = conn->session;
219 int datasn = be32_to_cpu(rhdr->datasn);
220
221 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
222 if (rc)
223 return rc;
224 /*
225 * setup Data-In byte counter (gets decremented..)
226 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500227 ctask->data_count = tcp_conn->in.datalen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700228
Mike Christie5bb0b552006-04-06 21:26:46 -0500229 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700230 return 0;
231
232 if (ctask->datasn != datasn)
233 return ISCSI_ERR_DATASN;
234
235 ctask->datasn++;
236
Mike Christie5bb0b552006-04-06 21:26:46 -0500237 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
238 if (tcp_ctask->data_offset + tcp_conn->in.datalen > ctask->total_length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700239 return ISCSI_ERR_DATA_OFFSET;
240
241 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
242 struct scsi_cmnd *sc = ctask->sc;
243
244 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600245 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700246 int res_count = be32_to_cpu(rhdr->residual_count);
247
248 if (res_count > 0 &&
249 res_count <= sc->request_bufflen) {
250 sc->resid = res_count;
251 sc->result = (DID_OK << 16) | rhdr->cmd_status;
252 } else
253 sc->result = (DID_BAD_TARGET << 16) |
254 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600255 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700256 sc->resid = be32_to_cpu(rhdr->residual_count);
257 sc->result = (DID_OK << 16) | rhdr->cmd_status;
258 } else
259 sc->result = (DID_OK << 16) | rhdr->cmd_status;
260 }
261
262 conn->datain_pdus_cnt++;
263 return 0;
264}
265
266/**
267 * iscsi_solicit_data_init - initialize first Data-Out
268 * @conn: iscsi connection
269 * @ctask: scsi command task
270 * @r2t: R2T info
271 *
272 * Notes:
273 * Initialize first Data-Out within this R2T sequence and finds
274 * proper data_offset within this SCSI command.
275 *
276 * This function is called with connection lock taken.
277 **/
278static void
279iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
280 struct iscsi_r2t_info *r2t)
281{
282 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700283 struct scsi_cmnd *sc = ctask->sc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500284 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700285
Mike Christieffbfe922006-05-18 20:31:36 -0500286 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700287 memset(hdr, 0, sizeof(struct iscsi_data));
288 hdr->ttt = r2t->ttt;
289 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
290 r2t->solicit_datasn++;
291 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500292 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
293 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700294 hdr->exp_statsn = r2t->exp_statsn;
295 hdr->offset = cpu_to_be32(r2t->data_offset);
296 if (r2t->data_length > conn->max_xmit_dlength) {
297 hton24(hdr->dlength, conn->max_xmit_dlength);
298 r2t->data_count = conn->max_xmit_dlength;
299 hdr->flags = 0;
300 } else {
301 hton24(hdr->dlength, r2t->data_length);
302 r2t->data_count = r2t->data_length;
303 hdr->flags = ISCSI_FLAG_CMD_FINAL;
304 }
305 conn->dataout_pdus_cnt++;
306
307 r2t->sent = 0;
308
Mike Christie6e458cc2006-05-18 20:31:31 -0500309 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -0500310 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -0700311
Alex Aizman7ba24712005-08-04 19:30:08 -0700312 if (sc->use_sg) {
313 int i, sg_count = 0;
314 struct scatterlist *sg = sc->request_buffer;
315
316 r2t->sg = NULL;
317 for (i = 0; i < sc->use_sg; i++, sg += 1) {
318 /* FIXME: prefetch ? */
319 if (sg_count + sg->length > r2t->data_offset) {
320 int page_offset;
321
322 /* sg page found! */
323
324 /* offset within this page */
325 page_offset = r2t->data_offset - sg_count;
326
327 /* fill in this buffer */
328 iscsi_buf_init_sg(&r2t->sendbuf, sg);
329 r2t->sendbuf.sg.offset += page_offset;
330 r2t->sendbuf.sg.length -= page_offset;
331
332 /* xmit logic will continue with next one */
333 r2t->sg = sg + 1;
334 break;
335 }
336 sg_count += sg->length;
337 }
338 BUG_ON(r2t->sg == NULL);
339 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500340 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -0700341 (char*)sc->request_buffer + r2t->data_offset,
342 r2t->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -0700343}
344
345/**
346 * iscsi_r2t_rsp - iSCSI R2T Response processing
347 * @conn: iscsi connection
348 * @ctask: scsi command task
349 **/
350static int
351iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
352{
353 struct iscsi_r2t_info *r2t;
354 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500355 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
356 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
357 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700358 int r2tsn = be32_to_cpu(rhdr->r2tsn);
359 int rc;
360
Mike Christie98a94162006-08-31 18:09:26 -0400361 if (tcp_conn->in.datalen) {
362 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
363 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700364 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400365 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700366
Mike Christie5bb0b552006-04-06 21:26:46 -0500367 if (tcp_ctask->exp_r2tsn && tcp_ctask->exp_r2tsn != r2tsn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700368 return ISCSI_ERR_R2TSN;
369
370 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
371 if (rc)
372 return rc;
373
374 /* FIXME: use R2TSN to detect missing R2T */
375
376 /* fill-in new R2T associated with the task */
377 spin_lock(&session->lock);
378 if (!ctask->sc || ctask->mtask ||
379 session->state != ISCSI_STATE_LOGGED_IN) {
380 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
381 "recovery...\n", ctask->itt);
382 spin_unlock(&session->lock);
383 return 0;
384 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500385
Mike Christie5bb0b552006-04-06 21:26:46 -0500386 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700387 BUG_ON(!rc);
388
389 r2t->exp_statsn = rhdr->statsn;
390 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400391 if (r2t->data_length == 0) {
392 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
Alex Aizman7ba24712005-08-04 19:30:08 -0700393 spin_unlock(&session->lock);
394 return ISCSI_ERR_DATALEN;
395 }
396
Mike Christie98a94162006-08-31 18:09:26 -0400397 if (r2t->data_length > session->max_burst)
398 debug_scsi("invalid R2T with data len %u and max burst %u."
399 "Attempting to execute request.\n",
400 r2t->data_length, session->max_burst);
401
Alex Aizman7ba24712005-08-04 19:30:08 -0700402 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
403 if (r2t->data_offset + r2t->data_length > ctask->total_length) {
404 spin_unlock(&session->lock);
Mike Christie98a94162006-08-31 18:09:26 -0400405 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
406 "offset %u and total length %d\n", r2t->data_length,
407 r2t->data_offset, ctask->total_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700408 return ISCSI_ERR_DATALEN;
409 }
410
411 r2t->ttt = rhdr->ttt; /* no flip */
412 r2t->solicit_datasn = 0;
413
414 iscsi_solicit_data_init(conn, ctask, r2t);
415
Mike Christie5bb0b552006-04-06 21:26:46 -0500416 tcp_ctask->exp_r2tsn = r2tsn + 1;
417 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
418 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christieb6c395e2006-07-24 15:47:15 -0500419 list_move_tail(&ctask->running, &conn->xmitqueue);
Alex Aizman7ba24712005-08-04 19:30:08 -0700420
Mike Christie55e32992006-01-13 18:05:53 -0600421 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -0700422 conn->r2t_pdus_cnt++;
423 spin_unlock(&session->lock);
424
425 return 0;
426}
427
428static int
Mike Christie5bb0b552006-04-06 21:26:46 -0500429iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700430{
Mike Christie5bb0b552006-04-06 21:26:46 -0500431 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700432 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700433 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500434 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
435 uint32_t cdgst, rdgst = 0, itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700436
Mike Christie5bb0b552006-04-06 21:26:46 -0500437 hdr = tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700438
439 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500440 tcp_conn->in.datalen = ntoh24(hdr->dlength);
441 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700442 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500443 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700444 return ISCSI_ERR_DATALEN;
445 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500446 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700447
448 /* read AHS */
Mike Christie5bb0b552006-04-06 21:26:46 -0500449 ahslen = hdr->hlength << 2;
450 tcp_conn->in.offset += ahslen;
451 tcp_conn->in.copy -= ahslen;
452 if (tcp_conn->in.copy < 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700453 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
Mike Christie5bb0b552006-04-06 21:26:46 -0500454 "%d bytes\n", ahslen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700455 return ISCSI_ERR_AHSLEN;
456 }
457
458 /* calculate read padding */
Mike Christie5bb0b552006-04-06 21:26:46 -0500459 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
460 if (tcp_conn->in.padding) {
461 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
462 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
Alex Aizman7ba24712005-08-04 19:30:08 -0700463 }
464
465 if (conn->hdrdgst_en) {
466 struct scatterlist sg;
467
468 sg_init_one(&sg, (u8 *)hdr,
Mike Christie5bb0b552006-04-06 21:26:46 -0500469 sizeof(struct iscsi_hdr) + ahslen);
470 crypto_digest_digest(tcp_conn->rx_tfm, &sg, 1, (u8 *)&cdgst);
Alex Aizman7ba24712005-08-04 19:30:08 -0700471 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
Mike Christie5bb0b552006-04-06 21:26:46 -0500472 ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600473 if (cdgst != rdgst) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500474 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
475 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
Mike Christie8a47cd32005-11-30 02:27:19 -0600476 return ISCSI_ERR_HDR_DGST;
477 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700478 }
479
Mike Christie5bb0b552006-04-06 21:26:46 -0500480 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700481 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500482 rc = iscsi_verify_itt(conn, hdr, &itt);
483 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
484 tcp_conn->in.datalen = 0; /* force drop */
485 return 0;
486 } else if (rc)
487 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700488
489 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500490 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
491 ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700492
Mike Christie5bb0b552006-04-06 21:26:46 -0500493 switch(opcode) {
494 case ISCSI_OP_SCSI_DATA_IN:
495 tcp_conn->in.ctask = session->cmds[itt];
496 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
Mike Christie275fd7d2006-07-24 15:47:17 -0500497 if (rc)
498 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500499 /* fall through */
500 case ISCSI_OP_SCSI_CMD_RSP:
501 tcp_conn->in.ctask = session->cmds[itt];
502 if (tcp_conn->in.datalen)
503 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700504
Mike Christie5bb0b552006-04-06 21:26:46 -0500505 spin_lock(&session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500506 iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500507 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
508 spin_unlock(&session->lock);
509 break;
510 case ISCSI_OP_R2T:
511 tcp_conn->in.ctask = session->cmds[itt];
512 if (ahslen)
513 rc = ISCSI_ERR_AHSLEN;
514 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
515 DMA_TO_DEVICE)
516 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
517 else
518 rc = ISCSI_ERR_PROTO;
519 break;
520 case ISCSI_OP_LOGIN_RSP:
521 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500522 case ISCSI_OP_REJECT:
523 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500524 /*
525 * It is possible that we could get a PDU with a buffer larger
526 * than 8K, but there are no targets that currently do this.
527 * For now we fail until we find a vendor that needs it
528 */
529 if (DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH <
530 tcp_conn->in.datalen) {
531 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
532 "but conn buffer is only %u (opcode %0x)\n",
533 tcp_conn->in.datalen,
534 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, opcode);
535 rc = ISCSI_ERR_PROTO;
536 break;
537 }
538
Mike Christie5bb0b552006-04-06 21:26:46 -0500539 if (tcp_conn->in.datalen)
540 goto copy_hdr;
541 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500542 case ISCSI_OP_LOGOUT_RSP:
543 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500544 case ISCSI_OP_SCSI_TMFUNC_RSP:
545 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
546 break;
547 default:
548 rc = ISCSI_ERR_BAD_OPCODE;
549 break;
550 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700551
552 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500553
554copy_hdr:
555 /*
556 * if we did zero copy for the header but we will need multiple
557 * skbs to complete the command then we have to copy the header
558 * for later use
559 */
Mike Christie275fd7d2006-07-24 15:47:17 -0500560 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
Mike Christie5bb0b552006-04-06 21:26:46 -0500561 (tcp_conn->in.datalen + tcp_conn->in.padding +
562 (conn->datadgst_en ? 4 : 0))) {
563 debug_tcp("Copying header for later use. in.copy %d in.datalen"
564 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
565 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
566 sizeof(struct iscsi_hdr));
567 tcp_conn->in.hdr = &tcp_conn->hdr;
568 tcp_conn->in.zero_copy_hdr = 0;
569 }
570 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700571}
572
573/**
574 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
Mike Christie5bb0b552006-04-06 21:26:46 -0500575 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700576 * @ctask: scsi command task
577 * @buf: buffer to copy to
578 * @buf_size: size of buffer
579 * @offset: offset within the buffer
580 *
581 * Notes:
582 * The function calls skb_copy_bits() and updates per-connection and
583 * per-cmd byte counters.
584 *
585 * Read counters (in bytes):
586 *
587 * conn->in.offset offset within in progress SKB
588 * conn->in.copy left to copy from in progress SKB
589 * including padding
590 * conn->in.copied copied already from in progress SKB
591 * conn->data_copied copied already from in progress buffer
592 * ctask->sent total bytes sent up to the MidLayer
593 * ctask->data_count left to copy from in progress Data-In
594 * buf_left left to copy from in progress buffer
595 **/
596static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500597iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -0700598 void *buf, int buf_size, int offset)
599{
Mike Christie5bb0b552006-04-06 21:26:46 -0500600 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
601 int buf_left = buf_size - (tcp_conn->data_copied + offset);
602 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700603 int rc;
604
605 size = min(size, ctask->data_count);
606
607 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500608 size, tcp_conn->in.offset, tcp_conn->in.copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700609
610 BUG_ON(size <= 0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500611 BUG_ON(tcp_ctask->sent + size > ctask->total_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700612
Mike Christie5bb0b552006-04-06 21:26:46 -0500613 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
614 (char*)buf + (offset + tcp_conn->data_copied), size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700615 /* must fit into skb->len */
616 BUG_ON(rc);
617
Mike Christie5bb0b552006-04-06 21:26:46 -0500618 tcp_conn->in.offset += size;
619 tcp_conn->in.copy -= size;
620 tcp_conn->in.copied += size;
621 tcp_conn->data_copied += size;
622 tcp_ctask->sent += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700623 ctask->data_count -= size;
624
Mike Christie5bb0b552006-04-06 21:26:46 -0500625 BUG_ON(tcp_conn->in.copy < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700626 BUG_ON(ctask->data_count < 0);
627
Mike Christie5bb0b552006-04-06 21:26:46 -0500628 if (buf_size != (tcp_conn->data_copied + offset)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700629 if (!ctask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500630 BUG_ON(buf_size - tcp_conn->data_copied < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700631 /* done with this PDU */
Mike Christie5bb0b552006-04-06 21:26:46 -0500632 return buf_size - tcp_conn->data_copied;
Alex Aizman7ba24712005-08-04 19:30:08 -0700633 }
634 return -EAGAIN;
635 }
636
637 /* done with this buffer or with both - PDU and buffer */
Mike Christie5bb0b552006-04-06 21:26:46 -0500638 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700639 return 0;
640}
641
642/**
643 * iscsi_tcp_copy - copy skb bits to the destanation buffer
Mike Christie5bb0b552006-04-06 21:26:46 -0500644 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700645 *
646 * Notes:
647 * The function calls skb_copy_bits() and updates per-connection
648 * byte counters.
649 **/
650static inline int
Mike Christiec8dc1e52006-07-24 15:47:39 -0500651iscsi_tcp_copy(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700652{
Mike Christiec8dc1e52006-07-24 15:47:39 -0500653 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500654 int buf_size = tcp_conn->in.datalen;
655 int buf_left = buf_size - tcp_conn->data_copied;
656 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700657 int rc;
658
659 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500660 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700661 BUG_ON(size <= 0);
662
Mike Christie5bb0b552006-04-06 21:26:46 -0500663 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christiec8dc1e52006-07-24 15:47:39 -0500664 (char*)conn->data + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700665 BUG_ON(rc);
666
Mike Christie5bb0b552006-04-06 21:26:46 -0500667 tcp_conn->in.offset += size;
668 tcp_conn->in.copy -= size;
669 tcp_conn->in.copied += size;
670 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700671
Mike Christie5bb0b552006-04-06 21:26:46 -0500672 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700673 return -EAGAIN;
674
675 return 0;
676}
677
678static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -0500679partial_sg_digest_update(struct iscsi_tcp_conn *tcp_conn,
680 struct scatterlist *sg, int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700681{
682 struct scatterlist temp;
683
684 memcpy(&temp, sg, sizeof(struct scatterlist));
685 temp.offset = offset;
686 temp.length = length;
Mike Christie5bb0b552006-04-06 21:26:46 -0500687 crypto_digest_update(tcp_conn->data_rx_tfm, &temp, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700688}
689
Mike Christief6cfba12005-11-29 23:12:57 -0600690static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500691iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600692{
693 struct scatterlist tmp;
694
695 sg_init_one(&tmp, buf, len);
Mike Christie5bb0b552006-04-06 21:26:46 -0500696 crypto_digest_update(tcp_conn->data_rx_tfm, &tmp, 1);
Mike Christief6cfba12005-11-29 23:12:57 -0600697}
698
Alex Aizman7ba24712005-08-04 19:30:08 -0700699static int iscsi_scsi_data_in(struct iscsi_conn *conn)
700{
Mike Christie5bb0b552006-04-06 21:26:46 -0500701 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
702 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
703 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700704 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600705 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700706 int i, offset, rc = 0;
707
708 BUG_ON((void*)ctask != sc->SCp.ptr);
709
710 /*
711 * copying Data-In into the Scsi_Cmnd
712 */
713 if (!sc->use_sg) {
714 i = ctask->data_count;
Mike Christie5bb0b552006-04-06 21:26:46 -0500715 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
716 sc->request_bufflen,
717 tcp_ctask->data_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700718 if (rc == -EAGAIN)
719 return rc;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600720 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500721 iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
722 i);
Alex Aizman7ba24712005-08-04 19:30:08 -0700723 rc = 0;
724 goto done;
725 }
726
Mike Christie5bb0b552006-04-06 21:26:46 -0500727 offset = tcp_ctask->data_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700728 sg = sc->request_buffer;
729
Mike Christie5bb0b552006-04-06 21:26:46 -0500730 if (tcp_ctask->data_offset)
731 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700732 offset -= sg[i].length;
733 /* we've passed through partial sg*/
734 if (offset < 0)
735 offset = 0;
736
Mike Christie5bb0b552006-04-06 21:26:46 -0500737 for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700738 char *dest;
739
740 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500741 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700742 sg[i].length, offset);
743 kunmap_atomic(dest, KM_SOFTIRQ0);
744 if (rc == -EAGAIN)
745 /* continue with the next SKB/PDU */
746 return rc;
747 if (!rc) {
748 if (conn->datadgst_en) {
749 if (!offset)
Mike Christie5bb0b552006-04-06 21:26:46 -0500750 crypto_digest_update(
751 tcp_conn->data_rx_tfm,
752 &sg[i], 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700753 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500754 partial_sg_digest_update(tcp_conn,
755 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700756 sg[i].offset + offset,
757 sg[i].length - offset);
758 }
759 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500760 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700761 }
762
763 if (!ctask->data_count) {
764 if (rc && conn->datadgst_en)
765 /*
766 * data-in is complete, but buffer not...
767 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500768 partial_sg_digest_update(tcp_conn, &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700769 sg[i].offset, sg[i].length-rc);
770 rc = 0;
771 break;
772 }
773
Mike Christie5bb0b552006-04-06 21:26:46 -0500774 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700775 return -EAGAIN;
776 }
777 BUG_ON(ctask->data_count);
778
779done:
780 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500781 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Mike Christieb6c395e2006-07-24 15:47:15 -0500782 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
783 (long)sc, sc->result, ctask->itt,
784 tcp_conn->in.hdr->flags);
Mike Christie5bb0b552006-04-06 21:26:46 -0500785 spin_lock(&conn->session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500786 iscsi_tcp_cleanup_ctask(conn, ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500787 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
788 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700789 }
790
791 return rc;
792}
793
794static int
795iscsi_data_recv(struct iscsi_conn *conn)
796{
Mike Christie5bb0b552006-04-06 21:26:46 -0500797 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
798 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700799
Mike Christie5bb0b552006-04-06 21:26:46 -0500800 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
801 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700802 case ISCSI_OP_SCSI_DATA_IN:
803 rc = iscsi_scsi_data_in(conn);
804 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500805 case ISCSI_OP_SCSI_CMD_RSP:
806 spin_lock(&conn->session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500807 iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500808 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700809 case ISCSI_OP_TEXT_RSP:
810 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500811 case ISCSI_OP_ASYNC_EVENT:
812 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700813 /*
814 * Collect data segment to the connection's data
815 * placeholder
816 */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500817 if (iscsi_tcp_copy(conn)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700818 rc = -EAGAIN;
819 goto exit;
820 }
821
Mike Christiec8dc1e52006-07-24 15:47:39 -0500822 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500823 tcp_conn->in.datalen);
824 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
Mike Christiec8dc1e52006-07-24 15:47:39 -0500825 iscsi_recv_digest_update(tcp_conn, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500826 tcp_conn->in.datalen);
827 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700828 default:
829 BUG_ON(1);
830 }
831exit:
832 return rc;
833}
834
835/**
836 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
837 * @rd_desc: read descriptor
838 * @skb: socket buffer
839 * @offset: offset in skb
840 * @len: skb->len - offset
841 **/
842static int
843iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
844 unsigned int offset, size_t len)
845{
846 int rc;
847 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500848 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700849 int processed;
850 char pad[ISCSI_PAD_LEN];
851 struct scatterlist sg;
852
853 /*
854 * Save current SKB and its offset in the corresponding
855 * connection context.
856 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500857 tcp_conn->in.copy = skb->len - offset;
858 tcp_conn->in.offset = offset;
859 tcp_conn->in.skb = skb;
860 tcp_conn->in.len = tcp_conn->in.copy;
861 BUG_ON(tcp_conn->in.copy <= 0);
862 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700863
864more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500865 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700866 rc = 0;
867
868 if (unlikely(conn->suspend_rx)) {
869 debug_tcp("conn %d Rx suspended!\n", conn->id);
870 return 0;
871 }
872
Mike Christie5bb0b552006-04-06 21:26:46 -0500873 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
874 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
875 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700876 if (rc) {
877 if (rc == -EAGAIN)
878 goto nomore;
879 else {
Mike Christied82967c2006-07-24 15:47:11 -0500880 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700881 return 0;
882 }
883 }
884
885 /*
886 * Verify and process incoming PDU header.
887 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500888 rc = iscsi_tcp_hdr_recv(conn);
889 if (!rc && tcp_conn->in.datalen) {
Mike Christie8a47cd32005-11-30 02:27:19 -0600890 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500891 BUG_ON(!tcp_conn->data_rx_tfm);
892 crypto_digest_init(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -0700893 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500894 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700895 } else if (rc) {
Mike Christie40527af2006-07-24 15:47:45 -0500896 iscsi_conn_failure(conn, rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700897 return 0;
898 }
899 }
900
Mike Christie5bb0b552006-04-06 21:26:46 -0500901 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
Mike Christief6cfba12005-11-29 23:12:57 -0600902 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500903
Alex Aizman7ba24712005-08-04 19:30:08 -0700904 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500905 tcp_conn->in.offset, tcp_conn->in.copy);
906 skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christief6cfba12005-11-29 23:12:57 -0600907 &recv_digest, 4);
Mike Christie5bb0b552006-04-06 21:26:46 -0500908 tcp_conn->in.offset += 4;
909 tcp_conn->in.copy -= 4;
910 if (recv_digest != tcp_conn->in.datadgst) {
Mike Christief6cfba12005-11-29 23:12:57 -0600911 debug_tcp("iscsi_tcp: data digest error!"
912 "0x%x != 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500913 tcp_conn->in.datadgst);
Mike Christief6cfba12005-11-29 23:12:57 -0600914 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
915 return 0;
916 } else {
917 debug_tcp("iscsi_tcp: data digest match!"
918 "0x%x == 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500919 tcp_conn->in.datadgst);
920 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700921 }
922 }
923
Mike Christie5bb0b552006-04-06 21:26:46 -0500924 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
925 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700926
927 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500928 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700929
930 rc = iscsi_data_recv(conn);
931 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500932 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700933 goto again;
Mike Christied82967c2006-07-24 15:47:11 -0500934 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700935 return 0;
936 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500937 tcp_conn->in.copy -= tcp_conn->in.padding;
938 tcp_conn->in.offset += tcp_conn->in.padding;
Mike Christie8a47cd32005-11-30 02:27:19 -0600939 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500940 if (tcp_conn->in.padding) {
941 debug_tcp("padding -> %d\n",
942 tcp_conn->in.padding);
943 memset(pad, 0, tcp_conn->in.padding);
944 sg_init_one(&sg, pad, tcp_conn->in.padding);
945 crypto_digest_update(tcp_conn->data_rx_tfm,
946 &sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700947 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500948 crypto_digest_final(tcp_conn->data_rx_tfm,
949 (u8 *) & tcp_conn->in.datadgst);
950 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
951 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700952 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500953 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700954 }
955
956 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500957 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
958 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700959
Mike Christie5bb0b552006-04-06 21:26:46 -0500960 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700961 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500962 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700963 goto more;
964 }
965
966nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500967 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700968 BUG_ON(processed == 0);
969 return processed;
970
971again:
Mike Christie5bb0b552006-04-06 21:26:46 -0500972 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700973 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
974 processed, (int)len, (int)rd_desc->count);
975 BUG_ON(processed == 0);
976 BUG_ON(processed > len);
977
978 conn->rxdata_octets += processed;
979 return processed;
980}
981
982static void
983iscsi_tcp_data_ready(struct sock *sk, int flag)
984{
985 struct iscsi_conn *conn = sk->sk_user_data;
986 read_descriptor_t rd_desc;
987
988 read_lock(&sk->sk_callback_lock);
989
Mike Christie665b44a2006-05-02 19:46:49 -0500990 /*
991 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
992 * We set count to 1 because we want the network layer to
993 * hand us all the skbs that are available. iscsi_tcp_data_recv
994 * handled pdus that cross buffers or pdus that still need data.
995 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700996 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -0500997 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -0700998 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
999
1000 read_unlock(&sk->sk_callback_lock);
1001}
1002
1003static void
1004iscsi_tcp_state_change(struct sock *sk)
1005{
Mike Christie5bb0b552006-04-06 21:26:46 -05001006 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001007 struct iscsi_conn *conn;
1008 struct iscsi_session *session;
1009 void (*old_state_change)(struct sock *);
1010
1011 read_lock(&sk->sk_callback_lock);
1012
1013 conn = (struct iscsi_conn*)sk->sk_user_data;
1014 session = conn->session;
1015
Mike Christiee6273992005-11-29 23:12:49 -06001016 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1017 sk->sk_state == TCP_CLOSE) &&
1018 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001019 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1020 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1021 }
1022
Mike Christie5bb0b552006-04-06 21:26:46 -05001023 tcp_conn = conn->dd_data;
1024 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001025
1026 read_unlock(&sk->sk_callback_lock);
1027
1028 old_state_change(sk);
1029}
1030
1031/**
1032 * iscsi_write_space - Called when more output buffer space is available
1033 * @sk: socket space is available for
1034 **/
1035static void
1036iscsi_write_space(struct sock *sk)
1037{
1038 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001039 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1040
1041 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001042 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001043 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001044}
1045
1046static void
1047iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1048{
Mike Christie5bb0b552006-04-06 21:26:46 -05001049 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1050 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001051
1052 /* assign new callbacks */
1053 write_lock_bh(&sk->sk_callback_lock);
1054 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001055 tcp_conn->old_data_ready = sk->sk_data_ready;
1056 tcp_conn->old_state_change = sk->sk_state_change;
1057 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001058 sk->sk_data_ready = iscsi_tcp_data_ready;
1059 sk->sk_state_change = iscsi_tcp_state_change;
1060 sk->sk_write_space = iscsi_write_space;
1061 write_unlock_bh(&sk->sk_callback_lock);
1062}
1063
1064static void
Mike Christie1c834692006-07-24 15:47:26 -05001065iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001066{
Mike Christie5bb0b552006-04-06 21:26:46 -05001067 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001068
1069 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1070 write_lock_bh(&sk->sk_callback_lock);
1071 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001072 sk->sk_data_ready = tcp_conn->old_data_ready;
1073 sk->sk_state_change = tcp_conn->old_state_change;
1074 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001075 sk->sk_no_check = 0;
1076 write_unlock_bh(&sk->sk_callback_lock);
1077}
1078
1079/**
1080 * iscsi_send - generic send routine
1081 * @sk: kernel's socket
1082 * @buf: buffer to write from
1083 * @size: actual size to write
1084 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001085 */
1086static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001087iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001088{
Mike Christie5bb0b552006-04-06 21:26:46 -05001089 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1090 struct socket *sk = tcp_conn->sock;
Mike Christie3219e522006-05-30 00:37:28 -05001091 int offset = buf->sg.offset + buf->sent, res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001092
Mike Christie7cae5152006-01-13 18:05:47 -06001093 /*
1094 * if we got use_sg=0 or are sending something we kmallocd
1095 * then we did not have to do kmap (kmap returns page_address)
1096 *
1097 * if we got use_sg > 0, but had to drop down, we do not
1098 * set clustering so this should only happen for that
1099 * slab case.
1100 */
1101 if (buf->use_sendmsg)
Mike Christie3219e522006-05-30 00:37:28 -05001102 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
Mike Christie7cae5152006-01-13 18:05:47 -06001103 else
Mike Christie3219e522006-05-30 00:37:28 -05001104 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1105
1106 if (res >= 0) {
1107 conn->txdata_octets += res;
1108 buf->sent += res;
1109 return res;
1110 }
1111
1112 tcp_conn->sendpage_failures_cnt++;
1113 if (res == -EAGAIN)
1114 res = -ENOBUFS;
1115 else
1116 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1117 return res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001118}
1119
1120/**
1121 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1122 * @conn: iscsi connection
1123 * @buf: buffer to write from
1124 * @datalen: lenght of data to be sent after the header
1125 *
1126 * Notes:
1127 * (Tx, Fast Path)
1128 **/
1129static inline int
1130iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1131{
Alex Aizman7ba24712005-08-04 19:30:08 -07001132 int flags = 0; /* MSG_DONTWAIT; */
1133 int res, size;
1134
1135 size = buf->sg.length - buf->sent;
1136 BUG_ON(buf->sent + size > buf->sg.length);
1137 if (buf->sent + size != buf->sg.length || datalen)
1138 flags |= MSG_MORE;
1139
FUJITA Tomonori56851692006-01-13 18:05:44 -06001140 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001141 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1142 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001143 if (size != res)
1144 return -EAGAIN;
1145 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001146 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001147
1148 return res;
1149}
1150
1151/**
1152 * iscsi_sendpage - send one page of iSCSI Data-Out.
1153 * @conn: iscsi connection
1154 * @buf: buffer to write from
1155 * @count: remaining data
1156 * @sent: number of bytes sent
1157 *
1158 * Notes:
1159 * (Tx, Fast Path)
1160 **/
1161static inline int
1162iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1163 int *count, int *sent)
1164{
Alex Aizman7ba24712005-08-04 19:30:08 -07001165 int flags = 0; /* MSG_DONTWAIT; */
1166 int res, size;
1167
1168 size = buf->sg.length - buf->sent;
1169 BUG_ON(buf->sent + size > buf->sg.length);
1170 if (size > *count)
1171 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001172 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001173 flags |= MSG_MORE;
1174
FUJITA Tomonori56851692006-01-13 18:05:44 -06001175 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001176 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1177 size, buf->sent, *count, *sent, res);
1178 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001179 *count -= res;
1180 *sent += res;
1181 if (size != res)
1182 return -EAGAIN;
1183 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001184 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001185
1186 return res;
1187}
1188
1189static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001190iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
1191 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001192{
Mike Christie5bb0b552006-04-06 21:26:46 -05001193 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1194
1195 BUG_ON(!tcp_conn->data_tx_tfm);
1196 crypto_digest_init(tcp_conn->data_tx_tfm);
1197 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001198}
1199
Arjan van de Ven858119e2006-01-14 13:20:43 -08001200static int
Alex Aizman7ba24712005-08-04 19:30:08 -07001201iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1202 struct iscsi_buf *buf, uint32_t *digest, int final)
1203{
Mike Christie5bb0b552006-04-06 21:26:46 -05001204 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1205 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001206 int rc = 0;
1207 int sent = 0;
1208
1209 if (final)
Mike Christie5bb0b552006-04-06 21:26:46 -05001210 crypto_digest_final(tcp_conn->data_tx_tfm, (u8*)digest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001211
Mike Christie6e458cc2006-05-18 20:31:31 -05001212 iscsi_buf_init_iov(buf, (char*)digest, 4);
Mike Christie5bb0b552006-04-06 21:26:46 -05001213 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001214 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001215 tcp_ctask->datadigest = *digest;
1216 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
Alex Aizman7ba24712005-08-04 19:30:08 -07001217 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001218 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001219 return rc;
1220}
1221
1222/**
1223 * iscsi_solicit_data_cont - initialize next Data-Out
1224 * @conn: iscsi connection
1225 * @ctask: scsi command task
1226 * @r2t: R2T info
1227 * @left: bytes left to transfer
1228 *
1229 * Notes:
1230 * Initialize next Data-Out within this R2T sequence and continue
1231 * to process next Scatter-Gather element(if any) of this SCSI command.
1232 *
1233 * Called under connection lock.
1234 **/
1235static void
1236iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1237 struct iscsi_r2t_info *r2t, int left)
1238{
Mike Christie5bb0b552006-04-06 21:26:46 -05001239 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001240 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001241 struct scsi_cmnd *sc = ctask->sc;
1242 int new_offset;
1243
Mike Christieffbfe922006-05-18 20:31:36 -05001244 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001245 memset(hdr, 0, sizeof(struct iscsi_data));
1246 hdr->ttt = r2t->ttt;
1247 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1248 r2t->solicit_datasn++;
1249 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001250 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1251 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001252 hdr->exp_statsn = r2t->exp_statsn;
1253 new_offset = r2t->data_offset + r2t->sent;
1254 hdr->offset = cpu_to_be32(new_offset);
1255 if (left > conn->max_xmit_dlength) {
1256 hton24(hdr->dlength, conn->max_xmit_dlength);
1257 r2t->data_count = conn->max_xmit_dlength;
1258 } else {
1259 hton24(hdr->dlength, left);
1260 r2t->data_count = left;
1261 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1262 }
1263 conn->dataout_pdus_cnt++;
1264
Mike Christie6e458cc2006-05-18 20:31:31 -05001265 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001266 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001267
Alex Aizman7ba24712005-08-04 19:30:08 -07001268 if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001269 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
Alex Aizman7ba24712005-08-04 19:30:08 -07001270 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1271 r2t->sg += 1;
1272 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001273 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -07001274 (char*)sc->request_buffer + new_offset,
1275 r2t->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001276}
1277
Alex Aizman7ba24712005-08-04 19:30:08 -07001278/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001279 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001280 * @conn: iscsi connection
1281 * @ctask: scsi command task
1282 * @sc: scsi command
1283 **/
1284static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001285iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001286{
Mike Christie5bb0b552006-04-06 21:26:46 -05001287 struct scsi_cmnd *sc = ctask->sc;
1288 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001289
Mike Christie5bb0b552006-04-06 21:26:46 -05001290 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Alex Aizman7ba24712005-08-04 19:30:08 -07001291
Mike Christie5bb0b552006-04-06 21:26:46 -05001292 tcp_ctask->sent = 0;
1293 tcp_ctask->sg_count = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001294
1295 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001296 tcp_ctask->xmstate = XMSTATE_W_HDR;
1297 tcp_ctask->exp_r2tsn = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001298 BUG_ON(ctask->total_length == 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05001299
Alex Aizman7ba24712005-08-04 19:30:08 -07001300 if (sc->use_sg) {
1301 struct scatterlist *sg = sc->request_buffer;
1302
Mike Christie5bb0b552006-04-06 21:26:46 -05001303 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1304 &sg[tcp_ctask->sg_count++]);
1305 tcp_ctask->sg = sg;
1306 tcp_ctask->bad_sg = sg + sc->use_sg;
Alex Aizman7ba24712005-08-04 19:30:08 -07001307 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001308 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1309 sc->request_buffer,
1310 sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001311
Mike Christie5bb0b552006-04-06 21:26:46 -05001312 if (ctask->imm_count)
1313 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001314
Mike Christie5bb0b552006-04-06 21:26:46 -05001315 tcp_ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
1316 if (tcp_ctask->pad_count) {
1317 tcp_ctask->pad_count = ISCSI_PAD_LEN -
1318 tcp_ctask->pad_count;
1319 debug_scsi("write padding %d bytes\n",
1320 tcp_ctask->pad_count);
1321 tcp_ctask->xmstate |= XMSTATE_W_PAD;
1322 }
1323
1324 if (ctask->unsol_count)
1325 tcp_ctask->xmstate |= XMSTATE_UNS_HDR |
1326 XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001327
Mike Christieffd04362006-08-31 18:09:24 -04001328 debug_scsi("cmd [itt 0x%x total %d imm_data %d "
1329 "unsol count %d, unsol offset %d]\n",
Alex Aizman7ba24712005-08-04 19:30:08 -07001330 ctask->itt, ctask->total_length, ctask->imm_count,
Mike Christieffd04362006-08-31 18:09:24 -04001331 ctask->unsol_count, ctask->unsol_offset);
Mike Christie5bb0b552006-04-06 21:26:46 -05001332 } else
1333 tcp_ctask->xmstate = XMSTATE_R_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001334
Mike Christie6e458cc2006-05-18 20:31:31 -05001335 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001336 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001337}
1338
1339/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001340 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001341 * @conn: iscsi connection
1342 * @mtask: task management task
1343 *
1344 * Notes:
1345 * The function can return -EAGAIN in which case caller must
1346 * call it again later, or recover. '0' return code means successful
1347 * xmit.
1348 *
1349 * Management xmit state machine consists of two states:
1350 * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
1351 * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1352 **/
1353static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001354iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001355{
Mike Christie5bb0b552006-04-06 21:26:46 -05001356 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001357 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001358
1359 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001360 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001361
Mike Christie5bb0b552006-04-06 21:26:46 -05001362 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
1363 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001364 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001365 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christieaf973482005-09-12 21:01:32 -05001366 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001367 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001368 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001369 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1370 (u8*)tcp_mtask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001371 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1372 mtask->data_count);
1373 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001374 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001375 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001376 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001377 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001378 }
1379 }
1380
Mike Christie5bb0b552006-04-06 21:26:46 -05001381 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001382 BUG_ON(!mtask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001383 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001384 /* FIXME: implement.
1385 * Virtual buffer could be spreaded across multiple pages...
1386 */
1387 do {
Mike Christie3219e522006-05-30 00:37:28 -05001388 int rc;
1389
1390 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1391 &mtask->data_count, &tcp_mtask->sent);
1392 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001393 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001394 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001395 }
1396 } while (mtask->data_count);
1397 }
1398
Mike Christie5bb0b552006-04-06 21:26:46 -05001399 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
1400 if (mtask->hdr->itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1401 struct iscsi_session *session = conn->session;
1402
1403 spin_lock_bh(&session->lock);
1404 list_del(&conn->mtask->running);
1405 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1406 sizeof(void*));
1407 spin_unlock_bh(&session->lock);
1408 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001409 return 0;
1410}
1411
1412static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -05001413handle_xmstate_r_hdr(struct iscsi_conn *conn,
1414 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001415{
Mike Christie3219e522006-05-30 00:37:28 -05001416 int rc;
1417
Mike Christie5bb0b552006-04-06 21:26:46 -05001418 tcp_ctask->xmstate &= ~XMSTATE_R_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001419 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001420 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1421 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001422 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, 0);
1423 if (!rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001424 BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE);
Alex Aizman7ba24712005-08-04 19:30:08 -07001425 return 0; /* wait for Data-In */
1426 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001427 tcp_ctask->xmstate |= XMSTATE_R_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001428 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001429}
1430
1431static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -05001432handle_xmstate_w_hdr(struct iscsi_conn *conn,
1433 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001434{
Mike Christie5bb0b552006-04-06 21:26:46 -05001435 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001436 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001437
1438 tcp_ctask->xmstate &= ~XMSTATE_W_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001439 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001440 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1441 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001442 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1443 if (rc)
Mike Christie5bb0b552006-04-06 21:26:46 -05001444 tcp_ctask->xmstate |= XMSTATE_W_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001445 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001446}
1447
1448static inline int
1449handle_xmstate_data_digest(struct iscsi_conn *conn,
1450 struct iscsi_cmd_task *ctask)
1451{
Mike Christie5bb0b552006-04-06 21:26:46 -05001452 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001453 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001454
1455 tcp_ctask->xmstate &= ~XMSTATE_DATA_DIGEST;
1456 debug_tcp("resent data digest 0x%x\n", tcp_ctask->datadigest);
Mike Christie3219e522006-05-30 00:37:28 -05001457 rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1458 &tcp_ctask->datadigest, 0);
1459 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001460 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
Alex Aizman7ba24712005-08-04 19:30:08 -07001461 debug_tcp("resent data digest 0x%x fail!\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001462 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001463 }
Mike Christie3219e522006-05-30 00:37:28 -05001464
1465 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001466}
1467
1468static inline int
1469handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1470{
Mike Christie5bb0b552006-04-06 21:26:46 -05001471 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1472 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001473 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001474
Alex Aizman7ba24712005-08-04 19:30:08 -07001475 BUG_ON(!ctask->imm_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001476 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001477
1478 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001479 iscsi_data_digest_init(tcp_conn, ctask);
1480 tcp_ctask->immdigest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001481 }
1482
1483 for (;;) {
Mike Christie3219e522006-05-30 00:37:28 -05001484 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1485 &ctask->imm_count, &tcp_ctask->sent);
1486 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001487 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001488 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001489 crypto_digest_final(tcp_conn->data_tx_tfm,
1490 (u8*)&tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001491 debug_tcp("tx imm sendpage fail 0x%x\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001492 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001493 }
Mike Christie3219e522006-05-30 00:37:28 -05001494 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001495 }
1496 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001497 crypto_digest_update(tcp_conn->data_tx_tfm,
1498 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001499
1500 if (!ctask->imm_count)
1501 break;
Mike Christie5bb0b552006-04-06 21:26:46 -05001502 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1503 &tcp_ctask->sg[tcp_ctask->sg_count++]);
Alex Aizman7ba24712005-08-04 19:30:08 -07001504 }
1505
Mike Christie5bb0b552006-04-06 21:26:46 -05001506 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
Mike Christie3219e522006-05-30 00:37:28 -05001507 rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1508 &tcp_ctask->immdigest, 1);
1509 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001510 debug_tcp("sending imm digest 0x%x fail!\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001511 tcp_ctask->immdigest);
Mike Christie3219e522006-05-30 00:37:28 -05001512 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001513 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001514 debug_tcp("sending imm digest 0x%x\n", tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001515 }
1516
1517 return 0;
1518}
1519
1520static inline int
1521handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1522{
Mike Christie5bb0b552006-04-06 21:26:46 -05001523 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001524 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001525 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001526
Mike Christie5bb0b552006-04-06 21:26:46 -05001527 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1528 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Mike Christieffd04362006-08-31 18:09:24 -04001529 dtask = tcp_ctask->dtask = &tcp_ctask->unsol_dtask;
1530 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
1531 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
1532 sizeof(struct iscsi_hdr));
Mike Christieaf973482005-09-12 21:01:32 -05001533 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001534 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001535 (u8*)dtask->hdrext);
Mike Christie5bb0b552006-04-06 21:26:46 -05001536 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001537 }
Mike Christie3219e522006-05-30 00:37:28 -05001538
1539 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1540 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001541 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1542 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001543 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001544 }
1545
1546 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001547 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001548 return 0;
1549}
1550
1551static inline int
1552handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1553{
Mike Christie5bb0b552006-04-06 21:26:46 -05001554 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1555 struct iscsi_data_task *dtask = tcp_ctask->dtask;
1556 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001557 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001558
1559 BUG_ON(!ctask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001560 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001561
1562 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001563 iscsi_data_digest_init(tcp_conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001564 dtask->digest = 0;
1565 }
1566
1567 for (;;) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001568 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001569
Mike Christie3219e522006-05-30 00:37:28 -05001570 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1571 &ctask->data_count, &tcp_ctask->sent);
1572 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001573 ctask->unsol_count -= tcp_ctask->sent - start;
1574 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001575 /* will continue with this ctask later.. */
1576 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001577 crypto_digest_final(tcp_conn->data_tx_tfm,
Alex Aizman7ba24712005-08-04 19:30:08 -07001578 (u8 *)&dtask->digest);
1579 debug_tcp("tx uns data fail 0x%x\n",
1580 dtask->digest);
1581 }
Mike Christie3219e522006-05-30 00:37:28 -05001582 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001583 }
1584
Mike Christie5bb0b552006-04-06 21:26:46 -05001585 BUG_ON(tcp_ctask->sent > ctask->total_length);
1586 ctask->unsol_count -= tcp_ctask->sent - start;
Alex Aizman7ba24712005-08-04 19:30:08 -07001587
1588 /*
1589 * XXX:we may run here with un-initial sendbuf.
1590 * so pass it
1591 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001592 if (conn->datadgst_en && tcp_ctask->sent - start > 0)
1593 crypto_digest_update(tcp_conn->data_tx_tfm,
1594 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001595
1596 if (!ctask->data_count)
1597 break;
Mike Christie5bb0b552006-04-06 21:26:46 -05001598 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1599 &tcp_ctask->sg[tcp_ctask->sg_count++]);
Alex Aizman7ba24712005-08-04 19:30:08 -07001600 }
1601 BUG_ON(ctask->unsol_count < 0);
1602
1603 /*
1604 * Done with the Data-Out. Next, check if we need
1605 * to send another unsolicited Data-Out.
1606 */
1607 if (ctask->unsol_count) {
1608 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001609 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001610 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001611 &dtask->digest, 1);
1612 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001613 debug_tcp("send uns digest 0x%x fail\n",
1614 dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001615 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001616 }
1617 debug_tcp("sending uns digest 0x%x, more uns\n",
1618 dtask->digest);
1619 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001620 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001621 return 1;
1622 }
1623
Mike Christie5bb0b552006-04-06 21:26:46 -05001624 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
Mike Christie3219e522006-05-30 00:37:28 -05001625 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001626 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001627 &dtask->digest, 1);
1628 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001629 debug_tcp("send last uns digest 0x%x fail\n",
1630 dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001631 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001632 }
1633 debug_tcp("sending uns digest 0x%x\n",dtask->digest);
1634 }
1635
1636 return 0;
1637}
1638
1639static inline int
1640handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1641{
1642 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001643 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1644 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1645 struct iscsi_r2t_info *r2t = tcp_ctask->r2t;
Mike Christieffbfe922006-05-18 20:31:36 -05001646 struct iscsi_data_task *dtask = &r2t->dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001647 int left, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001648
Mike Christie5bb0b552006-04-06 21:26:46 -05001649 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1650 tcp_ctask->dtask = dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001651
1652 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001653 iscsi_data_digest_init(tcp_conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001654 dtask->digest = 0;
1655 }
1656solicit_again:
1657 /*
Mike Christieb6c395e2006-07-24 15:47:15 -05001658 * send Data-Out within this R2T sequence.
Alex Aizman7ba24712005-08-04 19:30:08 -07001659 */
1660 if (!r2t->data_count)
1661 goto data_out_done;
1662
Mike Christie3219e522006-05-30 00:37:28 -05001663 rc = iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent);
1664 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001665 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001666 /* will continue with this ctask later.. */
1667 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001668 crypto_digest_final(tcp_conn->data_tx_tfm,
Alex Aizman7ba24712005-08-04 19:30:08 -07001669 (u8 *)&dtask->digest);
1670 debug_tcp("r2t data send fail 0x%x\n", dtask->digest);
1671 }
Mike Christie3219e522006-05-30 00:37:28 -05001672 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001673 }
1674
1675 BUG_ON(r2t->data_count < 0);
1676 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001677 crypto_digest_update(tcp_conn->data_tx_tfm, &r2t->sendbuf.sg,
1678 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001679
1680 if (r2t->data_count) {
1681 BUG_ON(ctask->sc->use_sg == 0);
1682 if (!iscsi_buf_left(&r2t->sendbuf)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001683 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
Alex Aizman7ba24712005-08-04 19:30:08 -07001684 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1685 r2t->sg += 1;
1686 }
1687 goto solicit_again;
1688 }
1689
1690data_out_done:
1691 /*
1692 * Done with this Data-Out. Next, check if we have
1693 * to send another Data-Out for this R2T.
1694 */
1695 BUG_ON(r2t->data_length - r2t->sent < 0);
1696 left = r2t->data_length - r2t->sent;
1697 if (left) {
1698 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001699 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001700 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001701 &dtask->digest, 1);
1702 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001703 debug_tcp("send r2t data digest 0x%x"
1704 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001705 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001706 }
1707 debug_tcp("r2t data send digest 0x%x\n",
1708 dtask->digest);
1709 }
1710 iscsi_solicit_data_cont(conn, ctask, r2t, left);
Mike Christie5bb0b552006-04-06 21:26:46 -05001711 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1712 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001713 return 1;
1714 }
1715
1716 /*
1717 * Done with this R2T. Check if there are more
1718 * outstanding R2Ts ready to be processed.
1719 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001720 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001721 rc = iscsi_digest_final_send(conn, ctask, &dtask->digestbuf,
1722 &dtask->digest, 1);
1723 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001724 debug_tcp("send last r2t data digest 0x%x"
1725 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001726 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001727 }
1728 debug_tcp("r2t done dout digest 0x%x\n", dtask->digest);
1729 }
1730
Mike Christie5bb0b552006-04-06 21:26:46 -05001731 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001732 spin_lock_bh(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05001733 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -07001734 spin_unlock_bh(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05001735 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
1736 tcp_ctask->r2t = r2t;
1737 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1738 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001739 return 1;
1740 }
1741
1742 return 0;
1743}
1744
1745static inline int
1746handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1747{
Mike Christie5bb0b552006-04-06 21:26:46 -05001748 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1749 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1750 struct iscsi_data_task *dtask = tcp_ctask->dtask;
Mike Christieb6c395e2006-07-24 15:47:15 -05001751 int sent = 0, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001752
Mike Christie5bb0b552006-04-06 21:26:46 -05001753 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
Mike Christie6e458cc2006-05-18 20:31:31 -05001754 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
Mike Christie5bb0b552006-04-06 21:26:46 -05001755 tcp_ctask->pad_count);
Mike Christie3219e522006-05-30 00:37:28 -05001756 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1757 &sent);
1758 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001759 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Mike Christie3219e522006-05-30 00:37:28 -05001760 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001761 }
1762
1763 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001764 crypto_digest_update(tcp_conn->data_tx_tfm,
1765 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001766 /* imm data? */
1767 if (!dtask) {
Mike Christie3219e522006-05-30 00:37:28 -05001768 rc = iscsi_digest_final_send(conn, ctask,
Mike Christie5bb0b552006-04-06 21:26:46 -05001769 &tcp_ctask->immbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001770 &tcp_ctask->immdigest, 1);
1771 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001772 debug_tcp("send padding digest 0x%x"
Mike Christie5bb0b552006-04-06 21:26:46 -05001773 "fail!\n", tcp_ctask->immdigest);
Mike Christie3219e522006-05-30 00:37:28 -05001774 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001775 }
1776 debug_tcp("done with padding, digest 0x%x\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001777 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001778 } else {
Mike Christie3219e522006-05-30 00:37:28 -05001779 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001780 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001781 &dtask->digest, 1);
1782 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001783 debug_tcp("send padding digest 0x%x"
1784 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001785 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001786 }
1787 debug_tcp("done with padding, digest 0x%x\n",
1788 dtask->digest);
1789 }
1790 }
1791
1792 return 0;
1793}
1794
1795static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001796iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001797{
Mike Christie5bb0b552006-04-06 21:26:46 -05001798 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001799 int rc = 0;
1800
1801 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001802 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001803
1804 /*
1805 * serialize with TMF AbortTask
1806 */
1807 if (ctask->mtask)
1808 return rc;
1809
Mike Christie3219e522006-05-30 00:37:28 -05001810 if (tcp_ctask->xmstate & XMSTATE_R_HDR)
1811 return handle_xmstate_r_hdr(conn, tcp_ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001812
Mike Christie5bb0b552006-04-06 21:26:46 -05001813 if (tcp_ctask->xmstate & XMSTATE_W_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001814 rc = handle_xmstate_w_hdr(conn, ctask);
1815 if (rc)
1816 return rc;
1817 }
1818
1819 /* XXX: for data digest xmit recover */
Mike Christie5bb0b552006-04-06 21:26:46 -05001820 if (tcp_ctask->xmstate & XMSTATE_DATA_DIGEST) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001821 rc = handle_xmstate_data_digest(conn, ctask);
1822 if (rc)
1823 return rc;
1824 }
1825
Mike Christie5bb0b552006-04-06 21:26:46 -05001826 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001827 rc = handle_xmstate_imm_data(conn, ctask);
1828 if (rc)
1829 return rc;
1830 }
1831
Mike Christie5bb0b552006-04-06 21:26:46 -05001832 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001833 BUG_ON(!ctask->unsol_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001834 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001835unsolicit_head_again:
1836 rc = handle_xmstate_uns_hdr(conn, ctask);
1837 if (rc)
1838 return rc;
1839 }
1840
Mike Christie5bb0b552006-04-06 21:26:46 -05001841 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001842 rc = handle_xmstate_uns_data(conn, ctask);
1843 if (rc == 1)
1844 goto unsolicit_head_again;
1845 else if (rc)
1846 return rc;
1847 goto done;
1848 }
1849
Mike Christie5bb0b552006-04-06 21:26:46 -05001850 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001851 struct iscsi_r2t_info *r2t;
1852
Mike Christie5bb0b552006-04-06 21:26:46 -05001853 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1854 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1855 if (!tcp_ctask->r2t)
1856 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
Alex Aizman7ba24712005-08-04 19:30:08 -07001857 sizeof(void*));
1858solicit_head_again:
Mike Christie5bb0b552006-04-06 21:26:46 -05001859 r2t = tcp_ctask->r2t;
Mike Christieaf973482005-09-12 21:01:32 -05001860 if (conn->hdrdgst_en)
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001861 iscsi_hdr_digest(conn, &r2t->headbuf,
Mike Christieffbfe922006-05-18 20:31:36 -05001862 (u8*)r2t->dtask.hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001863 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
1864 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001865 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1866 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001867 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001868 }
1869
1870 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1871 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1872 r2t->sent);
1873 }
1874
Mike Christie5bb0b552006-04-06 21:26:46 -05001875 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001876 rc = handle_xmstate_sol_data(conn, ctask);
1877 if (rc == 1)
1878 goto solicit_head_again;
1879 if (rc)
1880 return rc;
1881 }
1882
1883done:
1884 /*
1885 * Last thing to check is whether we need to send write
1886 * padding. Note that we check for xmstate equality, not just the bit.
1887 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001888 if (tcp_ctask->xmstate == XMSTATE_W_PAD)
Alex Aizman7ba24712005-08-04 19:30:08 -07001889 rc = handle_xmstate_w_pad(conn, ctask);
1890
1891 return rc;
1892}
1893
Mike Christie7b8631b2006-01-13 18:05:50 -06001894static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001895iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001896{
Mike Christie7b8631b2006-01-13 18:05:50 -06001897 struct iscsi_conn *conn;
1898 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001899 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001900
Mike Christie5bb0b552006-04-06 21:26:46 -05001901 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001902 if (!cls_conn)
1903 return NULL;
1904 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001905 /*
1906 * due to strange issues with iser these are not set
1907 * in iscsi_conn_setup
1908 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001909 conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
1910
Mike Christie5bb0b552006-04-06 21:26:46 -05001911 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1912 if (!tcp_conn)
1913 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001914
Mike Christie5bb0b552006-04-06 21:26:46 -05001915 conn->dd_data = tcp_conn;
1916 tcp_conn->iscsi_conn = conn;
1917 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1918 /* initial operational parameters */
1919 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07001920
Mike Christie7b8631b2006-01-13 18:05:50 -06001921 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001922
Mike Christie5bb0b552006-04-06 21:26:46 -05001923tcp_conn_alloc_fail:
1924 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001925 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001926}
1927
1928static void
Mike Christie1c834692006-07-24 15:47:26 -05001929iscsi_tcp_release_conn(struct iscsi_conn *conn)
1930{
1931 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1932
1933 if (!tcp_conn->sock)
1934 return;
1935
1936 sock_hold(tcp_conn->sock->sk);
1937 iscsi_conn_restore_callbacks(tcp_conn);
1938 sock_put(tcp_conn->sock->sk);
1939
1940 sock_release(tcp_conn->sock);
1941 tcp_conn->sock = NULL;
1942 conn->recv_lock = NULL;
1943}
1944
1945static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001946iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001947{
Mike Christie7b8631b2006-01-13 18:05:50 -06001948 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001949 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1950 int digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001951
Mike Christie5bb0b552006-04-06 21:26:46 -05001952 if (conn->hdrdgst_en || conn->datadgst_en)
1953 digest = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001954
Mike Christie1c834692006-07-24 15:47:26 -05001955 iscsi_tcp_release_conn(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001956 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001957
Mike Christie5bb0b552006-04-06 21:26:46 -05001958 /* now free tcp_conn */
1959 if (digest) {
1960 if (tcp_conn->tx_tfm)
1961 crypto_free_tfm(tcp_conn->tx_tfm);
1962 if (tcp_conn->rx_tfm)
1963 crypto_free_tfm(tcp_conn->rx_tfm);
1964 if (tcp_conn->data_tx_tfm)
1965 crypto_free_tfm(tcp_conn->data_tx_tfm);
1966 if (tcp_conn->data_rx_tfm)
1967 crypto_free_tfm(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001968 }
1969
Mike Christie5bb0b552006-04-06 21:26:46 -05001970 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001971}
1972
Mike Christie1c834692006-07-24 15:47:26 -05001973static void
1974iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1975{
1976 struct iscsi_conn *conn = cls_conn->dd_data;
1977
1978 iscsi_conn_stop(cls_conn, flag);
1979 iscsi_tcp_release_conn(conn);
1980}
1981
Alex Aizman7ba24712005-08-04 19:30:08 -07001982static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001983iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001984 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001985 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001986{
Mike Christie5bb0b552006-04-06 21:26:46 -05001987 struct iscsi_conn *conn = cls_conn->dd_data;
1988 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001989 struct sock *sk;
1990 struct socket *sock;
1991 int err;
1992
1993 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001994 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001995 if (!sock) {
1996 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1997 return -EEXIST;
1998 }
1999
Mike Christie5bb0b552006-04-06 21:26:46 -05002000 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
2001 if (err)
2002 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07002003
Mike Christie67a61112006-05-30 00:37:20 -05002004 /* bind iSCSI connection and socket */
2005 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07002006
Mike Christie67a61112006-05-30 00:37:20 -05002007 /* setup Socket parameters */
2008 sk = sock->sk;
2009 sk->sk_reuse = 1;
2010 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
2011 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07002012
Mike Christie67a61112006-05-30 00:37:20 -05002013 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07002014
Mike Christie67a61112006-05-30 00:37:20 -05002015 /*
2016 * Intercept TCP callbacks for sendfile like receive
2017 * processing.
2018 */
2019 conn->recv_lock = &sk->sk_callback_lock;
2020 iscsi_conn_set_callbacks(conn);
2021 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
2022 /*
2023 * set receive state machine into initial state
2024 */
2025 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07002026
Alex Aizman7ba24712005-08-04 19:30:08 -07002027 return 0;
2028}
2029
Mike Christie5bb0b552006-04-06 21:26:46 -05002030/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05002031static void
Mike Christie5bb0b552006-04-06 21:26:46 -05002032iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask,
2033 char *data, uint32_t data_size)
Mike Christie30a6c652006-04-06 21:13:39 -05002034{
Mike Christie5bb0b552006-04-06 21:26:46 -05002035 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie30a6c652006-04-06 21:13:39 -05002036
Mike Christie6e458cc2006-05-18 20:31:31 -05002037 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
2038 sizeof(struct iscsi_hdr));
Mike Christie5bb0b552006-04-06 21:26:46 -05002039 tcp_mtask->xmstate = XMSTATE_IMM_HDR;
Mike Christieb6c395e2006-07-24 15:47:15 -05002040 tcp_mtask->sent = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002041
Mike Christie5bb0b552006-04-06 21:26:46 -05002042 if (mtask->data_count)
2043 iscsi_buf_init_iov(&tcp_mtask->sendbuf, (char*)mtask->data,
Alex Aizman7ba24712005-08-04 19:30:08 -07002044 mtask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07002045}
2046
2047static int
2048iscsi_r2tpool_alloc(struct iscsi_session *session)
2049{
2050 int i;
2051 int cmd_i;
2052
2053 /*
2054 * initialize per-task: R2T pool and xmit queue
2055 */
2056 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2057 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05002058 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002059
2060 /*
2061 * pre-allocated x4 as much r2ts to handle race when
2062 * target acks DataOut faster than we data_xmit() queues
2063 * could replenish r2tqueue.
2064 */
2065
2066 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05002067 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
2068 (void***)&tcp_ctask->r2ts,
2069 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002070 goto r2t_alloc_fail;
2071 }
2072
2073 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05002074 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07002075 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05002076 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2077 iscsi_pool_free(&tcp_ctask->r2tpool,
2078 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002079 goto r2t_alloc_fail;
2080 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002081 }
2082
2083 return 0;
2084
2085r2t_alloc_fail:
2086 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002087 struct iscsi_cmd_task *ctask = session->cmds[i];
2088 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2089
Mike Christie5bb0b552006-04-06 21:26:46 -05002090 kfifo_free(tcp_ctask->r2tqueue);
2091 iscsi_pool_free(&tcp_ctask->r2tpool,
2092 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002093 }
2094 return -ENOMEM;
2095}
2096
2097static void
2098iscsi_r2tpool_free(struct iscsi_session *session)
2099{
2100 int i;
2101
2102 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002103 struct iscsi_cmd_task *ctask = session->cmds[i];
2104 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2105
Mike Christie5bb0b552006-04-06 21:26:46 -05002106 kfifo_free(tcp_ctask->r2tqueue);
2107 iscsi_pool_free(&tcp_ctask->r2tpool,
2108 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002109 }
2110}
2111
Alex Aizman7ba24712005-08-04 19:30:08 -07002112static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002113iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002114 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07002115{
Mike Christie7b7232f2006-02-01 21:06:49 -06002116 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002117 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05002118 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002119 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07002120
Alex Aizman7ba24712005-08-04 19:30:08 -07002121 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002122 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002123 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002124 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07002125 if (conn->hdrdgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002126 tcp_conn->hdr_size += sizeof(__u32);
2127 if (!tcp_conn->tx_tfm)
2128 tcp_conn->tx_tfm = crypto_alloc_tfm("crc32c",
2129 0);
2130 if (!tcp_conn->tx_tfm)
Alex Aizman7ba24712005-08-04 19:30:08 -07002131 return -ENOMEM;
Mike Christie5bb0b552006-04-06 21:26:46 -05002132 if (!tcp_conn->rx_tfm)
2133 tcp_conn->rx_tfm = crypto_alloc_tfm("crc32c",
2134 0);
2135 if (!tcp_conn->rx_tfm) {
2136 crypto_free_tfm(tcp_conn->tx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002137 return -ENOMEM;
2138 }
2139 } else {
Mike Christie5bb0b552006-04-06 21:26:46 -05002140 if (tcp_conn->tx_tfm)
2141 crypto_free_tfm(tcp_conn->tx_tfm);
2142 if (tcp_conn->rx_tfm)
2143 crypto_free_tfm(tcp_conn->rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002144 }
2145 break;
2146 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002147 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002148 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002149 if (!tcp_conn->data_tx_tfm)
2150 tcp_conn->data_tx_tfm =
Alex Aizman7ba24712005-08-04 19:30:08 -07002151 crypto_alloc_tfm("crc32c", 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05002152 if (!tcp_conn->data_tx_tfm)
Alex Aizman7ba24712005-08-04 19:30:08 -07002153 return -ENOMEM;
Mike Christie5bb0b552006-04-06 21:26:46 -05002154 if (!tcp_conn->data_rx_tfm)
2155 tcp_conn->data_rx_tfm =
Alex Aizman7ba24712005-08-04 19:30:08 -07002156 crypto_alloc_tfm("crc32c", 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05002157 if (!tcp_conn->data_rx_tfm) {
2158 crypto_free_tfm(tcp_conn->data_tx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002159 return -ENOMEM;
2160 }
2161 } else {
Mike Christie5bb0b552006-04-06 21:26:46 -05002162 if (tcp_conn->data_tx_tfm)
2163 crypto_free_tfm(tcp_conn->data_tx_tfm);
2164 if (tcp_conn->data_rx_tfm)
2165 crypto_free_tfm(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002166 }
Mike Christie5bb0b552006-04-06 21:26:46 -05002167 tcp_conn->sendpage = conn->datadgst_en ?
2168 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07002169 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002170 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002171 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07002172 if (session->max_r2t == roundup_pow_of_two(value))
2173 break;
2174 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002175 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002176 if (session->max_r2t & (session->max_r2t - 1))
2177 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2178 if (iscsi_r2tpool_alloc(session))
2179 return -ENOMEM;
2180 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002181 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002182 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002183 }
2184
2185 return 0;
2186}
2187
2188static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05002189iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2190 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06002191{
Mike Christie7b7232f2006-02-01 21:06:49 -06002192 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002193 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christiefd7255f2006-04-06 21:13:36 -05002194 struct inet_sock *inet;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002195 struct ipv6_pinfo *np;
2196 struct sock *sk;
2197 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06002198
2199 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002200 case ISCSI_PARAM_CONN_PORT:
2201 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002202 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002203 mutex_unlock(&conn->xmitmutex);
2204 return -EINVAL;
2205 }
2206
Mike Christie5bb0b552006-04-06 21:26:46 -05002207 inet = inet_sk(tcp_conn->sock->sk);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002208 len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
Mike Christiefd7255f2006-04-06 21:13:36 -05002209 mutex_unlock(&conn->xmitmutex);
Mike Christie8d2860b2006-05-02 19:46:47 -05002210 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002211 case ISCSI_PARAM_CONN_ADDRESS:
2212 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002213 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002214 mutex_unlock(&conn->xmitmutex);
2215 return -EINVAL;
2216 }
2217
Mike Christie5bb0b552006-04-06 21:26:46 -05002218 sk = tcp_conn->sock->sk;
Mike Christiefd7255f2006-04-06 21:13:36 -05002219 if (sk->sk_family == PF_INET) {
2220 inet = inet_sk(sk);
2221 len = sprintf(buf, "%u.%u.%u.%u\n",
2222 NIPQUAD(inet->daddr));
2223 } else {
2224 np = inet6_sk(sk);
2225 len = sprintf(buf,
2226 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
2227 NIP6(np->daddr));
2228 }
2229 mutex_unlock(&conn->xmitmutex);
2230 break;
2231 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002232 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05002233 }
2234
2235 return len;
2236}
2237
Alex Aizman7ba24712005-08-04 19:30:08 -07002238static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002239iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002240{
Mike Christie7b7232f2006-02-01 21:06:49 -06002241 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002242 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002243
2244 stats->txdata_octets = conn->txdata_octets;
2245 stats->rxdata_octets = conn->rxdata_octets;
2246 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2247 stats->dataout_pdus = conn->dataout_pdus_cnt;
2248 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2249 stats->datain_pdus = conn->datain_pdus_cnt;
2250 stats->r2t_pdus = conn->r2t_pdus_cnt;
2251 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2252 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2253 stats->custom_length = 3;
2254 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002255 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002256 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002257 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002258 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2259 stats->custom[2].value = conn->eh_abort_cnt;
2260}
2261
Mike Christie5bb0b552006-04-06 21:26:46 -05002262static struct iscsi_cls_session *
2263iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2264 struct scsi_transport_template *scsit,
2265 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002266{
Mike Christie5bb0b552006-04-06 21:26:46 -05002267 struct iscsi_cls_session *cls_session;
2268 struct iscsi_session *session;
2269 uint32_t hn;
2270 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002271
Mike Christie5bb0b552006-04-06 21:26:46 -05002272 cls_session = iscsi_session_setup(iscsit, scsit,
2273 sizeof(struct iscsi_tcp_cmd_task),
2274 sizeof(struct iscsi_tcp_mgmt_task),
2275 initial_cmdsn, &hn);
2276 if (!cls_session)
2277 return NULL;
2278 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002279
Mike Christie5bb0b552006-04-06 21:26:46 -05002280 session = class_to_transport_session(cls_session);
2281 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2282 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2283 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2284
2285 ctask->hdr = &tcp_ctask->hdr;
2286 }
2287
2288 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2289 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2290 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2291
2292 mtask->hdr = &tcp_mtask->hdr;
2293 }
2294
2295 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2296 goto r2tpool_alloc_fail;
2297
2298 return cls_session;
2299
2300r2tpool_alloc_fail:
2301 iscsi_session_teardown(cls_session);
2302 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002303}
2304
Mike Christie5bb0b552006-04-06 21:26:46 -05002305static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2306{
Mike Christie5bb0b552006-04-06 21:26:46 -05002307 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2308 iscsi_session_teardown(cls_session);
2309}
2310
2311static struct scsi_host_template iscsi_sht = {
Mike Christief4246b32006-07-24 15:47:54 -05002312 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05002313 .queuecommand = iscsi_queuecommand,
2314 .change_queue_depth = iscsi_change_queue_depth,
2315 .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
2316 .sg_tablesize = ISCSI_SG_TABLESIZE,
2317 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2318 .eh_abort_handler = iscsi_eh_abort,
2319 .eh_host_reset_handler = iscsi_eh_host_reset,
2320 .use_clustering = DISABLE_CLUSTERING,
2321 .proc_name = "iscsi_tcp",
2322 .this_id = -1,
2323};
2324
Alex Aizman7ba24712005-08-04 19:30:08 -07002325static struct iscsi_transport iscsi_tcp_transport = {
2326 .owner = THIS_MODULE,
2327 .name = "tcp",
2328 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2329 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002330 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2331 ISCSI_MAX_XMIT_DLENGTH |
2332 ISCSI_HDRDGST_EN |
2333 ISCSI_DATADGST_EN |
2334 ISCSI_INITIAL_R2T_EN |
2335 ISCSI_MAX_R2T |
2336 ISCSI_IMM_DATA_EN |
2337 ISCSI_FIRST_BURST |
2338 ISCSI_MAX_BURST |
2339 ISCSI_PDU_INORDER_EN |
2340 ISCSI_DATASEQ_INORDER_EN |
2341 ISCSI_ERL |
2342 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002343 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05002344 ISCSI_EXP_STATSN |
2345 ISCSI_PERSISTENT_PORT |
2346 ISCSI_PERSISTENT_ADDRESS |
2347 ISCSI_TARGET_NAME |
2348 ISCSI_TPGT,
Alex Aizman7ba24712005-08-04 19:30:08 -07002349 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002350 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002351 .max_conn = 1,
2352 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002353 /* session management */
2354 .create_session = iscsi_tcp_session_create,
2355 .destroy_session = iscsi_tcp_session_destroy,
2356 /* connection management */
2357 .create_conn = iscsi_tcp_conn_create,
2358 .bind_conn = iscsi_tcp_conn_bind,
2359 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002360 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002361 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002362 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002363 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05002364 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie5bb0b552006-04-06 21:26:46 -05002365 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002366 .send_pdu = iscsi_conn_send_pdu,
2367 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002368 .init_cmd_task = iscsi_tcp_cmd_init,
2369 .init_mgmt_task = iscsi_tcp_mgmt_init,
2370 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2371 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2372 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2373 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002374 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002375};
2376
2377static int __init
2378iscsi_tcp_init(void)
2379{
Alex Aizman7ba24712005-08-04 19:30:08 -07002380 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002381 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2382 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002383 return -EINVAL;
2384 }
2385 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2386
Mike Christie7b8631b2006-01-13 18:05:50 -06002387 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002388 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002389
Mike Christie7b8631b2006-01-13 18:05:50 -06002390 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002391}
2392
2393static void __exit
2394iscsi_tcp_exit(void)
2395{
2396 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002397}
2398
2399module_init(iscsi_tcp_init);
2400module_exit(iscsi_tcp_exit);