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 * @segment: iscsi segment to check
163 * @recv: set to one of this is called from the recv path
164 * @copied: number of bytes copied
166 * Check if we're done receiving this segment. If the receive
167 * buffer is full but we expect more data, move on to the
168 * next entry in the scatterlist.
170 * If the amount of data we received isn't a multiple of 4,
171 * we will transparently receive the pad bytes, too.
173 * This function must be re-entrant.
175 int iscsi_tcp_segment_done(struct iscsi_segment *segment, int recv,
178 static unsigned char padbuf[ISCSI_PAD_LEN];
179 struct scatterlist sg;
182 debug_tcp("copied %u %u size %u %s\n", segment->copied, copied,
183 segment->size, recv ? "recv" : "xmit");
184 if (segment->hash && copied) {
186 * If a segment is kmapd we must unmap it before sending
187 * to the crypto layer since that will try to kmap it again.
189 iscsi_tcp_segment_unmap(segment);
191 if (!segment->data) {
192 sg_init_table(&sg, 1);
193 sg_set_page(&sg, sg_page(segment->sg), copied,
194 segment->copied + segment->sg_offset +
195 segment->sg->offset);
197 sg_init_one(&sg, segment->data + segment->copied,
199 crypto_hash_update(segment->hash, &sg, copied);
202 segment->copied += copied;
203 if (segment->copied < segment->size) {
204 iscsi_tcp_segment_map(segment, recv);
208 segment->total_copied += segment->copied;
212 /* Unmap the current scatterlist page, if there is one. */
213 iscsi_tcp_segment_unmap(segment);
215 /* Do we have more scatterlist entries? */
216 debug_tcp("total copied %u total size %u\n", segment->total_copied,
217 segment->total_size);
218 if (segment->total_copied < segment->total_size) {
219 /* Proceed to the next entry in the scatterlist. */
220 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
222 iscsi_tcp_segment_map(segment, recv);
223 BUG_ON(segment->size == 0);
227 /* Do we need to handle padding? */
228 pad = iscsi_padding(segment->total_copied);
230 debug_tcp("consume %d pad bytes\n", pad);
231 segment->total_size += pad;
233 segment->data = padbuf;
238 * Set us up for transferring the data digest. hdr digest
239 * is completely handled in hdr done function.
242 crypto_hash_final(segment->hash, segment->digest);
243 iscsi_tcp_segment_splice_digest(segment,
244 recv ? segment->recv_digest : segment->digest);
250 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
253 * iscsi_tcp_segment_recv - copy data to segment
254 * @tcp_conn: the iSCSI TCP connection
255 * @segment: the buffer to copy to
257 * @len: amount of data available
259 * This function copies up to @len bytes to the
260 * given buffer, and returns the number of bytes
261 * consumed, which can actually be less than @len.
263 * If hash digest is enabled, the function will update the
264 * hash while copying.
265 * Combining these two operations doesn't buy us a lot (yet),
266 * but in the future we could implement combined copy+crc,
267 * just way we do for network layer checksums.
270 iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
271 struct iscsi_segment *segment, const void *ptr,
274 unsigned int copy = 0, copied = 0;
276 while (!iscsi_tcp_segment_done(segment, 1, copy)) {
278 debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n",
283 copy = min(len - copied, segment->size - segment->copied);
284 debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy);
285 memcpy(segment->data + segment->copied, ptr + copied, copy);
292 iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
293 unsigned char digest[ISCSI_DIGEST_SIZE])
295 struct scatterlist sg;
297 sg_init_one(&sg, hdr, hdrlen);
298 crypto_hash_digest(hash, &sg, hdrlen, digest);
300 EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
303 iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
304 struct iscsi_segment *segment)
306 if (!segment->digest_len)
309 if (memcmp(segment->recv_digest, segment->digest,
310 segment->digest_len)) {
311 debug_scsi("digest mismatch\n");
319 * Helper function to set up segment buffer
322 __iscsi_segment_init(struct iscsi_segment *segment, size_t size,
323 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
325 memset(segment, 0, sizeof(*segment));
326 segment->total_size = size;
327 segment->done = done;
330 segment->hash = hash;
331 crypto_hash_init(hash);
336 iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
337 size_t size, iscsi_segment_done_fn_t *done,
338 struct hash_desc *hash)
340 __iscsi_segment_init(segment, size, done, hash);
341 segment->data = data;
342 segment->size = size;
344 EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
347 iscsi_segment_seek_sg(struct iscsi_segment *segment,
348 struct scatterlist *sg_list, unsigned int sg_count,
349 unsigned int offset, size_t size,
350 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
352 struct scatterlist *sg;
355 debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n",
357 __iscsi_segment_init(segment, size, done, hash);
358 for_each_sg(sg_list, sg, sg_count, i) {
359 debug_scsi("sg %d, len %u offset %u\n", i, sg->length,
361 if (offset < sg->length) {
362 iscsi_tcp_segment_init_sg(segment, sg, offset);
365 offset -= sg->length;
368 return ISCSI_ERR_DATA_OFFSET;
370 EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
373 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
374 * @tcp_conn: iscsi connection to prep for
376 * This function always passes NULL for the hash argument, because when this
377 * function is called we do not yet know the final size of the header and want
378 * to delay the digest processing until we know that.
380 void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
382 debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn,
383 tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : "");
384 iscsi_segment_init_linear(&tcp_conn->in.segment,
385 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
386 iscsi_tcp_hdr_recv_done, NULL);
388 EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
391 * Handle incoming reply to any other type of command
394 iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
395 struct iscsi_segment *segment)
397 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
400 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
401 return ISCSI_ERR_DATA_DGST;
403 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
404 conn->data, tcp_conn->in.datalen);
408 iscsi_tcp_hdr_recv_prep(tcp_conn);
413 iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
415 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
416 struct hash_desc *rx_hash = NULL;
418 if (conn->datadgst_en &
419 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
420 rx_hash = tcp_conn->rx_hash;
422 iscsi_segment_init_linear(&tcp_conn->in.segment,
423 conn->data, tcp_conn->in.datalen,
424 iscsi_tcp_data_recv_done, rx_hash);
428 * iscsi_tcp_cleanup_task - free tcp_task resources
431 * must be called with session lock
433 void iscsi_tcp_cleanup_task(struct iscsi_task *task)
435 struct iscsi_tcp_task *tcp_task = task->dd_data;
436 struct iscsi_r2t_info *r2t;
438 /* nothing to do for mgmt or pending tasks */
439 if (!task->sc || task->state == ISCSI_TASK_PENDING)
442 /* flush task's r2t queues */
443 while (__kfifo_get(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
444 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
446 debug_scsi("iscsi_tcp_cleanup_task pending r2t dropped\n");
451 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
453 tcp_task->r2t = NULL;
456 EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
459 * iscsi_tcp_data_in - SCSI Data-In Response processing
460 * @conn: iscsi connection
461 * @task: scsi command task
463 static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
465 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
466 struct iscsi_tcp_task *tcp_task = task->dd_data;
467 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
468 int datasn = be32_to_cpu(rhdr->datasn);
469 unsigned total_in_length = scsi_in(task->sc)->length;
471 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
472 if (tcp_conn->in.datalen == 0)
475 if (tcp_task->exp_datasn != datasn) {
476 debug_tcp("%s: task->exp_datasn(%d) != rhdr->datasn(%d)\n",
477 __func__, tcp_task->exp_datasn, datasn);
478 return ISCSI_ERR_DATASN;
481 tcp_task->exp_datasn++;
483 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
484 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
485 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
486 __func__, tcp_task->data_offset,
487 tcp_conn->in.datalen, total_in_length);
488 return ISCSI_ERR_DATA_OFFSET;
491 conn->datain_pdus_cnt++;
496 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
497 * @conn: iscsi connection
498 * @task: scsi command task
500 static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
502 struct iscsi_session *session = conn->session;
503 struct iscsi_tcp_task *tcp_task = task->dd_data;
504 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
505 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
506 struct iscsi_r2t_info *r2t;
507 int r2tsn = be32_to_cpu(rhdr->r2tsn);
510 if (tcp_conn->in.datalen) {
511 iscsi_conn_printk(KERN_ERR, conn,
512 "invalid R2t with datalen %d\n",
513 tcp_conn->in.datalen);
514 return ISCSI_ERR_DATALEN;
517 if (tcp_task->exp_datasn != r2tsn){
518 debug_tcp("%s: task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
519 __func__, tcp_task->exp_datasn, r2tsn);
520 return ISCSI_ERR_R2TSN;
523 /* fill-in new R2T associated with the task */
524 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
526 if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
527 iscsi_conn_printk(KERN_INFO, conn,
528 "dropping R2T itt %d in recovery.\n",
533 rc = __kfifo_get(tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*));
535 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
536 "Target has sent more R2Ts than it "
537 "negotiated for or driver has has leaked.\n");
538 return ISCSI_ERR_PROTO;
541 r2t->exp_statsn = rhdr->statsn;
542 r2t->data_length = be32_to_cpu(rhdr->data_length);
543 if (r2t->data_length == 0) {
544 iscsi_conn_printk(KERN_ERR, conn,
545 "invalid R2T with zero data len\n");
546 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
548 return ISCSI_ERR_DATALEN;
551 if (r2t->data_length > session->max_burst)
552 debug_scsi("invalid R2T with data len %u and max burst %u."
553 "Attempting to execute request.\n",
554 r2t->data_length, session->max_burst);
556 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
557 if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) {
558 iscsi_conn_printk(KERN_ERR, conn,
559 "invalid R2T with data len %u at offset %u "
560 "and total length %d\n", r2t->data_length,
561 r2t->data_offset, scsi_out(task->sc)->length);
562 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
564 return ISCSI_ERR_DATALEN;
567 r2t->ttt = rhdr->ttt; /* no flip */
571 tcp_task->exp_datasn = r2tsn + 1;
572 __kfifo_put(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
573 conn->r2t_pdus_cnt++;
575 iscsi_requeue_task(task);
580 * Handle incoming reply to DataIn command
583 iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
584 struct iscsi_segment *segment)
586 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
587 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
590 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
591 return ISCSI_ERR_DATA_DGST;
593 /* check for non-exceptional status */
594 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
595 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
600 iscsi_tcp_hdr_recv_prep(tcp_conn);
605 * iscsi_tcp_hdr_dissect - process PDU header
606 * @conn: iSCSI connection
609 * This function analyzes the header of the PDU received,
610 * and performs several sanity checks. If the PDU is accompanied
611 * by data, the receive buffer is set up to copy the incoming data
612 * to the correct location.
615 iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
617 int rc = 0, opcode, ahslen;
618 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
619 struct iscsi_task *task;
621 /* verify PDU length */
622 tcp_conn->in.datalen = ntoh24(hdr->dlength);
623 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
624 iscsi_conn_printk(KERN_ERR, conn,
625 "iscsi_tcp: datalen %d > %d\n",
626 tcp_conn->in.datalen, conn->max_recv_dlength);
627 return ISCSI_ERR_DATALEN;
630 /* Additional header segments. So far, we don't
631 * process additional headers.
633 ahslen = hdr->hlength << 2;
635 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
636 /* verify itt (itt encoding: age+cid+itt) */
637 rc = iscsi_verify_itt(conn, hdr->itt);
641 debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
642 opcode, ahslen, tcp_conn->in.datalen);
645 case ISCSI_OP_SCSI_DATA_IN:
646 spin_lock(&conn->session->lock);
647 task = iscsi_itt_to_ctask(conn, hdr->itt);
649 rc = ISCSI_ERR_BAD_ITT;
651 rc = iscsi_tcp_data_in(conn, task);
653 spin_unlock(&conn->session->lock);
657 if (tcp_conn->in.datalen) {
658 struct iscsi_tcp_task *tcp_task = task->dd_data;
659 struct hash_desc *rx_hash = NULL;
660 struct scsi_data_buffer *sdb = scsi_in(task->sc);
663 * Setup copy of Data-In into the Scsi_Cmnd
665 * We set up the iscsi_segment to point to the next
666 * scatterlist entry to copy to. As we go along,
667 * we move on to the next scatterlist entry and
668 * update the digest per-entry.
670 if (conn->datadgst_en &&
671 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
672 rx_hash = tcp_conn->rx_hash;
674 debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
675 "datalen=%d)\n", tcp_conn,
676 tcp_task->data_offset,
677 tcp_conn->in.datalen);
678 rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
681 tcp_task->data_offset,
682 tcp_conn->in.datalen,
683 iscsi_tcp_process_data_in,
685 spin_unlock(&conn->session->lock);
688 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
689 spin_unlock(&conn->session->lock);
691 case ISCSI_OP_SCSI_CMD_RSP:
692 if (tcp_conn->in.datalen) {
693 iscsi_tcp_data_recv_prep(tcp_conn);
696 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
699 spin_lock(&conn->session->lock);
700 task = iscsi_itt_to_ctask(conn, hdr->itt);
702 rc = ISCSI_ERR_BAD_ITT;
704 rc = ISCSI_ERR_AHSLEN;
705 else if (task->sc->sc_data_direction == DMA_TO_DEVICE)
706 rc = iscsi_tcp_r2t_rsp(conn, task);
708 rc = ISCSI_ERR_PROTO;
709 spin_unlock(&conn->session->lock);
711 case ISCSI_OP_LOGIN_RSP:
712 case ISCSI_OP_TEXT_RSP:
713 case ISCSI_OP_REJECT:
714 case ISCSI_OP_ASYNC_EVENT:
716 * It is possible that we could get a PDU with a buffer larger
717 * than 8K, but there are no targets that currently do this.
718 * For now we fail until we find a vendor that needs it
720 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
721 iscsi_conn_printk(KERN_ERR, conn,
722 "iscsi_tcp: received buffer of "
723 "len %u but conn buffer is only %u "
725 tcp_conn->in.datalen,
726 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
727 rc = ISCSI_ERR_PROTO;
731 /* If there's data coming in with the response,
732 * receive it to the connection's buffer.
734 if (tcp_conn->in.datalen) {
735 iscsi_tcp_data_recv_prep(tcp_conn);
739 case ISCSI_OP_LOGOUT_RSP:
740 case ISCSI_OP_NOOP_IN:
741 case ISCSI_OP_SCSI_TMFUNC_RSP:
742 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
745 rc = ISCSI_ERR_BAD_OPCODE;
750 /* Anything that comes with data should have
751 * been handled above. */
752 if (tcp_conn->in.datalen)
753 return ISCSI_ERR_PROTO;
754 iscsi_tcp_hdr_recv_prep(tcp_conn);
761 * iscsi_tcp_hdr_recv_done - process PDU header
763 * This is the callback invoked when the PDU header has
764 * been received. If the header is followed by additional
765 * header segments, we go back for more data.
768 iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
769 struct iscsi_segment *segment)
771 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
772 struct iscsi_hdr *hdr;
774 /* Check if there are additional header segments
775 * *prior* to computing the digest, because we
776 * may need to go back to the caller for more.
778 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
779 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
780 /* Bump the header length - the caller will
781 * just loop around and get the AHS for us, and
783 unsigned int ahslen = hdr->hlength << 2;
785 /* Make sure we don't overflow */
786 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
787 return ISCSI_ERR_AHSLEN;
789 segment->total_size += ahslen;
790 segment->size += ahslen;
794 /* We're done processing the header. See if we're doing
795 * header digests; if so, set up the recv_digest buffer
796 * and go back for more. */
797 if (conn->hdrdgst_en) {
798 if (segment->digest_len == 0) {
800 * Even if we offload the digest processing we
801 * splice it in so we can increment the skb/segment
802 * counters in preparation for the data segment.
804 iscsi_tcp_segment_splice_digest(segment,
805 segment->recv_digest);
809 if (!(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
810 iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
811 segment->total_copied - ISCSI_DIGEST_SIZE,
814 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
815 return ISCSI_ERR_HDR_DGST;
819 tcp_conn->in.hdr = hdr;
820 return iscsi_tcp_hdr_dissect(conn, hdr);
824 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
825 * @tcp_conn: iscsi tcp conn
827 * returns non zero if we are currently processing or setup to process
830 inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
832 return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
834 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
837 * iscsi_tcp_recv_skb - Process skb
838 * @conn: iscsi connection
839 * @skb: network buffer with header and/or data segment
840 * @offset: offset in skb
841 * @offload: bool indicating if transfer was offloaded
843 * Will return status of transfer in status. And will return
844 * number of bytes copied.
846 int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
847 unsigned int offset, bool offloaded, int *status)
849 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
850 struct iscsi_segment *segment = &tcp_conn->in.segment;
851 struct skb_seq_state seq;
852 unsigned int consumed = 0;
855 debug_tcp("in %d bytes\n", skb->len - offset);
857 if (unlikely(conn->suspend_rx)) {
858 debug_tcp("conn %d Rx suspended!\n", conn->id);
859 *status = ISCSI_TCP_SUSPENDED;
864 segment->total_copied = segment->total_size;
868 skb_prepare_seq_read(skb, offset, skb->len, &seq);
873 avail = skb_seq_read(consumed, &ptr, &seq);
875 debug_tcp("no more data avail. Consumed %d\n",
877 *status = ISCSI_TCP_SKB_DONE;
878 skb_abort_seq_read(&seq);
881 BUG_ON(segment->copied >= segment->size);
883 debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
884 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
888 if (segment->total_copied >= segment->total_size) {
889 skb_abort_seq_read(&seq);
895 *status = ISCSI_TCP_SEGMENT_DONE;
896 debug_tcp("segment done\n");
897 rc = segment->done(tcp_conn, segment);
899 *status = ISCSI_TCP_CONN_ERR;
900 debug_tcp("Error receiving PDU, errno=%d\n", rc);
901 iscsi_conn_failure(conn, rc);
904 /* The done() functions sets up the next segment. */
907 conn->rxdata_octets += consumed;
910 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
913 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
914 * @conn: iscsi connection
915 * @task: scsi command task
918 int iscsi_tcp_task_init(struct iscsi_task *task)
920 struct iscsi_tcp_task *tcp_task = task->dd_data;
921 struct iscsi_conn *conn = task->conn;
922 struct scsi_cmnd *sc = task->sc;
927 * mgmt tasks do not have a scatterlist since they come
928 * in from the iscsi interface.
930 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id,
933 return conn->session->tt->init_pdu(task, 0, task->data_count);
936 BUG_ON(__kfifo_len(tcp_task->r2tqueue));
937 tcp_task->exp_datasn = 0;
939 /* Prepare PDU, optionally w/ immediate data */
940 debug_scsi("task deq [cid %d itt 0x%x imm %d unsol %d]\n",
941 conn->id, task->itt, task->imm_count,
942 task->unsol_r2t.data_length);
944 err = conn->session->tt->init_pdu(task, 0, task->imm_count);
950 EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
952 static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
954 struct iscsi_session *session = task->conn->session;
955 struct iscsi_tcp_task *tcp_task = task->dd_data;
956 struct iscsi_r2t_info *r2t = NULL;
958 if (iscsi_task_has_unsol_data(task))
959 r2t = &task->unsol_r2t;
961 spin_lock_bh(&session->lock);
964 /* Continue with this R2T? */
965 if (r2t->data_length <= r2t->sent) {
966 debug_scsi(" done with r2t %p\n", r2t);
967 __kfifo_put(tcp_task->r2tpool.queue,
968 (void *)&tcp_task->r2t,
970 tcp_task->r2t = r2t = NULL;
975 __kfifo_get(tcp_task->r2tqueue,
976 (void *)&tcp_task->r2t, sizeof(void *));
979 spin_unlock_bh(&session->lock);
986 * iscsi_tcp_task_xmit - xmit normal PDU task
987 * @task: iscsi command task
989 * We're expected to return 0 when everything was transmitted succesfully,
990 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
993 int iscsi_tcp_task_xmit(struct iscsi_task *task)
995 struct iscsi_conn *conn = task->conn;
996 struct iscsi_session *session = conn->session;
997 struct iscsi_r2t_info *r2t;
1001 /* Flush any pending data first. */
1002 rc = session->tt->xmit_pdu(task);
1008 if (task->hdr->itt == RESERVED_ITT)
1009 iscsi_put_task(task);
1013 /* Are we done already? */
1014 if (task->sc->sc_data_direction != DMA_TO_DEVICE)
1017 r2t = iscsi_tcp_get_curr_r2t(task);
1019 /* Waiting for more R2Ts to arrive. */
1020 debug_tcp("no R2Ts yet\n");
1024 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
1027 iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1029 debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1030 r2t, r2t->datasn - 1, task->hdr->itt,
1031 r2t->data_offset + r2t->sent, r2t->data_count);
1033 rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1037 r2t->sent += r2t->data_count;
1040 EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
1042 struct iscsi_cls_conn *
1043 iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
1047 struct iscsi_conn *conn;
1048 struct iscsi_cls_conn *cls_conn;
1049 struct iscsi_tcp_conn *tcp_conn;
1051 cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx);
1054 conn = cls_conn->dd_data;
1056 * due to strange issues with iser these are not set
1057 * in iscsi_conn_setup
1059 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1061 tcp_conn = conn->dd_data;
1062 tcp_conn->iscsi_conn = conn;
1064 tcp_conn->dd_data = kzalloc(dd_data_size, GFP_KERNEL);
1065 if (!tcp_conn->dd_data) {
1066 iscsi_conn_teardown(cls_conn);
1071 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
1073 void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
1075 struct iscsi_conn *conn = cls_conn->dd_data;
1076 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1078 kfree(tcp_conn->dd_data);
1079 iscsi_conn_teardown(cls_conn);
1081 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
1083 int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
1089 * initialize per-task: R2T pool and xmit queue
1091 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1092 struct iscsi_task *task = session->cmds[cmd_i];
1093 struct iscsi_tcp_task *tcp_task = task->dd_data;
1096 * pre-allocated x2 as much r2ts to handle race when
1097 * target acks DataOut faster than we data_xmit() queues
1098 * could replenish r2tqueue.
1102 if (iscsi_pool_init(&tcp_task->r2tpool,
1103 session->max_r2t * 2, NULL,
1104 sizeof(struct iscsi_r2t_info))) {
1105 goto r2t_alloc_fail;
1108 /* R2T xmit queue */
1109 tcp_task->r2tqueue = kfifo_alloc(
1110 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
1111 if (tcp_task->r2tqueue == ERR_PTR(-ENOMEM)) {
1112 iscsi_pool_free(&tcp_task->r2tpool);
1113 goto r2t_alloc_fail;
1120 for (i = 0; i < cmd_i; i++) {
1121 struct iscsi_task *task = session->cmds[i];
1122 struct iscsi_tcp_task *tcp_task = task->dd_data;
1124 kfifo_free(tcp_task->r2tqueue);
1125 iscsi_pool_free(&tcp_task->r2tpool);
1129 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
1131 void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
1135 for (i = 0; i < session->cmds_max; i++) {
1136 struct iscsi_task *task = session->cmds[i];
1137 struct iscsi_tcp_task *tcp_task = task->dd_data;
1139 kfifo_free(tcp_task->r2tqueue);
1140 iscsi_pool_free(&tcp_task->r2tpool);
1143 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
1145 void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1146 struct iscsi_stats *stats)
1148 struct iscsi_conn *conn = cls_conn->dd_data;
1150 stats->txdata_octets = conn->txdata_octets;
1151 stats->rxdata_octets = conn->rxdata_octets;
1152 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1153 stats->dataout_pdus = conn->dataout_pdus_cnt;
1154 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1155 stats->datain_pdus = conn->datain_pdus_cnt;
1156 stats->r2t_pdus = conn->r2t_pdus_cnt;
1157 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1158 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1160 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);