blob: 4b226b88b68a20ebbc9ecdbbfd94ee9c5cca2328 [file] [log] [blame]
Alex Aizman7ba24712005-08-04 19:30:08 -07001/*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
Mike Christie5bb0b552006-04-06 21:26:46 -05006 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
Alex Aizman7ba24712005-08-04 19:30:08 -07008 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * See the file COPYING included with this distribution for more details.
21 *
22 * Credits:
23 * Christoph Hellwig
24 * FUJITA Tomonori
25 * Arne Redlich
26 * Zhenyu Wang
27 */
28
29#include <linux/types.h>
30#include <linux/list.h>
31#include <linux/inet.h>
Mike Christie4e7aba72007-05-30 12:57:23 -050032#include <linux/file.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070033#include <linux/blkdev.h>
34#include <linux/crypto.h>
35#include <linux/delay.h>
36#include <linux/kfifo.h>
37#include <linux/scatterlist.h>
38#include <net/tcp.h>
39#include <scsi/scsi_cmnd.h>
Mike Christied1d81c02007-05-30 12:57:21 -050040#include <scsi/scsi_device.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070041#include <scsi/scsi_host.h>
42#include <scsi/scsi.h>
43#include <scsi/scsi_transport_iscsi.h>
44
45#include "iscsi_tcp.h"
46
47MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
48 "Alex Aizman <itn780@yahoo.com>");
49MODULE_DESCRIPTION("iSCSI/TCP data-path");
50MODULE_LICENSE("GPL");
Alex Aizman7ba24712005-08-04 19:30:08 -070051/* #define DEBUG_TCP */
Alex Aizman7ba24712005-08-04 19:30:08 -070052#define DEBUG_ASSERT
53
54#ifdef DEBUG_TCP
Mike Christie5bb0b552006-04-06 21:26:46 -050055#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
Alex Aizman7ba24712005-08-04 19:30:08 -070056#else
57#define debug_tcp(fmt...)
58#endif
59
Alex Aizman7ba24712005-08-04 19:30:08 -070060#ifndef DEBUG_ASSERT
61#ifdef BUG_ON
62#undef BUG_ON
63#endif
64#define BUG_ON(expr)
65#endif
66
Alex Aizman7ba24712005-08-04 19:30:08 -070067static unsigned int iscsi_max_lun = 512;
68module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
69
Alex Aizman7ba24712005-08-04 19:30:08 -070070static inline void
Alex Aizman7ba24712005-08-04 19:30:08 -070071iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
72{
Jens Axboe45711f12007-10-22 21:19:53 +020073 sg_init_one(&ibuf->sg, vbuf, size);
Alex Aizman7ba24712005-08-04 19:30:08 -070074 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{
Jens Axboe45711f12007-10-22 21:19:53 +020081 sg_init_table(&ibuf->sg, 1);
Jens Axboe642f149032007-10-24 11:20:47 +020082 sg_set_page(&ibuf->sg, sg_page(sg), sg->length, sg->offset);
Alex Aizman7ba24712005-08-04 19:30:08 -070083 /*
84 * Fastpath: sg element fits into single page
85 */
Jens Axboe45711f12007-10-22 21:19:53 +020086 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg_page(sg)))
Mike Christie7cae5152006-01-13 18:05:47 -060087 ibuf->use_sendmsg = 0;
88 else
89 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070090 ibuf->sent = 0;
91}
92
93static inline int
94iscsi_buf_left(struct iscsi_buf *ibuf)
95{
96 int rc;
97
98 rc = ibuf->sg.length - ibuf->sent;
99 BUG_ON(rc < 0);
100 return rc;
101}
102
103static inline void
Mike Christieaf973482005-09-12 21:01:32 -0500104iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
105 u8* crc)
Alex Aizman7ba24712005-08-04 19:30:08 -0700106{
Mike Christie5bb0b552006-04-06 21:26:46 -0500107 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
108
James Bottomleyc9802cd2006-09-23 15:33:43 -0500109 crypto_hash_digest(&tcp_conn->tx_hash, &buf->sg, buf->sg.length, crc);
Mike Christie218432c2007-05-30 12:57:17 -0500110 buf->sg.length += sizeof(u32);
Alex Aizman7ba24712005-08-04 19:30:08 -0700111}
112
Alex Aizman7ba24712005-08-04 19:30:08 -0700113static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500114iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700115{
Mike Christie5bb0b552006-04-06 21:26:46 -0500116 struct sk_buff *skb = tcp_conn->in.skb;
Alex Aizman7ba24712005-08-04 19:30:08 -0700117
Mike Christie5bb0b552006-04-06 21:26:46 -0500118 tcp_conn->in.zero_copy_hdr = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700119
Mike Christie5bb0b552006-04-06 21:26:46 -0500120 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
121 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700122 /*
123 * Zero-copy PDU Header: using connection context
124 * to store header pointer.
125 */
126 if (skb_shinfo(skb)->frag_list == NULL &&
Mike Christie5bb0b552006-04-06 21:26:46 -0500127 !skb_shinfo(skb)->nr_frags) {
128 tcp_conn->in.hdr = (struct iscsi_hdr *)
129 ((char*)skb->data + tcp_conn->in.offset);
130 tcp_conn->in.zero_copy_hdr = 1;
131 } else {
Alex Aizman7ba24712005-08-04 19:30:08 -0700132 /* ignoring return code since we checked
133 * in.copy before */
Mike Christie5bb0b552006-04-06 21:26:46 -0500134 skb_copy_bits(skb, tcp_conn->in.offset,
135 &tcp_conn->hdr, tcp_conn->hdr_size);
136 tcp_conn->in.hdr = &tcp_conn->hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700137 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500138 tcp_conn->in.offset += tcp_conn->hdr_size;
139 tcp_conn->in.copy -= tcp_conn->hdr_size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700140 } else {
141 int hdr_remains;
142 int copylen;
143
144 /*
145 * PDU header scattered across SKB's,
146 * copying it... This'll happen quite rarely.
147 */
148
Mike Christie5bb0b552006-04-06 21:26:46 -0500149 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
150 tcp_conn->in.hdr_offset = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700151
Mike Christie5bb0b552006-04-06 21:26:46 -0500152 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700153 BUG_ON(hdr_remains <= 0);
154
Mike Christie5bb0b552006-04-06 21:26:46 -0500155 copylen = min(tcp_conn->in.copy, hdr_remains);
156 skb_copy_bits(skb, tcp_conn->in.offset,
157 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
158 copylen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700159
160 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
Mike Christie5bb0b552006-04-06 21:26:46 -0500161 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
162 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700163
Mike Christie5bb0b552006-04-06 21:26:46 -0500164 tcp_conn->in.offset += copylen;
165 tcp_conn->in.copy -= copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700166 if (copylen < hdr_remains) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500167 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
168 tcp_conn->in.hdr_offset += copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700169 return -EAGAIN;
170 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500171 tcp_conn->in.hdr = &tcp_conn->hdr;
172 tcp_conn->discontiguous_hdr_cnt++;
173 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700174 }
175
176 return 0;
177}
178
Mike Christie30a6c652006-04-06 21:13:39 -0500179/*
180 * must be called with session lock
181 */
182static void
Mike Christieb6c395ed2006-07-24 15:47:15 -0500183iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700184{
Mike Christie5bb0b552006-04-06 21:26:46 -0500185 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christieb6c395ed2006-07-24 15:47:15 -0500186 struct iscsi_r2t_info *r2t;
Mike Christie30a6c652006-04-06 21:13:39 -0500187 struct scsi_cmnd *sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700188
Mike Christieb6c395ed2006-07-24 15:47:15 -0500189 /* flush ctask's r2t queues */
190 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
191 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
192 sizeof(void*));
193 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
194 }
195
Mike Christie30a6c652006-04-06 21:13:39 -0500196 sc = ctask->sc;
197 if (unlikely(!sc))
Alex Aizman7ba24712005-08-04 19:30:08 -0700198 return;
Mike Christie30a6c652006-04-06 21:13:39 -0500199
Mike Christie843c0a82007-12-13 12:43:20 -0600200 tcp_ctask->xmstate = XMSTATE_IDLE;
Mike Christie5bb0b552006-04-06 21:26:46 -0500201 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700202}
203
204/**
205 * iscsi_data_rsp - SCSI Data-In Response processing
206 * @conn: iscsi connection
207 * @ctask: scsi command task
208 **/
209static int
210iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
211{
Mike Christie5bb0b552006-04-06 21:26:46 -0500212 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
213 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
214 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700215 struct iscsi_session *session = conn->session;
Mike Christie857ae0b2007-05-30 12:57:15 -0500216 struct scsi_cmnd *sc = ctask->sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700217 int datasn = be32_to_cpu(rhdr->datasn);
218
Mike Christie77a23c22007-05-30 12:57:18 -0500219 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Alex Aizman7ba24712005-08-04 19:30:08 -0700220 /*
221 * setup Data-In byte counter (gets decremented..)
222 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500223 ctask->data_count = tcp_conn->in.datalen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700224
Mike Christie5bb0b552006-04-06 21:26:46 -0500225 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700226 return 0;
227
Mike Christied473cc72007-05-30 12:57:14 -0500228 if (tcp_ctask->exp_datasn != datasn) {
229 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
230 __FUNCTION__, tcp_ctask->exp_datasn, datasn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700231 return ISCSI_ERR_DATASN;
Mike Christied473cc72007-05-30 12:57:14 -0500232 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700233
Mike Christied473cc72007-05-30 12:57:14 -0500234 tcp_ctask->exp_datasn++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700235
Mike Christie5bb0b552006-04-06 21:26:46 -0500236 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900237 if (tcp_ctask->data_offset + tcp_conn->in.datalen > scsi_bufflen(sc)) {
Mike Christie857ae0b2007-05-30 12:57:15 -0500238 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
239 __FUNCTION__, tcp_ctask->data_offset,
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900240 tcp_conn->in.datalen, scsi_bufflen(sc));
Alex Aizman7ba24712005-08-04 19:30:08 -0700241 return ISCSI_ERR_DATA_OFFSET;
Mike Christie857ae0b2007-05-30 12:57:15 -0500242 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700243
244 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700245 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600246 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700247 int res_count = be32_to_cpu(rhdr->residual_count);
248
249 if (res_count > 0 &&
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900250 res_count <= scsi_bufflen(sc)) {
251 scsi_set_resid(sc, res_count);
Alex Aizman7ba24712005-08-04 19:30:08 -0700252 sc->result = (DID_OK << 16) | rhdr->cmd_status;
253 } else
254 sc->result = (DID_BAD_TARGET << 16) |
255 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600256 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900257 scsi_set_resid(sc, be32_to_cpu(rhdr->residual_count));
Alex Aizman7ba24712005-08-04 19:30:08 -0700258 sc->result = (DID_OK << 16) | rhdr->cmd_status;
259 } else
260 sc->result = (DID_OK << 16) | rhdr->cmd_status;
261 }
262
263 conn->datain_pdus_cnt++;
264 return 0;
265}
266
267/**
268 * iscsi_solicit_data_init - initialize first Data-Out
269 * @conn: iscsi connection
270 * @ctask: scsi command task
271 * @r2t: R2T info
272 *
273 * Notes:
274 * Initialize first Data-Out within this R2T sequence and finds
275 * proper data_offset within this SCSI command.
276 *
277 * This function is called with connection lock taken.
278 **/
279static void
280iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
281 struct iscsi_r2t_info *r2t)
282{
283 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700284 struct scsi_cmnd *sc = ctask->sc;
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900285 int i, sg_count = 0;
286 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700287
Mike Christieffbfe922006-05-18 20:31:36 -0500288 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700289 memset(hdr, 0, sizeof(struct iscsi_data));
290 hdr->ttt = r2t->ttt;
291 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
292 r2t->solicit_datasn++;
293 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500294 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
295 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700296 hdr->exp_statsn = r2t->exp_statsn;
297 hdr->offset = cpu_to_be32(r2t->data_offset);
298 if (r2t->data_length > conn->max_xmit_dlength) {
299 hton24(hdr->dlength, conn->max_xmit_dlength);
300 r2t->data_count = conn->max_xmit_dlength;
301 hdr->flags = 0;
302 } else {
303 hton24(hdr->dlength, r2t->data_length);
304 r2t->data_count = r2t->data_length;
305 hdr->flags = ISCSI_FLAG_CMD_FINAL;
306 }
307 conn->dataout_pdus_cnt++;
308
309 r2t->sent = 0;
310
Mike Christie6e458cc2006-05-18 20:31:31 -0500311 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -0500312 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -0700313
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900314 sg = scsi_sglist(sc);
315 r2t->sg = NULL;
316 for (i = 0; i < scsi_sg_count(sc); i++, sg += 1) {
317 /* FIXME: prefetch ? */
318 if (sg_count + sg->length > r2t->data_offset) {
319 int page_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700320
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900321 /* sg page found! */
Alex Aizman7ba24712005-08-04 19:30:08 -0700322
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900323 /* offset within this page */
324 page_offset = r2t->data_offset - sg_count;
Alex Aizman7ba24712005-08-04 19:30:08 -0700325
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900326 /* fill in this buffer */
327 iscsi_buf_init_sg(&r2t->sendbuf, sg);
328 r2t->sendbuf.sg.offset += page_offset;
329 r2t->sendbuf.sg.length -= page_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700330
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900331 /* xmit logic will continue with next one */
332 r2t->sg = sg + 1;
333 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700334 }
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900335 sg_count += sg->length;
Mike Christie62f38302006-08-31 18:09:27 -0400336 }
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900337 BUG_ON(r2t->sg == NULL);
Alex Aizman7ba24712005-08-04 19:30:08 -0700338}
339
340/**
341 * iscsi_r2t_rsp - iSCSI R2T Response processing
342 * @conn: iscsi connection
343 * @ctask: scsi command task
344 **/
345static int
346iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
347{
348 struct iscsi_r2t_info *r2t;
349 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500350 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
351 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
352 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700353 int r2tsn = be32_to_cpu(rhdr->r2tsn);
354 int rc;
355
Mike Christie98a94162006-08-31 18:09:26 -0400356 if (tcp_conn->in.datalen) {
357 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
358 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700359 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400360 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700361
Mike Christied473cc72007-05-30 12:57:14 -0500362 if (tcp_ctask->exp_datasn != r2tsn){
363 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
364 __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700365 return ISCSI_ERR_R2TSN;
Mike Christied473cc72007-05-30 12:57:14 -0500366 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700367
Alex Aizman7ba24712005-08-04 19:30:08 -0700368 /* fill-in new R2T associated with the task */
369 spin_lock(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -0500370 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
371
Mike Christie843c0a82007-12-13 12:43:20 -0600372 if (!ctask->sc || session->state != ISCSI_STATE_LOGGED_IN) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700373 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
374 "recovery...\n", ctask->itt);
375 spin_unlock(&session->lock);
376 return 0;
377 }
Mike Christieb6c395ed2006-07-24 15:47:15 -0500378
Mike Christie5bb0b552006-04-06 21:26:46 -0500379 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700380 BUG_ON(!rc);
381
382 r2t->exp_statsn = rhdr->statsn;
383 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400384 if (r2t->data_length == 0) {
385 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
Alex Aizman7ba24712005-08-04 19:30:08 -0700386 spin_unlock(&session->lock);
387 return ISCSI_ERR_DATALEN;
388 }
389
Mike Christie98a94162006-08-31 18:09:26 -0400390 if (r2t->data_length > session->max_burst)
391 debug_scsi("invalid R2T with data len %u and max burst %u."
392 "Attempting to execute request.\n",
393 r2t->data_length, session->max_burst);
394
Alex Aizman7ba24712005-08-04 19:30:08 -0700395 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900396 if (r2t->data_offset + r2t->data_length > scsi_bufflen(ctask->sc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700397 spin_unlock(&session->lock);
Mike Christie98a94162006-08-31 18:09:26 -0400398 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
399 "offset %u and total length %d\n", r2t->data_length,
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900400 r2t->data_offset, scsi_bufflen(ctask->sc));
Alex Aizman7ba24712005-08-04 19:30:08 -0700401 return ISCSI_ERR_DATALEN;
402 }
403
404 r2t->ttt = rhdr->ttt; /* no flip */
405 r2t->solicit_datasn = 0;
406
407 iscsi_solicit_data_init(conn, ctask, r2t);
408
Mike Christied473cc72007-05-30 12:57:14 -0500409 tcp_ctask->exp_datasn = r2tsn + 1;
Mike Christie5bb0b552006-04-06 21:26:46 -0500410 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christie843c0a82007-12-13 12:43:20 -0600411 tcp_ctask->xmstate |= XMSTATE_SOL_HDR_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -0700412 conn->r2t_pdus_cnt++;
Mike Christie843c0a82007-12-13 12:43:20 -0600413
414 iscsi_requeue_ctask(ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -0700415 spin_unlock(&session->lock);
416
417 return 0;
418}
419
420static int
Mike Christie5bb0b552006-04-06 21:26:46 -0500421iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700422{
Mike Christie5bb0b552006-04-06 21:26:46 -0500423 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700424 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700425 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500426 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
427 uint32_t cdgst, rdgst = 0, itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700428
Mike Christie5bb0b552006-04-06 21:26:46 -0500429 hdr = tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700430
431 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500432 tcp_conn->in.datalen = ntoh24(hdr->dlength);
433 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700434 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500435 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700436 return ISCSI_ERR_DATALEN;
437 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500438 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700439
440 /* read AHS */
Mike Christie5bb0b552006-04-06 21:26:46 -0500441 ahslen = hdr->hlength << 2;
442 tcp_conn->in.offset += ahslen;
443 tcp_conn->in.copy -= ahslen;
444 if (tcp_conn->in.copy < 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700445 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
Mike Christie5bb0b552006-04-06 21:26:46 -0500446 "%d bytes\n", ahslen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700447 return ISCSI_ERR_AHSLEN;
448 }
449
450 /* calculate read padding */
Mike Christie5bb0b552006-04-06 21:26:46 -0500451 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
452 if (tcp_conn->in.padding) {
453 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
454 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
Alex Aizman7ba24712005-08-04 19:30:08 -0700455 }
456
457 if (conn->hdrdgst_en) {
458 struct scatterlist sg;
459
460 sg_init_one(&sg, (u8 *)hdr,
Mike Christie5bb0b552006-04-06 21:26:46 -0500461 sizeof(struct iscsi_hdr) + ahslen);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500462 crypto_hash_digest(&tcp_conn->rx_hash, &sg, sg.length,
463 (u8 *)&cdgst);
Alex Aizman7ba24712005-08-04 19:30:08 -0700464 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
Mike Christie5bb0b552006-04-06 21:26:46 -0500465 ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600466 if (cdgst != rdgst) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500467 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
468 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
Mike Christie8a47cd32005-11-30 02:27:19 -0600469 return ISCSI_ERR_HDR_DGST;
470 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700471 }
472
Mike Christie5bb0b552006-04-06 21:26:46 -0500473 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700474 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500475 rc = iscsi_verify_itt(conn, hdr, &itt);
476 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
477 tcp_conn->in.datalen = 0; /* force drop */
478 return 0;
479 } else if (rc)
480 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700481
482 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500483 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
484 ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700485
Mike Christie5bb0b552006-04-06 21:26:46 -0500486 switch(opcode) {
487 case ISCSI_OP_SCSI_DATA_IN:
488 tcp_conn->in.ctask = session->cmds[itt];
489 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
Mike Christie275fd7d2006-07-24 15:47:17 -0500490 if (rc)
491 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500492 /* fall through */
493 case ISCSI_OP_SCSI_CMD_RSP:
494 tcp_conn->in.ctask = session->cmds[itt];
495 if (tcp_conn->in.datalen)
496 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700497
Mike Christie5bb0b552006-04-06 21:26:46 -0500498 spin_lock(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500499 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
500 spin_unlock(&session->lock);
501 break;
502 case ISCSI_OP_R2T:
503 tcp_conn->in.ctask = session->cmds[itt];
504 if (ahslen)
505 rc = ISCSI_ERR_AHSLEN;
506 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
507 DMA_TO_DEVICE)
508 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
509 else
510 rc = ISCSI_ERR_PROTO;
511 break;
512 case ISCSI_OP_LOGIN_RSP:
513 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500514 case ISCSI_OP_REJECT:
515 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500516 /*
517 * It is possible that we could get a PDU with a buffer larger
518 * than 8K, but there are no targets that currently do this.
519 * For now we fail until we find a vendor that needs it
520 */
Mike Christiebf32ed32007-02-28 17:32:17 -0600521 if (ISCSI_DEF_MAX_RECV_SEG_LEN <
Mike Christiec8dc1e52006-07-24 15:47:39 -0500522 tcp_conn->in.datalen) {
523 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
524 "but conn buffer is only %u (opcode %0x)\n",
525 tcp_conn->in.datalen,
Mike Christiebf32ed32007-02-28 17:32:17 -0600526 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
Mike Christiec8dc1e52006-07-24 15:47:39 -0500527 rc = ISCSI_ERR_PROTO;
528 break;
529 }
530
Mike Christie5bb0b552006-04-06 21:26:46 -0500531 if (tcp_conn->in.datalen)
532 goto copy_hdr;
533 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500534 case ISCSI_OP_LOGOUT_RSP:
535 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500536 case ISCSI_OP_SCSI_TMFUNC_RSP:
537 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
538 break;
539 default:
540 rc = ISCSI_ERR_BAD_OPCODE;
541 break;
542 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700543
544 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500545
546copy_hdr:
547 /*
548 * if we did zero copy for the header but we will need multiple
549 * skbs to complete the command then we have to copy the header
550 * for later use
551 */
Mike Christie275fd7d2006-07-24 15:47:17 -0500552 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
Mike Christie5bb0b552006-04-06 21:26:46 -0500553 (tcp_conn->in.datalen + tcp_conn->in.padding +
554 (conn->datadgst_en ? 4 : 0))) {
555 debug_tcp("Copying header for later use. in.copy %d in.datalen"
556 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
557 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
558 sizeof(struct iscsi_hdr));
559 tcp_conn->in.hdr = &tcp_conn->hdr;
560 tcp_conn->in.zero_copy_hdr = 0;
561 }
562 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700563}
564
565/**
566 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
Mike Christie5bb0b552006-04-06 21:26:46 -0500567 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700568 * @ctask: scsi command task
569 * @buf: buffer to copy to
570 * @buf_size: size of buffer
571 * @offset: offset within the buffer
572 *
573 * Notes:
574 * The function calls skb_copy_bits() and updates per-connection and
575 * per-cmd byte counters.
576 *
577 * Read counters (in bytes):
578 *
579 * conn->in.offset offset within in progress SKB
580 * conn->in.copy left to copy from in progress SKB
581 * including padding
582 * conn->in.copied copied already from in progress SKB
583 * conn->data_copied copied already from in progress buffer
584 * ctask->sent total bytes sent up to the MidLayer
585 * ctask->data_count left to copy from in progress Data-In
586 * buf_left left to copy from in progress buffer
587 **/
588static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500589iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -0700590 void *buf, int buf_size, int offset)
591{
Mike Christie5bb0b552006-04-06 21:26:46 -0500592 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
593 int buf_left = buf_size - (tcp_conn->data_copied + offset);
Mike Christie857ae0b2007-05-30 12:57:15 -0500594 unsigned size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700595 int rc;
596
597 size = min(size, ctask->data_count);
598
599 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500600 size, tcp_conn->in.offset, tcp_conn->in.copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700601
602 BUG_ON(size <= 0);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900603 BUG_ON(tcp_ctask->sent + size > scsi_bufflen(ctask->sc));
Alex Aizman7ba24712005-08-04 19:30:08 -0700604
Mike Christie5bb0b552006-04-06 21:26:46 -0500605 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
606 (char*)buf + (offset + tcp_conn->data_copied), size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700607 /* must fit into skb->len */
608 BUG_ON(rc);
609
Mike Christie5bb0b552006-04-06 21:26:46 -0500610 tcp_conn->in.offset += size;
611 tcp_conn->in.copy -= size;
612 tcp_conn->in.copied += size;
613 tcp_conn->data_copied += size;
614 tcp_ctask->sent += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700615 ctask->data_count -= size;
616
Mike Christie5bb0b552006-04-06 21:26:46 -0500617 BUG_ON(tcp_conn->in.copy < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700618 BUG_ON(ctask->data_count < 0);
619
Mike Christie5bb0b552006-04-06 21:26:46 -0500620 if (buf_size != (tcp_conn->data_copied + offset)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700621 if (!ctask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500622 BUG_ON(buf_size - tcp_conn->data_copied < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700623 /* done with this PDU */
Mike Christie5bb0b552006-04-06 21:26:46 -0500624 return buf_size - tcp_conn->data_copied;
Alex Aizman7ba24712005-08-04 19:30:08 -0700625 }
626 return -EAGAIN;
627 }
628
629 /* done with this buffer or with both - PDU and buffer */
Mike Christie5bb0b552006-04-06 21:26:46 -0500630 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700631 return 0;
632}
633
634/**
635 * iscsi_tcp_copy - copy skb bits to the destanation buffer
Mike Christie5bb0b552006-04-06 21:26:46 -0500636 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700637 *
638 * Notes:
639 * The function calls skb_copy_bits() and updates per-connection
640 * byte counters.
641 **/
642static inline int
Mike Christieca518682006-08-31 18:09:32 -0400643iscsi_tcp_copy(struct iscsi_conn *conn, int buf_size)
Alex Aizman7ba24712005-08-04 19:30:08 -0700644{
Mike Christiec8dc1e52006-07-24 15:47:39 -0500645 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500646 int buf_left = buf_size - tcp_conn->data_copied;
647 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700648 int rc;
649
650 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500651 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700652 BUG_ON(size <= 0);
653
Mike Christie5bb0b552006-04-06 21:26:46 -0500654 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christiec8dc1e52006-07-24 15:47:39 -0500655 (char*)conn->data + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700656 BUG_ON(rc);
657
Mike Christie5bb0b552006-04-06 21:26:46 -0500658 tcp_conn->in.offset += size;
659 tcp_conn->in.copy -= size;
660 tcp_conn->in.copied += size;
661 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700662
Mike Christie5bb0b552006-04-06 21:26:46 -0500663 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700664 return -EAGAIN;
665
666 return 0;
667}
668
669static inline void
James Bottomleyc9802cd2006-09-23 15:33:43 -0500670partial_sg_digest_update(struct hash_desc *desc, struct scatterlist *sg,
Mike Christie62f38302006-08-31 18:09:27 -0400671 int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700672{
673 struct scatterlist temp;
674
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700675 sg_init_table(&temp, 1);
676 sg_set_page(&temp, sg_page(sg), length, offset);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500677 crypto_hash_update(desc, &temp, length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700678}
679
Mike Christief6cfba12005-11-29 23:12:57 -0600680static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500681iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600682{
683 struct scatterlist tmp;
684
685 sg_init_one(&tmp, buf, len);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500686 crypto_hash_update(&tcp_conn->rx_hash, &tmp, len);
Mike Christief6cfba12005-11-29 23:12:57 -0600687}
688
Alex Aizman7ba24712005-08-04 19:30:08 -0700689static int iscsi_scsi_data_in(struct iscsi_conn *conn)
690{
Mike Christie5bb0b552006-04-06 21:26:46 -0500691 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
692 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
693 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700694 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600695 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700696 int i, offset, rc = 0;
697
698 BUG_ON((void*)ctask != sc->SCp.ptr);
699
Mike Christie5bb0b552006-04-06 21:26:46 -0500700 offset = tcp_ctask->data_offset;
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900701 sg = scsi_sglist(sc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700702
Mike Christie5bb0b552006-04-06 21:26:46 -0500703 if (tcp_ctask->data_offset)
704 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700705 offset -= sg[i].length;
706 /* we've passed through partial sg*/
707 if (offset < 0)
708 offset = 0;
709
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900710 for (i = tcp_ctask->sg_count; i < scsi_sg_count(sc); i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700711 char *dest;
712
Jens Axboe45711f12007-10-22 21:19:53 +0200713 dest = kmap_atomic(sg_page(&sg[i]), KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500714 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700715 sg[i].length, offset);
716 kunmap_atomic(dest, KM_SOFTIRQ0);
717 if (rc == -EAGAIN)
718 /* continue with the next SKB/PDU */
719 return rc;
720 if (!rc) {
721 if (conn->datadgst_en) {
722 if (!offset)
Herbert Xudc64ddf2006-08-24 18:45:50 +1000723 crypto_hash_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500724 &tcp_conn->rx_hash,
Arne Redlichc959e1c2006-12-17 12:10:24 -0600725 &sg[i], sg[i].length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700726 else
Mike Christie62f38302006-08-31 18:09:27 -0400727 partial_sg_digest_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500728 &tcp_conn->rx_hash,
Mike Christie5bb0b552006-04-06 21:26:46 -0500729 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700730 sg[i].offset + offset,
731 sg[i].length - offset);
732 }
733 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500734 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700735 }
736
737 if (!ctask->data_count) {
738 if (rc && conn->datadgst_en)
739 /*
740 * data-in is complete, but buffer not...
741 */
James Bottomleyc9802cd2006-09-23 15:33:43 -0500742 partial_sg_digest_update(&tcp_conn->rx_hash,
743 &sg[i],
744 sg[i].offset,
745 sg[i].length-rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700746 rc = 0;
747 break;
748 }
749
Mike Christie5bb0b552006-04-06 21:26:46 -0500750 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700751 return -EAGAIN;
752 }
753 BUG_ON(ctask->data_count);
754
Alex Aizman7ba24712005-08-04 19:30:08 -0700755 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500756 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Mike Christieb6c395ed2006-07-24 15:47:15 -0500757 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
758 (long)sc, sc->result, ctask->itt,
759 tcp_conn->in.hdr->flags);
Mike Christie5bb0b552006-04-06 21:26:46 -0500760 spin_lock(&conn->session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500761 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
762 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700763 }
764
765 return rc;
766}
767
768static int
769iscsi_data_recv(struct iscsi_conn *conn)
770{
Mike Christie5bb0b552006-04-06 21:26:46 -0500771 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
772 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700773
Mike Christie5bb0b552006-04-06 21:26:46 -0500774 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
775 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700776 case ISCSI_OP_SCSI_DATA_IN:
777 rc = iscsi_scsi_data_in(conn);
778 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500779 case ISCSI_OP_SCSI_CMD_RSP:
Alex Aizman7ba24712005-08-04 19:30:08 -0700780 case ISCSI_OP_TEXT_RSP:
781 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500782 case ISCSI_OP_ASYNC_EVENT:
783 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700784 /*
785 * Collect data segment to the connection's data
786 * placeholder
787 */
Mike Christieca518682006-08-31 18:09:32 -0400788 if (iscsi_tcp_copy(conn, tcp_conn->in.datalen)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700789 rc = -EAGAIN;
790 goto exit;
791 }
792
Mike Christiec8dc1e52006-07-24 15:47:39 -0500793 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500794 tcp_conn->in.datalen);
795 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
Mike Christiec8dc1e52006-07-24 15:47:39 -0500796 iscsi_recv_digest_update(tcp_conn, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500797 tcp_conn->in.datalen);
798 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700799 default:
800 BUG_ON(1);
801 }
802exit:
803 return rc;
804}
805
806/**
807 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
808 * @rd_desc: read descriptor
809 * @skb: socket buffer
810 * @offset: offset in skb
811 * @len: skb->len - offset
812 **/
813static int
814iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
815 unsigned int offset, size_t len)
816{
817 int rc;
818 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500819 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700820 int processed;
821 char pad[ISCSI_PAD_LEN];
822 struct scatterlist sg;
823
824 /*
825 * Save current SKB and its offset in the corresponding
826 * connection context.
827 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500828 tcp_conn->in.copy = skb->len - offset;
829 tcp_conn->in.offset = offset;
830 tcp_conn->in.skb = skb;
831 tcp_conn->in.len = tcp_conn->in.copy;
832 BUG_ON(tcp_conn->in.copy <= 0);
833 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700834
835more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500836 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700837 rc = 0;
838
839 if (unlikely(conn->suspend_rx)) {
840 debug_tcp("conn %d Rx suspended!\n", conn->id);
841 return 0;
842 }
843
Mike Christie5bb0b552006-04-06 21:26:46 -0500844 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
845 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
846 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700847 if (rc) {
848 if (rc == -EAGAIN)
849 goto nomore;
850 else {
Mike Christied82967c2006-07-24 15:47:11 -0500851 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700852 return 0;
853 }
854 }
855
856 /*
857 * Verify and process incoming PDU header.
858 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500859 rc = iscsi_tcp_hdr_recv(conn);
860 if (!rc && tcp_conn->in.datalen) {
Mike Christiedd8c0d92006-08-31 18:09:28 -0400861 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -0500862 crypto_hash_init(&tcp_conn->rx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -0500863 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700864 } else if (rc) {
Mike Christie40527af2006-07-24 15:47:45 -0500865 iscsi_conn_failure(conn, rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700866 return 0;
867 }
868 }
869
Mike Christiedbdb0162007-05-30 12:57:20 -0500870 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV &&
871 tcp_conn->in.copy) {
Mike Christief6cfba12005-11-29 23:12:57 -0600872 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500873
Alex Aizman7ba24712005-08-04 19:30:08 -0700874 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500875 tcp_conn->in.offset, tcp_conn->in.copy);
Mike Christiedbdb0162007-05-30 12:57:20 -0500876
877 if (!tcp_conn->data_copied) {
878 if (tcp_conn->in.padding) {
879 debug_tcp("padding -> %d\n",
880 tcp_conn->in.padding);
881 memset(pad, 0, tcp_conn->in.padding);
882 sg_init_one(&sg, pad, tcp_conn->in.padding);
883 crypto_hash_update(&tcp_conn->rx_hash,
884 &sg, sg.length);
885 }
886 crypto_hash_final(&tcp_conn->rx_hash,
887 (u8 *) &tcp_conn->in.datadgst);
888 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
889 }
890
Mike Christieca518682006-08-31 18:09:32 -0400891 rc = iscsi_tcp_copy(conn, sizeof(uint32_t));
892 if (rc) {
893 if (rc == -EAGAIN)
894 goto again;
895 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
896 return 0;
897 }
898
899 memcpy(&recv_digest, conn->data, sizeof(uint32_t));
Mike Christie5bb0b552006-04-06 21:26:46 -0500900 if (recv_digest != tcp_conn->in.datadgst) {
Mike Christief6cfba12005-11-29 23:12:57 -0600901 debug_tcp("iscsi_tcp: data digest error!"
902 "0x%x != 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500903 tcp_conn->in.datadgst);
Mike Christief6cfba12005-11-29 23:12:57 -0600904 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
905 return 0;
906 } else {
907 debug_tcp("iscsi_tcp: data digest match!"
908 "0x%x == 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500909 tcp_conn->in.datadgst);
910 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700911 }
912 }
913
Mike Christie5bb0b552006-04-06 21:26:46 -0500914 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
Mike Christiedbdb0162007-05-30 12:57:20 -0500915 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700916 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500917 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700918
919 rc = iscsi_data_recv(conn);
920 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500921 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700922 goto again;
Mike Christied82967c2006-07-24 15:47:11 -0500923 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700924 return 0;
925 }
Mike Christiedbdb0162007-05-30 12:57:20 -0500926
927 if (tcp_conn->in.padding)
928 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
929 else if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500930 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Mike Christiedbdb0162007-05-30 12:57:20 -0500931 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500932 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Mike Christiedbdb0162007-05-30 12:57:20 -0500933 tcp_conn->data_copied = 0;
934 }
935
936 if (tcp_conn->in_progress == IN_PROGRESS_PAD_RECV &&
937 tcp_conn->in.copy) {
938 int copylen = min(tcp_conn->in.padding - tcp_conn->data_copied,
939 tcp_conn->in.copy);
940
941 tcp_conn->in.copy -= copylen;
942 tcp_conn->in.offset += copylen;
943 tcp_conn->data_copied += copylen;
944
945 if (tcp_conn->data_copied != tcp_conn->in.padding)
946 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
947 else if (conn->datadgst_en)
948 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
949 else
950 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
951 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700952 }
953
954 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500955 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
956 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700957
Mike Christie5bb0b552006-04-06 21:26:46 -0500958 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700959 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500960 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700961 goto more;
962 }
963
964nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500965 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700966 BUG_ON(processed == 0);
967 return processed;
968
969again:
Mike Christie5bb0b552006-04-06 21:26:46 -0500970 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700971 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
972 processed, (int)len, (int)rd_desc->count);
973 BUG_ON(processed == 0);
974 BUG_ON(processed > len);
975
976 conn->rxdata_octets += processed;
977 return processed;
978}
979
980static void
981iscsi_tcp_data_ready(struct sock *sk, int flag)
982{
983 struct iscsi_conn *conn = sk->sk_user_data;
984 read_descriptor_t rd_desc;
985
986 read_lock(&sk->sk_callback_lock);
987
Mike Christie665b44a2006-05-02 19:46:49 -0500988 /*
989 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
990 * We set count to 1 because we want the network layer to
991 * hand us all the skbs that are available. iscsi_tcp_data_recv
992 * handled pdus that cross buffers or pdus that still need data.
993 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700994 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -0500995 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -0700996 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
997
998 read_unlock(&sk->sk_callback_lock);
999}
1000
1001static void
1002iscsi_tcp_state_change(struct sock *sk)
1003{
Mike Christie5bb0b552006-04-06 21:26:46 -05001004 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001005 struct iscsi_conn *conn;
1006 struct iscsi_session *session;
1007 void (*old_state_change)(struct sock *);
1008
1009 read_lock(&sk->sk_callback_lock);
1010
1011 conn = (struct iscsi_conn*)sk->sk_user_data;
1012 session = conn->session;
1013
Mike Christiee6273992005-11-29 23:12:49 -06001014 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1015 sk->sk_state == TCP_CLOSE) &&
1016 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001017 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1018 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1019 }
1020
Mike Christie5bb0b552006-04-06 21:26:46 -05001021 tcp_conn = conn->dd_data;
1022 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001023
1024 read_unlock(&sk->sk_callback_lock);
1025
1026 old_state_change(sk);
1027}
1028
1029/**
1030 * iscsi_write_space - Called when more output buffer space is available
1031 * @sk: socket space is available for
1032 **/
1033static void
1034iscsi_write_space(struct sock *sk)
1035{
1036 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001037 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1038
1039 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001040 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001041 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001042}
1043
1044static void
1045iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1046{
Mike Christie5bb0b552006-04-06 21:26:46 -05001047 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1048 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001049
1050 /* assign new callbacks */
1051 write_lock_bh(&sk->sk_callback_lock);
1052 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001053 tcp_conn->old_data_ready = sk->sk_data_ready;
1054 tcp_conn->old_state_change = sk->sk_state_change;
1055 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001056 sk->sk_data_ready = iscsi_tcp_data_ready;
1057 sk->sk_state_change = iscsi_tcp_state_change;
1058 sk->sk_write_space = iscsi_write_space;
1059 write_unlock_bh(&sk->sk_callback_lock);
1060}
1061
1062static void
Mike Christie1c834692006-07-24 15:47:26 -05001063iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001064{
Mike Christie5bb0b552006-04-06 21:26:46 -05001065 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001066
1067 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1068 write_lock_bh(&sk->sk_callback_lock);
1069 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001070 sk->sk_data_ready = tcp_conn->old_data_ready;
1071 sk->sk_state_change = tcp_conn->old_state_change;
1072 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001073 sk->sk_no_check = 0;
1074 write_unlock_bh(&sk->sk_callback_lock);
1075}
1076
1077/**
1078 * iscsi_send - generic send routine
1079 * @sk: kernel's socket
1080 * @buf: buffer to write from
1081 * @size: actual size to write
1082 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001083 */
1084static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001085iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001086{
Mike Christie5bb0b552006-04-06 21:26:46 -05001087 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1088 struct socket *sk = tcp_conn->sock;
Mike Christie3219e522006-05-30 00:37:28 -05001089 int offset = buf->sg.offset + buf->sent, res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001090
Mike Christie7cae5152006-01-13 18:05:47 -06001091 /*
1092 * if we got use_sg=0 or are sending something we kmallocd
1093 * then we did not have to do kmap (kmap returns page_address)
1094 *
1095 * if we got use_sg > 0, but had to drop down, we do not
1096 * set clustering so this should only happen for that
1097 * slab case.
1098 */
1099 if (buf->use_sendmsg)
Jens Axboe45711f12007-10-22 21:19:53 +02001100 res = sock_no_sendpage(sk, sg_page(&buf->sg), offset, size, flags);
Mike Christie7cae5152006-01-13 18:05:47 -06001101 else
Jens Axboe45711f12007-10-22 21:19:53 +02001102 res = tcp_conn->sendpage(sk, sg_page(&buf->sg), offset, size, flags);
Mike Christie3219e522006-05-30 00:37:28 -05001103
1104 if (res >= 0) {
1105 conn->txdata_octets += res;
1106 buf->sent += res;
1107 return res;
1108 }
1109
1110 tcp_conn->sendpage_failures_cnt++;
1111 if (res == -EAGAIN)
1112 res = -ENOBUFS;
1113 else
1114 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1115 return res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001116}
1117
1118/**
1119 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1120 * @conn: iscsi connection
1121 * @buf: buffer to write from
1122 * @datalen: lenght of data to be sent after the header
1123 *
1124 * Notes:
1125 * (Tx, Fast Path)
1126 **/
1127static inline int
1128iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1129{
Alex Aizman7ba24712005-08-04 19:30:08 -07001130 int flags = 0; /* MSG_DONTWAIT; */
1131 int res, size;
1132
1133 size = buf->sg.length - buf->sent;
1134 BUG_ON(buf->sent + size > buf->sg.length);
1135 if (buf->sent + size != buf->sg.length || datalen)
1136 flags |= MSG_MORE;
1137
FUJITA Tomonori56851692006-01-13 18:05:44 -06001138 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001139 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1140 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001141 if (size != res)
1142 return -EAGAIN;
1143 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001144 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001145
1146 return res;
1147}
1148
1149/**
1150 * iscsi_sendpage - send one page of iSCSI Data-Out.
1151 * @conn: iscsi connection
1152 * @buf: buffer to write from
1153 * @count: remaining data
1154 * @sent: number of bytes sent
1155 *
1156 * Notes:
1157 * (Tx, Fast Path)
1158 **/
1159static inline int
1160iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1161 int *count, int *sent)
1162{
Alex Aizman7ba24712005-08-04 19:30:08 -07001163 int flags = 0; /* MSG_DONTWAIT; */
1164 int res, size;
1165
1166 size = buf->sg.length - buf->sent;
1167 BUG_ON(buf->sent + size > buf->sg.length);
1168 if (size > *count)
1169 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001170 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001171 flags |= MSG_MORE;
1172
FUJITA Tomonori56851692006-01-13 18:05:44 -06001173 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001174 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1175 size, buf->sent, *count, *sent, res);
1176 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001177 *count -= res;
1178 *sent += res;
1179 if (size != res)
1180 return -EAGAIN;
1181 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001182 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001183
1184 return res;
1185}
1186
1187static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001188iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
Mike Christie62f38302006-08-31 18:09:27 -04001189 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001190{
James Bottomleyc9802cd2006-09-23 15:33:43 -05001191 crypto_hash_init(&tcp_conn->tx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -05001192 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001193}
1194
Alex Aizman7ba24712005-08-04 19:30:08 -07001195/**
1196 * iscsi_solicit_data_cont - initialize next Data-Out
1197 * @conn: iscsi connection
1198 * @ctask: scsi command task
1199 * @r2t: R2T info
1200 * @left: bytes left to transfer
1201 *
1202 * Notes:
1203 * Initialize next Data-Out within this R2T sequence and continue
1204 * to process next Scatter-Gather element(if any) of this SCSI command.
1205 *
1206 * Called under connection lock.
1207 **/
1208static void
1209iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1210 struct iscsi_r2t_info *r2t, int left)
1211{
1212 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001213 int new_offset;
1214
Mike Christieffbfe922006-05-18 20:31:36 -05001215 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001216 memset(hdr, 0, sizeof(struct iscsi_data));
1217 hdr->ttt = r2t->ttt;
1218 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1219 r2t->solicit_datasn++;
1220 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001221 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1222 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001223 hdr->exp_statsn = r2t->exp_statsn;
1224 new_offset = r2t->data_offset + r2t->sent;
1225 hdr->offset = cpu_to_be32(new_offset);
1226 if (left > conn->max_xmit_dlength) {
1227 hton24(hdr->dlength, conn->max_xmit_dlength);
1228 r2t->data_count = conn->max_xmit_dlength;
1229 } else {
1230 hton24(hdr->dlength, left);
1231 r2t->data_count = left;
1232 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1233 }
1234 conn->dataout_pdus_cnt++;
1235
Mike Christie6e458cc2006-05-18 20:31:31 -05001236 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001237 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001238
Mike Christie62f38302006-08-31 18:09:27 -04001239 if (iscsi_buf_left(&r2t->sendbuf))
1240 return;
1241
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001242 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1243 r2t->sg += 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001244}
1245
Mike Christie62f38302006-08-31 18:09:27 -04001246static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask,
1247 unsigned long len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001248{
Mike Christie62f38302006-08-31 18:09:27 -04001249 tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1);
1250 if (!tcp_ctask->pad_count)
1251 return;
Alex Aizman7ba24712005-08-04 19:30:08 -07001252
Mike Christie62f38302006-08-31 18:09:27 -04001253 tcp_ctask->pad_count = ISCSI_PAD_LEN - tcp_ctask->pad_count;
1254 debug_scsi("write padding %d bytes\n", tcp_ctask->pad_count);
Mike Christie843c0a82007-12-13 12:43:20 -06001255 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Alex Aizman7ba24712005-08-04 19:30:08 -07001256}
1257
1258/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001259 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001260 * @conn: iscsi connection
1261 * @ctask: scsi command task
1262 * @sc: scsi command
1263 **/
1264static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001265iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001266{
Mike Christie5bb0b552006-04-06 21:26:46 -05001267 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001268
Mike Christie5bb0b552006-04-06 21:26:46 -05001269 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Mike Christie843c0a82007-12-13 12:43:20 -06001270 tcp_ctask->xmstate = XMSTATE_CMD_HDR_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001271}
1272
1273/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001274 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001275 * @conn: iscsi connection
1276 * @mtask: task management task
1277 *
1278 * Notes:
1279 * The function can return -EAGAIN in which case caller must
1280 * call it again later, or recover. '0' return code means successful
1281 * xmit.
1282 *
Mike Christie218432c2007-05-30 12:57:17 -05001283 * Management xmit state machine consists of these states:
Mike Christie843c0a82007-12-13 12:43:20 -06001284 * XMSTATE_IMM_HDR_INIT - calculate digest of PDU Header
1285 * XMSTATE_IMM_HDR - PDU Header xmit in progress
1286 * XMSTATE_IMM_DATA - PDU Data xmit in progress
1287 * XMSTATE_IDLE - management PDU is done
Alex Aizman7ba24712005-08-04 19:30:08 -07001288 **/
1289static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001290iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001291{
Mike Christie5bb0b552006-04-06 21:26:46 -05001292 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001293 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001294
1295 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001296 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001297
Mike Christie843c0a82007-12-13 12:43:20 -06001298 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR_INIT) {
Mike Christie218432c2007-05-30 12:57:17 -05001299 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
1300 sizeof(struct iscsi_hdr));
1301
1302 if (mtask->data_count) {
Mike Christie843c0a82007-12-13 12:43:20 -06001303 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie218432c2007-05-30 12:57:17 -05001304 iscsi_buf_init_iov(&tcp_mtask->sendbuf,
1305 (char*)mtask->data,
1306 mtask->data_count);
1307 }
1308
Mike Christieaf973482005-09-12 21:01:32 -05001309 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001310 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001311 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001312 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1313 (u8*)tcp_mtask->hdrext);
Mike Christie218432c2007-05-30 12:57:17 -05001314
1315 tcp_mtask->sent = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06001316 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR_INIT;
1317 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
Mike Christie218432c2007-05-30 12:57:17 -05001318 }
1319
Mike Christie843c0a82007-12-13 12:43:20 -06001320 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
Mike Christie3219e522006-05-30 00:37:28 -05001321 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1322 mtask->data_count);
Mike Christie218432c2007-05-30 12:57:17 -05001323 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001324 return rc;
Mike Christie843c0a82007-12-13 12:43:20 -06001325 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001326 }
1327
Mike Christie843c0a82007-12-13 12:43:20 -06001328 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001329 BUG_ON(!mtask->data_count);
Mike Christie843c0a82007-12-13 12:43:20 -06001330 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001331 /* FIXME: implement.
1332 * Virtual buffer could be spreaded across multiple pages...
1333 */
1334 do {
Mike Christie3219e522006-05-30 00:37:28 -05001335 int rc;
1336
1337 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1338 &mtask->data_count, &tcp_mtask->sent);
1339 if (rc) {
Mike Christie843c0a82007-12-13 12:43:20 -06001340 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001341 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001342 }
1343 } while (mtask->data_count);
1344 }
1345
Mike Christie843c0a82007-12-13 12:43:20 -06001346 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
Al Virob4377352007-02-09 16:39:40 +00001347 if (mtask->hdr->itt == RESERVED_ITT) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001348 struct iscsi_session *session = conn->session;
1349
1350 spin_lock_bh(&session->lock);
1351 list_del(&conn->mtask->running);
1352 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1353 sizeof(void*));
1354 spin_unlock_bh(&session->lock);
1355 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001356 return 0;
1357}
1358
Mike Christie218432c2007-05-30 12:57:17 -05001359static int
1360iscsi_send_cmd_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001361{
Mike Christie218432c2007-05-30 12:57:17 -05001362 struct scsi_cmnd *sc = ctask->sc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001363 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie218432c2007-05-30 12:57:17 -05001364 int rc = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -05001365
Mike Christie843c0a82007-12-13 12:43:20 -06001366 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_INIT) {
Mike Christie218432c2007-05-30 12:57:17 -05001367 tcp_ctask->sent = 0;
1368 tcp_ctask->sg_count = 0;
1369 tcp_ctask->exp_datasn = 0;
Mike Christie3219e522006-05-30 00:37:28 -05001370
Mike Christie218432c2007-05-30 12:57:17 -05001371 if (sc->sc_data_direction == DMA_TO_DEVICE) {
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001372 struct scatterlist *sg = scsi_sglist(sc);
Alex Aizman7ba24712005-08-04 19:30:08 -07001373
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001374 iscsi_buf_init_sg(&tcp_ctask->sendbuf, sg);
1375 tcp_ctask->sg = sg + 1;
1376 tcp_ctask->bad_sg = sg + scsi_sg_count(sc);
Mike Christie218432c2007-05-30 12:57:17 -05001377
1378 debug_scsi("cmd [itt 0x%x total %d imm_data %d "
1379 "unsol count %d, unsol offset %d]\n",
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001380 ctask->itt, scsi_bufflen(sc),
Mike Christie218432c2007-05-30 12:57:17 -05001381 ctask->imm_count, ctask->unsol_count,
1382 ctask->unsol_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -07001383 }
Mike Christie218432c2007-05-30 12:57:17 -05001384
1385 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
1386 sizeof(struct iscsi_hdr));
1387
1388 if (conn->hdrdgst_en)
1389 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1390 (u8*)tcp_ctask->hdrext);
Mike Christie843c0a82007-12-13 12:43:20 -06001391 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_INIT;
1392 tcp_ctask->xmstate |= XMSTATE_CMD_HDR_XMIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001393 }
1394
Mike Christie843c0a82007-12-13 12:43:20 -06001395 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_XMIT) {
Mike Christie218432c2007-05-30 12:57:17 -05001396 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1397 if (rc)
1398 return rc;
Mike Christie843c0a82007-12-13 12:43:20 -06001399 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_XMIT;
Mike Christie218432c2007-05-30 12:57:17 -05001400
1401 if (sc->sc_data_direction != DMA_TO_DEVICE)
1402 return 0;
1403
1404 if (ctask->imm_count) {
Mike Christie843c0a82007-12-13 12:43:20 -06001405 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie218432c2007-05-30 12:57:17 -05001406 iscsi_set_padding(tcp_ctask, ctask->imm_count);
1407
1408 if (ctask->conn->datadgst_en) {
1409 iscsi_data_digest_init(ctask->conn->dd_data,
1410 tcp_ctask);
1411 tcp_ctask->immdigest = 0;
1412 }
1413 }
1414
Mike Christie843c0a82007-12-13 12:43:20 -06001415 if (ctask->unsol_count)
1416 tcp_ctask->xmstate |=
1417 XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
Mike Christie218432c2007-05-30 12:57:17 -05001418 }
1419 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001420}
1421
Mike Christie62f38302006-08-31 18:09:27 -04001422static int
1423iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1424{
1425 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1426 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1427 int sent = 0, rc;
1428
Mike Christie843c0a82007-12-13 12:43:20 -06001429 if (tcp_ctask->xmstate & XMSTATE_W_PAD) {
Mike Christie62f38302006-08-31 18:09:27 -04001430 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
1431 tcp_ctask->pad_count);
1432 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001433 crypto_hash_update(&tcp_conn->tx_hash,
1434 &tcp_ctask->sendbuf.sg,
1435 tcp_ctask->sendbuf.sg.length);
Mike Christie843c0a82007-12-13 12:43:20 -06001436 } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD))
Mike Christie62f38302006-08-31 18:09:27 -04001437 return 0;
1438
Mike Christie843c0a82007-12-13 12:43:20 -06001439 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
1440 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_PAD;
Mike Christie62f38302006-08-31 18:09:27 -04001441 debug_scsi("sending %d pad bytes for itt 0x%x\n",
1442 tcp_ctask->pad_count, ctask->itt);
1443 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1444 &sent);
1445 if (rc) {
1446 debug_scsi("padding send failed %d\n", rc);
Mike Christie843c0a82007-12-13 12:43:20 -06001447 tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD;
Mike Christie62f38302006-08-31 18:09:27 -04001448 }
1449 return rc;
1450}
1451
1452static int
1453iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1454 struct iscsi_buf *buf, uint32_t *digest)
1455{
1456 struct iscsi_tcp_cmd_task *tcp_ctask;
1457 struct iscsi_tcp_conn *tcp_conn;
1458 int rc, sent = 0;
1459
1460 if (!conn->datadgst_en)
1461 return 0;
1462
1463 tcp_ctask = ctask->dd_data;
1464 tcp_conn = conn->dd_data;
1465
Mike Christie843c0a82007-12-13 12:43:20 -06001466 if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) {
James Bottomleyc9802cd2006-09-23 15:33:43 -05001467 crypto_hash_final(&tcp_conn->tx_hash, (u8*)digest);
Mike Christie62f38302006-08-31 18:09:27 -04001468 iscsi_buf_init_iov(buf, (char*)digest, 4);
1469 }
Mike Christie843c0a82007-12-13 12:43:20 -06001470 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST;
Mike Christie62f38302006-08-31 18:09:27 -04001471
1472 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
1473 if (!rc)
1474 debug_scsi("sent digest 0x%x for itt 0x%x\n", *digest,
1475 ctask->itt);
1476 else {
1477 debug_scsi("sending digest 0x%x failed for itt 0x%x!\n",
1478 *digest, ctask->itt);
Mike Christie843c0a82007-12-13 12:43:20 -06001479 tcp_ctask->xmstate |= XMSTATE_W_RESEND_DATA_DIGEST;
Mike Christie62f38302006-08-31 18:09:27 -04001480 }
1481 return rc;
1482}
1483
1484static int
1485iscsi_send_data(struct iscsi_cmd_task *ctask, struct iscsi_buf *sendbuf,
1486 struct scatterlist **sg, int *sent, int *count,
1487 struct iscsi_buf *digestbuf, uint32_t *digest)
1488{
1489 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1490 struct iscsi_conn *conn = ctask->conn;
1491 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1492 int rc, buf_sent, offset;
1493
1494 while (*count) {
1495 buf_sent = 0;
1496 offset = sendbuf->sent;
1497
1498 rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent);
1499 *sent = *sent + buf_sent;
1500 if (buf_sent && conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001501 partial_sg_digest_update(&tcp_conn->tx_hash,
Mike Christie62f38302006-08-31 18:09:27 -04001502 &sendbuf->sg, sendbuf->sg.offset + offset,
1503 buf_sent);
1504 if (!iscsi_buf_left(sendbuf) && *sg != tcp_ctask->bad_sg) {
1505 iscsi_buf_init_sg(sendbuf, *sg);
1506 *sg = *sg + 1;
1507 }
1508
1509 if (rc)
1510 return rc;
1511 }
1512
1513 rc = iscsi_send_padding(conn, ctask);
1514 if (rc)
1515 return rc;
1516
1517 return iscsi_send_digest(conn, ctask, digestbuf, digest);
1518}
1519
1520static int
1521iscsi_send_unsol_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001522{
Mike Christie5bb0b552006-04-06 21:26:46 -05001523 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001524 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001525 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001526
Mike Christie843c0a82007-12-13 12:43:20 -06001527 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1528 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Mike Christie62f38302006-08-31 18:09:27 -04001529 dtask = &tcp_ctask->unsol_dtask;
1530
Mike Christieffd04362006-08-31 18:09:24 -04001531 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
1532 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
1533 sizeof(struct iscsi_hdr));
Mike Christieaf973482005-09-12 21:01:32 -05001534 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001535 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001536 (u8*)dtask->hdrext);
Mike Christie62f38302006-08-31 18:09:27 -04001537
Mike Christie843c0a82007-12-13 12:43:20 -06001538 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Mike Christie62f38302006-08-31 18:09:27 -04001539 iscsi_set_padding(tcp_ctask, ctask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001540 }
Mike Christie3219e522006-05-30 00:37:28 -05001541
1542 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1543 if (rc) {
Mike Christie843c0a82007-12-13 12:43:20 -06001544 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1545 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001546 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001547 }
1548
Mike Christiedd8c0d92006-08-31 18:09:28 -04001549 if (conn->datadgst_en) {
1550 dtask = &tcp_ctask->unsol_dtask;
1551 iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
1552 dtask->digest = 0;
1553 }
1554
Alex Aizman7ba24712005-08-04 19:30:08 -07001555 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001556 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001557 return 0;
1558}
1559
Mike Christie62f38302006-08-31 18:09:27 -04001560static int
1561iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001562{
Mike Christie5bb0b552006-04-06 21:26:46 -05001563 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001564 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001565
Mike Christie843c0a82007-12-13 12:43:20 -06001566 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
Mike Christie62f38302006-08-31 18:09:27 -04001567 BUG_ON(!ctask->unsol_count);
Mike Christie843c0a82007-12-13 12:43:20 -06001568 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
Mike Christie62f38302006-08-31 18:09:27 -04001569send_hdr:
1570 rc = iscsi_send_unsol_hdr(conn, ctask);
1571 if (rc)
1572 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001573 }
1574
Mike Christie843c0a82007-12-13 12:43:20 -06001575 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
Mike Christie62f38302006-08-31 18:09:27 -04001576 struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask;
Mike Christie5bb0b552006-04-06 21:26:46 -05001577 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001578
Mike Christie62f38302006-08-31 18:09:27 -04001579 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1580 &tcp_ctask->sent, &ctask->data_count,
1581 &dtask->digestbuf, &dtask->digest);
Mike Christie5bb0b552006-04-06 21:26:46 -05001582 ctask->unsol_count -= tcp_ctask->sent - start;
Mike Christie62f38302006-08-31 18:09:27 -04001583 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001584 return rc;
Mike Christie843c0a82007-12-13 12:43:20 -06001585 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
Mike Christie62f38302006-08-31 18:09:27 -04001586 /*
1587 * Done with the Data-Out. Next, check if we need
1588 * to send another unsolicited Data-Out.
1589 */
1590 if (ctask->unsol_count) {
1591 debug_scsi("sending more uns\n");
Mike Christie843c0a82007-12-13 12:43:20 -06001592 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
Mike Christie62f38302006-08-31 18:09:27 -04001593 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001594 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001595 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001596 return 0;
1597}
1598
Mike Christie62f38302006-08-31 18:09:27 -04001599static int iscsi_send_sol_pdu(struct iscsi_conn *conn,
1600 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001601{
Mike Christie5bb0b552006-04-06 21:26:46 -05001602 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie62f38302006-08-31 18:09:27 -04001603 struct iscsi_session *session = conn->session;
1604 struct iscsi_r2t_info *r2t;
1605 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001606 int left, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001607
Mike Christie843c0a82007-12-13 12:43:20 -06001608 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR_INIT) {
Mike Christiedb37c502006-11-08 15:58:33 -06001609 if (!tcp_ctask->r2t) {
1610 spin_lock_bh(&session->lock);
Mike Christie62f38302006-08-31 18:09:27 -04001611 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1612 sizeof(void*));
Mike Christiedb37c502006-11-08 15:58:33 -06001613 spin_unlock_bh(&session->lock);
1614 }
Mike Christie62f38302006-08-31 18:09:27 -04001615send_hdr:
1616 r2t = tcp_ctask->r2t;
1617 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001618
Mike Christie62f38302006-08-31 18:09:27 -04001619 if (conn->hdrdgst_en)
1620 iscsi_hdr_digest(conn, &r2t->headbuf,
1621 (u8*)dtask->hdrext);
Mike Christie843c0a82007-12-13 12:43:20 -06001622 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR_INIT;
1623 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
Mike Christie218432c2007-05-30 12:57:17 -05001624 }
1625
Mike Christie843c0a82007-12-13 12:43:20 -06001626 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
Mike Christie218432c2007-05-30 12:57:17 -05001627 r2t = tcp_ctask->r2t;
1628 dtask = &r2t->dtask;
1629
Mike Christie62f38302006-08-31 18:09:27 -04001630 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
Mike Christie218432c2007-05-30 12:57:17 -05001631 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001632 return rc;
Mike Christie843c0a82007-12-13 12:43:20 -06001633 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1634 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001635
Mike Christiedd8c0d92006-08-31 18:09:28 -04001636 if (conn->datadgst_en) {
1637 iscsi_data_digest_init(conn->dd_data, tcp_ctask);
1638 dtask->digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001639 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001640
Mike Christie62f38302006-08-31 18:09:27 -04001641 iscsi_set_padding(tcp_ctask, r2t->data_count);
1642 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1643 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1644 r2t->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001645 }
1646
Mike Christie843c0a82007-12-13 12:43:20 -06001647 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
Mike Christie62f38302006-08-31 18:09:27 -04001648 r2t = tcp_ctask->r2t;
1649 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001650
Mike Christie62f38302006-08-31 18:09:27 -04001651 rc = iscsi_send_data(ctask, &r2t->sendbuf, &r2t->sg,
1652 &r2t->sent, &r2t->data_count,
1653 &dtask->digestbuf, &dtask->digest);
1654 if (rc)
1655 return rc;
Mike Christie843c0a82007-12-13 12:43:20 -06001656 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001657
Mike Christie62f38302006-08-31 18:09:27 -04001658 /*
1659 * Done with this Data-Out. Next, check if we have
1660 * to send another Data-Out for this R2T.
1661 */
1662 BUG_ON(r2t->data_length - r2t->sent < 0);
1663 left = r2t->data_length - r2t->sent;
1664 if (left) {
1665 iscsi_solicit_data_cont(conn, ctask, r2t, left);
Mike Christie62f38302006-08-31 18:09:27 -04001666 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001667 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001668
Mike Christie62f38302006-08-31 18:09:27 -04001669 /*
1670 * Done with this R2T. Check if there are more
1671 * outstanding R2Ts ready to be processed.
1672 */
1673 spin_lock_bh(&session->lock);
1674 tcp_ctask->r2t = NULL;
1675 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
1676 sizeof(void*));
1677 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t,
1678 sizeof(void*))) {
1679 tcp_ctask->r2t = r2t;
Mike Christie62f38302006-08-31 18:09:27 -04001680 spin_unlock_bh(&session->lock);
1681 goto send_hdr;
1682 }
1683 spin_unlock_bh(&session->lock);
1684 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001685 return 0;
1686}
1687
Mike Christie218432c2007-05-30 12:57:17 -05001688/**
1689 * iscsi_tcp_ctask_xmit - xmit normal PDU task
1690 * @conn: iscsi connection
1691 * @ctask: iscsi command task
1692 *
1693 * Notes:
1694 * The function can return -EAGAIN in which case caller must
1695 * call it again later, or recover. '0' return code means successful
1696 * xmit.
1697 * The function is devided to logical helpers (above) for the different
1698 * xmit stages.
1699 *
1700 *iscsi_send_cmd_hdr()
Mike Christie843c0a82007-12-13 12:43:20 -06001701 * XMSTATE_CMD_HDR_INIT - prepare Header and Data buffers Calculate
1702 * Header Digest
1703 * XMSTATE_CMD_HDR_XMIT - Transmit header in progress
Mike Christie218432c2007-05-30 12:57:17 -05001704 *
1705 *iscsi_send_padding
Mike Christie843c0a82007-12-13 12:43:20 -06001706 * XMSTATE_W_PAD - Prepare and send pading
1707 * XMSTATE_W_RESEND_PAD - retry send pading
Mike Christie218432c2007-05-30 12:57:17 -05001708 *
1709 *iscsi_send_digest
Mike Christie843c0a82007-12-13 12:43:20 -06001710 * XMSTATE_W_RESEND_DATA_DIGEST - Finalize and send Data Digest
1711 * XMSTATE_W_RESEND_DATA_DIGEST - retry sending digest
Mike Christie218432c2007-05-30 12:57:17 -05001712 *
1713 *iscsi_send_unsol_hdr
Mike Christie843c0a82007-12-13 12:43:20 -06001714 * XMSTATE_UNS_INIT - prepare un-solicit data header and digest
1715 * XMSTATE_UNS_HDR - send un-solicit header
Mike Christie218432c2007-05-30 12:57:17 -05001716 *
1717 *iscsi_send_unsol_pdu
Mike Christie843c0a82007-12-13 12:43:20 -06001718 * XMSTATE_UNS_DATA - send un-solicit data in progress
Mike Christie218432c2007-05-30 12:57:17 -05001719 *
1720 *iscsi_send_sol_pdu
Mike Christie843c0a82007-12-13 12:43:20 -06001721 * XMSTATE_SOL_HDR_INIT - solicit data header and digest initialize
1722 * XMSTATE_SOL_HDR - send solicit header
1723 * XMSTATE_SOL_DATA - send solicit data
Mike Christie218432c2007-05-30 12:57:17 -05001724 *
1725 *iscsi_tcp_ctask_xmit
Mike Christie843c0a82007-12-13 12:43:20 -06001726 * XMSTATE_IMM_DATA - xmit managment data (??)
Mike Christie218432c2007-05-30 12:57:17 -05001727 **/
Alex Aizman7ba24712005-08-04 19:30:08 -07001728static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001729iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001730{
Mike Christie5bb0b552006-04-06 21:26:46 -05001731 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001732 int rc = 0;
1733
1734 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001735 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001736
Mike Christie218432c2007-05-30 12:57:17 -05001737 rc = iscsi_send_cmd_hdr(conn, ctask);
1738 if (rc)
1739 return rc;
1740 if (ctask->sc->sc_data_direction != DMA_TO_DEVICE)
1741 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001742
Mike Christie843c0a82007-12-13 12:43:20 -06001743 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Mike Christie62f38302006-08-31 18:09:27 -04001744 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1745 &tcp_ctask->sent, &ctask->imm_count,
1746 &tcp_ctask->immbuf, &tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001747 if (rc)
1748 return rc;
Mike Christie843c0a82007-12-13 12:43:20 -06001749 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001750 }
1751
Mike Christie62f38302006-08-31 18:09:27 -04001752 rc = iscsi_send_unsol_pdu(conn, ctask);
1753 if (rc)
1754 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001755
Mike Christie62f38302006-08-31 18:09:27 -04001756 rc = iscsi_send_sol_pdu(conn, ctask);
1757 if (rc)
1758 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001759
1760 return rc;
1761}
1762
Mike Christie7b8631b2006-01-13 18:05:50 -06001763static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001764iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001765{
Mike Christie7b8631b2006-01-13 18:05:50 -06001766 struct iscsi_conn *conn;
1767 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001768 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001769
Mike Christie5bb0b552006-04-06 21:26:46 -05001770 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001771 if (!cls_conn)
1772 return NULL;
1773 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001774 /*
1775 * due to strange issues with iser these are not set
1776 * in iscsi_conn_setup
1777 */
Mike Christiebf32ed32007-02-28 17:32:17 -06001778 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
Alex Aizman7ba24712005-08-04 19:30:08 -07001779
Mike Christie5bb0b552006-04-06 21:26:46 -05001780 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1781 if (!tcp_conn)
1782 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001783
Mike Christie5bb0b552006-04-06 21:26:46 -05001784 conn->dd_data = tcp_conn;
1785 tcp_conn->iscsi_conn = conn;
1786 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1787 /* initial operational parameters */
1788 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07001789
James Bottomleyc9802cd2006-09-23 15:33:43 -05001790 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1791 CRYPTO_ALG_ASYNC);
1792 tcp_conn->tx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001793 if (IS_ERR(tcp_conn->tx_hash.tfm)) {
1794 printk(KERN_ERR "Could not create connection due to crc32c "
1795 "loading error %ld. Make sure the crc32c module is "
1796 "built as a module or into the kernel\n",
1797 PTR_ERR(tcp_conn->tx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001798 goto free_tcp_conn;
Mike Christie0f238412007-02-28 17:32:21 -06001799 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001800
James Bottomleyc9802cd2006-09-23 15:33:43 -05001801 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1802 CRYPTO_ALG_ASYNC);
1803 tcp_conn->rx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001804 if (IS_ERR(tcp_conn->rx_hash.tfm)) {
1805 printk(KERN_ERR "Could not create connection due to crc32c "
1806 "loading error %ld. Make sure the crc32c module is "
1807 "built as a module or into the kernel\n",
1808 PTR_ERR(tcp_conn->rx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001809 goto free_tx_tfm;
Mike Christie0f238412007-02-28 17:32:21 -06001810 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001811
Mike Christie7b8631b2006-01-13 18:05:50 -06001812 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001813
Mike Christiedd8c0d92006-08-31 18:09:28 -04001814free_tx_tfm:
James Bottomleyc9802cd2006-09-23 15:33:43 -05001815 crypto_free_hash(tcp_conn->tx_hash.tfm);
Mike Christiedd8c0d92006-08-31 18:09:28 -04001816free_tcp_conn:
1817 kfree(tcp_conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001818tcp_conn_alloc_fail:
1819 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001820 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001821}
1822
1823static void
Mike Christie1c834692006-07-24 15:47:26 -05001824iscsi_tcp_release_conn(struct iscsi_conn *conn)
1825{
Mike Christie22236962007-05-30 12:57:24 -05001826 struct iscsi_session *session = conn->session;
Mike Christie1c834692006-07-24 15:47:26 -05001827 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie22236962007-05-30 12:57:24 -05001828 struct socket *sock = tcp_conn->sock;
Mike Christie1c834692006-07-24 15:47:26 -05001829
Mike Christie22236962007-05-30 12:57:24 -05001830 if (!sock)
Mike Christie1c834692006-07-24 15:47:26 -05001831 return;
1832
Mike Christie22236962007-05-30 12:57:24 -05001833 sock_hold(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001834 iscsi_conn_restore_callbacks(tcp_conn);
Mike Christie22236962007-05-30 12:57:24 -05001835 sock_put(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001836
Mike Christie22236962007-05-30 12:57:24 -05001837 spin_lock_bh(&session->lock);
Mike Christie1c834692006-07-24 15:47:26 -05001838 tcp_conn->sock = NULL;
1839 conn->recv_lock = NULL;
Mike Christie22236962007-05-30 12:57:24 -05001840 spin_unlock_bh(&session->lock);
1841 sockfd_put(sock);
Mike Christie1c834692006-07-24 15:47:26 -05001842}
1843
1844static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001845iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001846{
Mike Christie7b8631b2006-01-13 18:05:50 -06001847 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001848 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001849
Mike Christie1c834692006-07-24 15:47:26 -05001850 iscsi_tcp_release_conn(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001851 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001852
Pete Wyckoff534284a2006-11-08 15:58:31 -06001853 if (tcp_conn->tx_hash.tfm)
1854 crypto_free_hash(tcp_conn->tx_hash.tfm);
1855 if (tcp_conn->rx_hash.tfm)
1856 crypto_free_hash(tcp_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001857
Mike Christie5bb0b552006-04-06 21:26:46 -05001858 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001859}
1860
Mike Christie1c834692006-07-24 15:47:26 -05001861static void
1862iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1863{
1864 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christied5390f52006-08-31 18:09:30 -04001865 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie1c834692006-07-24 15:47:26 -05001866
1867 iscsi_conn_stop(cls_conn, flag);
1868 iscsi_tcp_release_conn(conn);
Mike Christied5390f52006-08-31 18:09:30 -04001869 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christie1c834692006-07-24 15:47:26 -05001870}
1871
Mike Christie22236962007-05-30 12:57:24 -05001872static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
1873 char *buf, int *port,
1874 int (*getname)(struct socket *, struct sockaddr *,
1875 int *addrlen))
1876{
1877 struct sockaddr_storage *addr;
1878 struct sockaddr_in6 *sin6;
1879 struct sockaddr_in *sin;
1880 int rc = 0, len;
1881
Al Viroa9204872007-07-20 16:03:40 +01001882 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
Mike Christie22236962007-05-30 12:57:24 -05001883 if (!addr)
1884 return -ENOMEM;
1885
1886 if (getname(sock, (struct sockaddr *) addr, &len)) {
1887 rc = -ENODEV;
1888 goto free_addr;
1889 }
1890
1891 switch (addr->ss_family) {
1892 case AF_INET:
1893 sin = (struct sockaddr_in *)addr;
1894 spin_lock_bh(&conn->session->lock);
1895 sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
1896 *port = be16_to_cpu(sin->sin_port);
1897 spin_unlock_bh(&conn->session->lock);
1898 break;
1899 case AF_INET6:
1900 sin6 = (struct sockaddr_in6 *)addr;
1901 spin_lock_bh(&conn->session->lock);
1902 sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
1903 *port = be16_to_cpu(sin6->sin6_port);
1904 spin_unlock_bh(&conn->session->lock);
1905 break;
1906 }
1907free_addr:
1908 kfree(addr);
1909 return rc;
1910}
1911
Alex Aizman7ba24712005-08-04 19:30:08 -07001912static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001913iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001914 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001915 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001916{
Mike Christie5bb0b552006-04-06 21:26:46 -05001917 struct iscsi_conn *conn = cls_conn->dd_data;
1918 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001919 struct sock *sk;
1920 struct socket *sock;
1921 int err;
1922
1923 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001924 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001925 if (!sock) {
1926 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1927 return -EEXIST;
1928 }
Mike Christie22236962007-05-30 12:57:24 -05001929 /*
1930 * copy these values now because if we drop the session
1931 * userspace may still want to query the values since we will
1932 * be using them for the reconnect
1933 */
1934 err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1935 &conn->portal_port, kernel_getpeername);
1936 if (err)
1937 goto free_socket;
1938
1939 err = iscsi_tcp_get_addr(conn, sock, conn->local_address,
1940 &conn->local_port, kernel_getsockname);
1941 if (err)
1942 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001943
Mike Christie5bb0b552006-04-06 21:26:46 -05001944 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1945 if (err)
Mike Christie22236962007-05-30 12:57:24 -05001946 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001947
Mike Christie67a61112006-05-30 00:37:20 -05001948 /* bind iSCSI connection and socket */
1949 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001950
Mike Christie67a61112006-05-30 00:37:20 -05001951 /* setup Socket parameters */
1952 sk = sock->sk;
1953 sk->sk_reuse = 1;
1954 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1955 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001956
Mike Christie67a61112006-05-30 00:37:20 -05001957 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07001958
Mike Christie67a61112006-05-30 00:37:20 -05001959 /*
1960 * Intercept TCP callbacks for sendfile like receive
1961 * processing.
1962 */
1963 conn->recv_lock = &sk->sk_callback_lock;
1964 iscsi_conn_set_callbacks(conn);
1965 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1966 /*
1967 * set receive state machine into initial state
1968 */
1969 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07001970 return 0;
Mike Christie22236962007-05-30 12:57:24 -05001971
1972free_socket:
1973 sockfd_put(sock);
1974 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001975}
1976
Mike Christie5bb0b552006-04-06 21:26:46 -05001977/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05001978static void
Mike Christie77a23c22007-05-30 12:57:18 -05001979iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Mike Christie30a6c652006-04-06 21:13:39 -05001980{
Mike Christie5bb0b552006-04-06 21:26:46 -05001981 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie843c0a82007-12-13 12:43:20 -06001982 tcp_mtask->xmstate = XMSTATE_IMM_HDR_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001983}
1984
1985static int
1986iscsi_r2tpool_alloc(struct iscsi_session *session)
1987{
1988 int i;
1989 int cmd_i;
1990
1991 /*
1992 * initialize per-task: R2T pool and xmit queue
1993 */
1994 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1995 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05001996 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001997
1998 /*
1999 * pre-allocated x4 as much r2ts to handle race when
2000 * target acks DataOut faster than we data_xmit() queues
2001 * could replenish r2tqueue.
2002 */
2003
2004 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05002005 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
2006 (void***)&tcp_ctask->r2ts,
2007 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002008 goto r2t_alloc_fail;
2009 }
2010
2011 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05002012 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07002013 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05002014 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2015 iscsi_pool_free(&tcp_ctask->r2tpool,
2016 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002017 goto r2t_alloc_fail;
2018 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002019 }
2020
2021 return 0;
2022
2023r2t_alloc_fail:
2024 for (i = 0; i < cmd_i; 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 return -ENOMEM;
2033}
2034
2035static void
2036iscsi_r2tpool_free(struct iscsi_session *session)
2037{
2038 int i;
2039
2040 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002041 struct iscsi_cmd_task *ctask = session->cmds[i];
2042 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2043
Mike Christie5bb0b552006-04-06 21:26:46 -05002044 kfifo_free(tcp_ctask->r2tqueue);
2045 iscsi_pool_free(&tcp_ctask->r2tpool,
2046 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002047 }
2048}
2049
Alex Aizman7ba24712005-08-04 19:30:08 -07002050static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002051iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002052 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07002053{
Mike Christie7b7232f2006-02-01 21:06:49 -06002054 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002055 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05002056 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002057 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07002058
Alex Aizman7ba24712005-08-04 19:30:08 -07002059 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002060 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002061 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002062 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christiedd8c0d92006-08-31 18:09:28 -04002063 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05002064 tcp_conn->hdr_size += sizeof(__u32);
Alex Aizman7ba24712005-08-04 19:30:08 -07002065 break;
2066 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002067 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002068 tcp_conn->sendpage = conn->datadgst_en ?
2069 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07002070 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002071 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002072 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07002073 if (session->max_r2t == roundup_pow_of_two(value))
2074 break;
2075 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002076 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002077 if (session->max_r2t & (session->max_r2t - 1))
2078 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2079 if (iscsi_r2tpool_alloc(session))
2080 return -ENOMEM;
2081 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002082 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002083 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002084 }
2085
2086 return 0;
2087}
2088
2089static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05002090iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2091 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06002092{
Mike Christie7b7232f2006-02-01 21:06:49 -06002093 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002094 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06002095
2096 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002097 case ISCSI_PARAM_CONN_PORT:
Mike Christie22236962007-05-30 12:57:24 -05002098 spin_lock_bh(&conn->session->lock);
2099 len = sprintf(buf, "%hu\n", conn->portal_port);
2100 spin_unlock_bh(&conn->session->lock);
Mike Christie8d2860b2006-05-02 19:46:47 -05002101 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002102 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie22236962007-05-30 12:57:24 -05002103 spin_lock_bh(&conn->session->lock);
2104 len = sprintf(buf, "%s\n", conn->portal_address);
2105 spin_unlock_bh(&conn->session->lock);
Mike Christiefd7255f2006-04-06 21:13:36 -05002106 break;
2107 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002108 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05002109 }
2110
2111 return len;
2112}
2113
Mike Christie22236962007-05-30 12:57:24 -05002114static int
2115iscsi_tcp_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2116 char *buf)
2117{
2118 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2119 int len;
2120
2121 switch (param) {
2122 case ISCSI_HOST_PARAM_IPADDRESS:
2123 spin_lock_bh(&session->lock);
2124 if (!session->leadconn)
2125 len = -ENODEV;
2126 else
2127 len = sprintf(buf, "%s\n",
2128 session->leadconn->local_address);
2129 spin_unlock_bh(&session->lock);
2130 break;
2131 default:
2132 return iscsi_host_get_param(shost, param, buf);
2133 }
2134 return len;
2135}
2136
Alex Aizman7ba24712005-08-04 19:30:08 -07002137static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002138iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002139{
Mike Christie7b7232f2006-02-01 21:06:49 -06002140 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002141 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002142
2143 stats->txdata_octets = conn->txdata_octets;
2144 stats->rxdata_octets = conn->rxdata_octets;
2145 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2146 stats->dataout_pdus = conn->dataout_pdus_cnt;
2147 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2148 stats->datain_pdus = conn->datain_pdus_cnt;
2149 stats->r2t_pdus = conn->r2t_pdus_cnt;
2150 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2151 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2152 stats->custom_length = 3;
2153 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002154 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002155 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002156 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002157 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2158 stats->custom[2].value = conn->eh_abort_cnt;
2159}
2160
Mike Christie5bb0b552006-04-06 21:26:46 -05002161static struct iscsi_cls_session *
2162iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2163 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05002164 uint16_t cmds_max, uint16_t qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05002165 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002166{
Mike Christie5bb0b552006-04-06 21:26:46 -05002167 struct iscsi_cls_session *cls_session;
2168 struct iscsi_session *session;
2169 uint32_t hn;
2170 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002171
Mike Christie15482712007-05-30 12:57:19 -05002172 cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05002173 sizeof(struct iscsi_tcp_cmd_task),
2174 sizeof(struct iscsi_tcp_mgmt_task),
2175 initial_cmdsn, &hn);
2176 if (!cls_session)
2177 return NULL;
2178 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002179
Mike Christie5bb0b552006-04-06 21:26:46 -05002180 session = class_to_transport_session(cls_session);
2181 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2182 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2183 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2184
2185 ctask->hdr = &tcp_ctask->hdr;
2186 }
2187
2188 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2189 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2190 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2191
2192 mtask->hdr = &tcp_mtask->hdr;
2193 }
2194
2195 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2196 goto r2tpool_alloc_fail;
2197
2198 return cls_session;
2199
2200r2tpool_alloc_fail:
2201 iscsi_session_teardown(cls_session);
2202 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002203}
2204
Mike Christie5bb0b552006-04-06 21:26:46 -05002205static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2206{
Mike Christie5bb0b552006-04-06 21:26:46 -05002207 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2208 iscsi_session_teardown(cls_session);
2209}
2210
Mike Christied1d81c02007-05-30 12:57:21 -05002211static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
2212{
Mike Christieb6d44fe2007-07-26 12:46:47 -05002213 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
Mike Christied1d81c02007-05-30 12:57:21 -05002214 blk_queue_dma_alignment(sdev->request_queue, 0);
2215 return 0;
2216}
2217
Mike Christie5bb0b552006-04-06 21:26:46 -05002218static struct scsi_host_template iscsi_sht = {
Mike Christie79743922007-07-26 12:46:46 -05002219 .module = THIS_MODULE,
Mike Christief4246b32006-07-24 15:47:54 -05002220 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05002221 .queuecommand = iscsi_queuecommand,
2222 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -05002223 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie5bb0b552006-04-06 21:26:46 -05002224 .sg_tablesize = ISCSI_SG_TABLESIZE,
Mike Christie8231f0e2007-02-28 17:32:20 -06002225 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -05002226 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2227 .eh_abort_handler = iscsi_eh_abort,
Mike Christie843c0a82007-12-13 12:43:20 -06002228 .eh_device_reset_handler= iscsi_eh_device_reset,
Mike Christie5bb0b552006-04-06 21:26:46 -05002229 .eh_host_reset_handler = iscsi_eh_host_reset,
2230 .use_clustering = DISABLE_CLUSTERING,
Mike Christied1d81c02007-05-30 12:57:21 -05002231 .slave_configure = iscsi_tcp_slave_configure,
Mike Christie5bb0b552006-04-06 21:26:46 -05002232 .proc_name = "iscsi_tcp",
2233 .this_id = -1,
2234};
2235
Alex Aizman7ba24712005-08-04 19:30:08 -07002236static struct iscsi_transport iscsi_tcp_transport = {
2237 .owner = THIS_MODULE,
2238 .name = "tcp",
2239 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2240 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002241 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2242 ISCSI_MAX_XMIT_DLENGTH |
2243 ISCSI_HDRDGST_EN |
2244 ISCSI_DATADGST_EN |
2245 ISCSI_INITIAL_R2T_EN |
2246 ISCSI_MAX_R2T |
2247 ISCSI_IMM_DATA_EN |
2248 ISCSI_FIRST_BURST |
2249 ISCSI_MAX_BURST |
2250 ISCSI_PDU_INORDER_EN |
2251 ISCSI_DATASEQ_INORDER_EN |
2252 ISCSI_ERL |
2253 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002254 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05002255 ISCSI_EXP_STATSN |
2256 ISCSI_PERSISTENT_PORT |
2257 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -05002258 ISCSI_TARGET_NAME | ISCSI_TPGT |
2259 ISCSI_USERNAME | ISCSI_PASSWORD |
Mike Christie843c0a82007-12-13 12:43:20 -06002260 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
2261 ISCSI_FAST_ABORT,
Mike Christie22236962007-05-30 12:57:24 -05002262 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
Mike Christied8196ed2007-05-30 12:57:25 -05002263 ISCSI_HOST_INITIATOR_NAME |
2264 ISCSI_HOST_NETDEV_NAME,
Alex Aizman7ba24712005-08-04 19:30:08 -07002265 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002266 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002267 .max_conn = 1,
2268 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002269 /* session management */
2270 .create_session = iscsi_tcp_session_create,
2271 .destroy_session = iscsi_tcp_session_destroy,
2272 /* connection management */
2273 .create_conn = iscsi_tcp_conn_create,
2274 .bind_conn = iscsi_tcp_conn_bind,
2275 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002276 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002277 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002278 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002279 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05002280 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -05002281 /* iscsi host params */
Mike Christie22236962007-05-30 12:57:24 -05002282 .get_host_param = iscsi_tcp_host_get_param,
Mike Christie0801c242007-05-30 12:57:12 -05002283 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -05002284 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002285 .send_pdu = iscsi_conn_send_pdu,
2286 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002287 .init_cmd_task = iscsi_tcp_cmd_init,
2288 .init_mgmt_task = iscsi_tcp_mgmt_init,
2289 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2290 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2291 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2292 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002293 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002294};
2295
2296static int __init
2297iscsi_tcp_init(void)
2298{
Alex Aizman7ba24712005-08-04 19:30:08 -07002299 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002300 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2301 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002302 return -EINVAL;
2303 }
2304 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2305
Mike Christie7b8631b2006-01-13 18:05:50 -06002306 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002307 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002308
Mike Christie7b8631b2006-01-13 18:05:50 -06002309 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002310}
2311
2312static void __exit
2313iscsi_tcp_exit(void)
2314{
2315 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002316}
2317
2318module_init(iscsi_tcp_init);
2319module_exit(iscsi_tcp_exit);