blob: 4e9f0d9a55eaacabdc85443825d7ed460bdcb4ea [file] [log] [blame]
Alex Aizman7ba24712005-08-04 19:30:08 -07001/*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
Mike Christie5bb0b552006-04-06 21:26:46 -05006 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
Alex Aizman7ba24712005-08-04 19:30:08 -07008 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * See the file COPYING included with this distribution for more details.
21 *
22 * Credits:
23 * Christoph Hellwig
24 * FUJITA Tomonori
25 * Arne Redlich
26 * Zhenyu Wang
27 */
28
29#include <linux/types.h>
30#include <linux/list.h>
31#include <linux/inet.h>
32#include <linux/blkdev.h>
33#include <linux/crypto.h>
34#include <linux/delay.h>
35#include <linux/kfifo.h>
36#include <linux/scatterlist.h>
37#include <net/tcp.h>
38#include <scsi/scsi_cmnd.h>
Mike Christied1d81c02007-05-30 12:57:21 -050039#include <scsi/scsi_device.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070040#include <scsi/scsi_host.h>
41#include <scsi/scsi.h>
42#include <scsi/scsi_transport_iscsi.h>
43
44#include "iscsi_tcp.h"
45
46MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
47 "Alex Aizman <itn780@yahoo.com>");
48MODULE_DESCRIPTION("iSCSI/TCP data-path");
49MODULE_LICENSE("GPL");
Alex Aizman7ba24712005-08-04 19:30:08 -070050/* #define DEBUG_TCP */
Alex Aizman7ba24712005-08-04 19:30:08 -070051#define DEBUG_ASSERT
52
53#ifdef DEBUG_TCP
Mike Christie5bb0b552006-04-06 21:26:46 -050054#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
Alex Aizman7ba24712005-08-04 19:30:08 -070055#else
56#define debug_tcp(fmt...)
57#endif
58
Alex Aizman7ba24712005-08-04 19:30:08 -070059#ifndef DEBUG_ASSERT
60#ifdef BUG_ON
61#undef BUG_ON
62#endif
63#define BUG_ON(expr)
64#endif
65
Alex Aizman7ba24712005-08-04 19:30:08 -070066static unsigned int iscsi_max_lun = 512;
67module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
68
Alex Aizman7ba24712005-08-04 19:30:08 -070069static inline void
Alex Aizman7ba24712005-08-04 19:30:08 -070070iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
71{
Mike Christie7cae5152006-01-13 18:05:47 -060072 ibuf->sg.page = virt_to_page(vbuf);
73 ibuf->sg.offset = offset_in_page(vbuf);
Alex Aizman7ba24712005-08-04 19:30:08 -070074 ibuf->sg.length = size;
75 ibuf->sent = 0;
Mike Christie7cae5152006-01-13 18:05:47 -060076 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070077}
78
79static inline void
80iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
81{
Mike Christie7cae5152006-01-13 18:05:47 -060082 ibuf->sg.page = sg->page;
83 ibuf->sg.offset = sg->offset;
84 ibuf->sg.length = sg->length;
Alex Aizman7ba24712005-08-04 19:30:08 -070085 /*
86 * Fastpath: sg element fits into single page
87 */
Mike Christiea1e80c22006-01-13 18:05:56 -060088 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
Mike Christie7cae5152006-01-13 18:05:47 -060089 ibuf->use_sendmsg = 0;
90 else
91 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070092 ibuf->sent = 0;
93}
94
95static inline int
96iscsi_buf_left(struct iscsi_buf *ibuf)
97{
98 int rc;
99
100 rc = ibuf->sg.length - ibuf->sent;
101 BUG_ON(rc < 0);
102 return rc;
103}
104
105static inline void
Mike Christieaf973482005-09-12 21:01:32 -0500106iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
107 u8* crc)
Alex Aizman7ba24712005-08-04 19:30:08 -0700108{
Mike Christie5bb0b552006-04-06 21:26:46 -0500109 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
110
James Bottomleyc9802cd2006-09-23 15:33:43 -0500111 crypto_hash_digest(&tcp_conn->tx_hash, &buf->sg, buf->sg.length, crc);
Mike Christie218432c2007-05-30 12:57:17 -0500112 buf->sg.length += sizeof(u32);
Alex Aizman7ba24712005-08-04 19:30:08 -0700113}
114
Alex Aizman7ba24712005-08-04 19:30:08 -0700115static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500116iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700117{
Mike Christie5bb0b552006-04-06 21:26:46 -0500118 struct sk_buff *skb = tcp_conn->in.skb;
Alex Aizman7ba24712005-08-04 19:30:08 -0700119
Mike Christie5bb0b552006-04-06 21:26:46 -0500120 tcp_conn->in.zero_copy_hdr = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700121
Mike Christie5bb0b552006-04-06 21:26:46 -0500122 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
123 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700124 /*
125 * Zero-copy PDU Header: using connection context
126 * to store header pointer.
127 */
128 if (skb_shinfo(skb)->frag_list == NULL &&
Mike Christie5bb0b552006-04-06 21:26:46 -0500129 !skb_shinfo(skb)->nr_frags) {
130 tcp_conn->in.hdr = (struct iscsi_hdr *)
131 ((char*)skb->data + tcp_conn->in.offset);
132 tcp_conn->in.zero_copy_hdr = 1;
133 } else {
Alex Aizman7ba24712005-08-04 19:30:08 -0700134 /* ignoring return code since we checked
135 * in.copy before */
Mike Christie5bb0b552006-04-06 21:26:46 -0500136 skb_copy_bits(skb, tcp_conn->in.offset,
137 &tcp_conn->hdr, tcp_conn->hdr_size);
138 tcp_conn->in.hdr = &tcp_conn->hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700139 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500140 tcp_conn->in.offset += tcp_conn->hdr_size;
141 tcp_conn->in.copy -= tcp_conn->hdr_size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700142 } else {
143 int hdr_remains;
144 int copylen;
145
146 /*
147 * PDU header scattered across SKB's,
148 * copying it... This'll happen quite rarely.
149 */
150
Mike Christie5bb0b552006-04-06 21:26:46 -0500151 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
152 tcp_conn->in.hdr_offset = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700153
Mike Christie5bb0b552006-04-06 21:26:46 -0500154 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700155 BUG_ON(hdr_remains <= 0);
156
Mike Christie5bb0b552006-04-06 21:26:46 -0500157 copylen = min(tcp_conn->in.copy, hdr_remains);
158 skb_copy_bits(skb, tcp_conn->in.offset,
159 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
160 copylen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700161
162 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
Mike Christie5bb0b552006-04-06 21:26:46 -0500163 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
164 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700165
Mike Christie5bb0b552006-04-06 21:26:46 -0500166 tcp_conn->in.offset += copylen;
167 tcp_conn->in.copy -= copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700168 if (copylen < hdr_remains) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500169 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
170 tcp_conn->in.hdr_offset += copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700171 return -EAGAIN;
172 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500173 tcp_conn->in.hdr = &tcp_conn->hdr;
174 tcp_conn->discontiguous_hdr_cnt++;
175 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700176 }
177
178 return 0;
179}
180
Mike Christie30a6c652006-04-06 21:13:39 -0500181/*
182 * must be called with session lock
183 */
184static void
Mike Christieb6c395e2006-07-24 15:47:15 -0500185iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700186{
Mike Christie5bb0b552006-04-06 21:26:46 -0500187 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500188 struct iscsi_r2t_info *r2t;
Mike Christie30a6c652006-04-06 21:13:39 -0500189 struct scsi_cmnd *sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700190
Mike Christieb6c395e2006-07-24 15:47:15 -0500191 /* flush ctask's r2t queues */
192 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
193 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
194 sizeof(void*));
195 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
196 }
197
Mike Christie30a6c652006-04-06 21:13:39 -0500198 sc = ctask->sc;
199 if (unlikely(!sc))
Alex Aizman7ba24712005-08-04 19:30:08 -0700200 return;
Mike Christie30a6c652006-04-06 21:13:39 -0500201
Mike Christie5bb0b552006-04-06 21:26:46 -0500202 tcp_ctask->xmstate = XMSTATE_IDLE;
203 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700204}
205
206/**
207 * iscsi_data_rsp - SCSI Data-In Response processing
208 * @conn: iscsi connection
209 * @ctask: scsi command task
210 **/
211static int
212iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
213{
Mike Christie5bb0b552006-04-06 21:26:46 -0500214 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
215 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
216 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700217 struct iscsi_session *session = conn->session;
Mike Christie857ae0b2007-05-30 12:57:15 -0500218 struct scsi_cmnd *sc = ctask->sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700219 int datasn = be32_to_cpu(rhdr->datasn);
220
Mike Christie77a23c22007-05-30 12:57:18 -0500221 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Alex Aizman7ba24712005-08-04 19:30:08 -0700222 /*
223 * setup Data-In byte counter (gets decremented..)
224 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500225 ctask->data_count = tcp_conn->in.datalen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700226
Mike Christie5bb0b552006-04-06 21:26:46 -0500227 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700228 return 0;
229
Mike Christied473cc72007-05-30 12:57:14 -0500230 if (tcp_ctask->exp_datasn != datasn) {
231 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
232 __FUNCTION__, tcp_ctask->exp_datasn, datasn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700233 return ISCSI_ERR_DATASN;
Mike Christied473cc72007-05-30 12:57:14 -0500234 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700235
Mike Christied473cc72007-05-30 12:57:14 -0500236 tcp_ctask->exp_datasn++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700237
Mike Christie5bb0b552006-04-06 21:26:46 -0500238 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
Mike Christie857ae0b2007-05-30 12:57:15 -0500239 if (tcp_ctask->data_offset + tcp_conn->in.datalen > sc->request_bufflen) {
240 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
241 __FUNCTION__, tcp_ctask->data_offset,
242 tcp_conn->in.datalen, sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700243 return ISCSI_ERR_DATA_OFFSET;
Mike Christie857ae0b2007-05-30 12:57:15 -0500244 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700245
246 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700247 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600248 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700249 int res_count = be32_to_cpu(rhdr->residual_count);
250
251 if (res_count > 0 &&
252 res_count <= sc->request_bufflen) {
253 sc->resid = res_count;
254 sc->result = (DID_OK << 16) | rhdr->cmd_status;
255 } else
256 sc->result = (DID_BAD_TARGET << 16) |
257 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600258 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700259 sc->resid = be32_to_cpu(rhdr->residual_count);
260 sc->result = (DID_OK << 16) | rhdr->cmd_status;
261 } else
262 sc->result = (DID_OK << 16) | rhdr->cmd_status;
263 }
264
265 conn->datain_pdus_cnt++;
266 return 0;
267}
268
269/**
270 * iscsi_solicit_data_init - initialize first Data-Out
271 * @conn: iscsi connection
272 * @ctask: scsi command task
273 * @r2t: R2T info
274 *
275 * Notes:
276 * Initialize first Data-Out within this R2T sequence and finds
277 * proper data_offset within this SCSI command.
278 *
279 * This function is called with connection lock taken.
280 **/
281static void
282iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
283 struct iscsi_r2t_info *r2t)
284{
285 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700286 struct scsi_cmnd *sc = ctask->sc;
287
Mike Christieffbfe922006-05-18 20:31:36 -0500288 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700289 memset(hdr, 0, sizeof(struct iscsi_data));
290 hdr->ttt = r2t->ttt;
291 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
292 r2t->solicit_datasn++;
293 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500294 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
295 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700296 hdr->exp_statsn = r2t->exp_statsn;
297 hdr->offset = cpu_to_be32(r2t->data_offset);
298 if (r2t->data_length > conn->max_xmit_dlength) {
299 hton24(hdr->dlength, conn->max_xmit_dlength);
300 r2t->data_count = conn->max_xmit_dlength;
301 hdr->flags = 0;
302 } else {
303 hton24(hdr->dlength, r2t->data_length);
304 r2t->data_count = r2t->data_length;
305 hdr->flags = ISCSI_FLAG_CMD_FINAL;
306 }
307 conn->dataout_pdus_cnt++;
308
309 r2t->sent = 0;
310
Mike Christie6e458cc2006-05-18 20:31:31 -0500311 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -0500312 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -0700313
Alex Aizman7ba24712005-08-04 19:30:08 -0700314 if (sc->use_sg) {
315 int i, sg_count = 0;
316 struct scatterlist *sg = sc->request_buffer;
317
318 r2t->sg = NULL;
319 for (i = 0; i < sc->use_sg; i++, sg += 1) {
320 /* FIXME: prefetch ? */
321 if (sg_count + sg->length > r2t->data_offset) {
322 int page_offset;
323
324 /* sg page found! */
325
326 /* offset within this page */
327 page_offset = r2t->data_offset - sg_count;
328
329 /* 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;
333
334 /* xmit logic will continue with next one */
335 r2t->sg = sg + 1;
336 break;
337 }
338 sg_count += sg->length;
339 }
340 BUG_ON(r2t->sg == NULL);
Mike Christie62f38302006-08-31 18:09:27 -0400341 } else {
342 iscsi_buf_init_iov(&r2t->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -0700343 (char*)sc->request_buffer + r2t->data_offset,
344 r2t->data_count);
Mike Christie62f38302006-08-31 18:09:27 -0400345 r2t->sg = NULL;
346 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700347}
348
349/**
350 * iscsi_r2t_rsp - iSCSI R2T Response processing
351 * @conn: iscsi connection
352 * @ctask: scsi command task
353 **/
354static int
355iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
356{
357 struct iscsi_r2t_info *r2t;
358 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500359 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
360 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
361 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700362 int r2tsn = be32_to_cpu(rhdr->r2tsn);
363 int rc;
364
Mike Christie98a94162006-08-31 18:09:26 -0400365 if (tcp_conn->in.datalen) {
366 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
367 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700368 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400369 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700370
Mike Christied473cc72007-05-30 12:57:14 -0500371 if (tcp_ctask->exp_datasn != r2tsn){
372 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
373 __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700374 return ISCSI_ERR_R2TSN;
Mike Christied473cc72007-05-30 12:57:14 -0500375 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700376
Alex Aizman7ba24712005-08-04 19:30:08 -0700377 /* fill-in new R2T associated with the task */
378 spin_lock(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -0500379 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
380
Alex Aizman7ba24712005-08-04 19:30:08 -0700381 if (!ctask->sc || ctask->mtask ||
382 session->state != ISCSI_STATE_LOGGED_IN) {
383 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
384 "recovery...\n", ctask->itt);
385 spin_unlock(&session->lock);
386 return 0;
387 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500388
Mike Christie5bb0b552006-04-06 21:26:46 -0500389 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700390 BUG_ON(!rc);
391
392 r2t->exp_statsn = rhdr->statsn;
393 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400394 if (r2t->data_length == 0) {
395 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
Alex Aizman7ba24712005-08-04 19:30:08 -0700396 spin_unlock(&session->lock);
397 return ISCSI_ERR_DATALEN;
398 }
399
Mike Christie98a94162006-08-31 18:09:26 -0400400 if (r2t->data_length > session->max_burst)
401 debug_scsi("invalid R2T with data len %u and max burst %u."
402 "Attempting to execute request.\n",
403 r2t->data_length, session->max_burst);
404
Alex Aizman7ba24712005-08-04 19:30:08 -0700405 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
Mike Christie857ae0b2007-05-30 12:57:15 -0500406 if (r2t->data_offset + r2t->data_length > ctask->sc->request_bufflen) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700407 spin_unlock(&session->lock);
Mike Christie98a94162006-08-31 18:09:26 -0400408 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
409 "offset %u and total length %d\n", r2t->data_length,
Mike Christie857ae0b2007-05-30 12:57:15 -0500410 r2t->data_offset, ctask->sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700411 return ISCSI_ERR_DATALEN;
412 }
413
414 r2t->ttt = rhdr->ttt; /* no flip */
415 r2t->solicit_datasn = 0;
416
417 iscsi_solicit_data_init(conn, ctask, r2t);
418
Mike Christied473cc72007-05-30 12:57:14 -0500419 tcp_ctask->exp_datasn = r2tsn + 1;
Mike Christie5bb0b552006-04-06 21:26:46 -0500420 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christie218432c2007-05-30 12:57:17 -0500421 tcp_ctask->xmstate |= XMSTATE_SOL_HDR_INIT;
Mike Christieb6c395e2006-07-24 15:47:15 -0500422 list_move_tail(&ctask->running, &conn->xmitqueue);
Alex Aizman7ba24712005-08-04 19:30:08 -0700423
Mike Christie55e32992006-01-13 18:05:53 -0600424 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -0700425 conn->r2t_pdus_cnt++;
426 spin_unlock(&session->lock);
427
428 return 0;
429}
430
431static int
Mike Christie5bb0b552006-04-06 21:26:46 -0500432iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700433{
Mike Christie5bb0b552006-04-06 21:26:46 -0500434 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700435 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700436 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500437 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
438 uint32_t cdgst, rdgst = 0, itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700439
Mike Christie5bb0b552006-04-06 21:26:46 -0500440 hdr = tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700441
442 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500443 tcp_conn->in.datalen = ntoh24(hdr->dlength);
444 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700445 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500446 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700447 return ISCSI_ERR_DATALEN;
448 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500449 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700450
451 /* read AHS */
Mike Christie5bb0b552006-04-06 21:26:46 -0500452 ahslen = hdr->hlength << 2;
453 tcp_conn->in.offset += ahslen;
454 tcp_conn->in.copy -= ahslen;
455 if (tcp_conn->in.copy < 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700456 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
Mike Christie5bb0b552006-04-06 21:26:46 -0500457 "%d bytes\n", ahslen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700458 return ISCSI_ERR_AHSLEN;
459 }
460
461 /* calculate read padding */
Mike Christie5bb0b552006-04-06 21:26:46 -0500462 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
463 if (tcp_conn->in.padding) {
464 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
465 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
Alex Aizman7ba24712005-08-04 19:30:08 -0700466 }
467
468 if (conn->hdrdgst_en) {
469 struct scatterlist sg;
470
471 sg_init_one(&sg, (u8 *)hdr,
Mike Christie5bb0b552006-04-06 21:26:46 -0500472 sizeof(struct iscsi_hdr) + ahslen);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500473 crypto_hash_digest(&tcp_conn->rx_hash, &sg, sg.length,
474 (u8 *)&cdgst);
Alex Aizman7ba24712005-08-04 19:30:08 -0700475 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
Mike Christie5bb0b552006-04-06 21:26:46 -0500476 ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600477 if (cdgst != rdgst) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500478 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
479 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
Mike Christie8a47cd32005-11-30 02:27:19 -0600480 return ISCSI_ERR_HDR_DGST;
481 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700482 }
483
Mike Christie5bb0b552006-04-06 21:26:46 -0500484 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700485 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500486 rc = iscsi_verify_itt(conn, hdr, &itt);
487 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
488 tcp_conn->in.datalen = 0; /* force drop */
489 return 0;
490 } else if (rc)
491 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700492
493 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500494 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
495 ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700496
Mike Christie5bb0b552006-04-06 21:26:46 -0500497 switch(opcode) {
498 case ISCSI_OP_SCSI_DATA_IN:
499 tcp_conn->in.ctask = session->cmds[itt];
500 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
Mike Christie275fd7d2006-07-24 15:47:17 -0500501 if (rc)
502 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500503 /* fall through */
504 case ISCSI_OP_SCSI_CMD_RSP:
505 tcp_conn->in.ctask = session->cmds[itt];
506 if (tcp_conn->in.datalen)
507 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700508
Mike Christie5bb0b552006-04-06 21:26:46 -0500509 spin_lock(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500510 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
511 spin_unlock(&session->lock);
512 break;
513 case ISCSI_OP_R2T:
514 tcp_conn->in.ctask = session->cmds[itt];
515 if (ahslen)
516 rc = ISCSI_ERR_AHSLEN;
517 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
518 DMA_TO_DEVICE)
519 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
520 else
521 rc = ISCSI_ERR_PROTO;
522 break;
523 case ISCSI_OP_LOGIN_RSP:
524 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500525 case ISCSI_OP_REJECT:
526 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500527 /*
528 * It is possible that we could get a PDU with a buffer larger
529 * than 8K, but there are no targets that currently do this.
530 * For now we fail until we find a vendor that needs it
531 */
Mike Christiebf32ed32007-02-28 17:32:17 -0600532 if (ISCSI_DEF_MAX_RECV_SEG_LEN <
Mike Christiec8dc1e52006-07-24 15:47:39 -0500533 tcp_conn->in.datalen) {
534 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
535 "but conn buffer is only %u (opcode %0x)\n",
536 tcp_conn->in.datalen,
Mike Christiebf32ed32007-02-28 17:32:17 -0600537 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
Mike Christiec8dc1e52006-07-24 15:47:39 -0500538 rc = ISCSI_ERR_PROTO;
539 break;
540 }
541
Mike Christie5bb0b552006-04-06 21:26:46 -0500542 if (tcp_conn->in.datalen)
543 goto copy_hdr;
544 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500545 case ISCSI_OP_LOGOUT_RSP:
546 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500547 case ISCSI_OP_SCSI_TMFUNC_RSP:
548 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
549 break;
550 default:
551 rc = ISCSI_ERR_BAD_OPCODE;
552 break;
553 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700554
555 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500556
557copy_hdr:
558 /*
559 * if we did zero copy for the header but we will need multiple
560 * skbs to complete the command then we have to copy the header
561 * for later use
562 */
Mike Christie275fd7d2006-07-24 15:47:17 -0500563 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
Mike Christie5bb0b552006-04-06 21:26:46 -0500564 (tcp_conn->in.datalen + tcp_conn->in.padding +
565 (conn->datadgst_en ? 4 : 0))) {
566 debug_tcp("Copying header for later use. in.copy %d in.datalen"
567 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
568 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
569 sizeof(struct iscsi_hdr));
570 tcp_conn->in.hdr = &tcp_conn->hdr;
571 tcp_conn->in.zero_copy_hdr = 0;
572 }
573 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700574}
575
576/**
577 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
Mike Christie5bb0b552006-04-06 21:26:46 -0500578 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700579 * @ctask: scsi command task
580 * @buf: buffer to copy to
581 * @buf_size: size of buffer
582 * @offset: offset within the buffer
583 *
584 * Notes:
585 * The function calls skb_copy_bits() and updates per-connection and
586 * per-cmd byte counters.
587 *
588 * Read counters (in bytes):
589 *
590 * conn->in.offset offset within in progress SKB
591 * conn->in.copy left to copy from in progress SKB
592 * including padding
593 * conn->in.copied copied already from in progress SKB
594 * conn->data_copied copied already from in progress buffer
595 * ctask->sent total bytes sent up to the MidLayer
596 * ctask->data_count left to copy from in progress Data-In
597 * buf_left left to copy from in progress buffer
598 **/
599static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500600iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -0700601 void *buf, int buf_size, int offset)
602{
Mike Christie5bb0b552006-04-06 21:26:46 -0500603 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
604 int buf_left = buf_size - (tcp_conn->data_copied + offset);
Mike Christie857ae0b2007-05-30 12:57:15 -0500605 unsigned size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700606 int rc;
607
608 size = min(size, ctask->data_count);
609
610 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500611 size, tcp_conn->in.offset, tcp_conn->in.copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700612
613 BUG_ON(size <= 0);
Mike Christie857ae0b2007-05-30 12:57:15 -0500614 BUG_ON(tcp_ctask->sent + size > ctask->sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700615
Mike Christie5bb0b552006-04-06 21:26:46 -0500616 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
617 (char*)buf + (offset + tcp_conn->data_copied), size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700618 /* must fit into skb->len */
619 BUG_ON(rc);
620
Mike Christie5bb0b552006-04-06 21:26:46 -0500621 tcp_conn->in.offset += size;
622 tcp_conn->in.copy -= size;
623 tcp_conn->in.copied += size;
624 tcp_conn->data_copied += size;
625 tcp_ctask->sent += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700626 ctask->data_count -= size;
627
Mike Christie5bb0b552006-04-06 21:26:46 -0500628 BUG_ON(tcp_conn->in.copy < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700629 BUG_ON(ctask->data_count < 0);
630
Mike Christie5bb0b552006-04-06 21:26:46 -0500631 if (buf_size != (tcp_conn->data_copied + offset)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700632 if (!ctask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500633 BUG_ON(buf_size - tcp_conn->data_copied < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700634 /* done with this PDU */
Mike Christie5bb0b552006-04-06 21:26:46 -0500635 return buf_size - tcp_conn->data_copied;
Alex Aizman7ba24712005-08-04 19:30:08 -0700636 }
637 return -EAGAIN;
638 }
639
640 /* done with this buffer or with both - PDU and buffer */
Mike Christie5bb0b552006-04-06 21:26:46 -0500641 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700642 return 0;
643}
644
645/**
646 * iscsi_tcp_copy - copy skb bits to the destanation buffer
Mike Christie5bb0b552006-04-06 21:26:46 -0500647 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700648 *
649 * Notes:
650 * The function calls skb_copy_bits() and updates per-connection
651 * byte counters.
652 **/
653static inline int
Mike Christieca518682006-08-31 18:09:32 -0400654iscsi_tcp_copy(struct iscsi_conn *conn, int buf_size)
Alex Aizman7ba24712005-08-04 19:30:08 -0700655{
Mike Christiec8dc1e52006-07-24 15:47:39 -0500656 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500657 int buf_left = buf_size - tcp_conn->data_copied;
658 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700659 int rc;
660
661 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500662 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700663 BUG_ON(size <= 0);
664
Mike Christie5bb0b552006-04-06 21:26:46 -0500665 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christiec8dc1e52006-07-24 15:47:39 -0500666 (char*)conn->data + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700667 BUG_ON(rc);
668
Mike Christie5bb0b552006-04-06 21:26:46 -0500669 tcp_conn->in.offset += size;
670 tcp_conn->in.copy -= size;
671 tcp_conn->in.copied += size;
672 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700673
Mike Christie5bb0b552006-04-06 21:26:46 -0500674 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700675 return -EAGAIN;
676
677 return 0;
678}
679
680static inline void
James Bottomleyc9802cd2006-09-23 15:33:43 -0500681partial_sg_digest_update(struct hash_desc *desc, struct scatterlist *sg,
Mike Christie62f38302006-08-31 18:09:27 -0400682 int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700683{
684 struct scatterlist temp;
685
686 memcpy(&temp, sg, sizeof(struct scatterlist));
687 temp.offset = offset;
688 temp.length = length;
James Bottomleyc9802cd2006-09-23 15:33:43 -0500689 crypto_hash_update(desc, &temp, length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700690}
691
Mike Christief6cfba12005-11-29 23:12:57 -0600692static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500693iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600694{
695 struct scatterlist tmp;
696
697 sg_init_one(&tmp, buf, len);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500698 crypto_hash_update(&tcp_conn->rx_hash, &tmp, len);
Mike Christief6cfba12005-11-29 23:12:57 -0600699}
700
Alex Aizman7ba24712005-08-04 19:30:08 -0700701static int iscsi_scsi_data_in(struct iscsi_conn *conn)
702{
Mike Christie5bb0b552006-04-06 21:26:46 -0500703 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
704 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
705 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700706 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600707 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700708 int i, offset, rc = 0;
709
710 BUG_ON((void*)ctask != sc->SCp.ptr);
711
712 /*
713 * copying Data-In into the Scsi_Cmnd
714 */
715 if (!sc->use_sg) {
716 i = ctask->data_count;
Mike Christie5bb0b552006-04-06 21:26:46 -0500717 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
718 sc->request_bufflen,
719 tcp_ctask->data_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700720 if (rc == -EAGAIN)
721 return rc;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600722 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500723 iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
724 i);
Alex Aizman7ba24712005-08-04 19:30:08 -0700725 rc = 0;
726 goto done;
727 }
728
Mike Christie5bb0b552006-04-06 21:26:46 -0500729 offset = tcp_ctask->data_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700730 sg = sc->request_buffer;
731
Mike Christie5bb0b552006-04-06 21:26:46 -0500732 if (tcp_ctask->data_offset)
733 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700734 offset -= sg[i].length;
735 /* we've passed through partial sg*/
736 if (offset < 0)
737 offset = 0;
738
Mike Christie5bb0b552006-04-06 21:26:46 -0500739 for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700740 char *dest;
741
742 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500743 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700744 sg[i].length, offset);
745 kunmap_atomic(dest, KM_SOFTIRQ0);
746 if (rc == -EAGAIN)
747 /* continue with the next SKB/PDU */
748 return rc;
749 if (!rc) {
750 if (conn->datadgst_en) {
751 if (!offset)
Herbert Xudc64ddf2006-08-24 18:45:50 +1000752 crypto_hash_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500753 &tcp_conn->rx_hash,
Arne Redlichc959e1c2006-12-17 12:10:24 -0600754 &sg[i], sg[i].length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700755 else
Mike Christie62f38302006-08-31 18:09:27 -0400756 partial_sg_digest_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500757 &tcp_conn->rx_hash,
Mike Christie5bb0b552006-04-06 21:26:46 -0500758 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700759 sg[i].offset + offset,
760 sg[i].length - offset);
761 }
762 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500763 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700764 }
765
766 if (!ctask->data_count) {
767 if (rc && conn->datadgst_en)
768 /*
769 * data-in is complete, but buffer not...
770 */
James Bottomleyc9802cd2006-09-23 15:33:43 -0500771 partial_sg_digest_update(&tcp_conn->rx_hash,
772 &sg[i],
773 sg[i].offset,
774 sg[i].length-rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700775 rc = 0;
776 break;
777 }
778
Mike Christie5bb0b552006-04-06 21:26:46 -0500779 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700780 return -EAGAIN;
781 }
782 BUG_ON(ctask->data_count);
783
784done:
785 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500786 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Mike Christieb6c395e2006-07-24 15:47:15 -0500787 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
788 (long)sc, sc->result, ctask->itt,
789 tcp_conn->in.hdr->flags);
Mike Christie5bb0b552006-04-06 21:26:46 -0500790 spin_lock(&conn->session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500791 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
792 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700793 }
794
795 return rc;
796}
797
798static int
799iscsi_data_recv(struct iscsi_conn *conn)
800{
Mike Christie5bb0b552006-04-06 21:26:46 -0500801 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
802 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700803
Mike Christie5bb0b552006-04-06 21:26:46 -0500804 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
805 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700806 case ISCSI_OP_SCSI_DATA_IN:
807 rc = iscsi_scsi_data_in(conn);
808 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500809 case ISCSI_OP_SCSI_CMD_RSP:
Alex Aizman7ba24712005-08-04 19:30:08 -0700810 case ISCSI_OP_TEXT_RSP:
811 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500812 case ISCSI_OP_ASYNC_EVENT:
813 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700814 /*
815 * Collect data segment to the connection's data
816 * placeholder
817 */
Mike Christieca518682006-08-31 18:09:32 -0400818 if (iscsi_tcp_copy(conn, tcp_conn->in.datalen)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700819 rc = -EAGAIN;
820 goto exit;
821 }
822
Mike Christiec8dc1e52006-07-24 15:47:39 -0500823 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500824 tcp_conn->in.datalen);
825 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
Mike Christiec8dc1e52006-07-24 15:47:39 -0500826 iscsi_recv_digest_update(tcp_conn, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500827 tcp_conn->in.datalen);
828 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700829 default:
830 BUG_ON(1);
831 }
832exit:
833 return rc;
834}
835
836/**
837 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
838 * @rd_desc: read descriptor
839 * @skb: socket buffer
840 * @offset: offset in skb
841 * @len: skb->len - offset
842 **/
843static int
844iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
845 unsigned int offset, size_t len)
846{
847 int rc;
848 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500849 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700850 int processed;
851 char pad[ISCSI_PAD_LEN];
852 struct scatterlist sg;
853
854 /*
855 * Save current SKB and its offset in the corresponding
856 * connection context.
857 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500858 tcp_conn->in.copy = skb->len - offset;
859 tcp_conn->in.offset = offset;
860 tcp_conn->in.skb = skb;
861 tcp_conn->in.len = tcp_conn->in.copy;
862 BUG_ON(tcp_conn->in.copy <= 0);
863 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700864
865more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500866 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700867 rc = 0;
868
869 if (unlikely(conn->suspend_rx)) {
870 debug_tcp("conn %d Rx suspended!\n", conn->id);
871 return 0;
872 }
873
Mike Christie5bb0b552006-04-06 21:26:46 -0500874 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
875 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
876 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700877 if (rc) {
878 if (rc == -EAGAIN)
879 goto nomore;
880 else {
Mike Christied82967c2006-07-24 15:47:11 -0500881 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700882 return 0;
883 }
884 }
885
886 /*
887 * Verify and process incoming PDU header.
888 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500889 rc = iscsi_tcp_hdr_recv(conn);
890 if (!rc && tcp_conn->in.datalen) {
Mike Christiedd8c0d92006-08-31 18:09:28 -0400891 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -0500892 crypto_hash_init(&tcp_conn->rx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -0500893 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700894 } else if (rc) {
Mike Christie40527af2006-07-24 15:47:45 -0500895 iscsi_conn_failure(conn, rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700896 return 0;
897 }
898 }
899
Mike Christiedbdb0162007-05-30 12:57:20 -0500900 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV &&
901 tcp_conn->in.copy) {
Mike Christief6cfba12005-11-29 23:12:57 -0600902 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500903
Alex Aizman7ba24712005-08-04 19:30:08 -0700904 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500905 tcp_conn->in.offset, tcp_conn->in.copy);
Mike Christiedbdb0162007-05-30 12:57:20 -0500906
907 if (!tcp_conn->data_copied) {
908 if (tcp_conn->in.padding) {
909 debug_tcp("padding -> %d\n",
910 tcp_conn->in.padding);
911 memset(pad, 0, tcp_conn->in.padding);
912 sg_init_one(&sg, pad, tcp_conn->in.padding);
913 crypto_hash_update(&tcp_conn->rx_hash,
914 &sg, sg.length);
915 }
916 crypto_hash_final(&tcp_conn->rx_hash,
917 (u8 *) &tcp_conn->in.datadgst);
918 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
919 }
920
Mike Christieca518682006-08-31 18:09:32 -0400921 rc = iscsi_tcp_copy(conn, sizeof(uint32_t));
922 if (rc) {
923 if (rc == -EAGAIN)
924 goto again;
925 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
926 return 0;
927 }
928
929 memcpy(&recv_digest, conn->data, sizeof(uint32_t));
Mike Christie5bb0b552006-04-06 21:26:46 -0500930 if (recv_digest != tcp_conn->in.datadgst) {
Mike Christief6cfba12005-11-29 23:12:57 -0600931 debug_tcp("iscsi_tcp: data digest error!"
932 "0x%x != 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500933 tcp_conn->in.datadgst);
Mike Christief6cfba12005-11-29 23:12:57 -0600934 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
935 return 0;
936 } else {
937 debug_tcp("iscsi_tcp: data digest match!"
938 "0x%x == 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500939 tcp_conn->in.datadgst);
940 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700941 }
942 }
943
Mike Christie5bb0b552006-04-06 21:26:46 -0500944 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
Mike Christiedbdb0162007-05-30 12:57:20 -0500945 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700946 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500947 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700948
949 rc = iscsi_data_recv(conn);
950 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500951 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700952 goto again;
Mike Christied82967c2006-07-24 15:47:11 -0500953 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700954 return 0;
955 }
Mike Christiedbdb0162007-05-30 12:57:20 -0500956
957 if (tcp_conn->in.padding)
958 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
959 else if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500960 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Mike Christiedbdb0162007-05-30 12:57:20 -0500961 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500962 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Mike Christiedbdb0162007-05-30 12:57:20 -0500963 tcp_conn->data_copied = 0;
964 }
965
966 if (tcp_conn->in_progress == IN_PROGRESS_PAD_RECV &&
967 tcp_conn->in.copy) {
968 int copylen = min(tcp_conn->in.padding - tcp_conn->data_copied,
969 tcp_conn->in.copy);
970
971 tcp_conn->in.copy -= copylen;
972 tcp_conn->in.offset += copylen;
973 tcp_conn->data_copied += copylen;
974
975 if (tcp_conn->data_copied != tcp_conn->in.padding)
976 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
977 else if (conn->datadgst_en)
978 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
979 else
980 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
981 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700982 }
983
984 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500985 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
986 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700987
Mike Christie5bb0b552006-04-06 21:26:46 -0500988 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700989 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500990 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700991 goto more;
992 }
993
994nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500995 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700996 BUG_ON(processed == 0);
997 return processed;
998
999again:
Mike Christie5bb0b552006-04-06 21:26:46 -05001000 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -07001001 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
1002 processed, (int)len, (int)rd_desc->count);
1003 BUG_ON(processed == 0);
1004 BUG_ON(processed > len);
1005
1006 conn->rxdata_octets += processed;
1007 return processed;
1008}
1009
1010static void
1011iscsi_tcp_data_ready(struct sock *sk, int flag)
1012{
1013 struct iscsi_conn *conn = sk->sk_user_data;
1014 read_descriptor_t rd_desc;
1015
1016 read_lock(&sk->sk_callback_lock);
1017
Mike Christie665b44a2006-05-02 19:46:49 -05001018 /*
1019 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
1020 * We set count to 1 because we want the network layer to
1021 * hand us all the skbs that are available. iscsi_tcp_data_recv
1022 * handled pdus that cross buffers or pdus that still need data.
1023 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001024 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -05001025 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001026 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
1027
1028 read_unlock(&sk->sk_callback_lock);
1029}
1030
1031static void
1032iscsi_tcp_state_change(struct sock *sk)
1033{
Mike Christie5bb0b552006-04-06 21:26:46 -05001034 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001035 struct iscsi_conn *conn;
1036 struct iscsi_session *session;
1037 void (*old_state_change)(struct sock *);
1038
1039 read_lock(&sk->sk_callback_lock);
1040
1041 conn = (struct iscsi_conn*)sk->sk_user_data;
1042 session = conn->session;
1043
Mike Christiee6273992005-11-29 23:12:49 -06001044 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1045 sk->sk_state == TCP_CLOSE) &&
1046 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001047 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1048 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1049 }
1050
Mike Christie5bb0b552006-04-06 21:26:46 -05001051 tcp_conn = conn->dd_data;
1052 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001053
1054 read_unlock(&sk->sk_callback_lock);
1055
1056 old_state_change(sk);
1057}
1058
1059/**
1060 * iscsi_write_space - Called when more output buffer space is available
1061 * @sk: socket space is available for
1062 **/
1063static void
1064iscsi_write_space(struct sock *sk)
1065{
1066 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001067 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1068
1069 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001070 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001071 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001072}
1073
1074static void
1075iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1076{
Mike Christie5bb0b552006-04-06 21:26:46 -05001077 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1078 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001079
1080 /* assign new callbacks */
1081 write_lock_bh(&sk->sk_callback_lock);
1082 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001083 tcp_conn->old_data_ready = sk->sk_data_ready;
1084 tcp_conn->old_state_change = sk->sk_state_change;
1085 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001086 sk->sk_data_ready = iscsi_tcp_data_ready;
1087 sk->sk_state_change = iscsi_tcp_state_change;
1088 sk->sk_write_space = iscsi_write_space;
1089 write_unlock_bh(&sk->sk_callback_lock);
1090}
1091
1092static void
Mike Christie1c834692006-07-24 15:47:26 -05001093iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001094{
Mike Christie5bb0b552006-04-06 21:26:46 -05001095 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001096
1097 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1098 write_lock_bh(&sk->sk_callback_lock);
1099 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001100 sk->sk_data_ready = tcp_conn->old_data_ready;
1101 sk->sk_state_change = tcp_conn->old_state_change;
1102 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001103 sk->sk_no_check = 0;
1104 write_unlock_bh(&sk->sk_callback_lock);
1105}
1106
1107/**
1108 * iscsi_send - generic send routine
1109 * @sk: kernel's socket
1110 * @buf: buffer to write from
1111 * @size: actual size to write
1112 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001113 */
1114static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001115iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001116{
Mike Christie5bb0b552006-04-06 21:26:46 -05001117 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1118 struct socket *sk = tcp_conn->sock;
Mike Christie3219e522006-05-30 00:37:28 -05001119 int offset = buf->sg.offset + buf->sent, res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001120
Mike Christie7cae5152006-01-13 18:05:47 -06001121 /*
1122 * if we got use_sg=0 or are sending something we kmallocd
1123 * then we did not have to do kmap (kmap returns page_address)
1124 *
1125 * if we got use_sg > 0, but had to drop down, we do not
1126 * set clustering so this should only happen for that
1127 * slab case.
1128 */
1129 if (buf->use_sendmsg)
Mike Christie3219e522006-05-30 00:37:28 -05001130 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
Mike Christie7cae5152006-01-13 18:05:47 -06001131 else
Mike Christie3219e522006-05-30 00:37:28 -05001132 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1133
1134 if (res >= 0) {
1135 conn->txdata_octets += res;
1136 buf->sent += res;
1137 return res;
1138 }
1139
1140 tcp_conn->sendpage_failures_cnt++;
1141 if (res == -EAGAIN)
1142 res = -ENOBUFS;
1143 else
1144 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1145 return res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001146}
1147
1148/**
1149 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1150 * @conn: iscsi connection
1151 * @buf: buffer to write from
1152 * @datalen: lenght of data to be sent after the header
1153 *
1154 * Notes:
1155 * (Tx, Fast Path)
1156 **/
1157static inline int
1158iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1159{
Alex Aizman7ba24712005-08-04 19:30:08 -07001160 int flags = 0; /* MSG_DONTWAIT; */
1161 int res, size;
1162
1163 size = buf->sg.length - buf->sent;
1164 BUG_ON(buf->sent + size > buf->sg.length);
1165 if (buf->sent + size != buf->sg.length || datalen)
1166 flags |= MSG_MORE;
1167
FUJITA Tomonori56851692006-01-13 18:05:44 -06001168 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001169 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1170 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001171 if (size != res)
1172 return -EAGAIN;
1173 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001174 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001175
1176 return res;
1177}
1178
1179/**
1180 * iscsi_sendpage - send one page of iSCSI Data-Out.
1181 * @conn: iscsi connection
1182 * @buf: buffer to write from
1183 * @count: remaining data
1184 * @sent: number of bytes sent
1185 *
1186 * Notes:
1187 * (Tx, Fast Path)
1188 **/
1189static inline int
1190iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1191 int *count, int *sent)
1192{
Alex Aizman7ba24712005-08-04 19:30:08 -07001193 int flags = 0; /* MSG_DONTWAIT; */
1194 int res, size;
1195
1196 size = buf->sg.length - buf->sent;
1197 BUG_ON(buf->sent + size > buf->sg.length);
1198 if (size > *count)
1199 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001200 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001201 flags |= MSG_MORE;
1202
FUJITA Tomonori56851692006-01-13 18:05:44 -06001203 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001204 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1205 size, buf->sent, *count, *sent, res);
1206 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001207 *count -= res;
1208 *sent += res;
1209 if (size != res)
1210 return -EAGAIN;
1211 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001212 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001213
1214 return res;
1215}
1216
1217static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001218iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
Mike Christie62f38302006-08-31 18:09:27 -04001219 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001220{
James Bottomleyc9802cd2006-09-23 15:33:43 -05001221 crypto_hash_init(&tcp_conn->tx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -05001222 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001223}
1224
Alex Aizman7ba24712005-08-04 19:30:08 -07001225/**
1226 * iscsi_solicit_data_cont - initialize next Data-Out
1227 * @conn: iscsi connection
1228 * @ctask: scsi command task
1229 * @r2t: R2T info
1230 * @left: bytes left to transfer
1231 *
1232 * Notes:
1233 * Initialize next Data-Out within this R2T sequence and continue
1234 * to process next Scatter-Gather element(if any) of this SCSI command.
1235 *
1236 * Called under connection lock.
1237 **/
1238static void
1239iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1240 struct iscsi_r2t_info *r2t, int left)
1241{
1242 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001243 struct scsi_cmnd *sc = ctask->sc;
1244 int new_offset;
1245
Mike Christieffbfe922006-05-18 20:31:36 -05001246 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001247 memset(hdr, 0, sizeof(struct iscsi_data));
1248 hdr->ttt = r2t->ttt;
1249 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1250 r2t->solicit_datasn++;
1251 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001252 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1253 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001254 hdr->exp_statsn = r2t->exp_statsn;
1255 new_offset = r2t->data_offset + r2t->sent;
1256 hdr->offset = cpu_to_be32(new_offset);
1257 if (left > conn->max_xmit_dlength) {
1258 hton24(hdr->dlength, conn->max_xmit_dlength);
1259 r2t->data_count = conn->max_xmit_dlength;
1260 } else {
1261 hton24(hdr->dlength, left);
1262 r2t->data_count = left;
1263 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1264 }
1265 conn->dataout_pdus_cnt++;
1266
Mike Christie6e458cc2006-05-18 20:31:31 -05001267 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001268 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001269
Mike Christie62f38302006-08-31 18:09:27 -04001270 if (iscsi_buf_left(&r2t->sendbuf))
1271 return;
1272
1273 if (sc->use_sg) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001274 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1275 r2t->sg += 1;
Mike Christie62f38302006-08-31 18:09:27 -04001276 } else {
1277 iscsi_buf_init_iov(&r2t->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -07001278 (char*)sc->request_buffer + new_offset,
1279 r2t->data_count);
Mike Christie62f38302006-08-31 18:09:27 -04001280 r2t->sg = NULL;
1281 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001282}
1283
Mike Christie62f38302006-08-31 18:09:27 -04001284static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask,
1285 unsigned long len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001286{
Mike Christie62f38302006-08-31 18:09:27 -04001287 tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1);
1288 if (!tcp_ctask->pad_count)
1289 return;
Alex Aizman7ba24712005-08-04 19:30:08 -07001290
Mike Christie62f38302006-08-31 18:09:27 -04001291 tcp_ctask->pad_count = ISCSI_PAD_LEN - tcp_ctask->pad_count;
1292 debug_scsi("write padding %d bytes\n", tcp_ctask->pad_count);
1293 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Alex Aizman7ba24712005-08-04 19:30:08 -07001294}
1295
1296/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001297 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001298 * @conn: iscsi connection
1299 * @ctask: scsi command task
1300 * @sc: scsi command
1301 **/
1302static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001303iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001304{
Mike Christie5bb0b552006-04-06 21:26:46 -05001305 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001306
Mike Christie5bb0b552006-04-06 21:26:46 -05001307 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Mike Christie218432c2007-05-30 12:57:17 -05001308 tcp_ctask->xmstate = XMSTATE_CMD_HDR_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001309}
1310
1311/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001312 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001313 * @conn: iscsi connection
1314 * @mtask: task management task
1315 *
1316 * Notes:
1317 * The function can return -EAGAIN in which case caller must
1318 * call it again later, or recover. '0' return code means successful
1319 * xmit.
1320 *
Mike Christie218432c2007-05-30 12:57:17 -05001321 * Management xmit state machine consists of these states:
1322 * XMSTATE_IMM_HDR_INIT - calculate digest of PDU Header
1323 * XMSTATE_IMM_HDR - PDU Header xmit in progress
1324 * XMSTATE_IMM_DATA - PDU Data xmit in progress
1325 * XMSTATE_IDLE - management PDU is done
Alex Aizman7ba24712005-08-04 19:30:08 -07001326 **/
1327static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001328iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001329{
Mike Christie5bb0b552006-04-06 21:26:46 -05001330 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001331 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001332
1333 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001334 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001335
Mike Christie218432c2007-05-30 12:57:17 -05001336 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR_INIT) {
1337 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
1338 sizeof(struct iscsi_hdr));
1339
1340 if (mtask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001341 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie218432c2007-05-30 12:57:17 -05001342 iscsi_buf_init_iov(&tcp_mtask->sendbuf,
1343 (char*)mtask->data,
1344 mtask->data_count);
1345 }
1346
Mike Christieaf973482005-09-12 21:01:32 -05001347 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001348 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001349 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001350 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1351 (u8*)tcp_mtask->hdrext);
Mike Christie218432c2007-05-30 12:57:17 -05001352
1353 tcp_mtask->sent = 0;
1354 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR_INIT;
1355 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
1356 }
1357
1358 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
Mike Christie3219e522006-05-30 00:37:28 -05001359 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1360 mtask->data_count);
Mike Christie218432c2007-05-30 12:57:17 -05001361 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001362 return rc;
Mike Christie218432c2007-05-30 12:57:17 -05001363 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001364 }
1365
Mike Christie5bb0b552006-04-06 21:26:46 -05001366 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001367 BUG_ON(!mtask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001368 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001369 /* FIXME: implement.
1370 * Virtual buffer could be spreaded across multiple pages...
1371 */
1372 do {
Mike Christie3219e522006-05-30 00:37:28 -05001373 int rc;
1374
1375 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1376 &mtask->data_count, &tcp_mtask->sent);
1377 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001378 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001379 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001380 }
1381 } while (mtask->data_count);
1382 }
1383
Mike Christie5bb0b552006-04-06 21:26:46 -05001384 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
Al Virob4377352007-02-09 16:39:40 +00001385 if (mtask->hdr->itt == RESERVED_ITT) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001386 struct iscsi_session *session = conn->session;
1387
1388 spin_lock_bh(&session->lock);
1389 list_del(&conn->mtask->running);
1390 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1391 sizeof(void*));
1392 spin_unlock_bh(&session->lock);
1393 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001394 return 0;
1395}
1396
Mike Christie218432c2007-05-30 12:57:17 -05001397static int
1398iscsi_send_cmd_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001399{
Mike Christie218432c2007-05-30 12:57:17 -05001400 struct scsi_cmnd *sc = ctask->sc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001401 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie218432c2007-05-30 12:57:17 -05001402 int rc = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -05001403
Mike Christie218432c2007-05-30 12:57:17 -05001404 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_INIT) {
1405 tcp_ctask->sent = 0;
1406 tcp_ctask->sg_count = 0;
1407 tcp_ctask->exp_datasn = 0;
Mike Christie3219e522006-05-30 00:37:28 -05001408
Mike Christie218432c2007-05-30 12:57:17 -05001409 if (sc->sc_data_direction == DMA_TO_DEVICE) {
1410 if (sc->use_sg) {
1411 struct scatterlist *sg = sc->request_buffer;
Alex Aizman7ba24712005-08-04 19:30:08 -07001412
Mike Christie218432c2007-05-30 12:57:17 -05001413 iscsi_buf_init_sg(&tcp_ctask->sendbuf, sg);
1414 tcp_ctask->sg = sg + 1;
1415 tcp_ctask->bad_sg = sg + sc->use_sg;
1416 } else {
1417 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1418 sc->request_buffer,
1419 sc->request_bufflen);
1420 tcp_ctask->sg = NULL;
1421 tcp_ctask->bad_sg = NULL;
1422 }
1423
1424 debug_scsi("cmd [itt 0x%x total %d imm_data %d "
1425 "unsol count %d, unsol offset %d]\n",
1426 ctask->itt, sc->request_bufflen,
1427 ctask->imm_count, ctask->unsol_count,
1428 ctask->unsol_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -07001429 }
Mike Christie218432c2007-05-30 12:57:17 -05001430
1431 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
1432 sizeof(struct iscsi_hdr));
1433
1434 if (conn->hdrdgst_en)
1435 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1436 (u8*)tcp_ctask->hdrext);
1437 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_INIT;
1438 tcp_ctask->xmstate |= XMSTATE_CMD_HDR_XMIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001439 }
1440
Mike Christie218432c2007-05-30 12:57:17 -05001441 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_XMIT) {
1442 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1443 if (rc)
1444 return rc;
1445 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_XMIT;
1446
1447 if (sc->sc_data_direction != DMA_TO_DEVICE)
1448 return 0;
1449
1450 if (ctask->imm_count) {
1451 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1452 iscsi_set_padding(tcp_ctask, ctask->imm_count);
1453
1454 if (ctask->conn->datadgst_en) {
1455 iscsi_data_digest_init(ctask->conn->dd_data,
1456 tcp_ctask);
1457 tcp_ctask->immdigest = 0;
1458 }
1459 }
1460
1461 if (ctask->unsol_count)
1462 tcp_ctask->xmstate |=
1463 XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
1464 }
1465 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001466}
1467
Mike Christie62f38302006-08-31 18:09:27 -04001468static int
1469iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1470{
1471 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1472 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1473 int sent = 0, rc;
1474
1475 if (tcp_ctask->xmstate & XMSTATE_W_PAD) {
1476 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
1477 tcp_ctask->pad_count);
1478 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001479 crypto_hash_update(&tcp_conn->tx_hash,
1480 &tcp_ctask->sendbuf.sg,
1481 tcp_ctask->sendbuf.sg.length);
Mike Christie62f38302006-08-31 18:09:27 -04001482 } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD))
1483 return 0;
1484
1485 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
1486 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_PAD;
1487 debug_scsi("sending %d pad bytes for itt 0x%x\n",
1488 tcp_ctask->pad_count, ctask->itt);
1489 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1490 &sent);
1491 if (rc) {
1492 debug_scsi("padding send failed %d\n", rc);
1493 tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD;
1494 }
1495 return rc;
1496}
1497
1498static int
1499iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1500 struct iscsi_buf *buf, uint32_t *digest)
1501{
1502 struct iscsi_tcp_cmd_task *tcp_ctask;
1503 struct iscsi_tcp_conn *tcp_conn;
1504 int rc, sent = 0;
1505
1506 if (!conn->datadgst_en)
1507 return 0;
1508
1509 tcp_ctask = ctask->dd_data;
1510 tcp_conn = conn->dd_data;
1511
1512 if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) {
James Bottomleyc9802cd2006-09-23 15:33:43 -05001513 crypto_hash_final(&tcp_conn->tx_hash, (u8*)digest);
Mike Christie62f38302006-08-31 18:09:27 -04001514 iscsi_buf_init_iov(buf, (char*)digest, 4);
1515 }
1516 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST;
1517
1518 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
1519 if (!rc)
1520 debug_scsi("sent digest 0x%x for itt 0x%x\n", *digest,
1521 ctask->itt);
1522 else {
1523 debug_scsi("sending digest 0x%x failed for itt 0x%x!\n",
1524 *digest, ctask->itt);
1525 tcp_ctask->xmstate |= XMSTATE_W_RESEND_DATA_DIGEST;
1526 }
1527 return rc;
1528}
1529
1530static int
1531iscsi_send_data(struct iscsi_cmd_task *ctask, struct iscsi_buf *sendbuf,
1532 struct scatterlist **sg, int *sent, int *count,
1533 struct iscsi_buf *digestbuf, uint32_t *digest)
1534{
1535 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1536 struct iscsi_conn *conn = ctask->conn;
1537 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1538 int rc, buf_sent, offset;
1539
1540 while (*count) {
1541 buf_sent = 0;
1542 offset = sendbuf->sent;
1543
1544 rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent);
1545 *sent = *sent + buf_sent;
1546 if (buf_sent && conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001547 partial_sg_digest_update(&tcp_conn->tx_hash,
Mike Christie62f38302006-08-31 18:09:27 -04001548 &sendbuf->sg, sendbuf->sg.offset + offset,
1549 buf_sent);
1550 if (!iscsi_buf_left(sendbuf) && *sg != tcp_ctask->bad_sg) {
1551 iscsi_buf_init_sg(sendbuf, *sg);
1552 *sg = *sg + 1;
1553 }
1554
1555 if (rc)
1556 return rc;
1557 }
1558
1559 rc = iscsi_send_padding(conn, ctask);
1560 if (rc)
1561 return rc;
1562
1563 return iscsi_send_digest(conn, ctask, digestbuf, digest);
1564}
1565
1566static int
1567iscsi_send_unsol_hdr(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;
Alex Aizman7ba24712005-08-04 19:30:08 -07001570 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001571 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001572
Mike Christie5bb0b552006-04-06 21:26:46 -05001573 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1574 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Mike Christie62f38302006-08-31 18:09:27 -04001575 dtask = &tcp_ctask->unsol_dtask;
1576
Mike Christieffd04362006-08-31 18:09:24 -04001577 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
1578 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
1579 sizeof(struct iscsi_hdr));
Mike Christieaf973482005-09-12 21:01:32 -05001580 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001581 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001582 (u8*)dtask->hdrext);
Mike Christie62f38302006-08-31 18:09:27 -04001583
Mike Christie5bb0b552006-04-06 21:26:46 -05001584 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Mike Christie62f38302006-08-31 18:09:27 -04001585 iscsi_set_padding(tcp_ctask, ctask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001586 }
Mike Christie3219e522006-05-30 00:37:28 -05001587
1588 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1589 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001590 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1591 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001592 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001593 }
1594
Mike Christiedd8c0d92006-08-31 18:09:28 -04001595 if (conn->datadgst_en) {
1596 dtask = &tcp_ctask->unsol_dtask;
1597 iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
1598 dtask->digest = 0;
1599 }
1600
Alex Aizman7ba24712005-08-04 19:30:08 -07001601 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001602 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001603 return 0;
1604}
1605
Mike Christie62f38302006-08-31 18:09:27 -04001606static int
1607iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001608{
Mike Christie5bb0b552006-04-06 21:26:46 -05001609 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001610 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001611
Mike Christie62f38302006-08-31 18:09:27 -04001612 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
1613 BUG_ON(!ctask->unsol_count);
1614 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
1615send_hdr:
1616 rc = iscsi_send_unsol_hdr(conn, ctask);
1617 if (rc)
1618 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001619 }
1620
Mike Christie62f38302006-08-31 18:09:27 -04001621 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
1622 struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask;
Mike Christie5bb0b552006-04-06 21:26:46 -05001623 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001624
Mike Christie62f38302006-08-31 18:09:27 -04001625 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1626 &tcp_ctask->sent, &ctask->data_count,
1627 &dtask->digestbuf, &dtask->digest);
Mike Christie5bb0b552006-04-06 21:26:46 -05001628 ctask->unsol_count -= tcp_ctask->sent - start;
Mike Christie62f38302006-08-31 18:09:27 -04001629 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001630 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001631 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1632 /*
1633 * Done with the Data-Out. Next, check if we need
1634 * to send another unsolicited Data-Out.
1635 */
1636 if (ctask->unsol_count) {
1637 debug_scsi("sending more uns\n");
1638 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
1639 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001640 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001641 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001642 return 0;
1643}
1644
Mike Christie62f38302006-08-31 18:09:27 -04001645static int iscsi_send_sol_pdu(struct iscsi_conn *conn,
1646 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001647{
Mike Christie5bb0b552006-04-06 21:26:46 -05001648 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie62f38302006-08-31 18:09:27 -04001649 struct iscsi_session *session = conn->session;
1650 struct iscsi_r2t_info *r2t;
1651 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001652 int left, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001653
Mike Christie218432c2007-05-30 12:57:17 -05001654 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR_INIT) {
Mike Christiedb37c502006-11-08 15:58:33 -06001655 if (!tcp_ctask->r2t) {
1656 spin_lock_bh(&session->lock);
Mike Christie62f38302006-08-31 18:09:27 -04001657 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1658 sizeof(void*));
Mike Christiedb37c502006-11-08 15:58:33 -06001659 spin_unlock_bh(&session->lock);
1660 }
Mike Christie62f38302006-08-31 18:09:27 -04001661send_hdr:
1662 r2t = tcp_ctask->r2t;
1663 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001664
Mike Christie62f38302006-08-31 18:09:27 -04001665 if (conn->hdrdgst_en)
1666 iscsi_hdr_digest(conn, &r2t->headbuf,
1667 (u8*)dtask->hdrext);
Mike Christie218432c2007-05-30 12:57:17 -05001668 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR_INIT;
1669 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
1670 }
1671
1672 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
1673 r2t = tcp_ctask->r2t;
1674 dtask = &r2t->dtask;
1675
Mike Christie62f38302006-08-31 18:09:27 -04001676 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
Mike Christie218432c2007-05-30 12:57:17 -05001677 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001678 return rc;
Mike Christie218432c2007-05-30 12:57:17 -05001679 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1680 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001681
Mike Christiedd8c0d92006-08-31 18:09:28 -04001682 if (conn->datadgst_en) {
1683 iscsi_data_digest_init(conn->dd_data, tcp_ctask);
1684 dtask->digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001685 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001686
Mike Christie62f38302006-08-31 18:09:27 -04001687 iscsi_set_padding(tcp_ctask, r2t->data_count);
1688 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1689 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1690 r2t->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001691 }
1692
Mike Christie62f38302006-08-31 18:09:27 -04001693 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
1694 r2t = tcp_ctask->r2t;
1695 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001696
Mike Christie62f38302006-08-31 18:09:27 -04001697 rc = iscsi_send_data(ctask, &r2t->sendbuf, &r2t->sg,
1698 &r2t->sent, &r2t->data_count,
1699 &dtask->digestbuf, &dtask->digest);
1700 if (rc)
1701 return rc;
1702 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001703
Mike Christie62f38302006-08-31 18:09:27 -04001704 /*
1705 * Done with this Data-Out. Next, check if we have
1706 * to send another Data-Out for this R2T.
1707 */
1708 BUG_ON(r2t->data_length - r2t->sent < 0);
1709 left = r2t->data_length - r2t->sent;
1710 if (left) {
1711 iscsi_solicit_data_cont(conn, ctask, r2t, left);
Mike Christie62f38302006-08-31 18:09:27 -04001712 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001713 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001714
Mike Christie62f38302006-08-31 18:09:27 -04001715 /*
1716 * Done with this R2T. Check if there are more
1717 * outstanding R2Ts ready to be processed.
1718 */
1719 spin_lock_bh(&session->lock);
1720 tcp_ctask->r2t = NULL;
1721 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
1722 sizeof(void*));
1723 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t,
1724 sizeof(void*))) {
1725 tcp_ctask->r2t = r2t;
Mike Christie62f38302006-08-31 18:09:27 -04001726 spin_unlock_bh(&session->lock);
1727 goto send_hdr;
1728 }
1729 spin_unlock_bh(&session->lock);
1730 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001731 return 0;
1732}
1733
Mike Christie218432c2007-05-30 12:57:17 -05001734/**
1735 * iscsi_tcp_ctask_xmit - xmit normal PDU task
1736 * @conn: iscsi connection
1737 * @ctask: iscsi command task
1738 *
1739 * Notes:
1740 * The function can return -EAGAIN in which case caller must
1741 * call it again later, or recover. '0' return code means successful
1742 * xmit.
1743 * The function is devided to logical helpers (above) for the different
1744 * xmit stages.
1745 *
1746 *iscsi_send_cmd_hdr()
1747 * XMSTATE_CMD_HDR_INIT - prepare Header and Data buffers Calculate
1748 * Header Digest
1749 * XMSTATE_CMD_HDR_XMIT - Transmit header in progress
1750 *
1751 *iscsi_send_padding
1752 * XMSTATE_W_PAD - Prepare and send pading
1753 * XMSTATE_W_RESEND_PAD - retry send pading
1754 *
1755 *iscsi_send_digest
1756 * XMSTATE_W_RESEND_DATA_DIGEST - Finalize and send Data Digest
1757 * XMSTATE_W_RESEND_DATA_DIGEST - retry sending digest
1758 *
1759 *iscsi_send_unsol_hdr
1760 * XMSTATE_UNS_INIT - prepare un-solicit data header and digest
1761 * XMSTATE_UNS_HDR - send un-solicit header
1762 *
1763 *iscsi_send_unsol_pdu
1764 * XMSTATE_UNS_DATA - send un-solicit data in progress
1765 *
1766 *iscsi_send_sol_pdu
1767 * XMSTATE_SOL_HDR_INIT - solicit data header and digest initialize
1768 * XMSTATE_SOL_HDR - send solicit header
1769 * XMSTATE_SOL_DATA - send solicit data
1770 *
1771 *iscsi_tcp_ctask_xmit
1772 * XMSTATE_IMM_DATA - xmit managment data (??)
1773 **/
Alex Aizman7ba24712005-08-04 19:30:08 -07001774static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001775iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001776{
Mike Christie5bb0b552006-04-06 21:26:46 -05001777 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001778 int rc = 0;
1779
1780 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001781 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001782
Mike Christie218432c2007-05-30 12:57:17 -05001783 rc = iscsi_send_cmd_hdr(conn, ctask);
1784 if (rc)
1785 return rc;
1786 if (ctask->sc->sc_data_direction != DMA_TO_DEVICE)
1787 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001788
Mike Christie5bb0b552006-04-06 21:26:46 -05001789 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Mike Christie62f38302006-08-31 18:09:27 -04001790 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1791 &tcp_ctask->sent, &ctask->imm_count,
1792 &tcp_ctask->immbuf, &tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001793 if (rc)
1794 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001795 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001796 }
1797
Mike Christie62f38302006-08-31 18:09:27 -04001798 rc = iscsi_send_unsol_pdu(conn, ctask);
1799 if (rc)
1800 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001801
Mike Christie62f38302006-08-31 18:09:27 -04001802 rc = iscsi_send_sol_pdu(conn, ctask);
1803 if (rc)
1804 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001805
1806 return rc;
1807}
1808
Mike Christie7b8631b2006-01-13 18:05:50 -06001809static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001810iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001811{
Mike Christie7b8631b2006-01-13 18:05:50 -06001812 struct iscsi_conn *conn;
1813 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001814 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001815
Mike Christie5bb0b552006-04-06 21:26:46 -05001816 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001817 if (!cls_conn)
1818 return NULL;
1819 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001820 /*
1821 * due to strange issues with iser these are not set
1822 * in iscsi_conn_setup
1823 */
Mike Christiebf32ed32007-02-28 17:32:17 -06001824 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
Alex Aizman7ba24712005-08-04 19:30:08 -07001825
Mike Christie5bb0b552006-04-06 21:26:46 -05001826 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1827 if (!tcp_conn)
1828 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001829
Mike Christie5bb0b552006-04-06 21:26:46 -05001830 conn->dd_data = tcp_conn;
1831 tcp_conn->iscsi_conn = conn;
1832 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1833 /* initial operational parameters */
1834 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07001835
James Bottomleyc9802cd2006-09-23 15:33:43 -05001836 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1837 CRYPTO_ALG_ASYNC);
1838 tcp_conn->tx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001839 if (IS_ERR(tcp_conn->tx_hash.tfm)) {
1840 printk(KERN_ERR "Could not create connection due to crc32c "
1841 "loading error %ld. Make sure the crc32c module is "
1842 "built as a module or into the kernel\n",
1843 PTR_ERR(tcp_conn->tx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001844 goto free_tcp_conn;
Mike Christie0f238412007-02-28 17:32:21 -06001845 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001846
James Bottomleyc9802cd2006-09-23 15:33:43 -05001847 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1848 CRYPTO_ALG_ASYNC);
1849 tcp_conn->rx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001850 if (IS_ERR(tcp_conn->rx_hash.tfm)) {
1851 printk(KERN_ERR "Could not create connection due to crc32c "
1852 "loading error %ld. Make sure the crc32c module is "
1853 "built as a module or into the kernel\n",
1854 PTR_ERR(tcp_conn->rx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001855 goto free_tx_tfm;
Mike Christie0f238412007-02-28 17:32:21 -06001856 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001857
Mike Christie7b8631b2006-01-13 18:05:50 -06001858 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001859
Mike Christiedd8c0d92006-08-31 18:09:28 -04001860free_tx_tfm:
James Bottomleyc9802cd2006-09-23 15:33:43 -05001861 crypto_free_hash(tcp_conn->tx_hash.tfm);
Mike Christiedd8c0d92006-08-31 18:09:28 -04001862free_tcp_conn:
1863 kfree(tcp_conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001864tcp_conn_alloc_fail:
1865 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001866 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001867}
1868
1869static void
Mike Christie1c834692006-07-24 15:47:26 -05001870iscsi_tcp_release_conn(struct iscsi_conn *conn)
1871{
1872 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1873
1874 if (!tcp_conn->sock)
1875 return;
1876
1877 sock_hold(tcp_conn->sock->sk);
1878 iscsi_conn_restore_callbacks(tcp_conn);
1879 sock_put(tcp_conn->sock->sk);
1880
1881 sock_release(tcp_conn->sock);
1882 tcp_conn->sock = NULL;
1883 conn->recv_lock = NULL;
1884}
1885
1886static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001887iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001888{
Mike Christie7b8631b2006-01-13 18:05:50 -06001889 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001890 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001891
Mike Christie1c834692006-07-24 15:47:26 -05001892 iscsi_tcp_release_conn(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001893 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001894
Pete Wyckoff534284a2006-11-08 15:58:31 -06001895 if (tcp_conn->tx_hash.tfm)
1896 crypto_free_hash(tcp_conn->tx_hash.tfm);
1897 if (tcp_conn->rx_hash.tfm)
1898 crypto_free_hash(tcp_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001899
Mike Christie5bb0b552006-04-06 21:26:46 -05001900 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001901}
1902
Mike Christie1c834692006-07-24 15:47:26 -05001903static void
1904iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1905{
1906 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christied5390f52006-08-31 18:09:30 -04001907 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie1c834692006-07-24 15:47:26 -05001908
1909 iscsi_conn_stop(cls_conn, flag);
1910 iscsi_tcp_release_conn(conn);
Mike Christied5390f52006-08-31 18:09:30 -04001911 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christie1c834692006-07-24 15:47:26 -05001912}
1913
Alex Aizman7ba24712005-08-04 19:30:08 -07001914static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001915iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001916 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001917 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001918{
Mike Christie5bb0b552006-04-06 21:26:46 -05001919 struct iscsi_conn *conn = cls_conn->dd_data;
1920 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001921 struct sock *sk;
1922 struct socket *sock;
1923 int err;
1924
1925 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001926 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001927 if (!sock) {
1928 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1929 return -EEXIST;
1930 }
1931
Mike Christie5bb0b552006-04-06 21:26:46 -05001932 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1933 if (err)
1934 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001935
Mike Christie67a61112006-05-30 00:37:20 -05001936 /* bind iSCSI connection and socket */
1937 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001938
Mike Christie67a61112006-05-30 00:37:20 -05001939 /* setup Socket parameters */
1940 sk = sock->sk;
1941 sk->sk_reuse = 1;
1942 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1943 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001944
Mike Christie67a61112006-05-30 00:37:20 -05001945 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07001946
Mike Christie67a61112006-05-30 00:37:20 -05001947 /*
1948 * Intercept TCP callbacks for sendfile like receive
1949 * processing.
1950 */
1951 conn->recv_lock = &sk->sk_callback_lock;
1952 iscsi_conn_set_callbacks(conn);
1953 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1954 /*
1955 * set receive state machine into initial state
1956 */
1957 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07001958
Alex Aizman7ba24712005-08-04 19:30:08 -07001959 return 0;
1960}
1961
Mike Christie5bb0b552006-04-06 21:26:46 -05001962/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05001963static void
Mike Christie77a23c22007-05-30 12:57:18 -05001964iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Mike Christie30a6c652006-04-06 21:13:39 -05001965{
Mike Christie5bb0b552006-04-06 21:26:46 -05001966 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie218432c2007-05-30 12:57:17 -05001967 tcp_mtask->xmstate = XMSTATE_IMM_HDR_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001968}
1969
1970static int
1971iscsi_r2tpool_alloc(struct iscsi_session *session)
1972{
1973 int i;
1974 int cmd_i;
1975
1976 /*
1977 * initialize per-task: R2T pool and xmit queue
1978 */
1979 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1980 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05001981 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001982
1983 /*
1984 * pre-allocated x4 as much r2ts to handle race when
1985 * target acks DataOut faster than we data_xmit() queues
1986 * could replenish r2tqueue.
1987 */
1988
1989 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05001990 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
1991 (void***)&tcp_ctask->r2ts,
1992 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001993 goto r2t_alloc_fail;
1994 }
1995
1996 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05001997 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07001998 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05001999 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2000 iscsi_pool_free(&tcp_ctask->r2tpool,
2001 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002002 goto r2t_alloc_fail;
2003 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002004 }
2005
2006 return 0;
2007
2008r2t_alloc_fail:
2009 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002010 struct iscsi_cmd_task *ctask = session->cmds[i];
2011 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2012
Mike Christie5bb0b552006-04-06 21:26:46 -05002013 kfifo_free(tcp_ctask->r2tqueue);
2014 iscsi_pool_free(&tcp_ctask->r2tpool,
2015 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002016 }
2017 return -ENOMEM;
2018}
2019
2020static void
2021iscsi_r2tpool_free(struct iscsi_session *session)
2022{
2023 int i;
2024
2025 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002026 struct iscsi_cmd_task *ctask = session->cmds[i];
2027 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2028
Mike Christie5bb0b552006-04-06 21:26:46 -05002029 kfifo_free(tcp_ctask->r2tqueue);
2030 iscsi_pool_free(&tcp_ctask->r2tpool,
2031 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002032 }
2033}
2034
Alex Aizman7ba24712005-08-04 19:30:08 -07002035static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002036iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002037 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07002038{
Mike Christie7b7232f2006-02-01 21:06:49 -06002039 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002040 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05002041 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002042 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07002043
Alex Aizman7ba24712005-08-04 19:30:08 -07002044 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002045 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002046 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002047 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christiedd8c0d92006-08-31 18:09:28 -04002048 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05002049 tcp_conn->hdr_size += sizeof(__u32);
Alex Aizman7ba24712005-08-04 19:30:08 -07002050 break;
2051 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002052 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002053 tcp_conn->sendpage = conn->datadgst_en ?
2054 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07002055 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002056 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002057 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07002058 if (session->max_r2t == roundup_pow_of_two(value))
2059 break;
2060 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002061 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002062 if (session->max_r2t & (session->max_r2t - 1))
2063 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2064 if (iscsi_r2tpool_alloc(session))
2065 return -ENOMEM;
2066 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002067 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002068 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002069 }
2070
2071 return 0;
2072}
2073
2074static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05002075iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2076 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06002077{
Mike Christie7b7232f2006-02-01 21:06:49 -06002078 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002079 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christiefd7255f2006-04-06 21:13:36 -05002080 struct inet_sock *inet;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002081 struct ipv6_pinfo *np;
2082 struct sock *sk;
2083 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06002084
2085 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002086 case ISCSI_PARAM_CONN_PORT:
Mike Christie77a23c22007-05-30 12:57:18 -05002087 if (!tcp_conn->sock)
Mike Christiefd7255f2006-04-06 21:13:36 -05002088 return -EINVAL;
Mike Christiefd7255f2006-04-06 21:13:36 -05002089
Mike Christie5bb0b552006-04-06 21:26:46 -05002090 inet = inet_sk(tcp_conn->sock->sk);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002091 len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
Mike Christie8d2860b2006-05-02 19:46:47 -05002092 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002093 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie77a23c22007-05-30 12:57:18 -05002094 if (!tcp_conn->sock)
Mike Christiefd7255f2006-04-06 21:13:36 -05002095 return -EINVAL;
Mike Christiefd7255f2006-04-06 21:13:36 -05002096
Mike Christie5bb0b552006-04-06 21:26:46 -05002097 sk = tcp_conn->sock->sk;
Mike Christiefd7255f2006-04-06 21:13:36 -05002098 if (sk->sk_family == PF_INET) {
2099 inet = inet_sk(sk);
FUJITA Tomonori94cb3f82006-12-17 12:10:27 -06002100 len = sprintf(buf, NIPQUAD_FMT "\n",
Mike Christiefd7255f2006-04-06 21:13:36 -05002101 NIPQUAD(inet->daddr));
2102 } else {
2103 np = inet6_sk(sk);
FUJITA Tomonori94cb3f82006-12-17 12:10:27 -06002104 len = sprintf(buf, NIP6_FMT "\n", NIP6(np->daddr));
Mike Christiefd7255f2006-04-06 21:13:36 -05002105 }
Mike Christiefd7255f2006-04-06 21:13:36 -05002106 break;
2107 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002108 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05002109 }
2110
2111 return len;
2112}
2113
Alex Aizman7ba24712005-08-04 19:30:08 -07002114static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002115iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002116{
Mike Christie7b7232f2006-02-01 21:06:49 -06002117 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002118 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002119
2120 stats->txdata_octets = conn->txdata_octets;
2121 stats->rxdata_octets = conn->rxdata_octets;
2122 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2123 stats->dataout_pdus = conn->dataout_pdus_cnt;
2124 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2125 stats->datain_pdus = conn->datain_pdus_cnt;
2126 stats->r2t_pdus = conn->r2t_pdus_cnt;
2127 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2128 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2129 stats->custom_length = 3;
2130 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002131 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002132 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002133 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002134 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2135 stats->custom[2].value = conn->eh_abort_cnt;
2136}
2137
Mike Christie5bb0b552006-04-06 21:26:46 -05002138static struct iscsi_cls_session *
2139iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2140 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05002141 uint16_t cmds_max, uint16_t qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05002142 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002143{
Mike Christie5bb0b552006-04-06 21:26:46 -05002144 struct iscsi_cls_session *cls_session;
2145 struct iscsi_session *session;
2146 uint32_t hn;
2147 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002148
Mike Christie15482712007-05-30 12:57:19 -05002149 cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05002150 sizeof(struct iscsi_tcp_cmd_task),
2151 sizeof(struct iscsi_tcp_mgmt_task),
2152 initial_cmdsn, &hn);
2153 if (!cls_session)
2154 return NULL;
2155 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002156
Mike Christie5bb0b552006-04-06 21:26:46 -05002157 session = class_to_transport_session(cls_session);
2158 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2159 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2160 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2161
2162 ctask->hdr = &tcp_ctask->hdr;
2163 }
2164
2165 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2166 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2167 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2168
2169 mtask->hdr = &tcp_mtask->hdr;
2170 }
2171
2172 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2173 goto r2tpool_alloc_fail;
2174
2175 return cls_session;
2176
2177r2tpool_alloc_fail:
2178 iscsi_session_teardown(cls_session);
2179 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002180}
2181
Mike Christie5bb0b552006-04-06 21:26:46 -05002182static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2183{
Mike Christie5bb0b552006-04-06 21:26:46 -05002184 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2185 iscsi_session_teardown(cls_session);
2186}
2187
Mike Christied1d81c02007-05-30 12:57:21 -05002188static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
2189{
2190 blk_queue_dma_alignment(sdev->request_queue, 0);
2191 return 0;
2192}
2193
Mike Christie5bb0b552006-04-06 21:26:46 -05002194static struct scsi_host_template iscsi_sht = {
Mike Christief4246b32006-07-24 15:47:54 -05002195 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05002196 .queuecommand = iscsi_queuecommand,
2197 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -05002198 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie5bb0b552006-04-06 21:26:46 -05002199 .sg_tablesize = ISCSI_SG_TABLESIZE,
Mike Christie8231f0e2007-02-28 17:32:20 -06002200 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -05002201 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2202 .eh_abort_handler = iscsi_eh_abort,
2203 .eh_host_reset_handler = iscsi_eh_host_reset,
2204 .use_clustering = DISABLE_CLUSTERING,
Mike Christied1d81c02007-05-30 12:57:21 -05002205 .slave_configure = iscsi_tcp_slave_configure,
Mike Christie5bb0b552006-04-06 21:26:46 -05002206 .proc_name = "iscsi_tcp",
2207 .this_id = -1,
2208};
2209
Alex Aizman7ba24712005-08-04 19:30:08 -07002210static struct iscsi_transport iscsi_tcp_transport = {
2211 .owner = THIS_MODULE,
2212 .name = "tcp",
2213 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2214 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002215 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2216 ISCSI_MAX_XMIT_DLENGTH |
2217 ISCSI_HDRDGST_EN |
2218 ISCSI_DATADGST_EN |
2219 ISCSI_INITIAL_R2T_EN |
2220 ISCSI_MAX_R2T |
2221 ISCSI_IMM_DATA_EN |
2222 ISCSI_FIRST_BURST |
2223 ISCSI_MAX_BURST |
2224 ISCSI_PDU_INORDER_EN |
2225 ISCSI_DATASEQ_INORDER_EN |
2226 ISCSI_ERL |
2227 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002228 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05002229 ISCSI_EXP_STATSN |
2230 ISCSI_PERSISTENT_PORT |
2231 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -05002232 ISCSI_TARGET_NAME | ISCSI_TPGT |
2233 ISCSI_USERNAME | ISCSI_PASSWORD |
2234 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN,
Mike Christie8ad57812007-05-30 12:57:13 -05002235 .host_param_mask = ISCSI_HOST_HWADDRESS |
2236 ISCSI_HOST_INITIATOR_NAME,
Alex Aizman7ba24712005-08-04 19:30:08 -07002237 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002238 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002239 .max_conn = 1,
2240 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002241 /* session management */
2242 .create_session = iscsi_tcp_session_create,
2243 .destroy_session = iscsi_tcp_session_destroy,
2244 /* connection management */
2245 .create_conn = iscsi_tcp_conn_create,
2246 .bind_conn = iscsi_tcp_conn_bind,
2247 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002248 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002249 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002250 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002251 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05002252 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -05002253 /* iscsi host params */
2254 .get_host_param = iscsi_host_get_param,
2255 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -05002256 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002257 .send_pdu = iscsi_conn_send_pdu,
2258 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002259 .init_cmd_task = iscsi_tcp_cmd_init,
2260 .init_mgmt_task = iscsi_tcp_mgmt_init,
2261 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2262 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2263 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2264 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002265 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002266};
2267
2268static int __init
2269iscsi_tcp_init(void)
2270{
Alex Aizman7ba24712005-08-04 19:30:08 -07002271 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002272 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2273 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002274 return -EINVAL;
2275 }
2276 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2277
Mike Christie7b8631b2006-01-13 18:05:50 -06002278 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002279 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002280
Mike Christie7b8631b2006-01-13 18:05:50 -06002281 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002282}
2283
2284static void __exit
2285iscsi_tcp_exit(void)
2286{
2287 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002288}
2289
2290module_init(iscsi_tcp_init);
2291module_exit(iscsi_tcp_exit);