blob: a21455d0274cb5d634a40baf768726ff4daf1472 [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>
Mike Christie4e7aba72007-05-30 12:57:23 -050032#include <linux/file.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070033#include <linux/blkdev.h>
34#include <linux/crypto.h>
35#include <linux/delay.h>
36#include <linux/kfifo.h>
37#include <linux/scatterlist.h>
38#include <net/tcp.h>
39#include <scsi/scsi_cmnd.h>
Mike Christied1d81c02007-05-30 12:57:21 -050040#include <scsi/scsi_device.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070041#include <scsi/scsi_host.h>
42#include <scsi/scsi.h>
43#include <scsi/scsi_transport_iscsi.h>
44
45#include "iscsi_tcp.h"
46
47MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
48 "Alex Aizman <itn780@yahoo.com>");
49MODULE_DESCRIPTION("iSCSI/TCP data-path");
50MODULE_LICENSE("GPL");
Alex Aizman7ba24712005-08-04 19:30:08 -070051/* #define DEBUG_TCP */
Alex Aizman7ba24712005-08-04 19:30:08 -070052#define DEBUG_ASSERT
53
54#ifdef DEBUG_TCP
Mike Christie5bb0b552006-04-06 21:26:46 -050055#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
Alex Aizman7ba24712005-08-04 19:30:08 -070056#else
57#define debug_tcp(fmt...)
58#endif
59
Alex Aizman7ba24712005-08-04 19:30:08 -070060#ifndef DEBUG_ASSERT
61#ifdef BUG_ON
62#undef BUG_ON
63#endif
64#define BUG_ON(expr)
65#endif
66
Alex Aizman7ba24712005-08-04 19:30:08 -070067static unsigned int iscsi_max_lun = 512;
68module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
69
Alex Aizman7ba24712005-08-04 19:30:08 -070070static inline void
Alex Aizman7ba24712005-08-04 19:30:08 -070071iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
72{
Mike Christie7cae5152006-01-13 18:05:47 -060073 ibuf->sg.page = virt_to_page(vbuf);
74 ibuf->sg.offset = offset_in_page(vbuf);
Alex Aizman7ba24712005-08-04 19:30:08 -070075 ibuf->sg.length = size;
76 ibuf->sent = 0;
Mike Christie7cae5152006-01-13 18:05:47 -060077 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070078}
79
80static inline void
81iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
82{
Mike Christie7cae5152006-01-13 18:05:47 -060083 ibuf->sg.page = sg->page;
84 ibuf->sg.offset = sg->offset;
85 ibuf->sg.length = sg->length;
Alex Aizman7ba24712005-08-04 19:30:08 -070086 /*
87 * Fastpath: sg element fits into single page
88 */
Mike Christiea1e80c22006-01-13 18:05:56 -060089 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
Mike Christie7cae5152006-01-13 18:05:47 -060090 ibuf->use_sendmsg = 0;
91 else
92 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070093 ibuf->sent = 0;
94}
95
96static inline int
97iscsi_buf_left(struct iscsi_buf *ibuf)
98{
99 int rc;
100
101 rc = ibuf->sg.length - ibuf->sent;
102 BUG_ON(rc < 0);
103 return rc;
104}
105
106static inline void
Mike Christieaf973482005-09-12 21:01:32 -0500107iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
108 u8* crc)
Alex Aizman7ba24712005-08-04 19:30:08 -0700109{
Mike Christie5bb0b552006-04-06 21:26:46 -0500110 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
111
James Bottomleyc9802cd2006-09-23 15:33:43 -0500112 crypto_hash_digest(&tcp_conn->tx_hash, &buf->sg, buf->sg.length, crc);
Mike Christie218432c2007-05-30 12:57:17 -0500113 buf->sg.length += sizeof(u32);
Alex Aizman7ba24712005-08-04 19:30:08 -0700114}
115
Alex Aizman7ba24712005-08-04 19:30:08 -0700116static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500117iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700118{
Mike Christie5bb0b552006-04-06 21:26:46 -0500119 struct sk_buff *skb = tcp_conn->in.skb;
Alex Aizman7ba24712005-08-04 19:30:08 -0700120
Mike Christie5bb0b552006-04-06 21:26:46 -0500121 tcp_conn->in.zero_copy_hdr = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700122
Mike Christie5bb0b552006-04-06 21:26:46 -0500123 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
124 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700125 /*
126 * Zero-copy PDU Header: using connection context
127 * to store header pointer.
128 */
129 if (skb_shinfo(skb)->frag_list == NULL &&
Mike Christie5bb0b552006-04-06 21:26:46 -0500130 !skb_shinfo(skb)->nr_frags) {
131 tcp_conn->in.hdr = (struct iscsi_hdr *)
132 ((char*)skb->data + tcp_conn->in.offset);
133 tcp_conn->in.zero_copy_hdr = 1;
134 } else {
Alex Aizman7ba24712005-08-04 19:30:08 -0700135 /* ignoring return code since we checked
136 * in.copy before */
Mike Christie5bb0b552006-04-06 21:26:46 -0500137 skb_copy_bits(skb, tcp_conn->in.offset,
138 &tcp_conn->hdr, tcp_conn->hdr_size);
139 tcp_conn->in.hdr = &tcp_conn->hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700140 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500141 tcp_conn->in.offset += tcp_conn->hdr_size;
142 tcp_conn->in.copy -= tcp_conn->hdr_size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700143 } else {
144 int hdr_remains;
145 int copylen;
146
147 /*
148 * PDU header scattered across SKB's,
149 * copying it... This'll happen quite rarely.
150 */
151
Mike Christie5bb0b552006-04-06 21:26:46 -0500152 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
153 tcp_conn->in.hdr_offset = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700154
Mike Christie5bb0b552006-04-06 21:26:46 -0500155 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700156 BUG_ON(hdr_remains <= 0);
157
Mike Christie5bb0b552006-04-06 21:26:46 -0500158 copylen = min(tcp_conn->in.copy, hdr_remains);
159 skb_copy_bits(skb, tcp_conn->in.offset,
160 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
161 copylen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700162
163 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
Mike Christie5bb0b552006-04-06 21:26:46 -0500164 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
165 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700166
Mike Christie5bb0b552006-04-06 21:26:46 -0500167 tcp_conn->in.offset += copylen;
168 tcp_conn->in.copy -= copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700169 if (copylen < hdr_remains) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500170 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
171 tcp_conn->in.hdr_offset += copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700172 return -EAGAIN;
173 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500174 tcp_conn->in.hdr = &tcp_conn->hdr;
175 tcp_conn->discontiguous_hdr_cnt++;
176 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700177 }
178
179 return 0;
180}
181
Mike Christie30a6c652006-04-06 21:13:39 -0500182/*
183 * must be called with session lock
184 */
185static void
Mike Christieb6c395e2006-07-24 15:47:15 -0500186iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700187{
Mike Christie5bb0b552006-04-06 21:26:46 -0500188 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500189 struct iscsi_r2t_info *r2t;
Mike Christie30a6c652006-04-06 21:13:39 -0500190 struct scsi_cmnd *sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700191
Mike Christieb6c395e2006-07-24 15:47:15 -0500192 /* flush ctask's r2t queues */
193 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
194 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
195 sizeof(void*));
196 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
197 }
198
Mike Christie30a6c652006-04-06 21:13:39 -0500199 sc = ctask->sc;
200 if (unlikely(!sc))
Alex Aizman7ba24712005-08-04 19:30:08 -0700201 return;
Mike Christie30a6c652006-04-06 21:13:39 -0500202
Mike Christie5bb0b552006-04-06 21:26:46 -0500203 tcp_ctask->xmstate = XMSTATE_IDLE;
204 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700205}
206
207/**
208 * iscsi_data_rsp - SCSI Data-In Response processing
209 * @conn: iscsi connection
210 * @ctask: scsi command task
211 **/
212static int
213iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
214{
Mike Christie5bb0b552006-04-06 21:26:46 -0500215 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
216 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
217 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700218 struct iscsi_session *session = conn->session;
Mike Christie857ae0b2007-05-30 12:57:15 -0500219 struct scsi_cmnd *sc = ctask->sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700220 int datasn = be32_to_cpu(rhdr->datasn);
221
Mike Christie77a23c22007-05-30 12:57:18 -0500222 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Alex Aizman7ba24712005-08-04 19:30:08 -0700223 /*
224 * setup Data-In byte counter (gets decremented..)
225 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500226 ctask->data_count = tcp_conn->in.datalen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700227
Mike Christie5bb0b552006-04-06 21:26:46 -0500228 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700229 return 0;
230
Mike Christied473cc72007-05-30 12:57:14 -0500231 if (tcp_ctask->exp_datasn != datasn) {
232 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
233 __FUNCTION__, tcp_ctask->exp_datasn, datasn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700234 return ISCSI_ERR_DATASN;
Mike Christied473cc72007-05-30 12:57:14 -0500235 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700236
Mike Christied473cc72007-05-30 12:57:14 -0500237 tcp_ctask->exp_datasn++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700238
Mike Christie5bb0b552006-04-06 21:26:46 -0500239 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900240 if (tcp_ctask->data_offset + tcp_conn->in.datalen > scsi_bufflen(sc)) {
Mike Christie857ae0b2007-05-30 12:57:15 -0500241 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
242 __FUNCTION__, tcp_ctask->data_offset,
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900243 tcp_conn->in.datalen, scsi_bufflen(sc));
Alex Aizman7ba24712005-08-04 19:30:08 -0700244 return ISCSI_ERR_DATA_OFFSET;
Mike Christie857ae0b2007-05-30 12:57:15 -0500245 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700246
247 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700248 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600249 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700250 int res_count = be32_to_cpu(rhdr->residual_count);
251
252 if (res_count > 0 &&
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900253 res_count <= scsi_bufflen(sc)) {
254 scsi_set_resid(sc, res_count);
Alex Aizman7ba24712005-08-04 19:30:08 -0700255 sc->result = (DID_OK << 16) | rhdr->cmd_status;
256 } else
257 sc->result = (DID_BAD_TARGET << 16) |
258 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600259 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900260 scsi_set_resid(sc, be32_to_cpu(rhdr->residual_count));
Alex Aizman7ba24712005-08-04 19:30:08 -0700261 sc->result = (DID_OK << 16) | rhdr->cmd_status;
262 } else
263 sc->result = (DID_OK << 16) | rhdr->cmd_status;
264 }
265
266 conn->datain_pdus_cnt++;
267 return 0;
268}
269
270/**
271 * iscsi_solicit_data_init - initialize first Data-Out
272 * @conn: iscsi connection
273 * @ctask: scsi command task
274 * @r2t: R2T info
275 *
276 * Notes:
277 * Initialize first Data-Out within this R2T sequence and finds
278 * proper data_offset within this SCSI command.
279 *
280 * This function is called with connection lock taken.
281 **/
282static void
283iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
284 struct iscsi_r2t_info *r2t)
285{
286 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700287 struct scsi_cmnd *sc = ctask->sc;
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900288 int i, sg_count = 0;
289 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700290
Mike Christieffbfe922006-05-18 20:31:36 -0500291 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700292 memset(hdr, 0, sizeof(struct iscsi_data));
293 hdr->ttt = r2t->ttt;
294 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
295 r2t->solicit_datasn++;
296 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500297 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
298 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700299 hdr->exp_statsn = r2t->exp_statsn;
300 hdr->offset = cpu_to_be32(r2t->data_offset);
301 if (r2t->data_length > conn->max_xmit_dlength) {
302 hton24(hdr->dlength, conn->max_xmit_dlength);
303 r2t->data_count = conn->max_xmit_dlength;
304 hdr->flags = 0;
305 } else {
306 hton24(hdr->dlength, r2t->data_length);
307 r2t->data_count = r2t->data_length;
308 hdr->flags = ISCSI_FLAG_CMD_FINAL;
309 }
310 conn->dataout_pdus_cnt++;
311
312 r2t->sent = 0;
313
Mike Christie6e458cc2006-05-18 20:31:31 -0500314 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -0500315 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -0700316
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900317 sg = scsi_sglist(sc);
318 r2t->sg = NULL;
319 for (i = 0; i < scsi_sg_count(sc); i++, sg += 1) {
320 /* FIXME: prefetch ? */
321 if (sg_count + sg->length > r2t->data_offset) {
322 int page_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700323
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900324 /* sg page found! */
Alex Aizman7ba24712005-08-04 19:30:08 -0700325
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900326 /* offset within this page */
327 page_offset = r2t->data_offset - sg_count;
Alex Aizman7ba24712005-08-04 19:30:08 -0700328
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900329 /* fill in this buffer */
330 iscsi_buf_init_sg(&r2t->sendbuf, sg);
331 r2t->sendbuf.sg.offset += page_offset;
332 r2t->sendbuf.sg.length -= page_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700333
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900334 /* xmit logic will continue with next one */
335 r2t->sg = sg + 1;
336 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700337 }
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900338 sg_count += sg->length;
Mike Christie62f38302006-08-31 18:09:27 -0400339 }
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900340 BUG_ON(r2t->sg == NULL);
Alex Aizman7ba24712005-08-04 19:30:08 -0700341}
342
343/**
344 * iscsi_r2t_rsp - iSCSI R2T Response processing
345 * @conn: iscsi connection
346 * @ctask: scsi command task
347 **/
348static int
349iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
350{
351 struct iscsi_r2t_info *r2t;
352 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500353 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
354 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
355 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700356 int r2tsn = be32_to_cpu(rhdr->r2tsn);
357 int rc;
358
Mike Christie98a94162006-08-31 18:09:26 -0400359 if (tcp_conn->in.datalen) {
360 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
361 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700362 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400363 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700364
Mike Christied473cc72007-05-30 12:57:14 -0500365 if (tcp_ctask->exp_datasn != r2tsn){
366 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
367 __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700368 return ISCSI_ERR_R2TSN;
Mike Christied473cc72007-05-30 12:57:14 -0500369 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700370
Alex Aizman7ba24712005-08-04 19:30:08 -0700371 /* fill-in new R2T associated with the task */
372 spin_lock(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -0500373 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
374
Alex Aizman7ba24712005-08-04 19:30:08 -0700375 if (!ctask->sc || ctask->mtask ||
376 session->state != ISCSI_STATE_LOGGED_IN) {
377 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
378 "recovery...\n", ctask->itt);
379 spin_unlock(&session->lock);
380 return 0;
381 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500382
Mike Christie5bb0b552006-04-06 21:26:46 -0500383 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700384 BUG_ON(!rc);
385
386 r2t->exp_statsn = rhdr->statsn;
387 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400388 if (r2t->data_length == 0) {
389 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
Alex Aizman7ba24712005-08-04 19:30:08 -0700390 spin_unlock(&session->lock);
391 return ISCSI_ERR_DATALEN;
392 }
393
Mike Christie98a94162006-08-31 18:09:26 -0400394 if (r2t->data_length > session->max_burst)
395 debug_scsi("invalid R2T with data len %u and max burst %u."
396 "Attempting to execute request.\n",
397 r2t->data_length, session->max_burst);
398
Alex Aizman7ba24712005-08-04 19:30:08 -0700399 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900400 if (r2t->data_offset + r2t->data_length > scsi_bufflen(ctask->sc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700401 spin_unlock(&session->lock);
Mike Christie98a94162006-08-31 18:09:26 -0400402 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
403 "offset %u and total length %d\n", r2t->data_length,
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900404 r2t->data_offset, scsi_bufflen(ctask->sc));
Alex Aizman7ba24712005-08-04 19:30:08 -0700405 return ISCSI_ERR_DATALEN;
406 }
407
408 r2t->ttt = rhdr->ttt; /* no flip */
409 r2t->solicit_datasn = 0;
410
411 iscsi_solicit_data_init(conn, ctask, r2t);
412
Mike Christied473cc72007-05-30 12:57:14 -0500413 tcp_ctask->exp_datasn = r2tsn + 1;
Mike Christie5bb0b552006-04-06 21:26:46 -0500414 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christie218432c2007-05-30 12:57:17 -0500415 tcp_ctask->xmstate |= XMSTATE_SOL_HDR_INIT;
Mike Christieb6c395e2006-07-24 15:47:15 -0500416 list_move_tail(&ctask->running, &conn->xmitqueue);
Alex Aizman7ba24712005-08-04 19:30:08 -0700417
Mike Christie55e32992006-01-13 18:05:53 -0600418 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -0700419 conn->r2t_pdus_cnt++;
420 spin_unlock(&session->lock);
421
422 return 0;
423}
424
425static int
Mike Christie5bb0b552006-04-06 21:26:46 -0500426iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700427{
Mike Christie5bb0b552006-04-06 21:26:46 -0500428 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700429 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700430 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500431 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
432 uint32_t cdgst, rdgst = 0, itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700433
Mike Christie5bb0b552006-04-06 21:26:46 -0500434 hdr = tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700435
436 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500437 tcp_conn->in.datalen = ntoh24(hdr->dlength);
438 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700439 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500440 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700441 return ISCSI_ERR_DATALEN;
442 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500443 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700444
445 /* read AHS */
Mike Christie5bb0b552006-04-06 21:26:46 -0500446 ahslen = hdr->hlength << 2;
447 tcp_conn->in.offset += ahslen;
448 tcp_conn->in.copy -= ahslen;
449 if (tcp_conn->in.copy < 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700450 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
Mike Christie5bb0b552006-04-06 21:26:46 -0500451 "%d bytes\n", ahslen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700452 return ISCSI_ERR_AHSLEN;
453 }
454
455 /* calculate read padding */
Mike Christie5bb0b552006-04-06 21:26:46 -0500456 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
457 if (tcp_conn->in.padding) {
458 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
459 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
Alex Aizman7ba24712005-08-04 19:30:08 -0700460 }
461
462 if (conn->hdrdgst_en) {
463 struct scatterlist sg;
464
465 sg_init_one(&sg, (u8 *)hdr,
Mike Christie5bb0b552006-04-06 21:26:46 -0500466 sizeof(struct iscsi_hdr) + ahslen);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500467 crypto_hash_digest(&tcp_conn->rx_hash, &sg, sg.length,
468 (u8 *)&cdgst);
Alex Aizman7ba24712005-08-04 19:30:08 -0700469 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
Mike Christie5bb0b552006-04-06 21:26:46 -0500470 ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600471 if (cdgst != rdgst) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500472 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
473 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
Mike Christie8a47cd32005-11-30 02:27:19 -0600474 return ISCSI_ERR_HDR_DGST;
475 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700476 }
477
Mike Christie5bb0b552006-04-06 21:26:46 -0500478 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700479 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500480 rc = iscsi_verify_itt(conn, hdr, &itt);
481 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
482 tcp_conn->in.datalen = 0; /* force drop */
483 return 0;
484 } else if (rc)
485 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700486
487 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500488 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
489 ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700490
Mike Christie5bb0b552006-04-06 21:26:46 -0500491 switch(opcode) {
492 case ISCSI_OP_SCSI_DATA_IN:
493 tcp_conn->in.ctask = session->cmds[itt];
494 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
Mike Christie275fd7d2006-07-24 15:47:17 -0500495 if (rc)
496 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500497 /* fall through */
498 case ISCSI_OP_SCSI_CMD_RSP:
499 tcp_conn->in.ctask = session->cmds[itt];
500 if (tcp_conn->in.datalen)
501 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700502
Mike Christie5bb0b552006-04-06 21:26:46 -0500503 spin_lock(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500504 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
505 spin_unlock(&session->lock);
506 break;
507 case ISCSI_OP_R2T:
508 tcp_conn->in.ctask = session->cmds[itt];
509 if (ahslen)
510 rc = ISCSI_ERR_AHSLEN;
511 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
512 DMA_TO_DEVICE)
513 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
514 else
515 rc = ISCSI_ERR_PROTO;
516 break;
517 case ISCSI_OP_LOGIN_RSP:
518 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500519 case ISCSI_OP_REJECT:
520 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500521 /*
522 * It is possible that we could get a PDU with a buffer larger
523 * than 8K, but there are no targets that currently do this.
524 * For now we fail until we find a vendor that needs it
525 */
Mike Christiebf32ed32007-02-28 17:32:17 -0600526 if (ISCSI_DEF_MAX_RECV_SEG_LEN <
Mike Christiec8dc1e52006-07-24 15:47:39 -0500527 tcp_conn->in.datalen) {
528 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
529 "but conn buffer is only %u (opcode %0x)\n",
530 tcp_conn->in.datalen,
Mike Christiebf32ed32007-02-28 17:32:17 -0600531 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
Mike Christiec8dc1e52006-07-24 15:47:39 -0500532 rc = ISCSI_ERR_PROTO;
533 break;
534 }
535
Mike Christie5bb0b552006-04-06 21:26:46 -0500536 if (tcp_conn->in.datalen)
537 goto copy_hdr;
538 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500539 case ISCSI_OP_LOGOUT_RSP:
540 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500541 case ISCSI_OP_SCSI_TMFUNC_RSP:
542 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
543 break;
544 default:
545 rc = ISCSI_ERR_BAD_OPCODE;
546 break;
547 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700548
549 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500550
551copy_hdr:
552 /*
553 * if we did zero copy for the header but we will need multiple
554 * skbs to complete the command then we have to copy the header
555 * for later use
556 */
Mike Christie275fd7d2006-07-24 15:47:17 -0500557 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
Mike Christie5bb0b552006-04-06 21:26:46 -0500558 (tcp_conn->in.datalen + tcp_conn->in.padding +
559 (conn->datadgst_en ? 4 : 0))) {
560 debug_tcp("Copying header for later use. in.copy %d in.datalen"
561 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
562 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
563 sizeof(struct iscsi_hdr));
564 tcp_conn->in.hdr = &tcp_conn->hdr;
565 tcp_conn->in.zero_copy_hdr = 0;
566 }
567 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700568}
569
570/**
571 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
Mike Christie5bb0b552006-04-06 21:26:46 -0500572 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700573 * @ctask: scsi command task
574 * @buf: buffer to copy to
575 * @buf_size: size of buffer
576 * @offset: offset within the buffer
577 *
578 * Notes:
579 * The function calls skb_copy_bits() and updates per-connection and
580 * per-cmd byte counters.
581 *
582 * Read counters (in bytes):
583 *
584 * conn->in.offset offset within in progress SKB
585 * conn->in.copy left to copy from in progress SKB
586 * including padding
587 * conn->in.copied copied already from in progress SKB
588 * conn->data_copied copied already from in progress buffer
589 * ctask->sent total bytes sent up to the MidLayer
590 * ctask->data_count left to copy from in progress Data-In
591 * buf_left left to copy from in progress buffer
592 **/
593static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500594iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -0700595 void *buf, int buf_size, int offset)
596{
Mike Christie5bb0b552006-04-06 21:26:46 -0500597 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
598 int buf_left = buf_size - (tcp_conn->data_copied + offset);
Mike Christie857ae0b2007-05-30 12:57:15 -0500599 unsigned size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700600 int rc;
601
602 size = min(size, ctask->data_count);
603
604 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500605 size, tcp_conn->in.offset, tcp_conn->in.copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700606
607 BUG_ON(size <= 0);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900608 BUG_ON(tcp_ctask->sent + size > scsi_bufflen(ctask->sc));
Alex Aizman7ba24712005-08-04 19:30:08 -0700609
Mike Christie5bb0b552006-04-06 21:26:46 -0500610 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
611 (char*)buf + (offset + tcp_conn->data_copied), size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700612 /* must fit into skb->len */
613 BUG_ON(rc);
614
Mike Christie5bb0b552006-04-06 21:26:46 -0500615 tcp_conn->in.offset += size;
616 tcp_conn->in.copy -= size;
617 tcp_conn->in.copied += size;
618 tcp_conn->data_copied += size;
619 tcp_ctask->sent += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700620 ctask->data_count -= size;
621
Mike Christie5bb0b552006-04-06 21:26:46 -0500622 BUG_ON(tcp_conn->in.copy < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700623 BUG_ON(ctask->data_count < 0);
624
Mike Christie5bb0b552006-04-06 21:26:46 -0500625 if (buf_size != (tcp_conn->data_copied + offset)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700626 if (!ctask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500627 BUG_ON(buf_size - tcp_conn->data_copied < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700628 /* done with this PDU */
Mike Christie5bb0b552006-04-06 21:26:46 -0500629 return buf_size - tcp_conn->data_copied;
Alex Aizman7ba24712005-08-04 19:30:08 -0700630 }
631 return -EAGAIN;
632 }
633
634 /* done with this buffer or with both - PDU and buffer */
Mike Christie5bb0b552006-04-06 21:26:46 -0500635 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700636 return 0;
637}
638
639/**
640 * iscsi_tcp_copy - copy skb bits to the destanation buffer
Mike Christie5bb0b552006-04-06 21:26:46 -0500641 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700642 *
643 * Notes:
644 * The function calls skb_copy_bits() and updates per-connection
645 * byte counters.
646 **/
647static inline int
Mike Christieca518682006-08-31 18:09:32 -0400648iscsi_tcp_copy(struct iscsi_conn *conn, int buf_size)
Alex Aizman7ba24712005-08-04 19:30:08 -0700649{
Mike Christiec8dc1e52006-07-24 15:47:39 -0500650 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500651 int buf_left = buf_size - tcp_conn->data_copied;
652 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700653 int rc;
654
655 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500656 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700657 BUG_ON(size <= 0);
658
Mike Christie5bb0b552006-04-06 21:26:46 -0500659 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christiec8dc1e52006-07-24 15:47:39 -0500660 (char*)conn->data + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700661 BUG_ON(rc);
662
Mike Christie5bb0b552006-04-06 21:26:46 -0500663 tcp_conn->in.offset += size;
664 tcp_conn->in.copy -= size;
665 tcp_conn->in.copied += size;
666 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700667
Mike Christie5bb0b552006-04-06 21:26:46 -0500668 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700669 return -EAGAIN;
670
671 return 0;
672}
673
674static inline void
James Bottomleyc9802cd2006-09-23 15:33:43 -0500675partial_sg_digest_update(struct hash_desc *desc, struct scatterlist *sg,
Mike Christie62f38302006-08-31 18:09:27 -0400676 int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700677{
678 struct scatterlist temp;
679
680 memcpy(&temp, sg, sizeof(struct scatterlist));
681 temp.offset = offset;
682 temp.length = length;
James Bottomleyc9802cd2006-09-23 15:33:43 -0500683 crypto_hash_update(desc, &temp, length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700684}
685
Mike Christief6cfba12005-11-29 23:12:57 -0600686static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500687iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600688{
689 struct scatterlist tmp;
690
691 sg_init_one(&tmp, buf, len);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500692 crypto_hash_update(&tcp_conn->rx_hash, &tmp, len);
Mike Christief6cfba12005-11-29 23:12:57 -0600693}
694
Alex Aizman7ba24712005-08-04 19:30:08 -0700695static int iscsi_scsi_data_in(struct iscsi_conn *conn)
696{
Mike Christie5bb0b552006-04-06 21:26:46 -0500697 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
698 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
699 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700700 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600701 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700702 int i, offset, rc = 0;
703
704 BUG_ON((void*)ctask != sc->SCp.ptr);
705
Mike Christie5bb0b552006-04-06 21:26:46 -0500706 offset = tcp_ctask->data_offset;
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900707 sg = scsi_sglist(sc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700708
Mike Christie5bb0b552006-04-06 21:26:46 -0500709 if (tcp_ctask->data_offset)
710 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700711 offset -= sg[i].length;
712 /* we've passed through partial sg*/
713 if (offset < 0)
714 offset = 0;
715
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900716 for (i = tcp_ctask->sg_count; i < scsi_sg_count(sc); i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700717 char *dest;
718
719 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500720 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700721 sg[i].length, offset);
722 kunmap_atomic(dest, KM_SOFTIRQ0);
723 if (rc == -EAGAIN)
724 /* continue with the next SKB/PDU */
725 return rc;
726 if (!rc) {
727 if (conn->datadgst_en) {
728 if (!offset)
Herbert Xudc64ddf2006-08-24 18:45:50 +1000729 crypto_hash_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500730 &tcp_conn->rx_hash,
Arne Redlichc959e1c2006-12-17 12:10:24 -0600731 &sg[i], sg[i].length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700732 else
Mike Christie62f38302006-08-31 18:09:27 -0400733 partial_sg_digest_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500734 &tcp_conn->rx_hash,
Mike Christie5bb0b552006-04-06 21:26:46 -0500735 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700736 sg[i].offset + offset,
737 sg[i].length - offset);
738 }
739 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500740 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700741 }
742
743 if (!ctask->data_count) {
744 if (rc && conn->datadgst_en)
745 /*
746 * data-in is complete, but buffer not...
747 */
James Bottomleyc9802cd2006-09-23 15:33:43 -0500748 partial_sg_digest_update(&tcp_conn->rx_hash,
749 &sg[i],
750 sg[i].offset,
751 sg[i].length-rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700752 rc = 0;
753 break;
754 }
755
Mike Christie5bb0b552006-04-06 21:26:46 -0500756 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700757 return -EAGAIN;
758 }
759 BUG_ON(ctask->data_count);
760
Alex Aizman7ba24712005-08-04 19:30:08 -0700761 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500762 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Mike Christieb6c395e2006-07-24 15:47:15 -0500763 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
764 (long)sc, sc->result, ctask->itt,
765 tcp_conn->in.hdr->flags);
Mike Christie5bb0b552006-04-06 21:26:46 -0500766 spin_lock(&conn->session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500767 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
768 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700769 }
770
771 return rc;
772}
773
774static int
775iscsi_data_recv(struct iscsi_conn *conn)
776{
Mike Christie5bb0b552006-04-06 21:26:46 -0500777 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
778 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700779
Mike Christie5bb0b552006-04-06 21:26:46 -0500780 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
781 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700782 case ISCSI_OP_SCSI_DATA_IN:
783 rc = iscsi_scsi_data_in(conn);
784 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500785 case ISCSI_OP_SCSI_CMD_RSP:
Alex Aizman7ba24712005-08-04 19:30:08 -0700786 case ISCSI_OP_TEXT_RSP:
787 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500788 case ISCSI_OP_ASYNC_EVENT:
789 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700790 /*
791 * Collect data segment to the connection's data
792 * placeholder
793 */
Mike Christieca518682006-08-31 18:09:32 -0400794 if (iscsi_tcp_copy(conn, tcp_conn->in.datalen)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700795 rc = -EAGAIN;
796 goto exit;
797 }
798
Mike Christiec8dc1e52006-07-24 15:47:39 -0500799 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500800 tcp_conn->in.datalen);
801 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
Mike Christiec8dc1e52006-07-24 15:47:39 -0500802 iscsi_recv_digest_update(tcp_conn, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500803 tcp_conn->in.datalen);
804 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700805 default:
806 BUG_ON(1);
807 }
808exit:
809 return rc;
810}
811
812/**
813 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
814 * @rd_desc: read descriptor
815 * @skb: socket buffer
816 * @offset: offset in skb
817 * @len: skb->len - offset
818 **/
819static int
820iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
821 unsigned int offset, size_t len)
822{
823 int rc;
824 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500825 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700826 int processed;
827 char pad[ISCSI_PAD_LEN];
828 struct scatterlist sg;
829
830 /*
831 * Save current SKB and its offset in the corresponding
832 * connection context.
833 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500834 tcp_conn->in.copy = skb->len - offset;
835 tcp_conn->in.offset = offset;
836 tcp_conn->in.skb = skb;
837 tcp_conn->in.len = tcp_conn->in.copy;
838 BUG_ON(tcp_conn->in.copy <= 0);
839 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700840
841more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500842 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700843 rc = 0;
844
845 if (unlikely(conn->suspend_rx)) {
846 debug_tcp("conn %d Rx suspended!\n", conn->id);
847 return 0;
848 }
849
Mike Christie5bb0b552006-04-06 21:26:46 -0500850 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
851 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
852 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700853 if (rc) {
854 if (rc == -EAGAIN)
855 goto nomore;
856 else {
Mike Christied82967c2006-07-24 15:47:11 -0500857 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700858 return 0;
859 }
860 }
861
862 /*
863 * Verify and process incoming PDU header.
864 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500865 rc = iscsi_tcp_hdr_recv(conn);
866 if (!rc && tcp_conn->in.datalen) {
Mike Christiedd8c0d92006-08-31 18:09:28 -0400867 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -0500868 crypto_hash_init(&tcp_conn->rx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -0500869 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700870 } else if (rc) {
Mike Christie40527af2006-07-24 15:47:45 -0500871 iscsi_conn_failure(conn, rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700872 return 0;
873 }
874 }
875
Mike Christiedbdb0162007-05-30 12:57:20 -0500876 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV &&
877 tcp_conn->in.copy) {
Mike Christief6cfba12005-11-29 23:12:57 -0600878 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500879
Alex Aizman7ba24712005-08-04 19:30:08 -0700880 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500881 tcp_conn->in.offset, tcp_conn->in.copy);
Mike Christiedbdb0162007-05-30 12:57:20 -0500882
883 if (!tcp_conn->data_copied) {
884 if (tcp_conn->in.padding) {
885 debug_tcp("padding -> %d\n",
886 tcp_conn->in.padding);
887 memset(pad, 0, tcp_conn->in.padding);
888 sg_init_one(&sg, pad, tcp_conn->in.padding);
889 crypto_hash_update(&tcp_conn->rx_hash,
890 &sg, sg.length);
891 }
892 crypto_hash_final(&tcp_conn->rx_hash,
893 (u8 *) &tcp_conn->in.datadgst);
894 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
895 }
896
Mike Christieca518682006-08-31 18:09:32 -0400897 rc = iscsi_tcp_copy(conn, sizeof(uint32_t));
898 if (rc) {
899 if (rc == -EAGAIN)
900 goto again;
901 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
902 return 0;
903 }
904
905 memcpy(&recv_digest, conn->data, sizeof(uint32_t));
Mike Christie5bb0b552006-04-06 21:26:46 -0500906 if (recv_digest != tcp_conn->in.datadgst) {
Mike Christief6cfba12005-11-29 23:12:57 -0600907 debug_tcp("iscsi_tcp: data digest error!"
908 "0x%x != 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500909 tcp_conn->in.datadgst);
Mike Christief6cfba12005-11-29 23:12:57 -0600910 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
911 return 0;
912 } else {
913 debug_tcp("iscsi_tcp: data digest match!"
914 "0x%x == 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500915 tcp_conn->in.datadgst);
916 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700917 }
918 }
919
Mike Christie5bb0b552006-04-06 21:26:46 -0500920 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
Mike Christiedbdb0162007-05-30 12:57:20 -0500921 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700922 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500923 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700924
925 rc = iscsi_data_recv(conn);
926 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500927 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700928 goto again;
Mike Christied82967c2006-07-24 15:47:11 -0500929 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700930 return 0;
931 }
Mike Christiedbdb0162007-05-30 12:57:20 -0500932
933 if (tcp_conn->in.padding)
934 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
935 else if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500936 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Mike Christiedbdb0162007-05-30 12:57:20 -0500937 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500938 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Mike Christiedbdb0162007-05-30 12:57:20 -0500939 tcp_conn->data_copied = 0;
940 }
941
942 if (tcp_conn->in_progress == IN_PROGRESS_PAD_RECV &&
943 tcp_conn->in.copy) {
944 int copylen = min(tcp_conn->in.padding - tcp_conn->data_copied,
945 tcp_conn->in.copy);
946
947 tcp_conn->in.copy -= copylen;
948 tcp_conn->in.offset += copylen;
949 tcp_conn->data_copied += copylen;
950
951 if (tcp_conn->data_copied != tcp_conn->in.padding)
952 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
953 else if (conn->datadgst_en)
954 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
955 else
956 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
957 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700958 }
959
960 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500961 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
962 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700963
Mike Christie5bb0b552006-04-06 21:26:46 -0500964 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700965 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500966 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700967 goto more;
968 }
969
970nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500971 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700972 BUG_ON(processed == 0);
973 return processed;
974
975again:
Mike Christie5bb0b552006-04-06 21:26:46 -0500976 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700977 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
978 processed, (int)len, (int)rd_desc->count);
979 BUG_ON(processed == 0);
980 BUG_ON(processed > len);
981
982 conn->rxdata_octets += processed;
983 return processed;
984}
985
986static void
987iscsi_tcp_data_ready(struct sock *sk, int flag)
988{
989 struct iscsi_conn *conn = sk->sk_user_data;
990 read_descriptor_t rd_desc;
991
992 read_lock(&sk->sk_callback_lock);
993
Mike Christie665b44a2006-05-02 19:46:49 -0500994 /*
995 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
996 * We set count to 1 because we want the network layer to
997 * hand us all the skbs that are available. iscsi_tcp_data_recv
998 * handled pdus that cross buffers or pdus that still need data.
999 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001000 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -05001001 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001002 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
1003
1004 read_unlock(&sk->sk_callback_lock);
1005}
1006
1007static void
1008iscsi_tcp_state_change(struct sock *sk)
1009{
Mike Christie5bb0b552006-04-06 21:26:46 -05001010 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001011 struct iscsi_conn *conn;
1012 struct iscsi_session *session;
1013 void (*old_state_change)(struct sock *);
1014
1015 read_lock(&sk->sk_callback_lock);
1016
1017 conn = (struct iscsi_conn*)sk->sk_user_data;
1018 session = conn->session;
1019
Mike Christiee6273992005-11-29 23:12:49 -06001020 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1021 sk->sk_state == TCP_CLOSE) &&
1022 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001023 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1024 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1025 }
1026
Mike Christie5bb0b552006-04-06 21:26:46 -05001027 tcp_conn = conn->dd_data;
1028 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001029
1030 read_unlock(&sk->sk_callback_lock);
1031
1032 old_state_change(sk);
1033}
1034
1035/**
1036 * iscsi_write_space - Called when more output buffer space is available
1037 * @sk: socket space is available for
1038 **/
1039static void
1040iscsi_write_space(struct sock *sk)
1041{
1042 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001043 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1044
1045 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001046 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001047 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001048}
1049
1050static void
1051iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1052{
Mike Christie5bb0b552006-04-06 21:26:46 -05001053 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1054 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001055
1056 /* assign new callbacks */
1057 write_lock_bh(&sk->sk_callback_lock);
1058 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001059 tcp_conn->old_data_ready = sk->sk_data_ready;
1060 tcp_conn->old_state_change = sk->sk_state_change;
1061 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001062 sk->sk_data_ready = iscsi_tcp_data_ready;
1063 sk->sk_state_change = iscsi_tcp_state_change;
1064 sk->sk_write_space = iscsi_write_space;
1065 write_unlock_bh(&sk->sk_callback_lock);
1066}
1067
1068static void
Mike Christie1c834692006-07-24 15:47:26 -05001069iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001070{
Mike Christie5bb0b552006-04-06 21:26:46 -05001071 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001072
1073 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1074 write_lock_bh(&sk->sk_callback_lock);
1075 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001076 sk->sk_data_ready = tcp_conn->old_data_ready;
1077 sk->sk_state_change = tcp_conn->old_state_change;
1078 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001079 sk->sk_no_check = 0;
1080 write_unlock_bh(&sk->sk_callback_lock);
1081}
1082
1083/**
1084 * iscsi_send - generic send routine
1085 * @sk: kernel's socket
1086 * @buf: buffer to write from
1087 * @size: actual size to write
1088 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001089 */
1090static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001091iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001092{
Mike Christie5bb0b552006-04-06 21:26:46 -05001093 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1094 struct socket *sk = tcp_conn->sock;
Mike Christie3219e522006-05-30 00:37:28 -05001095 int offset = buf->sg.offset + buf->sent, res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001096
Mike Christie7cae5152006-01-13 18:05:47 -06001097 /*
1098 * if we got use_sg=0 or are sending something we kmallocd
1099 * then we did not have to do kmap (kmap returns page_address)
1100 *
1101 * if we got use_sg > 0, but had to drop down, we do not
1102 * set clustering so this should only happen for that
1103 * slab case.
1104 */
1105 if (buf->use_sendmsg)
Mike Christie3219e522006-05-30 00:37:28 -05001106 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
Mike Christie7cae5152006-01-13 18:05:47 -06001107 else
Mike Christie3219e522006-05-30 00:37:28 -05001108 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1109
1110 if (res >= 0) {
1111 conn->txdata_octets += res;
1112 buf->sent += res;
1113 return res;
1114 }
1115
1116 tcp_conn->sendpage_failures_cnt++;
1117 if (res == -EAGAIN)
1118 res = -ENOBUFS;
1119 else
1120 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1121 return res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001122}
1123
1124/**
1125 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1126 * @conn: iscsi connection
1127 * @buf: buffer to write from
1128 * @datalen: lenght of data to be sent after the header
1129 *
1130 * Notes:
1131 * (Tx, Fast Path)
1132 **/
1133static inline int
1134iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1135{
Alex Aizman7ba24712005-08-04 19:30:08 -07001136 int flags = 0; /* MSG_DONTWAIT; */
1137 int res, size;
1138
1139 size = buf->sg.length - buf->sent;
1140 BUG_ON(buf->sent + size > buf->sg.length);
1141 if (buf->sent + size != buf->sg.length || datalen)
1142 flags |= MSG_MORE;
1143
FUJITA Tomonori56851692006-01-13 18:05:44 -06001144 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001145 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1146 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001147 if (size != res)
1148 return -EAGAIN;
1149 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001150 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001151
1152 return res;
1153}
1154
1155/**
1156 * iscsi_sendpage - send one page of iSCSI Data-Out.
1157 * @conn: iscsi connection
1158 * @buf: buffer to write from
1159 * @count: remaining data
1160 * @sent: number of bytes sent
1161 *
1162 * Notes:
1163 * (Tx, Fast Path)
1164 **/
1165static inline int
1166iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1167 int *count, int *sent)
1168{
Alex Aizman7ba24712005-08-04 19:30:08 -07001169 int flags = 0; /* MSG_DONTWAIT; */
1170 int res, size;
1171
1172 size = buf->sg.length - buf->sent;
1173 BUG_ON(buf->sent + size > buf->sg.length);
1174 if (size > *count)
1175 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001176 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001177 flags |= MSG_MORE;
1178
FUJITA Tomonori56851692006-01-13 18:05:44 -06001179 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001180 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1181 size, buf->sent, *count, *sent, res);
1182 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001183 *count -= res;
1184 *sent += res;
1185 if (size != res)
1186 return -EAGAIN;
1187 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001188 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001189
1190 return res;
1191}
1192
1193static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001194iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
Mike Christie62f38302006-08-31 18:09:27 -04001195 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001196{
James Bottomleyc9802cd2006-09-23 15:33:43 -05001197 crypto_hash_init(&tcp_conn->tx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -05001198 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001199}
1200
Alex Aizman7ba24712005-08-04 19:30:08 -07001201/**
1202 * iscsi_solicit_data_cont - initialize next Data-Out
1203 * @conn: iscsi connection
1204 * @ctask: scsi command task
1205 * @r2t: R2T info
1206 * @left: bytes left to transfer
1207 *
1208 * Notes:
1209 * Initialize next Data-Out within this R2T sequence and continue
1210 * to process next Scatter-Gather element(if any) of this SCSI command.
1211 *
1212 * Called under connection lock.
1213 **/
1214static void
1215iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1216 struct iscsi_r2t_info *r2t, int left)
1217{
1218 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001219 int new_offset;
1220
Mike Christieffbfe922006-05-18 20:31:36 -05001221 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001222 memset(hdr, 0, sizeof(struct iscsi_data));
1223 hdr->ttt = r2t->ttt;
1224 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1225 r2t->solicit_datasn++;
1226 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001227 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1228 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001229 hdr->exp_statsn = r2t->exp_statsn;
1230 new_offset = r2t->data_offset + r2t->sent;
1231 hdr->offset = cpu_to_be32(new_offset);
1232 if (left > conn->max_xmit_dlength) {
1233 hton24(hdr->dlength, conn->max_xmit_dlength);
1234 r2t->data_count = conn->max_xmit_dlength;
1235 } else {
1236 hton24(hdr->dlength, left);
1237 r2t->data_count = left;
1238 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1239 }
1240 conn->dataout_pdus_cnt++;
1241
Mike Christie6e458cc2006-05-18 20:31:31 -05001242 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001243 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001244
Mike Christie62f38302006-08-31 18:09:27 -04001245 if (iscsi_buf_left(&r2t->sendbuf))
1246 return;
1247
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001248 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1249 r2t->sg += 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001250}
1251
Mike Christie62f38302006-08-31 18:09:27 -04001252static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask,
1253 unsigned long len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001254{
Mike Christie62f38302006-08-31 18:09:27 -04001255 tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1);
1256 if (!tcp_ctask->pad_count)
1257 return;
Alex Aizman7ba24712005-08-04 19:30:08 -07001258
Mike Christie62f38302006-08-31 18:09:27 -04001259 tcp_ctask->pad_count = ISCSI_PAD_LEN - tcp_ctask->pad_count;
1260 debug_scsi("write padding %d bytes\n", tcp_ctask->pad_count);
1261 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Alex Aizman7ba24712005-08-04 19:30:08 -07001262}
1263
1264/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001265 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001266 * @conn: iscsi connection
1267 * @ctask: scsi command task
1268 * @sc: scsi command
1269 **/
1270static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001271iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001272{
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
Mike Christie5bb0b552006-04-06 21:26:46 -05001275 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Mike Christie218432c2007-05-30 12:57:17 -05001276 tcp_ctask->xmstate = XMSTATE_CMD_HDR_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001277}
1278
1279/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001280 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001281 * @conn: iscsi connection
1282 * @mtask: task management task
1283 *
1284 * Notes:
1285 * The function can return -EAGAIN in which case caller must
1286 * call it again later, or recover. '0' return code means successful
1287 * xmit.
1288 *
Mike Christie218432c2007-05-30 12:57:17 -05001289 * Management xmit state machine consists of these states:
1290 * XMSTATE_IMM_HDR_INIT - calculate digest of PDU Header
1291 * XMSTATE_IMM_HDR - PDU Header xmit in progress
1292 * XMSTATE_IMM_DATA - PDU Data xmit in progress
1293 * XMSTATE_IDLE - management PDU is done
Alex Aizman7ba24712005-08-04 19:30:08 -07001294 **/
1295static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001296iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001297{
Mike Christie5bb0b552006-04-06 21:26:46 -05001298 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001299 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001300
1301 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001302 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001303
Mike Christie218432c2007-05-30 12:57:17 -05001304 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR_INIT) {
1305 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
1306 sizeof(struct iscsi_hdr));
1307
1308 if (mtask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001309 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie218432c2007-05-30 12:57:17 -05001310 iscsi_buf_init_iov(&tcp_mtask->sendbuf,
1311 (char*)mtask->data,
1312 mtask->data_count);
1313 }
1314
Mike Christieaf973482005-09-12 21:01:32 -05001315 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001316 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001317 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001318 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1319 (u8*)tcp_mtask->hdrext);
Mike Christie218432c2007-05-30 12:57:17 -05001320
1321 tcp_mtask->sent = 0;
1322 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR_INIT;
1323 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
1324 }
1325
1326 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
Mike Christie3219e522006-05-30 00:37:28 -05001327 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1328 mtask->data_count);
Mike Christie218432c2007-05-30 12:57:17 -05001329 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001330 return rc;
Mike Christie218432c2007-05-30 12:57:17 -05001331 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001332 }
1333
Mike Christie5bb0b552006-04-06 21:26:46 -05001334 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001335 BUG_ON(!mtask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001336 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001337 /* FIXME: implement.
1338 * Virtual buffer could be spreaded across multiple pages...
1339 */
1340 do {
Mike Christie3219e522006-05-30 00:37:28 -05001341 int rc;
1342
1343 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1344 &mtask->data_count, &tcp_mtask->sent);
1345 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001346 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001347 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001348 }
1349 } while (mtask->data_count);
1350 }
1351
Mike Christie5bb0b552006-04-06 21:26:46 -05001352 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
Al Virob4377352007-02-09 16:39:40 +00001353 if (mtask->hdr->itt == RESERVED_ITT) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001354 struct iscsi_session *session = conn->session;
1355
1356 spin_lock_bh(&session->lock);
1357 list_del(&conn->mtask->running);
1358 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1359 sizeof(void*));
1360 spin_unlock_bh(&session->lock);
1361 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001362 return 0;
1363}
1364
Mike Christie218432c2007-05-30 12:57:17 -05001365static int
1366iscsi_send_cmd_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001367{
Mike Christie218432c2007-05-30 12:57:17 -05001368 struct scsi_cmnd *sc = ctask->sc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001369 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie218432c2007-05-30 12:57:17 -05001370 int rc = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -05001371
Mike Christie218432c2007-05-30 12:57:17 -05001372 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_INIT) {
1373 tcp_ctask->sent = 0;
1374 tcp_ctask->sg_count = 0;
1375 tcp_ctask->exp_datasn = 0;
Mike Christie3219e522006-05-30 00:37:28 -05001376
Mike Christie218432c2007-05-30 12:57:17 -05001377 if (sc->sc_data_direction == DMA_TO_DEVICE) {
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001378 struct scatterlist *sg = scsi_sglist(sc);
Alex Aizman7ba24712005-08-04 19:30:08 -07001379
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001380 iscsi_buf_init_sg(&tcp_ctask->sendbuf, sg);
1381 tcp_ctask->sg = sg + 1;
1382 tcp_ctask->bad_sg = sg + scsi_sg_count(sc);
Mike Christie218432c2007-05-30 12:57:17 -05001383
1384 debug_scsi("cmd [itt 0x%x total %d imm_data %d "
1385 "unsol count %d, unsol offset %d]\n",
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001386 ctask->itt, scsi_bufflen(sc),
Mike Christie218432c2007-05-30 12:57:17 -05001387 ctask->imm_count, ctask->unsol_count,
1388 ctask->unsol_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -07001389 }
Mike Christie218432c2007-05-30 12:57:17 -05001390
1391 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
1392 sizeof(struct iscsi_hdr));
1393
1394 if (conn->hdrdgst_en)
1395 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1396 (u8*)tcp_ctask->hdrext);
1397 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_INIT;
1398 tcp_ctask->xmstate |= XMSTATE_CMD_HDR_XMIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001399 }
1400
Mike Christie218432c2007-05-30 12:57:17 -05001401 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_XMIT) {
1402 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1403 if (rc)
1404 return rc;
1405 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_XMIT;
1406
1407 if (sc->sc_data_direction != DMA_TO_DEVICE)
1408 return 0;
1409
1410 if (ctask->imm_count) {
1411 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1412 iscsi_set_padding(tcp_ctask, ctask->imm_count);
1413
1414 if (ctask->conn->datadgst_en) {
1415 iscsi_data_digest_init(ctask->conn->dd_data,
1416 tcp_ctask);
1417 tcp_ctask->immdigest = 0;
1418 }
1419 }
1420
1421 if (ctask->unsol_count)
1422 tcp_ctask->xmstate |=
1423 XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
1424 }
1425 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001426}
1427
Mike Christie62f38302006-08-31 18:09:27 -04001428static int
1429iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1430{
1431 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1432 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1433 int sent = 0, rc;
1434
1435 if (tcp_ctask->xmstate & XMSTATE_W_PAD) {
1436 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
1437 tcp_ctask->pad_count);
1438 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001439 crypto_hash_update(&tcp_conn->tx_hash,
1440 &tcp_ctask->sendbuf.sg,
1441 tcp_ctask->sendbuf.sg.length);
Mike Christie62f38302006-08-31 18:09:27 -04001442 } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD))
1443 return 0;
1444
1445 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
1446 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_PAD;
1447 debug_scsi("sending %d pad bytes for itt 0x%x\n",
1448 tcp_ctask->pad_count, ctask->itt);
1449 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1450 &sent);
1451 if (rc) {
1452 debug_scsi("padding send failed %d\n", rc);
1453 tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD;
1454 }
1455 return rc;
1456}
1457
1458static int
1459iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1460 struct iscsi_buf *buf, uint32_t *digest)
1461{
1462 struct iscsi_tcp_cmd_task *tcp_ctask;
1463 struct iscsi_tcp_conn *tcp_conn;
1464 int rc, sent = 0;
1465
1466 if (!conn->datadgst_en)
1467 return 0;
1468
1469 tcp_ctask = ctask->dd_data;
1470 tcp_conn = conn->dd_data;
1471
1472 if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) {
James Bottomleyc9802cd2006-09-23 15:33:43 -05001473 crypto_hash_final(&tcp_conn->tx_hash, (u8*)digest);
Mike Christie62f38302006-08-31 18:09:27 -04001474 iscsi_buf_init_iov(buf, (char*)digest, 4);
1475 }
1476 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST;
1477
1478 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
1479 if (!rc)
1480 debug_scsi("sent digest 0x%x for itt 0x%x\n", *digest,
1481 ctask->itt);
1482 else {
1483 debug_scsi("sending digest 0x%x failed for itt 0x%x!\n",
1484 *digest, ctask->itt);
1485 tcp_ctask->xmstate |= XMSTATE_W_RESEND_DATA_DIGEST;
1486 }
1487 return rc;
1488}
1489
1490static int
1491iscsi_send_data(struct iscsi_cmd_task *ctask, struct iscsi_buf *sendbuf,
1492 struct scatterlist **sg, int *sent, int *count,
1493 struct iscsi_buf *digestbuf, uint32_t *digest)
1494{
1495 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1496 struct iscsi_conn *conn = ctask->conn;
1497 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1498 int rc, buf_sent, offset;
1499
1500 while (*count) {
1501 buf_sent = 0;
1502 offset = sendbuf->sent;
1503
1504 rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent);
1505 *sent = *sent + buf_sent;
1506 if (buf_sent && conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001507 partial_sg_digest_update(&tcp_conn->tx_hash,
Mike Christie62f38302006-08-31 18:09:27 -04001508 &sendbuf->sg, sendbuf->sg.offset + offset,
1509 buf_sent);
1510 if (!iscsi_buf_left(sendbuf) && *sg != tcp_ctask->bad_sg) {
1511 iscsi_buf_init_sg(sendbuf, *sg);
1512 *sg = *sg + 1;
1513 }
1514
1515 if (rc)
1516 return rc;
1517 }
1518
1519 rc = iscsi_send_padding(conn, ctask);
1520 if (rc)
1521 return rc;
1522
1523 return iscsi_send_digest(conn, ctask, digestbuf, digest);
1524}
1525
1526static int
1527iscsi_send_unsol_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001528{
Mike Christie5bb0b552006-04-06 21:26:46 -05001529 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001530 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001531 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001532
Mike Christie5bb0b552006-04-06 21:26:46 -05001533 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1534 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Mike Christie62f38302006-08-31 18:09:27 -04001535 dtask = &tcp_ctask->unsol_dtask;
1536
Mike Christieffd04362006-08-31 18:09:24 -04001537 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
1538 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
1539 sizeof(struct iscsi_hdr));
Mike Christieaf973482005-09-12 21:01:32 -05001540 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001541 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001542 (u8*)dtask->hdrext);
Mike Christie62f38302006-08-31 18:09:27 -04001543
Mike Christie5bb0b552006-04-06 21:26:46 -05001544 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Mike Christie62f38302006-08-31 18:09:27 -04001545 iscsi_set_padding(tcp_ctask, ctask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001546 }
Mike Christie3219e522006-05-30 00:37:28 -05001547
1548 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1549 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001550 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1551 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001552 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001553 }
1554
Mike Christiedd8c0d92006-08-31 18:09:28 -04001555 if (conn->datadgst_en) {
1556 dtask = &tcp_ctask->unsol_dtask;
1557 iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
1558 dtask->digest = 0;
1559 }
1560
Alex Aizman7ba24712005-08-04 19:30:08 -07001561 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001562 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001563 return 0;
1564}
1565
Mike Christie62f38302006-08-31 18:09:27 -04001566static int
1567iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001568{
Mike Christie5bb0b552006-04-06 21:26:46 -05001569 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001570 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001571
Mike Christie62f38302006-08-31 18:09:27 -04001572 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
1573 BUG_ON(!ctask->unsol_count);
1574 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
1575send_hdr:
1576 rc = iscsi_send_unsol_hdr(conn, ctask);
1577 if (rc)
1578 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001579 }
1580
Mike Christie62f38302006-08-31 18:09:27 -04001581 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
1582 struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask;
Mike Christie5bb0b552006-04-06 21:26:46 -05001583 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001584
Mike Christie62f38302006-08-31 18:09:27 -04001585 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1586 &tcp_ctask->sent, &ctask->data_count,
1587 &dtask->digestbuf, &dtask->digest);
Mike Christie5bb0b552006-04-06 21:26:46 -05001588 ctask->unsol_count -= tcp_ctask->sent - start;
Mike Christie62f38302006-08-31 18:09:27 -04001589 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001590 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001591 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1592 /*
1593 * Done with the Data-Out. Next, check if we need
1594 * to send another unsolicited Data-Out.
1595 */
1596 if (ctask->unsol_count) {
1597 debug_scsi("sending more uns\n");
1598 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
1599 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001600 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001601 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001602 return 0;
1603}
1604
Mike Christie62f38302006-08-31 18:09:27 -04001605static int iscsi_send_sol_pdu(struct iscsi_conn *conn,
1606 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001607{
Mike Christie5bb0b552006-04-06 21:26:46 -05001608 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie62f38302006-08-31 18:09:27 -04001609 struct iscsi_session *session = conn->session;
1610 struct iscsi_r2t_info *r2t;
1611 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001612 int left, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001613
Mike Christie218432c2007-05-30 12:57:17 -05001614 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR_INIT) {
Mike Christiedb37c502006-11-08 15:58:33 -06001615 if (!tcp_ctask->r2t) {
1616 spin_lock_bh(&session->lock);
Mike Christie62f38302006-08-31 18:09:27 -04001617 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1618 sizeof(void*));
Mike Christiedb37c502006-11-08 15:58:33 -06001619 spin_unlock_bh(&session->lock);
1620 }
Mike Christie62f38302006-08-31 18:09:27 -04001621send_hdr:
1622 r2t = tcp_ctask->r2t;
1623 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001624
Mike Christie62f38302006-08-31 18:09:27 -04001625 if (conn->hdrdgst_en)
1626 iscsi_hdr_digest(conn, &r2t->headbuf,
1627 (u8*)dtask->hdrext);
Mike Christie218432c2007-05-30 12:57:17 -05001628 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR_INIT;
1629 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
1630 }
1631
1632 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
1633 r2t = tcp_ctask->r2t;
1634 dtask = &r2t->dtask;
1635
Mike Christie62f38302006-08-31 18:09:27 -04001636 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
Mike Christie218432c2007-05-30 12:57:17 -05001637 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001638 return rc;
Mike Christie218432c2007-05-30 12:57:17 -05001639 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1640 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001641
Mike Christiedd8c0d92006-08-31 18:09:28 -04001642 if (conn->datadgst_en) {
1643 iscsi_data_digest_init(conn->dd_data, tcp_ctask);
1644 dtask->digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001645 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001646
Mike Christie62f38302006-08-31 18:09:27 -04001647 iscsi_set_padding(tcp_ctask, r2t->data_count);
1648 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1649 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1650 r2t->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001651 }
1652
Mike Christie62f38302006-08-31 18:09:27 -04001653 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
1654 r2t = tcp_ctask->r2t;
1655 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001656
Mike Christie62f38302006-08-31 18:09:27 -04001657 rc = iscsi_send_data(ctask, &r2t->sendbuf, &r2t->sg,
1658 &r2t->sent, &r2t->data_count,
1659 &dtask->digestbuf, &dtask->digest);
1660 if (rc)
1661 return rc;
1662 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001663
Mike Christie62f38302006-08-31 18:09:27 -04001664 /*
1665 * Done with this Data-Out. Next, check if we have
1666 * to send another Data-Out for this R2T.
1667 */
1668 BUG_ON(r2t->data_length - r2t->sent < 0);
1669 left = r2t->data_length - r2t->sent;
1670 if (left) {
1671 iscsi_solicit_data_cont(conn, ctask, r2t, left);
Mike Christie62f38302006-08-31 18:09:27 -04001672 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001673 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001674
Mike Christie62f38302006-08-31 18:09:27 -04001675 /*
1676 * Done with this R2T. Check if there are more
1677 * outstanding R2Ts ready to be processed.
1678 */
1679 spin_lock_bh(&session->lock);
1680 tcp_ctask->r2t = NULL;
1681 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
1682 sizeof(void*));
1683 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t,
1684 sizeof(void*))) {
1685 tcp_ctask->r2t = r2t;
Mike Christie62f38302006-08-31 18:09:27 -04001686 spin_unlock_bh(&session->lock);
1687 goto send_hdr;
1688 }
1689 spin_unlock_bh(&session->lock);
1690 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001691 return 0;
1692}
1693
Mike Christie218432c2007-05-30 12:57:17 -05001694/**
1695 * iscsi_tcp_ctask_xmit - xmit normal PDU task
1696 * @conn: iscsi connection
1697 * @ctask: iscsi command task
1698 *
1699 * Notes:
1700 * The function can return -EAGAIN in which case caller must
1701 * call it again later, or recover. '0' return code means successful
1702 * xmit.
1703 * The function is devided to logical helpers (above) for the different
1704 * xmit stages.
1705 *
1706 *iscsi_send_cmd_hdr()
1707 * XMSTATE_CMD_HDR_INIT - prepare Header and Data buffers Calculate
1708 * Header Digest
1709 * XMSTATE_CMD_HDR_XMIT - Transmit header in progress
1710 *
1711 *iscsi_send_padding
1712 * XMSTATE_W_PAD - Prepare and send pading
1713 * XMSTATE_W_RESEND_PAD - retry send pading
1714 *
1715 *iscsi_send_digest
1716 * XMSTATE_W_RESEND_DATA_DIGEST - Finalize and send Data Digest
1717 * XMSTATE_W_RESEND_DATA_DIGEST - retry sending digest
1718 *
1719 *iscsi_send_unsol_hdr
1720 * XMSTATE_UNS_INIT - prepare un-solicit data header and digest
1721 * XMSTATE_UNS_HDR - send un-solicit header
1722 *
1723 *iscsi_send_unsol_pdu
1724 * XMSTATE_UNS_DATA - send un-solicit data in progress
1725 *
1726 *iscsi_send_sol_pdu
1727 * XMSTATE_SOL_HDR_INIT - solicit data header and digest initialize
1728 * XMSTATE_SOL_HDR - send solicit header
1729 * XMSTATE_SOL_DATA - send solicit data
1730 *
1731 *iscsi_tcp_ctask_xmit
1732 * XMSTATE_IMM_DATA - xmit managment data (??)
1733 **/
Alex Aizman7ba24712005-08-04 19:30:08 -07001734static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001735iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001736{
Mike Christie5bb0b552006-04-06 21:26:46 -05001737 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001738 int rc = 0;
1739
1740 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001741 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001742
Mike Christie218432c2007-05-30 12:57:17 -05001743 rc = iscsi_send_cmd_hdr(conn, ctask);
1744 if (rc)
1745 return rc;
1746 if (ctask->sc->sc_data_direction != DMA_TO_DEVICE)
1747 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001748
Mike Christie5bb0b552006-04-06 21:26:46 -05001749 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Mike Christie62f38302006-08-31 18:09:27 -04001750 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1751 &tcp_ctask->sent, &ctask->imm_count,
1752 &tcp_ctask->immbuf, &tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001753 if (rc)
1754 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001755 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001756 }
1757
Mike Christie62f38302006-08-31 18:09:27 -04001758 rc = iscsi_send_unsol_pdu(conn, ctask);
1759 if (rc)
1760 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001761
Mike Christie62f38302006-08-31 18:09:27 -04001762 rc = iscsi_send_sol_pdu(conn, ctask);
1763 if (rc)
1764 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001765
1766 return rc;
1767}
1768
Mike Christie7b8631b2006-01-13 18:05:50 -06001769static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001770iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001771{
Mike Christie7b8631b2006-01-13 18:05:50 -06001772 struct iscsi_conn *conn;
1773 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001774 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001775
Mike Christie5bb0b552006-04-06 21:26:46 -05001776 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001777 if (!cls_conn)
1778 return NULL;
1779 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001780 /*
1781 * due to strange issues with iser these are not set
1782 * in iscsi_conn_setup
1783 */
Mike Christiebf32ed32007-02-28 17:32:17 -06001784 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
Alex Aizman7ba24712005-08-04 19:30:08 -07001785
Mike Christie5bb0b552006-04-06 21:26:46 -05001786 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1787 if (!tcp_conn)
1788 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001789
Mike Christie5bb0b552006-04-06 21:26:46 -05001790 conn->dd_data = tcp_conn;
1791 tcp_conn->iscsi_conn = conn;
1792 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1793 /* initial operational parameters */
1794 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07001795
James Bottomleyc9802cd2006-09-23 15:33:43 -05001796 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1797 CRYPTO_ALG_ASYNC);
1798 tcp_conn->tx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001799 if (IS_ERR(tcp_conn->tx_hash.tfm)) {
1800 printk(KERN_ERR "Could not create connection due to crc32c "
1801 "loading error %ld. Make sure the crc32c module is "
1802 "built as a module or into the kernel\n",
1803 PTR_ERR(tcp_conn->tx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001804 goto free_tcp_conn;
Mike Christie0f238412007-02-28 17:32:21 -06001805 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001806
James Bottomleyc9802cd2006-09-23 15:33:43 -05001807 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1808 CRYPTO_ALG_ASYNC);
1809 tcp_conn->rx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001810 if (IS_ERR(tcp_conn->rx_hash.tfm)) {
1811 printk(KERN_ERR "Could not create connection due to crc32c "
1812 "loading error %ld. Make sure the crc32c module is "
1813 "built as a module or into the kernel\n",
1814 PTR_ERR(tcp_conn->rx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001815 goto free_tx_tfm;
Mike Christie0f238412007-02-28 17:32:21 -06001816 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001817
Mike Christie7b8631b2006-01-13 18:05:50 -06001818 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001819
Mike Christiedd8c0d92006-08-31 18:09:28 -04001820free_tx_tfm:
James Bottomleyc9802cd2006-09-23 15:33:43 -05001821 crypto_free_hash(tcp_conn->tx_hash.tfm);
Mike Christiedd8c0d92006-08-31 18:09:28 -04001822free_tcp_conn:
1823 kfree(tcp_conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001824tcp_conn_alloc_fail:
1825 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001826 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001827}
1828
1829static void
Mike Christie1c834692006-07-24 15:47:26 -05001830iscsi_tcp_release_conn(struct iscsi_conn *conn)
1831{
Mike Christie22236962007-05-30 12:57:24 -05001832 struct iscsi_session *session = conn->session;
Mike Christie1c834692006-07-24 15:47:26 -05001833 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie22236962007-05-30 12:57:24 -05001834 struct socket *sock = tcp_conn->sock;
Mike Christie1c834692006-07-24 15:47:26 -05001835
Mike Christie22236962007-05-30 12:57:24 -05001836 if (!sock)
Mike Christie1c834692006-07-24 15:47:26 -05001837 return;
1838
Mike Christie22236962007-05-30 12:57:24 -05001839 sock_hold(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001840 iscsi_conn_restore_callbacks(tcp_conn);
Mike Christie22236962007-05-30 12:57:24 -05001841 sock_put(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001842
Mike Christie22236962007-05-30 12:57:24 -05001843 spin_lock_bh(&session->lock);
Mike Christie1c834692006-07-24 15:47:26 -05001844 tcp_conn->sock = NULL;
1845 conn->recv_lock = NULL;
Mike Christie22236962007-05-30 12:57:24 -05001846 spin_unlock_bh(&session->lock);
1847 sockfd_put(sock);
Mike Christie1c834692006-07-24 15:47:26 -05001848}
1849
1850static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001851iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001852{
Mike Christie7b8631b2006-01-13 18:05:50 -06001853 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001854 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001855
Mike Christie1c834692006-07-24 15:47:26 -05001856 iscsi_tcp_release_conn(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001857 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001858
Pete Wyckoff534284a2006-11-08 15:58:31 -06001859 if (tcp_conn->tx_hash.tfm)
1860 crypto_free_hash(tcp_conn->tx_hash.tfm);
1861 if (tcp_conn->rx_hash.tfm)
1862 crypto_free_hash(tcp_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001863
Mike Christie5bb0b552006-04-06 21:26:46 -05001864 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001865}
1866
Mike Christie1c834692006-07-24 15:47:26 -05001867static void
1868iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1869{
1870 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christied5390f52006-08-31 18:09:30 -04001871 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie1c834692006-07-24 15:47:26 -05001872
1873 iscsi_conn_stop(cls_conn, flag);
1874 iscsi_tcp_release_conn(conn);
Mike Christied5390f52006-08-31 18:09:30 -04001875 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christie1c834692006-07-24 15:47:26 -05001876}
1877
Mike Christie22236962007-05-30 12:57:24 -05001878static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
1879 char *buf, int *port,
1880 int (*getname)(struct socket *, struct sockaddr *,
1881 int *addrlen))
1882{
1883 struct sockaddr_storage *addr;
1884 struct sockaddr_in6 *sin6;
1885 struct sockaddr_in *sin;
1886 int rc = 0, len;
1887
Al Viroa9204872007-07-20 16:03:40 +01001888 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
Mike Christie22236962007-05-30 12:57:24 -05001889 if (!addr)
1890 return -ENOMEM;
1891
1892 if (getname(sock, (struct sockaddr *) addr, &len)) {
1893 rc = -ENODEV;
1894 goto free_addr;
1895 }
1896
1897 switch (addr->ss_family) {
1898 case AF_INET:
1899 sin = (struct sockaddr_in *)addr;
1900 spin_lock_bh(&conn->session->lock);
1901 sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
1902 *port = be16_to_cpu(sin->sin_port);
1903 spin_unlock_bh(&conn->session->lock);
1904 break;
1905 case AF_INET6:
1906 sin6 = (struct sockaddr_in6 *)addr;
1907 spin_lock_bh(&conn->session->lock);
1908 sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
1909 *port = be16_to_cpu(sin6->sin6_port);
1910 spin_unlock_bh(&conn->session->lock);
1911 break;
1912 }
1913free_addr:
1914 kfree(addr);
1915 return rc;
1916}
1917
Alex Aizman7ba24712005-08-04 19:30:08 -07001918static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001919iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001920 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001921 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001922{
Mike Christie5bb0b552006-04-06 21:26:46 -05001923 struct iscsi_conn *conn = cls_conn->dd_data;
1924 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001925 struct sock *sk;
1926 struct socket *sock;
1927 int err;
1928
1929 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001930 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001931 if (!sock) {
1932 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1933 return -EEXIST;
1934 }
Mike Christie22236962007-05-30 12:57:24 -05001935 /*
1936 * copy these values now because if we drop the session
1937 * userspace may still want to query the values since we will
1938 * be using them for the reconnect
1939 */
1940 err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1941 &conn->portal_port, kernel_getpeername);
1942 if (err)
1943 goto free_socket;
1944
1945 err = iscsi_tcp_get_addr(conn, sock, conn->local_address,
1946 &conn->local_port, kernel_getsockname);
1947 if (err)
1948 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001949
Mike Christie5bb0b552006-04-06 21:26:46 -05001950 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1951 if (err)
Mike Christie22236962007-05-30 12:57:24 -05001952 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001953
Mike Christie67a61112006-05-30 00:37:20 -05001954 /* bind iSCSI connection and socket */
1955 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001956
Mike Christie67a61112006-05-30 00:37:20 -05001957 /* setup Socket parameters */
1958 sk = sock->sk;
1959 sk->sk_reuse = 1;
1960 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1961 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001962
Mike Christie67a61112006-05-30 00:37:20 -05001963 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07001964
Mike Christie67a61112006-05-30 00:37:20 -05001965 /*
1966 * Intercept TCP callbacks for sendfile like receive
1967 * processing.
1968 */
1969 conn->recv_lock = &sk->sk_callback_lock;
1970 iscsi_conn_set_callbacks(conn);
1971 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1972 /*
1973 * set receive state machine into initial state
1974 */
1975 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07001976 return 0;
Mike Christie22236962007-05-30 12:57:24 -05001977
1978free_socket:
1979 sockfd_put(sock);
1980 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001981}
1982
Mike Christie5bb0b552006-04-06 21:26:46 -05001983/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05001984static void
Mike Christie77a23c22007-05-30 12:57:18 -05001985iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Mike Christie30a6c652006-04-06 21:13:39 -05001986{
Mike Christie5bb0b552006-04-06 21:26:46 -05001987 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie218432c2007-05-30 12:57:17 -05001988 tcp_mtask->xmstate = XMSTATE_IMM_HDR_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001989}
1990
1991static int
1992iscsi_r2tpool_alloc(struct iscsi_session *session)
1993{
1994 int i;
1995 int cmd_i;
1996
1997 /*
1998 * initialize per-task: R2T pool and xmit queue
1999 */
2000 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2001 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05002002 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002003
2004 /*
2005 * pre-allocated x4 as much r2ts to handle race when
2006 * target acks DataOut faster than we data_xmit() queues
2007 * could replenish r2tqueue.
2008 */
2009
2010 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05002011 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
2012 (void***)&tcp_ctask->r2ts,
2013 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002014 goto r2t_alloc_fail;
2015 }
2016
2017 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05002018 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07002019 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05002020 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2021 iscsi_pool_free(&tcp_ctask->r2tpool,
2022 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002023 goto r2t_alloc_fail;
2024 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002025 }
2026
2027 return 0;
2028
2029r2t_alloc_fail:
2030 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002031 struct iscsi_cmd_task *ctask = session->cmds[i];
2032 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2033
Mike Christie5bb0b552006-04-06 21:26:46 -05002034 kfifo_free(tcp_ctask->r2tqueue);
2035 iscsi_pool_free(&tcp_ctask->r2tpool,
2036 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002037 }
2038 return -ENOMEM;
2039}
2040
2041static void
2042iscsi_r2tpool_free(struct iscsi_session *session)
2043{
2044 int i;
2045
2046 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002047 struct iscsi_cmd_task *ctask = session->cmds[i];
2048 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2049
Mike Christie5bb0b552006-04-06 21:26:46 -05002050 kfifo_free(tcp_ctask->r2tqueue);
2051 iscsi_pool_free(&tcp_ctask->r2tpool,
2052 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002053 }
2054}
2055
Alex Aizman7ba24712005-08-04 19:30:08 -07002056static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002057iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002058 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07002059{
Mike Christie7b7232f2006-02-01 21:06:49 -06002060 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002061 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05002062 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002063 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07002064
Alex Aizman7ba24712005-08-04 19:30:08 -07002065 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002066 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002067 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002068 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christiedd8c0d92006-08-31 18:09:28 -04002069 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05002070 tcp_conn->hdr_size += sizeof(__u32);
Alex Aizman7ba24712005-08-04 19:30:08 -07002071 break;
2072 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002073 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002074 tcp_conn->sendpage = conn->datadgst_en ?
2075 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07002076 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002077 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002078 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07002079 if (session->max_r2t == roundup_pow_of_two(value))
2080 break;
2081 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002082 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002083 if (session->max_r2t & (session->max_r2t - 1))
2084 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2085 if (iscsi_r2tpool_alloc(session))
2086 return -ENOMEM;
2087 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002088 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002089 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002090 }
2091
2092 return 0;
2093}
2094
2095static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05002096iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2097 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06002098{
Mike Christie7b7232f2006-02-01 21:06:49 -06002099 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002100 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06002101
2102 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002103 case ISCSI_PARAM_CONN_PORT:
Mike Christie22236962007-05-30 12:57:24 -05002104 spin_lock_bh(&conn->session->lock);
2105 len = sprintf(buf, "%hu\n", conn->portal_port);
2106 spin_unlock_bh(&conn->session->lock);
Mike Christie8d2860b2006-05-02 19:46:47 -05002107 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002108 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie22236962007-05-30 12:57:24 -05002109 spin_lock_bh(&conn->session->lock);
2110 len = sprintf(buf, "%s\n", conn->portal_address);
2111 spin_unlock_bh(&conn->session->lock);
Mike Christiefd7255f2006-04-06 21:13:36 -05002112 break;
2113 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002114 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05002115 }
2116
2117 return len;
2118}
2119
Mike Christie22236962007-05-30 12:57:24 -05002120static int
2121iscsi_tcp_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2122 char *buf)
2123{
2124 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2125 int len;
2126
2127 switch (param) {
2128 case ISCSI_HOST_PARAM_IPADDRESS:
2129 spin_lock_bh(&session->lock);
2130 if (!session->leadconn)
2131 len = -ENODEV;
2132 else
2133 len = sprintf(buf, "%s\n",
2134 session->leadconn->local_address);
2135 spin_unlock_bh(&session->lock);
2136 break;
2137 default:
2138 return iscsi_host_get_param(shost, param, buf);
2139 }
2140 return len;
2141}
2142
Alex Aizman7ba24712005-08-04 19:30:08 -07002143static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002144iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002145{
Mike Christie7b7232f2006-02-01 21:06:49 -06002146 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002147 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002148
2149 stats->txdata_octets = conn->txdata_octets;
2150 stats->rxdata_octets = conn->rxdata_octets;
2151 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2152 stats->dataout_pdus = conn->dataout_pdus_cnt;
2153 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2154 stats->datain_pdus = conn->datain_pdus_cnt;
2155 stats->r2t_pdus = conn->r2t_pdus_cnt;
2156 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2157 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2158 stats->custom_length = 3;
2159 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002160 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002161 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002162 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002163 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2164 stats->custom[2].value = conn->eh_abort_cnt;
2165}
2166
Mike Christie5bb0b552006-04-06 21:26:46 -05002167static struct iscsi_cls_session *
2168iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2169 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05002170 uint16_t cmds_max, uint16_t qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05002171 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002172{
Mike Christie5bb0b552006-04-06 21:26:46 -05002173 struct iscsi_cls_session *cls_session;
2174 struct iscsi_session *session;
2175 uint32_t hn;
2176 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002177
Mike Christie15482712007-05-30 12:57:19 -05002178 cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05002179 sizeof(struct iscsi_tcp_cmd_task),
2180 sizeof(struct iscsi_tcp_mgmt_task),
2181 initial_cmdsn, &hn);
2182 if (!cls_session)
2183 return NULL;
2184 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002185
Mike Christie5bb0b552006-04-06 21:26:46 -05002186 session = class_to_transport_session(cls_session);
2187 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2188 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2189 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2190
2191 ctask->hdr = &tcp_ctask->hdr;
2192 }
2193
2194 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2195 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2196 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2197
2198 mtask->hdr = &tcp_mtask->hdr;
2199 }
2200
2201 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2202 goto r2tpool_alloc_fail;
2203
2204 return cls_session;
2205
2206r2tpool_alloc_fail:
2207 iscsi_session_teardown(cls_session);
2208 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002209}
2210
Mike Christie5bb0b552006-04-06 21:26:46 -05002211static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2212{
Mike Christie5bb0b552006-04-06 21:26:46 -05002213 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2214 iscsi_session_teardown(cls_session);
2215}
2216
Mike Christied1d81c02007-05-30 12:57:21 -05002217static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
2218{
Mike Christieb6d44fe2007-07-26 12:46:47 -05002219 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
Mike Christied1d81c02007-05-30 12:57:21 -05002220 blk_queue_dma_alignment(sdev->request_queue, 0);
2221 return 0;
2222}
2223
Mike Christie5bb0b552006-04-06 21:26:46 -05002224static struct scsi_host_template iscsi_sht = {
Mike Christie79743922007-07-26 12:46:46 -05002225 .module = THIS_MODULE,
Mike Christief4246b32006-07-24 15:47:54 -05002226 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05002227 .queuecommand = iscsi_queuecommand,
2228 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -05002229 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie5bb0b552006-04-06 21:26:46 -05002230 .sg_tablesize = ISCSI_SG_TABLESIZE,
Mike Christie8231f0e2007-02-28 17:32:20 -06002231 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -05002232 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2233 .eh_abort_handler = iscsi_eh_abort,
2234 .eh_host_reset_handler = iscsi_eh_host_reset,
2235 .use_clustering = DISABLE_CLUSTERING,
Mike Christied1d81c02007-05-30 12:57:21 -05002236 .slave_configure = iscsi_tcp_slave_configure,
Mike Christie5bb0b552006-04-06 21:26:46 -05002237 .proc_name = "iscsi_tcp",
2238 .this_id = -1,
2239};
2240
Alex Aizman7ba24712005-08-04 19:30:08 -07002241static struct iscsi_transport iscsi_tcp_transport = {
2242 .owner = THIS_MODULE,
2243 .name = "tcp",
2244 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2245 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002246 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2247 ISCSI_MAX_XMIT_DLENGTH |
2248 ISCSI_HDRDGST_EN |
2249 ISCSI_DATADGST_EN |
2250 ISCSI_INITIAL_R2T_EN |
2251 ISCSI_MAX_R2T |
2252 ISCSI_IMM_DATA_EN |
2253 ISCSI_FIRST_BURST |
2254 ISCSI_MAX_BURST |
2255 ISCSI_PDU_INORDER_EN |
2256 ISCSI_DATASEQ_INORDER_EN |
2257 ISCSI_ERL |
2258 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002259 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05002260 ISCSI_EXP_STATSN |
2261 ISCSI_PERSISTENT_PORT |
2262 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -05002263 ISCSI_TARGET_NAME | ISCSI_TPGT |
2264 ISCSI_USERNAME | ISCSI_PASSWORD |
2265 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN,
Mike Christie22236962007-05-30 12:57:24 -05002266 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
Mike Christied8196ed2007-05-30 12:57:25 -05002267 ISCSI_HOST_INITIATOR_NAME |
2268 ISCSI_HOST_NETDEV_NAME,
Alex Aizman7ba24712005-08-04 19:30:08 -07002269 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002270 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002271 .max_conn = 1,
2272 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002273 /* session management */
2274 .create_session = iscsi_tcp_session_create,
2275 .destroy_session = iscsi_tcp_session_destroy,
2276 /* connection management */
2277 .create_conn = iscsi_tcp_conn_create,
2278 .bind_conn = iscsi_tcp_conn_bind,
2279 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002280 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002281 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002282 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002283 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05002284 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -05002285 /* iscsi host params */
Mike Christie22236962007-05-30 12:57:24 -05002286 .get_host_param = iscsi_tcp_host_get_param,
Mike Christie0801c242007-05-30 12:57:12 -05002287 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -05002288 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002289 .send_pdu = iscsi_conn_send_pdu,
2290 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002291 .init_cmd_task = iscsi_tcp_cmd_init,
2292 .init_mgmt_task = iscsi_tcp_mgmt_init,
2293 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2294 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2295 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2296 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002297 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002298};
2299
2300static int __init
2301iscsi_tcp_init(void)
2302{
Alex Aizman7ba24712005-08-04 19:30:08 -07002303 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002304 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2305 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002306 return -EINVAL;
2307 }
2308 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2309
Mike Christie7b8631b2006-01-13 18:05:50 -06002310 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002311 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002312
Mike Christie7b8631b2006-01-13 18:05:50 -06002313 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002314}
2315
2316static void __exit
2317iscsi_tcp_exit(void)
2318{
2319 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002320}
2321
2322module_init(iscsi_tcp_init);
2323module_exit(iscsi_tcp_exit);