1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e48354ceSNicholas Bellinger /*******************************************************************************
3e48354ceSNicholas Bellinger  * This file contains the login functions used by the iSCSI Target driver.
4e48354ceSNicholas Bellinger  *
54c76251eSNicholas Bellinger  * (c) Copyright 2007-2013 Datera, Inc.
6e48354ceSNicholas Bellinger  *
7e48354ceSNicholas Bellinger  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8e48354ceSNicholas Bellinger  *
9e48354ceSNicholas Bellinger  ******************************************************************************/
10e48354ceSNicholas Bellinger 
1169110e3cSHerbert Xu #include <crypto/hash.h>
12da5df620SRussell King #include <linux/module.h>
13e48354ceSNicholas Bellinger #include <linux/string.h>
14e48354ceSNicholas Bellinger #include <linux/kthread.h>
153f07c014SIngo Molnar #include <linux/sched/signal.h>
1640401530SAl Viro #include <linux/idr.h>
178dcf07beSBart Van Assche #include <linux/tcp.h>        /* TCP_NODELAY */
18c4e446bfSChristoph Hellwig #include <net/ip.h>
198dcf07beSBart Van Assche #include <net/ipv6.h>         /* ipv6_addr_v4mapped() */
20e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
21e48354ceSNicholas Bellinger #include <target/target_core_base.h>
22c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
23e48354ceSNicholas Bellinger 
2467f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h>
2567f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_stat.h>
26e48354ceSNicholas Bellinger #include "iscsi_target_device.h"
27e48354ceSNicholas Bellinger #include "iscsi_target_nego.h"
28e48354ceSNicholas Bellinger #include "iscsi_target_erl0.h"
29e48354ceSNicholas Bellinger #include "iscsi_target_erl2.h"
30e48354ceSNicholas Bellinger #include "iscsi_target_login.h"
31e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
32e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
33e48354ceSNicholas Bellinger #include "iscsi_target.h"
34e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
35e48354ceSNicholas Bellinger 
36baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
37baa4d64bSNicholas Bellinger 
iscsi_login_init_conn(struct iscsit_conn * conn)38be36d683SMax Gurtovoy static struct iscsi_login *iscsi_login_init_conn(struct iscsit_conn *conn)
39e48354ceSNicholas Bellinger {
40baa4d64bSNicholas Bellinger 	struct iscsi_login *login;
41baa4d64bSNicholas Bellinger 
42baa4d64bSNicholas Bellinger 	login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
43baa4d64bSNicholas Bellinger 	if (!login) {
44baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for struct iscsi_login.\n");
45baa4d64bSNicholas Bellinger 		return NULL;
46baa4d64bSNicholas Bellinger 	}
47a91eb7d9SNicholas Bellinger 	conn->login = login;
48baa4d64bSNicholas Bellinger 	login->conn = conn;
49baa4d64bSNicholas Bellinger 	login->first_request = 1;
50baa4d64bSNicholas Bellinger 
51baa4d64bSNicholas Bellinger 	login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
52baa4d64bSNicholas Bellinger 	if (!login->req_buf) {
53baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for response buffer.\n");
54baa4d64bSNicholas Bellinger 		goto out_login;
55baa4d64bSNicholas Bellinger 	}
56baa4d64bSNicholas Bellinger 
57baa4d64bSNicholas Bellinger 	login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
58baa4d64bSNicholas Bellinger 	if (!login->rsp_buf) {
59baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for request buffer.\n");
60baa4d64bSNicholas Bellinger 		goto out_req_buf;
61baa4d64bSNicholas Bellinger 	}
62baa4d64bSNicholas Bellinger 
63baa4d64bSNicholas Bellinger 	conn->conn_login = login;
64e48354ceSNicholas Bellinger 
65baa4d64bSNicholas Bellinger 	return login;
66baa4d64bSNicholas Bellinger 
67baa4d64bSNicholas Bellinger out_req_buf:
68baa4d64bSNicholas Bellinger 	kfree(login->req_buf);
69baa4d64bSNicholas Bellinger out_login:
70baa4d64bSNicholas Bellinger 	kfree(login);
71baa4d64bSNicholas Bellinger 	return NULL;
72e48354ceSNicholas Bellinger }
73e48354ceSNicholas Bellinger 
74e48354ceSNicholas Bellinger /*
75e48354ceSNicholas Bellinger  * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
76be36d683SMax Gurtovoy  * per struct iscsit_conn libcrypto contexts for crc32c and crc32-intel
77e48354ceSNicholas Bellinger  */
iscsi_login_setup_crypto(struct iscsit_conn * conn)78be36d683SMax Gurtovoy int iscsi_login_setup_crypto(struct iscsit_conn *conn)
79e48354ceSNicholas Bellinger {
8069110e3cSHerbert Xu 	struct crypto_ahash *tfm;
8169110e3cSHerbert Xu 
82e48354ceSNicholas Bellinger 	/*
83e48354ceSNicholas Bellinger 	 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
84e48354ceSNicholas Bellinger 	 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
85e48354ceSNicholas Bellinger 	 * to software 1x8 byte slicing from crc32c.ko
86e48354ceSNicholas Bellinger 	 */
8769110e3cSHerbert Xu 	tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
8869110e3cSHerbert Xu 	if (IS_ERR(tfm)) {
8969110e3cSHerbert Xu 		pr_err("crypto_alloc_ahash() failed\n");
90e48354ceSNicholas Bellinger 		return -ENOMEM;
91e48354ceSNicholas Bellinger 	}
92e48354ceSNicholas Bellinger 
9369110e3cSHerbert Xu 	conn->conn_rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
9469110e3cSHerbert Xu 	if (!conn->conn_rx_hash) {
9569110e3cSHerbert Xu 		pr_err("ahash_request_alloc() failed for conn_rx_hash\n");
9669110e3cSHerbert Xu 		crypto_free_ahash(tfm);
97e48354ceSNicholas Bellinger 		return -ENOMEM;
98e48354ceSNicholas Bellinger 	}
9969110e3cSHerbert Xu 	ahash_request_set_callback(conn->conn_rx_hash, 0, NULL, NULL);
10069110e3cSHerbert Xu 
10169110e3cSHerbert Xu 	conn->conn_tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
10269110e3cSHerbert Xu 	if (!conn->conn_tx_hash) {
10369110e3cSHerbert Xu 		pr_err("ahash_request_alloc() failed for conn_tx_hash\n");
10469110e3cSHerbert Xu 		ahash_request_free(conn->conn_rx_hash);
10569110e3cSHerbert Xu 		conn->conn_rx_hash = NULL;
10669110e3cSHerbert Xu 		crypto_free_ahash(tfm);
10769110e3cSHerbert Xu 		return -ENOMEM;
10869110e3cSHerbert Xu 	}
10969110e3cSHerbert Xu 	ahash_request_set_callback(conn->conn_tx_hash, 0, NULL, NULL);
110e48354ceSNicholas Bellinger 
111e48354ceSNicholas Bellinger 	return 0;
112e48354ceSNicholas Bellinger }
113e48354ceSNicholas Bellinger 
iscsi_login_check_initiator_version(struct iscsit_conn * conn,u8 version_max,u8 version_min)114e48354ceSNicholas Bellinger static int iscsi_login_check_initiator_version(
115be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
116e48354ceSNicholas Bellinger 	u8 version_max,
117e48354ceSNicholas Bellinger 	u8 version_min)
118e48354ceSNicholas Bellinger {
119e48354ceSNicholas Bellinger 	if ((version_max != 0x00) || (version_min != 0x00)) {
120e48354ceSNicholas Bellinger 		pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
121e48354ceSNicholas Bellinger 			" version Min/Max 0x%02x/0x%02x, rejecting login.\n",
122e48354ceSNicholas Bellinger 			version_min, version_max);
123e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
124e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_VERSION);
125e48354ceSNicholas Bellinger 		return -1;
126e48354ceSNicholas Bellinger 	}
127e48354ceSNicholas Bellinger 
128e48354ceSNicholas Bellinger 	return 0;
129e48354ceSNicholas Bellinger }
130e48354ceSNicholas Bellinger 
iscsi_check_for_session_reinstatement(struct iscsit_conn * conn)131be36d683SMax Gurtovoy int iscsi_check_for_session_reinstatement(struct iscsit_conn *conn)
132e48354ceSNicholas Bellinger {
133e48354ceSNicholas Bellinger 	int sessiontype;
134e48354ceSNicholas Bellinger 	struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
135e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
1360873fe44SMax Gurtovoy 	struct iscsit_session *sess = NULL, *sess_p = NULL;
137e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
138e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
139e48354ceSNicholas Bellinger 
140e48354ceSNicholas Bellinger 	initiatorname_param = iscsi_find_param_from_key(
141e48354ceSNicholas Bellinger 			INITIATORNAME, conn->param_list);
142e48354ceSNicholas Bellinger 	sessiontype_param = iscsi_find_param_from_key(
143e48354ceSNicholas Bellinger 			SESSIONTYPE, conn->param_list);
1441c5c12c6SRoland Dreier 	if (!initiatorname_param || !sessiontype_param) {
1451c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1461c5c12c6SRoland Dreier 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
147e48354ceSNicholas Bellinger 		return -1;
1481c5c12c6SRoland Dreier 	}
149e48354ceSNicholas Bellinger 
150e48354ceSNicholas Bellinger 	sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
151e48354ceSNicholas Bellinger 
152e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
153e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
154e48354ceSNicholas Bellinger 			sess_list) {
155e48354ceSNicholas Bellinger 
1568359cf43SJörn Engel 		sess_p = se_sess->fabric_sess_ptr;
157e48354ceSNicholas Bellinger 		spin_lock(&sess_p->conn_lock);
158e48354ceSNicholas Bellinger 		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
159e48354ceSNicholas Bellinger 		    atomic_read(&sess_p->session_logout) ||
16057c46e9fSMaurizio Lombardi 		    atomic_read(&sess_p->session_close) ||
161e48354ceSNicholas Bellinger 		    (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
162e48354ceSNicholas Bellinger 			spin_unlock(&sess_p->conn_lock);
163e48354ceSNicholas Bellinger 			continue;
164e48354ceSNicholas Bellinger 		}
1658359cf43SJörn Engel 		if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
1668359cf43SJörn Engel 		   (!strcmp(sess_p->sess_ops->InitiatorName,
1678359cf43SJörn Engel 			    initiatorname_param->value) &&
168e48354ceSNicholas Bellinger 		   (sess_p->sess_ops->SessionType == sessiontype))) {
169e48354ceSNicholas Bellinger 			atomic_set(&sess_p->session_reinstatement, 1);
170197b806aSNicholas Bellinger 			atomic_set(&sess_p->session_fall_back_to_erl0, 1);
17157c46e9fSMaurizio Lombardi 			atomic_set(&sess_p->session_close, 1);
172e48354ceSNicholas Bellinger 			spin_unlock(&sess_p->conn_lock);
173e48354ceSNicholas Bellinger 			iscsit_inc_session_usage_count(sess_p);
174e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess_p);
175e48354ceSNicholas Bellinger 			sess = sess_p;
176e48354ceSNicholas Bellinger 			break;
177e48354ceSNicholas Bellinger 		}
178e48354ceSNicholas Bellinger 		spin_unlock(&sess_p->conn_lock);
179e48354ceSNicholas Bellinger 	}
180e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
181e48354ceSNicholas Bellinger 	/*
182e48354ceSNicholas Bellinger 	 * If the Time2Retain handler has expired, the session is already gone.
183e48354ceSNicholas Bellinger 	 */
184e48354ceSNicholas Bellinger 	if (!sess)
185e48354ceSNicholas Bellinger 		return 0;
186e48354ceSNicholas Bellinger 
187e48354ceSNicholas Bellinger 	pr_debug("%s iSCSI Session SID %u is still active for %s,"
1880d5efb8aSBart Van Assche 		" performing session reinstatement.\n", (sessiontype) ?
189e48354ceSNicholas Bellinger 		"Discovery" : "Normal", sess->sid,
190e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
191e48354ceSNicholas Bellinger 
192e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
193e48354ceSNicholas Bellinger 	if (sess->session_state == TARG_SESS_STATE_FAILED) {
194e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
195e48354ceSNicholas Bellinger 		iscsit_dec_session_usage_count(sess);
19699367f01SNicholas Bellinger 		return 0;
197e48354ceSNicholas Bellinger 	}
198e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
199e48354ceSNicholas Bellinger 
200e48354ceSNicholas Bellinger 	iscsit_stop_session(sess, 1, 1);
201e48354ceSNicholas Bellinger 	iscsit_dec_session_usage_count(sess);
202e48354ceSNicholas Bellinger 
20399367f01SNicholas Bellinger 	return 0;
204e48354ceSNicholas Bellinger }
205e48354ceSNicholas Bellinger 
iscsi_login_set_conn_values(struct iscsit_session * sess,struct iscsit_conn * conn,__be16 cid)2066787ab81SJason A. Donenfeld static int iscsi_login_set_conn_values(
2070873fe44SMax Gurtovoy 	struct iscsit_session *sess,
208be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
20950e5c87dSChristoph Hellwig 	__be16 cid)
210e48354ceSNicholas Bellinger {
2116787ab81SJason A. Donenfeld 	int ret;
212e48354ceSNicholas Bellinger 	conn->sess		= sess;
21350e5c87dSChristoph Hellwig 	conn->cid		= be16_to_cpu(cid);
214e48354ceSNicholas Bellinger 	/*
215e48354ceSNicholas Bellinger 	 * Generate a random Status sequence number (statsn) for the new
216e48354ceSNicholas Bellinger 	 * iSCSI connection.
217e48354ceSNicholas Bellinger 	 */
2186787ab81SJason A. Donenfeld 	ret = get_random_bytes_wait(&conn->stat_sn, sizeof(u32));
2196787ab81SJason A. Donenfeld 	if (unlikely(ret))
2206787ab81SJason A. Donenfeld 		return ret;
221e48354ceSNicholas Bellinger 
222e48354ceSNicholas Bellinger 	mutex_lock(&auth_id_lock);
223e48354ceSNicholas Bellinger 	conn->auth_id		= iscsit_global->auth_id++;
224e48354ceSNicholas Bellinger 	mutex_unlock(&auth_id_lock);
2256787ab81SJason A. Donenfeld 	return 0;
226e48354ceSNicholas Bellinger }
227e48354ceSNicholas Bellinger 
iscsi_change_param_sprintf(struct iscsit_conn * conn,const char * fmt,...)228d2faaefbSVarun Prakash __printf(2, 3) int iscsi_change_param_sprintf(
229be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
23079d59d08SRoland Dreier 	const char *fmt, ...)
23179d59d08SRoland Dreier {
23279d59d08SRoland Dreier 	va_list args;
23379d59d08SRoland Dreier 	unsigned char buf[64];
23479d59d08SRoland Dreier 
23579d59d08SRoland Dreier 	memset(buf, 0, sizeof buf);
23679d59d08SRoland Dreier 
23779d59d08SRoland Dreier 	va_start(args, fmt);
23879d59d08SRoland Dreier 	vsnprintf(buf, sizeof buf, fmt, args);
23979d59d08SRoland Dreier 	va_end(args);
24079d59d08SRoland Dreier 
24179d59d08SRoland Dreier 	if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
24279d59d08SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
24379d59d08SRoland Dreier 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
24479d59d08SRoland Dreier 		return -1;
24579d59d08SRoland Dreier 	}
24679d59d08SRoland Dreier 
24779d59d08SRoland Dreier 	return 0;
24879d59d08SRoland Dreier }
249d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsi_change_param_sprintf);
25079d59d08SRoland Dreier 
251e48354ceSNicholas Bellinger /*
252e48354ceSNicholas Bellinger  *	This is the leading connection of a new session,
253e48354ceSNicholas Bellinger  *	or session reinstatement.
254e48354ceSNicholas Bellinger  */
iscsi_login_zero_tsih_s1(struct iscsit_conn * conn,unsigned char * buf)255e48354ceSNicholas Bellinger static int iscsi_login_zero_tsih_s1(
256be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
257e48354ceSNicholas Bellinger 	unsigned char *buf)
258e48354ceSNicholas Bellinger {
2590873fe44SMax Gurtovoy 	struct iscsit_session *sess = NULL;
260e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
26113b5533aSBenjamin Wang 	int ret;
262e48354ceSNicholas Bellinger 
2630873fe44SMax Gurtovoy 	sess = kzalloc(sizeof(struct iscsit_session), GFP_KERNEL);
264e48354ceSNicholas Bellinger 	if (!sess) {
265e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
266e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
267e48354ceSNicholas Bellinger 		pr_err("Could not allocate memory for session\n");
2680957627aSNicholas Bellinger 		return -ENOMEM;
269e48354ceSNicholas Bellinger 	}
270e48354ceSNicholas Bellinger 
2717915919bSVincent Pelletier 	if (iscsi_login_set_conn_values(sess, conn, pdu->cid))
2727915919bSVincent Pelletier 		goto free_sess;
2737915919bSVincent Pelletier 
274e48354ceSNicholas Bellinger 	sess->init_task_tag	= pdu->itt;
2758359cf43SJörn Engel 	memcpy(&sess->isid, pdu->isid, 6);
27650e5c87dSChristoph Hellwig 	sess->exp_cmd_sn	= be32_to_cpu(pdu->cmdsn);
277e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->sess_conn_list);
278e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
279e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->cr_active_list);
280e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->cr_inactive_list);
281e48354ceSNicholas Bellinger 	init_completion(&sess->async_msg_comp);
282e48354ceSNicholas Bellinger 	init_completion(&sess->reinstatement_comp);
283e48354ceSNicholas Bellinger 	init_completion(&sess->session_wait_comp);
284e48354ceSNicholas Bellinger 	init_completion(&sess->session_waiting_on_uc_comp);
285e48354ceSNicholas Bellinger 	mutex_init(&sess->cmdsn_mutex);
286e48354ceSNicholas Bellinger 	spin_lock_init(&sess->conn_lock);
287e48354ceSNicholas Bellinger 	spin_lock_init(&sess->cr_a_lock);
288e48354ceSNicholas Bellinger 	spin_lock_init(&sess->cr_i_lock);
289e48354ceSNicholas Bellinger 	spin_lock_init(&sess->session_usage_lock);
290e48354ceSNicholas Bellinger 	spin_lock_init(&sess->ttt_lock);
291e48354ceSNicholas Bellinger 
292f7c9564aSKees Cook 	timer_setup(&sess->time2retain_timer,
293f7c9564aSKees Cook 		    iscsit_handle_time2retain_timeout, 0);
2948a47aa9dSBart Van Assche 
29531ff0ceeSMatthew Wilcox 	ret = ida_alloc(&sess_ida, GFP_KERNEL);
29613b5533aSBenjamin Wang 	if (ret < 0) {
29731ff0ceeSMatthew Wilcox 		pr_err("Session ID allocation failed %d\n", ret);
29813b5533aSBenjamin Wang 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
29913b5533aSBenjamin Wang 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
30026abc916SMike Christie 		goto free_sess;
30113b5533aSBenjamin Wang 	}
30213b5533aSBenjamin Wang 
30331ff0ceeSMatthew Wilcox 	sess->session_index = ret;
304e48354ceSNicholas Bellinger 	sess->creation_time = get_jiffies_64();
305e48354ceSNicholas Bellinger 	/*
306e48354ceSNicholas Bellinger 	 * The FFP CmdSN window values will be allocated from the TPG's
307e48354ceSNicholas Bellinger 	 * Initiator Node's ACL once the login has been successfully completed.
308e48354ceSNicholas Bellinger 	 */
309109e2381SRoland Dreier 	atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn));
310e48354ceSNicholas Bellinger 
311e48354ceSNicholas Bellinger 	sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
312e48354ceSNicholas Bellinger 	if (!sess->sess_ops) {
313e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
314e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
315e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for"
316e48354ceSNicholas Bellinger 				" struct iscsi_sess_ops.\n");
31731ff0ceeSMatthew Wilcox 		goto free_id;
318e48354ceSNicholas Bellinger 	}
319e48354ceSNicholas Bellinger 
320317f8971SBart Van Assche 	sess->se_sess = transport_alloc_session(TARGET_PROT_NORMAL);
3210957627aSNicholas Bellinger 	if (IS_ERR(sess->se_sess)) {
322e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
323e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
32426abc916SMike Christie 		goto free_ops;
325e48354ceSNicholas Bellinger 	}
326e48354ceSNicholas Bellinger 
327e48354ceSNicholas Bellinger 	return 0;
32826abc916SMike Christie 
32926abc916SMike Christie free_ops:
33026abc916SMike Christie 	kfree(sess->sess_ops);
33131ff0ceeSMatthew Wilcox free_id:
33231ff0ceeSMatthew Wilcox 	ida_free(&sess_ida, sess->session_index);
33326abc916SMike Christie free_sess:
33426abc916SMike Christie 	kfree(sess);
33526abc916SMike Christie 	conn->sess = NULL;
33626abc916SMike Christie 	return -ENOMEM;
337e48354ceSNicholas Bellinger }
338e48354ceSNicholas Bellinger 
iscsi_login_zero_tsih_s2(struct iscsit_conn * conn)339e48354ceSNicholas Bellinger static int iscsi_login_zero_tsih_s2(
340be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
341e48354ceSNicholas Bellinger {
342e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na;
3430873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
344e52b904bSDmitry Bogdanov 	struct iscsi_param *param;
34503aa2070SNicholas Bellinger 	bool iser = false;
346e48354ceSNicholas Bellinger 
347e48354ceSNicholas Bellinger 	sess->tpg = conn->tpg;
348e48354ceSNicholas Bellinger 
349e48354ceSNicholas Bellinger 	/*
350e48354ceSNicholas Bellinger 	 * Assign a new TPG Session Handle.  Note this is protected with
351e48354ceSNicholas Bellinger 	 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
352e48354ceSNicholas Bellinger 	 */
35360bfcf8eSAndy Grover 	sess->tsih = ++sess->tpg->ntsih;
354e48354ceSNicholas Bellinger 	if (!sess->tsih)
35560bfcf8eSAndy Grover 		sess->tsih = ++sess->tpg->ntsih;
356e48354ceSNicholas Bellinger 
357e48354ceSNicholas Bellinger 	/*
358e48354ceSNicholas Bellinger 	 * Create the default params from user defined values..
359e48354ceSNicholas Bellinger 	 */
360e48354ceSNicholas Bellinger 	if (iscsi_copy_param_list(&conn->param_list,
36160bfcf8eSAndy Grover 				conn->tpg->param_list, 1) < 0) {
362e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
363e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
364e48354ceSNicholas Bellinger 		return -1;
365e48354ceSNicholas Bellinger 	}
366e48354ceSNicholas Bellinger 
36703aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
36803aa2070SNicholas Bellinger 		iser = true;
36903aa2070SNicholas Bellinger 
37003aa2070SNicholas Bellinger 	iscsi_set_keys_to_negotiate(conn->param_list, iser);
371e48354ceSNicholas Bellinger 
372e48354ceSNicholas Bellinger 	if (sess->sess_ops->SessionType)
373e48354ceSNicholas Bellinger 		return iscsi_set_keys_irrelevant_for_discovery(
374e48354ceSNicholas Bellinger 				conn->param_list);
375e48354ceSNicholas Bellinger 
376e48354ceSNicholas Bellinger 	na = iscsit_tpg_get_node_attrib(sess);
377e48354ceSNicholas Bellinger 
378e48354ceSNicholas Bellinger 	/*
379e52b904bSDmitry Bogdanov 	 * If ACL allows non-authorized access in TPG with CHAP,
380e52b904bSDmitry Bogdanov 	 * then set None to AuthMethod.
381e52b904bSDmitry Bogdanov 	 */
382e52b904bSDmitry Bogdanov 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
383e52b904bSDmitry Bogdanov 	if (param && !strstr(param->value, NONE)) {
384e52b904bSDmitry Bogdanov 		if (!iscsi_conn_auth_required(conn))
385e52b904bSDmitry Bogdanov 			if (iscsi_change_param_sprintf(conn, "AuthMethod=%s",
386e52b904bSDmitry Bogdanov 						       NONE))
387e52b904bSDmitry Bogdanov 				return -1;
388e52b904bSDmitry Bogdanov 	}
389e52b904bSDmitry Bogdanov 
390e52b904bSDmitry Bogdanov 	/*
391e48354ceSNicholas Bellinger 	 * Need to send TargetPortalGroupTag back in first login response
392e48354ceSNicholas Bellinger 	 * on any iSCSI connection where the Initiator provides TargetName.
393e48354ceSNicholas Bellinger 	 * See 5.3.1.  Login Phase Start
394e48354ceSNicholas Bellinger 	 *
395e48354ceSNicholas Bellinger 	 * In our case, we have already located the struct iscsi_tiqn at this point.
396e48354ceSNicholas Bellinger 	 */
39779d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
398e48354ceSNicholas Bellinger 		return -1;
399e48354ceSNicholas Bellinger 
400e48354ceSNicholas Bellinger 	/*
401e48354ceSNicholas Bellinger 	 * Workaround for Initiators that have broken connection recovery logic.
402e48354ceSNicholas Bellinger 	 *
403e48354ceSNicholas Bellinger 	 * "We would really like to get rid of this." Linux-iSCSI.org team
404e48354ceSNicholas Bellinger 	 */
40579d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
406e48354ceSNicholas Bellinger 		return -1;
407e48354ceSNicholas Bellinger 
40803aa2070SNicholas Bellinger 	/*
40903aa2070SNicholas Bellinger 	 * Set RDMAExtensions=Yes by default for iSER enabled network portals
41003aa2070SNicholas Bellinger 	 */
41103aa2070SNicholas Bellinger 	if (iser) {
41203aa2070SNicholas Bellinger 		struct iscsi_param *param;
41303aa2070SNicholas Bellinger 		unsigned long mrdsl, off;
41403aa2070SNicholas Bellinger 		int rc;
41503aa2070SNicholas Bellinger 
41679d59d08SRoland Dreier 		if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
41703aa2070SNicholas Bellinger 			return -1;
41879d59d08SRoland Dreier 
41903aa2070SNicholas Bellinger 		/*
42003aa2070SNicholas Bellinger 		 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
4213fc6a642SColin Ian King 		 * Immediate Data + Unsolicited Data-OUT if necessary..
42203aa2070SNicholas Bellinger 		 */
42303aa2070SNicholas Bellinger 		param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
42403aa2070SNicholas Bellinger 						  conn->param_list);
42503aa2070SNicholas Bellinger 		if (!param) {
42603aa2070SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
42703aa2070SNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
42803aa2070SNicholas Bellinger 			return -1;
42903aa2070SNicholas Bellinger 		}
43057103d7fSJingoo Han 		rc = kstrtoul(param->value, 0, &mrdsl);
43103aa2070SNicholas Bellinger 		if (rc < 0) {
43203aa2070SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
43303aa2070SNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
43403aa2070SNicholas Bellinger 			return -1;
43503aa2070SNicholas Bellinger 		}
43603aa2070SNicholas Bellinger 		off = mrdsl % PAGE_SIZE;
43703aa2070SNicholas Bellinger 		if (!off)
43852d0aa79SNicholas Bellinger 			goto check_prot;
43903aa2070SNicholas Bellinger 
44003aa2070SNicholas Bellinger 		if (mrdsl < PAGE_SIZE)
44103aa2070SNicholas Bellinger 			mrdsl = PAGE_SIZE;
44203aa2070SNicholas Bellinger 		else
44303aa2070SNicholas Bellinger 			mrdsl -= off;
44403aa2070SNicholas Bellinger 
44503aa2070SNicholas Bellinger 		pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
44603aa2070SNicholas Bellinger 			" to PAGE_SIZE\n", mrdsl);
44703aa2070SNicholas Bellinger 
44879d59d08SRoland Dreier 		if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
44903aa2070SNicholas Bellinger 			return -1;
45052d0aa79SNicholas Bellinger 		/*
45152d0aa79SNicholas Bellinger 		 * ISER currently requires that ImmediateData + Unsolicited
45252d0aa79SNicholas Bellinger 		 * Data be disabled when protection / signature MRs are enabled.
45352d0aa79SNicholas Bellinger 		 */
45452d0aa79SNicholas Bellinger check_prot:
45552d0aa79SNicholas Bellinger 		if (sess->se_sess->sup_prot_ops &
45652d0aa79SNicholas Bellinger 		   (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
45752d0aa79SNicholas Bellinger 		    TARGET_PROT_DOUT_INSERT)) {
45852d0aa79SNicholas Bellinger 
45979d59d08SRoland Dreier 			if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
46052d0aa79SNicholas Bellinger 				return -1;
46152d0aa79SNicholas Bellinger 
46279d59d08SRoland Dreier 			if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
46352d0aa79SNicholas Bellinger 				return -1;
46479d59d08SRoland Dreier 
46552d0aa79SNicholas Bellinger 			pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
46652d0aa79SNicholas Bellinger 				 " T10-PI enabled ISER session\n");
46752d0aa79SNicholas Bellinger 		}
46803aa2070SNicholas Bellinger 	}
469e48354ceSNicholas Bellinger 
470e48354ceSNicholas Bellinger 	return 0;
471e48354ceSNicholas Bellinger }
472e48354ceSNicholas Bellinger 
iscsi_login_non_zero_tsih_s1(struct iscsit_conn * conn,unsigned char * buf)473e48354ceSNicholas Bellinger static int iscsi_login_non_zero_tsih_s1(
474be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
475e48354ceSNicholas Bellinger 	unsigned char *buf)
476e48354ceSNicholas Bellinger {
477e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
478e48354ceSNicholas Bellinger 
4796787ab81SJason A. Donenfeld 	return iscsi_login_set_conn_values(NULL, conn, pdu->cid);
480e48354ceSNicholas Bellinger }
481e48354ceSNicholas Bellinger 
482e48354ceSNicholas Bellinger /*
483e48354ceSNicholas Bellinger  *	Add a new connection to an existing session.
484e48354ceSNicholas Bellinger  */
iscsi_login_non_zero_tsih_s2(struct iscsit_conn * conn,unsigned char * buf)485e48354ceSNicholas Bellinger static int iscsi_login_non_zero_tsih_s2(
486be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
487e48354ceSNicholas Bellinger 	unsigned char *buf)
488e48354ceSNicholas Bellinger {
489e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
4900873fe44SMax Gurtovoy 	struct iscsit_session *sess = NULL, *sess_p = NULL;
491e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
492e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
493e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
49403aa2070SNicholas Bellinger 	bool iser = false;
495e48354ceSNicholas Bellinger 
496e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
497e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
498e48354ceSNicholas Bellinger 			sess_list) {
499e48354ceSNicholas Bellinger 
5000873fe44SMax Gurtovoy 		sess_p = (struct iscsit_session *)se_sess->fabric_sess_ptr;
501e48354ceSNicholas Bellinger 		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
502e48354ceSNicholas Bellinger 		    atomic_read(&sess_p->session_logout) ||
50357c46e9fSMaurizio Lombardi 		    atomic_read(&sess_p->session_close) ||
504e48354ceSNicholas Bellinger 		   (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
505e48354ceSNicholas Bellinger 			continue;
5068359cf43SJörn Engel 		if (!memcmp(sess_p->isid, pdu->isid, 6) &&
50750e5c87dSChristoph Hellwig 		     (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
508e48354ceSNicholas Bellinger 			iscsit_inc_session_usage_count(sess_p);
509e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess_p);
510e48354ceSNicholas Bellinger 			sess = sess_p;
511e48354ceSNicholas Bellinger 			break;
512e48354ceSNicholas Bellinger 		}
513e48354ceSNicholas Bellinger 	}
514e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
515e48354ceSNicholas Bellinger 
516e48354ceSNicholas Bellinger 	/*
517e48354ceSNicholas Bellinger 	 * If the Time2Retain handler has expired, the session is already gone.
518e48354ceSNicholas Bellinger 	 */
519e48354ceSNicholas Bellinger 	if (!sess) {
520e48354ceSNicholas Bellinger 		pr_err("Initiator attempting to add a connection to"
521e48354ceSNicholas Bellinger 			" a non-existent session, rejecting iSCSI Login.\n");
522e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
523e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_SESSION);
524e48354ceSNicholas Bellinger 		return -1;
525e48354ceSNicholas Bellinger 	}
526e48354ceSNicholas Bellinger 
527e48354ceSNicholas Bellinger 	/*
528e48354ceSNicholas Bellinger 	 * Stop the Time2Retain timer if this is a failed session, we restart
529e48354ceSNicholas Bellinger 	 * the timer if the login is not successful.
530e48354ceSNicholas Bellinger 	 */
531e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
532e48354ceSNicholas Bellinger 	if (sess->session_state == TARG_SESS_STATE_FAILED)
533e48354ceSNicholas Bellinger 		atomic_set(&sess->session_continuation, 1);
534e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
535e48354ceSNicholas Bellinger 
5366787ab81SJason A. Donenfeld 	if (iscsi_login_set_conn_values(sess, conn, pdu->cid) < 0 ||
5376787ab81SJason A. Donenfeld 	    iscsi_copy_param_list(&conn->param_list,
53860bfcf8eSAndy Grover 			conn->tpg->param_list, 0) < 0) {
539e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
540e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
541e48354ceSNicholas Bellinger 		return -1;
542e48354ceSNicholas Bellinger 	}
543e48354ceSNicholas Bellinger 
54403aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
54503aa2070SNicholas Bellinger 		iser = true;
54603aa2070SNicholas Bellinger 
54703aa2070SNicholas Bellinger 	iscsi_set_keys_to_negotiate(conn->param_list, iser);
548e48354ceSNicholas Bellinger 	/*
549e48354ceSNicholas Bellinger 	 * Need to send TargetPortalGroupTag back in first login response
550e48354ceSNicholas Bellinger 	 * on any iSCSI connection where the Initiator provides TargetName.
551e48354ceSNicholas Bellinger 	 * See 5.3.1.  Login Phase Start
552e48354ceSNicholas Bellinger 	 *
553e48354ceSNicholas Bellinger 	 * In our case, we have already located the struct iscsi_tiqn at this point.
554e48354ceSNicholas Bellinger 	 */
55579d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
556e48354ceSNicholas Bellinger 		return -1;
557e48354ceSNicholas Bellinger 
558c04a6091SChristophe Vu-Brugier 	return 0;
559e48354ceSNicholas Bellinger }
560e48354ceSNicholas Bellinger 
iscsi_login_post_auth_non_zero_tsih(struct iscsit_conn * conn,u16 cid,u32 exp_statsn)561e48354ceSNicholas Bellinger int iscsi_login_post_auth_non_zero_tsih(
562be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
563e48354ceSNicholas Bellinger 	u16 cid,
564e48354ceSNicholas Bellinger 	u32 exp_statsn)
565e48354ceSNicholas Bellinger {
566be36d683SMax Gurtovoy 	struct iscsit_conn *conn_ptr = NULL;
567e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr = NULL;
5680873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
569e48354ceSNicholas Bellinger 
570e48354ceSNicholas Bellinger 	/*
571e48354ceSNicholas Bellinger 	 * By following item 5 in the login table,  if we have found
572e48354ceSNicholas Bellinger 	 * an existing ISID and a valid/existing TSIH and an existing
573e48354ceSNicholas Bellinger 	 * CID we do connection reinstatement.  Currently we dont not
574e48354ceSNicholas Bellinger 	 * support it so we send back an non-zero status class to the
575e48354ceSNicholas Bellinger 	 * initiator and release the new connection.
576e48354ceSNicholas Bellinger 	 */
577e48354ceSNicholas Bellinger 	conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
578ee1b1b9cSAndy Grover 	if (conn_ptr) {
579e48354ceSNicholas Bellinger 		pr_err("Connection exists with CID %hu for %s,"
580e48354ceSNicholas Bellinger 			" performing connection reinstatement.\n",
581e48354ceSNicholas Bellinger 			conn_ptr->cid, sess->sess_ops->InitiatorName);
582e48354ceSNicholas Bellinger 
583e48354ceSNicholas Bellinger 		iscsit_connection_reinstatement_rcfr(conn_ptr);
584e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn_ptr);
585e48354ceSNicholas Bellinger 	}
586e48354ceSNicholas Bellinger 
587e48354ceSNicholas Bellinger 	/*
588902ff860SColin Ian King 	 * Check for any connection recovery entries containing CID.
589e48354ceSNicholas Bellinger 	 * We use the original ExpStatSN sent in the first login request
590e48354ceSNicholas Bellinger 	 * to acknowledge commands for the failed connection.
591e48354ceSNicholas Bellinger 	 *
592e48354ceSNicholas Bellinger 	 * Also note that an explict logout may have already been sent,
593e48354ceSNicholas Bellinger 	 * but the response may not be sent due to additional connection
594e48354ceSNicholas Bellinger 	 * loss.
595e48354ceSNicholas Bellinger 	 */
596e48354ceSNicholas Bellinger 	if (sess->sess_ops->ErrorRecoveryLevel == 2) {
597e48354ceSNicholas Bellinger 		cr = iscsit_get_inactive_connection_recovery_entry(
598e48354ceSNicholas Bellinger 				sess, cid);
599ee1b1b9cSAndy Grover 		if (cr) {
600e48354ceSNicholas Bellinger 			pr_debug("Performing implicit logout"
601e48354ceSNicholas Bellinger 				" for connection recovery on CID: %hu\n",
602e48354ceSNicholas Bellinger 					conn->cid);
603e48354ceSNicholas Bellinger 			iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
604e48354ceSNicholas Bellinger 		}
605e48354ceSNicholas Bellinger 	}
606e48354ceSNicholas Bellinger 
607e48354ceSNicholas Bellinger 	/*
608e48354ceSNicholas Bellinger 	 * Else we follow item 4 from the login table in that we have
609e48354ceSNicholas Bellinger 	 * found an existing ISID and a valid/existing TSIH and a new
610e48354ceSNicholas Bellinger 	 * CID we go ahead and continue to add a new connection to the
611e48354ceSNicholas Bellinger 	 * session.
612e48354ceSNicholas Bellinger 	 */
613e48354ceSNicholas Bellinger 	pr_debug("Adding CID %hu to existing session for %s.\n",
614e48354ceSNicholas Bellinger 			cid, sess->sess_ops->InitiatorName);
615e48354ceSNicholas Bellinger 
616e48354ceSNicholas Bellinger 	if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
617e48354ceSNicholas Bellinger 		pr_err("Adding additional connection to this session"
618e48354ceSNicholas Bellinger 			" would exceed MaxConnections %d, login failed.\n",
619e48354ceSNicholas Bellinger 				sess->sess_ops->MaxConnections);
620e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
621e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_ISID_ERROR);
622e48354ceSNicholas Bellinger 		return -1;
623e48354ceSNicholas Bellinger 	}
624e48354ceSNicholas Bellinger 
625e48354ceSNicholas Bellinger 	return 0;
626e48354ceSNicholas Bellinger }
627e48354ceSNicholas Bellinger 
iscsi_post_login_start_timers(struct iscsit_conn * conn)628be36d683SMax Gurtovoy static void iscsi_post_login_start_timers(struct iscsit_conn *conn)
629e48354ceSNicholas Bellinger {
6300873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
63103aa2070SNicholas Bellinger 	/*
6323fc6a642SColin Ian King 	 * FIXME: Unsolicited NopIN support for ISER
63303aa2070SNicholas Bellinger 	 */
63403aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
63503aa2070SNicholas Bellinger 		return;
636e48354ceSNicholas Bellinger 
637e48354ceSNicholas Bellinger 	if (!sess->sess_ops->SessionType)
638e48354ceSNicholas Bellinger 		iscsit_start_nopin_timer(conn);
639e48354ceSNicholas Bellinger }
640e48354ceSNicholas Bellinger 
iscsit_start_kthreads(struct iscsit_conn * conn)641be36d683SMax Gurtovoy int iscsit_start_kthreads(struct iscsit_conn *conn)
64288dcd2daSNicholas Bellinger {
64388dcd2daSNicholas Bellinger 	int ret = 0;
64488dcd2daSNicholas Bellinger 
64588dcd2daSNicholas Bellinger 	spin_lock(&iscsit_global->ts_bitmap_lock);
64688dcd2daSNicholas Bellinger 	conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
64788dcd2daSNicholas Bellinger 					ISCSIT_BITMAP_BITS, get_order(1));
64888dcd2daSNicholas Bellinger 	spin_unlock(&iscsit_global->ts_bitmap_lock);
64988dcd2daSNicholas Bellinger 
65088dcd2daSNicholas Bellinger 	if (conn->bitmap_id < 0) {
65188dcd2daSNicholas Bellinger 		pr_err("bitmap_find_free_region() failed for"
65288dcd2daSNicholas Bellinger 		       " iscsit_start_kthreads()\n");
65388dcd2daSNicholas Bellinger 		return -ENOMEM;
65488dcd2daSNicholas Bellinger 	}
65588dcd2daSNicholas Bellinger 
65688dcd2daSNicholas Bellinger 	conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
65788dcd2daSNicholas Bellinger 				      "%s", ISCSI_TX_THREAD_NAME);
65888dcd2daSNicholas Bellinger 	if (IS_ERR(conn->tx_thread)) {
65988dcd2daSNicholas Bellinger 		pr_err("Unable to start iscsi_target_tx_thread\n");
66088dcd2daSNicholas Bellinger 		ret = PTR_ERR(conn->tx_thread);
66188dcd2daSNicholas Bellinger 		goto out_bitmap;
66288dcd2daSNicholas Bellinger 	}
66388dcd2daSNicholas Bellinger 	conn->tx_thread_active = true;
66488dcd2daSNicholas Bellinger 
66588dcd2daSNicholas Bellinger 	conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
66688dcd2daSNicholas Bellinger 				      "%s", ISCSI_RX_THREAD_NAME);
66788dcd2daSNicholas Bellinger 	if (IS_ERR(conn->rx_thread)) {
66888dcd2daSNicholas Bellinger 		pr_err("Unable to start iscsi_target_rx_thread\n");
66988dcd2daSNicholas Bellinger 		ret = PTR_ERR(conn->rx_thread);
67088dcd2daSNicholas Bellinger 		goto out_tx;
67188dcd2daSNicholas Bellinger 	}
67288dcd2daSNicholas Bellinger 	conn->rx_thread_active = true;
67388dcd2daSNicholas Bellinger 
67488dcd2daSNicholas Bellinger 	return 0;
67588dcd2daSNicholas Bellinger out_tx:
676e5419865SNicholas Bellinger 	send_sig(SIGINT, conn->tx_thread, 1);
67788dcd2daSNicholas Bellinger 	kthread_stop(conn->tx_thread);
67888dcd2daSNicholas Bellinger 	conn->tx_thread_active = false;
67988dcd2daSNicholas Bellinger out_bitmap:
68088dcd2daSNicholas Bellinger 	spin_lock(&iscsit_global->ts_bitmap_lock);
68188dcd2daSNicholas Bellinger 	bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
68288dcd2daSNicholas Bellinger 			      get_order(1));
68388dcd2daSNicholas Bellinger 	spin_unlock(&iscsit_global->ts_bitmap_lock);
68488dcd2daSNicholas Bellinger 	return ret;
68588dcd2daSNicholas Bellinger }
68688dcd2daSNicholas Bellinger 
iscsi_post_login_handler(struct iscsi_np * np,struct iscsit_conn * conn,u8 zero_tsih)687e5419865SNicholas Bellinger void iscsi_post_login_handler(
688e48354ceSNicholas Bellinger 	struct iscsi_np *np,
689be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
690e48354ceSNicholas Bellinger 	u8 zero_tsih)
691e48354ceSNicholas Bellinger {
692e48354ceSNicholas Bellinger 	int stop_timer = 0;
6930873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
694e48354ceSNicholas Bellinger 	struct se_session *se_sess = sess->se_sess;
69560bfcf8eSAndy Grover 	struct iscsi_portal_group *tpg = sess->tpg;
696e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
697e48354ceSNicholas Bellinger 
698e48354ceSNicholas Bellinger 	iscsit_inc_conn_usage_count(conn);
699e48354ceSNicholas Bellinger 
700e48354ceSNicholas Bellinger 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
701e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_ACCEPT);
702e48354ceSNicholas Bellinger 
703e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
704e48354ceSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
705e48354ceSNicholas Bellinger 
706e48354ceSNicholas Bellinger 	iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
707e48354ceSNicholas Bellinger 	/*
708e48354ceSNicholas Bellinger 	 * SCSI Initiator -> SCSI Target Port Mapping
709e48354ceSNicholas Bellinger 	 */
710e48354ceSNicholas Bellinger 	if (!zero_tsih) {
711e48354ceSNicholas Bellinger 		iscsi_set_session_parameters(sess->sess_ops,
712e48354ceSNicholas Bellinger 				conn->param_list, 0);
713e48354ceSNicholas Bellinger 		iscsi_release_param_list(conn->param_list);
714e48354ceSNicholas Bellinger 		conn->param_list = NULL;
715e48354ceSNicholas Bellinger 
716e48354ceSNicholas Bellinger 		spin_lock_bh(&sess->conn_lock);
717e48354ceSNicholas Bellinger 		atomic_set(&sess->session_continuation, 0);
718e48354ceSNicholas Bellinger 		if (sess->session_state == TARG_SESS_STATE_FAILED) {
719e48354ceSNicholas Bellinger 			pr_debug("Moving to"
720e48354ceSNicholas Bellinger 					" TARG_SESS_STATE_LOGGED_IN.\n");
721e48354ceSNicholas Bellinger 			sess->session_state = TARG_SESS_STATE_LOGGED_IN;
722e48354ceSNicholas Bellinger 			stop_timer = 1;
723e48354ceSNicholas Bellinger 		}
724e48354ceSNicholas Bellinger 
725dc58f760SAndy Grover 		pr_debug("iSCSI Login successful on CID: %hu from %pISpc to"
726dc58f760SAndy Grover 			" %pISpc,%hu\n", conn->cid, &conn->login_sockaddr,
72769d75574SAndy Grover 			&conn->local_sockaddr, tpg->tpgt);
728e48354ceSNicholas Bellinger 
729e48354ceSNicholas Bellinger 		list_add_tail(&conn->conn_list, &sess->sess_conn_list);
730e48354ceSNicholas Bellinger 		atomic_inc(&sess->nconn);
73171b25693SJustin Stitt 		pr_debug("Incremented iSCSI Connection count to %d"
732e48354ceSNicholas Bellinger 			" from node: %s\n", atomic_read(&sess->nconn),
733e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
734e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
735e48354ceSNicholas Bellinger 
73688dcd2daSNicholas Bellinger 		iscsi_post_login_start_timers(conn);
737e48354ceSNicholas Bellinger 		/*
738e48354ceSNicholas Bellinger 		 * Determine CPU mask to ensure connection's RX and TX kthreads
739e48354ceSNicholas Bellinger 		 * are scheduled on the same CPU.
740e48354ceSNicholas Bellinger 		 */
741e48354ceSNicholas Bellinger 		iscsit_thread_get_cpumask(conn);
742e48354ceSNicholas Bellinger 		conn->conn_rx_reset_cpumask = 1;
743e48354ceSNicholas Bellinger 		conn->conn_tx_reset_cpumask = 1;
744e5419865SNicholas Bellinger 		/*
745e5419865SNicholas Bellinger 		 * Wakeup the sleeping iscsi_target_rx_thread() now that
746be36d683SMax Gurtovoy 		 * iscsit_conn is in TARG_CONN_STATE_LOGGED_IN state.
747e5419865SNicholas Bellinger 		 */
748e5419865SNicholas Bellinger 		complete(&conn->rx_login_comp);
749e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn);
750e5419865SNicholas Bellinger 
751e48354ceSNicholas Bellinger 		if (stop_timer) {
752e48354ceSNicholas Bellinger 			spin_lock_bh(&se_tpg->session_lock);
753e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess);
754e48354ceSNicholas Bellinger 			spin_unlock_bh(&se_tpg->session_lock);
755e48354ceSNicholas Bellinger 		}
756e48354ceSNicholas Bellinger 		iscsit_dec_session_usage_count(sess);
757e5419865SNicholas Bellinger 		return;
758e48354ceSNicholas Bellinger 	}
759e48354ceSNicholas Bellinger 
760e48354ceSNicholas Bellinger 	iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
761e48354ceSNicholas Bellinger 	iscsi_release_param_list(conn->param_list);
762e48354ceSNicholas Bellinger 	conn->param_list = NULL;
763e48354ceSNicholas Bellinger 
764e48354ceSNicholas Bellinger 	iscsit_determine_maxcmdsn(sess);
765e48354ceSNicholas Bellinger 
766e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
767e48354ceSNicholas Bellinger 	__transport_register_session(&sess->tpg->tpg_se_tpg,
7688359cf43SJörn Engel 			se_sess->se_node_acl, se_sess, sess);
769e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
770e48354ceSNicholas Bellinger 	sess->session_state = TARG_SESS_STATE_LOGGED_IN;
771e48354ceSNicholas Bellinger 
772dc58f760SAndy Grover 	pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n",
773dc58f760SAndy Grover 		conn->cid, &conn->login_sockaddr, &conn->local_sockaddr,
7742f9bc894SNicholas Bellinger 		tpg->tpgt);
775e48354ceSNicholas Bellinger 
776e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
777e48354ceSNicholas Bellinger 	list_add_tail(&conn->conn_list, &sess->sess_conn_list);
778e48354ceSNicholas Bellinger 	atomic_inc(&sess->nconn);
77971b25693SJustin Stitt 	pr_debug("Incremented iSCSI Connection count to %d from node:"
780e48354ceSNicholas Bellinger 		" %s\n", atomic_read(&sess->nconn),
781e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
782e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
783e48354ceSNicholas Bellinger 
784e48354ceSNicholas Bellinger 	sess->sid = tpg->sid++;
785e48354ceSNicholas Bellinger 	if (!sess->sid)
786e48354ceSNicholas Bellinger 		sess->sid = tpg->sid++;
787e48354ceSNicholas Bellinger 	pr_debug("Established iSCSI session from node: %s\n",
788e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
789e48354ceSNicholas Bellinger 
790e48354ceSNicholas Bellinger 	tpg->nsessions++;
791e48354ceSNicholas Bellinger 	if (tpg->tpg_tiqn)
792e48354ceSNicholas Bellinger 		tpg->tpg_tiqn->tiqn_nsessions++;
793e48354ceSNicholas Bellinger 
794e48354ceSNicholas Bellinger 	pr_debug("Incremented number of active iSCSI sessions to %u on"
795e48354ceSNicholas Bellinger 		" iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
796e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
797e48354ceSNicholas Bellinger 
798e48354ceSNicholas Bellinger 	iscsi_post_login_start_timers(conn);
799e48354ceSNicholas Bellinger 	/*
800e48354ceSNicholas Bellinger 	 * Determine CPU mask to ensure connection's RX and TX kthreads
801e48354ceSNicholas Bellinger 	 * are scheduled on the same CPU.
802e48354ceSNicholas Bellinger 	 */
803e48354ceSNicholas Bellinger 	iscsit_thread_get_cpumask(conn);
804e48354ceSNicholas Bellinger 	conn->conn_rx_reset_cpumask = 1;
805e48354ceSNicholas Bellinger 	conn->conn_tx_reset_cpumask = 1;
806e5419865SNicholas Bellinger 	/*
807e5419865SNicholas Bellinger 	 * Wakeup the sleeping iscsi_target_rx_thread() now that
808be36d683SMax Gurtovoy 	 * iscsit_conn is in TARG_CONN_STATE_LOGGED_IN state.
809e5419865SNicholas Bellinger 	 */
810e5419865SNicholas Bellinger 	complete(&conn->rx_login_comp);
811e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
812e48354ceSNicholas Bellinger }
813e48354ceSNicholas Bellinger 
iscsit_setup_np(struct iscsi_np * np,struct sockaddr_storage * sockaddr)814baa4d64bSNicholas Bellinger int iscsit_setup_np(
815e48354ceSNicholas Bellinger 	struct iscsi_np *np,
81613a3cf08SAndy Grover 	struct sockaddr_storage *sockaddr)
817e48354ceSNicholas Bellinger {
818baa4d64bSNicholas Bellinger 	struct socket *sock = NULL;
819c4e446bfSChristoph Hellwig 	int backlog = ISCSIT_TCP_BACKLOG, ret, len;
820e48354ceSNicholas Bellinger 
821e48354ceSNicholas Bellinger 	switch (np->np_network_transport) {
822e48354ceSNicholas Bellinger 	case ISCSI_TCP:
823e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_TCP;
824e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_STREAM;
825e48354ceSNicholas Bellinger 		break;
826e48354ceSNicholas Bellinger 	case ISCSI_SCTP_TCP:
827e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_SCTP;
828e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_STREAM;
829e48354ceSNicholas Bellinger 		break;
830e48354ceSNicholas Bellinger 	case ISCSI_SCTP_UDP:
831e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_SCTP;
832e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_SEQPACKET;
833e48354ceSNicholas Bellinger 		break;
834e48354ceSNicholas Bellinger 	default:
835e48354ceSNicholas Bellinger 		pr_err("Unsupported network_transport: %d\n",
836e48354ceSNicholas Bellinger 				np->np_network_transport);
837e48354ceSNicholas Bellinger 		return -EINVAL;
838e48354ceSNicholas Bellinger 	}
839e48354ceSNicholas Bellinger 
840e48354ceSNicholas Bellinger 	ret = sock_create(sockaddr->ss_family, np->np_sock_type,
841e48354ceSNicholas Bellinger 			np->np_ip_proto, &sock);
842e48354ceSNicholas Bellinger 	if (ret < 0) {
843e48354ceSNicholas Bellinger 		pr_err("sock_create() failed.\n");
844e48354ceSNicholas Bellinger 		return ret;
845e48354ceSNicholas Bellinger 	}
846e48354ceSNicholas Bellinger 	np->np_socket = sock;
847e48354ceSNicholas Bellinger 	/*
848e48354ceSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
849e48354ceSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
850e48354ceSNicholas Bellinger 	 */
8518359cf43SJörn Engel 	memcpy(&np->np_sockaddr, sockaddr,
85213a3cf08SAndy Grover 			sizeof(struct sockaddr_storage));
853e48354ceSNicholas Bellinger 
854e48354ceSNicholas Bellinger 	if (sockaddr->ss_family == AF_INET6)
855e48354ceSNicholas Bellinger 		len = sizeof(struct sockaddr_in6);
856e48354ceSNicholas Bellinger 	else
857e48354ceSNicholas Bellinger 		len = sizeof(struct sockaddr_in);
858e48354ceSNicholas Bellinger 	/*
859389102a3SValdis Klētnieks 	 * Set SO_REUSEADDR, and disable Nagle Algorithm with TCP_NODELAY.
860e48354ceSNicholas Bellinger 	 */
86112abc5eeSChristoph Hellwig 	if (np->np_network_transport == ISCSI_TCP)
86212abc5eeSChristoph Hellwig 		tcp_sock_set_nodelay(sock->sk);
863b58f0e8fSChristoph Hellwig 	sock_set_reuseaddr(sock->sk);
864c4e446bfSChristoph Hellwig 	ip_sock_set_freebind(sock->sk);
8659f9ef6d3SDax Kelson 
866e48354ceSNicholas Bellinger 	ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
867e48354ceSNicholas Bellinger 	if (ret < 0) {
868e48354ceSNicholas Bellinger 		pr_err("kernel_bind() failed: %d\n", ret);
869e48354ceSNicholas Bellinger 		goto fail;
870e48354ceSNicholas Bellinger 	}
871e48354ceSNicholas Bellinger 
872e48354ceSNicholas Bellinger 	ret = kernel_listen(sock, backlog);
873e48354ceSNicholas Bellinger 	if (ret != 0) {
874e48354ceSNicholas Bellinger 		pr_err("kernel_listen() failed: %d\n", ret);
875e48354ceSNicholas Bellinger 		goto fail;
876e48354ceSNicholas Bellinger 	}
877e48354ceSNicholas Bellinger 
878e48354ceSNicholas Bellinger 	return 0;
879e48354ceSNicholas Bellinger fail:
880e48354ceSNicholas Bellinger 	np->np_socket = NULL;
881e48354ceSNicholas Bellinger 	sock_release(sock);
882e48354ceSNicholas Bellinger 	return ret;
883e48354ceSNicholas Bellinger }
884e48354ceSNicholas Bellinger 
iscsi_target_setup_login_socket(struct iscsi_np * np,struct sockaddr_storage * sockaddr)885baa4d64bSNicholas Bellinger int iscsi_target_setup_login_socket(
886baa4d64bSNicholas Bellinger 	struct iscsi_np *np,
88713a3cf08SAndy Grover 	struct sockaddr_storage *sockaddr)
888baa4d64bSNicholas Bellinger {
889baa4d64bSNicholas Bellinger 	struct iscsit_transport *t;
890baa4d64bSNicholas Bellinger 	int rc;
891baa4d64bSNicholas Bellinger 
892baa4d64bSNicholas Bellinger 	t = iscsit_get_transport(np->np_network_transport);
893baa4d64bSNicholas Bellinger 	if (!t)
894baa4d64bSNicholas Bellinger 		return -EINVAL;
895baa4d64bSNicholas Bellinger 
896baa4d64bSNicholas Bellinger 	rc = t->iscsit_setup_np(np, sockaddr);
897baa4d64bSNicholas Bellinger 	if (rc < 0) {
898baa4d64bSNicholas Bellinger 		iscsit_put_transport(t);
899baa4d64bSNicholas Bellinger 		return rc;
900baa4d64bSNicholas Bellinger 	}
901baa4d64bSNicholas Bellinger 
902baa4d64bSNicholas Bellinger 	np->np_transport = t;
90314f4b54fSSagi Grimberg 	np->enabled = true;
904baa4d64bSNicholas Bellinger 	return 0;
905baa4d64bSNicholas Bellinger }
906baa4d64bSNicholas Bellinger 
iscsit_accept_np(struct iscsi_np * np,struct iscsit_conn * conn)907be36d683SMax Gurtovoy int iscsit_accept_np(struct iscsi_np *np, struct iscsit_conn *conn)
908baa4d64bSNicholas Bellinger {
909baa4d64bSNicholas Bellinger 	struct socket *new_sock, *sock = np->np_socket;
910baa4d64bSNicholas Bellinger 	struct sockaddr_in sock_in;
911baa4d64bSNicholas Bellinger 	struct sockaddr_in6 sock_in6;
9129b2c45d4SDenys Vlasenko 	int rc;
913baa4d64bSNicholas Bellinger 
914baa4d64bSNicholas Bellinger 	rc = kernel_accept(sock, &new_sock, 0);
915baa4d64bSNicholas Bellinger 	if (rc < 0)
916baa4d64bSNicholas Bellinger 		return rc;
917baa4d64bSNicholas Bellinger 
918baa4d64bSNicholas Bellinger 	conn->sock = new_sock;
919baa4d64bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
920baa4d64bSNicholas Bellinger 
921baa4d64bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
922baa4d64bSNicholas Bellinger 		memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
923baa4d64bSNicholas Bellinger 
924baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
9259b2c45d4SDenys Vlasenko 				(struct sockaddr *)&sock_in6, 1);
9269b2c45d4SDenys Vlasenko 		if (rc >= 0) {
927dc58f760SAndy Grover 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
928dc58f760SAndy Grover 				memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6));
929dc58f760SAndy Grover 			} else {
930dc58f760SAndy Grover 				/* Pretend to be an ipv4 socket */
931dc58f760SAndy Grover 				sock_in.sin_family = AF_INET;
932dc58f760SAndy Grover 				sock_in.sin_port = sock_in6.sin6_port;
933dc58f760SAndy Grover 				memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
934dc58f760SAndy Grover 				memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
935dc58f760SAndy Grover 			}
936baa4d64bSNicholas Bellinger 		}
937baa4d64bSNicholas Bellinger 
938baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
9399b2c45d4SDenys Vlasenko 				(struct sockaddr *)&sock_in6, 0);
9409b2c45d4SDenys Vlasenko 		if (rc >= 0) {
94169d75574SAndy Grover 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
94269d75574SAndy Grover 				memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
94369d75574SAndy Grover 			} else {
94469d75574SAndy Grover 				/* Pretend to be an ipv4 socket */
94569d75574SAndy Grover 				sock_in.sin_family = AF_INET;
94669d75574SAndy Grover 				sock_in.sin_port = sock_in6.sin6_port;
94769d75574SAndy Grover 				memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
94869d75574SAndy Grover 				memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
94969d75574SAndy Grover 			}
950baa4d64bSNicholas Bellinger 		}
951baa4d64bSNicholas Bellinger 	} else {
952baa4d64bSNicholas Bellinger 		memset(&sock_in, 0, sizeof(struct sockaddr_in));
953baa4d64bSNicholas Bellinger 
954baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
9559b2c45d4SDenys Vlasenko 				(struct sockaddr *)&sock_in, 1);
9569b2c45d4SDenys Vlasenko 		if (rc >= 0)
957dc58f760SAndy Grover 			memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
958baa4d64bSNicholas Bellinger 
959baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
9609b2c45d4SDenys Vlasenko 				(struct sockaddr *)&sock_in, 0);
9619b2c45d4SDenys Vlasenko 		if (rc >= 0)
96269d75574SAndy Grover 			memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
963baa4d64bSNicholas Bellinger 	}
964baa4d64bSNicholas Bellinger 
965baa4d64bSNicholas Bellinger 	return 0;
966baa4d64bSNicholas Bellinger }
967baa4d64bSNicholas Bellinger 
iscsit_get_login_rx(struct iscsit_conn * conn,struct iscsi_login * login)968be36d683SMax Gurtovoy int iscsit_get_login_rx(struct iscsit_conn *conn, struct iscsi_login *login)
969baa4d64bSNicholas Bellinger {
970baa4d64bSNicholas Bellinger 	struct iscsi_login_req *login_req;
971baa4d64bSNicholas Bellinger 	u32 padding = 0, payload_length;
972baa4d64bSNicholas Bellinger 
973baa4d64bSNicholas Bellinger 	if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
974baa4d64bSNicholas Bellinger 		return -1;
975baa4d64bSNicholas Bellinger 
976baa4d64bSNicholas Bellinger 	login_req = (struct iscsi_login_req *)login->req;
977baa4d64bSNicholas Bellinger 	payload_length	= ntoh24(login_req->dlength);
978baa4d64bSNicholas Bellinger 	padding = ((-payload_length) & 3);
979baa4d64bSNicholas Bellinger 
980baa4d64bSNicholas Bellinger 	pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
981baa4d64bSNicholas Bellinger 		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
982baa4d64bSNicholas Bellinger 		login_req->flags, login_req->itt, login_req->cmdsn,
983baa4d64bSNicholas Bellinger 		login_req->exp_statsn, login_req->cid, payload_length);
984baa4d64bSNicholas Bellinger 	/*
985baa4d64bSNicholas Bellinger 	 * Setup the initial iscsi_login values from the leading
986baa4d64bSNicholas Bellinger 	 * login request PDU.
987baa4d64bSNicholas Bellinger 	 */
988baa4d64bSNicholas Bellinger 	if (login->first_request) {
989baa4d64bSNicholas Bellinger 		login_req = (struct iscsi_login_req *)login->req;
990baa4d64bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
9913e1c81a9SNicholas Bellinger 		login->current_stage	= ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
992baa4d64bSNicholas Bellinger 		login->version_min	= login_req->min_version;
993baa4d64bSNicholas Bellinger 		login->version_max	= login_req->max_version;
994baa4d64bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
995baa4d64bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
996baa4d64bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
997baa4d64bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
998baa4d64bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
999baa4d64bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1000baa4d64bSNicholas Bellinger 	}
1001baa4d64bSNicholas Bellinger 
1002baa4d64bSNicholas Bellinger 	if (iscsi_target_check_login_request(conn, login) < 0)
1003baa4d64bSNicholas Bellinger 		return -1;
1004baa4d64bSNicholas Bellinger 
1005baa4d64bSNicholas Bellinger 	memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1006baa4d64bSNicholas Bellinger 	if (iscsi_login_rx_data(conn, login->req_buf,
1007baa4d64bSNicholas Bellinger 				payload_length + padding) < 0)
1008baa4d64bSNicholas Bellinger 		return -1;
1009baa4d64bSNicholas Bellinger 
1010baa4d64bSNicholas Bellinger 	return 0;
1011baa4d64bSNicholas Bellinger }
1012baa4d64bSNicholas Bellinger 
iscsit_put_login_tx(struct iscsit_conn * conn,struct iscsi_login * login,u32 length)1013be36d683SMax Gurtovoy int iscsit_put_login_tx(struct iscsit_conn *conn, struct iscsi_login *login,
1014baa4d64bSNicholas Bellinger 			u32 length)
1015baa4d64bSNicholas Bellinger {
1016baa4d64bSNicholas Bellinger 	if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1017baa4d64bSNicholas Bellinger 		return -1;
1018baa4d64bSNicholas Bellinger 
1019baa4d64bSNicholas Bellinger 	return 0;
1020baa4d64bSNicholas Bellinger }
1021baa4d64bSNicholas Bellinger 
1022baa4d64bSNicholas Bellinger static int
iscsit_conn_set_transport(struct iscsit_conn * conn,struct iscsit_transport * t)1023be36d683SMax Gurtovoy iscsit_conn_set_transport(struct iscsit_conn *conn, struct iscsit_transport *t)
1024baa4d64bSNicholas Bellinger {
1025baa4d64bSNicholas Bellinger 	int rc;
1026baa4d64bSNicholas Bellinger 
1027baa4d64bSNicholas Bellinger 	if (!t->owner) {
1028baa4d64bSNicholas Bellinger 		conn->conn_transport = t;
1029baa4d64bSNicholas Bellinger 		return 0;
1030baa4d64bSNicholas Bellinger 	}
1031baa4d64bSNicholas Bellinger 
1032baa4d64bSNicholas Bellinger 	rc = try_module_get(t->owner);
1033baa4d64bSNicholas Bellinger 	if (!rc) {
1034baa4d64bSNicholas Bellinger 		pr_err("try_module_get() failed for %s\n", t->name);
1035baa4d64bSNicholas Bellinger 		return -EINVAL;
1036baa4d64bSNicholas Bellinger 	}
1037baa4d64bSNicholas Bellinger 
1038baa4d64bSNicholas Bellinger 	conn->conn_transport = t;
1039baa4d64bSNicholas Bellinger 	return 0;
1040baa4d64bSNicholas Bellinger }
1041baa4d64bSNicholas Bellinger 
iscsit_alloc_conn(struct iscsi_np * np)1042be36d683SMax Gurtovoy static struct iscsit_conn *iscsit_alloc_conn(struct iscsi_np *np)
104305a86e78SMike Christie {
1044be36d683SMax Gurtovoy 	struct iscsit_conn *conn;
104505a86e78SMike Christie 
1046be36d683SMax Gurtovoy 	conn = kzalloc(sizeof(struct iscsit_conn), GFP_KERNEL);
104705a86e78SMike Christie 	if (!conn) {
104805a86e78SMike Christie 		pr_err("Could not allocate memory for new connection\n");
104905a86e78SMike Christie 		return NULL;
105005a86e78SMike Christie 	}
105105a86e78SMike Christie 	pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
105205a86e78SMike Christie 	conn->conn_state = TARG_CONN_STATE_FREE;
105305a86e78SMike Christie 
105405a86e78SMike Christie 	init_waitqueue_head(&conn->queues_wq);
105505a86e78SMike Christie 	INIT_LIST_HEAD(&conn->conn_list);
105605a86e78SMike Christie 	INIT_LIST_HEAD(&conn->conn_cmd_list);
105705a86e78SMike Christie 	INIT_LIST_HEAD(&conn->immed_queue_list);
105805a86e78SMike Christie 	INIT_LIST_HEAD(&conn->response_queue_list);
105905a86e78SMike Christie 	init_completion(&conn->conn_post_wait_comp);
106005a86e78SMike Christie 	init_completion(&conn->conn_wait_comp);
106105a86e78SMike Christie 	init_completion(&conn->conn_wait_rcfr_comp);
106205a86e78SMike Christie 	init_completion(&conn->conn_waiting_on_uc_comp);
106305a86e78SMike Christie 	init_completion(&conn->conn_logout_comp);
106405a86e78SMike Christie 	init_completion(&conn->rx_half_close_comp);
106505a86e78SMike Christie 	init_completion(&conn->tx_half_close_comp);
106605a86e78SMike Christie 	init_completion(&conn->rx_login_comp);
106705a86e78SMike Christie 	spin_lock_init(&conn->cmd_lock);
106805a86e78SMike Christie 	spin_lock_init(&conn->conn_usage_lock);
106905a86e78SMike Christie 	spin_lock_init(&conn->immed_queue_lock);
107005a86e78SMike Christie 	spin_lock_init(&conn->nopin_timer_lock);
107105a86e78SMike Christie 	spin_lock_init(&conn->response_queue_lock);
107205a86e78SMike Christie 	spin_lock_init(&conn->state_lock);
1073*13247018SMaurizio Lombardi 	spin_lock_init(&conn->login_worker_lock);
1074*13247018SMaurizio Lombardi 	spin_lock_init(&conn->login_timer_lock);
107505a86e78SMike Christie 
107605a86e78SMike Christie 	timer_setup(&conn->nopin_response_timer,
107705a86e78SMike Christie 		    iscsit_handle_nopin_response_timeout, 0);
107805a86e78SMike Christie 	timer_setup(&conn->nopin_timer, iscsit_handle_nopin_timeout, 0);
1079*13247018SMaurizio Lombardi 	timer_setup(&conn->login_timer, iscsit_login_timeout, 0);
108005a86e78SMike Christie 
108105a86e78SMike Christie 	if (iscsit_conn_set_transport(conn, np->np_transport) < 0)
108205a86e78SMike Christie 		goto free_conn;
108305a86e78SMike Christie 
108405a86e78SMike Christie 	conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
108505a86e78SMike Christie 	if (!conn->conn_ops) {
108605a86e78SMike Christie 		pr_err("Unable to allocate memory for struct iscsi_conn_ops.\n");
108705a86e78SMike Christie 		goto put_transport;
108805a86e78SMike Christie 	}
108905a86e78SMike Christie 
109005a86e78SMike Christie 	if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
109105a86e78SMike Christie 		pr_err("Unable to allocate conn->conn_cpumask\n");
1092f55d0b40Stangwenji 		goto free_conn_ops;
109305a86e78SMike Christie 	}
109405a86e78SMike Christie 
1095d72d827fSMingzhe Zou 	if (!zalloc_cpumask_var(&conn->allowed_cpumask, GFP_KERNEL)) {
1096d72d827fSMingzhe Zou 		pr_err("Unable to allocate conn->allowed_cpumask\n");
1097d72d827fSMingzhe Zou 		goto free_conn_cpumask;
1098d72d827fSMingzhe Zou 	}
1099d72d827fSMingzhe Zou 
11006d256beeSMike Christie 	conn->cmd_cnt = target_alloc_cmd_counter();
11016d256beeSMike Christie 	if (!conn->cmd_cnt)
11026d256beeSMike Christie 		goto free_conn_allowed_cpumask;
11036d256beeSMike Christie 
110405a86e78SMike Christie 	return conn;
110505a86e78SMike Christie 
11066d256beeSMike Christie free_conn_allowed_cpumask:
11076d256beeSMike Christie 	free_cpumask_var(conn->allowed_cpumask);
1108d72d827fSMingzhe Zou free_conn_cpumask:
1109d72d827fSMingzhe Zou 	free_cpumask_var(conn->conn_cpumask);
1110f55d0b40Stangwenji free_conn_ops:
1111f55d0b40Stangwenji 	kfree(conn->conn_ops);
111205a86e78SMike Christie put_transport:
111305a86e78SMike Christie 	iscsit_put_transport(conn->conn_transport);
111405a86e78SMike Christie free_conn:
111505a86e78SMike Christie 	kfree(conn);
111605a86e78SMike Christie 	return NULL;
111705a86e78SMike Christie }
111805a86e78SMike Christie 
iscsit_free_conn(struct iscsit_conn * conn)1119be36d683SMax Gurtovoy void iscsit_free_conn(struct iscsit_conn *conn)
112005a86e78SMike Christie {
11216d256beeSMike Christie 	target_free_cmd_counter(conn->cmd_cnt);
1122d72d827fSMingzhe Zou 	free_cpumask_var(conn->allowed_cpumask);
112305a86e78SMike Christie 	free_cpumask_var(conn->conn_cpumask);
112405a86e78SMike Christie 	kfree(conn->conn_ops);
112505a86e78SMike Christie 	iscsit_put_transport(conn->conn_transport);
112605a86e78SMike Christie 	kfree(conn);
112705a86e78SMike Christie }
112805a86e78SMike Christie 
iscsi_target_login_sess_out(struct iscsit_conn * conn,bool zero_tsih,bool new_sess)1129be36d683SMax Gurtovoy void iscsi_target_login_sess_out(struct iscsit_conn *conn,
1130ed43ffeaSHou Pu 				 bool zero_tsih, bool new_sess)
1131a91eb7d9SNicholas Bellinger {
11320bcc297eSChristophe Vu-Brugier 	if (!new_sess)
1133a91eb7d9SNicholas Bellinger 		goto old_sess_out;
1134a91eb7d9SNicholas Bellinger 
1135a91eb7d9SNicholas Bellinger 	pr_err("iSCSI Login negotiation failed.\n");
1136a91eb7d9SNicholas Bellinger 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1137a91eb7d9SNicholas Bellinger 				   ISCSI_LOGIN_STATUS_INIT_ERR);
1138a91eb7d9SNicholas Bellinger 	if (!zero_tsih || !conn->sess)
1139a91eb7d9SNicholas Bellinger 		goto old_sess_out;
114026abc916SMike Christie 
1141a91eb7d9SNicholas Bellinger 	transport_free_session(conn->sess->se_sess);
114231ff0ceeSMatthew Wilcox 	ida_free(&sess_ida, conn->sess->session_index);
1143a91eb7d9SNicholas Bellinger 	kfree(conn->sess->sess_ops);
1144a91eb7d9SNicholas Bellinger 	kfree(conn->sess);
1145a0b3b9b2SSagi Grimberg 	conn->sess = NULL;
1146a91eb7d9SNicholas Bellinger 
1147a91eb7d9SNicholas Bellinger old_sess_out:
1148a91eb7d9SNicholas Bellinger 	/*
1149a91eb7d9SNicholas Bellinger 	 * If login negotiation fails check if the Time2Retain timer
1150a91eb7d9SNicholas Bellinger 	 * needs to be restarted.
1151a91eb7d9SNicholas Bellinger 	 */
1152a91eb7d9SNicholas Bellinger 	if (!zero_tsih && conn->sess) {
1153a91eb7d9SNicholas Bellinger 		spin_lock_bh(&conn->sess->conn_lock);
1154a91eb7d9SNicholas Bellinger 		if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1155a91eb7d9SNicholas Bellinger 			struct se_portal_group *se_tpg =
115660bfcf8eSAndy Grover 					&conn->tpg->tpg_se_tpg;
1157a91eb7d9SNicholas Bellinger 
1158a91eb7d9SNicholas Bellinger 			atomic_set(&conn->sess->session_continuation, 0);
1159a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&conn->sess->conn_lock);
1160a91eb7d9SNicholas Bellinger 			spin_lock_bh(&se_tpg->session_lock);
1161a91eb7d9SNicholas Bellinger 			iscsit_start_time2retain_handler(conn->sess);
1162a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&se_tpg->session_lock);
1163a91eb7d9SNicholas Bellinger 		} else
1164a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&conn->sess->conn_lock);
1165a91eb7d9SNicholas Bellinger 		iscsit_dec_session_usage_count(conn->sess);
1166a91eb7d9SNicholas Bellinger 	}
1167a91eb7d9SNicholas Bellinger 
116869110e3cSHerbert Xu 	ahash_request_free(conn->conn_tx_hash);
116969110e3cSHerbert Xu 	if (conn->conn_rx_hash) {
117069110e3cSHerbert Xu 		struct crypto_ahash *tfm;
117169110e3cSHerbert Xu 
117269110e3cSHerbert Xu 		tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
117369110e3cSHerbert Xu 		ahash_request_free(conn->conn_rx_hash);
117469110e3cSHerbert Xu 		crypto_free_ahash(tfm);
117569110e3cSHerbert Xu 	}
1176a91eb7d9SNicholas Bellinger 
1177a91eb7d9SNicholas Bellinger 	if (conn->param_list) {
1178a91eb7d9SNicholas Bellinger 		iscsi_release_param_list(conn->param_list);
1179a91eb7d9SNicholas Bellinger 		conn->param_list = NULL;
1180a91eb7d9SNicholas Bellinger 	}
1181a91eb7d9SNicholas Bellinger 	iscsi_target_nego_release(conn);
1182a91eb7d9SNicholas Bellinger 
1183a91eb7d9SNicholas Bellinger 	if (conn->sock) {
1184a91eb7d9SNicholas Bellinger 		sock_release(conn->sock);
1185a91eb7d9SNicholas Bellinger 		conn->sock = NULL;
1186a91eb7d9SNicholas Bellinger 	}
1187a91eb7d9SNicholas Bellinger 
1188954f2372SSagi Grimberg 	if (conn->conn_transport->iscsit_wait_conn)
1189954f2372SSagi Grimberg 		conn->conn_transport->iscsit_wait_conn(conn);
1190954f2372SSagi Grimberg 
1191a91eb7d9SNicholas Bellinger 	if (conn->conn_transport->iscsit_free_conn)
1192a91eb7d9SNicholas Bellinger 		conn->conn_transport->iscsit_free_conn(conn);
1193a91eb7d9SNicholas Bellinger 
119405a86e78SMike Christie 	iscsit_free_conn(conn);
1195a91eb7d9SNicholas Bellinger }
1196a91eb7d9SNicholas Bellinger 
__iscsi_target_login_thread(struct iscsi_np * np)1197e48354ceSNicholas Bellinger static int __iscsi_target_login_thread(struct iscsi_np *np)
1198e48354ceSNicholas Bellinger {
1199baa4d64bSNicholas Bellinger 	u8 *buffer, zero_tsih = 0;
120081a9c5e7SMikulas Patocka 	int ret = 0, rc;
1201be36d683SMax Gurtovoy 	struct iscsit_conn *conn = NULL;
1202e48354ceSNicholas Bellinger 	struct iscsi_login *login;
1203e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = NULL;
1204e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu;
1205a91eb7d9SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np;
1206a91eb7d9SNicholas Bellinger 	bool new_sess = false;
1207e48354ceSNicholas Bellinger 
1208e48354ceSNicholas Bellinger 	flush_signals(current);
1209e48354ceSNicholas Bellinger 
1210e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1211978d13d6SNicholas Bellinger 	if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1212e48354ceSNicholas Bellinger 		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1213978d13d6SNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
1214e48354ceSNicholas Bellinger 		complete(&np->np_restart_comp);
1215978d13d6SNicholas Bellinger 		return 1;
121681a9c5e7SMikulas Patocka 	} else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
121781a9c5e7SMikulas Patocka 		spin_unlock_bh(&np->np_thread_lock);
121881a9c5e7SMikulas Patocka 		goto exit;
1219e48354ceSNicholas Bellinger 	} else {
1220e48354ceSNicholas Bellinger 		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1221e48354ceSNicholas Bellinger 	}
1222e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1223e48354ceSNicholas Bellinger 
122405a86e78SMike Christie 	conn = iscsit_alloc_conn(np);
1225e48354ceSNicholas Bellinger 	if (!conn) {
1226e48354ceSNicholas Bellinger 		/* Get another socket */
1227e48354ceSNicholas Bellinger 		return 1;
1228e48354ceSNicholas Bellinger 	}
1229e48354ceSNicholas Bellinger 
1230baa4d64bSNicholas Bellinger 	rc = np->np_transport->iscsit_accept_np(np, conn);
1231baa4d64bSNicholas Bellinger 	if (rc == -ENOSYS) {
1232baa4d64bSNicholas Bellinger 		complete(&np->np_restart_comp);
123305a86e78SMike Christie 		iscsit_free_conn(conn);
1234baa4d64bSNicholas Bellinger 		goto exit;
1235baa4d64bSNicholas Bellinger 	} else if (rc < 0) {
1236baa4d64bSNicholas Bellinger 		spin_lock_bh(&np->np_thread_lock);
1237978d13d6SNicholas Bellinger 		if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1238978d13d6SNicholas Bellinger 			np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1239baa4d64bSNicholas Bellinger 			spin_unlock_bh(&np->np_thread_lock);
1240baa4d64bSNicholas Bellinger 			complete(&np->np_restart_comp);
124105a86e78SMike Christie 			iscsit_free_conn(conn);
1242baa4d64bSNicholas Bellinger 			/* Get another socket */
1243baa4d64bSNicholas Bellinger 			return 1;
1244baa4d64bSNicholas Bellinger 		}
1245baa4d64bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
124605a86e78SMike Christie 		iscsit_free_conn(conn);
124705a86e78SMike Christie 		return 1;
1248e48354ceSNicholas Bellinger 	}
1249e48354ceSNicholas Bellinger 	/*
1250e48354ceSNicholas Bellinger 	 * Perform the remaining iSCSI connection initialization items..
1251e48354ceSNicholas Bellinger 	 */
1252baa4d64bSNicholas Bellinger 	login = iscsi_login_init_conn(conn);
1253baa4d64bSNicholas Bellinger 	if (!login) {
1254e48354ceSNicholas Bellinger 		goto new_sess_out;
1255e48354ceSNicholas Bellinger 	}
1256e48354ceSNicholas Bellinger 
1257*13247018SMaurizio Lombardi 	iscsit_start_login_timer(conn, current);
1258e48354ceSNicholas Bellinger 
1259baa4d64bSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1260baa4d64bSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_XPT_UP;
1261baa4d64bSNicholas Bellinger 	/*
1262baa4d64bSNicholas Bellinger 	 * This will process the first login request + payload..
1263baa4d64bSNicholas Bellinger 	 */
1264baa4d64bSNicholas Bellinger 	rc = np->np_transport->iscsit_get_login_rx(conn, login);
1265baa4d64bSNicholas Bellinger 	if (rc == 1)
1266baa4d64bSNicholas Bellinger 		return 1;
1267baa4d64bSNicholas Bellinger 	else if (rc < 0)
1268baa4d64bSNicholas Bellinger 		goto new_sess_out;
1269baa4d64bSNicholas Bellinger 
1270baa4d64bSNicholas Bellinger 	buffer = &login->req[0];
1271e48354ceSNicholas Bellinger 	pdu = (struct iscsi_login_req *)buffer;
1272e48354ceSNicholas Bellinger 	/*
1273e48354ceSNicholas Bellinger 	 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1274e48354ceSNicholas Bellinger 	 * when Status-Class != 0.
1275e48354ceSNicholas Bellinger 	*/
1276e48354ceSNicholas Bellinger 	conn->login_itt	= pdu->itt;
1277e48354ceSNicholas Bellinger 
1278e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1279e48354ceSNicholas Bellinger 	if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1280e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
128169d75574SAndy Grover 		pr_err("iSCSI Network Portal on %pISpc currently not"
128269d75574SAndy Grover 			" active.\n", &np->np_sockaddr);
1283e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1284e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1285e48354ceSNicholas Bellinger 		goto new_sess_out;
1286e48354ceSNicholas Bellinger 	}
1287e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1288e48354ceSNicholas Bellinger 
1289e48354ceSNicholas Bellinger 	conn->network_transport = np->np_network_transport;
1290e48354ceSNicholas Bellinger 
1291dc58f760SAndy Grover 	pr_debug("Received iSCSI login request from %pISpc on %s Network"
1292dc58f760SAndy Grover 		" Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name,
129369d75574SAndy Grover 		&conn->local_sockaddr);
1294e48354ceSNicholas Bellinger 
1295e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1296e48354ceSNicholas Bellinger 	conn->conn_state	= TARG_CONN_STATE_IN_LOGIN;
1297e48354ceSNicholas Bellinger 
1298e48354ceSNicholas Bellinger 	if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1299e48354ceSNicholas Bellinger 			pdu->min_version) < 0)
1300e48354ceSNicholas Bellinger 		goto new_sess_out;
1301e48354ceSNicholas Bellinger 
1302e48354ceSNicholas Bellinger 	zero_tsih = (pdu->tsih == 0x0000);
1303ee1b1b9cSAndy Grover 	if (zero_tsih) {
1304e48354ceSNicholas Bellinger 		/*
1305e48354ceSNicholas Bellinger 		 * This is the leading connection of a new session.
1306e48354ceSNicholas Bellinger 		 * We wait until after authentication to check for
1307e48354ceSNicholas Bellinger 		 * session reinstatement.
1308e48354ceSNicholas Bellinger 		 */
1309e48354ceSNicholas Bellinger 		if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1310e48354ceSNicholas Bellinger 			goto new_sess_out;
1311e48354ceSNicholas Bellinger 	} else {
1312e48354ceSNicholas Bellinger 		/*
1313e48354ceSNicholas Bellinger 		 * Add a new connection to an existing session.
1314e48354ceSNicholas Bellinger 		 * We check for a non-existant session in
1315e48354ceSNicholas Bellinger 		 * iscsi_login_non_zero_tsih_s2() below based
1316e48354ceSNicholas Bellinger 		 * on ISID/TSIH, but wait until after authentication
1317e48354ceSNicholas Bellinger 		 * to check for connection reinstatement, etc.
1318e48354ceSNicholas Bellinger 		 */
1319e48354ceSNicholas Bellinger 		if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1320e48354ceSNicholas Bellinger 			goto new_sess_out;
1321e48354ceSNicholas Bellinger 	}
1322e48354ceSNicholas Bellinger 	/*
1323baa4d64bSNicholas Bellinger 	 * SessionType: Discovery
1324baa4d64bSNicholas Bellinger 	 *
1325baa4d64bSNicholas Bellinger 	 * 	Locates Default Portal
1326baa4d64bSNicholas Bellinger 	 *
1327baa4d64bSNicholas Bellinger 	 * SessionType: Normal
1328baa4d64bSNicholas Bellinger 	 *
1329baa4d64bSNicholas Bellinger 	 * 	Locates Target Portal from NP -> Target IQN
1330e48354ceSNicholas Bellinger 	 */
1331baa4d64bSNicholas Bellinger 	rc = iscsi_target_locate_portal(np, conn, login);
1332baa4d64bSNicholas Bellinger 	if (rc < 0) {
1333e48354ceSNicholas Bellinger 		tpg = conn->tpg;
1334e48354ceSNicholas Bellinger 		goto new_sess_out;
1335e48354ceSNicholas Bellinger 	}
1336a91eb7d9SNicholas Bellinger 	login->zero_tsih = zero_tsih;
1337e48354ceSNicholas Bellinger 
13388abc718dSFeng Li 	if (conn->sess)
133923a548eeSSagi Grimberg 		conn->sess->se_sess->sup_prot_ops =
134023a548eeSSagi Grimberg 			conn->conn_transport->iscsit_get_sup_prot_ops(conn);
134123a548eeSSagi Grimberg 
1342e48354ceSNicholas Bellinger 	tpg = conn->tpg;
1343e48354ceSNicholas Bellinger 	if (!tpg) {
1344be36d683SMax Gurtovoy 		pr_err("Unable to locate struct iscsit_conn->tpg\n");
1345e48354ceSNicholas Bellinger 		goto new_sess_out;
1346e48354ceSNicholas Bellinger 	}
1347e48354ceSNicholas Bellinger 
1348e48354ceSNicholas Bellinger 	if (zero_tsih) {
1349baa4d64bSNicholas Bellinger 		if (iscsi_login_zero_tsih_s2(conn) < 0)
1350e48354ceSNicholas Bellinger 			goto new_sess_out;
1351e48354ceSNicholas Bellinger 	} else {
1352baa4d64bSNicholas Bellinger 		if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1353e48354ceSNicholas Bellinger 			goto old_sess_out;
1354e48354ceSNicholas Bellinger 	}
1355e48354ceSNicholas Bellinger 
135642bc3e57SVarun Prakash 	if (conn->conn_transport->iscsit_validate_params) {
135742bc3e57SVarun Prakash 		ret = conn->conn_transport->iscsit_validate_params(conn);
135842bc3e57SVarun Prakash 		if (ret < 0) {
135942bc3e57SVarun Prakash 			if (zero_tsih)
136042bc3e57SVarun Prakash 				goto new_sess_out;
136142bc3e57SVarun Prakash 			else
136242bc3e57SVarun Prakash 				goto old_sess_out;
136342bc3e57SVarun Prakash 		}
136442bc3e57SVarun Prakash 	}
136542bc3e57SVarun Prakash 
1366a91eb7d9SNicholas Bellinger 	ret = iscsi_target_start_negotiation(login, conn);
1367a91eb7d9SNicholas Bellinger 	if (ret < 0)
1368e48354ceSNicholas Bellinger 		goto new_sess_out;
1369e48354ceSNicholas Bellinger 
1370a91eb7d9SNicholas Bellinger 	if (ret == 1) {
1371a91eb7d9SNicholas Bellinger 		tpg_np = conn->tpg_np;
1372e48354ceSNicholas Bellinger 
1373e5419865SNicholas Bellinger 		iscsi_post_login_handler(np, conn, zero_tsih);
1374a91eb7d9SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
1375a91eb7d9SNicholas Bellinger 	}
1376a91eb7d9SNicholas Bellinger 
1377e48354ceSNicholas Bellinger 	tpg = NULL;
1378a91eb7d9SNicholas Bellinger 	tpg_np = NULL;
1379e48354ceSNicholas Bellinger 	/* Get another socket */
1380e48354ceSNicholas Bellinger 	return 1;
1381e48354ceSNicholas Bellinger 
1382e48354ceSNicholas Bellinger new_sess_out:
1383a91eb7d9SNicholas Bellinger 	new_sess = true;
1384e48354ceSNicholas Bellinger old_sess_out:
1385*13247018SMaurizio Lombardi 	iscsit_stop_login_timer(conn);
1386a91eb7d9SNicholas Bellinger 	tpg_np = conn->tpg_np;
1387ed43ffeaSHou Pu 	iscsi_target_login_sess_out(conn, zero_tsih, new_sess);
1388a91eb7d9SNicholas Bellinger 	new_sess = false;
1389e48354ceSNicholas Bellinger 
1390e48354ceSNicholas Bellinger 	if (tpg) {
1391a91eb7d9SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
1392e48354ceSNicholas Bellinger 		tpg = NULL;
1393a91eb7d9SNicholas Bellinger 		tpg_np = NULL;
1394e48354ceSNicholas Bellinger 	}
1395e48354ceSNicholas Bellinger 
1396e48354ceSNicholas Bellinger 	return 1;
139781a9c5e7SMikulas Patocka 
1398baa4d64bSNicholas Bellinger exit:
1399e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1400e48354ceSNicholas Bellinger 	np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1401e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1402baa4d64bSNicholas Bellinger 
1403e48354ceSNicholas Bellinger 	return 0;
1404e48354ceSNicholas Bellinger }
1405e48354ceSNicholas Bellinger 
iscsi_target_login_thread(void * arg)1406e48354ceSNicholas Bellinger int iscsi_target_login_thread(void *arg)
1407e48354ceSNicholas Bellinger {
14088359cf43SJörn Engel 	struct iscsi_np *np = arg;
1409e48354ceSNicholas Bellinger 	int ret;
1410e48354ceSNicholas Bellinger 
1411e48354ceSNicholas Bellinger 	allow_signal(SIGINT);
1412e48354ceSNicholas Bellinger 
141381a9c5e7SMikulas Patocka 	while (1) {
1414e48354ceSNicholas Bellinger 		ret = __iscsi_target_login_thread(np);
1415e48354ceSNicholas Bellinger 		/*
1416e48354ceSNicholas Bellinger 		 * We break and exit here unless another sock_accept() call
1417e48354ceSNicholas Bellinger 		 * is expected.
1418e48354ceSNicholas Bellinger 		 */
1419e48354ceSNicholas Bellinger 		if (ret != 1)
1420e48354ceSNicholas Bellinger 			break;
1421e48354ceSNicholas Bellinger 	}
1422e48354ceSNicholas Bellinger 
14235e0cf5e6SJiang Yi 	while (!kthread_should_stop()) {
14245e0cf5e6SJiang Yi 		msleep(100);
14255e0cf5e6SJiang Yi 	}
14265e0cf5e6SJiang Yi 
1427e48354ceSNicholas Bellinger 	return 0;
1428e48354ceSNicholas Bellinger }
1429