blob: 464de780d95331aebb80f2a84f809fa4d8d7adb4 [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
Mike Christie63c62f12008-12-02 00:32:04 -0600489 if (conn->datadgst_en &
490 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600491 rx_hash = &tcp_conn->rx_hash;
492
Olaf Kircha8ac6312007-12-13 12:43:35 -0600493 iscsi_segment_init_linear(&tcp_conn->in.segment,
Olaf Kirchda32dd62007-12-13 12:43:21 -0600494 conn->data, tcp_conn->in.datalen,
495 iscsi_tcp_data_recv_done, rx_hash);
496}
497
Mike Christie30a6c652006-04-06 21:13:39 -0500498/*
499 * must be called with session lock
500 */
501static void
Mike Christie135a8ad2008-05-21 15:54:10 -0500502iscsi_tcp_cleanup_task(struct iscsi_conn *conn, struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -0700503{
Mike Christie135a8ad2008-05-21 15:54:10 -0500504 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500505 struct iscsi_r2t_info *r2t;
Alex Aizman7ba24712005-08-04 19:30:08 -0700506
Mike Christie135a8ad2008-05-21 15:54:10 -0500507 /* nothing to do for mgmt tasks */
508 if (!task->sc)
Mike Christiefbc514b2008-05-21 15:54:07 -0500509 return;
510
Mike Christie135a8ad2008-05-21 15:54:10 -0500511 /* flush task's r2t queues */
512 while (__kfifo_get(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
513 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christieb6c395e2006-07-24 15:47:15 -0500514 sizeof(void*));
Mike Christie135a8ad2008-05-21 15:54:10 -0500515 debug_scsi("iscsi_tcp_cleanup_task pending r2t dropped\n");
Mike Christieb6c395e2006-07-24 15:47:15 -0500516 }
517
Mike Christie135a8ad2008-05-21 15:54:10 -0500518 r2t = tcp_task->r2t;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600519 if (r2t != NULL) {
Mike Christie135a8ad2008-05-21 15:54:10 -0500520 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600521 sizeof(void*));
Mike Christie135a8ad2008-05-21 15:54:10 -0500522 tcp_task->r2t = NULL;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600523 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700524}
525
526/**
Mike Christie1d9edf02008-09-24 11:46:09 -0500527 * iscsi_data_in - SCSI Data-In Response processing
Alex Aizman7ba24712005-08-04 19:30:08 -0700528 * @conn: iscsi connection
Mike Christie135a8ad2008-05-21 15:54:10 -0500529 * @task: scsi command task
Alex Aizman7ba24712005-08-04 19:30:08 -0700530 **/
531static int
Mike Christie1d9edf02008-09-24 11:46:09 -0500532iscsi_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -0700533{
Mike Christie5bb0b552006-04-06 21:26:46 -0500534 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie135a8ad2008-05-21 15:54:10 -0500535 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500536 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700537 int datasn = be32_to_cpu(rhdr->datasn);
Mike Christie1d9edf02008-09-24 11:46:09 -0500538 unsigned total_in_length = scsi_in(task->sc)->length;
Alex Aizman7ba24712005-08-04 19:30:08 -0700539
Mike Christie1d9edf02008-09-24 11:46:09 -0500540 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
Mike Christie5bb0b552006-04-06 21:26:46 -0500541 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700542 return 0;
543
Mike Christie135a8ad2008-05-21 15:54:10 -0500544 if (tcp_task->exp_datasn != datasn) {
545 debug_tcp("%s: task->exp_datasn(%d) != rhdr->datasn(%d)\n",
Harvey Harrison3a12b192008-05-21 15:54:19 -0500546 __func__, tcp_task->exp_datasn, datasn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700547 return ISCSI_ERR_DATASN;
Mike Christied473cc72007-05-30 12:57:14 -0500548 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700549
Mike Christie135a8ad2008-05-21 15:54:10 -0500550 tcp_task->exp_datasn++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700551
Mike Christie135a8ad2008-05-21 15:54:10 -0500552 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
553 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
Mike Christie857ae0b2007-05-30 12:57:15 -0500554 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
Harvey Harrison3a12b192008-05-21 15:54:19 -0500555 __func__, tcp_task->data_offset,
Boaz Harrosh94795b62008-04-18 10:11:53 -0500556 tcp_conn->in.datalen, total_in_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700557 return ISCSI_ERR_DATA_OFFSET;
Mike Christie857ae0b2007-05-30 12:57:15 -0500558 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700559
Alex Aizman7ba24712005-08-04 19:30:08 -0700560 conn->datain_pdus_cnt++;
561 return 0;
562}
563
564/**
565 * iscsi_solicit_data_init - initialize first Data-Out
566 * @conn: iscsi connection
Mike Christie135a8ad2008-05-21 15:54:10 -0500567 * @task: scsi command task
Alex Aizman7ba24712005-08-04 19:30:08 -0700568 * @r2t: R2T info
569 *
570 * Notes:
571 * Initialize first Data-Out within this R2T sequence and finds
572 * proper data_offset within this SCSI command.
573 *
574 * This function is called with connection lock taken.
575 **/
576static void
Mike Christie135a8ad2008-05-21 15:54:10 -0500577iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_task *task,
Alex Aizman7ba24712005-08-04 19:30:08 -0700578 struct iscsi_r2t_info *r2t)
579{
580 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700581
Mike Christieffbfe922006-05-18 20:31:36 -0500582 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700583 memset(hdr, 0, sizeof(struct iscsi_data));
584 hdr->ttt = r2t->ttt;
585 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
586 r2t->solicit_datasn++;
587 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie135a8ad2008-05-21 15:54:10 -0500588 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
589 hdr->itt = task->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700590 hdr->exp_statsn = r2t->exp_statsn;
591 hdr->offset = cpu_to_be32(r2t->data_offset);
592 if (r2t->data_length > conn->max_xmit_dlength) {
593 hton24(hdr->dlength, conn->max_xmit_dlength);
594 r2t->data_count = conn->max_xmit_dlength;
595 hdr->flags = 0;
596 } else {
597 hton24(hdr->dlength, r2t->data_length);
598 r2t->data_count = r2t->data_length;
599 hdr->flags = ISCSI_FLAG_CMD_FINAL;
600 }
601 conn->dataout_pdus_cnt++;
602
603 r2t->sent = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700604}
605
606/**
607 * iscsi_r2t_rsp - iSCSI R2T Response processing
608 * @conn: iscsi connection
Mike Christie135a8ad2008-05-21 15:54:10 -0500609 * @task: scsi command task
Alex Aizman7ba24712005-08-04 19:30:08 -0700610 **/
611static int
Mike Christie135a8ad2008-05-21 15:54:10 -0500612iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -0700613{
614 struct iscsi_r2t_info *r2t;
615 struct iscsi_session *session = conn->session;
Mike Christie135a8ad2008-05-21 15:54:10 -0500616 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500617 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
618 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700619 int r2tsn = be32_to_cpu(rhdr->r2tsn);
620 int rc;
621
Mike Christie98a94162006-08-31 18:09:26 -0400622 if (tcp_conn->in.datalen) {
Mike Christie322d7392008-01-31 13:36:52 -0600623 iscsi_conn_printk(KERN_ERR, conn,
624 "invalid R2t with datalen %d\n",
625 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700626 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400627 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700628
Mike Christie135a8ad2008-05-21 15:54:10 -0500629 if (tcp_task->exp_datasn != r2tsn){
630 debug_tcp("%s: task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
Harvey Harrison3a12b192008-05-21 15:54:19 -0500631 __func__, tcp_task->exp_datasn, r2tsn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700632 return ISCSI_ERR_R2TSN;
Mike Christied473cc72007-05-30 12:57:14 -0500633 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700634
Mike Christie135a8ad2008-05-21 15:54:10 -0500635 /* fill-in new R2T associated with the task */
Mike Christie77a23c22007-05-30 12:57:18 -0500636 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
637
Mike Christie135a8ad2008-05-21 15:54:10 -0500638 if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie322d7392008-01-31 13:36:52 -0600639 iscsi_conn_printk(KERN_INFO, conn,
640 "dropping R2T itt %d in recovery.\n",
Mike Christie135a8ad2008-05-21 15:54:10 -0500641 task->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -0700642 return 0;
643 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500644
Mike Christie135a8ad2008-05-21 15:54:10 -0500645 rc = __kfifo_get(tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700646 BUG_ON(!rc);
647
648 r2t->exp_statsn = rhdr->statsn;
649 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400650 if (r2t->data_length == 0) {
Mike Christie322d7392008-01-31 13:36:52 -0600651 iscsi_conn_printk(KERN_ERR, conn,
652 "invalid R2T with zero data len\n");
Mike Christie135a8ad2008-05-21 15:54:10 -0500653 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
Olaf Kirch03766a12007-12-13 12:43:36 -0600654 sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700655 return ISCSI_ERR_DATALEN;
656 }
657
Mike Christie98a94162006-08-31 18:09:26 -0400658 if (r2t->data_length > session->max_burst)
659 debug_scsi("invalid R2T with data len %u and max burst %u."
660 "Attempting to execute request.\n",
661 r2t->data_length, session->max_burst);
662
Alex Aizman7ba24712005-08-04 19:30:08 -0700663 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
Mike Christie135a8ad2008-05-21 15:54:10 -0500664 if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) {
Mike Christie322d7392008-01-31 13:36:52 -0600665 iscsi_conn_printk(KERN_ERR, conn,
666 "invalid R2T with data len %u at offset %u "
667 "and total length %d\n", r2t->data_length,
Mike Christie135a8ad2008-05-21 15:54:10 -0500668 r2t->data_offset, scsi_out(task->sc)->length);
669 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
Olaf Kirch03766a12007-12-13 12:43:36 -0600670 sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700671 return ISCSI_ERR_DATALEN;
672 }
673
674 r2t->ttt = rhdr->ttt; /* no flip */
675 r2t->solicit_datasn = 0;
676
Mike Christie135a8ad2008-05-21 15:54:10 -0500677 iscsi_solicit_data_init(conn, task, r2t);
Alex Aizman7ba24712005-08-04 19:30:08 -0700678
Mike Christie135a8ad2008-05-21 15:54:10 -0500679 tcp_task->exp_datasn = r2tsn + 1;
680 __kfifo_put(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700681 conn->r2t_pdus_cnt++;
Mike Christie843c0a82007-12-13 12:43:20 -0600682
Mike Christie135a8ad2008-05-21 15:54:10 -0500683 iscsi_requeue_task(task);
Alex Aizman7ba24712005-08-04 19:30:08 -0700684 return 0;
685}
686
Olaf Kirchda32dd62007-12-13 12:43:21 -0600687/*
688 * Handle incoming reply to DataIn command
689 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700690static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600691iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600692 struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600693{
694 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
695 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
696 int rc;
697
Olaf Kircha8ac6312007-12-13 12:43:35 -0600698 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600699 return ISCSI_ERR_DATA_DGST;
700
701 /* check for non-exceptional status */
702 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
703 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
704 if (rc)
705 return rc;
706 }
707
708 iscsi_tcp_hdr_recv_prep(tcp_conn);
709 return 0;
710}
711
712/**
713 * iscsi_tcp_hdr_dissect - process PDU header
714 * @conn: iSCSI connection
715 * @hdr: PDU header
716 *
717 * This function analyzes the header of the PDU received,
718 * and performs several sanity checks. If the PDU is accompanied
719 * by data, the receive buffer is set up to copy the incoming data
720 * to the correct location.
721 */
722static int
723iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
Alex Aizman7ba24712005-08-04 19:30:08 -0700724{
Mike Christie5bb0b552006-04-06 21:26:46 -0500725 int rc = 0, opcode, ahslen;
Mike Christie5bb0b552006-04-06 21:26:46 -0500726 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie135a8ad2008-05-21 15:54:10 -0500727 struct iscsi_task *task;
Alex Aizman7ba24712005-08-04 19:30:08 -0700728
729 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500730 tcp_conn->in.datalen = ntoh24(hdr->dlength);
731 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Mike Christie322d7392008-01-31 13:36:52 -0600732 iscsi_conn_printk(KERN_ERR, conn,
733 "iscsi_tcp: datalen %d > %d\n",
734 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700735 return ISCSI_ERR_DATALEN;
736 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700737
Olaf Kirchda32dd62007-12-13 12:43:21 -0600738 /* Additional header segments. So far, we don't
739 * process additional headers.
740 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500741 ahslen = hdr->hlength << 2;
Alex Aizman7ba24712005-08-04 19:30:08 -0700742
Mike Christie5bb0b552006-04-06 21:26:46 -0500743 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700744 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie0af967f2008-05-21 15:54:04 -0500745 rc = iscsi_verify_itt(conn, hdr->itt);
Mike Christie7a53dc52007-12-13 12:43:37 -0600746 if (rc)
Mike Christie5bb0b552006-04-06 21:26:46 -0500747 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700748
Olaf Kirchda32dd62007-12-13 12:43:21 -0600749 debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
750 opcode, ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700751
Mike Christie5bb0b552006-04-06 21:26:46 -0500752 switch(opcode) {
753 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie913e5bf2008-05-21 15:54:18 -0500754 spin_lock(&conn->session->lock);
Mike Christie135a8ad2008-05-21 15:54:10 -0500755 task = iscsi_itt_to_ctask(conn, hdr->itt);
756 if (!task)
Mike Christie913e5bf2008-05-21 15:54:18 -0500757 rc = ISCSI_ERR_BAD_ITT;
758 else
Mike Christie1d9edf02008-09-24 11:46:09 -0500759 rc = iscsi_data_in(conn, task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500760 if (rc) {
761 spin_unlock(&conn->session->lock);
762 break;
763 }
Mike Christie0af967f2008-05-21 15:54:04 -0500764
Olaf Kirchda32dd62007-12-13 12:43:21 -0600765 if (tcp_conn->in.datalen) {
Mike Christie135a8ad2008-05-21 15:54:10 -0500766 struct iscsi_tcp_task *tcp_task = task->dd_data;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600767 struct hash_desc *rx_hash = NULL;
Mike Christie135a8ad2008-05-21 15:54:10 -0500768 struct scsi_data_buffer *sdb = scsi_in(task->sc);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600769
770 /*
771 * Setup copy of Data-In into the Scsi_Cmnd
772 * Scatterlist case:
Olaf Kircha8ac6312007-12-13 12:43:35 -0600773 * We set up the iscsi_segment to point to the next
Olaf Kirchda32dd62007-12-13 12:43:21 -0600774 * scatterlist entry to copy to. As we go along,
775 * we move on to the next scatterlist entry and
776 * update the digest per-entry.
777 */
Mike Christie63c62f12008-12-02 00:32:04 -0600778 if (conn->datadgst_en &&
779 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600780 rx_hash = &tcp_conn->rx_hash;
781
782 debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
783 "datalen=%d)\n", tcp_conn,
Mike Christie135a8ad2008-05-21 15:54:10 -0500784 tcp_task->data_offset,
Olaf Kirchda32dd62007-12-13 12:43:21 -0600785 tcp_conn->in.datalen);
Mike Christie913e5bf2008-05-21 15:54:18 -0500786 rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
787 sdb->table.sgl,
788 sdb->table.nents,
789 tcp_task->data_offset,
790 tcp_conn->in.datalen,
791 iscsi_tcp_process_data_in,
792 rx_hash);
793 spin_unlock(&conn->session->lock);
794 return rc;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600795 }
Mike Christie913e5bf2008-05-21 15:54:18 -0500796 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
797 spin_unlock(&conn->session->lock);
798 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500799 case ISCSI_OP_SCSI_CMD_RSP:
Olaf Kirchda32dd62007-12-13 12:43:21 -0600800 if (tcp_conn->in.datalen) {
801 iscsi_tcp_data_recv_prep(tcp_conn);
802 return 0;
803 }
804 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500805 break;
806 case ISCSI_OP_R2T:
Mike Christie913e5bf2008-05-21 15:54:18 -0500807 spin_lock(&conn->session->lock);
Mike Christie135a8ad2008-05-21 15:54:10 -0500808 task = iscsi_itt_to_ctask(conn, hdr->itt);
809 if (!task)
Mike Christie913e5bf2008-05-21 15:54:18 -0500810 rc = ISCSI_ERR_BAD_ITT;
811 else if (ahslen)
Mike Christie5bb0b552006-04-06 21:26:46 -0500812 rc = ISCSI_ERR_AHSLEN;
Mike Christie913e5bf2008-05-21 15:54:18 -0500813 else if (task->sc->sc_data_direction == DMA_TO_DEVICE)
Mike Christie135a8ad2008-05-21 15:54:10 -0500814 rc = iscsi_r2t_rsp(conn, task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500815 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500816 rc = ISCSI_ERR_PROTO;
Mike Christie913e5bf2008-05-21 15:54:18 -0500817 spin_unlock(&conn->session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500818 break;
819 case ISCSI_OP_LOGIN_RSP:
820 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500821 case ISCSI_OP_REJECT:
822 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500823 /*
824 * It is possible that we could get a PDU with a buffer larger
825 * than 8K, but there are no targets that currently do this.
826 * For now we fail until we find a vendor that needs it
827 */
Olaf Kirchda32dd62007-12-13 12:43:21 -0600828 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
Mike Christie322d7392008-01-31 13:36:52 -0600829 iscsi_conn_printk(KERN_ERR, conn,
830 "iscsi_tcp: received buffer of "
831 "len %u but conn buffer is only %u "
832 "(opcode %0x)\n",
833 tcp_conn->in.datalen,
834 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
Mike Christiec8dc1e52006-07-24 15:47:39 -0500835 rc = ISCSI_ERR_PROTO;
836 break;
837 }
838
Olaf Kirchda32dd62007-12-13 12:43:21 -0600839 /* If there's data coming in with the response,
840 * receive it to the connection's buffer.
841 */
842 if (tcp_conn->in.datalen) {
843 iscsi_tcp_data_recv_prep(tcp_conn);
844 return 0;
845 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500846 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500847 case ISCSI_OP_LOGOUT_RSP:
848 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500849 case ISCSI_OP_SCSI_TMFUNC_RSP:
850 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
851 break;
852 default:
853 rc = ISCSI_ERR_BAD_OPCODE;
854 break;
855 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700856
Olaf Kirchda32dd62007-12-13 12:43:21 -0600857 if (rc == 0) {
858 /* Anything that comes with data should have
859 * been handled above. */
860 if (tcp_conn->in.datalen)
861 return ISCSI_ERR_PROTO;
862 iscsi_tcp_hdr_recv_prep(tcp_conn);
863 }
864
Alex Aizman7ba24712005-08-04 19:30:08 -0700865 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700866}
867
Olaf Kirchda32dd62007-12-13 12:43:21 -0600868/**
869 * iscsi_tcp_hdr_recv_done - process PDU header
870 *
871 * This is the callback invoked when the PDU header has
872 * been received. If the header is followed by additional
873 * header segments, we go back for more data.
874 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700875static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600876iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600877 struct iscsi_segment *segment)
Alex Aizman7ba24712005-08-04 19:30:08 -0700878{
Olaf Kirchda32dd62007-12-13 12:43:21 -0600879 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
880 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700881
Olaf Kirchda32dd62007-12-13 12:43:21 -0600882 /* Check if there are additional header segments
883 * *prior* to computing the digest, because we
884 * may need to go back to the caller for more.
885 */
886 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600887 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600888 /* Bump the header length - the caller will
889 * just loop around and get the AHS for us, and
890 * call again. */
891 unsigned int ahslen = hdr->hlength << 2;
Alex Aizman7ba24712005-08-04 19:30:08 -0700892
Olaf Kirchda32dd62007-12-13 12:43:21 -0600893 /* Make sure we don't overflow */
894 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
895 return ISCSI_ERR_AHSLEN;
896
Olaf Kircha8ac6312007-12-13 12:43:35 -0600897 segment->total_size += ahslen;
898 segment->size += ahslen;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600899 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700900 }
Olaf Kirchda32dd62007-12-13 12:43:21 -0600901
902 /* We're done processing the header. See if we're doing
903 * header digests; if so, set up the recv_digest buffer
904 * and go back for more. */
905 if (conn->hdrdgst_en) {
Olaf Kircha8ac6312007-12-13 12:43:35 -0600906 if (segment->digest_len == 0) {
Mike Christie63c62f12008-12-02 00:32:04 -0600907 /*
908 * Even if we offload the digest processing we
909 * splice it in so we can increment the skb/segment
910 * counters in preparation for the data segment.
911 */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600912 iscsi_tcp_segment_splice_digest(segment,
913 segment->recv_digest);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600914 return 0;
915 }
Olaf Kirchda32dd62007-12-13 12:43:21 -0600916
Mike Christie63c62f12008-12-02 00:32:04 -0600917 if (!(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
918 iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
919 segment->total_copied - ISCSI_DIGEST_SIZE,
920 segment->digest);
921
922 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
923 return ISCSI_ERR_HDR_DGST;
924 }
Olaf Kirchda32dd62007-12-13 12:43:21 -0600925 }
926
927 tcp_conn->in.hdr = hdr;
928 return iscsi_tcp_hdr_dissect(conn, hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -0700929}
930
Mike Christie63c62f12008-12-02 00:32:04 -0600931inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
932{
933 return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
934}
935
936enum {
937 ISCSI_TCP_SEGMENT_DONE, /* curr seg has been processed */
938 ISCSI_TCP_SKB_DONE, /* skb is out of data */
939 ISCSI_TCP_CONN_ERR, /* iscsi layer has fired a conn err */
940 ISCSI_TCP_SUSPENDED, /* conn is suspended */
941};
942
943/**
944 * iscsi_tcp_recv_skb - Process skb
945 * @conn: iscsi connection
946 * @skb: network buffer with header and/or data segment
947 * @offset: offset in skb
948 * @offload: bool indicating if transfer was offloaded
949 */
950int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
951 unsigned int offset, bool offloaded, int *status)
952{
953 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
954 struct iscsi_segment *segment = &tcp_conn->in.segment;
955 struct skb_seq_state seq;
956 unsigned int consumed = 0;
957 int rc = 0;
958
959 debug_tcp("in %d bytes\n", skb->len - offset);
960
961 if (unlikely(conn->suspend_rx)) {
962 debug_tcp("conn %d Rx suspended!\n", conn->id);
963 *status = ISCSI_TCP_SUSPENDED;
964 return 0;
965 }
966
967 if (offloaded) {
968 segment->total_copied = segment->total_size;
969 goto segment_done;
970 }
971
972 skb_prepare_seq_read(skb, offset, skb->len, &seq);
973 while (1) {
974 unsigned int avail;
975 const u8 *ptr;
976
977 avail = skb_seq_read(consumed, &ptr, &seq);
978 if (avail == 0) {
979 debug_tcp("no more data avail. Consumed %d\n",
980 consumed);
981 *status = ISCSI_TCP_SKB_DONE;
982 skb_abort_seq_read(&seq);
983 goto skb_done;
984 }
985 BUG_ON(segment->copied >= segment->size);
986
987 debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
988 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
989 BUG_ON(rc == 0);
990 consumed += rc;
991
992 if (segment->total_copied >= segment->total_size) {
993 skb_abort_seq_read(&seq);
994 goto segment_done;
995 }
996 }
997
998segment_done:
999 *status = ISCSI_TCP_SEGMENT_DONE;
1000 debug_tcp("segment done\n");
1001 rc = segment->done(tcp_conn, segment);
1002 if (rc != 0) {
1003 *status = ISCSI_TCP_CONN_ERR;
1004 debug_tcp("Error receiving PDU, errno=%d\n", rc);
1005 iscsi_conn_failure(conn, rc);
1006 return 0;
1007 }
1008 /* The done() functions sets up the next segment. */
1009
1010skb_done:
1011 conn->rxdata_octets += consumed;
1012 return consumed;
1013}
1014EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
1015
Alex Aizman7ba24712005-08-04 19:30:08 -07001016/**
Olaf Kirchda32dd62007-12-13 12:43:21 -06001017 * iscsi_tcp_recv - TCP receive in sendfile fashion
Alex Aizman7ba24712005-08-04 19:30:08 -07001018 * @rd_desc: read descriptor
1019 * @skb: socket buffer
1020 * @offset: offset in skb
1021 * @len: skb->len - offset
1022 **/
1023static int
Olaf Kirchda32dd62007-12-13 12:43:21 -06001024iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
1025 unsigned int offset, size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001026{
Alex Aizman7ba24712005-08-04 19:30:08 -07001027 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie63c62f12008-12-02 00:32:04 -06001028 unsigned int consumed, total_consumed = 0;
1029 int status;
Alex Aizman7ba24712005-08-04 19:30:08 -07001030
Olaf Kirchda32dd62007-12-13 12:43:21 -06001031 debug_tcp("in %d bytes\n", skb->len - offset);
Alex Aizman7ba24712005-08-04 19:30:08 -07001032
Mike Christie63c62f12008-12-02 00:32:04 -06001033 do {
1034 status = 0;
1035 consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
1036 offset += consumed;
1037 total_consumed += consumed;
1038 } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
Alex Aizman7ba24712005-08-04 19:30:08 -07001039
Mike Christie63c62f12008-12-02 00:32:04 -06001040 debug_tcp("read %d bytes status %d\n", skb->len - offset, status);
1041 return total_consumed;
Alex Aizman7ba24712005-08-04 19:30:08 -07001042}
1043
1044static void
1045iscsi_tcp_data_ready(struct sock *sk, int flag)
1046{
1047 struct iscsi_conn *conn = sk->sk_user_data;
Olaf Kirchda32dd62007-12-13 12:43:21 -06001048 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001049 read_descriptor_t rd_desc;
1050
1051 read_lock(&sk->sk_callback_lock);
1052
Mike Christie665b44a2006-05-02 19:46:49 -05001053 /*
Olaf Kirchda32dd62007-12-13 12:43:21 -06001054 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
Mike Christie665b44a2006-05-02 19:46:49 -05001055 * We set count to 1 because we want the network layer to
Olaf Kirchda32dd62007-12-13 12:43:21 -06001056 * hand us all the skbs that are available. iscsi_tcp_recv
Mike Christie665b44a2006-05-02 19:46:49 -05001057 * handled pdus that cross buffers or pdus that still need data.
1058 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001059 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -05001060 rd_desc.count = 1;
Olaf Kirchda32dd62007-12-13 12:43:21 -06001061 tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
Alex Aizman7ba24712005-08-04 19:30:08 -07001062
1063 read_unlock(&sk->sk_callback_lock);
Olaf Kirchda32dd62007-12-13 12:43:21 -06001064
1065 /* If we had to (atomically) map a highmem page,
1066 * unmap it now. */
Olaf Kircha8ac6312007-12-13 12:43:35 -06001067 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
Alex Aizman7ba24712005-08-04 19:30:08 -07001068}
1069
1070static void
1071iscsi_tcp_state_change(struct sock *sk)
1072{
Mike Christie5bb0b552006-04-06 21:26:46 -05001073 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001074 struct iscsi_conn *conn;
1075 struct iscsi_session *session;
1076 void (*old_state_change)(struct sock *);
1077
1078 read_lock(&sk->sk_callback_lock);
1079
1080 conn = (struct iscsi_conn*)sk->sk_user_data;
1081 session = conn->session;
1082
Mike Christiee6273992005-11-29 23:12:49 -06001083 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1084 sk->sk_state == TCP_CLOSE) &&
1085 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001086 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1087 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1088 }
1089
Mike Christie5bb0b552006-04-06 21:26:46 -05001090 tcp_conn = conn->dd_data;
1091 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001092
1093 read_unlock(&sk->sk_callback_lock);
1094
1095 old_state_change(sk);
1096}
1097
1098/**
1099 * iscsi_write_space - Called when more output buffer space is available
1100 * @sk: socket space is available for
1101 **/
1102static void
1103iscsi_write_space(struct sock *sk)
1104{
1105 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001106 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1107
1108 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001109 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001110 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001111}
1112
1113static void
1114iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1115{
Mike Christie5bb0b552006-04-06 21:26:46 -05001116 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1117 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001118
1119 /* assign new callbacks */
1120 write_lock_bh(&sk->sk_callback_lock);
1121 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001122 tcp_conn->old_data_ready = sk->sk_data_ready;
1123 tcp_conn->old_state_change = sk->sk_state_change;
1124 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001125 sk->sk_data_ready = iscsi_tcp_data_ready;
1126 sk->sk_state_change = iscsi_tcp_state_change;
1127 sk->sk_write_space = iscsi_write_space;
1128 write_unlock_bh(&sk->sk_callback_lock);
1129}
1130
1131static void
Mike Christie1c834692006-07-24 15:47:26 -05001132iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001133{
Mike Christie5bb0b552006-04-06 21:26:46 -05001134 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001135
1136 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1137 write_lock_bh(&sk->sk_callback_lock);
1138 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001139 sk->sk_data_ready = tcp_conn->old_data_ready;
1140 sk->sk_state_change = tcp_conn->old_state_change;
1141 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001142 sk->sk_no_check = 0;
1143 write_unlock_bh(&sk->sk_callback_lock);
1144}
1145
1146/**
Olaf Kircha8ac6312007-12-13 12:43:35 -06001147 * iscsi_xmit - TCP transmit
1148 **/
1149static int
1150iscsi_xmit(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001151{
Mike Christie5bb0b552006-04-06 21:26:46 -05001152 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001153 struct iscsi_segment *segment = &tcp_conn->out.segment;
1154 unsigned int consumed = 0;
1155 int rc = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001156
Olaf Kircha8ac6312007-12-13 12:43:35 -06001157 while (1) {
1158 rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
Mike Christie6f481e32008-09-24 11:46:13 -05001159 if (rc < 0) {
1160 rc = ISCSI_ERR_XMIT_FAILED;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001161 goto error;
Mike Christie6f481e32008-09-24 11:46:13 -05001162 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001163 if (rc == 0)
1164 break;
1165
1166 consumed += rc;
1167
1168 if (segment->total_copied >= segment->total_size) {
1169 if (segment->done != NULL) {
1170 rc = segment->done(tcp_conn, segment);
Mike Christie6f481e32008-09-24 11:46:13 -05001171 if (rc != 0)
Olaf Kircha8ac6312007-12-13 12:43:35 -06001172 goto error;
1173 }
1174 }
1175 }
1176
1177 debug_tcp("xmit %d bytes\n", consumed);
1178
1179 conn->txdata_octets += consumed;
1180 return consumed;
1181
1182error:
1183 /* Transmit error. We could initiate error recovery
1184 * here. */
1185 debug_tcp("Error sending PDU, errno=%d\n", rc);
Mike Christie6f481e32008-09-24 11:46:13 -05001186 iscsi_conn_failure(conn, rc);
1187 return -EIO;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001188}
1189
1190/**
1191 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
1192 */
1193static inline int
1194iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
1195{
1196 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1197 struct iscsi_segment *segment = &tcp_conn->out.segment;
1198
1199 return segment->total_copied - segment->total_size;
1200}
1201
1202static inline int
1203iscsi_tcp_flush(struct iscsi_conn *conn)
1204{
1205 int rc;
1206
1207 while (iscsi_tcp_xmit_qlen(conn)) {
1208 rc = iscsi_xmit(conn);
1209 if (rc == 0)
1210 return -EAGAIN;
1211 if (rc < 0)
1212 return rc;
1213 }
1214
1215 return 0;
1216}
1217
1218/*
1219 * This is called when we're done sending the header.
1220 * Simply copy the data_segment to the send segment, and return.
1221 */
1222static int
1223iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
1224 struct iscsi_segment *segment)
1225{
1226 tcp_conn->out.segment = tcp_conn->out.data_segment;
1227 debug_tcp("Header done. Next segment size %u total_size %u\n",
1228 tcp_conn->out.segment.size, tcp_conn->out.segment.total_size);
1229 return 0;
1230}
1231
1232static void
1233iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen)
1234{
1235 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1236
Harvey Harrison3a12b192008-05-21 15:54:19 -05001237 debug_tcp("%s(%p%s)\n", __func__, tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001238 conn->hdrdgst_en? ", digest enabled" : "");
1239
1240 /* Clear the data segment - needs to be filled in by the
1241 * caller using iscsi_tcp_send_data_prep() */
1242 memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment));
1243
1244 /* If header digest is enabled, compute the CRC and
1245 * place the digest into the same buffer. We make
Mike Christie135a8ad2008-05-21 15:54:10 -05001246 * sure that both iscsi_tcp_task and mtask have
Olaf Kircha8ac6312007-12-13 12:43:35 -06001247 * sufficient room.
Mike Christie7cae5152006-01-13 18:05:47 -06001248 */
Olaf Kircha8ac6312007-12-13 12:43:35 -06001249 if (conn->hdrdgst_en) {
1250 iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
1251 hdr + hdrlen);
1252 hdrlen += ISCSI_DIGEST_SIZE;
Mike Christie3219e522006-05-30 00:37:28 -05001253 }
1254
Olaf Kircha8ac6312007-12-13 12:43:35 -06001255 /* Remember header pointer for later, when we need
1256 * to decide whether there's a payload to go along
1257 * with the header. */
1258 tcp_conn->out.hdr = hdr;
1259
1260 iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen,
1261 iscsi_tcp_send_hdr_done, NULL);
Alex Aizman7ba24712005-08-04 19:30:08 -07001262}
1263
Olaf Kircha8ac6312007-12-13 12:43:35 -06001264/*
1265 * Prepare the send buffer for the payload data.
1266 * Padding and checksumming will all be taken care
1267 * of by the iscsi_segment routines.
1268 */
1269static int
1270iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
1271 unsigned int count, unsigned int offset,
1272 unsigned int len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001273{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001274 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1275 struct hash_desc *tx_hash = NULL;
1276 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -07001277
Harvey Harrison3a12b192008-05-21 15:54:19 -05001278 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __func__,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001279 tcp_conn, offset, len,
1280 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -07001281
Olaf Kircha8ac6312007-12-13 12:43:35 -06001282 /* Make sure the datalen matches what the caller
1283 said he would send. */
1284 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1285 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -07001286
Olaf Kircha8ac6312007-12-13 12:43:35 -06001287 if (conn->datadgst_en)
1288 tx_hash = &tcp_conn->tx_hash;
1289
1290 return iscsi_segment_seek_sg(&tcp_conn->out.data_segment,
1291 sg, count, offset, len,
1292 NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -07001293}
1294
Olaf Kircha8ac6312007-12-13 12:43:35 -06001295static void
1296iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
1297 size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001298{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001299 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1300 struct hash_desc *tx_hash = NULL;
1301 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -07001302
Harvey Harrison3a12b192008-05-21 15:54:19 -05001303 debug_tcp("%s(%p, datalen=%d%s)\n", __func__, tcp_conn, len,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001304 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -07001305
Olaf Kircha8ac6312007-12-13 12:43:35 -06001306 /* Make sure the datalen matches what the caller
1307 said he would send. */
1308 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1309 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -07001310
Olaf Kircha8ac6312007-12-13 12:43:35 -06001311 if (conn->datadgst_en)
1312 tx_hash = &tcp_conn->tx_hash;
Alex Aizman7ba24712005-08-04 19:30:08 -07001313
Olaf Kircha8ac6312007-12-13 12:43:35 -06001314 iscsi_segment_init_linear(&tcp_conn->out.data_segment,
1315 data, len, NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -07001316}
1317
Alex Aizman7ba24712005-08-04 19:30:08 -07001318/**
1319 * iscsi_solicit_data_cont - initialize next Data-Out
1320 * @conn: iscsi connection
Mike Christie135a8ad2008-05-21 15:54:10 -05001321 * @task: scsi command task
Alex Aizman7ba24712005-08-04 19:30:08 -07001322 * @r2t: R2T info
1323 * @left: bytes left to transfer
1324 *
1325 * Notes:
1326 * Initialize next Data-Out within this R2T sequence and continue
1327 * to process next Scatter-Gather element(if any) of this SCSI command.
1328 *
1329 * Called under connection lock.
1330 **/
Olaf Kircha8ac6312007-12-13 12:43:35 -06001331static int
Mike Christie135a8ad2008-05-21 15:54:10 -05001332iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_task *task,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001333 struct iscsi_r2t_info *r2t)
Alex Aizman7ba24712005-08-04 19:30:08 -07001334{
1335 struct iscsi_data *hdr;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001336 int new_offset, left;
1337
1338 BUG_ON(r2t->data_length - r2t->sent < 0);
1339 left = r2t->data_length - r2t->sent;
1340 if (left == 0)
1341 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001342
Mike Christieffbfe922006-05-18 20:31:36 -05001343 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001344 memset(hdr, 0, sizeof(struct iscsi_data));
1345 hdr->ttt = r2t->ttt;
1346 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1347 r2t->solicit_datasn++;
1348 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie135a8ad2008-05-21 15:54:10 -05001349 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
1350 hdr->itt = task->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001351 hdr->exp_statsn = r2t->exp_statsn;
1352 new_offset = r2t->data_offset + r2t->sent;
1353 hdr->offset = cpu_to_be32(new_offset);
1354 if (left > conn->max_xmit_dlength) {
1355 hton24(hdr->dlength, conn->max_xmit_dlength);
1356 r2t->data_count = conn->max_xmit_dlength;
1357 } else {
1358 hton24(hdr->dlength, left);
1359 r2t->data_count = left;
1360 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1361 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001362
Alex Aizman7ba24712005-08-04 19:30:08 -07001363 conn->dataout_pdus_cnt++;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001364 return 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001365}
1366
1367/**
Mike Christiefbc514b2008-05-21 15:54:07 -05001368 * iscsi_tcp_task - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001369 * @conn: iscsi connection
Mike Christie135a8ad2008-05-21 15:54:10 -05001370 * @task: scsi command task
Alex Aizman7ba24712005-08-04 19:30:08 -07001371 * @sc: scsi command
1372 **/
Olaf Kircha8ac6312007-12-13 12:43:35 -06001373static int
Mike Christie135a8ad2008-05-21 15:54:10 -05001374iscsi_tcp_task_init(struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -07001375{
Mike Christie135a8ad2008-05-21 15:54:10 -05001376 struct iscsi_tcp_task *tcp_task = task->dd_data;
1377 struct iscsi_conn *conn = task->conn;
1378 struct scsi_cmnd *sc = task->sc;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001379 int err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001380
Mike Christiefbc514b2008-05-21 15:54:07 -05001381 if (!sc) {
1382 /*
Mike Christie135a8ad2008-05-21 15:54:10 -05001383 * mgmt tasks do not have a scatterlist since they come
Mike Christiefbc514b2008-05-21 15:54:07 -05001384 * in from the iscsi interface.
1385 */
Mike Christie135a8ad2008-05-21 15:54:10 -05001386 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id,
1387 task->itt);
Mike Christiefbc514b2008-05-21 15:54:07 -05001388
1389 /* Prepare PDU, optionally w/ immediate data */
Mike Christie135a8ad2008-05-21 15:54:10 -05001390 iscsi_tcp_send_hdr_prep(conn, task->hdr, sizeof(*task->hdr));
Mike Christiefbc514b2008-05-21 15:54:07 -05001391
1392 /* If we have immediate data, attach a payload */
Mike Christie135a8ad2008-05-21 15:54:10 -05001393 if (task->data_count)
1394 iscsi_tcp_send_linear_data_prepare(conn, task->data,
1395 task->data_count);
Mike Christiefbc514b2008-05-21 15:54:07 -05001396 return 0;
1397 }
1398
Mike Christie135a8ad2008-05-21 15:54:10 -05001399 BUG_ON(__kfifo_len(tcp_task->r2tqueue));
1400 tcp_task->sent = 0;
1401 tcp_task->exp_datasn = 0;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001402
1403 /* Prepare PDU, optionally w/ immediate data */
Mike Christie135a8ad2008-05-21 15:54:10 -05001404 debug_scsi("task deq [cid %d itt 0x%x imm %d unsol %d]\n",
1405 conn->id, task->itt, task->imm_count,
1406 task->unsol_count);
1407 iscsi_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
Olaf Kircha8ac6312007-12-13 12:43:35 -06001408
Mike Christie135a8ad2008-05-21 15:54:10 -05001409 if (!task->imm_count)
Olaf Kircha8ac6312007-12-13 12:43:35 -06001410 return 0;
1411
1412 /* If we have immediate data, attach a payload */
Boaz Harrosh94795b62008-04-18 10:11:53 -05001413 err = iscsi_tcp_send_data_prep(conn, scsi_out(sc)->table.sgl,
1414 scsi_out(sc)->table.nents,
Mike Christie135a8ad2008-05-21 15:54:10 -05001415 0, task->imm_count);
Olaf Kircha8ac6312007-12-13 12:43:35 -06001416 if (err)
1417 return err;
Mike Christie135a8ad2008-05-21 15:54:10 -05001418 tcp_task->sent += task->imm_count;
1419 task->imm_count = 0;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001420 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001421}
1422
Olaf Kircha8ac6312007-12-13 12:43:35 -06001423/*
Mike Christie135a8ad2008-05-21 15:54:10 -05001424 * iscsi_tcp_task_xmit - xmit normal PDU task
1425 * @task: iscsi command task
Mike Christie218432c2007-05-30 12:57:17 -05001426 *
Olaf Kircha8ac6312007-12-13 12:43:35 -06001427 * We're expected to return 0 when everything was transmitted succesfully,
1428 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1429 * of error.
1430 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001431static int
Mike Christie135a8ad2008-05-21 15:54:10 -05001432iscsi_tcp_task_xmit(struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -07001433{
Mike Christie135a8ad2008-05-21 15:54:10 -05001434 struct iscsi_conn *conn = task->conn;
1435 struct iscsi_tcp_task *tcp_task = task->dd_data;
1436 struct scsi_cmnd *sc = task->sc;
Mike Christiefbc514b2008-05-21 15:54:07 -05001437 struct scsi_data_buffer *sdb;
Alex Aizman7ba24712005-08-04 19:30:08 -07001438 int rc = 0;
1439
Olaf Kircha8ac6312007-12-13 12:43:35 -06001440flush:
1441 /* Flush any pending data first. */
1442 rc = iscsi_tcp_flush(conn);
1443 if (rc < 0)
Mike Christie218432c2007-05-30 12:57:17 -05001444 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001445
Mike Christiefbc514b2008-05-21 15:54:07 -05001446 /* mgmt command */
1447 if (!sc) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001448 if (task->hdr->itt == RESERVED_ITT)
1449 iscsi_put_task(task);
Mike Christiefbc514b2008-05-21 15:54:07 -05001450 return 0;
1451 }
1452
Olaf Kircha8ac6312007-12-13 12:43:35 -06001453 /* Are we done already? */
1454 if (sc->sc_data_direction != DMA_TO_DEVICE)
Mike Christie218432c2007-05-30 12:57:17 -05001455 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001456
Mike Christiefbc514b2008-05-21 15:54:07 -05001457 sdb = scsi_out(sc);
Mike Christie135a8ad2008-05-21 15:54:10 -05001458 if (task->unsol_count != 0) {
1459 struct iscsi_data *hdr = &tcp_task->unsol_dtask.hdr;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001460
1461 /* Prepare a header for the unsolicited PDU.
1462 * The amount of data we want to send will be
Mike Christie135a8ad2008-05-21 15:54:10 -05001463 * in task->data_count.
Olaf Kircha8ac6312007-12-13 12:43:35 -06001464 * FIXME: return the data count instead.
1465 */
Mike Christie135a8ad2008-05-21 15:54:10 -05001466 iscsi_prep_unsolicit_data_pdu(task, hdr);
Olaf Kircha8ac6312007-12-13 12:43:35 -06001467
1468 debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n",
Mike Christie135a8ad2008-05-21 15:54:10 -05001469 task->itt, tcp_task->sent, task->data_count);
Olaf Kircha8ac6312007-12-13 12:43:35 -06001470
1471 iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr));
Boaz Harrosh94795b62008-04-18 10:11:53 -05001472 rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
Mike Christie135a8ad2008-05-21 15:54:10 -05001473 sdb->table.nents, tcp_task->sent,
1474 task->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001475 if (rc)
Olaf Kircha8ac6312007-12-13 12:43:35 -06001476 goto fail;
Mike Christie135a8ad2008-05-21 15:54:10 -05001477 tcp_task->sent += task->data_count;
1478 task->unsol_count -= task->data_count;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001479 goto flush;
1480 } else {
1481 struct iscsi_session *session = conn->session;
1482 struct iscsi_r2t_info *r2t;
1483
1484 /* All unsolicited PDUs sent. Check for solicited PDUs.
1485 */
1486 spin_lock_bh(&session->lock);
Mike Christie135a8ad2008-05-21 15:54:10 -05001487 r2t = tcp_task->r2t;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001488 if (r2t != NULL) {
1489 /* Continue with this R2T? */
Mike Christie135a8ad2008-05-21 15:54:10 -05001490 if (!iscsi_solicit_data_cont(conn, task, r2t)) {
Olaf Kircha8ac6312007-12-13 12:43:35 -06001491 debug_scsi(" done with r2t %p\n", r2t);
1492
Mike Christie135a8ad2008-05-21 15:54:10 -05001493 __kfifo_put(tcp_task->r2tpool.queue,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001494 (void*)&r2t, sizeof(void*));
Mike Christie135a8ad2008-05-21 15:54:10 -05001495 tcp_task->r2t = r2t = NULL;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001496 }
1497 }
1498
1499 if (r2t == NULL) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001500 __kfifo_get(tcp_task->r2tqueue, (void*)&tcp_task->r2t,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001501 sizeof(void*));
Mike Christie135a8ad2008-05-21 15:54:10 -05001502 r2t = tcp_task->r2t;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001503 }
1504 spin_unlock_bh(&session->lock);
1505
1506 /* Waiting for more R2Ts to arrive. */
1507 if (r2t == NULL) {
1508 debug_tcp("no R2Ts yet\n");
1509 return 0;
1510 }
1511
1512 debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
Mike Christie135a8ad2008-05-21 15:54:10 -05001513 r2t, r2t->solicit_datasn - 1, task->itt,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001514 r2t->data_offset + r2t->sent, r2t->data_count);
1515
1516 iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr,
1517 sizeof(struct iscsi_hdr));
1518
Boaz Harrosh94795b62008-04-18 10:11:53 -05001519 rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
1520 sdb->table.nents,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001521 r2t->data_offset + r2t->sent,
1522 r2t->data_count);
1523 if (rc)
1524 goto fail;
Mike Christie135a8ad2008-05-21 15:54:10 -05001525 tcp_task->sent += r2t->data_count;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001526 r2t->sent += r2t->data_count;
1527 goto flush;
Alex Aizman7ba24712005-08-04 19:30:08 -07001528 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001529 return 0;
1530fail:
1531 iscsi_conn_failure(conn, rc);
1532 return -EIO;
Alex Aizman7ba24712005-08-04 19:30:08 -07001533}
1534
Mike Christie7b8631b2006-01-13 18:05:50 -06001535static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001536iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001537{
Mike Christie7b8631b2006-01-13 18:05:50 -06001538 struct iscsi_conn *conn;
1539 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001540 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001541
Mike Christie5d91e202008-05-21 15:54:01 -05001542 cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001543 if (!cls_conn)
1544 return NULL;
1545 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001546 /*
1547 * due to strange issues with iser these are not set
1548 * in iscsi_conn_setup
1549 */
Mike Christiebf32ed32007-02-28 17:32:17 -06001550 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
Alex Aizman7ba24712005-08-04 19:30:08 -07001551
Mike Christie5d91e202008-05-21 15:54:01 -05001552 tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001553 tcp_conn->iscsi_conn = conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001554
James Bottomleyc9802cd2006-09-23 15:33:43 -05001555 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1556 CRYPTO_ALG_ASYNC);
1557 tcp_conn->tx_hash.flags = 0;
Mike Christie322d7392008-01-31 13:36:52 -06001558 if (IS_ERR(tcp_conn->tx_hash.tfm))
Mike Christie5d91e202008-05-21 15:54:01 -05001559 goto free_conn;
Mike Christiedd8c0d92006-08-31 18:09:28 -04001560
James Bottomleyc9802cd2006-09-23 15:33:43 -05001561 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1562 CRYPTO_ALG_ASYNC);
1563 tcp_conn->rx_hash.flags = 0;
Mike Christie322d7392008-01-31 13:36:52 -06001564 if (IS_ERR(tcp_conn->rx_hash.tfm))
Mike Christiedd8c0d92006-08-31 18:09:28 -04001565 goto free_tx_tfm;
1566
Mike Christie7b8631b2006-01-13 18:05:50 -06001567 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001568
Mike Christiedd8c0d92006-08-31 18:09:28 -04001569free_tx_tfm:
James Bottomleyc9802cd2006-09-23 15:33:43 -05001570 crypto_free_hash(tcp_conn->tx_hash.tfm);
Mike Christie5d91e202008-05-21 15:54:01 -05001571free_conn:
Mike Christie322d7392008-01-31 13:36:52 -06001572 iscsi_conn_printk(KERN_ERR, conn,
1573 "Could not create connection due to crc32c "
1574 "loading error. Make sure the crc32c "
1575 "module is built as a module or into the "
1576 "kernel\n");
Mike Christie5bb0b552006-04-06 21:26:46 -05001577 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001578 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001579}
1580
1581static void
Mike Christie1c834692006-07-24 15:47:26 -05001582iscsi_tcp_release_conn(struct iscsi_conn *conn)
1583{
Mike Christie22236962007-05-30 12:57:24 -05001584 struct iscsi_session *session = conn->session;
Mike Christie1c834692006-07-24 15:47:26 -05001585 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie22236962007-05-30 12:57:24 -05001586 struct socket *sock = tcp_conn->sock;
Mike Christie1c834692006-07-24 15:47:26 -05001587
Mike Christie22236962007-05-30 12:57:24 -05001588 if (!sock)
Mike Christie1c834692006-07-24 15:47:26 -05001589 return;
1590
Mike Christie22236962007-05-30 12:57:24 -05001591 sock_hold(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001592 iscsi_conn_restore_callbacks(tcp_conn);
Mike Christie22236962007-05-30 12:57:24 -05001593 sock_put(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001594
Mike Christie22236962007-05-30 12:57:24 -05001595 spin_lock_bh(&session->lock);
Mike Christie1c834692006-07-24 15:47:26 -05001596 tcp_conn->sock = NULL;
Mike Christie22236962007-05-30 12:57:24 -05001597 spin_unlock_bh(&session->lock);
1598 sockfd_put(sock);
Mike Christie1c834692006-07-24 15:47:26 -05001599}
1600
1601static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001602iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001603{
Mike Christie7b8631b2006-01-13 18:05:50 -06001604 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001605 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001606
Mike Christie1c834692006-07-24 15:47:26 -05001607 iscsi_tcp_release_conn(conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001608
Pete Wyckoff534284a2006-11-08 15:58:31 -06001609 if (tcp_conn->tx_hash.tfm)
1610 crypto_free_hash(tcp_conn->tx_hash.tfm);
1611 if (tcp_conn->rx_hash.tfm)
1612 crypto_free_hash(tcp_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001613
Mike Christie5d91e202008-05-21 15:54:01 -05001614 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001615}
1616
Mike Christie1c834692006-07-24 15:47:26 -05001617static void
1618iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1619{
1620 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie913e5bf2008-05-21 15:54:18 -05001621 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1622
1623 /* userspace may have goofed up and not bound us */
1624 if (!tcp_conn->sock)
1625 return;
1626 /*
1627 * Make sure our recv side is stopped.
1628 * Older tools called conn stop before ep_disconnect
1629 * so IO could still be coming in.
1630 */
1631 write_lock_bh(&tcp_conn->sock->sk->sk_callback_lock);
1632 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1633 write_unlock_bh(&tcp_conn->sock->sk->sk_callback_lock);
Mike Christie1c834692006-07-24 15:47:26 -05001634
1635 iscsi_conn_stop(cls_conn, flag);
1636 iscsi_tcp_release_conn(conn);
1637}
1638
Mike Christie22236962007-05-30 12:57:24 -05001639static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
1640 char *buf, int *port,
1641 int (*getname)(struct socket *, struct sockaddr *,
1642 int *addrlen))
1643{
1644 struct sockaddr_storage *addr;
1645 struct sockaddr_in6 *sin6;
1646 struct sockaddr_in *sin;
1647 int rc = 0, len;
1648
Al Viroa9204872007-07-20 16:03:40 +01001649 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
Mike Christie22236962007-05-30 12:57:24 -05001650 if (!addr)
1651 return -ENOMEM;
1652
1653 if (getname(sock, (struct sockaddr *) addr, &len)) {
1654 rc = -ENODEV;
1655 goto free_addr;
1656 }
1657
1658 switch (addr->ss_family) {
1659 case AF_INET:
1660 sin = (struct sockaddr_in *)addr;
1661 spin_lock_bh(&conn->session->lock);
Harvey Harrison63779432008-10-31 00:56:00 -07001662 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
Mike Christie22236962007-05-30 12:57:24 -05001663 *port = be16_to_cpu(sin->sin_port);
1664 spin_unlock_bh(&conn->session->lock);
1665 break;
1666 case AF_INET6:
1667 sin6 = (struct sockaddr_in6 *)addr;
1668 spin_lock_bh(&conn->session->lock);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001669 sprintf(buf, "%pI6", &sin6->sin6_addr);
Mike Christie22236962007-05-30 12:57:24 -05001670 *port = be16_to_cpu(sin6->sin6_port);
1671 spin_unlock_bh(&conn->session->lock);
1672 break;
1673 }
1674free_addr:
1675 kfree(addr);
1676 return rc;
1677}
1678
Alex Aizman7ba24712005-08-04 19:30:08 -07001679static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001680iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001681 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001682 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001683{
Mike Christie75613522008-05-21 15:53:59 -05001684 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1685 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001686 struct iscsi_conn *conn = cls_conn->dd_data;
1687 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001688 struct sock *sk;
1689 struct socket *sock;
1690 int err;
1691
1692 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001693 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001694 if (!sock) {
Mike Christie322d7392008-01-31 13:36:52 -06001695 iscsi_conn_printk(KERN_ERR, conn,
1696 "sockfd_lookup failed %d\n", err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001697 return -EEXIST;
1698 }
Mike Christie22236962007-05-30 12:57:24 -05001699 /*
1700 * copy these values now because if we drop the session
1701 * userspace may still want to query the values since we will
1702 * be using them for the reconnect
1703 */
1704 err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1705 &conn->portal_port, kernel_getpeername);
1706 if (err)
1707 goto free_socket;
1708
Mike Christie75613522008-05-21 15:53:59 -05001709 err = iscsi_tcp_get_addr(conn, sock, ihost->local_address,
1710 &ihost->local_port, kernel_getsockname);
Mike Christie22236962007-05-30 12:57:24 -05001711 if (err)
1712 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001713
Mike Christie5bb0b552006-04-06 21:26:46 -05001714 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1715 if (err)
Mike Christie22236962007-05-30 12:57:24 -05001716 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001717
Mike Christie67a61112006-05-30 00:37:20 -05001718 /* bind iSCSI connection and socket */
1719 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001720
Mike Christie67a61112006-05-30 00:37:20 -05001721 /* setup Socket parameters */
1722 sk = sock->sk;
1723 sk->sk_reuse = 1;
1724 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1725 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001726
Mike Christie67a61112006-05-30 00:37:20 -05001727 iscsi_conn_set_callbacks(conn);
1728 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1729 /*
1730 * set receive state machine into initial state
1731 */
Olaf Kirchda32dd62007-12-13 12:43:21 -06001732 iscsi_tcp_hdr_recv_prep(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001733 return 0;
Mike Christie22236962007-05-30 12:57:24 -05001734
1735free_socket:
1736 sockfd_put(sock);
1737 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001738}
1739
Alex Aizman7ba24712005-08-04 19:30:08 -07001740static int
1741iscsi_r2tpool_alloc(struct iscsi_session *session)
1742{
1743 int i;
1744 int cmd_i;
1745
1746 /*
Mike Christie135a8ad2008-05-21 15:54:10 -05001747 * initialize per-task: R2T pool and xmit queue
Alex Aizman7ba24712005-08-04 19:30:08 -07001748 */
1749 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001750 struct iscsi_task *task = session->cmds[cmd_i];
1751 struct iscsi_tcp_task *tcp_task = task->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001752
1753 /*
1754 * pre-allocated x4 as much r2ts to handle race when
1755 * target acks DataOut faster than we data_xmit() queues
1756 * could replenish r2tqueue.
1757 */
1758
1759 /* R2T pool */
Mike Christie135a8ad2008-05-21 15:54:10 -05001760 if (iscsi_pool_init(&tcp_task->r2tpool, session->max_r2t * 4, NULL,
Mike Christie5bb0b552006-04-06 21:26:46 -05001761 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001762 goto r2t_alloc_fail;
1763 }
1764
1765 /* R2T xmit queue */
Mike Christie135a8ad2008-05-21 15:54:10 -05001766 tcp_task->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07001767 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie135a8ad2008-05-21 15:54:10 -05001768 if (tcp_task->r2tqueue == ERR_PTR(-ENOMEM)) {
1769 iscsi_pool_free(&tcp_task->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001770 goto r2t_alloc_fail;
1771 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001772 }
1773
1774 return 0;
1775
1776r2t_alloc_fail:
1777 for (i = 0; i < cmd_i; i++) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001778 struct iscsi_task *task = session->cmds[i];
1779 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001780
Mike Christie135a8ad2008-05-21 15:54:10 -05001781 kfifo_free(tcp_task->r2tqueue);
1782 iscsi_pool_free(&tcp_task->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001783 }
1784 return -ENOMEM;
1785}
1786
1787static void
1788iscsi_r2tpool_free(struct iscsi_session *session)
1789{
1790 int i;
1791
1792 for (i = 0; i < session->cmds_max; i++) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001793 struct iscsi_task *task = session->cmds[i];
1794 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001795
Mike Christie135a8ad2008-05-21 15:54:10 -05001796 kfifo_free(tcp_task->r2tqueue);
1797 iscsi_pool_free(&tcp_task->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001798 }
1799}
1800
Alex Aizman7ba24712005-08-04 19:30:08 -07001801static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001802iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05001803 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07001804{
Mike Christie7b7232f2006-02-01 21:06:49 -06001805 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001806 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001807 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001808 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07001809
Alex Aizman7ba24712005-08-04 19:30:08 -07001810 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001811 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001812 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001813 break;
1814 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001815 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05001816 tcp_conn->sendpage = conn->datadgst_en ?
1817 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07001818 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07001819 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001820 sscanf(buf, "%d", &value);
Mike Christiedf93ffc2007-12-13 12:43:42 -06001821 if (value <= 0 || !is_power_of_2(value))
1822 return -EINVAL;
1823 if (session->max_r2t == value)
Alex Aizman7ba24712005-08-04 19:30:08 -07001824 break;
1825 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05001826 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001827 if (iscsi_r2tpool_alloc(session))
1828 return -ENOMEM;
1829 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07001830 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001831 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001832 }
1833
1834 return 0;
1835}
1836
1837static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05001838iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
1839 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06001840{
Mike Christie7b7232f2006-02-01 21:06:49 -06001841 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001842 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06001843
1844 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05001845 case ISCSI_PARAM_CONN_PORT:
Mike Christie22236962007-05-30 12:57:24 -05001846 spin_lock_bh(&conn->session->lock);
1847 len = sprintf(buf, "%hu\n", conn->portal_port);
1848 spin_unlock_bh(&conn->session->lock);
Mike Christie8d2860b2006-05-02 19:46:47 -05001849 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001850 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie22236962007-05-30 12:57:24 -05001851 spin_lock_bh(&conn->session->lock);
1852 len = sprintf(buf, "%s\n", conn->portal_address);
1853 spin_unlock_bh(&conn->session->lock);
Mike Christiefd7255f2006-04-06 21:13:36 -05001854 break;
1855 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001856 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05001857 }
1858
1859 return len;
1860}
1861
Alex Aizman7ba24712005-08-04 19:30:08 -07001862static void
Mike Christie7b7232f2006-02-01 21:06:49 -06001863iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07001864{
Mike Christie7b7232f2006-02-01 21:06:49 -06001865 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001866 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001867
1868 stats->txdata_octets = conn->txdata_octets;
1869 stats->rxdata_octets = conn->rxdata_octets;
1870 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1871 stats->dataout_pdus = conn->dataout_pdus_cnt;
1872 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1873 stats->datain_pdus = conn->datain_pdus_cnt;
1874 stats->r2t_pdus = conn->r2t_pdus_cnt;
1875 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1876 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1877 stats->custom_length = 3;
1878 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05001879 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001880 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05001881 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001882 strcpy(stats->custom[2].desc, "eh_abort_cnt");
1883 stats->custom[2].value = conn->eh_abort_cnt;
1884}
1885
Mike Christie5bb0b552006-04-06 21:26:46 -05001886static struct iscsi_cls_session *
Mike Christie06520ed2008-05-21 15:54:15 -05001887iscsi_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
Mike Christie40753ca2008-05-21 15:53:56 -05001888 uint16_t qdepth, uint32_t initial_cmdsn,
1889 uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07001890{
Mike Christie5bb0b552006-04-06 21:26:46 -05001891 struct iscsi_cls_session *cls_session;
1892 struct iscsi_session *session;
Mike Christie06520ed2008-05-21 15:54:15 -05001893 struct Scsi_Host *shost;
Mike Christie5bb0b552006-04-06 21:26:46 -05001894 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07001895
Mike Christie06520ed2008-05-21 15:54:15 -05001896 if (ep) {
1897 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
Mike Christie5bb0b552006-04-06 21:26:46 -05001898 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05001899 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001900
Mike Christiea4804cd2008-05-21 15:54:00 -05001901 shost = iscsi_host_alloc(&iscsi_sht, 0, qdepth);
Mike Christie75613522008-05-21 15:53:59 -05001902 if (!shost)
1903 return NULL;
1904 shost->transportt = iscsi_tcp_scsi_transport;
1905 shost->max_lun = iscsi_max_lun;
1906 shost->max_id = 0;
1907 shost->max_channel = 0;
Boaz Harrosh30e9ba92008-05-26 11:31:19 +03001908 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
Mike Christie75613522008-05-21 15:53:59 -05001909
Mike Christiea4804cd2008-05-21 15:54:00 -05001910 if (iscsi_host_add(shost, NULL))
Mike Christie75613522008-05-21 15:53:59 -05001911 goto free_host;
1912 *hostno = shost->host_no;
1913
1914 cls_session = iscsi_session_setup(&iscsi_tcp_transport, shost, cmds_max,
Mike Christie135a8ad2008-05-21 15:54:10 -05001915 sizeof(struct iscsi_tcp_task),
Mike Christie79706342008-05-21 15:54:12 -05001916 initial_cmdsn, 0);
Mike Christie75613522008-05-21 15:53:59 -05001917 if (!cls_session)
1918 goto remove_host;
1919 session = cls_session->dd_data;
1920
Mike Christiefbc514b2008-05-21 15:54:07 -05001921 shost->can_queue = session->scsi_cmds_max;
Mike Christie5bb0b552006-04-06 21:26:46 -05001922 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001923 struct iscsi_task *task = session->cmds[cmd_i];
1924 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001925
Mike Christie135a8ad2008-05-21 15:54:10 -05001926 task->hdr = &tcp_task->hdr.cmd_hdr;
1927 task->hdr_max = sizeof(tcp_task->hdr) - ISCSI_DIGEST_SIZE;
Mike Christie5bb0b552006-04-06 21:26:46 -05001928 }
1929
Mike Christie75613522008-05-21 15:53:59 -05001930 if (iscsi_r2tpool_alloc(session))
1931 goto remove_session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001932 return cls_session;
1933
Mike Christie75613522008-05-21 15:53:59 -05001934remove_session:
Mike Christie5bb0b552006-04-06 21:26:46 -05001935 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -05001936remove_host:
Mike Christiea4804cd2008-05-21 15:54:00 -05001937 iscsi_host_remove(shost);
Mike Christie75613522008-05-21 15:53:59 -05001938free_host:
Mike Christiea4804cd2008-05-21 15:54:00 -05001939 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001940 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001941}
1942
Mike Christie5bb0b552006-04-06 21:26:46 -05001943static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
1944{
Mike Christie75613522008-05-21 15:53:59 -05001945 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1946
1947 iscsi_r2tpool_free(cls_session->dd_data);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001948 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -05001949
Mike Christiea4804cd2008-05-21 15:54:00 -05001950 iscsi_host_remove(shost);
1951 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001952}
1953
Mike Christied1d81c02007-05-30 12:57:21 -05001954static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
1955{
Mike Christieb6d44fe2007-07-26 12:46:47 -05001956 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
Mike Christied1d81c02007-05-30 12:57:21 -05001957 blk_queue_dma_alignment(sdev->request_queue, 0);
1958 return 0;
1959}
1960
Mike Christie5bb0b552006-04-06 21:26:46 -05001961static struct scsi_host_template iscsi_sht = {
Mike Christie79743922007-07-26 12:46:46 -05001962 .module = THIS_MODULE,
Mike Christief4246b32006-07-24 15:47:54 -05001963 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05001964 .queuecommand = iscsi_queuecommand,
1965 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -05001966 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie66bbe0c2007-12-13 12:43:39 -06001967 .sg_tablesize = 4096,
Mike Christie8231f0e2007-02-28 17:32:20 -06001968 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -05001969 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
1970 .eh_abort_handler = iscsi_eh_abort,
Mike Christie843c0a82007-12-13 12:43:20 -06001971 .eh_device_reset_handler= iscsi_eh_device_reset,
Mike Christie8e124522008-09-24 11:46:12 -05001972 .eh_target_reset_handler= iscsi_eh_target_reset,
Mike Christie5bb0b552006-04-06 21:26:46 -05001973 .use_clustering = DISABLE_CLUSTERING,
Mike Christied1d81c02007-05-30 12:57:21 -05001974 .slave_configure = iscsi_tcp_slave_configure,
Mike Christie5bb0b552006-04-06 21:26:46 -05001975 .proc_name = "iscsi_tcp",
1976 .this_id = -1,
1977};
1978
Alex Aizman7ba24712005-08-04 19:30:08 -07001979static struct iscsi_transport iscsi_tcp_transport = {
1980 .owner = THIS_MODULE,
1981 .name = "tcp",
1982 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
1983 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05001984 .param_mask = ISCSI_MAX_RECV_DLENGTH |
1985 ISCSI_MAX_XMIT_DLENGTH |
1986 ISCSI_HDRDGST_EN |
1987 ISCSI_DATADGST_EN |
1988 ISCSI_INITIAL_R2T_EN |
1989 ISCSI_MAX_R2T |
1990 ISCSI_IMM_DATA_EN |
1991 ISCSI_FIRST_BURST |
1992 ISCSI_MAX_BURST |
1993 ISCSI_PDU_INORDER_EN |
1994 ISCSI_DATASEQ_INORDER_EN |
1995 ISCSI_ERL |
1996 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05001997 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05001998 ISCSI_EXP_STATSN |
1999 ISCSI_PERSISTENT_PORT |
2000 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -05002001 ISCSI_TARGET_NAME | ISCSI_TPGT |
2002 ISCSI_USERNAME | ISCSI_PASSWORD |
Mike Christie843c0a82007-12-13 12:43:20 -06002003 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
Mike Christief6d51802007-12-13 12:43:30 -06002004 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
2005 ISCSI_LU_RESET_TMO |
Mike Christie88dfd342008-05-21 15:54:16 -05002006 ISCSI_PING_TMO | ISCSI_RECV_TMO |
2007 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
Mike Christie22236962007-05-30 12:57:24 -05002008 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
Mike Christied8196ed2007-05-30 12:57:25 -05002009 ISCSI_HOST_INITIATOR_NAME |
2010 ISCSI_HOST_NETDEV_NAME,
Mike Christie5bb0b552006-04-06 21:26:46 -05002011 /* session management */
2012 .create_session = iscsi_tcp_session_create,
2013 .destroy_session = iscsi_tcp_session_destroy,
2014 /* connection management */
2015 .create_conn = iscsi_tcp_conn_create,
2016 .bind_conn = iscsi_tcp_conn_bind,
2017 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002018 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002019 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002020 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002021 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05002022 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -05002023 /* iscsi host params */
Mike Christie75613522008-05-21 15:53:59 -05002024 .get_host_param = iscsi_host_get_param,
Mike Christie0801c242007-05-30 12:57:12 -05002025 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -05002026 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002027 .send_pdu = iscsi_conn_send_pdu,
2028 .get_stats = iscsi_conn_get_stats,
Mike Christiefbc514b2008-05-21 15:54:07 -05002029 .init_task = iscsi_tcp_task_init,
2030 .xmit_task = iscsi_tcp_task_xmit,
2031 .cleanup_task = iscsi_tcp_cleanup_task,
Mike Christie5bb0b552006-04-06 21:26:46 -05002032 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002033 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002034};
2035
2036static int __init
2037iscsi_tcp_init(void)
2038{
Alex Aizman7ba24712005-08-04 19:30:08 -07002039 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002040 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2041 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002042 return -EINVAL;
2043 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002044
Mike Christie75613522008-05-21 15:53:59 -05002045 iscsi_tcp_scsi_transport = iscsi_register_transport(
2046 &iscsi_tcp_transport);
2047 if (!iscsi_tcp_scsi_transport)
Mike Christieffbfe922006-05-18 20:31:36 -05002048 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002049
Mike Christie7b8631b2006-01-13 18:05:50 -06002050 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002051}
2052
2053static void __exit
2054iscsi_tcp_exit(void)
2055{
2056 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002057}
2058
2059module_init(iscsi_tcp_init);
2060module_exit(iscsi_tcp_exit);