blob: 97f3f68aeb6474a145da6ac026d9fadad3b7ed7b [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
Mike Christie75613522008-05-21 15:53:59 -050060static struct scsi_transport_template *iscsi_tcp_scsi_transport;
61static struct scsi_host_template iscsi_sht;
62static struct iscsi_transport iscsi_tcp_transport;
63
Alex Aizman7ba24712005-08-04 19:30:08 -070064static unsigned int iscsi_max_lun = 512;
65module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
66
Olaf Kirchda32dd62007-12-13 12:43:21 -060067static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -060068 struct iscsi_segment *segment);
Alex Aizman7ba24712005-08-04 19:30:08 -070069
Olaf Kirchda32dd62007-12-13 12:43:21 -060070/*
Olaf Kircha8ac6312007-12-13 12:43:35 -060071 * Scatterlist handling: inside the iscsi_segment, we
Olaf Kirchda32dd62007-12-13 12:43:21 -060072 * remember an index into the scatterlist, and set data/size
73 * to the current scatterlist entry. For highmem pages, we
74 * kmap as needed.
75 *
76 * Note that the page is unmapped when we return from
77 * TCP's data_ready handler, so we may end up mapping and
78 * unmapping the same page repeatedly. The whole reason
79 * for this is that we shouldn't keep the page mapped
80 * outside the softirq.
81 */
82
83/**
Olaf Kircha8ac6312007-12-13 12:43:35 -060084 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
85 * @segment: the buffer object
86 * @sg: scatterlist
Olaf Kirchda32dd62007-12-13 12:43:21 -060087 * @offset: byte offset into that sg entry
88 *
Olaf Kircha8ac6312007-12-13 12:43:35 -060089 * This function sets up the segment so that subsequent
Olaf Kirchda32dd62007-12-13 12:43:21 -060090 * data is copied to the indicated sg entry, at the given
91 * offset.
92 */
93static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -060094iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
95 struct scatterlist *sg, unsigned int offset)
Alex Aizman7ba24712005-08-04 19:30:08 -070096{
Olaf Kircha8ac6312007-12-13 12:43:35 -060097 segment->sg = sg;
98 segment->sg_offset = offset;
99 segment->size = min(sg->length - offset,
100 segment->total_size - segment->total_copied);
101 segment->data = NULL;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600102}
Alex Aizman7ba24712005-08-04 19:30:08 -0700103
Olaf Kirchda32dd62007-12-13 12:43:21 -0600104/**
Olaf Kircha8ac6312007-12-13 12:43:35 -0600105 * iscsi_tcp_segment_map - map the current S/G page
106 * @segment: iscsi_segment
107 * @recv: 1 if called from recv path
Olaf Kirchda32dd62007-12-13 12:43:21 -0600108 *
109 * We only need to possibly kmap data if scatter lists are being used,
110 * because the iscsi passthrough and internal IO paths will never use high
111 * mem pages.
112 */
113static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600114iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600115{
116 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700117
Olaf Kircha8ac6312007-12-13 12:43:35 -0600118 if (segment->data != NULL || !segment->sg)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600119 return;
Alex Aizman7ba24712005-08-04 19:30:08 -0700120
Olaf Kircha8ac6312007-12-13 12:43:35 -0600121 sg = segment->sg;
122 BUG_ON(segment->sg_mapped);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600123 BUG_ON(sg->length == 0);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600124
125 /*
126 * If the page count is greater than one it is ok to send
127 * to the network layer's zero copy send path. If not we
128 * have to go the slow sendmsg path. We always map for the
129 * recv path.
130 */
131 if (page_count(sg_page(sg)) >= 1 && !recv)
132 return;
133
134 debug_tcp("iscsi_tcp_segment_map %s %p\n", recv ? "recv" : "xmit",
135 segment);
136 segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0);
137 segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600138}
Alex Aizman7ba24712005-08-04 19:30:08 -0700139
Olaf Kirchda32dd62007-12-13 12:43:21 -0600140static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600141iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600142{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600143 debug_tcp("iscsi_tcp_segment_unmap %p\n", segment);
144
145 if (segment->sg_mapped) {
146 debug_tcp("iscsi_tcp_segment_unmap valid\n");
147 kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0);
148 segment->sg_mapped = NULL;
149 segment->data = NULL;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600150 }
151}
Alex Aizman7ba24712005-08-04 19:30:08 -0700152
Olaf Kirchda32dd62007-12-13 12:43:21 -0600153/*
154 * Splice the digest buffer into the buffer
155 */
156static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600157iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600158{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600159 segment->data = digest;
160 segment->digest_len = ISCSI_DIGEST_SIZE;
161 segment->total_size += ISCSI_DIGEST_SIZE;
162 segment->size = ISCSI_DIGEST_SIZE;
163 segment->copied = 0;
164 segment->sg = NULL;
165 segment->hash = NULL;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600166}
Alex Aizman7ba24712005-08-04 19:30:08 -0700167
Olaf Kirchda32dd62007-12-13 12:43:21 -0600168/**
Olaf Kircha8ac6312007-12-13 12:43:35 -0600169 * iscsi_tcp_segment_done - check whether the segment is complete
170 * @segment: iscsi segment to check
171 * @recv: set to one of this is called from the recv path
172 * @copied: number of bytes copied
Olaf Kirchda32dd62007-12-13 12:43:21 -0600173 *
Olaf Kircha8ac6312007-12-13 12:43:35 -0600174 * Check if we're done receiving this segment. If the receive
Olaf Kirchda32dd62007-12-13 12:43:21 -0600175 * buffer is full but we expect more data, move on to the
176 * next entry in the scatterlist.
177 *
178 * If the amount of data we received isn't a multiple of 4,
179 * we will transparently receive the pad bytes, too.
180 *
181 * This function must be re-entrant.
182 */
183static inline int
Olaf Kircha8ac6312007-12-13 12:43:35 -0600184iscsi_tcp_segment_done(struct iscsi_segment *segment, int recv, unsigned copied)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600185{
186 static unsigned char padbuf[ISCSI_PAD_LEN];
Olaf Kircha8ac6312007-12-13 12:43:35 -0600187 struct scatterlist sg;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600188 unsigned int pad;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600189
Olaf Kircha8ac6312007-12-13 12:43:35 -0600190 debug_tcp("copied %u %u size %u %s\n", segment->copied, copied,
191 segment->size, recv ? "recv" : "xmit");
192 if (segment->hash && copied) {
193 /*
194 * If a segment is kmapd we must unmap it before sending
195 * to the crypto layer since that will try to kmap it again.
196 */
197 iscsi_tcp_segment_unmap(segment);
198
199 if (!segment->data) {
200 sg_init_table(&sg, 1);
201 sg_set_page(&sg, sg_page(segment->sg), copied,
202 segment->copied + segment->sg_offset +
203 segment->sg->offset);
204 } else
205 sg_init_one(&sg, segment->data + segment->copied,
206 copied);
207 crypto_hash_update(segment->hash, &sg, copied);
208 }
209
210 segment->copied += copied;
211 if (segment->copied < segment->size) {
212 iscsi_tcp_segment_map(segment, recv);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600213 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700214 }
215
Olaf Kircha8ac6312007-12-13 12:43:35 -0600216 segment->total_copied += segment->copied;
217 segment->copied = 0;
218 segment->size = 0;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600219
220 /* Unmap the current scatterlist page, if there is one. */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600221 iscsi_tcp_segment_unmap(segment);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600222
223 /* Do we have more scatterlist entries? */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600224 debug_tcp("total copied %u total size %u\n", segment->total_copied,
225 segment->total_size);
226 if (segment->total_copied < segment->total_size) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600227 /* Proceed to the next entry in the scatterlist. */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600228 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
229 0);
230 iscsi_tcp_segment_map(segment, recv);
231 BUG_ON(segment->size == 0);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600232 return 0;
233 }
234
235 /* Do we need to handle padding? */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600236 pad = iscsi_padding(segment->total_copied);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600237 if (pad != 0) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600238 debug_tcp("consume %d pad bytes\n", pad);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600239 segment->total_size += pad;
240 segment->size = pad;
241 segment->data = padbuf;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600242 return 0;
243 }
244
245 /*
Olaf Kircha8ac6312007-12-13 12:43:35 -0600246 * Set us up for transferring the data digest. hdr digest
Olaf Kirchda32dd62007-12-13 12:43:21 -0600247 * is completely handled in hdr done function.
248 */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600249 if (segment->hash) {
250 crypto_hash_final(segment->hash, segment->digest);
251 iscsi_tcp_segment_splice_digest(segment,
252 recv ? segment->recv_digest : segment->digest);
253 return 0;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600254 }
255
256 return 1;
257}
258
259/**
Olaf Kircha8ac6312007-12-13 12:43:35 -0600260 * iscsi_tcp_xmit_segment - transmit segment
Olaf Kirchda32dd62007-12-13 12:43:21 -0600261 * @tcp_conn: the iSCSI TCP connection
Olaf Kircha8ac6312007-12-13 12:43:35 -0600262 * @segment: the buffer to transmnit
263 *
264 * This function transmits as much of the buffer as
265 * the network layer will accept, and returns the number of
266 * bytes transmitted.
267 *
268 * If CRC hashing is enabled, the function will compute the
269 * hash as it goes. When the entire segment has been transmitted,
270 * it will retrieve the hash value and send it as well.
271 */
272static int
273iscsi_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
274 struct iscsi_segment *segment)
275{
276 struct socket *sk = tcp_conn->sock;
277 unsigned int copied = 0;
278 int r = 0;
279
280 while (!iscsi_tcp_segment_done(segment, 0, r)) {
281 struct scatterlist *sg;
282 unsigned int offset, copy;
283 int flags = 0;
284
285 r = 0;
286 offset = segment->copied;
287 copy = segment->size - offset;
288
289 if (segment->total_copied + segment->size < segment->total_size)
290 flags |= MSG_MORE;
291
292 /* Use sendpage if we can; else fall back to sendmsg */
293 if (!segment->data) {
294 sg = segment->sg;
295 offset += segment->sg_offset + sg->offset;
296 r = tcp_conn->sendpage(sk, sg_page(sg), offset, copy,
297 flags);
298 } else {
299 struct msghdr msg = { .msg_flags = flags };
300 struct kvec iov = {
301 .iov_base = segment->data + offset,
302 .iov_len = copy
303 };
304
305 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
306 }
307
308 if (r < 0) {
309 iscsi_tcp_segment_unmap(segment);
310 if (copied || r == -EAGAIN)
311 break;
312 return r;
313 }
314 copied += r;
315 }
316 return copied;
317}
318
319/**
320 * iscsi_tcp_segment_recv - copy data to segment
321 * @tcp_conn: the iSCSI TCP connection
322 * @segment: the buffer to copy to
Olaf Kirchda32dd62007-12-13 12:43:21 -0600323 * @ptr: data pointer
324 * @len: amount of data available
325 *
326 * This function copies up to @len bytes to the
327 * given buffer, and returns the number of bytes
328 * consumed, which can actually be less than @len.
329 *
330 * If hash digest is enabled, the function will update the
331 * hash while copying.
332 * Combining these two operations doesn't buy us a lot (yet),
333 * but in the future we could implement combined copy+crc,
334 * just way we do for network layer checksums.
335 */
336static int
Olaf Kircha8ac6312007-12-13 12:43:35 -0600337iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
338 struct iscsi_segment *segment, const void *ptr,
339 unsigned int len)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600340{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600341 unsigned int copy = 0, copied = 0;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600342
Olaf Kircha8ac6312007-12-13 12:43:35 -0600343 while (!iscsi_tcp_segment_done(segment, 1, copy)) {
344 if (copied == len) {
345 debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n",
346 len);
347 break;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600348 }
Olaf Kircha8ac6312007-12-13 12:43:35 -0600349
350 copy = min(len - copied, segment->size - segment->copied);
351 debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy);
352 memcpy(segment->data + segment->copied, ptr + copied, copy);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600353 copied += copy;
354 }
Olaf Kirchda32dd62007-12-13 12:43:21 -0600355 return copied;
356}
357
358static inline void
359iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
360 unsigned char digest[ISCSI_DIGEST_SIZE])
361{
362 struct scatterlist sg;
363
364 sg_init_one(&sg, hdr, hdrlen);
365 crypto_hash_digest(hash, &sg, hdrlen, digest);
366}
367
368static inline int
369iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600370 struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600371{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600372 if (!segment->digest_len)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600373 return 1;
374
Olaf Kircha8ac6312007-12-13 12:43:35 -0600375 if (memcmp(segment->recv_digest, segment->digest,
376 segment->digest_len)) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600377 debug_scsi("digest mismatch\n");
378 return 0;
379 }
380
381 return 1;
382}
383
384/*
Olaf Kircha8ac6312007-12-13 12:43:35 -0600385 * Helper function to set up segment buffer
Olaf Kirchda32dd62007-12-13 12:43:21 -0600386 */
387static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600388__iscsi_segment_init(struct iscsi_segment *segment, size_t size,
389 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600390{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600391 memset(segment, 0, sizeof(*segment));
392 segment->total_size = size;
393 segment->done = done;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600394
395 if (hash) {
Olaf Kircha8ac6312007-12-13 12:43:35 -0600396 segment->hash = hash;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600397 crypto_hash_init(hash);
398 }
399}
400
401static inline void
Olaf Kircha8ac6312007-12-13 12:43:35 -0600402iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
403 size_t size, iscsi_segment_done_fn_t *done,
404 struct hash_desc *hash)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600405{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600406 __iscsi_segment_init(segment, size, done, hash);
407 segment->data = data;
408 segment->size = size;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600409}
410
411static inline int
Olaf Kircha8ac6312007-12-13 12:43:35 -0600412iscsi_segment_seek_sg(struct iscsi_segment *segment,
413 struct scatterlist *sg_list, unsigned int sg_count,
414 unsigned int offset, size_t size,
415 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600416{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600417 struct scatterlist *sg;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600418 unsigned int i;
419
Olaf Kircha8ac6312007-12-13 12:43:35 -0600420 debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n",
421 offset, size);
422 __iscsi_segment_init(segment, size, done, hash);
423 for_each_sg(sg_list, sg, sg_count, i) {
424 debug_scsi("sg %d, len %u offset %u\n", i, sg->length,
425 sg->offset);
426 if (offset < sg->length) {
427 iscsi_tcp_segment_init_sg(segment, sg, offset);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600428 return 0;
429 }
Olaf Kircha8ac6312007-12-13 12:43:35 -0600430 offset -= sg->length;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600431 }
432
433 return ISCSI_ERR_DATA_OFFSET;
434}
435
436/**
Olaf Kircha8ac6312007-12-13 12:43:35 -0600437 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
Olaf Kirchda32dd62007-12-13 12:43:21 -0600438 * @tcp_conn: iscsi connection to prep for
439 *
440 * This function always passes NULL for the hash argument, because when this
441 * function is called we do not yet know the final size of the header and want
442 * to delay the digest processing until we know that.
443 */
444static void
445iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
446{
447 debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn,
448 tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : "");
Olaf Kircha8ac6312007-12-13 12:43:35 -0600449 iscsi_segment_init_linear(&tcp_conn->in.segment,
Olaf Kirchda32dd62007-12-13 12:43:21 -0600450 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
451 iscsi_tcp_hdr_recv_done, NULL);
452}
453
454/*
455 * Handle incoming reply to any other type of command
456 */
457static int
458iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600459 struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600460{
461 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
462 int rc = 0;
463
Olaf Kircha8ac6312007-12-13 12:43:35 -0600464 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600465 return ISCSI_ERR_DATA_DGST;
466
467 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
468 conn->data, tcp_conn->in.datalen);
469 if (rc)
470 return rc;
471
472 iscsi_tcp_hdr_recv_prep(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700473 return 0;
474}
475
Olaf Kirchda32dd62007-12-13 12:43:21 -0600476static void
477iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
478{
479 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
480 struct hash_desc *rx_hash = NULL;
481
Mike Christie63c62f12008-12-02 00:32:04 -0600482 if (conn->datadgst_en &
483 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600484 rx_hash = &tcp_conn->rx_hash;
485
Olaf Kircha8ac6312007-12-13 12:43:35 -0600486 iscsi_segment_init_linear(&tcp_conn->in.segment,
Olaf Kirchda32dd62007-12-13 12:43:21 -0600487 conn->data, tcp_conn->in.datalen,
488 iscsi_tcp_data_recv_done, rx_hash);
489}
490
Mike Christie30a6c652006-04-06 21:13:39 -0500491/*
492 * must be called with session lock
493 */
Mike Christiee5a7efe2008-12-02 00:32:07 -0600494static void iscsi_tcp_cleanup_task(struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -0700495{
Mike Christie135a8ad2008-05-21 15:54:10 -0500496 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500497 struct iscsi_r2t_info *r2t;
Alex Aizman7ba24712005-08-04 19:30:08 -0700498
Mike Christiee5a7efe2008-12-02 00:32:07 -0600499 /* nothing to do for mgmt or pending tasks */
500 if (!task->sc || task->state == ISCSI_TASK_PENDING)
Mike Christiefbc514b2008-05-21 15:54:07 -0500501 return;
502
Mike Christie135a8ad2008-05-21 15:54:10 -0500503 /* flush task's r2t queues */
504 while (__kfifo_get(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
505 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christieb6c395e2006-07-24 15:47:15 -0500506 sizeof(void*));
Mike Christie135a8ad2008-05-21 15:54:10 -0500507 debug_scsi("iscsi_tcp_cleanup_task pending r2t dropped\n");
Mike Christieb6c395e2006-07-24 15:47:15 -0500508 }
509
Mike Christie135a8ad2008-05-21 15:54:10 -0500510 r2t = tcp_task->r2t;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600511 if (r2t != NULL) {
Mike Christie135a8ad2008-05-21 15:54:10 -0500512 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600513 sizeof(void*));
Mike Christie135a8ad2008-05-21 15:54:10 -0500514 tcp_task->r2t = NULL;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600515 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700516}
517
518/**
Mike Christie30b49152008-12-02 00:32:10 -0600519 * iscsi_tcp_data_in - SCSI Data-In Response processing
Alex Aizman7ba24712005-08-04 19:30:08 -0700520 * @conn: iscsi connection
Mike Christie135a8ad2008-05-21 15:54:10 -0500521 * @task: scsi command task
Mike Christie30b49152008-12-02 00:32:10 -0600522 */
523static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -0700524{
Mike Christie5bb0b552006-04-06 21:26:46 -0500525 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie135a8ad2008-05-21 15:54:10 -0500526 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500527 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700528 int datasn = be32_to_cpu(rhdr->datasn);
Mike Christie1d9edf02008-09-24 11:46:09 -0500529 unsigned total_in_length = scsi_in(task->sc)->length;
Alex Aizman7ba24712005-08-04 19:30:08 -0700530
Mike Christie1d9edf02008-09-24 11:46:09 -0500531 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
Mike Christie5bb0b552006-04-06 21:26:46 -0500532 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700533 return 0;
534
Mike Christie135a8ad2008-05-21 15:54:10 -0500535 if (tcp_task->exp_datasn != datasn) {
536 debug_tcp("%s: task->exp_datasn(%d) != rhdr->datasn(%d)\n",
Harvey Harrison3a12b192008-05-21 15:54:19 -0500537 __func__, tcp_task->exp_datasn, datasn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700538 return ISCSI_ERR_DATASN;
Mike Christied473cc72007-05-30 12:57:14 -0500539 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700540
Mike Christie135a8ad2008-05-21 15:54:10 -0500541 tcp_task->exp_datasn++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700542
Mike Christie135a8ad2008-05-21 15:54:10 -0500543 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
544 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
Mike Christie857ae0b2007-05-30 12:57:15 -0500545 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
Harvey Harrison3a12b192008-05-21 15:54:19 -0500546 __func__, tcp_task->data_offset,
Boaz Harrosh94795b62008-04-18 10:11:53 -0500547 tcp_conn->in.datalen, total_in_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700548 return ISCSI_ERR_DATA_OFFSET;
Mike Christie857ae0b2007-05-30 12:57:15 -0500549 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700550
Alex Aizman7ba24712005-08-04 19:30:08 -0700551 conn->datain_pdus_cnt++;
552 return 0;
553}
554
555/**
Mike Christie30b49152008-12-02 00:32:10 -0600556 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
Alex Aizman7ba24712005-08-04 19:30:08 -0700557 * @conn: iscsi connection
Mike Christie135a8ad2008-05-21 15:54:10 -0500558 * @task: scsi command task
Mike Christie30b49152008-12-02 00:32:10 -0600559 */
560static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -0700561{
Alex Aizman7ba24712005-08-04 19:30:08 -0700562 struct iscsi_session *session = conn->session;
Mike Christie135a8ad2008-05-21 15:54:10 -0500563 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500564 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
565 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Mike Christiee5a7efe2008-12-02 00:32:07 -0600566 struct iscsi_r2t_info *r2t;
Alex Aizman7ba24712005-08-04 19:30:08 -0700567 int r2tsn = be32_to_cpu(rhdr->r2tsn);
568 int rc;
569
Mike Christie98a94162006-08-31 18:09:26 -0400570 if (tcp_conn->in.datalen) {
Mike Christie322d7392008-01-31 13:36:52 -0600571 iscsi_conn_printk(KERN_ERR, conn,
572 "invalid R2t with datalen %d\n",
573 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700574 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400575 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700576
Mike Christie135a8ad2008-05-21 15:54:10 -0500577 if (tcp_task->exp_datasn != r2tsn){
578 debug_tcp("%s: task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
Harvey Harrison3a12b192008-05-21 15:54:19 -0500579 __func__, tcp_task->exp_datasn, r2tsn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700580 return ISCSI_ERR_R2TSN;
Mike Christied473cc72007-05-30 12:57:14 -0500581 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700582
Mike Christie135a8ad2008-05-21 15:54:10 -0500583 /* fill-in new R2T associated with the task */
Mike Christie77a23c22007-05-30 12:57:18 -0500584 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
585
Mike Christie135a8ad2008-05-21 15:54:10 -0500586 if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie322d7392008-01-31 13:36:52 -0600587 iscsi_conn_printk(KERN_INFO, conn,
588 "dropping R2T itt %d in recovery.\n",
Mike Christie135a8ad2008-05-21 15:54:10 -0500589 task->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -0700590 return 0;
591 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500592
Mike Christie135a8ad2008-05-21 15:54:10 -0500593 rc = __kfifo_get(tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*));
Mike Christiee5a7efe2008-12-02 00:32:07 -0600594 if (!rc) {
595 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
596 "Target has sent more R2Ts than it "
597 "negotiated for or driver has has leaked.\n");
598 return ISCSI_ERR_PROTO;
599 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700600
601 r2t->exp_statsn = rhdr->statsn;
602 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400603 if (r2t->data_length == 0) {
Mike Christie322d7392008-01-31 13:36:52 -0600604 iscsi_conn_printk(KERN_ERR, conn,
605 "invalid R2T with zero data len\n");
Mike Christie135a8ad2008-05-21 15:54:10 -0500606 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
Olaf Kirch03766a12007-12-13 12:43:36 -0600607 sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700608 return ISCSI_ERR_DATALEN;
609 }
610
Mike Christie98a94162006-08-31 18:09:26 -0400611 if (r2t->data_length > session->max_burst)
612 debug_scsi("invalid R2T with data len %u and max burst %u."
613 "Attempting to execute request.\n",
614 r2t->data_length, session->max_burst);
615
Alex Aizman7ba24712005-08-04 19:30:08 -0700616 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
Mike Christie135a8ad2008-05-21 15:54:10 -0500617 if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) {
Mike Christie322d7392008-01-31 13:36:52 -0600618 iscsi_conn_printk(KERN_ERR, conn,
619 "invalid R2T with data len %u at offset %u "
620 "and total length %d\n", r2t->data_length,
Mike Christie135a8ad2008-05-21 15:54:10 -0500621 r2t->data_offset, scsi_out(task->sc)->length);
622 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
Olaf Kirch03766a12007-12-13 12:43:36 -0600623 sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700624 return ISCSI_ERR_DATALEN;
625 }
626
627 r2t->ttt = rhdr->ttt; /* no flip */
Mike Christiee5a7efe2008-12-02 00:32:07 -0600628 r2t->datasn = 0;
629 r2t->sent = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700630
Mike Christie135a8ad2008-05-21 15:54:10 -0500631 tcp_task->exp_datasn = r2tsn + 1;
632 __kfifo_put(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700633 conn->r2t_pdus_cnt++;
Mike Christie843c0a82007-12-13 12:43:20 -0600634
Mike Christie135a8ad2008-05-21 15:54:10 -0500635 iscsi_requeue_task(task);
Alex Aizman7ba24712005-08-04 19:30:08 -0700636 return 0;
637}
638
Olaf Kirchda32dd62007-12-13 12:43:21 -0600639/*
640 * Handle incoming reply to DataIn command
641 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700642static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600643iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600644 struct iscsi_segment *segment)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600645{
646 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
647 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
648 int rc;
649
Olaf Kircha8ac6312007-12-13 12:43:35 -0600650 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600651 return ISCSI_ERR_DATA_DGST;
652
653 /* check for non-exceptional status */
654 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
655 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
656 if (rc)
657 return rc;
658 }
659
660 iscsi_tcp_hdr_recv_prep(tcp_conn);
661 return 0;
662}
663
664/**
665 * iscsi_tcp_hdr_dissect - process PDU header
666 * @conn: iSCSI connection
667 * @hdr: PDU header
668 *
669 * This function analyzes the header of the PDU received,
670 * and performs several sanity checks. If the PDU is accompanied
671 * by data, the receive buffer is set up to copy the incoming data
672 * to the correct location.
673 */
674static int
675iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
Alex Aizman7ba24712005-08-04 19:30:08 -0700676{
Mike Christie5bb0b552006-04-06 21:26:46 -0500677 int rc = 0, opcode, ahslen;
Mike Christie5bb0b552006-04-06 21:26:46 -0500678 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie135a8ad2008-05-21 15:54:10 -0500679 struct iscsi_task *task;
Alex Aizman7ba24712005-08-04 19:30:08 -0700680
681 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500682 tcp_conn->in.datalen = ntoh24(hdr->dlength);
683 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Mike Christie322d7392008-01-31 13:36:52 -0600684 iscsi_conn_printk(KERN_ERR, conn,
685 "iscsi_tcp: datalen %d > %d\n",
686 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700687 return ISCSI_ERR_DATALEN;
688 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700689
Olaf Kirchda32dd62007-12-13 12:43:21 -0600690 /* Additional header segments. So far, we don't
691 * process additional headers.
692 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500693 ahslen = hdr->hlength << 2;
Alex Aizman7ba24712005-08-04 19:30:08 -0700694
Mike Christie5bb0b552006-04-06 21:26:46 -0500695 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700696 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie0af967f2008-05-21 15:54:04 -0500697 rc = iscsi_verify_itt(conn, hdr->itt);
Mike Christie7a53dc52007-12-13 12:43:37 -0600698 if (rc)
Mike Christie5bb0b552006-04-06 21:26:46 -0500699 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700700
Olaf Kirchda32dd62007-12-13 12:43:21 -0600701 debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
702 opcode, ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700703
Mike Christie5bb0b552006-04-06 21:26:46 -0500704 switch(opcode) {
705 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie913e5bf2008-05-21 15:54:18 -0500706 spin_lock(&conn->session->lock);
Mike Christie135a8ad2008-05-21 15:54:10 -0500707 task = iscsi_itt_to_ctask(conn, hdr->itt);
708 if (!task)
Mike Christie913e5bf2008-05-21 15:54:18 -0500709 rc = ISCSI_ERR_BAD_ITT;
710 else
Mike Christie30b49152008-12-02 00:32:10 -0600711 rc = iscsi_tcp_data_in(conn, task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500712 if (rc) {
713 spin_unlock(&conn->session->lock);
714 break;
715 }
Mike Christie0af967f2008-05-21 15:54:04 -0500716
Olaf Kirchda32dd62007-12-13 12:43:21 -0600717 if (tcp_conn->in.datalen) {
Mike Christie135a8ad2008-05-21 15:54:10 -0500718 struct iscsi_tcp_task *tcp_task = task->dd_data;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600719 struct hash_desc *rx_hash = NULL;
Mike Christie135a8ad2008-05-21 15:54:10 -0500720 struct scsi_data_buffer *sdb = scsi_in(task->sc);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600721
722 /*
723 * Setup copy of Data-In into the Scsi_Cmnd
724 * Scatterlist case:
Olaf Kircha8ac6312007-12-13 12:43:35 -0600725 * We set up the iscsi_segment to point to the next
Olaf Kirchda32dd62007-12-13 12:43:21 -0600726 * scatterlist entry to copy to. As we go along,
727 * we move on to the next scatterlist entry and
728 * update the digest per-entry.
729 */
Mike Christie63c62f12008-12-02 00:32:04 -0600730 if (conn->datadgst_en &&
731 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
Olaf Kirchda32dd62007-12-13 12:43:21 -0600732 rx_hash = &tcp_conn->rx_hash;
733
734 debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
735 "datalen=%d)\n", tcp_conn,
Mike Christie135a8ad2008-05-21 15:54:10 -0500736 tcp_task->data_offset,
Olaf Kirchda32dd62007-12-13 12:43:21 -0600737 tcp_conn->in.datalen);
Mike Christie913e5bf2008-05-21 15:54:18 -0500738 rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
739 sdb->table.sgl,
740 sdb->table.nents,
741 tcp_task->data_offset,
742 tcp_conn->in.datalen,
743 iscsi_tcp_process_data_in,
744 rx_hash);
745 spin_unlock(&conn->session->lock);
746 return rc;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600747 }
Mike Christie913e5bf2008-05-21 15:54:18 -0500748 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
749 spin_unlock(&conn->session->lock);
750 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500751 case ISCSI_OP_SCSI_CMD_RSP:
Olaf Kirchda32dd62007-12-13 12:43:21 -0600752 if (tcp_conn->in.datalen) {
753 iscsi_tcp_data_recv_prep(tcp_conn);
754 return 0;
755 }
756 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500757 break;
758 case ISCSI_OP_R2T:
Mike Christie913e5bf2008-05-21 15:54:18 -0500759 spin_lock(&conn->session->lock);
Mike Christie135a8ad2008-05-21 15:54:10 -0500760 task = iscsi_itt_to_ctask(conn, hdr->itt);
761 if (!task)
Mike Christie913e5bf2008-05-21 15:54:18 -0500762 rc = ISCSI_ERR_BAD_ITT;
763 else if (ahslen)
Mike Christie5bb0b552006-04-06 21:26:46 -0500764 rc = ISCSI_ERR_AHSLEN;
Mike Christie913e5bf2008-05-21 15:54:18 -0500765 else if (task->sc->sc_data_direction == DMA_TO_DEVICE)
Mike Christie30b49152008-12-02 00:32:10 -0600766 rc = iscsi_tcp_r2t_rsp(conn, task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500767 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500768 rc = ISCSI_ERR_PROTO;
Mike Christie913e5bf2008-05-21 15:54:18 -0500769 spin_unlock(&conn->session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500770 break;
771 case ISCSI_OP_LOGIN_RSP:
772 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500773 case ISCSI_OP_REJECT:
774 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500775 /*
776 * It is possible that we could get a PDU with a buffer larger
777 * than 8K, but there are no targets that currently do this.
778 * For now we fail until we find a vendor that needs it
779 */
Olaf Kirchda32dd62007-12-13 12:43:21 -0600780 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
Mike Christie322d7392008-01-31 13:36:52 -0600781 iscsi_conn_printk(KERN_ERR, conn,
782 "iscsi_tcp: received buffer of "
783 "len %u but conn buffer is only %u "
784 "(opcode %0x)\n",
785 tcp_conn->in.datalen,
786 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
Mike Christiec8dc1e52006-07-24 15:47:39 -0500787 rc = ISCSI_ERR_PROTO;
788 break;
789 }
790
Olaf Kirchda32dd62007-12-13 12:43:21 -0600791 /* If there's data coming in with the response,
792 * receive it to the connection's buffer.
793 */
794 if (tcp_conn->in.datalen) {
795 iscsi_tcp_data_recv_prep(tcp_conn);
796 return 0;
797 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500798 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500799 case ISCSI_OP_LOGOUT_RSP:
800 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500801 case ISCSI_OP_SCSI_TMFUNC_RSP:
802 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
803 break;
804 default:
805 rc = ISCSI_ERR_BAD_OPCODE;
806 break;
807 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700808
Olaf Kirchda32dd62007-12-13 12:43:21 -0600809 if (rc == 0) {
810 /* Anything that comes with data should have
811 * been handled above. */
812 if (tcp_conn->in.datalen)
813 return ISCSI_ERR_PROTO;
814 iscsi_tcp_hdr_recv_prep(tcp_conn);
815 }
816
Alex Aizman7ba24712005-08-04 19:30:08 -0700817 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700818}
819
Olaf Kirchda32dd62007-12-13 12:43:21 -0600820/**
821 * iscsi_tcp_hdr_recv_done - process PDU header
822 *
823 * This is the callback invoked when the PDU header has
824 * been received. If the header is followed by additional
825 * header segments, we go back for more data.
826 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700827static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600828iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600829 struct iscsi_segment *segment)
Alex Aizman7ba24712005-08-04 19:30:08 -0700830{
Olaf Kirchda32dd62007-12-13 12:43:21 -0600831 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
832 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700833
Olaf Kirchda32dd62007-12-13 12:43:21 -0600834 /* Check if there are additional header segments
835 * *prior* to computing the digest, because we
836 * may need to go back to the caller for more.
837 */
838 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600839 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
Olaf Kirchda32dd62007-12-13 12:43:21 -0600840 /* Bump the header length - the caller will
841 * just loop around and get the AHS for us, and
842 * call again. */
843 unsigned int ahslen = hdr->hlength << 2;
Alex Aizman7ba24712005-08-04 19:30:08 -0700844
Olaf Kirchda32dd62007-12-13 12:43:21 -0600845 /* Make sure we don't overflow */
846 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
847 return ISCSI_ERR_AHSLEN;
848
Olaf Kircha8ac6312007-12-13 12:43:35 -0600849 segment->total_size += ahslen;
850 segment->size += ahslen;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600851 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700852 }
Olaf Kirchda32dd62007-12-13 12:43:21 -0600853
854 /* We're done processing the header. See if we're doing
855 * header digests; if so, set up the recv_digest buffer
856 * and go back for more. */
857 if (conn->hdrdgst_en) {
Olaf Kircha8ac6312007-12-13 12:43:35 -0600858 if (segment->digest_len == 0) {
Mike Christie63c62f12008-12-02 00:32:04 -0600859 /*
860 * Even if we offload the digest processing we
861 * splice it in so we can increment the skb/segment
862 * counters in preparation for the data segment.
863 */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600864 iscsi_tcp_segment_splice_digest(segment,
865 segment->recv_digest);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600866 return 0;
867 }
Olaf Kirchda32dd62007-12-13 12:43:21 -0600868
Mike Christie63c62f12008-12-02 00:32:04 -0600869 if (!(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
870 iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
871 segment->total_copied - ISCSI_DIGEST_SIZE,
872 segment->digest);
873
874 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
875 return ISCSI_ERR_HDR_DGST;
876 }
Olaf Kirchda32dd62007-12-13 12:43:21 -0600877 }
878
879 tcp_conn->in.hdr = hdr;
880 return iscsi_tcp_hdr_dissect(conn, hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -0700881}
882
Mike Christie63c62f12008-12-02 00:32:04 -0600883inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
884{
885 return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
886}
887
888enum {
889 ISCSI_TCP_SEGMENT_DONE, /* curr seg has been processed */
890 ISCSI_TCP_SKB_DONE, /* skb is out of data */
891 ISCSI_TCP_CONN_ERR, /* iscsi layer has fired a conn err */
892 ISCSI_TCP_SUSPENDED, /* conn is suspended */
893};
894
895/**
896 * iscsi_tcp_recv_skb - Process skb
897 * @conn: iscsi connection
898 * @skb: network buffer with header and/or data segment
899 * @offset: offset in skb
900 * @offload: bool indicating if transfer was offloaded
901 */
902int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
903 unsigned int offset, bool offloaded, int *status)
904{
905 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
906 struct iscsi_segment *segment = &tcp_conn->in.segment;
907 struct skb_seq_state seq;
908 unsigned int consumed = 0;
909 int rc = 0;
910
911 debug_tcp("in %d bytes\n", skb->len - offset);
912
913 if (unlikely(conn->suspend_rx)) {
914 debug_tcp("conn %d Rx suspended!\n", conn->id);
915 *status = ISCSI_TCP_SUSPENDED;
916 return 0;
917 }
918
919 if (offloaded) {
920 segment->total_copied = segment->total_size;
921 goto segment_done;
922 }
923
924 skb_prepare_seq_read(skb, offset, skb->len, &seq);
925 while (1) {
926 unsigned int avail;
927 const u8 *ptr;
928
929 avail = skb_seq_read(consumed, &ptr, &seq);
930 if (avail == 0) {
931 debug_tcp("no more data avail. Consumed %d\n",
932 consumed);
933 *status = ISCSI_TCP_SKB_DONE;
934 skb_abort_seq_read(&seq);
935 goto skb_done;
936 }
937 BUG_ON(segment->copied >= segment->size);
938
939 debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
940 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
941 BUG_ON(rc == 0);
942 consumed += rc;
943
944 if (segment->total_copied >= segment->total_size) {
945 skb_abort_seq_read(&seq);
946 goto segment_done;
947 }
948 }
949
950segment_done:
951 *status = ISCSI_TCP_SEGMENT_DONE;
952 debug_tcp("segment done\n");
953 rc = segment->done(tcp_conn, segment);
954 if (rc != 0) {
955 *status = ISCSI_TCP_CONN_ERR;
956 debug_tcp("Error receiving PDU, errno=%d\n", rc);
957 iscsi_conn_failure(conn, rc);
958 return 0;
959 }
960 /* The done() functions sets up the next segment. */
961
962skb_done:
963 conn->rxdata_octets += consumed;
964 return consumed;
965}
966EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
967
Alex Aizman7ba24712005-08-04 19:30:08 -0700968/**
Olaf Kirchda32dd62007-12-13 12:43:21 -0600969 * iscsi_tcp_recv - TCP receive in sendfile fashion
Alex Aizman7ba24712005-08-04 19:30:08 -0700970 * @rd_desc: read descriptor
971 * @skb: socket buffer
972 * @offset: offset in skb
973 * @len: skb->len - offset
974 **/
975static int
Olaf Kirchda32dd62007-12-13 12:43:21 -0600976iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
977 unsigned int offset, size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -0700978{
Alex Aizman7ba24712005-08-04 19:30:08 -0700979 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie63c62f12008-12-02 00:32:04 -0600980 unsigned int consumed, total_consumed = 0;
981 int status;
Alex Aizman7ba24712005-08-04 19:30:08 -0700982
Olaf Kirchda32dd62007-12-13 12:43:21 -0600983 debug_tcp("in %d bytes\n", skb->len - offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700984
Mike Christie63c62f12008-12-02 00:32:04 -0600985 do {
986 status = 0;
987 consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
988 offset += consumed;
989 total_consumed += consumed;
990 } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
Alex Aizman7ba24712005-08-04 19:30:08 -0700991
Mike Christie63c62f12008-12-02 00:32:04 -0600992 debug_tcp("read %d bytes status %d\n", skb->len - offset, status);
993 return total_consumed;
Alex Aizman7ba24712005-08-04 19:30:08 -0700994}
995
996static void
997iscsi_tcp_data_ready(struct sock *sk, int flag)
998{
999 struct iscsi_conn *conn = sk->sk_user_data;
Olaf Kirchda32dd62007-12-13 12:43:21 -06001000 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001001 read_descriptor_t rd_desc;
1002
1003 read_lock(&sk->sk_callback_lock);
1004
Mike Christie665b44a2006-05-02 19:46:49 -05001005 /*
Olaf Kirchda32dd62007-12-13 12:43:21 -06001006 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
Mike Christie665b44a2006-05-02 19:46:49 -05001007 * We set count to 1 because we want the network layer to
Olaf Kirchda32dd62007-12-13 12:43:21 -06001008 * hand us all the skbs that are available. iscsi_tcp_recv
Mike Christie665b44a2006-05-02 19:46:49 -05001009 * handled pdus that cross buffers or pdus that still need data.
1010 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001011 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -05001012 rd_desc.count = 1;
Olaf Kirchda32dd62007-12-13 12:43:21 -06001013 tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
Alex Aizman7ba24712005-08-04 19:30:08 -07001014
1015 read_unlock(&sk->sk_callback_lock);
Olaf Kirchda32dd62007-12-13 12:43:21 -06001016
1017 /* If we had to (atomically) map a highmem page,
1018 * unmap it now. */
Olaf Kircha8ac6312007-12-13 12:43:35 -06001019 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
Alex Aizman7ba24712005-08-04 19:30:08 -07001020}
1021
1022static void
1023iscsi_tcp_state_change(struct sock *sk)
1024{
Mike Christie5bb0b552006-04-06 21:26:46 -05001025 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001026 struct iscsi_conn *conn;
1027 struct iscsi_session *session;
1028 void (*old_state_change)(struct sock *);
1029
1030 read_lock(&sk->sk_callback_lock);
1031
1032 conn = (struct iscsi_conn*)sk->sk_user_data;
1033 session = conn->session;
1034
Mike Christiee6273992005-11-29 23:12:49 -06001035 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1036 sk->sk_state == TCP_CLOSE) &&
1037 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001038 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1039 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1040 }
1041
Mike Christie5bb0b552006-04-06 21:26:46 -05001042 tcp_conn = conn->dd_data;
1043 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001044
1045 read_unlock(&sk->sk_callback_lock);
1046
1047 old_state_change(sk);
1048}
1049
1050/**
1051 * iscsi_write_space - Called when more output buffer space is available
1052 * @sk: socket space is available for
1053 **/
Mike Christie30b49152008-12-02 00:32:10 -06001054static void iscsi_tcp_write_space(struct sock *sk)
Alex Aizman7ba24712005-08-04 19:30:08 -07001055{
1056 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001057 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1058
1059 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001060 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001061 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001062}
1063
Mike Christie30b49152008-12-02 00:32:10 -06001064static void iscsi_tcp_conn_set_callbacks(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001065{
Mike Christie5bb0b552006-04-06 21:26:46 -05001066 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1067 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001068
1069 /* assign new callbacks */
1070 write_lock_bh(&sk->sk_callback_lock);
1071 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001072 tcp_conn->old_data_ready = sk->sk_data_ready;
1073 tcp_conn->old_state_change = sk->sk_state_change;
1074 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001075 sk->sk_data_ready = iscsi_tcp_data_ready;
1076 sk->sk_state_change = iscsi_tcp_state_change;
Mike Christie30b49152008-12-02 00:32:10 -06001077 sk->sk_write_space = iscsi_tcp_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001078 write_unlock_bh(&sk->sk_callback_lock);
1079}
1080
Mike Christie30b49152008-12-02 00:32:10 -06001081static void iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001082{
Mike Christie5bb0b552006-04-06 21:26:46 -05001083 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001084
1085 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1086 write_lock_bh(&sk->sk_callback_lock);
1087 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001088 sk->sk_data_ready = tcp_conn->old_data_ready;
1089 sk->sk_state_change = tcp_conn->old_state_change;
1090 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001091 sk->sk_no_check = 0;
1092 write_unlock_bh(&sk->sk_callback_lock);
1093}
1094
1095/**
Mike Christie30b49152008-12-02 00:32:10 -06001096 * iscsi_tcp_xmit - TCP transmit
Olaf Kircha8ac6312007-12-13 12:43:35 -06001097 **/
Mike Christie30b49152008-12-02 00:32:10 -06001098static int iscsi_tcp_xmit(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001099{
Mike Christie5bb0b552006-04-06 21:26:46 -05001100 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001101 struct iscsi_segment *segment = &tcp_conn->out.segment;
1102 unsigned int consumed = 0;
1103 int rc = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001104
Olaf Kircha8ac6312007-12-13 12:43:35 -06001105 while (1) {
1106 rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
Mike Christie6f481e32008-09-24 11:46:13 -05001107 if (rc < 0) {
1108 rc = ISCSI_ERR_XMIT_FAILED;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001109 goto error;
Mike Christie6f481e32008-09-24 11:46:13 -05001110 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001111 if (rc == 0)
1112 break;
1113
1114 consumed += rc;
1115
1116 if (segment->total_copied >= segment->total_size) {
1117 if (segment->done != NULL) {
1118 rc = segment->done(tcp_conn, segment);
Mike Christie6f481e32008-09-24 11:46:13 -05001119 if (rc != 0)
Olaf Kircha8ac6312007-12-13 12:43:35 -06001120 goto error;
1121 }
1122 }
1123 }
1124
1125 debug_tcp("xmit %d bytes\n", consumed);
1126
1127 conn->txdata_octets += consumed;
1128 return consumed;
1129
1130error:
1131 /* Transmit error. We could initiate error recovery
1132 * here. */
1133 debug_tcp("Error sending PDU, errno=%d\n", rc);
Mike Christie6f481e32008-09-24 11:46:13 -05001134 iscsi_conn_failure(conn, rc);
1135 return -EIO;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001136}
1137
1138/**
1139 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
1140 */
1141static inline int
1142iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
1143{
1144 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1145 struct iscsi_segment *segment = &tcp_conn->out.segment;
1146
1147 return segment->total_copied - segment->total_size;
1148}
1149
Mike Christiee5a7efe2008-12-02 00:32:07 -06001150static int iscsi_tcp_flush(struct iscsi_task *task)
Olaf Kircha8ac6312007-12-13 12:43:35 -06001151{
Mike Christiee5a7efe2008-12-02 00:32:07 -06001152 struct iscsi_conn *conn = task->conn;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001153 int rc;
1154
1155 while (iscsi_tcp_xmit_qlen(conn)) {
Mike Christie30b49152008-12-02 00:32:10 -06001156 rc = iscsi_tcp_xmit(conn);
Olaf Kircha8ac6312007-12-13 12:43:35 -06001157 if (rc == 0)
1158 return -EAGAIN;
1159 if (rc < 0)
1160 return rc;
1161 }
1162
1163 return 0;
1164}
1165
1166/*
1167 * This is called when we're done sending the header.
1168 * Simply copy the data_segment to the send segment, and return.
1169 */
1170static int
1171iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
1172 struct iscsi_segment *segment)
1173{
1174 tcp_conn->out.segment = tcp_conn->out.data_segment;
1175 debug_tcp("Header done. Next segment size %u total_size %u\n",
1176 tcp_conn->out.segment.size, tcp_conn->out.segment.total_size);
1177 return 0;
1178}
1179
1180static void
1181iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen)
1182{
1183 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1184
Harvey Harrison3a12b192008-05-21 15:54:19 -05001185 debug_tcp("%s(%p%s)\n", __func__, tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001186 conn->hdrdgst_en? ", digest enabled" : "");
1187
1188 /* Clear the data segment - needs to be filled in by the
1189 * caller using iscsi_tcp_send_data_prep() */
1190 memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment));
1191
1192 /* If header digest is enabled, compute the CRC and
1193 * place the digest into the same buffer. We make
Mike Christie135a8ad2008-05-21 15:54:10 -05001194 * sure that both iscsi_tcp_task and mtask have
Olaf Kircha8ac6312007-12-13 12:43:35 -06001195 * sufficient room.
Mike Christie7cae5152006-01-13 18:05:47 -06001196 */
Olaf Kircha8ac6312007-12-13 12:43:35 -06001197 if (conn->hdrdgst_en) {
1198 iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
1199 hdr + hdrlen);
1200 hdrlen += ISCSI_DIGEST_SIZE;
Mike Christie3219e522006-05-30 00:37:28 -05001201 }
1202
Olaf Kircha8ac6312007-12-13 12:43:35 -06001203 /* Remember header pointer for later, when we need
1204 * to decide whether there's a payload to go along
1205 * with the header. */
1206 tcp_conn->out.hdr = hdr;
1207
1208 iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen,
1209 iscsi_tcp_send_hdr_done, NULL);
Alex Aizman7ba24712005-08-04 19:30:08 -07001210}
1211
Olaf Kircha8ac6312007-12-13 12:43:35 -06001212/*
1213 * Prepare the send buffer for the payload data.
1214 * Padding and checksumming will all be taken care
1215 * of by the iscsi_segment routines.
1216 */
1217static int
1218iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
1219 unsigned int count, unsigned int offset,
1220 unsigned int len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001221{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001222 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1223 struct hash_desc *tx_hash = NULL;
1224 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -07001225
Harvey Harrison3a12b192008-05-21 15:54:19 -05001226 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __func__,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001227 tcp_conn, offset, len,
1228 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -07001229
Olaf Kircha8ac6312007-12-13 12:43:35 -06001230 /* Make sure the datalen matches what the caller
1231 said he would send. */
1232 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1233 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -07001234
Olaf Kircha8ac6312007-12-13 12:43:35 -06001235 if (conn->datadgst_en)
1236 tx_hash = &tcp_conn->tx_hash;
1237
1238 return iscsi_segment_seek_sg(&tcp_conn->out.data_segment,
1239 sg, count, offset, len,
1240 NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -07001241}
1242
Olaf Kircha8ac6312007-12-13 12:43:35 -06001243static void
1244iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
1245 size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001246{
Olaf Kircha8ac6312007-12-13 12:43:35 -06001247 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1248 struct hash_desc *tx_hash = NULL;
1249 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -07001250
Harvey Harrison3a12b192008-05-21 15:54:19 -05001251 debug_tcp("%s(%p, datalen=%d%s)\n", __func__, tcp_conn, len,
Olaf Kircha8ac6312007-12-13 12:43:35 -06001252 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -07001253
Olaf Kircha8ac6312007-12-13 12:43:35 -06001254 /* Make sure the datalen matches what the caller
1255 said he would send. */
1256 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1257 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -07001258
Olaf Kircha8ac6312007-12-13 12:43:35 -06001259 if (conn->datadgst_en)
1260 tx_hash = &tcp_conn->tx_hash;
Alex Aizman7ba24712005-08-04 19:30:08 -07001261
Olaf Kircha8ac6312007-12-13 12:43:35 -06001262 iscsi_segment_init_linear(&tcp_conn->out.data_segment,
1263 data, len, NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -07001264}
1265
Mike Christiee5a7efe2008-12-02 00:32:07 -06001266static int iscsi_tcp_pdu_init(struct iscsi_task *task,
1267 unsigned int offset, unsigned int count)
1268{
1269 struct iscsi_conn *conn = task->conn;
1270 int err = 0;
1271
1272 iscsi_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
1273
1274 if (!count)
1275 return 0;
1276
1277 if (!task->sc)
1278 iscsi_tcp_send_linear_data_prepare(conn, task->data, count);
1279 else {
1280 struct scsi_data_buffer *sdb = scsi_out(task->sc);
1281
1282 err = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
1283 sdb->table.nents, offset, count);
1284 }
1285
1286 if (err) {
1287 iscsi_conn_failure(conn, err);
1288 return -EIO;
1289 }
1290 return 0;
1291}
1292
1293static int iscsi_tcp_pdu_alloc(struct iscsi_task *task)
1294{
1295 struct iscsi_tcp_task *tcp_task = task->dd_data;
1296
1297 task->hdr = &tcp_task->hdr.hdrbuf;
1298 task->hdr_max = sizeof(tcp_task->hdr) - ISCSI_DIGEST_SIZE;
1299 return 0;
1300}
1301
Alex Aizman7ba24712005-08-04 19:30:08 -07001302/**
Mike Christiefbc514b2008-05-21 15:54:07 -05001303 * iscsi_tcp_task - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001304 * @conn: iscsi connection
Mike Christie135a8ad2008-05-21 15:54:10 -05001305 * @task: scsi command task
Alex Aizman7ba24712005-08-04 19:30:08 -07001306 * @sc: scsi command
1307 **/
Mike Christiee5a7efe2008-12-02 00:32:07 -06001308static int iscsi_tcp_task_init(struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -07001309{
Mike Christie135a8ad2008-05-21 15:54:10 -05001310 struct iscsi_tcp_task *tcp_task = task->dd_data;
1311 struct iscsi_conn *conn = task->conn;
1312 struct scsi_cmnd *sc = task->sc;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001313 int err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001314
Mike Christiefbc514b2008-05-21 15:54:07 -05001315 if (!sc) {
1316 /*
Mike Christie135a8ad2008-05-21 15:54:10 -05001317 * mgmt tasks do not have a scatterlist since they come
Mike Christiefbc514b2008-05-21 15:54:07 -05001318 * in from the iscsi interface.
1319 */
Mike Christie135a8ad2008-05-21 15:54:10 -05001320 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id,
1321 task->itt);
Mike Christiefbc514b2008-05-21 15:54:07 -05001322
Mike Christiee5a7efe2008-12-02 00:32:07 -06001323 return conn->session->tt->init_pdu(task, 0, task->data_count);
Mike Christiefbc514b2008-05-21 15:54:07 -05001324 }
1325
Mike Christie135a8ad2008-05-21 15:54:10 -05001326 BUG_ON(__kfifo_len(tcp_task->r2tqueue));
Mike Christie135a8ad2008-05-21 15:54:10 -05001327 tcp_task->exp_datasn = 0;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001328
1329 /* Prepare PDU, optionally w/ immediate data */
Mike Christie135a8ad2008-05-21 15:54:10 -05001330 debug_scsi("task deq [cid %d itt 0x%x imm %d unsol %d]\n",
1331 conn->id, task->itt, task->imm_count,
Mike Christiee5a7efe2008-12-02 00:32:07 -06001332 task->unsol_r2t.data_length);
Olaf Kircha8ac6312007-12-13 12:43:35 -06001333
Mike Christiee5a7efe2008-12-02 00:32:07 -06001334 err = conn->session->tt->init_pdu(task, 0, task->imm_count);
Olaf Kircha8ac6312007-12-13 12:43:35 -06001335 if (err)
1336 return err;
Mike Christie135a8ad2008-05-21 15:54:10 -05001337 task->imm_count = 0;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001338 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001339}
1340
Mike Christiee5a7efe2008-12-02 00:32:07 -06001341static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
1342{
1343 struct iscsi_session *session = task->conn->session;
1344 struct iscsi_tcp_task *tcp_task = task->dd_data;
1345 struct iscsi_r2t_info *r2t = NULL;
1346
1347 if (iscsi_task_has_unsol_data(task))
1348 r2t = &task->unsol_r2t;
1349 else {
1350 spin_lock_bh(&session->lock);
1351 if (tcp_task->r2t) {
1352 r2t = tcp_task->r2t;
1353 /* Continue with this R2T? */
1354 if (r2t->data_length <= r2t->sent) {
1355 debug_scsi(" done with r2t %p\n", r2t);
1356 __kfifo_put(tcp_task->r2tpool.queue,
1357 (void *)&tcp_task->r2t,
1358 sizeof(void *));
1359 tcp_task->r2t = r2t = NULL;
1360 }
1361 }
1362
1363 if (r2t == NULL) {
1364 __kfifo_get(tcp_task->r2tqueue,
1365 (void *)&tcp_task->r2t, sizeof(void *));
1366 r2t = tcp_task->r2t;
1367 }
1368 spin_unlock_bh(&session->lock);
1369 }
1370
1371 return r2t;
1372}
1373
Olaf Kircha8ac6312007-12-13 12:43:35 -06001374/*
Mike Christie135a8ad2008-05-21 15:54:10 -05001375 * iscsi_tcp_task_xmit - xmit normal PDU task
1376 * @task: iscsi command task
Mike Christie218432c2007-05-30 12:57:17 -05001377 *
Olaf Kircha8ac6312007-12-13 12:43:35 -06001378 * We're expected to return 0 when everything was transmitted succesfully,
1379 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1380 * of error.
1381 */
Mike Christiee5a7efe2008-12-02 00:32:07 -06001382static int iscsi_tcp_task_xmit(struct iscsi_task *task)
Alex Aizman7ba24712005-08-04 19:30:08 -07001383{
Mike Christie135a8ad2008-05-21 15:54:10 -05001384 struct iscsi_conn *conn = task->conn;
Mike Christiee5a7efe2008-12-02 00:32:07 -06001385 struct iscsi_session *session = conn->session;
1386 struct iscsi_r2t_info *r2t;
Alex Aizman7ba24712005-08-04 19:30:08 -07001387 int rc = 0;
1388
Olaf Kircha8ac6312007-12-13 12:43:35 -06001389flush:
1390 /* Flush any pending data first. */
Mike Christiee5a7efe2008-12-02 00:32:07 -06001391 rc = session->tt->xmit_pdu(task);
Olaf Kircha8ac6312007-12-13 12:43:35 -06001392 if (rc < 0)
Mike Christie218432c2007-05-30 12:57:17 -05001393 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -06001394
Mike Christiefbc514b2008-05-21 15:54:07 -05001395 /* mgmt command */
Mike Christiee5a7efe2008-12-02 00:32:07 -06001396 if (!task->sc) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001397 if (task->hdr->itt == RESERVED_ITT)
1398 iscsi_put_task(task);
Mike Christiefbc514b2008-05-21 15:54:07 -05001399 return 0;
1400 }
1401
Olaf Kircha8ac6312007-12-13 12:43:35 -06001402 /* Are we done already? */
Mike Christiee5a7efe2008-12-02 00:32:07 -06001403 if (task->sc->sc_data_direction != DMA_TO_DEVICE)
Mike Christie218432c2007-05-30 12:57:17 -05001404 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001405
Mike Christiee5a7efe2008-12-02 00:32:07 -06001406 r2t = iscsi_tcp_get_curr_r2t(task);
1407 if (r2t == NULL) {
Olaf Kircha8ac6312007-12-13 12:43:35 -06001408 /* Waiting for more R2Ts to arrive. */
Mike Christiee5a7efe2008-12-02 00:32:07 -06001409 debug_tcp("no R2Ts yet\n");
1410 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001411 }
Mike Christiee5a7efe2008-12-02 00:32:07 -06001412
1413 rc = conn->session->tt->alloc_pdu(task);
1414 if (rc)
1415 return rc;
1416 iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1417
1418 debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1419 r2t, r2t->datasn - 1, task->hdr->itt,
1420 r2t->data_offset + r2t->sent, r2t->data_count);
1421
1422 rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1423 r2t->data_count);
1424 if (rc)
1425 return rc;
1426 r2t->sent += r2t->data_count;
1427 goto flush;
Alex Aizman7ba24712005-08-04 19:30:08 -07001428}
1429
Mike Christie7b8631b2006-01-13 18:05:50 -06001430static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001431iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001432{
Mike Christie7b8631b2006-01-13 18:05:50 -06001433 struct iscsi_conn *conn;
1434 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001435 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001436
Mike Christie5d91e202008-05-21 15:54:01 -05001437 cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001438 if (!cls_conn)
1439 return NULL;
1440 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001441 /*
1442 * due to strange issues with iser these are not set
1443 * in iscsi_conn_setup
1444 */
Mike Christiebf32ed32007-02-28 17:32:17 -06001445 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
Alex Aizman7ba24712005-08-04 19:30:08 -07001446
Mike Christie5d91e202008-05-21 15:54:01 -05001447 tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001448 tcp_conn->iscsi_conn = conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001449
James Bottomleyc9802cd2006-09-23 15:33:43 -05001450 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1451 CRYPTO_ALG_ASYNC);
1452 tcp_conn->tx_hash.flags = 0;
Mike Christie322d7392008-01-31 13:36:52 -06001453 if (IS_ERR(tcp_conn->tx_hash.tfm))
Mike Christie5d91e202008-05-21 15:54:01 -05001454 goto free_conn;
Mike Christiedd8c0d92006-08-31 18:09:28 -04001455
James Bottomleyc9802cd2006-09-23 15:33:43 -05001456 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1457 CRYPTO_ALG_ASYNC);
1458 tcp_conn->rx_hash.flags = 0;
Mike Christie322d7392008-01-31 13:36:52 -06001459 if (IS_ERR(tcp_conn->rx_hash.tfm))
Mike Christiedd8c0d92006-08-31 18:09:28 -04001460 goto free_tx_tfm;
1461
Mike Christie7b8631b2006-01-13 18:05:50 -06001462 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001463
Mike Christiedd8c0d92006-08-31 18:09:28 -04001464free_tx_tfm:
James Bottomleyc9802cd2006-09-23 15:33:43 -05001465 crypto_free_hash(tcp_conn->tx_hash.tfm);
Mike Christie5d91e202008-05-21 15:54:01 -05001466free_conn:
Mike Christie322d7392008-01-31 13:36:52 -06001467 iscsi_conn_printk(KERN_ERR, conn,
1468 "Could not create connection due to crc32c "
1469 "loading error. Make sure the crc32c "
1470 "module is built as a module or into the "
1471 "kernel\n");
Mike Christie5bb0b552006-04-06 21:26:46 -05001472 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001473 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001474}
1475
1476static void
Mike Christie1c834692006-07-24 15:47:26 -05001477iscsi_tcp_release_conn(struct iscsi_conn *conn)
1478{
Mike Christie22236962007-05-30 12:57:24 -05001479 struct iscsi_session *session = conn->session;
Mike Christie1c834692006-07-24 15:47:26 -05001480 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie22236962007-05-30 12:57:24 -05001481 struct socket *sock = tcp_conn->sock;
Mike Christie1c834692006-07-24 15:47:26 -05001482
Mike Christie22236962007-05-30 12:57:24 -05001483 if (!sock)
Mike Christie1c834692006-07-24 15:47:26 -05001484 return;
1485
Mike Christie22236962007-05-30 12:57:24 -05001486 sock_hold(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001487 iscsi_conn_restore_callbacks(tcp_conn);
Mike Christie22236962007-05-30 12:57:24 -05001488 sock_put(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -05001489
Mike Christie22236962007-05-30 12:57:24 -05001490 spin_lock_bh(&session->lock);
Mike Christie1c834692006-07-24 15:47:26 -05001491 tcp_conn->sock = NULL;
Mike Christie22236962007-05-30 12:57:24 -05001492 spin_unlock_bh(&session->lock);
1493 sockfd_put(sock);
Mike Christie1c834692006-07-24 15:47:26 -05001494}
1495
1496static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001497iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001498{
Mike Christie7b8631b2006-01-13 18:05:50 -06001499 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001500 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001501
Mike Christie1c834692006-07-24 15:47:26 -05001502 iscsi_tcp_release_conn(conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001503
Pete Wyckoff534284a2006-11-08 15:58:31 -06001504 if (tcp_conn->tx_hash.tfm)
1505 crypto_free_hash(tcp_conn->tx_hash.tfm);
1506 if (tcp_conn->rx_hash.tfm)
1507 crypto_free_hash(tcp_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001508
Mike Christie5d91e202008-05-21 15:54:01 -05001509 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001510}
1511
Mike Christie1c834692006-07-24 15:47:26 -05001512static void
1513iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1514{
1515 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie913e5bf2008-05-21 15:54:18 -05001516 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1517
1518 /* userspace may have goofed up and not bound us */
1519 if (!tcp_conn->sock)
1520 return;
1521 /*
1522 * Make sure our recv side is stopped.
1523 * Older tools called conn stop before ep_disconnect
1524 * so IO could still be coming in.
1525 */
1526 write_lock_bh(&tcp_conn->sock->sk->sk_callback_lock);
1527 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1528 write_unlock_bh(&tcp_conn->sock->sk->sk_callback_lock);
Mike Christie1c834692006-07-24 15:47:26 -05001529
1530 iscsi_conn_stop(cls_conn, flag);
1531 iscsi_tcp_release_conn(conn);
1532}
1533
Mike Christie22236962007-05-30 12:57:24 -05001534static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
1535 char *buf, int *port,
1536 int (*getname)(struct socket *, struct sockaddr *,
1537 int *addrlen))
1538{
1539 struct sockaddr_storage *addr;
1540 struct sockaddr_in6 *sin6;
1541 struct sockaddr_in *sin;
1542 int rc = 0, len;
1543
Al Viroa9204872007-07-20 16:03:40 +01001544 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
Mike Christie22236962007-05-30 12:57:24 -05001545 if (!addr)
1546 return -ENOMEM;
1547
1548 if (getname(sock, (struct sockaddr *) addr, &len)) {
1549 rc = -ENODEV;
1550 goto free_addr;
1551 }
1552
1553 switch (addr->ss_family) {
1554 case AF_INET:
1555 sin = (struct sockaddr_in *)addr;
1556 spin_lock_bh(&conn->session->lock);
Harvey Harrison63779432008-10-31 00:56:00 -07001557 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
Mike Christie22236962007-05-30 12:57:24 -05001558 *port = be16_to_cpu(sin->sin_port);
1559 spin_unlock_bh(&conn->session->lock);
1560 break;
1561 case AF_INET6:
1562 sin6 = (struct sockaddr_in6 *)addr;
1563 spin_lock_bh(&conn->session->lock);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001564 sprintf(buf, "%pI6", &sin6->sin6_addr);
Mike Christie22236962007-05-30 12:57:24 -05001565 *port = be16_to_cpu(sin6->sin6_port);
1566 spin_unlock_bh(&conn->session->lock);
1567 break;
1568 }
1569free_addr:
1570 kfree(addr);
1571 return rc;
1572}
1573
Alex Aizman7ba24712005-08-04 19:30:08 -07001574static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001575iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001576 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001577 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001578{
Mike Christie75613522008-05-21 15:53:59 -05001579 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1580 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001581 struct iscsi_conn *conn = cls_conn->dd_data;
1582 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001583 struct sock *sk;
1584 struct socket *sock;
1585 int err;
1586
1587 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001588 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001589 if (!sock) {
Mike Christie322d7392008-01-31 13:36:52 -06001590 iscsi_conn_printk(KERN_ERR, conn,
1591 "sockfd_lookup failed %d\n", err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001592 return -EEXIST;
1593 }
Mike Christie22236962007-05-30 12:57:24 -05001594 /*
1595 * copy these values now because if we drop the session
1596 * userspace may still want to query the values since we will
1597 * be using them for the reconnect
1598 */
1599 err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1600 &conn->portal_port, kernel_getpeername);
1601 if (err)
1602 goto free_socket;
1603
Mike Christie75613522008-05-21 15:53:59 -05001604 err = iscsi_tcp_get_addr(conn, sock, ihost->local_address,
1605 &ihost->local_port, kernel_getsockname);
Mike Christie22236962007-05-30 12:57:24 -05001606 if (err)
1607 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001608
Mike Christie5bb0b552006-04-06 21:26:46 -05001609 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1610 if (err)
Mike Christie22236962007-05-30 12:57:24 -05001611 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -07001612
Mike Christie67a61112006-05-30 00:37:20 -05001613 /* bind iSCSI connection and socket */
1614 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001615
Mike Christie67a61112006-05-30 00:37:20 -05001616 /* setup Socket parameters */
1617 sk = sock->sk;
1618 sk->sk_reuse = 1;
1619 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1620 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001621
Mike Christie30b49152008-12-02 00:32:10 -06001622 iscsi_tcp_conn_set_callbacks(conn);
Mike Christie67a61112006-05-30 00:37:20 -05001623 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1624 /*
1625 * set receive state machine into initial state
1626 */
Olaf Kirchda32dd62007-12-13 12:43:21 -06001627 iscsi_tcp_hdr_recv_prep(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001628 return 0;
Mike Christie22236962007-05-30 12:57:24 -05001629
1630free_socket:
1631 sockfd_put(sock);
1632 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001633}
1634
Alex Aizman7ba24712005-08-04 19:30:08 -07001635static int
1636iscsi_r2tpool_alloc(struct iscsi_session *session)
1637{
1638 int i;
1639 int cmd_i;
1640
1641 /*
Mike Christie135a8ad2008-05-21 15:54:10 -05001642 * initialize per-task: R2T pool and xmit queue
Alex Aizman7ba24712005-08-04 19:30:08 -07001643 */
1644 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001645 struct iscsi_task *task = session->cmds[cmd_i];
1646 struct iscsi_tcp_task *tcp_task = task->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001647
1648 /*
Mike Christiee5a7efe2008-12-02 00:32:07 -06001649 * pre-allocated x2 as much r2ts to handle race when
Alex Aizman7ba24712005-08-04 19:30:08 -07001650 * target acks DataOut faster than we data_xmit() queues
1651 * could replenish r2tqueue.
1652 */
1653
1654 /* R2T pool */
Mike Christiee5a7efe2008-12-02 00:32:07 -06001655 if (iscsi_pool_init(&tcp_task->r2tpool,
1656 session->max_r2t * 2, NULL,
Mike Christie5bb0b552006-04-06 21:26:46 -05001657 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001658 goto r2t_alloc_fail;
1659 }
1660
1661 /* R2T xmit queue */
Mike Christie135a8ad2008-05-21 15:54:10 -05001662 tcp_task->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07001663 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie135a8ad2008-05-21 15:54:10 -05001664 if (tcp_task->r2tqueue == ERR_PTR(-ENOMEM)) {
1665 iscsi_pool_free(&tcp_task->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001666 goto r2t_alloc_fail;
1667 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001668 }
1669
1670 return 0;
1671
1672r2t_alloc_fail:
1673 for (i = 0; i < cmd_i; i++) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001674 struct iscsi_task *task = session->cmds[i];
1675 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001676
Mike Christie135a8ad2008-05-21 15:54:10 -05001677 kfifo_free(tcp_task->r2tqueue);
1678 iscsi_pool_free(&tcp_task->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001679 }
1680 return -ENOMEM;
1681}
1682
1683static void
1684iscsi_r2tpool_free(struct iscsi_session *session)
1685{
1686 int i;
1687
1688 for (i = 0; i < session->cmds_max; i++) {
Mike Christie135a8ad2008-05-21 15:54:10 -05001689 struct iscsi_task *task = session->cmds[i];
1690 struct iscsi_tcp_task *tcp_task = task->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001691
Mike Christie135a8ad2008-05-21 15:54:10 -05001692 kfifo_free(tcp_task->r2tqueue);
1693 iscsi_pool_free(&tcp_task->r2tpool);
Alex Aizman7ba24712005-08-04 19:30:08 -07001694 }
1695}
1696
Alex Aizman7ba24712005-08-04 19:30:08 -07001697static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001698iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05001699 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07001700{
Mike Christie7b7232f2006-02-01 21:06:49 -06001701 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001702 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001703 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001704 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07001705
Alex Aizman7ba24712005-08-04 19:30:08 -07001706 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001707 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001708 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001709 break;
1710 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001711 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05001712 tcp_conn->sendpage = conn->datadgst_en ?
1713 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07001714 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07001715 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001716 sscanf(buf, "%d", &value);
Mike Christiedf93ffc2007-12-13 12:43:42 -06001717 if (value <= 0 || !is_power_of_2(value))
1718 return -EINVAL;
1719 if (session->max_r2t == value)
Alex Aizman7ba24712005-08-04 19:30:08 -07001720 break;
1721 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05001722 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001723 if (iscsi_r2tpool_alloc(session))
1724 return -ENOMEM;
1725 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07001726 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001727 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001728 }
1729
1730 return 0;
1731}
1732
1733static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05001734iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
1735 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06001736{
Mike Christie7b7232f2006-02-01 21:06:49 -06001737 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001738 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06001739
1740 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05001741 case ISCSI_PARAM_CONN_PORT:
Mike Christie22236962007-05-30 12:57:24 -05001742 spin_lock_bh(&conn->session->lock);
1743 len = sprintf(buf, "%hu\n", conn->portal_port);
1744 spin_unlock_bh(&conn->session->lock);
Mike Christie8d2860b2006-05-02 19:46:47 -05001745 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001746 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie22236962007-05-30 12:57:24 -05001747 spin_lock_bh(&conn->session->lock);
1748 len = sprintf(buf, "%s\n", conn->portal_address);
1749 spin_unlock_bh(&conn->session->lock);
Mike Christiefd7255f2006-04-06 21:13:36 -05001750 break;
1751 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001752 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05001753 }
1754
1755 return len;
1756}
1757
Alex Aizman7ba24712005-08-04 19:30:08 -07001758static void
Mike Christie7b7232f2006-02-01 21:06:49 -06001759iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07001760{
Mike Christie7b7232f2006-02-01 21:06:49 -06001761 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001762 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001763
1764 stats->txdata_octets = conn->txdata_octets;
1765 stats->rxdata_octets = conn->rxdata_octets;
1766 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1767 stats->dataout_pdus = conn->dataout_pdus_cnt;
1768 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1769 stats->datain_pdus = conn->datain_pdus_cnt;
1770 stats->r2t_pdus = conn->r2t_pdus_cnt;
1771 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1772 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1773 stats->custom_length = 3;
1774 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05001775 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001776 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05001777 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001778 strcpy(stats->custom[2].desc, "eh_abort_cnt");
1779 stats->custom[2].value = conn->eh_abort_cnt;
1780}
1781
Mike Christie5bb0b552006-04-06 21:26:46 -05001782static struct iscsi_cls_session *
Mike Christie06520ed2008-05-21 15:54:15 -05001783iscsi_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
Mike Christie40753ca2008-05-21 15:53:56 -05001784 uint16_t qdepth, uint32_t initial_cmdsn,
1785 uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07001786{
Mike Christie5bb0b552006-04-06 21:26:46 -05001787 struct iscsi_cls_session *cls_session;
1788 struct iscsi_session *session;
Mike Christie06520ed2008-05-21 15:54:15 -05001789 struct Scsi_Host *shost;
Alex Aizman7ba24712005-08-04 19:30:08 -07001790
Mike Christie06520ed2008-05-21 15:54:15 -05001791 if (ep) {
1792 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
Mike Christie5bb0b552006-04-06 21:26:46 -05001793 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05001794 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001795
Mike Christiea4804cd2008-05-21 15:54:00 -05001796 shost = iscsi_host_alloc(&iscsi_sht, 0, qdepth);
Mike Christie75613522008-05-21 15:53:59 -05001797 if (!shost)
1798 return NULL;
1799 shost->transportt = iscsi_tcp_scsi_transport;
1800 shost->max_lun = iscsi_max_lun;
1801 shost->max_id = 0;
1802 shost->max_channel = 0;
Boaz Harrosh30e9ba92008-05-26 11:31:19 +03001803 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
Mike Christie75613522008-05-21 15:53:59 -05001804
Mike Christiea4804cd2008-05-21 15:54:00 -05001805 if (iscsi_host_add(shost, NULL))
Mike Christie75613522008-05-21 15:53:59 -05001806 goto free_host;
1807 *hostno = shost->host_no;
1808
1809 cls_session = iscsi_session_setup(&iscsi_tcp_transport, shost, cmds_max,
Mike Christie135a8ad2008-05-21 15:54:10 -05001810 sizeof(struct iscsi_tcp_task),
Mike Christie79706342008-05-21 15:54:12 -05001811 initial_cmdsn, 0);
Mike Christie75613522008-05-21 15:53:59 -05001812 if (!cls_session)
1813 goto remove_host;
1814 session = cls_session->dd_data;
1815
Mike Christiefbc514b2008-05-21 15:54:07 -05001816 shost->can_queue = session->scsi_cmds_max;
Mike Christie75613522008-05-21 15:53:59 -05001817 if (iscsi_r2tpool_alloc(session))
1818 goto remove_session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001819 return cls_session;
1820
Mike Christie75613522008-05-21 15:53:59 -05001821remove_session:
Mike Christie5bb0b552006-04-06 21:26:46 -05001822 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -05001823remove_host:
Mike Christiea4804cd2008-05-21 15:54:00 -05001824 iscsi_host_remove(shost);
Mike Christie75613522008-05-21 15:53:59 -05001825free_host:
Mike Christiea4804cd2008-05-21 15:54:00 -05001826 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001827 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001828}
1829
Mike Christie5bb0b552006-04-06 21:26:46 -05001830static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
1831{
Mike Christie75613522008-05-21 15:53:59 -05001832 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1833
1834 iscsi_r2tpool_free(cls_session->dd_data);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001835 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -05001836
Mike Christiea4804cd2008-05-21 15:54:00 -05001837 iscsi_host_remove(shost);
1838 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -05001839}
1840
Mike Christied1d81c02007-05-30 12:57:21 -05001841static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
1842{
Mike Christieb6d44fe2007-07-26 12:46:47 -05001843 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
Mike Christied1d81c02007-05-30 12:57:21 -05001844 blk_queue_dma_alignment(sdev->request_queue, 0);
1845 return 0;
1846}
1847
Mike Christie5bb0b552006-04-06 21:26:46 -05001848static struct scsi_host_template iscsi_sht = {
Mike Christie79743922007-07-26 12:46:46 -05001849 .module = THIS_MODULE,
Mike Christief4246b32006-07-24 15:47:54 -05001850 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05001851 .queuecommand = iscsi_queuecommand,
1852 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -05001853 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie66bbe0c2007-12-13 12:43:39 -06001854 .sg_tablesize = 4096,
Mike Christie8231f0e2007-02-28 17:32:20 -06001855 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -05001856 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
1857 .eh_abort_handler = iscsi_eh_abort,
Mike Christie843c0a82007-12-13 12:43:20 -06001858 .eh_device_reset_handler= iscsi_eh_device_reset,
Mike Christie8e124522008-09-24 11:46:12 -05001859 .eh_target_reset_handler= iscsi_eh_target_reset,
Mike Christie5bb0b552006-04-06 21:26:46 -05001860 .use_clustering = DISABLE_CLUSTERING,
Mike Christied1d81c02007-05-30 12:57:21 -05001861 .slave_configure = iscsi_tcp_slave_configure,
Mike Christie5bb0b552006-04-06 21:26:46 -05001862 .proc_name = "iscsi_tcp",
1863 .this_id = -1,
1864};
1865
Alex Aizman7ba24712005-08-04 19:30:08 -07001866static struct iscsi_transport iscsi_tcp_transport = {
1867 .owner = THIS_MODULE,
1868 .name = "tcp",
1869 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
1870 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05001871 .param_mask = ISCSI_MAX_RECV_DLENGTH |
1872 ISCSI_MAX_XMIT_DLENGTH |
1873 ISCSI_HDRDGST_EN |
1874 ISCSI_DATADGST_EN |
1875 ISCSI_INITIAL_R2T_EN |
1876 ISCSI_MAX_R2T |
1877 ISCSI_IMM_DATA_EN |
1878 ISCSI_FIRST_BURST |
1879 ISCSI_MAX_BURST |
1880 ISCSI_PDU_INORDER_EN |
1881 ISCSI_DATASEQ_INORDER_EN |
1882 ISCSI_ERL |
1883 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05001884 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05001885 ISCSI_EXP_STATSN |
1886 ISCSI_PERSISTENT_PORT |
1887 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -05001888 ISCSI_TARGET_NAME | ISCSI_TPGT |
1889 ISCSI_USERNAME | ISCSI_PASSWORD |
Mike Christie843c0a82007-12-13 12:43:20 -06001890 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
Mike Christief6d51802007-12-13 12:43:30 -06001891 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
1892 ISCSI_LU_RESET_TMO |
Mike Christie88dfd342008-05-21 15:54:16 -05001893 ISCSI_PING_TMO | ISCSI_RECV_TMO |
1894 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
Mike Christie22236962007-05-30 12:57:24 -05001895 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
Mike Christied8196ed2007-05-30 12:57:25 -05001896 ISCSI_HOST_INITIATOR_NAME |
1897 ISCSI_HOST_NETDEV_NAME,
Mike Christie5bb0b552006-04-06 21:26:46 -05001898 /* session management */
1899 .create_session = iscsi_tcp_session_create,
1900 .destroy_session = iscsi_tcp_session_destroy,
1901 /* connection management */
1902 .create_conn = iscsi_tcp_conn_create,
1903 .bind_conn = iscsi_tcp_conn_bind,
1904 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07001905 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05001906 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06001907 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07001908 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05001909 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -05001910 /* iscsi host params */
Mike Christie75613522008-05-21 15:53:59 -05001911 .get_host_param = iscsi_host_get_param,
Mike Christie0801c242007-05-30 12:57:12 -05001912 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -05001913 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07001914 .send_pdu = iscsi_conn_send_pdu,
1915 .get_stats = iscsi_conn_get_stats,
Mike Christiee5a7efe2008-12-02 00:32:07 -06001916 /* iscsi task/cmd helpers */
Mike Christiefbc514b2008-05-21 15:54:07 -05001917 .init_task = iscsi_tcp_task_init,
1918 .xmit_task = iscsi_tcp_task_xmit,
1919 .cleanup_task = iscsi_tcp_cleanup_task,
Mike Christiee5a7efe2008-12-02 00:32:07 -06001920 /* low level pdu helpers */
1921 .xmit_pdu = iscsi_tcp_flush,
1922 .init_pdu = iscsi_tcp_pdu_init,
1923 .alloc_pdu = iscsi_tcp_pdu_alloc,
Mike Christie5bb0b552006-04-06 21:26:46 -05001924 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05001925 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07001926};
1927
1928static int __init
1929iscsi_tcp_init(void)
1930{
Alex Aizman7ba24712005-08-04 19:30:08 -07001931 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05001932 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
1933 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07001934 return -EINVAL;
1935 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001936
Mike Christie75613522008-05-21 15:53:59 -05001937 iscsi_tcp_scsi_transport = iscsi_register_transport(
1938 &iscsi_tcp_transport);
1939 if (!iscsi_tcp_scsi_transport)
Mike Christieffbfe922006-05-18 20:31:36 -05001940 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07001941
Mike Christie7b8631b2006-01-13 18:05:50 -06001942 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001943}
1944
1945static void __exit
1946iscsi_tcp_exit(void)
1947{
1948 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07001949}
1950
1951module_init(iscsi_tcp_init);
1952module_exit(iscsi_tcp_exit);