blob: edebdf27af9b1e1b64b3d6cffd191e8586abd7dc [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
Alex Aizman7ba24712005-08-04 19:30:08 -070067static unsigned int iscsi_max_lun = 512;
68module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
69
Olaf Kirchda32dd62007-12-13 12:43:21 -060070static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -060071 struct iscsi_segment *segment);
Alex Aizman7ba24712005-08-04 19:30:08 -070072
Olaf Kirchda32dd62007-12-13 12:43:21 -060073/*
Olaf Kircha8ac6312007-12-13 12:43:35 -060074 * Scatterlist handling: inside the iscsi_segment, we
Olaf Kirchda32dd62007-12-13 12:43:21 -060075 * remember an index into the scatterlist, and set data/size
76 * to the current scatterlist entry. For highmem pages, we
77 * kmap as needed.
78 *
79 * Note that the page is unmapped when we return from
80 * TCP's data_ready handler, so we may end up mapping and
81 * unmapping the same page repeatedly. The whole reason
82 * for this is that we shouldn't keep the page mapped
83 * outside the softirq.
84 */
85
86/**
Olaf Kircha8ac6312007-12-13 12:43:35 -060087 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
88 * @segment: the buffer object
89 * @sg: scatterlist
Olaf Kirchda32dd62007-12-13 12:43:21 -060090 * @offset: byte offset into that sg entry
91 *
Olaf Kircha8ac6312007-12-13 12:43:35 -060092 * This function sets up the segment so that subsequent
Olaf Kirchda32dd62007-12-13 12:43:21 -060093 * data is copied to the indicated sg entry, at the given
94 * offset.
95 */
96static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -060097iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
98 struct scatterlist *sg, unsigned int offset)
Alex Aizman7ba24712005-08-04 19:30:08 -070099{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600100 segment->sg = sg;
101 segment->sg_offset = offset;
102 segment->size = min(sg->length - offset,
103 segment->total_size - segment->total_copied);
104 segment->data = NULL;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600105}
Alex Aizman7ba24712005-08-04 19:30:08 -0700106
Olaf Kirchda32dd62007-12-13 12:43:21 -0600107/**
Olaf Kircha8ac6312007-12-13 12:43:35 -0600108 * iscsi_tcp_segment_map - map the current S/G page
109 * @segment: iscsi_segment
110 * @recv: 1 if called from recv path
Olaf Kirchda32dd62007-12-13 12:43:21 -0600111 *
112 * We only need to possibly kmap data if scatter lists are being used,
113 * because the iscsi passthrough and internal IO paths will never use high
114 * mem pages.
115 */
116static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600117iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600118{
119 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700120
Olaf Kircha8ac6312007-12-13 12:43:35 -0600121 if (segment->data != NULL || !segment->sg)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600122 return;
Alex Aizman7ba24712005-08-04 19:30:08 -0700123
Olaf Kircha8ac6312007-12-13 12:43:35 -0600124 sg = segment->sg;
125 BUG_ON(segment->sg_mapped);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600126 BUG_ON(sg->length == 0);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600127
128 /*
129 * If the page count is greater than one it is ok to send
130 * to the network layer's zero copy send path. If not we
131 * have to go the slow sendmsg path. We always map for the
132 * recv path.
133 */
134 if (page_count(sg_page(sg)) >= 1 && !recv)
135 return;
136
137 debug_tcp("iscsi_tcp_segment_map %s %p\n", recv ? "recv" : "xmit",
138 segment);
139 segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0);
140 segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600141}
Alex Aizman7ba24712005-08-04 19:30:08 -0700142
Olaf Kirchda32dd62007-12-13 12:43:21 -0600143static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600144iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600145{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600146 debug_tcp("iscsi_tcp_segment_unmap %p\n", segment);
147
148 if (segment->sg_mapped) {
149 debug_tcp("iscsi_tcp_segment_unmap valid\n");
150 kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0);
151 segment->sg_mapped = NULL;
152 segment->data = NULL;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600153 }
154}
Alex Aizman7ba24712005-08-04 19:30:08 -0700155
Olaf Kirchda32dd62007-12-13 12:43:21 -0600156/*
157 * Splice the digest buffer into the buffer
158 */
159static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600160iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600161{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600162 segment->data = digest;
163 segment->digest_len = ISCSI_DIGEST_SIZE;
164 segment->total_size += ISCSI_DIGEST_SIZE;
165 segment->size = ISCSI_DIGEST_SIZE;
166 segment->copied = 0;
167 segment->sg = NULL;
168 segment->hash = NULL;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600169}
Alex Aizman7ba24712005-08-04 19:30:08 -0700170
Olaf Kirchda32dd62007-12-13 12:43:21 -0600171/**
Olaf Kircha8ac6312007-12-13 12:43:35 -0600172 * iscsi_tcp_segment_done - check whether the segment is complete
173 * @segment: iscsi segment to check
174 * @recv: set to one of this is called from the recv path
175 * @copied: number of bytes copied
Olaf Kirchda32dd62007-12-13 12:43:21 -0600176 *
Olaf Kircha8ac6312007-12-13 12:43:35 -0600177 * Check if we're done receiving this segment. If the receive
Olaf Kirchda32dd62007-12-13 12:43:21 -0600178 * buffer is full but we expect more data, move on to the
179 * next entry in the scatterlist.
180 *
181 * If the amount of data we received isn't a multiple of 4,
182 * we will transparently receive the pad bytes, too.
183 *
184 * This function must be re-entrant.
185 */
186static inline int
Olaf Kircha8ac6312007-12-13 12:43:35 -0600187iscsi_tcp_segment_done(struct iscsi_segment *segment, int recv, unsigned copied)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600188{
189 static unsigned char padbuf[ISCSI_PAD_LEN];
Olaf Kircha8ac6312007-12-13 12:43:35 -0600190 struct scatterlist sg;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600191 unsigned int pad;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600192
Olaf Kircha8ac6312007-12-13 12:43:35 -0600193 debug_tcp("copied %u %u size %u %s\n", segment->copied, copied,
194 segment->size, recv ? "recv" : "xmit");
195 if (segment->hash && copied) {
196 /*
197 * If a segment is kmapd we must unmap it before sending
198 * to the crypto layer since that will try to kmap it again.
199 */
200 iscsi_tcp_segment_unmap(segment);
201
202 if (!segment->data) {
203 sg_init_table(&sg, 1);
204 sg_set_page(&sg, sg_page(segment->sg), copied,
205 segment->copied + segment->sg_offset +
206 segment->sg->offset);
207 } else
208 sg_init_one(&sg, segment->data + segment->copied,
209 copied);
210 crypto_hash_update(segment->hash, &sg, copied);
211 }
212
213 segment->copied += copied;
214 if (segment->copied < segment->size) {
215 iscsi_tcp_segment_map(segment, recv);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600216 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700217 }
218
Olaf Kircha8ac6312007-12-13 12:43:35 -0600219 segment->total_copied += segment->copied;
220 segment->copied = 0;
221 segment->size = 0;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600222
223 /* Unmap the current scatterlist page, if there is one. */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600224 iscsi_tcp_segment_unmap(segment);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600225
226 /* Do we have more scatterlist entries? */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600227 debug_tcp("total copied %u total size %u\n", segment->total_copied,
228 segment->total_size);
229 if (segment->total_copied < segment->total_size) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600230 /* Proceed to the next entry in the scatterlist. */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600231 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
232 0);
233 iscsi_tcp_segment_map(segment, recv);
234 BUG_ON(segment->size == 0);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600235 return 0;
236 }
237
238 /* Do we need to handle padding? */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600239 pad = iscsi_padding(segment->total_copied);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600240 if (pad != 0) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600241 debug_tcp("consume %d pad bytes\n", pad);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600242 segment->total_size += pad;
243 segment->size = pad;
244 segment->data = padbuf;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600245 return 0;
246 }
247
248 /*
Olaf Kircha8ac6312007-12-13 12:43:35 -0600249 * Set us up for transferring the data digest. hdr digest
Olaf Kirchda32dd62007-12-13 12:43:21 -0600250 * is completely handled in hdr done function.
251 */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600252 if (segment->hash) {
253 crypto_hash_final(segment->hash, segment->digest);
254 iscsi_tcp_segment_splice_digest(segment,
255 recv ? segment->recv_digest : segment->digest);
256 return 0;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600257 }
258
259 return 1;
260}
261
262/**
Olaf Kircha8ac6312007-12-13 12:43:35 -0600263 * iscsi_tcp_xmit_segment - transmit segment
Olaf Kirchda32dd62007-12-13 12:43:21 -0600264 * @tcp_conn: the iSCSI TCP connection
Olaf Kircha8ac6312007-12-13 12:43:35 -0600265 * @segment: the buffer to transmnit
266 *
267 * This function transmits as much of the buffer as
268 * the network layer will accept, and returns the number of
269 * bytes transmitted.
270 *
271 * If CRC hashing is enabled, the function will compute the
272 * hash as it goes. When the entire segment has been transmitted,
273 * it will retrieve the hash value and send it as well.
274 */
275static int
276iscsi_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
277 struct iscsi_segment *segment)
278{
279 struct socket *sk = tcp_conn->sock;
280 unsigned int copied = 0;
281 int r = 0;
282
283 while (!iscsi_tcp_segment_done(segment, 0, r)) {
284 struct scatterlist *sg;
285 unsigned int offset, copy;
286 int flags = 0;
287
288 r = 0;
289 offset = segment->copied;
290 copy = segment->size - offset;
291
292 if (segment->total_copied + segment->size < segment->total_size)
293 flags |= MSG_MORE;
294
295 /* Use sendpage if we can; else fall back to sendmsg */
296 if (!segment->data) {
297 sg = segment->sg;
298 offset += segment->sg_offset + sg->offset;
299 r = tcp_conn->sendpage(sk, sg_page(sg), offset, copy,
300 flags);
301 } else {
302 struct msghdr msg = { .msg_flags = flags };
303 struct kvec iov = {
304 .iov_base = segment->data + offset,
305 .iov_len = copy
306 };
307
308 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
309 }
310
311 if (r < 0) {
312 iscsi_tcp_segment_unmap(segment);
313 if (copied || r == -EAGAIN)
314 break;
315 return r;
316 }
317 copied += r;
318 }
319 return copied;
320}
321
322/**
323 * iscsi_tcp_segment_recv - copy data to segment
324 * @tcp_conn: the iSCSI TCP connection
325 * @segment: the buffer to copy to
Olaf Kirchda32dd62007-12-13 12:43:21 -0600326 * @ptr: data pointer
327 * @len: amount of data available
328 *
329 * This function copies up to @len bytes to the
330 * given buffer, and returns the number of bytes
331 * consumed, which can actually be less than @len.
332 *
333 * If hash digest is enabled, the function will update the
334 * hash while copying.
335 * Combining these two operations doesn't buy us a lot (yet),
336 * but in the future we could implement combined copy+crc,
337 * just way we do for network layer checksums.
338 */
339static int
Olaf Kircha8ac6312007-12-13 12:43:35 -0600340iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
341 struct iscsi_segment *segment, const void *ptr,
342 unsigned int len)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600343{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600344 unsigned int copy = 0, copied = 0;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600345
Olaf Kircha8ac6312007-12-13 12:43:35 -0600346 while (!iscsi_tcp_segment_done(segment, 1, copy)) {
347 if (copied == len) {
348 debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n",
349 len);
350 break;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600351 }
Olaf Kircha8ac6312007-12-13 12:43:35 -0600352
353 copy = min(len - copied, segment->size - segment->copied);
354 debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy);
355 memcpy(segment->data + segment->copied, ptr + copied, copy);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600356 copied += copy;
357 }
Olaf Kirchda32dd62007-12-13 12:43:21 -0600358 return copied;
359}
360
361static inline void
362iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
363 unsigned char digest[ISCSI_DIGEST_SIZE])
364{
365 struct scatterlist sg;
366
367 sg_init_one(&sg, hdr, hdrlen);
368 crypto_hash_digest(hash, &sg, hdrlen, digest);
369}
370
371static inline int
372iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600373 struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600374{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600375 if (!segment->digest_len)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600376 return 1;
377
Olaf Kircha8ac6312007-12-13 12:43:35 -0600378 if (memcmp(segment->recv_digest, segment->digest,
379 segment->digest_len)) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600380 debug_scsi("digest mismatch\n");
381 return 0;
382 }
383
384 return 1;
385}
386
387/*
Olaf Kircha8ac6312007-12-13 12:43:35 -0600388 * Helper function to set up segment buffer
Olaf Kirchda32dd62007-12-13 12:43:21 -0600389 */
390static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600391__iscsi_segment_init(struct iscsi_segment *segment, size_t size,
392 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600393{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600394 memset(segment, 0, sizeof(*segment));
395 segment->total_size = size;
396 segment->done = done;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600397
398 if (hash) {
Olaf Kircha8ac6312007-12-13 12:43:35 -0600399 segment->hash = hash;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600400 crypto_hash_init(hash);
401 }
402}
403
404static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600405iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
406 size_t size, iscsi_segment_done_fn_t *done,
407 struct hash_desc *hash)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600408{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600409 __iscsi_segment_init(segment, size, done, hash);
410 segment->data = data;
411 segment->size = size;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600412}
413
414static inline int
Olaf Kircha8ac6312007-12-13 12:43:35 -0600415iscsi_segment_seek_sg(struct iscsi_segment *segment,
416 struct scatterlist *sg_list, unsigned int sg_count,
417 unsigned int offset, size_t size,
418 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600419{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600420 struct scatterlist *sg;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600421 unsigned int i;
422
Olaf Kircha8ac6312007-12-13 12:43:35 -0600423 debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n",
424 offset, size);
425 __iscsi_segment_init(segment, size, done, hash);
426 for_each_sg(sg_list, sg, sg_count, i) {
427 debug_scsi("sg %d, len %u offset %u\n", i, sg->length,
428 sg->offset);
429 if (offset < sg->length) {
430 iscsi_tcp_segment_init_sg(segment, sg, offset);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600431 return 0;
432 }
Olaf Kircha8ac6312007-12-13 12:43:35 -0600433 offset -= sg->length;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600434 }
435
436 return ISCSI_ERR_DATA_OFFSET;
437}
438
439/**
Olaf Kircha8ac6312007-12-13 12:43:35 -0600440 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
Olaf Kirchda32dd62007-12-13 12:43:21 -0600441 * @tcp_conn: iscsi connection to prep for
442 *
443 * This function always passes NULL for the hash argument, because when this
444 * function is called we do not yet know the final size of the header and want
445 * to delay the digest processing until we know that.
446 */
447static void
448iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
449{
450 debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn,
451 tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : "");
Olaf Kircha8ac6312007-12-13 12:43:35 -0600452 iscsi_segment_init_linear(&tcp_conn->in.segment,
Olaf Kirchda32dd62007-12-13 12:43:21 -0600453 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
454 iscsi_tcp_hdr_recv_done, NULL);
455}
456
457/*
458 * Handle incoming reply to any other type of command
459 */
460static int
461iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600462 struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600463{
464 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
465 int rc = 0;
466
Olaf Kircha8ac6312007-12-13 12:43:35 -0600467 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600468 return ISCSI_ERR_DATA_DGST;
469
470 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
471 conn->data, tcp_conn->in.datalen);
472 if (rc)
473 return rc;
474
475 iscsi_tcp_hdr_recv_prep(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700476 return 0;
477}
478
Olaf Kirchda32dd62007-12-13 12:43:21 -0600479static void
480iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
481{
482 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
483 struct hash_desc *rx_hash = NULL;
484
485 if (conn->datadgst_en)
486 rx_hash = &tcp_conn->rx_hash;
487
Olaf Kircha8ac6312007-12-13 12:43:35 -0600488 iscsi_segment_init_linear(&tcp_conn->in.segment,
Olaf Kirchda32dd62007-12-13 12:43:21 -0600489 conn->data, tcp_conn->in.datalen,
490 iscsi_tcp_data_recv_done, rx_hash);
491}
492
Mike Christie30a6c652006-04-06 21:13:39 -0500493/*
494 * must be called with session lock
495 */
496static void
Mike Christieb6c395e2006-07-24 15:47:15 -0500497iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700498{
Mike Christie5bb0b552006-04-06 21:26:46 -0500499 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500500 struct iscsi_r2t_info *r2t;
Alex Aizman7ba24712005-08-04 19:30:08 -0700501
Mike Christieb6c395e2006-07-24 15:47:15 -0500502 /* flush ctask's r2t queues */
503 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
504 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
505 sizeof(void*));
506 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
507 }
508
Olaf Kircha8ac6312007-12-13 12:43:35 -0600509 r2t = tcp_ctask->r2t;
510 if (r2t != NULL) {
511 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
512 sizeof(void*));
513 tcp_ctask->r2t = NULL;
514 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700515}
516
517/**
518 * iscsi_data_rsp - SCSI Data-In Response processing
519 * @conn: iscsi connection
520 * @ctask: scsi command task
521 **/
522static int
523iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
524{
Mike Christie5bb0b552006-04-06 21:26:46 -0500525 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
526 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
527 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700528 struct iscsi_session *session = conn->session;
Mike Christie857ae0b2007-05-30 12:57:15 -0500529 struct scsi_cmnd *sc = ctask->sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700530 int datasn = be32_to_cpu(rhdr->datasn);
531
Mike Christie77a23c22007-05-30 12:57:18 -0500532 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie5bb0b552006-04-06 21:26:46 -0500533 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700534 return 0;
535
Mike Christied473cc72007-05-30 12:57:14 -0500536 if (tcp_ctask->exp_datasn != datasn) {
537 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
538 __FUNCTION__, tcp_ctask->exp_datasn, datasn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700539 return ISCSI_ERR_DATASN;
Mike Christied473cc72007-05-30 12:57:14 -0500540 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700541
Mike Christied473cc72007-05-30 12:57:14 -0500542 tcp_ctask->exp_datasn++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700543
Mike Christie5bb0b552006-04-06 21:26:46 -0500544 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900545 if (tcp_ctask->data_offset + tcp_conn->in.datalen > scsi_bufflen(sc)) {
Mike Christie857ae0b2007-05-30 12:57:15 -0500546 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
547 __FUNCTION__, tcp_ctask->data_offset,
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900548 tcp_conn->in.datalen, scsi_bufflen(sc));
Alex Aizman7ba24712005-08-04 19:30:08 -0700549 return ISCSI_ERR_DATA_OFFSET;
Mike Christie857ae0b2007-05-30 12:57:15 -0500550 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700551
552 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600553 sc->result = (DID_OK << 16) | rhdr->cmd_status;
Alex Aizman7ba24712005-08-04 19:30:08 -0700554 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600555 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
556 ISCSI_FLAG_DATA_OVERFLOW)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700557 int res_count = be32_to_cpu(rhdr->residual_count);
558
559 if (res_count > 0 &&
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600560 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
561 res_count <= scsi_bufflen(sc)))
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900562 scsi_set_resid(sc, res_count);
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600563 else
Alex Aizman7ba24712005-08-04 19:30:08 -0700564 sc->result = (DID_BAD_TARGET << 16) |
565 rhdr->cmd_status;
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600566 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700567 }
568
569 conn->datain_pdus_cnt++;
570 return 0;
571}
572
573/**
574 * iscsi_solicit_data_init - initialize first Data-Out
575 * @conn: iscsi connection
576 * @ctask: scsi command task
577 * @r2t: R2T info
578 *
579 * Notes:
580 * Initialize first Data-Out within this R2T sequence and finds
581 * proper data_offset within this SCSI command.
582 *
583 * This function is called with connection lock taken.
584 **/
585static void
586iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
587 struct iscsi_r2t_info *r2t)
588{
589 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700590
Mike Christieffbfe922006-05-18 20:31:36 -0500591 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700592 memset(hdr, 0, sizeof(struct iscsi_data));
593 hdr->ttt = r2t->ttt;
594 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
595 r2t->solicit_datasn++;
596 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500597 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
598 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700599 hdr->exp_statsn = r2t->exp_statsn;
600 hdr->offset = cpu_to_be32(r2t->data_offset);
601 if (r2t->data_length > conn->max_xmit_dlength) {
602 hton24(hdr->dlength, conn->max_xmit_dlength);
603 r2t->data_count = conn->max_xmit_dlength;
604 hdr->flags = 0;
605 } else {
606 hton24(hdr->dlength, r2t->data_length);
607 r2t->data_count = r2t->data_length;
608 hdr->flags = ISCSI_FLAG_CMD_FINAL;
609 }
610 conn->dataout_pdus_cnt++;
611
612 r2t->sent = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700613}
614
615/**
616 * iscsi_r2t_rsp - iSCSI R2T Response processing
617 * @conn: iscsi connection
618 * @ctask: scsi command task
619 **/
620static int
621iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
622{
623 struct iscsi_r2t_info *r2t;
624 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500625 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
626 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
627 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700628 int r2tsn = be32_to_cpu(rhdr->r2tsn);
629 int rc;
630
Mike Christie98a94162006-08-31 18:09:26 -0400631 if (tcp_conn->in.datalen) {
632 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
633 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700634 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400635 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700636
Mike Christied473cc72007-05-30 12:57:14 -0500637 if (tcp_ctask->exp_datasn != r2tsn){
638 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
639 __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700640 return ISCSI_ERR_R2TSN;
Mike Christied473cc72007-05-30 12:57:14 -0500641 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700642
Alex Aizman7ba24712005-08-04 19:30:08 -0700643 /* fill-in new R2T associated with the task */
Mike Christie77a23c22007-05-30 12:57:18 -0500644 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
645
Mike Christie843c0a82007-12-13 12:43:20 -0600646 if (!ctask->sc || session->state != ISCSI_STATE_LOGGED_IN) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700647 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
648 "recovery...\n", ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -0700649 return 0;
650 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500651
Mike Christie5bb0b552006-04-06 21:26:46 -0500652 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700653 BUG_ON(!rc);
654
655 r2t->exp_statsn = rhdr->statsn;
656 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400657 if (r2t->data_length == 0) {
658 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
Olaf Kirch03766a12007-12-13 12:43:36 -0600659 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
660 sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700661 return ISCSI_ERR_DATALEN;
662 }
663
Mike Christie98a94162006-08-31 18:09:26 -0400664 if (r2t->data_length > session->max_burst)
665 debug_scsi("invalid R2T with data len %u and max burst %u."
666 "Attempting to execute request.\n",
667 r2t->data_length, session->max_burst);
668
Alex Aizman7ba24712005-08-04 19:30:08 -0700669 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900670 if (r2t->data_offset + r2t->data_length > scsi_bufflen(ctask->sc)) {
Mike Christie98a94162006-08-31 18:09:26 -0400671 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
672 "offset %u and total length %d\n", r2t->data_length,
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900673 r2t->data_offset, scsi_bufflen(ctask->sc));
Olaf Kirch03766a12007-12-13 12:43:36 -0600674 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
675 sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700676 return ISCSI_ERR_DATALEN;
677 }
678
679 r2t->ttt = rhdr->ttt; /* no flip */
680 r2t->solicit_datasn = 0;
681
682 iscsi_solicit_data_init(conn, ctask, r2t);
683
Mike Christied473cc72007-05-30 12:57:14 -0500684 tcp_ctask->exp_datasn = r2tsn + 1;
Mike Christie5bb0b552006-04-06 21:26:46 -0500685 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700686 conn->r2t_pdus_cnt++;
Mike Christie843c0a82007-12-13 12:43:20 -0600687
688 iscsi_requeue_ctask(ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -0700689 return 0;
690}
691
Olaf Kirchda32dd62007-12-13 12:43:21 -0600692/*
693 * Handle incoming reply to DataIn command
694 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700695static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600696iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600697 struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600698{
699 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
700 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
701 int rc;
702
Olaf Kircha8ac6312007-12-13 12:43:35 -0600703 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600704 return ISCSI_ERR_DATA_DGST;
705
706 /* check for non-exceptional status */
707 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
708 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
709 if (rc)
710 return rc;
711 }
712
713 iscsi_tcp_hdr_recv_prep(tcp_conn);
714 return 0;
715}
716
717/**
718 * iscsi_tcp_hdr_dissect - process PDU header
719 * @conn: iSCSI connection
720 * @hdr: PDU header
721 *
722 * This function analyzes the header of the PDU received,
723 * and performs several sanity checks. If the PDU is accompanied
724 * by data, the receive buffer is set up to copy the incoming data
725 * to the correct location.
726 */
727static int
728iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
Alex Aizman7ba24712005-08-04 19:30:08 -0700729{
Mike Christie5bb0b552006-04-06 21:26:46 -0500730 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700731 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500732 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600733 struct iscsi_cmd_task *ctask;
734 uint32_t itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700735
736 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500737 tcp_conn->in.datalen = ntoh24(hdr->dlength);
738 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700739 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500740 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700741 return ISCSI_ERR_DATALEN;
742 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700743
Olaf Kirchda32dd62007-12-13 12:43:21 -0600744 /* Additional header segments. So far, we don't
745 * process additional headers.
746 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500747 ahslen = hdr->hlength << 2;
Alex Aizman7ba24712005-08-04 19:30:08 -0700748
Mike Christie5bb0b552006-04-06 21:26:46 -0500749 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700750 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500751 rc = iscsi_verify_itt(conn, hdr, &itt);
Mike Christie7a53dc52007-12-13 12:43:37 -0600752 if (rc)
Mike Christie5bb0b552006-04-06 21:26:46 -0500753 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700754
Olaf Kirchda32dd62007-12-13 12:43:21 -0600755 debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
756 opcode, ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700757
Mike Christie5bb0b552006-04-06 21:26:46 -0500758 switch(opcode) {
759 case ISCSI_OP_SCSI_DATA_IN:
Olaf Kirchda32dd62007-12-13 12:43:21 -0600760 ctask = session->cmds[itt];
Mike Christie4545a882007-12-13 12:43:40 -0600761 spin_lock(&conn->session->lock);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600762 rc = iscsi_data_rsp(conn, ctask);
Mike Christie4545a882007-12-13 12:43:40 -0600763 spin_unlock(&conn->session->lock);
Mike Christie275fd7d2006-07-24 15:47:17 -0500764 if (rc)
765 return rc;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600766 if (tcp_conn->in.datalen) {
767 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
768 struct hash_desc *rx_hash = NULL;
769
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 */
778 if (conn->datadgst_en)
779 rx_hash = &tcp_conn->rx_hash;
780
781 debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
782 "datalen=%d)\n", tcp_conn,
783 tcp_ctask->data_offset,
784 tcp_conn->in.datalen);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600785 return iscsi_segment_seek_sg(&tcp_conn->in.segment,
786 scsi_sglist(ctask->sc),
787 scsi_sg_count(ctask->sc),
788 tcp_ctask->data_offset,
789 tcp_conn->in.datalen,
790 iscsi_tcp_process_data_in,
791 rx_hash);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600792 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500793 /* fall through */
794 case ISCSI_OP_SCSI_CMD_RSP:
Olaf Kirchda32dd62007-12-13 12:43:21 -0600795 if (tcp_conn->in.datalen) {
796 iscsi_tcp_data_recv_prep(tcp_conn);
797 return 0;
798 }
799 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500800 break;
801 case ISCSI_OP_R2T:
Olaf Kirchda32dd62007-12-13 12:43:21 -0600802 ctask = session->cmds[itt];
Mike Christie5bb0b552006-04-06 21:26:46 -0500803 if (ahslen)
804 rc = ISCSI_ERR_AHSLEN;
Mike Christie4545a882007-12-13 12:43:40 -0600805 else if (ctask->sc->sc_data_direction == DMA_TO_DEVICE) {
806 spin_lock(&session->lock);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600807 rc = iscsi_r2t_rsp(conn, ctask);
Mike Christie4545a882007-12-13 12:43:40 -0600808 spin_unlock(&session->lock);
809 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500810 rc = ISCSI_ERR_PROTO;
811 break;
812 case ISCSI_OP_LOGIN_RSP:
813 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500814 case ISCSI_OP_REJECT:
815 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500816 /*
817 * It is possible that we could get a PDU with a buffer larger
818 * than 8K, but there are no targets that currently do this.
819 * For now we fail until we find a vendor that needs it
820 */
Olaf Kirchda32dd62007-12-13 12:43:21 -0600821 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
Mike Christiec8dc1e52006-07-24 15:47:39 -0500822 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
823 "but conn buffer is only %u (opcode %0x)\n",
824 tcp_conn->in.datalen,
Mike Christiebf32ed32007-02-28 17:32:17 -0600825 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
Mike Christiec8dc1e52006-07-24 15:47:39 -0500826 rc = ISCSI_ERR_PROTO;
827 break;
828 }
829
Olaf Kirchda32dd62007-12-13 12:43:21 -0600830 /* If there's data coming in with the response,
831 * receive it to the connection's buffer.
832 */
833 if (tcp_conn->in.datalen) {
834 iscsi_tcp_data_recv_prep(tcp_conn);
835 return 0;
836 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500837 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500838 case ISCSI_OP_LOGOUT_RSP:
839 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500840 case ISCSI_OP_SCSI_TMFUNC_RSP:
841 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
842 break;
843 default:
844 rc = ISCSI_ERR_BAD_OPCODE;
845 break;
846 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700847
Olaf Kirchda32dd62007-12-13 12:43:21 -0600848 if (rc == 0) {
849 /* Anything that comes with data should have
850 * been handled above. */
851 if (tcp_conn->in.datalen)
852 return ISCSI_ERR_PROTO;
853 iscsi_tcp_hdr_recv_prep(tcp_conn);
854 }
855
Alex Aizman7ba24712005-08-04 19:30:08 -0700856 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700857}
858
Olaf Kirchda32dd62007-12-13 12:43:21 -0600859/**
860 * iscsi_tcp_hdr_recv_done - process PDU header
861 *
862 * This is the callback invoked when the PDU header has
863 * been received. If the header is followed by additional
864 * header segments, we go back for more data.
865 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700866static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600867iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600868 struct iscsi_segment *segment)
Alex Aizman7ba24712005-08-04 19:30:08 -0700869{
Olaf Kirchda32dd62007-12-13 12:43:21 -0600870 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
871 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700872
Olaf Kirchda32dd62007-12-13 12:43:21 -0600873 /* Check if there are additional header segments
874 * *prior* to computing the digest, because we
875 * may need to go back to the caller for more.
876 */
877 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600878 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600879 /* Bump the header length - the caller will
880 * just loop around and get the AHS for us, and
881 * call again. */
882 unsigned int ahslen = hdr->hlength << 2;
Alex Aizman7ba24712005-08-04 19:30:08 -0700883
Olaf Kirchda32dd62007-12-13 12:43:21 -0600884 /* Make sure we don't overflow */
885 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
886 return ISCSI_ERR_AHSLEN;
887
Olaf Kircha8ac6312007-12-13 12:43:35 -0600888 segment->total_size += ahslen;
889 segment->size += ahslen;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600890 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700891 }
Olaf Kirchda32dd62007-12-13 12:43:21 -0600892
893 /* We're done processing the header. See if we're doing
894 * header digests; if so, set up the recv_digest buffer
895 * and go back for more. */
896 if (conn->hdrdgst_en) {
Olaf Kircha8ac6312007-12-13 12:43:35 -0600897 if (segment->digest_len == 0) {
898 iscsi_tcp_segment_splice_digest(segment,
899 segment->recv_digest);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600900 return 0;
901 }
902 iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600903 segment->total_copied - ISCSI_DIGEST_SIZE,
904 segment->digest);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600905
Olaf Kircha8ac6312007-12-13 12:43:35 -0600906 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600907 return ISCSI_ERR_HDR_DGST;
908 }
909
910 tcp_conn->in.hdr = hdr;
911 return iscsi_tcp_hdr_dissect(conn, hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -0700912}
913
914/**
Olaf Kirchda32dd62007-12-13 12:43:21 -0600915 * iscsi_tcp_recv - TCP receive in sendfile fashion
Alex Aizman7ba24712005-08-04 19:30:08 -0700916 * @rd_desc: read descriptor
917 * @skb: socket buffer
918 * @offset: offset in skb
919 * @len: skb->len - offset
920 **/
921static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600922iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
923 unsigned int offset, size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -0700924{
Alex Aizman7ba24712005-08-04 19:30:08 -0700925 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500926 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600927 struct iscsi_segment *segment = &tcp_conn->in.segment;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600928 struct skb_seq_state seq;
929 unsigned int consumed = 0;
930 int rc = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700931
Olaf Kirchda32dd62007-12-13 12:43:21 -0600932 debug_tcp("in %d bytes\n", skb->len - offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700933
934 if (unlikely(conn->suspend_rx)) {
935 debug_tcp("conn %d Rx suspended!\n", conn->id);
936 return 0;
937 }
938
Olaf Kirchda32dd62007-12-13 12:43:21 -0600939 skb_prepare_seq_read(skb, offset, skb->len, &seq);
940 while (1) {
941 unsigned int avail;
942 const u8 *ptr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700943
Olaf Kirchda32dd62007-12-13 12:43:21 -0600944 avail = skb_seq_read(consumed, &ptr, &seq);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600945 if (avail == 0) {
946 debug_tcp("no more data avail. Consumed %d\n",
947 consumed);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600948 break;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600949 }
950 BUG_ON(segment->copied >= segment->size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700951
Olaf Kirchda32dd62007-12-13 12:43:21 -0600952 debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600953 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600954 BUG_ON(rc == 0);
955 consumed += rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500956
Olaf Kircha8ac6312007-12-13 12:43:35 -0600957 if (segment->total_copied >= segment->total_size) {
958 debug_tcp("segment done\n");
959 rc = segment->done(tcp_conn, segment);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600960 if (rc != 0) {
961 skb_abort_seq_read(&seq);
962 goto error;
Mike Christiedbdb0162007-05-30 12:57:20 -0500963 }
Mike Christiedbdb0162007-05-30 12:57:20 -0500964
Olaf Kirchda32dd62007-12-13 12:43:21 -0600965 /* The done() functions sets up the
Olaf Kircha8ac6312007-12-13 12:43:35 -0600966 * next segment. */
Alex Aizman7ba24712005-08-04 19:30:08 -0700967 }
968 }
Olaf Kircha8ac6312007-12-13 12:43:35 -0600969 skb_abort_seq_read(&seq);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600970 conn->rxdata_octets += consumed;
971 return consumed;
Alex Aizman7ba24712005-08-04 19:30:08 -0700972
Olaf Kirchda32dd62007-12-13 12:43:21 -0600973error:
974 debug_tcp("Error receiving PDU, errno=%d\n", rc);
975 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
976 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700977}
978
979static void
980iscsi_tcp_data_ready(struct sock *sk, int flag)
981{
982 struct iscsi_conn *conn = sk->sk_user_data;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600983 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700984 read_descriptor_t rd_desc;
985
986 read_lock(&sk->sk_callback_lock);
987
Mike Christie665b44a2006-05-02 19:46:49 -0500988 /*
Olaf Kirchda32dd62007-12-13 12:43:21 -0600989 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
Mike Christie665b44a2006-05-02 19:46:49 -0500990 * We set count to 1 because we want the network layer to
Olaf Kirchda32dd62007-12-13 12:43:21 -0600991 * hand us all the skbs that are available. iscsi_tcp_recv
Mike Christie665b44a2006-05-02 19:46:49 -0500992 * handled pdus that cross buffers or pdus that still need data.
993 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700994 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -0500995 rd_desc.count = 1;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600996 tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
Alex Aizman7ba24712005-08-04 19:30:08 -0700997
998 read_unlock(&sk->sk_callback_lock);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600999
1000 /* If we had to (atomically) map a highmem page,
1001 * unmap it now. */
Olaf Kircha8ac6312007-12-13 12:43:35 -06001002 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
Alex Aizman7ba24712005-08-04 19:30:08 -07001003}
1004
1005static void
1006iscsi_tcp_state_change(struct sock *sk)
1007{
Mike Christie5bb0b552006-04-06 21:26:46 -05001008 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001009 struct iscsi_conn *conn;
1010 struct iscsi_session *session;
1011 void (*old_state_change)(struct sock *);
1012
1013 read_lock(&sk->sk_callback_lock);
1014
1015 conn = (struct iscsi_conn*)sk->sk_user_data;
1016 session = conn->session;
1017
Mike Christiee6273992005-11-29 23:12:49 -06001018 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1019 sk->sk_state == TCP_CLOSE) &&
1020 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001021 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1022 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1023 }
1024
Mike Christie5bb0b552006-04-06 21:26:46 -05001025 tcp_conn = conn->dd_data;
1026 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001027
1028 read_unlock(&sk->sk_callback_lock);
1029
1030 old_state_change(sk);
1031}
1032
1033/**
1034 * iscsi_write_space - Called when more output buffer space is available
1035 * @sk: socket space is available for
1036 **/
1037static void
1038iscsi_write_space(struct sock *sk)
1039{
1040 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001041 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1042
1043 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001044 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001045 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001046}
1047
1048static void
1049iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1050{
Mike Christie5bb0b552006-04-06 21:26:46 -05001051 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1052 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001053
1054 /* assign new callbacks */
1055 write_lock_bh(&sk->sk_callback_lock);
1056 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001057 tcp_conn->old_data_ready = sk->sk_data_ready;
1058 tcp_conn->old_state_change = sk->sk_state_change;
1059 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001060 sk->sk_data_ready = iscsi_tcp_data_ready;
1061 sk->sk_state_change = iscsi_tcp_state_change;
1062 sk->sk_write_space = iscsi_write_space;
1063 write_unlock_bh(&sk->sk_callback_lock);
1064}
1065
1066static void
Mike Christie1c834692006-07-24 15:47:26 -05001067iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001068{
Mike Christie5bb0b552006-04-06 21:26:46 -05001069 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001070
1071 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1072 write_lock_bh(&sk->sk_callback_lock);
1073 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001074 sk->sk_data_ready = tcp_conn->old_data_ready;
1075 sk->sk_state_change = tcp_conn->old_state_change;
1076 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001077 sk->sk_no_check = 0;
1078 write_unlock_bh(&sk->sk_callback_lock);
1079}
1080
1081/**
Olaf Kircha8ac6312007-12-13 12:43:35 -06001082 * iscsi_xmit - TCP transmit
1083 **/
1084static int
1085iscsi_xmit(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001086{
Mike Christie5bb0b552006-04-06 21:26:46 -05001087 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001088 struct iscsi_segment *segment = &tcp_conn->out.segment;
1089 unsigned int consumed = 0;
1090 int rc = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001091
Olaf Kircha8ac6312007-12-13 12:43:35 -06001092 while (1) {
1093 rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
1094 if (rc < 0)
1095 goto error;
1096 if (rc == 0)
1097 break;
1098
1099 consumed += rc;
1100
1101 if (segment->total_copied >= segment->total_size) {
1102 if (segment->done != NULL) {
1103 rc = segment->done(tcp_conn, segment);
1104 if (rc < 0)
1105 goto error;
1106 }
1107 }
1108 }
1109
1110 debug_tcp("xmit %d bytes\n", consumed);
1111
1112 conn->txdata_octets += consumed;
1113 return consumed;
1114
1115error:
1116 /* Transmit error. We could initiate error recovery
1117 * here. */
1118 debug_tcp("Error sending PDU, errno=%d\n", rc);
1119 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1120 return rc;
1121}
1122
1123/**
1124 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
1125 */
1126static inline int
1127iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
1128{
1129 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1130 struct iscsi_segment *segment = &tcp_conn->out.segment;
1131
1132 return segment->total_copied - segment->total_size;
1133}
1134
1135static inline int
1136iscsi_tcp_flush(struct iscsi_conn *conn)
1137{
1138 int rc;
1139
1140 while (iscsi_tcp_xmit_qlen(conn)) {
1141 rc = iscsi_xmit(conn);
1142 if (rc == 0)
1143 return -EAGAIN;
1144 if (rc < 0)
1145 return rc;
1146 }
1147
1148 return 0;
1149}
1150
1151/*
1152 * This is called when we're done sending the header.
1153 * Simply copy the data_segment to the send segment, and return.
1154 */
1155static int
1156iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
1157 struct iscsi_segment *segment)
1158{
1159 tcp_conn->out.segment = tcp_conn->out.data_segment;
1160 debug_tcp("Header done. Next segment size %u total_size %u\n",
1161 tcp_conn->out.segment.size, tcp_conn->out.segment.total_size);
1162 return 0;
1163}
1164
1165static void
1166iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen)
1167{
1168 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1169
1170 debug_tcp("%s(%p%s)\n", __FUNCTION__, tcp_conn,
1171 conn->hdrdgst_en? ", digest enabled" : "");
1172
1173 /* Clear the data segment - needs to be filled in by the
1174 * caller using iscsi_tcp_send_data_prep() */
1175 memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment));
1176
1177 /* If header digest is enabled, compute the CRC and
1178 * place the digest into the same buffer. We make
1179 * sure that both iscsi_tcp_ctask and mtask have
1180 * sufficient room.
Mike Christie7cae5152006-01-13 18:05:47 -06001181 */
Olaf Kircha8ac6312007-12-13 12:43:35 -06001182 if (conn->hdrdgst_en) {
1183 iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
1184 hdr + hdrlen);
1185 hdrlen += ISCSI_DIGEST_SIZE;
Mike Christie3219e522006-05-30 00:37:28 -05001186 }
1187
Olaf Kircha8ac6312007-12-13 12:43:35 -06001188 /* Remember header pointer for later, when we need
1189 * to decide whether there's a payload to go along
1190 * with the header. */
1191 tcp_conn->out.hdr = hdr;
1192
1193 iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen,
1194 iscsi_tcp_send_hdr_done, NULL);
Alex Aizman7ba24712005-08-04 19:30:08 -07001195}
1196
Olaf Kircha8ac6312007-12-13 12:43:35 -06001197/*
1198 * Prepare the send buffer for the payload data.
1199 * Padding and checksumming will all be taken care
1200 * of by the iscsi_segment routines.
1201 */
1202static int
1203iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
1204 unsigned int count, unsigned int offset,
1205 unsigned int len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001206{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001207 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1208 struct hash_desc *tx_hash = NULL;
1209 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -07001210
Olaf Kircha8ac6312007-12-13 12:43:35 -06001211 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __FUNCTION__,
1212 tcp_conn, offset, len,
1213 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -07001214
Olaf Kircha8ac6312007-12-13 12:43:35 -06001215 /* Make sure the datalen matches what the caller
1216 said he would send. */
1217 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1218 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -07001219
Olaf Kircha8ac6312007-12-13 12:43:35 -06001220 if (conn->datadgst_en)
1221 tx_hash = &tcp_conn->tx_hash;
1222
1223 return iscsi_segment_seek_sg(&tcp_conn->out.data_segment,
1224 sg, count, offset, len,
1225 NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -07001226}
1227
Olaf Kircha8ac6312007-12-13 12:43:35 -06001228static void
1229iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
1230 size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001231{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001232 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1233 struct hash_desc *tx_hash = NULL;
1234 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -07001235
Olaf Kircha8ac6312007-12-13 12:43:35 -06001236 debug_tcp("%s(%p, datalen=%d%s)\n", __FUNCTION__, tcp_conn, len,
1237 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -07001238
Olaf Kircha8ac6312007-12-13 12:43:35 -06001239 /* Make sure the datalen matches what the caller
1240 said he would send. */
1241 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1242 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -07001243
Olaf Kircha8ac6312007-12-13 12:43:35 -06001244 if (conn->datadgst_en)
1245 tx_hash = &tcp_conn->tx_hash;
Alex Aizman7ba24712005-08-04 19:30:08 -07001246
Olaf Kircha8ac6312007-12-13 12:43:35 -06001247 iscsi_segment_init_linear(&tcp_conn->out.data_segment,
1248 data, len, NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -07001249}
1250
Alex Aizman7ba24712005-08-04 19:30:08 -07001251/**
1252 * iscsi_solicit_data_cont - initialize next Data-Out
1253 * @conn: iscsi connection
1254 * @ctask: scsi command task
1255 * @r2t: R2T info
1256 * @left: bytes left to transfer
1257 *
1258 * Notes:
1259 * Initialize next Data-Out within this R2T sequence and continue
1260 * to process next Scatter-Gather element(if any) of this SCSI command.
1261 *
1262 * Called under connection lock.
1263 **/
Olaf Kircha8ac6312007-12-13 12:43:35 -06001264static int
Alex Aizman7ba24712005-08-04 19:30:08 -07001265iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001266 struct iscsi_r2t_info *r2t)
Alex Aizman7ba24712005-08-04 19:30:08 -07001267{
1268 struct iscsi_data *hdr;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001269 int new_offset, left;
1270
1271 BUG_ON(r2t->data_length - r2t->sent < 0);
1272 left = r2t->data_length - r2t->sent;
1273 if (left == 0)
1274 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001275
Mike Christieffbfe922006-05-18 20:31:36 -05001276 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001277 memset(hdr, 0, sizeof(struct iscsi_data));
1278 hdr->ttt = r2t->ttt;
1279 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1280 r2t->solicit_datasn++;
1281 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001282 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1283 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001284 hdr->exp_statsn = r2t->exp_statsn;
1285 new_offset = r2t->data_offset + r2t->sent;
1286 hdr->offset = cpu_to_be32(new_offset);
1287 if (left > conn->max_xmit_dlength) {
1288 hton24(hdr->dlength, conn->max_xmit_dlength);
1289 r2t->data_count = conn->max_xmit_dlength;
1290 } else {
1291 hton24(hdr->dlength, left);
1292 r2t->data_count = left;
1293 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1294 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001295
Alex Aizman7ba24712005-08-04 19:30:08 -07001296 conn->dataout_pdus_cnt++;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001297 return 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001298}
1299
1300/**
Olaf Kircha8ac6312007-12-13 12:43:35 -06001301 * iscsi_tcp_ctask - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001302 * @conn: iscsi connection
1303 * @ctask: scsi command task
1304 * @sc: scsi command
1305 **/
Olaf Kircha8ac6312007-12-13 12:43:35 -06001306static int
1307iscsi_tcp_ctask_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001308{
Mike Christie5bb0b552006-04-06 21:26:46 -05001309 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001310 struct iscsi_conn *conn = ctask->conn;
1311 struct scsi_cmnd *sc = ctask->sc;
1312 int err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001313
Mike Christie5bb0b552006-04-06 21:26:46 -05001314 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Olaf Kircha8ac6312007-12-13 12:43:35 -06001315 tcp_ctask->sent = 0;
1316 tcp_ctask->exp_datasn = 0;
1317
1318 /* Prepare PDU, optionally w/ immediate data */
1319 debug_scsi("ctask deq [cid %d itt 0x%x imm %d unsol %d]\n",
1320 conn->id, ctask->itt, ctask->imm_count,
1321 ctask->unsol_count);
1322 iscsi_tcp_send_hdr_prep(conn, ctask->hdr, ctask->hdr_len);
1323
1324 if (!ctask->imm_count)
1325 return 0;
1326
1327 /* If we have immediate data, attach a payload */
1328 err = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc), scsi_sg_count(sc),
1329 0, ctask->imm_count);
1330 if (err)
1331 return err;
1332 tcp_ctask->sent += ctask->imm_count;
1333 ctask->imm_count = 0;
1334 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001335}
1336
1337/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001338 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001339 * @conn: iscsi connection
1340 * @mtask: task management task
1341 *
1342 * Notes:
1343 * The function can return -EAGAIN in which case caller must
1344 * call it again later, or recover. '0' return code means successful
1345 * xmit.
Alex Aizman7ba24712005-08-04 19:30:08 -07001346 **/
1347static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001348iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001349{
Mike Christie3219e522006-05-30 00:37:28 -05001350 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001351
Olaf Kircha8ac6312007-12-13 12:43:35 -06001352 /* Flush any pending data first. */
1353 rc = iscsi_tcp_flush(conn);
1354 if (rc < 0)
1355 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001356
Al Virob4377352007-02-09 16:39:40 +00001357 if (mtask->hdr->itt == RESERVED_ITT) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001358 struct iscsi_session *session = conn->session;
1359
1360 spin_lock_bh(&session->lock);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001361 iscsi_free_mgmt_task(conn, mtask);
Mike Christie5bb0b552006-04-06 21:26:46 -05001362 spin_unlock_bh(&session->lock);
1363 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001364
Alex Aizman7ba24712005-08-04 19:30:08 -07001365 return 0;
1366}
1367
Olaf Kircha8ac6312007-12-13 12:43:35 -06001368/*
Mike Christie218432c2007-05-30 12:57:17 -05001369 * iscsi_tcp_ctask_xmit - xmit normal PDU task
1370 * @conn: iscsi connection
1371 * @ctask: iscsi command task
1372 *
Olaf Kircha8ac6312007-12-13 12:43:35 -06001373 * We're expected to return 0 when everything was transmitted succesfully,
1374 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1375 * of error.
1376 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001377static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001378iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001379{
Mike Christie5bb0b552006-04-06 21:26:46 -05001380 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001381 struct scsi_cmnd *sc = ctask->sc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001382 int rc = 0;
1383
Olaf Kircha8ac6312007-12-13 12:43:35 -06001384flush:
1385 /* Flush any pending data first. */
1386 rc = iscsi_tcp_flush(conn);
1387 if (rc < 0)
Mike Christie218432c2007-05-30 12:57:17 -05001388 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001389
1390 /* Are we done already? */
1391 if (sc->sc_data_direction != DMA_TO_DEVICE)
Mike Christie218432c2007-05-30 12:57:17 -05001392 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001393
Olaf Kircha8ac6312007-12-13 12:43:35 -06001394 if (ctask->unsol_count != 0) {
1395 struct iscsi_data *hdr = &tcp_ctask->unsol_dtask.hdr;
1396
1397 /* Prepare a header for the unsolicited PDU.
1398 * The amount of data we want to send will be
1399 * in ctask->data_count.
1400 * FIXME: return the data count instead.
1401 */
1402 iscsi_prep_unsolicit_data_pdu(ctask, hdr);
1403
1404 debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n",
1405 ctask->itt, tcp_ctask->sent, ctask->data_count);
1406
1407 iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr));
1408 rc = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc),
1409 scsi_sg_count(sc),
1410 tcp_ctask->sent,
1411 ctask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001412 if (rc)
Olaf Kircha8ac6312007-12-13 12:43:35 -06001413 goto fail;
1414 tcp_ctask->sent += ctask->data_count;
1415 ctask->unsol_count -= ctask->data_count;
1416 goto flush;
1417 } else {
1418 struct iscsi_session *session = conn->session;
1419 struct iscsi_r2t_info *r2t;
1420
1421 /* All unsolicited PDUs sent. Check for solicited PDUs.
1422 */
1423 spin_lock_bh(&session->lock);
1424 r2t = tcp_ctask->r2t;
1425 if (r2t != NULL) {
1426 /* Continue with this R2T? */
1427 if (!iscsi_solicit_data_cont(conn, ctask, r2t)) {
1428 debug_scsi(" done with r2t %p\n", r2t);
1429
1430 __kfifo_put(tcp_ctask->r2tpool.queue,
1431 (void*)&r2t, sizeof(void*));
1432 tcp_ctask->r2t = r2t = NULL;
1433 }
1434 }
1435
1436 if (r2t == NULL) {
1437 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1438 sizeof(void*));
1439 r2t = tcp_ctask->r2t;
1440 }
1441 spin_unlock_bh(&session->lock);
1442
1443 /* Waiting for more R2Ts to arrive. */
1444 if (r2t == NULL) {
1445 debug_tcp("no R2Ts yet\n");
1446 return 0;
1447 }
1448
1449 debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1450 r2t, r2t->solicit_datasn - 1, ctask->itt,
1451 r2t->data_offset + r2t->sent, r2t->data_count);
1452
1453 iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr,
1454 sizeof(struct iscsi_hdr));
1455
1456 rc = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc),
1457 scsi_sg_count(sc),
1458 r2t->data_offset + r2t->sent,
1459 r2t->data_count);
1460 if (rc)
1461 goto fail;
1462 tcp_ctask->sent += r2t->data_count;
1463 r2t->sent += r2t->data_count;
1464 goto flush;
Alex Aizman7ba24712005-08-04 19:30:08 -07001465 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001466 return 0;
1467fail:
1468 iscsi_conn_failure(conn, rc);
1469 return -EIO;
Alex Aizman7ba24712005-08-04 19:30:08 -07001470}
1471
Mike Christie7b8631b2006-01-13 18:05:50 -06001472static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001473iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001474{
Mike Christie7b8631b2006-01-13 18:05:50 -06001475 struct iscsi_conn *conn;
1476 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001477 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001478
Mike Christie5bb0b552006-04-06 21:26:46 -05001479 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001480 if (!cls_conn)
1481 return NULL;
1482 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001483 /*
1484 * due to strange issues with iser these are not set
1485 * in iscsi_conn_setup
1486 */
Mike Christiebf32ed32007-02-28 17:32:17 -06001487 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
Alex Aizman7ba24712005-08-04 19:30:08 -07001488
Mike Christie5bb0b552006-04-06 21:26:46 -05001489 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1490 if (!tcp_conn)
1491 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001492
Mike Christie5bb0b552006-04-06 21:26:46 -05001493 conn->dd_data = tcp_conn;
1494 tcp_conn->iscsi_conn = conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001495
James Bottomleyc9802cd2006-09-23 15:33:43 -05001496 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1497 CRYPTO_ALG_ASYNC);
1498 tcp_conn->tx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001499 if (IS_ERR(tcp_conn->tx_hash.tfm)) {
1500 printk(KERN_ERR "Could not create connection due to crc32c "
1501 "loading error %ld. Make sure the crc32c module is "
1502 "built as a module or into the kernel\n",
1503 PTR_ERR(tcp_conn->tx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001504 goto free_tcp_conn;
Mike Christie0f238412007-02-28 17:32:21 -06001505 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001506
James Bottomleyc9802cd2006-09-23 15:33:43 -05001507 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1508 CRYPTO_ALG_ASYNC);
1509 tcp_conn->rx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001510 if (IS_ERR(tcp_conn->rx_hash.tfm)) {
1511 printk(KERN_ERR "Could not create connection due to crc32c "
1512 "loading error %ld. Make sure the crc32c module is "
1513 "built as a module or into the kernel\n",
1514 PTR_ERR(tcp_conn->rx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001515 goto free_tx_tfm;
Mike Christie0f238412007-02-28 17:32:21 -06001516 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001517
Mike Christie7b8631b2006-01-13 18:05:50 -06001518 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001519
Mike Christiedd8c0d92006-08-31 18:09:28 -04001520free_tx_tfm:
James Bottomleyc9802cd2006-09-23 15:33:43 -05001521 crypto_free_hash(tcp_conn->tx_hash.tfm);
Mike Christiedd8c0d92006-08-31 18:09:28 -04001522free_tcp_conn:
1523 kfree(tcp_conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001524tcp_conn_alloc_fail:
1525 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001526 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001527}
1528
1529static void
Mike Christie1c834692006-07-24 15:47:26 -05001530iscsi_tcp_release_conn(struct iscsi_conn *conn)
1531{
Mike Christie22236962007-05-30 12:57:24 -05001532 struct iscsi_session *session = conn->session;
Mike Christie1c834692006-07-24 15:47:26 -05001533 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie22236962007-05-30 12:57:24 -05001534 struct socket *sock = tcp_conn->sock;
Mike Christie1c834692006-07-24 15:47:26 -05001535
Mike Christie22236962007-05-30 12:57:24 -05001536 if (!sock)
Mike Christie1c834692006-07-24 15:47:26 -05001537 return;
1538
Mike Christie22236962007-05-30 12:57:24 -05001539 sock_hold(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001540 iscsi_conn_restore_callbacks(tcp_conn);
Mike Christie22236962007-05-30 12:57:24 -05001541 sock_put(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001542
Mike Christie22236962007-05-30 12:57:24 -05001543 spin_lock_bh(&session->lock);
Mike Christie1c834692006-07-24 15:47:26 -05001544 tcp_conn->sock = NULL;
1545 conn->recv_lock = NULL;
Mike Christie22236962007-05-30 12:57:24 -05001546 spin_unlock_bh(&session->lock);
1547 sockfd_put(sock);
Mike Christie1c834692006-07-24 15:47:26 -05001548}
1549
1550static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001551iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001552{
Mike Christie7b8631b2006-01-13 18:05:50 -06001553 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001554 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001555
Mike Christie1c834692006-07-24 15:47:26 -05001556 iscsi_tcp_release_conn(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001557 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001558
Pete Wyckoff534284a2006-11-08 15:58:31 -06001559 if (tcp_conn->tx_hash.tfm)
1560 crypto_free_hash(tcp_conn->tx_hash.tfm);
1561 if (tcp_conn->rx_hash.tfm)
1562 crypto_free_hash(tcp_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001563
Mike Christie5bb0b552006-04-06 21:26:46 -05001564 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001565}
1566
Mike Christie1c834692006-07-24 15:47:26 -05001567static void
1568iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1569{
1570 struct iscsi_conn *conn = cls_conn->dd_data;
1571
1572 iscsi_conn_stop(cls_conn, flag);
1573 iscsi_tcp_release_conn(conn);
1574}
1575
Mike Christie22236962007-05-30 12:57:24 -05001576static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
1577 char *buf, int *port,
1578 int (*getname)(struct socket *, struct sockaddr *,
1579 int *addrlen))
1580{
1581 struct sockaddr_storage *addr;
1582 struct sockaddr_in6 *sin6;
1583 struct sockaddr_in *sin;
1584 int rc = 0, len;
1585
Al Viroa9204872007-07-20 16:03:40 +01001586 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
Mike Christie22236962007-05-30 12:57:24 -05001587 if (!addr)
1588 return -ENOMEM;
1589
1590 if (getname(sock, (struct sockaddr *) addr, &len)) {
1591 rc = -ENODEV;
1592 goto free_addr;
1593 }
1594
1595 switch (addr->ss_family) {
1596 case AF_INET:
1597 sin = (struct sockaddr_in *)addr;
1598 spin_lock_bh(&conn->session->lock);
1599 sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
1600 *port = be16_to_cpu(sin->sin_port);
1601 spin_unlock_bh(&conn->session->lock);
1602 break;
1603 case AF_INET6:
1604 sin6 = (struct sockaddr_in6 *)addr;
1605 spin_lock_bh(&conn->session->lock);
1606 sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
1607 *port = be16_to_cpu(sin6->sin6_port);
1608 spin_unlock_bh(&conn->session->lock);
1609 break;
1610 }
1611free_addr:
1612 kfree(addr);
1613 return rc;
1614}
1615
Alex Aizman7ba24712005-08-04 19:30:08 -07001616static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001617iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001618 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001619 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001620{
Mike Christie5bb0b552006-04-06 21:26:46 -05001621 struct iscsi_conn *conn = cls_conn->dd_data;
1622 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001623 struct sock *sk;
1624 struct socket *sock;
1625 int err;
1626
1627 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001628 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001629 if (!sock) {
1630 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1631 return -EEXIST;
1632 }
Mike Christie22236962007-05-30 12:57:24 -05001633 /*
1634 * copy these values now because if we drop the session
1635 * userspace may still want to query the values since we will
1636 * be using them for the reconnect
1637 */
1638 err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1639 &conn->portal_port, kernel_getpeername);
1640 if (err)
1641 goto free_socket;
1642
1643 err = iscsi_tcp_get_addr(conn, sock, conn->local_address,
1644 &conn->local_port, kernel_getsockname);
1645 if (err)
1646 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001647
Mike Christie5bb0b552006-04-06 21:26:46 -05001648 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1649 if (err)
Mike Christie22236962007-05-30 12:57:24 -05001650 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001651
Mike Christie67a61112006-05-30 00:37:20 -05001652 /* bind iSCSI connection and socket */
1653 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001654
Mike Christie67a61112006-05-30 00:37:20 -05001655 /* setup Socket parameters */
1656 sk = sock->sk;
1657 sk->sk_reuse = 1;
1658 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1659 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001660
Mike Christie67a61112006-05-30 00:37:20 -05001661 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07001662
Mike Christie67a61112006-05-30 00:37:20 -05001663 /*
1664 * Intercept TCP callbacks for sendfile like receive
1665 * processing.
1666 */
1667 conn->recv_lock = &sk->sk_callback_lock;
1668 iscsi_conn_set_callbacks(conn);
1669 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1670 /*
1671 * set receive state machine into initial state
1672 */
Olaf Kirchda32dd62007-12-13 12:43:21 -06001673 iscsi_tcp_hdr_recv_prep(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001674 return 0;
Mike Christie22236962007-05-30 12:57:24 -05001675
1676free_socket:
1677 sockfd_put(sock);
1678 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001679}
1680
Mike Christie5bb0b552006-04-06 21:26:46 -05001681/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05001682static void
Olaf Kircha8ac6312007-12-13 12:43:35 -06001683iscsi_tcp_mtask_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Mike Christie30a6c652006-04-06 21:13:39 -05001684{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001685 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt);
1686
1687 /* Prepare PDU, optionally w/ immediate data */
1688 iscsi_tcp_send_hdr_prep(conn, mtask->hdr, sizeof(*mtask->hdr));
1689
1690 /* If we have immediate data, attach a payload */
1691 if (mtask->data_count)
1692 iscsi_tcp_send_linear_data_prepare(conn, mtask->data,
1693 mtask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001694}
1695
1696static int
1697iscsi_r2tpool_alloc(struct iscsi_session *session)
1698{
1699 int i;
1700 int cmd_i;
1701
1702 /*
1703 * initialize per-task: R2T pool and xmit queue
1704 */
1705 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1706 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05001707 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001708
1709 /*
1710 * pre-allocated x4 as much r2ts to handle race when
1711 * target acks DataOut faster than we data_xmit() queues
1712 * could replenish r2tqueue.
1713 */
1714
1715 /* R2T pool */
Olaf Kirch63203772007-12-13 12:43:25 -06001716 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4, NULL,
Mike Christie5bb0b552006-04-06 21:26:46 -05001717 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001718 goto r2t_alloc_fail;
1719 }
1720
1721 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05001722 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07001723 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05001724 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
Olaf Kirch63203772007-12-13 12:43:25 -06001725 iscsi_pool_free(&tcp_ctask->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001726 goto r2t_alloc_fail;
1727 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001728 }
1729
1730 return 0;
1731
1732r2t_alloc_fail:
1733 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001734 struct iscsi_cmd_task *ctask = session->cmds[i];
1735 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1736
Mike Christie5bb0b552006-04-06 21:26:46 -05001737 kfifo_free(tcp_ctask->r2tqueue);
Olaf Kirch63203772007-12-13 12:43:25 -06001738 iscsi_pool_free(&tcp_ctask->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001739 }
1740 return -ENOMEM;
1741}
1742
1743static void
1744iscsi_r2tpool_free(struct iscsi_session *session)
1745{
1746 int i;
1747
1748 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001749 struct iscsi_cmd_task *ctask = session->cmds[i];
1750 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1751
Mike Christie5bb0b552006-04-06 21:26:46 -05001752 kfifo_free(tcp_ctask->r2tqueue);
Olaf Kirch63203772007-12-13 12:43:25 -06001753 iscsi_pool_free(&tcp_ctask->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001754 }
1755}
1756
Alex Aizman7ba24712005-08-04 19:30:08 -07001757static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001758iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05001759 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07001760{
Mike Christie7b7232f2006-02-01 21:06:49 -06001761 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001762 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001763 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001764 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07001765
Alex Aizman7ba24712005-08-04 19:30:08 -07001766 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001767 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001768 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001769 break;
1770 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001771 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05001772 tcp_conn->sendpage = conn->datadgst_en ?
1773 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07001774 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07001775 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001776 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07001777 if (session->max_r2t == roundup_pow_of_two(value))
1778 break;
1779 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05001780 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001781 if (session->max_r2t & (session->max_r2t - 1))
1782 session->max_r2t = roundup_pow_of_two(session->max_r2t);
1783 if (iscsi_r2tpool_alloc(session))
1784 return -ENOMEM;
1785 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07001786 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001787 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001788 }
1789
1790 return 0;
1791}
1792
1793static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05001794iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
1795 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06001796{
Mike Christie7b7232f2006-02-01 21:06:49 -06001797 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001798 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06001799
1800 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05001801 case ISCSI_PARAM_CONN_PORT:
Mike Christie22236962007-05-30 12:57:24 -05001802 spin_lock_bh(&conn->session->lock);
1803 len = sprintf(buf, "%hu\n", conn->portal_port);
1804 spin_unlock_bh(&conn->session->lock);
Mike Christie8d2860b2006-05-02 19:46:47 -05001805 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001806 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie22236962007-05-30 12:57:24 -05001807 spin_lock_bh(&conn->session->lock);
1808 len = sprintf(buf, "%s\n", conn->portal_address);
1809 spin_unlock_bh(&conn->session->lock);
Mike Christiefd7255f2006-04-06 21:13:36 -05001810 break;
1811 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001812 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05001813 }
1814
1815 return len;
1816}
1817
Mike Christie22236962007-05-30 12:57:24 -05001818static int
1819iscsi_tcp_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
1820 char *buf)
1821{
1822 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1823 int len;
1824
1825 switch (param) {
1826 case ISCSI_HOST_PARAM_IPADDRESS:
1827 spin_lock_bh(&session->lock);
1828 if (!session->leadconn)
1829 len = -ENODEV;
1830 else
1831 len = sprintf(buf, "%s\n",
1832 session->leadconn->local_address);
1833 spin_unlock_bh(&session->lock);
1834 break;
1835 default:
1836 return iscsi_host_get_param(shost, param, buf);
1837 }
1838 return len;
1839}
1840
Alex Aizman7ba24712005-08-04 19:30:08 -07001841static void
Mike Christie7b7232f2006-02-01 21:06:49 -06001842iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07001843{
Mike Christie7b7232f2006-02-01 21:06:49 -06001844 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001845 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001846
1847 stats->txdata_octets = conn->txdata_octets;
1848 stats->rxdata_octets = conn->rxdata_octets;
1849 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1850 stats->dataout_pdus = conn->dataout_pdus_cnt;
1851 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1852 stats->datain_pdus = conn->datain_pdus_cnt;
1853 stats->r2t_pdus = conn->r2t_pdus_cnt;
1854 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1855 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1856 stats->custom_length = 3;
1857 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05001858 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001859 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05001860 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001861 strcpy(stats->custom[2].desc, "eh_abort_cnt");
1862 stats->custom[2].value = conn->eh_abort_cnt;
1863}
1864
Mike Christie5bb0b552006-04-06 21:26:46 -05001865static struct iscsi_cls_session *
1866iscsi_tcp_session_create(struct iscsi_transport *iscsit,
1867 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05001868 uint16_t cmds_max, uint16_t qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05001869 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07001870{
Mike Christie5bb0b552006-04-06 21:26:46 -05001871 struct iscsi_cls_session *cls_session;
1872 struct iscsi_session *session;
1873 uint32_t hn;
1874 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07001875
Mike Christie15482712007-05-30 12:57:19 -05001876 cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05001877 sizeof(struct iscsi_tcp_cmd_task),
1878 sizeof(struct iscsi_tcp_mgmt_task),
1879 initial_cmdsn, &hn);
1880 if (!cls_session)
1881 return NULL;
1882 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001883
Mike Christie5bb0b552006-04-06 21:26:46 -05001884 session = class_to_transport_session(cls_session);
1885 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1886 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1887 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1888
Boaz Harrosh004d6532007-12-13 12:43:23 -06001889 ctask->hdr = &tcp_ctask->hdr.cmd_hdr;
1890 ctask->hdr_max = sizeof(tcp_ctask->hdr) - ISCSI_DIGEST_SIZE;
Mike Christie5bb0b552006-04-06 21:26:46 -05001891 }
1892
1893 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1894 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1895 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
1896
Olaf Kircha8ac6312007-12-13 12:43:35 -06001897 mtask->hdr = (struct iscsi_hdr *) &tcp_mtask->hdr;
Mike Christie5bb0b552006-04-06 21:26:46 -05001898 }
1899
1900 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
1901 goto r2tpool_alloc_fail;
1902
1903 return cls_session;
1904
1905r2tpool_alloc_fail:
1906 iscsi_session_teardown(cls_session);
1907 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001908}
1909
Mike Christie5bb0b552006-04-06 21:26:46 -05001910static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
1911{
Mike Christie5bb0b552006-04-06 21:26:46 -05001912 iscsi_r2tpool_free(class_to_transport_session(cls_session));
1913 iscsi_session_teardown(cls_session);
1914}
1915
Mike Christied1d81c02007-05-30 12:57:21 -05001916static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
1917{
Mike Christieb6d44fe2007-07-26 12:46:47 -05001918 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
Mike Christied1d81c02007-05-30 12:57:21 -05001919 blk_queue_dma_alignment(sdev->request_queue, 0);
1920 return 0;
1921}
1922
Mike Christie5bb0b552006-04-06 21:26:46 -05001923static struct scsi_host_template iscsi_sht = {
Mike Christie79743922007-07-26 12:46:46 -05001924 .module = THIS_MODULE,
Mike Christief4246b32006-07-24 15:47:54 -05001925 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05001926 .queuecommand = iscsi_queuecommand,
1927 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -05001928 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie66bbe0c2007-12-13 12:43:39 -06001929 .sg_tablesize = 4096,
Mike Christie8231f0e2007-02-28 17:32:20 -06001930 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -05001931 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
1932 .eh_abort_handler = iscsi_eh_abort,
Mike Christie843c0a82007-12-13 12:43:20 -06001933 .eh_device_reset_handler= iscsi_eh_device_reset,
Mike Christie5bb0b552006-04-06 21:26:46 -05001934 .eh_host_reset_handler = iscsi_eh_host_reset,
1935 .use_clustering = DISABLE_CLUSTERING,
Mike Christie66bbe0c2007-12-13 12:43:39 -06001936 .use_sg_chaining = ENABLE_SG_CHAINING,
Mike Christied1d81c02007-05-30 12:57:21 -05001937 .slave_configure = iscsi_tcp_slave_configure,
Mike Christie5bb0b552006-04-06 21:26:46 -05001938 .proc_name = "iscsi_tcp",
1939 .this_id = -1,
1940};
1941
Alex Aizman7ba24712005-08-04 19:30:08 -07001942static struct iscsi_transport iscsi_tcp_transport = {
1943 .owner = THIS_MODULE,
1944 .name = "tcp",
1945 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
1946 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05001947 .param_mask = ISCSI_MAX_RECV_DLENGTH |
1948 ISCSI_MAX_XMIT_DLENGTH |
1949 ISCSI_HDRDGST_EN |
1950 ISCSI_DATADGST_EN |
1951 ISCSI_INITIAL_R2T_EN |
1952 ISCSI_MAX_R2T |
1953 ISCSI_IMM_DATA_EN |
1954 ISCSI_FIRST_BURST |
1955 ISCSI_MAX_BURST |
1956 ISCSI_PDU_INORDER_EN |
1957 ISCSI_DATASEQ_INORDER_EN |
1958 ISCSI_ERL |
1959 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05001960 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05001961 ISCSI_EXP_STATSN |
1962 ISCSI_PERSISTENT_PORT |
1963 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -05001964 ISCSI_TARGET_NAME | ISCSI_TPGT |
1965 ISCSI_USERNAME | ISCSI_PASSWORD |
Mike Christie843c0a82007-12-13 12:43:20 -06001966 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
Mike Christief6d51802007-12-13 12:43:30 -06001967 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
1968 ISCSI_LU_RESET_TMO |
1969 ISCSI_PING_TMO | ISCSI_RECV_TMO,
Mike Christie22236962007-05-30 12:57:24 -05001970 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
Mike Christied8196ed2007-05-30 12:57:25 -05001971 ISCSI_HOST_INITIATOR_NAME |
1972 ISCSI_HOST_NETDEV_NAME,
Alex Aizman7ba24712005-08-04 19:30:08 -07001973 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06001974 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07001975 .max_conn = 1,
Mike Christie66bbe0c2007-12-13 12:43:39 -06001976 .max_cmd_len = 16,
Mike Christie5bb0b552006-04-06 21:26:46 -05001977 /* session management */
1978 .create_session = iscsi_tcp_session_create,
1979 .destroy_session = iscsi_tcp_session_destroy,
1980 /* connection management */
1981 .create_conn = iscsi_tcp_conn_create,
1982 .bind_conn = iscsi_tcp_conn_bind,
1983 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07001984 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05001985 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06001986 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07001987 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05001988 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -05001989 /* iscsi host params */
Mike Christie22236962007-05-30 12:57:24 -05001990 .get_host_param = iscsi_tcp_host_get_param,
Mike Christie0801c242007-05-30 12:57:12 -05001991 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -05001992 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07001993 .send_pdu = iscsi_conn_send_pdu,
1994 .get_stats = iscsi_conn_get_stats,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001995 .init_cmd_task = iscsi_tcp_ctask_init,
1996 .init_mgmt_task = iscsi_tcp_mtask_init,
Mike Christie5bb0b552006-04-06 21:26:46 -05001997 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
1998 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
1999 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2000 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002001 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002002};
2003
2004static int __init
2005iscsi_tcp_init(void)
2006{
Alex Aizman7ba24712005-08-04 19:30:08 -07002007 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002008 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2009 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002010 return -EINVAL;
2011 }
2012 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2013
Mike Christie7b8631b2006-01-13 18:05:50 -06002014 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002015 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002016
Mike Christie7b8631b2006-01-13 18:05:50 -06002017 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002018}
2019
2020static void __exit
2021iscsi_tcp_exit(void)
2022{
2023 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002024}
2025
2026module_init(iscsi_tcp_init);
2027module_exit(iscsi_tcp_exit);