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 <linux/types.h>
30 #include <linux/list.h>
31 #include <linux/inet.h>
32 #include <linux/slab.h>
33 #include <linux/file.h>
34 #include <linux/blkdev.h>
35 #include <linux/crypto.h>
36 #include <linux/delay.h>
37 #include <linux/kfifo.h>
38 #include <linux/scatterlist.h>
40 #include <scsi/scsi_cmnd.h>
41 #include <scsi/scsi_device.h>
42 #include <scsi/scsi_host.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_transport_iscsi.h>
46 #include "iscsi_tcp.h"
48 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
49 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
50 "Alex Aizman <itn780@yahoo.com>");
51 MODULE_DESCRIPTION("iSCSI/TCP data-path");
52 MODULE_LICENSE("GPL");
54 static int iscsi_dbg_libtcp
;
55 module_param_named(debug_libiscsi_tcp
, iscsi_dbg_libtcp
, int,
57 MODULE_PARM_DESC(debug_libiscsi_tcp
, "Turn on debugging for libiscsi_tcp "
58 "module. Set to 1 to turn on, and zero to turn off. Default "
61 #define ISCSI_DBG_TCP(_conn, dbg_fmt, arg...) \
63 if (iscsi_dbg_libtcp) \
64 iscsi_conn_printk(KERN_INFO, _conn, \
69 static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn
*tcp_conn
,
70 struct iscsi_segment
*segment
);
73 * Scatterlist handling: inside the iscsi_segment, we
74 * remember an index into the scatterlist, and set data/size
75 * to the current scatterlist entry. For highmem pages, we
78 * Note that the page is unmapped when we return from
79 * TCP's data_ready handler, so we may end up mapping and
80 * unmapping the same page repeatedly. The whole reason
81 * for this is that we shouldn't keep the page mapped
82 * outside the softirq.
86 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
87 * @segment: the buffer object
89 * @offset: byte offset into that sg entry
91 * This function sets up the segment so that subsequent
92 * data is copied to the indicated sg entry, at the given
96 iscsi_tcp_segment_init_sg(struct iscsi_segment
*segment
,
97 struct scatterlist
*sg
, unsigned int offset
)
100 segment
->sg_offset
= offset
;
101 segment
->size
= min(sg
->length
- offset
,
102 segment
->total_size
- segment
->total_copied
);
103 segment
->data
= NULL
;
107 * iscsi_tcp_segment_map - map the current S/G page
108 * @segment: iscsi_segment
109 * @recv: 1 if called from recv path
111 * We only need to possibly kmap data if scatter lists are being used,
112 * because the iscsi passthrough and internal IO paths will never use high
115 static void iscsi_tcp_segment_map(struct iscsi_segment
*segment
, int recv
)
117 struct scatterlist
*sg
;
119 if (segment
->data
!= NULL
|| !segment
->sg
)
123 BUG_ON(segment
->sg_mapped
);
124 BUG_ON(sg
->length
== 0);
127 * If the page count is greater than one it is ok to send
128 * to the network layer's zero copy send path. If not we
129 * have to go the slow sendmsg path. We always map for the
132 if (page_count(sg_page(sg
)) >= 1 && !recv
)
135 segment
->sg_mapped
= kmap_atomic(sg_page(sg
), KM_SOFTIRQ0
);
136 segment
->data
= segment
->sg_mapped
+ sg
->offset
+ segment
->sg_offset
;
139 void iscsi_tcp_segment_unmap(struct iscsi_segment
*segment
)
141 if (segment
->sg_mapped
) {
142 kunmap_atomic(segment
->sg_mapped
, KM_SOFTIRQ0
);
143 segment
->sg_mapped
= NULL
;
144 segment
->data
= NULL
;
147 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap
);
150 * Splice the digest buffer into the buffer
153 iscsi_tcp_segment_splice_digest(struct iscsi_segment
*segment
, void *digest
)
155 segment
->data
= digest
;
156 segment
->digest_len
= ISCSI_DIGEST_SIZE
;
157 segment
->total_size
+= ISCSI_DIGEST_SIZE
;
158 segment
->size
= ISCSI_DIGEST_SIZE
;
161 segment
->hash
= NULL
;
165 * iscsi_tcp_segment_done - check whether the segment is complete
166 * @tcp_conn: iscsi tcp connection
167 * @segment: iscsi segment to check
168 * @recv: set to one of this is called from the recv path
169 * @copied: number of bytes copied
171 * Check if we're done receiving this segment. If the receive
172 * buffer is full but we expect more data, move on to the
173 * next entry in the scatterlist.
175 * If the amount of data we received isn't a multiple of 4,
176 * we will transparently receive the pad bytes, too.
178 * This function must be re-entrant.
180 int iscsi_tcp_segment_done(struct iscsi_tcp_conn
*tcp_conn
,
181 struct iscsi_segment
*segment
, int recv
,
184 struct scatterlist sg
;
187 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
, "copied %u %u size %u %s\n",
188 segment
->copied
, copied
, segment
->size
,
189 recv
? "recv" : "xmit");
190 if (segment
->hash
&& copied
) {
192 * If a segment is kmapd we must unmap it before sending
193 * to the crypto layer since that will try to kmap it again.
195 iscsi_tcp_segment_unmap(segment
);
197 if (!segment
->data
) {
198 sg_init_table(&sg
, 1);
199 sg_set_page(&sg
, sg_page(segment
->sg
), copied
,
200 segment
->copied
+ segment
->sg_offset
+
201 segment
->sg
->offset
);
203 sg_init_one(&sg
, segment
->data
+ segment
->copied
,
205 crypto_hash_update(segment
->hash
, &sg
, copied
);
208 segment
->copied
+= copied
;
209 if (segment
->copied
< segment
->size
) {
210 iscsi_tcp_segment_map(segment
, recv
);
214 segment
->total_copied
+= segment
->copied
;
218 /* Unmap the current scatterlist page, if there is one. */
219 iscsi_tcp_segment_unmap(segment
);
221 /* Do we have more scatterlist entries? */
222 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
, "total copied %u total size %u\n",
223 segment
->total_copied
, segment
->total_size
);
224 if (segment
->total_copied
< segment
->total_size
) {
225 /* Proceed to the next entry in the scatterlist. */
226 iscsi_tcp_segment_init_sg(segment
, sg_next(segment
->sg
),
228 iscsi_tcp_segment_map(segment
, recv
);
229 BUG_ON(segment
->size
== 0);
233 /* Do we need to handle padding? */
234 if (!(tcp_conn
->iscsi_conn
->session
->tt
->caps
& CAP_PADDING_OFFLOAD
)) {
235 pad
= iscsi_padding(segment
->total_copied
);
237 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
,
238 "consume %d pad bytes\n", pad
);
239 segment
->total_size
+= pad
;
241 segment
->data
= segment
->padbuf
;
247 * Set us up for transferring the data digest. hdr digest
248 * is completely handled in hdr done function.
251 crypto_hash_final(segment
->hash
, segment
->digest
);
252 iscsi_tcp_segment_splice_digest(segment
,
253 recv
? segment
->recv_digest
: segment
->digest
);
259 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done
);
262 * iscsi_tcp_segment_recv - copy data to segment
263 * @tcp_conn: the iSCSI TCP connection
264 * @segment: the buffer to copy to
266 * @len: amount of data available
268 * This function copies up to @len bytes to the
269 * given buffer, and returns the number of bytes
270 * consumed, which can actually be less than @len.
272 * If hash digest is enabled, the function will update the
273 * hash while copying.
274 * Combining these two operations doesn't buy us a lot (yet),
275 * but in the future we could implement combined copy+crc,
276 * just way we do for network layer checksums.
279 iscsi_tcp_segment_recv(struct iscsi_tcp_conn
*tcp_conn
,
280 struct iscsi_segment
*segment
, const void *ptr
,
283 unsigned int copy
= 0, copied
= 0;
285 while (!iscsi_tcp_segment_done(tcp_conn
, segment
, 1, copy
)) {
287 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
,
288 "copied %d bytes\n", len
);
292 copy
= min(len
- copied
, segment
->size
- segment
->copied
);
293 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
, "copying %d\n", copy
);
294 memcpy(segment
->data
+ segment
->copied
, ptr
+ copied
, copy
);
301 iscsi_tcp_dgst_header(struct hash_desc
*hash
, const void *hdr
, size_t hdrlen
,
302 unsigned char digest
[ISCSI_DIGEST_SIZE
])
304 struct scatterlist sg
;
306 sg_init_one(&sg
, hdr
, hdrlen
);
307 crypto_hash_digest(hash
, &sg
, hdrlen
, digest
);
309 EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header
);
312 iscsi_tcp_dgst_verify(struct iscsi_tcp_conn
*tcp_conn
,
313 struct iscsi_segment
*segment
)
315 if (!segment
->digest_len
)
318 if (memcmp(segment
->recv_digest
, segment
->digest
,
319 segment
->digest_len
)) {
320 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
, "digest mismatch\n");
328 * Helper function to set up segment buffer
331 __iscsi_segment_init(struct iscsi_segment
*segment
, size_t size
,
332 iscsi_segment_done_fn_t
*done
, struct hash_desc
*hash
)
334 memset(segment
, 0, sizeof(*segment
));
335 segment
->total_size
= size
;
336 segment
->done
= done
;
339 segment
->hash
= hash
;
340 crypto_hash_init(hash
);
345 iscsi_segment_init_linear(struct iscsi_segment
*segment
, void *data
,
346 size_t size
, iscsi_segment_done_fn_t
*done
,
347 struct hash_desc
*hash
)
349 __iscsi_segment_init(segment
, size
, done
, hash
);
350 segment
->data
= data
;
351 segment
->size
= size
;
353 EXPORT_SYMBOL_GPL(iscsi_segment_init_linear
);
356 iscsi_segment_seek_sg(struct iscsi_segment
*segment
,
357 struct scatterlist
*sg_list
, unsigned int sg_count
,
358 unsigned int offset
, size_t size
,
359 iscsi_segment_done_fn_t
*done
, struct hash_desc
*hash
)
361 struct scatterlist
*sg
;
364 __iscsi_segment_init(segment
, size
, done
, hash
);
365 for_each_sg(sg_list
, sg
, sg_count
, i
) {
366 if (offset
< sg
->length
) {
367 iscsi_tcp_segment_init_sg(segment
, sg
, offset
);
370 offset
-= sg
->length
;
373 return ISCSI_ERR_DATA_OFFSET
;
375 EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg
);
378 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
379 * @tcp_conn: iscsi connection to prep for
381 * This function always passes NULL for the hash argument, because when this
382 * function is called we do not yet know the final size of the header and want
383 * to delay the digest processing until we know that.
385 void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn
*tcp_conn
)
387 ISCSI_DBG_TCP(tcp_conn
->iscsi_conn
,
388 "(%s)\n", tcp_conn
->iscsi_conn
->hdrdgst_en
?
389 "digest enabled" : "digest disabled");
390 iscsi_segment_init_linear(&tcp_conn
->in
.segment
,
391 tcp_conn
->in
.hdr_buf
, sizeof(struct iscsi_hdr
),
392 iscsi_tcp_hdr_recv_done
, NULL
);
394 EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep
);
397 * Handle incoming reply to any other type of command
400 iscsi_tcp_data_recv_done(struct iscsi_tcp_conn
*tcp_conn
,
401 struct iscsi_segment
*segment
)
403 struct iscsi_conn
*conn
= tcp_conn
->iscsi_conn
;
406 if (!iscsi_tcp_dgst_verify(tcp_conn
, segment
))
407 return ISCSI_ERR_DATA_DGST
;
409 rc
= iscsi_complete_pdu(conn
, tcp_conn
->in
.hdr
,
410 conn
->data
, tcp_conn
->in
.datalen
);
414 iscsi_tcp_hdr_recv_prep(tcp_conn
);
419 iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn
*tcp_conn
)
421 struct iscsi_conn
*conn
= tcp_conn
->iscsi_conn
;
422 struct hash_desc
*rx_hash
= NULL
;
424 if (conn
->datadgst_en
&
425 !(conn
->session
->tt
->caps
& CAP_DIGEST_OFFLOAD
))
426 rx_hash
= tcp_conn
->rx_hash
;
428 iscsi_segment_init_linear(&tcp_conn
->in
.segment
,
429 conn
->data
, tcp_conn
->in
.datalen
,
430 iscsi_tcp_data_recv_done
, rx_hash
);
434 * iscsi_tcp_cleanup_task - free tcp_task resources
437 * must be called with session lock
439 void iscsi_tcp_cleanup_task(struct iscsi_task
*task
)
441 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
442 struct iscsi_r2t_info
*r2t
;
444 /* nothing to do for mgmt */
448 /* flush task's r2t queues */
449 while (kfifo_out(&tcp_task
->r2tqueue
, (void*)&r2t
, sizeof(void*))) {
450 kfifo_in(&tcp_task
->r2tpool
.queue
, (void*)&r2t
,
452 ISCSI_DBG_TCP(task
->conn
, "pending r2t dropped\n");
457 kfifo_in(&tcp_task
->r2tpool
.queue
, (void*)&r2t
,
459 tcp_task
->r2t
= NULL
;
462 EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task
);
465 * iscsi_tcp_data_in - SCSI Data-In Response processing
466 * @conn: iscsi connection
467 * @task: scsi command task
469 static int iscsi_tcp_data_in(struct iscsi_conn
*conn
, struct iscsi_task
*task
)
471 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
472 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
473 struct iscsi_data_rsp
*rhdr
= (struct iscsi_data_rsp
*)tcp_conn
->in
.hdr
;
474 int datasn
= be32_to_cpu(rhdr
->datasn
);
475 unsigned total_in_length
= scsi_in(task
->sc
)->length
;
478 * lib iscsi will update this in the completion handling if there
481 if (!(rhdr
->flags
& ISCSI_FLAG_DATA_STATUS
))
482 iscsi_update_cmdsn(conn
->session
, (struct iscsi_nopin
*)rhdr
);
484 if (tcp_conn
->in
.datalen
== 0)
487 if (tcp_task
->exp_datasn
!= datasn
) {
488 ISCSI_DBG_TCP(conn
, "task->exp_datasn(%d) != rhdr->datasn(%d)"
489 "\n", tcp_task
->exp_datasn
, datasn
);
490 return ISCSI_ERR_DATASN
;
493 tcp_task
->exp_datasn
++;
495 tcp_task
->data_offset
= be32_to_cpu(rhdr
->offset
);
496 if (tcp_task
->data_offset
+ tcp_conn
->in
.datalen
> total_in_length
) {
497 ISCSI_DBG_TCP(conn
, "data_offset(%d) + data_len(%d) > "
498 "total_length_in(%d)\n", tcp_task
->data_offset
,
499 tcp_conn
->in
.datalen
, total_in_length
);
500 return ISCSI_ERR_DATA_OFFSET
;
503 conn
->datain_pdus_cnt
++;
508 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
509 * @conn: iscsi connection
510 * @task: scsi command task
512 static int iscsi_tcp_r2t_rsp(struct iscsi_conn
*conn
, struct iscsi_task
*task
)
514 struct iscsi_session
*session
= conn
->session
;
515 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
516 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
517 struct iscsi_r2t_rsp
*rhdr
= (struct iscsi_r2t_rsp
*)tcp_conn
->in
.hdr
;
518 struct iscsi_r2t_info
*r2t
;
519 int r2tsn
= be32_to_cpu(rhdr
->r2tsn
);
522 if (tcp_conn
->in
.datalen
) {
523 iscsi_conn_printk(KERN_ERR
, conn
,
524 "invalid R2t with datalen %d\n",
525 tcp_conn
->in
.datalen
);
526 return ISCSI_ERR_DATALEN
;
529 if (tcp_task
->exp_datasn
!= r2tsn
){
530 ISCSI_DBG_TCP(conn
, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
531 tcp_task
->exp_datasn
, r2tsn
);
532 return ISCSI_ERR_R2TSN
;
535 /* fill-in new R2T associated with the task */
536 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
538 if (!task
->sc
|| session
->state
!= ISCSI_STATE_LOGGED_IN
) {
539 iscsi_conn_printk(KERN_INFO
, conn
,
540 "dropping R2T itt %d in recovery.\n",
545 rc
= kfifo_out(&tcp_task
->r2tpool
.queue
, (void*)&r2t
, sizeof(void*));
547 iscsi_conn_printk(KERN_ERR
, conn
, "Could not allocate R2T. "
548 "Target has sent more R2Ts than it "
549 "negotiated for or driver has has leaked.\n");
550 return ISCSI_ERR_PROTO
;
553 r2t
->exp_statsn
= rhdr
->statsn
;
554 r2t
->data_length
= be32_to_cpu(rhdr
->data_length
);
555 if (r2t
->data_length
== 0) {
556 iscsi_conn_printk(KERN_ERR
, conn
,
557 "invalid R2T with zero data len\n");
558 kfifo_in(&tcp_task
->r2tpool
.queue
, (void*)&r2t
,
560 return ISCSI_ERR_DATALEN
;
563 if (r2t
->data_length
> session
->max_burst
)
564 ISCSI_DBG_TCP(conn
, "invalid R2T with data len %u and max "
565 "burst %u. Attempting to execute request.\n",
566 r2t
->data_length
, session
->max_burst
);
568 r2t
->data_offset
= be32_to_cpu(rhdr
->data_offset
);
569 if (r2t
->data_offset
+ r2t
->data_length
> scsi_out(task
->sc
)->length
) {
570 iscsi_conn_printk(KERN_ERR
, conn
,
571 "invalid R2T with data len %u at offset %u "
572 "and total length %d\n", r2t
->data_length
,
573 r2t
->data_offset
, scsi_out(task
->sc
)->length
);
574 kfifo_in(&tcp_task
->r2tpool
.queue
, (void*)&r2t
,
576 return ISCSI_ERR_DATALEN
;
579 r2t
->ttt
= rhdr
->ttt
; /* no flip */
583 tcp_task
->exp_datasn
= r2tsn
+ 1;
584 kfifo_in(&tcp_task
->r2tqueue
, (void*)&r2t
, sizeof(void*));
585 conn
->r2t_pdus_cnt
++;
587 iscsi_requeue_task(task
);
592 * Handle incoming reply to DataIn command
595 iscsi_tcp_process_data_in(struct iscsi_tcp_conn
*tcp_conn
,
596 struct iscsi_segment
*segment
)
598 struct iscsi_conn
*conn
= tcp_conn
->iscsi_conn
;
599 struct iscsi_hdr
*hdr
= tcp_conn
->in
.hdr
;
602 if (!iscsi_tcp_dgst_verify(tcp_conn
, segment
))
603 return ISCSI_ERR_DATA_DGST
;
605 /* check for non-exceptional status */
606 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
607 rc
= iscsi_complete_pdu(conn
, tcp_conn
->in
.hdr
, NULL
, 0);
612 iscsi_tcp_hdr_recv_prep(tcp_conn
);
617 * iscsi_tcp_hdr_dissect - process PDU header
618 * @conn: iSCSI connection
621 * This function analyzes the header of the PDU received,
622 * and performs several sanity checks. If the PDU is accompanied
623 * by data, the receive buffer is set up to copy the incoming data
624 * to the correct location.
627 iscsi_tcp_hdr_dissect(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
)
629 int rc
= 0, opcode
, ahslen
;
630 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
631 struct iscsi_task
*task
;
633 /* verify PDU length */
634 tcp_conn
->in
.datalen
= ntoh24(hdr
->dlength
);
635 if (tcp_conn
->in
.datalen
> conn
->max_recv_dlength
) {
636 iscsi_conn_printk(KERN_ERR
, conn
,
637 "iscsi_tcp: datalen %d > %d\n",
638 tcp_conn
->in
.datalen
, conn
->max_recv_dlength
);
639 return ISCSI_ERR_DATALEN
;
642 /* Additional header segments. So far, we don't
643 * process additional headers.
645 ahslen
= hdr
->hlength
<< 2;
647 opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
;
648 /* verify itt (itt encoding: age+cid+itt) */
649 rc
= iscsi_verify_itt(conn
, hdr
->itt
);
653 ISCSI_DBG_TCP(conn
, "opcode 0x%x ahslen %d datalen %d\n",
654 opcode
, ahslen
, tcp_conn
->in
.datalen
);
657 case ISCSI_OP_SCSI_DATA_IN
:
658 spin_lock(&conn
->session
->lock
);
659 task
= iscsi_itt_to_ctask(conn
, hdr
->itt
);
661 rc
= ISCSI_ERR_BAD_ITT
;
663 rc
= iscsi_tcp_data_in(conn
, task
);
665 spin_unlock(&conn
->session
->lock
);
669 if (tcp_conn
->in
.datalen
) {
670 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
671 struct hash_desc
*rx_hash
= NULL
;
672 struct scsi_data_buffer
*sdb
= scsi_in(task
->sc
);
675 * Setup copy of Data-In into the Scsi_Cmnd
677 * We set up the iscsi_segment to point to the next
678 * scatterlist entry to copy to. As we go along,
679 * we move on to the next scatterlist entry and
680 * update the digest per-entry.
682 if (conn
->datadgst_en
&&
683 !(conn
->session
->tt
->caps
& CAP_DIGEST_OFFLOAD
))
684 rx_hash
= tcp_conn
->rx_hash
;
686 ISCSI_DBG_TCP(conn
, "iscsi_tcp_begin_data_in( "
687 "offset=%d, datalen=%d)\n",
688 tcp_task
->data_offset
,
689 tcp_conn
->in
.datalen
);
690 task
->last_xfer
= jiffies
;
691 rc
= iscsi_segment_seek_sg(&tcp_conn
->in
.segment
,
694 tcp_task
->data_offset
,
695 tcp_conn
->in
.datalen
,
696 iscsi_tcp_process_data_in
,
698 spin_unlock(&conn
->session
->lock
);
701 rc
= __iscsi_complete_pdu(conn
, hdr
, NULL
, 0);
702 spin_unlock(&conn
->session
->lock
);
704 case ISCSI_OP_SCSI_CMD_RSP
:
705 if (tcp_conn
->in
.datalen
) {
706 iscsi_tcp_data_recv_prep(tcp_conn
);
709 rc
= iscsi_complete_pdu(conn
, hdr
, NULL
, 0);
712 spin_lock(&conn
->session
->lock
);
713 task
= iscsi_itt_to_ctask(conn
, hdr
->itt
);
715 rc
= ISCSI_ERR_BAD_ITT
;
717 rc
= ISCSI_ERR_AHSLEN
;
718 else if (task
->sc
->sc_data_direction
== DMA_TO_DEVICE
) {
719 task
->last_xfer
= jiffies
;
720 rc
= iscsi_tcp_r2t_rsp(conn
, task
);
722 rc
= ISCSI_ERR_PROTO
;
723 spin_unlock(&conn
->session
->lock
);
725 case ISCSI_OP_LOGIN_RSP
:
726 case ISCSI_OP_TEXT_RSP
:
727 case ISCSI_OP_REJECT
:
728 case ISCSI_OP_ASYNC_EVENT
:
730 * It is possible that we could get a PDU with a buffer larger
731 * than 8K, but there are no targets that currently do this.
732 * For now we fail until we find a vendor that needs it
734 if (ISCSI_DEF_MAX_RECV_SEG_LEN
< tcp_conn
->in
.datalen
) {
735 iscsi_conn_printk(KERN_ERR
, conn
,
736 "iscsi_tcp: received buffer of "
737 "len %u but conn buffer is only %u "
739 tcp_conn
->in
.datalen
,
740 ISCSI_DEF_MAX_RECV_SEG_LEN
, opcode
);
741 rc
= ISCSI_ERR_PROTO
;
745 /* If there's data coming in with the response,
746 * receive it to the connection's buffer.
748 if (tcp_conn
->in
.datalen
) {
749 iscsi_tcp_data_recv_prep(tcp_conn
);
753 case ISCSI_OP_LOGOUT_RSP
:
754 case ISCSI_OP_NOOP_IN
:
755 case ISCSI_OP_SCSI_TMFUNC_RSP
:
756 rc
= iscsi_complete_pdu(conn
, hdr
, NULL
, 0);
759 rc
= ISCSI_ERR_BAD_OPCODE
;
764 /* Anything that comes with data should have
765 * been handled above. */
766 if (tcp_conn
->in
.datalen
)
767 return ISCSI_ERR_PROTO
;
768 iscsi_tcp_hdr_recv_prep(tcp_conn
);
775 * iscsi_tcp_hdr_recv_done - process PDU header
777 * This is the callback invoked when the PDU header has
778 * been received. If the header is followed by additional
779 * header segments, we go back for more data.
782 iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn
*tcp_conn
,
783 struct iscsi_segment
*segment
)
785 struct iscsi_conn
*conn
= tcp_conn
->iscsi_conn
;
786 struct iscsi_hdr
*hdr
;
788 /* Check if there are additional header segments
789 * *prior* to computing the digest, because we
790 * may need to go back to the caller for more.
792 hdr
= (struct iscsi_hdr
*) tcp_conn
->in
.hdr_buf
;
793 if (segment
->copied
== sizeof(struct iscsi_hdr
) && hdr
->hlength
) {
794 /* Bump the header length - the caller will
795 * just loop around and get the AHS for us, and
797 unsigned int ahslen
= hdr
->hlength
<< 2;
799 /* Make sure we don't overflow */
800 if (sizeof(*hdr
) + ahslen
> sizeof(tcp_conn
->in
.hdr_buf
))
801 return ISCSI_ERR_AHSLEN
;
803 segment
->total_size
+= ahslen
;
804 segment
->size
+= ahslen
;
808 /* We're done processing the header. See if we're doing
809 * header digests; if so, set up the recv_digest buffer
810 * and go back for more. */
811 if (conn
->hdrdgst_en
&&
812 !(conn
->session
->tt
->caps
& CAP_DIGEST_OFFLOAD
)) {
813 if (segment
->digest_len
== 0) {
815 * Even if we offload the digest processing we
816 * splice it in so we can increment the skb/segment
817 * counters in preparation for the data segment.
819 iscsi_tcp_segment_splice_digest(segment
,
820 segment
->recv_digest
);
824 iscsi_tcp_dgst_header(tcp_conn
->rx_hash
, hdr
,
825 segment
->total_copied
- ISCSI_DIGEST_SIZE
,
828 if (!iscsi_tcp_dgst_verify(tcp_conn
, segment
))
829 return ISCSI_ERR_HDR_DGST
;
832 tcp_conn
->in
.hdr
= hdr
;
833 return iscsi_tcp_hdr_dissect(conn
, hdr
);
837 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
838 * @tcp_conn: iscsi tcp conn
840 * returns non zero if we are currently processing or setup to process
843 inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn
*tcp_conn
)
845 return tcp_conn
->in
.segment
.done
== iscsi_tcp_hdr_recv_done
;
847 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr
);
850 * iscsi_tcp_recv_skb - Process skb
851 * @conn: iscsi connection
852 * @skb: network buffer with header and/or data segment
853 * @offset: offset in skb
854 * @offload: bool indicating if transfer was offloaded
856 * Will return status of transfer in status. And will return
857 * number of bytes copied.
859 int iscsi_tcp_recv_skb(struct iscsi_conn
*conn
, struct sk_buff
*skb
,
860 unsigned int offset
, bool offloaded
, int *status
)
862 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
863 struct iscsi_segment
*segment
= &tcp_conn
->in
.segment
;
864 struct skb_seq_state seq
;
865 unsigned int consumed
= 0;
868 ISCSI_DBG_TCP(conn
, "in %d bytes\n", skb
->len
- offset
);
870 * Update for each skb instead of pdu, because over slow networks a
871 * data_in's data could take a while to read in. We also want to
874 conn
->last_recv
= jiffies
;
876 if (unlikely(conn
->suspend_rx
)) {
877 ISCSI_DBG_TCP(conn
, "Rx suspended!\n");
878 *status
= ISCSI_TCP_SUSPENDED
;
883 segment
->total_copied
= segment
->total_size
;
887 skb_prepare_seq_read(skb
, offset
, skb
->len
, &seq
);
892 avail
= skb_seq_read(consumed
, &ptr
, &seq
);
894 ISCSI_DBG_TCP(conn
, "no more data avail. Consumed %d\n",
896 *status
= ISCSI_TCP_SKB_DONE
;
897 skb_abort_seq_read(&seq
);
900 BUG_ON(segment
->copied
>= segment
->size
);
902 ISCSI_DBG_TCP(conn
, "skb %p ptr=%p avail=%u\n", skb
, ptr
,
904 rc
= iscsi_tcp_segment_recv(tcp_conn
, segment
, ptr
, avail
);
908 if (segment
->total_copied
>= segment
->total_size
) {
909 skb_abort_seq_read(&seq
);
915 *status
= ISCSI_TCP_SEGMENT_DONE
;
916 ISCSI_DBG_TCP(conn
, "segment done\n");
917 rc
= segment
->done(tcp_conn
, segment
);
919 *status
= ISCSI_TCP_CONN_ERR
;
920 ISCSI_DBG_TCP(conn
, "Error receiving PDU, errno=%d\n", rc
);
921 iscsi_conn_failure(conn
, rc
);
924 /* The done() functions sets up the next segment. */
927 conn
->rxdata_octets
+= consumed
;
930 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb
);
933 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
934 * @conn: iscsi connection
935 * @task: scsi command task
938 int iscsi_tcp_task_init(struct iscsi_task
*task
)
940 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
941 struct iscsi_conn
*conn
= task
->conn
;
942 struct scsi_cmnd
*sc
= task
->sc
;
947 * mgmt tasks do not have a scatterlist since they come
948 * in from the iscsi interface.
950 ISCSI_DBG_TCP(conn
, "mtask deq [itt 0x%x]\n", task
->itt
);
952 return conn
->session
->tt
->init_pdu(task
, 0, task
->data_count
);
955 BUG_ON(kfifo_len(&tcp_task
->r2tqueue
));
956 tcp_task
->exp_datasn
= 0;
958 /* Prepare PDU, optionally w/ immediate data */
959 ISCSI_DBG_TCP(conn
, "task deq [itt 0x%x imm %d unsol %d]\n",
960 task
->itt
, task
->imm_count
, task
->unsol_r2t
.data_length
);
962 err
= conn
->session
->tt
->init_pdu(task
, 0, task
->imm_count
);
968 EXPORT_SYMBOL_GPL(iscsi_tcp_task_init
);
970 static struct iscsi_r2t_info
*iscsi_tcp_get_curr_r2t(struct iscsi_task
*task
)
972 struct iscsi_session
*session
= task
->conn
->session
;
973 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
974 struct iscsi_r2t_info
*r2t
= NULL
;
976 if (iscsi_task_has_unsol_data(task
))
977 r2t
= &task
->unsol_r2t
;
979 spin_lock_bh(&session
->lock
);
982 /* Continue with this R2T? */
983 if (r2t
->data_length
<= r2t
->sent
) {
984 ISCSI_DBG_TCP(task
->conn
,
985 " done with r2t %p\n", r2t
);
986 kfifo_in(&tcp_task
->r2tpool
.queue
,
987 (void *)&tcp_task
->r2t
,
989 tcp_task
->r2t
= r2t
= NULL
;
994 if (kfifo_out(&tcp_task
->r2tqueue
,
995 (void *)&tcp_task
->r2t
, sizeof(void *)) !=
1001 spin_unlock_bh(&session
->lock
);
1008 * iscsi_tcp_task_xmit - xmit normal PDU task
1009 * @task: iscsi command task
1011 * We're expected to return 0 when everything was transmitted successfully,
1012 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1015 int iscsi_tcp_task_xmit(struct iscsi_task
*task
)
1017 struct iscsi_conn
*conn
= task
->conn
;
1018 struct iscsi_session
*session
= conn
->session
;
1019 struct iscsi_r2t_info
*r2t
;
1023 /* Flush any pending data first. */
1024 rc
= session
->tt
->xmit_pdu(task
);
1030 if (task
->hdr
->itt
== RESERVED_ITT
)
1031 iscsi_put_task(task
);
1035 /* Are we done already? */
1036 if (task
->sc
->sc_data_direction
!= DMA_TO_DEVICE
)
1039 r2t
= iscsi_tcp_get_curr_r2t(task
);
1041 /* Waiting for more R2Ts to arrive. */
1042 ISCSI_DBG_TCP(conn
, "no R2Ts yet\n");
1046 rc
= conn
->session
->tt
->alloc_pdu(task
, ISCSI_OP_SCSI_DATA_OUT
);
1049 iscsi_prep_data_out_pdu(task
, r2t
, (struct iscsi_data
*) task
->hdr
);
1051 ISCSI_DBG_TCP(conn
, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1052 r2t
, r2t
->datasn
- 1, task
->hdr
->itt
,
1053 r2t
->data_offset
+ r2t
->sent
, r2t
->data_count
);
1055 rc
= conn
->session
->tt
->init_pdu(task
, r2t
->data_offset
+ r2t
->sent
,
1058 iscsi_conn_failure(conn
, ISCSI_ERR_XMIT_FAILED
);
1062 r2t
->sent
+= r2t
->data_count
;
1065 EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit
);
1067 struct iscsi_cls_conn
*
1068 iscsi_tcp_conn_setup(struct iscsi_cls_session
*cls_session
, int dd_data_size
,
1072 struct iscsi_conn
*conn
;
1073 struct iscsi_cls_conn
*cls_conn
;
1074 struct iscsi_tcp_conn
*tcp_conn
;
1076 cls_conn
= iscsi_conn_setup(cls_session
, sizeof(*tcp_conn
), conn_idx
);
1079 conn
= cls_conn
->dd_data
;
1081 * due to strange issues with iser these are not set
1082 * in iscsi_conn_setup
1084 conn
->max_recv_dlength
= ISCSI_DEF_MAX_RECV_SEG_LEN
;
1086 tcp_conn
= conn
->dd_data
;
1087 tcp_conn
->iscsi_conn
= conn
;
1089 tcp_conn
->dd_data
= kzalloc(dd_data_size
, GFP_KERNEL
);
1090 if (!tcp_conn
->dd_data
) {
1091 iscsi_conn_teardown(cls_conn
);
1096 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup
);
1098 void iscsi_tcp_conn_teardown(struct iscsi_cls_conn
*cls_conn
)
1100 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1101 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
1103 kfree(tcp_conn
->dd_data
);
1104 iscsi_conn_teardown(cls_conn
);
1106 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown
);
1108 int iscsi_tcp_r2tpool_alloc(struct iscsi_session
*session
)
1114 * initialize per-task: R2T pool and xmit queue
1116 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
1117 struct iscsi_task
*task
= session
->cmds
[cmd_i
];
1118 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
1121 * pre-allocated x2 as much r2ts to handle race when
1122 * target acks DataOut faster than we data_xmit() queues
1123 * could replenish r2tqueue.
1127 if (iscsi_pool_init(&tcp_task
->r2tpool
,
1128 session
->max_r2t
* 2, NULL
,
1129 sizeof(struct iscsi_r2t_info
))) {
1130 goto r2t_alloc_fail
;
1133 /* R2T xmit queue */
1134 if (kfifo_alloc(&tcp_task
->r2tqueue
,
1135 session
->max_r2t
* 4 * sizeof(void*), GFP_KERNEL
)) {
1136 iscsi_pool_free(&tcp_task
->r2tpool
);
1137 goto r2t_alloc_fail
;
1144 for (i
= 0; i
< cmd_i
; i
++) {
1145 struct iscsi_task
*task
= session
->cmds
[i
];
1146 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
1148 kfifo_free(&tcp_task
->r2tqueue
);
1149 iscsi_pool_free(&tcp_task
->r2tpool
);
1153 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc
);
1155 void iscsi_tcp_r2tpool_free(struct iscsi_session
*session
)
1159 for (i
= 0; i
< session
->cmds_max
; i
++) {
1160 struct iscsi_task
*task
= session
->cmds
[i
];
1161 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
1163 kfifo_free(&tcp_task
->r2tqueue
);
1164 iscsi_pool_free(&tcp_task
->r2tpool
);
1167 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free
);
1169 void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn
*cls_conn
,
1170 struct iscsi_stats
*stats
)
1172 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1174 stats
->txdata_octets
= conn
->txdata_octets
;
1175 stats
->rxdata_octets
= conn
->rxdata_octets
;
1176 stats
->scsicmd_pdus
= conn
->scsicmd_pdus_cnt
;
1177 stats
->dataout_pdus
= conn
->dataout_pdus_cnt
;
1178 stats
->scsirsp_pdus
= conn
->scsirsp_pdus_cnt
;
1179 stats
->datain_pdus
= conn
->datain_pdus_cnt
;
1180 stats
->r2t_pdus
= conn
->r2t_pdus_cnt
;
1181 stats
->tmfcmd_pdus
= conn
->tmfcmd_pdus_cnt
;
1182 stats
->tmfrsp_pdus
= conn
->tmfrsp_pdus_cnt
;
1184 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats
);