aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/libiscsi_tcp.c
diff options
context:
space:
mode:
authorMike Christie <michaelc@cs.wisc.edu>2008-12-02 01:32:11 -0500
committerJames Bottomley <James.Bottomley@HansenPartnership.com>2008-12-29 12:24:22 -0500
commita081c13e39b5c17052a7b46fafa61019c4c110ff (patch)
treed00093d2e869b825d4e6aea62ff3b69b0c38c7b9 /drivers/scsi/libiscsi_tcp.c
parent30b4915015c112547e930cadf734d875aec8438d (diff)
[SCSI] iscsi_tcp: split module into lib and lld
As explained in the previous mails, cxgb3i needs iscsi_tcp's r2t/data_out and data_in procesing so this just moves functions that both drivers want to use to a new module libiscsi_tcp. The next patch will hook iscsi_tcp in. Signed-off-by: Mike Christie <michaelc@cs.wisc.edu> Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
Diffstat (limited to 'drivers/scsi/libiscsi_tcp.c')
-rw-r--r--drivers/scsi/libiscsi_tcp.c1160
1 files changed, 1160 insertions, 0 deletions
diff --git a/drivers/scsi/libiscsi_tcp.c b/drivers/scsi/libiscsi_tcp.c
new file mode 100644
index 000000000000..e86508936314
--- /dev/null
+++ b/drivers/scsi/libiscsi_tcp.c
@@ -0,0 +1,1160 @@
1/*
2 * iSCSI over TCP/IP Data-Path lib
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
6 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
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>
32#include <linux/file.h>
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>
40#include <scsi/scsi_device.h>
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("Mike Christie <michaelc@cs.wisc.edu>, "
48 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
49 "Alex Aizman <itn780@yahoo.com>");
50MODULE_DESCRIPTION("iSCSI/TCP data-path");
51MODULE_LICENSE("GPL");
52#undef DEBUG_TCP
53
54#ifdef DEBUG_TCP
55#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
56#else
57#define debug_tcp(fmt...)
58#endif
59
60static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
61 struct iscsi_segment *segment);
62
63/*
64 * Scatterlist handling: inside the iscsi_segment, we
65 * remember an index into the scatterlist, and set data/size
66 * to the current scatterlist entry. For highmem pages, we
67 * kmap as needed.
68 *
69 * Note that the page is unmapped when we return from
70 * TCP's data_ready handler, so we may end up mapping and
71 * unmapping the same page repeatedly. The whole reason
72 * for this is that we shouldn't keep the page mapped
73 * outside the softirq.
74 */
75
76/**
77 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
78 * @segment: the buffer object
79 * @sg: scatterlist
80 * @offset: byte offset into that sg entry
81 *
82 * This function sets up the segment so that subsequent
83 * data is copied to the indicated sg entry, at the given
84 * offset.
85 */
86static inline void
87iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
88 struct scatterlist *sg, unsigned int offset)
89{
90 segment->sg = sg;
91 segment->sg_offset = offset;
92 segment->size = min(sg->length - offset,
93 segment->total_size - segment->total_copied);
94 segment->data = NULL;
95}
96
97/**
98 * iscsi_tcp_segment_map - map the current S/G page
99 * @segment: iscsi_segment
100 * @recv: 1 if called from recv path
101 *
102 * We only need to possibly kmap data if scatter lists are being used,
103 * because the iscsi passthrough and internal IO paths will never use high
104 * mem pages.
105 */
106static void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
107{
108 struct scatterlist *sg;
109
110 if (segment->data != NULL || !segment->sg)
111 return;
112
113 sg = segment->sg;
114 BUG_ON(segment->sg_mapped);
115 BUG_ON(sg->length == 0);
116
117 /*
118 * If the page count is greater than one it is ok to send
119 * to the network layer's zero copy send path. If not we
120 * have to go the slow sendmsg path. We always map for the
121 * recv path.
122 */
123 if (page_count(sg_page(sg)) >= 1 && !recv)
124 return;
125
126 debug_tcp("iscsi_tcp_segment_map %s %p\n", recv ? "recv" : "xmit",
127 segment);
128 segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0);
129 segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
130}
131
132void iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
133{
134 debug_tcp("iscsi_tcp_segment_unmap %p\n", segment);
135
136 if (segment->sg_mapped) {
137 debug_tcp("iscsi_tcp_segment_unmap valid\n");
138 kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0);
139 segment->sg_mapped = NULL;
140 segment->data = NULL;
141 }
142}
143EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap);
144
145/*
146 * Splice the digest buffer into the buffer
147 */
148static inline void
149iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
150{
151 segment->data = digest;
152 segment->digest_len = ISCSI_DIGEST_SIZE;
153 segment->total_size += ISCSI_DIGEST_SIZE;
154 segment->size = ISCSI_DIGEST_SIZE;
155 segment->copied = 0;
156 segment->sg = NULL;
157 segment->hash = NULL;
158}
159
160/**
161 * iscsi_tcp_segment_done - check whether the segment is complete
162 * @segment: iscsi segment to check
163 * @recv: set to one of this is called from the recv path
164 * @copied: number of bytes copied
165 *
166 * Check if we're done receiving this segment. If the receive
167 * buffer is full but we expect more data, move on to the
168 * next entry in the scatterlist.
169 *
170 * If the amount of data we received isn't a multiple of 4,
171 * we will transparently receive the pad bytes, too.
172 *
173 * This function must be re-entrant.
174 */
175int iscsi_tcp_segment_done(struct iscsi_segment *segment, int recv,
176 unsigned copied)
177{
178 static unsigned char padbuf[ISCSI_PAD_LEN];
179 struct scatterlist sg;
180 unsigned int pad;
181
182 debug_tcp("copied %u %u size %u %s\n", segment->copied, copied,
183 segment->size, recv ? "recv" : "xmit");
184 if (segment->hash && copied) {
185 /*
186 * If a segment is kmapd we must unmap it before sending
187 * to the crypto layer since that will try to kmap it again.
188 */
189 iscsi_tcp_segment_unmap(segment);
190
191 if (!segment->data) {
192 sg_init_table(&sg, 1);
193 sg_set_page(&sg, sg_page(segment->sg), copied,
194 segment->copied + segment->sg_offset +
195 segment->sg->offset);
196 } else
197 sg_init_one(&sg, segment->data + segment->copied,
198 copied);
199 crypto_hash_update(segment->hash, &sg, copied);
200 }
201
202 segment->copied += copied;
203 if (segment->copied < segment->size) {
204 iscsi_tcp_segment_map(segment, recv);
205 return 0;
206 }
207
208 segment->total_copied += segment->copied;
209 segment->copied = 0;
210 segment->size = 0;
211
212 /* Unmap the current scatterlist page, if there is one. */
213 iscsi_tcp_segment_unmap(segment);
214
215 /* Do we have more scatterlist entries? */
216 debug_tcp("total copied %u total size %u\n", segment->total_copied,
217 segment->total_size);
218 if (segment->total_copied < segment->total_size) {
219 /* Proceed to the next entry in the scatterlist. */
220 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
221 0);
222 iscsi_tcp_segment_map(segment, recv);
223 BUG_ON(segment->size == 0);
224 return 0;
225 }
226
227 /* Do we need to handle padding? */
228 pad = iscsi_padding(segment->total_copied);
229 if (pad != 0) {
230 debug_tcp("consume %d pad bytes\n", pad);
231 segment->total_size += pad;
232 segment->size = pad;
233 segment->data = padbuf;
234 return 0;
235 }
236
237 /*
238 * Set us up for transferring the data digest. hdr digest
239 * is completely handled in hdr done function.
240 */
241 if (segment->hash) {
242 crypto_hash_final(segment->hash, segment->digest);
243 iscsi_tcp_segment_splice_digest(segment,
244 recv ? segment->recv_digest : segment->digest);
245 return 0;
246 }
247
248 return 1;
249}
250EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
251
252/**
253 * iscsi_tcp_segment_recv - copy data to segment
254 * @tcp_conn: the iSCSI TCP connection
255 * @segment: the buffer to copy to
256 * @ptr: data pointer
257 * @len: amount of data available
258 *
259 * This function copies up to @len bytes to the
260 * given buffer, and returns the number of bytes
261 * consumed, which can actually be less than @len.
262 *
263 * If hash digest is enabled, the function will update the
264 * hash while copying.
265 * Combining these two operations doesn't buy us a lot (yet),
266 * but in the future we could implement combined copy+crc,
267 * just way we do for network layer checksums.
268 */
269static int
270iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
271 struct iscsi_segment *segment, const void *ptr,
272 unsigned int len)
273{
274 unsigned int copy = 0, copied = 0;
275
276 while (!iscsi_tcp_segment_done(segment, 1, copy)) {
277 if (copied == len) {
278 debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n",
279 len);
280 break;
281 }
282
283 copy = min(len - copied, segment->size - segment->copied);
284 debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy);
285 memcpy(segment->data + segment->copied, ptr + copied, copy);
286 copied += copy;
287 }
288 return copied;
289}
290
291inline void
292iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
293 unsigned char digest[ISCSI_DIGEST_SIZE])
294{
295 struct scatterlist sg;
296
297 sg_init_one(&sg, hdr, hdrlen);
298 crypto_hash_digest(hash, &sg, hdrlen, digest);
299}
300EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
301
302static inline int
303iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
304 struct iscsi_segment *segment)
305{
306 if (!segment->digest_len)
307 return 1;
308
309 if (memcmp(segment->recv_digest, segment->digest,
310 segment->digest_len)) {
311 debug_scsi("digest mismatch\n");
312 return 0;
313 }
314
315 return 1;
316}
317
318/*
319 * Helper function to set up segment buffer
320 */
321static inline void
322__iscsi_segment_init(struct iscsi_segment *segment, size_t size,
323 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
324{
325 memset(segment, 0, sizeof(*segment));
326 segment->total_size = size;
327 segment->done = done;
328
329 if (hash) {
330 segment->hash = hash;
331 crypto_hash_init(hash);
332 }
333}
334
335inline void
336iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
337 size_t size, iscsi_segment_done_fn_t *done,
338 struct hash_desc *hash)
339{
340 __iscsi_segment_init(segment, size, done, hash);
341 segment->data = data;
342 segment->size = size;
343}
344EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
345
346inline int
347iscsi_segment_seek_sg(struct iscsi_segment *segment,
348 struct scatterlist *sg_list, unsigned int sg_count,
349 unsigned int offset, size_t size,
350 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
351{
352 struct scatterlist *sg;
353 unsigned int i;
354
355 debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n",
356 offset, size);
357 __iscsi_segment_init(segment, size, done, hash);
358 for_each_sg(sg_list, sg, sg_count, i) {
359 debug_scsi("sg %d, len %u offset %u\n", i, sg->length,
360 sg->offset);
361 if (offset < sg->length) {
362 iscsi_tcp_segment_init_sg(segment, sg, offset);
363 return 0;
364 }
365 offset -= sg->length;
366 }
367
368 return ISCSI_ERR_DATA_OFFSET;
369}
370EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
371
372/**
373 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
374 * @tcp_conn: iscsi connection to prep for
375 *
376 * This function always passes NULL for the hash argument, because when this
377 * function is called we do not yet know the final size of the header and want
378 * to delay the digest processing until we know that.
379 */
380void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
381{
382 debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn,
383 tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : "");
384 iscsi_segment_init_linear(&tcp_conn->in.segment,
385 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
386 iscsi_tcp_hdr_recv_done, NULL);
387}
388EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
389
390/*
391 * Handle incoming reply to any other type of command
392 */
393static int
394iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
395 struct iscsi_segment *segment)
396{
397 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
398 int rc = 0;
399
400 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
401 return ISCSI_ERR_DATA_DGST;
402
403 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
404 conn->data, tcp_conn->in.datalen);
405 if (rc)
406 return rc;
407
408 iscsi_tcp_hdr_recv_prep(tcp_conn);
409 return 0;
410}
411
412static void
413iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
414{
415 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
416 struct hash_desc *rx_hash = NULL;
417
418 if (conn->datadgst_en &
419 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
420 rx_hash = tcp_conn->rx_hash;
421
422 iscsi_segment_init_linear(&tcp_conn->in.segment,
423 conn->data, tcp_conn->in.datalen,
424 iscsi_tcp_data_recv_done, rx_hash);
425}
426
427/**
428 * iscsi_tcp_cleanup_task - free tcp_task resources
429 * @task: iscsi task
430 *
431 * must be called with session lock
432 */
433void iscsi_tcp_cleanup_task(struct iscsi_task *task)
434{
435 struct iscsi_tcp_task *tcp_task = task->dd_data;
436 struct iscsi_r2t_info *r2t;
437
438 /* nothing to do for mgmt or pending tasks */
439 if (!task->sc || task->state == ISCSI_TASK_PENDING)
440 return;
441
442 /* flush task's r2t queues */
443 while (__kfifo_get(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
444 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
445 sizeof(void*));
446 debug_scsi("iscsi_tcp_cleanup_task pending r2t dropped\n");
447 }
448
449 r2t = tcp_task->r2t;
450 if (r2t != NULL) {
451 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
452 sizeof(void*));
453 tcp_task->r2t = NULL;
454 }
455}
456EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
457
458/**
459 * iscsi_tcp_data_in - SCSI Data-In Response processing
460 * @conn: iscsi connection
461 * @task: scsi command task
462 */
463static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
464{
465 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
466 struct iscsi_tcp_task *tcp_task = task->dd_data;
467 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
468 int datasn = be32_to_cpu(rhdr->datasn);
469 unsigned total_in_length = scsi_in(task->sc)->length;
470
471 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
472 if (tcp_conn->in.datalen == 0)
473 return 0;
474
475 if (tcp_task->exp_datasn != datasn) {
476 debug_tcp("%s: task->exp_datasn(%d) != rhdr->datasn(%d)\n",
477 __func__, tcp_task->exp_datasn, datasn);
478 return ISCSI_ERR_DATASN;
479 }
480
481 tcp_task->exp_datasn++;
482
483 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
484 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
485 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
486 __func__, tcp_task->data_offset,
487 tcp_conn->in.datalen, total_in_length);
488 return ISCSI_ERR_DATA_OFFSET;
489 }
490
491 conn->datain_pdus_cnt++;
492 return 0;
493}
494
495/**
496 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
497 * @conn: iscsi connection
498 * @task: scsi command task
499 */
500static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
501{
502 struct iscsi_session *session = conn->session;
503 struct iscsi_tcp_task *tcp_task = task->dd_data;
504 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
505 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
506 struct iscsi_r2t_info *r2t;
507 int r2tsn = be32_to_cpu(rhdr->r2tsn);
508 int rc;
509
510 if (tcp_conn->in.datalen) {
511 iscsi_conn_printk(KERN_ERR, conn,
512 "invalid R2t with datalen %d\n",
513 tcp_conn->in.datalen);
514 return ISCSI_ERR_DATALEN;
515 }
516
517 if (tcp_task->exp_datasn != r2tsn){
518 debug_tcp("%s: task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
519 __func__, tcp_task->exp_datasn, r2tsn);
520 return ISCSI_ERR_R2TSN;
521 }
522
523 /* fill-in new R2T associated with the task */
524 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
525
526 if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
527 iscsi_conn_printk(KERN_INFO, conn,
528 "dropping R2T itt %d in recovery.\n",
529 task->itt);
530 return 0;
531 }
532
533 rc = __kfifo_get(tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*));
534 if (!rc) {
535 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
536 "Target has sent more R2Ts than it "
537 "negotiated for or driver has has leaked.\n");
538 return ISCSI_ERR_PROTO;
539 }
540
541 r2t->exp_statsn = rhdr->statsn;
542 r2t->data_length = be32_to_cpu(rhdr->data_length);
543 if (r2t->data_length == 0) {
544 iscsi_conn_printk(KERN_ERR, conn,
545 "invalid R2T with zero data len\n");
546 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
547 sizeof(void*));
548 return ISCSI_ERR_DATALEN;
549 }
550
551 if (r2t->data_length > session->max_burst)
552 debug_scsi("invalid R2T with data len %u and max burst %u."
553 "Attempting to execute request.\n",
554 r2t->data_length, session->max_burst);
555
556 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
557 if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) {
558 iscsi_conn_printk(KERN_ERR, conn,
559 "invalid R2T with data len %u at offset %u "
560 "and total length %d\n", r2t->data_length,
561 r2t->data_offset, scsi_out(task->sc)->length);
562 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
563 sizeof(void*));
564 return ISCSI_ERR_DATALEN;
565 }
566
567 r2t->ttt = rhdr->ttt; /* no flip */
568 r2t->datasn = 0;
569 r2t->sent = 0;
570
571 tcp_task->exp_datasn = r2tsn + 1;
572 __kfifo_put(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
573 conn->r2t_pdus_cnt++;
574
575 iscsi_requeue_task(task);
576 return 0;
577}
578
579/*
580 * Handle incoming reply to DataIn command
581 */
582static int
583iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
584 struct iscsi_segment *segment)
585{
586 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
587 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
588 int rc;
589
590 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
591 return ISCSI_ERR_DATA_DGST;
592
593 /* check for non-exceptional status */
594 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
595 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
596 if (rc)
597 return rc;
598 }
599
600 iscsi_tcp_hdr_recv_prep(tcp_conn);
601 return 0;
602}
603
604/**
605 * iscsi_tcp_hdr_dissect - process PDU header
606 * @conn: iSCSI connection
607 * @hdr: PDU header
608 *
609 * This function analyzes the header of the PDU received,
610 * and performs several sanity checks. If the PDU is accompanied
611 * by data, the receive buffer is set up to copy the incoming data
612 * to the correct location.
613 */
614static int
615iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
616{
617 int rc = 0, opcode, ahslen;
618 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
619 struct iscsi_task *task;
620
621 /* verify PDU length */
622 tcp_conn->in.datalen = ntoh24(hdr->dlength);
623 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
624 iscsi_conn_printk(KERN_ERR, conn,
625 "iscsi_tcp: datalen %d > %d\n",
626 tcp_conn->in.datalen, conn->max_recv_dlength);
627 return ISCSI_ERR_DATALEN;
628 }
629
630 /* Additional header segments. So far, we don't
631 * process additional headers.
632 */
633 ahslen = hdr->hlength << 2;
634
635 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
636 /* verify itt (itt encoding: age+cid+itt) */
637 rc = iscsi_verify_itt(conn, hdr->itt);
638 if (rc)
639 return rc;
640
641 debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
642 opcode, ahslen, tcp_conn->in.datalen);
643
644 switch(opcode) {
645 case ISCSI_OP_SCSI_DATA_IN:
646 spin_lock(&conn->session->lock);
647 task = iscsi_itt_to_ctask(conn, hdr->itt);
648 if (!task)
649 rc = ISCSI_ERR_BAD_ITT;
650 else
651 rc = iscsi_tcp_data_in(conn, task);
652 if (rc) {
653 spin_unlock(&conn->session->lock);
654 break;
655 }
656
657 if (tcp_conn->in.datalen) {
658 struct iscsi_tcp_task *tcp_task = task->dd_data;
659 struct hash_desc *rx_hash = NULL;
660 struct scsi_data_buffer *sdb = scsi_in(task->sc);
661
662 /*
663 * Setup copy of Data-In into the Scsi_Cmnd
664 * Scatterlist case:
665 * We set up the iscsi_segment to point to the next
666 * scatterlist entry to copy to. As we go along,
667 * we move on to the next scatterlist entry and
668 * update the digest per-entry.
669 */
670 if (conn->datadgst_en &&
671 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
672 rx_hash = tcp_conn->rx_hash;
673
674 debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
675 "datalen=%d)\n", tcp_conn,
676 tcp_task->data_offset,
677 tcp_conn->in.datalen);
678 rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
679 sdb->table.sgl,
680 sdb->table.nents,
681 tcp_task->data_offset,
682 tcp_conn->in.datalen,
683 iscsi_tcp_process_data_in,
684 rx_hash);
685 spin_unlock(&conn->session->lock);
686 return rc;
687 }
688 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
689 spin_unlock(&conn->session->lock);
690 break;
691 case ISCSI_OP_SCSI_CMD_RSP:
692 if (tcp_conn->in.datalen) {
693 iscsi_tcp_data_recv_prep(tcp_conn);
694 return 0;
695 }
696 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
697 break;
698 case ISCSI_OP_R2T:
699 spin_lock(&conn->session->lock);
700 task = iscsi_itt_to_ctask(conn, hdr->itt);
701 if (!task)
702 rc = ISCSI_ERR_BAD_ITT;
703 else if (ahslen)
704 rc = ISCSI_ERR_AHSLEN;
705 else if (task->sc->sc_data_direction == DMA_TO_DEVICE)
706 rc = iscsi_tcp_r2t_rsp(conn, task);
707 else
708 rc = ISCSI_ERR_PROTO;
709 spin_unlock(&conn->session->lock);
710 break;
711 case ISCSI_OP_LOGIN_RSP:
712 case ISCSI_OP_TEXT_RSP:
713 case ISCSI_OP_REJECT:
714 case ISCSI_OP_ASYNC_EVENT:
715 /*
716 * It is possible that we could get a PDU with a buffer larger
717 * than 8K, but there are no targets that currently do this.
718 * For now we fail until we find a vendor that needs it
719 */
720 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
721 iscsi_conn_printk(KERN_ERR, conn,
722 "iscsi_tcp: received buffer of "
723 "len %u but conn buffer is only %u "
724 "(opcode %0x)\n",
725 tcp_conn->in.datalen,
726 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
727 rc = ISCSI_ERR_PROTO;
728 break;
729 }
730
731 /* If there's data coming in with the response,
732 * receive it to the connection's buffer.
733 */
734 if (tcp_conn->in.datalen) {
735 iscsi_tcp_data_recv_prep(tcp_conn);
736 return 0;
737 }
738 /* fall through */
739 case ISCSI_OP_LOGOUT_RSP:
740 case ISCSI_OP_NOOP_IN:
741 case ISCSI_OP_SCSI_TMFUNC_RSP:
742 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
743 break;
744 default:
745 rc = ISCSI_ERR_BAD_OPCODE;
746 break;
747 }
748
749 if (rc == 0) {
750 /* Anything that comes with data should have
751 * been handled above. */
752 if (tcp_conn->in.datalen)
753 return ISCSI_ERR_PROTO;
754 iscsi_tcp_hdr_recv_prep(tcp_conn);
755 }
756
757 return rc;
758}
759
760/**
761 * iscsi_tcp_hdr_recv_done - process PDU header
762 *
763 * This is the callback invoked when the PDU header has
764 * been received. If the header is followed by additional
765 * header segments, we go back for more data.
766 */
767static int
768iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
769 struct iscsi_segment *segment)
770{
771 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
772 struct iscsi_hdr *hdr;
773
774 /* Check if there are additional header segments
775 * *prior* to computing the digest, because we
776 * may need to go back to the caller for more.
777 */
778 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
779 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
780 /* Bump the header length - the caller will
781 * just loop around and get the AHS for us, and
782 * call again. */
783 unsigned int ahslen = hdr->hlength << 2;
784
785 /* Make sure we don't overflow */
786 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
787 return ISCSI_ERR_AHSLEN;
788
789 segment->total_size += ahslen;
790 segment->size += ahslen;
791 return 0;
792 }
793
794 /* We're done processing the header. See if we're doing
795 * header digests; if so, set up the recv_digest buffer
796 * and go back for more. */
797 if (conn->hdrdgst_en) {
798 if (segment->digest_len == 0) {
799 /*
800 * Even if we offload the digest processing we
801 * splice it in so we can increment the skb/segment
802 * counters in preparation for the data segment.
803 */
804 iscsi_tcp_segment_splice_digest(segment,
805 segment->recv_digest);
806 return 0;
807 }
808
809 if (!(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
810 iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
811 segment->total_copied - ISCSI_DIGEST_SIZE,
812 segment->digest);
813
814 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
815 return ISCSI_ERR_HDR_DGST;
816 }
817 }
818
819 tcp_conn->in.hdr = hdr;
820 return iscsi_tcp_hdr_dissect(conn, hdr);
821}
822
823/**
824 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
825 * @tcp_conn: iscsi tcp conn
826 *
827 * returns non zero if we are currently processing or setup to process
828 * a header.
829 */
830inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
831{
832 return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
833}
834EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
835
836/**
837 * iscsi_tcp_recv_skb - Process skb
838 * @conn: iscsi connection
839 * @skb: network buffer with header and/or data segment
840 * @offset: offset in skb
841 * @offload: bool indicating if transfer was offloaded
842 *
843 * Will return status of transfer in status. And will return
844 * number of bytes copied.
845 */
846int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
847 unsigned int offset, bool offloaded, int *status)
848{
849 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
850 struct iscsi_segment *segment = &tcp_conn->in.segment;
851 struct skb_seq_state seq;
852 unsigned int consumed = 0;
853 int rc = 0;
854
855 debug_tcp("in %d bytes\n", skb->len - offset);
856
857 if (unlikely(conn->suspend_rx)) {
858 debug_tcp("conn %d Rx suspended!\n", conn->id);
859 *status = ISCSI_TCP_SUSPENDED;
860 return 0;
861 }
862
863 if (offloaded) {
864 segment->total_copied = segment->total_size;
865 goto segment_done;
866 }
867
868 skb_prepare_seq_read(skb, offset, skb->len, &seq);
869 while (1) {
870 unsigned int avail;
871 const u8 *ptr;
872
873 avail = skb_seq_read(consumed, &ptr, &seq);
874 if (avail == 0) {
875 debug_tcp("no more data avail. Consumed %d\n",
876 consumed);
877 *status = ISCSI_TCP_SKB_DONE;
878 skb_abort_seq_read(&seq);
879 goto skb_done;
880 }
881 BUG_ON(segment->copied >= segment->size);
882
883 debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
884 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
885 BUG_ON(rc == 0);
886 consumed += rc;
887
888 if (segment->total_copied >= segment->total_size) {
889 skb_abort_seq_read(&seq);
890 goto segment_done;
891 }
892 }
893
894segment_done:
895 *status = ISCSI_TCP_SEGMENT_DONE;
896 debug_tcp("segment done\n");
897 rc = segment->done(tcp_conn, segment);
898 if (rc != 0) {
899 *status = ISCSI_TCP_CONN_ERR;
900 debug_tcp("Error receiving PDU, errno=%d\n", rc);
901 iscsi_conn_failure(conn, rc);
902 return 0;
903 }
904 /* The done() functions sets up the next segment. */
905
906skb_done:
907 conn->rxdata_octets += consumed;
908 return consumed;
909}
910EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
911
912/**
913 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
914 * @conn: iscsi connection
915 * @task: scsi command task
916 * @sc: scsi command
917 */
918int iscsi_tcp_task_init(struct iscsi_task *task)
919{
920 struct iscsi_tcp_task *tcp_task = task->dd_data;
921 struct iscsi_conn *conn = task->conn;
922 struct scsi_cmnd *sc = task->sc;
923 int err;
924
925 if (!sc) {
926 /*
927 * mgmt tasks do not have a scatterlist since they come
928 * in from the iscsi interface.
929 */
930 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id,
931 task->itt);
932
933 return conn->session->tt->init_pdu(task, 0, task->data_count);
934 }
935
936 BUG_ON(__kfifo_len(tcp_task->r2tqueue));
937 tcp_task->exp_datasn = 0;
938
939 /* Prepare PDU, optionally w/ immediate data */
940 debug_scsi("task deq [cid %d itt 0x%x imm %d unsol %d]\n",
941 conn->id, task->itt, task->imm_count,
942 task->unsol_r2t.data_length);
943
944 err = conn->session->tt->init_pdu(task, 0, task->imm_count);
945 if (err)
946 return err;
947 task->imm_count = 0;
948 return 0;
949}
950EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
951
952static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
953{
954 struct iscsi_session *session = task->conn->session;
955 struct iscsi_tcp_task *tcp_task = task->dd_data;
956 struct iscsi_r2t_info *r2t = NULL;
957
958 if (iscsi_task_has_unsol_data(task))
959 r2t = &task->unsol_r2t;
960 else {
961 spin_lock_bh(&session->lock);
962 if (tcp_task->r2t) {
963 r2t = tcp_task->r2t;
964 /* Continue with this R2T? */
965 if (r2t->data_length <= r2t->sent) {
966 debug_scsi(" done with r2t %p\n", r2t);
967 __kfifo_put(tcp_task->r2tpool.queue,
968 (void *)&tcp_task->r2t,
969 sizeof(void *));
970 tcp_task->r2t = r2t = NULL;
971 }
972 }
973
974 if (r2t == NULL) {
975 __kfifo_get(tcp_task->r2tqueue,
976 (void *)&tcp_task->r2t, sizeof(void *));
977 r2t = tcp_task->r2t;
978 }
979 spin_unlock_bh(&session->lock);
980 }
981
982 return r2t;
983}
984
985/**
986 * iscsi_tcp_task_xmit - xmit normal PDU task
987 * @task: iscsi command task
988 *
989 * We're expected to return 0 when everything was transmitted succesfully,
990 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
991 * of error.
992 */
993int iscsi_tcp_task_xmit(struct iscsi_task *task)
994{
995 struct iscsi_conn *conn = task->conn;
996 struct iscsi_session *session = conn->session;
997 struct iscsi_r2t_info *r2t;
998 int rc = 0;
999
1000flush:
1001 /* Flush any pending data first. */
1002 rc = session->tt->xmit_pdu(task);
1003 if (rc < 0)
1004 return rc;
1005
1006 /* mgmt command */
1007 if (!task->sc) {
1008 if (task->hdr->itt == RESERVED_ITT)
1009 iscsi_put_task(task);
1010 return 0;
1011 }
1012
1013 /* Are we done already? */
1014 if (task->sc->sc_data_direction != DMA_TO_DEVICE)
1015 return 0;
1016
1017 r2t = iscsi_tcp_get_curr_r2t(task);
1018 if (r2t == NULL) {
1019 /* Waiting for more R2Ts to arrive. */
1020 debug_tcp("no R2Ts yet\n");
1021 return 0;
1022 }
1023
1024 rc = conn->session->tt->alloc_pdu(task);
1025 if (rc)
1026 return rc;
1027 iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1028
1029 debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1030 r2t, r2t->datasn - 1, task->hdr->itt,
1031 r2t->data_offset + r2t->sent, r2t->data_count);
1032
1033 rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1034 r2t->data_count);
1035 if (rc)
1036 return rc;
1037 r2t->sent += r2t->data_count;
1038 goto flush;
1039}
1040EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
1041
1042struct iscsi_cls_conn *
1043iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
1044 uint32_t conn_idx)
1045
1046{
1047 struct iscsi_conn *conn;
1048 struct iscsi_cls_conn *cls_conn;
1049 struct iscsi_tcp_conn *tcp_conn;
1050
1051 cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx);
1052 if (!cls_conn)
1053 return NULL;
1054 conn = cls_conn->dd_data;
1055 /*
1056 * due to strange issues with iser these are not set
1057 * in iscsi_conn_setup
1058 */
1059 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1060
1061 tcp_conn = conn->dd_data;
1062 tcp_conn->iscsi_conn = conn;
1063
1064 tcp_conn->dd_data = kzalloc(dd_data_size, GFP_KERNEL);
1065 if (!tcp_conn->dd_data) {
1066 iscsi_conn_teardown(cls_conn);
1067 return NULL;
1068 }
1069 return cls_conn;
1070}
1071EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
1072
1073void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
1074{
1075 struct iscsi_conn *conn = cls_conn->dd_data;
1076 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1077
1078 kfree(tcp_conn->dd_data);
1079 iscsi_conn_teardown(cls_conn);
1080}
1081EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
1082
1083int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
1084{
1085 int i;
1086 int cmd_i;
1087
1088 /*
1089 * initialize per-task: R2T pool and xmit queue
1090 */
1091 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1092 struct iscsi_task *task = session->cmds[cmd_i];
1093 struct iscsi_tcp_task *tcp_task = task->dd_data;
1094
1095 /*
1096 * pre-allocated x2 as much r2ts to handle race when
1097 * target acks DataOut faster than we data_xmit() queues
1098 * could replenish r2tqueue.
1099 */
1100
1101 /* R2T pool */
1102 if (iscsi_pool_init(&tcp_task->r2tpool,
1103 session->max_r2t * 2, NULL,
1104 sizeof(struct iscsi_r2t_info))) {
1105 goto r2t_alloc_fail;
1106 }
1107
1108 /* R2T xmit queue */
1109 tcp_task->r2tqueue = kfifo_alloc(
1110 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
1111 if (tcp_task->r2tqueue == ERR_PTR(-ENOMEM)) {
1112 iscsi_pool_free(&tcp_task->r2tpool);
1113 goto r2t_alloc_fail;
1114 }
1115 }
1116
1117 return 0;
1118
1119r2t_alloc_fail:
1120 for (i = 0; i < cmd_i; i++) {
1121 struct iscsi_task *task = session->cmds[i];
1122 struct iscsi_tcp_task *tcp_task = task->dd_data;
1123
1124 kfifo_free(tcp_task->r2tqueue);
1125 iscsi_pool_free(&tcp_task->r2tpool);
1126 }
1127 return -ENOMEM;
1128}
1129EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
1130
1131void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
1132{
1133 int i;
1134
1135 for (i = 0; i < session->cmds_max; i++) {
1136 struct iscsi_task *task = session->cmds[i];
1137 struct iscsi_tcp_task *tcp_task = task->dd_data;
1138
1139 kfifo_free(tcp_task->r2tqueue);
1140 iscsi_pool_free(&tcp_task->r2tpool);
1141 }
1142}
1143EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
1144
1145void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1146 struct iscsi_stats *stats)
1147{
1148 struct iscsi_conn *conn = cls_conn->dd_data;
1149
1150 stats->txdata_octets = conn->txdata_octets;
1151 stats->rxdata_octets = conn->rxdata_octets;
1152 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1153 stats->dataout_pdus = conn->dataout_pdus_cnt;
1154 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1155 stats->datain_pdus = conn->datain_pdus_cnt;
1156 stats->r2t_pdus = conn->r2t_pdus_cnt;
1157 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1158 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1159}
1160EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);