[SCSI] iscsi_tcp: split module into lib and lld
[deliverable/linux.git] / drivers / scsi / iscsi_tcp.c
CommitLineData
7ba24713
AA
1/*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
5bb0b55a
MC
6 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
7ba24713
AA
8 * 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>
4e7aba73 32#include <linux/file.h>
7ba24713
AA
33#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>
d1d81c01 40#include <scsi/scsi_device.h>
7ba24713
AA
41#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");
da32dd68 51#undef DEBUG_TCP
7ba24713
AA
52#define DEBUG_ASSERT
53
54#ifdef DEBUG_TCP
5bb0b55a 55#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
7ba24713
AA
56#else
57#define debug_tcp(fmt...)
58#endif
59
75613521
MC
60static struct scsi_transport_template *iscsi_tcp_scsi_transport;
61static struct scsi_host_template iscsi_sht;
62static struct iscsi_transport iscsi_tcp_transport;
63
7ba24713
AA
64static unsigned int iscsi_max_lun = 512;
65module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
66
da32dd68 67static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
a8ac6311 68 struct iscsi_segment *segment);
7ba24713 69
da32dd68 70/*
a8ac6311 71 * Scatterlist handling: inside the iscsi_segment, we
da32dd68
OK
72 * 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/**
a8ac6311
OK
84 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
85 * @segment: the buffer object
86 * @sg: scatterlist
da32dd68
OK
87 * @offset: byte offset into that sg entry
88 *
a8ac6311 89 * This function sets up the segment so that subsequent
da32dd68
OK
90 * data is copied to the indicated sg entry, at the given
91 * offset.
92 */
93static inline void
a8ac6311
OK
94iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
95 struct scatterlist *sg, unsigned int offset)
da32dd68 96{
a8ac6311
OK
97 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;
da32dd68
OK
102}
103
104/**
a8ac6311
OK
105 * iscsi_tcp_segment_map - map the current S/G page
106 * @segment: iscsi_segment
107 * @recv: 1 if called from recv path
da32dd68
OK
108 *
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
a8ac6311 114iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
da32dd68
OK
115{
116 struct scatterlist *sg;
117
a8ac6311 118 if (segment->data != NULL || !segment->sg)
da32dd68
OK
119 return;
120
a8ac6311
OK
121 sg = segment->sg;
122 BUG_ON(segment->sg_mapped);
da32dd68 123 BUG_ON(sg->length == 0);
a8ac6311
OK
124
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;
da32dd68
OK
138}
139
140static inline void
a8ac6311 141iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
da32dd68 142{
a8ac6311
OK
143 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;
da32dd68
OK
150 }
151}
152
153/*
154 * Splice the digest buffer into the buffer
155 */
156static inline void
a8ac6311 157iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
da32dd68 158{
a8ac6311
OK
159 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;
da32dd68
OK
166}
167
168/**
a8ac6311
OK
169 * 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
da32dd68 173 *
a8ac6311 174 * Check if we're done receiving this segment. If the receive
da32dd68
OK
175 * 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 */
7ba24713 183static inline int
a8ac6311 184iscsi_tcp_segment_done(struct iscsi_segment *segment, int recv, unsigned copied)
7ba24713 185{
da32dd68 186 static unsigned char padbuf[ISCSI_PAD_LEN];
a8ac6311 187 struct scatterlist sg;
004d6530 188 unsigned int pad;
da32dd68 189
a8ac6311
OK
190 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);
da32dd68
OK
213 return 0;
214 }
7ba24713 215
a8ac6311
OK
216 segment->total_copied += segment->copied;
217 segment->copied = 0;
218 segment->size = 0;
7ba24713 219
da32dd68 220 /* Unmap the current scatterlist page, if there is one. */
a8ac6311 221 iscsi_tcp_segment_unmap(segment);
da32dd68
OK
222
223 /* Do we have more scatterlist entries? */
a8ac6311
OK
224 debug_tcp("total copied %u total size %u\n", segment->total_copied,
225 segment->total_size);
226 if (segment->total_copied < segment->total_size) {
da32dd68 227 /* Proceed to the next entry in the scatterlist. */
a8ac6311
OK
228 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);
da32dd68
OK
232 return 0;
233 }
234
235 /* Do we need to handle padding? */
a8ac6311 236 pad = iscsi_padding(segment->total_copied);
004d6530 237 if (pad != 0) {
da32dd68 238 debug_tcp("consume %d pad bytes\n", pad);
a8ac6311
OK
239 segment->total_size += pad;
240 segment->size = pad;
241 segment->data = padbuf;
da32dd68
OK
242 return 0;
243 }
244
245 /*
a8ac6311 246 * Set us up for transferring the data digest. hdr digest
da32dd68
OK
247 * is completely handled in hdr done function.
248 */
a8ac6311
OK
249 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;
da32dd68 254 }
7ba24713 255
da32dd68
OK
256 return 1;
257}
7ba24713 258
da32dd68 259/**
a8ac6311 260 * iscsi_tcp_xmit_segment - transmit segment
da32dd68 261 * @tcp_conn: the iSCSI TCP connection
a8ac6311
OK
262 * @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
da32dd68
OK
323 * @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
a8ac6311
OK
337iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
338 struct iscsi_segment *segment, const void *ptr,
339 unsigned int len)
da32dd68 340{
a8ac6311 341 unsigned int copy = 0, copied = 0;
7ba24713 342
a8ac6311
OK
343 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;
da32dd68 348 }
a8ac6311
OK
349
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);
da32dd68
OK
353 copied += copy;
354 }
da32dd68
OK
355 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;
7ba24713 363
da32dd68
OK
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,
a8ac6311 370 struct iscsi_segment *segment)
da32dd68 371{
a8ac6311 372 if (!segment->digest_len)
da32dd68
OK
373 return 1;
374
a8ac6311
OK
375 if (memcmp(segment->recv_digest, segment->digest,
376 segment->digest_len)) {
da32dd68
OK
377 debug_scsi("digest mismatch\n");
378 return 0;
379 }
380
381 return 1;
382}
383
384/*
a8ac6311 385 * Helper function to set up segment buffer
da32dd68
OK
386 */
387static inline void
a8ac6311
OK
388__iscsi_segment_init(struct iscsi_segment *segment, size_t size,
389 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
da32dd68 390{
a8ac6311
OK
391 memset(segment, 0, sizeof(*segment));
392 segment->total_size = size;
393 segment->done = done;
da32dd68
OK
394
395 if (hash) {
a8ac6311 396 segment->hash = hash;
da32dd68
OK
397 crypto_hash_init(hash);
398 }
399}
400
401static inline void
a8ac6311
OK
402iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
403 size_t size, iscsi_segment_done_fn_t *done,
404 struct hash_desc *hash)
da32dd68 405{
a8ac6311
OK
406 __iscsi_segment_init(segment, size, done, hash);
407 segment->data = data;
408 segment->size = size;
da32dd68
OK
409}
410
411static inline int
a8ac6311
OK
412iscsi_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)
da32dd68 416{
a8ac6311 417 struct scatterlist *sg;
da32dd68
OK
418 unsigned int i;
419
a8ac6311
OK
420 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);
da32dd68 428 return 0;
7ba24713 429 }
a8ac6311 430 offset -= sg->length;
7ba24713
AA
431 }
432
da32dd68
OK
433 return ISCSI_ERR_DATA_OFFSET;
434}
435
436/**
a8ac6311 437 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
da32dd68
OK
438 * @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" : "");
a8ac6311 449 iscsi_segment_init_linear(&tcp_conn->in.segment,
da32dd68
OK
450 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,
a8ac6311 459 struct iscsi_segment *segment)
da32dd68
OK
460{
461 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
462 int rc = 0;
463
a8ac6311 464 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
da32dd68
OK
465 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);
7ba24713
AA
473 return 0;
474}
475
da32dd68
OK
476static 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
63c62f1c
MC
482 if (conn->datadgst_en &
483 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
da32dd68
OK
484 rx_hash = &tcp_conn->rx_hash;
485
a8ac6311 486 iscsi_segment_init_linear(&tcp_conn->in.segment,
da32dd68
OK
487 conn->data, tcp_conn->in.datalen,
488 iscsi_tcp_data_recv_done, rx_hash);
489}
490
30a6c652
MC
491/*
492 * must be called with session lock
493 */
e5a7efef 494static void iscsi_tcp_cleanup_task(struct iscsi_task *task)
7ba24713 495{
135a8ad4 496 struct iscsi_tcp_task *tcp_task = task->dd_data;
b6c395ed 497 struct iscsi_r2t_info *r2t;
7ba24713 498
e5a7efef
MC
499 /* nothing to do for mgmt or pending tasks */
500 if (!task->sc || task->state == ISCSI_TASK_PENDING)
fbc514b4
MC
501 return;
502
135a8ad4
MC
503 /* 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,
b6c395ed 506 sizeof(void*));
135a8ad4 507 debug_scsi("iscsi_tcp_cleanup_task pending r2t dropped\n");
b6c395ed
MC
508 }
509
135a8ad4 510 r2t = tcp_task->r2t;
a8ac6311 511 if (r2t != NULL) {
135a8ad4 512 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
a8ac6311 513 sizeof(void*));
135a8ad4 514 tcp_task->r2t = NULL;
a8ac6311 515 }
7ba24713
AA
516}
517
518/**
30b49150 519 * iscsi_tcp_data_in - SCSI Data-In Response processing
7ba24713 520 * @conn: iscsi connection
135a8ad4 521 * @task: scsi command task
30b49150
MC
522 */
523static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
7ba24713 524{
5bb0b55a 525 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
135a8ad4 526 struct iscsi_tcp_task *tcp_task = task->dd_data;
5bb0b55a 527 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
7ba24713 528 int datasn = be32_to_cpu(rhdr->datasn);
1d9edf02 529 unsigned total_in_length = scsi_in(task->sc)->length;
7ba24713 530
1d9edf02 531 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
5bb0b55a 532 if (tcp_conn->in.datalen == 0)
7ba24713
AA
533 return 0;
534
135a8ad4
MC
535 if (tcp_task->exp_datasn != datasn) {
536 debug_tcp("%s: task->exp_datasn(%d) != rhdr->datasn(%d)\n",
3a12b199 537 __func__, tcp_task->exp_datasn, datasn);
7ba24713 538 return ISCSI_ERR_DATASN;
d473cc7f 539 }
7ba24713 540
135a8ad4 541 tcp_task->exp_datasn++;
7ba24713 542
135a8ad4
MC
543 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
544 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
857ae0bd 545 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
3a12b199 546 __func__, tcp_task->data_offset,
94795b61 547 tcp_conn->in.datalen, total_in_length);
7ba24713 548 return ISCSI_ERR_DATA_OFFSET;
857ae0bd 549 }
7ba24713 550
7ba24713
AA
551 conn->datain_pdus_cnt++;
552 return 0;
553}
554
7ba24713 555/**
30b49150 556 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
7ba24713 557 * @conn: iscsi connection
135a8ad4 558 * @task: scsi command task
30b49150
MC
559 */
560static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
7ba24713 561{
7ba24713 562 struct iscsi_session *session = conn->session;
135a8ad4 563 struct iscsi_tcp_task *tcp_task = task->dd_data;
5bb0b55a
MC
564 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
565 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
e5a7efef 566 struct iscsi_r2t_info *r2t;
7ba24713
AA
567 int r2tsn = be32_to_cpu(rhdr->r2tsn);
568 int rc;
569
98a9416a 570 if (tcp_conn->in.datalen) {
322d739d
MC
571 iscsi_conn_printk(KERN_ERR, conn,
572 "invalid R2t with datalen %d\n",
573 tcp_conn->in.datalen);
7ba24713 574 return ISCSI_ERR_DATALEN;
98a9416a 575 }
7ba24713 576
135a8ad4
MC
577 if (tcp_task->exp_datasn != r2tsn){
578 debug_tcp("%s: task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
3a12b199 579 __func__, tcp_task->exp_datasn, r2tsn);
7ba24713 580 return ISCSI_ERR_R2TSN;
d473cc7f 581 }
7ba24713 582
135a8ad4 583 /* fill-in new R2T associated with the task */
77a23c21
MC
584 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
585
135a8ad4 586 if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
322d739d
MC
587 iscsi_conn_printk(KERN_INFO, conn,
588 "dropping R2T itt %d in recovery.\n",
135a8ad4 589 task->itt);
7ba24713
AA
590 return 0;
591 }
b6c395ed 592
135a8ad4 593 rc = __kfifo_get(tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*));
e5a7efef
MC
594 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 }
7ba24713
AA
600
601 r2t->exp_statsn = rhdr->statsn;
602 r2t->data_length = be32_to_cpu(rhdr->data_length);
98a9416a 603 if (r2t->data_length == 0) {
322d739d
MC
604 iscsi_conn_printk(KERN_ERR, conn,
605 "invalid R2T with zero data len\n");
135a8ad4 606 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
03766a1d 607 sizeof(void*));
7ba24713
AA
608 return ISCSI_ERR_DATALEN;
609 }
610
98a9416a
MC
611 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
7ba24713 616 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
135a8ad4 617 if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) {
322d739d
MC
618 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,
135a8ad4
MC
621 r2t->data_offset, scsi_out(task->sc)->length);
622 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
03766a1d 623 sizeof(void*));
7ba24713
AA
624 return ISCSI_ERR_DATALEN;
625 }
626
627 r2t->ttt = rhdr->ttt; /* no flip */
e5a7efef
MC
628 r2t->datasn = 0;
629 r2t->sent = 0;
7ba24713 630
135a8ad4
MC
631 tcp_task->exp_datasn = r2tsn + 1;
632 __kfifo_put(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
7ba24713 633 conn->r2t_pdus_cnt++;
843c0a8a 634
135a8ad4 635 iscsi_requeue_task(task);
7ba24713
AA
636 return 0;
637}
638
da32dd68
OK
639/*
640 * Handle incoming reply to DataIn command
641 */
642static int
643iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
a8ac6311 644 struct iscsi_segment *segment)
da32dd68
OK
645{
646 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
647 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
648 int rc;
649
a8ac6311 650 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
da32dd68
OK
651 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 */
7ba24713 674static int
da32dd68 675iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
7ba24713 676{
5bb0b55a 677 int rc = 0, opcode, ahslen;
5bb0b55a 678 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
135a8ad4 679 struct iscsi_task *task;
7ba24713
AA
680
681 /* verify PDU length */
5bb0b55a
MC
682 tcp_conn->in.datalen = ntoh24(hdr->dlength);
683 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
322d739d
MC
684 iscsi_conn_printk(KERN_ERR, conn,
685 "iscsi_tcp: datalen %d > %d\n",
686 tcp_conn->in.datalen, conn->max_recv_dlength);
7ba24713
AA
687 return ISCSI_ERR_DATALEN;
688 }
7ba24713 689
da32dd68
OK
690 /* Additional header segments. So far, we don't
691 * process additional headers.
692 */
5bb0b55a 693 ahslen = hdr->hlength << 2;
7ba24713 694
5bb0b55a 695 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
7ba24713 696 /* verify itt (itt encoding: age+cid+itt) */
0af967f5 697 rc = iscsi_verify_itt(conn, hdr->itt);
7a53dc52 698 if (rc)
5bb0b55a 699 return rc;
7ba24713 700
da32dd68
OK
701 debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
702 opcode, ahslen, tcp_conn->in.datalen);
7ba24713 703
5bb0b55a
MC
704 switch(opcode) {
705 case ISCSI_OP_SCSI_DATA_IN:
913e5bf4 706 spin_lock(&conn->session->lock);
135a8ad4
MC
707 task = iscsi_itt_to_ctask(conn, hdr->itt);
708 if (!task)
913e5bf4
MC
709 rc = ISCSI_ERR_BAD_ITT;
710 else
30b49150 711 rc = iscsi_tcp_data_in(conn, task);
913e5bf4
MC
712 if (rc) {
713 spin_unlock(&conn->session->lock);
714 break;
715 }
0af967f5 716
da32dd68 717 if (tcp_conn->in.datalen) {
135a8ad4 718 struct iscsi_tcp_task *tcp_task = task->dd_data;
da32dd68 719 struct hash_desc *rx_hash = NULL;
135a8ad4 720 struct scsi_data_buffer *sdb = scsi_in(task->sc);
da32dd68
OK
721
722 /*
723 * Setup copy of Data-In into the Scsi_Cmnd
724 * Scatterlist case:
a8ac6311 725 * We set up the iscsi_segment to point to the next
da32dd68
OK
726 * 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 */
63c62f1c
MC
730 if (conn->datadgst_en &&
731 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
da32dd68
OK
732 rx_hash = &tcp_conn->rx_hash;
733
734 debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
735 "datalen=%d)\n", tcp_conn,
135a8ad4 736 tcp_task->data_offset,
da32dd68 737 tcp_conn->in.datalen);
913e5bf4
MC
738 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;
da32dd68 747 }
913e5bf4
MC
748 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
749 spin_unlock(&conn->session->lock);
750 break;
5bb0b55a 751 case ISCSI_OP_SCSI_CMD_RSP:
da32dd68
OK
752 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);
5bb0b55a
MC
757 break;
758 case ISCSI_OP_R2T:
913e5bf4 759 spin_lock(&conn->session->lock);
135a8ad4
MC
760 task = iscsi_itt_to_ctask(conn, hdr->itt);
761 if (!task)
913e5bf4
MC
762 rc = ISCSI_ERR_BAD_ITT;
763 else if (ahslen)
5bb0b55a 764 rc = ISCSI_ERR_AHSLEN;
913e5bf4 765 else if (task->sc->sc_data_direction == DMA_TO_DEVICE)
30b49150 766 rc = iscsi_tcp_r2t_rsp(conn, task);
913e5bf4 767 else
5bb0b55a 768 rc = ISCSI_ERR_PROTO;
913e5bf4 769 spin_unlock(&conn->session->lock);
5bb0b55a
MC
770 break;
771 case ISCSI_OP_LOGIN_RSP:
772 case ISCSI_OP_TEXT_RSP:
5bb0b55a
MC
773 case ISCSI_OP_REJECT:
774 case ISCSI_OP_ASYNC_EVENT:
c8dc1e52
MC
775 /*
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 */
da32dd68 780 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
322d739d
MC
781 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);
c8dc1e52
MC
787 rc = ISCSI_ERR_PROTO;
788 break;
789 }
790
da32dd68
OK
791 /* 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 }
5bb0b55a 798 /* fall through */
c8dc1e52
MC
799 case ISCSI_OP_LOGOUT_RSP:
800 case ISCSI_OP_NOOP_IN:
5bb0b55a
MC
801 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 }
7ba24713 808
da32dd68
OK
809 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);
7ba24713
AA
815 }
816
da32dd68 817 return rc;
7ba24713
AA
818}
819
da32dd68
OK
820/**
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 */
827static int
828iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
a8ac6311 829 struct iscsi_segment *segment)
7ba24713 830{
da32dd68
OK
831 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
832 struct iscsi_hdr *hdr;
7ba24713 833
da32dd68
OK
834 /* 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;
a8ac6311 839 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
da32dd68
OK
840 /* 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;
844
845 /* Make sure we don't overflow */
846 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
847 return ISCSI_ERR_AHSLEN;
848
a8ac6311
OK
849 segment->total_size += ahslen;
850 segment->size += ahslen;
da32dd68 851 return 0;
7ba24713
AA
852 }
853
da32dd68
OK
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) {
a8ac6311 858 if (segment->digest_len == 0) {
63c62f1c
MC
859 /*
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 */
a8ac6311
OK
864 iscsi_tcp_segment_splice_digest(segment,
865 segment->recv_digest);
da32dd68 866 return 0;
7ba24713
AA
867 }
868
63c62f1c
MC
869 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 }
7ba24713 877 }
da32dd68
OK
878
879 tcp_conn->in.hdr = hdr;
880 return iscsi_tcp_hdr_dissect(conn, hdr);
7ba24713
AA
881}
882
63c62f1c
MC
883inline 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
7ba24713 895/**
63c62f1c
MC
896 * iscsi_tcp_recv_skb - Process skb
897 * @conn: iscsi connection
898 * @skb: network buffer with header and/or data segment
7ba24713 899 * @offset: offset in skb
63c62f1c
MC
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)
7ba24713 904{
5bb0b55a 905 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
a8ac6311 906 struct iscsi_segment *segment = &tcp_conn->in.segment;
da32dd68
OK
907 struct skb_seq_state seq;
908 unsigned int consumed = 0;
909 int rc = 0;
7ba24713 910
da32dd68 911 debug_tcp("in %d bytes\n", skb->len - offset);
7ba24713
AA
912
913 if (unlikely(conn->suspend_rx)) {
914 debug_tcp("conn %d Rx suspended!\n", conn->id);
63c62f1c 915 *status = ISCSI_TCP_SUSPENDED;
7ba24713
AA
916 return 0;
917 }
918
63c62f1c
MC
919 if (offloaded) {
920 segment->total_copied = segment->total_size;
921 goto segment_done;
922 }
923
da32dd68
OK
924 skb_prepare_seq_read(skb, offset, skb->len, &seq);
925 while (1) {
926 unsigned int avail;
927 const u8 *ptr;
7ba24713 928
da32dd68 929 avail = skb_seq_read(consumed, &ptr, &seq);
a8ac6311
OK
930 if (avail == 0) {
931 debug_tcp("no more data avail. Consumed %d\n",
932 consumed);
63c62f1c
MC
933 *status = ISCSI_TCP_SKB_DONE;
934 skb_abort_seq_read(&seq);
935 goto skb_done;
a8ac6311
OK
936 }
937 BUG_ON(segment->copied >= segment->size);
da32dd68
OK
938
939 debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
a8ac6311 940 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
da32dd68
OK
941 BUG_ON(rc == 0);
942 consumed += rc;
943
a8ac6311 944 if (segment->total_copied >= segment->total_size) {
63c62f1c
MC
945 skb_abort_seq_read(&seq);
946 goto segment_done;
7ba24713 947 }
7ba24713 948 }
63c62f1c
MC
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:
da32dd68
OK
963 conn->rxdata_octets += consumed;
964 return consumed;
63c62f1c
MC
965}
966EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
7ba24713 967
63c62f1c
MC
968/**
969 * iscsi_tcp_recv - TCP receive in sendfile fashion
970 * @rd_desc: read descriptor
971 * @skb: socket buffer
972 * @offset: offset in skb
973 * @len: skb->len - offset
974 **/
975static int
976iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
977 unsigned int offset, size_t len)
978{
979 struct iscsi_conn *conn = rd_desc->arg.data;
980 unsigned int consumed, total_consumed = 0;
981 int status;
982
983 debug_tcp("in %d bytes\n", skb->len - offset);
984
985 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);
991
992 debug_tcp("read %d bytes status %d\n", skb->len - offset, status);
993 return total_consumed;
7ba24713
AA
994}
995
996static void
997iscsi_tcp_data_ready(struct sock *sk, int flag)
998{
999 struct iscsi_conn *conn = sk->sk_user_data;
da32dd68 1000 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
7ba24713
AA
1001 read_descriptor_t rd_desc;
1002
1003 read_lock(&sk->sk_callback_lock);
1004
665b44ae 1005 /*
da32dd68 1006 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
665b44ae 1007 * We set count to 1 because we want the network layer to
da32dd68 1008 * hand us all the skbs that are available. iscsi_tcp_recv
665b44ae
MC
1009 * handled pdus that cross buffers or pdus that still need data.
1010 */
7ba24713 1011 rd_desc.arg.data = conn;
665b44ae 1012 rd_desc.count = 1;
da32dd68 1013 tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
7ba24713
AA
1014
1015 read_unlock(&sk->sk_callback_lock);
da32dd68
OK
1016
1017 /* If we had to (atomically) map a highmem page,
1018 * unmap it now. */
a8ac6311 1019 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
7ba24713
AA
1020}
1021
1022static void
1023iscsi_tcp_state_change(struct sock *sk)
1024{
5bb0b55a 1025 struct iscsi_tcp_conn *tcp_conn;
7ba24713
AA
1026 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
e6273993
MC
1035 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1036 sk->sk_state == TCP_CLOSE) &&
1037 !atomic_read(&sk->sk_rmem_alloc)) {
7ba24713
AA
1038 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1039 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1040 }
1041
5bb0b55a
MC
1042 tcp_conn = conn->dd_data;
1043 old_state_change = tcp_conn->old_state_change;
7ba24713
AA
1044
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 **/
30b49150 1054static void iscsi_tcp_write_space(struct sock *sk)
7ba24713
AA
1055{
1056 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
5bb0b55a
MC
1057 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1058
1059 tcp_conn->old_write_space(sk);
7ba24713 1060 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
55e3299d 1061 scsi_queue_work(conn->session->host, &conn->xmitwork);
7ba24713
AA
1062}
1063
30b49150 1064static void iscsi_tcp_conn_set_callbacks(struct iscsi_conn *conn)
7ba24713 1065{
5bb0b55a
MC
1066 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1067 struct sock *sk = tcp_conn->sock->sk;
7ba24713
AA
1068
1069 /* assign new callbacks */
1070 write_lock_bh(&sk->sk_callback_lock);
1071 sk->sk_user_data = conn;
5bb0b55a
MC
1072 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;
7ba24713
AA
1075 sk->sk_data_ready = iscsi_tcp_data_ready;
1076 sk->sk_state_change = iscsi_tcp_state_change;
30b49150 1077 sk->sk_write_space = iscsi_tcp_write_space;
7ba24713
AA
1078 write_unlock_bh(&sk->sk_callback_lock);
1079}
1080
30b49150 1081static void iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
7ba24713 1082{
5bb0b55a 1083 struct sock *sk = tcp_conn->sock->sk;
7ba24713
AA
1084
1085 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1086 write_lock_bh(&sk->sk_callback_lock);
1087 sk->sk_user_data = NULL;
5bb0b55a
MC
1088 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;
7ba24713
AA
1091 sk->sk_no_check = 0;
1092 write_unlock_bh(&sk->sk_callback_lock);
1093}
1094
1095/**
30b49150 1096 * iscsi_tcp_xmit - TCP transmit
a8ac6311 1097 **/
30b49150 1098static int iscsi_tcp_xmit(struct iscsi_conn *conn)
7ba24713 1099{
5bb0b55a 1100 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
a8ac6311
OK
1101 struct iscsi_segment *segment = &tcp_conn->out.segment;
1102 unsigned int consumed = 0;
1103 int rc = 0;
7ba24713 1104
a8ac6311
OK
1105 while (1) {
1106 rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
6f481e3c
MC
1107 if (rc < 0) {
1108 rc = ISCSI_ERR_XMIT_FAILED;
a8ac6311 1109 goto error;
6f481e3c 1110 }
a8ac6311
OK
1111 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);
6f481e3c 1119 if (rc != 0)
a8ac6311
OK
1120 goto error;
1121 }
1122 }
3219e529
MC
1123 }
1124
a8ac6311
OK
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);
6f481e3c
MC
1134 iscsi_conn_failure(conn, rc);
1135 return -EIO;
7ba24713
AA
1136}
1137
1138/**
a8ac6311
OK
1139 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
1140 */
7ba24713 1141static inline int
a8ac6311 1142iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
7ba24713 1143{
a8ac6311
OK
1144 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1145 struct iscsi_segment *segment = &tcp_conn->out.segment;
7ba24713 1146
a8ac6311 1147 return segment->total_copied - segment->total_size;
7ba24713
AA
1148}
1149
e5a7efef 1150static int iscsi_tcp_flush(struct iscsi_task *task)
7ba24713 1151{
e5a7efef 1152 struct iscsi_conn *conn = task->conn;
a8ac6311
OK
1153 int rc;
1154
1155 while (iscsi_tcp_xmit_qlen(conn)) {
30b49150 1156 rc = iscsi_tcp_xmit(conn);
a8ac6311 1157 if (rc == 0)
7ba24713 1158 return -EAGAIN;
a8ac6311
OK
1159 if (rc < 0)
1160 return rc;
3219e529 1161 }
7ba24713 1162
a8ac6311 1163 return 0;
7ba24713
AA
1164}
1165
a8ac6311
OK
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)
7ba24713 1173{
a8ac6311
OK
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
3a12b199 1185 debug_tcp("%s(%p%s)\n", __func__, tcp_conn,
a8ac6311
OK
1186 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
135a8ad4 1194 * sure that both iscsi_tcp_task and mtask have
a8ac6311
OK
1195 * sufficient room.
1196 */
1197 if (conn->hdrdgst_en) {
1198 iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
1199 hdr + hdrlen);
1200 hdrlen += ISCSI_DIGEST_SIZE;
1201 }
1202
1203 /* 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);
1210}
1211
1212/*
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)
1221{
1222 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1223 struct hash_desc *tx_hash = NULL;
1224 unsigned int hdr_spec_len;
1225
3a12b199 1226 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __func__,
a8ac6311
OK
1227 tcp_conn, offset, len,
1228 conn->datadgst_en? ", digest enabled" : "");
1229
1230 /* 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));
1234
1235 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);
1241}
1242
1243static void
1244iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
1245 size_t len)
1246{
1247 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1248 struct hash_desc *tx_hash = NULL;
1249 unsigned int hdr_spec_len;
1250
3a12b199 1251 debug_tcp("%s(%p, datalen=%d%s)\n", __func__, tcp_conn, len,
a8ac6311
OK
1252 conn->datadgst_en? ", digest enabled" : "");
1253
1254 /* 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));
1258
1259 if (conn->datadgst_en)
1260 tx_hash = &tcp_conn->tx_hash;
1261
1262 iscsi_segment_init_linear(&tcp_conn->out.data_segment,
1263 data, len, NULL, tx_hash);
7ba24713
AA
1264}
1265
e5a7efef
MC
1266static 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
7ba24713 1302/**
fbc514b4 1303 * iscsi_tcp_task - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
7ba24713 1304 * @conn: iscsi connection
135a8ad4 1305 * @task: scsi command task
7ba24713
AA
1306 * @sc: scsi command
1307 **/
e5a7efef 1308static int iscsi_tcp_task_init(struct iscsi_task *task)
7ba24713 1309{
135a8ad4
MC
1310 struct iscsi_tcp_task *tcp_task = task->dd_data;
1311 struct iscsi_conn *conn = task->conn;
1312 struct scsi_cmnd *sc = task->sc;
a8ac6311 1313 int err;
7ba24713 1314
fbc514b4
MC
1315 if (!sc) {
1316 /*
135a8ad4 1317 * mgmt tasks do not have a scatterlist since they come
fbc514b4
MC
1318 * in from the iscsi interface.
1319 */
135a8ad4
MC
1320 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id,
1321 task->itt);
fbc514b4 1322
e5a7efef 1323 return conn->session->tt->init_pdu(task, 0, task->data_count);
fbc514b4
MC
1324 }
1325
135a8ad4 1326 BUG_ON(__kfifo_len(tcp_task->r2tqueue));
135a8ad4 1327 tcp_task->exp_datasn = 0;
a8ac6311
OK
1328
1329 /* Prepare PDU, optionally w/ immediate data */
135a8ad4
MC
1330 debug_scsi("task deq [cid %d itt 0x%x imm %d unsol %d]\n",
1331 conn->id, task->itt, task->imm_count,
e5a7efef 1332 task->unsol_r2t.data_length);
a8ac6311 1333
e5a7efef 1334 err = conn->session->tt->init_pdu(task, 0, task->imm_count);
a8ac6311
OK
1335 if (err)
1336 return err;
135a8ad4 1337 task->imm_count = 0;
a8ac6311 1338 return 0;
7ba24713
AA
1339}
1340
e5a7efef
MC
1341static 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
a8ac6311 1374/*
135a8ad4
MC
1375 * iscsi_tcp_task_xmit - xmit normal PDU task
1376 * @task: iscsi command task
a8ac6311
OK
1377 *
1378 * 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 */
e5a7efef 1382static int iscsi_tcp_task_xmit(struct iscsi_task *task)
7ba24713 1383{
135a8ad4 1384 struct iscsi_conn *conn = task->conn;
e5a7efef
MC
1385 struct iscsi_session *session = conn->session;
1386 struct iscsi_r2t_info *r2t;
218432c6 1387 int rc = 0;
3219e529 1388
a8ac6311
OK
1389flush:
1390 /* Flush any pending data first. */
e5a7efef 1391 rc = session->tt->xmit_pdu(task);
a8ac6311 1392 if (rc < 0)
62f38300
MC
1393 return rc;
1394
fbc514b4 1395 /* mgmt command */
e5a7efef 1396 if (!task->sc) {
135a8ad4
MC
1397 if (task->hdr->itt == RESERVED_ITT)
1398 iscsi_put_task(task);
fbc514b4
MC
1399 return 0;
1400 }
1401
a8ac6311 1402 /* Are we done already? */
e5a7efef 1403 if (task->sc->sc_data_direction != DMA_TO_DEVICE)
a8ac6311 1404 return 0;
7ba24713 1405
e5a7efef
MC
1406 r2t = iscsi_tcp_get_curr_r2t(task);
1407 if (r2t == NULL) {
a8ac6311 1408 /* Waiting for more R2Ts to arrive. */
e5a7efef
MC
1409 debug_tcp("no R2Ts yet\n");
1410 return 0;
1411 }
7ba24713 1412
e5a7efef
MC
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);
7ba24713 1417
e5a7efef
MC
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);
7ba24713 1421
e5a7efef
MC
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;
7ba24713
AA
1428}
1429
5bb0b55a
MC
1430static struct iscsi_cls_conn *
1431iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
7ba24713 1432{
5bb0b55a
MC
1433 struct iscsi_conn *conn;
1434 struct iscsi_cls_conn *cls_conn;
1435 struct iscsi_tcp_conn *tcp_conn;
7ba24713 1436
5d91e209 1437 cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx);
5bb0b55a
MC
1438 if (!cls_conn)
1439 return NULL;
1440 conn = cls_conn->dd_data;
7ba24713 1441 /*
5bb0b55a
MC
1442 * due to strange issues with iser these are not set
1443 * in iscsi_conn_setup
7ba24713 1444 */
bf32ed33 1445 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
7ba24713 1446
5d91e209 1447 tcp_conn = conn->dd_data;
5bb0b55a 1448 tcp_conn->iscsi_conn = conn;
7ba24713 1449
c9802cd9
JB
1450 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1451 CRYPTO_ALG_ASYNC);
1452 tcp_conn->tx_hash.flags = 0;
322d739d 1453 if (IS_ERR(tcp_conn->tx_hash.tfm))
5d91e209 1454 goto free_conn;
dd8c0d95 1455
c9802cd9
JB
1456 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1457 CRYPTO_ALG_ASYNC);
1458 tcp_conn->rx_hash.flags = 0;
322d739d 1459 if (IS_ERR(tcp_conn->rx_hash.tfm))
dd8c0d95
MC
1460 goto free_tx_tfm;
1461
5bb0b55a 1462 return cls_conn;
7ba24713 1463
dd8c0d95 1464free_tx_tfm:
c9802cd9 1465 crypto_free_hash(tcp_conn->tx_hash.tfm);
5d91e209 1466free_conn:
322d739d
MC
1467 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");
5bb0b55a
MC
1472 iscsi_conn_teardown(cls_conn);
1473 return NULL;
7ba24713
AA
1474}
1475
1c83469d
MC
1476static void
1477iscsi_tcp_release_conn(struct iscsi_conn *conn)
1478{
22236961 1479 struct iscsi_session *session = conn->session;
1c83469d 1480 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
22236961 1481 struct socket *sock = tcp_conn->sock;
1c83469d 1482
22236961 1483 if (!sock)
1c83469d
MC
1484 return;
1485
22236961 1486 sock_hold(sock->sk);
1c83469d 1487 iscsi_conn_restore_callbacks(tcp_conn);
22236961 1488 sock_put(sock->sk);
1c83469d 1489
22236961 1490 spin_lock_bh(&session->lock);
1c83469d 1491 tcp_conn->sock = NULL;
22236961
MC
1492 spin_unlock_bh(&session->lock);
1493 sockfd_put(sock);
1c83469d
MC
1494}
1495
7ba24713 1496static void
5bb0b55a 1497iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
7ba24713 1498{
5bb0b55a
MC
1499 struct iscsi_conn *conn = cls_conn->dd_data;
1500 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
7ba24713 1501
1c83469d 1502 iscsi_tcp_release_conn(conn);
7ba24713 1503
534284a0
PW
1504 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);
7ba24713 1508
5d91e209 1509 iscsi_conn_teardown(cls_conn);
5bb0b55a 1510}
7ba24713 1511
1c83469d
MC
1512static void
1513iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1514{
1515 struct iscsi_conn *conn = cls_conn->dd_data;
913e5bf4
MC
1516 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);
1c83469d
MC
1529
1530 iscsi_conn_stop(cls_conn, flag);
1531 iscsi_tcp_release_conn(conn);
1532}
1533
22236961
MC
1534static 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
a9204879 1544 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
22236961
MC
1545 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);
63779436 1557 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
22236961
MC
1558 *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);
5b095d98 1564 sprintf(buf, "%pI6", &sin6->sin6_addr);
22236961
MC
1565 *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
5bb0b55a
MC
1574static int
1575iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
264faaaa 1576 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
5bb0b55a
MC
1577 int is_leading)
1578{
75613521
MC
1579 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1580 struct iscsi_host *ihost = shost_priv(shost);
5bb0b55a
MC
1581 struct iscsi_conn *conn = cls_conn->dd_data;
1582 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1583 struct sock *sk;
1584 struct socket *sock;
1585 int err;
7ba24713 1586
5bb0b55a 1587 /* lookup for existing socket */
264faaaa 1588 sock = sockfd_lookup((int)transport_eph, &err);
5bb0b55a 1589 if (!sock) {
322d739d
MC
1590 iscsi_conn_printk(KERN_ERR, conn,
1591 "sockfd_lookup failed %d\n", err);
5bb0b55a 1592 return -EEXIST;
7ba24713 1593 }
22236961
MC
1594 /*
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
75613521
MC
1604 err = iscsi_tcp_get_addr(conn, sock, ihost->local_address,
1605 &ihost->local_port, kernel_getsockname);
22236961
MC
1606 if (err)
1607 goto free_socket;
7ba24713 1608
5bb0b55a
MC
1609 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1610 if (err)
22236961 1611 goto free_socket;
7ba24713 1612
67a61114
MC
1613 /* bind iSCSI connection and socket */
1614 tcp_conn->sock = sock;
7ba24713 1615
67a61114
MC
1616 /* 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;
7ba24713 1621
30b49150 1622 iscsi_tcp_conn_set_callbacks(conn);
67a61114
MC
1623 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1624 /*
1625 * set receive state machine into initial state
1626 */
da32dd68 1627 iscsi_tcp_hdr_recv_prep(tcp_conn);
7ba24713 1628 return 0;
22236961
MC
1629
1630free_socket:
1631 sockfd_put(sock);
1632 return err;
7ba24713
AA
1633}
1634
7ba24713
AA
1635static int
1636iscsi_r2tpool_alloc(struct iscsi_session *session)
1637{
1638 int i;
1639 int cmd_i;
1640
1641 /*
135a8ad4 1642 * initialize per-task: R2T pool and xmit queue
7ba24713
AA
1643 */
1644 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
135a8ad4
MC
1645 struct iscsi_task *task = session->cmds[cmd_i];
1646 struct iscsi_tcp_task *tcp_task = task->dd_data;
7ba24713
AA
1647
1648 /*
e5a7efef 1649 * pre-allocated x2 as much r2ts to handle race when
7ba24713
AA
1650 * target acks DataOut faster than we data_xmit() queues
1651 * could replenish r2tqueue.
1652 */
1653
1654 /* R2T pool */
e5a7efef
MC
1655 if (iscsi_pool_init(&tcp_task->r2tpool,
1656 session->max_r2t * 2, NULL,
5bb0b55a 1657 sizeof(struct iscsi_r2t_info))) {
7ba24713
AA
1658 goto r2t_alloc_fail;
1659 }
1660
1661 /* R2T xmit queue */
135a8ad4 1662 tcp_task->r2tqueue = kfifo_alloc(
7ba24713 1663 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
135a8ad4
MC
1664 if (tcp_task->r2tqueue == ERR_PTR(-ENOMEM)) {
1665 iscsi_pool_free(&tcp_task->r2tpool);
7ba24713
AA
1666 goto r2t_alloc_fail;
1667 }
7ba24713
AA
1668 }
1669
1670 return 0;
1671
1672r2t_alloc_fail:
1673 for (i = 0; i < cmd_i; i++) {
135a8ad4
MC
1674 struct iscsi_task *task = session->cmds[i];
1675 struct iscsi_tcp_task *tcp_task = task->dd_data;
5bb0b55a 1676
135a8ad4
MC
1677 kfifo_free(tcp_task->r2tqueue);
1678 iscsi_pool_free(&tcp_task->r2tpool);
7ba24713
AA
1679 }
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++) {
135a8ad4
MC
1689 struct iscsi_task *task = session->cmds[i];
1690 struct iscsi_tcp_task *tcp_task = task->dd_data;
7ba24713 1691
135a8ad4
MC
1692 kfifo_free(tcp_task->r2tqueue);
1693 iscsi_pool_free(&tcp_task->r2tpool);
7ba24713 1694 }
7ba24713
AA
1695}
1696
1697static int
7b7232f3 1698iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
5c75b7fc 1699 char *buf, int buflen)
7ba24713 1700{
7b7232f3 1701 struct iscsi_conn *conn = cls_conn->dd_data;
7ba24713 1702 struct iscsi_session *session = conn->session;
5bb0b55a 1703 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
5c75b7fc 1704 int value;
7ba24713 1705
7ba24713 1706 switch(param) {
7ba24713 1707 case ISCSI_PARAM_HDRDGST_EN:
5c75b7fc 1708 iscsi_set_param(cls_conn, param, buf, buflen);
7ba24713
AA
1709 break;
1710 case ISCSI_PARAM_DATADGST_EN:
5c75b7fc 1711 iscsi_set_param(cls_conn, param, buf, buflen);
5bb0b55a
MC
1712 tcp_conn->sendpage = conn->datadgst_en ?
1713 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
7ba24713 1714 break;
7ba24713 1715 case ISCSI_PARAM_MAX_R2T:
5c75b7fc 1716 sscanf(buf, "%d", &value);
df93ffcd
MC
1717 if (value <= 0 || !is_power_of_2(value))
1718 return -EINVAL;
1719 if (session->max_r2t == value)
7ba24713
AA
1720 break;
1721 iscsi_r2tpool_free(session);
5c75b7fc 1722 iscsi_set_param(cls_conn, param, buf, buflen);
7ba24713
AA
1723 if (iscsi_r2tpool_alloc(session))
1724 return -ENOMEM;
1725 break;
7ba24713 1726 default:
5c75b7fc 1727 return iscsi_set_param(cls_conn, param, buf, buflen);
7ba24713
AA
1728 }
1729
1730 return 0;
1731}
1732
1733static int
5c75b7fc
MC
1734iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
1735 enum iscsi_param param, char *buf)
7b8631b5 1736{
7b7232f3 1737 struct iscsi_conn *conn = cls_conn->dd_data;
5c75b7fc 1738 int len;
7b8631b5
MC
1739
1740 switch(param) {
fd7255f5 1741 case ISCSI_PARAM_CONN_PORT:
22236961
MC
1742 spin_lock_bh(&conn->session->lock);
1743 len = sprintf(buf, "%hu\n", conn->portal_port);
1744 spin_unlock_bh(&conn->session->lock);
8d2860b3 1745 break;
fd7255f5 1746 case ISCSI_PARAM_CONN_ADDRESS:
22236961
MC
1747 spin_lock_bh(&conn->session->lock);
1748 len = sprintf(buf, "%s\n", conn->portal_address);
1749 spin_unlock_bh(&conn->session->lock);
fd7255f5
MC
1750 break;
1751 default:
5c75b7fc 1752 return iscsi_conn_get_param(cls_conn, param, buf);
fd7255f5
MC
1753 }
1754
1755 return len;
1756}
1757
7ba24713 1758static void
7b7232f3 1759iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
7ba24713 1760{
7b7232f3 1761 struct iscsi_conn *conn = cls_conn->dd_data;
5bb0b55a 1762 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
7ba24713
AA
1763
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");
5bb0b55a 1775 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
7ba24713 1776 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
5bb0b55a 1777 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
7ba24713
AA
1778 strcpy(stats->custom[2].desc, "eh_abort_cnt");
1779 stats->custom[2].value = conn->eh_abort_cnt;
1780}
1781
5bb0b55a 1782static struct iscsi_cls_session *
06520ede 1783iscsi_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
40753caa
MC
1784 uint16_t qdepth, uint32_t initial_cmdsn,
1785 uint32_t *hostno)
7ba24713 1786{
5bb0b55a
MC
1787 struct iscsi_cls_session *cls_session;
1788 struct iscsi_session *session;
06520ede 1789 struct Scsi_Host *shost;
7ba24713 1790
06520ede
MC
1791 if (ep) {
1792 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
75613521
MC
1793 return NULL;
1794 }
1795
a4804cd6 1796 shost = iscsi_host_alloc(&iscsi_sht, 0, qdepth);
75613521 1797 if (!shost)
5bb0b55a 1798 return NULL;
75613521
MC
1799 shost->transportt = iscsi_tcp_scsi_transport;
1800 shost->max_lun = iscsi_max_lun;
1801 shost->max_id = 0;
1802 shost->max_channel = 0;
30e9ba9f 1803 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
75613521 1804
a4804cd6 1805 if (iscsi_host_add(shost, NULL))
75613521
MC
1806 goto free_host;
1807 *hostno = shost->host_no;
1808
1809 cls_session = iscsi_session_setup(&iscsi_tcp_transport, shost, cmds_max,
135a8ad4 1810 sizeof(struct iscsi_tcp_task),
7970634b 1811 initial_cmdsn, 0);
75613521
MC
1812 if (!cls_session)
1813 goto remove_host;
1814 session = cls_session->dd_data;
7ba24713 1815
fbc514b4 1816 shost->can_queue = session->scsi_cmds_max;
75613521
MC
1817 if (iscsi_r2tpool_alloc(session))
1818 goto remove_session;
5bb0b55a
MC
1819 return cls_session;
1820
75613521 1821remove_session:
5bb0b55a 1822 iscsi_session_teardown(cls_session);
75613521 1823remove_host:
a4804cd6 1824 iscsi_host_remove(shost);
75613521 1825free_host:
a4804cd6 1826 iscsi_host_free(shost);
5bb0b55a
MC
1827 return NULL;
1828}
1829
1830static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
1831{
75613521
MC
1832 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1833
1834 iscsi_r2tpool_free(cls_session->dd_data);
e5bd7b54 1835 iscsi_session_teardown(cls_session);
75613521 1836
a4804cd6
MC
1837 iscsi_host_remove(shost);
1838 iscsi_host_free(shost);
7ba24713
AA
1839}
1840
d1d81c01
MC
1841static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
1842{
b6d44fe9 1843 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
d1d81c01
MC
1844 blk_queue_dma_alignment(sdev->request_queue, 0);
1845 return 0;
1846}
1847
5bb0b55a 1848static struct scsi_host_template iscsi_sht = {
7974392c 1849 .module = THIS_MODULE,
f4246b33 1850 .name = "iSCSI Initiator over TCP/IP",
5bb0b55a
MC
1851 .queuecommand = iscsi_queuecommand,
1852 .change_queue_depth = iscsi_change_queue_depth,
1548271e 1853 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
66bbe0ce 1854 .sg_tablesize = 4096,
8231f0ed 1855 .max_sectors = 0xFFFF,
5bb0b55a
MC
1856 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
1857 .eh_abort_handler = iscsi_eh_abort,
843c0a8a 1858 .eh_device_reset_handler= iscsi_eh_device_reset,
8e124525 1859 .eh_target_reset_handler= iscsi_eh_target_reset,
5bb0b55a 1860 .use_clustering = DISABLE_CLUSTERING,
d1d81c01 1861 .slave_configure = iscsi_tcp_slave_configure,
5bb0b55a
MC
1862 .proc_name = "iscsi_tcp",
1863 .this_id = -1,
1864};
1865
7ba24713
AA
1866static 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,
fd7255f5
MC
1871 .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 |
8d2860b3 1884 ISCSI_CONN_ADDRESS |
5c75b7fc
MC
1885 ISCSI_EXP_STATSN |
1886 ISCSI_PERSISTENT_PORT |
1887 ISCSI_PERSISTENT_ADDRESS |
b2c64167
MC
1888 ISCSI_TARGET_NAME | ISCSI_TPGT |
1889 ISCSI_USERNAME | ISCSI_PASSWORD |
843c0a8a 1890 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
f6d5180c
MC
1891 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
1892 ISCSI_LU_RESET_TMO |
88dfd340
MC
1893 ISCSI_PING_TMO | ISCSI_RECV_TMO |
1894 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
22236961 1895 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
d8196ed2
MC
1896 ISCSI_HOST_INITIATOR_NAME |
1897 ISCSI_HOST_NETDEV_NAME,
5bb0b55a
MC
1898 /* 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,
7ba24713 1905 .set_param = iscsi_conn_set_param,
5c75b7fc 1906 .get_conn_param = iscsi_tcp_conn_get_param,
7b8631b5 1907 .get_session_param = iscsi_session_get_param,
7ba24713 1908 .start_conn = iscsi_conn_start,
1c83469d 1909 .stop_conn = iscsi_tcp_conn_stop,
0801c242 1910 /* iscsi host params */
75613521 1911 .get_host_param = iscsi_host_get_param,
0801c242 1912 .set_host_param = iscsi_host_set_param,
5bb0b55a 1913 /* IO */
7ba24713
AA
1914 .send_pdu = iscsi_conn_send_pdu,
1915 .get_stats = iscsi_conn_get_stats,
e5a7efef 1916 /* iscsi task/cmd helpers */
fbc514b4
MC
1917 .init_task = iscsi_tcp_task_init,
1918 .xmit_task = iscsi_tcp_task_xmit,
1919 .cleanup_task = iscsi_tcp_cleanup_task,
e5a7efef
MC
1920 /* low level pdu helpers */
1921 .xmit_pdu = iscsi_tcp_flush,
1922 .init_pdu = iscsi_tcp_pdu_init,
1923 .alloc_pdu = iscsi_tcp_pdu_alloc,
5bb0b55a 1924 /* recovery */
30a6c652 1925 .session_recovery_timedout = iscsi_session_recovery_timedout,
7ba24713
AA
1926};
1927
1928static int __init
1929iscsi_tcp_init(void)
1930{
7ba24713 1931 if (iscsi_max_lun < 1) {
be2df72e
OG
1932 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
1933 iscsi_max_lun);
7ba24713
AA
1934 return -EINVAL;
1935 }
7ba24713 1936
75613521
MC
1937 iscsi_tcp_scsi_transport = iscsi_register_transport(
1938 &iscsi_tcp_transport);
1939 if (!iscsi_tcp_scsi_transport)
ffbfe925 1940 return -ENODEV;
7ba24713 1941
7b8631b5 1942 return 0;
7ba24713
AA
1943}
1944
1945static void __exit
1946iscsi_tcp_exit(void)
1947{
1948 iscsi_unregister_transport(&iscsi_tcp_transport);
7ba24713
AA
1949}
1950
1951module_init(iscsi_tcp_init);
1952module_exit(iscsi_tcp_exit);
This page took 1.022494 seconds and 5 git commands to generate.