blob: aa20adc79f0279c321990080b504377d2bd4ba90 [file] [log] [blame]
Alex Aizman7ba24712005-08-04 19:30:08 -07001/*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
Mike Christie5bb0b552006-04-06 21:26:46 -05006 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
Alex Aizman7ba24712005-08-04 19:30:08 -07008 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * See the file COPYING included with this distribution for more details.
21 *
22 * Credits:
23 * Christoph Hellwig
24 * FUJITA Tomonori
25 * Arne Redlich
26 * Zhenyu Wang
27 */
28
29#include <linux/types.h>
30#include <linux/list.h>
31#include <linux/inet.h>
32#include <linux/blkdev.h>
33#include <linux/crypto.h>
34#include <linux/delay.h>
35#include <linux/kfifo.h>
36#include <linux/scatterlist.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010037#include <linux/mutex.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070038#include <net/tcp.h>
39#include <scsi/scsi_cmnd.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070040#include <scsi/scsi_host.h>
41#include <scsi/scsi.h>
42#include <scsi/scsi_transport_iscsi.h>
43
44#include "iscsi_tcp.h"
45
Mike Christief70e9c52006-05-30 01:04:51 -050046#define ISCSI_TCP_VERSION "1.0-595"
Mike Christiee0ecae82006-05-18 20:31:43 -050047
Alex Aizman7ba24712005-08-04 19:30:08 -070048MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
49 "Alex Aizman <itn780@yahoo.com>");
50MODULE_DESCRIPTION("iSCSI/TCP data-path");
51MODULE_LICENSE("GPL");
Mike Christiee0ecae82006-05-18 20:31:43 -050052MODULE_VERSION(ISCSI_TCP_VERSION);
Alex Aizman7ba24712005-08-04 19:30:08 -070053/* #define DEBUG_TCP */
Alex Aizman7ba24712005-08-04 19:30:08 -070054#define DEBUG_ASSERT
55
56#ifdef DEBUG_TCP
Mike Christie5bb0b552006-04-06 21:26:46 -050057#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
Alex Aizman7ba24712005-08-04 19:30:08 -070058#else
59#define debug_tcp(fmt...)
60#endif
61
Alex Aizman7ba24712005-08-04 19:30:08 -070062#ifndef DEBUG_ASSERT
63#ifdef BUG_ON
64#undef BUG_ON
65#endif
66#define BUG_ON(expr)
67#endif
68
Alex Aizman7ba24712005-08-04 19:30:08 -070069static unsigned int iscsi_max_lun = 512;
70module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
71
Alex Aizman7ba24712005-08-04 19:30:08 -070072static inline void
Alex Aizman7ba24712005-08-04 19:30:08 -070073iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
74{
Mike Christie7cae5152006-01-13 18:05:47 -060075 ibuf->sg.page = virt_to_page(vbuf);
76 ibuf->sg.offset = offset_in_page(vbuf);
Alex Aizman7ba24712005-08-04 19:30:08 -070077 ibuf->sg.length = size;
78 ibuf->sent = 0;
Mike Christie7cae5152006-01-13 18:05:47 -060079 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070080}
81
82static inline void
83iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
84{
Mike Christie7cae5152006-01-13 18:05:47 -060085 ibuf->sg.page = sg->page;
86 ibuf->sg.offset = sg->offset;
87 ibuf->sg.length = sg->length;
Alex Aizman7ba24712005-08-04 19:30:08 -070088 /*
89 * Fastpath: sg element fits into single page
90 */
Mike Christiea1e80c22006-01-13 18:05:56 -060091 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
Mike Christie7cae5152006-01-13 18:05:47 -060092 ibuf->use_sendmsg = 0;
93 else
94 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070095 ibuf->sent = 0;
96}
97
98static inline int
99iscsi_buf_left(struct iscsi_buf *ibuf)
100{
101 int rc;
102
103 rc = ibuf->sg.length - ibuf->sent;
104 BUG_ON(rc < 0);
105 return rc;
106}
107
108static inline void
Mike Christieaf973482005-09-12 21:01:32 -0500109iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
110 u8* crc)
Alex Aizman7ba24712005-08-04 19:30:08 -0700111{
Mike Christie5bb0b552006-04-06 21:26:46 -0500112 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
113
114 crypto_digest_digest(tcp_conn->tx_tfm, &buf->sg, 1, crc);
Mike Christieaf973482005-09-12 21:01:32 -0500115 buf->sg.length += sizeof(uint32_t);
Alex Aizman7ba24712005-08-04 19:30:08 -0700116}
117
Alex Aizman7ba24712005-08-04 19:30:08 -0700118static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500119iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700120{
Mike Christie5bb0b552006-04-06 21:26:46 -0500121 struct sk_buff *skb = tcp_conn->in.skb;
Alex Aizman7ba24712005-08-04 19:30:08 -0700122
Mike Christie5bb0b552006-04-06 21:26:46 -0500123 tcp_conn->in.zero_copy_hdr = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700124
Mike Christie5bb0b552006-04-06 21:26:46 -0500125 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
126 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700127 /*
128 * Zero-copy PDU Header: using connection context
129 * to store header pointer.
130 */
131 if (skb_shinfo(skb)->frag_list == NULL &&
Mike Christie5bb0b552006-04-06 21:26:46 -0500132 !skb_shinfo(skb)->nr_frags) {
133 tcp_conn->in.hdr = (struct iscsi_hdr *)
134 ((char*)skb->data + tcp_conn->in.offset);
135 tcp_conn->in.zero_copy_hdr = 1;
136 } else {
Alex Aizman7ba24712005-08-04 19:30:08 -0700137 /* ignoring return code since we checked
138 * in.copy before */
Mike Christie5bb0b552006-04-06 21:26:46 -0500139 skb_copy_bits(skb, tcp_conn->in.offset,
140 &tcp_conn->hdr, tcp_conn->hdr_size);
141 tcp_conn->in.hdr = &tcp_conn->hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700142 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500143 tcp_conn->in.offset += tcp_conn->hdr_size;
144 tcp_conn->in.copy -= tcp_conn->hdr_size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700145 } else {
146 int hdr_remains;
147 int copylen;
148
149 /*
150 * PDU header scattered across SKB's,
151 * copying it... This'll happen quite rarely.
152 */
153
Mike Christie5bb0b552006-04-06 21:26:46 -0500154 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
155 tcp_conn->in.hdr_offset = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700156
Mike Christie5bb0b552006-04-06 21:26:46 -0500157 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700158 BUG_ON(hdr_remains <= 0);
159
Mike Christie5bb0b552006-04-06 21:26:46 -0500160 copylen = min(tcp_conn->in.copy, hdr_remains);
161 skb_copy_bits(skb, tcp_conn->in.offset,
162 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
163 copylen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700164
165 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
Mike Christie5bb0b552006-04-06 21:26:46 -0500166 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
167 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700168
Mike Christie5bb0b552006-04-06 21:26:46 -0500169 tcp_conn->in.offset += copylen;
170 tcp_conn->in.copy -= copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700171 if (copylen < hdr_remains) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500172 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
173 tcp_conn->in.hdr_offset += copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700174 return -EAGAIN;
175 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500176 tcp_conn->in.hdr = &tcp_conn->hdr;
177 tcp_conn->discontiguous_hdr_cnt++;
178 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700179 }
180
181 return 0;
182}
183
Mike Christie30a6c652006-04-06 21:13:39 -0500184/*
185 * must be called with session lock
186 */
187static void
Mike Christieb6c395e2006-07-24 15:47:15 -0500188iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700189{
Mike Christie5bb0b552006-04-06 21:26:46 -0500190 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500191 struct iscsi_r2t_info *r2t;
Mike Christie30a6c652006-04-06 21:13:39 -0500192 struct scsi_cmnd *sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700193
Mike Christieb6c395e2006-07-24 15:47:15 -0500194 /* flush ctask's r2t queues */
195 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
196 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
197 sizeof(void*));
198 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
199 }
200
Mike Christie30a6c652006-04-06 21:13:39 -0500201 sc = ctask->sc;
202 if (unlikely(!sc))
Alex Aizman7ba24712005-08-04 19:30:08 -0700203 return;
Mike Christie30a6c652006-04-06 21:13:39 -0500204
Mike Christie5bb0b552006-04-06 21:26:46 -0500205 tcp_ctask->xmstate = XMSTATE_IDLE;
206 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700207}
208
209/**
210 * iscsi_data_rsp - SCSI Data-In Response processing
211 * @conn: iscsi connection
212 * @ctask: scsi command task
213 **/
214static int
215iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
216{
217 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500218 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
219 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
220 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700221 struct iscsi_session *session = conn->session;
222 int datasn = be32_to_cpu(rhdr->datasn);
223
224 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
225 if (rc)
226 return rc;
227 /*
228 * setup Data-In byte counter (gets decremented..)
229 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500230 ctask->data_count = tcp_conn->in.datalen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700231
Mike Christie5bb0b552006-04-06 21:26:46 -0500232 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700233 return 0;
234
235 if (ctask->datasn != datasn)
236 return ISCSI_ERR_DATASN;
237
238 ctask->datasn++;
239
Mike Christie5bb0b552006-04-06 21:26:46 -0500240 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
241 if (tcp_ctask->data_offset + tcp_conn->in.datalen > ctask->total_length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700242 return ISCSI_ERR_DATA_OFFSET;
243
244 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
245 struct scsi_cmnd *sc = ctask->sc;
246
247 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600248 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700249 int res_count = be32_to_cpu(rhdr->residual_count);
250
251 if (res_count > 0 &&
252 res_count <= sc->request_bufflen) {
253 sc->resid = res_count;
254 sc->result = (DID_OK << 16) | rhdr->cmd_status;
255 } else
256 sc->result = (DID_BAD_TARGET << 16) |
257 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600258 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700259 sc->resid = be32_to_cpu(rhdr->residual_count);
260 sc->result = (DID_OK << 16) | rhdr->cmd_status;
261 } else
262 sc->result = (DID_OK << 16) | rhdr->cmd_status;
263 }
264
265 conn->datain_pdus_cnt++;
266 return 0;
267}
268
269/**
270 * iscsi_solicit_data_init - initialize first Data-Out
271 * @conn: iscsi connection
272 * @ctask: scsi command task
273 * @r2t: R2T info
274 *
275 * Notes:
276 * Initialize first Data-Out within this R2T sequence and finds
277 * proper data_offset within this SCSI command.
278 *
279 * This function is called with connection lock taken.
280 **/
281static void
282iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
283 struct iscsi_r2t_info *r2t)
284{
285 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700286 struct scsi_cmnd *sc = ctask->sc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500287 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700288
Mike Christieffbfe922006-05-18 20:31:36 -0500289 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700290 memset(hdr, 0, sizeof(struct iscsi_data));
291 hdr->ttt = r2t->ttt;
292 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
293 r2t->solicit_datasn++;
294 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500295 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
296 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700297 hdr->exp_statsn = r2t->exp_statsn;
298 hdr->offset = cpu_to_be32(r2t->data_offset);
299 if (r2t->data_length > conn->max_xmit_dlength) {
300 hton24(hdr->dlength, conn->max_xmit_dlength);
301 r2t->data_count = conn->max_xmit_dlength;
302 hdr->flags = 0;
303 } else {
304 hton24(hdr->dlength, r2t->data_length);
305 r2t->data_count = r2t->data_length;
306 hdr->flags = ISCSI_FLAG_CMD_FINAL;
307 }
308 conn->dataout_pdus_cnt++;
309
310 r2t->sent = 0;
311
Mike Christie6e458cc2006-05-18 20:31:31 -0500312 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -0500313 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -0700314
Alex Aizman7ba24712005-08-04 19:30:08 -0700315 if (sc->use_sg) {
316 int i, sg_count = 0;
317 struct scatterlist *sg = sc->request_buffer;
318
319 r2t->sg = NULL;
320 for (i = 0; i < sc->use_sg; i++, sg += 1) {
321 /* FIXME: prefetch ? */
322 if (sg_count + sg->length > r2t->data_offset) {
323 int page_offset;
324
325 /* sg page found! */
326
327 /* offset within this page */
328 page_offset = r2t->data_offset - sg_count;
329
330 /* fill in this buffer */
331 iscsi_buf_init_sg(&r2t->sendbuf, sg);
332 r2t->sendbuf.sg.offset += page_offset;
333 r2t->sendbuf.sg.length -= page_offset;
334
335 /* xmit logic will continue with next one */
336 r2t->sg = sg + 1;
337 break;
338 }
339 sg_count += sg->length;
340 }
341 BUG_ON(r2t->sg == NULL);
342 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500343 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -0700344 (char*)sc->request_buffer + r2t->data_offset,
345 r2t->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -0700346}
347
348/**
349 * iscsi_r2t_rsp - iSCSI R2T Response processing
350 * @conn: iscsi connection
351 * @ctask: scsi command task
352 **/
353static int
354iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
355{
356 struct iscsi_r2t_info *r2t;
357 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500358 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
359 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
360 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700361 int r2tsn = be32_to_cpu(rhdr->r2tsn);
362 int rc;
363
Mike Christie5bb0b552006-04-06 21:26:46 -0500364 if (tcp_conn->in.datalen)
Alex Aizman7ba24712005-08-04 19:30:08 -0700365 return ISCSI_ERR_DATALEN;
366
Mike Christie5bb0b552006-04-06 21:26:46 -0500367 if (tcp_ctask->exp_r2tsn && tcp_ctask->exp_r2tsn != r2tsn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700368 return ISCSI_ERR_R2TSN;
369
370 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
371 if (rc)
372 return rc;
373
374 /* FIXME: use R2TSN to detect missing R2T */
375
376 /* fill-in new R2T associated with the task */
377 spin_lock(&session->lock);
378 if (!ctask->sc || ctask->mtask ||
379 session->state != ISCSI_STATE_LOGGED_IN) {
380 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
381 "recovery...\n", ctask->itt);
382 spin_unlock(&session->lock);
383 return 0;
384 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500385
Mike Christie5bb0b552006-04-06 21:26:46 -0500386 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700387 BUG_ON(!rc);
388
389 r2t->exp_statsn = rhdr->statsn;
390 r2t->data_length = be32_to_cpu(rhdr->data_length);
391 if (r2t->data_length == 0 ||
392 r2t->data_length > session->max_burst) {
393 spin_unlock(&session->lock);
394 return ISCSI_ERR_DATALEN;
395 }
396
397 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
398 if (r2t->data_offset + r2t->data_length > ctask->total_length) {
399 spin_unlock(&session->lock);
400 return ISCSI_ERR_DATALEN;
401 }
402
403 r2t->ttt = rhdr->ttt; /* no flip */
404 r2t->solicit_datasn = 0;
405
406 iscsi_solicit_data_init(conn, ctask, r2t);
407
Mike Christie5bb0b552006-04-06 21:26:46 -0500408 tcp_ctask->exp_r2tsn = r2tsn + 1;
409 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
410 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christieb6c395e2006-07-24 15:47:15 -0500411 list_move_tail(&ctask->running, &conn->xmitqueue);
Alex Aizman7ba24712005-08-04 19:30:08 -0700412
Mike Christie55e32992006-01-13 18:05:53 -0600413 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -0700414 conn->r2t_pdus_cnt++;
415 spin_unlock(&session->lock);
416
417 return 0;
418}
419
420static int
Mike Christie5bb0b552006-04-06 21:26:46 -0500421iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700422{
Mike Christie5bb0b552006-04-06 21:26:46 -0500423 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700424 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700425 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500426 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
427 uint32_t cdgst, rdgst = 0, itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700428
Mike Christie5bb0b552006-04-06 21:26:46 -0500429 hdr = tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700430
431 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500432 tcp_conn->in.datalen = ntoh24(hdr->dlength);
433 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700434 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500435 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700436 return ISCSI_ERR_DATALEN;
437 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500438 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700439
440 /* read AHS */
Mike Christie5bb0b552006-04-06 21:26:46 -0500441 ahslen = hdr->hlength << 2;
442 tcp_conn->in.offset += ahslen;
443 tcp_conn->in.copy -= ahslen;
444 if (tcp_conn->in.copy < 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700445 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
Mike Christie5bb0b552006-04-06 21:26:46 -0500446 "%d bytes\n", ahslen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700447 return ISCSI_ERR_AHSLEN;
448 }
449
450 /* calculate read padding */
Mike Christie5bb0b552006-04-06 21:26:46 -0500451 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
452 if (tcp_conn->in.padding) {
453 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
454 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
Alex Aizman7ba24712005-08-04 19:30:08 -0700455 }
456
457 if (conn->hdrdgst_en) {
458 struct scatterlist sg;
459
460 sg_init_one(&sg, (u8 *)hdr,
Mike Christie5bb0b552006-04-06 21:26:46 -0500461 sizeof(struct iscsi_hdr) + ahslen);
462 crypto_digest_digest(tcp_conn->rx_tfm, &sg, 1, (u8 *)&cdgst);
Alex Aizman7ba24712005-08-04 19:30:08 -0700463 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
Mike Christie5bb0b552006-04-06 21:26:46 -0500464 ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600465 if (cdgst != rdgst) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500466 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
467 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
Mike Christie8a47cd32005-11-30 02:27:19 -0600468 return ISCSI_ERR_HDR_DGST;
469 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700470 }
471
Mike Christie5bb0b552006-04-06 21:26:46 -0500472 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700473 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500474 rc = iscsi_verify_itt(conn, hdr, &itt);
475 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
476 tcp_conn->in.datalen = 0; /* force drop */
477 return 0;
478 } else if (rc)
479 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700480
481 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500482 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
483 ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700484
Mike Christie5bb0b552006-04-06 21:26:46 -0500485 switch(opcode) {
486 case ISCSI_OP_SCSI_DATA_IN:
487 tcp_conn->in.ctask = session->cmds[itt];
488 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
Mike Christie275fd7d2006-07-24 15:47:17 -0500489 if (rc)
490 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500491 /* fall through */
492 case ISCSI_OP_SCSI_CMD_RSP:
493 tcp_conn->in.ctask = session->cmds[itt];
494 if (tcp_conn->in.datalen)
495 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700496
Mike Christie5bb0b552006-04-06 21:26:46 -0500497 spin_lock(&session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500498 iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500499 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
500 spin_unlock(&session->lock);
501 break;
502 case ISCSI_OP_R2T:
503 tcp_conn->in.ctask = session->cmds[itt];
504 if (ahslen)
505 rc = ISCSI_ERR_AHSLEN;
506 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
507 DMA_TO_DEVICE)
508 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
509 else
510 rc = ISCSI_ERR_PROTO;
511 break;
512 case ISCSI_OP_LOGIN_RSP:
513 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500514 case ISCSI_OP_REJECT:
515 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500516 /*
517 * It is possible that we could get a PDU with a buffer larger
518 * than 8K, but there are no targets that currently do this.
519 * For now we fail until we find a vendor that needs it
520 */
521 if (DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH <
522 tcp_conn->in.datalen) {
523 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
524 "but conn buffer is only %u (opcode %0x)\n",
525 tcp_conn->in.datalen,
526 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, opcode);
527 rc = ISCSI_ERR_PROTO;
528 break;
529 }
530
Mike Christie5bb0b552006-04-06 21:26:46 -0500531 if (tcp_conn->in.datalen)
532 goto copy_hdr;
533 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500534 case ISCSI_OP_LOGOUT_RSP:
535 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500536 case ISCSI_OP_SCSI_TMFUNC_RSP:
537 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
538 break;
539 default:
540 rc = ISCSI_ERR_BAD_OPCODE;
541 break;
542 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700543
544 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500545
546copy_hdr:
547 /*
548 * if we did zero copy for the header but we will need multiple
549 * skbs to complete the command then we have to copy the header
550 * for later use
551 */
Mike Christie275fd7d2006-07-24 15:47:17 -0500552 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
Mike Christie5bb0b552006-04-06 21:26:46 -0500553 (tcp_conn->in.datalen + tcp_conn->in.padding +
554 (conn->datadgst_en ? 4 : 0))) {
555 debug_tcp("Copying header for later use. in.copy %d in.datalen"
556 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
557 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
558 sizeof(struct iscsi_hdr));
559 tcp_conn->in.hdr = &tcp_conn->hdr;
560 tcp_conn->in.zero_copy_hdr = 0;
561 }
562 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700563}
564
565/**
566 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
Mike Christie5bb0b552006-04-06 21:26:46 -0500567 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700568 * @ctask: scsi command task
569 * @buf: buffer to copy to
570 * @buf_size: size of buffer
571 * @offset: offset within the buffer
572 *
573 * Notes:
574 * The function calls skb_copy_bits() and updates per-connection and
575 * per-cmd byte counters.
576 *
577 * Read counters (in bytes):
578 *
579 * conn->in.offset offset within in progress SKB
580 * conn->in.copy left to copy from in progress SKB
581 * including padding
582 * conn->in.copied copied already from in progress SKB
583 * conn->data_copied copied already from in progress buffer
584 * ctask->sent total bytes sent up to the MidLayer
585 * ctask->data_count left to copy from in progress Data-In
586 * buf_left left to copy from in progress buffer
587 **/
588static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500589iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -0700590 void *buf, int buf_size, int offset)
591{
Mike Christie5bb0b552006-04-06 21:26:46 -0500592 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
593 int buf_left = buf_size - (tcp_conn->data_copied + offset);
594 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700595 int rc;
596
597 size = min(size, ctask->data_count);
598
599 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500600 size, tcp_conn->in.offset, tcp_conn->in.copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700601
602 BUG_ON(size <= 0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500603 BUG_ON(tcp_ctask->sent + size > ctask->total_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700604
Mike Christie5bb0b552006-04-06 21:26:46 -0500605 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
606 (char*)buf + (offset + tcp_conn->data_copied), size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700607 /* must fit into skb->len */
608 BUG_ON(rc);
609
Mike Christie5bb0b552006-04-06 21:26:46 -0500610 tcp_conn->in.offset += size;
611 tcp_conn->in.copy -= size;
612 tcp_conn->in.copied += size;
613 tcp_conn->data_copied += size;
614 tcp_ctask->sent += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700615 ctask->data_count -= size;
616
Mike Christie5bb0b552006-04-06 21:26:46 -0500617 BUG_ON(tcp_conn->in.copy < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700618 BUG_ON(ctask->data_count < 0);
619
Mike Christie5bb0b552006-04-06 21:26:46 -0500620 if (buf_size != (tcp_conn->data_copied + offset)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700621 if (!ctask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500622 BUG_ON(buf_size - tcp_conn->data_copied < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700623 /* done with this PDU */
Mike Christie5bb0b552006-04-06 21:26:46 -0500624 return buf_size - tcp_conn->data_copied;
Alex Aizman7ba24712005-08-04 19:30:08 -0700625 }
626 return -EAGAIN;
627 }
628
629 /* done with this buffer or with both - PDU and buffer */
Mike Christie5bb0b552006-04-06 21:26:46 -0500630 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700631 return 0;
632}
633
634/**
635 * iscsi_tcp_copy - copy skb bits to the destanation buffer
Mike Christie5bb0b552006-04-06 21:26:46 -0500636 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700637 *
638 * Notes:
639 * The function calls skb_copy_bits() and updates per-connection
640 * byte counters.
641 **/
642static inline int
Mike Christiec8dc1e52006-07-24 15:47:39 -0500643iscsi_tcp_copy(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700644{
Mike Christiec8dc1e52006-07-24 15:47:39 -0500645 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500646 int buf_size = tcp_conn->in.datalen;
647 int buf_left = buf_size - tcp_conn->data_copied;
648 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700649 int rc;
650
651 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500652 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700653 BUG_ON(size <= 0);
654
Mike Christie5bb0b552006-04-06 21:26:46 -0500655 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christiec8dc1e52006-07-24 15:47:39 -0500656 (char*)conn->data + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700657 BUG_ON(rc);
658
Mike Christie5bb0b552006-04-06 21:26:46 -0500659 tcp_conn->in.offset += size;
660 tcp_conn->in.copy -= size;
661 tcp_conn->in.copied += size;
662 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700663
Mike Christie5bb0b552006-04-06 21:26:46 -0500664 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700665 return -EAGAIN;
666
667 return 0;
668}
669
670static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -0500671partial_sg_digest_update(struct iscsi_tcp_conn *tcp_conn,
672 struct scatterlist *sg, int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700673{
674 struct scatterlist temp;
675
676 memcpy(&temp, sg, sizeof(struct scatterlist));
677 temp.offset = offset;
678 temp.length = length;
Mike Christie5bb0b552006-04-06 21:26:46 -0500679 crypto_digest_update(tcp_conn->data_rx_tfm, &temp, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700680}
681
Mike Christief6cfba12005-11-29 23:12:57 -0600682static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500683iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600684{
685 struct scatterlist tmp;
686
687 sg_init_one(&tmp, buf, len);
Mike Christie5bb0b552006-04-06 21:26:46 -0500688 crypto_digest_update(tcp_conn->data_rx_tfm, &tmp, 1);
Mike Christief6cfba12005-11-29 23:12:57 -0600689}
690
Alex Aizman7ba24712005-08-04 19:30:08 -0700691static int iscsi_scsi_data_in(struct iscsi_conn *conn)
692{
Mike Christie5bb0b552006-04-06 21:26:46 -0500693 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
694 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
695 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700696 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600697 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700698 int i, offset, rc = 0;
699
700 BUG_ON((void*)ctask != sc->SCp.ptr);
701
702 /*
703 * copying Data-In into the Scsi_Cmnd
704 */
705 if (!sc->use_sg) {
706 i = ctask->data_count;
Mike Christie5bb0b552006-04-06 21:26:46 -0500707 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
708 sc->request_bufflen,
709 tcp_ctask->data_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700710 if (rc == -EAGAIN)
711 return rc;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600712 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500713 iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
714 i);
Alex Aizman7ba24712005-08-04 19:30:08 -0700715 rc = 0;
716 goto done;
717 }
718
Mike Christie5bb0b552006-04-06 21:26:46 -0500719 offset = tcp_ctask->data_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700720 sg = sc->request_buffer;
721
Mike Christie5bb0b552006-04-06 21:26:46 -0500722 if (tcp_ctask->data_offset)
723 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700724 offset -= sg[i].length;
725 /* we've passed through partial sg*/
726 if (offset < 0)
727 offset = 0;
728
Mike Christie5bb0b552006-04-06 21:26:46 -0500729 for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700730 char *dest;
731
732 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500733 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700734 sg[i].length, offset);
735 kunmap_atomic(dest, KM_SOFTIRQ0);
736 if (rc == -EAGAIN)
737 /* continue with the next SKB/PDU */
738 return rc;
739 if (!rc) {
740 if (conn->datadgst_en) {
741 if (!offset)
Mike Christie5bb0b552006-04-06 21:26:46 -0500742 crypto_digest_update(
743 tcp_conn->data_rx_tfm,
744 &sg[i], 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700745 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500746 partial_sg_digest_update(tcp_conn,
747 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700748 sg[i].offset + offset,
749 sg[i].length - offset);
750 }
751 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500752 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700753 }
754
755 if (!ctask->data_count) {
756 if (rc && conn->datadgst_en)
757 /*
758 * data-in is complete, but buffer not...
759 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500760 partial_sg_digest_update(tcp_conn, &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700761 sg[i].offset, sg[i].length-rc);
762 rc = 0;
763 break;
764 }
765
Mike Christie5bb0b552006-04-06 21:26:46 -0500766 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700767 return -EAGAIN;
768 }
769 BUG_ON(ctask->data_count);
770
771done:
772 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500773 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Mike Christieb6c395e2006-07-24 15:47:15 -0500774 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
775 (long)sc, sc->result, ctask->itt,
776 tcp_conn->in.hdr->flags);
Mike Christie5bb0b552006-04-06 21:26:46 -0500777 spin_lock(&conn->session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500778 iscsi_tcp_cleanup_ctask(conn, ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500779 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
780 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700781 }
782
783 return rc;
784}
785
786static int
787iscsi_data_recv(struct iscsi_conn *conn)
788{
Mike Christie5bb0b552006-04-06 21:26:46 -0500789 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
790 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700791
Mike Christie5bb0b552006-04-06 21:26:46 -0500792 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
793 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700794 case ISCSI_OP_SCSI_DATA_IN:
795 rc = iscsi_scsi_data_in(conn);
796 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500797 case ISCSI_OP_SCSI_CMD_RSP:
798 spin_lock(&conn->session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500799 iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -0500800 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700801 case ISCSI_OP_TEXT_RSP:
802 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500803 case ISCSI_OP_ASYNC_EVENT:
804 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700805 /*
806 * Collect data segment to the connection's data
807 * placeholder
808 */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500809 if (iscsi_tcp_copy(conn)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700810 rc = -EAGAIN;
811 goto exit;
812 }
813
Mike Christiec8dc1e52006-07-24 15:47:39 -0500814 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500815 tcp_conn->in.datalen);
816 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
Mike Christiec8dc1e52006-07-24 15:47:39 -0500817 iscsi_recv_digest_update(tcp_conn, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500818 tcp_conn->in.datalen);
819 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700820 default:
821 BUG_ON(1);
822 }
823exit:
824 return rc;
825}
826
827/**
828 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
829 * @rd_desc: read descriptor
830 * @skb: socket buffer
831 * @offset: offset in skb
832 * @len: skb->len - offset
833 **/
834static int
835iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
836 unsigned int offset, size_t len)
837{
838 int rc;
839 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500840 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700841 int processed;
842 char pad[ISCSI_PAD_LEN];
843 struct scatterlist sg;
844
845 /*
846 * Save current SKB and its offset in the corresponding
847 * connection context.
848 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500849 tcp_conn->in.copy = skb->len - offset;
850 tcp_conn->in.offset = offset;
851 tcp_conn->in.skb = skb;
852 tcp_conn->in.len = tcp_conn->in.copy;
853 BUG_ON(tcp_conn->in.copy <= 0);
854 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700855
856more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500857 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700858 rc = 0;
859
860 if (unlikely(conn->suspend_rx)) {
861 debug_tcp("conn %d Rx suspended!\n", conn->id);
862 return 0;
863 }
864
Mike Christie5bb0b552006-04-06 21:26:46 -0500865 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
866 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
867 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700868 if (rc) {
869 if (rc == -EAGAIN)
870 goto nomore;
871 else {
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
877 /*
878 * Verify and process incoming PDU header.
879 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500880 rc = iscsi_tcp_hdr_recv(conn);
881 if (!rc && tcp_conn->in.datalen) {
Mike Christie8a47cd32005-11-30 02:27:19 -0600882 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500883 BUG_ON(!tcp_conn->data_rx_tfm);
884 crypto_digest_init(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -0700885 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500886 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700887 } else if (rc) {
Mike Christied82967c2006-07-24 15:47:11 -0500888 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700889 return 0;
890 }
891 }
892
Mike Christie5bb0b552006-04-06 21:26:46 -0500893 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
Mike Christief6cfba12005-11-29 23:12:57 -0600894 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500895
Alex Aizman7ba24712005-08-04 19:30:08 -0700896 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500897 tcp_conn->in.offset, tcp_conn->in.copy);
898 skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christief6cfba12005-11-29 23:12:57 -0600899 &recv_digest, 4);
Mike Christie5bb0b552006-04-06 21:26:46 -0500900 tcp_conn->in.offset += 4;
901 tcp_conn->in.copy -= 4;
902 if (recv_digest != tcp_conn->in.datadgst) {
Mike Christief6cfba12005-11-29 23:12:57 -0600903 debug_tcp("iscsi_tcp: data digest error!"
904 "0x%x != 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500905 tcp_conn->in.datadgst);
Mike Christief6cfba12005-11-29 23:12:57 -0600906 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
907 return 0;
908 } else {
909 debug_tcp("iscsi_tcp: data digest match!"
910 "0x%x == 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500911 tcp_conn->in.datadgst);
912 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700913 }
914 }
915
Mike Christie5bb0b552006-04-06 21:26:46 -0500916 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
917 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700918
919 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500920 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700921
922 rc = iscsi_data_recv(conn);
923 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500924 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700925 goto again;
Mike Christied82967c2006-07-24 15:47:11 -0500926 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700927 return 0;
928 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500929 tcp_conn->in.copy -= tcp_conn->in.padding;
930 tcp_conn->in.offset += tcp_conn->in.padding;
Mike Christie8a47cd32005-11-30 02:27:19 -0600931 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500932 if (tcp_conn->in.padding) {
933 debug_tcp("padding -> %d\n",
934 tcp_conn->in.padding);
935 memset(pad, 0, tcp_conn->in.padding);
936 sg_init_one(&sg, pad, tcp_conn->in.padding);
937 crypto_digest_update(tcp_conn->data_rx_tfm,
938 &sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700939 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500940 crypto_digest_final(tcp_conn->data_rx_tfm,
941 (u8 *) & tcp_conn->in.datadgst);
942 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
943 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700944 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500945 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700946 }
947
948 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500949 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
950 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700951
Mike Christie5bb0b552006-04-06 21:26:46 -0500952 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700953 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500954 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700955 goto more;
956 }
957
958nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500959 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700960 BUG_ON(processed == 0);
961 return processed;
962
963again:
Mike Christie5bb0b552006-04-06 21:26:46 -0500964 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700965 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
966 processed, (int)len, (int)rd_desc->count);
967 BUG_ON(processed == 0);
968 BUG_ON(processed > len);
969
970 conn->rxdata_octets += processed;
971 return processed;
972}
973
974static void
975iscsi_tcp_data_ready(struct sock *sk, int flag)
976{
977 struct iscsi_conn *conn = sk->sk_user_data;
978 read_descriptor_t rd_desc;
979
980 read_lock(&sk->sk_callback_lock);
981
Mike Christie665b44a2006-05-02 19:46:49 -0500982 /*
983 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
984 * We set count to 1 because we want the network layer to
985 * hand us all the skbs that are available. iscsi_tcp_data_recv
986 * handled pdus that cross buffers or pdus that still need data.
987 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700988 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -0500989 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -0700990 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
991
992 read_unlock(&sk->sk_callback_lock);
993}
994
995static void
996iscsi_tcp_state_change(struct sock *sk)
997{
Mike Christie5bb0b552006-04-06 21:26:46 -0500998 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -0700999 struct iscsi_conn *conn;
1000 struct iscsi_session *session;
1001 void (*old_state_change)(struct sock *);
1002
1003 read_lock(&sk->sk_callback_lock);
1004
1005 conn = (struct iscsi_conn*)sk->sk_user_data;
1006 session = conn->session;
1007
Mike Christiee6273992005-11-29 23:12:49 -06001008 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1009 sk->sk_state == TCP_CLOSE) &&
1010 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001011 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1012 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1013 }
1014
Mike Christie5bb0b552006-04-06 21:26:46 -05001015 tcp_conn = conn->dd_data;
1016 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001017
1018 read_unlock(&sk->sk_callback_lock);
1019
1020 old_state_change(sk);
1021}
1022
1023/**
1024 * iscsi_write_space - Called when more output buffer space is available
1025 * @sk: socket space is available for
1026 **/
1027static void
1028iscsi_write_space(struct sock *sk)
1029{
1030 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001031 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1032
1033 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001034 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001035 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001036}
1037
1038static void
1039iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1040{
Mike Christie5bb0b552006-04-06 21:26:46 -05001041 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1042 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001043
1044 /* assign new callbacks */
1045 write_lock_bh(&sk->sk_callback_lock);
1046 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001047 tcp_conn->old_data_ready = sk->sk_data_ready;
1048 tcp_conn->old_state_change = sk->sk_state_change;
1049 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001050 sk->sk_data_ready = iscsi_tcp_data_ready;
1051 sk->sk_state_change = iscsi_tcp_state_change;
1052 sk->sk_write_space = iscsi_write_space;
1053 write_unlock_bh(&sk->sk_callback_lock);
1054}
1055
1056static void
Mike Christie1c834692006-07-24 15:47:26 -05001057iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001058{
Mike Christie5bb0b552006-04-06 21:26:46 -05001059 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001060
1061 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1062 write_lock_bh(&sk->sk_callback_lock);
1063 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001064 sk->sk_data_ready = tcp_conn->old_data_ready;
1065 sk->sk_state_change = tcp_conn->old_state_change;
1066 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001067 sk->sk_no_check = 0;
1068 write_unlock_bh(&sk->sk_callback_lock);
1069}
1070
1071/**
1072 * iscsi_send - generic send routine
1073 * @sk: kernel's socket
1074 * @buf: buffer to write from
1075 * @size: actual size to write
1076 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001077 */
1078static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001079iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001080{
Mike Christie5bb0b552006-04-06 21:26:46 -05001081 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1082 struct socket *sk = tcp_conn->sock;
Mike Christie3219e522006-05-30 00:37:28 -05001083 int offset = buf->sg.offset + buf->sent, res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001084
Mike Christie7cae5152006-01-13 18:05:47 -06001085 /*
1086 * if we got use_sg=0 or are sending something we kmallocd
1087 * then we did not have to do kmap (kmap returns page_address)
1088 *
1089 * if we got use_sg > 0, but had to drop down, we do not
1090 * set clustering so this should only happen for that
1091 * slab case.
1092 */
1093 if (buf->use_sendmsg)
Mike Christie3219e522006-05-30 00:37:28 -05001094 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
Mike Christie7cae5152006-01-13 18:05:47 -06001095 else
Mike Christie3219e522006-05-30 00:37:28 -05001096 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1097
1098 if (res >= 0) {
1099 conn->txdata_octets += res;
1100 buf->sent += res;
1101 return res;
1102 }
1103
1104 tcp_conn->sendpage_failures_cnt++;
1105 if (res == -EAGAIN)
1106 res = -ENOBUFS;
1107 else
1108 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1109 return res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001110}
1111
1112/**
1113 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1114 * @conn: iscsi connection
1115 * @buf: buffer to write from
1116 * @datalen: lenght of data to be sent after the header
1117 *
1118 * Notes:
1119 * (Tx, Fast Path)
1120 **/
1121static inline int
1122iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1123{
Alex Aizman7ba24712005-08-04 19:30:08 -07001124 int flags = 0; /* MSG_DONTWAIT; */
1125 int res, size;
1126
1127 size = buf->sg.length - buf->sent;
1128 BUG_ON(buf->sent + size > buf->sg.length);
1129 if (buf->sent + size != buf->sg.length || datalen)
1130 flags |= MSG_MORE;
1131
FUJITA Tomonori56851692006-01-13 18:05:44 -06001132 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001133 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1134 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001135 if (size != res)
1136 return -EAGAIN;
1137 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001138 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001139
1140 return res;
1141}
1142
1143/**
1144 * iscsi_sendpage - send one page of iSCSI Data-Out.
1145 * @conn: iscsi connection
1146 * @buf: buffer to write from
1147 * @count: remaining data
1148 * @sent: number of bytes sent
1149 *
1150 * Notes:
1151 * (Tx, Fast Path)
1152 **/
1153static inline int
1154iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1155 int *count, int *sent)
1156{
Alex Aizman7ba24712005-08-04 19:30:08 -07001157 int flags = 0; /* MSG_DONTWAIT; */
1158 int res, size;
1159
1160 size = buf->sg.length - buf->sent;
1161 BUG_ON(buf->sent + size > buf->sg.length);
1162 if (size > *count)
1163 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001164 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001165 flags |= MSG_MORE;
1166
FUJITA Tomonori56851692006-01-13 18:05:44 -06001167 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001168 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1169 size, buf->sent, *count, *sent, res);
1170 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001171 *count -= res;
1172 *sent += res;
1173 if (size != res)
1174 return -EAGAIN;
1175 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001176 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001177
1178 return res;
1179}
1180
1181static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001182iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
1183 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001184{
Mike Christie5bb0b552006-04-06 21:26:46 -05001185 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1186
1187 BUG_ON(!tcp_conn->data_tx_tfm);
1188 crypto_digest_init(tcp_conn->data_tx_tfm);
1189 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001190}
1191
Arjan van de Ven858119e2006-01-14 13:20:43 -08001192static int
Alex Aizman7ba24712005-08-04 19:30:08 -07001193iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1194 struct iscsi_buf *buf, uint32_t *digest, int final)
1195{
Mike Christie5bb0b552006-04-06 21:26:46 -05001196 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1197 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001198 int rc = 0;
1199 int sent = 0;
1200
1201 if (final)
Mike Christie5bb0b552006-04-06 21:26:46 -05001202 crypto_digest_final(tcp_conn->data_tx_tfm, (u8*)digest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001203
Mike Christie6e458cc2006-05-18 20:31:31 -05001204 iscsi_buf_init_iov(buf, (char*)digest, 4);
Mike Christie5bb0b552006-04-06 21:26:46 -05001205 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001206 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001207 tcp_ctask->datadigest = *digest;
1208 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
Alex Aizman7ba24712005-08-04 19:30:08 -07001209 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001210 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001211 return rc;
1212}
1213
1214/**
1215 * iscsi_solicit_data_cont - initialize next Data-Out
1216 * @conn: iscsi connection
1217 * @ctask: scsi command task
1218 * @r2t: R2T info
1219 * @left: bytes left to transfer
1220 *
1221 * Notes:
1222 * Initialize next Data-Out within this R2T sequence and continue
1223 * to process next Scatter-Gather element(if any) of this SCSI command.
1224 *
1225 * Called under connection lock.
1226 **/
1227static void
1228iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1229 struct iscsi_r2t_info *r2t, int left)
1230{
Mike Christie5bb0b552006-04-06 21:26:46 -05001231 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001232 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001233 struct scsi_cmnd *sc = ctask->sc;
1234 int new_offset;
1235
Mike Christieffbfe922006-05-18 20:31:36 -05001236 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001237 memset(hdr, 0, sizeof(struct iscsi_data));
1238 hdr->ttt = r2t->ttt;
1239 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1240 r2t->solicit_datasn++;
1241 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001242 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1243 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001244 hdr->exp_statsn = r2t->exp_statsn;
1245 new_offset = r2t->data_offset + r2t->sent;
1246 hdr->offset = cpu_to_be32(new_offset);
1247 if (left > conn->max_xmit_dlength) {
1248 hton24(hdr->dlength, conn->max_xmit_dlength);
1249 r2t->data_count = conn->max_xmit_dlength;
1250 } else {
1251 hton24(hdr->dlength, left);
1252 r2t->data_count = left;
1253 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1254 }
1255 conn->dataout_pdus_cnt++;
1256
Mike Christie6e458cc2006-05-18 20:31:31 -05001257 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001258 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001259
Alex Aizman7ba24712005-08-04 19:30:08 -07001260 if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001261 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
Alex Aizman7ba24712005-08-04 19:30:08 -07001262 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1263 r2t->sg += 1;
1264 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001265 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -07001266 (char*)sc->request_buffer + new_offset,
1267 r2t->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001268}
1269
1270static void
1271iscsi_unsolicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1272{
Mike Christie5bb0b552006-04-06 21:26:46 -05001273 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001274 struct iscsi_data_task *dtask;
1275
Mike Christieffbfe922006-05-18 20:31:36 -05001276 dtask = tcp_ctask->dtask = &tcp_ctask->unsol_dtask;
Mike Christie5bb0b552006-04-06 21:26:46 -05001277 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr,
1278 tcp_ctask->r2t_data_count);
Mike Christie6e458cc2006-05-18 20:31:31 -05001279 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001280 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001281}
1282
1283/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001284 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001285 * @conn: iscsi connection
1286 * @ctask: scsi command task
1287 * @sc: scsi command
1288 **/
1289static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001290iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001291{
Mike Christie5bb0b552006-04-06 21:26:46 -05001292 struct scsi_cmnd *sc = ctask->sc;
1293 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001294
Mike Christie5bb0b552006-04-06 21:26:46 -05001295 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Alex Aizman7ba24712005-08-04 19:30:08 -07001296
Mike Christie5bb0b552006-04-06 21:26:46 -05001297 tcp_ctask->sent = 0;
1298 tcp_ctask->sg_count = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001299
1300 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001301 tcp_ctask->xmstate = XMSTATE_W_HDR;
1302 tcp_ctask->exp_r2tsn = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001303 BUG_ON(ctask->total_length == 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05001304
Alex Aizman7ba24712005-08-04 19:30:08 -07001305 if (sc->use_sg) {
1306 struct scatterlist *sg = sc->request_buffer;
1307
Mike Christie5bb0b552006-04-06 21:26:46 -05001308 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1309 &sg[tcp_ctask->sg_count++]);
1310 tcp_ctask->sg = sg;
1311 tcp_ctask->bad_sg = sg + sc->use_sg;
Alex Aizman7ba24712005-08-04 19:30:08 -07001312 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001313 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1314 sc->request_buffer,
1315 sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001316
Mike Christie5bb0b552006-04-06 21:26:46 -05001317 if (ctask->imm_count)
1318 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001319
Mike Christie5bb0b552006-04-06 21:26:46 -05001320 tcp_ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
1321 if (tcp_ctask->pad_count) {
1322 tcp_ctask->pad_count = ISCSI_PAD_LEN -
1323 tcp_ctask->pad_count;
1324 debug_scsi("write padding %d bytes\n",
1325 tcp_ctask->pad_count);
1326 tcp_ctask->xmstate |= XMSTATE_W_PAD;
1327 }
1328
1329 if (ctask->unsol_count)
1330 tcp_ctask->xmstate |= XMSTATE_UNS_HDR |
1331 XMSTATE_UNS_INIT;
1332 tcp_ctask->r2t_data_count = ctask->total_length -
Alex Aizman7ba24712005-08-04 19:30:08 -07001333 ctask->imm_count -
1334 ctask->unsol_count;
1335
Mike Christieb6c395e2006-07-24 15:47:15 -05001336 debug_scsi("cmd [itt 0x%x total %d imm %d imm_data %d "
Alex Aizman7ba24712005-08-04 19:30:08 -07001337 "r2t_data %d]\n",
1338 ctask->itt, ctask->total_length, ctask->imm_count,
Mike Christie5bb0b552006-04-06 21:26:46 -05001339 ctask->unsol_count, tcp_ctask->r2t_data_count);
1340 } else
1341 tcp_ctask->xmstate = XMSTATE_R_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001342
Mike Christie6e458cc2006-05-18 20:31:31 -05001343 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001344 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001345}
1346
1347/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001348 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001349 * @conn: iscsi connection
1350 * @mtask: task management task
1351 *
1352 * Notes:
1353 * The function can return -EAGAIN in which case caller must
1354 * call it again later, or recover. '0' return code means successful
1355 * xmit.
1356 *
1357 * Management xmit state machine consists of two states:
1358 * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
1359 * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1360 **/
1361static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001362iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001363{
Mike Christie5bb0b552006-04-06 21:26:46 -05001364 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001365 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001366
1367 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001368 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001369
Mike Christie5bb0b552006-04-06 21:26:46 -05001370 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
1371 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001372 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001373 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christieaf973482005-09-12 21:01:32 -05001374 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001375 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001376 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001377 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1378 (u8*)tcp_mtask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001379 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1380 mtask->data_count);
1381 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001382 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001383 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001384 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001385 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001386 }
1387 }
1388
Mike Christie5bb0b552006-04-06 21:26:46 -05001389 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001390 BUG_ON(!mtask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001391 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001392 /* FIXME: implement.
1393 * Virtual buffer could be spreaded across multiple pages...
1394 */
1395 do {
Mike Christie3219e522006-05-30 00:37:28 -05001396 int rc;
1397
1398 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1399 &mtask->data_count, &tcp_mtask->sent);
1400 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001401 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001402 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001403 }
1404 } while (mtask->data_count);
1405 }
1406
Mike Christie5bb0b552006-04-06 21:26:46 -05001407 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
1408 if (mtask->hdr->itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1409 struct iscsi_session *session = conn->session;
1410
1411 spin_lock_bh(&session->lock);
1412 list_del(&conn->mtask->running);
1413 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1414 sizeof(void*));
1415 spin_unlock_bh(&session->lock);
1416 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001417 return 0;
1418}
1419
1420static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -05001421handle_xmstate_r_hdr(struct iscsi_conn *conn,
1422 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001423{
Mike Christie3219e522006-05-30 00:37:28 -05001424 int rc;
1425
Mike Christie5bb0b552006-04-06 21:26:46 -05001426 tcp_ctask->xmstate &= ~XMSTATE_R_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001427 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001428 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1429 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001430 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, 0);
1431 if (!rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001432 BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE);
Alex Aizman7ba24712005-08-04 19:30:08 -07001433 return 0; /* wait for Data-In */
1434 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001435 tcp_ctask->xmstate |= XMSTATE_R_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001436 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001437}
1438
1439static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -05001440handle_xmstate_w_hdr(struct iscsi_conn *conn,
1441 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001442{
Mike Christie5bb0b552006-04-06 21:26:46 -05001443 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001444 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001445
1446 tcp_ctask->xmstate &= ~XMSTATE_W_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001447 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001448 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1449 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001450 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1451 if (rc)
Mike Christie5bb0b552006-04-06 21:26:46 -05001452 tcp_ctask->xmstate |= XMSTATE_W_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001453 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001454}
1455
1456static inline int
1457handle_xmstate_data_digest(struct iscsi_conn *conn,
1458 struct iscsi_cmd_task *ctask)
1459{
Mike Christie5bb0b552006-04-06 21:26:46 -05001460 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001461 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001462
1463 tcp_ctask->xmstate &= ~XMSTATE_DATA_DIGEST;
1464 debug_tcp("resent data digest 0x%x\n", tcp_ctask->datadigest);
Mike Christie3219e522006-05-30 00:37:28 -05001465 rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1466 &tcp_ctask->datadigest, 0);
1467 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001468 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
Alex Aizman7ba24712005-08-04 19:30:08 -07001469 debug_tcp("resent data digest 0x%x fail!\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001470 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001471 }
Mike Christie3219e522006-05-30 00:37:28 -05001472
1473 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001474}
1475
1476static inline int
1477handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1478{
Mike Christie5bb0b552006-04-06 21:26:46 -05001479 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1480 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001481 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001482
Alex Aizman7ba24712005-08-04 19:30:08 -07001483 BUG_ON(!ctask->imm_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001484 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001485
1486 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001487 iscsi_data_digest_init(tcp_conn, ctask);
1488 tcp_ctask->immdigest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001489 }
1490
1491 for (;;) {
Mike Christie3219e522006-05-30 00:37:28 -05001492 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1493 &ctask->imm_count, &tcp_ctask->sent);
1494 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001495 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001496 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001497 crypto_digest_final(tcp_conn->data_tx_tfm,
1498 (u8*)&tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001499 debug_tcp("tx imm sendpage fail 0x%x\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001500 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001501 }
Mike Christie3219e522006-05-30 00:37:28 -05001502 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001503 }
1504 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001505 crypto_digest_update(tcp_conn->data_tx_tfm,
1506 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001507
1508 if (!ctask->imm_count)
1509 break;
Mike Christie5bb0b552006-04-06 21:26:46 -05001510 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1511 &tcp_ctask->sg[tcp_ctask->sg_count++]);
Alex Aizman7ba24712005-08-04 19:30:08 -07001512 }
1513
Mike Christie5bb0b552006-04-06 21:26:46 -05001514 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
Mike Christie3219e522006-05-30 00:37:28 -05001515 rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1516 &tcp_ctask->immdigest, 1);
1517 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001518 debug_tcp("sending imm digest 0x%x fail!\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001519 tcp_ctask->immdigest);
Mike Christie3219e522006-05-30 00:37:28 -05001520 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001521 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001522 debug_tcp("sending imm digest 0x%x\n", tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001523 }
1524
1525 return 0;
1526}
1527
1528static inline int
1529handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1530{
Mike Christie5bb0b552006-04-06 21:26:46 -05001531 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001532 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001533 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001534
Mike Christie5bb0b552006-04-06 21:26:46 -05001535 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1536 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001537 iscsi_unsolicit_data_init(conn, ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -05001538 dtask = tcp_ctask->dtask;
Mike Christieaf973482005-09-12 21:01:32 -05001539 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001540 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001541 (u8*)dtask->hdrext);
Mike Christie5bb0b552006-04-06 21:26:46 -05001542 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001543 }
Mike Christie3219e522006-05-30 00:37:28 -05001544
1545 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1546 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001547 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1548 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001549 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001550 }
1551
1552 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001553 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001554 return 0;
1555}
1556
1557static inline int
1558handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1559{
Mike Christie5bb0b552006-04-06 21:26:46 -05001560 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1561 struct iscsi_data_task *dtask = tcp_ctask->dtask;
1562 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001563 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001564
1565 BUG_ON(!ctask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001566 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001567
1568 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001569 iscsi_data_digest_init(tcp_conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001570 dtask->digest = 0;
1571 }
1572
1573 for (;;) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001574 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001575
Mike Christie3219e522006-05-30 00:37:28 -05001576 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1577 &ctask->data_count, &tcp_ctask->sent);
1578 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001579 ctask->unsol_count -= tcp_ctask->sent - start;
1580 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001581 /* will continue with this ctask later.. */
1582 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001583 crypto_digest_final(tcp_conn->data_tx_tfm,
Alex Aizman7ba24712005-08-04 19:30:08 -07001584 (u8 *)&dtask->digest);
1585 debug_tcp("tx uns data fail 0x%x\n",
1586 dtask->digest);
1587 }
Mike Christie3219e522006-05-30 00:37:28 -05001588 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001589 }
1590
Mike Christie5bb0b552006-04-06 21:26:46 -05001591 BUG_ON(tcp_ctask->sent > ctask->total_length);
1592 ctask->unsol_count -= tcp_ctask->sent - start;
Alex Aizman7ba24712005-08-04 19:30:08 -07001593
1594 /*
1595 * XXX:we may run here with un-initial sendbuf.
1596 * so pass it
1597 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001598 if (conn->datadgst_en && tcp_ctask->sent - start > 0)
1599 crypto_digest_update(tcp_conn->data_tx_tfm,
1600 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001601
1602 if (!ctask->data_count)
1603 break;
Mike Christie5bb0b552006-04-06 21:26:46 -05001604 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1605 &tcp_ctask->sg[tcp_ctask->sg_count++]);
Alex Aizman7ba24712005-08-04 19:30:08 -07001606 }
1607 BUG_ON(ctask->unsol_count < 0);
1608
1609 /*
1610 * Done with the Data-Out. Next, check if we need
1611 * to send another unsolicited Data-Out.
1612 */
1613 if (ctask->unsol_count) {
1614 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001615 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001616 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001617 &dtask->digest, 1);
1618 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001619 debug_tcp("send uns digest 0x%x fail\n",
1620 dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001621 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001622 }
1623 debug_tcp("sending uns digest 0x%x, more uns\n",
1624 dtask->digest);
1625 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001626 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001627 return 1;
1628 }
1629
Mike Christie5bb0b552006-04-06 21:26:46 -05001630 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
Mike Christie3219e522006-05-30 00:37:28 -05001631 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001632 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001633 &dtask->digest, 1);
1634 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001635 debug_tcp("send last uns digest 0x%x fail\n",
1636 dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001637 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001638 }
1639 debug_tcp("sending uns digest 0x%x\n",dtask->digest);
1640 }
1641
1642 return 0;
1643}
1644
1645static inline int
1646handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1647{
1648 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001649 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1650 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1651 struct iscsi_r2t_info *r2t = tcp_ctask->r2t;
Mike Christieffbfe922006-05-18 20:31:36 -05001652 struct iscsi_data_task *dtask = &r2t->dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001653 int left, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001654
Mike Christie5bb0b552006-04-06 21:26:46 -05001655 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1656 tcp_ctask->dtask = dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001657
1658 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001659 iscsi_data_digest_init(tcp_conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001660 dtask->digest = 0;
1661 }
1662solicit_again:
1663 /*
Mike Christieb6c395e2006-07-24 15:47:15 -05001664 * send Data-Out within this R2T sequence.
Alex Aizman7ba24712005-08-04 19:30:08 -07001665 */
1666 if (!r2t->data_count)
1667 goto data_out_done;
1668
Mike Christie3219e522006-05-30 00:37:28 -05001669 rc = iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent);
1670 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001671 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001672 /* will continue with this ctask later.. */
1673 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001674 crypto_digest_final(tcp_conn->data_tx_tfm,
Alex Aizman7ba24712005-08-04 19:30:08 -07001675 (u8 *)&dtask->digest);
1676 debug_tcp("r2t data send fail 0x%x\n", dtask->digest);
1677 }
Mike Christie3219e522006-05-30 00:37:28 -05001678 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001679 }
1680
1681 BUG_ON(r2t->data_count < 0);
1682 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001683 crypto_digest_update(tcp_conn->data_tx_tfm, &r2t->sendbuf.sg,
1684 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001685
1686 if (r2t->data_count) {
1687 BUG_ON(ctask->sc->use_sg == 0);
1688 if (!iscsi_buf_left(&r2t->sendbuf)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001689 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
Alex Aizman7ba24712005-08-04 19:30:08 -07001690 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1691 r2t->sg += 1;
1692 }
1693 goto solicit_again;
1694 }
1695
1696data_out_done:
1697 /*
1698 * Done with this Data-Out. Next, check if we have
1699 * to send another Data-Out for this R2T.
1700 */
1701 BUG_ON(r2t->data_length - r2t->sent < 0);
1702 left = r2t->data_length - r2t->sent;
1703 if (left) {
1704 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001705 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001706 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001707 &dtask->digest, 1);
1708 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001709 debug_tcp("send r2t data digest 0x%x"
1710 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001711 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001712 }
1713 debug_tcp("r2t data send digest 0x%x\n",
1714 dtask->digest);
1715 }
1716 iscsi_solicit_data_cont(conn, ctask, r2t, left);
Mike Christie5bb0b552006-04-06 21:26:46 -05001717 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1718 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001719 return 1;
1720 }
1721
1722 /*
1723 * Done with this R2T. Check if there are more
1724 * outstanding R2Ts ready to be processed.
1725 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001726 BUG_ON(tcp_ctask->r2t_data_count - r2t->data_length < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -07001727 if (conn->datadgst_en) {
Mike Christie3219e522006-05-30 00:37:28 -05001728 rc = iscsi_digest_final_send(conn, ctask, &dtask->digestbuf,
1729 &dtask->digest, 1);
1730 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001731 debug_tcp("send last r2t data digest 0x%x"
1732 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001733 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001734 }
1735 debug_tcp("r2t done dout digest 0x%x\n", dtask->digest);
1736 }
1737
Mike Christie5bb0b552006-04-06 21:26:46 -05001738 tcp_ctask->r2t_data_count -= r2t->data_length;
1739 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001740 spin_lock_bh(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05001741 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -07001742 spin_unlock_bh(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05001743 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
1744 tcp_ctask->r2t = r2t;
1745 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1746 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001747 return 1;
1748 }
1749
1750 return 0;
1751}
1752
1753static inline int
1754handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1755{
Mike Christie5bb0b552006-04-06 21:26:46 -05001756 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1757 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1758 struct iscsi_data_task *dtask = tcp_ctask->dtask;
Mike Christieb6c395e2006-07-24 15:47:15 -05001759 int sent = 0, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001760
Mike Christie5bb0b552006-04-06 21:26:46 -05001761 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
Mike Christie6e458cc2006-05-18 20:31:31 -05001762 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
Mike Christie5bb0b552006-04-06 21:26:46 -05001763 tcp_ctask->pad_count);
Mike Christie3219e522006-05-30 00:37:28 -05001764 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1765 &sent);
1766 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001767 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Mike Christie3219e522006-05-30 00:37:28 -05001768 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001769 }
1770
1771 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001772 crypto_digest_update(tcp_conn->data_tx_tfm,
1773 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001774 /* imm data? */
1775 if (!dtask) {
Mike Christie3219e522006-05-30 00:37:28 -05001776 rc = iscsi_digest_final_send(conn, ctask,
Mike Christie5bb0b552006-04-06 21:26:46 -05001777 &tcp_ctask->immbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001778 &tcp_ctask->immdigest, 1);
1779 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001780 debug_tcp("send padding digest 0x%x"
Mike Christie5bb0b552006-04-06 21:26:46 -05001781 "fail!\n", tcp_ctask->immdigest);
Mike Christie3219e522006-05-30 00:37:28 -05001782 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001783 }
1784 debug_tcp("done with padding, digest 0x%x\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001785 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001786 } else {
Mike Christie3219e522006-05-30 00:37:28 -05001787 rc = iscsi_digest_final_send(conn, ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -07001788 &dtask->digestbuf,
Mike Christie3219e522006-05-30 00:37:28 -05001789 &dtask->digest, 1);
1790 if (rc) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001791 debug_tcp("send padding digest 0x%x"
1792 "fail\n", dtask->digest);
Mike Christie3219e522006-05-30 00:37:28 -05001793 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001794 }
1795 debug_tcp("done with padding, digest 0x%x\n",
1796 dtask->digest);
1797 }
1798 }
1799
1800 return 0;
1801}
1802
1803static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001804iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001805{
Mike Christie5bb0b552006-04-06 21:26:46 -05001806 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001807 int rc = 0;
1808
1809 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001810 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001811
1812 /*
1813 * serialize with TMF AbortTask
1814 */
1815 if (ctask->mtask)
1816 return rc;
1817
Mike Christie3219e522006-05-30 00:37:28 -05001818 if (tcp_ctask->xmstate & XMSTATE_R_HDR)
1819 return handle_xmstate_r_hdr(conn, tcp_ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001820
Mike Christie5bb0b552006-04-06 21:26:46 -05001821 if (tcp_ctask->xmstate & XMSTATE_W_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001822 rc = handle_xmstate_w_hdr(conn, ctask);
1823 if (rc)
1824 return rc;
1825 }
1826
1827 /* XXX: for data digest xmit recover */
Mike Christie5bb0b552006-04-06 21:26:46 -05001828 if (tcp_ctask->xmstate & XMSTATE_DATA_DIGEST) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001829 rc = handle_xmstate_data_digest(conn, ctask);
1830 if (rc)
1831 return rc;
1832 }
1833
Mike Christie5bb0b552006-04-06 21:26:46 -05001834 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001835 rc = handle_xmstate_imm_data(conn, ctask);
1836 if (rc)
1837 return rc;
1838 }
1839
Mike Christie5bb0b552006-04-06 21:26:46 -05001840 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001841 BUG_ON(!ctask->unsol_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001842 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001843unsolicit_head_again:
1844 rc = handle_xmstate_uns_hdr(conn, ctask);
1845 if (rc)
1846 return rc;
1847 }
1848
Mike Christie5bb0b552006-04-06 21:26:46 -05001849 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001850 rc = handle_xmstate_uns_data(conn, ctask);
1851 if (rc == 1)
1852 goto unsolicit_head_again;
1853 else if (rc)
1854 return rc;
1855 goto done;
1856 }
1857
Mike Christie5bb0b552006-04-06 21:26:46 -05001858 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001859 struct iscsi_r2t_info *r2t;
1860
Mike Christie5bb0b552006-04-06 21:26:46 -05001861 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1862 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1863 if (!tcp_ctask->r2t)
1864 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
Alex Aizman7ba24712005-08-04 19:30:08 -07001865 sizeof(void*));
1866solicit_head_again:
Mike Christie5bb0b552006-04-06 21:26:46 -05001867 r2t = tcp_ctask->r2t;
Mike Christieaf973482005-09-12 21:01:32 -05001868 if (conn->hdrdgst_en)
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001869 iscsi_hdr_digest(conn, &r2t->headbuf,
Mike Christieffbfe922006-05-18 20:31:36 -05001870 (u8*)r2t->dtask.hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001871 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
1872 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001873 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1874 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001875 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001876 }
1877
1878 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1879 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1880 r2t->sent);
1881 }
1882
Mike Christie5bb0b552006-04-06 21:26:46 -05001883 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001884 rc = handle_xmstate_sol_data(conn, ctask);
1885 if (rc == 1)
1886 goto solicit_head_again;
1887 if (rc)
1888 return rc;
1889 }
1890
1891done:
1892 /*
1893 * Last thing to check is whether we need to send write
1894 * padding. Note that we check for xmstate equality, not just the bit.
1895 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001896 if (tcp_ctask->xmstate == XMSTATE_W_PAD)
Alex Aizman7ba24712005-08-04 19:30:08 -07001897 rc = handle_xmstate_w_pad(conn, ctask);
1898
1899 return rc;
1900}
1901
Mike Christie7b8631b2006-01-13 18:05:50 -06001902static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001903iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001904{
Mike Christie7b8631b2006-01-13 18:05:50 -06001905 struct iscsi_conn *conn;
1906 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001907 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001908
Mike Christie5bb0b552006-04-06 21:26:46 -05001909 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001910 if (!cls_conn)
1911 return NULL;
1912 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001913 /*
1914 * due to strange issues with iser these are not set
1915 * in iscsi_conn_setup
1916 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001917 conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
1918
Mike Christie5bb0b552006-04-06 21:26:46 -05001919 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1920 if (!tcp_conn)
1921 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001922
Mike Christie5bb0b552006-04-06 21:26:46 -05001923 conn->dd_data = tcp_conn;
1924 tcp_conn->iscsi_conn = conn;
1925 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1926 /* initial operational parameters */
1927 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07001928
Mike Christie7b8631b2006-01-13 18:05:50 -06001929 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001930
Mike Christie5bb0b552006-04-06 21:26:46 -05001931tcp_conn_alloc_fail:
1932 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001933 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001934}
1935
1936static void
Mike Christie1c834692006-07-24 15:47:26 -05001937iscsi_tcp_release_conn(struct iscsi_conn *conn)
1938{
1939 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1940
1941 if (!tcp_conn->sock)
1942 return;
1943
1944 sock_hold(tcp_conn->sock->sk);
1945 iscsi_conn_restore_callbacks(tcp_conn);
1946 sock_put(tcp_conn->sock->sk);
1947
1948 sock_release(tcp_conn->sock);
1949 tcp_conn->sock = NULL;
1950 conn->recv_lock = NULL;
1951}
1952
1953static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001954iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001955{
Mike Christie7b8631b2006-01-13 18:05:50 -06001956 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001957 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1958 int digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001959
Mike Christie5bb0b552006-04-06 21:26:46 -05001960 if (conn->hdrdgst_en || conn->datadgst_en)
1961 digest = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001962
Mike Christie1c834692006-07-24 15:47:26 -05001963 iscsi_tcp_release_conn(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001964 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001965
Mike Christie5bb0b552006-04-06 21:26:46 -05001966 /* now free tcp_conn */
1967 if (digest) {
1968 if (tcp_conn->tx_tfm)
1969 crypto_free_tfm(tcp_conn->tx_tfm);
1970 if (tcp_conn->rx_tfm)
1971 crypto_free_tfm(tcp_conn->rx_tfm);
1972 if (tcp_conn->data_tx_tfm)
1973 crypto_free_tfm(tcp_conn->data_tx_tfm);
1974 if (tcp_conn->data_rx_tfm)
1975 crypto_free_tfm(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001976 }
1977
Mike Christie5bb0b552006-04-06 21:26:46 -05001978 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001979}
1980
Mike Christie1c834692006-07-24 15:47:26 -05001981static void
1982iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1983{
1984 struct iscsi_conn *conn = cls_conn->dd_data;
1985
1986 iscsi_conn_stop(cls_conn, flag);
1987 iscsi_tcp_release_conn(conn);
1988}
1989
Alex Aizman7ba24712005-08-04 19:30:08 -07001990static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001991iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001992 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001993 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001994{
Mike Christie5bb0b552006-04-06 21:26:46 -05001995 struct iscsi_conn *conn = cls_conn->dd_data;
1996 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001997 struct sock *sk;
1998 struct socket *sock;
1999 int err;
2000
2001 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05002002 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07002003 if (!sock) {
2004 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
2005 return -EEXIST;
2006 }
2007
Mike Christie5bb0b552006-04-06 21:26:46 -05002008 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
2009 if (err)
2010 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07002011
Mike Christie67a61112006-05-30 00:37:20 -05002012 /* bind iSCSI connection and socket */
2013 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07002014
Mike Christie67a61112006-05-30 00:37:20 -05002015 /* setup Socket parameters */
2016 sk = sock->sk;
2017 sk->sk_reuse = 1;
2018 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
2019 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07002020
Mike Christie67a61112006-05-30 00:37:20 -05002021 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07002022
Mike Christie67a61112006-05-30 00:37:20 -05002023 /*
2024 * Intercept TCP callbacks for sendfile like receive
2025 * processing.
2026 */
2027 conn->recv_lock = &sk->sk_callback_lock;
2028 iscsi_conn_set_callbacks(conn);
2029 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
2030 /*
2031 * set receive state machine into initial state
2032 */
2033 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07002034
Alex Aizman7ba24712005-08-04 19:30:08 -07002035 return 0;
2036}
2037
Mike Christie5bb0b552006-04-06 21:26:46 -05002038/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05002039static void
Mike Christie5bb0b552006-04-06 21:26:46 -05002040iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask,
2041 char *data, uint32_t data_size)
Mike Christie30a6c652006-04-06 21:13:39 -05002042{
Mike Christie5bb0b552006-04-06 21:26:46 -05002043 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie30a6c652006-04-06 21:13:39 -05002044
Mike Christie6e458cc2006-05-18 20:31:31 -05002045 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
2046 sizeof(struct iscsi_hdr));
Mike Christie5bb0b552006-04-06 21:26:46 -05002047 tcp_mtask->xmstate = XMSTATE_IMM_HDR;
Mike Christieb6c395e2006-07-24 15:47:15 -05002048 tcp_mtask->sent = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002049
Mike Christie5bb0b552006-04-06 21:26:46 -05002050 if (mtask->data_count)
2051 iscsi_buf_init_iov(&tcp_mtask->sendbuf, (char*)mtask->data,
Alex Aizman7ba24712005-08-04 19:30:08 -07002052 mtask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07002053}
2054
2055static int
2056iscsi_r2tpool_alloc(struct iscsi_session *session)
2057{
2058 int i;
2059 int cmd_i;
2060
2061 /*
2062 * initialize per-task: R2T pool and xmit queue
2063 */
2064 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2065 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05002066 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002067
2068 /*
2069 * pre-allocated x4 as much r2ts to handle race when
2070 * target acks DataOut faster than we data_xmit() queues
2071 * could replenish r2tqueue.
2072 */
2073
2074 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05002075 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
2076 (void***)&tcp_ctask->r2ts,
2077 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002078 goto r2t_alloc_fail;
2079 }
2080
2081 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05002082 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07002083 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05002084 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2085 iscsi_pool_free(&tcp_ctask->r2tpool,
2086 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002087 goto r2t_alloc_fail;
2088 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002089 }
2090
2091 return 0;
2092
2093r2t_alloc_fail:
2094 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002095 struct iscsi_cmd_task *ctask = session->cmds[i];
2096 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2097
Mike Christie5bb0b552006-04-06 21:26:46 -05002098 kfifo_free(tcp_ctask->r2tqueue);
2099 iscsi_pool_free(&tcp_ctask->r2tpool,
2100 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002101 }
2102 return -ENOMEM;
2103}
2104
2105static void
2106iscsi_r2tpool_free(struct iscsi_session *session)
2107{
2108 int i;
2109
2110 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002111 struct iscsi_cmd_task *ctask = session->cmds[i];
2112 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2113
Mike Christie5bb0b552006-04-06 21:26:46 -05002114 kfifo_free(tcp_ctask->r2tqueue);
2115 iscsi_pool_free(&tcp_ctask->r2tpool,
2116 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002117 }
2118}
2119
Alex Aizman7ba24712005-08-04 19:30:08 -07002120static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002121iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002122 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07002123{
Mike Christie7b7232f2006-02-01 21:06:49 -06002124 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002125 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05002126 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002127 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07002128
Alex Aizman7ba24712005-08-04 19:30:08 -07002129 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002130 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002131 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002132 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07002133 if (conn->hdrdgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002134 tcp_conn->hdr_size += sizeof(__u32);
2135 if (!tcp_conn->tx_tfm)
2136 tcp_conn->tx_tfm = crypto_alloc_tfm("crc32c",
2137 0);
2138 if (!tcp_conn->tx_tfm)
Alex Aizman7ba24712005-08-04 19:30:08 -07002139 return -ENOMEM;
Mike Christie5bb0b552006-04-06 21:26:46 -05002140 if (!tcp_conn->rx_tfm)
2141 tcp_conn->rx_tfm = crypto_alloc_tfm("crc32c",
2142 0);
2143 if (!tcp_conn->rx_tfm) {
2144 crypto_free_tfm(tcp_conn->tx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002145 return -ENOMEM;
2146 }
2147 } else {
Mike Christie5bb0b552006-04-06 21:26:46 -05002148 if (tcp_conn->tx_tfm)
2149 crypto_free_tfm(tcp_conn->tx_tfm);
2150 if (tcp_conn->rx_tfm)
2151 crypto_free_tfm(tcp_conn->rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002152 }
2153 break;
2154 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002155 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002156 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002157 if (!tcp_conn->data_tx_tfm)
2158 tcp_conn->data_tx_tfm =
Alex Aizman7ba24712005-08-04 19:30:08 -07002159 crypto_alloc_tfm("crc32c", 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05002160 if (!tcp_conn->data_tx_tfm)
Alex Aizman7ba24712005-08-04 19:30:08 -07002161 return -ENOMEM;
Mike Christie5bb0b552006-04-06 21:26:46 -05002162 if (!tcp_conn->data_rx_tfm)
2163 tcp_conn->data_rx_tfm =
Alex Aizman7ba24712005-08-04 19:30:08 -07002164 crypto_alloc_tfm("crc32c", 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05002165 if (!tcp_conn->data_rx_tfm) {
2166 crypto_free_tfm(tcp_conn->data_tx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002167 return -ENOMEM;
2168 }
2169 } else {
Mike Christie5bb0b552006-04-06 21:26:46 -05002170 if (tcp_conn->data_tx_tfm)
2171 crypto_free_tfm(tcp_conn->data_tx_tfm);
2172 if (tcp_conn->data_rx_tfm)
2173 crypto_free_tfm(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002174 }
Mike Christie5bb0b552006-04-06 21:26:46 -05002175 tcp_conn->sendpage = conn->datadgst_en ?
2176 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07002177 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002178 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002179 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07002180 if (session->max_r2t == roundup_pow_of_two(value))
2181 break;
2182 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002183 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002184 if (session->max_r2t & (session->max_r2t - 1))
2185 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2186 if (iscsi_r2tpool_alloc(session))
2187 return -ENOMEM;
2188 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002189 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002190 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002191 }
2192
2193 return 0;
2194}
2195
2196static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05002197iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2198 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06002199{
Mike Christie7b7232f2006-02-01 21:06:49 -06002200 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002201 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christiefd7255f2006-04-06 21:13:36 -05002202 struct inet_sock *inet;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002203 struct ipv6_pinfo *np;
2204 struct sock *sk;
2205 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06002206
2207 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002208 case ISCSI_PARAM_CONN_PORT:
2209 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002210 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002211 mutex_unlock(&conn->xmitmutex);
2212 return -EINVAL;
2213 }
2214
Mike Christie5bb0b552006-04-06 21:26:46 -05002215 inet = inet_sk(tcp_conn->sock->sk);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002216 len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
Mike Christiefd7255f2006-04-06 21:13:36 -05002217 mutex_unlock(&conn->xmitmutex);
Mike Christie8d2860b2006-05-02 19:46:47 -05002218 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002219 case ISCSI_PARAM_CONN_ADDRESS:
2220 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002221 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002222 mutex_unlock(&conn->xmitmutex);
2223 return -EINVAL;
2224 }
2225
Mike Christie5bb0b552006-04-06 21:26:46 -05002226 sk = tcp_conn->sock->sk;
Mike Christiefd7255f2006-04-06 21:13:36 -05002227 if (sk->sk_family == PF_INET) {
2228 inet = inet_sk(sk);
2229 len = sprintf(buf, "%u.%u.%u.%u\n",
2230 NIPQUAD(inet->daddr));
2231 } else {
2232 np = inet6_sk(sk);
2233 len = sprintf(buf,
2234 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
2235 NIP6(np->daddr));
2236 }
2237 mutex_unlock(&conn->xmitmutex);
2238 break;
2239 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002240 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05002241 }
2242
2243 return len;
2244}
2245
Alex Aizman7ba24712005-08-04 19:30:08 -07002246static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002247iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002248{
Mike Christie7b7232f2006-02-01 21:06:49 -06002249 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002250 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002251
2252 stats->txdata_octets = conn->txdata_octets;
2253 stats->rxdata_octets = conn->rxdata_octets;
2254 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2255 stats->dataout_pdus = conn->dataout_pdus_cnt;
2256 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2257 stats->datain_pdus = conn->datain_pdus_cnt;
2258 stats->r2t_pdus = conn->r2t_pdus_cnt;
2259 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2260 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2261 stats->custom_length = 3;
2262 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002263 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002264 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002265 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002266 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2267 stats->custom[2].value = conn->eh_abort_cnt;
2268}
2269
Mike Christie5bb0b552006-04-06 21:26:46 -05002270static struct iscsi_cls_session *
2271iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2272 struct scsi_transport_template *scsit,
2273 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002274{
Mike Christie5bb0b552006-04-06 21:26:46 -05002275 struct iscsi_cls_session *cls_session;
2276 struct iscsi_session *session;
2277 uint32_t hn;
2278 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002279
Mike Christie5bb0b552006-04-06 21:26:46 -05002280 cls_session = iscsi_session_setup(iscsit, scsit,
2281 sizeof(struct iscsi_tcp_cmd_task),
2282 sizeof(struct iscsi_tcp_mgmt_task),
2283 initial_cmdsn, &hn);
2284 if (!cls_session)
2285 return NULL;
2286 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002287
Mike Christie5bb0b552006-04-06 21:26:46 -05002288 session = class_to_transport_session(cls_session);
2289 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2290 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2291 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2292
2293 ctask->hdr = &tcp_ctask->hdr;
2294 }
2295
2296 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2297 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2298 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2299
2300 mtask->hdr = &tcp_mtask->hdr;
2301 }
2302
2303 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2304 goto r2tpool_alloc_fail;
2305
2306 return cls_session;
2307
2308r2tpool_alloc_fail:
2309 iscsi_session_teardown(cls_session);
2310 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002311}
2312
Mike Christie5bb0b552006-04-06 21:26:46 -05002313static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2314{
Mike Christie5bb0b552006-04-06 21:26:46 -05002315 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2316 iscsi_session_teardown(cls_session);
2317}
2318
2319static struct scsi_host_template iscsi_sht = {
Mike Christiee0ecae82006-05-18 20:31:43 -05002320 .name = "iSCSI Initiator over TCP/IP, v"
2321 ISCSI_TCP_VERSION,
Mike Christie5bb0b552006-04-06 21:26:46 -05002322 .queuecommand = iscsi_queuecommand,
2323 .change_queue_depth = iscsi_change_queue_depth,
2324 .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
2325 .sg_tablesize = ISCSI_SG_TABLESIZE,
2326 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2327 .eh_abort_handler = iscsi_eh_abort,
2328 .eh_host_reset_handler = iscsi_eh_host_reset,
2329 .use_clustering = DISABLE_CLUSTERING,
2330 .proc_name = "iscsi_tcp",
2331 .this_id = -1,
2332};
2333
Alex Aizman7ba24712005-08-04 19:30:08 -07002334static struct iscsi_transport iscsi_tcp_transport = {
2335 .owner = THIS_MODULE,
2336 .name = "tcp",
2337 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2338 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002339 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2340 ISCSI_MAX_XMIT_DLENGTH |
2341 ISCSI_HDRDGST_EN |
2342 ISCSI_DATADGST_EN |
2343 ISCSI_INITIAL_R2T_EN |
2344 ISCSI_MAX_R2T |
2345 ISCSI_IMM_DATA_EN |
2346 ISCSI_FIRST_BURST |
2347 ISCSI_MAX_BURST |
2348 ISCSI_PDU_INORDER_EN |
2349 ISCSI_DATASEQ_INORDER_EN |
2350 ISCSI_ERL |
2351 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002352 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05002353 ISCSI_EXP_STATSN |
2354 ISCSI_PERSISTENT_PORT |
2355 ISCSI_PERSISTENT_ADDRESS |
2356 ISCSI_TARGET_NAME |
2357 ISCSI_TPGT,
Alex Aizman7ba24712005-08-04 19:30:08 -07002358 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002359 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002360 .max_conn = 1,
2361 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002362 /* session management */
2363 .create_session = iscsi_tcp_session_create,
2364 .destroy_session = iscsi_tcp_session_destroy,
2365 /* connection management */
2366 .create_conn = iscsi_tcp_conn_create,
2367 .bind_conn = iscsi_tcp_conn_bind,
2368 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002369 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002370 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002371 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002372 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05002373 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie5bb0b552006-04-06 21:26:46 -05002374 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002375 .send_pdu = iscsi_conn_send_pdu,
2376 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002377 .init_cmd_task = iscsi_tcp_cmd_init,
2378 .init_mgmt_task = iscsi_tcp_mgmt_init,
2379 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2380 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2381 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2382 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002383 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002384};
2385
2386static int __init
2387iscsi_tcp_init(void)
2388{
Alex Aizman7ba24712005-08-04 19:30:08 -07002389 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002390 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2391 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002392 return -EINVAL;
2393 }
2394 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2395
Mike Christie7b8631b2006-01-13 18:05:50 -06002396 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002397 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002398
Mike Christie7b8631b2006-01-13 18:05:50 -06002399 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002400}
2401
2402static void __exit
2403iscsi_tcp_exit(void)
2404{
2405 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002406}
2407
2408module_init(iscsi_tcp_init);
2409module_exit(iscsi_tcp_exit);