blob: 517bad160bea17adbffe5b073ff68d6e9d89943d [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 Christiefbc514b2008-05-21 15:54:07 -0500501iscsi_tcp_cleanup_task(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 Christieb6c395e2006-07-24 15:47:15 -0500504 struct iscsi_r2t_info *r2t;
Alex Aizman7ba24712005-08-04 19:30:08 -0700505
Mike Christiefbc514b2008-05-21 15:54:07 -0500506 /* nothing to do for mgmt ctasks */
507 if (!ctask->sc)
508 return;
509
Mike Christieb6c395e2006-07-24 15:47:15 -0500510 /* flush ctask's r2t queues */
511 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
512 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
513 sizeof(void*));
514 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
515 }
516
Olaf Kircha8ac6312007-12-13 12:43:35 -0600517 r2t = tcp_ctask->r2t;
518 if (r2t != NULL) {
519 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
520 sizeof(void*));
521 tcp_ctask->r2t = NULL;
522 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700523}
524
525/**
526 * iscsi_data_rsp - SCSI Data-In Response processing
527 * @conn: iscsi connection
Mike Christiefbc514b2008-05-21 15:54:07 -0500528 * @ctask: scsi command ctask
Alex Aizman7ba24712005-08-04 19:30:08 -0700529 **/
530static int
531iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
532{
Mike Christie5bb0b552006-04-06 21:26:46 -0500533 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
534 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
535 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700536 struct iscsi_session *session = conn->session;
Mike Christie857ae0b2007-05-30 12:57:15 -0500537 struct scsi_cmnd *sc = ctask->sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700538 int datasn = be32_to_cpu(rhdr->datasn);
Boaz Harrosh94795b62008-04-18 10:11:53 -0500539 unsigned total_in_length = scsi_in(sc)->length;
Alex Aizman7ba24712005-08-04 19:30:08 -0700540
Mike Christie77a23c22007-05-30 12:57:18 -0500541 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie5bb0b552006-04-06 21:26:46 -0500542 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700543 return 0;
544
Mike Christied473cc72007-05-30 12:57:14 -0500545 if (tcp_ctask->exp_datasn != datasn) {
546 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
547 __FUNCTION__, tcp_ctask->exp_datasn, datasn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700548 return ISCSI_ERR_DATASN;
Mike Christied473cc72007-05-30 12:57:14 -0500549 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700550
Mike Christied473cc72007-05-30 12:57:14 -0500551 tcp_ctask->exp_datasn++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700552
Mike Christie5bb0b552006-04-06 21:26:46 -0500553 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
Boaz Harrosh94795b62008-04-18 10:11:53 -0500554 if (tcp_ctask->data_offset + tcp_conn->in.datalen > total_in_length) {
Mike Christie857ae0b2007-05-30 12:57:15 -0500555 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
556 __FUNCTION__, tcp_ctask->data_offset,
Boaz Harrosh94795b62008-04-18 10:11:53 -0500557 tcp_conn->in.datalen, total_in_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700558 return ISCSI_ERR_DATA_OFFSET;
Mike Christie857ae0b2007-05-30 12:57:15 -0500559 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700560
561 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600562 sc->result = (DID_OK << 16) | rhdr->cmd_status;
Alex Aizman7ba24712005-08-04 19:30:08 -0700563 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600564 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
565 ISCSI_FLAG_DATA_OVERFLOW)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700566 int res_count = be32_to_cpu(rhdr->residual_count);
567
568 if (res_count > 0 &&
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600569 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
Boaz Harrosh94795b62008-04-18 10:11:53 -0500570 res_count <= total_in_length))
571 scsi_in(sc)->resid = res_count;
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600572 else
Alex Aizman7ba24712005-08-04 19:30:08 -0700573 sc->result = (DID_BAD_TARGET << 16) |
574 rhdr->cmd_status;
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600575 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700576 }
577
578 conn->datain_pdus_cnt++;
579 return 0;
580}
581
582/**
583 * iscsi_solicit_data_init - initialize first Data-Out
584 * @conn: iscsi connection
Mike Christiefbc514b2008-05-21 15:54:07 -0500585 * @ctask: scsi command ctask
Alex Aizman7ba24712005-08-04 19:30:08 -0700586 * @r2t: R2T info
587 *
588 * Notes:
589 * Initialize first Data-Out within this R2T sequence and finds
590 * proper data_offset within this SCSI command.
591 *
592 * This function is called with connection lock taken.
593 **/
594static void
595iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
596 struct iscsi_r2t_info *r2t)
597{
598 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700599
Mike Christieffbfe922006-05-18 20:31:36 -0500600 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700601 memset(hdr, 0, sizeof(struct iscsi_data));
602 hdr->ttt = r2t->ttt;
603 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
604 r2t->solicit_datasn++;
605 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500606 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
607 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700608 hdr->exp_statsn = r2t->exp_statsn;
609 hdr->offset = cpu_to_be32(r2t->data_offset);
610 if (r2t->data_length > conn->max_xmit_dlength) {
611 hton24(hdr->dlength, conn->max_xmit_dlength);
612 r2t->data_count = conn->max_xmit_dlength;
613 hdr->flags = 0;
614 } else {
615 hton24(hdr->dlength, r2t->data_length);
616 r2t->data_count = r2t->data_length;
617 hdr->flags = ISCSI_FLAG_CMD_FINAL;
618 }
619 conn->dataout_pdus_cnt++;
620
621 r2t->sent = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700622}
623
624/**
625 * iscsi_r2t_rsp - iSCSI R2T Response processing
626 * @conn: iscsi connection
Mike Christiefbc514b2008-05-21 15:54:07 -0500627 * @ctask: scsi command ctask
Alex Aizman7ba24712005-08-04 19:30:08 -0700628 **/
629static int
630iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
631{
632 struct iscsi_r2t_info *r2t;
633 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500634 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
635 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
636 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700637 int r2tsn = be32_to_cpu(rhdr->r2tsn);
638 int rc;
639
Mike Christie98a94162006-08-31 18:09:26 -0400640 if (tcp_conn->in.datalen) {
Mike Christie322d7392008-01-31 13:36:52 -0600641 iscsi_conn_printk(KERN_ERR, conn,
642 "invalid R2t with datalen %d\n",
643 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700644 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400645 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700646
Mike Christied473cc72007-05-30 12:57:14 -0500647 if (tcp_ctask->exp_datasn != r2tsn){
648 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
649 __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700650 return ISCSI_ERR_R2TSN;
Mike Christied473cc72007-05-30 12:57:14 -0500651 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700652
Mike Christiefbc514b2008-05-21 15:54:07 -0500653 /* fill-in new R2T associated with the ctask */
Mike Christie77a23c22007-05-30 12:57:18 -0500654 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
655
Mike Christie843c0a82007-12-13 12:43:20 -0600656 if (!ctask->sc || session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie322d7392008-01-31 13:36:52 -0600657 iscsi_conn_printk(KERN_INFO, conn,
658 "dropping R2T itt %d in recovery.\n",
659 ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -0700660 return 0;
661 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500662
Mike Christie5bb0b552006-04-06 21:26:46 -0500663 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700664 BUG_ON(!rc);
665
666 r2t->exp_statsn = rhdr->statsn;
667 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400668 if (r2t->data_length == 0) {
Mike Christie322d7392008-01-31 13:36:52 -0600669 iscsi_conn_printk(KERN_ERR, conn,
670 "invalid R2T with zero data len\n");
Olaf Kirch03766a12007-12-13 12:43:36 -0600671 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
672 sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700673 return ISCSI_ERR_DATALEN;
674 }
675
Mike Christie98a94162006-08-31 18:09:26 -0400676 if (r2t->data_length > session->max_burst)
677 debug_scsi("invalid R2T with data len %u and max burst %u."
678 "Attempting to execute request.\n",
679 r2t->data_length, session->max_burst);
680
Alex Aizman7ba24712005-08-04 19:30:08 -0700681 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
Boaz Harrosh94795b62008-04-18 10:11:53 -0500682 if (r2t->data_offset + r2t->data_length > scsi_out(ctask->sc)->length) {
Mike Christie322d7392008-01-31 13:36:52 -0600683 iscsi_conn_printk(KERN_ERR, conn,
684 "invalid R2T with data len %u at offset %u "
685 "and total length %d\n", r2t->data_length,
Boaz Harrosh94795b62008-04-18 10:11:53 -0500686 r2t->data_offset, scsi_out(ctask->sc)->length);
Olaf Kirch03766a12007-12-13 12:43:36 -0600687 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
688 sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700689 return ISCSI_ERR_DATALEN;
690 }
691
692 r2t->ttt = rhdr->ttt; /* no flip */
693 r2t->solicit_datasn = 0;
694
695 iscsi_solicit_data_init(conn, ctask, r2t);
696
Mike Christied473cc72007-05-30 12:57:14 -0500697 tcp_ctask->exp_datasn = r2tsn + 1;
Mike Christie5bb0b552006-04-06 21:26:46 -0500698 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700699 conn->r2t_pdus_cnt++;
Mike Christie843c0a82007-12-13 12:43:20 -0600700
701 iscsi_requeue_ctask(ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -0700702 return 0;
703}
704
Olaf Kirchda32dd62007-12-13 12:43:21 -0600705/*
706 * Handle incoming reply to DataIn command
707 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700708static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600709iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600710 struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600711{
712 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
713 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
714 int rc;
715
Olaf Kircha8ac6312007-12-13 12:43:35 -0600716 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600717 return ISCSI_ERR_DATA_DGST;
718
719 /* check for non-exceptional status */
720 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
721 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
722 if (rc)
723 return rc;
724 }
725
726 iscsi_tcp_hdr_recv_prep(tcp_conn);
727 return 0;
728}
729
730/**
731 * iscsi_tcp_hdr_dissect - process PDU header
732 * @conn: iSCSI connection
733 * @hdr: PDU header
734 *
735 * This function analyzes the header of the PDU received,
736 * and performs several sanity checks. If the PDU is accompanied
737 * by data, the receive buffer is set up to copy the incoming data
738 * to the correct location.
739 */
740static int
741iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
Alex Aizman7ba24712005-08-04 19:30:08 -0700742{
Mike Christie5bb0b552006-04-06 21:26:46 -0500743 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700744 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500745 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600746 struct iscsi_cmd_task *ctask;
Alex Aizman7ba24712005-08-04 19:30:08 -0700747
748 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500749 tcp_conn->in.datalen = ntoh24(hdr->dlength);
750 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Mike Christie322d7392008-01-31 13:36:52 -0600751 iscsi_conn_printk(KERN_ERR, conn,
752 "iscsi_tcp: datalen %d > %d\n",
753 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700754 return ISCSI_ERR_DATALEN;
755 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700756
Olaf Kirchda32dd62007-12-13 12:43:21 -0600757 /* Additional header segments. So far, we don't
758 * process additional headers.
759 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500760 ahslen = hdr->hlength << 2;
Alex Aizman7ba24712005-08-04 19:30:08 -0700761
Mike Christie5bb0b552006-04-06 21:26:46 -0500762 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700763 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie0af967f2008-05-21 15:54:04 -0500764 rc = iscsi_verify_itt(conn, hdr->itt);
Mike Christie7a53dc52007-12-13 12:43:37 -0600765 if (rc)
Mike Christie5bb0b552006-04-06 21:26:46 -0500766 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700767
Olaf Kirchda32dd62007-12-13 12:43:21 -0600768 debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
769 opcode, ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700770
Mike Christie5bb0b552006-04-06 21:26:46 -0500771 switch(opcode) {
772 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie0af967f2008-05-21 15:54:04 -0500773 ctask = iscsi_itt_to_ctask(conn, hdr->itt);
774 if (!ctask)
775 return ISCSI_ERR_BAD_ITT;
Mike Christiefbc514b2008-05-21 15:54:07 -0500776 if (!ctask->sc)
777 return ISCSI_ERR_NO_SCSI_CMD;
Mike Christie0af967f2008-05-21 15:54:04 -0500778
Mike Christie4545a882007-12-13 12:43:40 -0600779 spin_lock(&conn->session->lock);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600780 rc = iscsi_data_rsp(conn, ctask);
Mike Christie4545a882007-12-13 12:43:40 -0600781 spin_unlock(&conn->session->lock);
Mike Christie275fd7d2006-07-24 15:47:17 -0500782 if (rc)
783 return rc;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600784 if (tcp_conn->in.datalen) {
785 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
786 struct hash_desc *rx_hash = NULL;
Boaz Harrosh94795b62008-04-18 10:11:53 -0500787 struct scsi_data_buffer *sdb = scsi_in(ctask->sc);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600788
789 /*
790 * Setup copy of Data-In into the Scsi_Cmnd
791 * Scatterlist case:
Olaf Kircha8ac6312007-12-13 12:43:35 -0600792 * We set up the iscsi_segment to point to the next
Olaf Kirchda32dd62007-12-13 12:43:21 -0600793 * scatterlist entry to copy to. As we go along,
794 * we move on to the next scatterlist entry and
795 * update the digest per-entry.
796 */
797 if (conn->datadgst_en)
798 rx_hash = &tcp_conn->rx_hash;
799
800 debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
801 "datalen=%d)\n", tcp_conn,
802 tcp_ctask->data_offset,
803 tcp_conn->in.datalen);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600804 return iscsi_segment_seek_sg(&tcp_conn->in.segment,
Boaz Harrosh94795b62008-04-18 10:11:53 -0500805 sdb->table.sgl,
806 sdb->table.nents,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600807 tcp_ctask->data_offset,
808 tcp_conn->in.datalen,
809 iscsi_tcp_process_data_in,
810 rx_hash);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600811 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500812 /* fall through */
813 case ISCSI_OP_SCSI_CMD_RSP:
Olaf Kirchda32dd62007-12-13 12:43:21 -0600814 if (tcp_conn->in.datalen) {
815 iscsi_tcp_data_recv_prep(tcp_conn);
816 return 0;
817 }
818 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500819 break;
820 case ISCSI_OP_R2T:
Mike Christie0af967f2008-05-21 15:54:04 -0500821 ctask = iscsi_itt_to_ctask(conn, hdr->itt);
822 if (!ctask)
823 return ISCSI_ERR_BAD_ITT;
Mike Christiefbc514b2008-05-21 15:54:07 -0500824 if (!ctask->sc)
825 return ISCSI_ERR_NO_SCSI_CMD;
Mike Christie0af967f2008-05-21 15:54:04 -0500826
Mike Christie5bb0b552006-04-06 21:26:46 -0500827 if (ahslen)
828 rc = ISCSI_ERR_AHSLEN;
Mike Christie4545a882007-12-13 12:43:40 -0600829 else if (ctask->sc->sc_data_direction == DMA_TO_DEVICE) {
830 spin_lock(&session->lock);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600831 rc = iscsi_r2t_rsp(conn, ctask);
Mike Christie4545a882007-12-13 12:43:40 -0600832 spin_unlock(&session->lock);
833 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500834 rc = ISCSI_ERR_PROTO;
835 break;
836 case ISCSI_OP_LOGIN_RSP:
837 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500838 case ISCSI_OP_REJECT:
839 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500840 /*
841 * It is possible that we could get a PDU with a buffer larger
842 * than 8K, but there are no targets that currently do this.
843 * For now we fail until we find a vendor that needs it
844 */
Olaf Kirchda32dd62007-12-13 12:43:21 -0600845 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
Mike Christie322d7392008-01-31 13:36:52 -0600846 iscsi_conn_printk(KERN_ERR, conn,
847 "iscsi_tcp: received buffer of "
848 "len %u but conn buffer is only %u "
849 "(opcode %0x)\n",
850 tcp_conn->in.datalen,
851 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
Mike Christiec8dc1e52006-07-24 15:47:39 -0500852 rc = ISCSI_ERR_PROTO;
853 break;
854 }
855
Olaf Kirchda32dd62007-12-13 12:43:21 -0600856 /* If there's data coming in with the response,
857 * receive it to the connection's buffer.
858 */
859 if (tcp_conn->in.datalen) {
860 iscsi_tcp_data_recv_prep(tcp_conn);
861 return 0;
862 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500863 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500864 case ISCSI_OP_LOGOUT_RSP:
865 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500866 case ISCSI_OP_SCSI_TMFUNC_RSP:
867 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
868 break;
869 default:
870 rc = ISCSI_ERR_BAD_OPCODE;
871 break;
872 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700873
Olaf Kirchda32dd62007-12-13 12:43:21 -0600874 if (rc == 0) {
875 /* Anything that comes with data should have
876 * been handled above. */
877 if (tcp_conn->in.datalen)
878 return ISCSI_ERR_PROTO;
879 iscsi_tcp_hdr_recv_prep(tcp_conn);
880 }
881
Alex Aizman7ba24712005-08-04 19:30:08 -0700882 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700883}
884
Olaf Kirchda32dd62007-12-13 12:43:21 -0600885/**
886 * iscsi_tcp_hdr_recv_done - process PDU header
887 *
888 * This is the callback invoked when the PDU header has
889 * been received. If the header is followed by additional
890 * header segments, we go back for more data.
891 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700892static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600893iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600894 struct iscsi_segment *segment)
Alex Aizman7ba24712005-08-04 19:30:08 -0700895{
Olaf Kirchda32dd62007-12-13 12:43:21 -0600896 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
897 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700898
Olaf Kirchda32dd62007-12-13 12:43:21 -0600899 /* Check if there are additional header segments
900 * *prior* to computing the digest, because we
901 * may need to go back to the caller for more.
902 */
903 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600904 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600905 /* Bump the header length - the caller will
906 * just loop around and get the AHS for us, and
907 * call again. */
908 unsigned int ahslen = hdr->hlength << 2;
Alex Aizman7ba24712005-08-04 19:30:08 -0700909
Olaf Kirchda32dd62007-12-13 12:43:21 -0600910 /* Make sure we don't overflow */
911 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
912 return ISCSI_ERR_AHSLEN;
913
Olaf Kircha8ac6312007-12-13 12:43:35 -0600914 segment->total_size += ahslen;
915 segment->size += ahslen;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600916 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700917 }
Olaf Kirchda32dd62007-12-13 12:43:21 -0600918
919 /* We're done processing the header. See if we're doing
920 * header digests; if so, set up the recv_digest buffer
921 * and go back for more. */
922 if (conn->hdrdgst_en) {
Olaf Kircha8ac6312007-12-13 12:43:35 -0600923 if (segment->digest_len == 0) {
924 iscsi_tcp_segment_splice_digest(segment,
925 segment->recv_digest);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600926 return 0;
927 }
928 iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600929 segment->total_copied - ISCSI_DIGEST_SIZE,
930 segment->digest);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600931
Olaf Kircha8ac6312007-12-13 12:43:35 -0600932 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600933 return ISCSI_ERR_HDR_DGST;
934 }
935
936 tcp_conn->in.hdr = hdr;
937 return iscsi_tcp_hdr_dissect(conn, hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -0700938}
939
940/**
Olaf Kirchda32dd62007-12-13 12:43:21 -0600941 * iscsi_tcp_recv - TCP receive in sendfile fashion
Alex Aizman7ba24712005-08-04 19:30:08 -0700942 * @rd_desc: read descriptor
943 * @skb: socket buffer
944 * @offset: offset in skb
945 * @len: skb->len - offset
946 **/
947static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600948iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
949 unsigned int offset, size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -0700950{
Alex Aizman7ba24712005-08-04 19:30:08 -0700951 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500952 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600953 struct iscsi_segment *segment = &tcp_conn->in.segment;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600954 struct skb_seq_state seq;
955 unsigned int consumed = 0;
956 int rc = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700957
Olaf Kirchda32dd62007-12-13 12:43:21 -0600958 debug_tcp("in %d bytes\n", skb->len - offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700959
960 if (unlikely(conn->suspend_rx)) {
961 debug_tcp("conn %d Rx suspended!\n", conn->id);
962 return 0;
963 }
964
Olaf Kirchda32dd62007-12-13 12:43:21 -0600965 skb_prepare_seq_read(skb, offset, skb->len, &seq);
966 while (1) {
967 unsigned int avail;
968 const u8 *ptr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700969
Olaf Kirchda32dd62007-12-13 12:43:21 -0600970 avail = skb_seq_read(consumed, &ptr, &seq);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600971 if (avail == 0) {
972 debug_tcp("no more data avail. Consumed %d\n",
973 consumed);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600974 break;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600975 }
976 BUG_ON(segment->copied >= segment->size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700977
Olaf Kirchda32dd62007-12-13 12:43:21 -0600978 debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600979 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600980 BUG_ON(rc == 0);
981 consumed += rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500982
Olaf Kircha8ac6312007-12-13 12:43:35 -0600983 if (segment->total_copied >= segment->total_size) {
984 debug_tcp("segment done\n");
985 rc = segment->done(tcp_conn, segment);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600986 if (rc != 0) {
987 skb_abort_seq_read(&seq);
988 goto error;
Mike Christiedbdb0162007-05-30 12:57:20 -0500989 }
Mike Christiedbdb0162007-05-30 12:57:20 -0500990
Olaf Kirchda32dd62007-12-13 12:43:21 -0600991 /* The done() functions sets up the
Olaf Kircha8ac6312007-12-13 12:43:35 -0600992 * next segment. */
Alex Aizman7ba24712005-08-04 19:30:08 -0700993 }
994 }
Olaf Kircha8ac6312007-12-13 12:43:35 -0600995 skb_abort_seq_read(&seq);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600996 conn->rxdata_octets += consumed;
997 return consumed;
Alex Aizman7ba24712005-08-04 19:30:08 -0700998
Olaf Kirchda32dd62007-12-13 12:43:21 -0600999error:
1000 debug_tcp("Error receiving PDU, errno=%d\n", rc);
1001 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1002 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001003}
1004
1005static void
1006iscsi_tcp_data_ready(struct sock *sk, int flag)
1007{
1008 struct iscsi_conn *conn = sk->sk_user_data;
Olaf Kirchda32dd62007-12-13 12:43:21 -06001009 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001010 read_descriptor_t rd_desc;
1011
1012 read_lock(&sk->sk_callback_lock);
1013
Mike Christie665b44a2006-05-02 19:46:49 -05001014 /*
Olaf Kirchda32dd62007-12-13 12:43:21 -06001015 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
Mike Christie665b44a2006-05-02 19:46:49 -05001016 * We set count to 1 because we want the network layer to
Olaf Kirchda32dd62007-12-13 12:43:21 -06001017 * hand us all the skbs that are available. iscsi_tcp_recv
Mike Christie665b44a2006-05-02 19:46:49 -05001018 * handled pdus that cross buffers or pdus that still need data.
1019 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001020 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -05001021 rd_desc.count = 1;
Olaf Kirchda32dd62007-12-13 12:43:21 -06001022 tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
Alex Aizman7ba24712005-08-04 19:30:08 -07001023
1024 read_unlock(&sk->sk_callback_lock);
Olaf Kirchda32dd62007-12-13 12:43:21 -06001025
1026 /* If we had to (atomically) map a highmem page,
1027 * unmap it now. */
Olaf Kircha8ac6312007-12-13 12:43:35 -06001028 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
Alex Aizman7ba24712005-08-04 19:30:08 -07001029}
1030
1031static void
1032iscsi_tcp_state_change(struct sock *sk)
1033{
Mike Christie5bb0b552006-04-06 21:26:46 -05001034 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001035 struct iscsi_conn *conn;
1036 struct iscsi_session *session;
1037 void (*old_state_change)(struct sock *);
1038
1039 read_lock(&sk->sk_callback_lock);
1040
1041 conn = (struct iscsi_conn*)sk->sk_user_data;
1042 session = conn->session;
1043
Mike Christiee6273992005-11-29 23:12:49 -06001044 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1045 sk->sk_state == TCP_CLOSE) &&
1046 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001047 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1048 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1049 }
1050
Mike Christie5bb0b552006-04-06 21:26:46 -05001051 tcp_conn = conn->dd_data;
1052 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001053
1054 read_unlock(&sk->sk_callback_lock);
1055
1056 old_state_change(sk);
1057}
1058
1059/**
1060 * iscsi_write_space - Called when more output buffer space is available
1061 * @sk: socket space is available for
1062 **/
1063static void
1064iscsi_write_space(struct sock *sk)
1065{
1066 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001067 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1068
1069 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001070 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001071 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001072}
1073
1074static void
1075iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1076{
Mike Christie5bb0b552006-04-06 21:26:46 -05001077 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1078 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001079
1080 /* assign new callbacks */
1081 write_lock_bh(&sk->sk_callback_lock);
1082 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001083 tcp_conn->old_data_ready = sk->sk_data_ready;
1084 tcp_conn->old_state_change = sk->sk_state_change;
1085 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001086 sk->sk_data_ready = iscsi_tcp_data_ready;
1087 sk->sk_state_change = iscsi_tcp_state_change;
1088 sk->sk_write_space = iscsi_write_space;
1089 write_unlock_bh(&sk->sk_callback_lock);
1090}
1091
1092static void
Mike Christie1c834692006-07-24 15:47:26 -05001093iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001094{
Mike Christie5bb0b552006-04-06 21:26:46 -05001095 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001096
1097 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1098 write_lock_bh(&sk->sk_callback_lock);
1099 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001100 sk->sk_data_ready = tcp_conn->old_data_ready;
1101 sk->sk_state_change = tcp_conn->old_state_change;
1102 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001103 sk->sk_no_check = 0;
1104 write_unlock_bh(&sk->sk_callback_lock);
1105}
1106
1107/**
Olaf Kircha8ac6312007-12-13 12:43:35 -06001108 * iscsi_xmit - TCP transmit
1109 **/
1110static int
1111iscsi_xmit(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001112{
Mike Christie5bb0b552006-04-06 21:26:46 -05001113 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001114 struct iscsi_segment *segment = &tcp_conn->out.segment;
1115 unsigned int consumed = 0;
1116 int rc = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001117
Olaf Kircha8ac6312007-12-13 12:43:35 -06001118 while (1) {
1119 rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
1120 if (rc < 0)
1121 goto error;
1122 if (rc == 0)
1123 break;
1124
1125 consumed += rc;
1126
1127 if (segment->total_copied >= segment->total_size) {
1128 if (segment->done != NULL) {
1129 rc = segment->done(tcp_conn, segment);
1130 if (rc < 0)
1131 goto error;
1132 }
1133 }
1134 }
1135
1136 debug_tcp("xmit %d bytes\n", consumed);
1137
1138 conn->txdata_octets += consumed;
1139 return consumed;
1140
1141error:
1142 /* Transmit error. We could initiate error recovery
1143 * here. */
1144 debug_tcp("Error sending PDU, errno=%d\n", rc);
1145 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1146 return rc;
1147}
1148
1149/**
1150 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
1151 */
1152static inline int
1153iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
1154{
1155 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1156 struct iscsi_segment *segment = &tcp_conn->out.segment;
1157
1158 return segment->total_copied - segment->total_size;
1159}
1160
1161static inline int
1162iscsi_tcp_flush(struct iscsi_conn *conn)
1163{
1164 int rc;
1165
1166 while (iscsi_tcp_xmit_qlen(conn)) {
1167 rc = iscsi_xmit(conn);
1168 if (rc == 0)
1169 return -EAGAIN;
1170 if (rc < 0)
1171 return rc;
1172 }
1173
1174 return 0;
1175}
1176
1177/*
1178 * This is called when we're done sending the header.
1179 * Simply copy the data_segment to the send segment, and return.
1180 */
1181static int
1182iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
1183 struct iscsi_segment *segment)
1184{
1185 tcp_conn->out.segment = tcp_conn->out.data_segment;
1186 debug_tcp("Header done. Next segment size %u total_size %u\n",
1187 tcp_conn->out.segment.size, tcp_conn->out.segment.total_size);
1188 return 0;
1189}
1190
1191static void
1192iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen)
1193{
1194 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1195
1196 debug_tcp("%s(%p%s)\n", __FUNCTION__, tcp_conn,
1197 conn->hdrdgst_en? ", digest enabled" : "");
1198
1199 /* Clear the data segment - needs to be filled in by the
1200 * caller using iscsi_tcp_send_data_prep() */
1201 memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment));
1202
1203 /* If header digest is enabled, compute the CRC and
1204 * place the digest into the same buffer. We make
Mike Christiefbc514b2008-05-21 15:54:07 -05001205 * sure that both iscsi_tcp_cmd_task and mctask have
Olaf Kircha8ac6312007-12-13 12:43:35 -06001206 * sufficient room.
Mike Christie7cae5152006-01-13 18:05:47 -06001207 */
Olaf Kircha8ac6312007-12-13 12:43:35 -06001208 if (conn->hdrdgst_en) {
1209 iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
1210 hdr + hdrlen);
1211 hdrlen += ISCSI_DIGEST_SIZE;
Mike Christie3219e522006-05-30 00:37:28 -05001212 }
1213
Olaf Kircha8ac6312007-12-13 12:43:35 -06001214 /* Remember header pointer for later, when we need
1215 * to decide whether there's a payload to go along
1216 * with the header. */
1217 tcp_conn->out.hdr = hdr;
1218
1219 iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen,
1220 iscsi_tcp_send_hdr_done, NULL);
Alex Aizman7ba24712005-08-04 19:30:08 -07001221}
1222
Olaf Kircha8ac6312007-12-13 12:43:35 -06001223/*
1224 * Prepare the send buffer for the payload data.
1225 * Padding and checksumming will all be taken care
1226 * of by the iscsi_segment routines.
1227 */
1228static int
1229iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
1230 unsigned int count, unsigned int offset,
1231 unsigned int len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001232{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001233 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1234 struct hash_desc *tx_hash = NULL;
1235 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -07001236
Olaf Kircha8ac6312007-12-13 12:43:35 -06001237 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __FUNCTION__,
1238 tcp_conn, offset, len,
1239 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -07001240
Olaf Kircha8ac6312007-12-13 12:43:35 -06001241 /* Make sure the datalen matches what the caller
1242 said he would send. */
1243 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1244 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -07001245
Olaf Kircha8ac6312007-12-13 12:43:35 -06001246 if (conn->datadgst_en)
1247 tx_hash = &tcp_conn->tx_hash;
1248
1249 return iscsi_segment_seek_sg(&tcp_conn->out.data_segment,
1250 sg, count, offset, len,
1251 NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -07001252}
1253
Olaf Kircha8ac6312007-12-13 12:43:35 -06001254static void
1255iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
1256 size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001257{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001258 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1259 struct hash_desc *tx_hash = NULL;
1260 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -07001261
Olaf Kircha8ac6312007-12-13 12:43:35 -06001262 debug_tcp("%s(%p, datalen=%d%s)\n", __FUNCTION__, tcp_conn, len,
1263 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -07001264
Olaf Kircha8ac6312007-12-13 12:43:35 -06001265 /* Make sure the datalen matches what the caller
1266 said he would send. */
1267 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1268 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -07001269
Olaf Kircha8ac6312007-12-13 12:43:35 -06001270 if (conn->datadgst_en)
1271 tx_hash = &tcp_conn->tx_hash;
Alex Aizman7ba24712005-08-04 19:30:08 -07001272
Olaf Kircha8ac6312007-12-13 12:43:35 -06001273 iscsi_segment_init_linear(&tcp_conn->out.data_segment,
1274 data, len, NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -07001275}
1276
Alex Aizman7ba24712005-08-04 19:30:08 -07001277/**
1278 * iscsi_solicit_data_cont - initialize next Data-Out
1279 * @conn: iscsi connection
Mike Christiefbc514b2008-05-21 15:54:07 -05001280 * @ctask: scsi command ctask
Alex Aizman7ba24712005-08-04 19:30:08 -07001281 * @r2t: R2T info
1282 * @left: bytes left to transfer
1283 *
1284 * Notes:
1285 * Initialize next Data-Out within this R2T sequence and continue
1286 * to process next Scatter-Gather element(if any) of this SCSI command.
1287 *
1288 * Called under connection lock.
1289 **/
Olaf Kircha8ac6312007-12-13 12:43:35 -06001290static int
Alex Aizman7ba24712005-08-04 19:30:08 -07001291iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001292 struct iscsi_r2t_info *r2t)
Alex Aizman7ba24712005-08-04 19:30:08 -07001293{
1294 struct iscsi_data *hdr;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001295 int new_offset, left;
1296
1297 BUG_ON(r2t->data_length - r2t->sent < 0);
1298 left = r2t->data_length - r2t->sent;
1299 if (left == 0)
1300 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001301
Mike Christieffbfe922006-05-18 20:31:36 -05001302 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001303 memset(hdr, 0, sizeof(struct iscsi_data));
1304 hdr->ttt = r2t->ttt;
1305 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1306 r2t->solicit_datasn++;
1307 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001308 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1309 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001310 hdr->exp_statsn = r2t->exp_statsn;
1311 new_offset = r2t->data_offset + r2t->sent;
1312 hdr->offset = cpu_to_be32(new_offset);
1313 if (left > conn->max_xmit_dlength) {
1314 hton24(hdr->dlength, conn->max_xmit_dlength);
1315 r2t->data_count = conn->max_xmit_dlength;
1316 } else {
1317 hton24(hdr->dlength, left);
1318 r2t->data_count = left;
1319 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1320 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001321
Alex Aizman7ba24712005-08-04 19:30:08 -07001322 conn->dataout_pdus_cnt++;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001323 return 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001324}
1325
1326/**
Mike Christiefbc514b2008-05-21 15:54:07 -05001327 * iscsi_tcp_task - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001328 * @conn: iscsi connection
Mike Christiefbc514b2008-05-21 15:54:07 -05001329 * @ctask: scsi command ctask
Alex Aizman7ba24712005-08-04 19:30:08 -07001330 * @sc: scsi command
1331 **/
Olaf Kircha8ac6312007-12-13 12:43:35 -06001332static int
Mike Christiefbc514b2008-05-21 15:54:07 -05001333iscsi_tcp_task_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001334{
Mike Christie5bb0b552006-04-06 21:26:46 -05001335 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001336 struct iscsi_conn *conn = ctask->conn;
1337 struct scsi_cmnd *sc = ctask->sc;
1338 int err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001339
Mike Christiefbc514b2008-05-21 15:54:07 -05001340 if (!sc) {
1341 /*
1342 * mgmt ctasks do not have a scatterlist since they come
1343 * in from the iscsi interface.
1344 */
1345 debug_scsi("mctask deq [cid %d itt 0x%x]\n", conn->id,
1346 ctask->itt);
1347
1348 /* Prepare PDU, optionally w/ immediate data */
1349 iscsi_tcp_send_hdr_prep(conn, ctask->hdr, sizeof(*ctask->hdr));
1350
1351 /* If we have immediate data, attach a payload */
1352 if (ctask->data_count)
1353 iscsi_tcp_send_linear_data_prepare(conn, ctask->data,
1354 ctask->data_count);
1355 return 0;
1356 }
1357
Mike Christie5bb0b552006-04-06 21:26:46 -05001358 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Olaf Kircha8ac6312007-12-13 12:43:35 -06001359 tcp_ctask->sent = 0;
1360 tcp_ctask->exp_datasn = 0;
1361
1362 /* Prepare PDU, optionally w/ immediate data */
1363 debug_scsi("ctask deq [cid %d itt 0x%x imm %d unsol %d]\n",
1364 conn->id, ctask->itt, ctask->imm_count,
1365 ctask->unsol_count);
1366 iscsi_tcp_send_hdr_prep(conn, ctask->hdr, ctask->hdr_len);
1367
1368 if (!ctask->imm_count)
1369 return 0;
1370
1371 /* If we have immediate data, attach a payload */
Boaz Harrosh94795b62008-04-18 10:11:53 -05001372 err = iscsi_tcp_send_data_prep(conn, scsi_out(sc)->table.sgl,
1373 scsi_out(sc)->table.nents,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001374 0, ctask->imm_count);
1375 if (err)
1376 return err;
1377 tcp_ctask->sent += ctask->imm_count;
1378 ctask->imm_count = 0;
1379 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001380}
1381
Olaf Kircha8ac6312007-12-13 12:43:35 -06001382/*
Mike Christiefbc514b2008-05-21 15:54:07 -05001383 * iscsi_tcp_task_xmit - xmit normal PDU ctask
1384 * @ctask: iscsi command ctask
Mike Christie218432c2007-05-30 12:57:17 -05001385 *
Olaf Kircha8ac6312007-12-13 12:43:35 -06001386 * We're expected to return 0 when everything was transmitted succesfully,
1387 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1388 * of error.
1389 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001390static int
Mike Christiefbc514b2008-05-21 15:54:07 -05001391iscsi_tcp_task_xmit(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001392{
Mike Christiefbc514b2008-05-21 15:54:07 -05001393 struct iscsi_conn *conn = ctask->conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001394 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001395 struct scsi_cmnd *sc = ctask->sc;
Mike Christiefbc514b2008-05-21 15:54:07 -05001396 struct scsi_data_buffer *sdb;
Alex Aizman7ba24712005-08-04 19:30:08 -07001397 int rc = 0;
1398
Olaf Kircha8ac6312007-12-13 12:43:35 -06001399flush:
1400 /* Flush any pending data first. */
1401 rc = iscsi_tcp_flush(conn);
1402 if (rc < 0)
Mike Christie218432c2007-05-30 12:57:17 -05001403 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001404
Mike Christiefbc514b2008-05-21 15:54:07 -05001405 /* mgmt command */
1406 if (!sc) {
1407 if (ctask->hdr->itt == RESERVED_ITT)
1408 iscsi_put_ctask(ctask);
1409 return 0;
1410 }
1411
Olaf Kircha8ac6312007-12-13 12:43:35 -06001412 /* Are we done already? */
1413 if (sc->sc_data_direction != DMA_TO_DEVICE)
Mike Christie218432c2007-05-30 12:57:17 -05001414 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001415
Mike Christiefbc514b2008-05-21 15:54:07 -05001416 sdb = scsi_out(sc);
Olaf Kircha8ac6312007-12-13 12:43:35 -06001417 if (ctask->unsol_count != 0) {
1418 struct iscsi_data *hdr = &tcp_ctask->unsol_dtask.hdr;
1419
1420 /* Prepare a header for the unsolicited PDU.
1421 * The amount of data we want to send will be
1422 * in ctask->data_count.
1423 * FIXME: return the data count instead.
1424 */
1425 iscsi_prep_unsolicit_data_pdu(ctask, hdr);
1426
1427 debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n",
1428 ctask->itt, tcp_ctask->sent, ctask->data_count);
1429
1430 iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr));
Boaz Harrosh94795b62008-04-18 10:11:53 -05001431 rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
1432 sdb->table.nents, tcp_ctask->sent,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001433 ctask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001434 if (rc)
Olaf Kircha8ac6312007-12-13 12:43:35 -06001435 goto fail;
1436 tcp_ctask->sent += ctask->data_count;
1437 ctask->unsol_count -= ctask->data_count;
1438 goto flush;
1439 } else {
1440 struct iscsi_session *session = conn->session;
1441 struct iscsi_r2t_info *r2t;
1442
1443 /* All unsolicited PDUs sent. Check for solicited PDUs.
1444 */
1445 spin_lock_bh(&session->lock);
1446 r2t = tcp_ctask->r2t;
1447 if (r2t != NULL) {
1448 /* Continue with this R2T? */
1449 if (!iscsi_solicit_data_cont(conn, ctask, r2t)) {
1450 debug_scsi(" done with r2t %p\n", r2t);
1451
1452 __kfifo_put(tcp_ctask->r2tpool.queue,
1453 (void*)&r2t, sizeof(void*));
1454 tcp_ctask->r2t = r2t = NULL;
1455 }
1456 }
1457
1458 if (r2t == NULL) {
1459 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1460 sizeof(void*));
1461 r2t = tcp_ctask->r2t;
1462 }
1463 spin_unlock_bh(&session->lock);
1464
1465 /* Waiting for more R2Ts to arrive. */
1466 if (r2t == NULL) {
1467 debug_tcp("no R2Ts yet\n");
1468 return 0;
1469 }
1470
1471 debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1472 r2t, r2t->solicit_datasn - 1, ctask->itt,
1473 r2t->data_offset + r2t->sent, r2t->data_count);
1474
1475 iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr,
1476 sizeof(struct iscsi_hdr));
1477
Boaz Harrosh94795b62008-04-18 10:11:53 -05001478 rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
1479 sdb->table.nents,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001480 r2t->data_offset + r2t->sent,
1481 r2t->data_count);
1482 if (rc)
1483 goto fail;
1484 tcp_ctask->sent += r2t->data_count;
1485 r2t->sent += r2t->data_count;
1486 goto flush;
Alex Aizman7ba24712005-08-04 19:30:08 -07001487 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001488 return 0;
1489fail:
1490 iscsi_conn_failure(conn, rc);
1491 return -EIO;
Alex Aizman7ba24712005-08-04 19:30:08 -07001492}
1493
Mike Christie7b8631b2006-01-13 18:05:50 -06001494static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001495iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001496{
Mike Christie7b8631b2006-01-13 18:05:50 -06001497 struct iscsi_conn *conn;
1498 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001499 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001500
Mike Christie5d91e202008-05-21 15:54:01 -05001501 cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001502 if (!cls_conn)
1503 return NULL;
1504 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001505 /*
1506 * due to strange issues with iser these are not set
1507 * in iscsi_conn_setup
1508 */
Mike Christiebf32ed32007-02-28 17:32:17 -06001509 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
Alex Aizman7ba24712005-08-04 19:30:08 -07001510
Mike Christie5d91e202008-05-21 15:54:01 -05001511 tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001512 tcp_conn->iscsi_conn = conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001513
James Bottomleyc9802cd2006-09-23 15:33:43 -05001514 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1515 CRYPTO_ALG_ASYNC);
1516 tcp_conn->tx_hash.flags = 0;
Mike Christie322d7392008-01-31 13:36:52 -06001517 if (IS_ERR(tcp_conn->tx_hash.tfm))
Mike Christie5d91e202008-05-21 15:54:01 -05001518 goto free_conn;
Mike Christiedd8c0d92006-08-31 18:09:28 -04001519
James Bottomleyc9802cd2006-09-23 15:33:43 -05001520 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1521 CRYPTO_ALG_ASYNC);
1522 tcp_conn->rx_hash.flags = 0;
Mike Christie322d7392008-01-31 13:36:52 -06001523 if (IS_ERR(tcp_conn->rx_hash.tfm))
Mike Christiedd8c0d92006-08-31 18:09:28 -04001524 goto free_tx_tfm;
1525
Mike Christie7b8631b2006-01-13 18:05:50 -06001526 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001527
Mike Christiedd8c0d92006-08-31 18:09:28 -04001528free_tx_tfm:
James Bottomleyc9802cd2006-09-23 15:33:43 -05001529 crypto_free_hash(tcp_conn->tx_hash.tfm);
Mike Christie5d91e202008-05-21 15:54:01 -05001530free_conn:
Mike Christie322d7392008-01-31 13:36:52 -06001531 iscsi_conn_printk(KERN_ERR, conn,
1532 "Could not create connection due to crc32c "
1533 "loading error. Make sure the crc32c "
1534 "module is built as a module or into the "
1535 "kernel\n");
Mike Christie5bb0b552006-04-06 21:26:46 -05001536 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001537 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001538}
1539
1540static void
Mike Christie1c834692006-07-24 15:47:26 -05001541iscsi_tcp_release_conn(struct iscsi_conn *conn)
1542{
Mike Christie22236962007-05-30 12:57:24 -05001543 struct iscsi_session *session = conn->session;
Mike Christie1c834692006-07-24 15:47:26 -05001544 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie22236962007-05-30 12:57:24 -05001545 struct socket *sock = tcp_conn->sock;
Mike Christie1c834692006-07-24 15:47:26 -05001546
Mike Christie22236962007-05-30 12:57:24 -05001547 if (!sock)
Mike Christie1c834692006-07-24 15:47:26 -05001548 return;
1549
Mike Christie22236962007-05-30 12:57:24 -05001550 sock_hold(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001551 iscsi_conn_restore_callbacks(tcp_conn);
Mike Christie22236962007-05-30 12:57:24 -05001552 sock_put(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001553
Mike Christie22236962007-05-30 12:57:24 -05001554 spin_lock_bh(&session->lock);
Mike Christie1c834692006-07-24 15:47:26 -05001555 tcp_conn->sock = NULL;
1556 conn->recv_lock = NULL;
Mike Christie22236962007-05-30 12:57:24 -05001557 spin_unlock_bh(&session->lock);
1558 sockfd_put(sock);
Mike Christie1c834692006-07-24 15:47:26 -05001559}
1560
1561static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001562iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001563{
Mike Christie7b8631b2006-01-13 18:05:50 -06001564 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001565 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001566
Mike Christie1c834692006-07-24 15:47:26 -05001567 iscsi_tcp_release_conn(conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001568
Pete Wyckoff534284a2006-11-08 15:58:31 -06001569 if (tcp_conn->tx_hash.tfm)
1570 crypto_free_hash(tcp_conn->tx_hash.tfm);
1571 if (tcp_conn->rx_hash.tfm)
1572 crypto_free_hash(tcp_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001573
Mike Christie5d91e202008-05-21 15:54:01 -05001574 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001575}
1576
Mike Christie1c834692006-07-24 15:47:26 -05001577static void
1578iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1579{
1580 struct iscsi_conn *conn = cls_conn->dd_data;
1581
1582 iscsi_conn_stop(cls_conn, flag);
1583 iscsi_tcp_release_conn(conn);
1584}
1585
Mike Christie22236962007-05-30 12:57:24 -05001586static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
1587 char *buf, int *port,
1588 int (*getname)(struct socket *, struct sockaddr *,
1589 int *addrlen))
1590{
1591 struct sockaddr_storage *addr;
1592 struct sockaddr_in6 *sin6;
1593 struct sockaddr_in *sin;
1594 int rc = 0, len;
1595
Al Viroa9204872007-07-20 16:03:40 +01001596 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
Mike Christie22236962007-05-30 12:57:24 -05001597 if (!addr)
1598 return -ENOMEM;
1599
1600 if (getname(sock, (struct sockaddr *) addr, &len)) {
1601 rc = -ENODEV;
1602 goto free_addr;
1603 }
1604
1605 switch (addr->ss_family) {
1606 case AF_INET:
1607 sin = (struct sockaddr_in *)addr;
1608 spin_lock_bh(&conn->session->lock);
1609 sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
1610 *port = be16_to_cpu(sin->sin_port);
1611 spin_unlock_bh(&conn->session->lock);
1612 break;
1613 case AF_INET6:
1614 sin6 = (struct sockaddr_in6 *)addr;
1615 spin_lock_bh(&conn->session->lock);
1616 sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
1617 *port = be16_to_cpu(sin6->sin6_port);
1618 spin_unlock_bh(&conn->session->lock);
1619 break;
1620 }
1621free_addr:
1622 kfree(addr);
1623 return rc;
1624}
1625
Alex Aizman7ba24712005-08-04 19:30:08 -07001626static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001627iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001628 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001629 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001630{
Mike Christie75613522008-05-21 15:53:59 -05001631 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1632 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001633 struct iscsi_conn *conn = cls_conn->dd_data;
1634 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001635 struct sock *sk;
1636 struct socket *sock;
1637 int err;
1638
1639 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001640 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001641 if (!sock) {
Mike Christie322d7392008-01-31 13:36:52 -06001642 iscsi_conn_printk(KERN_ERR, conn,
1643 "sockfd_lookup failed %d\n", err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001644 return -EEXIST;
1645 }
Mike Christie22236962007-05-30 12:57:24 -05001646 /*
1647 * copy these values now because if we drop the session
1648 * userspace may still want to query the values since we will
1649 * be using them for the reconnect
1650 */
1651 err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1652 &conn->portal_port, kernel_getpeername);
1653 if (err)
1654 goto free_socket;
1655
Mike Christie75613522008-05-21 15:53:59 -05001656 err = iscsi_tcp_get_addr(conn, sock, ihost->local_address,
1657 &ihost->local_port, kernel_getsockname);
Mike Christie22236962007-05-30 12:57:24 -05001658 if (err)
1659 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001660
Mike Christie5bb0b552006-04-06 21:26:46 -05001661 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1662 if (err)
Mike Christie22236962007-05-30 12:57:24 -05001663 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001664
Mike Christie67a61112006-05-30 00:37:20 -05001665 /* bind iSCSI connection and socket */
1666 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001667
Mike Christie67a61112006-05-30 00:37:20 -05001668 /* setup Socket parameters */
1669 sk = sock->sk;
1670 sk->sk_reuse = 1;
1671 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1672 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001673
Mike Christie67a61112006-05-30 00:37:20 -05001674 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07001675
Mike Christie67a61112006-05-30 00:37:20 -05001676 /*
1677 * Intercept TCP callbacks for sendfile like receive
1678 * processing.
1679 */
1680 conn->recv_lock = &sk->sk_callback_lock;
1681 iscsi_conn_set_callbacks(conn);
1682 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1683 /*
1684 * set receive state machine into initial state
1685 */
Olaf Kirchda32dd62007-12-13 12:43:21 -06001686 iscsi_tcp_hdr_recv_prep(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001687 return 0;
Mike Christie22236962007-05-30 12:57:24 -05001688
1689free_socket:
1690 sockfd_put(sock);
1691 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001692}
1693
Alex Aizman7ba24712005-08-04 19:30:08 -07001694static int
1695iscsi_r2tpool_alloc(struct iscsi_session *session)
1696{
1697 int i;
1698 int cmd_i;
1699
1700 /*
Mike Christiefbc514b2008-05-21 15:54:07 -05001701 * initialize per-ctask: R2T pool and xmit queue
Alex Aizman7ba24712005-08-04 19:30:08 -07001702 */
1703 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1704 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05001705 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001706
1707 /*
1708 * pre-allocated x4 as much r2ts to handle race when
1709 * target acks DataOut faster than we data_xmit() queues
1710 * could replenish r2tqueue.
1711 */
1712
1713 /* R2T pool */
Olaf Kirch63203772007-12-13 12:43:25 -06001714 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4, NULL,
Mike Christie5bb0b552006-04-06 21:26:46 -05001715 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001716 goto r2t_alloc_fail;
1717 }
1718
1719 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05001720 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07001721 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05001722 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
Olaf Kirch63203772007-12-13 12:43:25 -06001723 iscsi_pool_free(&tcp_ctask->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001724 goto r2t_alloc_fail;
1725 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001726 }
1727
1728 return 0;
1729
1730r2t_alloc_fail:
1731 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001732 struct iscsi_cmd_task *ctask = session->cmds[i];
1733 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1734
Mike Christie5bb0b552006-04-06 21:26:46 -05001735 kfifo_free(tcp_ctask->r2tqueue);
Olaf Kirch63203772007-12-13 12:43:25 -06001736 iscsi_pool_free(&tcp_ctask->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001737 }
1738 return -ENOMEM;
1739}
1740
1741static void
1742iscsi_r2tpool_free(struct iscsi_session *session)
1743{
1744 int i;
1745
1746 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001747 struct iscsi_cmd_task *ctask = session->cmds[i];
1748 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1749
Mike Christie5bb0b552006-04-06 21:26:46 -05001750 kfifo_free(tcp_ctask->r2tqueue);
Olaf Kirch63203772007-12-13 12:43:25 -06001751 iscsi_pool_free(&tcp_ctask->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001752 }
1753}
1754
Alex Aizman7ba24712005-08-04 19:30:08 -07001755static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001756iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05001757 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07001758{
Mike Christie7b7232f2006-02-01 21:06:49 -06001759 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001760 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001761 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001762 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07001763
Alex Aizman7ba24712005-08-04 19:30:08 -07001764 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001765 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001766 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001767 break;
1768 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001769 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05001770 tcp_conn->sendpage = conn->datadgst_en ?
1771 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07001772 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07001773 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001774 sscanf(buf, "%d", &value);
Mike Christiedf93ffc2007-12-13 12:43:42 -06001775 if (value <= 0 || !is_power_of_2(value))
1776 return -EINVAL;
1777 if (session->max_r2t == value)
Alex Aizman7ba24712005-08-04 19:30:08 -07001778 break;
1779 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05001780 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001781 if (iscsi_r2tpool_alloc(session))
1782 return -ENOMEM;
1783 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07001784 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001785 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001786 }
1787
1788 return 0;
1789}
1790
1791static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05001792iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
1793 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06001794{
Mike Christie7b7232f2006-02-01 21:06:49 -06001795 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001796 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06001797
1798 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05001799 case ISCSI_PARAM_CONN_PORT:
Mike Christie22236962007-05-30 12:57:24 -05001800 spin_lock_bh(&conn->session->lock);
1801 len = sprintf(buf, "%hu\n", conn->portal_port);
1802 spin_unlock_bh(&conn->session->lock);
Mike Christie8d2860b2006-05-02 19:46:47 -05001803 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001804 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie22236962007-05-30 12:57:24 -05001805 spin_lock_bh(&conn->session->lock);
1806 len = sprintf(buf, "%s\n", conn->portal_address);
1807 spin_unlock_bh(&conn->session->lock);
Mike Christiefd7255f2006-04-06 21:13:36 -05001808 break;
1809 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001810 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05001811 }
1812
1813 return len;
1814}
1815
Alex Aizman7ba24712005-08-04 19:30:08 -07001816static void
Mike Christie7b7232f2006-02-01 21:06:49 -06001817iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07001818{
Mike Christie7b7232f2006-02-01 21:06:49 -06001819 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001820 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001821
1822 stats->txdata_octets = conn->txdata_octets;
1823 stats->rxdata_octets = conn->rxdata_octets;
1824 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1825 stats->dataout_pdus = conn->dataout_pdus_cnt;
1826 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1827 stats->datain_pdus = conn->datain_pdus_cnt;
1828 stats->r2t_pdus = conn->r2t_pdus_cnt;
1829 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1830 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1831 stats->custom_length = 3;
1832 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05001833 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001834 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05001835 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001836 strcpy(stats->custom[2].desc, "eh_abort_cnt");
1837 stats->custom[2].value = conn->eh_abort_cnt;
1838}
1839
Mike Christie5bb0b552006-04-06 21:26:46 -05001840static struct iscsi_cls_session *
Mike Christie75613522008-05-21 15:53:59 -05001841iscsi_tcp_session_create(struct Scsi_Host *shost, uint16_t cmds_max,
Mike Christie40753ca2008-05-21 15:53:56 -05001842 uint16_t qdepth, uint32_t initial_cmdsn,
1843 uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07001844{
Mike Christie5bb0b552006-04-06 21:26:46 -05001845 struct iscsi_cls_session *cls_session;
1846 struct iscsi_session *session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001847 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07001848
Mike Christie75613522008-05-21 15:53:59 -05001849 if (shost) {
1850 printk(KERN_ERR "iscsi_tcp: invalid shost %d.\n",
1851 shost->host_no);
Mike Christie5bb0b552006-04-06 21:26:46 -05001852 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05001853 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001854
Mike Christiea4804cd2008-05-21 15:54:00 -05001855 shost = iscsi_host_alloc(&iscsi_sht, 0, qdepth);
Mike Christie75613522008-05-21 15:53:59 -05001856 if (!shost)
1857 return NULL;
1858 shost->transportt = iscsi_tcp_scsi_transport;
1859 shost->max_lun = iscsi_max_lun;
1860 shost->max_id = 0;
1861 shost->max_channel = 0;
1862 shost->max_cmd_len = 16;
Mike Christiea4804cd2008-05-21 15:54:00 -05001863 shost->can_queue = cmds_max;
Mike Christie75613522008-05-21 15:53:59 -05001864
Mike Christiea4804cd2008-05-21 15:54:00 -05001865 if (iscsi_host_add(shost, NULL))
Mike Christie75613522008-05-21 15:53:59 -05001866 goto free_host;
1867 *hostno = shost->host_no;
1868
1869 cls_session = iscsi_session_setup(&iscsi_tcp_transport, shost, cmds_max,
1870 sizeof(struct iscsi_tcp_cmd_task),
Mike Christie75613522008-05-21 15:53:59 -05001871 initial_cmdsn);
1872 if (!cls_session)
1873 goto remove_host;
1874 session = cls_session->dd_data;
1875
Mike Christiefbc514b2008-05-21 15:54:07 -05001876 shost->can_queue = session->scsi_cmds_max;
Mike Christie5bb0b552006-04-06 21:26:46 -05001877 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1878 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1879 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1880
Boaz Harrosh004d6532007-12-13 12:43:23 -06001881 ctask->hdr = &tcp_ctask->hdr.cmd_hdr;
1882 ctask->hdr_max = sizeof(tcp_ctask->hdr) - ISCSI_DIGEST_SIZE;
Mike Christie5bb0b552006-04-06 21:26:46 -05001883 }
1884
Mike Christie75613522008-05-21 15:53:59 -05001885 if (iscsi_r2tpool_alloc(session))
1886 goto remove_session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001887 return cls_session;
1888
Mike Christie75613522008-05-21 15:53:59 -05001889remove_session:
Mike Christie5bb0b552006-04-06 21:26:46 -05001890 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -05001891remove_host:
Mike Christiea4804cd2008-05-21 15:54:00 -05001892 iscsi_host_remove(shost);
Mike Christie75613522008-05-21 15:53:59 -05001893free_host:
Mike Christiea4804cd2008-05-21 15:54:00 -05001894 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001895 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001896}
1897
Mike Christie5bb0b552006-04-06 21:26:46 -05001898static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
1899{
Mike Christie75613522008-05-21 15:53:59 -05001900 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1901
1902 iscsi_r2tpool_free(cls_session->dd_data);
Mike Christie75613522008-05-21 15:53:59 -05001903
Mike Christiea4804cd2008-05-21 15:54:00 -05001904 iscsi_host_remove(shost);
1905 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001906}
1907
Mike Christied1d81c02007-05-30 12:57:21 -05001908static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
1909{
Mike Christieb6d44fe2007-07-26 12:46:47 -05001910 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
Mike Christied1d81c02007-05-30 12:57:21 -05001911 blk_queue_dma_alignment(sdev->request_queue, 0);
1912 return 0;
1913}
1914
Mike Christie5bb0b552006-04-06 21:26:46 -05001915static struct scsi_host_template iscsi_sht = {
Mike Christie79743922007-07-26 12:46:46 -05001916 .module = THIS_MODULE,
Mike Christief4246b32006-07-24 15:47:54 -05001917 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05001918 .queuecommand = iscsi_queuecommand,
1919 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -05001920 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie66bbe0c2007-12-13 12:43:39 -06001921 .sg_tablesize = 4096,
Mike Christie8231f0e2007-02-28 17:32:20 -06001922 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -05001923 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
1924 .eh_abort_handler = iscsi_eh_abort,
Mike Christie843c0a82007-12-13 12:43:20 -06001925 .eh_device_reset_handler= iscsi_eh_device_reset,
Mike Christie5bb0b552006-04-06 21:26:46 -05001926 .eh_host_reset_handler = iscsi_eh_host_reset,
1927 .use_clustering = DISABLE_CLUSTERING,
Mike Christied1d81c02007-05-30 12:57:21 -05001928 .slave_configure = iscsi_tcp_slave_configure,
Mike Christie5bb0b552006-04-06 21:26:46 -05001929 .proc_name = "iscsi_tcp",
1930 .this_id = -1,
1931};
1932
Alex Aizman7ba24712005-08-04 19:30:08 -07001933static struct iscsi_transport iscsi_tcp_transport = {
1934 .owner = THIS_MODULE,
1935 .name = "tcp",
1936 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
1937 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05001938 .param_mask = ISCSI_MAX_RECV_DLENGTH |
1939 ISCSI_MAX_XMIT_DLENGTH |
1940 ISCSI_HDRDGST_EN |
1941 ISCSI_DATADGST_EN |
1942 ISCSI_INITIAL_R2T_EN |
1943 ISCSI_MAX_R2T |
1944 ISCSI_IMM_DATA_EN |
1945 ISCSI_FIRST_BURST |
1946 ISCSI_MAX_BURST |
1947 ISCSI_PDU_INORDER_EN |
1948 ISCSI_DATASEQ_INORDER_EN |
1949 ISCSI_ERL |
1950 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05001951 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05001952 ISCSI_EXP_STATSN |
1953 ISCSI_PERSISTENT_PORT |
1954 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -05001955 ISCSI_TARGET_NAME | ISCSI_TPGT |
1956 ISCSI_USERNAME | ISCSI_PASSWORD |
Mike Christie843c0a82007-12-13 12:43:20 -06001957 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
Mike Christief6d51802007-12-13 12:43:30 -06001958 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
1959 ISCSI_LU_RESET_TMO |
1960 ISCSI_PING_TMO | ISCSI_RECV_TMO,
Mike Christie22236962007-05-30 12:57:24 -05001961 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
Mike Christied8196ed2007-05-30 12:57:25 -05001962 ISCSI_HOST_INITIATOR_NAME |
1963 ISCSI_HOST_NETDEV_NAME,
Mike Christie5bb0b552006-04-06 21:26:46 -05001964 /* session management */
1965 .create_session = iscsi_tcp_session_create,
1966 .destroy_session = iscsi_tcp_session_destroy,
1967 /* connection management */
1968 .create_conn = iscsi_tcp_conn_create,
1969 .bind_conn = iscsi_tcp_conn_bind,
1970 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07001971 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05001972 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06001973 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07001974 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05001975 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -05001976 /* iscsi host params */
Mike Christie75613522008-05-21 15:53:59 -05001977 .get_host_param = iscsi_host_get_param,
Mike Christie0801c242007-05-30 12:57:12 -05001978 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -05001979 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07001980 .send_pdu = iscsi_conn_send_pdu,
1981 .get_stats = iscsi_conn_get_stats,
Mike Christiefbc514b2008-05-21 15:54:07 -05001982 .init_task = iscsi_tcp_task_init,
1983 .xmit_task = iscsi_tcp_task_xmit,
1984 .cleanup_task = iscsi_tcp_cleanup_task,
Mike Christie5bb0b552006-04-06 21:26:46 -05001985 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05001986 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07001987};
1988
1989static int __init
1990iscsi_tcp_init(void)
1991{
Alex Aizman7ba24712005-08-04 19:30:08 -07001992 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05001993 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
1994 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07001995 return -EINVAL;
1996 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001997
Mike Christie75613522008-05-21 15:53:59 -05001998 iscsi_tcp_scsi_transport = iscsi_register_transport(
1999 &iscsi_tcp_transport);
2000 if (!iscsi_tcp_scsi_transport)
Mike Christieffbfe922006-05-18 20:31:36 -05002001 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002002
Mike Christie7b8631b2006-01-13 18:05:50 -06002003 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002004}
2005
2006static void __exit
2007iscsi_tcp_exit(void)
2008{
2009 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002010}
2011
2012module_init(iscsi_tcp_init);
2013module_exit(iscsi_tcp_exit);