blob: f2a08f7ed90205947a1f40fdf8d462589f81e9ff [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");
Olaf Kirchda32dd62007-12-13 12:43:21 -060051#undef 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
Mike Christie75613522008-05-21 15:53:59 -050067static struct scsi_transport_template *iscsi_tcp_scsi_transport;
68static struct scsi_host_template iscsi_sht;
69static struct iscsi_transport iscsi_tcp_transport;
70
Alex Aizman7ba24712005-08-04 19:30:08 -070071static unsigned int iscsi_max_lun = 512;
72module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
73
Olaf Kirchda32dd62007-12-13 12:43:21 -060074static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -060075 struct iscsi_segment *segment);
Alex Aizman7ba24712005-08-04 19:30:08 -070076
Olaf Kirchda32dd62007-12-13 12:43:21 -060077/*
Olaf Kircha8ac6312007-12-13 12:43:35 -060078 * Scatterlist handling: inside the iscsi_segment, we
Olaf Kirchda32dd62007-12-13 12:43:21 -060079 * remember an index into the scatterlist, and set data/size
80 * to the current scatterlist entry. For highmem pages, we
81 * kmap as needed.
82 *
83 * Note that the page is unmapped when we return from
84 * TCP's data_ready handler, so we may end up mapping and
85 * unmapping the same page repeatedly. The whole reason
86 * for this is that we shouldn't keep the page mapped
87 * outside the softirq.
88 */
89
90/**
Olaf Kircha8ac6312007-12-13 12:43:35 -060091 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
92 * @segment: the buffer object
93 * @sg: scatterlist
Olaf Kirchda32dd62007-12-13 12:43:21 -060094 * @offset: byte offset into that sg entry
95 *
Olaf Kircha8ac6312007-12-13 12:43:35 -060096 * This function sets up the segment so that subsequent
Olaf Kirchda32dd62007-12-13 12:43:21 -060097 * data is copied to the indicated sg entry, at the given
98 * offset.
99 */
100static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600101iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
102 struct scatterlist *sg, unsigned int offset)
Alex Aizman7ba24712005-08-04 19:30:08 -0700103{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600104 segment->sg = sg;
105 segment->sg_offset = offset;
106 segment->size = min(sg->length - offset,
107 segment->total_size - segment->total_copied);
108 segment->data = NULL;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600109}
Alex Aizman7ba24712005-08-04 19:30:08 -0700110
Olaf Kirchda32dd62007-12-13 12:43:21 -0600111/**
Olaf Kircha8ac6312007-12-13 12:43:35 -0600112 * iscsi_tcp_segment_map - map the current S/G page
113 * @segment: iscsi_segment
114 * @recv: 1 if called from recv path
Olaf Kirchda32dd62007-12-13 12:43:21 -0600115 *
116 * We only need to possibly kmap data if scatter lists are being used,
117 * because the iscsi passthrough and internal IO paths will never use high
118 * mem pages.
119 */
120static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600121iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600122{
123 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700124
Olaf Kircha8ac6312007-12-13 12:43:35 -0600125 if (segment->data != NULL || !segment->sg)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600126 return;
Alex Aizman7ba24712005-08-04 19:30:08 -0700127
Olaf Kircha8ac6312007-12-13 12:43:35 -0600128 sg = segment->sg;
129 BUG_ON(segment->sg_mapped);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600130 BUG_ON(sg->length == 0);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600131
132 /*
133 * If the page count is greater than one it is ok to send
134 * to the network layer's zero copy send path. If not we
135 * have to go the slow sendmsg path. We always map for the
136 * recv path.
137 */
138 if (page_count(sg_page(sg)) >= 1 && !recv)
139 return;
140
141 debug_tcp("iscsi_tcp_segment_map %s %p\n", recv ? "recv" : "xmit",
142 segment);
143 segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0);
144 segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600145}
Alex Aizman7ba24712005-08-04 19:30:08 -0700146
Olaf Kirchda32dd62007-12-13 12:43:21 -0600147static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600148iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600149{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600150 debug_tcp("iscsi_tcp_segment_unmap %p\n", segment);
151
152 if (segment->sg_mapped) {
153 debug_tcp("iscsi_tcp_segment_unmap valid\n");
154 kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0);
155 segment->sg_mapped = NULL;
156 segment->data = NULL;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600157 }
158}
Alex Aizman7ba24712005-08-04 19:30:08 -0700159
Olaf Kirchda32dd62007-12-13 12:43:21 -0600160/*
161 * Splice the digest buffer into the buffer
162 */
163static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600164iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600165{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600166 segment->data = digest;
167 segment->digest_len = ISCSI_DIGEST_SIZE;
168 segment->total_size += ISCSI_DIGEST_SIZE;
169 segment->size = ISCSI_DIGEST_SIZE;
170 segment->copied = 0;
171 segment->sg = NULL;
172 segment->hash = NULL;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600173}
Alex Aizman7ba24712005-08-04 19:30:08 -0700174
Olaf Kirchda32dd62007-12-13 12:43:21 -0600175/**
Olaf Kircha8ac6312007-12-13 12:43:35 -0600176 * iscsi_tcp_segment_done - check whether the segment is complete
177 * @segment: iscsi segment to check
178 * @recv: set to one of this is called from the recv path
179 * @copied: number of bytes copied
Olaf Kirchda32dd62007-12-13 12:43:21 -0600180 *
Olaf Kircha8ac6312007-12-13 12:43:35 -0600181 * Check if we're done receiving this segment. If the receive
Olaf Kirchda32dd62007-12-13 12:43:21 -0600182 * buffer is full but we expect more data, move on to the
183 * next entry in the scatterlist.
184 *
185 * If the amount of data we received isn't a multiple of 4,
186 * we will transparently receive the pad bytes, too.
187 *
188 * This function must be re-entrant.
189 */
190static inline int
Olaf Kircha8ac6312007-12-13 12:43:35 -0600191iscsi_tcp_segment_done(struct iscsi_segment *segment, int recv, unsigned copied)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600192{
193 static unsigned char padbuf[ISCSI_PAD_LEN];
Olaf Kircha8ac6312007-12-13 12:43:35 -0600194 struct scatterlist sg;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600195 unsigned int pad;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600196
Olaf Kircha8ac6312007-12-13 12:43:35 -0600197 debug_tcp("copied %u %u size %u %s\n", segment->copied, copied,
198 segment->size, recv ? "recv" : "xmit");
199 if (segment->hash && copied) {
200 /*
201 * If a segment is kmapd we must unmap it before sending
202 * to the crypto layer since that will try to kmap it again.
203 */
204 iscsi_tcp_segment_unmap(segment);
205
206 if (!segment->data) {
207 sg_init_table(&sg, 1);
208 sg_set_page(&sg, sg_page(segment->sg), copied,
209 segment->copied + segment->sg_offset +
210 segment->sg->offset);
211 } else
212 sg_init_one(&sg, segment->data + segment->copied,
213 copied);
214 crypto_hash_update(segment->hash, &sg, copied);
215 }
216
217 segment->copied += copied;
218 if (segment->copied < segment->size) {
219 iscsi_tcp_segment_map(segment, recv);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600220 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700221 }
222
Olaf Kircha8ac6312007-12-13 12:43:35 -0600223 segment->total_copied += segment->copied;
224 segment->copied = 0;
225 segment->size = 0;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600226
227 /* Unmap the current scatterlist page, if there is one. */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600228 iscsi_tcp_segment_unmap(segment);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600229
230 /* Do we have more scatterlist entries? */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600231 debug_tcp("total copied %u total size %u\n", segment->total_copied,
232 segment->total_size);
233 if (segment->total_copied < segment->total_size) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600234 /* Proceed to the next entry in the scatterlist. */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600235 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
236 0);
237 iscsi_tcp_segment_map(segment, recv);
238 BUG_ON(segment->size == 0);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600239 return 0;
240 }
241
242 /* Do we need to handle padding? */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600243 pad = iscsi_padding(segment->total_copied);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600244 if (pad != 0) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600245 debug_tcp("consume %d pad bytes\n", pad);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600246 segment->total_size += pad;
247 segment->size = pad;
248 segment->data = padbuf;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600249 return 0;
250 }
251
252 /*
Olaf Kircha8ac6312007-12-13 12:43:35 -0600253 * Set us up for transferring the data digest. hdr digest
Olaf Kirchda32dd62007-12-13 12:43:21 -0600254 * is completely handled in hdr done function.
255 */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600256 if (segment->hash) {
257 crypto_hash_final(segment->hash, segment->digest);
258 iscsi_tcp_segment_splice_digest(segment,
259 recv ? segment->recv_digest : segment->digest);
260 return 0;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600261 }
262
263 return 1;
264}
265
266/**
Olaf Kircha8ac6312007-12-13 12:43:35 -0600267 * iscsi_tcp_xmit_segment - transmit segment
Olaf Kirchda32dd62007-12-13 12:43:21 -0600268 * @tcp_conn: the iSCSI TCP connection
Olaf Kircha8ac6312007-12-13 12:43:35 -0600269 * @segment: the buffer to transmnit
270 *
271 * This function transmits as much of the buffer as
272 * the network layer will accept, and returns the number of
273 * bytes transmitted.
274 *
275 * If CRC hashing is enabled, the function will compute the
276 * hash as it goes. When the entire segment has been transmitted,
277 * it will retrieve the hash value and send it as well.
278 */
279static int
280iscsi_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
281 struct iscsi_segment *segment)
282{
283 struct socket *sk = tcp_conn->sock;
284 unsigned int copied = 0;
285 int r = 0;
286
287 while (!iscsi_tcp_segment_done(segment, 0, r)) {
288 struct scatterlist *sg;
289 unsigned int offset, copy;
290 int flags = 0;
291
292 r = 0;
293 offset = segment->copied;
294 copy = segment->size - offset;
295
296 if (segment->total_copied + segment->size < segment->total_size)
297 flags |= MSG_MORE;
298
299 /* Use sendpage if we can; else fall back to sendmsg */
300 if (!segment->data) {
301 sg = segment->sg;
302 offset += segment->sg_offset + sg->offset;
303 r = tcp_conn->sendpage(sk, sg_page(sg), offset, copy,
304 flags);
305 } else {
306 struct msghdr msg = { .msg_flags = flags };
307 struct kvec iov = {
308 .iov_base = segment->data + offset,
309 .iov_len = copy
310 };
311
312 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
313 }
314
315 if (r < 0) {
316 iscsi_tcp_segment_unmap(segment);
317 if (copied || r == -EAGAIN)
318 break;
319 return r;
320 }
321 copied += r;
322 }
323 return copied;
324}
325
326/**
327 * iscsi_tcp_segment_recv - copy data to segment
328 * @tcp_conn: the iSCSI TCP connection
329 * @segment: the buffer to copy to
Olaf Kirchda32dd62007-12-13 12:43:21 -0600330 * @ptr: data pointer
331 * @len: amount of data available
332 *
333 * This function copies up to @len bytes to the
334 * given buffer, and returns the number of bytes
335 * consumed, which can actually be less than @len.
336 *
337 * If hash digest is enabled, the function will update the
338 * hash while copying.
339 * Combining these two operations doesn't buy us a lot (yet),
340 * but in the future we could implement combined copy+crc,
341 * just way we do for network layer checksums.
342 */
343static int
Olaf Kircha8ac6312007-12-13 12:43:35 -0600344iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
345 struct iscsi_segment *segment, const void *ptr,
346 unsigned int len)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600347{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600348 unsigned int copy = 0, copied = 0;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600349
Olaf Kircha8ac6312007-12-13 12:43:35 -0600350 while (!iscsi_tcp_segment_done(segment, 1, copy)) {
351 if (copied == len) {
352 debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n",
353 len);
354 break;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600355 }
Olaf Kircha8ac6312007-12-13 12:43:35 -0600356
357 copy = min(len - copied, segment->size - segment->copied);
358 debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy);
359 memcpy(segment->data + segment->copied, ptr + copied, copy);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600360 copied += copy;
361 }
Olaf Kirchda32dd62007-12-13 12:43:21 -0600362 return copied;
363}
364
365static inline void
366iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
367 unsigned char digest[ISCSI_DIGEST_SIZE])
368{
369 struct scatterlist sg;
370
371 sg_init_one(&sg, hdr, hdrlen);
372 crypto_hash_digest(hash, &sg, hdrlen, digest);
373}
374
375static inline int
376iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600377 struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600378{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600379 if (!segment->digest_len)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600380 return 1;
381
Olaf Kircha8ac6312007-12-13 12:43:35 -0600382 if (memcmp(segment->recv_digest, segment->digest,
383 segment->digest_len)) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600384 debug_scsi("digest mismatch\n");
385 return 0;
386 }
387
388 return 1;
389}
390
391/*
Olaf Kircha8ac6312007-12-13 12:43:35 -0600392 * Helper function to set up segment buffer
Olaf Kirchda32dd62007-12-13 12:43:21 -0600393 */
394static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600395__iscsi_segment_init(struct iscsi_segment *segment, size_t size,
396 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600397{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600398 memset(segment, 0, sizeof(*segment));
399 segment->total_size = size;
400 segment->done = done;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600401
402 if (hash) {
Olaf Kircha8ac6312007-12-13 12:43:35 -0600403 segment->hash = hash;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600404 crypto_hash_init(hash);
405 }
406}
407
408static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600409iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
410 size_t size, iscsi_segment_done_fn_t *done,
411 struct hash_desc *hash)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600412{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600413 __iscsi_segment_init(segment, size, done, hash);
414 segment->data = data;
415 segment->size = size;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600416}
417
418static inline int
Olaf Kircha8ac6312007-12-13 12:43:35 -0600419iscsi_segment_seek_sg(struct iscsi_segment *segment,
420 struct scatterlist *sg_list, unsigned int sg_count,
421 unsigned int offset, size_t size,
422 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600423{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600424 struct scatterlist *sg;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600425 unsigned int i;
426
Olaf Kircha8ac6312007-12-13 12:43:35 -0600427 debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n",
428 offset, size);
429 __iscsi_segment_init(segment, size, done, hash);
430 for_each_sg(sg_list, sg, sg_count, i) {
431 debug_scsi("sg %d, len %u offset %u\n", i, sg->length,
432 sg->offset);
433 if (offset < sg->length) {
434 iscsi_tcp_segment_init_sg(segment, sg, offset);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600435 return 0;
436 }
Olaf Kircha8ac6312007-12-13 12:43:35 -0600437 offset -= sg->length;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600438 }
439
440 return ISCSI_ERR_DATA_OFFSET;
441}
442
443/**
Olaf Kircha8ac6312007-12-13 12:43:35 -0600444 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
Olaf Kirchda32dd62007-12-13 12:43:21 -0600445 * @tcp_conn: iscsi connection to prep for
446 *
447 * This function always passes NULL for the hash argument, because when this
448 * function is called we do not yet know the final size of the header and want
449 * to delay the digest processing until we know that.
450 */
451static void
452iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
453{
454 debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn,
455 tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : "");
Olaf Kircha8ac6312007-12-13 12:43:35 -0600456 iscsi_segment_init_linear(&tcp_conn->in.segment,
Olaf Kirchda32dd62007-12-13 12:43:21 -0600457 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
458 iscsi_tcp_hdr_recv_done, NULL);
459}
460
461/*
462 * Handle incoming reply to any other type of command
463 */
464static int
465iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600466 struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600467{
468 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
469 int rc = 0;
470
Olaf Kircha8ac6312007-12-13 12:43:35 -0600471 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600472 return ISCSI_ERR_DATA_DGST;
473
474 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
475 conn->data, tcp_conn->in.datalen);
476 if (rc)
477 return rc;
478
479 iscsi_tcp_hdr_recv_prep(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700480 return 0;
481}
482
Olaf Kirchda32dd62007-12-13 12:43:21 -0600483static void
484iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
485{
486 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
487 struct hash_desc *rx_hash = NULL;
488
489 if (conn->datadgst_en)
490 rx_hash = &tcp_conn->rx_hash;
491
Olaf Kircha8ac6312007-12-13 12:43:35 -0600492 iscsi_segment_init_linear(&tcp_conn->in.segment,
Olaf Kirchda32dd62007-12-13 12:43:21 -0600493 conn->data, tcp_conn->in.datalen,
494 iscsi_tcp_data_recv_done, rx_hash);
495}
496
Mike Christie30a6c652006-04-06 21:13:39 -0500497/*
498 * must be called with session lock
499 */
500static void
Mike Christieb6c395ed2006-07-24 15:47:15 -0500501iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700502{
Mike Christie5bb0b552006-04-06 21:26:46 -0500503 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christieb6c395ed2006-07-24 15:47:15 -0500504 struct iscsi_r2t_info *r2t;
Alex Aizman7ba24712005-08-04 19:30:08 -0700505
Mike Christieb6c395ed2006-07-24 15:47:15 -0500506 /* flush ctask's r2t queues */
507 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
508 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
509 sizeof(void*));
510 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
511 }
512
Olaf Kircha8ac6312007-12-13 12:43:35 -0600513 r2t = tcp_ctask->r2t;
514 if (r2t != NULL) {
515 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
516 sizeof(void*));
517 tcp_ctask->r2t = NULL;
518 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700519}
520
521/**
522 * iscsi_data_rsp - SCSI Data-In Response processing
523 * @conn: iscsi connection
524 * @ctask: scsi command task
525 **/
526static int
527iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
528{
Mike Christie5bb0b552006-04-06 21:26:46 -0500529 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
530 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
531 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700532 struct iscsi_session *session = conn->session;
Mike Christie857ae0b2007-05-30 12:57:15 -0500533 struct scsi_cmnd *sc = ctask->sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700534 int datasn = be32_to_cpu(rhdr->datasn);
Boaz Harrosh94795b62008-04-18 10:11:53 -0500535 unsigned total_in_length = scsi_in(sc)->length;
Alex Aizman7ba24712005-08-04 19:30:08 -0700536
Mike Christie77a23c22007-05-30 12:57:18 -0500537 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie5bb0b552006-04-06 21:26:46 -0500538 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700539 return 0;
540
Mike Christied473cc72007-05-30 12:57:14 -0500541 if (tcp_ctask->exp_datasn != datasn) {
542 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
543 __FUNCTION__, tcp_ctask->exp_datasn, datasn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700544 return ISCSI_ERR_DATASN;
Mike Christied473cc72007-05-30 12:57:14 -0500545 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700546
Mike Christied473cc72007-05-30 12:57:14 -0500547 tcp_ctask->exp_datasn++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700548
Mike Christie5bb0b552006-04-06 21:26:46 -0500549 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
Boaz Harrosh94795b62008-04-18 10:11:53 -0500550 if (tcp_ctask->data_offset + tcp_conn->in.datalen > total_in_length) {
Mike Christie857ae0b2007-05-30 12:57:15 -0500551 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
552 __FUNCTION__, tcp_ctask->data_offset,
Boaz Harrosh94795b62008-04-18 10:11:53 -0500553 tcp_conn->in.datalen, total_in_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700554 return ISCSI_ERR_DATA_OFFSET;
Mike Christie857ae0b2007-05-30 12:57:15 -0500555 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700556
557 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600558 sc->result = (DID_OK << 16) | rhdr->cmd_status;
Alex Aizman7ba24712005-08-04 19:30:08 -0700559 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600560 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
561 ISCSI_FLAG_DATA_OVERFLOW)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700562 int res_count = be32_to_cpu(rhdr->residual_count);
563
564 if (res_count > 0 &&
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600565 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
Boaz Harrosh94795b62008-04-18 10:11:53 -0500566 res_count <= total_in_length))
567 scsi_in(sc)->resid = res_count;
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600568 else
Alex Aizman7ba24712005-08-04 19:30:08 -0700569 sc->result = (DID_BAD_TARGET << 16) |
570 rhdr->cmd_status;
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600571 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700572 }
573
574 conn->datain_pdus_cnt++;
575 return 0;
576}
577
578/**
579 * iscsi_solicit_data_init - initialize first Data-Out
580 * @conn: iscsi connection
581 * @ctask: scsi command task
582 * @r2t: R2T info
583 *
584 * Notes:
585 * Initialize first Data-Out within this R2T sequence and finds
586 * proper data_offset within this SCSI command.
587 *
588 * This function is called with connection lock taken.
589 **/
590static void
591iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
592 struct iscsi_r2t_info *r2t)
593{
594 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700595
Mike Christieffbfe922006-05-18 20:31:36 -0500596 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700597 memset(hdr, 0, sizeof(struct iscsi_data));
598 hdr->ttt = r2t->ttt;
599 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
600 r2t->solicit_datasn++;
601 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500602 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
603 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700604 hdr->exp_statsn = r2t->exp_statsn;
605 hdr->offset = cpu_to_be32(r2t->data_offset);
606 if (r2t->data_length > conn->max_xmit_dlength) {
607 hton24(hdr->dlength, conn->max_xmit_dlength);
608 r2t->data_count = conn->max_xmit_dlength;
609 hdr->flags = 0;
610 } else {
611 hton24(hdr->dlength, r2t->data_length);
612 r2t->data_count = r2t->data_length;
613 hdr->flags = ISCSI_FLAG_CMD_FINAL;
614 }
615 conn->dataout_pdus_cnt++;
616
617 r2t->sent = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700618}
619
620/**
621 * iscsi_r2t_rsp - iSCSI R2T Response processing
622 * @conn: iscsi connection
623 * @ctask: scsi command task
624 **/
625static int
626iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
627{
628 struct iscsi_r2t_info *r2t;
629 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500630 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
631 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
632 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700633 int r2tsn = be32_to_cpu(rhdr->r2tsn);
634 int rc;
635
Mike Christie98a94162006-08-31 18:09:26 -0400636 if (tcp_conn->in.datalen) {
Mike Christie322d7392008-01-31 13:36:52 -0600637 iscsi_conn_printk(KERN_ERR, conn,
638 "invalid R2t with datalen %d\n",
639 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700640 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400641 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700642
Mike Christied473cc72007-05-30 12:57:14 -0500643 if (tcp_ctask->exp_datasn != r2tsn){
644 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
645 __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700646 return ISCSI_ERR_R2TSN;
Mike Christied473cc72007-05-30 12:57:14 -0500647 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700648
Alex Aizman7ba24712005-08-04 19:30:08 -0700649 /* fill-in new R2T associated with the task */
Mike Christie77a23c22007-05-30 12:57:18 -0500650 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
651
Mike Christie843c0a82007-12-13 12:43:20 -0600652 if (!ctask->sc || session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie322d7392008-01-31 13:36:52 -0600653 iscsi_conn_printk(KERN_INFO, conn,
654 "dropping R2T itt %d in recovery.\n",
655 ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -0700656 return 0;
657 }
Mike Christieb6c395ed2006-07-24 15:47:15 -0500658
Mike Christie5bb0b552006-04-06 21:26:46 -0500659 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700660 BUG_ON(!rc);
661
662 r2t->exp_statsn = rhdr->statsn;
663 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400664 if (r2t->data_length == 0) {
Mike Christie322d7392008-01-31 13:36:52 -0600665 iscsi_conn_printk(KERN_ERR, conn,
666 "invalid R2T with zero data len\n");
Olaf Kirch03766a12007-12-13 12:43:36 -0600667 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
668 sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700669 return ISCSI_ERR_DATALEN;
670 }
671
Mike Christie98a94162006-08-31 18:09:26 -0400672 if (r2t->data_length > session->max_burst)
673 debug_scsi("invalid R2T with data len %u and max burst %u."
674 "Attempting to execute request.\n",
675 r2t->data_length, session->max_burst);
676
Alex Aizman7ba24712005-08-04 19:30:08 -0700677 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
Boaz Harrosh94795b62008-04-18 10:11:53 -0500678 if (r2t->data_offset + r2t->data_length > scsi_out(ctask->sc)->length) {
Mike Christie322d7392008-01-31 13:36:52 -0600679 iscsi_conn_printk(KERN_ERR, conn,
680 "invalid R2T with data len %u at offset %u "
681 "and total length %d\n", r2t->data_length,
Boaz Harrosh94795b62008-04-18 10:11:53 -0500682 r2t->data_offset, scsi_out(ctask->sc)->length);
Olaf Kirch03766a12007-12-13 12:43:36 -0600683 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
684 sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700685 return ISCSI_ERR_DATALEN;
686 }
687
688 r2t->ttt = rhdr->ttt; /* no flip */
689 r2t->solicit_datasn = 0;
690
691 iscsi_solicit_data_init(conn, ctask, r2t);
692
Mike Christied473cc72007-05-30 12:57:14 -0500693 tcp_ctask->exp_datasn = r2tsn + 1;
Mike Christie5bb0b552006-04-06 21:26:46 -0500694 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700695 conn->r2t_pdus_cnt++;
Mike Christie843c0a82007-12-13 12:43:20 -0600696
697 iscsi_requeue_ctask(ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -0700698 return 0;
699}
700
Olaf Kirchda32dd62007-12-13 12:43:21 -0600701/*
702 * Handle incoming reply to DataIn command
703 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700704static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600705iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600706 struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600707{
708 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
709 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
710 int rc;
711
Olaf Kircha8ac6312007-12-13 12:43:35 -0600712 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600713 return ISCSI_ERR_DATA_DGST;
714
715 /* check for non-exceptional status */
716 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
717 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
718 if (rc)
719 return rc;
720 }
721
722 iscsi_tcp_hdr_recv_prep(tcp_conn);
723 return 0;
724}
725
726/**
727 * iscsi_tcp_hdr_dissect - process PDU header
728 * @conn: iSCSI connection
729 * @hdr: PDU header
730 *
731 * This function analyzes the header of the PDU received,
732 * and performs several sanity checks. If the PDU is accompanied
733 * by data, the receive buffer is set up to copy the incoming data
734 * to the correct location.
735 */
736static int
737iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
Alex Aizman7ba24712005-08-04 19:30:08 -0700738{
Mike Christie5bb0b552006-04-06 21:26:46 -0500739 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700740 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500741 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600742 struct iscsi_cmd_task *ctask;
Alex Aizman7ba24712005-08-04 19:30:08 -0700743
744 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500745 tcp_conn->in.datalen = ntoh24(hdr->dlength);
746 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Mike Christie322d7392008-01-31 13:36:52 -0600747 iscsi_conn_printk(KERN_ERR, conn,
748 "iscsi_tcp: datalen %d > %d\n",
749 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700750 return ISCSI_ERR_DATALEN;
751 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700752
Olaf Kirchda32dd62007-12-13 12:43:21 -0600753 /* Additional header segments. So far, we don't
754 * process additional headers.
755 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500756 ahslen = hdr->hlength << 2;
Alex Aizman7ba24712005-08-04 19:30:08 -0700757
Mike Christie5bb0b552006-04-06 21:26:46 -0500758 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700759 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie0af967f2008-05-21 15:54:04 -0500760 rc = iscsi_verify_itt(conn, hdr->itt);
Mike Christie7a53dc52007-12-13 12:43:37 -0600761 if (rc)
Mike Christie5bb0b552006-04-06 21:26:46 -0500762 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700763
Olaf Kirchda32dd62007-12-13 12:43:21 -0600764 debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
765 opcode, ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700766
Mike Christie5bb0b552006-04-06 21:26:46 -0500767 switch(opcode) {
768 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie0af967f2008-05-21 15:54:04 -0500769 ctask = iscsi_itt_to_ctask(conn, hdr->itt);
770 if (!ctask)
771 return ISCSI_ERR_BAD_ITT;
772
Mike Christie4545a882007-12-13 12:43:40 -0600773 spin_lock(&conn->session->lock);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600774 rc = iscsi_data_rsp(conn, ctask);
Mike Christie4545a882007-12-13 12:43:40 -0600775 spin_unlock(&conn->session->lock);
Mike Christie275fd7d2006-07-24 15:47:17 -0500776 if (rc)
777 return rc;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600778 if (tcp_conn->in.datalen) {
779 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
780 struct hash_desc *rx_hash = NULL;
Boaz Harrosh94795b62008-04-18 10:11:53 -0500781 struct scsi_data_buffer *sdb = scsi_in(ctask->sc);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600782
783 /*
784 * Setup copy of Data-In into the Scsi_Cmnd
785 * Scatterlist case:
Olaf Kircha8ac6312007-12-13 12:43:35 -0600786 * We set up the iscsi_segment to point to the next
Olaf Kirchda32dd62007-12-13 12:43:21 -0600787 * scatterlist entry to copy to. As we go along,
788 * we move on to the next scatterlist entry and
789 * update the digest per-entry.
790 */
791 if (conn->datadgst_en)
792 rx_hash = &tcp_conn->rx_hash;
793
794 debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
795 "datalen=%d)\n", tcp_conn,
796 tcp_ctask->data_offset,
797 tcp_conn->in.datalen);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600798 return iscsi_segment_seek_sg(&tcp_conn->in.segment,
Boaz Harrosh94795b62008-04-18 10:11:53 -0500799 sdb->table.sgl,
800 sdb->table.nents,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600801 tcp_ctask->data_offset,
802 tcp_conn->in.datalen,
803 iscsi_tcp_process_data_in,
804 rx_hash);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600805 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500806 /* fall through */
807 case ISCSI_OP_SCSI_CMD_RSP:
Olaf Kirchda32dd62007-12-13 12:43:21 -0600808 if (tcp_conn->in.datalen) {
809 iscsi_tcp_data_recv_prep(tcp_conn);
810 return 0;
811 }
812 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500813 break;
814 case ISCSI_OP_R2T:
Mike Christie0af967f2008-05-21 15:54:04 -0500815 ctask = iscsi_itt_to_ctask(conn, hdr->itt);
816 if (!ctask)
817 return ISCSI_ERR_BAD_ITT;
818
Mike Christie5bb0b552006-04-06 21:26:46 -0500819 if (ahslen)
820 rc = ISCSI_ERR_AHSLEN;
Mike Christie4545a882007-12-13 12:43:40 -0600821 else if (ctask->sc->sc_data_direction == DMA_TO_DEVICE) {
822 spin_lock(&session->lock);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600823 rc = iscsi_r2t_rsp(conn, ctask);
Mike Christie4545a882007-12-13 12:43:40 -0600824 spin_unlock(&session->lock);
825 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500826 rc = ISCSI_ERR_PROTO;
827 break;
828 case ISCSI_OP_LOGIN_RSP:
829 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500830 case ISCSI_OP_REJECT:
831 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500832 /*
833 * It is possible that we could get a PDU with a buffer larger
834 * than 8K, but there are no targets that currently do this.
835 * For now we fail until we find a vendor that needs it
836 */
Olaf Kirchda32dd62007-12-13 12:43:21 -0600837 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
Mike Christie322d7392008-01-31 13:36:52 -0600838 iscsi_conn_printk(KERN_ERR, conn,
839 "iscsi_tcp: received buffer of "
840 "len %u but conn buffer is only %u "
841 "(opcode %0x)\n",
842 tcp_conn->in.datalen,
843 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
Mike Christiec8dc1e52006-07-24 15:47:39 -0500844 rc = ISCSI_ERR_PROTO;
845 break;
846 }
847
Olaf Kirchda32dd62007-12-13 12:43:21 -0600848 /* If there's data coming in with the response,
849 * receive it to the connection's buffer.
850 */
851 if (tcp_conn->in.datalen) {
852 iscsi_tcp_data_recv_prep(tcp_conn);
853 return 0;
854 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500855 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500856 case ISCSI_OP_LOGOUT_RSP:
857 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500858 case ISCSI_OP_SCSI_TMFUNC_RSP:
859 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
860 break;
861 default:
862 rc = ISCSI_ERR_BAD_OPCODE;
863 break;
864 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700865
Olaf Kirchda32dd62007-12-13 12:43:21 -0600866 if (rc == 0) {
867 /* Anything that comes with data should have
868 * been handled above. */
869 if (tcp_conn->in.datalen)
870 return ISCSI_ERR_PROTO;
871 iscsi_tcp_hdr_recv_prep(tcp_conn);
872 }
873
Alex Aizman7ba24712005-08-04 19:30:08 -0700874 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700875}
876
Olaf Kirchda32dd62007-12-13 12:43:21 -0600877/**
878 * iscsi_tcp_hdr_recv_done - process PDU header
879 *
880 * This is the callback invoked when the PDU header has
881 * been received. If the header is followed by additional
882 * header segments, we go back for more data.
883 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700884static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600885iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600886 struct iscsi_segment *segment)
Alex Aizman7ba24712005-08-04 19:30:08 -0700887{
Olaf Kirchda32dd62007-12-13 12:43:21 -0600888 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
889 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700890
Olaf Kirchda32dd62007-12-13 12:43:21 -0600891 /* Check if there are additional header segments
892 * *prior* to computing the digest, because we
893 * may need to go back to the caller for more.
894 */
895 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600896 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600897 /* Bump the header length - the caller will
898 * just loop around and get the AHS for us, and
899 * call again. */
900 unsigned int ahslen = hdr->hlength << 2;
Alex Aizman7ba24712005-08-04 19:30:08 -0700901
Olaf Kirchda32dd62007-12-13 12:43:21 -0600902 /* Make sure we don't overflow */
903 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
904 return ISCSI_ERR_AHSLEN;
905
Olaf Kircha8ac6312007-12-13 12:43:35 -0600906 segment->total_size += ahslen;
907 segment->size += ahslen;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600908 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700909 }
Olaf Kirchda32dd62007-12-13 12:43:21 -0600910
911 /* We're done processing the header. See if we're doing
912 * header digests; if so, set up the recv_digest buffer
913 * and go back for more. */
914 if (conn->hdrdgst_en) {
Olaf Kircha8ac6312007-12-13 12:43:35 -0600915 if (segment->digest_len == 0) {
916 iscsi_tcp_segment_splice_digest(segment,
917 segment->recv_digest);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600918 return 0;
919 }
920 iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600921 segment->total_copied - ISCSI_DIGEST_SIZE,
922 segment->digest);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600923
Olaf Kircha8ac6312007-12-13 12:43:35 -0600924 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600925 return ISCSI_ERR_HDR_DGST;
926 }
927
928 tcp_conn->in.hdr = hdr;
929 return iscsi_tcp_hdr_dissect(conn, hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -0700930}
931
932/**
Olaf Kirchda32dd62007-12-13 12:43:21 -0600933 * iscsi_tcp_recv - TCP receive in sendfile fashion
Alex Aizman7ba24712005-08-04 19:30:08 -0700934 * @rd_desc: read descriptor
935 * @skb: socket buffer
936 * @offset: offset in skb
937 * @len: skb->len - offset
938 **/
939static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600940iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
941 unsigned int offset, size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -0700942{
Alex Aizman7ba24712005-08-04 19:30:08 -0700943 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500944 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600945 struct iscsi_segment *segment = &tcp_conn->in.segment;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600946 struct skb_seq_state seq;
947 unsigned int consumed = 0;
948 int rc = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700949
Olaf Kirchda32dd62007-12-13 12:43:21 -0600950 debug_tcp("in %d bytes\n", skb->len - offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700951
952 if (unlikely(conn->suspend_rx)) {
953 debug_tcp("conn %d Rx suspended!\n", conn->id);
954 return 0;
955 }
956
Olaf Kirchda32dd62007-12-13 12:43:21 -0600957 skb_prepare_seq_read(skb, offset, skb->len, &seq);
958 while (1) {
959 unsigned int avail;
960 const u8 *ptr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700961
Olaf Kirchda32dd62007-12-13 12:43:21 -0600962 avail = skb_seq_read(consumed, &ptr, &seq);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600963 if (avail == 0) {
964 debug_tcp("no more data avail. Consumed %d\n",
965 consumed);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600966 break;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600967 }
968 BUG_ON(segment->copied >= segment->size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700969
Olaf Kirchda32dd62007-12-13 12:43:21 -0600970 debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600971 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600972 BUG_ON(rc == 0);
973 consumed += rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500974
Olaf Kircha8ac6312007-12-13 12:43:35 -0600975 if (segment->total_copied >= segment->total_size) {
976 debug_tcp("segment done\n");
977 rc = segment->done(tcp_conn, segment);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600978 if (rc != 0) {
979 skb_abort_seq_read(&seq);
980 goto error;
Mike Christiedbdb0162007-05-30 12:57:20 -0500981 }
Mike Christiedbdb0162007-05-30 12:57:20 -0500982
Olaf Kirchda32dd62007-12-13 12:43:21 -0600983 /* The done() functions sets up the
Olaf Kircha8ac6312007-12-13 12:43:35 -0600984 * next segment. */
Alex Aizman7ba24712005-08-04 19:30:08 -0700985 }
986 }
Olaf Kircha8ac6312007-12-13 12:43:35 -0600987 skb_abort_seq_read(&seq);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600988 conn->rxdata_octets += consumed;
989 return consumed;
Alex Aizman7ba24712005-08-04 19:30:08 -0700990
Olaf Kirchda32dd62007-12-13 12:43:21 -0600991error:
992 debug_tcp("Error receiving PDU, errno=%d\n", rc);
993 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
994 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700995}
996
997static void
998iscsi_tcp_data_ready(struct sock *sk, int flag)
999{
1000 struct iscsi_conn *conn = sk->sk_user_data;
Olaf Kirchda32dd62007-12-13 12:43:21 -06001001 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001002 read_descriptor_t rd_desc;
1003
1004 read_lock(&sk->sk_callback_lock);
1005
Mike Christie665b44a2006-05-02 19:46:49 -05001006 /*
Olaf Kirchda32dd62007-12-13 12:43:21 -06001007 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
Mike Christie665b44a2006-05-02 19:46:49 -05001008 * We set count to 1 because we want the network layer to
Olaf Kirchda32dd62007-12-13 12:43:21 -06001009 * hand us all the skbs that are available. iscsi_tcp_recv
Mike Christie665b44a2006-05-02 19:46:49 -05001010 * handled pdus that cross buffers or pdus that still need data.
1011 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001012 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -05001013 rd_desc.count = 1;
Olaf Kirchda32dd62007-12-13 12:43:21 -06001014 tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
Alex Aizman7ba24712005-08-04 19:30:08 -07001015
1016 read_unlock(&sk->sk_callback_lock);
Olaf Kirchda32dd62007-12-13 12:43:21 -06001017
1018 /* If we had to (atomically) map a highmem page,
1019 * unmap it now. */
Olaf Kircha8ac6312007-12-13 12:43:35 -06001020 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
Alex Aizman7ba24712005-08-04 19:30:08 -07001021}
1022
1023static void
1024iscsi_tcp_state_change(struct sock *sk)
1025{
Mike Christie5bb0b552006-04-06 21:26:46 -05001026 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001027 struct iscsi_conn *conn;
1028 struct iscsi_session *session;
1029 void (*old_state_change)(struct sock *);
1030
1031 read_lock(&sk->sk_callback_lock);
1032
1033 conn = (struct iscsi_conn*)sk->sk_user_data;
1034 session = conn->session;
1035
Mike Christiee6273992005-11-29 23:12:49 -06001036 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1037 sk->sk_state == TCP_CLOSE) &&
1038 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001039 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1040 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1041 }
1042
Mike Christie5bb0b552006-04-06 21:26:46 -05001043 tcp_conn = conn->dd_data;
1044 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001045
1046 read_unlock(&sk->sk_callback_lock);
1047
1048 old_state_change(sk);
1049}
1050
1051/**
1052 * iscsi_write_space - Called when more output buffer space is available
1053 * @sk: socket space is available for
1054 **/
1055static void
1056iscsi_write_space(struct sock *sk)
1057{
1058 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001059 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1060
1061 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001062 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001063 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001064}
1065
1066static void
1067iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1068{
Mike Christie5bb0b552006-04-06 21:26:46 -05001069 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1070 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001071
1072 /* assign new callbacks */
1073 write_lock_bh(&sk->sk_callback_lock);
1074 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001075 tcp_conn->old_data_ready = sk->sk_data_ready;
1076 tcp_conn->old_state_change = sk->sk_state_change;
1077 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001078 sk->sk_data_ready = iscsi_tcp_data_ready;
1079 sk->sk_state_change = iscsi_tcp_state_change;
1080 sk->sk_write_space = iscsi_write_space;
1081 write_unlock_bh(&sk->sk_callback_lock);
1082}
1083
1084static void
Mike Christie1c834692006-07-24 15:47:26 -05001085iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001086{
Mike Christie5bb0b552006-04-06 21:26:46 -05001087 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001088
1089 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1090 write_lock_bh(&sk->sk_callback_lock);
1091 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001092 sk->sk_data_ready = tcp_conn->old_data_ready;
1093 sk->sk_state_change = tcp_conn->old_state_change;
1094 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001095 sk->sk_no_check = 0;
1096 write_unlock_bh(&sk->sk_callback_lock);
1097}
1098
1099/**
Olaf Kircha8ac6312007-12-13 12:43:35 -06001100 * iscsi_xmit - TCP transmit
1101 **/
1102static int
1103iscsi_xmit(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001104{
Mike Christie5bb0b552006-04-06 21:26:46 -05001105 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001106 struct iscsi_segment *segment = &tcp_conn->out.segment;
1107 unsigned int consumed = 0;
1108 int rc = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001109
Olaf Kircha8ac6312007-12-13 12:43:35 -06001110 while (1) {
1111 rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
1112 if (rc < 0)
1113 goto error;
1114 if (rc == 0)
1115 break;
1116
1117 consumed += rc;
1118
1119 if (segment->total_copied >= segment->total_size) {
1120 if (segment->done != NULL) {
1121 rc = segment->done(tcp_conn, segment);
1122 if (rc < 0)
1123 goto error;
1124 }
1125 }
1126 }
1127
1128 debug_tcp("xmit %d bytes\n", consumed);
1129
1130 conn->txdata_octets += consumed;
1131 return consumed;
1132
1133error:
1134 /* Transmit error. We could initiate error recovery
1135 * here. */
1136 debug_tcp("Error sending PDU, errno=%d\n", rc);
1137 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1138 return rc;
1139}
1140
1141/**
1142 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
1143 */
1144static inline int
1145iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
1146{
1147 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1148 struct iscsi_segment *segment = &tcp_conn->out.segment;
1149
1150 return segment->total_copied - segment->total_size;
1151}
1152
1153static inline int
1154iscsi_tcp_flush(struct iscsi_conn *conn)
1155{
1156 int rc;
1157
1158 while (iscsi_tcp_xmit_qlen(conn)) {
1159 rc = iscsi_xmit(conn);
1160 if (rc == 0)
1161 return -EAGAIN;
1162 if (rc < 0)
1163 return rc;
1164 }
1165
1166 return 0;
1167}
1168
1169/*
1170 * This is called when we're done sending the header.
1171 * Simply copy the data_segment to the send segment, and return.
1172 */
1173static int
1174iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
1175 struct iscsi_segment *segment)
1176{
1177 tcp_conn->out.segment = tcp_conn->out.data_segment;
1178 debug_tcp("Header done. Next segment size %u total_size %u\n",
1179 tcp_conn->out.segment.size, tcp_conn->out.segment.total_size);
1180 return 0;
1181}
1182
1183static void
1184iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen)
1185{
1186 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1187
1188 debug_tcp("%s(%p%s)\n", __FUNCTION__, tcp_conn,
1189 conn->hdrdgst_en? ", digest enabled" : "");
1190
1191 /* Clear the data segment - needs to be filled in by the
1192 * caller using iscsi_tcp_send_data_prep() */
1193 memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment));
1194
1195 /* If header digest is enabled, compute the CRC and
1196 * place the digest into the same buffer. We make
1197 * sure that both iscsi_tcp_ctask and mtask have
1198 * sufficient room.
Mike Christie7cae5152006-01-13 18:05:47 -06001199 */
Olaf Kircha8ac6312007-12-13 12:43:35 -06001200 if (conn->hdrdgst_en) {
1201 iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
1202 hdr + hdrlen);
1203 hdrlen += ISCSI_DIGEST_SIZE;
Mike Christie3219e522006-05-30 00:37:28 -05001204 }
1205
Olaf Kircha8ac6312007-12-13 12:43:35 -06001206 /* Remember header pointer for later, when we need
1207 * to decide whether there's a payload to go along
1208 * with the header. */
1209 tcp_conn->out.hdr = hdr;
1210
1211 iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen,
1212 iscsi_tcp_send_hdr_done, NULL);
Alex Aizman7ba24712005-08-04 19:30:08 -07001213}
1214
Olaf Kircha8ac6312007-12-13 12:43:35 -06001215/*
1216 * Prepare the send buffer for the payload data.
1217 * Padding and checksumming will all be taken care
1218 * of by the iscsi_segment routines.
1219 */
1220static int
1221iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
1222 unsigned int count, unsigned int offset,
1223 unsigned int len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001224{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001225 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1226 struct hash_desc *tx_hash = NULL;
1227 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -07001228
Olaf Kircha8ac6312007-12-13 12:43:35 -06001229 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __FUNCTION__,
1230 tcp_conn, offset, len,
1231 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -07001232
Olaf Kircha8ac6312007-12-13 12:43:35 -06001233 /* Make sure the datalen matches what the caller
1234 said he would send. */
1235 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1236 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -07001237
Olaf Kircha8ac6312007-12-13 12:43:35 -06001238 if (conn->datadgst_en)
1239 tx_hash = &tcp_conn->tx_hash;
1240
1241 return iscsi_segment_seek_sg(&tcp_conn->out.data_segment,
1242 sg, count, offset, len,
1243 NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -07001244}
1245
Olaf Kircha8ac6312007-12-13 12:43:35 -06001246static void
1247iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
1248 size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001249{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001250 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1251 struct hash_desc *tx_hash = NULL;
1252 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -07001253
Olaf Kircha8ac6312007-12-13 12:43:35 -06001254 debug_tcp("%s(%p, datalen=%d%s)\n", __FUNCTION__, tcp_conn, len,
1255 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -07001256
Olaf Kircha8ac6312007-12-13 12:43:35 -06001257 /* Make sure the datalen matches what the caller
1258 said he would send. */
1259 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1260 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -07001261
Olaf Kircha8ac6312007-12-13 12:43:35 -06001262 if (conn->datadgst_en)
1263 tx_hash = &tcp_conn->tx_hash;
Alex Aizman7ba24712005-08-04 19:30:08 -07001264
Olaf Kircha8ac6312007-12-13 12:43:35 -06001265 iscsi_segment_init_linear(&tcp_conn->out.data_segment,
1266 data, len, NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -07001267}
1268
Alex Aizman7ba24712005-08-04 19:30:08 -07001269/**
1270 * iscsi_solicit_data_cont - initialize next Data-Out
1271 * @conn: iscsi connection
1272 * @ctask: scsi command task
1273 * @r2t: R2T info
1274 * @left: bytes left to transfer
1275 *
1276 * Notes:
1277 * Initialize next Data-Out within this R2T sequence and continue
1278 * to process next Scatter-Gather element(if any) of this SCSI command.
1279 *
1280 * Called under connection lock.
1281 **/
Olaf Kircha8ac6312007-12-13 12:43:35 -06001282static int
Alex Aizman7ba24712005-08-04 19:30:08 -07001283iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001284 struct iscsi_r2t_info *r2t)
Alex Aizman7ba24712005-08-04 19:30:08 -07001285{
1286 struct iscsi_data *hdr;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001287 int new_offset, left;
1288
1289 BUG_ON(r2t->data_length - r2t->sent < 0);
1290 left = r2t->data_length - r2t->sent;
1291 if (left == 0)
1292 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001293
Mike Christieffbfe922006-05-18 20:31:36 -05001294 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001295 memset(hdr, 0, sizeof(struct iscsi_data));
1296 hdr->ttt = r2t->ttt;
1297 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1298 r2t->solicit_datasn++;
1299 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001300 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1301 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001302 hdr->exp_statsn = r2t->exp_statsn;
1303 new_offset = r2t->data_offset + r2t->sent;
1304 hdr->offset = cpu_to_be32(new_offset);
1305 if (left > conn->max_xmit_dlength) {
1306 hton24(hdr->dlength, conn->max_xmit_dlength);
1307 r2t->data_count = conn->max_xmit_dlength;
1308 } else {
1309 hton24(hdr->dlength, left);
1310 r2t->data_count = left;
1311 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1312 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001313
Alex Aizman7ba24712005-08-04 19:30:08 -07001314 conn->dataout_pdus_cnt++;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001315 return 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001316}
1317
1318/**
Olaf Kircha8ac6312007-12-13 12:43:35 -06001319 * iscsi_tcp_ctask - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001320 * @conn: iscsi connection
1321 * @ctask: scsi command task
1322 * @sc: scsi command
1323 **/
Olaf Kircha8ac6312007-12-13 12:43:35 -06001324static int
1325iscsi_tcp_ctask_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001326{
Mike Christie5bb0b552006-04-06 21:26:46 -05001327 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001328 struct iscsi_conn *conn = ctask->conn;
1329 struct scsi_cmnd *sc = ctask->sc;
1330 int err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001331
Mike Christie5bb0b552006-04-06 21:26:46 -05001332 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Olaf Kircha8ac6312007-12-13 12:43:35 -06001333 tcp_ctask->sent = 0;
1334 tcp_ctask->exp_datasn = 0;
1335
1336 /* Prepare PDU, optionally w/ immediate data */
1337 debug_scsi("ctask deq [cid %d itt 0x%x imm %d unsol %d]\n",
1338 conn->id, ctask->itt, ctask->imm_count,
1339 ctask->unsol_count);
1340 iscsi_tcp_send_hdr_prep(conn, ctask->hdr, ctask->hdr_len);
1341
1342 if (!ctask->imm_count)
1343 return 0;
1344
1345 /* If we have immediate data, attach a payload */
Boaz Harrosh94795b62008-04-18 10:11:53 -05001346 err = iscsi_tcp_send_data_prep(conn, scsi_out(sc)->table.sgl,
1347 scsi_out(sc)->table.nents,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001348 0, ctask->imm_count);
1349 if (err)
1350 return err;
1351 tcp_ctask->sent += ctask->imm_count;
1352 ctask->imm_count = 0;
1353 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001354}
1355
1356/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001357 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001358 * @conn: iscsi connection
1359 * @mtask: task management task
1360 *
1361 * Notes:
1362 * The function can return -EAGAIN in which case caller must
1363 * call it again later, or recover. '0' return code means successful
1364 * xmit.
Alex Aizman7ba24712005-08-04 19:30:08 -07001365 **/
1366static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001367iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001368{
Mike Christie3219e522006-05-30 00:37:28 -05001369 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001370
Olaf Kircha8ac6312007-12-13 12:43:35 -06001371 /* Flush any pending data first. */
1372 rc = iscsi_tcp_flush(conn);
1373 if (rc < 0)
1374 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001375
Al Virob4377352007-02-09 16:39:40 +00001376 if (mtask->hdr->itt == RESERVED_ITT) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001377 struct iscsi_session *session = conn->session;
1378
1379 spin_lock_bh(&session->lock);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001380 iscsi_free_mgmt_task(conn, mtask);
Mike Christie5bb0b552006-04-06 21:26:46 -05001381 spin_unlock_bh(&session->lock);
1382 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001383
Alex Aizman7ba24712005-08-04 19:30:08 -07001384 return 0;
1385}
1386
Olaf Kircha8ac6312007-12-13 12:43:35 -06001387/*
Mike Christie218432c2007-05-30 12:57:17 -05001388 * iscsi_tcp_ctask_xmit - xmit normal PDU task
1389 * @conn: iscsi connection
1390 * @ctask: iscsi command task
1391 *
Olaf Kircha8ac6312007-12-13 12:43:35 -06001392 * We're expected to return 0 when everything was transmitted succesfully,
1393 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1394 * of error.
1395 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001396static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001397iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001398{
Mike Christie5bb0b552006-04-06 21:26:46 -05001399 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001400 struct scsi_cmnd *sc = ctask->sc;
Boaz Harrosh94795b62008-04-18 10:11:53 -05001401 struct scsi_data_buffer *sdb = scsi_out(sc);
Alex Aizman7ba24712005-08-04 19:30:08 -07001402 int rc = 0;
1403
Olaf Kircha8ac6312007-12-13 12:43:35 -06001404flush:
1405 /* Flush any pending data first. */
1406 rc = iscsi_tcp_flush(conn);
1407 if (rc < 0)
Mike Christie218432c2007-05-30 12:57:17 -05001408 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001409
1410 /* Are we done already? */
1411 if (sc->sc_data_direction != DMA_TO_DEVICE)
Mike Christie218432c2007-05-30 12:57:17 -05001412 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001413
Olaf Kircha8ac6312007-12-13 12:43:35 -06001414 if (ctask->unsol_count != 0) {
1415 struct iscsi_data *hdr = &tcp_ctask->unsol_dtask.hdr;
1416
1417 /* Prepare a header for the unsolicited PDU.
1418 * The amount of data we want to send will be
1419 * in ctask->data_count.
1420 * FIXME: return the data count instead.
1421 */
1422 iscsi_prep_unsolicit_data_pdu(ctask, hdr);
1423
1424 debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n",
1425 ctask->itt, tcp_ctask->sent, ctask->data_count);
1426
1427 iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr));
Boaz Harrosh94795b62008-04-18 10:11:53 -05001428 rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
1429 sdb->table.nents, tcp_ctask->sent,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001430 ctask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001431 if (rc)
Olaf Kircha8ac6312007-12-13 12:43:35 -06001432 goto fail;
1433 tcp_ctask->sent += ctask->data_count;
1434 ctask->unsol_count -= ctask->data_count;
1435 goto flush;
1436 } else {
1437 struct iscsi_session *session = conn->session;
1438 struct iscsi_r2t_info *r2t;
1439
1440 /* All unsolicited PDUs sent. Check for solicited PDUs.
1441 */
1442 spin_lock_bh(&session->lock);
1443 r2t = tcp_ctask->r2t;
1444 if (r2t != NULL) {
1445 /* Continue with this R2T? */
1446 if (!iscsi_solicit_data_cont(conn, ctask, r2t)) {
1447 debug_scsi(" done with r2t %p\n", r2t);
1448
1449 __kfifo_put(tcp_ctask->r2tpool.queue,
1450 (void*)&r2t, sizeof(void*));
1451 tcp_ctask->r2t = r2t = NULL;
1452 }
1453 }
1454
1455 if (r2t == NULL) {
1456 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1457 sizeof(void*));
1458 r2t = tcp_ctask->r2t;
1459 }
1460 spin_unlock_bh(&session->lock);
1461
1462 /* Waiting for more R2Ts to arrive. */
1463 if (r2t == NULL) {
1464 debug_tcp("no R2Ts yet\n");
1465 return 0;
1466 }
1467
1468 debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1469 r2t, r2t->solicit_datasn - 1, ctask->itt,
1470 r2t->data_offset + r2t->sent, r2t->data_count);
1471
1472 iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr,
1473 sizeof(struct iscsi_hdr));
1474
Boaz Harrosh94795b62008-04-18 10:11:53 -05001475 rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
1476 sdb->table.nents,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001477 r2t->data_offset + r2t->sent,
1478 r2t->data_count);
1479 if (rc)
1480 goto fail;
1481 tcp_ctask->sent += r2t->data_count;
1482 r2t->sent += r2t->data_count;
1483 goto flush;
Alex Aizman7ba24712005-08-04 19:30:08 -07001484 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001485 return 0;
1486fail:
1487 iscsi_conn_failure(conn, rc);
1488 return -EIO;
Alex Aizman7ba24712005-08-04 19:30:08 -07001489}
1490
Mike Christie7b8631b2006-01-13 18:05:50 -06001491static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001492iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001493{
Mike Christie7b8631b2006-01-13 18:05:50 -06001494 struct iscsi_conn *conn;
1495 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001496 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001497
Mike Christie5d91e202008-05-21 15:54:01 -05001498 cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001499 if (!cls_conn)
1500 return NULL;
1501 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001502 /*
1503 * due to strange issues with iser these are not set
1504 * in iscsi_conn_setup
1505 */
Mike Christiebf32ed32007-02-28 17:32:17 -06001506 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
Alex Aizman7ba24712005-08-04 19:30:08 -07001507
Mike Christie5d91e202008-05-21 15:54:01 -05001508 tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001509 tcp_conn->iscsi_conn = conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001510
James Bottomleyc9802cd2006-09-23 15:33:43 -05001511 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1512 CRYPTO_ALG_ASYNC);
1513 tcp_conn->tx_hash.flags = 0;
Mike Christie322d7392008-01-31 13:36:52 -06001514 if (IS_ERR(tcp_conn->tx_hash.tfm))
Mike Christie5d91e202008-05-21 15:54:01 -05001515 goto free_conn;
Mike Christiedd8c0d92006-08-31 18:09:28 -04001516
James Bottomleyc9802cd2006-09-23 15:33:43 -05001517 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1518 CRYPTO_ALG_ASYNC);
1519 tcp_conn->rx_hash.flags = 0;
Mike Christie322d7392008-01-31 13:36:52 -06001520 if (IS_ERR(tcp_conn->rx_hash.tfm))
Mike Christiedd8c0d92006-08-31 18:09:28 -04001521 goto free_tx_tfm;
1522
Mike Christie7b8631b2006-01-13 18:05:50 -06001523 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001524
Mike Christiedd8c0d92006-08-31 18:09:28 -04001525free_tx_tfm:
James Bottomleyc9802cd2006-09-23 15:33:43 -05001526 crypto_free_hash(tcp_conn->tx_hash.tfm);
Mike Christie5d91e202008-05-21 15:54:01 -05001527free_conn:
Mike Christie322d7392008-01-31 13:36:52 -06001528 iscsi_conn_printk(KERN_ERR, conn,
1529 "Could not create connection due to crc32c "
1530 "loading error. Make sure the crc32c "
1531 "module is built as a module or into the "
1532 "kernel\n");
Mike Christie5bb0b552006-04-06 21:26:46 -05001533 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001534 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001535}
1536
1537static void
Mike Christie1c834692006-07-24 15:47:26 -05001538iscsi_tcp_release_conn(struct iscsi_conn *conn)
1539{
Mike Christie22236962007-05-30 12:57:24 -05001540 struct iscsi_session *session = conn->session;
Mike Christie1c834692006-07-24 15:47:26 -05001541 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie22236962007-05-30 12:57:24 -05001542 struct socket *sock = tcp_conn->sock;
Mike Christie1c834692006-07-24 15:47:26 -05001543
Mike Christie22236962007-05-30 12:57:24 -05001544 if (!sock)
Mike Christie1c834692006-07-24 15:47:26 -05001545 return;
1546
Mike Christie22236962007-05-30 12:57:24 -05001547 sock_hold(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001548 iscsi_conn_restore_callbacks(tcp_conn);
Mike Christie22236962007-05-30 12:57:24 -05001549 sock_put(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001550
Mike Christie22236962007-05-30 12:57:24 -05001551 spin_lock_bh(&session->lock);
Mike Christie1c834692006-07-24 15:47:26 -05001552 tcp_conn->sock = NULL;
1553 conn->recv_lock = NULL;
Mike Christie22236962007-05-30 12:57:24 -05001554 spin_unlock_bh(&session->lock);
1555 sockfd_put(sock);
Mike Christie1c834692006-07-24 15:47:26 -05001556}
1557
1558static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001559iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001560{
Mike Christie7b8631b2006-01-13 18:05:50 -06001561 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001562 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001563
Mike Christie1c834692006-07-24 15:47:26 -05001564 iscsi_tcp_release_conn(conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001565
Pete Wyckoff534284a2006-11-08 15:58:31 -06001566 if (tcp_conn->tx_hash.tfm)
1567 crypto_free_hash(tcp_conn->tx_hash.tfm);
1568 if (tcp_conn->rx_hash.tfm)
1569 crypto_free_hash(tcp_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001570
Mike Christie5d91e202008-05-21 15:54:01 -05001571 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001572}
1573
Mike Christie1c834692006-07-24 15:47:26 -05001574static void
1575iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1576{
1577 struct iscsi_conn *conn = cls_conn->dd_data;
1578
1579 iscsi_conn_stop(cls_conn, flag);
1580 iscsi_tcp_release_conn(conn);
1581}
1582
Mike Christie22236962007-05-30 12:57:24 -05001583static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
1584 char *buf, int *port,
1585 int (*getname)(struct socket *, struct sockaddr *,
1586 int *addrlen))
1587{
1588 struct sockaddr_storage *addr;
1589 struct sockaddr_in6 *sin6;
1590 struct sockaddr_in *sin;
1591 int rc = 0, len;
1592
Al Viroa9204872007-07-20 16:03:40 +01001593 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
Mike Christie22236962007-05-30 12:57:24 -05001594 if (!addr)
1595 return -ENOMEM;
1596
1597 if (getname(sock, (struct sockaddr *) addr, &len)) {
1598 rc = -ENODEV;
1599 goto free_addr;
1600 }
1601
1602 switch (addr->ss_family) {
1603 case AF_INET:
1604 sin = (struct sockaddr_in *)addr;
1605 spin_lock_bh(&conn->session->lock);
1606 sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
1607 *port = be16_to_cpu(sin->sin_port);
1608 spin_unlock_bh(&conn->session->lock);
1609 break;
1610 case AF_INET6:
1611 sin6 = (struct sockaddr_in6 *)addr;
1612 spin_lock_bh(&conn->session->lock);
1613 sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
1614 *port = be16_to_cpu(sin6->sin6_port);
1615 spin_unlock_bh(&conn->session->lock);
1616 break;
1617 }
1618free_addr:
1619 kfree(addr);
1620 return rc;
1621}
1622
Alex Aizman7ba24712005-08-04 19:30:08 -07001623static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001624iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001625 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001626 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001627{
Mike Christie75613522008-05-21 15:53:59 -05001628 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1629 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001630 struct iscsi_conn *conn = cls_conn->dd_data;
1631 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001632 struct sock *sk;
1633 struct socket *sock;
1634 int err;
1635
1636 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001637 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001638 if (!sock) {
Mike Christie322d7392008-01-31 13:36:52 -06001639 iscsi_conn_printk(KERN_ERR, conn,
1640 "sockfd_lookup failed %d\n", err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001641 return -EEXIST;
1642 }
Mike Christie22236962007-05-30 12:57:24 -05001643 /*
1644 * copy these values now because if we drop the session
1645 * userspace may still want to query the values since we will
1646 * be using them for the reconnect
1647 */
1648 err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1649 &conn->portal_port, kernel_getpeername);
1650 if (err)
1651 goto free_socket;
1652
Mike Christie75613522008-05-21 15:53:59 -05001653 err = iscsi_tcp_get_addr(conn, sock, ihost->local_address,
1654 &ihost->local_port, kernel_getsockname);
Mike Christie22236962007-05-30 12:57:24 -05001655 if (err)
1656 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001657
Mike Christie5bb0b552006-04-06 21:26:46 -05001658 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1659 if (err)
Mike Christie22236962007-05-30 12:57:24 -05001660 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001661
Mike Christie67a61112006-05-30 00:37:20 -05001662 /* bind iSCSI connection and socket */
1663 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001664
Mike Christie67a61112006-05-30 00:37:20 -05001665 /* setup Socket parameters */
1666 sk = sock->sk;
1667 sk->sk_reuse = 1;
1668 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1669 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001670
Mike Christie67a61112006-05-30 00:37:20 -05001671 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07001672
Mike Christie67a61112006-05-30 00:37:20 -05001673 /*
1674 * Intercept TCP callbacks for sendfile like receive
1675 * processing.
1676 */
1677 conn->recv_lock = &sk->sk_callback_lock;
1678 iscsi_conn_set_callbacks(conn);
1679 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1680 /*
1681 * set receive state machine into initial state
1682 */
Olaf Kirchda32dd62007-12-13 12:43:21 -06001683 iscsi_tcp_hdr_recv_prep(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001684 return 0;
Mike Christie22236962007-05-30 12:57:24 -05001685
1686free_socket:
1687 sockfd_put(sock);
1688 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001689}
1690
Mike Christie5bb0b552006-04-06 21:26:46 -05001691/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05001692static void
Olaf Kircha8ac6312007-12-13 12:43:35 -06001693iscsi_tcp_mtask_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Mike Christie30a6c652006-04-06 21:13:39 -05001694{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001695 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt);
1696
1697 /* Prepare PDU, optionally w/ immediate data */
1698 iscsi_tcp_send_hdr_prep(conn, mtask->hdr, sizeof(*mtask->hdr));
1699
1700 /* If we have immediate data, attach a payload */
1701 if (mtask->data_count)
1702 iscsi_tcp_send_linear_data_prepare(conn, mtask->data,
1703 mtask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001704}
1705
1706static int
1707iscsi_r2tpool_alloc(struct iscsi_session *session)
1708{
1709 int i;
1710 int cmd_i;
1711
1712 /*
1713 * initialize per-task: R2T pool and xmit queue
1714 */
1715 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1716 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05001717 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001718
1719 /*
1720 * pre-allocated x4 as much r2ts to handle race when
1721 * target acks DataOut faster than we data_xmit() queues
1722 * could replenish r2tqueue.
1723 */
1724
1725 /* R2T pool */
Olaf Kirch63203772007-12-13 12:43:25 -06001726 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4, NULL,
Mike Christie5bb0b552006-04-06 21:26:46 -05001727 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001728 goto r2t_alloc_fail;
1729 }
1730
1731 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05001732 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07001733 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05001734 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
Olaf Kirch63203772007-12-13 12:43:25 -06001735 iscsi_pool_free(&tcp_ctask->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001736 goto r2t_alloc_fail;
1737 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001738 }
1739
1740 return 0;
1741
1742r2t_alloc_fail:
1743 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001744 struct iscsi_cmd_task *ctask = session->cmds[i];
1745 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1746
Mike Christie5bb0b552006-04-06 21:26:46 -05001747 kfifo_free(tcp_ctask->r2tqueue);
Olaf Kirch63203772007-12-13 12:43:25 -06001748 iscsi_pool_free(&tcp_ctask->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001749 }
1750 return -ENOMEM;
1751}
1752
1753static void
1754iscsi_r2tpool_free(struct iscsi_session *session)
1755{
1756 int i;
1757
1758 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001759 struct iscsi_cmd_task *ctask = session->cmds[i];
1760 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1761
Mike Christie5bb0b552006-04-06 21:26:46 -05001762 kfifo_free(tcp_ctask->r2tqueue);
Olaf Kirch63203772007-12-13 12:43:25 -06001763 iscsi_pool_free(&tcp_ctask->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001764 }
1765}
1766
Alex Aizman7ba24712005-08-04 19:30:08 -07001767static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001768iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05001769 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07001770{
Mike Christie7b7232f2006-02-01 21:06:49 -06001771 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001772 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001773 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001774 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07001775
Alex Aizman7ba24712005-08-04 19:30:08 -07001776 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001777 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001778 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001779 break;
1780 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001781 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05001782 tcp_conn->sendpage = conn->datadgst_en ?
1783 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07001784 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07001785 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001786 sscanf(buf, "%d", &value);
Mike Christiedf93ffc2007-12-13 12:43:42 -06001787 if (value <= 0 || !is_power_of_2(value))
1788 return -EINVAL;
1789 if (session->max_r2t == value)
Alex Aizman7ba24712005-08-04 19:30:08 -07001790 break;
1791 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05001792 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001793 if (iscsi_r2tpool_alloc(session))
1794 return -ENOMEM;
1795 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07001796 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001797 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001798 }
1799
1800 return 0;
1801}
1802
1803static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05001804iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
1805 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06001806{
Mike Christie7b7232f2006-02-01 21:06:49 -06001807 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001808 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06001809
1810 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05001811 case ISCSI_PARAM_CONN_PORT:
Mike Christie22236962007-05-30 12:57:24 -05001812 spin_lock_bh(&conn->session->lock);
1813 len = sprintf(buf, "%hu\n", conn->portal_port);
1814 spin_unlock_bh(&conn->session->lock);
Mike Christie8d2860b2006-05-02 19:46:47 -05001815 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001816 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie22236962007-05-30 12:57:24 -05001817 spin_lock_bh(&conn->session->lock);
1818 len = sprintf(buf, "%s\n", conn->portal_address);
1819 spin_unlock_bh(&conn->session->lock);
Mike Christiefd7255f2006-04-06 21:13:36 -05001820 break;
1821 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001822 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05001823 }
1824
1825 return len;
1826}
1827
Alex Aizman7ba24712005-08-04 19:30:08 -07001828static void
Mike Christie7b7232f2006-02-01 21:06:49 -06001829iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07001830{
Mike Christie7b7232f2006-02-01 21:06:49 -06001831 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001832 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001833
1834 stats->txdata_octets = conn->txdata_octets;
1835 stats->rxdata_octets = conn->rxdata_octets;
1836 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1837 stats->dataout_pdus = conn->dataout_pdus_cnt;
1838 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1839 stats->datain_pdus = conn->datain_pdus_cnt;
1840 stats->r2t_pdus = conn->r2t_pdus_cnt;
1841 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1842 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1843 stats->custom_length = 3;
1844 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05001845 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001846 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05001847 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001848 strcpy(stats->custom[2].desc, "eh_abort_cnt");
1849 stats->custom[2].value = conn->eh_abort_cnt;
1850}
1851
Mike Christie5bb0b552006-04-06 21:26:46 -05001852static struct iscsi_cls_session *
Mike Christie75613522008-05-21 15:53:59 -05001853iscsi_tcp_session_create(struct Scsi_Host *shost, uint16_t cmds_max,
Mike Christie40753ca2008-05-21 15:53:56 -05001854 uint16_t qdepth, uint32_t initial_cmdsn,
1855 uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07001856{
Mike Christie5bb0b552006-04-06 21:26:46 -05001857 struct iscsi_cls_session *cls_session;
1858 struct iscsi_session *session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001859 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07001860
Mike Christie75613522008-05-21 15:53:59 -05001861 if (shost) {
1862 printk(KERN_ERR "iscsi_tcp: invalid shost %d.\n",
1863 shost->host_no);
Mike Christie5bb0b552006-04-06 21:26:46 -05001864 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05001865 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001866
Mike Christiea4804cd2008-05-21 15:54:00 -05001867 shost = iscsi_host_alloc(&iscsi_sht, 0, qdepth);
Mike Christie75613522008-05-21 15:53:59 -05001868 if (!shost)
1869 return NULL;
1870 shost->transportt = iscsi_tcp_scsi_transport;
1871 shost->max_lun = iscsi_max_lun;
1872 shost->max_id = 0;
1873 shost->max_channel = 0;
1874 shost->max_cmd_len = 16;
Mike Christiea4804cd2008-05-21 15:54:00 -05001875 shost->can_queue = cmds_max;
Mike Christie75613522008-05-21 15:53:59 -05001876
Mike Christiea4804cd2008-05-21 15:54:00 -05001877 if (iscsi_host_add(shost, NULL))
Mike Christie75613522008-05-21 15:53:59 -05001878 goto free_host;
1879 *hostno = shost->host_no;
1880
1881 cls_session = iscsi_session_setup(&iscsi_tcp_transport, shost, cmds_max,
1882 sizeof(struct iscsi_tcp_cmd_task),
1883 sizeof(struct iscsi_tcp_mgmt_task),
1884 initial_cmdsn);
1885 if (!cls_session)
1886 goto remove_host;
1887 session = cls_session->dd_data;
1888
1889 shost->can_queue = session->cmds_max;
Mike Christie5bb0b552006-04-06 21:26:46 -05001890 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1891 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1892 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1893
Boaz Harrosh004d6532007-12-13 12:43:23 -06001894 ctask->hdr = &tcp_ctask->hdr.cmd_hdr;
1895 ctask->hdr_max = sizeof(tcp_ctask->hdr) - ISCSI_DIGEST_SIZE;
Mike Christie5bb0b552006-04-06 21:26:46 -05001896 }
1897
1898 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1899 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1900 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
1901
Olaf Kircha8ac6312007-12-13 12:43:35 -06001902 mtask->hdr = (struct iscsi_hdr *) &tcp_mtask->hdr;
Mike Christie5bb0b552006-04-06 21:26:46 -05001903 }
1904
Mike Christie75613522008-05-21 15:53:59 -05001905 if (iscsi_r2tpool_alloc(session))
1906 goto remove_session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001907 return cls_session;
1908
Mike Christie75613522008-05-21 15:53:59 -05001909remove_session:
Mike Christie5bb0b552006-04-06 21:26:46 -05001910 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -05001911remove_host:
Mike Christiea4804cd2008-05-21 15:54:00 -05001912 iscsi_host_remove(shost);
Mike Christie75613522008-05-21 15:53:59 -05001913free_host:
Mike Christiea4804cd2008-05-21 15:54:00 -05001914 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001915 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001916}
1917
Mike Christie5bb0b552006-04-06 21:26:46 -05001918static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
1919{
Mike Christie75613522008-05-21 15:53:59 -05001920 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1921
1922 iscsi_r2tpool_free(cls_session->dd_data);
Mike Christie75613522008-05-21 15:53:59 -05001923
Mike Christiea4804cd2008-05-21 15:54:00 -05001924 iscsi_host_remove(shost);
1925 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001926}
1927
Mike Christied1d81c02007-05-30 12:57:21 -05001928static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
1929{
Mike Christieb6d44fe2007-07-26 12:46:47 -05001930 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
Mike Christied1d81c02007-05-30 12:57:21 -05001931 blk_queue_dma_alignment(sdev->request_queue, 0);
1932 return 0;
1933}
1934
Mike Christie5bb0b552006-04-06 21:26:46 -05001935static struct scsi_host_template iscsi_sht = {
Mike Christie79743922007-07-26 12:46:46 -05001936 .module = THIS_MODULE,
Mike Christief4246b32006-07-24 15:47:54 -05001937 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05001938 .queuecommand = iscsi_queuecommand,
1939 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -05001940 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie66bbe0c2007-12-13 12:43:39 -06001941 .sg_tablesize = 4096,
Mike Christie8231f0e2007-02-28 17:32:20 -06001942 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -05001943 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
1944 .eh_abort_handler = iscsi_eh_abort,
Mike Christie843c0a82007-12-13 12:43:20 -06001945 .eh_device_reset_handler= iscsi_eh_device_reset,
Mike Christie5bb0b552006-04-06 21:26:46 -05001946 .eh_host_reset_handler = iscsi_eh_host_reset,
1947 .use_clustering = DISABLE_CLUSTERING,
Mike Christied1d81c02007-05-30 12:57:21 -05001948 .slave_configure = iscsi_tcp_slave_configure,
Mike Christie5bb0b552006-04-06 21:26:46 -05001949 .proc_name = "iscsi_tcp",
1950 .this_id = -1,
1951};
1952
Alex Aizman7ba24712005-08-04 19:30:08 -07001953static struct iscsi_transport iscsi_tcp_transport = {
1954 .owner = THIS_MODULE,
1955 .name = "tcp",
1956 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
1957 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05001958 .param_mask = ISCSI_MAX_RECV_DLENGTH |
1959 ISCSI_MAX_XMIT_DLENGTH |
1960 ISCSI_HDRDGST_EN |
1961 ISCSI_DATADGST_EN |
1962 ISCSI_INITIAL_R2T_EN |
1963 ISCSI_MAX_R2T |
1964 ISCSI_IMM_DATA_EN |
1965 ISCSI_FIRST_BURST |
1966 ISCSI_MAX_BURST |
1967 ISCSI_PDU_INORDER_EN |
1968 ISCSI_DATASEQ_INORDER_EN |
1969 ISCSI_ERL |
1970 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05001971 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05001972 ISCSI_EXP_STATSN |
1973 ISCSI_PERSISTENT_PORT |
1974 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -05001975 ISCSI_TARGET_NAME | ISCSI_TPGT |
1976 ISCSI_USERNAME | ISCSI_PASSWORD |
Mike Christie843c0a82007-12-13 12:43:20 -06001977 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
Mike Christief6d51802007-12-13 12:43:30 -06001978 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
1979 ISCSI_LU_RESET_TMO |
1980 ISCSI_PING_TMO | ISCSI_RECV_TMO,
Mike Christie22236962007-05-30 12:57:24 -05001981 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
Mike Christied8196ed2007-05-30 12:57:25 -05001982 ISCSI_HOST_INITIATOR_NAME |
1983 ISCSI_HOST_NETDEV_NAME,
Mike Christie5bb0b552006-04-06 21:26:46 -05001984 /* session management */
1985 .create_session = iscsi_tcp_session_create,
1986 .destroy_session = iscsi_tcp_session_destroy,
1987 /* connection management */
1988 .create_conn = iscsi_tcp_conn_create,
1989 .bind_conn = iscsi_tcp_conn_bind,
1990 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07001991 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05001992 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06001993 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07001994 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05001995 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -05001996 /* iscsi host params */
Mike Christie75613522008-05-21 15:53:59 -05001997 .get_host_param = iscsi_host_get_param,
Mike Christie0801c242007-05-30 12:57:12 -05001998 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -05001999 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002000 .send_pdu = iscsi_conn_send_pdu,
2001 .get_stats = iscsi_conn_get_stats,
Olaf Kircha8ac6312007-12-13 12:43:35 -06002002 .init_cmd_task = iscsi_tcp_ctask_init,
2003 .init_mgmt_task = iscsi_tcp_mtask_init,
Mike Christie5bb0b552006-04-06 21:26:46 -05002004 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2005 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2006 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2007 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002008 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002009};
2010
2011static int __init
2012iscsi_tcp_init(void)
2013{
Alex Aizman7ba24712005-08-04 19:30:08 -07002014 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002015 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2016 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002017 return -EINVAL;
2018 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002019
Mike Christie75613522008-05-21 15:53:59 -05002020 iscsi_tcp_scsi_transport = iscsi_register_transport(
2021 &iscsi_tcp_transport);
2022 if (!iscsi_tcp_scsi_transport)
Mike Christieffbfe922006-05-18 20:31:36 -05002023 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002024
Mike Christie7b8631b2006-01-13 18:05:50 -06002025 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002026}
2027
2028static void __exit
2029iscsi_tcp_exit(void)
2030{
2031 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002032}
2033
2034module_init(iscsi_tcp_init);
2035module_exit(iscsi_tcp_exit);