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 static unsigned char padbuf[ISCSI_PAD_LEN];
181 struct scatterlist sg;
184 debug_tcp("copied %u %u size %u %s\n", segment->copied, copied,
185 segment->size, recv ? "recv" : "xmit");
186 if (segment->hash && copied) {
188 * If a segment is kmapd we must unmap it before sending
189 * to the crypto layer since that will try to kmap it again.
191 iscsi_tcp_segment_unmap(segment);
193 if (!segment->data) {
194 sg_init_table(&sg, 1);
195 sg_set_page(&sg, sg_page(segment->sg), copied,
196 segment->copied + segment->sg_offset +
197 segment->sg->offset);
199 sg_init_one(&sg, segment->data + segment->copied,
201 crypto_hash_update(segment->hash, &sg, copied);
204 segment->copied += copied;
205 if (segment->copied < segment->size) {
206 iscsi_tcp_segment_map(segment, recv);
210 segment->total_copied += segment->copied;
214 /* Unmap the current scatterlist page, if there is one. */
215 iscsi_tcp_segment_unmap(segment);
217 /* Do we have more scatterlist entries? */
218 debug_tcp("total copied %u total size %u\n", segment->total_copied,
219 segment->total_size);
220 if (segment->total_copied < segment->total_size) {
221 /* Proceed to the next entry in the scatterlist. */
222 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
224 iscsi_tcp_segment_map(segment, recv);
225 BUG_ON(segment->size == 0);
229 /* Do we need to handle padding? */
230 if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) {
231 pad = iscsi_padding(segment->total_copied);
233 debug_tcp("consume %d pad bytes\n", pad);
234 segment->total_size += pad;
236 segment->data = padbuf;
242 * Set us up for transferring the data digest. hdr digest
243 * is completely handled in hdr done function.
246 crypto_hash_final(segment->hash, segment->digest);
247 iscsi_tcp_segment_splice_digest(segment,
248 recv ? segment->recv_digest : segment->digest);
254 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
257 * iscsi_tcp_segment_recv - copy data to segment
258 * @tcp_conn: the iSCSI TCP connection
259 * @segment: the buffer to copy to
261 * @len: amount of data available
263 * This function copies up to @len bytes to the
264 * given buffer, and returns the number of bytes
265 * consumed, which can actually be less than @len.
267 * If hash digest is enabled, the function will update the
268 * hash while copying.
269 * Combining these two operations doesn't buy us a lot (yet),
270 * but in the future we could implement combined copy+crc,
271 * just way we do for network layer checksums.
274 iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
275 struct iscsi_segment *segment, const void *ptr,
278 unsigned int copy = 0, copied = 0;
280 while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) {
282 debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n",
287 copy = min(len - copied, segment->size - segment->copied);
288 debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy);
289 memcpy(segment->data + segment->copied, ptr + copied, copy);
296 iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
297 unsigned char digest[ISCSI_DIGEST_SIZE])
299 struct scatterlist sg;
301 sg_init_one(&sg, hdr, hdrlen);
302 crypto_hash_digest(hash, &sg, hdrlen, digest);
304 EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
307 iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
308 struct iscsi_segment *segment)
310 if (!segment->digest_len)
313 if (memcmp(segment->recv_digest, segment->digest,
314 segment->digest_len)) {
315 debug_scsi("digest mismatch\n");
323 * Helper function to set up segment buffer
326 __iscsi_segment_init(struct iscsi_segment *segment, size_t size,
327 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
329 memset(segment, 0, sizeof(*segment));
330 segment->total_size = size;
331 segment->done = done;
334 segment->hash = hash;
335 crypto_hash_init(hash);
340 iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
341 size_t size, iscsi_segment_done_fn_t *done,
342 struct hash_desc *hash)
344 __iscsi_segment_init(segment, size, done, hash);
345 segment->data = data;
346 segment->size = size;
348 EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
351 iscsi_segment_seek_sg(struct iscsi_segment *segment,
352 struct scatterlist *sg_list, unsigned int sg_count,
353 unsigned int offset, size_t size,
354 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
356 struct scatterlist *sg;
359 debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n",
361 __iscsi_segment_init(segment, size, done, hash);
362 for_each_sg(sg_list, sg, sg_count, i) {
363 debug_scsi("sg %d, len %u offset %u\n", i, sg->length,
365 if (offset < sg->length) {
366 iscsi_tcp_segment_init_sg(segment, sg, offset);
369 offset -= sg->length;
372 return ISCSI_ERR_DATA_OFFSET;
374 EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
377 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
378 * @tcp_conn: iscsi connection to prep for
380 * This function always passes NULL for the hash argument, because when this
381 * function is called we do not yet know the final size of the header and want
382 * to delay the digest processing until we know that.
384 void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
386 debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn,
387 tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : "");
388 iscsi_segment_init_linear(&tcp_conn->in.segment,
389 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
390 iscsi_tcp_hdr_recv_done, NULL);
392 EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
395 * Handle incoming reply to any other type of command
398 iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
399 struct iscsi_segment *segment)
401 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
404 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
405 return ISCSI_ERR_DATA_DGST;
407 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
408 conn->data, tcp_conn->in.datalen);
412 iscsi_tcp_hdr_recv_prep(tcp_conn);
417 iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
419 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
420 struct hash_desc *rx_hash = NULL;
422 if (conn->datadgst_en &
423 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
424 rx_hash = tcp_conn->rx_hash;
426 iscsi_segment_init_linear(&tcp_conn->in.segment,
427 conn->data, tcp_conn->in.datalen,
428 iscsi_tcp_data_recv_done, rx_hash);
432 * iscsi_tcp_cleanup_task - free tcp_task resources
435 * must be called with session lock
437 void iscsi_tcp_cleanup_task(struct iscsi_task *task)
439 struct iscsi_tcp_task *tcp_task = task->dd_data;
440 struct iscsi_r2t_info *r2t;
442 /* nothing to do for mgmt or pending tasks */
443 if (!task->sc || task->state == ISCSI_TASK_PENDING)
446 /* flush task's r2t queues */
447 while (__kfifo_get(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
448 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
450 debug_scsi("iscsi_tcp_cleanup_task pending r2t dropped\n");
455 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
457 tcp_task->r2t = NULL;
460 EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
463 * iscsi_tcp_data_in - SCSI Data-In Response processing
464 * @conn: iscsi connection
465 * @task: scsi command task
467 static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
469 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
470 struct iscsi_tcp_task *tcp_task = task->dd_data;
471 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
472 int datasn = be32_to_cpu(rhdr->datasn);
473 unsigned total_in_length = scsi_in(task->sc)->length;
475 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
476 if (tcp_conn->in.datalen == 0)
479 if (tcp_task->exp_datasn != datasn) {
480 debug_tcp("%s: task->exp_datasn(%d) != rhdr->datasn(%d)\n",
481 __func__, tcp_task->exp_datasn, datasn);
482 return ISCSI_ERR_DATASN;
485 tcp_task->exp_datasn++;
487 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
488 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
489 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
490 __func__, tcp_task->data_offset,
491 tcp_conn->in.datalen, total_in_length);
492 return ISCSI_ERR_DATA_OFFSET;
495 conn->datain_pdus_cnt++;
500 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
501 * @conn: iscsi connection
502 * @task: scsi command task
504 static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
506 struct iscsi_session *session = conn->session;
507 struct iscsi_tcp_task *tcp_task = task->dd_data;
508 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
509 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
510 struct iscsi_r2t_info *r2t;
511 int r2tsn = be32_to_cpu(rhdr->r2tsn);
514 if (tcp_conn->in.datalen) {
515 iscsi_conn_printk(KERN_ERR, conn,
516 "invalid R2t with datalen %d\n",
517 tcp_conn->in.datalen);
518 return ISCSI_ERR_DATALEN;
521 if (tcp_task->exp_datasn != r2tsn){
522 debug_tcp("%s: task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
523 __func__, tcp_task->exp_datasn, r2tsn);
524 return ISCSI_ERR_R2TSN;
527 /* fill-in new R2T associated with the task */
528 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
530 if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
531 iscsi_conn_printk(KERN_INFO, conn,
532 "dropping R2T itt %d in recovery.\n",
537 rc = __kfifo_get(tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*));
539 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
540 "Target has sent more R2Ts than it "
541 "negotiated for or driver has has leaked.\n");
542 return ISCSI_ERR_PROTO;
545 r2t->exp_statsn = rhdr->statsn;
546 r2t->data_length = be32_to_cpu(rhdr->data_length);
547 if (r2t->data_length == 0) {
548 iscsi_conn_printk(KERN_ERR, conn,
549 "invalid R2T with zero data len\n");
550 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
552 return ISCSI_ERR_DATALEN;
555 if (r2t->data_length > session->max_burst)
556 debug_scsi("invalid R2T with data len %u and max burst %u."
557 "Attempting to execute request.\n",
558 r2t->data_length, session->max_burst);
560 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
561 if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) {
562 iscsi_conn_printk(KERN_ERR, conn,
563 "invalid R2T with data len %u at offset %u "
564 "and total length %d\n", r2t->data_length,
565 r2t->data_offset, scsi_out(task->sc)->length);
566 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
568 return ISCSI_ERR_DATALEN;
571 r2t->ttt = rhdr->ttt; /* no flip */
575 tcp_task->exp_datasn = r2tsn + 1;
576 __kfifo_put(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
577 conn->r2t_pdus_cnt++;
579 iscsi_requeue_task(task);
584 * Handle incoming reply to DataIn command
587 iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
588 struct iscsi_segment *segment)
590 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
591 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
594 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
595 return ISCSI_ERR_DATA_DGST;
597 /* check for non-exceptional status */
598 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
599 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
604 iscsi_tcp_hdr_recv_prep(tcp_conn);
609 * iscsi_tcp_hdr_dissect - process PDU header
610 * @conn: iSCSI connection
613 * This function analyzes the header of the PDU received,
614 * and performs several sanity checks. If the PDU is accompanied
615 * by data, the receive buffer is set up to copy the incoming data
616 * to the correct location.
619 iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
621 int rc = 0, opcode, ahslen;
622 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
623 struct iscsi_task *task;
625 /* verify PDU length */
626 tcp_conn->in.datalen = ntoh24(hdr->dlength);
627 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
628 iscsi_conn_printk(KERN_ERR, conn,
629 "iscsi_tcp: datalen %d > %d\n",
630 tcp_conn->in.datalen, conn->max_recv_dlength);
631 return ISCSI_ERR_DATALEN;
634 /* Additional header segments. So far, we don't
635 * process additional headers.
637 ahslen = hdr->hlength << 2;
639 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
640 /* verify itt (itt encoding: age+cid+itt) */
641 rc = iscsi_verify_itt(conn, hdr->itt);
645 debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
646 opcode, ahslen, tcp_conn->in.datalen);
649 case ISCSI_OP_SCSI_DATA_IN:
650 spin_lock(&conn->session->lock);
651 task = iscsi_itt_to_ctask(conn, hdr->itt);
653 rc = ISCSI_ERR_BAD_ITT;
655 rc = iscsi_tcp_data_in(conn, task);
657 spin_unlock(&conn->session->lock);
661 if (tcp_conn->in.datalen) {
662 struct iscsi_tcp_task *tcp_task = task->dd_data;
663 struct hash_desc *rx_hash = NULL;
664 struct scsi_data_buffer *sdb = scsi_in(task->sc);
667 * Setup copy of Data-In into the Scsi_Cmnd
669 * We set up the iscsi_segment to point to the next
670 * scatterlist entry to copy to. As we go along,
671 * we move on to the next scatterlist entry and
672 * update the digest per-entry.
674 if (conn->datadgst_en &&
675 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
676 rx_hash = tcp_conn->rx_hash;
678 debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
679 "datalen=%d)\n", tcp_conn,
680 tcp_task->data_offset,
681 tcp_conn->in.datalen);
682 rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
685 tcp_task->data_offset,
686 tcp_conn->in.datalen,
687 iscsi_tcp_process_data_in,
689 spin_unlock(&conn->session->lock);
692 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
693 spin_unlock(&conn->session->lock);
695 case ISCSI_OP_SCSI_CMD_RSP:
696 if (tcp_conn->in.datalen) {
697 iscsi_tcp_data_recv_prep(tcp_conn);
700 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
703 spin_lock(&conn->session->lock);
704 task = iscsi_itt_to_ctask(conn, hdr->itt);
706 rc = ISCSI_ERR_BAD_ITT;
708 rc = ISCSI_ERR_AHSLEN;
709 else if (task->sc->sc_data_direction == DMA_TO_DEVICE)
710 rc = iscsi_tcp_r2t_rsp(conn, task);
712 rc = ISCSI_ERR_PROTO;
713 spin_unlock(&conn->session->lock);
715 case ISCSI_OP_LOGIN_RSP:
716 case ISCSI_OP_TEXT_RSP:
717 case ISCSI_OP_REJECT:
718 case ISCSI_OP_ASYNC_EVENT:
720 * It is possible that we could get a PDU with a buffer larger
721 * than 8K, but there are no targets that currently do this.
722 * For now we fail until we find a vendor that needs it
724 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
725 iscsi_conn_printk(KERN_ERR, conn,
726 "iscsi_tcp: received buffer of "
727 "len %u but conn buffer is only %u "
729 tcp_conn->in.datalen,
730 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
731 rc = ISCSI_ERR_PROTO;
735 /* If there's data coming in with the response,
736 * receive it to the connection's buffer.
738 if (tcp_conn->in.datalen) {
739 iscsi_tcp_data_recv_prep(tcp_conn);
743 case ISCSI_OP_LOGOUT_RSP:
744 case ISCSI_OP_NOOP_IN:
745 case ISCSI_OP_SCSI_TMFUNC_RSP:
746 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
749 rc = ISCSI_ERR_BAD_OPCODE;
754 /* Anything that comes with data should have
755 * been handled above. */
756 if (tcp_conn->in.datalen)
757 return ISCSI_ERR_PROTO;
758 iscsi_tcp_hdr_recv_prep(tcp_conn);
765 * iscsi_tcp_hdr_recv_done - process PDU header
767 * This is the callback invoked when the PDU header has
768 * been received. If the header is followed by additional
769 * header segments, we go back for more data.
772 iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
773 struct iscsi_segment *segment)
775 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
776 struct iscsi_hdr *hdr;
778 /* Check if there are additional header segments
779 * *prior* to computing the digest, because we
780 * may need to go back to the caller for more.
782 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
783 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
784 /* Bump the header length - the caller will
785 * just loop around and get the AHS for us, and
787 unsigned int ahslen = hdr->hlength << 2;
789 /* Make sure we don't overflow */
790 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
791 return ISCSI_ERR_AHSLEN;
793 segment->total_size += ahslen;
794 segment->size += ahslen;
798 /* We're done processing the header. See if we're doing
799 * header digests; if so, set up the recv_digest buffer
800 * and go back for more. */
801 if (conn->hdrdgst_en &&
802 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
803 if (segment->digest_len == 0) {
805 * Even if we offload the digest processing we
806 * splice it in so we can increment the skb/segment
807 * counters in preparation for the data segment.
809 iscsi_tcp_segment_splice_digest(segment,
810 segment->recv_digest);
814 iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
815 segment->total_copied - ISCSI_DIGEST_SIZE,
818 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
819 return ISCSI_ERR_HDR_DGST;
822 tcp_conn->in.hdr = hdr;
823 return iscsi_tcp_hdr_dissect(conn, hdr);
827 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
828 * @tcp_conn: iscsi tcp conn
830 * returns non zero if we are currently processing or setup to process
833 inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
835 return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
837 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
840 * iscsi_tcp_recv_skb - Process skb
841 * @conn: iscsi connection
842 * @skb: network buffer with header and/or data segment
843 * @offset: offset in skb
844 * @offload: bool indicating if transfer was offloaded
846 * Will return status of transfer in status. And will return
847 * number of bytes copied.
849 int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
850 unsigned int offset, bool offloaded, int *status)
852 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
853 struct iscsi_segment *segment = &tcp_conn->in.segment;
854 struct skb_seq_state seq;
855 unsigned int consumed = 0;
858 debug_tcp("in %d bytes\n", skb->len - offset);
860 if (unlikely(conn->suspend_rx)) {
861 debug_tcp("conn %d Rx suspended!\n", conn->id);
862 *status = ISCSI_TCP_SUSPENDED;
867 segment->total_copied = segment->total_size;
871 skb_prepare_seq_read(skb, offset, skb->len, &seq);
876 avail = skb_seq_read(consumed, &ptr, &seq);
878 debug_tcp("no more data avail. Consumed %d\n",
880 *status = ISCSI_TCP_SKB_DONE;
881 skb_abort_seq_read(&seq);
884 BUG_ON(segment->copied >= segment->size);
886 debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
887 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
891 if (segment->total_copied >= segment->total_size) {
892 skb_abort_seq_read(&seq);
898 *status = ISCSI_TCP_SEGMENT_DONE;
899 debug_tcp("segment done\n");
900 rc = segment->done(tcp_conn, segment);
902 *status = ISCSI_TCP_CONN_ERR;
903 debug_tcp("Error receiving PDU, errno=%d\n", rc);
904 iscsi_conn_failure(conn, rc);
907 /* The done() functions sets up the next segment. */
910 conn->rxdata_octets += consumed;
913 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
916 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
917 * @conn: iscsi connection
918 * @task: scsi command task
921 int iscsi_tcp_task_init(struct iscsi_task *task)
923 struct iscsi_tcp_task *tcp_task = task->dd_data;
924 struct iscsi_conn *conn = task->conn;
925 struct scsi_cmnd *sc = task->sc;
930 * mgmt tasks do not have a scatterlist since they come
931 * in from the iscsi interface.
933 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id,
936 return conn->session->tt->init_pdu(task, 0, task->data_count);
939 BUG_ON(__kfifo_len(tcp_task->r2tqueue));
940 tcp_task->exp_datasn = 0;
942 /* Prepare PDU, optionally w/ immediate data */
943 debug_scsi("task deq [cid %d itt 0x%x imm %d unsol %d]\n",
944 conn->id, task->itt, task->imm_count,
945 task->unsol_r2t.data_length);
947 err = conn->session->tt->init_pdu(task, 0, task->imm_count);
953 EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
955 static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
957 struct iscsi_session *session = task->conn->session;
958 struct iscsi_tcp_task *tcp_task = task->dd_data;
959 struct iscsi_r2t_info *r2t = NULL;
961 if (iscsi_task_has_unsol_data(task))
962 r2t = &task->unsol_r2t;
964 spin_lock_bh(&session->lock);
967 /* Continue with this R2T? */
968 if (r2t->data_length <= r2t->sent) {
969 debug_scsi(" done with r2t %p\n", r2t);
970 __kfifo_put(tcp_task->r2tpool.queue,
971 (void *)&tcp_task->r2t,
973 tcp_task->r2t = r2t = NULL;
978 __kfifo_get(tcp_task->r2tqueue,
979 (void *)&tcp_task->r2t, sizeof(void *));
982 spin_unlock_bh(&session->lock);
989 * iscsi_tcp_task_xmit - xmit normal PDU task
990 * @task: iscsi command task
992 * We're expected to return 0 when everything was transmitted succesfully,
993 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
996 int iscsi_tcp_task_xmit(struct iscsi_task *task)
998 struct iscsi_conn *conn = task->conn;
999 struct iscsi_session *session = conn->session;
1000 struct iscsi_r2t_info *r2t;
1004 /* Flush any pending data first. */
1005 rc = session->tt->xmit_pdu(task);
1011 if (task->hdr->itt == RESERVED_ITT)
1012 iscsi_put_task(task);
1016 /* Are we done already? */
1017 if (task->sc->sc_data_direction != DMA_TO_DEVICE)
1020 r2t = iscsi_tcp_get_curr_r2t(task);
1022 /* Waiting for more R2Ts to arrive. */
1023 debug_tcp("no R2Ts yet\n");
1027 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
1030 iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1032 debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1033 r2t, r2t->datasn - 1, task->hdr->itt,
1034 r2t->data_offset + r2t->sent, r2t->data_count);
1036 rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1040 r2t->sent += r2t->data_count;
1043 EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
1045 struct iscsi_cls_conn *
1046 iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
1050 struct iscsi_conn *conn;
1051 struct iscsi_cls_conn *cls_conn;
1052 struct iscsi_tcp_conn *tcp_conn;
1054 cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx);
1057 conn = cls_conn->dd_data;
1059 * due to strange issues with iser these are not set
1060 * in iscsi_conn_setup
1062 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1064 tcp_conn = conn->dd_data;
1065 tcp_conn->iscsi_conn = conn;
1067 tcp_conn->dd_data = kzalloc(dd_data_size, GFP_KERNEL);
1068 if (!tcp_conn->dd_data) {
1069 iscsi_conn_teardown(cls_conn);
1074 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
1076 void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
1078 struct iscsi_conn *conn = cls_conn->dd_data;
1079 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1081 kfree(tcp_conn->dd_data);
1082 iscsi_conn_teardown(cls_conn);
1084 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
1086 int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
1092 * initialize per-task: R2T pool and xmit queue
1094 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1095 struct iscsi_task *task = session->cmds[cmd_i];
1096 struct iscsi_tcp_task *tcp_task = task->dd_data;
1099 * pre-allocated x2 as much r2ts to handle race when
1100 * target acks DataOut faster than we data_xmit() queues
1101 * could replenish r2tqueue.
1105 if (iscsi_pool_init(&tcp_task->r2tpool,
1106 session->max_r2t * 2, NULL,
1107 sizeof(struct iscsi_r2t_info))) {
1108 goto r2t_alloc_fail;
1111 /* R2T xmit queue */
1112 tcp_task->r2tqueue = kfifo_alloc(
1113 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
1114 if (tcp_task->r2tqueue == ERR_PTR(-ENOMEM)) {
1115 iscsi_pool_free(&tcp_task->r2tpool);
1116 goto r2t_alloc_fail;
1123 for (i = 0; i < cmd_i; i++) {
1124 struct iscsi_task *task = session->cmds[i];
1125 struct iscsi_tcp_task *tcp_task = task->dd_data;
1127 kfifo_free(tcp_task->r2tqueue);
1128 iscsi_pool_free(&tcp_task->r2tpool);
1132 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
1134 void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
1138 for (i = 0; i < session->cmds_max; i++) {
1139 struct iscsi_task *task = session->cmds[i];
1140 struct iscsi_tcp_task *tcp_task = task->dd_data;
1142 kfifo_free(tcp_task->r2tqueue);
1143 iscsi_pool_free(&tcp_task->r2tpool);
1146 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
1148 void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1149 struct iscsi_stats *stats)
1151 struct iscsi_conn *conn = cls_conn->dd_data;
1153 stats->txdata_octets = conn->txdata_octets;
1154 stats->rxdata_octets = conn->rxdata_octets;
1155 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1156 stats->dataout_pdus = conn->dataout_pdus_cnt;
1157 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1158 stats->datain_pdus = conn->datain_pdus_cnt;
1159 stats->r2t_pdus = conn->r2t_pdus_cnt;
1160 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1161 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1163 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);