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 struct scsi_transport_template *iscsi_tcp_scsi_transport;
68 static struct scsi_host_template iscsi_sht;
69 static struct iscsi_transport iscsi_tcp_transport;
71 static unsigned int iscsi_max_lun = 512;
72 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
74 static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
75 struct iscsi_segment *segment);
78 * Scatterlist handling: inside the iscsi_segment, we
79 * remember an index into the scatterlist, and set data/size
80 * to the current scatterlist entry. For highmem pages, we
83 * Note that the page is unmapped when we return from
84 * TCP's data_ready handler, so we may end up mapping and
85 * unmapping the same page repeatedly. The whole reason
86 * for this is that we shouldn't keep the page mapped
87 * outside the softirq.
91 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
92 * @segment: the buffer object
94 * @offset: byte offset into that sg entry
96 * This function sets up the segment so that subsequent
97 * data is copied to the indicated sg entry, at the given
101 iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
102 struct scatterlist *sg, unsigned int offset)
105 segment->sg_offset = offset;
106 segment->size = min(sg->length - offset,
107 segment->total_size - segment->total_copied);
108 segment->data = NULL;
112 * iscsi_tcp_segment_map - map the current S/G page
113 * @segment: iscsi_segment
114 * @recv: 1 if called from recv path
116 * We only need to possibly kmap data if scatter lists are being used,
117 * because the iscsi passthrough and internal IO paths will never use high
121 iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
123 struct scatterlist *sg;
125 if (segment->data != NULL || !segment->sg)
129 BUG_ON(segment->sg_mapped);
130 BUG_ON(sg->length == 0);
133 * If the page count is greater than one it is ok to send
134 * to the network layer's zero copy send path. If not we
135 * have to go the slow sendmsg path. We always map for the
138 if (page_count(sg_page(sg)) >= 1 && !recv)
141 debug_tcp("iscsi_tcp_segment_map %s %p\n", recv ? "recv" : "xmit",
143 segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0);
144 segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
148 iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
150 debug_tcp("iscsi_tcp_segment_unmap %p\n", segment);
152 if (segment->sg_mapped) {
153 debug_tcp("iscsi_tcp_segment_unmap valid\n");
154 kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0);
155 segment->sg_mapped = NULL;
156 segment->data = NULL;
161 * Splice the digest buffer into the buffer
164 iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
166 segment->data = digest;
167 segment->digest_len = ISCSI_DIGEST_SIZE;
168 segment->total_size += ISCSI_DIGEST_SIZE;
169 segment->size = ISCSI_DIGEST_SIZE;
172 segment->hash = NULL;
176 * iscsi_tcp_segment_done - check whether the segment is complete
177 * @segment: iscsi segment to check
178 * @recv: set to one of this is called from the recv path
179 * @copied: number of bytes copied
181 * Check if we're done receiving this segment. If the receive
182 * buffer is full but we expect more data, move on to the
183 * next entry in the scatterlist.
185 * If the amount of data we received isn't a multiple of 4,
186 * we will transparently receive the pad bytes, too.
188 * This function must be re-entrant.
191 iscsi_tcp_segment_done(struct iscsi_segment *segment, int recv, unsigned copied)
193 static unsigned char padbuf[ISCSI_PAD_LEN];
194 struct scatterlist sg;
197 debug_tcp("copied %u %u size %u %s\n", segment->copied, copied,
198 segment->size, recv ? "recv" : "xmit");
199 if (segment->hash && copied) {
201 * If a segment is kmapd we must unmap it before sending
202 * to the crypto layer since that will try to kmap it again.
204 iscsi_tcp_segment_unmap(segment);
206 if (!segment->data) {
207 sg_init_table(&sg, 1);
208 sg_set_page(&sg, sg_page(segment->sg), copied,
209 segment->copied + segment->sg_offset +
210 segment->sg->offset);
212 sg_init_one(&sg, segment->data + segment->copied,
214 crypto_hash_update(segment->hash, &sg, copied);
217 segment->copied += copied;
218 if (segment->copied < segment->size) {
219 iscsi_tcp_segment_map(segment, recv);
223 segment->total_copied += segment->copied;
227 /* Unmap the current scatterlist page, if there is one. */
228 iscsi_tcp_segment_unmap(segment);
230 /* Do we have more scatterlist entries? */
231 debug_tcp("total copied %u total size %u\n", segment->total_copied,
232 segment->total_size);
233 if (segment->total_copied < segment->total_size) {
234 /* Proceed to the next entry in the scatterlist. */
235 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
237 iscsi_tcp_segment_map(segment, recv);
238 BUG_ON(segment->size == 0);
242 /* Do we need to handle padding? */
243 pad = iscsi_padding(segment->total_copied);
245 debug_tcp("consume %d pad bytes\n", pad);
246 segment->total_size += pad;
248 segment->data = padbuf;
253 * Set us up for transferring the data digest. hdr digest
254 * is completely handled in hdr done function.
257 crypto_hash_final(segment->hash, segment->digest);
258 iscsi_tcp_segment_splice_digest(segment,
259 recv ? segment->recv_digest : segment->digest);
267 * iscsi_tcp_xmit_segment - transmit segment
268 * @tcp_conn: the iSCSI TCP connection
269 * @segment: the buffer to transmnit
271 * This function transmits as much of the buffer as
272 * the network layer will accept, and returns the number of
275 * If CRC hashing is enabled, the function will compute the
276 * hash as it goes. When the entire segment has been transmitted,
277 * it will retrieve the hash value and send it as well.
280 iscsi_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
281 struct iscsi_segment *segment)
283 struct socket *sk = tcp_conn->sock;
284 unsigned int copied = 0;
287 while (!iscsi_tcp_segment_done(segment, 0, r)) {
288 struct scatterlist *sg;
289 unsigned int offset, copy;
293 offset = segment->copied;
294 copy = segment->size - offset;
296 if (segment->total_copied + segment->size < segment->total_size)
299 /* Use sendpage if we can; else fall back to sendmsg */
300 if (!segment->data) {
302 offset += segment->sg_offset + sg->offset;
303 r = tcp_conn->sendpage(sk, sg_page(sg), offset, copy,
306 struct msghdr msg = { .msg_flags = flags };
308 .iov_base = segment->data + offset,
312 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
316 iscsi_tcp_segment_unmap(segment);
317 if (copied || r == -EAGAIN)
327 * iscsi_tcp_segment_recv - copy data to segment
328 * @tcp_conn: the iSCSI TCP connection
329 * @segment: the buffer to copy to
331 * @len: amount of data available
333 * This function copies up to @len bytes to the
334 * given buffer, and returns the number of bytes
335 * consumed, which can actually be less than @len.
337 * If hash digest is enabled, the function will update the
338 * hash while copying.
339 * Combining these two operations doesn't buy us a lot (yet),
340 * but in the future we could implement combined copy+crc,
341 * just way we do for network layer checksums.
344 iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
345 struct iscsi_segment *segment, const void *ptr,
348 unsigned int copy = 0, copied = 0;
350 while (!iscsi_tcp_segment_done(segment, 1, copy)) {
352 debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n",
357 copy = min(len - copied, segment->size - segment->copied);
358 debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy);
359 memcpy(segment->data + segment->copied, ptr + copied, copy);
366 iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
367 unsigned char digest[ISCSI_DIGEST_SIZE])
369 struct scatterlist sg;
371 sg_init_one(&sg, hdr, hdrlen);
372 crypto_hash_digest(hash, &sg, hdrlen, digest);
376 iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
377 struct iscsi_segment *segment)
379 if (!segment->digest_len)
382 if (memcmp(segment->recv_digest, segment->digest,
383 segment->digest_len)) {
384 debug_scsi("digest mismatch\n");
392 * Helper function to set up segment buffer
395 __iscsi_segment_init(struct iscsi_segment *segment, size_t size,
396 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
398 memset(segment, 0, sizeof(*segment));
399 segment->total_size = size;
400 segment->done = done;
403 segment->hash = hash;
404 crypto_hash_init(hash);
409 iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
410 size_t size, iscsi_segment_done_fn_t *done,
411 struct hash_desc *hash)
413 __iscsi_segment_init(segment, size, done, hash);
414 segment->data = data;
415 segment->size = size;
419 iscsi_segment_seek_sg(struct iscsi_segment *segment,
420 struct scatterlist *sg_list, unsigned int sg_count,
421 unsigned int offset, size_t size,
422 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
424 struct scatterlist *sg;
427 debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n",
429 __iscsi_segment_init(segment, size, done, hash);
430 for_each_sg(sg_list, sg, sg_count, i) {
431 debug_scsi("sg %d, len %u offset %u\n", i, sg->length,
433 if (offset < sg->length) {
434 iscsi_tcp_segment_init_sg(segment, sg, offset);
437 offset -= sg->length;
440 return ISCSI_ERR_DATA_OFFSET;
444 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
445 * @tcp_conn: iscsi connection to prep for
447 * This function always passes NULL for the hash argument, because when this
448 * function is called we do not yet know the final size of the header and want
449 * to delay the digest processing until we know that.
452 iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
454 debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn,
455 tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : "");
456 iscsi_segment_init_linear(&tcp_conn->in.segment,
457 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
458 iscsi_tcp_hdr_recv_done, NULL);
462 * Handle incoming reply to any other type of command
465 iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
466 struct iscsi_segment *segment)
468 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
471 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
472 return ISCSI_ERR_DATA_DGST;
474 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
475 conn->data, tcp_conn->in.datalen);
479 iscsi_tcp_hdr_recv_prep(tcp_conn);
484 iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
486 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
487 struct hash_desc *rx_hash = NULL;
489 if (conn->datadgst_en &
490 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
491 rx_hash = &tcp_conn->rx_hash;
493 iscsi_segment_init_linear(&tcp_conn->in.segment,
494 conn->data, tcp_conn->in.datalen,
495 iscsi_tcp_data_recv_done, rx_hash);
499 * must be called with session lock
502 iscsi_tcp_cleanup_task(struct iscsi_conn *conn, struct iscsi_task *task)
504 struct iscsi_tcp_task *tcp_task = task->dd_data;
505 struct iscsi_r2t_info *r2t;
507 /* nothing to do for mgmt tasks */
511 /* flush task's r2t queues */
512 while (__kfifo_get(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
513 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
515 debug_scsi("iscsi_tcp_cleanup_task pending r2t dropped\n");
520 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
522 tcp_task->r2t = NULL;
527 * iscsi_data_in - SCSI Data-In Response processing
528 * @conn: iscsi connection
529 * @task: scsi command task
532 iscsi_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
534 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
535 struct iscsi_tcp_task *tcp_task = task->dd_data;
536 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
537 int datasn = be32_to_cpu(rhdr->datasn);
538 unsigned total_in_length = scsi_in(task->sc)->length;
540 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
541 if (tcp_conn->in.datalen == 0)
544 if (tcp_task->exp_datasn != datasn) {
545 debug_tcp("%s: task->exp_datasn(%d) != rhdr->datasn(%d)\n",
546 __func__, tcp_task->exp_datasn, datasn);
547 return ISCSI_ERR_DATASN;
550 tcp_task->exp_datasn++;
552 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
553 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
554 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
555 __func__, tcp_task->data_offset,
556 tcp_conn->in.datalen, total_in_length);
557 return ISCSI_ERR_DATA_OFFSET;
560 conn->datain_pdus_cnt++;
565 * iscsi_solicit_data_init - initialize first Data-Out
566 * @conn: iscsi connection
567 * @task: scsi command task
571 * Initialize first Data-Out within this R2T sequence and finds
572 * proper data_offset within this SCSI command.
574 * This function is called with connection lock taken.
577 iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_task *task,
578 struct iscsi_r2t_info *r2t)
580 struct iscsi_data *hdr;
582 hdr = &r2t->dtask.hdr;
583 memset(hdr, 0, sizeof(struct iscsi_data));
585 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
586 r2t->solicit_datasn++;
587 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
588 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
589 hdr->itt = task->hdr->itt;
590 hdr->exp_statsn = r2t->exp_statsn;
591 hdr->offset = cpu_to_be32(r2t->data_offset);
592 if (r2t->data_length > conn->max_xmit_dlength) {
593 hton24(hdr->dlength, conn->max_xmit_dlength);
594 r2t->data_count = conn->max_xmit_dlength;
597 hton24(hdr->dlength, r2t->data_length);
598 r2t->data_count = r2t->data_length;
599 hdr->flags = ISCSI_FLAG_CMD_FINAL;
601 conn->dataout_pdus_cnt++;
607 * iscsi_r2t_rsp - iSCSI R2T Response processing
608 * @conn: iscsi connection
609 * @task: scsi command task
612 iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
614 struct iscsi_r2t_info *r2t;
615 struct iscsi_session *session = conn->session;
616 struct iscsi_tcp_task *tcp_task = task->dd_data;
617 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
618 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
619 int r2tsn = be32_to_cpu(rhdr->r2tsn);
622 if (tcp_conn->in.datalen) {
623 iscsi_conn_printk(KERN_ERR, conn,
624 "invalid R2t with datalen %d\n",
625 tcp_conn->in.datalen);
626 return ISCSI_ERR_DATALEN;
629 if (tcp_task->exp_datasn != r2tsn){
630 debug_tcp("%s: task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
631 __func__, tcp_task->exp_datasn, r2tsn);
632 return ISCSI_ERR_R2TSN;
635 /* fill-in new R2T associated with the task */
636 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
638 if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
639 iscsi_conn_printk(KERN_INFO, conn,
640 "dropping R2T itt %d in recovery.\n",
645 rc = __kfifo_get(tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*));
648 r2t->exp_statsn = rhdr->statsn;
649 r2t->data_length = be32_to_cpu(rhdr->data_length);
650 if (r2t->data_length == 0) {
651 iscsi_conn_printk(KERN_ERR, conn,
652 "invalid R2T with zero data len\n");
653 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
655 return ISCSI_ERR_DATALEN;
658 if (r2t->data_length > session->max_burst)
659 debug_scsi("invalid R2T with data len %u and max burst %u."
660 "Attempting to execute request.\n",
661 r2t->data_length, session->max_burst);
663 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
664 if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) {
665 iscsi_conn_printk(KERN_ERR, conn,
666 "invalid R2T with data len %u at offset %u "
667 "and total length %d\n", r2t->data_length,
668 r2t->data_offset, scsi_out(task->sc)->length);
669 __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
671 return ISCSI_ERR_DATALEN;
674 r2t->ttt = rhdr->ttt; /* no flip */
675 r2t->solicit_datasn = 0;
677 iscsi_solicit_data_init(conn, task, r2t);
679 tcp_task->exp_datasn = r2tsn + 1;
680 __kfifo_put(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
681 conn->r2t_pdus_cnt++;
683 iscsi_requeue_task(task);
688 * Handle incoming reply to DataIn command
691 iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
692 struct iscsi_segment *segment)
694 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
695 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
698 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
699 return ISCSI_ERR_DATA_DGST;
701 /* check for non-exceptional status */
702 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
703 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
708 iscsi_tcp_hdr_recv_prep(tcp_conn);
713 * iscsi_tcp_hdr_dissect - process PDU header
714 * @conn: iSCSI connection
717 * This function analyzes the header of the PDU received,
718 * and performs several sanity checks. If the PDU is accompanied
719 * by data, the receive buffer is set up to copy the incoming data
720 * to the correct location.
723 iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
725 int rc = 0, opcode, ahslen;
726 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
727 struct iscsi_task *task;
729 /* verify PDU length */
730 tcp_conn->in.datalen = ntoh24(hdr->dlength);
731 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
732 iscsi_conn_printk(KERN_ERR, conn,
733 "iscsi_tcp: datalen %d > %d\n",
734 tcp_conn->in.datalen, conn->max_recv_dlength);
735 return ISCSI_ERR_DATALEN;
738 /* Additional header segments. So far, we don't
739 * process additional headers.
741 ahslen = hdr->hlength << 2;
743 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
744 /* verify itt (itt encoding: age+cid+itt) */
745 rc = iscsi_verify_itt(conn, hdr->itt);
749 debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
750 opcode, ahslen, tcp_conn->in.datalen);
753 case ISCSI_OP_SCSI_DATA_IN:
754 spin_lock(&conn->session->lock);
755 task = iscsi_itt_to_ctask(conn, hdr->itt);
757 rc = ISCSI_ERR_BAD_ITT;
759 rc = iscsi_data_in(conn, task);
761 spin_unlock(&conn->session->lock);
765 if (tcp_conn->in.datalen) {
766 struct iscsi_tcp_task *tcp_task = task->dd_data;
767 struct hash_desc *rx_hash = NULL;
768 struct scsi_data_buffer *sdb = scsi_in(task->sc);
771 * Setup copy of Data-In into the Scsi_Cmnd
773 * We set up the iscsi_segment to point to the next
774 * scatterlist entry to copy to. As we go along,
775 * we move on to the next scatterlist entry and
776 * update the digest per-entry.
778 if (conn->datadgst_en &&
779 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
780 rx_hash = &tcp_conn->rx_hash;
782 debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
783 "datalen=%d)\n", tcp_conn,
784 tcp_task->data_offset,
785 tcp_conn->in.datalen);
786 rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
789 tcp_task->data_offset,
790 tcp_conn->in.datalen,
791 iscsi_tcp_process_data_in,
793 spin_unlock(&conn->session->lock);
796 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
797 spin_unlock(&conn->session->lock);
799 case ISCSI_OP_SCSI_CMD_RSP:
800 if (tcp_conn->in.datalen) {
801 iscsi_tcp_data_recv_prep(tcp_conn);
804 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
807 spin_lock(&conn->session->lock);
808 task = iscsi_itt_to_ctask(conn, hdr->itt);
810 rc = ISCSI_ERR_BAD_ITT;
812 rc = ISCSI_ERR_AHSLEN;
813 else if (task->sc->sc_data_direction == DMA_TO_DEVICE)
814 rc = iscsi_r2t_rsp(conn, task);
816 rc = ISCSI_ERR_PROTO;
817 spin_unlock(&conn->session->lock);
819 case ISCSI_OP_LOGIN_RSP:
820 case ISCSI_OP_TEXT_RSP:
821 case ISCSI_OP_REJECT:
822 case ISCSI_OP_ASYNC_EVENT:
824 * It is possible that we could get a PDU with a buffer larger
825 * than 8K, but there are no targets that currently do this.
826 * For now we fail until we find a vendor that needs it
828 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
829 iscsi_conn_printk(KERN_ERR, conn,
830 "iscsi_tcp: received buffer of "
831 "len %u but conn buffer is only %u "
833 tcp_conn->in.datalen,
834 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
835 rc = ISCSI_ERR_PROTO;
839 /* If there's data coming in with the response,
840 * receive it to the connection's buffer.
842 if (tcp_conn->in.datalen) {
843 iscsi_tcp_data_recv_prep(tcp_conn);
847 case ISCSI_OP_LOGOUT_RSP:
848 case ISCSI_OP_NOOP_IN:
849 case ISCSI_OP_SCSI_TMFUNC_RSP:
850 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
853 rc = ISCSI_ERR_BAD_OPCODE;
858 /* Anything that comes with data should have
859 * been handled above. */
860 if (tcp_conn->in.datalen)
861 return ISCSI_ERR_PROTO;
862 iscsi_tcp_hdr_recv_prep(tcp_conn);
869 * iscsi_tcp_hdr_recv_done - process PDU header
871 * This is the callback invoked when the PDU header has
872 * been received. If the header is followed by additional
873 * header segments, we go back for more data.
876 iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
877 struct iscsi_segment *segment)
879 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
880 struct iscsi_hdr *hdr;
882 /* Check if there are additional header segments
883 * *prior* to computing the digest, because we
884 * may need to go back to the caller for more.
886 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
887 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
888 /* Bump the header length - the caller will
889 * just loop around and get the AHS for us, and
891 unsigned int ahslen = hdr->hlength << 2;
893 /* Make sure we don't overflow */
894 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
895 return ISCSI_ERR_AHSLEN;
897 segment->total_size += ahslen;
898 segment->size += ahslen;
902 /* We're done processing the header. See if we're doing
903 * header digests; if so, set up the recv_digest buffer
904 * and go back for more. */
905 if (conn->hdrdgst_en) {
906 if (segment->digest_len == 0) {
908 * Even if we offload the digest processing we
909 * splice it in so we can increment the skb/segment
910 * counters in preparation for the data segment.
912 iscsi_tcp_segment_splice_digest(segment,
913 segment->recv_digest);
917 if (!(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
918 iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
919 segment->total_copied - ISCSI_DIGEST_SIZE,
922 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
923 return ISCSI_ERR_HDR_DGST;
927 tcp_conn->in.hdr = hdr;
928 return iscsi_tcp_hdr_dissect(conn, hdr);
931 inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
933 return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
937 ISCSI_TCP_SEGMENT_DONE, /* curr seg has been processed */
938 ISCSI_TCP_SKB_DONE, /* skb is out of data */
939 ISCSI_TCP_CONN_ERR, /* iscsi layer has fired a conn err */
940 ISCSI_TCP_SUSPENDED, /* conn is suspended */
944 * iscsi_tcp_recv_skb - Process skb
945 * @conn: iscsi connection
946 * @skb: network buffer with header and/or data segment
947 * @offset: offset in skb
948 * @offload: bool indicating if transfer was offloaded
950 int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
951 unsigned int offset, bool offloaded, int *status)
953 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
954 struct iscsi_segment *segment = &tcp_conn->in.segment;
955 struct skb_seq_state seq;
956 unsigned int consumed = 0;
959 debug_tcp("in %d bytes\n", skb->len - offset);
961 if (unlikely(conn->suspend_rx)) {
962 debug_tcp("conn %d Rx suspended!\n", conn->id);
963 *status = ISCSI_TCP_SUSPENDED;
968 segment->total_copied = segment->total_size;
972 skb_prepare_seq_read(skb, offset, skb->len, &seq);
977 avail = skb_seq_read(consumed, &ptr, &seq);
979 debug_tcp("no more data avail. Consumed %d\n",
981 *status = ISCSI_TCP_SKB_DONE;
982 skb_abort_seq_read(&seq);
985 BUG_ON(segment->copied >= segment->size);
987 debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
988 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
992 if (segment->total_copied >= segment->total_size) {
993 skb_abort_seq_read(&seq);
999 *status = ISCSI_TCP_SEGMENT_DONE;
1000 debug_tcp("segment done\n");
1001 rc = segment->done(tcp_conn, segment);
1003 *status = ISCSI_TCP_CONN_ERR;
1004 debug_tcp("Error receiving PDU, errno=%d\n", rc);
1005 iscsi_conn_failure(conn, rc);
1008 /* The done() functions sets up the next segment. */
1011 conn->rxdata_octets += consumed;
1014 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
1017 * iscsi_tcp_recv - TCP receive in sendfile fashion
1018 * @rd_desc: read descriptor
1019 * @skb: socket buffer
1020 * @offset: offset in skb
1021 * @len: skb->len - offset
1024 iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
1025 unsigned int offset, size_t len)
1027 struct iscsi_conn *conn = rd_desc->arg.data;
1028 unsigned int consumed, total_consumed = 0;
1031 debug_tcp("in %d bytes\n", skb->len - offset);
1035 consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
1037 total_consumed += consumed;
1038 } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
1040 debug_tcp("read %d bytes status %d\n", skb->len - offset, status);
1041 return total_consumed;
1045 iscsi_tcp_data_ready(struct sock *sk, int flag)
1047 struct iscsi_conn *conn = sk->sk_user_data;
1048 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1049 read_descriptor_t rd_desc;
1051 read_lock(&sk->sk_callback_lock);
1054 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
1055 * We set count to 1 because we want the network layer to
1056 * hand us all the skbs that are available. iscsi_tcp_recv
1057 * handled pdus that cross buffers or pdus that still need data.
1059 rd_desc.arg.data = conn;
1061 tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
1063 read_unlock(&sk->sk_callback_lock);
1065 /* If we had to (atomically) map a highmem page,
1067 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
1071 iscsi_tcp_state_change(struct sock *sk)
1073 struct iscsi_tcp_conn *tcp_conn;
1074 struct iscsi_conn *conn;
1075 struct iscsi_session *session;
1076 void (*old_state_change)(struct sock *);
1078 read_lock(&sk->sk_callback_lock);
1080 conn = (struct iscsi_conn*)sk->sk_user_data;
1081 session = conn->session;
1083 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1084 sk->sk_state == TCP_CLOSE) &&
1085 !atomic_read(&sk->sk_rmem_alloc)) {
1086 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1087 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1090 tcp_conn = conn->dd_data;
1091 old_state_change = tcp_conn->old_state_change;
1093 read_unlock(&sk->sk_callback_lock);
1095 old_state_change(sk);
1099 * iscsi_write_space - Called when more output buffer space is available
1100 * @sk: socket space is available for
1103 iscsi_write_space(struct sock *sk)
1105 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
1106 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1108 tcp_conn->old_write_space(sk);
1109 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
1110 scsi_queue_work(conn->session->host, &conn->xmitwork);
1114 iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1116 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1117 struct sock *sk = tcp_conn->sock->sk;
1119 /* assign new callbacks */
1120 write_lock_bh(&sk->sk_callback_lock);
1121 sk->sk_user_data = conn;
1122 tcp_conn->old_data_ready = sk->sk_data_ready;
1123 tcp_conn->old_state_change = sk->sk_state_change;
1124 tcp_conn->old_write_space = sk->sk_write_space;
1125 sk->sk_data_ready = iscsi_tcp_data_ready;
1126 sk->sk_state_change = iscsi_tcp_state_change;
1127 sk->sk_write_space = iscsi_write_space;
1128 write_unlock_bh(&sk->sk_callback_lock);
1132 iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
1134 struct sock *sk = tcp_conn->sock->sk;
1136 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1137 write_lock_bh(&sk->sk_callback_lock);
1138 sk->sk_user_data = NULL;
1139 sk->sk_data_ready = tcp_conn->old_data_ready;
1140 sk->sk_state_change = tcp_conn->old_state_change;
1141 sk->sk_write_space = tcp_conn->old_write_space;
1142 sk->sk_no_check = 0;
1143 write_unlock_bh(&sk->sk_callback_lock);
1147 * iscsi_xmit - TCP transmit
1150 iscsi_xmit(struct iscsi_conn *conn)
1152 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1153 struct iscsi_segment *segment = &tcp_conn->out.segment;
1154 unsigned int consumed = 0;
1158 rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
1160 rc = ISCSI_ERR_XMIT_FAILED;
1168 if (segment->total_copied >= segment->total_size) {
1169 if (segment->done != NULL) {
1170 rc = segment->done(tcp_conn, segment);
1177 debug_tcp("xmit %d bytes\n", consumed);
1179 conn->txdata_octets += consumed;
1183 /* Transmit error. We could initiate error recovery
1185 debug_tcp("Error sending PDU, errno=%d\n", rc);
1186 iscsi_conn_failure(conn, rc);
1191 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
1194 iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
1196 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1197 struct iscsi_segment *segment = &tcp_conn->out.segment;
1199 return segment->total_copied - segment->total_size;
1203 iscsi_tcp_flush(struct iscsi_conn *conn)
1207 while (iscsi_tcp_xmit_qlen(conn)) {
1208 rc = iscsi_xmit(conn);
1219 * This is called when we're done sending the header.
1220 * Simply copy the data_segment to the send segment, and return.
1223 iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
1224 struct iscsi_segment *segment)
1226 tcp_conn->out.segment = tcp_conn->out.data_segment;
1227 debug_tcp("Header done. Next segment size %u total_size %u\n",
1228 tcp_conn->out.segment.size, tcp_conn->out.segment.total_size);
1233 iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen)
1235 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1237 debug_tcp("%s(%p%s)\n", __func__, tcp_conn,
1238 conn->hdrdgst_en? ", digest enabled" : "");
1240 /* Clear the data segment - needs to be filled in by the
1241 * caller using iscsi_tcp_send_data_prep() */
1242 memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment));
1244 /* If header digest is enabled, compute the CRC and
1245 * place the digest into the same buffer. We make
1246 * sure that both iscsi_tcp_task and mtask have
1249 if (conn->hdrdgst_en) {
1250 iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
1252 hdrlen += ISCSI_DIGEST_SIZE;
1255 /* Remember header pointer for later, when we need
1256 * to decide whether there's a payload to go along
1257 * with the header. */
1258 tcp_conn->out.hdr = hdr;
1260 iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen,
1261 iscsi_tcp_send_hdr_done, NULL);
1265 * Prepare the send buffer for the payload data.
1266 * Padding and checksumming will all be taken care
1267 * of by the iscsi_segment routines.
1270 iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
1271 unsigned int count, unsigned int offset,
1274 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1275 struct hash_desc *tx_hash = NULL;
1276 unsigned int hdr_spec_len;
1278 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __func__,
1279 tcp_conn, offset, len,
1280 conn->datadgst_en? ", digest enabled" : "");
1282 /* Make sure the datalen matches what the caller
1283 said he would send. */
1284 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1285 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
1287 if (conn->datadgst_en)
1288 tx_hash = &tcp_conn->tx_hash;
1290 return iscsi_segment_seek_sg(&tcp_conn->out.data_segment,
1291 sg, count, offset, len,
1296 iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
1299 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1300 struct hash_desc *tx_hash = NULL;
1301 unsigned int hdr_spec_len;
1303 debug_tcp("%s(%p, datalen=%d%s)\n", __func__, tcp_conn, len,
1304 conn->datadgst_en? ", digest enabled" : "");
1306 /* Make sure the datalen matches what the caller
1307 said he would send. */
1308 hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1309 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
1311 if (conn->datadgst_en)
1312 tx_hash = &tcp_conn->tx_hash;
1314 iscsi_segment_init_linear(&tcp_conn->out.data_segment,
1315 data, len, NULL, tx_hash);
1319 * iscsi_solicit_data_cont - initialize next Data-Out
1320 * @conn: iscsi connection
1321 * @task: scsi command task
1323 * @left: bytes left to transfer
1326 * Initialize next Data-Out within this R2T sequence and continue
1327 * to process next Scatter-Gather element(if any) of this SCSI command.
1329 * Called under connection lock.
1332 iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_task *task,
1333 struct iscsi_r2t_info *r2t)
1335 struct iscsi_data *hdr;
1336 int new_offset, left;
1338 BUG_ON(r2t->data_length - r2t->sent < 0);
1339 left = r2t->data_length - r2t->sent;
1343 hdr = &r2t->dtask.hdr;
1344 memset(hdr, 0, sizeof(struct iscsi_data));
1345 hdr->ttt = r2t->ttt;
1346 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1347 r2t->solicit_datasn++;
1348 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1349 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
1350 hdr->itt = task->hdr->itt;
1351 hdr->exp_statsn = r2t->exp_statsn;
1352 new_offset = r2t->data_offset + r2t->sent;
1353 hdr->offset = cpu_to_be32(new_offset);
1354 if (left > conn->max_xmit_dlength) {
1355 hton24(hdr->dlength, conn->max_xmit_dlength);
1356 r2t->data_count = conn->max_xmit_dlength;
1358 hton24(hdr->dlength, left);
1359 r2t->data_count = left;
1360 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1363 conn->dataout_pdus_cnt++;
1368 * iscsi_tcp_task - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
1369 * @conn: iscsi connection
1370 * @task: scsi command task
1374 iscsi_tcp_task_init(struct iscsi_task *task)
1376 struct iscsi_tcp_task *tcp_task = task->dd_data;
1377 struct iscsi_conn *conn = task->conn;
1378 struct scsi_cmnd *sc = task->sc;
1383 * mgmt tasks do not have a scatterlist since they come
1384 * in from the iscsi interface.
1386 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id,
1389 /* Prepare PDU, optionally w/ immediate data */
1390 iscsi_tcp_send_hdr_prep(conn, task->hdr, sizeof(*task->hdr));
1392 /* If we have immediate data, attach a payload */
1393 if (task->data_count)
1394 iscsi_tcp_send_linear_data_prepare(conn, task->data,
1399 BUG_ON(__kfifo_len(tcp_task->r2tqueue));
1401 tcp_task->exp_datasn = 0;
1403 /* Prepare PDU, optionally w/ immediate data */
1404 debug_scsi("task deq [cid %d itt 0x%x imm %d unsol %d]\n",
1405 conn->id, task->itt, task->imm_count,
1407 iscsi_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
1409 if (!task->imm_count)
1412 /* If we have immediate data, attach a payload */
1413 err = iscsi_tcp_send_data_prep(conn, scsi_out(sc)->table.sgl,
1414 scsi_out(sc)->table.nents,
1415 0, task->imm_count);
1418 tcp_task->sent += task->imm_count;
1419 task->imm_count = 0;
1424 * iscsi_tcp_task_xmit - xmit normal PDU task
1425 * @task: iscsi command task
1427 * We're expected to return 0 when everything was transmitted succesfully,
1428 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1432 iscsi_tcp_task_xmit(struct iscsi_task *task)
1434 struct iscsi_conn *conn = task->conn;
1435 struct iscsi_tcp_task *tcp_task = task->dd_data;
1436 struct scsi_cmnd *sc = task->sc;
1437 struct scsi_data_buffer *sdb;
1441 /* Flush any pending data first. */
1442 rc = iscsi_tcp_flush(conn);
1448 if (task->hdr->itt == RESERVED_ITT)
1449 iscsi_put_task(task);
1453 /* Are we done already? */
1454 if (sc->sc_data_direction != DMA_TO_DEVICE)
1458 if (task->unsol_count != 0) {
1459 struct iscsi_data *hdr = &tcp_task->unsol_dtask.hdr;
1461 /* Prepare a header for the unsolicited PDU.
1462 * The amount of data we want to send will be
1463 * in task->data_count.
1464 * FIXME: return the data count instead.
1466 iscsi_prep_unsolicit_data_pdu(task, hdr);
1468 debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n",
1469 task->itt, tcp_task->sent, task->data_count);
1471 iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr));
1472 rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
1473 sdb->table.nents, tcp_task->sent,
1477 tcp_task->sent += task->data_count;
1478 task->unsol_count -= task->data_count;
1481 struct iscsi_session *session = conn->session;
1482 struct iscsi_r2t_info *r2t;
1484 /* All unsolicited PDUs sent. Check for solicited PDUs.
1486 spin_lock_bh(&session->lock);
1487 r2t = tcp_task->r2t;
1489 /* Continue with this R2T? */
1490 if (!iscsi_solicit_data_cont(conn, task, r2t)) {
1491 debug_scsi(" done with r2t %p\n", r2t);
1493 __kfifo_put(tcp_task->r2tpool.queue,
1494 (void*)&r2t, sizeof(void*));
1495 tcp_task->r2t = r2t = NULL;
1500 __kfifo_get(tcp_task->r2tqueue, (void*)&tcp_task->r2t,
1502 r2t = tcp_task->r2t;
1504 spin_unlock_bh(&session->lock);
1506 /* Waiting for more R2Ts to arrive. */
1508 debug_tcp("no R2Ts yet\n");
1512 debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1513 r2t, r2t->solicit_datasn - 1, task->itt,
1514 r2t->data_offset + r2t->sent, r2t->data_count);
1516 iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr,
1517 sizeof(struct iscsi_hdr));
1519 rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
1521 r2t->data_offset + r2t->sent,
1525 tcp_task->sent += r2t->data_count;
1526 r2t->sent += r2t->data_count;
1531 iscsi_conn_failure(conn, rc);
1535 static struct iscsi_cls_conn *
1536 iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1538 struct iscsi_conn *conn;
1539 struct iscsi_cls_conn *cls_conn;
1540 struct iscsi_tcp_conn *tcp_conn;
1542 cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx);
1545 conn = cls_conn->dd_data;
1547 * due to strange issues with iser these are not set
1548 * in iscsi_conn_setup
1550 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1552 tcp_conn = conn->dd_data;
1553 tcp_conn->iscsi_conn = conn;
1555 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1557 tcp_conn->tx_hash.flags = 0;
1558 if (IS_ERR(tcp_conn->tx_hash.tfm))
1561 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1563 tcp_conn->rx_hash.flags = 0;
1564 if (IS_ERR(tcp_conn->rx_hash.tfm))
1570 crypto_free_hash(tcp_conn->tx_hash.tfm);
1572 iscsi_conn_printk(KERN_ERR, conn,
1573 "Could not create connection due to crc32c "
1574 "loading error. Make sure the crc32c "
1575 "module is built as a module or into the "
1577 iscsi_conn_teardown(cls_conn);
1582 iscsi_tcp_release_conn(struct iscsi_conn *conn)
1584 struct iscsi_session *session = conn->session;
1585 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1586 struct socket *sock = tcp_conn->sock;
1591 sock_hold(sock->sk);
1592 iscsi_conn_restore_callbacks(tcp_conn);
1595 spin_lock_bh(&session->lock);
1596 tcp_conn->sock = NULL;
1597 spin_unlock_bh(&session->lock);
1602 iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
1604 struct iscsi_conn *conn = cls_conn->dd_data;
1605 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1607 iscsi_tcp_release_conn(conn);
1609 if (tcp_conn->tx_hash.tfm)
1610 crypto_free_hash(tcp_conn->tx_hash.tfm);
1611 if (tcp_conn->rx_hash.tfm)
1612 crypto_free_hash(tcp_conn->rx_hash.tfm);
1614 iscsi_conn_teardown(cls_conn);
1618 iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1620 struct iscsi_conn *conn = cls_conn->dd_data;
1621 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1623 /* userspace may have goofed up and not bound us */
1624 if (!tcp_conn->sock)
1627 * Make sure our recv side is stopped.
1628 * Older tools called conn stop before ep_disconnect
1629 * so IO could still be coming in.
1631 write_lock_bh(&tcp_conn->sock->sk->sk_callback_lock);
1632 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1633 write_unlock_bh(&tcp_conn->sock->sk->sk_callback_lock);
1635 iscsi_conn_stop(cls_conn, flag);
1636 iscsi_tcp_release_conn(conn);
1639 static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
1640 char *buf, int *port,
1641 int (*getname)(struct socket *, struct sockaddr *,
1644 struct sockaddr_storage *addr;
1645 struct sockaddr_in6 *sin6;
1646 struct sockaddr_in *sin;
1649 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
1653 if (getname(sock, (struct sockaddr *) addr, &len)) {
1658 switch (addr->ss_family) {
1660 sin = (struct sockaddr_in *)addr;
1661 spin_lock_bh(&conn->session->lock);
1662 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
1663 *port = be16_to_cpu(sin->sin_port);
1664 spin_unlock_bh(&conn->session->lock);
1667 sin6 = (struct sockaddr_in6 *)addr;
1668 spin_lock_bh(&conn->session->lock);
1669 sprintf(buf, "%pI6", &sin6->sin6_addr);
1670 *port = be16_to_cpu(sin6->sin6_port);
1671 spin_unlock_bh(&conn->session->lock);
1680 iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
1681 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
1684 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1685 struct iscsi_host *ihost = shost_priv(shost);
1686 struct iscsi_conn *conn = cls_conn->dd_data;
1687 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1689 struct socket *sock;
1692 /* lookup for existing socket */
1693 sock = sockfd_lookup((int)transport_eph, &err);
1695 iscsi_conn_printk(KERN_ERR, conn,
1696 "sockfd_lookup failed %d\n", err);
1700 * copy these values now because if we drop the session
1701 * userspace may still want to query the values since we will
1702 * be using them for the reconnect
1704 err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1705 &conn->portal_port, kernel_getpeername);
1709 err = iscsi_tcp_get_addr(conn, sock, ihost->local_address,
1710 &ihost->local_port, kernel_getsockname);
1714 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1718 /* bind iSCSI connection and socket */
1719 tcp_conn->sock = sock;
1721 /* setup Socket parameters */
1724 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1725 sk->sk_allocation = GFP_ATOMIC;
1727 iscsi_conn_set_callbacks(conn);
1728 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1730 * set receive state machine into initial state
1732 iscsi_tcp_hdr_recv_prep(tcp_conn);
1741 iscsi_r2tpool_alloc(struct iscsi_session *session)
1747 * initialize per-task: R2T pool and xmit queue
1749 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1750 struct iscsi_task *task = session->cmds[cmd_i];
1751 struct iscsi_tcp_task *tcp_task = task->dd_data;
1754 * pre-allocated x4 as much r2ts to handle race when
1755 * target acks DataOut faster than we data_xmit() queues
1756 * could replenish r2tqueue.
1760 if (iscsi_pool_init(&tcp_task->r2tpool, session->max_r2t * 4, NULL,
1761 sizeof(struct iscsi_r2t_info))) {
1762 goto r2t_alloc_fail;
1765 /* R2T xmit queue */
1766 tcp_task->r2tqueue = kfifo_alloc(
1767 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
1768 if (tcp_task->r2tqueue == ERR_PTR(-ENOMEM)) {
1769 iscsi_pool_free(&tcp_task->r2tpool);
1770 goto r2t_alloc_fail;
1777 for (i = 0; i < cmd_i; i++) {
1778 struct iscsi_task *task = session->cmds[i];
1779 struct iscsi_tcp_task *tcp_task = task->dd_data;
1781 kfifo_free(tcp_task->r2tqueue);
1782 iscsi_pool_free(&tcp_task->r2tpool);
1788 iscsi_r2tpool_free(struct iscsi_session *session)
1792 for (i = 0; i < session->cmds_max; i++) {
1793 struct iscsi_task *task = session->cmds[i];
1794 struct iscsi_tcp_task *tcp_task = task->dd_data;
1796 kfifo_free(tcp_task->r2tqueue);
1797 iscsi_pool_free(&tcp_task->r2tpool);
1802 iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
1803 char *buf, int buflen)
1805 struct iscsi_conn *conn = cls_conn->dd_data;
1806 struct iscsi_session *session = conn->session;
1807 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1811 case ISCSI_PARAM_HDRDGST_EN:
1812 iscsi_set_param(cls_conn, param, buf, buflen);
1814 case ISCSI_PARAM_DATADGST_EN:
1815 iscsi_set_param(cls_conn, param, buf, buflen);
1816 tcp_conn->sendpage = conn->datadgst_en ?
1817 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
1819 case ISCSI_PARAM_MAX_R2T:
1820 sscanf(buf, "%d", &value);
1821 if (value <= 0 || !is_power_of_2(value))
1823 if (session->max_r2t == value)
1825 iscsi_r2tpool_free(session);
1826 iscsi_set_param(cls_conn, param, buf, buflen);
1827 if (iscsi_r2tpool_alloc(session))
1831 return iscsi_set_param(cls_conn, param, buf, buflen);
1838 iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
1839 enum iscsi_param param, char *buf)
1841 struct iscsi_conn *conn = cls_conn->dd_data;
1845 case ISCSI_PARAM_CONN_PORT:
1846 spin_lock_bh(&conn->session->lock);
1847 len = sprintf(buf, "%hu\n", conn->portal_port);
1848 spin_unlock_bh(&conn->session->lock);
1850 case ISCSI_PARAM_CONN_ADDRESS:
1851 spin_lock_bh(&conn->session->lock);
1852 len = sprintf(buf, "%s\n", conn->portal_address);
1853 spin_unlock_bh(&conn->session->lock);
1856 return iscsi_conn_get_param(cls_conn, param, buf);
1863 iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
1865 struct iscsi_conn *conn = cls_conn->dd_data;
1866 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1868 stats->txdata_octets = conn->txdata_octets;
1869 stats->rxdata_octets = conn->rxdata_octets;
1870 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1871 stats->dataout_pdus = conn->dataout_pdus_cnt;
1872 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1873 stats->datain_pdus = conn->datain_pdus_cnt;
1874 stats->r2t_pdus = conn->r2t_pdus_cnt;
1875 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1876 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1877 stats->custom_length = 3;
1878 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
1879 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
1880 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
1881 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
1882 strcpy(stats->custom[2].desc, "eh_abort_cnt");
1883 stats->custom[2].value = conn->eh_abort_cnt;
1886 static struct iscsi_cls_session *
1887 iscsi_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
1888 uint16_t qdepth, uint32_t initial_cmdsn,
1891 struct iscsi_cls_session *cls_session;
1892 struct iscsi_session *session;
1893 struct Scsi_Host *shost;
1897 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
1901 shost = iscsi_host_alloc(&iscsi_sht, 0, qdepth);
1904 shost->transportt = iscsi_tcp_scsi_transport;
1905 shost->max_lun = iscsi_max_lun;
1907 shost->max_channel = 0;
1908 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
1910 if (iscsi_host_add(shost, NULL))
1912 *hostno = shost->host_no;
1914 cls_session = iscsi_session_setup(&iscsi_tcp_transport, shost, cmds_max,
1915 sizeof(struct iscsi_tcp_task),
1919 session = cls_session->dd_data;
1921 shost->can_queue = session->scsi_cmds_max;
1922 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1923 struct iscsi_task *task = session->cmds[cmd_i];
1924 struct iscsi_tcp_task *tcp_task = task->dd_data;
1926 task->hdr = &tcp_task->hdr.cmd_hdr;
1927 task->hdr_max = sizeof(tcp_task->hdr) - ISCSI_DIGEST_SIZE;
1930 if (iscsi_r2tpool_alloc(session))
1931 goto remove_session;
1935 iscsi_session_teardown(cls_session);
1937 iscsi_host_remove(shost);
1939 iscsi_host_free(shost);
1943 static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
1945 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1947 iscsi_r2tpool_free(cls_session->dd_data);
1948 iscsi_session_teardown(cls_session);
1950 iscsi_host_remove(shost);
1951 iscsi_host_free(shost);
1954 static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
1956 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
1957 blk_queue_dma_alignment(sdev->request_queue, 0);
1961 static struct scsi_host_template iscsi_sht = {
1962 .module = THIS_MODULE,
1963 .name = "iSCSI Initiator over TCP/IP",
1964 .queuecommand = iscsi_queuecommand,
1965 .change_queue_depth = iscsi_change_queue_depth,
1966 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
1967 .sg_tablesize = 4096,
1968 .max_sectors = 0xFFFF,
1969 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
1970 .eh_abort_handler = iscsi_eh_abort,
1971 .eh_device_reset_handler= iscsi_eh_device_reset,
1972 .eh_target_reset_handler= iscsi_eh_target_reset,
1973 .use_clustering = DISABLE_CLUSTERING,
1974 .slave_configure = iscsi_tcp_slave_configure,
1975 .proc_name = "iscsi_tcp",
1979 static struct iscsi_transport iscsi_tcp_transport = {
1980 .owner = THIS_MODULE,
1982 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
1984 .param_mask = ISCSI_MAX_RECV_DLENGTH |
1985 ISCSI_MAX_XMIT_DLENGTH |
1988 ISCSI_INITIAL_R2T_EN |
1993 ISCSI_PDU_INORDER_EN |
1994 ISCSI_DATASEQ_INORDER_EN |
1997 ISCSI_CONN_ADDRESS |
1999 ISCSI_PERSISTENT_PORT |
2000 ISCSI_PERSISTENT_ADDRESS |
2001 ISCSI_TARGET_NAME | ISCSI_TPGT |
2002 ISCSI_USERNAME | ISCSI_PASSWORD |
2003 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
2004 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
2005 ISCSI_LU_RESET_TMO |
2006 ISCSI_PING_TMO | ISCSI_RECV_TMO |
2007 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
2008 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
2009 ISCSI_HOST_INITIATOR_NAME |
2010 ISCSI_HOST_NETDEV_NAME,
2011 /* session management */
2012 .create_session = iscsi_tcp_session_create,
2013 .destroy_session = iscsi_tcp_session_destroy,
2014 /* connection management */
2015 .create_conn = iscsi_tcp_conn_create,
2016 .bind_conn = iscsi_tcp_conn_bind,
2017 .destroy_conn = iscsi_tcp_conn_destroy,
2018 .set_param = iscsi_conn_set_param,
2019 .get_conn_param = iscsi_tcp_conn_get_param,
2020 .get_session_param = iscsi_session_get_param,
2021 .start_conn = iscsi_conn_start,
2022 .stop_conn = iscsi_tcp_conn_stop,
2023 /* iscsi host params */
2024 .get_host_param = iscsi_host_get_param,
2025 .set_host_param = iscsi_host_set_param,
2027 .send_pdu = iscsi_conn_send_pdu,
2028 .get_stats = iscsi_conn_get_stats,
2029 .init_task = iscsi_tcp_task_init,
2030 .xmit_task = iscsi_tcp_task_xmit,
2031 .cleanup_task = iscsi_tcp_cleanup_task,
2033 .session_recovery_timedout = iscsi_session_recovery_timedout,
2037 iscsi_tcp_init(void)
2039 if (iscsi_max_lun < 1) {
2040 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2045 iscsi_tcp_scsi_transport = iscsi_register_transport(
2046 &iscsi_tcp_transport);
2047 if (!iscsi_tcp_scsi_transport)
2054 iscsi_tcp_exit(void)
2056 iscsi_unregister_transport(&iscsi_tcp_transport);
2059 module_init(iscsi_tcp_init);
2060 module_exit(iscsi_tcp_exit);