blob: 4f096de81525d16d990aa8982debbbacab73a524 [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 Christie135a8ad2008-05-21 15:54:10 -0500501iscsi_tcp_cleanup_task(struct iscsi_conn *conn, struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -0700502{
Mike Christie135a8ad2008-05-21 15:54:10 -0500503 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500504 struct iscsi_r2t_info *r2t;
Alex Aizman7ba24712005-08-04 19:30:08 -0700505
Mike Christie135a8ad2008-05-21 15:54:10 -0500506 /* nothing to do for mgmt tasks */
507 if (!task->sc)
Mike Christiefbc514b2008-05-21 15:54:07 -0500508 return;
509
Mike Christie135a8ad2008-05-21 15:54:10 -0500510 /* flush task's r2t queues */
511 while (__kfifo_get(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
512 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christieb6c395e2006-07-24 15:47:15 -0500513 sizeof(void*));
Mike Christie135a8ad2008-05-21 15:54:10 -0500514 debug_scsi("iscsi_tcp_cleanup_task pending r2t dropped\n");
Mike Christieb6c395e2006-07-24 15:47:15 -0500515 }
516
Mike Christie135a8ad2008-05-21 15:54:10 -0500517 r2t = tcp_task->r2t;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600518 if (r2t != NULL) {
Mike Christie135a8ad2008-05-21 15:54:10 -0500519 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600520 sizeof(void*));
Mike Christie135a8ad2008-05-21 15:54:10 -0500521 tcp_task->r2t = NULL;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600522 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700523}
524
525/**
Mike Christie1d9edf02008-09-24 11:46:09 -0500526 * iscsi_data_in - SCSI Data-In Response processing
Alex Aizman7ba24712005-08-04 19:30:08 -0700527 * @conn: iscsi connection
Mike Christie135a8ad2008-05-21 15:54:10 -0500528 * @task: scsi command task
Alex Aizman7ba24712005-08-04 19:30:08 -0700529 **/
530static int
Mike Christie1d9edf02008-09-24 11:46:09 -0500531iscsi_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -0700532{
Mike Christie5bb0b552006-04-06 21:26:46 -0500533 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie135a8ad2008-05-21 15:54:10 -0500534 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500535 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700536 int datasn = be32_to_cpu(rhdr->datasn);
Mike Christie1d9edf02008-09-24 11:46:09 -0500537 unsigned total_in_length = scsi_in(task->sc)->length;
Alex Aizman7ba24712005-08-04 19:30:08 -0700538
Mike Christie1d9edf02008-09-24 11:46:09 -0500539 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
Mike Christie5bb0b552006-04-06 21:26:46 -0500540 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700541 return 0;
542
Mike Christie135a8ad2008-05-21 15:54:10 -0500543 if (tcp_task->exp_datasn != datasn) {
544 debug_tcp("%s: task->exp_datasn(%d) != rhdr->datasn(%d)\n",
Harvey Harrison3a12b192008-05-21 15:54:19 -0500545 __func__, tcp_task->exp_datasn, datasn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700546 return ISCSI_ERR_DATASN;
Mike Christied473cc72007-05-30 12:57:14 -0500547 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700548
Mike Christie135a8ad2008-05-21 15:54:10 -0500549 tcp_task->exp_datasn++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700550
Mike Christie135a8ad2008-05-21 15:54:10 -0500551 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
552 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
Mike Christie857ae0b2007-05-30 12:57:15 -0500553 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
Harvey Harrison3a12b192008-05-21 15:54:19 -0500554 __func__, tcp_task->data_offset,
Boaz Harrosh94795b62008-04-18 10:11:53 -0500555 tcp_conn->in.datalen, total_in_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700556 return ISCSI_ERR_DATA_OFFSET;
Mike Christie857ae0b2007-05-30 12:57:15 -0500557 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700558
Alex Aizman7ba24712005-08-04 19:30:08 -0700559 conn->datain_pdus_cnt++;
560 return 0;
561}
562
563/**
564 * iscsi_solicit_data_init - initialize first Data-Out
565 * @conn: iscsi connection
Mike Christie135a8ad2008-05-21 15:54:10 -0500566 * @task: scsi command task
Alex Aizman7ba24712005-08-04 19:30:08 -0700567 * @r2t: R2T info
568 *
569 * Notes:
570 * Initialize first Data-Out within this R2T sequence and finds
571 * proper data_offset within this SCSI command.
572 *
573 * This function is called with connection lock taken.
574 **/
575static void
Mike Christie135a8ad2008-05-21 15:54:10 -0500576iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_task *task,
Alex Aizman7ba24712005-08-04 19:30:08 -0700577 struct iscsi_r2t_info *r2t)
578{
579 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700580
Mike Christieffbfe922006-05-18 20:31:36 -0500581 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700582 memset(hdr, 0, sizeof(struct iscsi_data));
583 hdr->ttt = r2t->ttt;
584 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
585 r2t->solicit_datasn++;
586 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie135a8ad2008-05-21 15:54:10 -0500587 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
588 hdr->itt = task->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700589 hdr->exp_statsn = r2t->exp_statsn;
590 hdr->offset = cpu_to_be32(r2t->data_offset);
591 if (r2t->data_length > conn->max_xmit_dlength) {
592 hton24(hdr->dlength, conn->max_xmit_dlength);
593 r2t->data_count = conn->max_xmit_dlength;
594 hdr->flags = 0;
595 } else {
596 hton24(hdr->dlength, r2t->data_length);
597 r2t->data_count = r2t->data_length;
598 hdr->flags = ISCSI_FLAG_CMD_FINAL;
599 }
600 conn->dataout_pdus_cnt++;
601
602 r2t->sent = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700603}
604
605/**
606 * iscsi_r2t_rsp - iSCSI R2T Response processing
607 * @conn: iscsi connection
Mike Christie135a8ad2008-05-21 15:54:10 -0500608 * @task: scsi command task
Alex Aizman7ba24712005-08-04 19:30:08 -0700609 **/
610static int
Mike Christie135a8ad2008-05-21 15:54:10 -0500611iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -0700612{
613 struct iscsi_r2t_info *r2t;
614 struct iscsi_session *session = conn->session;
Mike Christie135a8ad2008-05-21 15:54:10 -0500615 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500616 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
617 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700618 int r2tsn = be32_to_cpu(rhdr->r2tsn);
619 int rc;
620
Mike Christie98a94162006-08-31 18:09:26 -0400621 if (tcp_conn->in.datalen) {
Mike Christie322d7392008-01-31 13:36:52 -0600622 iscsi_conn_printk(KERN_ERR, conn,
623 "invalid R2t with datalen %d\n",
624 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700625 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400626 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700627
Mike Christie135a8ad2008-05-21 15:54:10 -0500628 if (tcp_task->exp_datasn != r2tsn){
629 debug_tcp("%s: task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
Harvey Harrison3a12b192008-05-21 15:54:19 -0500630 __func__, tcp_task->exp_datasn, r2tsn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700631 return ISCSI_ERR_R2TSN;
Mike Christied473cc72007-05-30 12:57:14 -0500632 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700633
Mike Christie135a8ad2008-05-21 15:54:10 -0500634 /* fill-in new R2T associated with the task */
Mike Christie77a23c22007-05-30 12:57:18 -0500635 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
636
Mike Christie135a8ad2008-05-21 15:54:10 -0500637 if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie322d7392008-01-31 13:36:52 -0600638 iscsi_conn_printk(KERN_INFO, conn,
639 "dropping R2T itt %d in recovery.\n",
Mike Christie135a8ad2008-05-21 15:54:10 -0500640 task->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -0700641 return 0;
642 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500643
Mike Christie135a8ad2008-05-21 15:54:10 -0500644 rc = __kfifo_get(tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700645 BUG_ON(!rc);
646
647 r2t->exp_statsn = rhdr->statsn;
648 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400649 if (r2t->data_length == 0) {
Mike Christie322d7392008-01-31 13:36:52 -0600650 iscsi_conn_printk(KERN_ERR, conn,
651 "invalid R2T with zero data len\n");
Mike Christie135a8ad2008-05-21 15:54:10 -0500652 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
Olaf Kirch03766a12007-12-13 12:43:36 -0600653 sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700654 return ISCSI_ERR_DATALEN;
655 }
656
Mike Christie98a94162006-08-31 18:09:26 -0400657 if (r2t->data_length > session->max_burst)
658 debug_scsi("invalid R2T with data len %u and max burst %u."
659 "Attempting to execute request.\n",
660 r2t->data_length, session->max_burst);
661
Alex Aizman7ba24712005-08-04 19:30:08 -0700662 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
Mike Christie135a8ad2008-05-21 15:54:10 -0500663 if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) {
Mike Christie322d7392008-01-31 13:36:52 -0600664 iscsi_conn_printk(KERN_ERR, conn,
665 "invalid R2T with data len %u at offset %u "
666 "and total length %d\n", r2t->data_length,
Mike Christie135a8ad2008-05-21 15:54:10 -0500667 r2t->data_offset, scsi_out(task->sc)->length);
668 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
Olaf Kirch03766a12007-12-13 12:43:36 -0600669 sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700670 return ISCSI_ERR_DATALEN;
671 }
672
673 r2t->ttt = rhdr->ttt; /* no flip */
674 r2t->solicit_datasn = 0;
675
Mike Christie135a8ad2008-05-21 15:54:10 -0500676 iscsi_solicit_data_init(conn, task, r2t);
Alex Aizman7ba24712005-08-04 19:30:08 -0700677
Mike Christie135a8ad2008-05-21 15:54:10 -0500678 tcp_task->exp_datasn = r2tsn + 1;
679 __kfifo_put(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700680 conn->r2t_pdus_cnt++;
Mike Christie843c0a82007-12-13 12:43:20 -0600681
Mike Christie135a8ad2008-05-21 15:54:10 -0500682 iscsi_requeue_task(task);
Alex Aizman7ba24712005-08-04 19:30:08 -0700683 return 0;
684}
685
Olaf Kirchda32dd62007-12-13 12:43:21 -0600686/*
687 * Handle incoming reply to DataIn command
688 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700689static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600690iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600691 struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600692{
693 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
694 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
695 int rc;
696
Olaf Kircha8ac6312007-12-13 12:43:35 -0600697 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600698 return ISCSI_ERR_DATA_DGST;
699
700 /* check for non-exceptional status */
701 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
702 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
703 if (rc)
704 return rc;
705 }
706
707 iscsi_tcp_hdr_recv_prep(tcp_conn);
708 return 0;
709}
710
711/**
712 * iscsi_tcp_hdr_dissect - process PDU header
713 * @conn: iSCSI connection
714 * @hdr: PDU header
715 *
716 * This function analyzes the header of the PDU received,
717 * and performs several sanity checks. If the PDU is accompanied
718 * by data, the receive buffer is set up to copy the incoming data
719 * to the correct location.
720 */
721static int
722iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
Alex Aizman7ba24712005-08-04 19:30:08 -0700723{
Mike Christie5bb0b552006-04-06 21:26:46 -0500724 int rc = 0, opcode, ahslen;
Mike Christie5bb0b552006-04-06 21:26:46 -0500725 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie135a8ad2008-05-21 15:54:10 -0500726 struct iscsi_task *task;
Alex Aizman7ba24712005-08-04 19:30:08 -0700727
728 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500729 tcp_conn->in.datalen = ntoh24(hdr->dlength);
730 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Mike Christie322d7392008-01-31 13:36:52 -0600731 iscsi_conn_printk(KERN_ERR, conn,
732 "iscsi_tcp: datalen %d > %d\n",
733 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700734 return ISCSI_ERR_DATALEN;
735 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700736
Olaf Kirchda32dd62007-12-13 12:43:21 -0600737 /* Additional header segments. So far, we don't
738 * process additional headers.
739 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500740 ahslen = hdr->hlength << 2;
Alex Aizman7ba24712005-08-04 19:30:08 -0700741
Mike Christie5bb0b552006-04-06 21:26:46 -0500742 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700743 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie0af967f2008-05-21 15:54:04 -0500744 rc = iscsi_verify_itt(conn, hdr->itt);
Mike Christie7a53dc52007-12-13 12:43:37 -0600745 if (rc)
Mike Christie5bb0b552006-04-06 21:26:46 -0500746 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700747
Olaf Kirchda32dd62007-12-13 12:43:21 -0600748 debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
749 opcode, ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700750
Mike Christie5bb0b552006-04-06 21:26:46 -0500751 switch(opcode) {
752 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie913e5bf2008-05-21 15:54:18 -0500753 spin_lock(&conn->session->lock);
Mike Christie135a8ad2008-05-21 15:54:10 -0500754 task = iscsi_itt_to_ctask(conn, hdr->itt);
755 if (!task)
Mike Christie913e5bf2008-05-21 15:54:18 -0500756 rc = ISCSI_ERR_BAD_ITT;
757 else
Mike Christie1d9edf02008-09-24 11:46:09 -0500758 rc = iscsi_data_in(conn, task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500759 if (rc) {
760 spin_unlock(&conn->session->lock);
761 break;
762 }
Mike Christie0af967f2008-05-21 15:54:04 -0500763
Olaf Kirchda32dd62007-12-13 12:43:21 -0600764 if (tcp_conn->in.datalen) {
Mike Christie135a8ad2008-05-21 15:54:10 -0500765 struct iscsi_tcp_task *tcp_task = task->dd_data;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600766 struct hash_desc *rx_hash = NULL;
Mike Christie135a8ad2008-05-21 15:54:10 -0500767 struct scsi_data_buffer *sdb = scsi_in(task->sc);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600768
769 /*
770 * Setup copy of Data-In into the Scsi_Cmnd
771 * Scatterlist case:
Olaf Kircha8ac6312007-12-13 12:43:35 -0600772 * We set up the iscsi_segment to point to the next
Olaf Kirchda32dd62007-12-13 12:43:21 -0600773 * scatterlist entry to copy to. As we go along,
774 * we move on to the next scatterlist entry and
775 * update the digest per-entry.
776 */
777 if (conn->datadgst_en)
778 rx_hash = &tcp_conn->rx_hash;
779
780 debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
781 "datalen=%d)\n", tcp_conn,
Mike Christie135a8ad2008-05-21 15:54:10 -0500782 tcp_task->data_offset,
Olaf Kirchda32dd62007-12-13 12:43:21 -0600783 tcp_conn->in.datalen);
Mike Christie913e5bf2008-05-21 15:54:18 -0500784 rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
785 sdb->table.sgl,
786 sdb->table.nents,
787 tcp_task->data_offset,
788 tcp_conn->in.datalen,
789 iscsi_tcp_process_data_in,
790 rx_hash);
791 spin_unlock(&conn->session->lock);
792 return rc;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600793 }
Mike Christie913e5bf2008-05-21 15:54:18 -0500794 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
795 spin_unlock(&conn->session->lock);
796 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500797 case ISCSI_OP_SCSI_CMD_RSP:
Olaf Kirchda32dd62007-12-13 12:43:21 -0600798 if (tcp_conn->in.datalen) {
799 iscsi_tcp_data_recv_prep(tcp_conn);
800 return 0;
801 }
802 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500803 break;
804 case ISCSI_OP_R2T:
Mike Christie913e5bf2008-05-21 15:54:18 -0500805 spin_lock(&conn->session->lock);
Mike Christie135a8ad2008-05-21 15:54:10 -0500806 task = iscsi_itt_to_ctask(conn, hdr->itt);
807 if (!task)
Mike Christie913e5bf2008-05-21 15:54:18 -0500808 rc = ISCSI_ERR_BAD_ITT;
809 else if (ahslen)
Mike Christie5bb0b552006-04-06 21:26:46 -0500810 rc = ISCSI_ERR_AHSLEN;
Mike Christie913e5bf2008-05-21 15:54:18 -0500811 else if (task->sc->sc_data_direction == DMA_TO_DEVICE)
Mike Christie135a8ad2008-05-21 15:54:10 -0500812 rc = iscsi_r2t_rsp(conn, task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500813 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500814 rc = ISCSI_ERR_PROTO;
Mike Christie913e5bf2008-05-21 15:54:18 -0500815 spin_unlock(&conn->session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500816 break;
817 case ISCSI_OP_LOGIN_RSP:
818 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500819 case ISCSI_OP_REJECT:
820 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500821 /*
822 * It is possible that we could get a PDU with a buffer larger
823 * than 8K, but there are no targets that currently do this.
824 * For now we fail until we find a vendor that needs it
825 */
Olaf Kirchda32dd62007-12-13 12:43:21 -0600826 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
Mike Christie322d7392008-01-31 13:36:52 -0600827 iscsi_conn_printk(KERN_ERR, conn,
828 "iscsi_tcp: received buffer of "
829 "len %u but conn buffer is only %u "
830 "(opcode %0x)\n",
831 tcp_conn->in.datalen,
832 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
Mike Christiec8dc1e52006-07-24 15:47:39 -0500833 rc = ISCSI_ERR_PROTO;
834 break;
835 }
836
Olaf Kirchda32dd62007-12-13 12:43:21 -0600837 /* If there's data coming in with the response,
838 * receive it to the connection's buffer.
839 */
840 if (tcp_conn->in.datalen) {
841 iscsi_tcp_data_recv_prep(tcp_conn);
842 return 0;
843 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500844 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500845 case ISCSI_OP_LOGOUT_RSP:
846 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500847 case ISCSI_OP_SCSI_TMFUNC_RSP:
848 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
849 break;
850 default:
851 rc = ISCSI_ERR_BAD_OPCODE;
852 break;
853 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700854
Olaf Kirchda32dd62007-12-13 12:43:21 -0600855 if (rc == 0) {
856 /* Anything that comes with data should have
857 * been handled above. */
858 if (tcp_conn->in.datalen)
859 return ISCSI_ERR_PROTO;
860 iscsi_tcp_hdr_recv_prep(tcp_conn);
861 }
862
Alex Aizman7ba24712005-08-04 19:30:08 -0700863 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700864}
865
Olaf Kirchda32dd62007-12-13 12:43:21 -0600866/**
867 * iscsi_tcp_hdr_recv_done - process PDU header
868 *
869 * This is the callback invoked when the PDU header has
870 * been received. If the header is followed by additional
871 * header segments, we go back for more data.
872 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700873static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600874iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600875 struct iscsi_segment *segment)
Alex Aizman7ba24712005-08-04 19:30:08 -0700876{
Olaf Kirchda32dd62007-12-13 12:43:21 -0600877 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
878 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700879
Olaf Kirchda32dd62007-12-13 12:43:21 -0600880 /* Check if there are additional header segments
881 * *prior* to computing the digest, because we
882 * may need to go back to the caller for more.
883 */
884 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600885 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600886 /* Bump the header length - the caller will
887 * just loop around and get the AHS for us, and
888 * call again. */
889 unsigned int ahslen = hdr->hlength << 2;
Alex Aizman7ba24712005-08-04 19:30:08 -0700890
Olaf Kirchda32dd62007-12-13 12:43:21 -0600891 /* Make sure we don't overflow */
892 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
893 return ISCSI_ERR_AHSLEN;
894
Olaf Kircha8ac6312007-12-13 12:43:35 -0600895 segment->total_size += ahslen;
896 segment->size += ahslen;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600897 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700898 }
Olaf Kirchda32dd62007-12-13 12:43:21 -0600899
900 /* We're done processing the header. See if we're doing
901 * header digests; if so, set up the recv_digest buffer
902 * and go back for more. */
903 if (conn->hdrdgst_en) {
Olaf Kircha8ac6312007-12-13 12:43:35 -0600904 if (segment->digest_len == 0) {
905 iscsi_tcp_segment_splice_digest(segment,
906 segment->recv_digest);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600907 return 0;
908 }
909 iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600910 segment->total_copied - ISCSI_DIGEST_SIZE,
911 segment->digest);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600912
Olaf Kircha8ac6312007-12-13 12:43:35 -0600913 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600914 return ISCSI_ERR_HDR_DGST;
915 }
916
917 tcp_conn->in.hdr = hdr;
918 return iscsi_tcp_hdr_dissect(conn, hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -0700919}
920
921/**
Olaf Kirchda32dd62007-12-13 12:43:21 -0600922 * iscsi_tcp_recv - TCP receive in sendfile fashion
Alex Aizman7ba24712005-08-04 19:30:08 -0700923 * @rd_desc: read descriptor
924 * @skb: socket buffer
925 * @offset: offset in skb
926 * @len: skb->len - offset
927 **/
928static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600929iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
930 unsigned int offset, size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -0700931{
Alex Aizman7ba24712005-08-04 19:30:08 -0700932 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500933 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600934 struct iscsi_segment *segment = &tcp_conn->in.segment;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600935 struct skb_seq_state seq;
936 unsigned int consumed = 0;
937 int rc = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700938
Olaf Kirchda32dd62007-12-13 12:43:21 -0600939 debug_tcp("in %d bytes\n", skb->len - offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700940
941 if (unlikely(conn->suspend_rx)) {
942 debug_tcp("conn %d Rx suspended!\n", conn->id);
943 return 0;
944 }
945
Olaf Kirchda32dd62007-12-13 12:43:21 -0600946 skb_prepare_seq_read(skb, offset, skb->len, &seq);
947 while (1) {
948 unsigned int avail;
949 const u8 *ptr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700950
Olaf Kirchda32dd62007-12-13 12:43:21 -0600951 avail = skb_seq_read(consumed, &ptr, &seq);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600952 if (avail == 0) {
953 debug_tcp("no more data avail. Consumed %d\n",
954 consumed);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600955 break;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600956 }
957 BUG_ON(segment->copied >= segment->size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700958
Olaf Kirchda32dd62007-12-13 12:43:21 -0600959 debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600960 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600961 BUG_ON(rc == 0);
962 consumed += rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500963
Olaf Kircha8ac6312007-12-13 12:43:35 -0600964 if (segment->total_copied >= segment->total_size) {
965 debug_tcp("segment done\n");
966 rc = segment->done(tcp_conn, segment);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600967 if (rc != 0) {
968 skb_abort_seq_read(&seq);
969 goto error;
Mike Christiedbdb0162007-05-30 12:57:20 -0500970 }
Mike Christiedbdb0162007-05-30 12:57:20 -0500971
Olaf Kirchda32dd62007-12-13 12:43:21 -0600972 /* The done() functions sets up the
Olaf Kircha8ac6312007-12-13 12:43:35 -0600973 * next segment. */
Alex Aizman7ba24712005-08-04 19:30:08 -0700974 }
975 }
Olaf Kircha8ac6312007-12-13 12:43:35 -0600976 skb_abort_seq_read(&seq);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600977 conn->rxdata_octets += consumed;
978 return consumed;
Alex Aizman7ba24712005-08-04 19:30:08 -0700979
Olaf Kirchda32dd62007-12-13 12:43:21 -0600980error:
981 debug_tcp("Error receiving PDU, errno=%d\n", rc);
982 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
983 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700984}
985
986static void
987iscsi_tcp_data_ready(struct sock *sk, int flag)
988{
989 struct iscsi_conn *conn = sk->sk_user_data;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600990 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700991 read_descriptor_t rd_desc;
992
993 read_lock(&sk->sk_callback_lock);
994
Mike Christie665b44a2006-05-02 19:46:49 -0500995 /*
Olaf Kirchda32dd62007-12-13 12:43:21 -0600996 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
Mike Christie665b44a2006-05-02 19:46:49 -0500997 * We set count to 1 because we want the network layer to
Olaf Kirchda32dd62007-12-13 12:43:21 -0600998 * hand us all the skbs that are available. iscsi_tcp_recv
Mike Christie665b44a2006-05-02 19:46:49 -0500999 * handled pdus that cross buffers or pdus that still need data.
1000 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001001 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -05001002 rd_desc.count = 1;
Olaf Kirchda32dd62007-12-13 12:43:21 -06001003 tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
Alex Aizman7ba24712005-08-04 19:30:08 -07001004
1005 read_unlock(&sk->sk_callback_lock);
Olaf Kirchda32dd62007-12-13 12:43:21 -06001006
1007 /* If we had to (atomically) map a highmem page,
1008 * unmap it now. */
Olaf Kircha8ac6312007-12-13 12:43:35 -06001009 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
Alex Aizman7ba24712005-08-04 19:30:08 -07001010}
1011
1012static void
1013iscsi_tcp_state_change(struct sock *sk)
1014{
Mike Christie5bb0b552006-04-06 21:26:46 -05001015 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001016 struct iscsi_conn *conn;
1017 struct iscsi_session *session;
1018 void (*old_state_change)(struct sock *);
1019
1020 read_lock(&sk->sk_callback_lock);
1021
1022 conn = (struct iscsi_conn*)sk->sk_user_data;
1023 session = conn->session;
1024
Mike Christiee6273992005-11-29 23:12:49 -06001025 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1026 sk->sk_state == TCP_CLOSE) &&
1027 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001028 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1029 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1030 }
1031
Mike Christie5bb0b552006-04-06 21:26:46 -05001032 tcp_conn = conn->dd_data;
1033 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001034
1035 read_unlock(&sk->sk_callback_lock);
1036
1037 old_state_change(sk);
1038}
1039
1040/**
1041 * iscsi_write_space - Called when more output buffer space is available
1042 * @sk: socket space is available for
1043 **/
1044static void
1045iscsi_write_space(struct sock *sk)
1046{
1047 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001048 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1049
1050 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001051 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001052 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001053}
1054
1055static void
1056iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1057{
Mike Christie5bb0b552006-04-06 21:26:46 -05001058 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1059 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001060
1061 /* assign new callbacks */
1062 write_lock_bh(&sk->sk_callback_lock);
1063 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001064 tcp_conn->old_data_ready = sk->sk_data_ready;
1065 tcp_conn->old_state_change = sk->sk_state_change;
1066 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001067 sk->sk_data_ready = iscsi_tcp_data_ready;
1068 sk->sk_state_change = iscsi_tcp_state_change;
1069 sk->sk_write_space = iscsi_write_space;
1070 write_unlock_bh(&sk->sk_callback_lock);
1071}
1072
1073static void
Mike Christie1c834692006-07-24 15:47:26 -05001074iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001075{
Mike Christie5bb0b552006-04-06 21:26:46 -05001076 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001077
1078 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1079 write_lock_bh(&sk->sk_callback_lock);
1080 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001081 sk->sk_data_ready = tcp_conn->old_data_ready;
1082 sk->sk_state_change = tcp_conn->old_state_change;
1083 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001084 sk->sk_no_check = 0;
1085 write_unlock_bh(&sk->sk_callback_lock);
1086}
1087
1088/**
Olaf Kircha8ac6312007-12-13 12:43:35 -06001089 * iscsi_xmit - TCP transmit
1090 **/
1091static int
1092iscsi_xmit(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001093{
Mike Christie5bb0b552006-04-06 21:26:46 -05001094 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001095 struct iscsi_segment *segment = &tcp_conn->out.segment;
1096 unsigned int consumed = 0;
1097 int rc = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001098
Olaf Kircha8ac6312007-12-13 12:43:35 -06001099 while (1) {
1100 rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
1101 if (rc < 0)
1102 goto error;
1103 if (rc == 0)
1104 break;
1105
1106 consumed += rc;
1107
1108 if (segment->total_copied >= segment->total_size) {
1109 if (segment->done != NULL) {
1110 rc = segment->done(tcp_conn, segment);
1111 if (rc < 0)
1112 goto error;
1113 }
1114 }
1115 }
1116
1117 debug_tcp("xmit %d bytes\n", consumed);
1118
1119 conn->txdata_octets += consumed;
1120 return consumed;
1121
1122error:
1123 /* Transmit error. We could initiate error recovery
1124 * here. */
1125 debug_tcp("Error sending PDU, errno=%d\n", rc);
1126 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1127 return rc;
1128}
1129
1130/**
1131 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
1132 */
1133static inline int
1134iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
1135{
1136 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1137 struct iscsi_segment *segment = &tcp_conn->out.segment;
1138
1139 return segment->total_copied - segment->total_size;
1140}
1141
1142static inline int
1143iscsi_tcp_flush(struct iscsi_conn *conn)
1144{
1145 int rc;
1146
1147 while (iscsi_tcp_xmit_qlen(conn)) {
1148 rc = iscsi_xmit(conn);
1149 if (rc == 0)
1150 return -EAGAIN;
1151 if (rc < 0)
1152 return rc;
1153 }
1154
1155 return 0;
1156}
1157
1158/*
1159 * This is called when we're done sending the header.
1160 * Simply copy the data_segment to the send segment, and return.
1161 */
1162static int
1163iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
1164 struct iscsi_segment *segment)
1165{
1166 tcp_conn->out.segment = tcp_conn->out.data_segment;
1167 debug_tcp("Header done. Next segment size %u total_size %u\n",
1168 tcp_conn->out.segment.size, tcp_conn->out.segment.total_size);
1169 return 0;
1170}
1171
1172static void
1173iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen)
1174{
1175 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1176
Harvey Harrison3a12b192008-05-21 15:54:19 -05001177 debug_tcp("%s(%p%s)\n", __func__, tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001178 conn->hdrdgst_en? ", digest enabled" : "");
1179
1180 /* Clear the data segment - needs to be filled in by the
1181 * caller using iscsi_tcp_send_data_prep() */
1182 memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment));
1183
1184 /* If header digest is enabled, compute the CRC and
1185 * place the digest into the same buffer. We make
Mike Christie135a8ad2008-05-21 15:54:10 -05001186 * sure that both iscsi_tcp_task and mtask have
Olaf Kircha8ac6312007-12-13 12:43:35 -06001187 * sufficient room.
Mike Christie7cae5152006-01-13 18:05:47 -06001188 */
Olaf Kircha8ac6312007-12-13 12:43:35 -06001189 if (conn->hdrdgst_en) {
1190 iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
1191 hdr + hdrlen);
1192 hdrlen += ISCSI_DIGEST_SIZE;
Mike Christie3219e522006-05-30 00:37:28 -05001193 }
1194
Olaf Kircha8ac6312007-12-13 12:43:35 -06001195 /* Remember header pointer for later, when we need
1196 * to decide whether there's a payload to go along
1197 * with the header. */
1198 tcp_conn->out.hdr = hdr;
1199
1200 iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen,
1201 iscsi_tcp_send_hdr_done, NULL);
Alex Aizman7ba24712005-08-04 19:30:08 -07001202}
1203
Olaf Kircha8ac6312007-12-13 12:43:35 -06001204/*
1205 * Prepare the send buffer for the payload data.
1206 * Padding and checksumming will all be taken care
1207 * of by the iscsi_segment routines.
1208 */
1209static int
1210iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
1211 unsigned int count, unsigned int offset,
1212 unsigned int len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001213{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001214 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1215 struct hash_desc *tx_hash = NULL;
1216 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -07001217
Harvey Harrison3a12b192008-05-21 15:54:19 -05001218 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __func__,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001219 tcp_conn, offset, len,
1220 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -07001221
Olaf Kircha8ac6312007-12-13 12:43:35 -06001222 /* Make sure the datalen matches what the caller
1223 said he would send. */
1224 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1225 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -07001226
Olaf Kircha8ac6312007-12-13 12:43:35 -06001227 if (conn->datadgst_en)
1228 tx_hash = &tcp_conn->tx_hash;
1229
1230 return iscsi_segment_seek_sg(&tcp_conn->out.data_segment,
1231 sg, count, offset, len,
1232 NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -07001233}
1234
Olaf Kircha8ac6312007-12-13 12:43:35 -06001235static void
1236iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
1237 size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001238{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001239 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1240 struct hash_desc *tx_hash = NULL;
1241 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -07001242
Harvey Harrison3a12b192008-05-21 15:54:19 -05001243 debug_tcp("%s(%p, datalen=%d%s)\n", __func__, tcp_conn, len,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001244 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -07001245
Olaf Kircha8ac6312007-12-13 12:43:35 -06001246 /* Make sure the datalen matches what the caller
1247 said he would send. */
1248 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1249 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -07001250
Olaf Kircha8ac6312007-12-13 12:43:35 -06001251 if (conn->datadgst_en)
1252 tx_hash = &tcp_conn->tx_hash;
Alex Aizman7ba24712005-08-04 19:30:08 -07001253
Olaf Kircha8ac6312007-12-13 12:43:35 -06001254 iscsi_segment_init_linear(&tcp_conn->out.data_segment,
1255 data, len, NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -07001256}
1257
Alex Aizman7ba24712005-08-04 19:30:08 -07001258/**
1259 * iscsi_solicit_data_cont - initialize next Data-Out
1260 * @conn: iscsi connection
Mike Christie135a8ad2008-05-21 15:54:10 -05001261 * @task: scsi command task
Alex Aizman7ba24712005-08-04 19:30:08 -07001262 * @r2t: R2T info
1263 * @left: bytes left to transfer
1264 *
1265 * Notes:
1266 * Initialize next Data-Out within this R2T sequence and continue
1267 * to process next Scatter-Gather element(if any) of this SCSI command.
1268 *
1269 * Called under connection lock.
1270 **/
Olaf Kircha8ac6312007-12-13 12:43:35 -06001271static int
Mike Christie135a8ad2008-05-21 15:54:10 -05001272iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_task *task,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001273 struct iscsi_r2t_info *r2t)
Alex Aizman7ba24712005-08-04 19:30:08 -07001274{
1275 struct iscsi_data *hdr;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001276 int new_offset, left;
1277
1278 BUG_ON(r2t->data_length - r2t->sent < 0);
1279 left = r2t->data_length - r2t->sent;
1280 if (left == 0)
1281 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001282
Mike Christieffbfe922006-05-18 20:31:36 -05001283 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001284 memset(hdr, 0, sizeof(struct iscsi_data));
1285 hdr->ttt = r2t->ttt;
1286 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1287 r2t->solicit_datasn++;
1288 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie135a8ad2008-05-21 15:54:10 -05001289 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
1290 hdr->itt = task->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001291 hdr->exp_statsn = r2t->exp_statsn;
1292 new_offset = r2t->data_offset + r2t->sent;
1293 hdr->offset = cpu_to_be32(new_offset);
1294 if (left > conn->max_xmit_dlength) {
1295 hton24(hdr->dlength, conn->max_xmit_dlength);
1296 r2t->data_count = conn->max_xmit_dlength;
1297 } else {
1298 hton24(hdr->dlength, left);
1299 r2t->data_count = left;
1300 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1301 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001302
Alex Aizman7ba24712005-08-04 19:30:08 -07001303 conn->dataout_pdus_cnt++;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001304 return 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001305}
1306
1307/**
Mike Christiefbc514b2008-05-21 15:54:07 -05001308 * iscsi_tcp_task - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001309 * @conn: iscsi connection
Mike Christie135a8ad2008-05-21 15:54:10 -05001310 * @task: scsi command task
Alex Aizman7ba24712005-08-04 19:30:08 -07001311 * @sc: scsi command
1312 **/
Olaf Kircha8ac6312007-12-13 12:43:35 -06001313static int
Mike Christie135a8ad2008-05-21 15:54:10 -05001314iscsi_tcp_task_init(struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -07001315{
Mike Christie135a8ad2008-05-21 15:54:10 -05001316 struct iscsi_tcp_task *tcp_task = task->dd_data;
1317 struct iscsi_conn *conn = task->conn;
1318 struct scsi_cmnd *sc = task->sc;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001319 int err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001320
Mike Christiefbc514b2008-05-21 15:54:07 -05001321 if (!sc) {
1322 /*
Mike Christie135a8ad2008-05-21 15:54:10 -05001323 * mgmt tasks do not have a scatterlist since they come
Mike Christiefbc514b2008-05-21 15:54:07 -05001324 * in from the iscsi interface.
1325 */
Mike Christie135a8ad2008-05-21 15:54:10 -05001326 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id,
1327 task->itt);
Mike Christiefbc514b2008-05-21 15:54:07 -05001328
1329 /* Prepare PDU, optionally w/ immediate data */
Mike Christie135a8ad2008-05-21 15:54:10 -05001330 iscsi_tcp_send_hdr_prep(conn, task->hdr, sizeof(*task->hdr));
Mike Christiefbc514b2008-05-21 15:54:07 -05001331
1332 /* If we have immediate data, attach a payload */
Mike Christie135a8ad2008-05-21 15:54:10 -05001333 if (task->data_count)
1334 iscsi_tcp_send_linear_data_prepare(conn, task->data,
1335 task->data_count);
Mike Christiefbc514b2008-05-21 15:54:07 -05001336 return 0;
1337 }
1338
Mike Christie135a8ad2008-05-21 15:54:10 -05001339 BUG_ON(__kfifo_len(tcp_task->r2tqueue));
1340 tcp_task->sent = 0;
1341 tcp_task->exp_datasn = 0;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001342
1343 /* Prepare PDU, optionally w/ immediate data */
Mike Christie135a8ad2008-05-21 15:54:10 -05001344 debug_scsi("task deq [cid %d itt 0x%x imm %d unsol %d]\n",
1345 conn->id, task->itt, task->imm_count,
1346 task->unsol_count);
1347 iscsi_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
Olaf Kircha8ac6312007-12-13 12:43:35 -06001348
Mike Christie135a8ad2008-05-21 15:54:10 -05001349 if (!task->imm_count)
Olaf Kircha8ac6312007-12-13 12:43:35 -06001350 return 0;
1351
1352 /* If we have immediate data, attach a payload */
Boaz Harrosh94795b62008-04-18 10:11:53 -05001353 err = iscsi_tcp_send_data_prep(conn, scsi_out(sc)->table.sgl,
1354 scsi_out(sc)->table.nents,
Mike Christie135a8ad2008-05-21 15:54:10 -05001355 0, task->imm_count);
Olaf Kircha8ac6312007-12-13 12:43:35 -06001356 if (err)
1357 return err;
Mike Christie135a8ad2008-05-21 15:54:10 -05001358 tcp_task->sent += task->imm_count;
1359 task->imm_count = 0;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001360 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001361}
1362
Olaf Kircha8ac6312007-12-13 12:43:35 -06001363/*
Mike Christie135a8ad2008-05-21 15:54:10 -05001364 * iscsi_tcp_task_xmit - xmit normal PDU task
1365 * @task: iscsi command task
Mike Christie218432c2007-05-30 12:57:17 -05001366 *
Olaf Kircha8ac6312007-12-13 12:43:35 -06001367 * We're expected to return 0 when everything was transmitted succesfully,
1368 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1369 * of error.
1370 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001371static int
Mike Christie135a8ad2008-05-21 15:54:10 -05001372iscsi_tcp_task_xmit(struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -07001373{
Mike Christie135a8ad2008-05-21 15:54:10 -05001374 struct iscsi_conn *conn = task->conn;
1375 struct iscsi_tcp_task *tcp_task = task->dd_data;
1376 struct scsi_cmnd *sc = task->sc;
Mike Christiefbc514b2008-05-21 15:54:07 -05001377 struct scsi_data_buffer *sdb;
Alex Aizman7ba24712005-08-04 19:30:08 -07001378 int rc = 0;
1379
Olaf Kircha8ac6312007-12-13 12:43:35 -06001380flush:
1381 /* Flush any pending data first. */
1382 rc = iscsi_tcp_flush(conn);
1383 if (rc < 0)
Mike Christie218432c2007-05-30 12:57:17 -05001384 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001385
Mike Christiefbc514b2008-05-21 15:54:07 -05001386 /* mgmt command */
1387 if (!sc) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001388 if (task->hdr->itt == RESERVED_ITT)
1389 iscsi_put_task(task);
Mike Christiefbc514b2008-05-21 15:54:07 -05001390 return 0;
1391 }
1392
Olaf Kircha8ac6312007-12-13 12:43:35 -06001393 /* Are we done already? */
1394 if (sc->sc_data_direction != DMA_TO_DEVICE)
Mike Christie218432c2007-05-30 12:57:17 -05001395 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001396
Mike Christiefbc514b2008-05-21 15:54:07 -05001397 sdb = scsi_out(sc);
Mike Christie135a8ad2008-05-21 15:54:10 -05001398 if (task->unsol_count != 0) {
1399 struct iscsi_data *hdr = &tcp_task->unsol_dtask.hdr;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001400
1401 /* Prepare a header for the unsolicited PDU.
1402 * The amount of data we want to send will be
Mike Christie135a8ad2008-05-21 15:54:10 -05001403 * in task->data_count.
Olaf Kircha8ac6312007-12-13 12:43:35 -06001404 * FIXME: return the data count instead.
1405 */
Mike Christie135a8ad2008-05-21 15:54:10 -05001406 iscsi_prep_unsolicit_data_pdu(task, hdr);
Olaf Kircha8ac6312007-12-13 12:43:35 -06001407
1408 debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n",
Mike Christie135a8ad2008-05-21 15:54:10 -05001409 task->itt, tcp_task->sent, task->data_count);
Olaf Kircha8ac6312007-12-13 12:43:35 -06001410
1411 iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr));
Boaz Harrosh94795b62008-04-18 10:11:53 -05001412 rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
Mike Christie135a8ad2008-05-21 15:54:10 -05001413 sdb->table.nents, tcp_task->sent,
1414 task->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001415 if (rc)
Olaf Kircha8ac6312007-12-13 12:43:35 -06001416 goto fail;
Mike Christie135a8ad2008-05-21 15:54:10 -05001417 tcp_task->sent += task->data_count;
1418 task->unsol_count -= task->data_count;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001419 goto flush;
1420 } else {
1421 struct iscsi_session *session = conn->session;
1422 struct iscsi_r2t_info *r2t;
1423
1424 /* All unsolicited PDUs sent. Check for solicited PDUs.
1425 */
1426 spin_lock_bh(&session->lock);
Mike Christie135a8ad2008-05-21 15:54:10 -05001427 r2t = tcp_task->r2t;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001428 if (r2t != NULL) {
1429 /* Continue with this R2T? */
Mike Christie135a8ad2008-05-21 15:54:10 -05001430 if (!iscsi_solicit_data_cont(conn, task, r2t)) {
Olaf Kircha8ac6312007-12-13 12:43:35 -06001431 debug_scsi(" done with r2t %p\n", r2t);
1432
Mike Christie135a8ad2008-05-21 15:54:10 -05001433 __kfifo_put(tcp_task->r2tpool.queue,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001434 (void*)&r2t, sizeof(void*));
Mike Christie135a8ad2008-05-21 15:54:10 -05001435 tcp_task->r2t = r2t = NULL;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001436 }
1437 }
1438
1439 if (r2t == NULL) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001440 __kfifo_get(tcp_task->r2tqueue, (void*)&tcp_task->r2t,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001441 sizeof(void*));
Mike Christie135a8ad2008-05-21 15:54:10 -05001442 r2t = tcp_task->r2t;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001443 }
1444 spin_unlock_bh(&session->lock);
1445
1446 /* Waiting for more R2Ts to arrive. */
1447 if (r2t == NULL) {
1448 debug_tcp("no R2Ts yet\n");
1449 return 0;
1450 }
1451
1452 debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
Mike Christie135a8ad2008-05-21 15:54:10 -05001453 r2t, r2t->solicit_datasn - 1, task->itt,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001454 r2t->data_offset + r2t->sent, r2t->data_count);
1455
1456 iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr,
1457 sizeof(struct iscsi_hdr));
1458
Boaz Harrosh94795b62008-04-18 10:11:53 -05001459 rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
1460 sdb->table.nents,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001461 r2t->data_offset + r2t->sent,
1462 r2t->data_count);
1463 if (rc)
1464 goto fail;
Mike Christie135a8ad2008-05-21 15:54:10 -05001465 tcp_task->sent += r2t->data_count;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001466 r2t->sent += r2t->data_count;
1467 goto flush;
Alex Aizman7ba24712005-08-04 19:30:08 -07001468 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001469 return 0;
1470fail:
1471 iscsi_conn_failure(conn, rc);
1472 return -EIO;
Alex Aizman7ba24712005-08-04 19:30:08 -07001473}
1474
Mike Christie7b8631b2006-01-13 18:05:50 -06001475static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001476iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001477{
Mike Christie7b8631b2006-01-13 18:05:50 -06001478 struct iscsi_conn *conn;
1479 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001480 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001481
Mike Christie5d91e202008-05-21 15:54:01 -05001482 cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001483 if (!cls_conn)
1484 return NULL;
1485 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001486 /*
1487 * due to strange issues with iser these are not set
1488 * in iscsi_conn_setup
1489 */
Mike Christiebf32ed32007-02-28 17:32:17 -06001490 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
Alex Aizman7ba24712005-08-04 19:30:08 -07001491
Mike Christie5d91e202008-05-21 15:54:01 -05001492 tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001493 tcp_conn->iscsi_conn = conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001494
James Bottomleyc9802cd2006-09-23 15:33:43 -05001495 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1496 CRYPTO_ALG_ASYNC);
1497 tcp_conn->tx_hash.flags = 0;
Mike Christie322d7392008-01-31 13:36:52 -06001498 if (IS_ERR(tcp_conn->tx_hash.tfm))
Mike Christie5d91e202008-05-21 15:54:01 -05001499 goto free_conn;
Mike Christiedd8c0d92006-08-31 18:09:28 -04001500
James Bottomleyc9802cd2006-09-23 15:33:43 -05001501 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1502 CRYPTO_ALG_ASYNC);
1503 tcp_conn->rx_hash.flags = 0;
Mike Christie322d7392008-01-31 13:36:52 -06001504 if (IS_ERR(tcp_conn->rx_hash.tfm))
Mike Christiedd8c0d92006-08-31 18:09:28 -04001505 goto free_tx_tfm;
1506
Mike Christie7b8631b2006-01-13 18:05:50 -06001507 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001508
Mike Christiedd8c0d92006-08-31 18:09:28 -04001509free_tx_tfm:
James Bottomleyc9802cd2006-09-23 15:33:43 -05001510 crypto_free_hash(tcp_conn->tx_hash.tfm);
Mike Christie5d91e202008-05-21 15:54:01 -05001511free_conn:
Mike Christie322d7392008-01-31 13:36:52 -06001512 iscsi_conn_printk(KERN_ERR, conn,
1513 "Could not create connection due to crc32c "
1514 "loading error. Make sure the crc32c "
1515 "module is built as a module or into the "
1516 "kernel\n");
Mike Christie5bb0b552006-04-06 21:26:46 -05001517 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001518 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001519}
1520
1521static void
Mike Christie1c834692006-07-24 15:47:26 -05001522iscsi_tcp_release_conn(struct iscsi_conn *conn)
1523{
Mike Christie22236962007-05-30 12:57:24 -05001524 struct iscsi_session *session = conn->session;
Mike Christie1c834692006-07-24 15:47:26 -05001525 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie22236962007-05-30 12:57:24 -05001526 struct socket *sock = tcp_conn->sock;
Mike Christie1c834692006-07-24 15:47:26 -05001527
Mike Christie22236962007-05-30 12:57:24 -05001528 if (!sock)
Mike Christie1c834692006-07-24 15:47:26 -05001529 return;
1530
Mike Christie22236962007-05-30 12:57:24 -05001531 sock_hold(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001532 iscsi_conn_restore_callbacks(tcp_conn);
Mike Christie22236962007-05-30 12:57:24 -05001533 sock_put(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001534
Mike Christie22236962007-05-30 12:57:24 -05001535 spin_lock_bh(&session->lock);
Mike Christie1c834692006-07-24 15:47:26 -05001536 tcp_conn->sock = NULL;
Mike Christie22236962007-05-30 12:57:24 -05001537 spin_unlock_bh(&session->lock);
1538 sockfd_put(sock);
Mike Christie1c834692006-07-24 15:47:26 -05001539}
1540
1541static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001542iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001543{
Mike Christie7b8631b2006-01-13 18:05:50 -06001544 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001545 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001546
Mike Christie1c834692006-07-24 15:47:26 -05001547 iscsi_tcp_release_conn(conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001548
Pete Wyckoff534284a2006-11-08 15:58:31 -06001549 if (tcp_conn->tx_hash.tfm)
1550 crypto_free_hash(tcp_conn->tx_hash.tfm);
1551 if (tcp_conn->rx_hash.tfm)
1552 crypto_free_hash(tcp_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001553
Mike Christie5d91e202008-05-21 15:54:01 -05001554 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001555}
1556
Mike Christie1c834692006-07-24 15:47:26 -05001557static void
1558iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1559{
1560 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie913e5bf2008-05-21 15:54:18 -05001561 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1562
1563 /* userspace may have goofed up and not bound us */
1564 if (!tcp_conn->sock)
1565 return;
1566 /*
1567 * Make sure our recv side is stopped.
1568 * Older tools called conn stop before ep_disconnect
1569 * so IO could still be coming in.
1570 */
1571 write_lock_bh(&tcp_conn->sock->sk->sk_callback_lock);
1572 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1573 write_unlock_bh(&tcp_conn->sock->sk->sk_callback_lock);
Mike Christie1c834692006-07-24 15:47:26 -05001574
1575 iscsi_conn_stop(cls_conn, flag);
1576 iscsi_tcp_release_conn(conn);
1577}
1578
Mike Christie22236962007-05-30 12:57:24 -05001579static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
1580 char *buf, int *port,
1581 int (*getname)(struct socket *, struct sockaddr *,
1582 int *addrlen))
1583{
1584 struct sockaddr_storage *addr;
1585 struct sockaddr_in6 *sin6;
1586 struct sockaddr_in *sin;
1587 int rc = 0, len;
1588
Al Viroa9204872007-07-20 16:03:40 +01001589 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
Mike Christie22236962007-05-30 12:57:24 -05001590 if (!addr)
1591 return -ENOMEM;
1592
1593 if (getname(sock, (struct sockaddr *) addr, &len)) {
1594 rc = -ENODEV;
1595 goto free_addr;
1596 }
1597
1598 switch (addr->ss_family) {
1599 case AF_INET:
1600 sin = (struct sockaddr_in *)addr;
1601 spin_lock_bh(&conn->session->lock);
1602 sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
1603 *port = be16_to_cpu(sin->sin_port);
1604 spin_unlock_bh(&conn->session->lock);
1605 break;
1606 case AF_INET6:
1607 sin6 = (struct sockaddr_in6 *)addr;
1608 spin_lock_bh(&conn->session->lock);
1609 sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
1610 *port = be16_to_cpu(sin6->sin6_port);
1611 spin_unlock_bh(&conn->session->lock);
1612 break;
1613 }
1614free_addr:
1615 kfree(addr);
1616 return rc;
1617}
1618
Alex Aizman7ba24712005-08-04 19:30:08 -07001619static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001620iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001621 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001622 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001623{
Mike Christie75613522008-05-21 15:53:59 -05001624 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1625 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001626 struct iscsi_conn *conn = cls_conn->dd_data;
1627 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001628 struct sock *sk;
1629 struct socket *sock;
1630 int err;
1631
1632 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001633 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001634 if (!sock) {
Mike Christie322d7392008-01-31 13:36:52 -06001635 iscsi_conn_printk(KERN_ERR, conn,
1636 "sockfd_lookup failed %d\n", err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001637 return -EEXIST;
1638 }
Mike Christie22236962007-05-30 12:57:24 -05001639 /*
1640 * copy these values now because if we drop the session
1641 * userspace may still want to query the values since we will
1642 * be using them for the reconnect
1643 */
1644 err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1645 &conn->portal_port, kernel_getpeername);
1646 if (err)
1647 goto free_socket;
1648
Mike Christie75613522008-05-21 15:53:59 -05001649 err = iscsi_tcp_get_addr(conn, sock, ihost->local_address,
1650 &ihost->local_port, kernel_getsockname);
Mike Christie22236962007-05-30 12:57:24 -05001651 if (err)
1652 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001653
Mike Christie5bb0b552006-04-06 21:26:46 -05001654 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1655 if (err)
Mike Christie22236962007-05-30 12:57:24 -05001656 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001657
Mike Christie67a61112006-05-30 00:37:20 -05001658 /* bind iSCSI connection and socket */
1659 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001660
Mike Christie67a61112006-05-30 00:37:20 -05001661 /* setup Socket parameters */
1662 sk = sock->sk;
1663 sk->sk_reuse = 1;
1664 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1665 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001666
Mike Christie67a61112006-05-30 00:37:20 -05001667 iscsi_conn_set_callbacks(conn);
1668 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1669 /*
1670 * set receive state machine into initial state
1671 */
Olaf Kirchda32dd62007-12-13 12:43:21 -06001672 iscsi_tcp_hdr_recv_prep(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001673 return 0;
Mike Christie22236962007-05-30 12:57:24 -05001674
1675free_socket:
1676 sockfd_put(sock);
1677 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001678}
1679
Alex Aizman7ba24712005-08-04 19:30:08 -07001680static int
1681iscsi_r2tpool_alloc(struct iscsi_session *session)
1682{
1683 int i;
1684 int cmd_i;
1685
1686 /*
Mike Christie135a8ad2008-05-21 15:54:10 -05001687 * initialize per-task: R2T pool and xmit queue
Alex Aizman7ba24712005-08-04 19:30:08 -07001688 */
1689 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001690 struct iscsi_task *task = session->cmds[cmd_i];
1691 struct iscsi_tcp_task *tcp_task = task->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001692
1693 /*
1694 * pre-allocated x4 as much r2ts to handle race when
1695 * target acks DataOut faster than we data_xmit() queues
1696 * could replenish r2tqueue.
1697 */
1698
1699 /* R2T pool */
Mike Christie135a8ad2008-05-21 15:54:10 -05001700 if (iscsi_pool_init(&tcp_task->r2tpool, session->max_r2t * 4, NULL,
Mike Christie5bb0b552006-04-06 21:26:46 -05001701 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001702 goto r2t_alloc_fail;
1703 }
1704
1705 /* R2T xmit queue */
Mike Christie135a8ad2008-05-21 15:54:10 -05001706 tcp_task->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07001707 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie135a8ad2008-05-21 15:54:10 -05001708 if (tcp_task->r2tqueue == ERR_PTR(-ENOMEM)) {
1709 iscsi_pool_free(&tcp_task->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001710 goto r2t_alloc_fail;
1711 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001712 }
1713
1714 return 0;
1715
1716r2t_alloc_fail:
1717 for (i = 0; i < cmd_i; i++) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001718 struct iscsi_task *task = session->cmds[i];
1719 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001720
Mike Christie135a8ad2008-05-21 15:54:10 -05001721 kfifo_free(tcp_task->r2tqueue);
1722 iscsi_pool_free(&tcp_task->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001723 }
1724 return -ENOMEM;
1725}
1726
1727static void
1728iscsi_r2tpool_free(struct iscsi_session *session)
1729{
1730 int i;
1731
1732 for (i = 0; i < session->cmds_max; i++) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001733 struct iscsi_task *task = session->cmds[i];
1734 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001735
Mike Christie135a8ad2008-05-21 15:54:10 -05001736 kfifo_free(tcp_task->r2tqueue);
1737 iscsi_pool_free(&tcp_task->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001738 }
1739}
1740
Alex Aizman7ba24712005-08-04 19:30:08 -07001741static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001742iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05001743 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07001744{
Mike Christie7b7232f2006-02-01 21:06:49 -06001745 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001746 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001747 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001748 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07001749
Alex Aizman7ba24712005-08-04 19:30:08 -07001750 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001751 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001752 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001753 break;
1754 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001755 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05001756 tcp_conn->sendpage = conn->datadgst_en ?
1757 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07001758 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07001759 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001760 sscanf(buf, "%d", &value);
Mike Christiedf93ffc2007-12-13 12:43:42 -06001761 if (value <= 0 || !is_power_of_2(value))
1762 return -EINVAL;
1763 if (session->max_r2t == value)
Alex Aizman7ba24712005-08-04 19:30:08 -07001764 break;
1765 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05001766 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001767 if (iscsi_r2tpool_alloc(session))
1768 return -ENOMEM;
1769 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07001770 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001771 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001772 }
1773
1774 return 0;
1775}
1776
1777static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05001778iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
1779 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06001780{
Mike Christie7b7232f2006-02-01 21:06:49 -06001781 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001782 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06001783
1784 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05001785 case ISCSI_PARAM_CONN_PORT:
Mike Christie22236962007-05-30 12:57:24 -05001786 spin_lock_bh(&conn->session->lock);
1787 len = sprintf(buf, "%hu\n", conn->portal_port);
1788 spin_unlock_bh(&conn->session->lock);
Mike Christie8d2860b2006-05-02 19:46:47 -05001789 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001790 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie22236962007-05-30 12:57:24 -05001791 spin_lock_bh(&conn->session->lock);
1792 len = sprintf(buf, "%s\n", conn->portal_address);
1793 spin_unlock_bh(&conn->session->lock);
Mike Christiefd7255f2006-04-06 21:13:36 -05001794 break;
1795 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001796 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05001797 }
1798
1799 return len;
1800}
1801
Alex Aizman7ba24712005-08-04 19:30:08 -07001802static void
Mike Christie7b7232f2006-02-01 21:06:49 -06001803iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07001804{
Mike Christie7b7232f2006-02-01 21:06:49 -06001805 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001806 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001807
1808 stats->txdata_octets = conn->txdata_octets;
1809 stats->rxdata_octets = conn->rxdata_octets;
1810 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1811 stats->dataout_pdus = conn->dataout_pdus_cnt;
1812 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1813 stats->datain_pdus = conn->datain_pdus_cnt;
1814 stats->r2t_pdus = conn->r2t_pdus_cnt;
1815 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1816 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1817 stats->custom_length = 3;
1818 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05001819 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001820 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05001821 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001822 strcpy(stats->custom[2].desc, "eh_abort_cnt");
1823 stats->custom[2].value = conn->eh_abort_cnt;
1824}
1825
Mike Christie5bb0b552006-04-06 21:26:46 -05001826static struct iscsi_cls_session *
Mike Christie06520ede2008-05-21 15:54:15 -05001827iscsi_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
Mike Christie40753ca2008-05-21 15:53:56 -05001828 uint16_t qdepth, uint32_t initial_cmdsn,
1829 uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07001830{
Mike Christie5bb0b552006-04-06 21:26:46 -05001831 struct iscsi_cls_session *cls_session;
1832 struct iscsi_session *session;
Mike Christie06520ede2008-05-21 15:54:15 -05001833 struct Scsi_Host *shost;
Mike Christie5bb0b552006-04-06 21:26:46 -05001834 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07001835
Mike Christie06520ede2008-05-21 15:54:15 -05001836 if (ep) {
1837 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
Mike Christie5bb0b552006-04-06 21:26:46 -05001838 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05001839 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001840
Mike Christiea4804cd2008-05-21 15:54:00 -05001841 shost = iscsi_host_alloc(&iscsi_sht, 0, qdepth);
Mike Christie75613522008-05-21 15:53:59 -05001842 if (!shost)
1843 return NULL;
1844 shost->transportt = iscsi_tcp_scsi_transport;
1845 shost->max_lun = iscsi_max_lun;
1846 shost->max_id = 0;
1847 shost->max_channel = 0;
Boaz Harrosh30e9ba92008-05-26 11:31:19 +03001848 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
Mike Christie75613522008-05-21 15:53:59 -05001849
Mike Christiea4804cd2008-05-21 15:54:00 -05001850 if (iscsi_host_add(shost, NULL))
Mike Christie75613522008-05-21 15:53:59 -05001851 goto free_host;
1852 *hostno = shost->host_no;
1853
1854 cls_session = iscsi_session_setup(&iscsi_tcp_transport, shost, cmds_max,
Mike Christie135a8ad2008-05-21 15:54:10 -05001855 sizeof(struct iscsi_tcp_task),
Mike Christie79706342008-05-21 15:54:12 -05001856 initial_cmdsn, 0);
Mike Christie75613522008-05-21 15:53:59 -05001857 if (!cls_session)
1858 goto remove_host;
1859 session = cls_session->dd_data;
1860
Mike Christiefbc514b2008-05-21 15:54:07 -05001861 shost->can_queue = session->scsi_cmds_max;
Mike Christie5bb0b552006-04-06 21:26:46 -05001862 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001863 struct iscsi_task *task = session->cmds[cmd_i];
1864 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001865
Mike Christie135a8ad2008-05-21 15:54:10 -05001866 task->hdr = &tcp_task->hdr.cmd_hdr;
1867 task->hdr_max = sizeof(tcp_task->hdr) - ISCSI_DIGEST_SIZE;
Mike Christie5bb0b552006-04-06 21:26:46 -05001868 }
1869
Mike Christie75613522008-05-21 15:53:59 -05001870 if (iscsi_r2tpool_alloc(session))
1871 goto remove_session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001872 return cls_session;
1873
Mike Christie75613522008-05-21 15:53:59 -05001874remove_session:
Mike Christie5bb0b552006-04-06 21:26:46 -05001875 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -05001876remove_host:
Mike Christiea4804cd2008-05-21 15:54:00 -05001877 iscsi_host_remove(shost);
Mike Christie75613522008-05-21 15:53:59 -05001878free_host:
Mike Christiea4804cd2008-05-21 15:54:00 -05001879 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001880 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001881}
1882
Mike Christie5bb0b552006-04-06 21:26:46 -05001883static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
1884{
Mike Christie75613522008-05-21 15:53:59 -05001885 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1886
1887 iscsi_r2tpool_free(cls_session->dd_data);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001888 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -05001889
Mike Christiea4804cd2008-05-21 15:54:00 -05001890 iscsi_host_remove(shost);
1891 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001892}
1893
Mike Christied1d81c02007-05-30 12:57:21 -05001894static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
1895{
Mike Christieb6d44fe2007-07-26 12:46:47 -05001896 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
Mike Christied1d81c02007-05-30 12:57:21 -05001897 blk_queue_dma_alignment(sdev->request_queue, 0);
1898 return 0;
1899}
1900
Mike Christie5bb0b552006-04-06 21:26:46 -05001901static struct scsi_host_template iscsi_sht = {
Mike Christie79743922007-07-26 12:46:46 -05001902 .module = THIS_MODULE,
Mike Christief4246b32006-07-24 15:47:54 -05001903 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05001904 .queuecommand = iscsi_queuecommand,
1905 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -05001906 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie66bbe0c2007-12-13 12:43:39 -06001907 .sg_tablesize = 4096,
Mike Christie8231f0e2007-02-28 17:32:20 -06001908 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -05001909 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
1910 .eh_abort_handler = iscsi_eh_abort,
Mike Christie843c0a82007-12-13 12:43:20 -06001911 .eh_device_reset_handler= iscsi_eh_device_reset,
Mike Christie8e124522008-09-24 11:46:12 -05001912 .eh_target_reset_handler= iscsi_eh_target_reset,
Mike Christie5bb0b552006-04-06 21:26:46 -05001913 .use_clustering = DISABLE_CLUSTERING,
Mike Christied1d81c02007-05-30 12:57:21 -05001914 .slave_configure = iscsi_tcp_slave_configure,
Mike Christie5bb0b552006-04-06 21:26:46 -05001915 .proc_name = "iscsi_tcp",
1916 .this_id = -1,
1917};
1918
Alex Aizman7ba24712005-08-04 19:30:08 -07001919static struct iscsi_transport iscsi_tcp_transport = {
1920 .owner = THIS_MODULE,
1921 .name = "tcp",
1922 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
1923 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05001924 .param_mask = ISCSI_MAX_RECV_DLENGTH |
1925 ISCSI_MAX_XMIT_DLENGTH |
1926 ISCSI_HDRDGST_EN |
1927 ISCSI_DATADGST_EN |
1928 ISCSI_INITIAL_R2T_EN |
1929 ISCSI_MAX_R2T |
1930 ISCSI_IMM_DATA_EN |
1931 ISCSI_FIRST_BURST |
1932 ISCSI_MAX_BURST |
1933 ISCSI_PDU_INORDER_EN |
1934 ISCSI_DATASEQ_INORDER_EN |
1935 ISCSI_ERL |
1936 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05001937 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05001938 ISCSI_EXP_STATSN |
1939 ISCSI_PERSISTENT_PORT |
1940 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -05001941 ISCSI_TARGET_NAME | ISCSI_TPGT |
1942 ISCSI_USERNAME | ISCSI_PASSWORD |
Mike Christie843c0a82007-12-13 12:43:20 -06001943 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
Mike Christief6d51802007-12-13 12:43:30 -06001944 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
1945 ISCSI_LU_RESET_TMO |
Mike Christie88dfd342008-05-21 15:54:16 -05001946 ISCSI_PING_TMO | ISCSI_RECV_TMO |
1947 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
Mike Christie22236962007-05-30 12:57:24 -05001948 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
Mike Christied8196ed2007-05-30 12:57:25 -05001949 ISCSI_HOST_INITIATOR_NAME |
1950 ISCSI_HOST_NETDEV_NAME,
Mike Christie5bb0b552006-04-06 21:26:46 -05001951 /* session management */
1952 .create_session = iscsi_tcp_session_create,
1953 .destroy_session = iscsi_tcp_session_destroy,
1954 /* connection management */
1955 .create_conn = iscsi_tcp_conn_create,
1956 .bind_conn = iscsi_tcp_conn_bind,
1957 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07001958 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05001959 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06001960 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07001961 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05001962 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -05001963 /* iscsi host params */
Mike Christie75613522008-05-21 15:53:59 -05001964 .get_host_param = iscsi_host_get_param,
Mike Christie0801c242007-05-30 12:57:12 -05001965 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -05001966 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07001967 .send_pdu = iscsi_conn_send_pdu,
1968 .get_stats = iscsi_conn_get_stats,
Mike Christiefbc514b2008-05-21 15:54:07 -05001969 .init_task = iscsi_tcp_task_init,
1970 .xmit_task = iscsi_tcp_task_xmit,
1971 .cleanup_task = iscsi_tcp_cleanup_task,
Mike Christie5bb0b552006-04-06 21:26:46 -05001972 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05001973 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07001974};
1975
1976static int __init
1977iscsi_tcp_init(void)
1978{
Alex Aizman7ba24712005-08-04 19:30:08 -07001979 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05001980 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
1981 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07001982 return -EINVAL;
1983 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001984
Mike Christie75613522008-05-21 15:53:59 -05001985 iscsi_tcp_scsi_transport = iscsi_register_transport(
1986 &iscsi_tcp_transport);
1987 if (!iscsi_tcp_scsi_transport)
Mike Christieffbfe922006-05-18 20:31:36 -05001988 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07001989
Mike Christie7b8631b2006-01-13 18:05:50 -06001990 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001991}
1992
1993static void __exit
1994iscsi_tcp_exit(void)
1995{
1996 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07001997}
1998
1999module_init(iscsi_tcp_init);
2000module_exit(iscsi_tcp_exit);