blob: ab324d984b8d6d6922be5bd63eff35c3351a75f3 [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);
489 /* fall through */
490 case ISCSI_OP_SCSI_CMD_RSP:
491 tcp_conn->in.ctask = session->cmds[itt];
492 if (tcp_conn->in.datalen)
493 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700494
Mike Christie5bb0b552006-04-06 21:26:46 -0500495 spin_lock(&session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500496 iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500497 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
498 spin_unlock(&session->lock);
499 break;
500 case ISCSI_OP_R2T:
501 tcp_conn->in.ctask = session->cmds[itt];
502 if (ahslen)
503 rc = ISCSI_ERR_AHSLEN;
504 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
505 DMA_TO_DEVICE)
506 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
507 else
508 rc = ISCSI_ERR_PROTO;
509 break;
510 case ISCSI_OP_LOGIN_RSP:
511 case ISCSI_OP_TEXT_RSP:
512 case ISCSI_OP_LOGOUT_RSP:
513 case ISCSI_OP_NOOP_IN:
514 case ISCSI_OP_REJECT:
515 case ISCSI_OP_ASYNC_EVENT:
516 if (tcp_conn->in.datalen)
517 goto copy_hdr;
518 /* fall through */
519 case ISCSI_OP_SCSI_TMFUNC_RSP:
520 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
521 break;
522 default:
523 rc = ISCSI_ERR_BAD_OPCODE;
524 break;
525 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700526
527 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500528
529copy_hdr:
530 /*
531 * if we did zero copy for the header but we will need multiple
532 * skbs to complete the command then we have to copy the header
533 * for later use
534 */
535 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <
536 (tcp_conn->in.datalen + tcp_conn->in.padding +
537 (conn->datadgst_en ? 4 : 0))) {
538 debug_tcp("Copying header for later use. in.copy %d in.datalen"
539 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
540 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
541 sizeof(struct iscsi_hdr));
542 tcp_conn->in.hdr = &tcp_conn->hdr;
543 tcp_conn->in.zero_copy_hdr = 0;
544 }
545 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700546}
547
548/**
549 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
Mike Christie5bb0b552006-04-06 21:26:46 -0500550 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700551 * @ctask: scsi command task
552 * @buf: buffer to copy to
553 * @buf_size: size of buffer
554 * @offset: offset within the buffer
555 *
556 * Notes:
557 * The function calls skb_copy_bits() and updates per-connection and
558 * per-cmd byte counters.
559 *
560 * Read counters (in bytes):
561 *
562 * conn->in.offset offset within in progress SKB
563 * conn->in.copy left to copy from in progress SKB
564 * including padding
565 * conn->in.copied copied already from in progress SKB
566 * conn->data_copied copied already from in progress buffer
567 * ctask->sent total bytes sent up to the MidLayer
568 * ctask->data_count left to copy from in progress Data-In
569 * buf_left left to copy from in progress buffer
570 **/
571static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500572iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -0700573 void *buf, int buf_size, int offset)
574{
Mike Christie5bb0b552006-04-06 21:26:46 -0500575 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
576 int buf_left = buf_size - (tcp_conn->data_copied + offset);
577 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700578 int rc;
579
580 size = min(size, ctask->data_count);
581
582 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500583 size, tcp_conn->in.offset, tcp_conn->in.copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700584
585 BUG_ON(size <= 0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500586 BUG_ON(tcp_ctask->sent + size > ctask->total_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700587
Mike Christie5bb0b552006-04-06 21:26:46 -0500588 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
589 (char*)buf + (offset + tcp_conn->data_copied), size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700590 /* must fit into skb->len */
591 BUG_ON(rc);
592
Mike Christie5bb0b552006-04-06 21:26:46 -0500593 tcp_conn->in.offset += size;
594 tcp_conn->in.copy -= size;
595 tcp_conn->in.copied += size;
596 tcp_conn->data_copied += size;
597 tcp_ctask->sent += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700598 ctask->data_count -= size;
599
Mike Christie5bb0b552006-04-06 21:26:46 -0500600 BUG_ON(tcp_conn->in.copy < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700601 BUG_ON(ctask->data_count < 0);
602
Mike Christie5bb0b552006-04-06 21:26:46 -0500603 if (buf_size != (tcp_conn->data_copied + offset)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700604 if (!ctask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500605 BUG_ON(buf_size - tcp_conn->data_copied < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700606 /* done with this PDU */
Mike Christie5bb0b552006-04-06 21:26:46 -0500607 return buf_size - tcp_conn->data_copied;
Alex Aizman7ba24712005-08-04 19:30:08 -0700608 }
609 return -EAGAIN;
610 }
611
612 /* done with this buffer or with both - PDU and buffer */
Mike Christie5bb0b552006-04-06 21:26:46 -0500613 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700614 return 0;
615}
616
617/**
618 * iscsi_tcp_copy - copy skb bits to the destanation buffer
Mike Christie5bb0b552006-04-06 21:26:46 -0500619 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700620 *
621 * Notes:
622 * The function calls skb_copy_bits() and updates per-connection
623 * byte counters.
624 **/
625static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500626iscsi_tcp_copy(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700627{
Mike Christie5bb0b552006-04-06 21:26:46 -0500628 void *buf = tcp_conn->data;
629 int buf_size = tcp_conn->in.datalen;
630 int buf_left = buf_size - tcp_conn->data_copied;
631 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700632 int rc;
633
634 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500635 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700636 BUG_ON(size <= 0);
637
Mike Christie5bb0b552006-04-06 21:26:46 -0500638 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
639 (char*)buf + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700640 BUG_ON(rc);
641
Mike Christie5bb0b552006-04-06 21:26:46 -0500642 tcp_conn->in.offset += size;
643 tcp_conn->in.copy -= size;
644 tcp_conn->in.copied += size;
645 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700646
Mike Christie5bb0b552006-04-06 21:26:46 -0500647 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700648 return -EAGAIN;
649
650 return 0;
651}
652
653static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -0500654partial_sg_digest_update(struct iscsi_tcp_conn *tcp_conn,
655 struct scatterlist *sg, int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700656{
657 struct scatterlist temp;
658
659 memcpy(&temp, sg, sizeof(struct scatterlist));
660 temp.offset = offset;
661 temp.length = length;
Mike Christie5bb0b552006-04-06 21:26:46 -0500662 crypto_digest_update(tcp_conn->data_rx_tfm, &temp, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700663}
664
Mike Christief6cfba12005-11-29 23:12:57 -0600665static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500666iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600667{
668 struct scatterlist tmp;
669
670 sg_init_one(&tmp, buf, len);
Mike Christie5bb0b552006-04-06 21:26:46 -0500671 crypto_digest_update(tcp_conn->data_rx_tfm, &tmp, 1);
Mike Christief6cfba12005-11-29 23:12:57 -0600672}
673
Alex Aizman7ba24712005-08-04 19:30:08 -0700674static int iscsi_scsi_data_in(struct iscsi_conn *conn)
675{
Mike Christie5bb0b552006-04-06 21:26:46 -0500676 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
677 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
678 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700679 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600680 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700681 int i, offset, rc = 0;
682
683 BUG_ON((void*)ctask != sc->SCp.ptr);
684
685 /*
686 * copying Data-In into the Scsi_Cmnd
687 */
688 if (!sc->use_sg) {
689 i = ctask->data_count;
Mike Christie5bb0b552006-04-06 21:26:46 -0500690 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
691 sc->request_bufflen,
692 tcp_ctask->data_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700693 if (rc == -EAGAIN)
694 return rc;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600695 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500696 iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
697 i);
Alex Aizman7ba24712005-08-04 19:30:08 -0700698 rc = 0;
699 goto done;
700 }
701
Mike Christie5bb0b552006-04-06 21:26:46 -0500702 offset = tcp_ctask->data_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700703 sg = sc->request_buffer;
704
Mike Christie5bb0b552006-04-06 21:26:46 -0500705 if (tcp_ctask->data_offset)
706 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700707 offset -= sg[i].length;
708 /* we've passed through partial sg*/
709 if (offset < 0)
710 offset = 0;
711
Mike Christie5bb0b552006-04-06 21:26:46 -0500712 for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700713 char *dest;
714
715 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500716 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700717 sg[i].length, offset);
718 kunmap_atomic(dest, KM_SOFTIRQ0);
719 if (rc == -EAGAIN)
720 /* continue with the next SKB/PDU */
721 return rc;
722 if (!rc) {
723 if (conn->datadgst_en) {
724 if (!offset)
Mike Christie5bb0b552006-04-06 21:26:46 -0500725 crypto_digest_update(
726 tcp_conn->data_rx_tfm,
727 &sg[i], 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700728 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500729 partial_sg_digest_update(tcp_conn,
730 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700731 sg[i].offset + offset,
732 sg[i].length - offset);
733 }
734 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500735 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700736 }
737
738 if (!ctask->data_count) {
739 if (rc && conn->datadgst_en)
740 /*
741 * data-in is complete, but buffer not...
742 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500743 partial_sg_digest_update(tcp_conn, &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700744 sg[i].offset, sg[i].length-rc);
745 rc = 0;
746 break;
747 }
748
Mike Christie5bb0b552006-04-06 21:26:46 -0500749 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700750 return -EAGAIN;
751 }
752 BUG_ON(ctask->data_count);
753
754done:
755 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500756 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Mike Christieb6c395e2006-07-24 15:47:15 -0500757 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
758 (long)sc, sc->result, ctask->itt,
759 tcp_conn->in.hdr->flags);
Mike Christie5bb0b552006-04-06 21:26:46 -0500760 spin_lock(&conn->session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500761 iscsi_tcp_cleanup_ctask(conn, ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500762 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
763 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700764 }
765
766 return rc;
767}
768
769static int
770iscsi_data_recv(struct iscsi_conn *conn)
771{
Mike Christie5bb0b552006-04-06 21:26:46 -0500772 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
773 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700774
Mike Christie5bb0b552006-04-06 21:26:46 -0500775 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
776 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700777 case ISCSI_OP_SCSI_DATA_IN:
778 rc = iscsi_scsi_data_in(conn);
779 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500780 case ISCSI_OP_SCSI_CMD_RSP:
781 spin_lock(&conn->session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500782 iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500783 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700784 case ISCSI_OP_TEXT_RSP:
785 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500786 case ISCSI_OP_NOOP_IN:
787 case ISCSI_OP_ASYNC_EVENT:
788 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700789 /*
790 * Collect data segment to the connection's data
791 * placeholder
792 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500793 if (iscsi_tcp_copy(tcp_conn)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700794 rc = -EAGAIN;
795 goto exit;
796 }
797
Mike Christie5bb0b552006-04-06 21:26:46 -0500798 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, tcp_conn->data,
799 tcp_conn->in.datalen);
800 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
801 iscsi_recv_digest_update(tcp_conn, tcp_conn->data,
802 tcp_conn->in.datalen);
803 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700804 default:
805 BUG_ON(1);
806 }
807exit:
808 return rc;
809}
810
811/**
812 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
813 * @rd_desc: read descriptor
814 * @skb: socket buffer
815 * @offset: offset in skb
816 * @len: skb->len - offset
817 **/
818static int
819iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
820 unsigned int offset, size_t len)
821{
822 int rc;
823 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500824 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700825 int processed;
826 char pad[ISCSI_PAD_LEN];
827 struct scatterlist sg;
828
829 /*
830 * Save current SKB and its offset in the corresponding
831 * connection context.
832 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500833 tcp_conn->in.copy = skb->len - offset;
834 tcp_conn->in.offset = offset;
835 tcp_conn->in.skb = skb;
836 tcp_conn->in.len = tcp_conn->in.copy;
837 BUG_ON(tcp_conn->in.copy <= 0);
838 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700839
840more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500841 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700842 rc = 0;
843
844 if (unlikely(conn->suspend_rx)) {
845 debug_tcp("conn %d Rx suspended!\n", conn->id);
846 return 0;
847 }
848
Mike Christie5bb0b552006-04-06 21:26:46 -0500849 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
850 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
851 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700852 if (rc) {
853 if (rc == -EAGAIN)
854 goto nomore;
855 else {
Mike Christied82967c2006-07-24 15:47:11 -0500856 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700857 return 0;
858 }
859 }
860
861 /*
862 * Verify and process incoming PDU header.
863 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500864 rc = iscsi_tcp_hdr_recv(conn);
865 if (!rc && tcp_conn->in.datalen) {
Mike Christie8a47cd32005-11-30 02:27:19 -0600866 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500867 BUG_ON(!tcp_conn->data_rx_tfm);
868 crypto_digest_init(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -0700869 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500870 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700871 } else if (rc) {
Mike Christied82967c2006-07-24 15:47:11 -0500872 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700873 return 0;
874 }
875 }
876
Mike Christie5bb0b552006-04-06 21:26:46 -0500877 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
Mike Christief6cfba12005-11-29 23:12:57 -0600878 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500879
Alex Aizman7ba24712005-08-04 19:30:08 -0700880 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500881 tcp_conn->in.offset, tcp_conn->in.copy);
882 skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christief6cfba12005-11-29 23:12:57 -0600883 &recv_digest, 4);
Mike Christie5bb0b552006-04-06 21:26:46 -0500884 tcp_conn->in.offset += 4;
885 tcp_conn->in.copy -= 4;
886 if (recv_digest != tcp_conn->in.datadgst) {
Mike Christief6cfba12005-11-29 23:12:57 -0600887 debug_tcp("iscsi_tcp: data digest error!"
888 "0x%x != 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500889 tcp_conn->in.datadgst);
Mike Christief6cfba12005-11-29 23:12:57 -0600890 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
891 return 0;
892 } else {
893 debug_tcp("iscsi_tcp: data digest match!"
894 "0x%x == 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500895 tcp_conn->in.datadgst);
896 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700897 }
898 }
899
Mike Christie5bb0b552006-04-06 21:26:46 -0500900 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
901 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700902
903 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500904 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700905
906 rc = iscsi_data_recv(conn);
907 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500908 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700909 goto again;
Mike Christied82967c2006-07-24 15:47:11 -0500910 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700911 return 0;
912 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500913 tcp_conn->in.copy -= tcp_conn->in.padding;
914 tcp_conn->in.offset += tcp_conn->in.padding;
Mike Christie8a47cd32005-11-30 02:27:19 -0600915 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500916 if (tcp_conn->in.padding) {
917 debug_tcp("padding -> %d\n",
918 tcp_conn->in.padding);
919 memset(pad, 0, tcp_conn->in.padding);
920 sg_init_one(&sg, pad, tcp_conn->in.padding);
921 crypto_digest_update(tcp_conn->data_rx_tfm,
922 &sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700923 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500924 crypto_digest_final(tcp_conn->data_rx_tfm,
925 (u8 *) & tcp_conn->in.datadgst);
926 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
927 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700928 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500929 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700930 }
931
932 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500933 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
934 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700935
Mike Christie5bb0b552006-04-06 21:26:46 -0500936 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700937 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500938 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700939 goto more;
940 }
941
942nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500943 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700944 BUG_ON(processed == 0);
945 return processed;
946
947again:
Mike Christie5bb0b552006-04-06 21:26:46 -0500948 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700949 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
950 processed, (int)len, (int)rd_desc->count);
951 BUG_ON(processed == 0);
952 BUG_ON(processed > len);
953
954 conn->rxdata_octets += processed;
955 return processed;
956}
957
958static void
959iscsi_tcp_data_ready(struct sock *sk, int flag)
960{
961 struct iscsi_conn *conn = sk->sk_user_data;
962 read_descriptor_t rd_desc;
963
964 read_lock(&sk->sk_callback_lock);
965
Mike Christie665b44a2006-05-02 19:46:49 -0500966 /*
967 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
968 * We set count to 1 because we want the network layer to
969 * hand us all the skbs that are available. iscsi_tcp_data_recv
970 * handled pdus that cross buffers or pdus that still need data.
971 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700972 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -0500973 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -0700974 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
975
976 read_unlock(&sk->sk_callback_lock);
977}
978
979static void
980iscsi_tcp_state_change(struct sock *sk)
981{
Mike Christie5bb0b552006-04-06 21:26:46 -0500982 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -0700983 struct iscsi_conn *conn;
984 struct iscsi_session *session;
985 void (*old_state_change)(struct sock *);
986
987 read_lock(&sk->sk_callback_lock);
988
989 conn = (struct iscsi_conn*)sk->sk_user_data;
990 session = conn->session;
991
Mike Christiee6273992005-11-29 23:12:49 -0600992 if ((sk->sk_state == TCP_CLOSE_WAIT ||
993 sk->sk_state == TCP_CLOSE) &&
994 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700995 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
996 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
997 }
998
Mike Christie5bb0b552006-04-06 21:26:46 -0500999 tcp_conn = conn->dd_data;
1000 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001001
1002 read_unlock(&sk->sk_callback_lock);
1003
1004 old_state_change(sk);
1005}
1006
1007/**
1008 * iscsi_write_space - Called when more output buffer space is available
1009 * @sk: socket space is available for
1010 **/
1011static void
1012iscsi_write_space(struct sock *sk)
1013{
1014 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001015 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1016
1017 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001018 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001019 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001020}
1021
1022static void
1023iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1024{
Mike Christie5bb0b552006-04-06 21:26:46 -05001025 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1026 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001027
1028 /* assign new callbacks */
1029 write_lock_bh(&sk->sk_callback_lock);
1030 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001031 tcp_conn->old_data_ready = sk->sk_data_ready;
1032 tcp_conn->old_state_change = sk->sk_state_change;
1033 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001034 sk->sk_data_ready = iscsi_tcp_data_ready;
1035 sk->sk_state_change = iscsi_tcp_state_change;
1036 sk->sk_write_space = iscsi_write_space;
1037 write_unlock_bh(&sk->sk_callback_lock);
1038}
1039
1040static void
1041iscsi_conn_restore_callbacks(struct iscsi_conn *conn)
1042{
Mike Christie5bb0b552006-04-06 21:26:46 -05001043 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1044 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001045
1046 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1047 write_lock_bh(&sk->sk_callback_lock);
1048 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001049 sk->sk_data_ready = tcp_conn->old_data_ready;
1050 sk->sk_state_change = tcp_conn->old_state_change;
1051 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001052 sk->sk_no_check = 0;
1053 write_unlock_bh(&sk->sk_callback_lock);
1054}
1055
1056/**
1057 * iscsi_send - generic send routine
1058 * @sk: kernel's socket
1059 * @buf: buffer to write from
1060 * @size: actual size to write
1061 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001062 */
1063static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001064iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001065{
Mike Christie5bb0b552006-04-06 21:26:46 -05001066 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1067 struct socket *sk = tcp_conn->sock;
Mike Christie3219e522006-05-30 00:37:28 -05001068 int offset = buf->sg.offset + buf->sent, res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001069
Mike Christie7cae5152006-01-13 18:05:47 -06001070 /*
1071 * if we got use_sg=0 or are sending something we kmallocd
1072 * then we did not have to do kmap (kmap returns page_address)
1073 *
1074 * if we got use_sg > 0, but had to drop down, we do not
1075 * set clustering so this should only happen for that
1076 * slab case.
1077 */
1078 if (buf->use_sendmsg)
Mike Christie3219e522006-05-30 00:37:28 -05001079 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
Mike Christie7cae5152006-01-13 18:05:47 -06001080 else
Mike Christie3219e522006-05-30 00:37:28 -05001081 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1082
1083 if (res >= 0) {
1084 conn->txdata_octets += res;
1085 buf->sent += res;
1086 return res;
1087 }
1088
1089 tcp_conn->sendpage_failures_cnt++;
1090 if (res == -EAGAIN)
1091 res = -ENOBUFS;
1092 else
1093 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1094 return res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001095}
1096
1097/**
1098 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1099 * @conn: iscsi connection
1100 * @buf: buffer to write from
1101 * @datalen: lenght of data to be sent after the header
1102 *
1103 * Notes:
1104 * (Tx, Fast Path)
1105 **/
1106static inline int
1107iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1108{
Alex Aizman7ba24712005-08-04 19:30:08 -07001109 int flags = 0; /* MSG_DONTWAIT; */
1110 int res, size;
1111
1112 size = buf->sg.length - buf->sent;
1113 BUG_ON(buf->sent + size > buf->sg.length);
1114 if (buf->sent + size != buf->sg.length || datalen)
1115 flags |= MSG_MORE;
1116
FUJITA Tomonori56851692006-01-13 18:05:44 -06001117 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001118 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1119 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001120 if (size != res)
1121 return -EAGAIN;
1122 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001123 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001124
1125 return res;
1126}
1127
1128/**
1129 * iscsi_sendpage - send one page of iSCSI Data-Out.
1130 * @conn: iscsi connection
1131 * @buf: buffer to write from
1132 * @count: remaining data
1133 * @sent: number of bytes sent
1134 *
1135 * Notes:
1136 * (Tx, Fast Path)
1137 **/
1138static inline int
1139iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1140 int *count, int *sent)
1141{
Alex Aizman7ba24712005-08-04 19:30:08 -07001142 int flags = 0; /* MSG_DONTWAIT; */
1143 int res, size;
1144
1145 size = buf->sg.length - buf->sent;
1146 BUG_ON(buf->sent + size > buf->sg.length);
1147 if (size > *count)
1148 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001149 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001150 flags |= MSG_MORE;
1151
FUJITA Tomonori56851692006-01-13 18:05:44 -06001152 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001153 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1154 size, buf->sent, *count, *sent, res);
1155 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001156 *count -= res;
1157 *sent += res;
1158 if (size != res)
1159 return -EAGAIN;
1160 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001161 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001162
1163 return res;
1164}
1165
1166static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001167iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
1168 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001169{
Mike Christie5bb0b552006-04-06 21:26:46 -05001170 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1171
1172 BUG_ON(!tcp_conn->data_tx_tfm);
1173 crypto_digest_init(tcp_conn->data_tx_tfm);
1174 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001175}
1176
Arjan van de Ven858119e2006-01-14 13:20:43 -08001177static int
Alex Aizman7ba24712005-08-04 19:30:08 -07001178iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1179 struct iscsi_buf *buf, uint32_t *digest, int final)
1180{
Mike Christie5bb0b552006-04-06 21:26:46 -05001181 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1182 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001183 int rc = 0;
1184 int sent = 0;
1185
1186 if (final)
Mike Christie5bb0b552006-04-06 21:26:46 -05001187 crypto_digest_final(tcp_conn->data_tx_tfm, (u8*)digest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001188
Mike Christie6e458cc2006-05-18 20:31:31 -05001189 iscsi_buf_init_iov(buf, (char*)digest, 4);
Mike Christie5bb0b552006-04-06 21:26:46 -05001190 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001191 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001192 tcp_ctask->datadigest = *digest;
1193 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
Alex Aizman7ba24712005-08-04 19:30:08 -07001194 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001195 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001196 return rc;
1197}
1198
1199/**
1200 * iscsi_solicit_data_cont - initialize next Data-Out
1201 * @conn: iscsi connection
1202 * @ctask: scsi command task
1203 * @r2t: R2T info
1204 * @left: bytes left to transfer
1205 *
1206 * Notes:
1207 * Initialize next Data-Out within this R2T sequence and continue
1208 * to process next Scatter-Gather element(if any) of this SCSI command.
1209 *
1210 * Called under connection lock.
1211 **/
1212static void
1213iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1214 struct iscsi_r2t_info *r2t, int left)
1215{
Mike Christie5bb0b552006-04-06 21:26:46 -05001216 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001217 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001218 struct scsi_cmnd *sc = ctask->sc;
1219 int new_offset;
1220
Mike Christieffbfe922006-05-18 20:31:36 -05001221 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001222 memset(hdr, 0, sizeof(struct iscsi_data));
1223 hdr->ttt = r2t->ttt;
1224 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1225 r2t->solicit_datasn++;
1226 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001227 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1228 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001229 hdr->exp_statsn = r2t->exp_statsn;
1230 new_offset = r2t->data_offset + r2t->sent;
1231 hdr->offset = cpu_to_be32(new_offset);
1232 if (left > conn->max_xmit_dlength) {
1233 hton24(hdr->dlength, conn->max_xmit_dlength);
1234 r2t->data_count = conn->max_xmit_dlength;
1235 } else {
1236 hton24(hdr->dlength, left);
1237 r2t->data_count = left;
1238 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1239 }
1240 conn->dataout_pdus_cnt++;
1241
Mike Christie6e458cc2006-05-18 20:31:31 -05001242 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001243 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001244
Alex Aizman7ba24712005-08-04 19:30:08 -07001245 if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001246 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
Alex Aizman7ba24712005-08-04 19:30:08 -07001247 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1248 r2t->sg += 1;
1249 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001250 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -07001251 (char*)sc->request_buffer + new_offset,
1252 r2t->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001253}
1254
1255static void
1256iscsi_unsolicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1257{
Mike Christie5bb0b552006-04-06 21:26:46 -05001258 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001259 struct iscsi_data_task *dtask;
1260
Mike Christieffbfe922006-05-18 20:31:36 -05001261 dtask = tcp_ctask->dtask = &tcp_ctask->unsol_dtask;
Mike Christie5bb0b552006-04-06 21:26:46 -05001262 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr,
1263 tcp_ctask->r2t_data_count);
Mike Christie6e458cc2006-05-18 20:31:31 -05001264 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001265 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001266}
1267
1268/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001269 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001270 * @conn: iscsi connection
1271 * @ctask: scsi command task
1272 * @sc: scsi command
1273 **/
1274static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001275iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001276{
Mike Christie5bb0b552006-04-06 21:26:46 -05001277 struct scsi_cmnd *sc = ctask->sc;
1278 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001279
Mike Christie5bb0b552006-04-06 21:26:46 -05001280 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Alex Aizman7ba24712005-08-04 19:30:08 -07001281
Mike Christie5bb0b552006-04-06 21:26:46 -05001282 tcp_ctask->sent = 0;
1283 tcp_ctask->sg_count = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001284
1285 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001286 tcp_ctask->xmstate = XMSTATE_W_HDR;
1287 tcp_ctask->exp_r2tsn = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001288 BUG_ON(ctask->total_length == 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05001289
Alex Aizman7ba24712005-08-04 19:30:08 -07001290 if (sc->use_sg) {
1291 struct scatterlist *sg = sc->request_buffer;
1292
Mike Christie5bb0b552006-04-06 21:26:46 -05001293 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1294 &sg[tcp_ctask->sg_count++]);
1295 tcp_ctask->sg = sg;
1296 tcp_ctask->bad_sg = sg + sc->use_sg;
Alex Aizman7ba24712005-08-04 19:30:08 -07001297 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001298 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1299 sc->request_buffer,
1300 sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001301
Mike Christie5bb0b552006-04-06 21:26:46 -05001302 if (ctask->imm_count)
1303 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001304
Mike Christie5bb0b552006-04-06 21:26:46 -05001305 tcp_ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
1306 if (tcp_ctask->pad_count) {
1307 tcp_ctask->pad_count = ISCSI_PAD_LEN -
1308 tcp_ctask->pad_count;
1309 debug_scsi("write padding %d bytes\n",
1310 tcp_ctask->pad_count);
1311 tcp_ctask->xmstate |= XMSTATE_W_PAD;
1312 }
1313
1314 if (ctask->unsol_count)
1315 tcp_ctask->xmstate |= XMSTATE_UNS_HDR |
1316 XMSTATE_UNS_INIT;
1317 tcp_ctask->r2t_data_count = ctask->total_length -
Alex Aizman7ba24712005-08-04 19:30:08 -07001318 ctask->imm_count -
1319 ctask->unsol_count;
1320
Mike Christieb6c395e2006-07-24 15:47:15 -05001321 debug_scsi("cmd [itt 0x%x total %d imm %d imm_data %d "
Alex Aizman7ba24712005-08-04 19:30:08 -07001322 "r2t_data %d]\n",
1323 ctask->itt, ctask->total_length, ctask->imm_count,
Mike Christie5bb0b552006-04-06 21:26:46 -05001324 ctask->unsol_count, tcp_ctask->r2t_data_count);
1325 } else
1326 tcp_ctask->xmstate = XMSTATE_R_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001327
Mike Christie6e458cc2006-05-18 20:31:31 -05001328 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001329 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001330}
1331
1332/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001333 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001334 * @conn: iscsi connection
1335 * @mtask: task management task
1336 *
1337 * Notes:
1338 * The function can return -EAGAIN in which case caller must
1339 * call it again later, or recover. '0' return code means successful
1340 * xmit.
1341 *
1342 * Management xmit state machine consists of two states:
1343 * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
1344 * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1345 **/
1346static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001347iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001348{
Mike Christie5bb0b552006-04-06 21:26:46 -05001349 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001350 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001351
1352 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001353 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001354
Mike Christie5bb0b552006-04-06 21:26:46 -05001355 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
1356 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001357 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001358 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christieaf973482005-09-12 21:01:32 -05001359 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001360 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001361 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001362 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1363 (u8*)tcp_mtask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001364 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1365 mtask->data_count);
1366 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001367 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001368 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001369 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001370 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001371 }
1372 }
1373
Mike Christie5bb0b552006-04-06 21:26:46 -05001374 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001375 BUG_ON(!mtask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001376 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001377 /* FIXME: implement.
1378 * Virtual buffer could be spreaded across multiple pages...
1379 */
1380 do {
Mike Christie3219e522006-05-30 00:37:28 -05001381 int rc;
1382
1383 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1384 &mtask->data_count, &tcp_mtask->sent);
1385 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001386 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001387 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001388 }
1389 } while (mtask->data_count);
1390 }
1391
Mike Christie5bb0b552006-04-06 21:26:46 -05001392 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
1393 if (mtask->hdr->itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1394 struct iscsi_session *session = conn->session;
1395
1396 spin_lock_bh(&session->lock);
1397 list_del(&conn->mtask->running);
1398 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1399 sizeof(void*));
1400 spin_unlock_bh(&session->lock);
1401 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001402 return 0;
1403}
1404
1405static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -05001406handle_xmstate_r_hdr(struct iscsi_conn *conn,
1407 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001408{
Mike Christie3219e522006-05-30 00:37:28 -05001409 int rc;
1410
Mike Christie5bb0b552006-04-06 21:26:46 -05001411 tcp_ctask->xmstate &= ~XMSTATE_R_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001412 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001413 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1414 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001415 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, 0);
1416 if (!rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001417 BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE);
Alex Aizman7ba24712005-08-04 19:30:08 -07001418 return 0; /* wait for Data-In */
1419 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001420 tcp_ctask->xmstate |= XMSTATE_R_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001421 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001422}
1423
1424static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -05001425handle_xmstate_w_hdr(struct iscsi_conn *conn,
1426 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001427{
Mike Christie5bb0b552006-04-06 21:26:46 -05001428 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001429 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001430
1431 tcp_ctask->xmstate &= ~XMSTATE_W_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001432 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001433 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1434 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001435 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1436 if (rc)
Mike Christie5bb0b552006-04-06 21:26:46 -05001437 tcp_ctask->xmstate |= XMSTATE_W_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001438 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001439}
1440
1441static inline int
1442handle_xmstate_data_digest(struct iscsi_conn *conn,
1443 struct iscsi_cmd_task *ctask)
1444{
Mike Christie5bb0b552006-04-06 21:26:46 -05001445 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001446 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001447
1448 tcp_ctask->xmstate &= ~XMSTATE_DATA_DIGEST;
1449 debug_tcp("resent data digest 0x%x\n", tcp_ctask->datadigest);
Mike Christie3219e522006-05-30 00:37:28 -05001450 rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1451 &tcp_ctask->datadigest, 0);
1452 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001453 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
Alex Aizman7ba24712005-08-04 19:30:08 -07001454 debug_tcp("resent data digest 0x%x fail!\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001455 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001456 }
Mike Christie3219e522006-05-30 00:37:28 -05001457
1458 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001459}
1460
1461static inline int
1462handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1463{
Mike Christie5bb0b552006-04-06 21:26:46 -05001464 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1465 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001466 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001467
Alex Aizman7ba24712005-08-04 19:30:08 -07001468 BUG_ON(!ctask->imm_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001469 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001470
1471 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001472 iscsi_data_digest_init(tcp_conn, ctask);
1473 tcp_ctask->immdigest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001474 }
1475
1476 for (;;) {
Mike Christie3219e522006-05-30 00:37:28 -05001477 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1478 &ctask->imm_count, &tcp_ctask->sent);
1479 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001480 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001481 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001482 crypto_digest_final(tcp_conn->data_tx_tfm,
1483 (u8*)&tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001484 debug_tcp("tx imm sendpage fail 0x%x\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001485 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001486 }
Mike Christie3219e522006-05-30 00:37:28 -05001487 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001488 }
1489 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001490 crypto_digest_update(tcp_conn->data_tx_tfm,
1491 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001492
1493 if (!ctask->imm_count)
1494 break;
Mike Christie5bb0b552006-04-06 21:26:46 -05001495 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1496 &tcp_ctask->sg[tcp_ctask->sg_count++]);
Alex Aizman7ba24712005-08-04 19:30:08 -07001497 }
1498
Mike Christie5bb0b552006-04-06 21:26:46 -05001499 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
Mike Christie3219e522006-05-30 00:37:28 -05001500 rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1501 &tcp_ctask->immdigest, 1);
1502 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001503 debug_tcp("sending imm digest 0x%x fail!\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001504 tcp_ctask->immdigest);
Mike Christie3219e522006-05-30 00:37:28 -05001505 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001506 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001507 debug_tcp("sending imm digest 0x%x\n", tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001508 }
1509
1510 return 0;
1511}
1512
1513static inline int
1514handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1515{
Mike Christie5bb0b552006-04-06 21:26:46 -05001516 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001517 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001518 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001519
Mike Christie5bb0b552006-04-06 21:26:46 -05001520 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1521 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001522 iscsi_unsolicit_data_init(conn, ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -05001523 dtask = tcp_ctask->dtask;
Mike Christieaf973482005-09-12 21:01:32 -05001524 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001525 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001526 (u8*)dtask->hdrext);
Mike Christie5bb0b552006-04-06 21:26:46 -05001527 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001528 }
Mike Christie3219e522006-05-30 00:37:28 -05001529
1530 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1531 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001532 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1533 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001534 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001535 }
1536
1537 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001538 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001539 return 0;
1540}
1541
1542static inline int
1543handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1544{
Mike Christie5bb0b552006-04-06 21:26:46 -05001545 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1546 struct iscsi_data_task *dtask = tcp_ctask->dtask;
1547 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001548 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001549
1550 BUG_ON(!ctask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001551 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001552
1553 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001554 iscsi_data_digest_init(tcp_conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001555 dtask->digest = 0;
1556 }
1557
1558 for (;;) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001559 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001560
Mike Christie3219e522006-05-30 00:37:28 -05001561 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1562 &ctask->data_count, &tcp_ctask->sent);
1563 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001564 ctask->unsol_count -= tcp_ctask->sent - start;
1565 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001566 /* will continue with this ctask later.. */
1567 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001568 crypto_digest_final(tcp_conn->data_tx_tfm,
Alex Aizman7ba24712005-08-04 19:30:08 -07001569 (u8 *)&dtask->digest);
1570 debug_tcp("tx uns data fail 0x%x\n",
1571 dtask->digest);
1572 }
Mike Christie3219e522006-05-30 00:37:28 -05001573 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001574 }
1575
Mike Christie5bb0b552006-04-06 21:26:46 -05001576 BUG_ON(tcp_ctask->sent > ctask->total_length);
1577 ctask->unsol_count -= tcp_ctask->sent - start;
Alex Aizman7ba24712005-08-04 19:30:08 -07001578
1579 /*
1580 * XXX:we may run here with un-initial sendbuf.
1581 * so pass it
1582 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001583 if (conn->datadgst_en && tcp_ctask->sent - start > 0)
1584 crypto_digest_update(tcp_conn->data_tx_tfm,
1585 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001586
1587 if (!ctask->data_count)
1588 break;
Mike Christie5bb0b552006-04-06 21:26:46 -05001589 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1590 &tcp_ctask->sg[tcp_ctask->sg_count++]);
Alex Aizman7ba24712005-08-04 19:30:08 -07001591 }
1592 BUG_ON(ctask->unsol_count < 0);
1593
1594 /*
1595 * Done with the Data-Out. Next, check if we need
1596 * to send another unsolicited Data-Out.
1597 */
1598 if (ctask->unsol_count) {
1599 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001600 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001601 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001602 &dtask->digest, 1);
1603 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001604 debug_tcp("send uns digest 0x%x fail\n",
1605 dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001606 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001607 }
1608 debug_tcp("sending uns digest 0x%x, more uns\n",
1609 dtask->digest);
1610 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001611 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001612 return 1;
1613 }
1614
Mike Christie5bb0b552006-04-06 21:26:46 -05001615 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
Mike Christie3219e522006-05-30 00:37:28 -05001616 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001617 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001618 &dtask->digest, 1);
1619 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001620 debug_tcp("send last uns digest 0x%x fail\n",
1621 dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001622 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001623 }
1624 debug_tcp("sending uns digest 0x%x\n",dtask->digest);
1625 }
1626
1627 return 0;
1628}
1629
1630static inline int
1631handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1632{
1633 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001634 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1635 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1636 struct iscsi_r2t_info *r2t = tcp_ctask->r2t;
Mike Christieffbfe922006-05-18 20:31:36 -05001637 struct iscsi_data_task *dtask = &r2t->dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001638 int left, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001639
Mike Christie5bb0b552006-04-06 21:26:46 -05001640 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1641 tcp_ctask->dtask = dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001642
1643 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001644 iscsi_data_digest_init(tcp_conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001645 dtask->digest = 0;
1646 }
1647solicit_again:
1648 /*
Mike Christieb6c395e2006-07-24 15:47:15 -05001649 * send Data-Out within this R2T sequence.
Alex Aizman7ba24712005-08-04 19:30:08 -07001650 */
1651 if (!r2t->data_count)
1652 goto data_out_done;
1653
Mike Christie3219e522006-05-30 00:37:28 -05001654 rc = iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent);
1655 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001656 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001657 /* will continue with this ctask later.. */
1658 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001659 crypto_digest_final(tcp_conn->data_tx_tfm,
Alex Aizman7ba24712005-08-04 19:30:08 -07001660 (u8 *)&dtask->digest);
1661 debug_tcp("r2t data send fail 0x%x\n", dtask->digest);
1662 }
Mike Christie3219e522006-05-30 00:37:28 -05001663 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001664 }
1665
1666 BUG_ON(r2t->data_count < 0);
1667 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001668 crypto_digest_update(tcp_conn->data_tx_tfm, &r2t->sendbuf.sg,
1669 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001670
1671 if (r2t->data_count) {
1672 BUG_ON(ctask->sc->use_sg == 0);
1673 if (!iscsi_buf_left(&r2t->sendbuf)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001674 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
Alex Aizman7ba24712005-08-04 19:30:08 -07001675 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1676 r2t->sg += 1;
1677 }
1678 goto solicit_again;
1679 }
1680
1681data_out_done:
1682 /*
1683 * Done with this Data-Out. Next, check if we have
1684 * to send another Data-Out for this R2T.
1685 */
1686 BUG_ON(r2t->data_length - r2t->sent < 0);
1687 left = r2t->data_length - r2t->sent;
1688 if (left) {
1689 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001690 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001691 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001692 &dtask->digest, 1);
1693 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001694 debug_tcp("send r2t data digest 0x%x"
1695 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001696 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001697 }
1698 debug_tcp("r2t data send digest 0x%x\n",
1699 dtask->digest);
1700 }
1701 iscsi_solicit_data_cont(conn, ctask, r2t, left);
Mike Christie5bb0b552006-04-06 21:26:46 -05001702 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1703 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001704 return 1;
1705 }
1706
1707 /*
1708 * Done with this R2T. Check if there are more
1709 * outstanding R2Ts ready to be processed.
1710 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001711 BUG_ON(tcp_ctask->r2t_data_count - r2t->data_length < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -07001712 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001713 rc = iscsi_digest_final_send(conn, ctask, &dtask->digestbuf,
1714 &dtask->digest, 1);
1715 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001716 debug_tcp("send last r2t data digest 0x%x"
1717 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001718 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001719 }
1720 debug_tcp("r2t done dout digest 0x%x\n", dtask->digest);
1721 }
1722
Mike Christie5bb0b552006-04-06 21:26:46 -05001723 tcp_ctask->r2t_data_count -= r2t->data_length;
1724 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001725 spin_lock_bh(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05001726 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -07001727 spin_unlock_bh(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05001728 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
1729 tcp_ctask->r2t = r2t;
1730 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1731 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001732 return 1;
1733 }
1734
1735 return 0;
1736}
1737
1738static inline int
1739handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1740{
Mike Christie5bb0b552006-04-06 21:26:46 -05001741 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1742 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1743 struct iscsi_data_task *dtask = tcp_ctask->dtask;
Mike Christieb6c395e2006-07-24 15:47:15 -05001744 int sent = 0, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001745
Mike Christie5bb0b552006-04-06 21:26:46 -05001746 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
Mike Christie6e458cc2006-05-18 20:31:31 -05001747 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
Mike Christie5bb0b552006-04-06 21:26:46 -05001748 tcp_ctask->pad_count);
Mike Christie3219e522006-05-30 00:37:28 -05001749 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1750 &sent);
1751 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001752 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Mike Christie3219e522006-05-30 00:37:28 -05001753 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001754 }
1755
1756 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001757 crypto_digest_update(tcp_conn->data_tx_tfm,
1758 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001759 /* imm data? */
1760 if (!dtask) {
Mike Christie3219e522006-05-30 00:37:28 -05001761 rc = iscsi_digest_final_send(conn, ctask,
Mike Christie5bb0b552006-04-06 21:26:46 -05001762 &tcp_ctask->immbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001763 &tcp_ctask->immdigest, 1);
1764 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001765 debug_tcp("send padding digest 0x%x"
Mike Christie5bb0b552006-04-06 21:26:46 -05001766 "fail!\n", tcp_ctask->immdigest);
Mike Christie3219e522006-05-30 00:37:28 -05001767 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001768 }
1769 debug_tcp("done with padding, digest 0x%x\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001770 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001771 } else {
Mike Christie3219e522006-05-30 00:37:28 -05001772 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001773 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001774 &dtask->digest, 1);
1775 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001776 debug_tcp("send padding digest 0x%x"
1777 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001778 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001779 }
1780 debug_tcp("done with padding, digest 0x%x\n",
1781 dtask->digest);
1782 }
1783 }
1784
1785 return 0;
1786}
1787
1788static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001789iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001790{
Mike Christie5bb0b552006-04-06 21:26:46 -05001791 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001792 int rc = 0;
1793
1794 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001795 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001796
1797 /*
1798 * serialize with TMF AbortTask
1799 */
1800 if (ctask->mtask)
1801 return rc;
1802
Mike Christie3219e522006-05-30 00:37:28 -05001803 if (tcp_ctask->xmstate & XMSTATE_R_HDR)
1804 return handle_xmstate_r_hdr(conn, tcp_ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001805
Mike Christie5bb0b552006-04-06 21:26:46 -05001806 if (tcp_ctask->xmstate & XMSTATE_W_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001807 rc = handle_xmstate_w_hdr(conn, ctask);
1808 if (rc)
1809 return rc;
1810 }
1811
1812 /* XXX: for data digest xmit recover */
Mike Christie5bb0b552006-04-06 21:26:46 -05001813 if (tcp_ctask->xmstate & XMSTATE_DATA_DIGEST) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001814 rc = handle_xmstate_data_digest(conn, ctask);
1815 if (rc)
1816 return rc;
1817 }
1818
Mike Christie5bb0b552006-04-06 21:26:46 -05001819 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001820 rc = handle_xmstate_imm_data(conn, ctask);
1821 if (rc)
1822 return rc;
1823 }
1824
Mike Christie5bb0b552006-04-06 21:26:46 -05001825 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001826 BUG_ON(!ctask->unsol_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001827 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001828unsolicit_head_again:
1829 rc = handle_xmstate_uns_hdr(conn, ctask);
1830 if (rc)
1831 return rc;
1832 }
1833
Mike Christie5bb0b552006-04-06 21:26:46 -05001834 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001835 rc = handle_xmstate_uns_data(conn, ctask);
1836 if (rc == 1)
1837 goto unsolicit_head_again;
1838 else if (rc)
1839 return rc;
1840 goto done;
1841 }
1842
Mike Christie5bb0b552006-04-06 21:26:46 -05001843 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001844 struct iscsi_r2t_info *r2t;
1845
Mike Christie5bb0b552006-04-06 21:26:46 -05001846 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1847 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1848 if (!tcp_ctask->r2t)
1849 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
Alex Aizman7ba24712005-08-04 19:30:08 -07001850 sizeof(void*));
1851solicit_head_again:
Mike Christie5bb0b552006-04-06 21:26:46 -05001852 r2t = tcp_ctask->r2t;
Mike Christieaf973482005-09-12 21:01:32 -05001853 if (conn->hdrdgst_en)
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001854 iscsi_hdr_digest(conn, &r2t->headbuf,
Mike Christieffbfe922006-05-18 20:31:36 -05001855 (u8*)r2t->dtask.hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001856 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
1857 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001858 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1859 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001860 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001861 }
1862
1863 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1864 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1865 r2t->sent);
1866 }
1867
Mike Christie5bb0b552006-04-06 21:26:46 -05001868 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001869 rc = handle_xmstate_sol_data(conn, ctask);
1870 if (rc == 1)
1871 goto solicit_head_again;
1872 if (rc)
1873 return rc;
1874 }
1875
1876done:
1877 /*
1878 * Last thing to check is whether we need to send write
1879 * padding. Note that we check for xmstate equality, not just the bit.
1880 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001881 if (tcp_ctask->xmstate == XMSTATE_W_PAD)
Alex Aizman7ba24712005-08-04 19:30:08 -07001882 rc = handle_xmstate_w_pad(conn, ctask);
1883
1884 return rc;
1885}
1886
Mike Christie7b8631b2006-01-13 18:05:50 -06001887static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001888iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001889{
Mike Christie7b8631b2006-01-13 18:05:50 -06001890 struct iscsi_conn *conn;
1891 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001892 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001893
Mike Christie5bb0b552006-04-06 21:26:46 -05001894 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001895 if (!cls_conn)
1896 return NULL;
1897 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001898 /*
1899 * due to strange issues with iser these are not set
1900 * in iscsi_conn_setup
1901 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001902 conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
1903
Mike Christie5bb0b552006-04-06 21:26:46 -05001904 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1905 if (!tcp_conn)
1906 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001907
Mike Christie5bb0b552006-04-06 21:26:46 -05001908 conn->dd_data = tcp_conn;
1909 tcp_conn->iscsi_conn = conn;
1910 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1911 /* initial operational parameters */
1912 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
1913 tcp_conn->data_size = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
Alex Aizman7ba24712005-08-04 19:30:08 -07001914
1915 /* allocate initial PDU receive place holder */
Mike Christie5bb0b552006-04-06 21:26:46 -05001916 if (tcp_conn->data_size <= PAGE_SIZE)
1917 tcp_conn->data = kmalloc(tcp_conn->data_size, GFP_KERNEL);
Alex Aizman7ba24712005-08-04 19:30:08 -07001918 else
Mike Christie5bb0b552006-04-06 21:26:46 -05001919 tcp_conn->data = (void*)__get_free_pages(GFP_KERNEL,
1920 get_order(tcp_conn->data_size));
1921 if (!tcp_conn->data)
Alex Aizman7ba24712005-08-04 19:30:08 -07001922 goto max_recv_dlenght_alloc_fail;
1923
Mike Christie7b8631b2006-01-13 18:05:50 -06001924 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001925
1926max_recv_dlenght_alloc_fail:
Mike Christie5bb0b552006-04-06 21:26:46 -05001927 kfree(tcp_conn);
1928tcp_conn_alloc_fail:
1929 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001930 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001931}
1932
1933static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001934iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001935{
Mike Christie7b8631b2006-01-13 18:05:50 -06001936 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001937 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1938 int digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001939
Mike Christie5bb0b552006-04-06 21:26:46 -05001940 if (conn->hdrdgst_en || conn->datadgst_en)
1941 digest = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001942
Mike Christie5bb0b552006-04-06 21:26:46 -05001943 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001944
Mike Christie5bb0b552006-04-06 21:26:46 -05001945 /* now free tcp_conn */
1946 if (digest) {
1947 if (tcp_conn->tx_tfm)
1948 crypto_free_tfm(tcp_conn->tx_tfm);
1949 if (tcp_conn->rx_tfm)
1950 crypto_free_tfm(tcp_conn->rx_tfm);
1951 if (tcp_conn->data_tx_tfm)
1952 crypto_free_tfm(tcp_conn->data_tx_tfm);
1953 if (tcp_conn->data_rx_tfm)
1954 crypto_free_tfm(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001955 }
1956
1957 /* free conn->data, size = MaxRecvDataSegmentLength */
Mike Christie5bb0b552006-04-06 21:26:46 -05001958 if (tcp_conn->data_size <= PAGE_SIZE)
1959 kfree(tcp_conn->data);
Alex Aizman7ba24712005-08-04 19:30:08 -07001960 else
Mike Christie5bb0b552006-04-06 21:26:46 -05001961 free_pages((unsigned long)tcp_conn->data,
1962 get_order(tcp_conn->data_size));
1963 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001964}
1965
1966static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001967iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001968 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001969 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001970{
Mike Christie5bb0b552006-04-06 21:26:46 -05001971 struct iscsi_conn *conn = cls_conn->dd_data;
1972 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001973 struct sock *sk;
1974 struct socket *sock;
1975 int err;
1976
1977 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001978 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001979 if (!sock) {
1980 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1981 return -EEXIST;
1982 }
1983
Mike Christie5bb0b552006-04-06 21:26:46 -05001984 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1985 if (err)
1986 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001987
Mike Christie67a61112006-05-30 00:37:20 -05001988 /* bind iSCSI connection and socket */
1989 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001990
Mike Christie67a61112006-05-30 00:37:20 -05001991 /* setup Socket parameters */
1992 sk = sock->sk;
1993 sk->sk_reuse = 1;
1994 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1995 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001996
Mike Christie67a61112006-05-30 00:37:20 -05001997 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07001998
Mike Christie67a61112006-05-30 00:37:20 -05001999 /*
2000 * Intercept TCP callbacks for sendfile like receive
2001 * processing.
2002 */
2003 conn->recv_lock = &sk->sk_callback_lock;
2004 iscsi_conn_set_callbacks(conn);
2005 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
2006 /*
2007 * set receive state machine into initial state
2008 */
2009 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07002010
Alex Aizman7ba24712005-08-04 19:30:08 -07002011 return 0;
2012}
2013
Mike Christie30a6c652006-04-06 21:13:39 -05002014static void
Mike Christie5bb0b552006-04-06 21:26:46 -05002015iscsi_tcp_suspend_conn_rx(struct iscsi_conn *conn)
Mike Christie30a6c652006-04-06 21:13:39 -05002016{
Mike Christie5bb0b552006-04-06 21:26:46 -05002017 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002018 struct sock *sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07002019
Mike Christie5bb0b552006-04-06 21:26:46 -05002020 if (!tcp_conn->sock)
2021 return;
2022
2023 sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07002024 write_lock_bh(&sk->sk_callback_lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05002025 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Alex Aizman7ba24712005-08-04 19:30:08 -07002026 write_unlock_bh(&sk->sk_callback_lock);
Mike Christie30a6c652006-04-06 21:13:39 -05002027}
2028
2029static void
Mike Christie5bb0b552006-04-06 21:26:46 -05002030iscsi_tcp_terminate_conn(struct iscsi_conn *conn)
Mike Christie30a6c652006-04-06 21:13:39 -05002031{
Mike Christie5bb0b552006-04-06 21:26:46 -05002032 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2033
2034 if (!tcp_conn->sock)
Mike Christie30a6c652006-04-06 21:13:39 -05002035 return;
Mike Christie30a6c652006-04-06 21:13:39 -05002036
Mike Christie5bb0b552006-04-06 21:26:46 -05002037 sock_hold(tcp_conn->sock->sk);
Mike Christie30a6c652006-04-06 21:13:39 -05002038 iscsi_conn_restore_callbacks(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05002039 sock_put(tcp_conn->sock->sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07002040
Mike Christie5bb0b552006-04-06 21:26:46 -05002041 sock_release(tcp_conn->sock);
2042 tcp_conn->sock = NULL;
2043 conn->recv_lock = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002044}
2045
Mike Christie5bb0b552006-04-06 21:26:46 -05002046/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05002047static void
Mike Christie5bb0b552006-04-06 21:26:46 -05002048iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask,
2049 char *data, uint32_t data_size)
Mike Christie30a6c652006-04-06 21:13:39 -05002050{
Mike Christie5bb0b552006-04-06 21:26:46 -05002051 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie30a6c652006-04-06 21:13:39 -05002052
Mike Christie6e458cc2006-05-18 20:31:31 -05002053 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
2054 sizeof(struct iscsi_hdr));
Mike Christie5bb0b552006-04-06 21:26:46 -05002055 tcp_mtask->xmstate = XMSTATE_IMM_HDR;
Mike Christieb6c395e2006-07-24 15:47:15 -05002056 tcp_mtask->sent = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002057
Mike Christie5bb0b552006-04-06 21:26:46 -05002058 if (mtask->data_count)
2059 iscsi_buf_init_iov(&tcp_mtask->sendbuf, (char*)mtask->data,
Alex Aizman7ba24712005-08-04 19:30:08 -07002060 mtask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07002061}
2062
2063static int
2064iscsi_r2tpool_alloc(struct iscsi_session *session)
2065{
2066 int i;
2067 int cmd_i;
2068
2069 /*
2070 * initialize per-task: R2T pool and xmit queue
2071 */
2072 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2073 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05002074 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002075
2076 /*
2077 * pre-allocated x4 as much r2ts to handle race when
2078 * target acks DataOut faster than we data_xmit() queues
2079 * could replenish r2tqueue.
2080 */
2081
2082 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05002083 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
2084 (void***)&tcp_ctask->r2ts,
2085 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002086 goto r2t_alloc_fail;
2087 }
2088
2089 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05002090 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07002091 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05002092 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2093 iscsi_pool_free(&tcp_ctask->r2tpool,
2094 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002095 goto r2t_alloc_fail;
2096 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002097 }
2098
2099 return 0;
2100
2101r2t_alloc_fail:
2102 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002103 struct iscsi_cmd_task *ctask = session->cmds[i];
2104 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2105
Mike Christie5bb0b552006-04-06 21:26:46 -05002106 kfifo_free(tcp_ctask->r2tqueue);
2107 iscsi_pool_free(&tcp_ctask->r2tpool,
2108 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002109 }
2110 return -ENOMEM;
2111}
2112
2113static void
2114iscsi_r2tpool_free(struct iscsi_session *session)
2115{
2116 int i;
2117
2118 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002119 struct iscsi_cmd_task *ctask = session->cmds[i];
2120 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2121
Mike Christie5bb0b552006-04-06 21:26:46 -05002122 kfifo_free(tcp_ctask->r2tqueue);
2123 iscsi_pool_free(&tcp_ctask->r2tpool,
2124 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002125 }
2126}
2127
Alex Aizman7ba24712005-08-04 19:30:08 -07002128static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002129iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002130 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07002131{
Mike Christie7b7232f2006-02-01 21:06:49 -06002132 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002133 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05002134 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002135 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07002136
Alex Aizman7ba24712005-08-04 19:30:08 -07002137 switch(param) {
2138 case ISCSI_PARAM_MAX_RECV_DLENGTH: {
Mike Christie5bb0b552006-04-06 21:26:46 -05002139 char *saveptr = tcp_conn->data;
Al Virob53cb2a2005-12-15 09:17:19 +00002140 gfp_t flags = GFP_KERNEL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002141
Mike Christie5c75b7f2006-06-28 12:00:26 -05002142 sscanf(buf, "%d", &value);
Mike Christie5bb0b552006-04-06 21:26:46 -05002143 if (tcp_conn->data_size >= value) {
Mike Christie5c75b7f2006-06-28 12:00:26 -05002144 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002145 break;
2146 }
2147
2148 spin_lock_bh(&session->lock);
2149 if (conn->stop_stage == STOP_CONN_RECOVER)
2150 flags = GFP_ATOMIC;
2151 spin_unlock_bh(&session->lock);
2152
2153 if (value <= PAGE_SIZE)
Mike Christie5bb0b552006-04-06 21:26:46 -05002154 tcp_conn->data = kmalloc(value, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07002155 else
Mike Christie5bb0b552006-04-06 21:26:46 -05002156 tcp_conn->data = (void*)__get_free_pages(flags,
Alex Aizman7ba24712005-08-04 19:30:08 -07002157 get_order(value));
Mike Christie5bb0b552006-04-06 21:26:46 -05002158 if (tcp_conn->data == NULL) {
2159 tcp_conn->data = saveptr;
Alex Aizman7ba24712005-08-04 19:30:08 -07002160 return -ENOMEM;
2161 }
Mike Christie5bb0b552006-04-06 21:26:46 -05002162 if (tcp_conn->data_size <= PAGE_SIZE)
Alex Aizman7ba24712005-08-04 19:30:08 -07002163 kfree(saveptr);
2164 else
2165 free_pages((unsigned long)saveptr,
Mike Christie5bb0b552006-04-06 21:26:46 -05002166 get_order(tcp_conn->data_size));
Mike Christie5c75b7f2006-06-28 12:00:26 -05002167 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002168 tcp_conn->data_size = value;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002169 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002170 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002171 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002172 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002173 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07002174 if (conn->hdrdgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002175 tcp_conn->hdr_size += sizeof(__u32);
2176 if (!tcp_conn->tx_tfm)
2177 tcp_conn->tx_tfm = crypto_alloc_tfm("crc32c",
2178 0);
2179 if (!tcp_conn->tx_tfm)
Alex Aizman7ba24712005-08-04 19:30:08 -07002180 return -ENOMEM;
Mike Christie5bb0b552006-04-06 21:26:46 -05002181 if (!tcp_conn->rx_tfm)
2182 tcp_conn->rx_tfm = crypto_alloc_tfm("crc32c",
2183 0);
2184 if (!tcp_conn->rx_tfm) {
2185 crypto_free_tfm(tcp_conn->tx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002186 return -ENOMEM;
2187 }
2188 } else {
Mike Christie5bb0b552006-04-06 21:26:46 -05002189 if (tcp_conn->tx_tfm)
2190 crypto_free_tfm(tcp_conn->tx_tfm);
2191 if (tcp_conn->rx_tfm)
2192 crypto_free_tfm(tcp_conn->rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002193 }
2194 break;
2195 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002196 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002197 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002198 if (!tcp_conn->data_tx_tfm)
2199 tcp_conn->data_tx_tfm =
Alex Aizman7ba24712005-08-04 19:30:08 -07002200 crypto_alloc_tfm("crc32c", 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05002201 if (!tcp_conn->data_tx_tfm)
Alex Aizman7ba24712005-08-04 19:30:08 -07002202 return -ENOMEM;
Mike Christie5bb0b552006-04-06 21:26:46 -05002203 if (!tcp_conn->data_rx_tfm)
2204 tcp_conn->data_rx_tfm =
Alex Aizman7ba24712005-08-04 19:30:08 -07002205 crypto_alloc_tfm("crc32c", 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05002206 if (!tcp_conn->data_rx_tfm) {
2207 crypto_free_tfm(tcp_conn->data_tx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002208 return -ENOMEM;
2209 }
2210 } else {
Mike Christie5bb0b552006-04-06 21:26:46 -05002211 if (tcp_conn->data_tx_tfm)
2212 crypto_free_tfm(tcp_conn->data_tx_tfm);
2213 if (tcp_conn->data_rx_tfm)
2214 crypto_free_tfm(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002215 }
Mike Christie5bb0b552006-04-06 21:26:46 -05002216 tcp_conn->sendpage = conn->datadgst_en ?
2217 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07002218 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002219 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002220 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07002221 if (session->max_r2t == roundup_pow_of_two(value))
2222 break;
2223 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002224 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002225 if (session->max_r2t & (session->max_r2t - 1))
2226 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2227 if (iscsi_r2tpool_alloc(session))
2228 return -ENOMEM;
2229 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002230 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002231 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002232 }
2233
2234 return 0;
2235}
2236
2237static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05002238iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2239 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06002240{
Mike Christie7b7232f2006-02-01 21:06:49 -06002241 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002242 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christiefd7255f2006-04-06 21:13:36 -05002243 struct inet_sock *inet;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002244 struct ipv6_pinfo *np;
2245 struct sock *sk;
2246 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06002247
2248 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002249 case ISCSI_PARAM_CONN_PORT:
2250 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002251 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002252 mutex_unlock(&conn->xmitmutex);
2253 return -EINVAL;
2254 }
2255
Mike Christie5bb0b552006-04-06 21:26:46 -05002256 inet = inet_sk(tcp_conn->sock->sk);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002257 len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
Mike Christiefd7255f2006-04-06 21:13:36 -05002258 mutex_unlock(&conn->xmitmutex);
Mike Christie8d2860b2006-05-02 19:46:47 -05002259 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002260 case ISCSI_PARAM_CONN_ADDRESS:
2261 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002262 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002263 mutex_unlock(&conn->xmitmutex);
2264 return -EINVAL;
2265 }
2266
Mike Christie5bb0b552006-04-06 21:26:46 -05002267 sk = tcp_conn->sock->sk;
Mike Christiefd7255f2006-04-06 21:13:36 -05002268 if (sk->sk_family == PF_INET) {
2269 inet = inet_sk(sk);
2270 len = sprintf(buf, "%u.%u.%u.%u\n",
2271 NIPQUAD(inet->daddr));
2272 } else {
2273 np = inet6_sk(sk);
2274 len = sprintf(buf,
2275 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
2276 NIP6(np->daddr));
2277 }
2278 mutex_unlock(&conn->xmitmutex);
2279 break;
2280 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002281 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05002282 }
2283
2284 return len;
2285}
2286
Alex Aizman7ba24712005-08-04 19:30:08 -07002287static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002288iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002289{
Mike Christie7b7232f2006-02-01 21:06:49 -06002290 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002291 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002292
2293 stats->txdata_octets = conn->txdata_octets;
2294 stats->rxdata_octets = conn->rxdata_octets;
2295 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2296 stats->dataout_pdus = conn->dataout_pdus_cnt;
2297 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2298 stats->datain_pdus = conn->datain_pdus_cnt;
2299 stats->r2t_pdus = conn->r2t_pdus_cnt;
2300 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2301 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2302 stats->custom_length = 3;
2303 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002304 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002305 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002306 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002307 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2308 stats->custom[2].value = conn->eh_abort_cnt;
2309}
2310
Mike Christie5bb0b552006-04-06 21:26:46 -05002311static struct iscsi_cls_session *
2312iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2313 struct scsi_transport_template *scsit,
2314 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002315{
Mike Christie5bb0b552006-04-06 21:26:46 -05002316 struct iscsi_cls_session *cls_session;
2317 struct iscsi_session *session;
2318 uint32_t hn;
2319 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002320
Mike Christie5bb0b552006-04-06 21:26:46 -05002321 cls_session = iscsi_session_setup(iscsit, scsit,
2322 sizeof(struct iscsi_tcp_cmd_task),
2323 sizeof(struct iscsi_tcp_mgmt_task),
2324 initial_cmdsn, &hn);
2325 if (!cls_session)
2326 return NULL;
2327 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002328
Mike Christie5bb0b552006-04-06 21:26:46 -05002329 session = class_to_transport_session(cls_session);
2330 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2331 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2332 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2333
2334 ctask->hdr = &tcp_ctask->hdr;
2335 }
2336
2337 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2338 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2339 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2340
2341 mtask->hdr = &tcp_mtask->hdr;
2342 }
2343
2344 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2345 goto r2tpool_alloc_fail;
2346
2347 return cls_session;
2348
2349r2tpool_alloc_fail:
2350 iscsi_session_teardown(cls_session);
2351 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002352}
2353
Mike Christie5bb0b552006-04-06 21:26:46 -05002354static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2355{
Mike Christie5bb0b552006-04-06 21:26:46 -05002356 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2357 iscsi_session_teardown(cls_session);
2358}
2359
2360static struct scsi_host_template iscsi_sht = {
Mike Christiee0ecae82006-05-18 20:31:43 -05002361 .name = "iSCSI Initiator over TCP/IP, v"
2362 ISCSI_TCP_VERSION,
Mike Christie5bb0b552006-04-06 21:26:46 -05002363 .queuecommand = iscsi_queuecommand,
2364 .change_queue_depth = iscsi_change_queue_depth,
2365 .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
2366 .sg_tablesize = ISCSI_SG_TABLESIZE,
2367 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2368 .eh_abort_handler = iscsi_eh_abort,
2369 .eh_host_reset_handler = iscsi_eh_host_reset,
2370 .use_clustering = DISABLE_CLUSTERING,
2371 .proc_name = "iscsi_tcp",
2372 .this_id = -1,
2373};
2374
Alex Aizman7ba24712005-08-04 19:30:08 -07002375static struct iscsi_transport iscsi_tcp_transport = {
2376 .owner = THIS_MODULE,
2377 .name = "tcp",
2378 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2379 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002380 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2381 ISCSI_MAX_XMIT_DLENGTH |
2382 ISCSI_HDRDGST_EN |
2383 ISCSI_DATADGST_EN |
2384 ISCSI_INITIAL_R2T_EN |
2385 ISCSI_MAX_R2T |
2386 ISCSI_IMM_DATA_EN |
2387 ISCSI_FIRST_BURST |
2388 ISCSI_MAX_BURST |
2389 ISCSI_PDU_INORDER_EN |
2390 ISCSI_DATASEQ_INORDER_EN |
2391 ISCSI_ERL |
2392 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002393 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05002394 ISCSI_EXP_STATSN |
2395 ISCSI_PERSISTENT_PORT |
2396 ISCSI_PERSISTENT_ADDRESS |
2397 ISCSI_TARGET_NAME |
2398 ISCSI_TPGT,
Alex Aizman7ba24712005-08-04 19:30:08 -07002399 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002400 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002401 .max_conn = 1,
2402 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002403 /* session management */
2404 .create_session = iscsi_tcp_session_create,
2405 .destroy_session = iscsi_tcp_session_destroy,
2406 /* connection management */
2407 .create_conn = iscsi_tcp_conn_create,
2408 .bind_conn = iscsi_tcp_conn_bind,
2409 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002410 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002411 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002412 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002413 .start_conn = iscsi_conn_start,
2414 .stop_conn = iscsi_conn_stop,
Mike Christie5bb0b552006-04-06 21:26:46 -05002415 /* these are called as part of conn recovery */
2416 .suspend_conn_recv = iscsi_tcp_suspend_conn_rx,
2417 .terminate_conn = iscsi_tcp_terminate_conn,
2418 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002419 .send_pdu = iscsi_conn_send_pdu,
2420 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002421 .init_cmd_task = iscsi_tcp_cmd_init,
2422 .init_mgmt_task = iscsi_tcp_mgmt_init,
2423 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2424 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2425 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2426 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002427 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002428};
2429
2430static int __init
2431iscsi_tcp_init(void)
2432{
Alex Aizman7ba24712005-08-04 19:30:08 -07002433 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002434 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2435 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002436 return -EINVAL;
2437 }
2438 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2439
Mike Christie7b8631b2006-01-13 18:05:50 -06002440 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002441 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002442
Mike Christie7b8631b2006-01-13 18:05:50 -06002443 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002444}
2445
2446static void __exit
2447iscsi_tcp_exit(void)
2448{
2449 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002450}
2451
2452module_init(iscsi_tcp_init);
2453module_exit(iscsi_tcp_exit);