blob: 7d784596a1eabdf7629cb259a2212102a704278d [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
Mike Christief70e9c52006-05-30 01:04:51 -050046#define ISCSI_TCP_VERSION "1.0-595"
Mike Christiee0ecae82006-05-18 20:31:43 -050047
Alex Aizman7ba24712005-08-04 19:30:08 -070048MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
49 "Alex Aizman <itn780@yahoo.com>");
50MODULE_DESCRIPTION("iSCSI/TCP data-path");
51MODULE_LICENSE("GPL");
Mike Christiee0ecae82006-05-18 20:31:43 -050052MODULE_VERSION(ISCSI_TCP_VERSION);
Alex Aizman7ba24712005-08-04 19:30:08 -070053/* #define DEBUG_TCP */
Alex Aizman7ba24712005-08-04 19:30:08 -070054#define DEBUG_ASSERT
55
56#ifdef DEBUG_TCP
Mike Christie5bb0b552006-04-06 21:26:46 -050057#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
Alex Aizman7ba24712005-08-04 19:30:08 -070058#else
59#define debug_tcp(fmt...)
60#endif
61
Alex Aizman7ba24712005-08-04 19:30:08 -070062#ifndef DEBUG_ASSERT
63#ifdef BUG_ON
64#undef BUG_ON
65#endif
66#define BUG_ON(expr)
67#endif
68
Alex Aizman7ba24712005-08-04 19:30:08 -070069static unsigned int iscsi_max_lun = 512;
70module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
71
Alex Aizman7ba24712005-08-04 19:30:08 -070072static inline void
Alex Aizman7ba24712005-08-04 19:30:08 -070073iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
74{
Mike Christie7cae5152006-01-13 18:05:47 -060075 ibuf->sg.page = virt_to_page(vbuf);
76 ibuf->sg.offset = offset_in_page(vbuf);
Alex Aizman7ba24712005-08-04 19:30:08 -070077 ibuf->sg.length = size;
78 ibuf->sent = 0;
Mike Christie7cae5152006-01-13 18:05:47 -060079 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070080}
81
82static inline void
83iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
84{
Mike Christie7cae5152006-01-13 18:05:47 -060085 ibuf->sg.page = sg->page;
86 ibuf->sg.offset = sg->offset;
87 ibuf->sg.length = sg->length;
Alex Aizman7ba24712005-08-04 19:30:08 -070088 /*
89 * Fastpath: sg element fits into single page
90 */
Mike Christiea1e80c22006-01-13 18:05:56 -060091 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
Mike Christie7cae5152006-01-13 18:05:47 -060092 ibuf->use_sendmsg = 0;
93 else
94 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070095 ibuf->sent = 0;
96}
97
98static inline int
99iscsi_buf_left(struct iscsi_buf *ibuf)
100{
101 int rc;
102
103 rc = ibuf->sg.length - ibuf->sent;
104 BUG_ON(rc < 0);
105 return rc;
106}
107
108static inline void
Mike Christieaf973482005-09-12 21:01:32 -0500109iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
110 u8* crc)
Alex Aizman7ba24712005-08-04 19:30:08 -0700111{
Mike Christie5bb0b552006-04-06 21:26:46 -0500112 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
113
114 crypto_digest_digest(tcp_conn->tx_tfm, &buf->sg, 1, crc);
Mike Christieaf973482005-09-12 21:01:32 -0500115 buf->sg.length += sizeof(uint32_t);
Alex Aizman7ba24712005-08-04 19:30:08 -0700116}
117
Alex Aizman7ba24712005-08-04 19:30:08 -0700118static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500119iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700120{
Mike Christie5bb0b552006-04-06 21:26:46 -0500121 struct sk_buff *skb = tcp_conn->in.skb;
Alex Aizman7ba24712005-08-04 19:30:08 -0700122
Mike Christie5bb0b552006-04-06 21:26:46 -0500123 tcp_conn->in.zero_copy_hdr = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700124
Mike Christie5bb0b552006-04-06 21:26:46 -0500125 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
126 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700127 /*
128 * Zero-copy PDU Header: using connection context
129 * to store header pointer.
130 */
131 if (skb_shinfo(skb)->frag_list == NULL &&
Mike Christie5bb0b552006-04-06 21:26:46 -0500132 !skb_shinfo(skb)->nr_frags) {
133 tcp_conn->in.hdr = (struct iscsi_hdr *)
134 ((char*)skb->data + tcp_conn->in.offset);
135 tcp_conn->in.zero_copy_hdr = 1;
136 } else {
Alex Aizman7ba24712005-08-04 19:30:08 -0700137 /* ignoring return code since we checked
138 * in.copy before */
Mike Christie5bb0b552006-04-06 21:26:46 -0500139 skb_copy_bits(skb, tcp_conn->in.offset,
140 &tcp_conn->hdr, tcp_conn->hdr_size);
141 tcp_conn->in.hdr = &tcp_conn->hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700142 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500143 tcp_conn->in.offset += tcp_conn->hdr_size;
144 tcp_conn->in.copy -= tcp_conn->hdr_size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700145 } else {
146 int hdr_remains;
147 int copylen;
148
149 /*
150 * PDU header scattered across SKB's,
151 * copying it... This'll happen quite rarely.
152 */
153
Mike Christie5bb0b552006-04-06 21:26:46 -0500154 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
155 tcp_conn->in.hdr_offset = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700156
Mike Christie5bb0b552006-04-06 21:26:46 -0500157 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700158 BUG_ON(hdr_remains <= 0);
159
Mike Christie5bb0b552006-04-06 21:26:46 -0500160 copylen = min(tcp_conn->in.copy, hdr_remains);
161 skb_copy_bits(skb, tcp_conn->in.offset,
162 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
163 copylen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700164
165 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
Mike Christie5bb0b552006-04-06 21:26:46 -0500166 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
167 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700168
Mike Christie5bb0b552006-04-06 21:26:46 -0500169 tcp_conn->in.offset += copylen;
170 tcp_conn->in.copy -= copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700171 if (copylen < hdr_remains) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500172 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
173 tcp_conn->in.hdr_offset += copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700174 return -EAGAIN;
175 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500176 tcp_conn->in.hdr = &tcp_conn->hdr;
177 tcp_conn->discontiguous_hdr_cnt++;
178 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700179 }
180
181 return 0;
182}
183
Mike Christie30a6c652006-04-06 21:13:39 -0500184/*
185 * must be called with session lock
186 */
187static void
Mike Christieb6c395e2006-07-24 15:47:15 -0500188iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700189{
Mike Christie5bb0b552006-04-06 21:26:46 -0500190 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500191 struct iscsi_r2t_info *r2t;
Mike Christie30a6c652006-04-06 21:13:39 -0500192 struct scsi_cmnd *sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700193
Mike Christieb6c395e2006-07-24 15:47:15 -0500194 /* flush ctask's r2t queues */
195 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
196 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
197 sizeof(void*));
198 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
199 }
200
Mike Christie30a6c652006-04-06 21:13:39 -0500201 sc = ctask->sc;
202 if (unlikely(!sc))
Alex Aizman7ba24712005-08-04 19:30:08 -0700203 return;
Mike Christie30a6c652006-04-06 21:13:39 -0500204
Mike Christie5bb0b552006-04-06 21:26:46 -0500205 tcp_ctask->xmstate = XMSTATE_IDLE;
206 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700207}
208
209/**
210 * iscsi_data_rsp - SCSI Data-In Response processing
211 * @conn: iscsi connection
212 * @ctask: scsi command task
213 **/
214static int
215iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
216{
217 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500218 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
219 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
220 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700221 struct iscsi_session *session = conn->session;
222 int datasn = be32_to_cpu(rhdr->datasn);
223
224 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
225 if (rc)
226 return rc;
227 /*
228 * setup Data-In byte counter (gets decremented..)
229 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500230 ctask->data_count = tcp_conn->in.datalen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700231
Mike Christie5bb0b552006-04-06 21:26:46 -0500232 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700233 return 0;
234
235 if (ctask->datasn != datasn)
236 return ISCSI_ERR_DATASN;
237
238 ctask->datasn++;
239
Mike Christie5bb0b552006-04-06 21:26:46 -0500240 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
241 if (tcp_ctask->data_offset + tcp_conn->in.datalen > ctask->total_length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700242 return ISCSI_ERR_DATA_OFFSET;
243
244 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
245 struct scsi_cmnd *sc = ctask->sc;
246
247 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600248 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700249 int res_count = be32_to_cpu(rhdr->residual_count);
250
251 if (res_count > 0 &&
252 res_count <= sc->request_bufflen) {
253 sc->resid = res_count;
254 sc->result = (DID_OK << 16) | rhdr->cmd_status;
255 } else
256 sc->result = (DID_BAD_TARGET << 16) |
257 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600258 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700259 sc->resid = be32_to_cpu(rhdr->residual_count);
260 sc->result = (DID_OK << 16) | rhdr->cmd_status;
261 } else
262 sc->result = (DID_OK << 16) | rhdr->cmd_status;
263 }
264
265 conn->datain_pdus_cnt++;
266 return 0;
267}
268
269/**
270 * iscsi_solicit_data_init - initialize first Data-Out
271 * @conn: iscsi connection
272 * @ctask: scsi command task
273 * @r2t: R2T info
274 *
275 * Notes:
276 * Initialize first Data-Out within this R2T sequence and finds
277 * proper data_offset within this SCSI command.
278 *
279 * This function is called with connection lock taken.
280 **/
281static void
282iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
283 struct iscsi_r2t_info *r2t)
284{
285 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700286 struct scsi_cmnd *sc = ctask->sc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500287 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700288
Mike Christieffbfe922006-05-18 20:31:36 -0500289 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700290 memset(hdr, 0, sizeof(struct iscsi_data));
291 hdr->ttt = r2t->ttt;
292 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
293 r2t->solicit_datasn++;
294 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500295 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
296 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700297 hdr->exp_statsn = r2t->exp_statsn;
298 hdr->offset = cpu_to_be32(r2t->data_offset);
299 if (r2t->data_length > conn->max_xmit_dlength) {
300 hton24(hdr->dlength, conn->max_xmit_dlength);
301 r2t->data_count = conn->max_xmit_dlength;
302 hdr->flags = 0;
303 } else {
304 hton24(hdr->dlength, r2t->data_length);
305 r2t->data_count = r2t->data_length;
306 hdr->flags = ISCSI_FLAG_CMD_FINAL;
307 }
308 conn->dataout_pdus_cnt++;
309
310 r2t->sent = 0;
311
Mike Christie6e458cc2006-05-18 20:31:31 -0500312 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -0500313 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -0700314
Alex Aizman7ba24712005-08-04 19:30:08 -0700315 if (sc->use_sg) {
316 int i, sg_count = 0;
317 struct scatterlist *sg = sc->request_buffer;
318
319 r2t->sg = NULL;
320 for (i = 0; i < sc->use_sg; i++, sg += 1) {
321 /* FIXME: prefetch ? */
322 if (sg_count + sg->length > r2t->data_offset) {
323 int page_offset;
324
325 /* sg page found! */
326
327 /* offset within this page */
328 page_offset = r2t->data_offset - sg_count;
329
330 /* fill in this buffer */
331 iscsi_buf_init_sg(&r2t->sendbuf, sg);
332 r2t->sendbuf.sg.offset += page_offset;
333 r2t->sendbuf.sg.length -= page_offset;
334
335 /* xmit logic will continue with next one */
336 r2t->sg = sg + 1;
337 break;
338 }
339 sg_count += sg->length;
340 }
341 BUG_ON(r2t->sg == NULL);
342 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500343 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -0700344 (char*)sc->request_buffer + r2t->data_offset,
345 r2t->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -0700346}
347
348/**
349 * iscsi_r2t_rsp - iSCSI R2T Response processing
350 * @conn: iscsi connection
351 * @ctask: scsi command task
352 **/
353static int
354iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
355{
356 struct iscsi_r2t_info *r2t;
357 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500358 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
359 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
360 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700361 int r2tsn = be32_to_cpu(rhdr->r2tsn);
362 int rc;
363
Mike Christie5bb0b552006-04-06 21:26:46 -0500364 if (tcp_conn->in.datalen)
Alex Aizman7ba24712005-08-04 19:30:08 -0700365 return ISCSI_ERR_DATALEN;
366
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);
391 if (r2t->data_length == 0 ||
392 r2t->data_length > session->max_burst) {
393 spin_unlock(&session->lock);
394 return ISCSI_ERR_DATALEN;
395 }
396
397 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
398 if (r2t->data_offset + r2t->data_length > ctask->total_length) {
399 spin_unlock(&session->lock);
400 return ISCSI_ERR_DATALEN;
401 }
402
403 r2t->ttt = rhdr->ttt; /* no flip */
404 r2t->solicit_datasn = 0;
405
406 iscsi_solicit_data_init(conn, ctask, r2t);
407
Mike Christie5bb0b552006-04-06 21:26:46 -0500408 tcp_ctask->exp_r2tsn = r2tsn + 1;
409 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
410 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christieb6c395e2006-07-24 15:47:15 -0500411 list_move_tail(&ctask->running, &conn->xmitqueue);
Alex Aizman7ba24712005-08-04 19:30:08 -0700412
Mike Christie55e32992006-01-13 18:05:53 -0600413 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -0700414 conn->r2t_pdus_cnt++;
415 spin_unlock(&session->lock);
416
417 return 0;
418}
419
420static int
Mike Christie5bb0b552006-04-06 21:26:46 -0500421iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700422{
Mike Christie5bb0b552006-04-06 21:26:46 -0500423 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700424 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700425 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500426 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
427 uint32_t cdgst, rdgst = 0, itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700428
Mike Christie5bb0b552006-04-06 21:26:46 -0500429 hdr = tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700430
431 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500432 tcp_conn->in.datalen = ntoh24(hdr->dlength);
433 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700434 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500435 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700436 return ISCSI_ERR_DATALEN;
437 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500438 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700439
440 /* read AHS */
Mike Christie5bb0b552006-04-06 21:26:46 -0500441 ahslen = hdr->hlength << 2;
442 tcp_conn->in.offset += ahslen;
443 tcp_conn->in.copy -= ahslen;
444 if (tcp_conn->in.copy < 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700445 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
Mike Christie5bb0b552006-04-06 21:26:46 -0500446 "%d bytes\n", ahslen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700447 return ISCSI_ERR_AHSLEN;
448 }
449
450 /* calculate read padding */
Mike Christie5bb0b552006-04-06 21:26:46 -0500451 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
452 if (tcp_conn->in.padding) {
453 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
454 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
Alex Aizman7ba24712005-08-04 19:30:08 -0700455 }
456
457 if (conn->hdrdgst_en) {
458 struct scatterlist sg;
459
460 sg_init_one(&sg, (u8 *)hdr,
Mike Christie5bb0b552006-04-06 21:26:46 -0500461 sizeof(struct iscsi_hdr) + ahslen);
462 crypto_digest_digest(tcp_conn->rx_tfm, &sg, 1, (u8 *)&cdgst);
Alex Aizman7ba24712005-08-04 19:30:08 -0700463 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
Mike Christie5bb0b552006-04-06 21:26:46 -0500464 ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600465 if (cdgst != rdgst) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500466 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
467 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
Mike Christie8a47cd32005-11-30 02:27:19 -0600468 return ISCSI_ERR_HDR_DGST;
469 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700470 }
471
Mike Christie5bb0b552006-04-06 21:26:46 -0500472 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700473 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500474 rc = iscsi_verify_itt(conn, hdr, &itt);
475 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
476 tcp_conn->in.datalen = 0; /* force drop */
477 return 0;
478 } else if (rc)
479 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700480
481 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500482 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
483 ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700484
Mike Christie5bb0b552006-04-06 21:26:46 -0500485 switch(opcode) {
486 case ISCSI_OP_SCSI_DATA_IN:
487 tcp_conn->in.ctask = session->cmds[itt];
488 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
Mike Christie275fd7d2006-07-24 15:47:17 -0500489 if (rc)
490 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500491 /* fall through */
492 case ISCSI_OP_SCSI_CMD_RSP:
493 tcp_conn->in.ctask = session->cmds[itt];
494 if (tcp_conn->in.datalen)
495 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700496
Mike Christie5bb0b552006-04-06 21:26:46 -0500497 spin_lock(&session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500498 iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500499 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
500 spin_unlock(&session->lock);
501 break;
502 case ISCSI_OP_R2T:
503 tcp_conn->in.ctask = session->cmds[itt];
504 if (ahslen)
505 rc = ISCSI_ERR_AHSLEN;
506 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
507 DMA_TO_DEVICE)
508 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
509 else
510 rc = ISCSI_ERR_PROTO;
511 break;
512 case ISCSI_OP_LOGIN_RSP:
513 case ISCSI_OP_TEXT_RSP:
514 case ISCSI_OP_LOGOUT_RSP:
515 case ISCSI_OP_NOOP_IN:
516 case ISCSI_OP_REJECT:
517 case ISCSI_OP_ASYNC_EVENT:
518 if (tcp_conn->in.datalen)
519 goto copy_hdr;
520 /* fall through */
521 case ISCSI_OP_SCSI_TMFUNC_RSP:
522 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
523 break;
524 default:
525 rc = ISCSI_ERR_BAD_OPCODE;
526 break;
527 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700528
529 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500530
531copy_hdr:
532 /*
533 * if we did zero copy for the header but we will need multiple
534 * skbs to complete the command then we have to copy the header
535 * for later use
536 */
Mike Christie275fd7d2006-07-24 15:47:17 -0500537 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
Mike Christie5bb0b552006-04-06 21:26:46 -0500538 (tcp_conn->in.datalen + tcp_conn->in.padding +
539 (conn->datadgst_en ? 4 : 0))) {
540 debug_tcp("Copying header for later use. in.copy %d in.datalen"
541 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
542 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
543 sizeof(struct iscsi_hdr));
544 tcp_conn->in.hdr = &tcp_conn->hdr;
545 tcp_conn->in.zero_copy_hdr = 0;
546 }
547 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700548}
549
550/**
551 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
Mike Christie5bb0b552006-04-06 21:26:46 -0500552 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700553 * @ctask: scsi command task
554 * @buf: buffer to copy to
555 * @buf_size: size of buffer
556 * @offset: offset within the buffer
557 *
558 * Notes:
559 * The function calls skb_copy_bits() and updates per-connection and
560 * per-cmd byte counters.
561 *
562 * Read counters (in bytes):
563 *
564 * conn->in.offset offset within in progress SKB
565 * conn->in.copy left to copy from in progress SKB
566 * including padding
567 * conn->in.copied copied already from in progress SKB
568 * conn->data_copied copied already from in progress buffer
569 * ctask->sent total bytes sent up to the MidLayer
570 * ctask->data_count left to copy from in progress Data-In
571 * buf_left left to copy from in progress buffer
572 **/
573static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500574iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -0700575 void *buf, int buf_size, int offset)
576{
Mike Christie5bb0b552006-04-06 21:26:46 -0500577 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
578 int buf_left = buf_size - (tcp_conn->data_copied + offset);
579 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700580 int rc;
581
582 size = min(size, ctask->data_count);
583
584 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500585 size, tcp_conn->in.offset, tcp_conn->in.copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700586
587 BUG_ON(size <= 0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500588 BUG_ON(tcp_ctask->sent + size > ctask->total_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700589
Mike Christie5bb0b552006-04-06 21:26:46 -0500590 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
591 (char*)buf + (offset + tcp_conn->data_copied), size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700592 /* must fit into skb->len */
593 BUG_ON(rc);
594
Mike Christie5bb0b552006-04-06 21:26:46 -0500595 tcp_conn->in.offset += size;
596 tcp_conn->in.copy -= size;
597 tcp_conn->in.copied += size;
598 tcp_conn->data_copied += size;
599 tcp_ctask->sent += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700600 ctask->data_count -= size;
601
Mike Christie5bb0b552006-04-06 21:26:46 -0500602 BUG_ON(tcp_conn->in.copy < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700603 BUG_ON(ctask->data_count < 0);
604
Mike Christie5bb0b552006-04-06 21:26:46 -0500605 if (buf_size != (tcp_conn->data_copied + offset)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700606 if (!ctask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500607 BUG_ON(buf_size - tcp_conn->data_copied < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700608 /* done with this PDU */
Mike Christie5bb0b552006-04-06 21:26:46 -0500609 return buf_size - tcp_conn->data_copied;
Alex Aizman7ba24712005-08-04 19:30:08 -0700610 }
611 return -EAGAIN;
612 }
613
614 /* done with this buffer or with both - PDU and buffer */
Mike Christie5bb0b552006-04-06 21:26:46 -0500615 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700616 return 0;
617}
618
619/**
620 * iscsi_tcp_copy - copy skb bits to the destanation buffer
Mike Christie5bb0b552006-04-06 21:26:46 -0500621 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700622 *
623 * Notes:
624 * The function calls skb_copy_bits() and updates per-connection
625 * byte counters.
626 **/
627static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500628iscsi_tcp_copy(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700629{
Mike Christie5bb0b552006-04-06 21:26:46 -0500630 void *buf = tcp_conn->data;
631 int buf_size = tcp_conn->in.datalen;
632 int buf_left = buf_size - tcp_conn->data_copied;
633 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700634 int rc;
635
636 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500637 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700638 BUG_ON(size <= 0);
639
Mike Christie5bb0b552006-04-06 21:26:46 -0500640 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
641 (char*)buf + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700642 BUG_ON(rc);
643
Mike Christie5bb0b552006-04-06 21:26:46 -0500644 tcp_conn->in.offset += size;
645 tcp_conn->in.copy -= size;
646 tcp_conn->in.copied += size;
647 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700648
Mike Christie5bb0b552006-04-06 21:26:46 -0500649 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700650 return -EAGAIN;
651
652 return 0;
653}
654
655static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -0500656partial_sg_digest_update(struct iscsi_tcp_conn *tcp_conn,
657 struct scatterlist *sg, int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700658{
659 struct scatterlist temp;
660
661 memcpy(&temp, sg, sizeof(struct scatterlist));
662 temp.offset = offset;
663 temp.length = length;
Mike Christie5bb0b552006-04-06 21:26:46 -0500664 crypto_digest_update(tcp_conn->data_rx_tfm, &temp, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700665}
666
Mike Christief6cfba12005-11-29 23:12:57 -0600667static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500668iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600669{
670 struct scatterlist tmp;
671
672 sg_init_one(&tmp, buf, len);
Mike Christie5bb0b552006-04-06 21:26:46 -0500673 crypto_digest_update(tcp_conn->data_rx_tfm, &tmp, 1);
Mike Christief6cfba12005-11-29 23:12:57 -0600674}
675
Alex Aizman7ba24712005-08-04 19:30:08 -0700676static int iscsi_scsi_data_in(struct iscsi_conn *conn)
677{
Mike Christie5bb0b552006-04-06 21:26:46 -0500678 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
679 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
680 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700681 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600682 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700683 int i, offset, rc = 0;
684
685 BUG_ON((void*)ctask != sc->SCp.ptr);
686
687 /*
688 * copying Data-In into the Scsi_Cmnd
689 */
690 if (!sc->use_sg) {
691 i = ctask->data_count;
Mike Christie5bb0b552006-04-06 21:26:46 -0500692 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
693 sc->request_bufflen,
694 tcp_ctask->data_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700695 if (rc == -EAGAIN)
696 return rc;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600697 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500698 iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
699 i);
Alex Aizman7ba24712005-08-04 19:30:08 -0700700 rc = 0;
701 goto done;
702 }
703
Mike Christie5bb0b552006-04-06 21:26:46 -0500704 offset = tcp_ctask->data_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700705 sg = sc->request_buffer;
706
Mike Christie5bb0b552006-04-06 21:26:46 -0500707 if (tcp_ctask->data_offset)
708 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700709 offset -= sg[i].length;
710 /* we've passed through partial sg*/
711 if (offset < 0)
712 offset = 0;
713
Mike Christie5bb0b552006-04-06 21:26:46 -0500714 for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700715 char *dest;
716
717 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500718 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700719 sg[i].length, offset);
720 kunmap_atomic(dest, KM_SOFTIRQ0);
721 if (rc == -EAGAIN)
722 /* continue with the next SKB/PDU */
723 return rc;
724 if (!rc) {
725 if (conn->datadgst_en) {
726 if (!offset)
Mike Christie5bb0b552006-04-06 21:26:46 -0500727 crypto_digest_update(
728 tcp_conn->data_rx_tfm,
729 &sg[i], 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700730 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500731 partial_sg_digest_update(tcp_conn,
732 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700733 sg[i].offset + offset,
734 sg[i].length - offset);
735 }
736 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500737 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700738 }
739
740 if (!ctask->data_count) {
741 if (rc && conn->datadgst_en)
742 /*
743 * data-in is complete, but buffer not...
744 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500745 partial_sg_digest_update(tcp_conn, &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700746 sg[i].offset, sg[i].length-rc);
747 rc = 0;
748 break;
749 }
750
Mike Christie5bb0b552006-04-06 21:26:46 -0500751 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700752 return -EAGAIN;
753 }
754 BUG_ON(ctask->data_count);
755
756done:
757 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500758 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Mike Christieb6c395e2006-07-24 15:47:15 -0500759 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
760 (long)sc, sc->result, ctask->itt,
761 tcp_conn->in.hdr->flags);
Mike Christie5bb0b552006-04-06 21:26:46 -0500762 spin_lock(&conn->session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500763 iscsi_tcp_cleanup_ctask(conn, ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500764 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
765 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700766 }
767
768 return rc;
769}
770
771static int
772iscsi_data_recv(struct iscsi_conn *conn)
773{
Mike Christie5bb0b552006-04-06 21:26:46 -0500774 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
775 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700776
Mike Christie5bb0b552006-04-06 21:26:46 -0500777 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
778 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700779 case ISCSI_OP_SCSI_DATA_IN:
780 rc = iscsi_scsi_data_in(conn);
781 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500782 case ISCSI_OP_SCSI_CMD_RSP:
783 spin_lock(&conn->session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500784 iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500785 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700786 case ISCSI_OP_TEXT_RSP:
787 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500788 case ISCSI_OP_NOOP_IN:
789 case ISCSI_OP_ASYNC_EVENT:
790 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700791 /*
792 * Collect data segment to the connection's data
793 * placeholder
794 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500795 if (iscsi_tcp_copy(tcp_conn)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700796 rc = -EAGAIN;
797 goto exit;
798 }
799
Mike Christie5bb0b552006-04-06 21:26:46 -0500800 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, tcp_conn->data,
801 tcp_conn->in.datalen);
802 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
803 iscsi_recv_digest_update(tcp_conn, tcp_conn->data,
804 tcp_conn->in.datalen);
805 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700806 default:
807 BUG_ON(1);
808 }
809exit:
810 return rc;
811}
812
813/**
814 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
815 * @rd_desc: read descriptor
816 * @skb: socket buffer
817 * @offset: offset in skb
818 * @len: skb->len - offset
819 **/
820static int
821iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
822 unsigned int offset, size_t len)
823{
824 int rc;
825 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500826 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700827 int processed;
828 char pad[ISCSI_PAD_LEN];
829 struct scatterlist sg;
830
831 /*
832 * Save current SKB and its offset in the corresponding
833 * connection context.
834 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500835 tcp_conn->in.copy = skb->len - offset;
836 tcp_conn->in.offset = offset;
837 tcp_conn->in.skb = skb;
838 tcp_conn->in.len = tcp_conn->in.copy;
839 BUG_ON(tcp_conn->in.copy <= 0);
840 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700841
842more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500843 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700844 rc = 0;
845
846 if (unlikely(conn->suspend_rx)) {
847 debug_tcp("conn %d Rx suspended!\n", conn->id);
848 return 0;
849 }
850
Mike Christie5bb0b552006-04-06 21:26:46 -0500851 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
852 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
853 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700854 if (rc) {
855 if (rc == -EAGAIN)
856 goto nomore;
857 else {
Mike Christied82967c2006-07-24 15:47:11 -0500858 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700859 return 0;
860 }
861 }
862
863 /*
864 * Verify and process incoming PDU header.
865 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500866 rc = iscsi_tcp_hdr_recv(conn);
867 if (!rc && tcp_conn->in.datalen) {
Mike Christie8a47cd32005-11-30 02:27:19 -0600868 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500869 BUG_ON(!tcp_conn->data_rx_tfm);
870 crypto_digest_init(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -0700871 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500872 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700873 } else if (rc) {
Mike Christied82967c2006-07-24 15:47:11 -0500874 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700875 return 0;
876 }
877 }
878
Mike Christie5bb0b552006-04-06 21:26:46 -0500879 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
Mike Christief6cfba12005-11-29 23:12:57 -0600880 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500881
Alex Aizman7ba24712005-08-04 19:30:08 -0700882 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500883 tcp_conn->in.offset, tcp_conn->in.copy);
884 skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christief6cfba12005-11-29 23:12:57 -0600885 &recv_digest, 4);
Mike Christie5bb0b552006-04-06 21:26:46 -0500886 tcp_conn->in.offset += 4;
887 tcp_conn->in.copy -= 4;
888 if (recv_digest != tcp_conn->in.datadgst) {
Mike Christief6cfba12005-11-29 23:12:57 -0600889 debug_tcp("iscsi_tcp: data digest error!"
890 "0x%x != 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500891 tcp_conn->in.datadgst);
Mike Christief6cfba12005-11-29 23:12:57 -0600892 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
893 return 0;
894 } else {
895 debug_tcp("iscsi_tcp: data digest match!"
896 "0x%x == 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500897 tcp_conn->in.datadgst);
898 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700899 }
900 }
901
Mike Christie5bb0b552006-04-06 21:26:46 -0500902 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
903 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700904
905 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500906 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700907
908 rc = iscsi_data_recv(conn);
909 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500910 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700911 goto again;
Mike Christied82967c2006-07-24 15:47:11 -0500912 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700913 return 0;
914 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500915 tcp_conn->in.copy -= tcp_conn->in.padding;
916 tcp_conn->in.offset += tcp_conn->in.padding;
Mike Christie8a47cd32005-11-30 02:27:19 -0600917 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500918 if (tcp_conn->in.padding) {
919 debug_tcp("padding -> %d\n",
920 tcp_conn->in.padding);
921 memset(pad, 0, tcp_conn->in.padding);
922 sg_init_one(&sg, pad, tcp_conn->in.padding);
923 crypto_digest_update(tcp_conn->data_rx_tfm,
924 &sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700925 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500926 crypto_digest_final(tcp_conn->data_rx_tfm,
927 (u8 *) & tcp_conn->in.datadgst);
928 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
929 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700930 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500931 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700932 }
933
934 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500935 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
936 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700937
Mike Christie5bb0b552006-04-06 21:26:46 -0500938 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700939 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500940 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700941 goto more;
942 }
943
944nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500945 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700946 BUG_ON(processed == 0);
947 return processed;
948
949again:
Mike Christie5bb0b552006-04-06 21:26:46 -0500950 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700951 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
952 processed, (int)len, (int)rd_desc->count);
953 BUG_ON(processed == 0);
954 BUG_ON(processed > len);
955
956 conn->rxdata_octets += processed;
957 return processed;
958}
959
960static void
961iscsi_tcp_data_ready(struct sock *sk, int flag)
962{
963 struct iscsi_conn *conn = sk->sk_user_data;
964 read_descriptor_t rd_desc;
965
966 read_lock(&sk->sk_callback_lock);
967
Mike Christie665b44a2006-05-02 19:46:49 -0500968 /*
969 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
970 * We set count to 1 because we want the network layer to
971 * hand us all the skbs that are available. iscsi_tcp_data_recv
972 * handled pdus that cross buffers or pdus that still need data.
973 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700974 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -0500975 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -0700976 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
977
978 read_unlock(&sk->sk_callback_lock);
979}
980
981static void
982iscsi_tcp_state_change(struct sock *sk)
983{
Mike Christie5bb0b552006-04-06 21:26:46 -0500984 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -0700985 struct iscsi_conn *conn;
986 struct iscsi_session *session;
987 void (*old_state_change)(struct sock *);
988
989 read_lock(&sk->sk_callback_lock);
990
991 conn = (struct iscsi_conn*)sk->sk_user_data;
992 session = conn->session;
993
Mike Christiee6273992005-11-29 23:12:49 -0600994 if ((sk->sk_state == TCP_CLOSE_WAIT ||
995 sk->sk_state == TCP_CLOSE) &&
996 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700997 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
998 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
999 }
1000
Mike Christie5bb0b552006-04-06 21:26:46 -05001001 tcp_conn = conn->dd_data;
1002 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001003
1004 read_unlock(&sk->sk_callback_lock);
1005
1006 old_state_change(sk);
1007}
1008
1009/**
1010 * iscsi_write_space - Called when more output buffer space is available
1011 * @sk: socket space is available for
1012 **/
1013static void
1014iscsi_write_space(struct sock *sk)
1015{
1016 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001017 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1018
1019 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001020 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001021 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001022}
1023
1024static void
1025iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1026{
Mike Christie5bb0b552006-04-06 21:26:46 -05001027 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1028 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001029
1030 /* assign new callbacks */
1031 write_lock_bh(&sk->sk_callback_lock);
1032 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001033 tcp_conn->old_data_ready = sk->sk_data_ready;
1034 tcp_conn->old_state_change = sk->sk_state_change;
1035 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001036 sk->sk_data_ready = iscsi_tcp_data_ready;
1037 sk->sk_state_change = iscsi_tcp_state_change;
1038 sk->sk_write_space = iscsi_write_space;
1039 write_unlock_bh(&sk->sk_callback_lock);
1040}
1041
1042static void
1043iscsi_conn_restore_callbacks(struct iscsi_conn *conn)
1044{
Mike Christie5bb0b552006-04-06 21:26:46 -05001045 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1046 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001047
1048 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1049 write_lock_bh(&sk->sk_callback_lock);
1050 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001051 sk->sk_data_ready = tcp_conn->old_data_ready;
1052 sk->sk_state_change = tcp_conn->old_state_change;
1053 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001054 sk->sk_no_check = 0;
1055 write_unlock_bh(&sk->sk_callback_lock);
1056}
1057
1058/**
1059 * iscsi_send - generic send routine
1060 * @sk: kernel's socket
1061 * @buf: buffer to write from
1062 * @size: actual size to write
1063 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001064 */
1065static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001066iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001067{
Mike Christie5bb0b552006-04-06 21:26:46 -05001068 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1069 struct socket *sk = tcp_conn->sock;
Mike Christie3219e522006-05-30 00:37:28 -05001070 int offset = buf->sg.offset + buf->sent, res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001071
Mike Christie7cae5152006-01-13 18:05:47 -06001072 /*
1073 * if we got use_sg=0 or are sending something we kmallocd
1074 * then we did not have to do kmap (kmap returns page_address)
1075 *
1076 * if we got use_sg > 0, but had to drop down, we do not
1077 * set clustering so this should only happen for that
1078 * slab case.
1079 */
1080 if (buf->use_sendmsg)
Mike Christie3219e522006-05-30 00:37:28 -05001081 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
Mike Christie7cae5152006-01-13 18:05:47 -06001082 else
Mike Christie3219e522006-05-30 00:37:28 -05001083 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1084
1085 if (res >= 0) {
1086 conn->txdata_octets += res;
1087 buf->sent += res;
1088 return res;
1089 }
1090
1091 tcp_conn->sendpage_failures_cnt++;
1092 if (res == -EAGAIN)
1093 res = -ENOBUFS;
1094 else
1095 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1096 return res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001097}
1098
1099/**
1100 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1101 * @conn: iscsi connection
1102 * @buf: buffer to write from
1103 * @datalen: lenght of data to be sent after the header
1104 *
1105 * Notes:
1106 * (Tx, Fast Path)
1107 **/
1108static inline int
1109iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1110{
Alex Aizman7ba24712005-08-04 19:30:08 -07001111 int flags = 0; /* MSG_DONTWAIT; */
1112 int res, size;
1113
1114 size = buf->sg.length - buf->sent;
1115 BUG_ON(buf->sent + size > buf->sg.length);
1116 if (buf->sent + size != buf->sg.length || datalen)
1117 flags |= MSG_MORE;
1118
FUJITA Tomonori56851692006-01-13 18:05:44 -06001119 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001120 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1121 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001122 if (size != res)
1123 return -EAGAIN;
1124 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001125 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001126
1127 return res;
1128}
1129
1130/**
1131 * iscsi_sendpage - send one page of iSCSI Data-Out.
1132 * @conn: iscsi connection
1133 * @buf: buffer to write from
1134 * @count: remaining data
1135 * @sent: number of bytes sent
1136 *
1137 * Notes:
1138 * (Tx, Fast Path)
1139 **/
1140static inline int
1141iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1142 int *count, int *sent)
1143{
Alex Aizman7ba24712005-08-04 19:30:08 -07001144 int flags = 0; /* MSG_DONTWAIT; */
1145 int res, size;
1146
1147 size = buf->sg.length - buf->sent;
1148 BUG_ON(buf->sent + size > buf->sg.length);
1149 if (size > *count)
1150 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001151 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001152 flags |= MSG_MORE;
1153
FUJITA Tomonori56851692006-01-13 18:05:44 -06001154 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001155 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1156 size, buf->sent, *count, *sent, res);
1157 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001158 *count -= res;
1159 *sent += res;
1160 if (size != res)
1161 return -EAGAIN;
1162 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001163 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001164
1165 return res;
1166}
1167
1168static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001169iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
1170 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001171{
Mike Christie5bb0b552006-04-06 21:26:46 -05001172 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1173
1174 BUG_ON(!tcp_conn->data_tx_tfm);
1175 crypto_digest_init(tcp_conn->data_tx_tfm);
1176 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001177}
1178
Arjan van de Ven858119e2006-01-14 13:20:43 -08001179static int
Alex Aizman7ba24712005-08-04 19:30:08 -07001180iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1181 struct iscsi_buf *buf, uint32_t *digest, int final)
1182{
Mike Christie5bb0b552006-04-06 21:26:46 -05001183 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1184 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001185 int rc = 0;
1186 int sent = 0;
1187
1188 if (final)
Mike Christie5bb0b552006-04-06 21:26:46 -05001189 crypto_digest_final(tcp_conn->data_tx_tfm, (u8*)digest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001190
Mike Christie6e458cc2006-05-18 20:31:31 -05001191 iscsi_buf_init_iov(buf, (char*)digest, 4);
Mike Christie5bb0b552006-04-06 21:26:46 -05001192 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001193 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001194 tcp_ctask->datadigest = *digest;
1195 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
Alex Aizman7ba24712005-08-04 19:30:08 -07001196 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001197 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001198 return rc;
1199}
1200
1201/**
1202 * iscsi_solicit_data_cont - initialize next Data-Out
1203 * @conn: iscsi connection
1204 * @ctask: scsi command task
1205 * @r2t: R2T info
1206 * @left: bytes left to transfer
1207 *
1208 * Notes:
1209 * Initialize next Data-Out within this R2T sequence and continue
1210 * to process next Scatter-Gather element(if any) of this SCSI command.
1211 *
1212 * Called under connection lock.
1213 **/
1214static void
1215iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1216 struct iscsi_r2t_info *r2t, int left)
1217{
Mike Christie5bb0b552006-04-06 21:26:46 -05001218 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001219 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001220 struct scsi_cmnd *sc = ctask->sc;
1221 int new_offset;
1222
Mike Christieffbfe922006-05-18 20:31:36 -05001223 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001224 memset(hdr, 0, sizeof(struct iscsi_data));
1225 hdr->ttt = r2t->ttt;
1226 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1227 r2t->solicit_datasn++;
1228 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001229 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1230 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001231 hdr->exp_statsn = r2t->exp_statsn;
1232 new_offset = r2t->data_offset + r2t->sent;
1233 hdr->offset = cpu_to_be32(new_offset);
1234 if (left > conn->max_xmit_dlength) {
1235 hton24(hdr->dlength, conn->max_xmit_dlength);
1236 r2t->data_count = conn->max_xmit_dlength;
1237 } else {
1238 hton24(hdr->dlength, left);
1239 r2t->data_count = left;
1240 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1241 }
1242 conn->dataout_pdus_cnt++;
1243
Mike Christie6e458cc2006-05-18 20:31:31 -05001244 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001245 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001246
Alex Aizman7ba24712005-08-04 19:30:08 -07001247 if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001248 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
Alex Aizman7ba24712005-08-04 19:30:08 -07001249 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1250 r2t->sg += 1;
1251 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001252 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -07001253 (char*)sc->request_buffer + new_offset,
1254 r2t->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001255}
1256
1257static void
1258iscsi_unsolicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1259{
Mike Christie5bb0b552006-04-06 21:26:46 -05001260 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001261 struct iscsi_data_task *dtask;
1262
Mike Christieffbfe922006-05-18 20:31:36 -05001263 dtask = tcp_ctask->dtask = &tcp_ctask->unsol_dtask;
Mike Christie5bb0b552006-04-06 21:26:46 -05001264 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr,
1265 tcp_ctask->r2t_data_count);
Mike Christie6e458cc2006-05-18 20:31:31 -05001266 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001267 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001268}
1269
1270/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001271 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001272 * @conn: iscsi connection
1273 * @ctask: scsi command task
1274 * @sc: scsi command
1275 **/
1276static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001277iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001278{
Mike Christie5bb0b552006-04-06 21:26:46 -05001279 struct scsi_cmnd *sc = ctask->sc;
1280 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001281
Mike Christie5bb0b552006-04-06 21:26:46 -05001282 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Alex Aizman7ba24712005-08-04 19:30:08 -07001283
Mike Christie5bb0b552006-04-06 21:26:46 -05001284 tcp_ctask->sent = 0;
1285 tcp_ctask->sg_count = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001286
1287 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001288 tcp_ctask->xmstate = XMSTATE_W_HDR;
1289 tcp_ctask->exp_r2tsn = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001290 BUG_ON(ctask->total_length == 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05001291
Alex Aizman7ba24712005-08-04 19:30:08 -07001292 if (sc->use_sg) {
1293 struct scatterlist *sg = sc->request_buffer;
1294
Mike Christie5bb0b552006-04-06 21:26:46 -05001295 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1296 &sg[tcp_ctask->sg_count++]);
1297 tcp_ctask->sg = sg;
1298 tcp_ctask->bad_sg = sg + sc->use_sg;
Alex Aizman7ba24712005-08-04 19:30:08 -07001299 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001300 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1301 sc->request_buffer,
1302 sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001303
Mike Christie5bb0b552006-04-06 21:26:46 -05001304 if (ctask->imm_count)
1305 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001306
Mike Christie5bb0b552006-04-06 21:26:46 -05001307 tcp_ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
1308 if (tcp_ctask->pad_count) {
1309 tcp_ctask->pad_count = ISCSI_PAD_LEN -
1310 tcp_ctask->pad_count;
1311 debug_scsi("write padding %d bytes\n",
1312 tcp_ctask->pad_count);
1313 tcp_ctask->xmstate |= XMSTATE_W_PAD;
1314 }
1315
1316 if (ctask->unsol_count)
1317 tcp_ctask->xmstate |= XMSTATE_UNS_HDR |
1318 XMSTATE_UNS_INIT;
1319 tcp_ctask->r2t_data_count = ctask->total_length -
Alex Aizman7ba24712005-08-04 19:30:08 -07001320 ctask->imm_count -
1321 ctask->unsol_count;
1322
Mike Christieb6c395e2006-07-24 15:47:15 -05001323 debug_scsi("cmd [itt 0x%x total %d imm %d imm_data %d "
Alex Aizman7ba24712005-08-04 19:30:08 -07001324 "r2t_data %d]\n",
1325 ctask->itt, ctask->total_length, ctask->imm_count,
Mike Christie5bb0b552006-04-06 21:26:46 -05001326 ctask->unsol_count, tcp_ctask->r2t_data_count);
1327 } else
1328 tcp_ctask->xmstate = XMSTATE_R_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001329
Mike Christie6e458cc2006-05-18 20:31:31 -05001330 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001331 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001332}
1333
1334/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001335 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001336 * @conn: iscsi connection
1337 * @mtask: task management task
1338 *
1339 * Notes:
1340 * The function can return -EAGAIN in which case caller must
1341 * call it again later, or recover. '0' return code means successful
1342 * xmit.
1343 *
1344 * Management xmit state machine consists of two states:
1345 * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
1346 * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1347 **/
1348static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001349iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001350{
Mike Christie5bb0b552006-04-06 21:26:46 -05001351 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001352 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001353
1354 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001355 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001356
Mike Christie5bb0b552006-04-06 21:26:46 -05001357 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
1358 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001359 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001360 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christieaf973482005-09-12 21:01:32 -05001361 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001362 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001363 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001364 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1365 (u8*)tcp_mtask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001366 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1367 mtask->data_count);
1368 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001369 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001370 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001371 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001372 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001373 }
1374 }
1375
Mike Christie5bb0b552006-04-06 21:26:46 -05001376 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001377 BUG_ON(!mtask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001378 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001379 /* FIXME: implement.
1380 * Virtual buffer could be spreaded across multiple pages...
1381 */
1382 do {
Mike Christie3219e522006-05-30 00:37:28 -05001383 int rc;
1384
1385 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1386 &mtask->data_count, &tcp_mtask->sent);
1387 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001388 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001389 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001390 }
1391 } while (mtask->data_count);
1392 }
1393
Mike Christie5bb0b552006-04-06 21:26:46 -05001394 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
1395 if (mtask->hdr->itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1396 struct iscsi_session *session = conn->session;
1397
1398 spin_lock_bh(&session->lock);
1399 list_del(&conn->mtask->running);
1400 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1401 sizeof(void*));
1402 spin_unlock_bh(&session->lock);
1403 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001404 return 0;
1405}
1406
1407static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -05001408handle_xmstate_r_hdr(struct iscsi_conn *conn,
1409 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001410{
Mike Christie3219e522006-05-30 00:37:28 -05001411 int rc;
1412
Mike Christie5bb0b552006-04-06 21:26:46 -05001413 tcp_ctask->xmstate &= ~XMSTATE_R_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001414 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001415 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1416 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001417 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, 0);
1418 if (!rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001419 BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE);
Alex Aizman7ba24712005-08-04 19:30:08 -07001420 return 0; /* wait for Data-In */
1421 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001422 tcp_ctask->xmstate |= XMSTATE_R_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001423 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001424}
1425
1426static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -05001427handle_xmstate_w_hdr(struct iscsi_conn *conn,
1428 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001429{
Mike Christie5bb0b552006-04-06 21:26:46 -05001430 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001431 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001432
1433 tcp_ctask->xmstate &= ~XMSTATE_W_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001434 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001435 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1436 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001437 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1438 if (rc)
Mike Christie5bb0b552006-04-06 21:26:46 -05001439 tcp_ctask->xmstate |= XMSTATE_W_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001440 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001441}
1442
1443static inline int
1444handle_xmstate_data_digest(struct iscsi_conn *conn,
1445 struct iscsi_cmd_task *ctask)
1446{
Mike Christie5bb0b552006-04-06 21:26:46 -05001447 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001448 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001449
1450 tcp_ctask->xmstate &= ~XMSTATE_DATA_DIGEST;
1451 debug_tcp("resent data digest 0x%x\n", tcp_ctask->datadigest);
Mike Christie3219e522006-05-30 00:37:28 -05001452 rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1453 &tcp_ctask->datadigest, 0);
1454 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001455 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
Alex Aizman7ba24712005-08-04 19:30:08 -07001456 debug_tcp("resent data digest 0x%x fail!\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001457 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001458 }
Mike Christie3219e522006-05-30 00:37:28 -05001459
1460 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001461}
1462
1463static inline int
1464handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1465{
Mike Christie5bb0b552006-04-06 21:26:46 -05001466 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1467 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001468 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001469
Alex Aizman7ba24712005-08-04 19:30:08 -07001470 BUG_ON(!ctask->imm_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001471 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001472
1473 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001474 iscsi_data_digest_init(tcp_conn, ctask);
1475 tcp_ctask->immdigest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001476 }
1477
1478 for (;;) {
Mike Christie3219e522006-05-30 00:37:28 -05001479 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1480 &ctask->imm_count, &tcp_ctask->sent);
1481 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001482 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001483 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001484 crypto_digest_final(tcp_conn->data_tx_tfm,
1485 (u8*)&tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001486 debug_tcp("tx imm sendpage fail 0x%x\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001487 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001488 }
Mike Christie3219e522006-05-30 00:37:28 -05001489 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001490 }
1491 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001492 crypto_digest_update(tcp_conn->data_tx_tfm,
1493 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001494
1495 if (!ctask->imm_count)
1496 break;
Mike Christie5bb0b552006-04-06 21:26:46 -05001497 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1498 &tcp_ctask->sg[tcp_ctask->sg_count++]);
Alex Aizman7ba24712005-08-04 19:30:08 -07001499 }
1500
Mike Christie5bb0b552006-04-06 21:26:46 -05001501 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
Mike Christie3219e522006-05-30 00:37:28 -05001502 rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1503 &tcp_ctask->immdigest, 1);
1504 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001505 debug_tcp("sending imm digest 0x%x fail!\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001506 tcp_ctask->immdigest);
Mike Christie3219e522006-05-30 00:37:28 -05001507 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001508 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001509 debug_tcp("sending imm digest 0x%x\n", tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001510 }
1511
1512 return 0;
1513}
1514
1515static inline int
1516handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1517{
Mike Christie5bb0b552006-04-06 21:26:46 -05001518 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001519 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001520 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001521
Mike Christie5bb0b552006-04-06 21:26:46 -05001522 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1523 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001524 iscsi_unsolicit_data_init(conn, ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -05001525 dtask = tcp_ctask->dtask;
Mike Christieaf973482005-09-12 21:01:32 -05001526 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001527 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001528 (u8*)dtask->hdrext);
Mike Christie5bb0b552006-04-06 21:26:46 -05001529 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001530 }
Mike Christie3219e522006-05-30 00:37:28 -05001531
1532 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1533 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001534 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1535 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001536 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001537 }
1538
1539 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001540 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001541 return 0;
1542}
1543
1544static inline int
1545handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1546{
Mike Christie5bb0b552006-04-06 21:26:46 -05001547 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1548 struct iscsi_data_task *dtask = tcp_ctask->dtask;
1549 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001550 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001551
1552 BUG_ON(!ctask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001553 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001554
1555 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001556 iscsi_data_digest_init(tcp_conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001557 dtask->digest = 0;
1558 }
1559
1560 for (;;) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001561 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001562
Mike Christie3219e522006-05-30 00:37:28 -05001563 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1564 &ctask->data_count, &tcp_ctask->sent);
1565 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001566 ctask->unsol_count -= tcp_ctask->sent - start;
1567 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001568 /* will continue with this ctask later.. */
1569 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001570 crypto_digest_final(tcp_conn->data_tx_tfm,
Alex Aizman7ba24712005-08-04 19:30:08 -07001571 (u8 *)&dtask->digest);
1572 debug_tcp("tx uns data fail 0x%x\n",
1573 dtask->digest);
1574 }
Mike Christie3219e522006-05-30 00:37:28 -05001575 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001576 }
1577
Mike Christie5bb0b552006-04-06 21:26:46 -05001578 BUG_ON(tcp_ctask->sent > ctask->total_length);
1579 ctask->unsol_count -= tcp_ctask->sent - start;
Alex Aizman7ba24712005-08-04 19:30:08 -07001580
1581 /*
1582 * XXX:we may run here with un-initial sendbuf.
1583 * so pass it
1584 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001585 if (conn->datadgst_en && tcp_ctask->sent - start > 0)
1586 crypto_digest_update(tcp_conn->data_tx_tfm,
1587 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001588
1589 if (!ctask->data_count)
1590 break;
Mike Christie5bb0b552006-04-06 21:26:46 -05001591 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1592 &tcp_ctask->sg[tcp_ctask->sg_count++]);
Alex Aizman7ba24712005-08-04 19:30:08 -07001593 }
1594 BUG_ON(ctask->unsol_count < 0);
1595
1596 /*
1597 * Done with the Data-Out. Next, check if we need
1598 * to send another unsolicited Data-Out.
1599 */
1600 if (ctask->unsol_count) {
1601 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001602 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001603 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001604 &dtask->digest, 1);
1605 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001606 debug_tcp("send uns digest 0x%x fail\n",
1607 dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001608 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001609 }
1610 debug_tcp("sending uns digest 0x%x, more uns\n",
1611 dtask->digest);
1612 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001613 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001614 return 1;
1615 }
1616
Mike Christie5bb0b552006-04-06 21:26:46 -05001617 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
Mike Christie3219e522006-05-30 00:37:28 -05001618 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001619 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001620 &dtask->digest, 1);
1621 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001622 debug_tcp("send last uns digest 0x%x fail\n",
1623 dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001624 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001625 }
1626 debug_tcp("sending uns digest 0x%x\n",dtask->digest);
1627 }
1628
1629 return 0;
1630}
1631
1632static inline int
1633handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1634{
1635 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001636 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1637 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1638 struct iscsi_r2t_info *r2t = tcp_ctask->r2t;
Mike Christieffbfe922006-05-18 20:31:36 -05001639 struct iscsi_data_task *dtask = &r2t->dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001640 int left, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001641
Mike Christie5bb0b552006-04-06 21:26:46 -05001642 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1643 tcp_ctask->dtask = dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001644
1645 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001646 iscsi_data_digest_init(tcp_conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001647 dtask->digest = 0;
1648 }
1649solicit_again:
1650 /*
Mike Christieb6c395e2006-07-24 15:47:15 -05001651 * send Data-Out within this R2T sequence.
Alex Aizman7ba24712005-08-04 19:30:08 -07001652 */
1653 if (!r2t->data_count)
1654 goto data_out_done;
1655
Mike Christie3219e522006-05-30 00:37:28 -05001656 rc = iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent);
1657 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001658 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001659 /* will continue with this ctask later.. */
1660 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001661 crypto_digest_final(tcp_conn->data_tx_tfm,
Alex Aizman7ba24712005-08-04 19:30:08 -07001662 (u8 *)&dtask->digest);
1663 debug_tcp("r2t data send fail 0x%x\n", dtask->digest);
1664 }
Mike Christie3219e522006-05-30 00:37:28 -05001665 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001666 }
1667
1668 BUG_ON(r2t->data_count < 0);
1669 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001670 crypto_digest_update(tcp_conn->data_tx_tfm, &r2t->sendbuf.sg,
1671 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001672
1673 if (r2t->data_count) {
1674 BUG_ON(ctask->sc->use_sg == 0);
1675 if (!iscsi_buf_left(&r2t->sendbuf)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001676 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
Alex Aizman7ba24712005-08-04 19:30:08 -07001677 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1678 r2t->sg += 1;
1679 }
1680 goto solicit_again;
1681 }
1682
1683data_out_done:
1684 /*
1685 * Done with this Data-Out. Next, check if we have
1686 * to send another Data-Out for this R2T.
1687 */
1688 BUG_ON(r2t->data_length - r2t->sent < 0);
1689 left = r2t->data_length - r2t->sent;
1690 if (left) {
1691 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001692 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001693 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001694 &dtask->digest, 1);
1695 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001696 debug_tcp("send r2t data digest 0x%x"
1697 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001698 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001699 }
1700 debug_tcp("r2t data send digest 0x%x\n",
1701 dtask->digest);
1702 }
1703 iscsi_solicit_data_cont(conn, ctask, r2t, left);
Mike Christie5bb0b552006-04-06 21:26:46 -05001704 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1705 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001706 return 1;
1707 }
1708
1709 /*
1710 * Done with this R2T. Check if there are more
1711 * outstanding R2Ts ready to be processed.
1712 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001713 BUG_ON(tcp_ctask->r2t_data_count - r2t->data_length < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -07001714 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001715 rc = iscsi_digest_final_send(conn, ctask, &dtask->digestbuf,
1716 &dtask->digest, 1);
1717 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001718 debug_tcp("send last r2t data digest 0x%x"
1719 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001720 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001721 }
1722 debug_tcp("r2t done dout digest 0x%x\n", dtask->digest);
1723 }
1724
Mike Christie5bb0b552006-04-06 21:26:46 -05001725 tcp_ctask->r2t_data_count -= r2t->data_length;
1726 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001727 spin_lock_bh(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05001728 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -07001729 spin_unlock_bh(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05001730 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
1731 tcp_ctask->r2t = r2t;
1732 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1733 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001734 return 1;
1735 }
1736
1737 return 0;
1738}
1739
1740static inline int
1741handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1742{
Mike Christie5bb0b552006-04-06 21:26:46 -05001743 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1744 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1745 struct iscsi_data_task *dtask = tcp_ctask->dtask;
Mike Christieb6c395e2006-07-24 15:47:15 -05001746 int sent = 0, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001747
Mike Christie5bb0b552006-04-06 21:26:46 -05001748 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
Mike Christie6e458cc2006-05-18 20:31:31 -05001749 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
Mike Christie5bb0b552006-04-06 21:26:46 -05001750 tcp_ctask->pad_count);
Mike Christie3219e522006-05-30 00:37:28 -05001751 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1752 &sent);
1753 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001754 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Mike Christie3219e522006-05-30 00:37:28 -05001755 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001756 }
1757
1758 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001759 crypto_digest_update(tcp_conn->data_tx_tfm,
1760 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001761 /* imm data? */
1762 if (!dtask) {
Mike Christie3219e522006-05-30 00:37:28 -05001763 rc = iscsi_digest_final_send(conn, ctask,
Mike Christie5bb0b552006-04-06 21:26:46 -05001764 &tcp_ctask->immbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001765 &tcp_ctask->immdigest, 1);
1766 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001767 debug_tcp("send padding digest 0x%x"
Mike Christie5bb0b552006-04-06 21:26:46 -05001768 "fail!\n", tcp_ctask->immdigest);
Mike Christie3219e522006-05-30 00:37:28 -05001769 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001770 }
1771 debug_tcp("done with padding, digest 0x%x\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001772 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001773 } else {
Mike Christie3219e522006-05-30 00:37:28 -05001774 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001775 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001776 &dtask->digest, 1);
1777 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001778 debug_tcp("send padding digest 0x%x"
1779 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001780 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001781 }
1782 debug_tcp("done with padding, digest 0x%x\n",
1783 dtask->digest);
1784 }
1785 }
1786
1787 return 0;
1788}
1789
1790static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001791iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001792{
Mike Christie5bb0b552006-04-06 21:26:46 -05001793 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001794 int rc = 0;
1795
1796 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001797 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001798
1799 /*
1800 * serialize with TMF AbortTask
1801 */
1802 if (ctask->mtask)
1803 return rc;
1804
Mike Christie3219e522006-05-30 00:37:28 -05001805 if (tcp_ctask->xmstate & XMSTATE_R_HDR)
1806 return handle_xmstate_r_hdr(conn, tcp_ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001807
Mike Christie5bb0b552006-04-06 21:26:46 -05001808 if (tcp_ctask->xmstate & XMSTATE_W_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001809 rc = handle_xmstate_w_hdr(conn, ctask);
1810 if (rc)
1811 return rc;
1812 }
1813
1814 /* XXX: for data digest xmit recover */
Mike Christie5bb0b552006-04-06 21:26:46 -05001815 if (tcp_ctask->xmstate & XMSTATE_DATA_DIGEST) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001816 rc = handle_xmstate_data_digest(conn, ctask);
1817 if (rc)
1818 return rc;
1819 }
1820
Mike Christie5bb0b552006-04-06 21:26:46 -05001821 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001822 rc = handle_xmstate_imm_data(conn, ctask);
1823 if (rc)
1824 return rc;
1825 }
1826
Mike Christie5bb0b552006-04-06 21:26:46 -05001827 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001828 BUG_ON(!ctask->unsol_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001829 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001830unsolicit_head_again:
1831 rc = handle_xmstate_uns_hdr(conn, ctask);
1832 if (rc)
1833 return rc;
1834 }
1835
Mike Christie5bb0b552006-04-06 21:26:46 -05001836 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001837 rc = handle_xmstate_uns_data(conn, ctask);
1838 if (rc == 1)
1839 goto unsolicit_head_again;
1840 else if (rc)
1841 return rc;
1842 goto done;
1843 }
1844
Mike Christie5bb0b552006-04-06 21:26:46 -05001845 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001846 struct iscsi_r2t_info *r2t;
1847
Mike Christie5bb0b552006-04-06 21:26:46 -05001848 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1849 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1850 if (!tcp_ctask->r2t)
1851 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
Alex Aizman7ba24712005-08-04 19:30:08 -07001852 sizeof(void*));
1853solicit_head_again:
Mike Christie5bb0b552006-04-06 21:26:46 -05001854 r2t = tcp_ctask->r2t;
Mike Christieaf973482005-09-12 21:01:32 -05001855 if (conn->hdrdgst_en)
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001856 iscsi_hdr_digest(conn, &r2t->headbuf,
Mike Christieffbfe922006-05-18 20:31:36 -05001857 (u8*)r2t->dtask.hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001858 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
1859 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001860 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1861 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001862 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001863 }
1864
1865 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1866 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1867 r2t->sent);
1868 }
1869
Mike Christie5bb0b552006-04-06 21:26:46 -05001870 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001871 rc = handle_xmstate_sol_data(conn, ctask);
1872 if (rc == 1)
1873 goto solicit_head_again;
1874 if (rc)
1875 return rc;
1876 }
1877
1878done:
1879 /*
1880 * Last thing to check is whether we need to send write
1881 * padding. Note that we check for xmstate equality, not just the bit.
1882 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001883 if (tcp_ctask->xmstate == XMSTATE_W_PAD)
Alex Aizman7ba24712005-08-04 19:30:08 -07001884 rc = handle_xmstate_w_pad(conn, ctask);
1885
1886 return rc;
1887}
1888
Mike Christie7b8631b2006-01-13 18:05:50 -06001889static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001890iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001891{
Mike Christie7b8631b2006-01-13 18:05:50 -06001892 struct iscsi_conn *conn;
1893 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001894 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001895
Mike Christie5bb0b552006-04-06 21:26:46 -05001896 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001897 if (!cls_conn)
1898 return NULL;
1899 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001900 /*
1901 * due to strange issues with iser these are not set
1902 * in iscsi_conn_setup
1903 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001904 conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
1905
Mike Christie5bb0b552006-04-06 21:26:46 -05001906 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1907 if (!tcp_conn)
1908 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001909
Mike Christie5bb0b552006-04-06 21:26:46 -05001910 conn->dd_data = tcp_conn;
1911 tcp_conn->iscsi_conn = conn;
1912 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1913 /* initial operational parameters */
1914 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
1915 tcp_conn->data_size = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
Alex Aizman7ba24712005-08-04 19:30:08 -07001916
1917 /* allocate initial PDU receive place holder */
Mike Christie5bb0b552006-04-06 21:26:46 -05001918 if (tcp_conn->data_size <= PAGE_SIZE)
1919 tcp_conn->data = kmalloc(tcp_conn->data_size, GFP_KERNEL);
Alex Aizman7ba24712005-08-04 19:30:08 -07001920 else
Mike Christie5bb0b552006-04-06 21:26:46 -05001921 tcp_conn->data = (void*)__get_free_pages(GFP_KERNEL,
1922 get_order(tcp_conn->data_size));
1923 if (!tcp_conn->data)
Alex Aizman7ba24712005-08-04 19:30:08 -07001924 goto max_recv_dlenght_alloc_fail;
1925
Mike Christie7b8631b2006-01-13 18:05:50 -06001926 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001927
1928max_recv_dlenght_alloc_fail:
Mike Christie5bb0b552006-04-06 21:26:46 -05001929 kfree(tcp_conn);
1930tcp_conn_alloc_fail:
1931 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001932 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001933}
1934
1935static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001936iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001937{
Mike Christie7b8631b2006-01-13 18:05:50 -06001938 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001939 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1940 int digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001941
Mike Christie5bb0b552006-04-06 21:26:46 -05001942 if (conn->hdrdgst_en || conn->datadgst_en)
1943 digest = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001944
Mike Christie5bb0b552006-04-06 21:26:46 -05001945 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001946
Mike Christie5bb0b552006-04-06 21:26:46 -05001947 /* now free tcp_conn */
1948 if (digest) {
1949 if (tcp_conn->tx_tfm)
1950 crypto_free_tfm(tcp_conn->tx_tfm);
1951 if (tcp_conn->rx_tfm)
1952 crypto_free_tfm(tcp_conn->rx_tfm);
1953 if (tcp_conn->data_tx_tfm)
1954 crypto_free_tfm(tcp_conn->data_tx_tfm);
1955 if (tcp_conn->data_rx_tfm)
1956 crypto_free_tfm(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001957 }
1958
1959 /* free conn->data, size = MaxRecvDataSegmentLength */
Mike Christie5bb0b552006-04-06 21:26:46 -05001960 if (tcp_conn->data_size <= PAGE_SIZE)
1961 kfree(tcp_conn->data);
Alex Aizman7ba24712005-08-04 19:30:08 -07001962 else
Mike Christie5bb0b552006-04-06 21:26:46 -05001963 free_pages((unsigned long)tcp_conn->data,
1964 get_order(tcp_conn->data_size));
1965 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001966}
1967
1968static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001969iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001970 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001971 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001972{
Mike Christie5bb0b552006-04-06 21:26:46 -05001973 struct iscsi_conn *conn = cls_conn->dd_data;
1974 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001975 struct sock *sk;
1976 struct socket *sock;
1977 int err;
1978
1979 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001980 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001981 if (!sock) {
1982 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1983 return -EEXIST;
1984 }
1985
Mike Christie5bb0b552006-04-06 21:26:46 -05001986 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1987 if (err)
1988 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001989
Mike Christie67a61112006-05-30 00:37:20 -05001990 /* bind iSCSI connection and socket */
1991 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001992
Mike Christie67a61112006-05-30 00:37:20 -05001993 /* setup Socket parameters */
1994 sk = sock->sk;
1995 sk->sk_reuse = 1;
1996 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1997 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001998
Mike Christie67a61112006-05-30 00:37:20 -05001999 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07002000
Mike Christie67a61112006-05-30 00:37:20 -05002001 /*
2002 * Intercept TCP callbacks for sendfile like receive
2003 * processing.
2004 */
2005 conn->recv_lock = &sk->sk_callback_lock;
2006 iscsi_conn_set_callbacks(conn);
2007 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
2008 /*
2009 * set receive state machine into initial state
2010 */
2011 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07002012
Alex Aizman7ba24712005-08-04 19:30:08 -07002013 return 0;
2014}
2015
Mike Christie30a6c652006-04-06 21:13:39 -05002016static void
Mike Christie5bb0b552006-04-06 21:26:46 -05002017iscsi_tcp_suspend_conn_rx(struct iscsi_conn *conn)
Mike Christie30a6c652006-04-06 21:13:39 -05002018{
Mike Christie5bb0b552006-04-06 21:26:46 -05002019 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002020 struct sock *sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07002021
Mike Christie5bb0b552006-04-06 21:26:46 -05002022 if (!tcp_conn->sock)
2023 return;
2024
2025 sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07002026 write_lock_bh(&sk->sk_callback_lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05002027 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Alex Aizman7ba24712005-08-04 19:30:08 -07002028 write_unlock_bh(&sk->sk_callback_lock);
Mike Christie30a6c652006-04-06 21:13:39 -05002029}
2030
2031static void
Mike Christie5bb0b552006-04-06 21:26:46 -05002032iscsi_tcp_terminate_conn(struct iscsi_conn *conn)
Mike Christie30a6c652006-04-06 21:13:39 -05002033{
Mike Christie5bb0b552006-04-06 21:26:46 -05002034 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2035
2036 if (!tcp_conn->sock)
Mike Christie30a6c652006-04-06 21:13:39 -05002037 return;
Mike Christie30a6c652006-04-06 21:13:39 -05002038
Mike Christie5bb0b552006-04-06 21:26:46 -05002039 sock_hold(tcp_conn->sock->sk);
Mike Christie30a6c652006-04-06 21:13:39 -05002040 iscsi_conn_restore_callbacks(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05002041 sock_put(tcp_conn->sock->sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07002042
Mike Christie5bb0b552006-04-06 21:26:46 -05002043 sock_release(tcp_conn->sock);
2044 tcp_conn->sock = NULL;
2045 conn->recv_lock = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002046}
2047
Mike Christie5bb0b552006-04-06 21:26:46 -05002048/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05002049static void
Mike Christie5bb0b552006-04-06 21:26:46 -05002050iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask,
2051 char *data, uint32_t data_size)
Mike Christie30a6c652006-04-06 21:13:39 -05002052{
Mike Christie5bb0b552006-04-06 21:26:46 -05002053 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie30a6c652006-04-06 21:13:39 -05002054
Mike Christie6e458cc2006-05-18 20:31:31 -05002055 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
2056 sizeof(struct iscsi_hdr));
Mike Christie5bb0b552006-04-06 21:26:46 -05002057 tcp_mtask->xmstate = XMSTATE_IMM_HDR;
Mike Christieb6c395e2006-07-24 15:47:15 -05002058 tcp_mtask->sent = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002059
Mike Christie5bb0b552006-04-06 21:26:46 -05002060 if (mtask->data_count)
2061 iscsi_buf_init_iov(&tcp_mtask->sendbuf, (char*)mtask->data,
Alex Aizman7ba24712005-08-04 19:30:08 -07002062 mtask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07002063}
2064
2065static int
2066iscsi_r2tpool_alloc(struct iscsi_session *session)
2067{
2068 int i;
2069 int cmd_i;
2070
2071 /*
2072 * initialize per-task: R2T pool and xmit queue
2073 */
2074 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2075 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05002076 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002077
2078 /*
2079 * pre-allocated x4 as much r2ts to handle race when
2080 * target acks DataOut faster than we data_xmit() queues
2081 * could replenish r2tqueue.
2082 */
2083
2084 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05002085 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
2086 (void***)&tcp_ctask->r2ts,
2087 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002088 goto r2t_alloc_fail;
2089 }
2090
2091 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05002092 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07002093 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05002094 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2095 iscsi_pool_free(&tcp_ctask->r2tpool,
2096 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002097 goto r2t_alloc_fail;
2098 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002099 }
2100
2101 return 0;
2102
2103r2t_alloc_fail:
2104 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002105 struct iscsi_cmd_task *ctask = session->cmds[i];
2106 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2107
Mike Christie5bb0b552006-04-06 21:26:46 -05002108 kfifo_free(tcp_ctask->r2tqueue);
2109 iscsi_pool_free(&tcp_ctask->r2tpool,
2110 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002111 }
2112 return -ENOMEM;
2113}
2114
2115static void
2116iscsi_r2tpool_free(struct iscsi_session *session)
2117{
2118 int i;
2119
2120 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002121 struct iscsi_cmd_task *ctask = session->cmds[i];
2122 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2123
Mike Christie5bb0b552006-04-06 21:26:46 -05002124 kfifo_free(tcp_ctask->r2tqueue);
2125 iscsi_pool_free(&tcp_ctask->r2tpool,
2126 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002127 }
2128}
2129
Alex Aizman7ba24712005-08-04 19:30:08 -07002130static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002131iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002132 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07002133{
Mike Christie7b7232f2006-02-01 21:06:49 -06002134 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002135 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05002136 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002137 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07002138
Alex Aizman7ba24712005-08-04 19:30:08 -07002139 switch(param) {
2140 case ISCSI_PARAM_MAX_RECV_DLENGTH: {
Mike Christie5bb0b552006-04-06 21:26:46 -05002141 char *saveptr = tcp_conn->data;
Al Virob53cb2a2005-12-15 09:17:19 +00002142 gfp_t flags = GFP_KERNEL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002143
Mike Christie5c75b7f2006-06-28 12:00:26 -05002144 sscanf(buf, "%d", &value);
Mike Christie5bb0b552006-04-06 21:26:46 -05002145 if (tcp_conn->data_size >= value) {
Mike Christie5c75b7f2006-06-28 12:00:26 -05002146 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002147 break;
2148 }
2149
2150 spin_lock_bh(&session->lock);
2151 if (conn->stop_stage == STOP_CONN_RECOVER)
2152 flags = GFP_ATOMIC;
2153 spin_unlock_bh(&session->lock);
2154
2155 if (value <= PAGE_SIZE)
Mike Christie5bb0b552006-04-06 21:26:46 -05002156 tcp_conn->data = kmalloc(value, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07002157 else
Mike Christie5bb0b552006-04-06 21:26:46 -05002158 tcp_conn->data = (void*)__get_free_pages(flags,
Alex Aizman7ba24712005-08-04 19:30:08 -07002159 get_order(value));
Mike Christie5bb0b552006-04-06 21:26:46 -05002160 if (tcp_conn->data == NULL) {
2161 tcp_conn->data = saveptr;
Alex Aizman7ba24712005-08-04 19:30:08 -07002162 return -ENOMEM;
2163 }
Mike Christie5bb0b552006-04-06 21:26:46 -05002164 if (tcp_conn->data_size <= PAGE_SIZE)
Alex Aizman7ba24712005-08-04 19:30:08 -07002165 kfree(saveptr);
2166 else
2167 free_pages((unsigned long)saveptr,
Mike Christie5bb0b552006-04-06 21:26:46 -05002168 get_order(tcp_conn->data_size));
Mike Christie5c75b7f2006-06-28 12:00:26 -05002169 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002170 tcp_conn->data_size = value;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002171 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002172 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002173 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002174 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002175 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07002176 if (conn->hdrdgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002177 tcp_conn->hdr_size += sizeof(__u32);
2178 if (!tcp_conn->tx_tfm)
2179 tcp_conn->tx_tfm = crypto_alloc_tfm("crc32c",
2180 0);
2181 if (!tcp_conn->tx_tfm)
Alex Aizman7ba24712005-08-04 19:30:08 -07002182 return -ENOMEM;
Mike Christie5bb0b552006-04-06 21:26:46 -05002183 if (!tcp_conn->rx_tfm)
2184 tcp_conn->rx_tfm = crypto_alloc_tfm("crc32c",
2185 0);
2186 if (!tcp_conn->rx_tfm) {
2187 crypto_free_tfm(tcp_conn->tx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002188 return -ENOMEM;
2189 }
2190 } else {
Mike Christie5bb0b552006-04-06 21:26:46 -05002191 if (tcp_conn->tx_tfm)
2192 crypto_free_tfm(tcp_conn->tx_tfm);
2193 if (tcp_conn->rx_tfm)
2194 crypto_free_tfm(tcp_conn->rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002195 }
2196 break;
2197 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002198 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002199 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002200 if (!tcp_conn->data_tx_tfm)
2201 tcp_conn->data_tx_tfm =
Alex Aizman7ba24712005-08-04 19:30:08 -07002202 crypto_alloc_tfm("crc32c", 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05002203 if (!tcp_conn->data_tx_tfm)
Alex Aizman7ba24712005-08-04 19:30:08 -07002204 return -ENOMEM;
Mike Christie5bb0b552006-04-06 21:26:46 -05002205 if (!tcp_conn->data_rx_tfm)
2206 tcp_conn->data_rx_tfm =
Alex Aizman7ba24712005-08-04 19:30:08 -07002207 crypto_alloc_tfm("crc32c", 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05002208 if (!tcp_conn->data_rx_tfm) {
2209 crypto_free_tfm(tcp_conn->data_tx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002210 return -ENOMEM;
2211 }
2212 } else {
Mike Christie5bb0b552006-04-06 21:26:46 -05002213 if (tcp_conn->data_tx_tfm)
2214 crypto_free_tfm(tcp_conn->data_tx_tfm);
2215 if (tcp_conn->data_rx_tfm)
2216 crypto_free_tfm(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002217 }
Mike Christie5bb0b552006-04-06 21:26:46 -05002218 tcp_conn->sendpage = conn->datadgst_en ?
2219 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07002220 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002221 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002222 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07002223 if (session->max_r2t == roundup_pow_of_two(value))
2224 break;
2225 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002226 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002227 if (session->max_r2t & (session->max_r2t - 1))
2228 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2229 if (iscsi_r2tpool_alloc(session))
2230 return -ENOMEM;
2231 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002232 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002233 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002234 }
2235
2236 return 0;
2237}
2238
2239static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05002240iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2241 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06002242{
Mike Christie7b7232f2006-02-01 21:06:49 -06002243 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002244 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christiefd7255f2006-04-06 21:13:36 -05002245 struct inet_sock *inet;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002246 struct ipv6_pinfo *np;
2247 struct sock *sk;
2248 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06002249
2250 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002251 case ISCSI_PARAM_CONN_PORT:
2252 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002253 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002254 mutex_unlock(&conn->xmitmutex);
2255 return -EINVAL;
2256 }
2257
Mike Christie5bb0b552006-04-06 21:26:46 -05002258 inet = inet_sk(tcp_conn->sock->sk);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002259 len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
Mike Christiefd7255f2006-04-06 21:13:36 -05002260 mutex_unlock(&conn->xmitmutex);
Mike Christie8d2860b2006-05-02 19:46:47 -05002261 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002262 case ISCSI_PARAM_CONN_ADDRESS:
2263 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002264 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002265 mutex_unlock(&conn->xmitmutex);
2266 return -EINVAL;
2267 }
2268
Mike Christie5bb0b552006-04-06 21:26:46 -05002269 sk = tcp_conn->sock->sk;
Mike Christiefd7255f2006-04-06 21:13:36 -05002270 if (sk->sk_family == PF_INET) {
2271 inet = inet_sk(sk);
2272 len = sprintf(buf, "%u.%u.%u.%u\n",
2273 NIPQUAD(inet->daddr));
2274 } else {
2275 np = inet6_sk(sk);
2276 len = sprintf(buf,
2277 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
2278 NIP6(np->daddr));
2279 }
2280 mutex_unlock(&conn->xmitmutex);
2281 break;
2282 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002283 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05002284 }
2285
2286 return len;
2287}
2288
Alex Aizman7ba24712005-08-04 19:30:08 -07002289static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002290iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002291{
Mike Christie7b7232f2006-02-01 21:06:49 -06002292 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002293 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002294
2295 stats->txdata_octets = conn->txdata_octets;
2296 stats->rxdata_octets = conn->rxdata_octets;
2297 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2298 stats->dataout_pdus = conn->dataout_pdus_cnt;
2299 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2300 stats->datain_pdus = conn->datain_pdus_cnt;
2301 stats->r2t_pdus = conn->r2t_pdus_cnt;
2302 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2303 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2304 stats->custom_length = 3;
2305 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002306 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002307 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002308 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002309 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2310 stats->custom[2].value = conn->eh_abort_cnt;
2311}
2312
Mike Christie5bb0b552006-04-06 21:26:46 -05002313static struct iscsi_cls_session *
2314iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2315 struct scsi_transport_template *scsit,
2316 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002317{
Mike Christie5bb0b552006-04-06 21:26:46 -05002318 struct iscsi_cls_session *cls_session;
2319 struct iscsi_session *session;
2320 uint32_t hn;
2321 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002322
Mike Christie5bb0b552006-04-06 21:26:46 -05002323 cls_session = iscsi_session_setup(iscsit, scsit,
2324 sizeof(struct iscsi_tcp_cmd_task),
2325 sizeof(struct iscsi_tcp_mgmt_task),
2326 initial_cmdsn, &hn);
2327 if (!cls_session)
2328 return NULL;
2329 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002330
Mike Christie5bb0b552006-04-06 21:26:46 -05002331 session = class_to_transport_session(cls_session);
2332 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2333 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2334 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2335
2336 ctask->hdr = &tcp_ctask->hdr;
2337 }
2338
2339 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2340 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2341 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2342
2343 mtask->hdr = &tcp_mtask->hdr;
2344 }
2345
2346 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2347 goto r2tpool_alloc_fail;
2348
2349 return cls_session;
2350
2351r2tpool_alloc_fail:
2352 iscsi_session_teardown(cls_session);
2353 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002354}
2355
Mike Christie5bb0b552006-04-06 21:26:46 -05002356static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2357{
Mike Christie5bb0b552006-04-06 21:26:46 -05002358 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2359 iscsi_session_teardown(cls_session);
2360}
2361
2362static struct scsi_host_template iscsi_sht = {
Mike Christiee0ecae82006-05-18 20:31:43 -05002363 .name = "iSCSI Initiator over TCP/IP, v"
2364 ISCSI_TCP_VERSION,
Mike Christie5bb0b552006-04-06 21:26:46 -05002365 .queuecommand = iscsi_queuecommand,
2366 .change_queue_depth = iscsi_change_queue_depth,
2367 .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
2368 .sg_tablesize = ISCSI_SG_TABLESIZE,
2369 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2370 .eh_abort_handler = iscsi_eh_abort,
2371 .eh_host_reset_handler = iscsi_eh_host_reset,
2372 .use_clustering = DISABLE_CLUSTERING,
2373 .proc_name = "iscsi_tcp",
2374 .this_id = -1,
2375};
2376
Alex Aizman7ba24712005-08-04 19:30:08 -07002377static struct iscsi_transport iscsi_tcp_transport = {
2378 .owner = THIS_MODULE,
2379 .name = "tcp",
2380 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2381 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002382 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2383 ISCSI_MAX_XMIT_DLENGTH |
2384 ISCSI_HDRDGST_EN |
2385 ISCSI_DATADGST_EN |
2386 ISCSI_INITIAL_R2T_EN |
2387 ISCSI_MAX_R2T |
2388 ISCSI_IMM_DATA_EN |
2389 ISCSI_FIRST_BURST |
2390 ISCSI_MAX_BURST |
2391 ISCSI_PDU_INORDER_EN |
2392 ISCSI_DATASEQ_INORDER_EN |
2393 ISCSI_ERL |
2394 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002395 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05002396 ISCSI_EXP_STATSN |
2397 ISCSI_PERSISTENT_PORT |
2398 ISCSI_PERSISTENT_ADDRESS |
2399 ISCSI_TARGET_NAME |
2400 ISCSI_TPGT,
Alex Aizman7ba24712005-08-04 19:30:08 -07002401 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002402 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002403 .max_conn = 1,
2404 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002405 /* session management */
2406 .create_session = iscsi_tcp_session_create,
2407 .destroy_session = iscsi_tcp_session_destroy,
2408 /* connection management */
2409 .create_conn = iscsi_tcp_conn_create,
2410 .bind_conn = iscsi_tcp_conn_bind,
2411 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002412 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002413 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002414 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002415 .start_conn = iscsi_conn_start,
2416 .stop_conn = iscsi_conn_stop,
Mike Christie5bb0b552006-04-06 21:26:46 -05002417 /* these are called as part of conn recovery */
2418 .suspend_conn_recv = iscsi_tcp_suspend_conn_rx,
2419 .terminate_conn = iscsi_tcp_terminate_conn,
2420 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002421 .send_pdu = iscsi_conn_send_pdu,
2422 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002423 .init_cmd_task = iscsi_tcp_cmd_init,
2424 .init_mgmt_task = iscsi_tcp_mgmt_init,
2425 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2426 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2427 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2428 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002429 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002430};
2431
2432static int __init
2433iscsi_tcp_init(void)
2434{
Alex Aizman7ba24712005-08-04 19:30:08 -07002435 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002436 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2437 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002438 return -EINVAL;
2439 }
2440 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2441
Mike Christie7b8631b2006-01-13 18:05:50 -06002442 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002443 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002444
Mike Christie7b8631b2006-01-13 18:05:50 -06002445 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002446}
2447
2448static void __exit
2449iscsi_tcp_exit(void)
2450{
2451 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002452}
2453
2454module_init(iscsi_tcp_init);
2455module_exit(iscsi_tcp_exit);