blob: 6eaa2e3a9252473fe4e9fdcbb9c2323ef57ad995 [file] [log] [blame]
Alex Aizman7ba24712005-08-04 19:30:08 -07001/*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
Mike Christie5bb0b552006-04-06 21:26:46 -05006 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
Alex Aizman7ba24712005-08-04 19:30:08 -07008 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * See the file COPYING included with this distribution for more details.
21 *
22 * Credits:
23 * Christoph Hellwig
24 * FUJITA Tomonori
25 * Arne Redlich
26 * Zhenyu Wang
27 */
28
29#include <linux/types.h>
30#include <linux/list.h>
31#include <linux/inet.h>
32#include <linux/blkdev.h>
33#include <linux/crypto.h>
34#include <linux/delay.h>
35#include <linux/kfifo.h>
36#include <linux/scatterlist.h>
37#include <net/tcp.h>
38#include <scsi/scsi_cmnd.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070039#include <scsi/scsi_host.h>
40#include <scsi/scsi.h>
41#include <scsi/scsi_transport_iscsi.h>
42
43#include "iscsi_tcp.h"
44
45MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
46 "Alex Aizman <itn780@yahoo.com>");
47MODULE_DESCRIPTION("iSCSI/TCP data-path");
48MODULE_LICENSE("GPL");
Alex Aizman7ba24712005-08-04 19:30:08 -070049/* #define DEBUG_TCP */
Alex Aizman7ba24712005-08-04 19:30:08 -070050#define DEBUG_ASSERT
51
52#ifdef DEBUG_TCP
Mike Christie5bb0b552006-04-06 21:26:46 -050053#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
Alex Aizman7ba24712005-08-04 19:30:08 -070054#else
55#define debug_tcp(fmt...)
56#endif
57
Alex Aizman7ba24712005-08-04 19:30:08 -070058#ifndef DEBUG_ASSERT
59#ifdef BUG_ON
60#undef BUG_ON
61#endif
62#define BUG_ON(expr)
63#endif
64
Alex Aizman7ba24712005-08-04 19:30:08 -070065static unsigned int iscsi_max_lun = 512;
66module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
67
Alex Aizman7ba24712005-08-04 19:30:08 -070068static inline void
Alex Aizman7ba24712005-08-04 19:30:08 -070069iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
70{
Mike Christie7cae5152006-01-13 18:05:47 -060071 ibuf->sg.page = virt_to_page(vbuf);
72 ibuf->sg.offset = offset_in_page(vbuf);
Alex Aizman7ba24712005-08-04 19:30:08 -070073 ibuf->sg.length = size;
74 ibuf->sent = 0;
Mike Christie7cae5152006-01-13 18:05:47 -060075 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070076}
77
78static inline void
79iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
80{
Mike Christie7cae5152006-01-13 18:05:47 -060081 ibuf->sg.page = sg->page;
82 ibuf->sg.offset = sg->offset;
83 ibuf->sg.length = sg->length;
Alex Aizman7ba24712005-08-04 19:30:08 -070084 /*
85 * Fastpath: sg element fits into single page
86 */
Mike Christiea1e80c22006-01-13 18:05:56 -060087 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
Mike Christie7cae5152006-01-13 18:05:47 -060088 ibuf->use_sendmsg = 0;
89 else
90 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070091 ibuf->sent = 0;
92}
93
94static inline int
95iscsi_buf_left(struct iscsi_buf *ibuf)
96{
97 int rc;
98
99 rc = ibuf->sg.length - ibuf->sent;
100 BUG_ON(rc < 0);
101 return rc;
102}
103
104static inline void
Mike Christieaf973482005-09-12 21:01:32 -0500105iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
106 u8* crc)
Alex Aizman7ba24712005-08-04 19:30:08 -0700107{
Mike Christie5bb0b552006-04-06 21:26:46 -0500108 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
109
James Bottomleyc9802cd2006-09-23 15:33:43 -0500110 crypto_hash_digest(&tcp_conn->tx_hash, &buf->sg, buf->sg.length, crc);
Mike Christie218432c2007-05-30 12:57:17 -0500111 buf->sg.length += sizeof(u32);
Alex Aizman7ba24712005-08-04 19:30:08 -0700112}
113
Alex Aizman7ba24712005-08-04 19:30:08 -0700114static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500115iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700116{
Mike Christie5bb0b552006-04-06 21:26:46 -0500117 struct sk_buff *skb = tcp_conn->in.skb;
Alex Aizman7ba24712005-08-04 19:30:08 -0700118
Mike Christie5bb0b552006-04-06 21:26:46 -0500119 tcp_conn->in.zero_copy_hdr = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700120
Mike Christie5bb0b552006-04-06 21:26:46 -0500121 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
122 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700123 /*
124 * Zero-copy PDU Header: using connection context
125 * to store header pointer.
126 */
127 if (skb_shinfo(skb)->frag_list == NULL &&
Mike Christie5bb0b552006-04-06 21:26:46 -0500128 !skb_shinfo(skb)->nr_frags) {
129 tcp_conn->in.hdr = (struct iscsi_hdr *)
130 ((char*)skb->data + tcp_conn->in.offset);
131 tcp_conn->in.zero_copy_hdr = 1;
132 } else {
Alex Aizman7ba24712005-08-04 19:30:08 -0700133 /* ignoring return code since we checked
134 * in.copy before */
Mike Christie5bb0b552006-04-06 21:26:46 -0500135 skb_copy_bits(skb, tcp_conn->in.offset,
136 &tcp_conn->hdr, tcp_conn->hdr_size);
137 tcp_conn->in.hdr = &tcp_conn->hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700138 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500139 tcp_conn->in.offset += tcp_conn->hdr_size;
140 tcp_conn->in.copy -= tcp_conn->hdr_size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700141 } else {
142 int hdr_remains;
143 int copylen;
144
145 /*
146 * PDU header scattered across SKB's,
147 * copying it... This'll happen quite rarely.
148 */
149
Mike Christie5bb0b552006-04-06 21:26:46 -0500150 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
151 tcp_conn->in.hdr_offset = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700152
Mike Christie5bb0b552006-04-06 21:26:46 -0500153 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700154 BUG_ON(hdr_remains <= 0);
155
Mike Christie5bb0b552006-04-06 21:26:46 -0500156 copylen = min(tcp_conn->in.copy, hdr_remains);
157 skb_copy_bits(skb, tcp_conn->in.offset,
158 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
159 copylen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700160
161 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
Mike Christie5bb0b552006-04-06 21:26:46 -0500162 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
163 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700164
Mike Christie5bb0b552006-04-06 21:26:46 -0500165 tcp_conn->in.offset += copylen;
166 tcp_conn->in.copy -= copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700167 if (copylen < hdr_remains) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500168 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
169 tcp_conn->in.hdr_offset += copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700170 return -EAGAIN;
171 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500172 tcp_conn->in.hdr = &tcp_conn->hdr;
173 tcp_conn->discontiguous_hdr_cnt++;
174 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700175 }
176
177 return 0;
178}
179
Mike Christie30a6c652006-04-06 21:13:39 -0500180/*
181 * must be called with session lock
182 */
183static void
Mike Christieb6c395e2006-07-24 15:47:15 -0500184iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700185{
Mike Christie5bb0b552006-04-06 21:26:46 -0500186 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500187 struct iscsi_r2t_info *r2t;
Mike Christie30a6c652006-04-06 21:13:39 -0500188 struct scsi_cmnd *sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700189
Mike Christieb6c395e2006-07-24 15:47:15 -0500190 /* flush ctask's r2t queues */
191 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
192 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
193 sizeof(void*));
194 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
195 }
196
Mike Christie30a6c652006-04-06 21:13:39 -0500197 sc = ctask->sc;
198 if (unlikely(!sc))
Alex Aizman7ba24712005-08-04 19:30:08 -0700199 return;
Mike Christie30a6c652006-04-06 21:13:39 -0500200
Mike Christie5bb0b552006-04-06 21:26:46 -0500201 tcp_ctask->xmstate = XMSTATE_IDLE;
202 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700203}
204
205/**
206 * iscsi_data_rsp - SCSI Data-In Response processing
207 * @conn: iscsi connection
208 * @ctask: scsi command task
209 **/
210static int
211iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
212{
Mike Christie5bb0b552006-04-06 21:26:46 -0500213 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
214 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
215 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700216 struct iscsi_session *session = conn->session;
Mike Christie857ae0b2007-05-30 12:57:15 -0500217 struct scsi_cmnd *sc = ctask->sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700218 int datasn = be32_to_cpu(rhdr->datasn);
219
Mike Christie77a23c22007-05-30 12:57:18 -0500220 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Alex Aizman7ba24712005-08-04 19:30:08 -0700221 /*
222 * setup Data-In byte counter (gets decremented..)
223 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500224 ctask->data_count = tcp_conn->in.datalen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700225
Mike Christie5bb0b552006-04-06 21:26:46 -0500226 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700227 return 0;
228
Mike Christied473cc72007-05-30 12:57:14 -0500229 if (tcp_ctask->exp_datasn != datasn) {
230 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
231 __FUNCTION__, tcp_ctask->exp_datasn, datasn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700232 return ISCSI_ERR_DATASN;
Mike Christied473cc72007-05-30 12:57:14 -0500233 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700234
Mike Christied473cc72007-05-30 12:57:14 -0500235 tcp_ctask->exp_datasn++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700236
Mike Christie5bb0b552006-04-06 21:26:46 -0500237 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
Mike Christie857ae0b2007-05-30 12:57:15 -0500238 if (tcp_ctask->data_offset + tcp_conn->in.datalen > sc->request_bufflen) {
239 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
240 __FUNCTION__, tcp_ctask->data_offset,
241 tcp_conn->in.datalen, sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700242 return ISCSI_ERR_DATA_OFFSET;
Mike Christie857ae0b2007-05-30 12:57:15 -0500243 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700244
245 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700246 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600247 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700248 int res_count = be32_to_cpu(rhdr->residual_count);
249
250 if (res_count > 0 &&
251 res_count <= sc->request_bufflen) {
252 sc->resid = res_count;
253 sc->result = (DID_OK << 16) | rhdr->cmd_status;
254 } else
255 sc->result = (DID_BAD_TARGET << 16) |
256 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600257 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700258 sc->resid = be32_to_cpu(rhdr->residual_count);
259 sc->result = (DID_OK << 16) | rhdr->cmd_status;
260 } else
261 sc->result = (DID_OK << 16) | rhdr->cmd_status;
262 }
263
264 conn->datain_pdus_cnt++;
265 return 0;
266}
267
268/**
269 * iscsi_solicit_data_init - initialize first Data-Out
270 * @conn: iscsi connection
271 * @ctask: scsi command task
272 * @r2t: R2T info
273 *
274 * Notes:
275 * Initialize first Data-Out within this R2T sequence and finds
276 * proper data_offset within this SCSI command.
277 *
278 * This function is called with connection lock taken.
279 **/
280static void
281iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
282 struct iscsi_r2t_info *r2t)
283{
284 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700285 struct scsi_cmnd *sc = ctask->sc;
286
Mike Christieffbfe922006-05-18 20:31:36 -0500287 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700288 memset(hdr, 0, sizeof(struct iscsi_data));
289 hdr->ttt = r2t->ttt;
290 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
291 r2t->solicit_datasn++;
292 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500293 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
294 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700295 hdr->exp_statsn = r2t->exp_statsn;
296 hdr->offset = cpu_to_be32(r2t->data_offset);
297 if (r2t->data_length > conn->max_xmit_dlength) {
298 hton24(hdr->dlength, conn->max_xmit_dlength);
299 r2t->data_count = conn->max_xmit_dlength;
300 hdr->flags = 0;
301 } else {
302 hton24(hdr->dlength, r2t->data_length);
303 r2t->data_count = r2t->data_length;
304 hdr->flags = ISCSI_FLAG_CMD_FINAL;
305 }
306 conn->dataout_pdus_cnt++;
307
308 r2t->sent = 0;
309
Mike Christie6e458cc2006-05-18 20:31:31 -0500310 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -0500311 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -0700312
Alex Aizman7ba24712005-08-04 19:30:08 -0700313 if (sc->use_sg) {
314 int i, sg_count = 0;
315 struct scatterlist *sg = sc->request_buffer;
316
317 r2t->sg = NULL;
318 for (i = 0; i < sc->use_sg; i++, sg += 1) {
319 /* FIXME: prefetch ? */
320 if (sg_count + sg->length > r2t->data_offset) {
321 int page_offset;
322
323 /* sg page found! */
324
325 /* offset within this page */
326 page_offset = r2t->data_offset - sg_count;
327
328 /* fill in this buffer */
329 iscsi_buf_init_sg(&r2t->sendbuf, sg);
330 r2t->sendbuf.sg.offset += page_offset;
331 r2t->sendbuf.sg.length -= page_offset;
332
333 /* xmit logic will continue with next one */
334 r2t->sg = sg + 1;
335 break;
336 }
337 sg_count += sg->length;
338 }
339 BUG_ON(r2t->sg == NULL);
Mike Christie62f38302006-08-31 18:09:27 -0400340 } else {
341 iscsi_buf_init_iov(&r2t->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -0700342 (char*)sc->request_buffer + r2t->data_offset,
343 r2t->data_count);
Mike Christie62f38302006-08-31 18:09:27 -0400344 r2t->sg = NULL;
345 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700346}
347
348/**
349 * iscsi_r2t_rsp - iSCSI R2T Response processing
350 * @conn: iscsi connection
351 * @ctask: scsi command task
352 **/
353static int
354iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
355{
356 struct iscsi_r2t_info *r2t;
357 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500358 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
359 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
360 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700361 int r2tsn = be32_to_cpu(rhdr->r2tsn);
362 int rc;
363
Mike Christie98a94162006-08-31 18:09:26 -0400364 if (tcp_conn->in.datalen) {
365 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
366 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700367 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400368 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700369
Mike Christied473cc72007-05-30 12:57:14 -0500370 if (tcp_ctask->exp_datasn != r2tsn){
371 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
372 __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700373 return ISCSI_ERR_R2TSN;
Mike Christied473cc72007-05-30 12:57:14 -0500374 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700375
Alex Aizman7ba24712005-08-04 19:30:08 -0700376 /* fill-in new R2T associated with the task */
377 spin_lock(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -0500378 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
379
Alex Aizman7ba24712005-08-04 19:30:08 -0700380 if (!ctask->sc || ctask->mtask ||
381 session->state != ISCSI_STATE_LOGGED_IN) {
382 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
383 "recovery...\n", ctask->itt);
384 spin_unlock(&session->lock);
385 return 0;
386 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500387
Mike Christie5bb0b552006-04-06 21:26:46 -0500388 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700389 BUG_ON(!rc);
390
391 r2t->exp_statsn = rhdr->statsn;
392 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400393 if (r2t->data_length == 0) {
394 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
Alex Aizman7ba24712005-08-04 19:30:08 -0700395 spin_unlock(&session->lock);
396 return ISCSI_ERR_DATALEN;
397 }
398
Mike Christie98a94162006-08-31 18:09:26 -0400399 if (r2t->data_length > session->max_burst)
400 debug_scsi("invalid R2T with data len %u and max burst %u."
401 "Attempting to execute request.\n",
402 r2t->data_length, session->max_burst);
403
Alex Aizman7ba24712005-08-04 19:30:08 -0700404 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
Mike Christie857ae0b2007-05-30 12:57:15 -0500405 if (r2t->data_offset + r2t->data_length > ctask->sc->request_bufflen) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700406 spin_unlock(&session->lock);
Mike Christie98a94162006-08-31 18:09:26 -0400407 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
408 "offset %u and total length %d\n", r2t->data_length,
Mike Christie857ae0b2007-05-30 12:57:15 -0500409 r2t->data_offset, ctask->sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700410 return ISCSI_ERR_DATALEN;
411 }
412
413 r2t->ttt = rhdr->ttt; /* no flip */
414 r2t->solicit_datasn = 0;
415
416 iscsi_solicit_data_init(conn, ctask, r2t);
417
Mike Christied473cc72007-05-30 12:57:14 -0500418 tcp_ctask->exp_datasn = r2tsn + 1;
Mike Christie5bb0b552006-04-06 21:26:46 -0500419 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christie218432c2007-05-30 12:57:17 -0500420 tcp_ctask->xmstate |= XMSTATE_SOL_HDR_INIT;
Mike Christieb6c395e2006-07-24 15:47:15 -0500421 list_move_tail(&ctask->running, &conn->xmitqueue);
Alex Aizman7ba24712005-08-04 19:30:08 -0700422
Mike Christie55e32992006-01-13 18:05:53 -0600423 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -0700424 conn->r2t_pdus_cnt++;
425 spin_unlock(&session->lock);
426
427 return 0;
428}
429
430static int
Mike Christie5bb0b552006-04-06 21:26:46 -0500431iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700432{
Mike Christie5bb0b552006-04-06 21:26:46 -0500433 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700434 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700435 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500436 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
437 uint32_t cdgst, rdgst = 0, itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700438
Mike Christie5bb0b552006-04-06 21:26:46 -0500439 hdr = tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700440
441 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500442 tcp_conn->in.datalen = ntoh24(hdr->dlength);
443 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700444 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500445 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700446 return ISCSI_ERR_DATALEN;
447 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500448 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700449
450 /* read AHS */
Mike Christie5bb0b552006-04-06 21:26:46 -0500451 ahslen = hdr->hlength << 2;
452 tcp_conn->in.offset += ahslen;
453 tcp_conn->in.copy -= ahslen;
454 if (tcp_conn->in.copy < 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700455 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
Mike Christie5bb0b552006-04-06 21:26:46 -0500456 "%d bytes\n", ahslen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700457 return ISCSI_ERR_AHSLEN;
458 }
459
460 /* calculate read padding */
Mike Christie5bb0b552006-04-06 21:26:46 -0500461 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
462 if (tcp_conn->in.padding) {
463 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
464 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
Alex Aizman7ba24712005-08-04 19:30:08 -0700465 }
466
467 if (conn->hdrdgst_en) {
468 struct scatterlist sg;
469
470 sg_init_one(&sg, (u8 *)hdr,
Mike Christie5bb0b552006-04-06 21:26:46 -0500471 sizeof(struct iscsi_hdr) + ahslen);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500472 crypto_hash_digest(&tcp_conn->rx_hash, &sg, sg.length,
473 (u8 *)&cdgst);
Alex Aizman7ba24712005-08-04 19:30:08 -0700474 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
Mike Christie5bb0b552006-04-06 21:26:46 -0500475 ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600476 if (cdgst != rdgst) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500477 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
478 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
Mike Christie8a47cd32005-11-30 02:27:19 -0600479 return ISCSI_ERR_HDR_DGST;
480 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700481 }
482
Mike Christie5bb0b552006-04-06 21:26:46 -0500483 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700484 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500485 rc = iscsi_verify_itt(conn, hdr, &itt);
486 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
487 tcp_conn->in.datalen = 0; /* force drop */
488 return 0;
489 } else if (rc)
490 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700491
492 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500493 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
494 ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700495
Mike Christie5bb0b552006-04-06 21:26:46 -0500496 switch(opcode) {
497 case ISCSI_OP_SCSI_DATA_IN:
498 tcp_conn->in.ctask = session->cmds[itt];
499 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
Mike Christie275fd7d2006-07-24 15:47:17 -0500500 if (rc)
501 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500502 /* fall through */
503 case ISCSI_OP_SCSI_CMD_RSP:
504 tcp_conn->in.ctask = session->cmds[itt];
505 if (tcp_conn->in.datalen)
506 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700507
Mike Christie5bb0b552006-04-06 21:26:46 -0500508 spin_lock(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500509 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
510 spin_unlock(&session->lock);
511 break;
512 case ISCSI_OP_R2T:
513 tcp_conn->in.ctask = session->cmds[itt];
514 if (ahslen)
515 rc = ISCSI_ERR_AHSLEN;
516 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
517 DMA_TO_DEVICE)
518 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
519 else
520 rc = ISCSI_ERR_PROTO;
521 break;
522 case ISCSI_OP_LOGIN_RSP:
523 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500524 case ISCSI_OP_REJECT:
525 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500526 /*
527 * It is possible that we could get a PDU with a buffer larger
528 * than 8K, but there are no targets that currently do this.
529 * For now we fail until we find a vendor that needs it
530 */
Mike Christiebf32ed32007-02-28 17:32:17 -0600531 if (ISCSI_DEF_MAX_RECV_SEG_LEN <
Mike Christiec8dc1e52006-07-24 15:47:39 -0500532 tcp_conn->in.datalen) {
533 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
534 "but conn buffer is only %u (opcode %0x)\n",
535 tcp_conn->in.datalen,
Mike Christiebf32ed32007-02-28 17:32:17 -0600536 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
Mike Christiec8dc1e52006-07-24 15:47:39 -0500537 rc = ISCSI_ERR_PROTO;
538 break;
539 }
540
Mike Christie5bb0b552006-04-06 21:26:46 -0500541 if (tcp_conn->in.datalen)
542 goto copy_hdr;
543 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500544 case ISCSI_OP_LOGOUT_RSP:
545 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500546 case ISCSI_OP_SCSI_TMFUNC_RSP:
547 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
548 break;
549 default:
550 rc = ISCSI_ERR_BAD_OPCODE;
551 break;
552 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700553
554 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500555
556copy_hdr:
557 /*
558 * if we did zero copy for the header but we will need multiple
559 * skbs to complete the command then we have to copy the header
560 * for later use
561 */
Mike Christie275fd7d2006-07-24 15:47:17 -0500562 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
Mike Christie5bb0b552006-04-06 21:26:46 -0500563 (tcp_conn->in.datalen + tcp_conn->in.padding +
564 (conn->datadgst_en ? 4 : 0))) {
565 debug_tcp("Copying header for later use. in.copy %d in.datalen"
566 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
567 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
568 sizeof(struct iscsi_hdr));
569 tcp_conn->in.hdr = &tcp_conn->hdr;
570 tcp_conn->in.zero_copy_hdr = 0;
571 }
572 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700573}
574
575/**
576 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
Mike Christie5bb0b552006-04-06 21:26:46 -0500577 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700578 * @ctask: scsi command task
579 * @buf: buffer to copy to
580 * @buf_size: size of buffer
581 * @offset: offset within the buffer
582 *
583 * Notes:
584 * The function calls skb_copy_bits() and updates per-connection and
585 * per-cmd byte counters.
586 *
587 * Read counters (in bytes):
588 *
589 * conn->in.offset offset within in progress SKB
590 * conn->in.copy left to copy from in progress SKB
591 * including padding
592 * conn->in.copied copied already from in progress SKB
593 * conn->data_copied copied already from in progress buffer
594 * ctask->sent total bytes sent up to the MidLayer
595 * ctask->data_count left to copy from in progress Data-In
596 * buf_left left to copy from in progress buffer
597 **/
598static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500599iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -0700600 void *buf, int buf_size, int offset)
601{
Mike Christie5bb0b552006-04-06 21:26:46 -0500602 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
603 int buf_left = buf_size - (tcp_conn->data_copied + offset);
Mike Christie857ae0b2007-05-30 12:57:15 -0500604 unsigned size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700605 int rc;
606
607 size = min(size, ctask->data_count);
608
609 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500610 size, tcp_conn->in.offset, tcp_conn->in.copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700611
612 BUG_ON(size <= 0);
Mike Christie857ae0b2007-05-30 12:57:15 -0500613 BUG_ON(tcp_ctask->sent + size > ctask->sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700614
Mike Christie5bb0b552006-04-06 21:26:46 -0500615 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
616 (char*)buf + (offset + tcp_conn->data_copied), size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700617 /* must fit into skb->len */
618 BUG_ON(rc);
619
Mike Christie5bb0b552006-04-06 21:26:46 -0500620 tcp_conn->in.offset += size;
621 tcp_conn->in.copy -= size;
622 tcp_conn->in.copied += size;
623 tcp_conn->data_copied += size;
624 tcp_ctask->sent += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700625 ctask->data_count -= size;
626
Mike Christie5bb0b552006-04-06 21:26:46 -0500627 BUG_ON(tcp_conn->in.copy < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700628 BUG_ON(ctask->data_count < 0);
629
Mike Christie5bb0b552006-04-06 21:26:46 -0500630 if (buf_size != (tcp_conn->data_copied + offset)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700631 if (!ctask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500632 BUG_ON(buf_size - tcp_conn->data_copied < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700633 /* done with this PDU */
Mike Christie5bb0b552006-04-06 21:26:46 -0500634 return buf_size - tcp_conn->data_copied;
Alex Aizman7ba24712005-08-04 19:30:08 -0700635 }
636 return -EAGAIN;
637 }
638
639 /* done with this buffer or with both - PDU and buffer */
Mike Christie5bb0b552006-04-06 21:26:46 -0500640 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700641 return 0;
642}
643
644/**
645 * iscsi_tcp_copy - copy skb bits to the destanation buffer
Mike Christie5bb0b552006-04-06 21:26:46 -0500646 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700647 *
648 * Notes:
649 * The function calls skb_copy_bits() and updates per-connection
650 * byte counters.
651 **/
652static inline int
Mike Christieca518682006-08-31 18:09:32 -0400653iscsi_tcp_copy(struct iscsi_conn *conn, int buf_size)
Alex Aizman7ba24712005-08-04 19:30:08 -0700654{
Mike Christiec8dc1e52006-07-24 15:47:39 -0500655 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500656 int buf_left = buf_size - tcp_conn->data_copied;
657 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700658 int rc;
659
660 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500661 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700662 BUG_ON(size <= 0);
663
Mike Christie5bb0b552006-04-06 21:26:46 -0500664 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christiec8dc1e52006-07-24 15:47:39 -0500665 (char*)conn->data + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700666 BUG_ON(rc);
667
Mike Christie5bb0b552006-04-06 21:26:46 -0500668 tcp_conn->in.offset += size;
669 tcp_conn->in.copy -= size;
670 tcp_conn->in.copied += size;
671 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700672
Mike Christie5bb0b552006-04-06 21:26:46 -0500673 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700674 return -EAGAIN;
675
676 return 0;
677}
678
679static inline void
James Bottomleyc9802cd2006-09-23 15:33:43 -0500680partial_sg_digest_update(struct hash_desc *desc, struct scatterlist *sg,
Mike Christie62f38302006-08-31 18:09:27 -0400681 int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700682{
683 struct scatterlist temp;
684
685 memcpy(&temp, sg, sizeof(struct scatterlist));
686 temp.offset = offset;
687 temp.length = length;
James Bottomleyc9802cd2006-09-23 15:33:43 -0500688 crypto_hash_update(desc, &temp, length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700689}
690
Mike Christief6cfba12005-11-29 23:12:57 -0600691static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500692iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600693{
694 struct scatterlist tmp;
695
696 sg_init_one(&tmp, buf, len);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500697 crypto_hash_update(&tcp_conn->rx_hash, &tmp, len);
Mike Christief6cfba12005-11-29 23:12:57 -0600698}
699
Alex Aizman7ba24712005-08-04 19:30:08 -0700700static int iscsi_scsi_data_in(struct iscsi_conn *conn)
701{
Mike Christie5bb0b552006-04-06 21:26:46 -0500702 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
703 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
704 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700705 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600706 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700707 int i, offset, rc = 0;
708
709 BUG_ON((void*)ctask != sc->SCp.ptr);
710
711 /*
712 * copying Data-In into the Scsi_Cmnd
713 */
714 if (!sc->use_sg) {
715 i = ctask->data_count;
Mike Christie5bb0b552006-04-06 21:26:46 -0500716 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
717 sc->request_bufflen,
718 tcp_ctask->data_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700719 if (rc == -EAGAIN)
720 return rc;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600721 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500722 iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
723 i);
Alex Aizman7ba24712005-08-04 19:30:08 -0700724 rc = 0;
725 goto done;
726 }
727
Mike Christie5bb0b552006-04-06 21:26:46 -0500728 offset = tcp_ctask->data_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700729 sg = sc->request_buffer;
730
Mike Christie5bb0b552006-04-06 21:26:46 -0500731 if (tcp_ctask->data_offset)
732 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700733 offset -= sg[i].length;
734 /* we've passed through partial sg*/
735 if (offset < 0)
736 offset = 0;
737
Mike Christie5bb0b552006-04-06 21:26:46 -0500738 for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700739 char *dest;
740
741 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500742 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700743 sg[i].length, offset);
744 kunmap_atomic(dest, KM_SOFTIRQ0);
745 if (rc == -EAGAIN)
746 /* continue with the next SKB/PDU */
747 return rc;
748 if (!rc) {
749 if (conn->datadgst_en) {
750 if (!offset)
Herbert Xudc64ddf2006-08-24 18:45:50 +1000751 crypto_hash_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500752 &tcp_conn->rx_hash,
Arne Redlichc959e1c2006-12-17 12:10:24 -0600753 &sg[i], sg[i].length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700754 else
Mike Christie62f38302006-08-31 18:09:27 -0400755 partial_sg_digest_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500756 &tcp_conn->rx_hash,
Mike Christie5bb0b552006-04-06 21:26:46 -0500757 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700758 sg[i].offset + offset,
759 sg[i].length - offset);
760 }
761 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500762 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700763 }
764
765 if (!ctask->data_count) {
766 if (rc && conn->datadgst_en)
767 /*
768 * data-in is complete, but buffer not...
769 */
James Bottomleyc9802cd2006-09-23 15:33:43 -0500770 partial_sg_digest_update(&tcp_conn->rx_hash,
771 &sg[i],
772 sg[i].offset,
773 sg[i].length-rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700774 rc = 0;
775 break;
776 }
777
Mike Christie5bb0b552006-04-06 21:26:46 -0500778 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700779 return -EAGAIN;
780 }
781 BUG_ON(ctask->data_count);
782
783done:
784 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500785 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Mike Christieb6c395e2006-07-24 15:47:15 -0500786 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
787 (long)sc, sc->result, ctask->itt,
788 tcp_conn->in.hdr->flags);
Mike Christie5bb0b552006-04-06 21:26:46 -0500789 spin_lock(&conn->session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500790 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
791 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700792 }
793
794 return rc;
795}
796
797static int
798iscsi_data_recv(struct iscsi_conn *conn)
799{
Mike Christie5bb0b552006-04-06 21:26:46 -0500800 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
801 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700802
Mike Christie5bb0b552006-04-06 21:26:46 -0500803 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
804 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700805 case ISCSI_OP_SCSI_DATA_IN:
806 rc = iscsi_scsi_data_in(conn);
807 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500808 case ISCSI_OP_SCSI_CMD_RSP:
Alex Aizman7ba24712005-08-04 19:30:08 -0700809 case ISCSI_OP_TEXT_RSP:
810 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500811 case ISCSI_OP_ASYNC_EVENT:
812 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700813 /*
814 * Collect data segment to the connection's data
815 * placeholder
816 */
Mike Christieca518682006-08-31 18:09:32 -0400817 if (iscsi_tcp_copy(conn, tcp_conn->in.datalen)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700818 rc = -EAGAIN;
819 goto exit;
820 }
821
Mike Christiec8dc1e52006-07-24 15:47:39 -0500822 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500823 tcp_conn->in.datalen);
824 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
Mike Christiec8dc1e52006-07-24 15:47:39 -0500825 iscsi_recv_digest_update(tcp_conn, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500826 tcp_conn->in.datalen);
827 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700828 default:
829 BUG_ON(1);
830 }
831exit:
832 return rc;
833}
834
835/**
836 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
837 * @rd_desc: read descriptor
838 * @skb: socket buffer
839 * @offset: offset in skb
840 * @len: skb->len - offset
841 **/
842static int
843iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
844 unsigned int offset, size_t len)
845{
846 int rc;
847 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500848 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700849 int processed;
850 char pad[ISCSI_PAD_LEN];
851 struct scatterlist sg;
852
853 /*
854 * Save current SKB and its offset in the corresponding
855 * connection context.
856 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500857 tcp_conn->in.copy = skb->len - offset;
858 tcp_conn->in.offset = offset;
859 tcp_conn->in.skb = skb;
860 tcp_conn->in.len = tcp_conn->in.copy;
861 BUG_ON(tcp_conn->in.copy <= 0);
862 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700863
864more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500865 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700866 rc = 0;
867
868 if (unlikely(conn->suspend_rx)) {
869 debug_tcp("conn %d Rx suspended!\n", conn->id);
870 return 0;
871 }
872
Mike Christie5bb0b552006-04-06 21:26:46 -0500873 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
874 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
875 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700876 if (rc) {
877 if (rc == -EAGAIN)
878 goto nomore;
879 else {
Mike Christied82967c2006-07-24 15:47:11 -0500880 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700881 return 0;
882 }
883 }
884
885 /*
886 * Verify and process incoming PDU header.
887 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500888 rc = iscsi_tcp_hdr_recv(conn);
889 if (!rc && tcp_conn->in.datalen) {
Mike Christiedd8c0d92006-08-31 18:09:28 -0400890 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -0500891 crypto_hash_init(&tcp_conn->rx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -0500892 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700893 } else if (rc) {
Mike Christie40527af2006-07-24 15:47:45 -0500894 iscsi_conn_failure(conn, rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700895 return 0;
896 }
897 }
898
Mike Christiedbdb0162007-05-30 12:57:20 -0500899 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV &&
900 tcp_conn->in.copy) {
Mike Christief6cfba12005-11-29 23:12:57 -0600901 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500902
Alex Aizman7ba24712005-08-04 19:30:08 -0700903 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500904 tcp_conn->in.offset, tcp_conn->in.copy);
Mike Christiedbdb0162007-05-30 12:57:20 -0500905
906 if (!tcp_conn->data_copied) {
907 if (tcp_conn->in.padding) {
908 debug_tcp("padding -> %d\n",
909 tcp_conn->in.padding);
910 memset(pad, 0, tcp_conn->in.padding);
911 sg_init_one(&sg, pad, tcp_conn->in.padding);
912 crypto_hash_update(&tcp_conn->rx_hash,
913 &sg, sg.length);
914 }
915 crypto_hash_final(&tcp_conn->rx_hash,
916 (u8 *) &tcp_conn->in.datadgst);
917 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
918 }
919
Mike Christieca518682006-08-31 18:09:32 -0400920 rc = iscsi_tcp_copy(conn, sizeof(uint32_t));
921 if (rc) {
922 if (rc == -EAGAIN)
923 goto again;
924 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
925 return 0;
926 }
927
928 memcpy(&recv_digest, conn->data, sizeof(uint32_t));
Mike Christie5bb0b552006-04-06 21:26:46 -0500929 if (recv_digest != tcp_conn->in.datadgst) {
Mike Christief6cfba12005-11-29 23:12:57 -0600930 debug_tcp("iscsi_tcp: data digest error!"
931 "0x%x != 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500932 tcp_conn->in.datadgst);
Mike Christief6cfba12005-11-29 23:12:57 -0600933 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
934 return 0;
935 } else {
936 debug_tcp("iscsi_tcp: data digest match!"
937 "0x%x == 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500938 tcp_conn->in.datadgst);
939 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700940 }
941 }
942
Mike Christie5bb0b552006-04-06 21:26:46 -0500943 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
Mike Christiedbdb0162007-05-30 12:57:20 -0500944 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700945 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500946 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700947
948 rc = iscsi_data_recv(conn);
949 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500950 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700951 goto again;
Mike Christied82967c2006-07-24 15:47:11 -0500952 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700953 return 0;
954 }
Mike Christiedbdb0162007-05-30 12:57:20 -0500955
956 if (tcp_conn->in.padding)
957 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
958 else if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500959 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Mike Christiedbdb0162007-05-30 12:57:20 -0500960 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500961 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Mike Christiedbdb0162007-05-30 12:57:20 -0500962 tcp_conn->data_copied = 0;
963 }
964
965 if (tcp_conn->in_progress == IN_PROGRESS_PAD_RECV &&
966 tcp_conn->in.copy) {
967 int copylen = min(tcp_conn->in.padding - tcp_conn->data_copied,
968 tcp_conn->in.copy);
969
970 tcp_conn->in.copy -= copylen;
971 tcp_conn->in.offset += copylen;
972 tcp_conn->data_copied += copylen;
973
974 if (tcp_conn->data_copied != tcp_conn->in.padding)
975 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
976 else if (conn->datadgst_en)
977 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
978 else
979 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
980 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700981 }
982
983 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500984 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
985 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700986
Mike Christie5bb0b552006-04-06 21:26:46 -0500987 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700988 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500989 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700990 goto more;
991 }
992
993nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500994 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700995 BUG_ON(processed == 0);
996 return processed;
997
998again:
Mike Christie5bb0b552006-04-06 21:26:46 -0500999 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -07001000 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
1001 processed, (int)len, (int)rd_desc->count);
1002 BUG_ON(processed == 0);
1003 BUG_ON(processed > len);
1004
1005 conn->rxdata_octets += processed;
1006 return processed;
1007}
1008
1009static void
1010iscsi_tcp_data_ready(struct sock *sk, int flag)
1011{
1012 struct iscsi_conn *conn = sk->sk_user_data;
1013 read_descriptor_t rd_desc;
1014
1015 read_lock(&sk->sk_callback_lock);
1016
Mike Christie665b44a2006-05-02 19:46:49 -05001017 /*
1018 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
1019 * We set count to 1 because we want the network layer to
1020 * hand us all the skbs that are available. iscsi_tcp_data_recv
1021 * handled pdus that cross buffers or pdus that still need data.
1022 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001023 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -05001024 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001025 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
1026
1027 read_unlock(&sk->sk_callback_lock);
1028}
1029
1030static void
1031iscsi_tcp_state_change(struct sock *sk)
1032{
Mike Christie5bb0b552006-04-06 21:26:46 -05001033 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001034 struct iscsi_conn *conn;
1035 struct iscsi_session *session;
1036 void (*old_state_change)(struct sock *);
1037
1038 read_lock(&sk->sk_callback_lock);
1039
1040 conn = (struct iscsi_conn*)sk->sk_user_data;
1041 session = conn->session;
1042
Mike Christiee6273992005-11-29 23:12:49 -06001043 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1044 sk->sk_state == TCP_CLOSE) &&
1045 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001046 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1047 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1048 }
1049
Mike Christie5bb0b552006-04-06 21:26:46 -05001050 tcp_conn = conn->dd_data;
1051 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001052
1053 read_unlock(&sk->sk_callback_lock);
1054
1055 old_state_change(sk);
1056}
1057
1058/**
1059 * iscsi_write_space - Called when more output buffer space is available
1060 * @sk: socket space is available for
1061 **/
1062static void
1063iscsi_write_space(struct sock *sk)
1064{
1065 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001066 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1067
1068 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001069 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001070 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001071}
1072
1073static void
1074iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1075{
Mike Christie5bb0b552006-04-06 21:26:46 -05001076 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1077 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001078
1079 /* assign new callbacks */
1080 write_lock_bh(&sk->sk_callback_lock);
1081 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001082 tcp_conn->old_data_ready = sk->sk_data_ready;
1083 tcp_conn->old_state_change = sk->sk_state_change;
1084 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001085 sk->sk_data_ready = iscsi_tcp_data_ready;
1086 sk->sk_state_change = iscsi_tcp_state_change;
1087 sk->sk_write_space = iscsi_write_space;
1088 write_unlock_bh(&sk->sk_callback_lock);
1089}
1090
1091static void
Mike Christie1c834692006-07-24 15:47:26 -05001092iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001093{
Mike Christie5bb0b552006-04-06 21:26:46 -05001094 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001095
1096 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1097 write_lock_bh(&sk->sk_callback_lock);
1098 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001099 sk->sk_data_ready = tcp_conn->old_data_ready;
1100 sk->sk_state_change = tcp_conn->old_state_change;
1101 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001102 sk->sk_no_check = 0;
1103 write_unlock_bh(&sk->sk_callback_lock);
1104}
1105
1106/**
1107 * iscsi_send - generic send routine
1108 * @sk: kernel's socket
1109 * @buf: buffer to write from
1110 * @size: actual size to write
1111 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001112 */
1113static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001114iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001115{
Mike Christie5bb0b552006-04-06 21:26:46 -05001116 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1117 struct socket *sk = tcp_conn->sock;
Mike Christie3219e522006-05-30 00:37:28 -05001118 int offset = buf->sg.offset + buf->sent, res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001119
Mike Christie7cae5152006-01-13 18:05:47 -06001120 /*
1121 * if we got use_sg=0 or are sending something we kmallocd
1122 * then we did not have to do kmap (kmap returns page_address)
1123 *
1124 * if we got use_sg > 0, but had to drop down, we do not
1125 * set clustering so this should only happen for that
1126 * slab case.
1127 */
1128 if (buf->use_sendmsg)
Mike Christie3219e522006-05-30 00:37:28 -05001129 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
Mike Christie7cae5152006-01-13 18:05:47 -06001130 else
Mike Christie3219e522006-05-30 00:37:28 -05001131 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1132
1133 if (res >= 0) {
1134 conn->txdata_octets += res;
1135 buf->sent += res;
1136 return res;
1137 }
1138
1139 tcp_conn->sendpage_failures_cnt++;
1140 if (res == -EAGAIN)
1141 res = -ENOBUFS;
1142 else
1143 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1144 return res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001145}
1146
1147/**
1148 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1149 * @conn: iscsi connection
1150 * @buf: buffer to write from
1151 * @datalen: lenght of data to be sent after the header
1152 *
1153 * Notes:
1154 * (Tx, Fast Path)
1155 **/
1156static inline int
1157iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1158{
Alex Aizman7ba24712005-08-04 19:30:08 -07001159 int flags = 0; /* MSG_DONTWAIT; */
1160 int res, size;
1161
1162 size = buf->sg.length - buf->sent;
1163 BUG_ON(buf->sent + size > buf->sg.length);
1164 if (buf->sent + size != buf->sg.length || datalen)
1165 flags |= MSG_MORE;
1166
FUJITA Tomonori56851692006-01-13 18:05:44 -06001167 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001168 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1169 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001170 if (size != res)
1171 return -EAGAIN;
1172 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001173 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001174
1175 return res;
1176}
1177
1178/**
1179 * iscsi_sendpage - send one page of iSCSI Data-Out.
1180 * @conn: iscsi connection
1181 * @buf: buffer to write from
1182 * @count: remaining data
1183 * @sent: number of bytes sent
1184 *
1185 * Notes:
1186 * (Tx, Fast Path)
1187 **/
1188static inline int
1189iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1190 int *count, int *sent)
1191{
Alex Aizman7ba24712005-08-04 19:30:08 -07001192 int flags = 0; /* MSG_DONTWAIT; */
1193 int res, size;
1194
1195 size = buf->sg.length - buf->sent;
1196 BUG_ON(buf->sent + size > buf->sg.length);
1197 if (size > *count)
1198 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001199 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001200 flags |= MSG_MORE;
1201
FUJITA Tomonori56851692006-01-13 18:05:44 -06001202 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001203 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1204 size, buf->sent, *count, *sent, res);
1205 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001206 *count -= res;
1207 *sent += res;
1208 if (size != res)
1209 return -EAGAIN;
1210 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001211 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001212
1213 return res;
1214}
1215
1216static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001217iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
Mike Christie62f38302006-08-31 18:09:27 -04001218 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001219{
James Bottomleyc9802cd2006-09-23 15:33:43 -05001220 crypto_hash_init(&tcp_conn->tx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -05001221 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001222}
1223
Alex Aizman7ba24712005-08-04 19:30:08 -07001224/**
1225 * iscsi_solicit_data_cont - initialize next Data-Out
1226 * @conn: iscsi connection
1227 * @ctask: scsi command task
1228 * @r2t: R2T info
1229 * @left: bytes left to transfer
1230 *
1231 * Notes:
1232 * Initialize next Data-Out within this R2T sequence and continue
1233 * to process next Scatter-Gather element(if any) of this SCSI command.
1234 *
1235 * Called under connection lock.
1236 **/
1237static void
1238iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1239 struct iscsi_r2t_info *r2t, int left)
1240{
1241 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001242 struct scsi_cmnd *sc = ctask->sc;
1243 int new_offset;
1244
Mike Christieffbfe922006-05-18 20:31:36 -05001245 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001246 memset(hdr, 0, sizeof(struct iscsi_data));
1247 hdr->ttt = r2t->ttt;
1248 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1249 r2t->solicit_datasn++;
1250 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001251 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1252 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001253 hdr->exp_statsn = r2t->exp_statsn;
1254 new_offset = r2t->data_offset + r2t->sent;
1255 hdr->offset = cpu_to_be32(new_offset);
1256 if (left > conn->max_xmit_dlength) {
1257 hton24(hdr->dlength, conn->max_xmit_dlength);
1258 r2t->data_count = conn->max_xmit_dlength;
1259 } else {
1260 hton24(hdr->dlength, left);
1261 r2t->data_count = left;
1262 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1263 }
1264 conn->dataout_pdus_cnt++;
1265
Mike Christie6e458cc2006-05-18 20:31:31 -05001266 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001267 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001268
Mike Christie62f38302006-08-31 18:09:27 -04001269 if (iscsi_buf_left(&r2t->sendbuf))
1270 return;
1271
1272 if (sc->use_sg) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001273 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1274 r2t->sg += 1;
Mike Christie62f38302006-08-31 18:09:27 -04001275 } else {
1276 iscsi_buf_init_iov(&r2t->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -07001277 (char*)sc->request_buffer + new_offset,
1278 r2t->data_count);
Mike Christie62f38302006-08-31 18:09:27 -04001279 r2t->sg = NULL;
1280 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001281}
1282
Mike Christie62f38302006-08-31 18:09:27 -04001283static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask,
1284 unsigned long len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001285{
Mike Christie62f38302006-08-31 18:09:27 -04001286 tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1);
1287 if (!tcp_ctask->pad_count)
1288 return;
Alex Aizman7ba24712005-08-04 19:30:08 -07001289
Mike Christie62f38302006-08-31 18:09:27 -04001290 tcp_ctask->pad_count = ISCSI_PAD_LEN - tcp_ctask->pad_count;
1291 debug_scsi("write padding %d bytes\n", tcp_ctask->pad_count);
1292 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Alex Aizman7ba24712005-08-04 19:30:08 -07001293}
1294
1295/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001296 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001297 * @conn: iscsi connection
1298 * @ctask: scsi command task
1299 * @sc: scsi command
1300 **/
1301static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001302iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001303{
Mike Christie5bb0b552006-04-06 21:26:46 -05001304 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001305
Mike Christie5bb0b552006-04-06 21:26:46 -05001306 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Mike Christie218432c2007-05-30 12:57:17 -05001307 tcp_ctask->xmstate = XMSTATE_CMD_HDR_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001308}
1309
1310/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001311 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001312 * @conn: iscsi connection
1313 * @mtask: task management task
1314 *
1315 * Notes:
1316 * The function can return -EAGAIN in which case caller must
1317 * call it again later, or recover. '0' return code means successful
1318 * xmit.
1319 *
Mike Christie218432c2007-05-30 12:57:17 -05001320 * Management xmit state machine consists of these states:
1321 * XMSTATE_IMM_HDR_INIT - calculate digest of PDU Header
1322 * XMSTATE_IMM_HDR - PDU Header xmit in progress
1323 * XMSTATE_IMM_DATA - PDU Data xmit in progress
1324 * XMSTATE_IDLE - management PDU is done
Alex Aizman7ba24712005-08-04 19:30:08 -07001325 **/
1326static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001327iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001328{
Mike Christie5bb0b552006-04-06 21:26:46 -05001329 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001330 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001331
1332 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001333 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001334
Mike Christie218432c2007-05-30 12:57:17 -05001335 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR_INIT) {
1336 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
1337 sizeof(struct iscsi_hdr));
1338
1339 if (mtask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001340 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie218432c2007-05-30 12:57:17 -05001341 iscsi_buf_init_iov(&tcp_mtask->sendbuf,
1342 (char*)mtask->data,
1343 mtask->data_count);
1344 }
1345
Mike Christieaf973482005-09-12 21:01:32 -05001346 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001347 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001348 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001349 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1350 (u8*)tcp_mtask->hdrext);
Mike Christie218432c2007-05-30 12:57:17 -05001351
1352 tcp_mtask->sent = 0;
1353 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR_INIT;
1354 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
1355 }
1356
1357 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
Mike Christie3219e522006-05-30 00:37:28 -05001358 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1359 mtask->data_count);
Mike Christie218432c2007-05-30 12:57:17 -05001360 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001361 return rc;
Mike Christie218432c2007-05-30 12:57:17 -05001362 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001363 }
1364
Mike Christie5bb0b552006-04-06 21:26:46 -05001365 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001366 BUG_ON(!mtask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001367 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001368 /* FIXME: implement.
1369 * Virtual buffer could be spreaded across multiple pages...
1370 */
1371 do {
Mike Christie3219e522006-05-30 00:37:28 -05001372 int rc;
1373
1374 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1375 &mtask->data_count, &tcp_mtask->sent);
1376 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001377 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001378 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001379 }
1380 } while (mtask->data_count);
1381 }
1382
Mike Christie5bb0b552006-04-06 21:26:46 -05001383 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
Al Virob4377352007-02-09 16:39:40 +00001384 if (mtask->hdr->itt == RESERVED_ITT) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001385 struct iscsi_session *session = conn->session;
1386
1387 spin_lock_bh(&session->lock);
1388 list_del(&conn->mtask->running);
1389 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1390 sizeof(void*));
1391 spin_unlock_bh(&session->lock);
1392 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001393 return 0;
1394}
1395
Mike Christie218432c2007-05-30 12:57:17 -05001396static int
1397iscsi_send_cmd_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001398{
Mike Christie218432c2007-05-30 12:57:17 -05001399 struct scsi_cmnd *sc = ctask->sc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001400 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie218432c2007-05-30 12:57:17 -05001401 int rc = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -05001402
Mike Christie218432c2007-05-30 12:57:17 -05001403 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_INIT) {
1404 tcp_ctask->sent = 0;
1405 tcp_ctask->sg_count = 0;
1406 tcp_ctask->exp_datasn = 0;
Mike Christie3219e522006-05-30 00:37:28 -05001407
Mike Christie218432c2007-05-30 12:57:17 -05001408 if (sc->sc_data_direction == DMA_TO_DEVICE) {
1409 if (sc->use_sg) {
1410 struct scatterlist *sg = sc->request_buffer;
Alex Aizman7ba24712005-08-04 19:30:08 -07001411
Mike Christie218432c2007-05-30 12:57:17 -05001412 iscsi_buf_init_sg(&tcp_ctask->sendbuf, sg);
1413 tcp_ctask->sg = sg + 1;
1414 tcp_ctask->bad_sg = sg + sc->use_sg;
1415 } else {
1416 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1417 sc->request_buffer,
1418 sc->request_bufflen);
1419 tcp_ctask->sg = NULL;
1420 tcp_ctask->bad_sg = NULL;
1421 }
1422
1423 debug_scsi("cmd [itt 0x%x total %d imm_data %d "
1424 "unsol count %d, unsol offset %d]\n",
1425 ctask->itt, sc->request_bufflen,
1426 ctask->imm_count, ctask->unsol_count,
1427 ctask->unsol_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -07001428 }
Mike Christie218432c2007-05-30 12:57:17 -05001429
1430 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
1431 sizeof(struct iscsi_hdr));
1432
1433 if (conn->hdrdgst_en)
1434 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1435 (u8*)tcp_ctask->hdrext);
1436 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_INIT;
1437 tcp_ctask->xmstate |= XMSTATE_CMD_HDR_XMIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001438 }
1439
Mike Christie218432c2007-05-30 12:57:17 -05001440 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_XMIT) {
1441 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1442 if (rc)
1443 return rc;
1444 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_XMIT;
1445
1446 if (sc->sc_data_direction != DMA_TO_DEVICE)
1447 return 0;
1448
1449 if (ctask->imm_count) {
1450 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1451 iscsi_set_padding(tcp_ctask, ctask->imm_count);
1452
1453 if (ctask->conn->datadgst_en) {
1454 iscsi_data_digest_init(ctask->conn->dd_data,
1455 tcp_ctask);
1456 tcp_ctask->immdigest = 0;
1457 }
1458 }
1459
1460 if (ctask->unsol_count)
1461 tcp_ctask->xmstate |=
1462 XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
1463 }
1464 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001465}
1466
Mike Christie62f38302006-08-31 18:09:27 -04001467static int
1468iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1469{
1470 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1471 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1472 int sent = 0, rc;
1473
1474 if (tcp_ctask->xmstate & XMSTATE_W_PAD) {
1475 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
1476 tcp_ctask->pad_count);
1477 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001478 crypto_hash_update(&tcp_conn->tx_hash,
1479 &tcp_ctask->sendbuf.sg,
1480 tcp_ctask->sendbuf.sg.length);
Mike Christie62f38302006-08-31 18:09:27 -04001481 } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD))
1482 return 0;
1483
1484 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
1485 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_PAD;
1486 debug_scsi("sending %d pad bytes for itt 0x%x\n",
1487 tcp_ctask->pad_count, ctask->itt);
1488 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1489 &sent);
1490 if (rc) {
1491 debug_scsi("padding send failed %d\n", rc);
1492 tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD;
1493 }
1494 return rc;
1495}
1496
1497static int
1498iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1499 struct iscsi_buf *buf, uint32_t *digest)
1500{
1501 struct iscsi_tcp_cmd_task *tcp_ctask;
1502 struct iscsi_tcp_conn *tcp_conn;
1503 int rc, sent = 0;
1504
1505 if (!conn->datadgst_en)
1506 return 0;
1507
1508 tcp_ctask = ctask->dd_data;
1509 tcp_conn = conn->dd_data;
1510
1511 if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) {
James Bottomleyc9802cd2006-09-23 15:33:43 -05001512 crypto_hash_final(&tcp_conn->tx_hash, (u8*)digest);
Mike Christie62f38302006-08-31 18:09:27 -04001513 iscsi_buf_init_iov(buf, (char*)digest, 4);
1514 }
1515 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST;
1516
1517 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
1518 if (!rc)
1519 debug_scsi("sent digest 0x%x for itt 0x%x\n", *digest,
1520 ctask->itt);
1521 else {
1522 debug_scsi("sending digest 0x%x failed for itt 0x%x!\n",
1523 *digest, ctask->itt);
1524 tcp_ctask->xmstate |= XMSTATE_W_RESEND_DATA_DIGEST;
1525 }
1526 return rc;
1527}
1528
1529static int
1530iscsi_send_data(struct iscsi_cmd_task *ctask, struct iscsi_buf *sendbuf,
1531 struct scatterlist **sg, int *sent, int *count,
1532 struct iscsi_buf *digestbuf, uint32_t *digest)
1533{
1534 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1535 struct iscsi_conn *conn = ctask->conn;
1536 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1537 int rc, buf_sent, offset;
1538
1539 while (*count) {
1540 buf_sent = 0;
1541 offset = sendbuf->sent;
1542
1543 rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent);
1544 *sent = *sent + buf_sent;
1545 if (buf_sent && conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001546 partial_sg_digest_update(&tcp_conn->tx_hash,
Mike Christie62f38302006-08-31 18:09:27 -04001547 &sendbuf->sg, sendbuf->sg.offset + offset,
1548 buf_sent);
1549 if (!iscsi_buf_left(sendbuf) && *sg != tcp_ctask->bad_sg) {
1550 iscsi_buf_init_sg(sendbuf, *sg);
1551 *sg = *sg + 1;
1552 }
1553
1554 if (rc)
1555 return rc;
1556 }
1557
1558 rc = iscsi_send_padding(conn, ctask);
1559 if (rc)
1560 return rc;
1561
1562 return iscsi_send_digest(conn, ctask, digestbuf, digest);
1563}
1564
1565static int
1566iscsi_send_unsol_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001567{
Mike Christie5bb0b552006-04-06 21:26:46 -05001568 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001569 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001570 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001571
Mike Christie5bb0b552006-04-06 21:26:46 -05001572 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1573 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Mike Christie62f38302006-08-31 18:09:27 -04001574 dtask = &tcp_ctask->unsol_dtask;
1575
Mike Christieffd04362006-08-31 18:09:24 -04001576 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
1577 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
1578 sizeof(struct iscsi_hdr));
Mike Christieaf973482005-09-12 21:01:32 -05001579 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001580 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001581 (u8*)dtask->hdrext);
Mike Christie62f38302006-08-31 18:09:27 -04001582
Mike Christie5bb0b552006-04-06 21:26:46 -05001583 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Mike Christie62f38302006-08-31 18:09:27 -04001584 iscsi_set_padding(tcp_ctask, ctask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001585 }
Mike Christie3219e522006-05-30 00:37:28 -05001586
1587 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1588 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001589 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1590 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001591 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001592 }
1593
Mike Christiedd8c0d92006-08-31 18:09:28 -04001594 if (conn->datadgst_en) {
1595 dtask = &tcp_ctask->unsol_dtask;
1596 iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
1597 dtask->digest = 0;
1598 }
1599
Alex Aizman7ba24712005-08-04 19:30:08 -07001600 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001601 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001602 return 0;
1603}
1604
Mike Christie62f38302006-08-31 18:09:27 -04001605static int
1606iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001607{
Mike Christie5bb0b552006-04-06 21:26:46 -05001608 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001609 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001610
Mike Christie62f38302006-08-31 18:09:27 -04001611 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
1612 BUG_ON(!ctask->unsol_count);
1613 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
1614send_hdr:
1615 rc = iscsi_send_unsol_hdr(conn, ctask);
1616 if (rc)
1617 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001618 }
1619
Mike Christie62f38302006-08-31 18:09:27 -04001620 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
1621 struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask;
Mike Christie5bb0b552006-04-06 21:26:46 -05001622 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001623
Mike Christie62f38302006-08-31 18:09:27 -04001624 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1625 &tcp_ctask->sent, &ctask->data_count,
1626 &dtask->digestbuf, &dtask->digest);
Mike Christie5bb0b552006-04-06 21:26:46 -05001627 ctask->unsol_count -= tcp_ctask->sent - start;
Mike Christie62f38302006-08-31 18:09:27 -04001628 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001629 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001630 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1631 /*
1632 * Done with the Data-Out. Next, check if we need
1633 * to send another unsolicited Data-Out.
1634 */
1635 if (ctask->unsol_count) {
1636 debug_scsi("sending more uns\n");
1637 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
1638 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001639 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001640 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001641 return 0;
1642}
1643
Mike Christie62f38302006-08-31 18:09:27 -04001644static int iscsi_send_sol_pdu(struct iscsi_conn *conn,
1645 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001646{
Mike Christie5bb0b552006-04-06 21:26:46 -05001647 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie62f38302006-08-31 18:09:27 -04001648 struct iscsi_session *session = conn->session;
1649 struct iscsi_r2t_info *r2t;
1650 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001651 int left, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001652
Mike Christie218432c2007-05-30 12:57:17 -05001653 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR_INIT) {
Mike Christiedb37c502006-11-08 15:58:33 -06001654 if (!tcp_ctask->r2t) {
1655 spin_lock_bh(&session->lock);
Mike Christie62f38302006-08-31 18:09:27 -04001656 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1657 sizeof(void*));
Mike Christiedb37c502006-11-08 15:58:33 -06001658 spin_unlock_bh(&session->lock);
1659 }
Mike Christie62f38302006-08-31 18:09:27 -04001660send_hdr:
1661 r2t = tcp_ctask->r2t;
1662 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001663
Mike Christie62f38302006-08-31 18:09:27 -04001664 if (conn->hdrdgst_en)
1665 iscsi_hdr_digest(conn, &r2t->headbuf,
1666 (u8*)dtask->hdrext);
Mike Christie218432c2007-05-30 12:57:17 -05001667 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR_INIT;
1668 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
1669 }
1670
1671 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
1672 r2t = tcp_ctask->r2t;
1673 dtask = &r2t->dtask;
1674
Mike Christie62f38302006-08-31 18:09:27 -04001675 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
Mike Christie218432c2007-05-30 12:57:17 -05001676 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001677 return rc;
Mike Christie218432c2007-05-30 12:57:17 -05001678 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1679 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001680
Mike Christiedd8c0d92006-08-31 18:09:28 -04001681 if (conn->datadgst_en) {
1682 iscsi_data_digest_init(conn->dd_data, tcp_ctask);
1683 dtask->digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001684 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001685
Mike Christie62f38302006-08-31 18:09:27 -04001686 iscsi_set_padding(tcp_ctask, r2t->data_count);
1687 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1688 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1689 r2t->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001690 }
1691
Mike Christie62f38302006-08-31 18:09:27 -04001692 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
1693 r2t = tcp_ctask->r2t;
1694 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001695
Mike Christie62f38302006-08-31 18:09:27 -04001696 rc = iscsi_send_data(ctask, &r2t->sendbuf, &r2t->sg,
1697 &r2t->sent, &r2t->data_count,
1698 &dtask->digestbuf, &dtask->digest);
1699 if (rc)
1700 return rc;
1701 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001702
Mike Christie62f38302006-08-31 18:09:27 -04001703 /*
1704 * Done with this Data-Out. Next, check if we have
1705 * to send another Data-Out for this R2T.
1706 */
1707 BUG_ON(r2t->data_length - r2t->sent < 0);
1708 left = r2t->data_length - r2t->sent;
1709 if (left) {
1710 iscsi_solicit_data_cont(conn, ctask, r2t, left);
Mike Christie62f38302006-08-31 18:09:27 -04001711 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001712 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001713
Mike Christie62f38302006-08-31 18:09:27 -04001714 /*
1715 * Done with this R2T. Check if there are more
1716 * outstanding R2Ts ready to be processed.
1717 */
1718 spin_lock_bh(&session->lock);
1719 tcp_ctask->r2t = NULL;
1720 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
1721 sizeof(void*));
1722 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t,
1723 sizeof(void*))) {
1724 tcp_ctask->r2t = r2t;
Mike Christie62f38302006-08-31 18:09:27 -04001725 spin_unlock_bh(&session->lock);
1726 goto send_hdr;
1727 }
1728 spin_unlock_bh(&session->lock);
1729 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001730 return 0;
1731}
1732
Mike Christie218432c2007-05-30 12:57:17 -05001733/**
1734 * iscsi_tcp_ctask_xmit - xmit normal PDU task
1735 * @conn: iscsi connection
1736 * @ctask: iscsi command task
1737 *
1738 * Notes:
1739 * The function can return -EAGAIN in which case caller must
1740 * call it again later, or recover. '0' return code means successful
1741 * xmit.
1742 * The function is devided to logical helpers (above) for the different
1743 * xmit stages.
1744 *
1745 *iscsi_send_cmd_hdr()
1746 * XMSTATE_CMD_HDR_INIT - prepare Header and Data buffers Calculate
1747 * Header Digest
1748 * XMSTATE_CMD_HDR_XMIT - Transmit header in progress
1749 *
1750 *iscsi_send_padding
1751 * XMSTATE_W_PAD - Prepare and send pading
1752 * XMSTATE_W_RESEND_PAD - retry send pading
1753 *
1754 *iscsi_send_digest
1755 * XMSTATE_W_RESEND_DATA_DIGEST - Finalize and send Data Digest
1756 * XMSTATE_W_RESEND_DATA_DIGEST - retry sending digest
1757 *
1758 *iscsi_send_unsol_hdr
1759 * XMSTATE_UNS_INIT - prepare un-solicit data header and digest
1760 * XMSTATE_UNS_HDR - send un-solicit header
1761 *
1762 *iscsi_send_unsol_pdu
1763 * XMSTATE_UNS_DATA - send un-solicit data in progress
1764 *
1765 *iscsi_send_sol_pdu
1766 * XMSTATE_SOL_HDR_INIT - solicit data header and digest initialize
1767 * XMSTATE_SOL_HDR - send solicit header
1768 * XMSTATE_SOL_DATA - send solicit data
1769 *
1770 *iscsi_tcp_ctask_xmit
1771 * XMSTATE_IMM_DATA - xmit managment data (??)
1772 **/
Alex Aizman7ba24712005-08-04 19:30:08 -07001773static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001774iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001775{
Mike Christie5bb0b552006-04-06 21:26:46 -05001776 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001777 int rc = 0;
1778
1779 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001780 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001781
Mike Christie218432c2007-05-30 12:57:17 -05001782 rc = iscsi_send_cmd_hdr(conn, ctask);
1783 if (rc)
1784 return rc;
1785 if (ctask->sc->sc_data_direction != DMA_TO_DEVICE)
1786 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001787
Mike Christie5bb0b552006-04-06 21:26:46 -05001788 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Mike Christie62f38302006-08-31 18:09:27 -04001789 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1790 &tcp_ctask->sent, &ctask->imm_count,
1791 &tcp_ctask->immbuf, &tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001792 if (rc)
1793 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001794 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001795 }
1796
Mike Christie62f38302006-08-31 18:09:27 -04001797 rc = iscsi_send_unsol_pdu(conn, ctask);
1798 if (rc)
1799 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001800
Mike Christie62f38302006-08-31 18:09:27 -04001801 rc = iscsi_send_sol_pdu(conn, ctask);
1802 if (rc)
1803 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001804
1805 return rc;
1806}
1807
Mike Christie7b8631b2006-01-13 18:05:50 -06001808static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001809iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001810{
Mike Christie7b8631b2006-01-13 18:05:50 -06001811 struct iscsi_conn *conn;
1812 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001813 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001814
Mike Christie5bb0b552006-04-06 21:26:46 -05001815 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001816 if (!cls_conn)
1817 return NULL;
1818 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001819 /*
1820 * due to strange issues with iser these are not set
1821 * in iscsi_conn_setup
1822 */
Mike Christiebf32ed32007-02-28 17:32:17 -06001823 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
Alex Aizman7ba24712005-08-04 19:30:08 -07001824
Mike Christie5bb0b552006-04-06 21:26:46 -05001825 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1826 if (!tcp_conn)
1827 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001828
Mike Christie5bb0b552006-04-06 21:26:46 -05001829 conn->dd_data = tcp_conn;
1830 tcp_conn->iscsi_conn = conn;
1831 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1832 /* initial operational parameters */
1833 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07001834
James Bottomleyc9802cd2006-09-23 15:33:43 -05001835 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1836 CRYPTO_ALG_ASYNC);
1837 tcp_conn->tx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001838 if (IS_ERR(tcp_conn->tx_hash.tfm)) {
1839 printk(KERN_ERR "Could not create connection due to crc32c "
1840 "loading error %ld. Make sure the crc32c module is "
1841 "built as a module or into the kernel\n",
1842 PTR_ERR(tcp_conn->tx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001843 goto free_tcp_conn;
Mike Christie0f238412007-02-28 17:32:21 -06001844 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001845
James Bottomleyc9802cd2006-09-23 15:33:43 -05001846 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1847 CRYPTO_ALG_ASYNC);
1848 tcp_conn->rx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001849 if (IS_ERR(tcp_conn->rx_hash.tfm)) {
1850 printk(KERN_ERR "Could not create connection due to crc32c "
1851 "loading error %ld. Make sure the crc32c module is "
1852 "built as a module or into the kernel\n",
1853 PTR_ERR(tcp_conn->rx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001854 goto free_tx_tfm;
Mike Christie0f238412007-02-28 17:32:21 -06001855 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001856
Mike Christie7b8631b2006-01-13 18:05:50 -06001857 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001858
Mike Christiedd8c0d92006-08-31 18:09:28 -04001859free_tx_tfm:
James Bottomleyc9802cd2006-09-23 15:33:43 -05001860 crypto_free_hash(tcp_conn->tx_hash.tfm);
Mike Christiedd8c0d92006-08-31 18:09:28 -04001861free_tcp_conn:
1862 kfree(tcp_conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001863tcp_conn_alloc_fail:
1864 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001865 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001866}
1867
1868static void
Mike Christie1c834692006-07-24 15:47:26 -05001869iscsi_tcp_release_conn(struct iscsi_conn *conn)
1870{
1871 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1872
1873 if (!tcp_conn->sock)
1874 return;
1875
1876 sock_hold(tcp_conn->sock->sk);
1877 iscsi_conn_restore_callbacks(tcp_conn);
1878 sock_put(tcp_conn->sock->sk);
1879
1880 sock_release(tcp_conn->sock);
1881 tcp_conn->sock = NULL;
1882 conn->recv_lock = NULL;
1883}
1884
1885static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001886iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001887{
Mike Christie7b8631b2006-01-13 18:05:50 -06001888 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001889 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001890
Mike Christie1c834692006-07-24 15:47:26 -05001891 iscsi_tcp_release_conn(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001892 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001893
Pete Wyckoff534284a2006-11-08 15:58:31 -06001894 if (tcp_conn->tx_hash.tfm)
1895 crypto_free_hash(tcp_conn->tx_hash.tfm);
1896 if (tcp_conn->rx_hash.tfm)
1897 crypto_free_hash(tcp_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001898
Mike Christie5bb0b552006-04-06 21:26:46 -05001899 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001900}
1901
Mike Christie1c834692006-07-24 15:47:26 -05001902static void
1903iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1904{
1905 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christied5390f52006-08-31 18:09:30 -04001906 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie1c834692006-07-24 15:47:26 -05001907
1908 iscsi_conn_stop(cls_conn, flag);
1909 iscsi_tcp_release_conn(conn);
Mike Christied5390f52006-08-31 18:09:30 -04001910 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christie1c834692006-07-24 15:47:26 -05001911}
1912
Alex Aizman7ba24712005-08-04 19:30:08 -07001913static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001914iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001915 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001916 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001917{
Mike Christie5bb0b552006-04-06 21:26:46 -05001918 struct iscsi_conn *conn = cls_conn->dd_data;
1919 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001920 struct sock *sk;
1921 struct socket *sock;
1922 int err;
1923
1924 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001925 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001926 if (!sock) {
1927 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1928 return -EEXIST;
1929 }
1930
Mike Christie5bb0b552006-04-06 21:26:46 -05001931 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1932 if (err)
1933 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001934
Mike Christie67a61112006-05-30 00:37:20 -05001935 /* bind iSCSI connection and socket */
1936 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001937
Mike Christie67a61112006-05-30 00:37:20 -05001938 /* setup Socket parameters */
1939 sk = sock->sk;
1940 sk->sk_reuse = 1;
1941 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1942 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001943
Mike Christie67a61112006-05-30 00:37:20 -05001944 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07001945
Mike Christie67a61112006-05-30 00:37:20 -05001946 /*
1947 * Intercept TCP callbacks for sendfile like receive
1948 * processing.
1949 */
1950 conn->recv_lock = &sk->sk_callback_lock;
1951 iscsi_conn_set_callbacks(conn);
1952 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1953 /*
1954 * set receive state machine into initial state
1955 */
1956 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07001957
Alex Aizman7ba24712005-08-04 19:30:08 -07001958 return 0;
1959}
1960
Mike Christie5bb0b552006-04-06 21:26:46 -05001961/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05001962static void
Mike Christie77a23c22007-05-30 12:57:18 -05001963iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Mike Christie30a6c652006-04-06 21:13:39 -05001964{
Mike Christie5bb0b552006-04-06 21:26:46 -05001965 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie218432c2007-05-30 12:57:17 -05001966 tcp_mtask->xmstate = XMSTATE_IMM_HDR_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001967}
1968
1969static int
1970iscsi_r2tpool_alloc(struct iscsi_session *session)
1971{
1972 int i;
1973 int cmd_i;
1974
1975 /*
1976 * initialize per-task: R2T pool and xmit queue
1977 */
1978 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1979 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05001980 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001981
1982 /*
1983 * pre-allocated x4 as much r2ts to handle race when
1984 * target acks DataOut faster than we data_xmit() queues
1985 * could replenish r2tqueue.
1986 */
1987
1988 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05001989 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
1990 (void***)&tcp_ctask->r2ts,
1991 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001992 goto r2t_alloc_fail;
1993 }
1994
1995 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05001996 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07001997 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05001998 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
1999 iscsi_pool_free(&tcp_ctask->r2tpool,
2000 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002001 goto r2t_alloc_fail;
2002 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002003 }
2004
2005 return 0;
2006
2007r2t_alloc_fail:
2008 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002009 struct iscsi_cmd_task *ctask = session->cmds[i];
2010 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2011
Mike Christie5bb0b552006-04-06 21:26:46 -05002012 kfifo_free(tcp_ctask->r2tqueue);
2013 iscsi_pool_free(&tcp_ctask->r2tpool,
2014 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002015 }
2016 return -ENOMEM;
2017}
2018
2019static void
2020iscsi_r2tpool_free(struct iscsi_session *session)
2021{
2022 int i;
2023
2024 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002025 struct iscsi_cmd_task *ctask = session->cmds[i];
2026 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2027
Mike Christie5bb0b552006-04-06 21:26:46 -05002028 kfifo_free(tcp_ctask->r2tqueue);
2029 iscsi_pool_free(&tcp_ctask->r2tpool,
2030 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002031 }
2032}
2033
Alex Aizman7ba24712005-08-04 19:30:08 -07002034static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002035iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002036 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07002037{
Mike Christie7b7232f2006-02-01 21:06:49 -06002038 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002039 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05002040 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002041 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07002042
Alex Aizman7ba24712005-08-04 19:30:08 -07002043 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002044 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002045 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002046 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christiedd8c0d92006-08-31 18:09:28 -04002047 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05002048 tcp_conn->hdr_size += sizeof(__u32);
Alex Aizman7ba24712005-08-04 19:30:08 -07002049 break;
2050 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002051 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002052 tcp_conn->sendpage = conn->datadgst_en ?
2053 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07002054 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002055 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002056 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07002057 if (session->max_r2t == roundup_pow_of_two(value))
2058 break;
2059 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002060 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002061 if (session->max_r2t & (session->max_r2t - 1))
2062 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2063 if (iscsi_r2tpool_alloc(session))
2064 return -ENOMEM;
2065 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002066 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002067 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002068 }
2069
2070 return 0;
2071}
2072
2073static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05002074iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2075 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06002076{
Mike Christie7b7232f2006-02-01 21:06:49 -06002077 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002078 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christiefd7255f2006-04-06 21:13:36 -05002079 struct inet_sock *inet;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002080 struct ipv6_pinfo *np;
2081 struct sock *sk;
2082 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06002083
2084 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002085 case ISCSI_PARAM_CONN_PORT:
Mike Christie77a23c22007-05-30 12:57:18 -05002086 if (!tcp_conn->sock)
Mike Christiefd7255f2006-04-06 21:13:36 -05002087 return -EINVAL;
Mike Christiefd7255f2006-04-06 21:13:36 -05002088
Mike Christie5bb0b552006-04-06 21:26:46 -05002089 inet = inet_sk(tcp_conn->sock->sk);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002090 len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
Mike Christie8d2860b2006-05-02 19:46:47 -05002091 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002092 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie77a23c22007-05-30 12:57:18 -05002093 if (!tcp_conn->sock)
Mike Christiefd7255f2006-04-06 21:13:36 -05002094 return -EINVAL;
Mike Christiefd7255f2006-04-06 21:13:36 -05002095
Mike Christie5bb0b552006-04-06 21:26:46 -05002096 sk = tcp_conn->sock->sk;
Mike Christiefd7255f2006-04-06 21:13:36 -05002097 if (sk->sk_family == PF_INET) {
2098 inet = inet_sk(sk);
FUJITA Tomonori94cb3f82006-12-17 12:10:27 -06002099 len = sprintf(buf, NIPQUAD_FMT "\n",
Mike Christiefd7255f2006-04-06 21:13:36 -05002100 NIPQUAD(inet->daddr));
2101 } else {
2102 np = inet6_sk(sk);
FUJITA Tomonori94cb3f82006-12-17 12:10:27 -06002103 len = sprintf(buf, NIP6_FMT "\n", NIP6(np->daddr));
Mike Christiefd7255f2006-04-06 21:13:36 -05002104 }
Mike Christiefd7255f2006-04-06 21:13:36 -05002105 break;
2106 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002107 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05002108 }
2109
2110 return len;
2111}
2112
Alex Aizman7ba24712005-08-04 19:30:08 -07002113static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002114iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002115{
Mike Christie7b7232f2006-02-01 21:06:49 -06002116 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002117 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002118
2119 stats->txdata_octets = conn->txdata_octets;
2120 stats->rxdata_octets = conn->rxdata_octets;
2121 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2122 stats->dataout_pdus = conn->dataout_pdus_cnt;
2123 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2124 stats->datain_pdus = conn->datain_pdus_cnt;
2125 stats->r2t_pdus = conn->r2t_pdus_cnt;
2126 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2127 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2128 stats->custom_length = 3;
2129 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002130 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002131 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002132 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002133 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2134 stats->custom[2].value = conn->eh_abort_cnt;
2135}
2136
Mike Christie5bb0b552006-04-06 21:26:46 -05002137static struct iscsi_cls_session *
2138iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2139 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05002140 uint16_t cmds_max, uint16_t qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05002141 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002142{
Mike Christie5bb0b552006-04-06 21:26:46 -05002143 struct iscsi_cls_session *cls_session;
2144 struct iscsi_session *session;
2145 uint32_t hn;
2146 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002147
Mike Christie15482712007-05-30 12:57:19 -05002148 cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05002149 sizeof(struct iscsi_tcp_cmd_task),
2150 sizeof(struct iscsi_tcp_mgmt_task),
2151 initial_cmdsn, &hn);
2152 if (!cls_session)
2153 return NULL;
2154 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002155
Mike Christie5bb0b552006-04-06 21:26:46 -05002156 session = class_to_transport_session(cls_session);
2157 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2158 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2159 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2160
2161 ctask->hdr = &tcp_ctask->hdr;
2162 }
2163
2164 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2165 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2166 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2167
2168 mtask->hdr = &tcp_mtask->hdr;
2169 }
2170
2171 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2172 goto r2tpool_alloc_fail;
2173
2174 return cls_session;
2175
2176r2tpool_alloc_fail:
2177 iscsi_session_teardown(cls_session);
2178 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002179}
2180
Mike Christie5bb0b552006-04-06 21:26:46 -05002181static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2182{
Mike Christie5bb0b552006-04-06 21:26:46 -05002183 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2184 iscsi_session_teardown(cls_session);
2185}
2186
2187static struct scsi_host_template iscsi_sht = {
Mike Christief4246b32006-07-24 15:47:54 -05002188 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05002189 .queuecommand = iscsi_queuecommand,
2190 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -05002191 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie5bb0b552006-04-06 21:26:46 -05002192 .sg_tablesize = ISCSI_SG_TABLESIZE,
Mike Christie8231f0e2007-02-28 17:32:20 -06002193 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -05002194 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2195 .eh_abort_handler = iscsi_eh_abort,
2196 .eh_host_reset_handler = iscsi_eh_host_reset,
2197 .use_clustering = DISABLE_CLUSTERING,
2198 .proc_name = "iscsi_tcp",
2199 .this_id = -1,
2200};
2201
Alex Aizman7ba24712005-08-04 19:30:08 -07002202static struct iscsi_transport iscsi_tcp_transport = {
2203 .owner = THIS_MODULE,
2204 .name = "tcp",
2205 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2206 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002207 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2208 ISCSI_MAX_XMIT_DLENGTH |
2209 ISCSI_HDRDGST_EN |
2210 ISCSI_DATADGST_EN |
2211 ISCSI_INITIAL_R2T_EN |
2212 ISCSI_MAX_R2T |
2213 ISCSI_IMM_DATA_EN |
2214 ISCSI_FIRST_BURST |
2215 ISCSI_MAX_BURST |
2216 ISCSI_PDU_INORDER_EN |
2217 ISCSI_DATASEQ_INORDER_EN |
2218 ISCSI_ERL |
2219 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002220 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05002221 ISCSI_EXP_STATSN |
2222 ISCSI_PERSISTENT_PORT |
2223 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -05002224 ISCSI_TARGET_NAME | ISCSI_TPGT |
2225 ISCSI_USERNAME | ISCSI_PASSWORD |
2226 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN,
Mike Christie8ad57812007-05-30 12:57:13 -05002227 .host_param_mask = ISCSI_HOST_HWADDRESS |
2228 ISCSI_HOST_INITIATOR_NAME,
Alex Aizman7ba24712005-08-04 19:30:08 -07002229 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002230 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002231 .max_conn = 1,
2232 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002233 /* session management */
2234 .create_session = iscsi_tcp_session_create,
2235 .destroy_session = iscsi_tcp_session_destroy,
2236 /* connection management */
2237 .create_conn = iscsi_tcp_conn_create,
2238 .bind_conn = iscsi_tcp_conn_bind,
2239 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002240 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002241 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002242 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002243 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05002244 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -05002245 /* iscsi host params */
2246 .get_host_param = iscsi_host_get_param,
2247 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -05002248 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002249 .send_pdu = iscsi_conn_send_pdu,
2250 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002251 .init_cmd_task = iscsi_tcp_cmd_init,
2252 .init_mgmt_task = iscsi_tcp_mgmt_init,
2253 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2254 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2255 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2256 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002257 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002258};
2259
2260static int __init
2261iscsi_tcp_init(void)
2262{
Alex Aizman7ba24712005-08-04 19:30:08 -07002263 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002264 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2265 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002266 return -EINVAL;
2267 }
2268 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2269
Mike Christie7b8631b2006-01-13 18:05:50 -06002270 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002271 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002272
Mike Christie7b8631b2006-01-13 18:05:50 -06002273 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002274}
2275
2276static void __exit
2277iscsi_tcp_exit(void)
2278{
2279 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002280}
2281
2282module_init(iscsi_tcp_init);
2283module_exit(iscsi_tcp_exit);