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");
52 static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
53 static struct scsi_host_template iscsi_sw_tcp_sht;
54 static struct iscsi_transport iscsi_sw_tcp_transport;
56 static unsigned int iscsi_max_lun = 512;
57 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
59 static int iscsi_sw_tcp_dbg;
60 module_param_named(debug_iscsi_tcp, iscsi_sw_tcp_dbg, int,
62 MODULE_PARM_DESC(debug_iscsi_tcp, "Turn on debugging for iscsi_tcp module "
63 "Set to 1 to turn on, and zero to turn off. Default is off.");
65 #define ISCSI_SW_TCP_DBG(_conn, dbg_fmt, arg...) \
67 if (iscsi_sw_tcp_dbg) \
68 iscsi_conn_printk(KERN_INFO, _conn, \
75 * iscsi_sw_tcp_recv - TCP receive in sendfile fashion
76 * @rd_desc: read descriptor
78 * @offset: offset in skb
79 * @len: skb->len - offset
81 static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
82 unsigned int offset, size_t len)
84 struct iscsi_conn *conn = rd_desc->arg.data;
85 unsigned int consumed, total_consumed = 0;
88 ISCSI_SW_TCP_DBG(conn, "in %d bytes\n", skb->len - offset);
92 consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
94 total_consumed += consumed;
95 } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
97 ISCSI_SW_TCP_DBG(conn, "read %d bytes status %d\n",
98 skb->len - offset, status);
99 return total_consumed;
103 * iscsi_sw_sk_state_check - check socket state
106 * If the socket is in CLOSE or CLOSE_WAIT we should
107 * not close the connection if there is still some
110 static inline int iscsi_sw_sk_state_check(struct sock *sk)
112 if ((sk->sk_state == TCP_CLOSE_WAIT ||
113 sk->sk_state == TCP_CLOSE) &&
114 !atomic_read(&sk->sk_rmem_alloc))
120 static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag)
122 struct iscsi_conn *conn = sk->sk_user_data;
123 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
124 read_descriptor_t rd_desc;
126 read_lock(&sk->sk_callback_lock);
129 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
130 * We set count to 1 because we want the network layer to
131 * hand us all the skbs that are available. iscsi_tcp_recv
132 * handled pdus that cross buffers or pdus that still need data.
134 rd_desc.arg.data = conn;
136 tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
138 if (iscsi_sw_sk_state_check(sk) < 0) {
139 ISCSI_SW_TCP_DBG(conn, "iscsi_tcp_data_ready: "
140 "TCP_CLOSE|TCP_CLOSE_WAIT\n");
141 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
144 read_unlock(&sk->sk_callback_lock);
146 /* If we had to (atomically) map a highmem page,
148 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
151 static void iscsi_sw_tcp_state_change(struct sock *sk)
153 struct iscsi_tcp_conn *tcp_conn;
154 struct iscsi_sw_tcp_conn *tcp_sw_conn;
155 struct iscsi_conn *conn;
156 struct iscsi_session *session;
157 void (*old_state_change)(struct sock *);
159 read_lock(&sk->sk_callback_lock);
161 conn = (struct iscsi_conn*)sk->sk_user_data;
162 session = conn->session;
164 if (iscsi_sw_sk_state_check(sk) < 0) {
165 ISCSI_SW_TCP_DBG(conn, "iscsi_tcp_state_change: "
166 "TCP_CLOSE|TCP_CLOSE_WAIT\n");
167 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
170 tcp_conn = conn->dd_data;
171 tcp_sw_conn = tcp_conn->dd_data;
172 old_state_change = tcp_sw_conn->old_state_change;
174 read_unlock(&sk->sk_callback_lock);
176 old_state_change(sk);
180 * iscsi_write_space - Called when more output buffer space is available
181 * @sk: socket space is available for
183 static void iscsi_sw_tcp_write_space(struct sock *sk)
185 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
186 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
187 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
189 tcp_sw_conn->old_write_space(sk);
190 ISCSI_SW_TCP_DBG(conn, "iscsi_write_space\n");
191 iscsi_conn_queue_work(conn);
194 static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
196 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
197 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
198 struct sock *sk = tcp_sw_conn->sock->sk;
200 /* assign new callbacks */
201 write_lock_bh(&sk->sk_callback_lock);
202 sk->sk_user_data = conn;
203 tcp_sw_conn->old_data_ready = sk->sk_data_ready;
204 tcp_sw_conn->old_state_change = sk->sk_state_change;
205 tcp_sw_conn->old_write_space = sk->sk_write_space;
206 sk->sk_data_ready = iscsi_sw_tcp_data_ready;
207 sk->sk_state_change = iscsi_sw_tcp_state_change;
208 sk->sk_write_space = iscsi_sw_tcp_write_space;
209 write_unlock_bh(&sk->sk_callback_lock);
213 iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_sw_tcp_conn *tcp_sw_conn)
215 struct sock *sk = tcp_sw_conn->sock->sk;
217 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
218 write_lock_bh(&sk->sk_callback_lock);
219 sk->sk_user_data = NULL;
220 sk->sk_data_ready = tcp_sw_conn->old_data_ready;
221 sk->sk_state_change = tcp_sw_conn->old_state_change;
222 sk->sk_write_space = tcp_sw_conn->old_write_space;
224 write_unlock_bh(&sk->sk_callback_lock);
228 * iscsi_sw_tcp_xmit_segment - transmit segment
229 * @tcp_conn: the iSCSI TCP connection
230 * @segment: the buffer to transmnit
232 * This function transmits as much of the buffer as
233 * the network layer will accept, and returns the number of
236 * If CRC hashing is enabled, the function will compute the
237 * hash as it goes. When the entire segment has been transmitted,
238 * it will retrieve the hash value and send it as well.
240 static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
241 struct iscsi_segment *segment)
243 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
244 struct socket *sk = tcp_sw_conn->sock;
245 unsigned int copied = 0;
248 while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) {
249 struct scatterlist *sg;
250 unsigned int offset, copy;
254 offset = segment->copied;
255 copy = segment->size - offset;
257 if (segment->total_copied + segment->size < segment->total_size)
260 /* Use sendpage if we can; else fall back to sendmsg */
261 if (!segment->data) {
263 offset += segment->sg_offset + sg->offset;
264 r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
267 struct msghdr msg = { .msg_flags = flags };
269 .iov_base = segment->data + offset,
273 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
277 iscsi_tcp_segment_unmap(segment);
286 * iscsi_sw_tcp_xmit - TCP transmit
288 static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
290 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
291 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
292 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
293 unsigned int consumed = 0;
297 rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
299 * We may not have been able to send data because the conn
300 * is getting stopped. libiscsi will know so propogate err
301 * for it to do the right thing.
306 rc = ISCSI_ERR_XMIT_FAILED;
313 if (segment->total_copied >= segment->total_size) {
314 if (segment->done != NULL) {
315 rc = segment->done(tcp_conn, segment);
322 ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed);
324 conn->txdata_octets += consumed;
328 /* Transmit error. We could initiate error recovery
330 ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc);
331 iscsi_conn_failure(conn, rc);
336 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
338 static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
340 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
341 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
342 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
344 return segment->total_copied - segment->total_size;
347 static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
349 struct iscsi_conn *conn = task->conn;
352 while (iscsi_sw_tcp_xmit_qlen(conn)) {
353 rc = iscsi_sw_tcp_xmit(conn);
364 * This is called when we're done sending the header.
365 * Simply copy the data_segment to the send segment, and return.
367 static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
368 struct iscsi_segment *segment)
370 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
372 tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
373 ISCSI_SW_TCP_DBG(tcp_conn->iscsi_conn,
374 "Header done. Next segment size %u total_size %u\n",
375 tcp_sw_conn->out.segment.size,
376 tcp_sw_conn->out.segment.total_size);
380 static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
383 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
384 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
386 ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ?
387 "digest enabled" : "digest disabled");
389 /* Clear the data segment - needs to be filled in by the
390 * caller using iscsi_tcp_send_data_prep() */
391 memset(&tcp_sw_conn->out.data_segment, 0,
392 sizeof(struct iscsi_segment));
394 /* If header digest is enabled, compute the CRC and
395 * place the digest into the same buffer. We make
396 * sure that both iscsi_tcp_task and mtask have
399 if (conn->hdrdgst_en) {
400 iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
402 hdrlen += ISCSI_DIGEST_SIZE;
405 /* Remember header pointer for later, when we need
406 * to decide whether there's a payload to go along
407 * with the header. */
408 tcp_sw_conn->out.hdr = hdr;
410 iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
411 iscsi_sw_tcp_send_hdr_done, NULL);
415 * Prepare the send buffer for the payload data.
416 * Padding and checksumming will all be taken care
417 * of by the iscsi_segment routines.
420 iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
421 unsigned int count, unsigned int offset,
424 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
425 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
426 struct hash_desc *tx_hash = NULL;
427 unsigned int hdr_spec_len;
429 ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len,
431 "digest enabled" : "digest disabled");
433 /* Make sure the datalen matches what the caller
434 said he would send. */
435 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
436 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
438 if (conn->datadgst_en)
439 tx_hash = &tcp_sw_conn->tx_hash;
441 return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
442 sg, count, offset, len,
447 iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
450 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
451 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
452 struct hash_desc *tx_hash = NULL;
453 unsigned int hdr_spec_len;
455 ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ?
456 "digest enabled" : "digest disabled");
458 /* Make sure the datalen matches what the caller
459 said he would send. */
460 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
461 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
463 if (conn->datadgst_en)
464 tx_hash = &tcp_sw_conn->tx_hash;
466 iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
467 data, len, NULL, tx_hash);
470 static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
471 unsigned int offset, unsigned int count)
473 struct iscsi_conn *conn = task->conn;
476 iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
482 iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
484 struct scsi_data_buffer *sdb = scsi_out(task->sc);
486 err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
487 sdb->table.nents, offset,
492 /* got invalid offset/len */
498 static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
500 struct iscsi_tcp_task *tcp_task = task->dd_data;
502 task->hdr = task->dd_data + sizeof(*tcp_task);
503 task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
507 static struct iscsi_cls_conn *
508 iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
511 struct iscsi_conn *conn;
512 struct iscsi_cls_conn *cls_conn;
513 struct iscsi_tcp_conn *tcp_conn;
514 struct iscsi_sw_tcp_conn *tcp_sw_conn;
516 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
520 conn = cls_conn->dd_data;
521 tcp_conn = conn->dd_data;
522 tcp_sw_conn = tcp_conn->dd_data;
524 tcp_sw_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
526 tcp_sw_conn->tx_hash.flags = 0;
527 if (IS_ERR(tcp_sw_conn->tx_hash.tfm))
530 tcp_sw_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
532 tcp_sw_conn->rx_hash.flags = 0;
533 if (IS_ERR(tcp_sw_conn->rx_hash.tfm))
535 tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
540 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
542 iscsi_conn_printk(KERN_ERR, conn,
543 "Could not create connection due to crc32c "
544 "loading error. Make sure the crc32c "
545 "module is built as a module or into the "
547 iscsi_tcp_conn_teardown(cls_conn);
551 static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
553 struct iscsi_session *session = conn->session;
554 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
555 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
556 struct socket *sock = tcp_sw_conn->sock;
562 iscsi_sw_tcp_conn_restore_callbacks(tcp_sw_conn);
565 spin_lock_bh(&session->lock);
566 tcp_sw_conn->sock = NULL;
567 spin_unlock_bh(&session->lock);
571 static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
573 struct iscsi_conn *conn = cls_conn->dd_data;
574 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
575 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
577 iscsi_sw_tcp_release_conn(conn);
579 if (tcp_sw_conn->tx_hash.tfm)
580 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
581 if (tcp_sw_conn->rx_hash.tfm)
582 crypto_free_hash(tcp_sw_conn->rx_hash.tfm);
584 iscsi_tcp_conn_teardown(cls_conn);
587 static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
589 struct iscsi_conn *conn = cls_conn->dd_data;
590 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
591 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
593 /* userspace may have goofed up and not bound us */
594 if (!tcp_sw_conn->sock)
597 * Make sure our recv side is stopped.
598 * Older tools called conn stop before ep_disconnect
599 * so IO could still be coming in.
601 write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
602 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
603 write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
605 iscsi_conn_stop(cls_conn, flag);
606 iscsi_sw_tcp_release_conn(conn);
609 static int iscsi_sw_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
610 char *buf, int *port,
611 int (*getname)(struct socket *,
615 struct sockaddr_storage *addr;
616 struct sockaddr_in6 *sin6;
617 struct sockaddr_in *sin;
620 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
624 if (getname(sock, (struct sockaddr *) addr, &len)) {
629 switch (addr->ss_family) {
631 sin = (struct sockaddr_in *)addr;
632 spin_lock_bh(&conn->session->lock);
633 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
634 *port = be16_to_cpu(sin->sin_port);
635 spin_unlock_bh(&conn->session->lock);
638 sin6 = (struct sockaddr_in6 *)addr;
639 spin_lock_bh(&conn->session->lock);
640 sprintf(buf, "%pI6", &sin6->sin6_addr);
641 *port = be16_to_cpu(sin6->sin6_port);
642 spin_unlock_bh(&conn->session->lock);
651 iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
652 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
655 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
656 struct iscsi_host *ihost = shost_priv(shost);
657 struct iscsi_conn *conn = cls_conn->dd_data;
658 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
659 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
664 /* lookup for existing socket */
665 sock = sockfd_lookup((int)transport_eph, &err);
667 iscsi_conn_printk(KERN_ERR, conn,
668 "sockfd_lookup failed %d\n", err);
672 * copy these values now because if we drop the session
673 * userspace may still want to query the values since we will
674 * be using them for the reconnect
676 err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address,
677 &conn->portal_port, kernel_getpeername);
681 err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address,
682 &ihost->local_port, kernel_getsockname);
686 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
690 /* bind iSCSI connection and socket */
691 tcp_sw_conn->sock = sock;
693 /* setup Socket parameters */
696 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
697 sk->sk_allocation = GFP_ATOMIC;
699 iscsi_sw_tcp_conn_set_callbacks(conn);
700 tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
702 * set receive state machine into initial state
704 iscsi_tcp_hdr_recv_prep(tcp_conn);
712 static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
713 enum iscsi_param param, char *buf,
716 struct iscsi_conn *conn = cls_conn->dd_data;
717 struct iscsi_session *session = conn->session;
718 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
719 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
723 case ISCSI_PARAM_HDRDGST_EN:
724 iscsi_set_param(cls_conn, param, buf, buflen);
726 case ISCSI_PARAM_DATADGST_EN:
727 iscsi_set_param(cls_conn, param, buf, buflen);
728 tcp_sw_conn->sendpage = conn->datadgst_en ?
729 sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
731 case ISCSI_PARAM_MAX_R2T:
732 sscanf(buf, "%d", &value);
733 if (value <= 0 || !is_power_of_2(value))
735 if (session->max_r2t == value)
737 iscsi_tcp_r2tpool_free(session);
738 iscsi_set_param(cls_conn, param, buf, buflen);
739 if (iscsi_tcp_r2tpool_alloc(session))
743 return iscsi_set_param(cls_conn, param, buf, buflen);
749 static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
750 enum iscsi_param param, char *buf)
752 struct iscsi_conn *conn = cls_conn->dd_data;
756 case ISCSI_PARAM_CONN_PORT:
757 spin_lock_bh(&conn->session->lock);
758 len = sprintf(buf, "%hu\n", conn->portal_port);
759 spin_unlock_bh(&conn->session->lock);
761 case ISCSI_PARAM_CONN_ADDRESS:
762 spin_lock_bh(&conn->session->lock);
763 len = sprintf(buf, "%s\n", conn->portal_address);
764 spin_unlock_bh(&conn->session->lock);
767 return iscsi_conn_get_param(cls_conn, param, buf);
774 iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
775 struct iscsi_stats *stats)
777 struct iscsi_conn *conn = cls_conn->dd_data;
778 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
779 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
781 stats->custom_length = 3;
782 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
783 stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
784 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
785 stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
786 strcpy(stats->custom[2].desc, "eh_abort_cnt");
787 stats->custom[2].value = conn->eh_abort_cnt;
789 iscsi_tcp_conn_get_stats(cls_conn, stats);
792 static struct iscsi_cls_session *
793 iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
794 uint16_t qdepth, uint32_t initial_cmdsn)
796 struct iscsi_cls_session *cls_session;
797 struct iscsi_session *session;
798 struct Scsi_Host *shost;
801 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
805 shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, 1);
808 shost->transportt = iscsi_sw_tcp_scsi_transport;
809 shost->cmd_per_lun = qdepth;
810 shost->max_lun = iscsi_max_lun;
812 shost->max_channel = 0;
813 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
815 if (iscsi_host_add(shost, NULL))
818 cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
820 sizeof(struct iscsi_tcp_task) +
821 sizeof(struct iscsi_sw_tcp_hdrbuf),
825 session = cls_session->dd_data;
827 shost->can_queue = session->scsi_cmds_max;
828 if (iscsi_tcp_r2tpool_alloc(session))
833 iscsi_session_teardown(cls_session);
835 iscsi_host_remove(shost);
837 iscsi_host_free(shost);
841 static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
843 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
845 iscsi_tcp_r2tpool_free(cls_session->dd_data);
846 iscsi_session_teardown(cls_session);
848 iscsi_host_remove(shost);
849 iscsi_host_free(shost);
852 static int iscsi_sw_tcp_slave_alloc(struct scsi_device *sdev)
854 set_bit(QUEUE_FLAG_BIDI, &sdev->request_queue->queue_flags);
858 static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
860 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
861 blk_queue_dma_alignment(sdev->request_queue, 0);
865 static struct scsi_host_template iscsi_sw_tcp_sht = {
866 .module = THIS_MODULE,
867 .name = "iSCSI Initiator over TCP/IP",
868 .queuecommand = iscsi_queuecommand,
869 .change_queue_depth = iscsi_change_queue_depth,
870 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
871 .sg_tablesize = 4096,
872 .max_sectors = 0xFFFF,
873 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
874 .eh_abort_handler = iscsi_eh_abort,
875 .eh_device_reset_handler= iscsi_eh_device_reset,
876 .eh_target_reset_handler= iscsi_eh_target_reset,
877 .use_clustering = DISABLE_CLUSTERING,
878 .slave_alloc = iscsi_sw_tcp_slave_alloc,
879 .slave_configure = iscsi_sw_tcp_slave_configure,
880 .target_alloc = iscsi_target_alloc,
881 .proc_name = "iscsi_tcp",
885 static struct iscsi_transport iscsi_sw_tcp_transport = {
886 .owner = THIS_MODULE,
888 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
890 .param_mask = ISCSI_MAX_RECV_DLENGTH |
891 ISCSI_MAX_XMIT_DLENGTH |
894 ISCSI_INITIAL_R2T_EN |
899 ISCSI_PDU_INORDER_EN |
900 ISCSI_DATASEQ_INORDER_EN |
905 ISCSI_PERSISTENT_PORT |
906 ISCSI_PERSISTENT_ADDRESS |
907 ISCSI_TARGET_NAME | ISCSI_TPGT |
908 ISCSI_USERNAME | ISCSI_PASSWORD |
909 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
910 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
912 ISCSI_PING_TMO | ISCSI_RECV_TMO |
913 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
914 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
915 ISCSI_HOST_INITIATOR_NAME |
916 ISCSI_HOST_NETDEV_NAME,
917 /* session management */
918 .create_session = iscsi_sw_tcp_session_create,
919 .destroy_session = iscsi_sw_tcp_session_destroy,
920 /* connection management */
921 .create_conn = iscsi_sw_tcp_conn_create,
922 .bind_conn = iscsi_sw_tcp_conn_bind,
923 .destroy_conn = iscsi_sw_tcp_conn_destroy,
924 .set_param = iscsi_sw_tcp_conn_set_param,
925 .get_conn_param = iscsi_sw_tcp_conn_get_param,
926 .get_session_param = iscsi_session_get_param,
927 .start_conn = iscsi_conn_start,
928 .stop_conn = iscsi_sw_tcp_conn_stop,
929 /* iscsi host params */
930 .get_host_param = iscsi_host_get_param,
931 .set_host_param = iscsi_host_set_param,
933 .send_pdu = iscsi_conn_send_pdu,
934 .get_stats = iscsi_sw_tcp_conn_get_stats,
935 /* iscsi task/cmd helpers */
936 .init_task = iscsi_tcp_task_init,
937 .xmit_task = iscsi_tcp_task_xmit,
938 .cleanup_task = iscsi_tcp_cleanup_task,
939 /* low level pdu helpers */
940 .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
941 .init_pdu = iscsi_sw_tcp_pdu_init,
942 .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
944 .session_recovery_timedout = iscsi_session_recovery_timedout,
947 static int __init iscsi_sw_tcp_init(void)
949 if (iscsi_max_lun < 1) {
950 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
955 iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
956 &iscsi_sw_tcp_transport);
957 if (!iscsi_sw_tcp_scsi_transport)
963 static void __exit iscsi_sw_tcp_exit(void)
965 iscsi_unregister_transport(&iscsi_sw_tcp_transport);
968 module_init(iscsi_sw_tcp_init);
969 module_exit(iscsi_sw_tcp_exit);