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/inet.h>
31 #include <linux/file.h>
32 #include <linux/blkdev.h>
33 #include <linux/crypto.h>
34 #include <linux/delay.h>
35 #include <linux/kfifo.h>
36 #include <linux/scatterlist.h>
38 #include <scsi/scsi_cmnd.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_host.h>
41 #include <scsi/scsi.h>
42 #include <scsi/scsi_transport_iscsi.h>
44 #include "iscsi_tcp.h"
46 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
47 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
48 "Alex Aizman <itn780@yahoo.com>");
49 MODULE_DESCRIPTION("iSCSI/TCP data-path");
50 MODULE_LICENSE("GPL");
54 #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
56 #define debug_tcp(fmt...)
59 static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
60 static struct scsi_host_template iscsi_sw_tcp_sht;
61 static struct iscsi_transport iscsi_sw_tcp_transport;
63 static unsigned int iscsi_max_lun = 512;
64 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
67 * iscsi_sw_tcp_recv - TCP receive in sendfile fashion
68 * @rd_desc: read descriptor
70 * @offset: offset in skb
71 * @len: skb->len - offset
73 static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
74 unsigned int offset, size_t len)
76 struct iscsi_conn *conn = rd_desc->arg.data;
77 unsigned int consumed, total_consumed = 0;
80 debug_tcp("in %d bytes\n", skb->len - offset);
84 consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
86 total_consumed += consumed;
87 } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
89 debug_tcp("read %d bytes status %d\n", skb->len - offset, status);
90 return total_consumed;
93 static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag)
95 struct iscsi_conn *conn = sk->sk_user_data;
96 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
97 read_descriptor_t rd_desc;
99 read_lock(&sk->sk_callback_lock);
102 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
103 * We set count to 1 because we want the network layer to
104 * hand us all the skbs that are available. iscsi_tcp_recv
105 * handled pdus that cross buffers or pdus that still need data.
107 rd_desc.arg.data = conn;
109 tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
111 read_unlock(&sk->sk_callback_lock);
113 /* If we had to (atomically) map a highmem page,
115 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
118 static void iscsi_sw_tcp_state_change(struct sock *sk)
120 struct iscsi_tcp_conn *tcp_conn;
121 struct iscsi_sw_tcp_conn *tcp_sw_conn;
122 struct iscsi_conn *conn;
123 struct iscsi_session *session;
124 void (*old_state_change)(struct sock *);
126 read_lock(&sk->sk_callback_lock);
128 conn = (struct iscsi_conn*)sk->sk_user_data;
129 session = conn->session;
131 if ((sk->sk_state == TCP_CLOSE_WAIT ||
132 sk->sk_state == TCP_CLOSE) &&
133 !atomic_read(&sk->sk_rmem_alloc)) {
134 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
135 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
138 tcp_conn = conn->dd_data;
139 tcp_sw_conn = tcp_conn->dd_data;
140 old_state_change = tcp_sw_conn->old_state_change;
142 read_unlock(&sk->sk_callback_lock);
144 old_state_change(sk);
148 * iscsi_write_space - Called when more output buffer space is available
149 * @sk: socket space is available for
151 static void iscsi_sw_tcp_write_space(struct sock *sk)
153 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
154 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
155 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
157 tcp_sw_conn->old_write_space(sk);
158 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
159 scsi_queue_work(conn->session->host, &conn->xmitwork);
162 static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
164 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
165 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
166 struct sock *sk = tcp_sw_conn->sock->sk;
168 /* assign new callbacks */
169 write_lock_bh(&sk->sk_callback_lock);
170 sk->sk_user_data = conn;
171 tcp_sw_conn->old_data_ready = sk->sk_data_ready;
172 tcp_sw_conn->old_state_change = sk->sk_state_change;
173 tcp_sw_conn->old_write_space = sk->sk_write_space;
174 sk->sk_data_ready = iscsi_sw_tcp_data_ready;
175 sk->sk_state_change = iscsi_sw_tcp_state_change;
176 sk->sk_write_space = iscsi_sw_tcp_write_space;
177 write_unlock_bh(&sk->sk_callback_lock);
181 iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_sw_tcp_conn *tcp_sw_conn)
183 struct sock *sk = tcp_sw_conn->sock->sk;
185 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
186 write_lock_bh(&sk->sk_callback_lock);
187 sk->sk_user_data = NULL;
188 sk->sk_data_ready = tcp_sw_conn->old_data_ready;
189 sk->sk_state_change = tcp_sw_conn->old_state_change;
190 sk->sk_write_space = tcp_sw_conn->old_write_space;
192 write_unlock_bh(&sk->sk_callback_lock);
196 * iscsi_sw_tcp_xmit_segment - transmit segment
197 * @tcp_sw_conn: the iSCSI TCP connection
198 * @segment: the buffer to transmnit
200 * This function transmits as much of the buffer as
201 * the network layer will accept, and returns the number of
204 * If CRC hashing is enabled, the function will compute the
205 * hash as it goes. When the entire segment has been transmitted,
206 * it will retrieve the hash value and send it as well.
208 static int iscsi_sw_tcp_xmit_segment(struct iscsi_sw_tcp_conn *tcp_sw_conn,
209 struct iscsi_segment *segment)
211 struct socket *sk = tcp_sw_conn->sock;
212 unsigned int copied = 0;
215 while (!iscsi_tcp_segment_done(segment, 0, r)) {
216 struct scatterlist *sg;
217 unsigned int offset, copy;
221 offset = segment->copied;
222 copy = segment->size - offset;
224 if (segment->total_copied + segment->size < segment->total_size)
227 /* Use sendpage if we can; else fall back to sendmsg */
228 if (!segment->data) {
230 offset += segment->sg_offset + sg->offset;
231 r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
234 struct msghdr msg = { .msg_flags = flags };
236 .iov_base = segment->data + offset,
240 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
244 iscsi_tcp_segment_unmap(segment);
245 if (copied || r == -EAGAIN)
255 * iscsi_sw_tcp_xmit - TCP transmit
257 static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
259 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
260 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
261 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
262 unsigned int consumed = 0;
266 rc = iscsi_sw_tcp_xmit_segment(tcp_sw_conn, segment);
268 rc = ISCSI_ERR_XMIT_FAILED;
276 if (segment->total_copied >= segment->total_size) {
277 if (segment->done != NULL) {
278 rc = segment->done(tcp_conn, segment);
285 debug_tcp("xmit %d bytes\n", consumed);
287 conn->txdata_octets += consumed;
291 /* Transmit error. We could initiate error recovery
293 debug_tcp("Error sending PDU, errno=%d\n", rc);
294 iscsi_conn_failure(conn, rc);
299 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
301 static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
303 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
304 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
305 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
307 return segment->total_copied - segment->total_size;
310 static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
312 struct iscsi_conn *conn = task->conn;
315 while (iscsi_sw_tcp_xmit_qlen(conn)) {
316 rc = iscsi_sw_tcp_xmit(conn);
327 * This is called when we're done sending the header.
328 * Simply copy the data_segment to the send segment, and return.
330 static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
331 struct iscsi_segment *segment)
333 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
335 tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
336 debug_tcp("Header done. Next segment size %u total_size %u\n",
337 tcp_sw_conn->out.segment.size,
338 tcp_sw_conn->out.segment.total_size);
342 static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
345 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
346 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
348 debug_tcp("%s(%p%s)\n", __func__, tcp_conn,
349 conn->hdrdgst_en? ", digest enabled" : "");
351 /* Clear the data segment - needs to be filled in by the
352 * caller using iscsi_tcp_send_data_prep() */
353 memset(&tcp_sw_conn->out.data_segment, 0,
354 sizeof(struct iscsi_segment));
356 /* If header digest is enabled, compute the CRC and
357 * place the digest into the same buffer. We make
358 * sure that both iscsi_tcp_task and mtask have
361 if (conn->hdrdgst_en) {
362 iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
364 hdrlen += ISCSI_DIGEST_SIZE;
367 /* Remember header pointer for later, when we need
368 * to decide whether there's a payload to go along
369 * with the header. */
370 tcp_sw_conn->out.hdr = hdr;
372 iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
373 iscsi_sw_tcp_send_hdr_done, NULL);
377 * Prepare the send buffer for the payload data.
378 * Padding and checksumming will all be taken care
379 * of by the iscsi_segment routines.
382 iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
383 unsigned int count, unsigned int offset,
386 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
387 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
388 struct hash_desc *tx_hash = NULL;
389 unsigned int hdr_spec_len;
391 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __func__,
392 tcp_conn, offset, len,
393 conn->datadgst_en? ", digest enabled" : "");
395 /* Make sure the datalen matches what the caller
396 said he would send. */
397 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
398 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
400 if (conn->datadgst_en)
401 tx_hash = &tcp_sw_conn->tx_hash;
403 return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
404 sg, count, offset, len,
409 iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
412 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
413 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
414 struct hash_desc *tx_hash = NULL;
415 unsigned int hdr_spec_len;
417 debug_tcp("%s(%p, datalen=%d%s)\n", __func__, tcp_conn, len,
418 conn->datadgst_en? ", digest enabled" : "");
420 /* Make sure the datalen matches what the caller
421 said he would send. */
422 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
423 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
425 if (conn->datadgst_en)
426 tx_hash = &tcp_sw_conn->tx_hash;
428 iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
429 data, len, NULL, tx_hash);
432 static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
433 unsigned int offset, unsigned int count)
435 struct iscsi_conn *conn = task->conn;
438 iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
444 iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
446 struct scsi_data_buffer *sdb = scsi_out(task->sc);
448 err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
449 sdb->table.nents, offset,
454 iscsi_conn_failure(conn, err);
460 static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task)
462 struct iscsi_tcp_task *tcp_task = task->dd_data;
464 task->hdr = task->dd_data + sizeof(*tcp_task);
465 task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
469 static struct iscsi_cls_conn *
470 iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
473 struct iscsi_conn *conn;
474 struct iscsi_cls_conn *cls_conn;
475 struct iscsi_tcp_conn *tcp_conn;
476 struct iscsi_sw_tcp_conn *tcp_sw_conn;
478 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
482 conn = cls_conn->dd_data;
483 tcp_conn = conn->dd_data;
484 tcp_sw_conn = tcp_conn->dd_data;
486 tcp_sw_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
488 tcp_sw_conn->tx_hash.flags = 0;
489 if (IS_ERR(tcp_sw_conn->tx_hash.tfm))
492 tcp_sw_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
494 tcp_sw_conn->rx_hash.flags = 0;
495 if (IS_ERR(tcp_sw_conn->rx_hash.tfm))
497 tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
502 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
504 iscsi_conn_printk(KERN_ERR, conn,
505 "Could not create connection due to crc32c "
506 "loading error. Make sure the crc32c "
507 "module is built as a module or into the "
509 iscsi_tcp_conn_teardown(cls_conn);
513 static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
515 struct iscsi_session *session = conn->session;
516 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
517 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
518 struct socket *sock = tcp_sw_conn->sock;
524 iscsi_sw_tcp_conn_restore_callbacks(tcp_sw_conn);
527 spin_lock_bh(&session->lock);
528 tcp_sw_conn->sock = NULL;
529 spin_unlock_bh(&session->lock);
533 static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
535 struct iscsi_conn *conn = cls_conn->dd_data;
536 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
537 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
539 iscsi_sw_tcp_release_conn(conn);
541 if (tcp_sw_conn->tx_hash.tfm)
542 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
543 if (tcp_sw_conn->rx_hash.tfm)
544 crypto_free_hash(tcp_sw_conn->rx_hash.tfm);
546 iscsi_tcp_conn_teardown(cls_conn);
549 static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
551 struct iscsi_conn *conn = cls_conn->dd_data;
552 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
553 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
555 /* userspace may have goofed up and not bound us */
556 if (!tcp_sw_conn->sock)
559 * Make sure our recv side is stopped.
560 * Older tools called conn stop before ep_disconnect
561 * so IO could still be coming in.
563 write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
564 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
565 write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
567 iscsi_conn_stop(cls_conn, flag);
568 iscsi_sw_tcp_release_conn(conn);
571 static int iscsi_sw_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
572 char *buf, int *port,
573 int (*getname)(struct socket *,
577 struct sockaddr_storage *addr;
578 struct sockaddr_in6 *sin6;
579 struct sockaddr_in *sin;
582 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
586 if (getname(sock, (struct sockaddr *) addr, &len)) {
591 switch (addr->ss_family) {
593 sin = (struct sockaddr_in *)addr;
594 spin_lock_bh(&conn->session->lock);
595 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
596 *port = be16_to_cpu(sin->sin_port);
597 spin_unlock_bh(&conn->session->lock);
600 sin6 = (struct sockaddr_in6 *)addr;
601 spin_lock_bh(&conn->session->lock);
602 sprintf(buf, "%pI6", &sin6->sin6_addr);
603 *port = be16_to_cpu(sin6->sin6_port);
604 spin_unlock_bh(&conn->session->lock);
613 iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
614 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
617 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
618 struct iscsi_host *ihost = shost_priv(shost);
619 struct iscsi_conn *conn = cls_conn->dd_data;
620 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
621 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
626 /* lookup for existing socket */
627 sock = sockfd_lookup((int)transport_eph, &err);
629 iscsi_conn_printk(KERN_ERR, conn,
630 "sockfd_lookup failed %d\n", err);
634 * copy these values now because if we drop the session
635 * userspace may still want to query the values since we will
636 * be using them for the reconnect
638 err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address,
639 &conn->portal_port, kernel_getpeername);
643 err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address,
644 &ihost->local_port, kernel_getsockname);
648 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
652 /* bind iSCSI connection and socket */
653 tcp_sw_conn->sock = sock;
655 /* setup Socket parameters */
658 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
659 sk->sk_allocation = GFP_ATOMIC;
661 iscsi_sw_tcp_conn_set_callbacks(conn);
662 tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
664 * set receive state machine into initial state
666 iscsi_tcp_hdr_recv_prep(tcp_conn);
674 static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
675 enum iscsi_param param, char *buf,
678 struct iscsi_conn *conn = cls_conn->dd_data;
679 struct iscsi_session *session = conn->session;
680 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
681 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
685 case ISCSI_PARAM_HDRDGST_EN:
686 iscsi_set_param(cls_conn, param, buf, buflen);
688 case ISCSI_PARAM_DATADGST_EN:
689 iscsi_set_param(cls_conn, param, buf, buflen);
690 tcp_sw_conn->sendpage = conn->datadgst_en ?
691 sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
693 case ISCSI_PARAM_MAX_R2T:
694 sscanf(buf, "%d", &value);
695 if (value <= 0 || !is_power_of_2(value))
697 if (session->max_r2t == value)
699 iscsi_tcp_r2tpool_free(session);
700 iscsi_set_param(cls_conn, param, buf, buflen);
701 if (iscsi_tcp_r2tpool_alloc(session))
705 return iscsi_set_param(cls_conn, param, buf, buflen);
711 static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
712 enum iscsi_param param, char *buf)
714 struct iscsi_conn *conn = cls_conn->dd_data;
718 case ISCSI_PARAM_CONN_PORT:
719 spin_lock_bh(&conn->session->lock);
720 len = sprintf(buf, "%hu\n", conn->portal_port);
721 spin_unlock_bh(&conn->session->lock);
723 case ISCSI_PARAM_CONN_ADDRESS:
724 spin_lock_bh(&conn->session->lock);
725 len = sprintf(buf, "%s\n", conn->portal_address);
726 spin_unlock_bh(&conn->session->lock);
729 return iscsi_conn_get_param(cls_conn, param, buf);
736 iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
737 struct iscsi_stats *stats)
739 struct iscsi_conn *conn = cls_conn->dd_data;
740 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
741 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
743 stats->custom_length = 3;
744 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
745 stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
746 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
747 stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
748 strcpy(stats->custom[2].desc, "eh_abort_cnt");
749 stats->custom[2].value = conn->eh_abort_cnt;
751 iscsi_tcp_conn_get_stats(cls_conn, stats);
754 static struct iscsi_cls_session *
755 iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
756 uint16_t qdepth, uint32_t initial_cmdsn,
759 struct iscsi_cls_session *cls_session;
760 struct iscsi_session *session;
761 struct Scsi_Host *shost;
764 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
768 shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, qdepth);
771 shost->transportt = iscsi_sw_tcp_scsi_transport;
772 shost->max_lun = iscsi_max_lun;
774 shost->max_channel = 0;
775 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
777 if (iscsi_host_add(shost, NULL))
779 *hostno = shost->host_no;
781 cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
783 sizeof(struct iscsi_tcp_task) +
784 sizeof(struct iscsi_sw_tcp_hdrbuf),
788 session = cls_session->dd_data;
790 shost->can_queue = session->scsi_cmds_max;
791 if (iscsi_tcp_r2tpool_alloc(session))
796 iscsi_session_teardown(cls_session);
798 iscsi_host_remove(shost);
800 iscsi_host_free(shost);
804 static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
806 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
808 iscsi_tcp_r2tpool_free(cls_session->dd_data);
809 iscsi_session_teardown(cls_session);
811 iscsi_host_remove(shost);
812 iscsi_host_free(shost);
815 static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
817 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
818 blk_queue_dma_alignment(sdev->request_queue, 0);
822 static struct scsi_host_template iscsi_sw_tcp_sht = {
823 .module = THIS_MODULE,
824 .name = "iSCSI Initiator over TCP/IP",
825 .queuecommand = iscsi_queuecommand,
826 .change_queue_depth = iscsi_change_queue_depth,
827 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
828 .sg_tablesize = 4096,
829 .max_sectors = 0xFFFF,
830 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
831 .eh_abort_handler = iscsi_eh_abort,
832 .eh_device_reset_handler= iscsi_eh_device_reset,
833 .eh_target_reset_handler= iscsi_eh_target_reset,
834 .use_clustering = DISABLE_CLUSTERING,
835 .slave_configure = iscsi_sw_tcp_slave_configure,
836 .proc_name = "iscsi_tcp",
840 static struct iscsi_transport iscsi_sw_tcp_transport = {
841 .owner = THIS_MODULE,
843 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
845 .param_mask = ISCSI_MAX_RECV_DLENGTH |
846 ISCSI_MAX_XMIT_DLENGTH |
849 ISCSI_INITIAL_R2T_EN |
854 ISCSI_PDU_INORDER_EN |
855 ISCSI_DATASEQ_INORDER_EN |
860 ISCSI_PERSISTENT_PORT |
861 ISCSI_PERSISTENT_ADDRESS |
862 ISCSI_TARGET_NAME | ISCSI_TPGT |
863 ISCSI_USERNAME | ISCSI_PASSWORD |
864 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
865 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
867 ISCSI_PING_TMO | ISCSI_RECV_TMO |
868 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
869 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
870 ISCSI_HOST_INITIATOR_NAME |
871 ISCSI_HOST_NETDEV_NAME,
872 /* session management */
873 .create_session = iscsi_sw_tcp_session_create,
874 .destroy_session = iscsi_sw_tcp_session_destroy,
875 /* connection management */
876 .create_conn = iscsi_sw_tcp_conn_create,
877 .bind_conn = iscsi_sw_tcp_conn_bind,
878 .destroy_conn = iscsi_sw_tcp_conn_destroy,
879 .set_param = iscsi_sw_tcp_conn_set_param,
880 .get_conn_param = iscsi_sw_tcp_conn_get_param,
881 .get_session_param = iscsi_session_get_param,
882 .start_conn = iscsi_conn_start,
883 .stop_conn = iscsi_sw_tcp_conn_stop,
884 /* iscsi host params */
885 .get_host_param = iscsi_host_get_param,
886 .set_host_param = iscsi_host_set_param,
888 .send_pdu = iscsi_conn_send_pdu,
889 .get_stats = iscsi_sw_tcp_conn_get_stats,
890 /* iscsi task/cmd helpers */
891 .init_task = iscsi_tcp_task_init,
892 .xmit_task = iscsi_tcp_task_xmit,
893 .cleanup_task = iscsi_tcp_cleanup_task,
894 /* low level pdu helpers */
895 .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
896 .init_pdu = iscsi_sw_tcp_pdu_init,
897 .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
899 .session_recovery_timedout = iscsi_session_recovery_timedout,
902 static int __init iscsi_sw_tcp_init(void)
904 if (iscsi_max_lun < 1) {
905 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
910 iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
911 &iscsi_sw_tcp_transport);
912 if (!iscsi_sw_tcp_scsi_transport)
918 static void __exit iscsi_sw_tcp_exit(void)
920 iscsi_unregister_transport(&iscsi_sw_tcp_transport);
923 module_init(iscsi_sw_tcp_init);
924 module_exit(iscsi_sw_tcp_exit);