2 * iSCSI over TCP/IP Data-Path lib
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
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.
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.
20 * See the file COPYING included with this distribution for more details.
29 #include <crypto/hash.h>
30 #include <linux/types.h>
31 #include <linux/list.h>
32 #include <linux/inet.h>
33 #include <linux/slab.h>
34 #include <linux/file.h>
35 #include <linux/blkdev.h>
36 #include <linux/delay.h>
37 #include <linux/kfifo.h>
38 #include <linux/scatterlist.h>
39 #include <linux/module.h>
41 #include <scsi/scsi_cmnd.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_host.h>
44 #include <scsi/scsi.h>
45 #include <scsi/scsi_transport_iscsi.h>
46 #include <trace/events/iscsi.h>
48 #include "iscsi_tcp.h"
50 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
51 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
52 "Alex Aizman <itn780@yahoo.com>");
53 MODULE_DESCRIPTION("iSCSI/TCP data-path");
54 MODULE_LICENSE("GPL");
56 static int iscsi_dbg_libtcp
;
57 module_param_named(debug_libiscsi_tcp
, iscsi_dbg_libtcp
, int,
59 MODULE_PARM_DESC(debug_libiscsi_tcp
, "Turn on debugging for libiscsi_tcp "
60 "module. Set to 1 to turn on, and zero to turn off. Default "
63 #define ISCSI_DBG_TCP(_conn, dbg_fmt, arg...) \
65 if (iscsi_dbg_libtcp) \
66 iscsi_conn_printk(KERN_INFO, _conn, \
69 iscsi_dbg_trace(trace_iscsi_dbg_tcp, \
70 &(_conn)->cls_conn->dev, \
71 "%s " dbg_fmt, __func__, ##arg);\
74 static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn
*tcp_conn
,
75 struct iscsi_segment
*segment
);
78 * Scatterlist handling: inside the iscsi_segment, we
79 * remember an index into the scatterlist, and set data/size
80 * to the current scatterlist entry. For highmem pages, we
83 * Note that the page is unmapped when we return from
84 * TCP's data_ready handler, so we may end up mapping and
85 * unmapping the same page repeatedly. The whole reason
86 * for this is that we shouldn't keep the page mapped
87 * outside the softirq.
91 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
92 * @segment: the buffer object
94 * @offset: byte offset into that sg entry
96 * This function sets up the segment so that subsequent
97 * data is copied to the indicated sg entry, at the given
101 iscsi_tcp_segment_init_sg(struct iscsi_segment
*segment
,
102 struct scatterlist
*sg
, unsigned int offset
)
105 segment
->sg_offset
= offset
;
106 segment
->size
= min(sg
->length
- offset
,
107 segment
->total_size
- segment
->total_copied
);
108 segment
->data
= NULL
;
112 * iscsi_tcp_segment_map - map the current S/G page
113 * @segment: iscsi_segment
114 * @recv: 1 if called from recv path
116 * We only need to possibly kmap data if scatter lists are being used,
117 * because the iscsi passthrough and internal IO paths will never use high
120 static void iscsi_tcp_segment_map(struct iscsi_segment
*segment
, int recv
)
122 struct scatterlist
*sg
;
124 if (segment
->data
!= NULL
|| !segment
->sg
)
128 BUG_ON(segment
->sg_mapped
);
129 BUG_ON(sg
->length
== 0);
132 * If the page count is greater than one it is ok to send
133 * to the network layer's zero copy send path. If not we
134 * have to go the slow sendmsg path. We always map for the
137 if (page_count(sg_page(sg
)) >= 1 && !recv
)
141 segment
->atomic_mapped
= true;
142 segment
->sg_mapped
= kmap_atomic(sg_page(sg
));
144 segment
->atomic_mapped
= false;
145 /* the xmit path can sleep with the page mapped so use kmap */
146 segment
->sg_mapped
= kmap(sg_page(sg
));
149 segment
->data
= segment
->sg_mapped
+ sg
->offset
+ segment
->sg_offset
;
152 void iscsi_tcp_segment_unmap(struct iscsi_segment
*segment
)
154 if (segment
->sg_mapped
) {
155 if (segment
->atomic_mapped
)
156 kunmap_atomic(segment
->sg_mapped
);
158 kunmap(sg_page(segment
->sg
));
159 segment
->sg_mapped
= NULL
;
160 segment
->data
= NULL
;
163 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap
);
166 * Splice the digest buffer into the buffer
169 iscsi_tcp_segment_splice_digest(struct iscsi_segment
*segment
, void *digest
)
171 segment
->data
= digest
;
172 segment
->digest_len
= ISCSI_DIGEST_SIZE
;
173 segment
->total_size
+= ISCSI_DIGEST_SIZE
;
174 segment
->size
= ISCSI_DIGEST_SIZE
;
177 segment
->hash
= NULL
;
181 * iscsi_tcp_segment_done - check whether the segment is complete
182 * @tcp_conn: iscsi tcp connection
183 * @segment: iscsi segment to check
184 * @recv: set to one of this is called from the recv path
185 * @copied: number of bytes copied
187 * Check if we're done receiving this segment. If the receive
188 * buffer is full but we expect more data, move on to the
189 * next entry in the scatterlist.
191 * If the amount of data we received isn't a multiple of 4,
192 * we will transparently receive the pad bytes, too.
194 * This function must be re-entrant.
196 int iscsi_tcp_segment_done(struct iscsi_tcp_conn
*tcp_conn
,
197 struct iscsi_segment
*segment
, int recv
,
200 struct scatterlist sg
;
203 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
, "copied %u %u size %u %s\n",
204 segment
->copied
, copied
, segment
->size
,
205 recv
? "recv" : "xmit");
206 if (segment
->hash
&& copied
) {
208 * If a segment is kmapd we must unmap it before sending
209 * to the crypto layer since that will try to kmap it again.
211 iscsi_tcp_segment_unmap(segment
);
213 if (!segment
->data
) {
214 sg_init_table(&sg
, 1);
215 sg_set_page(&sg
, sg_page(segment
->sg
), copied
,
216 segment
->copied
+ segment
->sg_offset
+
217 segment
->sg
->offset
);
219 sg_init_one(&sg
, segment
->data
+ segment
->copied
,
221 ahash_request_set_crypt(segment
->hash
, &sg
, NULL
, copied
);
222 crypto_ahash_update(segment
->hash
);
225 segment
->copied
+= copied
;
226 if (segment
->copied
< segment
->size
) {
227 iscsi_tcp_segment_map(segment
, recv
);
231 segment
->total_copied
+= segment
->copied
;
235 /* Unmap the current scatterlist page, if there is one. */
236 iscsi_tcp_segment_unmap(segment
);
238 /* Do we have more scatterlist entries? */
239 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
, "total copied %u total size %u\n",
240 segment
->total_copied
, segment
->total_size
);
241 if (segment
->total_copied
< segment
->total_size
) {
242 /* Proceed to the next entry in the scatterlist. */
243 iscsi_tcp_segment_init_sg(segment
, sg_next(segment
->sg
),
245 iscsi_tcp_segment_map(segment
, recv
);
246 BUG_ON(segment
->size
== 0);
250 /* Do we need to handle padding? */
251 if (!(tcp_conn
->iscsi_conn
->session
->tt
->caps
& CAP_PADDING_OFFLOAD
)) {
252 pad
= iscsi_padding(segment
->total_copied
);
254 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
,
255 "consume %d pad bytes\n", pad
);
256 segment
->total_size
+= pad
;
258 segment
->data
= segment
->padbuf
;
264 * Set us up for transferring the data digest. hdr digest
265 * is completely handled in hdr done function.
268 ahash_request_set_crypt(segment
->hash
, NULL
,
270 crypto_ahash_final(segment
->hash
);
271 iscsi_tcp_segment_splice_digest(segment
,
272 recv
? segment
->recv_digest
: segment
->digest
);
278 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done
);
281 * iscsi_tcp_segment_recv - copy data to segment
282 * @tcp_conn: the iSCSI TCP connection
283 * @segment: the buffer to copy to
285 * @len: amount of data available
287 * This function copies up to @len bytes to the
288 * given buffer, and returns the number of bytes
289 * consumed, which can actually be less than @len.
291 * If hash digest is enabled, the function will update the
292 * hash while copying.
293 * Combining these two operations doesn't buy us a lot (yet),
294 * but in the future we could implement combined copy+crc,
295 * just way we do for network layer checksums.
298 iscsi_tcp_segment_recv(struct iscsi_tcp_conn
*tcp_conn
,
299 struct iscsi_segment
*segment
, const void *ptr
,
302 unsigned int copy
= 0, copied
= 0;
304 while (!iscsi_tcp_segment_done(tcp_conn
, segment
, 1, copy
)) {
306 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
,
307 "copied %d bytes\n", len
);
311 copy
= min(len
- copied
, segment
->size
- segment
->copied
);
312 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
, "copying %d\n", copy
);
313 memcpy(segment
->data
+ segment
->copied
, ptr
+ copied
, copy
);
320 iscsi_tcp_dgst_header(struct ahash_request
*hash
, const void *hdr
,
321 size_t hdrlen
, unsigned char digest
[ISCSI_DIGEST_SIZE
])
323 struct scatterlist sg
;
325 sg_init_one(&sg
, hdr
, hdrlen
);
326 ahash_request_set_crypt(hash
, &sg
, digest
, hdrlen
);
327 crypto_ahash_digest(hash
);
329 EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header
);
332 iscsi_tcp_dgst_verify(struct iscsi_tcp_conn
*tcp_conn
,
333 struct iscsi_segment
*segment
)
335 if (!segment
->digest_len
)
338 if (memcmp(segment
->recv_digest
, segment
->digest
,
339 segment
->digest_len
)) {
340 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
, "digest mismatch\n");
348 * Helper function to set up segment buffer
351 __iscsi_segment_init(struct iscsi_segment
*segment
, size_t size
,
352 iscsi_segment_done_fn_t
*done
, struct ahash_request
*hash
)
354 memset(segment
, 0, sizeof(*segment
));
355 segment
->total_size
= size
;
356 segment
->done
= done
;
359 segment
->hash
= hash
;
360 crypto_ahash_init(hash
);
365 iscsi_segment_init_linear(struct iscsi_segment
*segment
, void *data
,
366 size_t size
, iscsi_segment_done_fn_t
*done
,
367 struct ahash_request
*hash
)
369 __iscsi_segment_init(segment
, size
, done
, hash
);
370 segment
->data
= data
;
371 segment
->size
= size
;
373 EXPORT_SYMBOL_GPL(iscsi_segment_init_linear
);
376 iscsi_segment_seek_sg(struct iscsi_segment
*segment
,
377 struct scatterlist
*sg_list
, unsigned int sg_count
,
378 unsigned int offset
, size_t size
,
379 iscsi_segment_done_fn_t
*done
,
380 struct ahash_request
*hash
)
382 struct scatterlist
*sg
;
385 __iscsi_segment_init(segment
, size
, done
, hash
);
386 for_each_sg(sg_list
, sg
, sg_count
, i
) {
387 if (offset
< sg
->length
) {
388 iscsi_tcp_segment_init_sg(segment
, sg
, offset
);
391 offset
-= sg
->length
;
394 return ISCSI_ERR_DATA_OFFSET
;
396 EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg
);
399 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
400 * @tcp_conn: iscsi connection to prep for
402 * This function always passes NULL for the hash argument, because when this
403 * function is called we do not yet know the final size of the header and want
404 * to delay the digest processing until we know that.
406 void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn
*tcp_conn
)
408 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
,
409 "(%s)\n", tcp_conn
->iscsi_conn
->hdrdgst_en
?
410 "digest enabled" : "digest disabled");
411 iscsi_segment_init_linear(&tcp_conn
->in
.segment
,
412 tcp_conn
->in
.hdr_buf
, sizeof(struct iscsi_hdr
),
413 iscsi_tcp_hdr_recv_done
, NULL
);
415 EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep
);
418 * Handle incoming reply to any other type of command
421 iscsi_tcp_data_recv_done(struct iscsi_tcp_conn
*tcp_conn
,
422 struct iscsi_segment
*segment
)
424 struct iscsi_conn
*conn
= tcp_conn
->iscsi_conn
;
427 if (!iscsi_tcp_dgst_verify(tcp_conn
, segment
))
428 return ISCSI_ERR_DATA_DGST
;
430 rc
= iscsi_complete_pdu(conn
, tcp_conn
->in
.hdr
,
431 conn
->data
, tcp_conn
->in
.datalen
);
435 iscsi_tcp_hdr_recv_prep(tcp_conn
);
440 iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn
*tcp_conn
)
442 struct iscsi_conn
*conn
= tcp_conn
->iscsi_conn
;
443 struct ahash_request
*rx_hash
= NULL
;
445 if (conn
->datadgst_en
&&
446 !(conn
->session
->tt
->caps
& CAP_DIGEST_OFFLOAD
))
447 rx_hash
= tcp_conn
->rx_hash
;
449 iscsi_segment_init_linear(&tcp_conn
->in
.segment
,
450 conn
->data
, tcp_conn
->in
.datalen
,
451 iscsi_tcp_data_recv_done
, rx_hash
);
455 * iscsi_tcp_cleanup_task - free tcp_task resources
458 * must be called with session back_lock
460 void iscsi_tcp_cleanup_task(struct iscsi_task
*task
)
462 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
463 struct iscsi_r2t_info
*r2t
;
465 /* nothing to do for mgmt */
469 spin_lock_bh(&tcp_task
->queue2pool
);
470 /* flush task's r2t queues */
471 while (kfifo_out(&tcp_task
->r2tqueue
, (void*)&r2t
, sizeof(void*))) {
472 kfifo_in(&tcp_task
->r2tpool
.queue
, (void*)&r2t
,
474 ISCSI_DBG_TCP(task
->conn
, "pending r2t dropped\n");
479 kfifo_in(&tcp_task
->r2tpool
.queue
, (void*)&r2t
,
481 tcp_task
->r2t
= NULL
;
483 spin_unlock_bh(&tcp_task
->queue2pool
);
485 EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task
);
488 * iscsi_tcp_data_in - SCSI Data-In Response processing
489 * @conn: iscsi connection
490 * @task: scsi command task
492 static int iscsi_tcp_data_in(struct iscsi_conn
*conn
, struct iscsi_task
*task
)
494 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
495 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
496 struct iscsi_data_rsp
*rhdr
= (struct iscsi_data_rsp
*)tcp_conn
->in
.hdr
;
497 int datasn
= be32_to_cpu(rhdr
->datasn
);
498 unsigned total_in_length
= scsi_in(task
->sc
)->length
;
501 * lib iscsi will update this in the completion handling if there
504 if (!(rhdr
->flags
& ISCSI_FLAG_DATA_STATUS
))
505 iscsi_update_cmdsn(conn
->session
, (struct iscsi_nopin
*)rhdr
);
507 if (tcp_conn
->in
.datalen
== 0)
510 if (tcp_task
->exp_datasn
!= datasn
) {
511 ISCSI_DBG_TCP(conn
, "task->exp_datasn(%d) != rhdr->datasn(%d)"
512 "\n", tcp_task
->exp_datasn
, datasn
);
513 return ISCSI_ERR_DATASN
;
516 tcp_task
->exp_datasn
++;
518 tcp_task
->data_offset
= be32_to_cpu(rhdr
->offset
);
519 if (tcp_task
->data_offset
+ tcp_conn
->in
.datalen
> total_in_length
) {
520 ISCSI_DBG_TCP(conn
, "data_offset(%d) + data_len(%d) > "
521 "total_length_in(%d)\n", tcp_task
->data_offset
,
522 tcp_conn
->in
.datalen
, total_in_length
);
523 return ISCSI_ERR_DATA_OFFSET
;
526 conn
->datain_pdus_cnt
++;
531 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
532 * @conn: iscsi connection
533 * @task: scsi command task
535 static int iscsi_tcp_r2t_rsp(struct iscsi_conn
*conn
, struct iscsi_task
*task
)
537 struct iscsi_session
*session
= conn
->session
;
538 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
539 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
540 struct iscsi_r2t_rsp
*rhdr
= (struct iscsi_r2t_rsp
*)tcp_conn
->in
.hdr
;
541 struct iscsi_r2t_info
*r2t
;
542 int r2tsn
= be32_to_cpu(rhdr
->r2tsn
);
547 if (tcp_conn
->in
.datalen
) {
548 iscsi_conn_printk(KERN_ERR
, conn
,
549 "invalid R2t with datalen %d\n",
550 tcp_conn
->in
.datalen
);
551 return ISCSI_ERR_DATALEN
;
554 if (tcp_task
->exp_datasn
!= r2tsn
){
555 ISCSI_DBG_TCP(conn
, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
556 tcp_task
->exp_datasn
, r2tsn
);
557 return ISCSI_ERR_R2TSN
;
560 /* fill-in new R2T associated with the task */
561 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
563 if (!task
->sc
|| session
->state
!= ISCSI_STATE_LOGGED_IN
) {
564 iscsi_conn_printk(KERN_INFO
, conn
,
565 "dropping R2T itt %d in recovery.\n",
570 data_length
= be32_to_cpu(rhdr
->data_length
);
571 if (data_length
== 0) {
572 iscsi_conn_printk(KERN_ERR
, conn
,
573 "invalid R2T with zero data len\n");
574 return ISCSI_ERR_DATALEN
;
577 if (data_length
> session
->max_burst
)
578 ISCSI_DBG_TCP(conn
, "invalid R2T with data len %u and max "
579 "burst %u. Attempting to execute request.\n",
580 data_length
, session
->max_burst
);
582 data_offset
= be32_to_cpu(rhdr
->data_offset
);
583 if (data_offset
+ data_length
> scsi_out(task
->sc
)->length
) {
584 iscsi_conn_printk(KERN_ERR
, conn
,
585 "invalid R2T with data len %u at offset %u "
586 "and total length %d\n", data_length
,
587 data_offset
, scsi_out(task
->sc
)->length
);
588 return ISCSI_ERR_DATALEN
;
591 spin_lock(&tcp_task
->pool2queue
);
592 rc
= kfifo_out(&tcp_task
->r2tpool
.queue
, (void *)&r2t
, sizeof(void *));
594 iscsi_conn_printk(KERN_ERR
, conn
, "Could not allocate R2T. "
595 "Target has sent more R2Ts than it "
596 "negotiated for or driver has leaked.\n");
597 spin_unlock(&tcp_task
->pool2queue
);
598 return ISCSI_ERR_PROTO
;
601 r2t
->exp_statsn
= rhdr
->statsn
;
602 r2t
->data_length
= data_length
;
603 r2t
->data_offset
= data_offset
;
605 r2t
->ttt
= rhdr
->ttt
; /* no flip */
609 tcp_task
->exp_datasn
= r2tsn
+ 1;
610 kfifo_in(&tcp_task
->r2tqueue
, (void*)&r2t
, sizeof(void*));
611 conn
->r2t_pdus_cnt
++;
612 spin_unlock(&tcp_task
->pool2queue
);
614 iscsi_requeue_task(task
);
619 * Handle incoming reply to DataIn command
622 iscsi_tcp_process_data_in(struct iscsi_tcp_conn
*tcp_conn
,
623 struct iscsi_segment
*segment
)
625 struct iscsi_conn
*conn
= tcp_conn
->iscsi_conn
;
626 struct iscsi_hdr
*hdr
= tcp_conn
->in
.hdr
;
629 if (!iscsi_tcp_dgst_verify(tcp_conn
, segment
))
630 return ISCSI_ERR_DATA_DGST
;
632 /* check for non-exceptional status */
633 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
634 rc
= iscsi_complete_pdu(conn
, tcp_conn
->in
.hdr
, NULL
, 0);
639 iscsi_tcp_hdr_recv_prep(tcp_conn
);
644 * iscsi_tcp_hdr_dissect - process PDU header
645 * @conn: iSCSI connection
648 * This function analyzes the header of the PDU received,
649 * and performs several sanity checks. If the PDU is accompanied
650 * by data, the receive buffer is set up to copy the incoming data
651 * to the correct location.
654 iscsi_tcp_hdr_dissect(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
)
656 int rc
= 0, opcode
, ahslen
;
657 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
658 struct iscsi_task
*task
;
660 /* verify PDU length */
661 tcp_conn
->in
.datalen
= ntoh24(hdr
->dlength
);
662 if (tcp_conn
->in
.datalen
> conn
->max_recv_dlength
) {
663 iscsi_conn_printk(KERN_ERR
, conn
,
664 "iscsi_tcp: datalen %d > %d\n",
665 tcp_conn
->in
.datalen
, conn
->max_recv_dlength
);
666 return ISCSI_ERR_DATALEN
;
669 /* Additional header segments. So far, we don't
670 * process additional headers.
672 ahslen
= hdr
->hlength
<< 2;
674 opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
;
675 /* verify itt (itt encoding: age+cid+itt) */
676 rc
= iscsi_verify_itt(conn
, hdr
->itt
);
680 ISCSI_DBG_TCP(conn
, "opcode 0x%x ahslen %d datalen %d\n",
681 opcode
, ahslen
, tcp_conn
->in
.datalen
);
684 case ISCSI_OP_SCSI_DATA_IN
:
685 spin_lock(&conn
->session
->back_lock
);
686 task
= iscsi_itt_to_ctask(conn
, hdr
->itt
);
688 rc
= ISCSI_ERR_BAD_ITT
;
690 rc
= iscsi_tcp_data_in(conn
, task
);
692 spin_unlock(&conn
->session
->back_lock
);
696 if (tcp_conn
->in
.datalen
) {
697 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
698 struct ahash_request
*rx_hash
= NULL
;
699 struct scsi_data_buffer
*sdb
= scsi_in(task
->sc
);
702 * Setup copy of Data-In into the struct scsi_cmnd
704 * We set up the iscsi_segment to point to the next
705 * scatterlist entry to copy to. As we go along,
706 * we move on to the next scatterlist entry and
707 * update the digest per-entry.
709 if (conn
->datadgst_en
&&
710 !(conn
->session
->tt
->caps
& CAP_DIGEST_OFFLOAD
))
711 rx_hash
= tcp_conn
->rx_hash
;
713 ISCSI_DBG_TCP(conn
, "iscsi_tcp_begin_data_in( "
714 "offset=%d, datalen=%d)\n",
715 tcp_task
->data_offset
,
716 tcp_conn
->in
.datalen
);
717 task
->last_xfer
= jiffies
;
718 rc
= iscsi_segment_seek_sg(&tcp_conn
->in
.segment
,
721 tcp_task
->data_offset
,
722 tcp_conn
->in
.datalen
,
723 iscsi_tcp_process_data_in
,
725 spin_unlock(&conn
->session
->back_lock
);
728 rc
= __iscsi_complete_pdu(conn
, hdr
, NULL
, 0);
729 spin_unlock(&conn
->session
->back_lock
);
731 case ISCSI_OP_SCSI_CMD_RSP
:
732 if (tcp_conn
->in
.datalen
) {
733 iscsi_tcp_data_recv_prep(tcp_conn
);
736 rc
= iscsi_complete_pdu(conn
, hdr
, NULL
, 0);
739 spin_lock(&conn
->session
->back_lock
);
740 task
= iscsi_itt_to_ctask(conn
, hdr
->itt
);
741 spin_unlock(&conn
->session
->back_lock
);
743 rc
= ISCSI_ERR_BAD_ITT
;
745 rc
= ISCSI_ERR_AHSLEN
;
746 else if (task
->sc
->sc_data_direction
== DMA_TO_DEVICE
) {
747 task
->last_xfer
= jiffies
;
748 spin_lock(&conn
->session
->frwd_lock
);
749 rc
= iscsi_tcp_r2t_rsp(conn
, task
);
750 spin_unlock(&conn
->session
->frwd_lock
);
752 rc
= ISCSI_ERR_PROTO
;
754 case ISCSI_OP_LOGIN_RSP
:
755 case ISCSI_OP_TEXT_RSP
:
756 case ISCSI_OP_REJECT
:
757 case ISCSI_OP_ASYNC_EVENT
:
759 * It is possible that we could get a PDU with a buffer larger
760 * than 8K, but there are no targets that currently do this.
761 * For now we fail until we find a vendor that needs it
763 if (ISCSI_DEF_MAX_RECV_SEG_LEN
< tcp_conn
->in
.datalen
) {
764 iscsi_conn_printk(KERN_ERR
, conn
,
765 "iscsi_tcp: received buffer of "
766 "len %u but conn buffer is only %u "
768 tcp_conn
->in
.datalen
,
769 ISCSI_DEF_MAX_RECV_SEG_LEN
, opcode
);
770 rc
= ISCSI_ERR_PROTO
;
774 /* If there's data coming in with the response,
775 * receive it to the connection's buffer.
777 if (tcp_conn
->in
.datalen
) {
778 iscsi_tcp_data_recv_prep(tcp_conn
);
782 case ISCSI_OP_LOGOUT_RSP
:
783 case ISCSI_OP_NOOP_IN
:
784 case ISCSI_OP_SCSI_TMFUNC_RSP
:
785 rc
= iscsi_complete_pdu(conn
, hdr
, NULL
, 0);
788 rc
= ISCSI_ERR_BAD_OPCODE
;
793 /* Anything that comes with data should have
794 * been handled above. */
795 if (tcp_conn
->in
.datalen
)
796 return ISCSI_ERR_PROTO
;
797 iscsi_tcp_hdr_recv_prep(tcp_conn
);
804 * iscsi_tcp_hdr_recv_done - process PDU header
805 * @tcp_conn: iSCSI TCP connection
806 * @segment: the buffer segment being processed
808 * This is the callback invoked when the PDU header has
809 * been received. If the header is followed by additional
810 * header segments, we go back for more data.
813 iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn
*tcp_conn
,
814 struct iscsi_segment
*segment
)
816 struct iscsi_conn
*conn
= tcp_conn
->iscsi_conn
;
817 struct iscsi_hdr
*hdr
;
819 /* Check if there are additional header segments
820 * *prior* to computing the digest, because we
821 * may need to go back to the caller for more.
823 hdr
= (struct iscsi_hdr
*) tcp_conn
->in
.hdr_buf
;
824 if (segment
->copied
== sizeof(struct iscsi_hdr
) && hdr
->hlength
) {
825 /* Bump the header length - the caller will
826 * just loop around and get the AHS for us, and
828 unsigned int ahslen
= hdr
->hlength
<< 2;
830 /* Make sure we don't overflow */
831 if (sizeof(*hdr
) + ahslen
> sizeof(tcp_conn
->in
.hdr_buf
))
832 return ISCSI_ERR_AHSLEN
;
834 segment
->total_size
+= ahslen
;
835 segment
->size
+= ahslen
;
839 /* We're done processing the header. See if we're doing
840 * header digests; if so, set up the recv_digest buffer
841 * and go back for more. */
842 if (conn
->hdrdgst_en
&&
843 !(conn
->session
->tt
->caps
& CAP_DIGEST_OFFLOAD
)) {
844 if (segment
->digest_len
== 0) {
846 * Even if we offload the digest processing we
847 * splice it in so we can increment the skb/segment
848 * counters in preparation for the data segment.
850 iscsi_tcp_segment_splice_digest(segment
,
851 segment
->recv_digest
);
855 iscsi_tcp_dgst_header(tcp_conn
->rx_hash
, hdr
,
856 segment
->total_copied
- ISCSI_DIGEST_SIZE
,
859 if (!iscsi_tcp_dgst_verify(tcp_conn
, segment
))
860 return ISCSI_ERR_HDR_DGST
;
863 tcp_conn
->in
.hdr
= hdr
;
864 return iscsi_tcp_hdr_dissect(conn
, hdr
);
868 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
869 * @tcp_conn: iscsi tcp conn
871 * returns non zero if we are currently processing or setup to process
874 inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn
*tcp_conn
)
876 return tcp_conn
->in
.segment
.done
== iscsi_tcp_hdr_recv_done
;
878 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr
);
881 * iscsi_tcp_recv_skb - Process skb
882 * @conn: iscsi connection
883 * @skb: network buffer with header and/or data segment
884 * @offset: offset in skb
885 * @offloaded: bool indicating if transfer was offloaded
886 * @status: iscsi TCP status result
888 * Will return status of transfer in @status. And will return
889 * number of bytes copied.
891 int iscsi_tcp_recv_skb(struct iscsi_conn
*conn
, struct sk_buff
*skb
,
892 unsigned int offset
, bool offloaded
, int *status
)
894 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
895 struct iscsi_segment
*segment
= &tcp_conn
->in
.segment
;
896 struct skb_seq_state seq
;
897 unsigned int consumed
= 0;
900 ISCSI_DBG_TCP(conn
, "in %d bytes\n", skb
->len
- offset
);
902 * Update for each skb instead of pdu, because over slow networks a
903 * data_in's data could take a while to read in. We also want to
906 conn
->last_recv
= jiffies
;
908 if (unlikely(conn
->suspend_rx
)) {
909 ISCSI_DBG_TCP(conn
, "Rx suspended!\n");
910 *status
= ISCSI_TCP_SUSPENDED
;
915 segment
->total_copied
= segment
->total_size
;
919 skb_prepare_seq_read(skb
, offset
, skb
->len
, &seq
);
924 avail
= skb_seq_read(consumed
, &ptr
, &seq
);
926 ISCSI_DBG_TCP(conn
, "no more data avail. Consumed %d\n",
928 *status
= ISCSI_TCP_SKB_DONE
;
931 BUG_ON(segment
->copied
>= segment
->size
);
933 ISCSI_DBG_TCP(conn
, "skb %p ptr=%p avail=%u\n", skb
, ptr
,
935 rc
= iscsi_tcp_segment_recv(tcp_conn
, segment
, ptr
, avail
);
939 if (segment
->total_copied
>= segment
->total_size
) {
940 skb_abort_seq_read(&seq
);
946 *status
= ISCSI_TCP_SEGMENT_DONE
;
947 ISCSI_DBG_TCP(conn
, "segment done\n");
948 rc
= segment
->done(tcp_conn
, segment
);
950 *status
= ISCSI_TCP_CONN_ERR
;
951 ISCSI_DBG_TCP(conn
, "Error receiving PDU, errno=%d\n", rc
);
952 iscsi_conn_failure(conn
, rc
);
955 /* The done() functions sets up the next segment. */
958 conn
->rxdata_octets
+= consumed
;
961 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb
);
964 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
965 * @task: scsi command task
967 int iscsi_tcp_task_init(struct iscsi_task
*task
)
969 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
970 struct iscsi_conn
*conn
= task
->conn
;
971 struct scsi_cmnd
*sc
= task
->sc
;
976 * mgmt tasks do not have a scatterlist since they come
977 * in from the iscsi interface.
979 ISCSI_DBG_TCP(conn
, "mtask deq [itt 0x%x]\n", task
->itt
);
981 return conn
->session
->tt
->init_pdu(task
, 0, task
->data_count
);
984 BUG_ON(kfifo_len(&tcp_task
->r2tqueue
));
985 tcp_task
->exp_datasn
= 0;
987 /* Prepare PDU, optionally w/ immediate data */
988 ISCSI_DBG_TCP(conn
, "task deq [itt 0x%x imm %d unsol %d]\n",
989 task
->itt
, task
->imm_count
, task
->unsol_r2t
.data_length
);
991 err
= conn
->session
->tt
->init_pdu(task
, 0, task
->imm_count
);
997 EXPORT_SYMBOL_GPL(iscsi_tcp_task_init
);
999 static struct iscsi_r2t_info
*iscsi_tcp_get_curr_r2t(struct iscsi_task
*task
)
1001 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
1002 struct iscsi_r2t_info
*r2t
= NULL
;
1004 if (iscsi_task_has_unsol_data(task
))
1005 r2t
= &task
->unsol_r2t
;
1007 spin_lock_bh(&tcp_task
->queue2pool
);
1008 if (tcp_task
->r2t
) {
1009 r2t
= tcp_task
->r2t
;
1010 /* Continue with this R2T? */
1011 if (r2t
->data_length
<= r2t
->sent
) {
1012 ISCSI_DBG_TCP(task
->conn
,
1013 " done with r2t %p\n", r2t
);
1014 kfifo_in(&tcp_task
->r2tpool
.queue
,
1015 (void *)&tcp_task
->r2t
,
1017 tcp_task
->r2t
= r2t
= NULL
;
1022 if (kfifo_out(&tcp_task
->r2tqueue
,
1023 (void *)&tcp_task
->r2t
, sizeof(void *)) !=
1027 r2t
= tcp_task
->r2t
;
1029 spin_unlock_bh(&tcp_task
->queue2pool
);
1036 * iscsi_tcp_task_xmit - xmit normal PDU task
1037 * @task: iscsi command task
1039 * We're expected to return 0 when everything was transmitted successfully,
1040 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1043 int iscsi_tcp_task_xmit(struct iscsi_task
*task
)
1045 struct iscsi_conn
*conn
= task
->conn
;
1046 struct iscsi_session
*session
= conn
->session
;
1047 struct iscsi_r2t_info
*r2t
;
1051 /* Flush any pending data first. */
1052 rc
= session
->tt
->xmit_pdu(task
);
1058 if (task
->hdr
->itt
== RESERVED_ITT
)
1059 iscsi_put_task(task
);
1063 /* Are we done already? */
1064 if (task
->sc
->sc_data_direction
!= DMA_TO_DEVICE
)
1067 r2t
= iscsi_tcp_get_curr_r2t(task
);
1069 /* Waiting for more R2Ts to arrive. */
1070 ISCSI_DBG_TCP(conn
, "no R2Ts yet\n");
1074 rc
= conn
->session
->tt
->alloc_pdu(task
, ISCSI_OP_SCSI_DATA_OUT
);
1077 iscsi_prep_data_out_pdu(task
, r2t
, (struct iscsi_data
*) task
->hdr
);
1079 ISCSI_DBG_TCP(conn
, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1080 r2t
, r2t
->datasn
- 1, task
->hdr
->itt
,
1081 r2t
->data_offset
+ r2t
->sent
, r2t
->data_count
);
1083 rc
= conn
->session
->tt
->init_pdu(task
, r2t
->data_offset
+ r2t
->sent
,
1086 iscsi_conn_failure(conn
, ISCSI_ERR_XMIT_FAILED
);
1090 r2t
->sent
+= r2t
->data_count
;
1093 EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit
);
1095 struct iscsi_cls_conn
*
1096 iscsi_tcp_conn_setup(struct iscsi_cls_session
*cls_session
, int dd_data_size
,
1100 struct iscsi_conn
*conn
;
1101 struct iscsi_cls_conn
*cls_conn
;
1102 struct iscsi_tcp_conn
*tcp_conn
;
1104 cls_conn
= iscsi_conn_setup(cls_session
,
1105 sizeof(*tcp_conn
) + dd_data_size
, conn_idx
);
1108 conn
= cls_conn
->dd_data
;
1110 * due to strange issues with iser these are not set
1111 * in iscsi_conn_setup
1113 conn
->max_recv_dlength
= ISCSI_DEF_MAX_RECV_SEG_LEN
;
1115 tcp_conn
= conn
->dd_data
;
1116 tcp_conn
->iscsi_conn
= conn
;
1117 tcp_conn
->dd_data
= conn
->dd_data
+ sizeof(*tcp_conn
);
1120 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup
);
1122 void iscsi_tcp_conn_teardown(struct iscsi_cls_conn
*cls_conn
)
1124 iscsi_conn_teardown(cls_conn
);
1126 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown
);
1128 int iscsi_tcp_r2tpool_alloc(struct iscsi_session
*session
)
1134 * initialize per-task: R2T pool and xmit queue
1136 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
1137 struct iscsi_task
*task
= session
->cmds
[cmd_i
];
1138 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
1141 * pre-allocated x2 as much r2ts to handle race when
1142 * target acks DataOut faster than we data_xmit() queues
1143 * could replenish r2tqueue.
1147 if (iscsi_pool_init(&tcp_task
->r2tpool
,
1148 session
->max_r2t
* 2, NULL
,
1149 sizeof(struct iscsi_r2t_info
))) {
1150 goto r2t_alloc_fail
;
1153 /* R2T xmit queue */
1154 if (kfifo_alloc(&tcp_task
->r2tqueue
,
1155 session
->max_r2t
* 4 * sizeof(void*), GFP_KERNEL
)) {
1156 iscsi_pool_free(&tcp_task
->r2tpool
);
1157 goto r2t_alloc_fail
;
1159 spin_lock_init(&tcp_task
->pool2queue
);
1160 spin_lock_init(&tcp_task
->queue2pool
);
1166 for (i
= 0; i
< cmd_i
; i
++) {
1167 struct iscsi_task
*task
= session
->cmds
[i
];
1168 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
1170 kfifo_free(&tcp_task
->r2tqueue
);
1171 iscsi_pool_free(&tcp_task
->r2tpool
);
1175 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc
);
1177 void iscsi_tcp_r2tpool_free(struct iscsi_session
*session
)
1181 for (i
= 0; i
< session
->cmds_max
; i
++) {
1182 struct iscsi_task
*task
= session
->cmds
[i
];
1183 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
1185 kfifo_free(&tcp_task
->r2tqueue
);
1186 iscsi_pool_free(&tcp_task
->r2tpool
);
1189 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free
);
1191 int iscsi_tcp_set_max_r2t(struct iscsi_conn
*conn
, char *buf
)
1193 struct iscsi_session
*session
= conn
->session
;
1194 unsigned short r2ts
= 0;
1196 sscanf(buf
, "%hu", &r2ts
);
1197 if (session
->max_r2t
== r2ts
)
1200 if (!r2ts
|| !is_power_of_2(r2ts
))
1203 session
->max_r2t
= r2ts
;
1204 iscsi_tcp_r2tpool_free(session
);
1205 return iscsi_tcp_r2tpool_alloc(session
);
1207 EXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t
);
1209 void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn
*cls_conn
,
1210 struct iscsi_stats
*stats
)
1212 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1214 stats
->txdata_octets
= conn
->txdata_octets
;
1215 stats
->rxdata_octets
= conn
->rxdata_octets
;
1216 stats
->scsicmd_pdus
= conn
->scsicmd_pdus_cnt
;
1217 stats
->dataout_pdus
= conn
->dataout_pdus_cnt
;
1218 stats
->scsirsp_pdus
= conn
->scsirsp_pdus_cnt
;
1219 stats
->datain_pdus
= conn
->datain_pdus_cnt
;
1220 stats
->r2t_pdus
= conn
->r2t_pdus_cnt
;
1221 stats
->tmfcmd_pdus
= conn
->tmfcmd_pdus_cnt
;
1222 stats
->tmfrsp_pdus
= conn
->tmfrsp_pdus_cnt
;
1224 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats
);