2 * iSCSI Initiator over TCP/IP Data-Path
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("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
48 "Alex Aizman <itn780@yahoo.com>");
49 MODULE_DESCRIPTION("iSCSI/TCP data-path");
50 MODULE_LICENSE("GPL");
55 #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
57 #define debug_tcp(fmt...)
67 static unsigned int iscsi_max_lun = 512;
68 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
70 static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
71 struct iscsi_segment *segment);
74 * Scatterlist handling: inside the iscsi_segment, we
75 * remember an index into the scatterlist, and set data/size
76 * to the current scatterlist entry. For highmem pages, we
79 * Note that the page is unmapped when we return from
80 * TCP's data_ready handler, so we may end up mapping and
81 * unmapping the same page repeatedly. The whole reason
82 * for this is that we shouldn't keep the page mapped
83 * outside the softirq.
87 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
88 * @segment: the buffer object
90 * @offset: byte offset into that sg entry
92 * This function sets up the segment so that subsequent
93 * data is copied to the indicated sg entry, at the given
97 iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
98 struct scatterlist *sg, unsigned int offset)
101 segment->sg_offset = offset;
102 segment->size = min(sg->length - offset,
103 segment->total_size - segment->total_copied);
104 segment->data = NULL;
108 * iscsi_tcp_segment_map - map the current S/G page
109 * @segment: iscsi_segment
110 * @recv: 1 if called from recv path
112 * We only need to possibly kmap data if scatter lists are being used,
113 * because the iscsi passthrough and internal IO paths will never use high
117 iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
119 struct scatterlist *sg;
121 if (segment->data != NULL || !segment->sg)
125 BUG_ON(segment->sg_mapped);
126 BUG_ON(sg->length == 0);
129 * If the page count is greater than one it is ok to send
130 * to the network layer's zero copy send path. If not we
131 * have to go the slow sendmsg path. We always map for the
134 if (page_count(sg_page(sg)) >= 1 && !recv)
137 debug_tcp("iscsi_tcp_segment_map %s %p\n", recv ? "recv" : "xmit",
139 segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0);
140 segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
144 iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
146 debug_tcp("iscsi_tcp_segment_unmap %p\n", segment);
148 if (segment->sg_mapped) {
149 debug_tcp("iscsi_tcp_segment_unmap valid\n");
150 kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0);
151 segment->sg_mapped = NULL;
152 segment->data = NULL;
157 * Splice the digest buffer into the buffer
160 iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
162 segment->data = digest;
163 segment->digest_len = ISCSI_DIGEST_SIZE;
164 segment->total_size += ISCSI_DIGEST_SIZE;
165 segment->size = ISCSI_DIGEST_SIZE;
168 segment->hash = NULL;
172 * iscsi_tcp_segment_done - check whether the segment is complete
173 * @segment: iscsi segment to check
174 * @recv: set to one of this is called from the recv path
175 * @copied: number of bytes copied
177 * Check if we're done receiving this segment. If the receive
178 * buffer is full but we expect more data, move on to the
179 * next entry in the scatterlist.
181 * If the amount of data we received isn't a multiple of 4,
182 * we will transparently receive the pad bytes, too.
184 * This function must be re-entrant.
187 iscsi_tcp_segment_done(struct iscsi_segment *segment, int recv, unsigned copied)
189 static unsigned char padbuf[ISCSI_PAD_LEN];
190 struct scatterlist sg;
193 debug_tcp("copied %u %u size %u %s\n", segment->copied, copied,
194 segment->size, recv ? "recv" : "xmit");
195 if (segment->hash && copied) {
197 * If a segment is kmapd we must unmap it before sending
198 * to the crypto layer since that will try to kmap it again.
200 iscsi_tcp_segment_unmap(segment);
202 if (!segment->data) {
203 sg_init_table(&sg, 1);
204 sg_set_page(&sg, sg_page(segment->sg), copied,
205 segment->copied + segment->sg_offset +
206 segment->sg->offset);
208 sg_init_one(&sg, segment->data + segment->copied,
210 crypto_hash_update(segment->hash, &sg, copied);
213 segment->copied += copied;
214 if (segment->copied < segment->size) {
215 iscsi_tcp_segment_map(segment, recv);
219 segment->total_copied += segment->copied;
223 /* Unmap the current scatterlist page, if there is one. */
224 iscsi_tcp_segment_unmap(segment);
226 /* Do we have more scatterlist entries? */
227 debug_tcp("total copied %u total size %u\n", segment->total_copied,
228 segment->total_size);
229 if (segment->total_copied < segment->total_size) {
230 /* Proceed to the next entry in the scatterlist. */
231 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
233 iscsi_tcp_segment_map(segment, recv);
234 BUG_ON(segment->size == 0);
238 /* Do we need to handle padding? */
239 pad = iscsi_padding(segment->total_copied);
241 debug_tcp("consume %d pad bytes\n", pad);
242 segment->total_size += pad;
244 segment->data = padbuf;
249 * Set us up for transferring the data digest. hdr digest
250 * is completely handled in hdr done function.
253 crypto_hash_final(segment->hash, segment->digest);
254 iscsi_tcp_segment_splice_digest(segment,
255 recv ? segment->recv_digest : segment->digest);
263 * iscsi_tcp_xmit_segment - transmit segment
264 * @tcp_conn: the iSCSI TCP connection
265 * @segment: the buffer to transmnit
267 * This function transmits as much of the buffer as
268 * the network layer will accept, and returns the number of
271 * If CRC hashing is enabled, the function will compute the
272 * hash as it goes. When the entire segment has been transmitted,
273 * it will retrieve the hash value and send it as well.
276 iscsi_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
277 struct iscsi_segment *segment)
279 struct socket *sk = tcp_conn->sock;
280 unsigned int copied = 0;
283 while (!iscsi_tcp_segment_done(segment, 0, r)) {
284 struct scatterlist *sg;
285 unsigned int offset, copy;
289 offset = segment->copied;
290 copy = segment->size - offset;
292 if (segment->total_copied + segment->size < segment->total_size)
295 /* Use sendpage if we can; else fall back to sendmsg */
296 if (!segment->data) {
298 offset += segment->sg_offset + sg->offset;
299 r = tcp_conn->sendpage(sk, sg_page(sg), offset, copy,
302 struct msghdr msg = { .msg_flags = flags };
304 .iov_base = segment->data + offset,
308 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
312 iscsi_tcp_segment_unmap(segment);
313 if (copied || r == -EAGAIN)
323 * iscsi_tcp_segment_recv - copy data to segment
324 * @tcp_conn: the iSCSI TCP connection
325 * @segment: the buffer to copy to
327 * @len: amount of data available
329 * This function copies up to @len bytes to the
330 * given buffer, and returns the number of bytes
331 * consumed, which can actually be less than @len.
333 * If hash digest is enabled, the function will update the
334 * hash while copying.
335 * Combining these two operations doesn't buy us a lot (yet),
336 * but in the future we could implement combined copy+crc,
337 * just way we do for network layer checksums.
340 iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
341 struct iscsi_segment *segment, const void *ptr,
344 unsigned int copy = 0, copied = 0;
346 while (!iscsi_tcp_segment_done(segment, 1, copy)) {
348 debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n",
353 copy = min(len - copied, segment->size - segment->copied);
354 debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy);
355 memcpy(segment->data + segment->copied, ptr + copied, copy);
362 iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
363 unsigned char digest[ISCSI_DIGEST_SIZE])
365 struct scatterlist sg;
367 sg_init_one(&sg, hdr, hdrlen);
368 crypto_hash_digest(hash, &sg, hdrlen, digest);
372 iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
373 struct iscsi_segment *segment)
375 if (!segment->digest_len)
378 if (memcmp(segment->recv_digest, segment->digest,
379 segment->digest_len)) {
380 debug_scsi("digest mismatch\n");
388 * Helper function to set up segment buffer
391 __iscsi_segment_init(struct iscsi_segment *segment, size_t size,
392 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
394 memset(segment, 0, sizeof(*segment));
395 segment->total_size = size;
396 segment->done = done;
399 segment->hash = hash;
400 crypto_hash_init(hash);
405 iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
406 size_t size, iscsi_segment_done_fn_t *done,
407 struct hash_desc *hash)
409 __iscsi_segment_init(segment, size, done, hash);
410 segment->data = data;
411 segment->size = size;
415 iscsi_segment_seek_sg(struct iscsi_segment *segment,
416 struct scatterlist *sg_list, unsigned int sg_count,
417 unsigned int offset, size_t size,
418 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
420 struct scatterlist *sg;
423 debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n",
425 __iscsi_segment_init(segment, size, done, hash);
426 for_each_sg(sg_list, sg, sg_count, i) {
427 debug_scsi("sg %d, len %u offset %u\n", i, sg->length,
429 if (offset < sg->length) {
430 iscsi_tcp_segment_init_sg(segment, sg, offset);
433 offset -= sg->length;
436 return ISCSI_ERR_DATA_OFFSET;
440 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
441 * @tcp_conn: iscsi connection to prep for
443 * This function always passes NULL for the hash argument, because when this
444 * function is called we do not yet know the final size of the header and want
445 * to delay the digest processing until we know that.
448 iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
450 debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn,
451 tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : "");
452 iscsi_segment_init_linear(&tcp_conn->in.segment,
453 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
454 iscsi_tcp_hdr_recv_done, NULL);
458 * Handle incoming reply to any other type of command
461 iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
462 struct iscsi_segment *segment)
464 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
467 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
468 return ISCSI_ERR_DATA_DGST;
470 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
471 conn->data, tcp_conn->in.datalen);
475 iscsi_tcp_hdr_recv_prep(tcp_conn);
480 iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
482 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
483 struct hash_desc *rx_hash = NULL;
485 if (conn->datadgst_en)
486 rx_hash = &tcp_conn->rx_hash;
488 iscsi_segment_init_linear(&tcp_conn->in.segment,
489 conn->data, tcp_conn->in.datalen,
490 iscsi_tcp_data_recv_done, rx_hash);
494 * must be called with session lock
497 iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
499 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
500 struct iscsi_r2t_info *r2t;
502 /* flush ctask's r2t queues */
503 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
504 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
506 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
509 r2t = tcp_ctask->r2t;
511 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
513 tcp_ctask->r2t = NULL;
518 * iscsi_data_rsp - SCSI Data-In Response processing
519 * @conn: iscsi connection
520 * @ctask: scsi command task
523 iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
525 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
526 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
527 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
528 struct iscsi_session *session = conn->session;
529 struct scsi_cmnd *sc = ctask->sc;
530 int datasn = be32_to_cpu(rhdr->datasn);
532 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
533 if (tcp_conn->in.datalen == 0)
536 if (tcp_ctask->exp_datasn != datasn) {
537 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
538 __FUNCTION__, tcp_ctask->exp_datasn, datasn);
539 return ISCSI_ERR_DATASN;
542 tcp_ctask->exp_datasn++;
544 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
545 if (tcp_ctask->data_offset + tcp_conn->in.datalen > scsi_bufflen(sc)) {
546 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
547 __FUNCTION__, tcp_ctask->data_offset,
548 tcp_conn->in.datalen, scsi_bufflen(sc));
549 return ISCSI_ERR_DATA_OFFSET;
552 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
553 sc->result = (DID_OK << 16) | rhdr->cmd_status;
554 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
555 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
556 ISCSI_FLAG_DATA_OVERFLOW)) {
557 int res_count = be32_to_cpu(rhdr->residual_count);
560 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
561 res_count <= scsi_bufflen(sc)))
562 scsi_set_resid(sc, res_count);
564 sc->result = (DID_BAD_TARGET << 16) |
569 conn->datain_pdus_cnt++;
574 * iscsi_solicit_data_init - initialize first Data-Out
575 * @conn: iscsi connection
576 * @ctask: scsi command task
580 * Initialize first Data-Out within this R2T sequence and finds
581 * proper data_offset within this SCSI command.
583 * This function is called with connection lock taken.
586 iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
587 struct iscsi_r2t_info *r2t)
589 struct iscsi_data *hdr;
591 hdr = &r2t->dtask.hdr;
592 memset(hdr, 0, sizeof(struct iscsi_data));
594 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
595 r2t->solicit_datasn++;
596 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
597 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
598 hdr->itt = ctask->hdr->itt;
599 hdr->exp_statsn = r2t->exp_statsn;
600 hdr->offset = cpu_to_be32(r2t->data_offset);
601 if (r2t->data_length > conn->max_xmit_dlength) {
602 hton24(hdr->dlength, conn->max_xmit_dlength);
603 r2t->data_count = conn->max_xmit_dlength;
606 hton24(hdr->dlength, r2t->data_length);
607 r2t->data_count = r2t->data_length;
608 hdr->flags = ISCSI_FLAG_CMD_FINAL;
610 conn->dataout_pdus_cnt++;
616 * iscsi_r2t_rsp - iSCSI R2T Response processing
617 * @conn: iscsi connection
618 * @ctask: scsi command task
621 iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
623 struct iscsi_r2t_info *r2t;
624 struct iscsi_session *session = conn->session;
625 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
626 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
627 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
628 int r2tsn = be32_to_cpu(rhdr->r2tsn);
631 if (tcp_conn->in.datalen) {
632 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
633 tcp_conn->in.datalen);
634 return ISCSI_ERR_DATALEN;
637 if (tcp_ctask->exp_datasn != r2tsn){
638 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
639 __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
640 return ISCSI_ERR_R2TSN;
643 /* fill-in new R2T associated with the task */
644 spin_lock(&session->lock);
645 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
647 if (!ctask->sc || session->state != ISCSI_STATE_LOGGED_IN) {
648 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
649 "recovery...\n", ctask->itt);
650 spin_unlock(&session->lock);
654 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
657 r2t->exp_statsn = rhdr->statsn;
658 r2t->data_length = be32_to_cpu(rhdr->data_length);
659 if (r2t->data_length == 0) {
660 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
661 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
663 spin_unlock(&session->lock);
664 return ISCSI_ERR_DATALEN;
667 if (r2t->data_length > session->max_burst)
668 debug_scsi("invalid R2T with data len %u and max burst %u."
669 "Attempting to execute request.\n",
670 r2t->data_length, session->max_burst);
672 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
673 if (r2t->data_offset + r2t->data_length > scsi_bufflen(ctask->sc)) {
674 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
675 "offset %u and total length %d\n", r2t->data_length,
676 r2t->data_offset, scsi_bufflen(ctask->sc));
677 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
679 spin_unlock(&session->lock);
680 return ISCSI_ERR_DATALEN;
683 r2t->ttt = rhdr->ttt; /* no flip */
684 r2t->solicit_datasn = 0;
686 iscsi_solicit_data_init(conn, ctask, r2t);
688 tcp_ctask->exp_datasn = r2tsn + 1;
689 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
690 conn->r2t_pdus_cnt++;
692 iscsi_requeue_ctask(ctask);
693 spin_unlock(&session->lock);
699 * Handle incoming reply to DataIn command
702 iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
703 struct iscsi_segment *segment)
705 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
706 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
709 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
710 return ISCSI_ERR_DATA_DGST;
712 /* check for non-exceptional status */
713 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
714 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
719 iscsi_tcp_hdr_recv_prep(tcp_conn);
724 * iscsi_tcp_hdr_dissect - process PDU header
725 * @conn: iSCSI connection
728 * This function analyzes the header of the PDU received,
729 * and performs several sanity checks. If the PDU is accompanied
730 * by data, the receive buffer is set up to copy the incoming data
731 * to the correct location.
734 iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
736 int rc = 0, opcode, ahslen;
737 struct iscsi_session *session = conn->session;
738 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
739 struct iscsi_cmd_task *ctask;
742 /* verify PDU length */
743 tcp_conn->in.datalen = ntoh24(hdr->dlength);
744 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
745 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
746 tcp_conn->in.datalen, conn->max_recv_dlength);
747 return ISCSI_ERR_DATALEN;
750 /* Additional header segments. So far, we don't
751 * process additional headers.
753 ahslen = hdr->hlength << 2;
755 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
756 /* verify itt (itt encoding: age+cid+itt) */
757 rc = iscsi_verify_itt(conn, hdr, &itt);
758 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
759 /* XXX: what does this do? */
760 tcp_conn->in.datalen = 0; /* force drop */
765 debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
766 opcode, ahslen, tcp_conn->in.datalen);
769 case ISCSI_OP_SCSI_DATA_IN:
770 ctask = session->cmds[itt];
771 rc = iscsi_data_rsp(conn, ctask);
774 if (tcp_conn->in.datalen) {
775 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
776 struct hash_desc *rx_hash = NULL;
779 * Setup copy of Data-In into the Scsi_Cmnd
781 * We set up the iscsi_segment to point to the next
782 * scatterlist entry to copy to. As we go along,
783 * we move on to the next scatterlist entry and
784 * update the digest per-entry.
786 if (conn->datadgst_en)
787 rx_hash = &tcp_conn->rx_hash;
789 debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
790 "datalen=%d)\n", tcp_conn,
791 tcp_ctask->data_offset,
792 tcp_conn->in.datalen);
793 return iscsi_segment_seek_sg(&tcp_conn->in.segment,
794 scsi_sglist(ctask->sc),
795 scsi_sg_count(ctask->sc),
796 tcp_ctask->data_offset,
797 tcp_conn->in.datalen,
798 iscsi_tcp_process_data_in,
802 case ISCSI_OP_SCSI_CMD_RSP:
803 if (tcp_conn->in.datalen) {
804 iscsi_tcp_data_recv_prep(tcp_conn);
807 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
810 ctask = session->cmds[itt];
812 rc = ISCSI_ERR_AHSLEN;
813 else if (ctask->sc->sc_data_direction == DMA_TO_DEVICE)
814 rc = iscsi_r2t_rsp(conn, ctask);
816 rc = ISCSI_ERR_PROTO;
818 case ISCSI_OP_LOGIN_RSP:
819 case ISCSI_OP_TEXT_RSP:
820 case ISCSI_OP_REJECT:
821 case ISCSI_OP_ASYNC_EVENT:
823 * It is possible that we could get a PDU with a buffer larger
824 * than 8K, but there are no targets that currently do this.
825 * For now we fail until we find a vendor that needs it
827 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
828 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
829 "but conn buffer is only %u (opcode %0x)\n",
830 tcp_conn->in.datalen,
831 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
832 rc = ISCSI_ERR_PROTO;
836 /* If there's data coming in with the response,
837 * receive it to the connection's buffer.
839 if (tcp_conn->in.datalen) {
840 iscsi_tcp_data_recv_prep(tcp_conn);
844 case ISCSI_OP_LOGOUT_RSP:
845 case ISCSI_OP_NOOP_IN:
846 case ISCSI_OP_SCSI_TMFUNC_RSP:
847 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
850 rc = ISCSI_ERR_BAD_OPCODE;
855 /* Anything that comes with data should have
856 * been handled above. */
857 if (tcp_conn->in.datalen)
858 return ISCSI_ERR_PROTO;
859 iscsi_tcp_hdr_recv_prep(tcp_conn);
866 * iscsi_tcp_hdr_recv_done - process PDU header
868 * This is the callback invoked when the PDU header has
869 * been received. If the header is followed by additional
870 * header segments, we go back for more data.
873 iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
874 struct iscsi_segment *segment)
876 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
877 struct iscsi_hdr *hdr;
879 /* Check if there are additional header segments
880 * *prior* to computing the digest, because we
881 * may need to go back to the caller for more.
883 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
884 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
885 /* Bump the header length - the caller will
886 * just loop around and get the AHS for us, and
888 unsigned int ahslen = hdr->hlength << 2;
890 /* Make sure we don't overflow */
891 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
892 return ISCSI_ERR_AHSLEN;
894 segment->total_size += ahslen;
895 segment->size += ahslen;
899 /* We're done processing the header. See if we're doing
900 * header digests; if so, set up the recv_digest buffer
901 * and go back for more. */
902 if (conn->hdrdgst_en) {
903 if (segment->digest_len == 0) {
904 iscsi_tcp_segment_splice_digest(segment,
905 segment->recv_digest);
908 iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
909 segment->total_copied - ISCSI_DIGEST_SIZE,
912 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
913 return ISCSI_ERR_HDR_DGST;
916 tcp_conn->in.hdr = hdr;
917 return iscsi_tcp_hdr_dissect(conn, hdr);
921 * iscsi_tcp_recv - TCP receive in sendfile fashion
922 * @rd_desc: read descriptor
923 * @skb: socket buffer
924 * @offset: offset in skb
925 * @len: skb->len - offset
928 iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
929 unsigned int offset, size_t len)
931 struct iscsi_conn *conn = rd_desc->arg.data;
932 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
933 struct iscsi_segment *segment = &tcp_conn->in.segment;
934 struct skb_seq_state seq;
935 unsigned int consumed = 0;
938 debug_tcp("in %d bytes\n", skb->len - offset);
940 if (unlikely(conn->suspend_rx)) {
941 debug_tcp("conn %d Rx suspended!\n", conn->id);
945 skb_prepare_seq_read(skb, offset, skb->len, &seq);
950 avail = skb_seq_read(consumed, &ptr, &seq);
952 debug_tcp("no more data avail. Consumed %d\n",
956 BUG_ON(segment->copied >= segment->size);
958 debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
959 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
963 if (segment->total_copied >= segment->total_size) {
964 debug_tcp("segment done\n");
965 rc = segment->done(tcp_conn, segment);
967 skb_abort_seq_read(&seq);
971 /* The done() functions sets up the
975 skb_abort_seq_read(&seq);
976 conn->rxdata_octets += consumed;
980 debug_tcp("Error receiving PDU, errno=%d\n", rc);
981 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
986 iscsi_tcp_data_ready(struct sock *sk, int flag)
988 struct iscsi_conn *conn = sk->sk_user_data;
989 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
990 read_descriptor_t rd_desc;
992 read_lock(&sk->sk_callback_lock);
995 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
996 * We set count to 1 because we want the network layer to
997 * hand us all the skbs that are available. iscsi_tcp_recv
998 * handled pdus that cross buffers or pdus that still need data.
1000 rd_desc.arg.data = conn;
1002 tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
1004 read_unlock(&sk->sk_callback_lock);
1006 /* If we had to (atomically) map a highmem page,
1008 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
1012 iscsi_tcp_state_change(struct sock *sk)
1014 struct iscsi_tcp_conn *tcp_conn;
1015 struct iscsi_conn *conn;
1016 struct iscsi_session *session;
1017 void (*old_state_change)(struct sock *);
1019 read_lock(&sk->sk_callback_lock);
1021 conn = (struct iscsi_conn*)sk->sk_user_data;
1022 session = conn->session;
1024 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1025 sk->sk_state == TCP_CLOSE) &&
1026 !atomic_read(&sk->sk_rmem_alloc)) {
1027 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1028 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1031 tcp_conn = conn->dd_data;
1032 old_state_change = tcp_conn->old_state_change;
1034 read_unlock(&sk->sk_callback_lock);
1036 old_state_change(sk);
1040 * iscsi_write_space - Called when more output buffer space is available
1041 * @sk: socket space is available for
1044 iscsi_write_space(struct sock *sk)
1046 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
1047 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1049 tcp_conn->old_write_space(sk);
1050 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
1051 scsi_queue_work(conn->session->host, &conn->xmitwork);
1055 iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1057 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1058 struct sock *sk = tcp_conn->sock->sk;
1060 /* assign new callbacks */
1061 write_lock_bh(&sk->sk_callback_lock);
1062 sk->sk_user_data = conn;
1063 tcp_conn->old_data_ready = sk->sk_data_ready;
1064 tcp_conn->old_state_change = sk->sk_state_change;
1065 tcp_conn->old_write_space = sk->sk_write_space;
1066 sk->sk_data_ready = iscsi_tcp_data_ready;
1067 sk->sk_state_change = iscsi_tcp_state_change;
1068 sk->sk_write_space = iscsi_write_space;
1069 write_unlock_bh(&sk->sk_callback_lock);
1073 iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
1075 struct sock *sk = tcp_conn->sock->sk;
1077 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1078 write_lock_bh(&sk->sk_callback_lock);
1079 sk->sk_user_data = NULL;
1080 sk->sk_data_ready = tcp_conn->old_data_ready;
1081 sk->sk_state_change = tcp_conn->old_state_change;
1082 sk->sk_write_space = tcp_conn->old_write_space;
1083 sk->sk_no_check = 0;
1084 write_unlock_bh(&sk->sk_callback_lock);
1088 * iscsi_xmit - TCP transmit
1091 iscsi_xmit(struct iscsi_conn *conn)
1093 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1094 struct iscsi_segment *segment = &tcp_conn->out.segment;
1095 unsigned int consumed = 0;
1099 rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
1107 if (segment->total_copied >= segment->total_size) {
1108 if (segment->done != NULL) {
1109 rc = segment->done(tcp_conn, segment);
1116 debug_tcp("xmit %d bytes\n", consumed);
1118 conn->txdata_octets += consumed;
1122 /* Transmit error. We could initiate error recovery
1124 debug_tcp("Error sending PDU, errno=%d\n", rc);
1125 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1130 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
1133 iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
1135 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1136 struct iscsi_segment *segment = &tcp_conn->out.segment;
1138 return segment->total_copied - segment->total_size;
1142 iscsi_tcp_flush(struct iscsi_conn *conn)
1146 while (iscsi_tcp_xmit_qlen(conn)) {
1147 rc = iscsi_xmit(conn);
1158 * This is called when we're done sending the header.
1159 * Simply copy the data_segment to the send segment, and return.
1162 iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
1163 struct iscsi_segment *segment)
1165 tcp_conn->out.segment = tcp_conn->out.data_segment;
1166 debug_tcp("Header done. Next segment size %u total_size %u\n",
1167 tcp_conn->out.segment.size, tcp_conn->out.segment.total_size);
1172 iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen)
1174 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1176 debug_tcp("%s(%p%s)\n", __FUNCTION__, tcp_conn,
1177 conn->hdrdgst_en? ", digest enabled" : "");
1179 /* Clear the data segment - needs to be filled in by the
1180 * caller using iscsi_tcp_send_data_prep() */
1181 memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment));
1183 /* If header digest is enabled, compute the CRC and
1184 * place the digest into the same buffer. We make
1185 * sure that both iscsi_tcp_ctask and mtask have
1188 if (conn->hdrdgst_en) {
1189 iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
1191 hdrlen += ISCSI_DIGEST_SIZE;
1194 /* Remember header pointer for later, when we need
1195 * to decide whether there's a payload to go along
1196 * with the header. */
1197 tcp_conn->out.hdr = hdr;
1199 iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen,
1200 iscsi_tcp_send_hdr_done, NULL);
1204 * Prepare the send buffer for the payload data.
1205 * Padding and checksumming will all be taken care
1206 * of by the iscsi_segment routines.
1209 iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
1210 unsigned int count, unsigned int offset,
1213 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1214 struct hash_desc *tx_hash = NULL;
1215 unsigned int hdr_spec_len;
1217 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __FUNCTION__,
1218 tcp_conn, offset, len,
1219 conn->datadgst_en? ", digest enabled" : "");
1221 /* Make sure the datalen matches what the caller
1222 said he would send. */
1223 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1224 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
1226 if (conn->datadgst_en)
1227 tx_hash = &tcp_conn->tx_hash;
1229 return iscsi_segment_seek_sg(&tcp_conn->out.data_segment,
1230 sg, count, offset, len,
1235 iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
1238 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1239 struct hash_desc *tx_hash = NULL;
1240 unsigned int hdr_spec_len;
1242 debug_tcp("%s(%p, datalen=%d%s)\n", __FUNCTION__, tcp_conn, len,
1243 conn->datadgst_en? ", digest enabled" : "");
1245 /* Make sure the datalen matches what the caller
1246 said he would send. */
1247 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1248 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
1250 if (conn->datadgst_en)
1251 tx_hash = &tcp_conn->tx_hash;
1253 iscsi_segment_init_linear(&tcp_conn->out.data_segment,
1254 data, len, NULL, tx_hash);
1258 * iscsi_solicit_data_cont - initialize next Data-Out
1259 * @conn: iscsi connection
1260 * @ctask: scsi command task
1262 * @left: bytes left to transfer
1265 * Initialize next Data-Out within this R2T sequence and continue
1266 * to process next Scatter-Gather element(if any) of this SCSI command.
1268 * Called under connection lock.
1271 iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1272 struct iscsi_r2t_info *r2t)
1274 struct iscsi_data *hdr;
1275 int new_offset, left;
1277 BUG_ON(r2t->data_length - r2t->sent < 0);
1278 left = r2t->data_length - r2t->sent;
1282 hdr = &r2t->dtask.hdr;
1283 memset(hdr, 0, sizeof(struct iscsi_data));
1284 hdr->ttt = r2t->ttt;
1285 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1286 r2t->solicit_datasn++;
1287 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1288 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1289 hdr->itt = ctask->hdr->itt;
1290 hdr->exp_statsn = r2t->exp_statsn;
1291 new_offset = r2t->data_offset + r2t->sent;
1292 hdr->offset = cpu_to_be32(new_offset);
1293 if (left > conn->max_xmit_dlength) {
1294 hton24(hdr->dlength, conn->max_xmit_dlength);
1295 r2t->data_count = conn->max_xmit_dlength;
1297 hton24(hdr->dlength, left);
1298 r2t->data_count = left;
1299 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1302 conn->dataout_pdus_cnt++;
1307 * iscsi_tcp_ctask - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
1308 * @conn: iscsi connection
1309 * @ctask: scsi command task
1313 iscsi_tcp_ctask_init(struct iscsi_cmd_task *ctask)
1315 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1316 struct iscsi_conn *conn = ctask->conn;
1317 struct scsi_cmnd *sc = ctask->sc;
1320 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
1321 tcp_ctask->sent = 0;
1322 tcp_ctask->exp_datasn = 0;
1324 /* Prepare PDU, optionally w/ immediate data */
1325 debug_scsi("ctask deq [cid %d itt 0x%x imm %d unsol %d]\n",
1326 conn->id, ctask->itt, ctask->imm_count,
1327 ctask->unsol_count);
1328 iscsi_tcp_send_hdr_prep(conn, ctask->hdr, ctask->hdr_len);
1330 if (!ctask->imm_count)
1333 /* If we have immediate data, attach a payload */
1334 err = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc), scsi_sg_count(sc),
1335 0, ctask->imm_count);
1338 tcp_ctask->sent += ctask->imm_count;
1339 ctask->imm_count = 0;
1344 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
1345 * @conn: iscsi connection
1346 * @mtask: task management task
1349 * The function can return -EAGAIN in which case caller must
1350 * call it again later, or recover. '0' return code means successful
1354 iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1358 /* Flush any pending data first. */
1359 rc = iscsi_tcp_flush(conn);
1363 if (mtask->hdr->itt == RESERVED_ITT) {
1364 struct iscsi_session *session = conn->session;
1366 spin_lock_bh(&session->lock);
1367 iscsi_free_mgmt_task(conn, mtask);
1368 spin_unlock_bh(&session->lock);
1375 * iscsi_tcp_ctask_xmit - xmit normal PDU task
1376 * @conn: iscsi connection
1377 * @ctask: iscsi command task
1379 * We're expected to return 0 when everything was transmitted succesfully,
1380 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1384 iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1386 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1387 struct scsi_cmnd *sc = ctask->sc;
1391 /* Flush any pending data first. */
1392 rc = iscsi_tcp_flush(conn);
1396 /* Are we done already? */
1397 if (sc->sc_data_direction != DMA_TO_DEVICE)
1400 if (ctask->unsol_count != 0) {
1401 struct iscsi_data *hdr = &tcp_ctask->unsol_dtask.hdr;
1403 /* Prepare a header for the unsolicited PDU.
1404 * The amount of data we want to send will be
1405 * in ctask->data_count.
1406 * FIXME: return the data count instead.
1408 iscsi_prep_unsolicit_data_pdu(ctask, hdr);
1410 debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n",
1411 ctask->itt, tcp_ctask->sent, ctask->data_count);
1413 iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr));
1414 rc = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc),
1420 tcp_ctask->sent += ctask->data_count;
1421 ctask->unsol_count -= ctask->data_count;
1424 struct iscsi_session *session = conn->session;
1425 struct iscsi_r2t_info *r2t;
1427 /* All unsolicited PDUs sent. Check for solicited PDUs.
1429 spin_lock_bh(&session->lock);
1430 r2t = tcp_ctask->r2t;
1432 /* Continue with this R2T? */
1433 if (!iscsi_solicit_data_cont(conn, ctask, r2t)) {
1434 debug_scsi(" done with r2t %p\n", r2t);
1436 __kfifo_put(tcp_ctask->r2tpool.queue,
1437 (void*)&r2t, sizeof(void*));
1438 tcp_ctask->r2t = r2t = NULL;
1443 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1445 r2t = tcp_ctask->r2t;
1447 spin_unlock_bh(&session->lock);
1449 /* Waiting for more R2Ts to arrive. */
1451 debug_tcp("no R2Ts yet\n");
1455 debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1456 r2t, r2t->solicit_datasn - 1, ctask->itt,
1457 r2t->data_offset + r2t->sent, r2t->data_count);
1459 iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr,
1460 sizeof(struct iscsi_hdr));
1462 rc = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc),
1464 r2t->data_offset + r2t->sent,
1468 tcp_ctask->sent += r2t->data_count;
1469 r2t->sent += r2t->data_count;
1474 iscsi_conn_failure(conn, rc);
1478 static struct iscsi_cls_conn *
1479 iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1481 struct iscsi_conn *conn;
1482 struct iscsi_cls_conn *cls_conn;
1483 struct iscsi_tcp_conn *tcp_conn;
1485 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
1488 conn = cls_conn->dd_data;
1490 * due to strange issues with iser these are not set
1491 * in iscsi_conn_setup
1493 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1495 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1497 goto tcp_conn_alloc_fail;
1499 conn->dd_data = tcp_conn;
1500 tcp_conn->iscsi_conn = conn;
1502 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1504 tcp_conn->tx_hash.flags = 0;
1505 if (IS_ERR(tcp_conn->tx_hash.tfm)) {
1506 printk(KERN_ERR "Could not create connection due to crc32c "
1507 "loading error %ld. Make sure the crc32c module is "
1508 "built as a module or into the kernel\n",
1509 PTR_ERR(tcp_conn->tx_hash.tfm));
1513 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1515 tcp_conn->rx_hash.flags = 0;
1516 if (IS_ERR(tcp_conn->rx_hash.tfm)) {
1517 printk(KERN_ERR "Could not create connection due to crc32c "
1518 "loading error %ld. Make sure the crc32c module is "
1519 "built as a module or into the kernel\n",
1520 PTR_ERR(tcp_conn->rx_hash.tfm));
1527 crypto_free_hash(tcp_conn->tx_hash.tfm);
1530 tcp_conn_alloc_fail:
1531 iscsi_conn_teardown(cls_conn);
1536 iscsi_tcp_release_conn(struct iscsi_conn *conn)
1538 struct iscsi_session *session = conn->session;
1539 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1540 struct socket *sock = tcp_conn->sock;
1545 sock_hold(sock->sk);
1546 iscsi_conn_restore_callbacks(tcp_conn);
1549 spin_lock_bh(&session->lock);
1550 tcp_conn->sock = NULL;
1551 conn->recv_lock = NULL;
1552 spin_unlock_bh(&session->lock);
1557 iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
1559 struct iscsi_conn *conn = cls_conn->dd_data;
1560 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1562 iscsi_tcp_release_conn(conn);
1563 iscsi_conn_teardown(cls_conn);
1565 if (tcp_conn->tx_hash.tfm)
1566 crypto_free_hash(tcp_conn->tx_hash.tfm);
1567 if (tcp_conn->rx_hash.tfm)
1568 crypto_free_hash(tcp_conn->rx_hash.tfm);
1574 iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1576 struct iscsi_conn *conn = cls_conn->dd_data;
1578 iscsi_conn_stop(cls_conn, flag);
1579 iscsi_tcp_release_conn(conn);
1582 static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
1583 char *buf, int *port,
1584 int (*getname)(struct socket *, struct sockaddr *,
1587 struct sockaddr_storage *addr;
1588 struct sockaddr_in6 *sin6;
1589 struct sockaddr_in *sin;
1592 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
1596 if (getname(sock, (struct sockaddr *) addr, &len)) {
1601 switch (addr->ss_family) {
1603 sin = (struct sockaddr_in *)addr;
1604 spin_lock_bh(&conn->session->lock);
1605 sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
1606 *port = be16_to_cpu(sin->sin_port);
1607 spin_unlock_bh(&conn->session->lock);
1610 sin6 = (struct sockaddr_in6 *)addr;
1611 spin_lock_bh(&conn->session->lock);
1612 sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
1613 *port = be16_to_cpu(sin6->sin6_port);
1614 spin_unlock_bh(&conn->session->lock);
1623 iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
1624 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
1627 struct iscsi_conn *conn = cls_conn->dd_data;
1628 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1630 struct socket *sock;
1633 /* lookup for existing socket */
1634 sock = sockfd_lookup((int)transport_eph, &err);
1636 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1640 * copy these values now because if we drop the session
1641 * userspace may still want to query the values since we will
1642 * be using them for the reconnect
1644 err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1645 &conn->portal_port, kernel_getpeername);
1649 err = iscsi_tcp_get_addr(conn, sock, conn->local_address,
1650 &conn->local_port, kernel_getsockname);
1654 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1658 /* bind iSCSI connection and socket */
1659 tcp_conn->sock = sock;
1661 /* setup Socket parameters */
1664 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1665 sk->sk_allocation = GFP_ATOMIC;
1667 /* FIXME: disable Nagle's algorithm */
1670 * Intercept TCP callbacks for sendfile like receive
1673 conn->recv_lock = &sk->sk_callback_lock;
1674 iscsi_conn_set_callbacks(conn);
1675 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1677 * set receive state machine into initial state
1679 iscsi_tcp_hdr_recv_prep(tcp_conn);
1687 /* called with host lock */
1689 iscsi_tcp_mtask_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1691 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt);
1693 /* Prepare PDU, optionally w/ immediate data */
1694 iscsi_tcp_send_hdr_prep(conn, mtask->hdr, sizeof(*mtask->hdr));
1696 /* If we have immediate data, attach a payload */
1697 if (mtask->data_count)
1698 iscsi_tcp_send_linear_data_prepare(conn, mtask->data,
1703 iscsi_r2tpool_alloc(struct iscsi_session *session)
1709 * initialize per-task: R2T pool and xmit queue
1711 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1712 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1713 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1716 * pre-allocated x4 as much r2ts to handle race when
1717 * target acks DataOut faster than we data_xmit() queues
1718 * could replenish r2tqueue.
1722 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4, NULL,
1723 sizeof(struct iscsi_r2t_info))) {
1724 goto r2t_alloc_fail;
1727 /* R2T xmit queue */
1728 tcp_ctask->r2tqueue = kfifo_alloc(
1729 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
1730 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
1731 iscsi_pool_free(&tcp_ctask->r2tpool);
1732 goto r2t_alloc_fail;
1739 for (i = 0; i < cmd_i; i++) {
1740 struct iscsi_cmd_task *ctask = session->cmds[i];
1741 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1743 kfifo_free(tcp_ctask->r2tqueue);
1744 iscsi_pool_free(&tcp_ctask->r2tpool);
1750 iscsi_r2tpool_free(struct iscsi_session *session)
1754 for (i = 0; i < session->cmds_max; i++) {
1755 struct iscsi_cmd_task *ctask = session->cmds[i];
1756 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1758 kfifo_free(tcp_ctask->r2tqueue);
1759 iscsi_pool_free(&tcp_ctask->r2tpool);
1764 iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
1765 char *buf, int buflen)
1767 struct iscsi_conn *conn = cls_conn->dd_data;
1768 struct iscsi_session *session = conn->session;
1769 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1773 case ISCSI_PARAM_HDRDGST_EN:
1774 iscsi_set_param(cls_conn, param, buf, buflen);
1776 case ISCSI_PARAM_DATADGST_EN:
1777 iscsi_set_param(cls_conn, param, buf, buflen);
1778 tcp_conn->sendpage = conn->datadgst_en ?
1779 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
1781 case ISCSI_PARAM_MAX_R2T:
1782 sscanf(buf, "%d", &value);
1783 if (session->max_r2t == roundup_pow_of_two(value))
1785 iscsi_r2tpool_free(session);
1786 iscsi_set_param(cls_conn, param, buf, buflen);
1787 if (session->max_r2t & (session->max_r2t - 1))
1788 session->max_r2t = roundup_pow_of_two(session->max_r2t);
1789 if (iscsi_r2tpool_alloc(session))
1793 return iscsi_set_param(cls_conn, param, buf, buflen);
1800 iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
1801 enum iscsi_param param, char *buf)
1803 struct iscsi_conn *conn = cls_conn->dd_data;
1807 case ISCSI_PARAM_CONN_PORT:
1808 spin_lock_bh(&conn->session->lock);
1809 len = sprintf(buf, "%hu\n", conn->portal_port);
1810 spin_unlock_bh(&conn->session->lock);
1812 case ISCSI_PARAM_CONN_ADDRESS:
1813 spin_lock_bh(&conn->session->lock);
1814 len = sprintf(buf, "%s\n", conn->portal_address);
1815 spin_unlock_bh(&conn->session->lock);
1818 return iscsi_conn_get_param(cls_conn, param, buf);
1825 iscsi_tcp_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
1828 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1832 case ISCSI_HOST_PARAM_IPADDRESS:
1833 spin_lock_bh(&session->lock);
1834 if (!session->leadconn)
1837 len = sprintf(buf, "%s\n",
1838 session->leadconn->local_address);
1839 spin_unlock_bh(&session->lock);
1842 return iscsi_host_get_param(shost, param, buf);
1848 iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
1850 struct iscsi_conn *conn = cls_conn->dd_data;
1851 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1853 stats->txdata_octets = conn->txdata_octets;
1854 stats->rxdata_octets = conn->rxdata_octets;
1855 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1856 stats->dataout_pdus = conn->dataout_pdus_cnt;
1857 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1858 stats->datain_pdus = conn->datain_pdus_cnt;
1859 stats->r2t_pdus = conn->r2t_pdus_cnt;
1860 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1861 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1862 stats->custom_length = 3;
1863 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
1864 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
1865 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
1866 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
1867 strcpy(stats->custom[2].desc, "eh_abort_cnt");
1868 stats->custom[2].value = conn->eh_abort_cnt;
1871 static struct iscsi_cls_session *
1872 iscsi_tcp_session_create(struct iscsi_transport *iscsit,
1873 struct scsi_transport_template *scsit,
1874 uint16_t cmds_max, uint16_t qdepth,
1875 uint32_t initial_cmdsn, uint32_t *hostno)
1877 struct iscsi_cls_session *cls_session;
1878 struct iscsi_session *session;
1882 cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
1883 sizeof(struct iscsi_tcp_cmd_task),
1884 sizeof(struct iscsi_tcp_mgmt_task),
1885 initial_cmdsn, &hn);
1890 session = class_to_transport_session(cls_session);
1891 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1892 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1893 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1895 ctask->hdr = &tcp_ctask->hdr.cmd_hdr;
1896 ctask->hdr_max = sizeof(tcp_ctask->hdr) - ISCSI_DIGEST_SIZE;
1899 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1900 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1901 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
1903 mtask->hdr = (struct iscsi_hdr *) &tcp_mtask->hdr;
1906 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
1907 goto r2tpool_alloc_fail;
1912 iscsi_session_teardown(cls_session);
1916 static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
1918 iscsi_r2tpool_free(class_to_transport_session(cls_session));
1919 iscsi_session_teardown(cls_session);
1922 static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
1924 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
1925 blk_queue_dma_alignment(sdev->request_queue, 0);
1929 static struct scsi_host_template iscsi_sht = {
1930 .module = THIS_MODULE,
1931 .name = "iSCSI Initiator over TCP/IP",
1932 .queuecommand = iscsi_queuecommand,
1933 .change_queue_depth = iscsi_change_queue_depth,
1934 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
1935 .sg_tablesize = ISCSI_SG_TABLESIZE,
1936 .max_sectors = 0xFFFF,
1937 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
1938 .eh_abort_handler = iscsi_eh_abort,
1939 .eh_device_reset_handler= iscsi_eh_device_reset,
1940 .eh_host_reset_handler = iscsi_eh_host_reset,
1941 .use_clustering = DISABLE_CLUSTERING,
1942 .slave_configure = iscsi_tcp_slave_configure,
1943 .proc_name = "iscsi_tcp",
1947 static struct iscsi_transport iscsi_tcp_transport = {
1948 .owner = THIS_MODULE,
1950 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
1952 .param_mask = ISCSI_MAX_RECV_DLENGTH |
1953 ISCSI_MAX_XMIT_DLENGTH |
1956 ISCSI_INITIAL_R2T_EN |
1961 ISCSI_PDU_INORDER_EN |
1962 ISCSI_DATASEQ_INORDER_EN |
1965 ISCSI_CONN_ADDRESS |
1967 ISCSI_PERSISTENT_PORT |
1968 ISCSI_PERSISTENT_ADDRESS |
1969 ISCSI_TARGET_NAME | ISCSI_TPGT |
1970 ISCSI_USERNAME | ISCSI_PASSWORD |
1971 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
1972 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
1973 ISCSI_LU_RESET_TMO |
1974 ISCSI_PING_TMO | ISCSI_RECV_TMO,
1975 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
1976 ISCSI_HOST_INITIATOR_NAME |
1977 ISCSI_HOST_NETDEV_NAME,
1978 .host_template = &iscsi_sht,
1979 .conndata_size = sizeof(struct iscsi_conn),
1981 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
1982 /* session management */
1983 .create_session = iscsi_tcp_session_create,
1984 .destroy_session = iscsi_tcp_session_destroy,
1985 /* connection management */
1986 .create_conn = iscsi_tcp_conn_create,
1987 .bind_conn = iscsi_tcp_conn_bind,
1988 .destroy_conn = iscsi_tcp_conn_destroy,
1989 .set_param = iscsi_conn_set_param,
1990 .get_conn_param = iscsi_tcp_conn_get_param,
1991 .get_session_param = iscsi_session_get_param,
1992 .start_conn = iscsi_conn_start,
1993 .stop_conn = iscsi_tcp_conn_stop,
1994 /* iscsi host params */
1995 .get_host_param = iscsi_tcp_host_get_param,
1996 .set_host_param = iscsi_host_set_param,
1998 .send_pdu = iscsi_conn_send_pdu,
1999 .get_stats = iscsi_conn_get_stats,
2000 .init_cmd_task = iscsi_tcp_ctask_init,
2001 .init_mgmt_task = iscsi_tcp_mtask_init,
2002 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2003 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2004 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2006 .session_recovery_timedout = iscsi_session_recovery_timedout,
2010 iscsi_tcp_init(void)
2012 if (iscsi_max_lun < 1) {
2013 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2017 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2019 if (!iscsi_register_transport(&iscsi_tcp_transport))
2026 iscsi_tcp_exit(void)
2028 iscsi_unregister_transport(&iscsi_tcp_transport);
2031 module_init(iscsi_tcp_init);
2032 module_exit(iscsi_tcp_exit);