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/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>
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>
45 #include "iscsi_tcp.h"
47 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
48 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
49 "Alex Aizman <itn780@yahoo.com>");
50 MODULE_DESCRIPTION("iSCSI/TCP data-path");
51 MODULE_LICENSE("GPL");
55 #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
57 #define debug_tcp(fmt...)
60 static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn
*tcp_conn
,
61 struct iscsi_segment
*segment
);
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
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.
77 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
78 * @segment: the buffer object
80 * @offset: byte offset into that sg entry
82 * This function sets up the segment so that subsequent
83 * data is copied to the indicated sg entry, at the given
87 iscsi_tcp_segment_init_sg(struct iscsi_segment
*segment
,
88 struct scatterlist
*sg
, unsigned int offset
)
91 segment
->sg_offset
= offset
;
92 segment
->size
= min(sg
->length
- offset
,
93 segment
->total_size
- segment
->total_copied
);
98 * iscsi_tcp_segment_map - map the current S/G page
99 * @segment: iscsi_segment
100 * @recv: 1 if called from recv path
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
106 static void iscsi_tcp_segment_map(struct iscsi_segment
*segment
, int recv
)
108 struct scatterlist
*sg
;
110 if (segment
->data
!= NULL
|| !segment
->sg
)
114 BUG_ON(segment
->sg_mapped
);
115 BUG_ON(sg
->length
== 0);
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
123 if (page_count(sg_page(sg
)) >= 1 && !recv
)
126 debug_tcp("iscsi_tcp_segment_map %s %p\n", recv
? "recv" : "xmit",
128 segment
->sg_mapped
= kmap_atomic(sg_page(sg
), KM_SOFTIRQ0
);
129 segment
->data
= segment
->sg_mapped
+ sg
->offset
+ segment
->sg_offset
;
132 void iscsi_tcp_segment_unmap(struct iscsi_segment
*segment
)
134 debug_tcp("iscsi_tcp_segment_unmap %p\n", segment
);
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
;
143 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap
);
146 * Splice the digest buffer into the buffer
149 iscsi_tcp_segment_splice_digest(struct iscsi_segment
*segment
, void *digest
)
151 segment
->data
= digest
;
152 segment
->digest_len
= ISCSI_DIGEST_SIZE
;
153 segment
->total_size
+= ISCSI_DIGEST_SIZE
;
154 segment
->size
= ISCSI_DIGEST_SIZE
;
157 segment
->hash
= NULL
;
161 * iscsi_tcp_segment_done - check whether the segment is complete
162 * @tcp_conn: iscsi tcp connection
163 * @segment: iscsi segment to check
164 * @recv: set to one of this is called from the recv path
165 * @copied: number of bytes copied
167 * Check if we're done receiving this segment. If the receive
168 * buffer is full but we expect more data, move on to the
169 * next entry in the scatterlist.
171 * If the amount of data we received isn't a multiple of 4,
172 * we will transparently receive the pad bytes, too.
174 * This function must be re-entrant.
176 int iscsi_tcp_segment_done(struct iscsi_tcp_conn
*tcp_conn
,
177 struct iscsi_segment
*segment
, int recv
,
180 struct scatterlist sg
;
183 debug_tcp("copied %u %u size %u %s\n", segment
->copied
, copied
,
184 segment
->size
, recv
? "recv" : "xmit");
185 if (segment
->hash
&& copied
) {
187 * If a segment is kmapd we must unmap it before sending
188 * to the crypto layer since that will try to kmap it again.
190 iscsi_tcp_segment_unmap(segment
);
192 if (!segment
->data
) {
193 sg_init_table(&sg
, 1);
194 sg_set_page(&sg
, sg_page(segment
->sg
), copied
,
195 segment
->copied
+ segment
->sg_offset
+
196 segment
->sg
->offset
);
198 sg_init_one(&sg
, segment
->data
+ segment
->copied
,
200 crypto_hash_update(segment
->hash
, &sg
, copied
);
203 segment
->copied
+= copied
;
204 if (segment
->copied
< segment
->size
) {
205 iscsi_tcp_segment_map(segment
, recv
);
209 segment
->total_copied
+= segment
->copied
;
213 /* Unmap the current scatterlist page, if there is one. */
214 iscsi_tcp_segment_unmap(segment
);
216 /* Do we have more scatterlist entries? */
217 debug_tcp("total copied %u total size %u\n", segment
->total_copied
,
218 segment
->total_size
);
219 if (segment
->total_copied
< segment
->total_size
) {
220 /* Proceed to the next entry in the scatterlist. */
221 iscsi_tcp_segment_init_sg(segment
, sg_next(segment
->sg
),
223 iscsi_tcp_segment_map(segment
, recv
);
224 BUG_ON(segment
->size
== 0);
228 /* Do we need to handle padding? */
229 if (!(tcp_conn
->iscsi_conn
->session
->tt
->caps
& CAP_PADDING_OFFLOAD
)) {
230 pad
= iscsi_padding(segment
->total_copied
);
232 debug_tcp("consume %d pad bytes\n", pad
);
233 segment
->total_size
+= pad
;
235 segment
->data
= segment
->padbuf
;
241 * Set us up for transferring the data digest. hdr digest
242 * is completely handled in hdr done function.
245 crypto_hash_final(segment
->hash
, segment
->digest
);
246 iscsi_tcp_segment_splice_digest(segment
,
247 recv
? segment
->recv_digest
: segment
->digest
);
253 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done
);
256 * iscsi_tcp_segment_recv - copy data to segment
257 * @tcp_conn: the iSCSI TCP connection
258 * @segment: the buffer to copy to
260 * @len: amount of data available
262 * This function copies up to @len bytes to the
263 * given buffer, and returns the number of bytes
264 * consumed, which can actually be less than @len.
266 * If hash digest is enabled, the function will update the
267 * hash while copying.
268 * Combining these two operations doesn't buy us a lot (yet),
269 * but in the future we could implement combined copy+crc,
270 * just way we do for network layer checksums.
273 iscsi_tcp_segment_recv(struct iscsi_tcp_conn
*tcp_conn
,
274 struct iscsi_segment
*segment
, const void *ptr
,
277 unsigned int copy
= 0, copied
= 0;
279 while (!iscsi_tcp_segment_done(tcp_conn
, segment
, 1, copy
)) {
281 debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n",
286 copy
= min(len
- copied
, segment
->size
- segment
->copied
);
287 debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy
);
288 memcpy(segment
->data
+ segment
->copied
, ptr
+ copied
, copy
);
295 iscsi_tcp_dgst_header(struct hash_desc
*hash
, const void *hdr
, size_t hdrlen
,
296 unsigned char digest
[ISCSI_DIGEST_SIZE
])
298 struct scatterlist sg
;
300 sg_init_one(&sg
, hdr
, hdrlen
);
301 crypto_hash_digest(hash
, &sg
, hdrlen
, digest
);
303 EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header
);
306 iscsi_tcp_dgst_verify(struct iscsi_tcp_conn
*tcp_conn
,
307 struct iscsi_segment
*segment
)
309 if (!segment
->digest_len
)
312 if (memcmp(segment
->recv_digest
, segment
->digest
,
313 segment
->digest_len
)) {
314 debug_scsi("digest mismatch\n");
322 * Helper function to set up segment buffer
325 __iscsi_segment_init(struct iscsi_segment
*segment
, size_t size
,
326 iscsi_segment_done_fn_t
*done
, struct hash_desc
*hash
)
328 memset(segment
, 0, sizeof(*segment
));
329 segment
->total_size
= size
;
330 segment
->done
= done
;
333 segment
->hash
= hash
;
334 crypto_hash_init(hash
);
339 iscsi_segment_init_linear(struct iscsi_segment
*segment
, void *data
,
340 size_t size
, iscsi_segment_done_fn_t
*done
,
341 struct hash_desc
*hash
)
343 __iscsi_segment_init(segment
, size
, done
, hash
);
344 segment
->data
= data
;
345 segment
->size
= size
;
347 EXPORT_SYMBOL_GPL(iscsi_segment_init_linear
);
350 iscsi_segment_seek_sg(struct iscsi_segment
*segment
,
351 struct scatterlist
*sg_list
, unsigned int sg_count
,
352 unsigned int offset
, size_t size
,
353 iscsi_segment_done_fn_t
*done
, struct hash_desc
*hash
)
355 struct scatterlist
*sg
;
358 debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n",
360 __iscsi_segment_init(segment
, size
, done
, hash
);
361 for_each_sg(sg_list
, sg
, sg_count
, i
) {
362 debug_scsi("sg %d, len %u offset %u\n", i
, sg
->length
,
364 if (offset
< sg
->length
) {
365 iscsi_tcp_segment_init_sg(segment
, sg
, offset
);
368 offset
-= sg
->length
;
371 return ISCSI_ERR_DATA_OFFSET
;
373 EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg
);
376 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
377 * @tcp_conn: iscsi connection to prep for
379 * This function always passes NULL for the hash argument, because when this
380 * function is called we do not yet know the final size of the header and want
381 * to delay the digest processing until we know that.
383 void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn
*tcp_conn
)
385 debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn
,
386 tcp_conn
->iscsi_conn
->hdrdgst_en
? ", digest enabled" : "");
387 iscsi_segment_init_linear(&tcp_conn
->in
.segment
,
388 tcp_conn
->in
.hdr_buf
, sizeof(struct iscsi_hdr
),
389 iscsi_tcp_hdr_recv_done
, NULL
);
391 EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep
);
394 * Handle incoming reply to any other type of command
397 iscsi_tcp_data_recv_done(struct iscsi_tcp_conn
*tcp_conn
,
398 struct iscsi_segment
*segment
)
400 struct iscsi_conn
*conn
= tcp_conn
->iscsi_conn
;
403 if (!iscsi_tcp_dgst_verify(tcp_conn
, segment
))
404 return ISCSI_ERR_DATA_DGST
;
406 rc
= iscsi_complete_pdu(conn
, tcp_conn
->in
.hdr
,
407 conn
->data
, tcp_conn
->in
.datalen
);
411 iscsi_tcp_hdr_recv_prep(tcp_conn
);
416 iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn
*tcp_conn
)
418 struct iscsi_conn
*conn
= tcp_conn
->iscsi_conn
;
419 struct hash_desc
*rx_hash
= NULL
;
421 if (conn
->datadgst_en
&
422 !(conn
->session
->tt
->caps
& CAP_DIGEST_OFFLOAD
))
423 rx_hash
= tcp_conn
->rx_hash
;
425 iscsi_segment_init_linear(&tcp_conn
->in
.segment
,
426 conn
->data
, tcp_conn
->in
.datalen
,
427 iscsi_tcp_data_recv_done
, rx_hash
);
431 * iscsi_tcp_cleanup_task - free tcp_task resources
434 * must be called with session lock
436 void iscsi_tcp_cleanup_task(struct iscsi_task
*task
)
438 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
439 struct iscsi_r2t_info
*r2t
;
441 /* nothing to do for mgmt or pending tasks */
442 if (!task
->sc
|| task
->state
== ISCSI_TASK_PENDING
)
445 /* flush task's r2t queues */
446 while (__kfifo_get(tcp_task
->r2tqueue
, (void*)&r2t
, sizeof(void*))) {
447 __kfifo_put(tcp_task
->r2tpool
.queue
, (void*)&r2t
,
449 debug_scsi("iscsi_tcp_cleanup_task pending r2t dropped\n");
454 __kfifo_put(tcp_task
->r2tpool
.queue
, (void*)&r2t
,
456 tcp_task
->r2t
= NULL
;
459 EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task
);
462 * iscsi_tcp_data_in - SCSI Data-In Response processing
463 * @conn: iscsi connection
464 * @task: scsi command task
466 static int iscsi_tcp_data_in(struct iscsi_conn
*conn
, struct iscsi_task
*task
)
468 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
469 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
470 struct iscsi_data_rsp
*rhdr
= (struct iscsi_data_rsp
*)tcp_conn
->in
.hdr
;
471 int datasn
= be32_to_cpu(rhdr
->datasn
);
472 unsigned total_in_length
= scsi_in(task
->sc
)->length
;
474 iscsi_update_cmdsn(conn
->session
, (struct iscsi_nopin
*)rhdr
);
475 if (tcp_conn
->in
.datalen
== 0)
478 if (tcp_task
->exp_datasn
!= datasn
) {
479 debug_tcp("%s: task->exp_datasn(%d) != rhdr->datasn(%d)\n",
480 __func__
, tcp_task
->exp_datasn
, datasn
);
481 return ISCSI_ERR_DATASN
;
484 tcp_task
->exp_datasn
++;
486 tcp_task
->data_offset
= be32_to_cpu(rhdr
->offset
);
487 if (tcp_task
->data_offset
+ tcp_conn
->in
.datalen
> total_in_length
) {
488 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
489 __func__
, tcp_task
->data_offset
,
490 tcp_conn
->in
.datalen
, total_in_length
);
491 return ISCSI_ERR_DATA_OFFSET
;
494 conn
->datain_pdus_cnt
++;
499 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
500 * @conn: iscsi connection
501 * @task: scsi command task
503 static int iscsi_tcp_r2t_rsp(struct iscsi_conn
*conn
, struct iscsi_task
*task
)
505 struct iscsi_session
*session
= conn
->session
;
506 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
507 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
508 struct iscsi_r2t_rsp
*rhdr
= (struct iscsi_r2t_rsp
*)tcp_conn
->in
.hdr
;
509 struct iscsi_r2t_info
*r2t
;
510 int r2tsn
= be32_to_cpu(rhdr
->r2tsn
);
513 if (tcp_conn
->in
.datalen
) {
514 iscsi_conn_printk(KERN_ERR
, conn
,
515 "invalid R2t with datalen %d\n",
516 tcp_conn
->in
.datalen
);
517 return ISCSI_ERR_DATALEN
;
520 if (tcp_task
->exp_datasn
!= r2tsn
){
521 debug_tcp("%s: task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
522 __func__
, tcp_task
->exp_datasn
, r2tsn
);
523 return ISCSI_ERR_R2TSN
;
526 /* fill-in new R2T associated with the task */
527 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
529 if (!task
->sc
|| session
->state
!= ISCSI_STATE_LOGGED_IN
) {
530 iscsi_conn_printk(KERN_INFO
, conn
,
531 "dropping R2T itt %d in recovery.\n",
536 rc
= __kfifo_get(tcp_task
->r2tpool
.queue
, (void*)&r2t
, sizeof(void*));
538 iscsi_conn_printk(KERN_ERR
, conn
, "Could not allocate R2T. "
539 "Target has sent more R2Ts than it "
540 "negotiated for or driver has has leaked.\n");
541 return ISCSI_ERR_PROTO
;
544 r2t
->exp_statsn
= rhdr
->statsn
;
545 r2t
->data_length
= be32_to_cpu(rhdr
->data_length
);
546 if (r2t
->data_length
== 0) {
547 iscsi_conn_printk(KERN_ERR
, conn
,
548 "invalid R2T with zero data len\n");
549 __kfifo_put(tcp_task
->r2tpool
.queue
, (void*)&r2t
,
551 return ISCSI_ERR_DATALEN
;
554 if (r2t
->data_length
> session
->max_burst
)
555 debug_scsi("invalid R2T with data len %u and max burst %u."
556 "Attempting to execute request.\n",
557 r2t
->data_length
, session
->max_burst
);
559 r2t
->data_offset
= be32_to_cpu(rhdr
->data_offset
);
560 if (r2t
->data_offset
+ r2t
->data_length
> scsi_out(task
->sc
)->length
) {
561 iscsi_conn_printk(KERN_ERR
, conn
,
562 "invalid R2T with data len %u at offset %u "
563 "and total length %d\n", r2t
->data_length
,
564 r2t
->data_offset
, scsi_out(task
->sc
)->length
);
565 __kfifo_put(tcp_task
->r2tpool
.queue
, (void*)&r2t
,
567 return ISCSI_ERR_DATALEN
;
570 r2t
->ttt
= rhdr
->ttt
; /* no flip */
574 tcp_task
->exp_datasn
= r2tsn
+ 1;
575 __kfifo_put(tcp_task
->r2tqueue
, (void*)&r2t
, sizeof(void*));
576 conn
->r2t_pdus_cnt
++;
578 iscsi_requeue_task(task
);
583 * Handle incoming reply to DataIn command
586 iscsi_tcp_process_data_in(struct iscsi_tcp_conn
*tcp_conn
,
587 struct iscsi_segment
*segment
)
589 struct iscsi_conn
*conn
= tcp_conn
->iscsi_conn
;
590 struct iscsi_hdr
*hdr
= tcp_conn
->in
.hdr
;
593 if (!iscsi_tcp_dgst_verify(tcp_conn
, segment
))
594 return ISCSI_ERR_DATA_DGST
;
596 /* check for non-exceptional status */
597 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
598 rc
= iscsi_complete_pdu(conn
, tcp_conn
->in
.hdr
, NULL
, 0);
603 iscsi_tcp_hdr_recv_prep(tcp_conn
);
608 * iscsi_tcp_hdr_dissect - process PDU header
609 * @conn: iSCSI connection
612 * This function analyzes the header of the PDU received,
613 * and performs several sanity checks. If the PDU is accompanied
614 * by data, the receive buffer is set up to copy the incoming data
615 * to the correct location.
618 iscsi_tcp_hdr_dissect(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
)
620 int rc
= 0, opcode
, ahslen
;
621 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
622 struct iscsi_task
*task
;
624 /* verify PDU length */
625 tcp_conn
->in
.datalen
= ntoh24(hdr
->dlength
);
626 if (tcp_conn
->in
.datalen
> conn
->max_recv_dlength
) {
627 iscsi_conn_printk(KERN_ERR
, conn
,
628 "iscsi_tcp: datalen %d > %d\n",
629 tcp_conn
->in
.datalen
, conn
->max_recv_dlength
);
630 return ISCSI_ERR_DATALEN
;
633 /* Additional header segments. So far, we don't
634 * process additional headers.
636 ahslen
= hdr
->hlength
<< 2;
638 opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
;
639 /* verify itt (itt encoding: age+cid+itt) */
640 rc
= iscsi_verify_itt(conn
, hdr
->itt
);
644 debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
645 opcode
, ahslen
, tcp_conn
->in
.datalen
);
648 case ISCSI_OP_SCSI_DATA_IN
:
649 spin_lock(&conn
->session
->lock
);
650 task
= iscsi_itt_to_ctask(conn
, hdr
->itt
);
652 rc
= ISCSI_ERR_BAD_ITT
;
654 rc
= iscsi_tcp_data_in(conn
, task
);
656 spin_unlock(&conn
->session
->lock
);
660 if (tcp_conn
->in
.datalen
) {
661 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
662 struct hash_desc
*rx_hash
= NULL
;
663 struct scsi_data_buffer
*sdb
= scsi_in(task
->sc
);
666 * Setup copy of Data-In into the Scsi_Cmnd
668 * We set up the iscsi_segment to point to the next
669 * scatterlist entry to copy to. As we go along,
670 * we move on to the next scatterlist entry and
671 * update the digest per-entry.
673 if (conn
->datadgst_en
&&
674 !(conn
->session
->tt
->caps
& CAP_DIGEST_OFFLOAD
))
675 rx_hash
= tcp_conn
->rx_hash
;
677 debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
678 "datalen=%d)\n", tcp_conn
,
679 tcp_task
->data_offset
,
680 tcp_conn
->in
.datalen
);
681 rc
= iscsi_segment_seek_sg(&tcp_conn
->in
.segment
,
684 tcp_task
->data_offset
,
685 tcp_conn
->in
.datalen
,
686 iscsi_tcp_process_data_in
,
688 spin_unlock(&conn
->session
->lock
);
691 rc
= __iscsi_complete_pdu(conn
, hdr
, NULL
, 0);
692 spin_unlock(&conn
->session
->lock
);
694 case ISCSI_OP_SCSI_CMD_RSP
:
695 if (tcp_conn
->in
.datalen
) {
696 iscsi_tcp_data_recv_prep(tcp_conn
);
699 rc
= iscsi_complete_pdu(conn
, hdr
, NULL
, 0);
702 spin_lock(&conn
->session
->lock
);
703 task
= iscsi_itt_to_ctask(conn
, hdr
->itt
);
705 rc
= ISCSI_ERR_BAD_ITT
;
707 rc
= ISCSI_ERR_AHSLEN
;
708 else if (task
->sc
->sc_data_direction
== DMA_TO_DEVICE
)
709 rc
= iscsi_tcp_r2t_rsp(conn
, task
);
711 rc
= ISCSI_ERR_PROTO
;
712 spin_unlock(&conn
->session
->lock
);
714 case ISCSI_OP_LOGIN_RSP
:
715 case ISCSI_OP_TEXT_RSP
:
716 case ISCSI_OP_REJECT
:
717 case ISCSI_OP_ASYNC_EVENT
:
719 * It is possible that we could get a PDU with a buffer larger
720 * than 8K, but there are no targets that currently do this.
721 * For now we fail until we find a vendor that needs it
723 if (ISCSI_DEF_MAX_RECV_SEG_LEN
< tcp_conn
->in
.datalen
) {
724 iscsi_conn_printk(KERN_ERR
, conn
,
725 "iscsi_tcp: received buffer of "
726 "len %u but conn buffer is only %u "
728 tcp_conn
->in
.datalen
,
729 ISCSI_DEF_MAX_RECV_SEG_LEN
, opcode
);
730 rc
= ISCSI_ERR_PROTO
;
734 /* If there's data coming in with the response,
735 * receive it to the connection's buffer.
737 if (tcp_conn
->in
.datalen
) {
738 iscsi_tcp_data_recv_prep(tcp_conn
);
742 case ISCSI_OP_LOGOUT_RSP
:
743 case ISCSI_OP_NOOP_IN
:
744 case ISCSI_OP_SCSI_TMFUNC_RSP
:
745 rc
= iscsi_complete_pdu(conn
, hdr
, NULL
, 0);
748 rc
= ISCSI_ERR_BAD_OPCODE
;
753 /* Anything that comes with data should have
754 * been handled above. */
755 if (tcp_conn
->in
.datalen
)
756 return ISCSI_ERR_PROTO
;
757 iscsi_tcp_hdr_recv_prep(tcp_conn
);
764 * iscsi_tcp_hdr_recv_done - process PDU header
766 * This is the callback invoked when the PDU header has
767 * been received. If the header is followed by additional
768 * header segments, we go back for more data.
771 iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn
*tcp_conn
,
772 struct iscsi_segment
*segment
)
774 struct iscsi_conn
*conn
= tcp_conn
->iscsi_conn
;
775 struct iscsi_hdr
*hdr
;
777 /* Check if there are additional header segments
778 * *prior* to computing the digest, because we
779 * may need to go back to the caller for more.
781 hdr
= (struct iscsi_hdr
*) tcp_conn
->in
.hdr_buf
;
782 if (segment
->copied
== sizeof(struct iscsi_hdr
) && hdr
->hlength
) {
783 /* Bump the header length - the caller will
784 * just loop around and get the AHS for us, and
786 unsigned int ahslen
= hdr
->hlength
<< 2;
788 /* Make sure we don't overflow */
789 if (sizeof(*hdr
) + ahslen
> sizeof(tcp_conn
->in
.hdr_buf
))
790 return ISCSI_ERR_AHSLEN
;
792 segment
->total_size
+= ahslen
;
793 segment
->size
+= ahslen
;
797 /* We're done processing the header. See if we're doing
798 * header digests; if so, set up the recv_digest buffer
799 * and go back for more. */
800 if (conn
->hdrdgst_en
&&
801 !(conn
->session
->tt
->caps
& CAP_DIGEST_OFFLOAD
)) {
802 if (segment
->digest_len
== 0) {
804 * Even if we offload the digest processing we
805 * splice it in so we can increment the skb/segment
806 * counters in preparation for the data segment.
808 iscsi_tcp_segment_splice_digest(segment
,
809 segment
->recv_digest
);
813 iscsi_tcp_dgst_header(tcp_conn
->rx_hash
, hdr
,
814 segment
->total_copied
- ISCSI_DIGEST_SIZE
,
817 if (!iscsi_tcp_dgst_verify(tcp_conn
, segment
))
818 return ISCSI_ERR_HDR_DGST
;
821 tcp_conn
->in
.hdr
= hdr
;
822 return iscsi_tcp_hdr_dissect(conn
, hdr
);
826 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
827 * @tcp_conn: iscsi tcp conn
829 * returns non zero if we are currently processing or setup to process
832 inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn
*tcp_conn
)
834 return tcp_conn
->in
.segment
.done
== iscsi_tcp_hdr_recv_done
;
836 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr
);
839 * iscsi_tcp_recv_skb - Process skb
840 * @conn: iscsi connection
841 * @skb: network buffer with header and/or data segment
842 * @offset: offset in skb
843 * @offload: bool indicating if transfer was offloaded
845 * Will return status of transfer in status. And will return
846 * number of bytes copied.
848 int iscsi_tcp_recv_skb(struct iscsi_conn
*conn
, struct sk_buff
*skb
,
849 unsigned int offset
, bool offloaded
, int *status
)
851 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
852 struct iscsi_segment
*segment
= &tcp_conn
->in
.segment
;
853 struct skb_seq_state seq
;
854 unsigned int consumed
= 0;
857 debug_tcp("in %d bytes\n", skb
->len
- offset
);
859 if (unlikely(conn
->suspend_rx
)) {
860 debug_tcp("conn %d Rx suspended!\n", conn
->id
);
861 *status
= ISCSI_TCP_SUSPENDED
;
866 segment
->total_copied
= segment
->total_size
;
870 skb_prepare_seq_read(skb
, offset
, skb
->len
, &seq
);
875 avail
= skb_seq_read(consumed
, &ptr
, &seq
);
877 debug_tcp("no more data avail. Consumed %d\n",
879 *status
= ISCSI_TCP_SKB_DONE
;
880 skb_abort_seq_read(&seq
);
883 BUG_ON(segment
->copied
>= segment
->size
);
885 debug_tcp("skb %p ptr=%p avail=%u\n", skb
, ptr
, avail
);
886 rc
= iscsi_tcp_segment_recv(tcp_conn
, segment
, ptr
, avail
);
890 if (segment
->total_copied
>= segment
->total_size
) {
891 skb_abort_seq_read(&seq
);
897 *status
= ISCSI_TCP_SEGMENT_DONE
;
898 debug_tcp("segment done\n");
899 rc
= segment
->done(tcp_conn
, segment
);
901 *status
= ISCSI_TCP_CONN_ERR
;
902 debug_tcp("Error receiving PDU, errno=%d\n", rc
);
903 iscsi_conn_failure(conn
, rc
);
906 /* The done() functions sets up the next segment. */
909 conn
->rxdata_octets
+= consumed
;
912 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb
);
915 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
916 * @conn: iscsi connection
917 * @task: scsi command task
920 int iscsi_tcp_task_init(struct iscsi_task
*task
)
922 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
923 struct iscsi_conn
*conn
= task
->conn
;
924 struct scsi_cmnd
*sc
= task
->sc
;
929 * mgmt tasks do not have a scatterlist since they come
930 * in from the iscsi interface.
932 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn
->id
,
935 return conn
->session
->tt
->init_pdu(task
, 0, task
->data_count
);
938 BUG_ON(__kfifo_len(tcp_task
->r2tqueue
));
939 tcp_task
->exp_datasn
= 0;
941 /* Prepare PDU, optionally w/ immediate data */
942 debug_scsi("task deq [cid %d itt 0x%x imm %d unsol %d]\n",
943 conn
->id
, task
->itt
, task
->imm_count
,
944 task
->unsol_r2t
.data_length
);
946 err
= conn
->session
->tt
->init_pdu(task
, 0, task
->imm_count
);
952 EXPORT_SYMBOL_GPL(iscsi_tcp_task_init
);
954 static struct iscsi_r2t_info
*iscsi_tcp_get_curr_r2t(struct iscsi_task
*task
)
956 struct iscsi_session
*session
= task
->conn
->session
;
957 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
958 struct iscsi_r2t_info
*r2t
= NULL
;
960 if (iscsi_task_has_unsol_data(task
))
961 r2t
= &task
->unsol_r2t
;
963 spin_lock_bh(&session
->lock
);
966 /* Continue with this R2T? */
967 if (r2t
->data_length
<= r2t
->sent
) {
968 debug_scsi(" done with r2t %p\n", r2t
);
969 __kfifo_put(tcp_task
->r2tpool
.queue
,
970 (void *)&tcp_task
->r2t
,
972 tcp_task
->r2t
= r2t
= NULL
;
977 __kfifo_get(tcp_task
->r2tqueue
,
978 (void *)&tcp_task
->r2t
, sizeof(void *));
981 spin_unlock_bh(&session
->lock
);
988 * iscsi_tcp_task_xmit - xmit normal PDU task
989 * @task: iscsi command task
991 * We're expected to return 0 when everything was transmitted succesfully,
992 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
995 int iscsi_tcp_task_xmit(struct iscsi_task
*task
)
997 struct iscsi_conn
*conn
= task
->conn
;
998 struct iscsi_session
*session
= conn
->session
;
999 struct iscsi_r2t_info
*r2t
;
1003 /* Flush any pending data first. */
1004 rc
= session
->tt
->xmit_pdu(task
);
1010 if (task
->hdr
->itt
== RESERVED_ITT
)
1011 iscsi_put_task(task
);
1015 /* Are we done already? */
1016 if (task
->sc
->sc_data_direction
!= DMA_TO_DEVICE
)
1019 r2t
= iscsi_tcp_get_curr_r2t(task
);
1021 /* Waiting for more R2Ts to arrive. */
1022 debug_tcp("no R2Ts yet\n");
1026 rc
= conn
->session
->tt
->alloc_pdu(task
, ISCSI_OP_SCSI_DATA_OUT
);
1029 iscsi_prep_data_out_pdu(task
, r2t
, (struct iscsi_data
*) task
->hdr
);
1031 debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1032 r2t
, r2t
->datasn
- 1, task
->hdr
->itt
,
1033 r2t
->data_offset
+ r2t
->sent
, r2t
->data_count
);
1035 rc
= conn
->session
->tt
->init_pdu(task
, r2t
->data_offset
+ r2t
->sent
,
1039 r2t
->sent
+= r2t
->data_count
;
1042 EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit
);
1044 struct iscsi_cls_conn
*
1045 iscsi_tcp_conn_setup(struct iscsi_cls_session
*cls_session
, int dd_data_size
,
1049 struct iscsi_conn
*conn
;
1050 struct iscsi_cls_conn
*cls_conn
;
1051 struct iscsi_tcp_conn
*tcp_conn
;
1053 cls_conn
= iscsi_conn_setup(cls_session
, sizeof(*tcp_conn
), conn_idx
);
1056 conn
= cls_conn
->dd_data
;
1058 * due to strange issues with iser these are not set
1059 * in iscsi_conn_setup
1061 conn
->max_recv_dlength
= ISCSI_DEF_MAX_RECV_SEG_LEN
;
1063 tcp_conn
= conn
->dd_data
;
1064 tcp_conn
->iscsi_conn
= conn
;
1066 tcp_conn
->dd_data
= kzalloc(dd_data_size
, GFP_KERNEL
);
1067 if (!tcp_conn
->dd_data
) {
1068 iscsi_conn_teardown(cls_conn
);
1073 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup
);
1075 void iscsi_tcp_conn_teardown(struct iscsi_cls_conn
*cls_conn
)
1077 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1078 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
1080 kfree(tcp_conn
->dd_data
);
1081 iscsi_conn_teardown(cls_conn
);
1083 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown
);
1085 int iscsi_tcp_r2tpool_alloc(struct iscsi_session
*session
)
1091 * initialize per-task: R2T pool and xmit queue
1093 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
1094 struct iscsi_task
*task
= session
->cmds
[cmd_i
];
1095 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
1098 * pre-allocated x2 as much r2ts to handle race when
1099 * target acks DataOut faster than we data_xmit() queues
1100 * could replenish r2tqueue.
1104 if (iscsi_pool_init(&tcp_task
->r2tpool
,
1105 session
->max_r2t
* 2, NULL
,
1106 sizeof(struct iscsi_r2t_info
))) {
1107 goto r2t_alloc_fail
;
1110 /* R2T xmit queue */
1111 tcp_task
->r2tqueue
= kfifo_alloc(
1112 session
->max_r2t
* 4 * sizeof(void*), GFP_KERNEL
, NULL
);
1113 if (tcp_task
->r2tqueue
== ERR_PTR(-ENOMEM
)) {
1114 iscsi_pool_free(&tcp_task
->r2tpool
);
1115 goto r2t_alloc_fail
;
1122 for (i
= 0; i
< cmd_i
; i
++) {
1123 struct iscsi_task
*task
= session
->cmds
[i
];
1124 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
1126 kfifo_free(tcp_task
->r2tqueue
);
1127 iscsi_pool_free(&tcp_task
->r2tpool
);
1131 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc
);
1133 void iscsi_tcp_r2tpool_free(struct iscsi_session
*session
)
1137 for (i
= 0; i
< session
->cmds_max
; i
++) {
1138 struct iscsi_task
*task
= session
->cmds
[i
];
1139 struct iscsi_tcp_task
*tcp_task
= task
->dd_data
;
1141 kfifo_free(tcp_task
->r2tqueue
);
1142 iscsi_pool_free(&tcp_task
->r2tpool
);
1145 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free
);
1147 void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn
*cls_conn
,
1148 struct iscsi_stats
*stats
)
1150 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1152 stats
->txdata_octets
= conn
->txdata_octets
;
1153 stats
->rxdata_octets
= conn
->rxdata_octets
;
1154 stats
->scsicmd_pdus
= conn
->scsicmd_pdus_cnt
;
1155 stats
->dataout_pdus
= conn
->dataout_pdus_cnt
;
1156 stats
->scsirsp_pdus
= conn
->scsirsp_pdus_cnt
;
1157 stats
->datain_pdus
= conn
->datain_pdus_cnt
;
1158 stats
->r2t_pdus
= conn
->r2t_pdus_cnt
;
1159 stats
->tmfcmd_pdus
= conn
->tmfcmd_pdus_cnt
;
1160 stats
->tmfrsp_pdus
= conn
->tmfrsp_pdus_cnt
;
1162 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats
);