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.
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 <crypto/hash.h>
30 #include <linux/types.h>
31 #include <linux/inet.h>
32 #include <linux/slab.h>
33 #include <linux/sched/mm.h>
34 #include <linux/file.h>
35 #include <linux/blkdev.h>
36 #include <linux/delay.h>
37 #include <linux/kfifo.h>
38 #include <linux/scatterlist.h>
39 #include <linux/module.h>
40 #include <linux/backing-dev.h>
42 #include <scsi/scsi_cmnd.h>
43 #include <scsi/scsi_device.h>
44 #include <scsi/scsi_host.h>
45 #include <scsi/scsi.h>
46 #include <scsi/scsi_transport_iscsi.h>
47 #include <trace/events/iscsi.h>
49 #include "iscsi_tcp.h"
54 MODULE_DESCRIPTION("iSCSI/TCP data-path");
55 MODULE_LICENSE("GPL");
57 static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
58 static struct scsi_host_template iscsi_sw_tcp_sht;
59 static struct iscsi_transport iscsi_sw_tcp_transport;
61 static unsigned int iscsi_max_lun = ~0;
62 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
64 static int iscsi_sw_tcp_dbg;
65 module_param_named(debug_iscsi_tcp, iscsi_sw_tcp_dbg, int,
67 MODULE_PARM_DESC(debug_iscsi_tcp, "Turn on debugging for iscsi_tcp module "
68 "Set to 1 to turn on, and zero to turn off. Default is off.");
70 #define ISCSI_SW_TCP_DBG(_conn, dbg_fmt, arg...) \
72 if (iscsi_sw_tcp_dbg) \
73 iscsi_conn_printk(KERN_INFO, _conn, \
76 iscsi_dbg_trace(trace_iscsi_dbg_sw_tcp, \
77 &(_conn)->cls_conn->dev, \
78 "%s " dbg_fmt, __func__, ##arg);\
83 * iscsi_sw_tcp_recv - TCP receive in sendfile fashion
84 * @rd_desc: read descriptor
86 * @offset: offset in skb
87 * @len: skb->len - offset
89 static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
90 unsigned int offset, size_t len)
92 struct iscsi_conn *conn = rd_desc->arg.data;
93 unsigned int consumed, total_consumed = 0;
96 ISCSI_SW_TCP_DBG(conn, "in %d bytes\n", skb->len - offset);
100 consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
102 total_consumed += consumed;
103 } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
105 ISCSI_SW_TCP_DBG(conn, "read %d bytes status %d\n",
106 skb->len - offset, status);
107 return total_consumed;
111 * iscsi_sw_sk_state_check - check socket state
114 * If the socket is in CLOSE or CLOSE_WAIT we should
115 * not close the connection if there is still some
118 * Must be called with sk_callback_lock.
120 static inline int iscsi_sw_sk_state_check(struct sock *sk)
122 struct iscsi_conn *conn = sk->sk_user_data;
124 if ((sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) &&
125 (conn->session->state != ISCSI_STATE_LOGGING_OUT) &&
126 !atomic_read(&sk->sk_rmem_alloc)) {
127 ISCSI_SW_TCP_DBG(conn, "TCP_CLOSE|TCP_CLOSE_WAIT\n");
128 iscsi_conn_failure(conn, ISCSI_ERR_TCP_CONN_CLOSE);
134 static void iscsi_sw_tcp_data_ready(struct sock *sk)
136 struct iscsi_conn *conn;
137 struct iscsi_tcp_conn *tcp_conn;
138 read_descriptor_t rd_desc;
140 read_lock_bh(&sk->sk_callback_lock);
141 conn = sk->sk_user_data;
143 read_unlock_bh(&sk->sk_callback_lock);
146 tcp_conn = conn->dd_data;
149 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
150 * We set count to 1 because we want the network layer to
151 * hand us all the skbs that are available. iscsi_tcp_recv
152 * handled pdus that cross buffers or pdus that still need data.
154 rd_desc.arg.data = conn;
156 tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
158 iscsi_sw_sk_state_check(sk);
160 /* If we had to (atomically) map a highmem page,
162 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
163 read_unlock_bh(&sk->sk_callback_lock);
166 static void iscsi_sw_tcp_state_change(struct sock *sk)
168 struct iscsi_tcp_conn *tcp_conn;
169 struct iscsi_sw_tcp_conn *tcp_sw_conn;
170 struct iscsi_conn *conn;
171 void (*old_state_change)(struct sock *);
173 read_lock_bh(&sk->sk_callback_lock);
174 conn = sk->sk_user_data;
176 read_unlock_bh(&sk->sk_callback_lock);
180 iscsi_sw_sk_state_check(sk);
182 tcp_conn = conn->dd_data;
183 tcp_sw_conn = tcp_conn->dd_data;
184 old_state_change = tcp_sw_conn->old_state_change;
186 read_unlock_bh(&sk->sk_callback_lock);
188 old_state_change(sk);
192 * iscsi_write_space - Called when more output buffer space is available
193 * @sk: socket space is available for
195 static void iscsi_sw_tcp_write_space(struct sock *sk)
197 struct iscsi_conn *conn;
198 struct iscsi_tcp_conn *tcp_conn;
199 struct iscsi_sw_tcp_conn *tcp_sw_conn;
200 void (*old_write_space)(struct sock *);
202 read_lock_bh(&sk->sk_callback_lock);
203 conn = sk->sk_user_data;
205 read_unlock_bh(&sk->sk_callback_lock);
209 tcp_conn = conn->dd_data;
210 tcp_sw_conn = tcp_conn->dd_data;
211 old_write_space = tcp_sw_conn->old_write_space;
212 read_unlock_bh(&sk->sk_callback_lock);
216 ISCSI_SW_TCP_DBG(conn, "iscsi_write_space\n");
217 iscsi_conn_queue_work(conn);
220 static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
222 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
223 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
224 struct sock *sk = tcp_sw_conn->sock->sk;
226 /* assign new callbacks */
227 write_lock_bh(&sk->sk_callback_lock);
228 sk->sk_user_data = conn;
229 tcp_sw_conn->old_data_ready = sk->sk_data_ready;
230 tcp_sw_conn->old_state_change = sk->sk_state_change;
231 tcp_sw_conn->old_write_space = sk->sk_write_space;
232 sk->sk_data_ready = iscsi_sw_tcp_data_ready;
233 sk->sk_state_change = iscsi_sw_tcp_state_change;
234 sk->sk_write_space = iscsi_sw_tcp_write_space;
235 write_unlock_bh(&sk->sk_callback_lock);
239 iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_conn *conn)
241 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
242 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
243 struct sock *sk = tcp_sw_conn->sock->sk;
245 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
246 write_lock_bh(&sk->sk_callback_lock);
247 sk->sk_user_data = NULL;
248 sk->sk_data_ready = tcp_sw_conn->old_data_ready;
249 sk->sk_state_change = tcp_sw_conn->old_state_change;
250 sk->sk_write_space = tcp_sw_conn->old_write_space;
251 sk->sk_no_check_tx = 0;
252 write_unlock_bh(&sk->sk_callback_lock);
256 * iscsi_sw_tcp_xmit_segment - transmit segment
257 * @tcp_conn: the iSCSI TCP connection
258 * @segment: the buffer to transmnit
260 * This function transmits as much of the buffer as
261 * the network layer will accept, and returns the number of
264 * If CRC hashing is enabled, the function will compute the
265 * hash as it goes. When the entire segment has been transmitted,
266 * it will retrieve the hash value and send it as well.
268 static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
269 struct iscsi_segment *segment)
271 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
272 struct socket *sk = tcp_sw_conn->sock;
273 unsigned int copied = 0;
276 while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) {
277 struct scatterlist *sg;
278 unsigned int offset, copy;
282 offset = segment->copied;
283 copy = segment->size - offset;
285 if (segment->total_copied + segment->size < segment->total_size)
288 /* Use sendpage if we can; else fall back to sendmsg */
289 if (!segment->data) {
291 offset += segment->sg_offset + sg->offset;
292 r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
295 struct msghdr msg = { .msg_flags = flags };
297 .iov_base = segment->data + offset,
301 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
305 iscsi_tcp_segment_unmap(segment);
314 * iscsi_sw_tcp_xmit - TCP transmit
315 * @conn: iscsi connection
317 static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
319 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
320 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
321 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
322 unsigned int consumed = 0;
326 rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
328 * We may not have been able to send data because the conn
329 * is getting stopped. libiscsi will know so propagate err
330 * for it to do the right thing.
335 rc = ISCSI_ERR_XMIT_FAILED;
342 if (segment->total_copied >= segment->total_size) {
343 if (segment->done != NULL) {
344 rc = segment->done(tcp_conn, segment);
351 ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed);
353 conn->txdata_octets += consumed;
357 /* Transmit error. We could initiate error recovery
359 ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc);
360 iscsi_conn_failure(conn, rc);
365 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
366 * @conn: iscsi connection
368 static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
370 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
371 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
372 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
374 return segment->total_copied - segment->total_size;
377 static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
379 struct iscsi_conn *conn = task->conn;
380 unsigned int noreclaim_flag;
383 noreclaim_flag = memalloc_noreclaim_save();
385 while (iscsi_sw_tcp_xmit_qlen(conn)) {
386 rc = iscsi_sw_tcp_xmit(conn);
396 memalloc_noreclaim_restore(noreclaim_flag);
401 * This is called when we're done sending the header.
402 * Simply copy the data_segment to the send segment, and return.
404 static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
405 struct iscsi_segment *segment)
407 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
409 tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
410 ISCSI_SW_TCP_DBG(tcp_conn->iscsi_conn,
411 "Header done. Next segment size %u total_size %u\n",
412 tcp_sw_conn->out.segment.size,
413 tcp_sw_conn->out.segment.total_size);
417 static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
420 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
421 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
423 ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ?
424 "digest enabled" : "digest disabled");
426 /* Clear the data segment - needs to be filled in by the
427 * caller using iscsi_tcp_send_data_prep() */
428 memset(&tcp_sw_conn->out.data_segment, 0,
429 sizeof(struct iscsi_segment));
431 /* If header digest is enabled, compute the CRC and
432 * place the digest into the same buffer. We make
433 * sure that both iscsi_tcp_task and mtask have
436 if (conn->hdrdgst_en) {
437 iscsi_tcp_dgst_header(tcp_sw_conn->tx_hash, hdr, hdrlen,
439 hdrlen += ISCSI_DIGEST_SIZE;
442 /* Remember header pointer for later, when we need
443 * to decide whether there's a payload to go along
444 * with the header. */
445 tcp_sw_conn->out.hdr = hdr;
447 iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
448 iscsi_sw_tcp_send_hdr_done, NULL);
452 * Prepare the send buffer for the payload data.
453 * Padding and checksumming will all be taken care
454 * of by the iscsi_segment routines.
457 iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
458 unsigned int count, unsigned int offset,
461 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
462 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
463 struct ahash_request *tx_hash = NULL;
464 unsigned int hdr_spec_len;
466 ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len,
468 "digest enabled" : "digest disabled");
470 /* Make sure the datalen matches what the caller
471 said he would send. */
472 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
473 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
475 if (conn->datadgst_en)
476 tx_hash = tcp_sw_conn->tx_hash;
478 return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
479 sg, count, offset, len,
484 iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
487 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
488 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
489 struct ahash_request *tx_hash = NULL;
490 unsigned int hdr_spec_len;
492 ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ?
493 "digest enabled" : "digest disabled");
495 /* Make sure the datalen matches what the caller
496 said he would send. */
497 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
498 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
500 if (conn->datadgst_en)
501 tx_hash = tcp_sw_conn->tx_hash;
503 iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
504 data, len, NULL, tx_hash);
507 static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
508 unsigned int offset, unsigned int count)
510 struct iscsi_conn *conn = task->conn;
513 iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
519 iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
521 struct scsi_data_buffer *sdb = &task->sc->sdb;
523 err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
524 sdb->table.nents, offset,
529 /* got invalid offset/len */
535 static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
537 struct iscsi_tcp_task *tcp_task = task->dd_data;
539 task->hdr = task->dd_data + sizeof(*tcp_task);
540 task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
544 static struct iscsi_cls_conn *
545 iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
548 struct iscsi_conn *conn;
549 struct iscsi_cls_conn *cls_conn;
550 struct iscsi_tcp_conn *tcp_conn;
551 struct iscsi_sw_tcp_conn *tcp_sw_conn;
552 struct crypto_ahash *tfm;
554 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
558 conn = cls_conn->dd_data;
559 tcp_conn = conn->dd_data;
560 tcp_sw_conn = tcp_conn->dd_data;
562 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
566 tcp_sw_conn->tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
567 if (!tcp_sw_conn->tx_hash)
569 ahash_request_set_callback(tcp_sw_conn->tx_hash, 0, NULL, NULL);
571 tcp_sw_conn->rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
572 if (!tcp_sw_conn->rx_hash)
574 ahash_request_set_callback(tcp_sw_conn->rx_hash, 0, NULL, NULL);
576 tcp_conn->rx_hash = tcp_sw_conn->rx_hash;
581 ahash_request_free(tcp_sw_conn->tx_hash);
583 crypto_free_ahash(tfm);
585 iscsi_conn_printk(KERN_ERR, conn,
586 "Could not create connection due to crc32c "
587 "loading error. Make sure the crc32c "
588 "module is built as a module or into the "
590 iscsi_tcp_conn_teardown(cls_conn);
594 static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
596 struct iscsi_session *session = conn->session;
597 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
598 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
599 struct socket *sock = tcp_sw_conn->sock;
605 iscsi_sw_tcp_conn_restore_callbacks(conn);
608 spin_lock_bh(&session->frwd_lock);
609 tcp_sw_conn->sock = NULL;
610 spin_unlock_bh(&session->frwd_lock);
614 static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
616 struct iscsi_conn *conn = cls_conn->dd_data;
617 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
618 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
620 iscsi_sw_tcp_release_conn(conn);
622 ahash_request_free(tcp_sw_conn->rx_hash);
623 if (tcp_sw_conn->tx_hash) {
624 struct crypto_ahash *tfm;
626 tfm = crypto_ahash_reqtfm(tcp_sw_conn->tx_hash);
627 ahash_request_free(tcp_sw_conn->tx_hash);
628 crypto_free_ahash(tfm);
631 iscsi_tcp_conn_teardown(cls_conn);
634 static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
636 struct iscsi_conn *conn = cls_conn->dd_data;
637 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
638 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
639 struct socket *sock = tcp_sw_conn->sock;
641 /* userspace may have goofed up and not bound us */
645 sock->sk->sk_err = EIO;
646 wake_up_interruptible(sk_sleep(sock->sk));
649 iscsi_suspend_tx(conn);
651 /* stop recv side and release socket */
652 iscsi_sw_tcp_release_conn(conn);
654 iscsi_conn_stop(cls_conn, flag);
658 iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
659 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
662 struct iscsi_session *session = cls_session->dd_data;
663 struct iscsi_conn *conn = cls_conn->dd_data;
664 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
665 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
670 /* lookup for existing socket */
671 sock = sockfd_lookup((int)transport_eph, &err);
673 iscsi_conn_printk(KERN_ERR, conn,
674 "sockfd_lookup failed %d\n", err);
678 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
682 spin_lock_bh(&session->frwd_lock);
683 /* bind iSCSI connection and socket */
684 tcp_sw_conn->sock = sock;
685 spin_unlock_bh(&session->frwd_lock);
687 /* setup Socket parameters */
689 sk->sk_reuse = SK_CAN_REUSE;
690 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
691 sk->sk_allocation = GFP_ATOMIC;
694 iscsi_sw_tcp_conn_set_callbacks(conn);
695 tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
697 * set receive state machine into initial state
699 iscsi_tcp_hdr_recv_prep(tcp_conn);
707 static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
708 enum iscsi_param param, char *buf,
711 struct iscsi_conn *conn = cls_conn->dd_data;
712 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
713 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
716 case ISCSI_PARAM_HDRDGST_EN:
717 iscsi_set_param(cls_conn, param, buf, buflen);
719 case ISCSI_PARAM_DATADGST_EN:
720 iscsi_set_param(cls_conn, param, buf, buflen);
721 tcp_sw_conn->sendpage = conn->datadgst_en ?
722 sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
724 case ISCSI_PARAM_MAX_R2T:
725 return iscsi_tcp_set_max_r2t(conn, buf);
727 return iscsi_set_param(cls_conn, param, buf, buflen);
733 static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
734 enum iscsi_param param, char *buf)
736 struct iscsi_conn *conn = cls_conn->dd_data;
737 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
738 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
739 struct sockaddr_in6 addr;
743 case ISCSI_PARAM_CONN_PORT:
744 case ISCSI_PARAM_CONN_ADDRESS:
745 case ISCSI_PARAM_LOCAL_PORT:
746 spin_lock_bh(&conn->session->frwd_lock);
747 if (!tcp_sw_conn || !tcp_sw_conn->sock) {
748 spin_unlock_bh(&conn->session->frwd_lock);
751 if (param == ISCSI_PARAM_LOCAL_PORT)
752 rc = kernel_getsockname(tcp_sw_conn->sock,
753 (struct sockaddr *)&addr);
755 rc = kernel_getpeername(tcp_sw_conn->sock,
756 (struct sockaddr *)&addr);
757 spin_unlock_bh(&conn->session->frwd_lock);
761 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
764 return iscsi_conn_get_param(cls_conn, param, buf);
770 static int iscsi_sw_tcp_host_get_param(struct Scsi_Host *shost,
771 enum iscsi_host_param param, char *buf)
773 struct iscsi_sw_tcp_host *tcp_sw_host = iscsi_host_priv(shost);
774 struct iscsi_session *session = tcp_sw_host->session;
775 struct iscsi_conn *conn;
776 struct iscsi_tcp_conn *tcp_conn;
777 struct iscsi_sw_tcp_conn *tcp_sw_conn;
778 struct sockaddr_in6 addr;
782 case ISCSI_HOST_PARAM_IPADDRESS:
786 spin_lock_bh(&session->frwd_lock);
787 conn = session->leadconn;
789 spin_unlock_bh(&session->frwd_lock);
792 tcp_conn = conn->dd_data;
794 tcp_sw_conn = tcp_conn->dd_data;
795 if (!tcp_sw_conn->sock) {
796 spin_unlock_bh(&session->frwd_lock);
800 rc = kernel_getsockname(tcp_sw_conn->sock,
801 (struct sockaddr *)&addr);
802 spin_unlock_bh(&session->frwd_lock);
806 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
808 (enum iscsi_param)param, buf);
810 return iscsi_host_get_param(shost, param, buf);
817 iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
818 struct iscsi_stats *stats)
820 struct iscsi_conn *conn = cls_conn->dd_data;
821 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
822 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
824 stats->custom_length = 3;
825 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
826 stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
827 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
828 stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
829 strcpy(stats->custom[2].desc, "eh_abort_cnt");
830 stats->custom[2].value = conn->eh_abort_cnt;
832 iscsi_tcp_conn_get_stats(cls_conn, stats);
835 static struct iscsi_cls_session *
836 iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
837 uint16_t qdepth, uint32_t initial_cmdsn)
839 struct iscsi_cls_session *cls_session;
840 struct iscsi_session *session;
841 struct iscsi_sw_tcp_host *tcp_sw_host;
842 struct Scsi_Host *shost;
845 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
849 shost = iscsi_host_alloc(&iscsi_sw_tcp_sht,
850 sizeof(struct iscsi_sw_tcp_host), 1);
853 shost->transportt = iscsi_sw_tcp_scsi_transport;
854 shost->cmd_per_lun = qdepth;
855 shost->max_lun = iscsi_max_lun;
857 shost->max_channel = 0;
858 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
860 if (iscsi_host_add(shost, NULL))
863 cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
865 sizeof(struct iscsi_tcp_task) +
866 sizeof(struct iscsi_sw_tcp_hdrbuf),
870 session = cls_session->dd_data;
871 tcp_sw_host = iscsi_host_priv(shost);
872 tcp_sw_host->session = session;
874 shost->can_queue = session->scsi_cmds_max;
875 if (iscsi_tcp_r2tpool_alloc(session))
880 iscsi_session_teardown(cls_session);
882 iscsi_host_remove(shost);
884 iscsi_host_free(shost);
888 static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
890 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
892 iscsi_tcp_r2tpool_free(cls_session->dd_data);
893 iscsi_session_teardown(cls_session);
895 iscsi_host_remove(shost);
896 iscsi_host_free(shost);
899 static umode_t iscsi_sw_tcp_attr_is_visible(int param_type, int param)
901 switch (param_type) {
902 case ISCSI_HOST_PARAM:
904 case ISCSI_HOST_PARAM_NETDEV_NAME:
905 case ISCSI_HOST_PARAM_HWADDRESS:
906 case ISCSI_HOST_PARAM_IPADDRESS:
907 case ISCSI_HOST_PARAM_INITIATOR_NAME:
914 case ISCSI_PARAM_MAX_RECV_DLENGTH:
915 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
916 case ISCSI_PARAM_HDRDGST_EN:
917 case ISCSI_PARAM_DATADGST_EN:
918 case ISCSI_PARAM_CONN_ADDRESS:
919 case ISCSI_PARAM_CONN_PORT:
920 case ISCSI_PARAM_LOCAL_PORT:
921 case ISCSI_PARAM_EXP_STATSN:
922 case ISCSI_PARAM_PERSISTENT_ADDRESS:
923 case ISCSI_PARAM_PERSISTENT_PORT:
924 case ISCSI_PARAM_PING_TMO:
925 case ISCSI_PARAM_RECV_TMO:
926 case ISCSI_PARAM_INITIAL_R2T_EN:
927 case ISCSI_PARAM_MAX_R2T:
928 case ISCSI_PARAM_IMM_DATA_EN:
929 case ISCSI_PARAM_FIRST_BURST:
930 case ISCSI_PARAM_MAX_BURST:
931 case ISCSI_PARAM_PDU_INORDER_EN:
932 case ISCSI_PARAM_DATASEQ_INORDER_EN:
933 case ISCSI_PARAM_ERL:
934 case ISCSI_PARAM_TARGET_NAME:
935 case ISCSI_PARAM_TPGT:
936 case ISCSI_PARAM_USERNAME:
937 case ISCSI_PARAM_PASSWORD:
938 case ISCSI_PARAM_USERNAME_IN:
939 case ISCSI_PARAM_PASSWORD_IN:
940 case ISCSI_PARAM_FAST_ABORT:
941 case ISCSI_PARAM_ABORT_TMO:
942 case ISCSI_PARAM_LU_RESET_TMO:
943 case ISCSI_PARAM_TGT_RESET_TMO:
944 case ISCSI_PARAM_IFACE_NAME:
945 case ISCSI_PARAM_INITIATOR_NAME:
955 static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
957 struct iscsi_sw_tcp_host *tcp_sw_host = iscsi_host_priv(sdev->host);
958 struct iscsi_session *session = tcp_sw_host->session;
959 struct iscsi_conn *conn = session->leadconn;
961 if (conn->datadgst_en)
962 sdev->request_queue->backing_dev_info->capabilities
963 |= BDI_CAP_STABLE_WRITES;
964 blk_queue_dma_alignment(sdev->request_queue, 0);
968 static struct scsi_host_template iscsi_sw_tcp_sht = {
969 .module = THIS_MODULE,
970 .name = "iSCSI Initiator over TCP/IP",
971 .queuecommand = iscsi_queuecommand,
972 .change_queue_depth = scsi_change_queue_depth,
973 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
974 .sg_tablesize = 4096,
975 .max_sectors = 0xFFFF,
976 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
977 .eh_timed_out = iscsi_eh_cmd_timed_out,
978 .eh_abort_handler = iscsi_eh_abort,
979 .eh_device_reset_handler= iscsi_eh_device_reset,
980 .eh_target_reset_handler = iscsi_eh_recover_target,
981 .dma_boundary = PAGE_SIZE - 1,
982 .slave_configure = iscsi_sw_tcp_slave_configure,
983 .target_alloc = iscsi_target_alloc,
984 .proc_name = "iscsi_tcp",
986 .track_queue_depth = 1,
989 static struct iscsi_transport iscsi_sw_tcp_transport = {
990 .owner = THIS_MODULE,
992 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
994 /* session management */
995 .create_session = iscsi_sw_tcp_session_create,
996 .destroy_session = iscsi_sw_tcp_session_destroy,
997 /* connection management */
998 .create_conn = iscsi_sw_tcp_conn_create,
999 .bind_conn = iscsi_sw_tcp_conn_bind,
1000 .destroy_conn = iscsi_sw_tcp_conn_destroy,
1001 .attr_is_visible = iscsi_sw_tcp_attr_is_visible,
1002 .set_param = iscsi_sw_tcp_conn_set_param,
1003 .get_conn_param = iscsi_sw_tcp_conn_get_param,
1004 .get_session_param = iscsi_session_get_param,
1005 .start_conn = iscsi_conn_start,
1006 .stop_conn = iscsi_sw_tcp_conn_stop,
1007 /* iscsi host params */
1008 .get_host_param = iscsi_sw_tcp_host_get_param,
1009 .set_host_param = iscsi_host_set_param,
1011 .send_pdu = iscsi_conn_send_pdu,
1012 .get_stats = iscsi_sw_tcp_conn_get_stats,
1013 /* iscsi task/cmd helpers */
1014 .init_task = iscsi_tcp_task_init,
1015 .xmit_task = iscsi_tcp_task_xmit,
1016 .cleanup_task = iscsi_tcp_cleanup_task,
1017 /* low level pdu helpers */
1018 .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
1019 .init_pdu = iscsi_sw_tcp_pdu_init,
1020 .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
1022 .session_recovery_timedout = iscsi_session_recovery_timedout,
1025 static int __init iscsi_sw_tcp_init(void)
1027 if (iscsi_max_lun < 1) {
1028 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
1033 iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
1034 &iscsi_sw_tcp_transport);
1035 if (!iscsi_sw_tcp_scsi_transport)
1041 static void __exit iscsi_sw_tcp_exit(void)
1043 iscsi_unregister_transport(&iscsi_sw_tcp_transport);
1046 module_init(iscsi_sw_tcp_init);
1047 module_exit(iscsi_sw_tcp_exit);