blob: 17fc79c408a2c1b37ffd8acfe3c8d966e596223d [file] [log] [blame]
Alex Aizman7ba24712005-08-04 19:30:08 -07001/*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
Mike Christie5bb0b552006-04-06 21:26:46 -05006 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
Alex Aizman7ba24712005-08-04 19:30:08 -07008 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * See the file COPYING included with this distribution for more details.
21 *
22 * Credits:
23 * Christoph Hellwig
24 * FUJITA Tomonori
25 * Arne Redlich
26 * Zhenyu Wang
27 */
28
29#include <linux/types.h>
30#include <linux/list.h>
31#include <linux/inet.h>
32#include <linux/blkdev.h>
33#include <linux/crypto.h>
34#include <linux/delay.h>
35#include <linux/kfifo.h>
36#include <linux/scatterlist.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010037#include <linux/mutex.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070038#include <net/tcp.h>
39#include <scsi/scsi_cmnd.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070040#include <scsi/scsi_host.h>
41#include <scsi/scsi.h>
42#include <scsi/scsi_transport_iscsi.h>
43
44#include "iscsi_tcp.h"
45
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 Christie753e7d32006-08-31 18:09:29 -0400112 buf->sg.length = tcp_conn->hdr_size;
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{
214 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500215 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
216 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
217 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700218 struct iscsi_session *session = conn->session;
219 int datasn = be32_to_cpu(rhdr->datasn);
220
221 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
222 if (rc)
223 return rc;
224 /*
225 * setup Data-In byte counter (gets decremented..)
226 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500227 ctask->data_count = tcp_conn->in.datalen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700228
Mike Christie5bb0b552006-04-06 21:26:46 -0500229 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700230 return 0;
231
Mike Christied473cc72007-05-30 12:57:14 -0500232 if (tcp_ctask->exp_datasn != datasn) {
233 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
234 __FUNCTION__, tcp_ctask->exp_datasn, datasn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700235 return ISCSI_ERR_DATASN;
Mike Christied473cc72007-05-30 12:57:14 -0500236 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700237
Mike Christied473cc72007-05-30 12:57:14 -0500238 tcp_ctask->exp_datasn++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700239
Mike Christie5bb0b552006-04-06 21:26:46 -0500240 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
241 if (tcp_ctask->data_offset + tcp_conn->in.datalen > ctask->total_length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700242 return ISCSI_ERR_DATA_OFFSET;
243
244 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
245 struct scsi_cmnd *sc = ctask->sc;
246
247 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600248 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700249 int res_count = be32_to_cpu(rhdr->residual_count);
250
251 if (res_count > 0 &&
252 res_count <= sc->request_bufflen) {
253 sc->resid = res_count;
254 sc->result = (DID_OK << 16) | rhdr->cmd_status;
255 } else
256 sc->result = (DID_BAD_TARGET << 16) |
257 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600258 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700259 sc->resid = be32_to_cpu(rhdr->residual_count);
260 sc->result = (DID_OK << 16) | rhdr->cmd_status;
261 } else
262 sc->result = (DID_OK << 16) | rhdr->cmd_status;
263 }
264
265 conn->datain_pdus_cnt++;
266 return 0;
267}
268
269/**
270 * iscsi_solicit_data_init - initialize first Data-Out
271 * @conn: iscsi connection
272 * @ctask: scsi command task
273 * @r2t: R2T info
274 *
275 * Notes:
276 * Initialize first Data-Out within this R2T sequence and finds
277 * proper data_offset within this SCSI command.
278 *
279 * This function is called with connection lock taken.
280 **/
281static void
282iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
283 struct iscsi_r2t_info *r2t)
284{
285 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700286 struct scsi_cmnd *sc = ctask->sc;
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
377 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
378 if (rc)
379 return rc;
380
Alex Aizman7ba24712005-08-04 19:30:08 -0700381 /* fill-in new R2T associated with the task */
382 spin_lock(&session->lock);
383 if (!ctask->sc || ctask->mtask ||
384 session->state != ISCSI_STATE_LOGGED_IN) {
385 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
386 "recovery...\n", ctask->itt);
387 spin_unlock(&session->lock);
388 return 0;
389 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500390
Mike Christie5bb0b552006-04-06 21:26:46 -0500391 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700392 BUG_ON(!rc);
393
394 r2t->exp_statsn = rhdr->statsn;
395 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400396 if (r2t->data_length == 0) {
397 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
Alex Aizman7ba24712005-08-04 19:30:08 -0700398 spin_unlock(&session->lock);
399 return ISCSI_ERR_DATALEN;
400 }
401
Mike Christie98a94162006-08-31 18:09:26 -0400402 if (r2t->data_length > session->max_burst)
403 debug_scsi("invalid R2T with data len %u and max burst %u."
404 "Attempting to execute request.\n",
405 r2t->data_length, session->max_burst);
406
Alex Aizman7ba24712005-08-04 19:30:08 -0700407 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
408 if (r2t->data_offset + r2t->data_length > ctask->total_length) {
409 spin_unlock(&session->lock);
Mike Christie98a94162006-08-31 18:09:26 -0400410 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
411 "offset %u and total length %d\n", r2t->data_length,
412 r2t->data_offset, ctask->total_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700413 return ISCSI_ERR_DATALEN;
414 }
415
416 r2t->ttt = rhdr->ttt; /* no flip */
417 r2t->solicit_datasn = 0;
418
419 iscsi_solicit_data_init(conn, ctask, r2t);
420
Mike Christied473cc72007-05-30 12:57:14 -0500421 tcp_ctask->exp_datasn = r2tsn + 1;
Mike Christie5bb0b552006-04-06 21:26:46 -0500422 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christiedb37c502006-11-08 15:58:33 -0600423 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
Mike Christieb6c395e2006-07-24 15:47:15 -0500424 list_move_tail(&ctask->running, &conn->xmitqueue);
Alex Aizman7ba24712005-08-04 19:30:08 -0700425
Mike Christie55e32992006-01-13 18:05:53 -0600426 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -0700427 conn->r2t_pdus_cnt++;
428 spin_unlock(&session->lock);
429
430 return 0;
431}
432
433static int
Mike Christie5bb0b552006-04-06 21:26:46 -0500434iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700435{
Mike Christie5bb0b552006-04-06 21:26:46 -0500436 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700437 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700438 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500439 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
440 uint32_t cdgst, rdgst = 0, itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700441
Mike Christie5bb0b552006-04-06 21:26:46 -0500442 hdr = tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700443
444 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500445 tcp_conn->in.datalen = ntoh24(hdr->dlength);
446 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700447 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500448 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700449 return ISCSI_ERR_DATALEN;
450 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500451 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700452
453 /* read AHS */
Mike Christie5bb0b552006-04-06 21:26:46 -0500454 ahslen = hdr->hlength << 2;
455 tcp_conn->in.offset += ahslen;
456 tcp_conn->in.copy -= ahslen;
457 if (tcp_conn->in.copy < 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700458 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
Mike Christie5bb0b552006-04-06 21:26:46 -0500459 "%d bytes\n", ahslen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700460 return ISCSI_ERR_AHSLEN;
461 }
462
463 /* calculate read padding */
Mike Christie5bb0b552006-04-06 21:26:46 -0500464 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
465 if (tcp_conn->in.padding) {
466 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
467 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
Alex Aizman7ba24712005-08-04 19:30:08 -0700468 }
469
470 if (conn->hdrdgst_en) {
471 struct scatterlist sg;
472
473 sg_init_one(&sg, (u8 *)hdr,
Mike Christie5bb0b552006-04-06 21:26:46 -0500474 sizeof(struct iscsi_hdr) + ahslen);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500475 crypto_hash_digest(&tcp_conn->rx_hash, &sg, sg.length,
476 (u8 *)&cdgst);
Alex Aizman7ba24712005-08-04 19:30:08 -0700477 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
Mike Christie5bb0b552006-04-06 21:26:46 -0500478 ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600479 if (cdgst != rdgst) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500480 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
481 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
Mike Christie8a47cd32005-11-30 02:27:19 -0600482 return ISCSI_ERR_HDR_DGST;
483 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700484 }
485
Mike Christie5bb0b552006-04-06 21:26:46 -0500486 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700487 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500488 rc = iscsi_verify_itt(conn, hdr, &itt);
489 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
490 tcp_conn->in.datalen = 0; /* force drop */
491 return 0;
492 } else if (rc)
493 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700494
495 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500496 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
497 ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700498
Mike Christie5bb0b552006-04-06 21:26:46 -0500499 switch(opcode) {
500 case ISCSI_OP_SCSI_DATA_IN:
501 tcp_conn->in.ctask = session->cmds[itt];
502 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
Mike Christie275fd7d2006-07-24 15:47:17 -0500503 if (rc)
504 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500505 /* fall through */
506 case ISCSI_OP_SCSI_CMD_RSP:
507 tcp_conn->in.ctask = session->cmds[itt];
508 if (tcp_conn->in.datalen)
509 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700510
Mike Christie5bb0b552006-04-06 21:26:46 -0500511 spin_lock(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500512 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
513 spin_unlock(&session->lock);
514 break;
515 case ISCSI_OP_R2T:
516 tcp_conn->in.ctask = session->cmds[itt];
517 if (ahslen)
518 rc = ISCSI_ERR_AHSLEN;
519 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
520 DMA_TO_DEVICE)
521 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
522 else
523 rc = ISCSI_ERR_PROTO;
524 break;
525 case ISCSI_OP_LOGIN_RSP:
526 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500527 case ISCSI_OP_REJECT:
528 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500529 /*
530 * It is possible that we could get a PDU with a buffer larger
531 * than 8K, but there are no targets that currently do this.
532 * For now we fail until we find a vendor that needs it
533 */
Mike Christiebf32ed32007-02-28 17:32:17 -0600534 if (ISCSI_DEF_MAX_RECV_SEG_LEN <
Mike Christiec8dc1e52006-07-24 15:47:39 -0500535 tcp_conn->in.datalen) {
536 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
537 "but conn buffer is only %u (opcode %0x)\n",
538 tcp_conn->in.datalen,
Mike Christiebf32ed32007-02-28 17:32:17 -0600539 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
Mike Christiec8dc1e52006-07-24 15:47:39 -0500540 rc = ISCSI_ERR_PROTO;
541 break;
542 }
543
Mike Christie5bb0b552006-04-06 21:26:46 -0500544 if (tcp_conn->in.datalen)
545 goto copy_hdr;
546 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500547 case ISCSI_OP_LOGOUT_RSP:
548 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500549 case ISCSI_OP_SCSI_TMFUNC_RSP:
550 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
551 break;
552 default:
553 rc = ISCSI_ERR_BAD_OPCODE;
554 break;
555 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700556
557 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500558
559copy_hdr:
560 /*
561 * if we did zero copy for the header but we will need multiple
562 * skbs to complete the command then we have to copy the header
563 * for later use
564 */
Mike Christie275fd7d2006-07-24 15:47:17 -0500565 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
Mike Christie5bb0b552006-04-06 21:26:46 -0500566 (tcp_conn->in.datalen + tcp_conn->in.padding +
567 (conn->datadgst_en ? 4 : 0))) {
568 debug_tcp("Copying header for later use. in.copy %d in.datalen"
569 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
570 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
571 sizeof(struct iscsi_hdr));
572 tcp_conn->in.hdr = &tcp_conn->hdr;
573 tcp_conn->in.zero_copy_hdr = 0;
574 }
575 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700576}
577
578/**
579 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
Mike Christie5bb0b552006-04-06 21:26:46 -0500580 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700581 * @ctask: scsi command task
582 * @buf: buffer to copy to
583 * @buf_size: size of buffer
584 * @offset: offset within the buffer
585 *
586 * Notes:
587 * The function calls skb_copy_bits() and updates per-connection and
588 * per-cmd byte counters.
589 *
590 * Read counters (in bytes):
591 *
592 * conn->in.offset offset within in progress SKB
593 * conn->in.copy left to copy from in progress SKB
594 * including padding
595 * conn->in.copied copied already from in progress SKB
596 * conn->data_copied copied already from in progress buffer
597 * ctask->sent total bytes sent up to the MidLayer
598 * ctask->data_count left to copy from in progress Data-In
599 * buf_left left to copy from in progress buffer
600 **/
601static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500602iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -0700603 void *buf, int buf_size, int offset)
604{
Mike Christie5bb0b552006-04-06 21:26:46 -0500605 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
606 int buf_left = buf_size - (tcp_conn->data_copied + offset);
607 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700608 int rc;
609
610 size = min(size, ctask->data_count);
611
612 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500613 size, tcp_conn->in.offset, tcp_conn->in.copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700614
615 BUG_ON(size <= 0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500616 BUG_ON(tcp_ctask->sent + size > ctask->total_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700617
Mike Christie5bb0b552006-04-06 21:26:46 -0500618 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
619 (char*)buf + (offset + tcp_conn->data_copied), size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700620 /* must fit into skb->len */
621 BUG_ON(rc);
622
Mike Christie5bb0b552006-04-06 21:26:46 -0500623 tcp_conn->in.offset += size;
624 tcp_conn->in.copy -= size;
625 tcp_conn->in.copied += size;
626 tcp_conn->data_copied += size;
627 tcp_ctask->sent += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700628 ctask->data_count -= size;
629
Mike Christie5bb0b552006-04-06 21:26:46 -0500630 BUG_ON(tcp_conn->in.copy < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700631 BUG_ON(ctask->data_count < 0);
632
Mike Christie5bb0b552006-04-06 21:26:46 -0500633 if (buf_size != (tcp_conn->data_copied + offset)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700634 if (!ctask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500635 BUG_ON(buf_size - tcp_conn->data_copied < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700636 /* done with this PDU */
Mike Christie5bb0b552006-04-06 21:26:46 -0500637 return buf_size - tcp_conn->data_copied;
Alex Aizman7ba24712005-08-04 19:30:08 -0700638 }
639 return -EAGAIN;
640 }
641
642 /* done with this buffer or with both - PDU and buffer */
Mike Christie5bb0b552006-04-06 21:26:46 -0500643 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700644 return 0;
645}
646
647/**
648 * iscsi_tcp_copy - copy skb bits to the destanation buffer
Mike Christie5bb0b552006-04-06 21:26:46 -0500649 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700650 *
651 * Notes:
652 * The function calls skb_copy_bits() and updates per-connection
653 * byte counters.
654 **/
655static inline int
Mike Christieca518682006-08-31 18:09:32 -0400656iscsi_tcp_copy(struct iscsi_conn *conn, int buf_size)
Alex Aizman7ba24712005-08-04 19:30:08 -0700657{
Mike Christiec8dc1e52006-07-24 15:47:39 -0500658 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500659 int buf_left = buf_size - tcp_conn->data_copied;
660 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700661 int rc;
662
663 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500664 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700665 BUG_ON(size <= 0);
666
Mike Christie5bb0b552006-04-06 21:26:46 -0500667 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christiec8dc1e52006-07-24 15:47:39 -0500668 (char*)conn->data + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700669 BUG_ON(rc);
670
Mike Christie5bb0b552006-04-06 21:26:46 -0500671 tcp_conn->in.offset += size;
672 tcp_conn->in.copy -= size;
673 tcp_conn->in.copied += size;
674 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700675
Mike Christie5bb0b552006-04-06 21:26:46 -0500676 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700677 return -EAGAIN;
678
679 return 0;
680}
681
682static inline void
James Bottomleyc9802cd2006-09-23 15:33:43 -0500683partial_sg_digest_update(struct hash_desc *desc, struct scatterlist *sg,
Mike Christie62f38302006-08-31 18:09:27 -0400684 int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700685{
686 struct scatterlist temp;
687
688 memcpy(&temp, sg, sizeof(struct scatterlist));
689 temp.offset = offset;
690 temp.length = length;
James Bottomleyc9802cd2006-09-23 15:33:43 -0500691 crypto_hash_update(desc, &temp, length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700692}
693
Mike Christief6cfba12005-11-29 23:12:57 -0600694static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500695iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600696{
697 struct scatterlist tmp;
698
699 sg_init_one(&tmp, buf, len);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500700 crypto_hash_update(&tcp_conn->rx_hash, &tmp, len);
Mike Christief6cfba12005-11-29 23:12:57 -0600701}
702
Alex Aizman7ba24712005-08-04 19:30:08 -0700703static int iscsi_scsi_data_in(struct iscsi_conn *conn)
704{
Mike Christie5bb0b552006-04-06 21:26:46 -0500705 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
706 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
707 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700708 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600709 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700710 int i, offset, rc = 0;
711
712 BUG_ON((void*)ctask != sc->SCp.ptr);
713
714 /*
715 * copying Data-In into the Scsi_Cmnd
716 */
717 if (!sc->use_sg) {
718 i = ctask->data_count;
Mike Christie5bb0b552006-04-06 21:26:46 -0500719 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
720 sc->request_bufflen,
721 tcp_ctask->data_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700722 if (rc == -EAGAIN)
723 return rc;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600724 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500725 iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
726 i);
Alex Aizman7ba24712005-08-04 19:30:08 -0700727 rc = 0;
728 goto done;
729 }
730
Mike Christie5bb0b552006-04-06 21:26:46 -0500731 offset = tcp_ctask->data_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700732 sg = sc->request_buffer;
733
Mike Christie5bb0b552006-04-06 21:26:46 -0500734 if (tcp_ctask->data_offset)
735 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700736 offset -= sg[i].length;
737 /* we've passed through partial sg*/
738 if (offset < 0)
739 offset = 0;
740
Mike Christie5bb0b552006-04-06 21:26:46 -0500741 for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700742 char *dest;
743
744 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500745 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700746 sg[i].length, offset);
747 kunmap_atomic(dest, KM_SOFTIRQ0);
748 if (rc == -EAGAIN)
749 /* continue with the next SKB/PDU */
750 return rc;
751 if (!rc) {
752 if (conn->datadgst_en) {
753 if (!offset)
Herbert Xudc64ddf2006-08-24 18:45:50 +1000754 crypto_hash_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500755 &tcp_conn->rx_hash,
Arne Redlichc959e1c2006-12-17 12:10:24 -0600756 &sg[i], sg[i].length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700757 else
Mike Christie62f38302006-08-31 18:09:27 -0400758 partial_sg_digest_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500759 &tcp_conn->rx_hash,
Mike Christie5bb0b552006-04-06 21:26:46 -0500760 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700761 sg[i].offset + offset,
762 sg[i].length - offset);
763 }
764 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500765 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700766 }
767
768 if (!ctask->data_count) {
769 if (rc && conn->datadgst_en)
770 /*
771 * data-in is complete, but buffer not...
772 */
James Bottomleyc9802cd2006-09-23 15:33:43 -0500773 partial_sg_digest_update(&tcp_conn->rx_hash,
774 &sg[i],
775 sg[i].offset,
776 sg[i].length-rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700777 rc = 0;
778 break;
779 }
780
Mike Christie5bb0b552006-04-06 21:26:46 -0500781 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700782 return -EAGAIN;
783 }
784 BUG_ON(ctask->data_count);
785
786done:
787 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500788 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Mike Christieb6c395e2006-07-24 15:47:15 -0500789 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
790 (long)sc, sc->result, ctask->itt,
791 tcp_conn->in.hdr->flags);
Mike Christie5bb0b552006-04-06 21:26:46 -0500792 spin_lock(&conn->session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500793 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
794 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700795 }
796
797 return rc;
798}
799
800static int
801iscsi_data_recv(struct iscsi_conn *conn)
802{
Mike Christie5bb0b552006-04-06 21:26:46 -0500803 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
804 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700805
Mike Christie5bb0b552006-04-06 21:26:46 -0500806 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
807 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700808 case ISCSI_OP_SCSI_DATA_IN:
809 rc = iscsi_scsi_data_in(conn);
810 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500811 case ISCSI_OP_SCSI_CMD_RSP:
Alex Aizman7ba24712005-08-04 19:30:08 -0700812 case ISCSI_OP_TEXT_RSP:
813 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500814 case ISCSI_OP_ASYNC_EVENT:
815 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700816 /*
817 * Collect data segment to the connection's data
818 * placeholder
819 */
Mike Christieca518682006-08-31 18:09:32 -0400820 if (iscsi_tcp_copy(conn, tcp_conn->in.datalen)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700821 rc = -EAGAIN;
822 goto exit;
823 }
824
Mike Christiec8dc1e52006-07-24 15:47:39 -0500825 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500826 tcp_conn->in.datalen);
827 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
Mike Christiec8dc1e52006-07-24 15:47:39 -0500828 iscsi_recv_digest_update(tcp_conn, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500829 tcp_conn->in.datalen);
830 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700831 default:
832 BUG_ON(1);
833 }
834exit:
835 return rc;
836}
837
838/**
839 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
840 * @rd_desc: read descriptor
841 * @skb: socket buffer
842 * @offset: offset in skb
843 * @len: skb->len - offset
844 **/
845static int
846iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
847 unsigned int offset, size_t len)
848{
849 int rc;
850 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500851 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700852 int processed;
853 char pad[ISCSI_PAD_LEN];
854 struct scatterlist sg;
855
856 /*
857 * Save current SKB and its offset in the corresponding
858 * connection context.
859 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500860 tcp_conn->in.copy = skb->len - offset;
861 tcp_conn->in.offset = offset;
862 tcp_conn->in.skb = skb;
863 tcp_conn->in.len = tcp_conn->in.copy;
864 BUG_ON(tcp_conn->in.copy <= 0);
865 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700866
867more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500868 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700869 rc = 0;
870
871 if (unlikely(conn->suspend_rx)) {
872 debug_tcp("conn %d Rx suspended!\n", conn->id);
873 return 0;
874 }
875
Mike Christie5bb0b552006-04-06 21:26:46 -0500876 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
877 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
878 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700879 if (rc) {
880 if (rc == -EAGAIN)
881 goto nomore;
882 else {
Mike Christied82967c2006-07-24 15:47:11 -0500883 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700884 return 0;
885 }
886 }
887
888 /*
889 * Verify and process incoming PDU header.
890 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500891 rc = iscsi_tcp_hdr_recv(conn);
892 if (!rc && tcp_conn->in.datalen) {
Mike Christiedd8c0d92006-08-31 18:09:28 -0400893 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -0500894 crypto_hash_init(&tcp_conn->rx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -0500895 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700896 } else if (rc) {
Mike Christie40527af2006-07-24 15:47:45 -0500897 iscsi_conn_failure(conn, rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700898 return 0;
899 }
900 }
901
Mike Christie5bb0b552006-04-06 21:26:46 -0500902 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
Mike Christief6cfba12005-11-29 23:12:57 -0600903 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500904
Alex Aizman7ba24712005-08-04 19:30:08 -0700905 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500906 tcp_conn->in.offset, tcp_conn->in.copy);
Mike Christieca518682006-08-31 18:09:32 -0400907 rc = iscsi_tcp_copy(conn, sizeof(uint32_t));
908 if (rc) {
909 if (rc == -EAGAIN)
910 goto again;
911 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
912 return 0;
913 }
914
915 memcpy(&recv_digest, conn->data, sizeof(uint32_t));
Mike Christie5bb0b552006-04-06 21:26:46 -0500916 if (recv_digest != tcp_conn->in.datadgst) {
Mike Christief6cfba12005-11-29 23:12:57 -0600917 debug_tcp("iscsi_tcp: data digest error!"
918 "0x%x != 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500919 tcp_conn->in.datadgst);
Mike Christief6cfba12005-11-29 23:12:57 -0600920 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
921 return 0;
922 } else {
923 debug_tcp("iscsi_tcp: data digest match!"
924 "0x%x == 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500925 tcp_conn->in.datadgst);
926 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700927 }
928 }
929
Mike Christie5bb0b552006-04-06 21:26:46 -0500930 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
931 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700932
933 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500934 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700935
936 rc = iscsi_data_recv(conn);
937 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500938 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700939 goto again;
Mike Christied82967c2006-07-24 15:47:11 -0500940 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700941 return 0;
942 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500943 tcp_conn->in.copy -= tcp_conn->in.padding;
944 tcp_conn->in.offset += tcp_conn->in.padding;
Mike Christie8a47cd32005-11-30 02:27:19 -0600945 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500946 if (tcp_conn->in.padding) {
947 debug_tcp("padding -> %d\n",
948 tcp_conn->in.padding);
949 memset(pad, 0, tcp_conn->in.padding);
950 sg_init_one(&sg, pad, tcp_conn->in.padding);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500951 crypto_hash_update(&tcp_conn->rx_hash,
Herbert Xudc64ddf2006-08-24 18:45:50 +1000952 &sg, sg.length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700953 }
James Bottomleyc9802cd2006-09-23 15:33:43 -0500954 crypto_hash_final(&tcp_conn->rx_hash,
955 (u8 *) &tcp_conn->in.datadgst);
Mike Christie5bb0b552006-04-06 21:26:46 -0500956 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
957 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Mike Christieca518682006-08-31 18:09:32 -0400958 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700959 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500960 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700961 }
962
963 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500964 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
965 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700966
Mike Christie5bb0b552006-04-06 21:26:46 -0500967 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700968 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500969 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700970 goto more;
971 }
972
973nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500974 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700975 BUG_ON(processed == 0);
976 return processed;
977
978again:
Mike Christie5bb0b552006-04-06 21:26:46 -0500979 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700980 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
981 processed, (int)len, (int)rd_desc->count);
982 BUG_ON(processed == 0);
983 BUG_ON(processed > len);
984
985 conn->rxdata_octets += processed;
986 return processed;
987}
988
989static void
990iscsi_tcp_data_ready(struct sock *sk, int flag)
991{
992 struct iscsi_conn *conn = sk->sk_user_data;
993 read_descriptor_t rd_desc;
994
995 read_lock(&sk->sk_callback_lock);
996
Mike Christie665b44a2006-05-02 19:46:49 -0500997 /*
998 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
999 * We set count to 1 because we want the network layer to
1000 * hand us all the skbs that are available. iscsi_tcp_data_recv
1001 * handled pdus that cross buffers or pdus that still need data.
1002 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001003 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -05001004 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001005 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
1006
1007 read_unlock(&sk->sk_callback_lock);
1008}
1009
1010static void
1011iscsi_tcp_state_change(struct sock *sk)
1012{
Mike Christie5bb0b552006-04-06 21:26:46 -05001013 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001014 struct iscsi_conn *conn;
1015 struct iscsi_session *session;
1016 void (*old_state_change)(struct sock *);
1017
1018 read_lock(&sk->sk_callback_lock);
1019
1020 conn = (struct iscsi_conn*)sk->sk_user_data;
1021 session = conn->session;
1022
Mike Christiee6273992005-11-29 23:12:49 -06001023 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1024 sk->sk_state == TCP_CLOSE) &&
1025 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001026 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1027 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1028 }
1029
Mike Christie5bb0b552006-04-06 21:26:46 -05001030 tcp_conn = conn->dd_data;
1031 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001032
1033 read_unlock(&sk->sk_callback_lock);
1034
1035 old_state_change(sk);
1036}
1037
1038/**
1039 * iscsi_write_space - Called when more output buffer space is available
1040 * @sk: socket space is available for
1041 **/
1042static void
1043iscsi_write_space(struct sock *sk)
1044{
1045 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001046 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1047
1048 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001049 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001050 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001051}
1052
1053static void
1054iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1055{
Mike Christie5bb0b552006-04-06 21:26:46 -05001056 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1057 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001058
1059 /* assign new callbacks */
1060 write_lock_bh(&sk->sk_callback_lock);
1061 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001062 tcp_conn->old_data_ready = sk->sk_data_ready;
1063 tcp_conn->old_state_change = sk->sk_state_change;
1064 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001065 sk->sk_data_ready = iscsi_tcp_data_ready;
1066 sk->sk_state_change = iscsi_tcp_state_change;
1067 sk->sk_write_space = iscsi_write_space;
1068 write_unlock_bh(&sk->sk_callback_lock);
1069}
1070
1071static void
Mike Christie1c834692006-07-24 15:47:26 -05001072iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001073{
Mike Christie5bb0b552006-04-06 21:26:46 -05001074 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001075
1076 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1077 write_lock_bh(&sk->sk_callback_lock);
1078 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001079 sk->sk_data_ready = tcp_conn->old_data_ready;
1080 sk->sk_state_change = tcp_conn->old_state_change;
1081 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001082 sk->sk_no_check = 0;
1083 write_unlock_bh(&sk->sk_callback_lock);
1084}
1085
1086/**
1087 * iscsi_send - generic send routine
1088 * @sk: kernel's socket
1089 * @buf: buffer to write from
1090 * @size: actual size to write
1091 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001092 */
1093static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001094iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001095{
Mike Christie5bb0b552006-04-06 21:26:46 -05001096 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1097 struct socket *sk = tcp_conn->sock;
Mike Christie3219e522006-05-30 00:37:28 -05001098 int offset = buf->sg.offset + buf->sent, res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001099
Mike Christie7cae5152006-01-13 18:05:47 -06001100 /*
1101 * if we got use_sg=0 or are sending something we kmallocd
1102 * then we did not have to do kmap (kmap returns page_address)
1103 *
1104 * if we got use_sg > 0, but had to drop down, we do not
1105 * set clustering so this should only happen for that
1106 * slab case.
1107 */
1108 if (buf->use_sendmsg)
Mike Christie3219e522006-05-30 00:37:28 -05001109 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
Mike Christie7cae5152006-01-13 18:05:47 -06001110 else
Mike Christie3219e522006-05-30 00:37:28 -05001111 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1112
1113 if (res >= 0) {
1114 conn->txdata_octets += res;
1115 buf->sent += res;
1116 return res;
1117 }
1118
1119 tcp_conn->sendpage_failures_cnt++;
1120 if (res == -EAGAIN)
1121 res = -ENOBUFS;
1122 else
1123 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1124 return res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001125}
1126
1127/**
1128 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1129 * @conn: iscsi connection
1130 * @buf: buffer to write from
1131 * @datalen: lenght of data to be sent after the header
1132 *
1133 * Notes:
1134 * (Tx, Fast Path)
1135 **/
1136static inline int
1137iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1138{
Alex Aizman7ba24712005-08-04 19:30:08 -07001139 int flags = 0; /* MSG_DONTWAIT; */
1140 int res, size;
1141
1142 size = buf->sg.length - buf->sent;
1143 BUG_ON(buf->sent + size > buf->sg.length);
1144 if (buf->sent + size != buf->sg.length || datalen)
1145 flags |= MSG_MORE;
1146
FUJITA Tomonori56851692006-01-13 18:05:44 -06001147 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001148 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1149 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001150 if (size != res)
1151 return -EAGAIN;
1152 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001153 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001154
1155 return res;
1156}
1157
1158/**
1159 * iscsi_sendpage - send one page of iSCSI Data-Out.
1160 * @conn: iscsi connection
1161 * @buf: buffer to write from
1162 * @count: remaining data
1163 * @sent: number of bytes sent
1164 *
1165 * Notes:
1166 * (Tx, Fast Path)
1167 **/
1168static inline int
1169iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1170 int *count, int *sent)
1171{
Alex Aizman7ba24712005-08-04 19:30:08 -07001172 int flags = 0; /* MSG_DONTWAIT; */
1173 int res, size;
1174
1175 size = buf->sg.length - buf->sent;
1176 BUG_ON(buf->sent + size > buf->sg.length);
1177 if (size > *count)
1178 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001179 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001180 flags |= MSG_MORE;
1181
FUJITA Tomonori56851692006-01-13 18:05:44 -06001182 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001183 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1184 size, buf->sent, *count, *sent, res);
1185 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001186 *count -= res;
1187 *sent += res;
1188 if (size != res)
1189 return -EAGAIN;
1190 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001191 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001192
1193 return res;
1194}
1195
1196static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001197iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
Mike Christie62f38302006-08-31 18:09:27 -04001198 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001199{
James Bottomleyc9802cd2006-09-23 15:33:43 -05001200 crypto_hash_init(&tcp_conn->tx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -05001201 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001202}
1203
Alex Aizman7ba24712005-08-04 19:30:08 -07001204/**
1205 * iscsi_solicit_data_cont - initialize next Data-Out
1206 * @conn: iscsi connection
1207 * @ctask: scsi command task
1208 * @r2t: R2T info
1209 * @left: bytes left to transfer
1210 *
1211 * Notes:
1212 * Initialize next Data-Out within this R2T sequence and continue
1213 * to process next Scatter-Gather element(if any) of this SCSI command.
1214 *
1215 * Called under connection lock.
1216 **/
1217static void
1218iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1219 struct iscsi_r2t_info *r2t, int left)
1220{
1221 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001222 struct scsi_cmnd *sc = ctask->sc;
1223 int new_offset;
1224
Mike Christieffbfe922006-05-18 20:31:36 -05001225 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001226 memset(hdr, 0, sizeof(struct iscsi_data));
1227 hdr->ttt = r2t->ttt;
1228 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1229 r2t->solicit_datasn++;
1230 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001231 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1232 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001233 hdr->exp_statsn = r2t->exp_statsn;
1234 new_offset = r2t->data_offset + r2t->sent;
1235 hdr->offset = cpu_to_be32(new_offset);
1236 if (left > conn->max_xmit_dlength) {
1237 hton24(hdr->dlength, conn->max_xmit_dlength);
1238 r2t->data_count = conn->max_xmit_dlength;
1239 } else {
1240 hton24(hdr->dlength, left);
1241 r2t->data_count = left;
1242 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1243 }
1244 conn->dataout_pdus_cnt++;
1245
Mike Christie6e458cc2006-05-18 20:31:31 -05001246 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001247 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001248
Mike Christie62f38302006-08-31 18:09:27 -04001249 if (iscsi_buf_left(&r2t->sendbuf))
1250 return;
1251
1252 if (sc->use_sg) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001253 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1254 r2t->sg += 1;
Mike Christie62f38302006-08-31 18:09:27 -04001255 } else {
1256 iscsi_buf_init_iov(&r2t->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -07001257 (char*)sc->request_buffer + new_offset,
1258 r2t->data_count);
Mike Christie62f38302006-08-31 18:09:27 -04001259 r2t->sg = NULL;
1260 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001261}
1262
Mike Christie62f38302006-08-31 18:09:27 -04001263static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask,
1264 unsigned long len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001265{
Mike Christie62f38302006-08-31 18:09:27 -04001266 tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1);
1267 if (!tcp_ctask->pad_count)
1268 return;
Alex Aizman7ba24712005-08-04 19:30:08 -07001269
Mike Christie62f38302006-08-31 18:09:27 -04001270 tcp_ctask->pad_count = ISCSI_PAD_LEN - tcp_ctask->pad_count;
1271 debug_scsi("write padding %d bytes\n", tcp_ctask->pad_count);
1272 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Alex Aizman7ba24712005-08-04 19:30:08 -07001273}
1274
1275/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001276 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001277 * @conn: iscsi connection
1278 * @ctask: scsi command task
1279 * @sc: scsi command
1280 **/
1281static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001282iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001283{
Mike Christie5bb0b552006-04-06 21:26:46 -05001284 struct scsi_cmnd *sc = ctask->sc;
1285 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001286
Mike Christie5bb0b552006-04-06 21:26:46 -05001287 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Alex Aizman7ba24712005-08-04 19:30:08 -07001288
Mike Christie5bb0b552006-04-06 21:26:46 -05001289 tcp_ctask->sent = 0;
1290 tcp_ctask->sg_count = 0;
Mike Christied473cc72007-05-30 12:57:14 -05001291 tcp_ctask->exp_datasn = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001292
1293 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001294 tcp_ctask->xmstate = XMSTATE_W_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001295 BUG_ON(ctask->total_length == 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05001296
Alex Aizman7ba24712005-08-04 19:30:08 -07001297 if (sc->use_sg) {
1298 struct scatterlist *sg = sc->request_buffer;
1299
Mike Christie62f38302006-08-31 18:09:27 -04001300 iscsi_buf_init_sg(&tcp_ctask->sendbuf, sg);
1301 tcp_ctask->sg = sg + 1;
Mike Christie5bb0b552006-04-06 21:26:46 -05001302 tcp_ctask->bad_sg = sg + sc->use_sg;
Mike Christie62f38302006-08-31 18:09:27 -04001303 } else {
Mike Christie5bb0b552006-04-06 21:26:46 -05001304 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1305 sc->request_buffer,
1306 sc->request_bufflen);
Mike Christie62f38302006-08-31 18:09:27 -04001307 tcp_ctask->sg = NULL;
1308 tcp_ctask->bad_sg = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001309 }
Mike Christieffd04362006-08-31 18:09:24 -04001310 debug_scsi("cmd [itt 0x%x total %d imm_data %d "
1311 "unsol count %d, unsol offset %d]\n",
Alex Aizman7ba24712005-08-04 19:30:08 -07001312 ctask->itt, ctask->total_length, ctask->imm_count,
Mike Christieffd04362006-08-31 18:09:24 -04001313 ctask->unsol_count, ctask->unsol_offset);
Mike Christie5bb0b552006-04-06 21:26:46 -05001314 } else
1315 tcp_ctask->xmstate = XMSTATE_R_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001316
Mike Christie6e458cc2006-05-18 20:31:31 -05001317 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001318 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001319}
1320
1321/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001322 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001323 * @conn: iscsi connection
1324 * @mtask: task management task
1325 *
1326 * Notes:
1327 * The function can return -EAGAIN in which case caller must
1328 * call it again later, or recover. '0' return code means successful
1329 * xmit.
1330 *
1331 * Management xmit state machine consists of two states:
1332 * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
1333 * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1334 **/
1335static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001336iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001337{
Mike Christie5bb0b552006-04-06 21:26:46 -05001338 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001339 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001340
1341 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001342 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001343
Mike Christie5bb0b552006-04-06 21:26:46 -05001344 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
1345 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001346 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001347 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christieaf973482005-09-12 21:01:32 -05001348 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001349 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001350 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001351 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1352 (u8*)tcp_mtask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001353 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1354 mtask->data_count);
1355 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001356 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001357 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001358 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001359 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001360 }
1361 }
1362
Mike Christie5bb0b552006-04-06 21:26:46 -05001363 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001364 BUG_ON(!mtask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001365 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001366 /* FIXME: implement.
1367 * Virtual buffer could be spreaded across multiple pages...
1368 */
1369 do {
Mike Christie3219e522006-05-30 00:37:28 -05001370 int rc;
1371
1372 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1373 &mtask->data_count, &tcp_mtask->sent);
1374 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001375 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001376 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001377 }
1378 } while (mtask->data_count);
1379 }
1380
Mike Christie5bb0b552006-04-06 21:26:46 -05001381 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
Al Virob4377352007-02-09 16:39:40 +00001382 if (mtask->hdr->itt == RESERVED_ITT) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001383 struct iscsi_session *session = conn->session;
1384
1385 spin_lock_bh(&session->lock);
1386 list_del(&conn->mtask->running);
1387 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1388 sizeof(void*));
1389 spin_unlock_bh(&session->lock);
1390 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001391 return 0;
1392}
1393
1394static inline int
Mike Christie62f38302006-08-31 18:09:27 -04001395iscsi_send_read_hdr(struct iscsi_conn *conn,
1396 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001397{
Mike Christie3219e522006-05-30 00:37:28 -05001398 int rc;
1399
Mike Christie5bb0b552006-04-06 21:26:46 -05001400 tcp_ctask->xmstate &= ~XMSTATE_R_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001401 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001402 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1403 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001404 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, 0);
1405 if (!rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001406 BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE);
Alex Aizman7ba24712005-08-04 19:30:08 -07001407 return 0; /* wait for Data-In */
1408 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001409 tcp_ctask->xmstate |= XMSTATE_R_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001410 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001411}
1412
1413static inline int
Mike Christie62f38302006-08-31 18:09:27 -04001414iscsi_send_write_hdr(struct iscsi_conn *conn,
Mike Christie5bb0b552006-04-06 21:26:46 -05001415 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001416{
Mike Christie5bb0b552006-04-06 21:26:46 -05001417 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001418 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001419
1420 tcp_ctask->xmstate &= ~XMSTATE_W_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001421 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001422 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1423 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001424 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
Mike Christie3219e522006-05-30 00:37:28 -05001425 if (rc) {
Mike Christie62f38302006-08-31 18:09:27 -04001426 tcp_ctask->xmstate |= XMSTATE_W_HDR;
1427 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001428 }
Mike Christie3219e522006-05-30 00:37:28 -05001429
Mike Christie62f38302006-08-31 18:09:27 -04001430 if (ctask->imm_count) {
1431 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1432 iscsi_set_padding(tcp_ctask, ctask->imm_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001433
Mike Christie62f38302006-08-31 18:09:27 -04001434 if (ctask->conn->datadgst_en) {
1435 iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
1436 tcp_ctask->immdigest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001437 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001438 }
1439
Mike Christie62f38302006-08-31 18:09:27 -04001440 if (ctask->unsol_count)
1441 tcp_ctask->xmstate |= XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001442 return 0;
1443}
1444
Mike Christie62f38302006-08-31 18:09:27 -04001445static int
1446iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1447{
1448 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1449 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1450 int sent = 0, rc;
1451
1452 if (tcp_ctask->xmstate & XMSTATE_W_PAD) {
1453 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
1454 tcp_ctask->pad_count);
1455 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001456 crypto_hash_update(&tcp_conn->tx_hash,
1457 &tcp_ctask->sendbuf.sg,
1458 tcp_ctask->sendbuf.sg.length);
Mike Christie62f38302006-08-31 18:09:27 -04001459 } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD))
1460 return 0;
1461
1462 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
1463 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_PAD;
1464 debug_scsi("sending %d pad bytes for itt 0x%x\n",
1465 tcp_ctask->pad_count, ctask->itt);
1466 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1467 &sent);
1468 if (rc) {
1469 debug_scsi("padding send failed %d\n", rc);
1470 tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD;
1471 }
1472 return rc;
1473}
1474
1475static int
1476iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1477 struct iscsi_buf *buf, uint32_t *digest)
1478{
1479 struct iscsi_tcp_cmd_task *tcp_ctask;
1480 struct iscsi_tcp_conn *tcp_conn;
1481 int rc, sent = 0;
1482
1483 if (!conn->datadgst_en)
1484 return 0;
1485
1486 tcp_ctask = ctask->dd_data;
1487 tcp_conn = conn->dd_data;
1488
1489 if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) {
James Bottomleyc9802cd2006-09-23 15:33:43 -05001490 crypto_hash_final(&tcp_conn->tx_hash, (u8*)digest);
Mike Christie62f38302006-08-31 18:09:27 -04001491 iscsi_buf_init_iov(buf, (char*)digest, 4);
1492 }
1493 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST;
1494
1495 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
1496 if (!rc)
1497 debug_scsi("sent digest 0x%x for itt 0x%x\n", *digest,
1498 ctask->itt);
1499 else {
1500 debug_scsi("sending digest 0x%x failed for itt 0x%x!\n",
1501 *digest, ctask->itt);
1502 tcp_ctask->xmstate |= XMSTATE_W_RESEND_DATA_DIGEST;
1503 }
1504 return rc;
1505}
1506
1507static int
1508iscsi_send_data(struct iscsi_cmd_task *ctask, struct iscsi_buf *sendbuf,
1509 struct scatterlist **sg, int *sent, int *count,
1510 struct iscsi_buf *digestbuf, uint32_t *digest)
1511{
1512 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1513 struct iscsi_conn *conn = ctask->conn;
1514 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1515 int rc, buf_sent, offset;
1516
1517 while (*count) {
1518 buf_sent = 0;
1519 offset = sendbuf->sent;
1520
1521 rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent);
1522 *sent = *sent + buf_sent;
1523 if (buf_sent && conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001524 partial_sg_digest_update(&tcp_conn->tx_hash,
Mike Christie62f38302006-08-31 18:09:27 -04001525 &sendbuf->sg, sendbuf->sg.offset + offset,
1526 buf_sent);
1527 if (!iscsi_buf_left(sendbuf) && *sg != tcp_ctask->bad_sg) {
1528 iscsi_buf_init_sg(sendbuf, *sg);
1529 *sg = *sg + 1;
1530 }
1531
1532 if (rc)
1533 return rc;
1534 }
1535
1536 rc = iscsi_send_padding(conn, ctask);
1537 if (rc)
1538 return rc;
1539
1540 return iscsi_send_digest(conn, ctask, digestbuf, digest);
1541}
1542
1543static int
1544iscsi_send_unsol_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001545{
Mike Christie5bb0b552006-04-06 21:26:46 -05001546 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001547 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001548 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001549
Mike Christie5bb0b552006-04-06 21:26:46 -05001550 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1551 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Mike Christie62f38302006-08-31 18:09:27 -04001552 dtask = &tcp_ctask->unsol_dtask;
1553
Mike Christieffd04362006-08-31 18:09:24 -04001554 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
1555 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
1556 sizeof(struct iscsi_hdr));
Mike Christieaf973482005-09-12 21:01:32 -05001557 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001558 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001559 (u8*)dtask->hdrext);
Mike Christie62f38302006-08-31 18:09:27 -04001560
Mike Christie5bb0b552006-04-06 21:26:46 -05001561 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Mike Christie62f38302006-08-31 18:09:27 -04001562 iscsi_set_padding(tcp_ctask, ctask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001563 }
Mike Christie3219e522006-05-30 00:37:28 -05001564
1565 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1566 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001567 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1568 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001569 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001570 }
1571
Mike Christiedd8c0d92006-08-31 18:09:28 -04001572 if (conn->datadgst_en) {
1573 dtask = &tcp_ctask->unsol_dtask;
1574 iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
1575 dtask->digest = 0;
1576 }
1577
Alex Aizman7ba24712005-08-04 19:30:08 -07001578 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001579 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001580 return 0;
1581}
1582
Mike Christie62f38302006-08-31 18:09:27 -04001583static int
1584iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001585{
Mike Christie5bb0b552006-04-06 21:26:46 -05001586 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001587 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001588
Mike Christie62f38302006-08-31 18:09:27 -04001589 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
1590 BUG_ON(!ctask->unsol_count);
1591 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
1592send_hdr:
1593 rc = iscsi_send_unsol_hdr(conn, ctask);
1594 if (rc)
1595 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001596 }
1597
Mike Christie62f38302006-08-31 18:09:27 -04001598 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
1599 struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask;
Mike Christie5bb0b552006-04-06 21:26:46 -05001600 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001601
Mike Christie62f38302006-08-31 18:09:27 -04001602 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1603 &tcp_ctask->sent, &ctask->data_count,
1604 &dtask->digestbuf, &dtask->digest);
Mike Christie5bb0b552006-04-06 21:26:46 -05001605 ctask->unsol_count -= tcp_ctask->sent - start;
Mike Christie62f38302006-08-31 18:09:27 -04001606 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001607 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001608 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1609 /*
1610 * Done with the Data-Out. Next, check if we need
1611 * to send another unsolicited Data-Out.
1612 */
1613 if (ctask->unsol_count) {
1614 debug_scsi("sending more uns\n");
1615 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
1616 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001617 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001618 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001619 return 0;
1620}
1621
Mike Christie62f38302006-08-31 18:09:27 -04001622static int iscsi_send_sol_pdu(struct iscsi_conn *conn,
1623 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001624{
Mike Christie5bb0b552006-04-06 21:26:46 -05001625 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie62f38302006-08-31 18:09:27 -04001626 struct iscsi_session *session = conn->session;
1627 struct iscsi_r2t_info *r2t;
1628 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001629 int left, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001630
Mike Christie62f38302006-08-31 18:09:27 -04001631 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001632 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Mike Christie62f38302006-08-31 18:09:27 -04001633 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
Mike Christiedb37c502006-11-08 15:58:33 -06001634 if (!tcp_ctask->r2t) {
1635 spin_lock_bh(&session->lock);
Mike Christie62f38302006-08-31 18:09:27 -04001636 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1637 sizeof(void*));
Mike Christiedb37c502006-11-08 15:58:33 -06001638 spin_unlock_bh(&session->lock);
1639 }
Mike Christie62f38302006-08-31 18:09:27 -04001640send_hdr:
1641 r2t = tcp_ctask->r2t;
1642 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001643
Mike Christie62f38302006-08-31 18:09:27 -04001644 if (conn->hdrdgst_en)
1645 iscsi_hdr_digest(conn, &r2t->headbuf,
1646 (u8*)dtask->hdrext);
Mike Christie62f38302006-08-31 18:09:27 -04001647 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
Mike Christie3219e522006-05-30 00:37:28 -05001648 if (rc) {
Mike Christie62f38302006-08-31 18:09:27 -04001649 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1650 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001651 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001652 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001653
Mike Christiedd8c0d92006-08-31 18:09:28 -04001654 if (conn->datadgst_en) {
1655 iscsi_data_digest_init(conn->dd_data, tcp_ctask);
1656 dtask->digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001657 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001658
Mike Christie62f38302006-08-31 18:09:27 -04001659 iscsi_set_padding(tcp_ctask, r2t->data_count);
1660 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1661 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1662 r2t->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001663 }
1664
Mike Christie62f38302006-08-31 18:09:27 -04001665 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
1666 r2t = tcp_ctask->r2t;
1667 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001668
Mike Christie62f38302006-08-31 18:09:27 -04001669 rc = iscsi_send_data(ctask, &r2t->sendbuf, &r2t->sg,
1670 &r2t->sent, &r2t->data_count,
1671 &dtask->digestbuf, &dtask->digest);
1672 if (rc)
1673 return rc;
1674 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001675
Mike Christie62f38302006-08-31 18:09:27 -04001676 /*
1677 * Done with this Data-Out. Next, check if we have
1678 * to send another Data-Out for this R2T.
1679 */
1680 BUG_ON(r2t->data_length - r2t->sent < 0);
1681 left = r2t->data_length - r2t->sent;
1682 if (left) {
1683 iscsi_solicit_data_cont(conn, ctask, r2t, left);
1684 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1685 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1686 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001687 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001688
Mike Christie62f38302006-08-31 18:09:27 -04001689 /*
1690 * Done with this R2T. Check if there are more
1691 * outstanding R2Ts ready to be processed.
1692 */
1693 spin_lock_bh(&session->lock);
1694 tcp_ctask->r2t = NULL;
1695 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
1696 sizeof(void*));
1697 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t,
1698 sizeof(void*))) {
1699 tcp_ctask->r2t = r2t;
1700 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1701 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1702 spin_unlock_bh(&session->lock);
1703 goto send_hdr;
1704 }
1705 spin_unlock_bh(&session->lock);
1706 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001707 return 0;
1708}
1709
1710static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001711iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001712{
Mike Christie5bb0b552006-04-06 21:26:46 -05001713 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001714 int rc = 0;
1715
1716 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001717 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001718
1719 /*
1720 * serialize with TMF AbortTask
1721 */
1722 if (ctask->mtask)
1723 return rc;
1724
Mike Christie3219e522006-05-30 00:37:28 -05001725 if (tcp_ctask->xmstate & XMSTATE_R_HDR)
Mike Christie62f38302006-08-31 18:09:27 -04001726 return iscsi_send_read_hdr(conn, tcp_ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001727
Mike Christie5bb0b552006-04-06 21:26:46 -05001728 if (tcp_ctask->xmstate & XMSTATE_W_HDR) {
Mike Christie62f38302006-08-31 18:09:27 -04001729 rc = iscsi_send_write_hdr(conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001730 if (rc)
1731 return rc;
1732 }
1733
Mike Christie5bb0b552006-04-06 21:26:46 -05001734 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Mike Christie62f38302006-08-31 18:09:27 -04001735 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1736 &tcp_ctask->sent, &ctask->imm_count,
1737 &tcp_ctask->immbuf, &tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001738 if (rc)
1739 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001740 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001741 }
1742
Mike Christie62f38302006-08-31 18:09:27 -04001743 rc = iscsi_send_unsol_pdu(conn, ctask);
1744 if (rc)
1745 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001746
Mike Christie62f38302006-08-31 18:09:27 -04001747 rc = iscsi_send_sol_pdu(conn, ctask);
1748 if (rc)
1749 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001750
1751 return rc;
1752}
1753
Mike Christie7b8631b2006-01-13 18:05:50 -06001754static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001755iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001756{
Mike Christie7b8631b2006-01-13 18:05:50 -06001757 struct iscsi_conn *conn;
1758 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001759 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001760
Mike Christie5bb0b552006-04-06 21:26:46 -05001761 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001762 if (!cls_conn)
1763 return NULL;
1764 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001765 /*
1766 * due to strange issues with iser these are not set
1767 * in iscsi_conn_setup
1768 */
Mike Christiebf32ed32007-02-28 17:32:17 -06001769 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
Alex Aizman7ba24712005-08-04 19:30:08 -07001770
Mike Christie5bb0b552006-04-06 21:26:46 -05001771 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1772 if (!tcp_conn)
1773 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001774
Mike Christie5bb0b552006-04-06 21:26:46 -05001775 conn->dd_data = tcp_conn;
1776 tcp_conn->iscsi_conn = conn;
1777 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1778 /* initial operational parameters */
1779 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07001780
James Bottomleyc9802cd2006-09-23 15:33:43 -05001781 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1782 CRYPTO_ALG_ASYNC);
1783 tcp_conn->tx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001784 if (IS_ERR(tcp_conn->tx_hash.tfm)) {
1785 printk(KERN_ERR "Could not create connection due to crc32c "
1786 "loading error %ld. Make sure the crc32c module is "
1787 "built as a module or into the kernel\n",
1788 PTR_ERR(tcp_conn->tx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001789 goto free_tcp_conn;
Mike Christie0f238412007-02-28 17:32:21 -06001790 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001791
James Bottomleyc9802cd2006-09-23 15:33:43 -05001792 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1793 CRYPTO_ALG_ASYNC);
1794 tcp_conn->rx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001795 if (IS_ERR(tcp_conn->rx_hash.tfm)) {
1796 printk(KERN_ERR "Could not create connection due to crc32c "
1797 "loading error %ld. Make sure the crc32c module is "
1798 "built as a module or into the kernel\n",
1799 PTR_ERR(tcp_conn->rx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001800 goto free_tx_tfm;
Mike Christie0f238412007-02-28 17:32:21 -06001801 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001802
Mike Christie7b8631b2006-01-13 18:05:50 -06001803 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001804
Mike Christiedd8c0d92006-08-31 18:09:28 -04001805free_tx_tfm:
James Bottomleyc9802cd2006-09-23 15:33:43 -05001806 crypto_free_hash(tcp_conn->tx_hash.tfm);
Mike Christiedd8c0d92006-08-31 18:09:28 -04001807free_tcp_conn:
1808 kfree(tcp_conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001809tcp_conn_alloc_fail:
1810 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001811 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001812}
1813
1814static void
Mike Christie1c834692006-07-24 15:47:26 -05001815iscsi_tcp_release_conn(struct iscsi_conn *conn)
1816{
1817 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1818
1819 if (!tcp_conn->sock)
1820 return;
1821
1822 sock_hold(tcp_conn->sock->sk);
1823 iscsi_conn_restore_callbacks(tcp_conn);
1824 sock_put(tcp_conn->sock->sk);
1825
1826 sock_release(tcp_conn->sock);
1827 tcp_conn->sock = NULL;
1828 conn->recv_lock = NULL;
1829}
1830
1831static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001832iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001833{
Mike Christie7b8631b2006-01-13 18:05:50 -06001834 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001835 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001836
Mike Christie1c834692006-07-24 15:47:26 -05001837 iscsi_tcp_release_conn(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001838 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001839
Pete Wyckoff534284a2006-11-08 15:58:31 -06001840 if (tcp_conn->tx_hash.tfm)
1841 crypto_free_hash(tcp_conn->tx_hash.tfm);
1842 if (tcp_conn->rx_hash.tfm)
1843 crypto_free_hash(tcp_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001844
Mike Christie5bb0b552006-04-06 21:26:46 -05001845 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001846}
1847
Mike Christie1c834692006-07-24 15:47:26 -05001848static void
1849iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1850{
1851 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christied5390f52006-08-31 18:09:30 -04001852 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie1c834692006-07-24 15:47:26 -05001853
1854 iscsi_conn_stop(cls_conn, flag);
1855 iscsi_tcp_release_conn(conn);
Mike Christied5390f52006-08-31 18:09:30 -04001856 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christie1c834692006-07-24 15:47:26 -05001857}
1858
Alex Aizman7ba24712005-08-04 19:30:08 -07001859static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001860iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001861 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001862 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001863{
Mike Christie5bb0b552006-04-06 21:26:46 -05001864 struct iscsi_conn *conn = cls_conn->dd_data;
1865 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001866 struct sock *sk;
1867 struct socket *sock;
1868 int err;
1869
1870 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001871 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001872 if (!sock) {
1873 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1874 return -EEXIST;
1875 }
1876
Mike Christie5bb0b552006-04-06 21:26:46 -05001877 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1878 if (err)
1879 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001880
Mike Christie67a61112006-05-30 00:37:20 -05001881 /* bind iSCSI connection and socket */
1882 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001883
Mike Christie67a61112006-05-30 00:37:20 -05001884 /* setup Socket parameters */
1885 sk = sock->sk;
1886 sk->sk_reuse = 1;
1887 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1888 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001889
Mike Christie67a61112006-05-30 00:37:20 -05001890 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07001891
Mike Christie67a61112006-05-30 00:37:20 -05001892 /*
1893 * Intercept TCP callbacks for sendfile like receive
1894 * processing.
1895 */
1896 conn->recv_lock = &sk->sk_callback_lock;
1897 iscsi_conn_set_callbacks(conn);
1898 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1899 /*
1900 * set receive state machine into initial state
1901 */
1902 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07001903
Alex Aizman7ba24712005-08-04 19:30:08 -07001904 return 0;
1905}
1906
Mike Christie5bb0b552006-04-06 21:26:46 -05001907/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05001908static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001909iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask,
1910 char *data, uint32_t data_size)
Mike Christie30a6c652006-04-06 21:13:39 -05001911{
Mike Christie5bb0b552006-04-06 21:26:46 -05001912 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie30a6c652006-04-06 21:13:39 -05001913
Mike Christie6e458cc2006-05-18 20:31:31 -05001914 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
1915 sizeof(struct iscsi_hdr));
Mike Christie5bb0b552006-04-06 21:26:46 -05001916 tcp_mtask->xmstate = XMSTATE_IMM_HDR;
Mike Christieb6c395e2006-07-24 15:47:15 -05001917 tcp_mtask->sent = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001918
Mike Christie5bb0b552006-04-06 21:26:46 -05001919 if (mtask->data_count)
1920 iscsi_buf_init_iov(&tcp_mtask->sendbuf, (char*)mtask->data,
Alex Aizman7ba24712005-08-04 19:30:08 -07001921 mtask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001922}
1923
1924static int
1925iscsi_r2tpool_alloc(struct iscsi_session *session)
1926{
1927 int i;
1928 int cmd_i;
1929
1930 /*
1931 * initialize per-task: R2T pool and xmit queue
1932 */
1933 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1934 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05001935 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001936
1937 /*
1938 * pre-allocated x4 as much r2ts to handle race when
1939 * target acks DataOut faster than we data_xmit() queues
1940 * could replenish r2tqueue.
1941 */
1942
1943 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05001944 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
1945 (void***)&tcp_ctask->r2ts,
1946 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001947 goto r2t_alloc_fail;
1948 }
1949
1950 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05001951 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07001952 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05001953 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
1954 iscsi_pool_free(&tcp_ctask->r2tpool,
1955 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07001956 goto r2t_alloc_fail;
1957 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001958 }
1959
1960 return 0;
1961
1962r2t_alloc_fail:
1963 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001964 struct iscsi_cmd_task *ctask = session->cmds[i];
1965 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1966
Mike Christie5bb0b552006-04-06 21:26:46 -05001967 kfifo_free(tcp_ctask->r2tqueue);
1968 iscsi_pool_free(&tcp_ctask->r2tpool,
1969 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07001970 }
1971 return -ENOMEM;
1972}
1973
1974static void
1975iscsi_r2tpool_free(struct iscsi_session *session)
1976{
1977 int i;
1978
1979 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001980 struct iscsi_cmd_task *ctask = session->cmds[i];
1981 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1982
Mike Christie5bb0b552006-04-06 21:26:46 -05001983 kfifo_free(tcp_ctask->r2tqueue);
1984 iscsi_pool_free(&tcp_ctask->r2tpool,
1985 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07001986 }
1987}
1988
Alex Aizman7ba24712005-08-04 19:30:08 -07001989static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001990iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05001991 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07001992{
Mike Christie7b7232f2006-02-01 21:06:49 -06001993 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001994 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001995 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001996 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07001997
Alex Aizman7ba24712005-08-04 19:30:08 -07001998 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001999 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002000 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002001 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christiedd8c0d92006-08-31 18:09:28 -04002002 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05002003 tcp_conn->hdr_size += sizeof(__u32);
Alex Aizman7ba24712005-08-04 19:30:08 -07002004 break;
2005 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002006 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002007 tcp_conn->sendpage = conn->datadgst_en ?
2008 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07002009 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002010 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002011 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07002012 if (session->max_r2t == roundup_pow_of_two(value))
2013 break;
2014 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002015 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002016 if (session->max_r2t & (session->max_r2t - 1))
2017 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2018 if (iscsi_r2tpool_alloc(session))
2019 return -ENOMEM;
2020 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002021 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002022 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002023 }
2024
2025 return 0;
2026}
2027
2028static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05002029iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2030 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06002031{
Mike Christie7b7232f2006-02-01 21:06:49 -06002032 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002033 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christiefd7255f2006-04-06 21:13:36 -05002034 struct inet_sock *inet;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002035 struct ipv6_pinfo *np;
2036 struct sock *sk;
2037 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06002038
2039 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002040 case ISCSI_PARAM_CONN_PORT:
2041 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002042 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002043 mutex_unlock(&conn->xmitmutex);
2044 return -EINVAL;
2045 }
2046
Mike Christie5bb0b552006-04-06 21:26:46 -05002047 inet = inet_sk(tcp_conn->sock->sk);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002048 len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
Mike Christiefd7255f2006-04-06 21:13:36 -05002049 mutex_unlock(&conn->xmitmutex);
Mike Christie8d2860b2006-05-02 19:46:47 -05002050 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002051 case ISCSI_PARAM_CONN_ADDRESS:
2052 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002053 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002054 mutex_unlock(&conn->xmitmutex);
2055 return -EINVAL;
2056 }
2057
Mike Christie5bb0b552006-04-06 21:26:46 -05002058 sk = tcp_conn->sock->sk;
Mike Christiefd7255f2006-04-06 21:13:36 -05002059 if (sk->sk_family == PF_INET) {
2060 inet = inet_sk(sk);
FUJITA Tomonori94cb3f82006-12-17 12:10:27 -06002061 len = sprintf(buf, NIPQUAD_FMT "\n",
Mike Christiefd7255f2006-04-06 21:13:36 -05002062 NIPQUAD(inet->daddr));
2063 } else {
2064 np = inet6_sk(sk);
FUJITA Tomonori94cb3f82006-12-17 12:10:27 -06002065 len = sprintf(buf, NIP6_FMT "\n", NIP6(np->daddr));
Mike Christiefd7255f2006-04-06 21:13:36 -05002066 }
2067 mutex_unlock(&conn->xmitmutex);
2068 break;
2069 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002070 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05002071 }
2072
2073 return len;
2074}
2075
Alex Aizman7ba24712005-08-04 19:30:08 -07002076static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002077iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002078{
Mike Christie7b7232f2006-02-01 21:06:49 -06002079 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002080 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002081
2082 stats->txdata_octets = conn->txdata_octets;
2083 stats->rxdata_octets = conn->rxdata_octets;
2084 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2085 stats->dataout_pdus = conn->dataout_pdus_cnt;
2086 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2087 stats->datain_pdus = conn->datain_pdus_cnt;
2088 stats->r2t_pdus = conn->r2t_pdus_cnt;
2089 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2090 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2091 stats->custom_length = 3;
2092 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002093 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002094 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002095 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002096 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2097 stats->custom[2].value = conn->eh_abort_cnt;
2098}
2099
Mike Christie5bb0b552006-04-06 21:26:46 -05002100static struct iscsi_cls_session *
2101iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2102 struct scsi_transport_template *scsit,
2103 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002104{
Mike Christie5bb0b552006-04-06 21:26:46 -05002105 struct iscsi_cls_session *cls_session;
2106 struct iscsi_session *session;
2107 uint32_t hn;
2108 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002109
Mike Christie5bb0b552006-04-06 21:26:46 -05002110 cls_session = iscsi_session_setup(iscsit, scsit,
2111 sizeof(struct iscsi_tcp_cmd_task),
2112 sizeof(struct iscsi_tcp_mgmt_task),
2113 initial_cmdsn, &hn);
2114 if (!cls_session)
2115 return NULL;
2116 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002117
Mike Christie5bb0b552006-04-06 21:26:46 -05002118 session = class_to_transport_session(cls_session);
2119 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2120 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2121 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2122
2123 ctask->hdr = &tcp_ctask->hdr;
2124 }
2125
2126 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2127 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2128 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2129
2130 mtask->hdr = &tcp_mtask->hdr;
2131 }
2132
2133 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2134 goto r2tpool_alloc_fail;
2135
2136 return cls_session;
2137
2138r2tpool_alloc_fail:
2139 iscsi_session_teardown(cls_session);
2140 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002141}
2142
Mike Christie5bb0b552006-04-06 21:26:46 -05002143static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2144{
Mike Christie5bb0b552006-04-06 21:26:46 -05002145 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2146 iscsi_session_teardown(cls_session);
2147}
2148
2149static struct scsi_host_template iscsi_sht = {
Mike Christief4246b32006-07-24 15:47:54 -05002150 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05002151 .queuecommand = iscsi_queuecommand,
2152 .change_queue_depth = iscsi_change_queue_depth,
2153 .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
2154 .sg_tablesize = ISCSI_SG_TABLESIZE,
Mike Christie8231f0e2007-02-28 17:32:20 -06002155 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -05002156 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2157 .eh_abort_handler = iscsi_eh_abort,
2158 .eh_host_reset_handler = iscsi_eh_host_reset,
2159 .use_clustering = DISABLE_CLUSTERING,
2160 .proc_name = "iscsi_tcp",
2161 .this_id = -1,
2162};
2163
Alex Aizman7ba24712005-08-04 19:30:08 -07002164static struct iscsi_transport iscsi_tcp_transport = {
2165 .owner = THIS_MODULE,
2166 .name = "tcp",
2167 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2168 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002169 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2170 ISCSI_MAX_XMIT_DLENGTH |
2171 ISCSI_HDRDGST_EN |
2172 ISCSI_DATADGST_EN |
2173 ISCSI_INITIAL_R2T_EN |
2174 ISCSI_MAX_R2T |
2175 ISCSI_IMM_DATA_EN |
2176 ISCSI_FIRST_BURST |
2177 ISCSI_MAX_BURST |
2178 ISCSI_PDU_INORDER_EN |
2179 ISCSI_DATASEQ_INORDER_EN |
2180 ISCSI_ERL |
2181 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002182 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05002183 ISCSI_EXP_STATSN |
2184 ISCSI_PERSISTENT_PORT |
2185 ISCSI_PERSISTENT_ADDRESS |
2186 ISCSI_TARGET_NAME |
2187 ISCSI_TPGT,
Mike Christie8ad57812007-05-30 12:57:13 -05002188 .host_param_mask = ISCSI_HOST_HWADDRESS |
2189 ISCSI_HOST_INITIATOR_NAME,
Alex Aizman7ba24712005-08-04 19:30:08 -07002190 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002191 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002192 .max_conn = 1,
2193 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002194 /* session management */
2195 .create_session = iscsi_tcp_session_create,
2196 .destroy_session = iscsi_tcp_session_destroy,
2197 /* connection management */
2198 .create_conn = iscsi_tcp_conn_create,
2199 .bind_conn = iscsi_tcp_conn_bind,
2200 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002201 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002202 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002203 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002204 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05002205 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -05002206 /* iscsi host params */
2207 .get_host_param = iscsi_host_get_param,
2208 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -05002209 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002210 .send_pdu = iscsi_conn_send_pdu,
2211 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002212 .init_cmd_task = iscsi_tcp_cmd_init,
2213 .init_mgmt_task = iscsi_tcp_mgmt_init,
2214 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2215 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2216 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2217 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002218 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002219};
2220
2221static int __init
2222iscsi_tcp_init(void)
2223{
Alex Aizman7ba24712005-08-04 19:30:08 -07002224 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002225 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2226 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002227 return -EINVAL;
2228 }
2229 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2230
Mike Christie7b8631b2006-01-13 18:05:50 -06002231 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002232 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002233
Mike Christie7b8631b2006-01-13 18:05:50 -06002234 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002235}
2236
2237static void __exit
2238iscsi_tcp_exit(void)
2239{
2240 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002241}
2242
2243module_init(iscsi_tcp_init);
2244module_exit(iscsi_tcp_exit);