1e48354ceSNicholas Bellinger /*******************************************************************************
2e48354ceSNicholas Bellinger  * This file contains the login functions used by the iSCSI Target driver.
3e48354ceSNicholas Bellinger  *
44c76251eSNicholas Bellinger  * (c) Copyright 2007-2013 Datera, Inc.
5e48354ceSNicholas Bellinger  *
6e48354ceSNicholas Bellinger  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
7e48354ceSNicholas Bellinger  *
8e48354ceSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
9e48354ceSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
10e48354ceSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
11e48354ceSNicholas Bellinger  * (at your option) any later version.
12e48354ceSNicholas Bellinger  *
13e48354ceSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
14e48354ceSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15e48354ceSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16e48354ceSNicholas Bellinger  * GNU General Public License for more details.
17e48354ceSNicholas Bellinger  ******************************************************************************/
18e48354ceSNicholas Bellinger 
1969110e3cSHerbert Xu #include <crypto/hash.h>
20da5df620SRussell King #include <linux/module.h>
21e48354ceSNicholas Bellinger #include <linux/string.h>
22e48354ceSNicholas Bellinger #include <linux/kthread.h>
233f07c014SIngo Molnar #include <linux/sched/signal.h>
2440401530SAl Viro #include <linux/idr.h>
258dcf07beSBart Van Assche #include <linux/tcp.h>        /* TCP_NODELAY */
268dcf07beSBart Van Assche #include <net/ipv6.h>         /* ipv6_addr_v4mapped() */
27e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
28e48354ceSNicholas Bellinger #include <target/target_core_base.h>
29c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
30e48354ceSNicholas Bellinger 
3167f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h>
3267f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_stat.h>
33e48354ceSNicholas Bellinger #include "iscsi_target_device.h"
34e48354ceSNicholas Bellinger #include "iscsi_target_nego.h"
35e48354ceSNicholas Bellinger #include "iscsi_target_erl0.h"
36e48354ceSNicholas Bellinger #include "iscsi_target_erl2.h"
37e48354ceSNicholas Bellinger #include "iscsi_target_login.h"
38e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
39e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
40e48354ceSNicholas Bellinger #include "iscsi_target.h"
41e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
42e48354ceSNicholas Bellinger 
43baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
44baa4d64bSNicholas Bellinger 
45baa4d64bSNicholas Bellinger static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
46e48354ceSNicholas Bellinger {
47baa4d64bSNicholas Bellinger 	struct iscsi_login *login;
48baa4d64bSNicholas Bellinger 
49baa4d64bSNicholas Bellinger 	login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
50baa4d64bSNicholas Bellinger 	if (!login) {
51baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for struct iscsi_login.\n");
52baa4d64bSNicholas Bellinger 		return NULL;
53baa4d64bSNicholas Bellinger 	}
54a91eb7d9SNicholas Bellinger 	conn->login = login;
55baa4d64bSNicholas Bellinger 	login->conn = conn;
56baa4d64bSNicholas Bellinger 	login->first_request = 1;
57baa4d64bSNicholas Bellinger 
58baa4d64bSNicholas Bellinger 	login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
59baa4d64bSNicholas Bellinger 	if (!login->req_buf) {
60baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for response buffer.\n");
61baa4d64bSNicholas Bellinger 		goto out_login;
62baa4d64bSNicholas Bellinger 	}
63baa4d64bSNicholas Bellinger 
64baa4d64bSNicholas Bellinger 	login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
65baa4d64bSNicholas Bellinger 	if (!login->rsp_buf) {
66baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for request buffer.\n");
67baa4d64bSNicholas Bellinger 		goto out_req_buf;
68baa4d64bSNicholas Bellinger 	}
69baa4d64bSNicholas Bellinger 
70baa4d64bSNicholas Bellinger 	conn->conn_login = login;
71e48354ceSNicholas Bellinger 
72baa4d64bSNicholas Bellinger 	return login;
73baa4d64bSNicholas Bellinger 
74baa4d64bSNicholas Bellinger out_req_buf:
75baa4d64bSNicholas Bellinger 	kfree(login->req_buf);
76baa4d64bSNicholas Bellinger out_login:
77baa4d64bSNicholas Bellinger 	kfree(login);
78baa4d64bSNicholas Bellinger 	return NULL;
79e48354ceSNicholas Bellinger }
80e48354ceSNicholas Bellinger 
81e48354ceSNicholas Bellinger /*
82e48354ceSNicholas Bellinger  * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
83e48354ceSNicholas Bellinger  * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
84e48354ceSNicholas Bellinger  */
85e48354ceSNicholas Bellinger int iscsi_login_setup_crypto(struct iscsi_conn *conn)
86e48354ceSNicholas Bellinger {
8769110e3cSHerbert Xu 	struct crypto_ahash *tfm;
8869110e3cSHerbert Xu 
89e48354ceSNicholas Bellinger 	/*
90e48354ceSNicholas Bellinger 	 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
91e48354ceSNicholas Bellinger 	 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
92e48354ceSNicholas Bellinger 	 * to software 1x8 byte slicing from crc32c.ko
93e48354ceSNicholas Bellinger 	 */
9469110e3cSHerbert Xu 	tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
9569110e3cSHerbert Xu 	if (IS_ERR(tfm)) {
9669110e3cSHerbert Xu 		pr_err("crypto_alloc_ahash() failed\n");
97e48354ceSNicholas Bellinger 		return -ENOMEM;
98e48354ceSNicholas Bellinger 	}
99e48354ceSNicholas Bellinger 
10069110e3cSHerbert Xu 	conn->conn_rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
10169110e3cSHerbert Xu 	if (!conn->conn_rx_hash) {
10269110e3cSHerbert Xu 		pr_err("ahash_request_alloc() failed for conn_rx_hash\n");
10369110e3cSHerbert Xu 		crypto_free_ahash(tfm);
104e48354ceSNicholas Bellinger 		return -ENOMEM;
105e48354ceSNicholas Bellinger 	}
10669110e3cSHerbert Xu 	ahash_request_set_callback(conn->conn_rx_hash, 0, NULL, NULL);
10769110e3cSHerbert Xu 
10869110e3cSHerbert Xu 	conn->conn_tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
10969110e3cSHerbert Xu 	if (!conn->conn_tx_hash) {
11069110e3cSHerbert Xu 		pr_err("ahash_request_alloc() failed for conn_tx_hash\n");
11169110e3cSHerbert Xu 		ahash_request_free(conn->conn_rx_hash);
11269110e3cSHerbert Xu 		conn->conn_rx_hash = NULL;
11369110e3cSHerbert Xu 		crypto_free_ahash(tfm);
11469110e3cSHerbert Xu 		return -ENOMEM;
11569110e3cSHerbert Xu 	}
11669110e3cSHerbert Xu 	ahash_request_set_callback(conn->conn_tx_hash, 0, NULL, NULL);
117e48354ceSNicholas Bellinger 
118e48354ceSNicholas Bellinger 	return 0;
119e48354ceSNicholas Bellinger }
120e48354ceSNicholas Bellinger 
121e48354ceSNicholas Bellinger static int iscsi_login_check_initiator_version(
122e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
123e48354ceSNicholas Bellinger 	u8 version_max,
124e48354ceSNicholas Bellinger 	u8 version_min)
125e48354ceSNicholas Bellinger {
126e48354ceSNicholas Bellinger 	if ((version_max != 0x00) || (version_min != 0x00)) {
127e48354ceSNicholas Bellinger 		pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
128e48354ceSNicholas Bellinger 			" version Min/Max 0x%02x/0x%02x, rejecting login.\n",
129e48354ceSNicholas Bellinger 			version_min, version_max);
130e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
131e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_VERSION);
132e48354ceSNicholas Bellinger 		return -1;
133e48354ceSNicholas Bellinger 	}
134e48354ceSNicholas Bellinger 
135e48354ceSNicholas Bellinger 	return 0;
136e48354ceSNicholas Bellinger }
137e48354ceSNicholas Bellinger 
138e48354ceSNicholas Bellinger int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
139e48354ceSNicholas Bellinger {
140e48354ceSNicholas Bellinger 	int sessiontype;
141e48354ceSNicholas Bellinger 	struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
142e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
143e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL, *sess_p = NULL;
144e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
145e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
146e48354ceSNicholas Bellinger 
147e48354ceSNicholas Bellinger 	initiatorname_param = iscsi_find_param_from_key(
148e48354ceSNicholas Bellinger 			INITIATORNAME, conn->param_list);
149e48354ceSNicholas Bellinger 	sessiontype_param = iscsi_find_param_from_key(
150e48354ceSNicholas Bellinger 			SESSIONTYPE, conn->param_list);
1511c5c12c6SRoland Dreier 	if (!initiatorname_param || !sessiontype_param) {
1521c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1531c5c12c6SRoland Dreier 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
154e48354ceSNicholas Bellinger 		return -1;
1551c5c12c6SRoland Dreier 	}
156e48354ceSNicholas Bellinger 
157e48354ceSNicholas Bellinger 	sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
158e48354ceSNicholas Bellinger 
159e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
160e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
161e48354ceSNicholas Bellinger 			sess_list) {
162e48354ceSNicholas Bellinger 
1638359cf43SJörn Engel 		sess_p = se_sess->fabric_sess_ptr;
164e48354ceSNicholas Bellinger 		spin_lock(&sess_p->conn_lock);
165e48354ceSNicholas Bellinger 		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
166e48354ceSNicholas Bellinger 		    atomic_read(&sess_p->session_logout) ||
167e48354ceSNicholas Bellinger 		    (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
168e48354ceSNicholas Bellinger 			spin_unlock(&sess_p->conn_lock);
169e48354ceSNicholas Bellinger 			continue;
170e48354ceSNicholas Bellinger 		}
1718359cf43SJörn Engel 		if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
1728359cf43SJörn Engel 		   (!strcmp(sess_p->sess_ops->InitiatorName,
1738359cf43SJörn Engel 			    initiatorname_param->value) &&
174e48354ceSNicholas Bellinger 		   (sess_p->sess_ops->SessionType == sessiontype))) {
175e48354ceSNicholas Bellinger 			atomic_set(&sess_p->session_reinstatement, 1);
176197b806aSNicholas Bellinger 			atomic_set(&sess_p->session_fall_back_to_erl0, 1);
177e48354ceSNicholas Bellinger 			spin_unlock(&sess_p->conn_lock);
178e48354ceSNicholas Bellinger 			iscsit_inc_session_usage_count(sess_p);
179e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess_p);
180e48354ceSNicholas Bellinger 			sess = sess_p;
181e48354ceSNicholas Bellinger 			break;
182e48354ceSNicholas Bellinger 		}
183e48354ceSNicholas Bellinger 		spin_unlock(&sess_p->conn_lock);
184e48354ceSNicholas Bellinger 	}
185e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
186e48354ceSNicholas Bellinger 	/*
187e48354ceSNicholas Bellinger 	 * If the Time2Retain handler has expired, the session is already gone.
188e48354ceSNicholas Bellinger 	 */
189e48354ceSNicholas Bellinger 	if (!sess)
190e48354ceSNicholas Bellinger 		return 0;
191e48354ceSNicholas Bellinger 
192e48354ceSNicholas Bellinger 	pr_debug("%s iSCSI Session SID %u is still active for %s,"
1930d5efb8aSBart Van Assche 		" performing session reinstatement.\n", (sessiontype) ?
194e48354ceSNicholas Bellinger 		"Discovery" : "Normal", sess->sid,
195e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
196e48354ceSNicholas Bellinger 
197e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
198e48354ceSNicholas Bellinger 	if (sess->session_state == TARG_SESS_STATE_FAILED) {
199e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
200e48354ceSNicholas Bellinger 		iscsit_dec_session_usage_count(sess);
20144f33d0fSChristoph Hellwig 		iscsit_close_session(sess);
20299367f01SNicholas Bellinger 		return 0;
203e48354ceSNicholas Bellinger 	}
204e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
205e48354ceSNicholas Bellinger 
206e48354ceSNicholas Bellinger 	iscsit_stop_session(sess, 1, 1);
207e48354ceSNicholas Bellinger 	iscsit_dec_session_usage_count(sess);
208e48354ceSNicholas Bellinger 
20944f33d0fSChristoph Hellwig 	iscsit_close_session(sess);
21099367f01SNicholas Bellinger 	return 0;
211e48354ceSNicholas Bellinger }
212e48354ceSNicholas Bellinger 
2136787ab81SJason A. Donenfeld static int iscsi_login_set_conn_values(
214e48354ceSNicholas Bellinger 	struct iscsi_session *sess,
215e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
21650e5c87dSChristoph Hellwig 	__be16 cid)
217e48354ceSNicholas Bellinger {
2186787ab81SJason A. Donenfeld 	int ret;
219e48354ceSNicholas Bellinger 	conn->sess		= sess;
22050e5c87dSChristoph Hellwig 	conn->cid		= be16_to_cpu(cid);
221e48354ceSNicholas Bellinger 	/*
222e48354ceSNicholas Bellinger 	 * Generate a random Status sequence number (statsn) for the new
223e48354ceSNicholas Bellinger 	 * iSCSI connection.
224e48354ceSNicholas Bellinger 	 */
2256787ab81SJason A. Donenfeld 	ret = get_random_bytes_wait(&conn->stat_sn, sizeof(u32));
2266787ab81SJason A. Donenfeld 	if (unlikely(ret))
2276787ab81SJason A. Donenfeld 		return ret;
228e48354ceSNicholas Bellinger 
229e48354ceSNicholas Bellinger 	mutex_lock(&auth_id_lock);
230e48354ceSNicholas Bellinger 	conn->auth_id		= iscsit_global->auth_id++;
231e48354ceSNicholas Bellinger 	mutex_unlock(&auth_id_lock);
2326787ab81SJason A. Donenfeld 	return 0;
233e48354ceSNicholas Bellinger }
234e48354ceSNicholas Bellinger 
235d2faaefbSVarun Prakash __printf(2, 3) int iscsi_change_param_sprintf(
23679d59d08SRoland Dreier 	struct iscsi_conn *conn,
23779d59d08SRoland Dreier 	const char *fmt, ...)
23879d59d08SRoland Dreier {
23979d59d08SRoland Dreier 	va_list args;
24079d59d08SRoland Dreier 	unsigned char buf[64];
24179d59d08SRoland Dreier 
24279d59d08SRoland Dreier 	memset(buf, 0, sizeof buf);
24379d59d08SRoland Dreier 
24479d59d08SRoland Dreier 	va_start(args, fmt);
24579d59d08SRoland Dreier 	vsnprintf(buf, sizeof buf, fmt, args);
24679d59d08SRoland Dreier 	va_end(args);
24779d59d08SRoland Dreier 
24879d59d08SRoland Dreier 	if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
24979d59d08SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
25079d59d08SRoland Dreier 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
25179d59d08SRoland Dreier 		return -1;
25279d59d08SRoland Dreier 	}
25379d59d08SRoland Dreier 
25479d59d08SRoland Dreier 	return 0;
25579d59d08SRoland Dreier }
256d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsi_change_param_sprintf);
25779d59d08SRoland Dreier 
258e48354ceSNicholas Bellinger /*
259e48354ceSNicholas Bellinger  *	This is the leading connection of a new session,
260e48354ceSNicholas Bellinger  *	or session reinstatement.
261e48354ceSNicholas Bellinger  */
262e48354ceSNicholas Bellinger static int iscsi_login_zero_tsih_s1(
263e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
264e48354ceSNicholas Bellinger 	unsigned char *buf)
265e48354ceSNicholas Bellinger {
266e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL;
267e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
26813b5533aSBenjamin Wang 	int ret;
269e48354ceSNicholas Bellinger 
270e48354ceSNicholas Bellinger 	sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
271e48354ceSNicholas Bellinger 	if (!sess) {
272e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
273e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
274e48354ceSNicholas Bellinger 		pr_err("Could not allocate memory for session\n");
2750957627aSNicholas Bellinger 		return -ENOMEM;
276e48354ceSNicholas Bellinger 	}
277e48354ceSNicholas Bellinger 
2787915919bSVincent Pelletier 	if (iscsi_login_set_conn_values(sess, conn, pdu->cid))
2797915919bSVincent Pelletier 		goto free_sess;
2807915919bSVincent Pelletier 
281e48354ceSNicholas Bellinger 	sess->init_task_tag	= pdu->itt;
2828359cf43SJörn Engel 	memcpy(&sess->isid, pdu->isid, 6);
28350e5c87dSChristoph Hellwig 	sess->exp_cmd_sn	= be32_to_cpu(pdu->cmdsn);
284e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->sess_conn_list);
285e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
286e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->cr_active_list);
287e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->cr_inactive_list);
288e48354ceSNicholas Bellinger 	init_completion(&sess->async_msg_comp);
289e48354ceSNicholas Bellinger 	init_completion(&sess->reinstatement_comp);
290e48354ceSNicholas Bellinger 	init_completion(&sess->session_wait_comp);
291e48354ceSNicholas Bellinger 	init_completion(&sess->session_waiting_on_uc_comp);
292e48354ceSNicholas Bellinger 	mutex_init(&sess->cmdsn_mutex);
293e48354ceSNicholas Bellinger 	spin_lock_init(&sess->conn_lock);
294e48354ceSNicholas Bellinger 	spin_lock_init(&sess->cr_a_lock);
295e48354ceSNicholas Bellinger 	spin_lock_init(&sess->cr_i_lock);
296e48354ceSNicholas Bellinger 	spin_lock_init(&sess->session_usage_lock);
297e48354ceSNicholas Bellinger 	spin_lock_init(&sess->ttt_lock);
298e48354ceSNicholas Bellinger 
299f7c9564aSKees Cook 	timer_setup(&sess->time2retain_timer,
300f7c9564aSKees Cook 		    iscsit_handle_time2retain_timeout, 0);
3018a47aa9dSBart Van Assche 
30231ff0ceeSMatthew Wilcox 	ret = ida_alloc(&sess_ida, GFP_KERNEL);
30313b5533aSBenjamin Wang 	if (ret < 0) {
30431ff0ceeSMatthew Wilcox 		pr_err("Session ID allocation failed %d\n", ret);
30513b5533aSBenjamin Wang 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
30613b5533aSBenjamin Wang 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
30726abc916SMike Christie 		goto free_sess;
30813b5533aSBenjamin Wang 	}
30913b5533aSBenjamin Wang 
31031ff0ceeSMatthew Wilcox 	sess->session_index = ret;
311e48354ceSNicholas Bellinger 	sess->creation_time = get_jiffies_64();
312e48354ceSNicholas Bellinger 	/*
313e48354ceSNicholas Bellinger 	 * The FFP CmdSN window values will be allocated from the TPG's
314e48354ceSNicholas Bellinger 	 * Initiator Node's ACL once the login has been successfully completed.
315e48354ceSNicholas Bellinger 	 */
316109e2381SRoland Dreier 	atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn));
317e48354ceSNicholas Bellinger 
318e48354ceSNicholas Bellinger 	sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
319e48354ceSNicholas Bellinger 	if (!sess->sess_ops) {
320e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
321e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
322e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for"
323e48354ceSNicholas Bellinger 				" struct iscsi_sess_ops.\n");
32431ff0ceeSMatthew Wilcox 		goto free_id;
325e48354ceSNicholas Bellinger 	}
326e48354ceSNicholas Bellinger 
327317f8971SBart Van Assche 	sess->se_sess = transport_alloc_session(TARGET_PROT_NORMAL);
3280957627aSNicholas Bellinger 	if (IS_ERR(sess->se_sess)) {
329e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
330e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
33126abc916SMike Christie 		goto free_ops;
332e48354ceSNicholas Bellinger 	}
333e48354ceSNicholas Bellinger 
334e48354ceSNicholas Bellinger 	return 0;
33526abc916SMike Christie 
33626abc916SMike Christie free_ops:
33726abc916SMike Christie 	kfree(sess->sess_ops);
33831ff0ceeSMatthew Wilcox free_id:
33931ff0ceeSMatthew Wilcox 	ida_free(&sess_ida, sess->session_index);
34026abc916SMike Christie free_sess:
34126abc916SMike Christie 	kfree(sess);
34226abc916SMike Christie 	conn->sess = NULL;
34326abc916SMike Christie 	return -ENOMEM;
344e48354ceSNicholas Bellinger }
345e48354ceSNicholas Bellinger 
346e48354ceSNicholas Bellinger static int iscsi_login_zero_tsih_s2(
347e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
348e48354ceSNicholas Bellinger {
349e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na;
350e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
35103aa2070SNicholas Bellinger 	bool iser = false;
352e48354ceSNicholas Bellinger 
353e48354ceSNicholas Bellinger 	sess->tpg = conn->tpg;
354e48354ceSNicholas Bellinger 
355e48354ceSNicholas Bellinger 	/*
356e48354ceSNicholas Bellinger 	 * Assign a new TPG Session Handle.  Note this is protected with
357e48354ceSNicholas Bellinger 	 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
358e48354ceSNicholas Bellinger 	 */
35960bfcf8eSAndy Grover 	sess->tsih = ++sess->tpg->ntsih;
360e48354ceSNicholas Bellinger 	if (!sess->tsih)
36160bfcf8eSAndy Grover 		sess->tsih = ++sess->tpg->ntsih;
362e48354ceSNicholas Bellinger 
363e48354ceSNicholas Bellinger 	/*
364e48354ceSNicholas Bellinger 	 * Create the default params from user defined values..
365e48354ceSNicholas Bellinger 	 */
366e48354ceSNicholas Bellinger 	if (iscsi_copy_param_list(&conn->param_list,
36760bfcf8eSAndy Grover 				conn->tpg->param_list, 1) < 0) {
368e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
369e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
370e48354ceSNicholas Bellinger 		return -1;
371e48354ceSNicholas Bellinger 	}
372e48354ceSNicholas Bellinger 
37303aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
37403aa2070SNicholas Bellinger 		iser = true;
37503aa2070SNicholas Bellinger 
37603aa2070SNicholas Bellinger 	iscsi_set_keys_to_negotiate(conn->param_list, iser);
377e48354ceSNicholas Bellinger 
378e48354ceSNicholas Bellinger 	if (sess->sess_ops->SessionType)
379e48354ceSNicholas Bellinger 		return iscsi_set_keys_irrelevant_for_discovery(
380e48354ceSNicholas Bellinger 				conn->param_list);
381e48354ceSNicholas Bellinger 
382e48354ceSNicholas Bellinger 	na = iscsit_tpg_get_node_attrib(sess);
383e48354ceSNicholas Bellinger 
384e48354ceSNicholas Bellinger 	/*
385e48354ceSNicholas Bellinger 	 * Need to send TargetPortalGroupTag back in first login response
386e48354ceSNicholas Bellinger 	 * on any iSCSI connection where the Initiator provides TargetName.
387e48354ceSNicholas Bellinger 	 * See 5.3.1.  Login Phase Start
388e48354ceSNicholas Bellinger 	 *
389e48354ceSNicholas Bellinger 	 * In our case, we have already located the struct iscsi_tiqn at this point.
390e48354ceSNicholas Bellinger 	 */
39179d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
392e48354ceSNicholas Bellinger 		return -1;
393e48354ceSNicholas Bellinger 
394e48354ceSNicholas Bellinger 	/*
395e48354ceSNicholas Bellinger 	 * Workaround for Initiators that have broken connection recovery logic.
396e48354ceSNicholas Bellinger 	 *
397e48354ceSNicholas Bellinger 	 * "We would really like to get rid of this." Linux-iSCSI.org team
398e48354ceSNicholas Bellinger 	 */
39979d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
400e48354ceSNicholas Bellinger 		return -1;
401e48354ceSNicholas Bellinger 
40203aa2070SNicholas Bellinger 	/*
40303aa2070SNicholas Bellinger 	 * Set RDMAExtensions=Yes by default for iSER enabled network portals
40403aa2070SNicholas Bellinger 	 */
40503aa2070SNicholas Bellinger 	if (iser) {
40603aa2070SNicholas Bellinger 		struct iscsi_param *param;
40703aa2070SNicholas Bellinger 		unsigned long mrdsl, off;
40803aa2070SNicholas Bellinger 		int rc;
40903aa2070SNicholas Bellinger 
41079d59d08SRoland Dreier 		if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
41103aa2070SNicholas Bellinger 			return -1;
41279d59d08SRoland Dreier 
41303aa2070SNicholas Bellinger 		/*
41403aa2070SNicholas Bellinger 		 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
4153fc6a642SColin Ian King 		 * Immediate Data + Unsolicited Data-OUT if necessary..
41603aa2070SNicholas Bellinger 		 */
41703aa2070SNicholas Bellinger 		param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
41803aa2070SNicholas Bellinger 						  conn->param_list);
41903aa2070SNicholas Bellinger 		if (!param) {
42003aa2070SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
42103aa2070SNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
42203aa2070SNicholas Bellinger 			return -1;
42303aa2070SNicholas Bellinger 		}
42457103d7fSJingoo Han 		rc = kstrtoul(param->value, 0, &mrdsl);
42503aa2070SNicholas Bellinger 		if (rc < 0) {
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 		}
43003aa2070SNicholas Bellinger 		off = mrdsl % PAGE_SIZE;
43103aa2070SNicholas Bellinger 		if (!off)
43252d0aa79SNicholas Bellinger 			goto check_prot;
43303aa2070SNicholas Bellinger 
43403aa2070SNicholas Bellinger 		if (mrdsl < PAGE_SIZE)
43503aa2070SNicholas Bellinger 			mrdsl = PAGE_SIZE;
43603aa2070SNicholas Bellinger 		else
43703aa2070SNicholas Bellinger 			mrdsl -= off;
43803aa2070SNicholas Bellinger 
43903aa2070SNicholas Bellinger 		pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
44003aa2070SNicholas Bellinger 			" to PAGE_SIZE\n", mrdsl);
44103aa2070SNicholas Bellinger 
44279d59d08SRoland Dreier 		if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
44303aa2070SNicholas Bellinger 			return -1;
44452d0aa79SNicholas Bellinger 		/*
44552d0aa79SNicholas Bellinger 		 * ISER currently requires that ImmediateData + Unsolicited
44652d0aa79SNicholas Bellinger 		 * Data be disabled when protection / signature MRs are enabled.
44752d0aa79SNicholas Bellinger 		 */
44852d0aa79SNicholas Bellinger check_prot:
44952d0aa79SNicholas Bellinger 		if (sess->se_sess->sup_prot_ops &
45052d0aa79SNicholas Bellinger 		   (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
45152d0aa79SNicholas Bellinger 		    TARGET_PROT_DOUT_INSERT)) {
45252d0aa79SNicholas Bellinger 
45379d59d08SRoland Dreier 			if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
45452d0aa79SNicholas Bellinger 				return -1;
45552d0aa79SNicholas Bellinger 
45679d59d08SRoland Dreier 			if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
45752d0aa79SNicholas Bellinger 				return -1;
45879d59d08SRoland Dreier 
45952d0aa79SNicholas Bellinger 			pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
46052d0aa79SNicholas Bellinger 				 " T10-PI enabled ISER session\n");
46152d0aa79SNicholas Bellinger 		}
46203aa2070SNicholas Bellinger 	}
463e48354ceSNicholas Bellinger 
464e48354ceSNicholas Bellinger 	return 0;
465e48354ceSNicholas Bellinger }
466e48354ceSNicholas Bellinger 
467e48354ceSNicholas Bellinger static int iscsi_login_non_zero_tsih_s1(
468e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
469e48354ceSNicholas Bellinger 	unsigned char *buf)
470e48354ceSNicholas Bellinger {
471e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
472e48354ceSNicholas Bellinger 
4736787ab81SJason A. Donenfeld 	return iscsi_login_set_conn_values(NULL, conn, pdu->cid);
474e48354ceSNicholas Bellinger }
475e48354ceSNicholas Bellinger 
476e48354ceSNicholas Bellinger /*
477e48354ceSNicholas Bellinger  *	Add a new connection to an existing session.
478e48354ceSNicholas Bellinger  */
479e48354ceSNicholas Bellinger static int iscsi_login_non_zero_tsih_s2(
480e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
481e48354ceSNicholas Bellinger 	unsigned char *buf)
482e48354ceSNicholas Bellinger {
483e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
484e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL, *sess_p = NULL;
485e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
486e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
487e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
48803aa2070SNicholas Bellinger 	bool iser = false;
489e48354ceSNicholas Bellinger 
490e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
491e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
492e48354ceSNicholas Bellinger 			sess_list) {
493e48354ceSNicholas Bellinger 
494e48354ceSNicholas Bellinger 		sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
495e48354ceSNicholas Bellinger 		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
496e48354ceSNicholas Bellinger 		    atomic_read(&sess_p->session_logout) ||
497e48354ceSNicholas Bellinger 		   (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
498e48354ceSNicholas Bellinger 			continue;
4998359cf43SJörn Engel 		if (!memcmp(sess_p->isid, pdu->isid, 6) &&
50050e5c87dSChristoph Hellwig 		     (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
501e48354ceSNicholas Bellinger 			iscsit_inc_session_usage_count(sess_p);
502e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess_p);
503e48354ceSNicholas Bellinger 			sess = sess_p;
504e48354ceSNicholas Bellinger 			break;
505e48354ceSNicholas Bellinger 		}
506e48354ceSNicholas Bellinger 	}
507e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
508e48354ceSNicholas Bellinger 
509e48354ceSNicholas Bellinger 	/*
510e48354ceSNicholas Bellinger 	 * If the Time2Retain handler has expired, the session is already gone.
511e48354ceSNicholas Bellinger 	 */
512e48354ceSNicholas Bellinger 	if (!sess) {
513e48354ceSNicholas Bellinger 		pr_err("Initiator attempting to add a connection to"
514e48354ceSNicholas Bellinger 			" a non-existent session, rejecting iSCSI Login.\n");
515e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
516e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_SESSION);
517e48354ceSNicholas Bellinger 		return -1;
518e48354ceSNicholas Bellinger 	}
519e48354ceSNicholas Bellinger 
520e48354ceSNicholas Bellinger 	/*
521e48354ceSNicholas Bellinger 	 * Stop the Time2Retain timer if this is a failed session, we restart
522e48354ceSNicholas Bellinger 	 * the timer if the login is not successful.
523e48354ceSNicholas Bellinger 	 */
524e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
525e48354ceSNicholas Bellinger 	if (sess->session_state == TARG_SESS_STATE_FAILED)
526e48354ceSNicholas Bellinger 		atomic_set(&sess->session_continuation, 1);
527e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
528e48354ceSNicholas Bellinger 
5296787ab81SJason A. Donenfeld 	if (iscsi_login_set_conn_values(sess, conn, pdu->cid) < 0 ||
5306787ab81SJason A. Donenfeld 	    iscsi_copy_param_list(&conn->param_list,
53160bfcf8eSAndy Grover 			conn->tpg->param_list, 0) < 0) {
532e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
533e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
534e48354ceSNicholas Bellinger 		return -1;
535e48354ceSNicholas Bellinger 	}
536e48354ceSNicholas Bellinger 
53703aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
53803aa2070SNicholas Bellinger 		iser = true;
53903aa2070SNicholas Bellinger 
54003aa2070SNicholas Bellinger 	iscsi_set_keys_to_negotiate(conn->param_list, iser);
541e48354ceSNicholas Bellinger 	/*
542e48354ceSNicholas Bellinger 	 * Need to send TargetPortalGroupTag back in first login response
543e48354ceSNicholas Bellinger 	 * on any iSCSI connection where the Initiator provides TargetName.
544e48354ceSNicholas Bellinger 	 * See 5.3.1.  Login Phase Start
545e48354ceSNicholas Bellinger 	 *
546e48354ceSNicholas Bellinger 	 * In our case, we have already located the struct iscsi_tiqn at this point.
547e48354ceSNicholas Bellinger 	 */
54879d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
549e48354ceSNicholas Bellinger 		return -1;
550e48354ceSNicholas Bellinger 
551c04a6091SChristophe Vu-Brugier 	return 0;
552e48354ceSNicholas Bellinger }
553e48354ceSNicholas Bellinger 
554e48354ceSNicholas Bellinger int iscsi_login_post_auth_non_zero_tsih(
555e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
556e48354ceSNicholas Bellinger 	u16 cid,
557e48354ceSNicholas Bellinger 	u32 exp_statsn)
558e48354ceSNicholas Bellinger {
559e48354ceSNicholas Bellinger 	struct iscsi_conn *conn_ptr = NULL;
560e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr = NULL;
561e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
562e48354ceSNicholas Bellinger 
563e48354ceSNicholas Bellinger 	/*
564e48354ceSNicholas Bellinger 	 * By following item 5 in the login table,  if we have found
565e48354ceSNicholas Bellinger 	 * an existing ISID and a valid/existing TSIH and an existing
566e48354ceSNicholas Bellinger 	 * CID we do connection reinstatement.  Currently we dont not
567e48354ceSNicholas Bellinger 	 * support it so we send back an non-zero status class to the
568e48354ceSNicholas Bellinger 	 * initiator and release the new connection.
569e48354ceSNicholas Bellinger 	 */
570e48354ceSNicholas Bellinger 	conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
571ee1b1b9cSAndy Grover 	if (conn_ptr) {
572e48354ceSNicholas Bellinger 		pr_err("Connection exists with CID %hu for %s,"
573e48354ceSNicholas Bellinger 			" performing connection reinstatement.\n",
574e48354ceSNicholas Bellinger 			conn_ptr->cid, sess->sess_ops->InitiatorName);
575e48354ceSNicholas Bellinger 
576e48354ceSNicholas Bellinger 		iscsit_connection_reinstatement_rcfr(conn_ptr);
577e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn_ptr);
578e48354ceSNicholas Bellinger 	}
579e48354ceSNicholas Bellinger 
580e48354ceSNicholas Bellinger 	/*
581902ff860SColin Ian King 	 * Check for any connection recovery entries containing CID.
582e48354ceSNicholas Bellinger 	 * We use the original ExpStatSN sent in the first login request
583e48354ceSNicholas Bellinger 	 * to acknowledge commands for the failed connection.
584e48354ceSNicholas Bellinger 	 *
585e48354ceSNicholas Bellinger 	 * Also note that an explict logout may have already been sent,
586e48354ceSNicholas Bellinger 	 * but the response may not be sent due to additional connection
587e48354ceSNicholas Bellinger 	 * loss.
588e48354ceSNicholas Bellinger 	 */
589e48354ceSNicholas Bellinger 	if (sess->sess_ops->ErrorRecoveryLevel == 2) {
590e48354ceSNicholas Bellinger 		cr = iscsit_get_inactive_connection_recovery_entry(
591e48354ceSNicholas Bellinger 				sess, cid);
592ee1b1b9cSAndy Grover 		if (cr) {
593e48354ceSNicholas Bellinger 			pr_debug("Performing implicit logout"
594e48354ceSNicholas Bellinger 				" for connection recovery on CID: %hu\n",
595e48354ceSNicholas Bellinger 					conn->cid);
596e48354ceSNicholas Bellinger 			iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
597e48354ceSNicholas Bellinger 		}
598e48354ceSNicholas Bellinger 	}
599e48354ceSNicholas Bellinger 
600e48354ceSNicholas Bellinger 	/*
601e48354ceSNicholas Bellinger 	 * Else we follow item 4 from the login table in that we have
602e48354ceSNicholas Bellinger 	 * found an existing ISID and a valid/existing TSIH and a new
603e48354ceSNicholas Bellinger 	 * CID we go ahead and continue to add a new connection to the
604e48354ceSNicholas Bellinger 	 * session.
605e48354ceSNicholas Bellinger 	 */
606e48354ceSNicholas Bellinger 	pr_debug("Adding CID %hu to existing session for %s.\n",
607e48354ceSNicholas Bellinger 			cid, sess->sess_ops->InitiatorName);
608e48354ceSNicholas Bellinger 
609e48354ceSNicholas Bellinger 	if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
610e48354ceSNicholas Bellinger 		pr_err("Adding additional connection to this session"
611e48354ceSNicholas Bellinger 			" would exceed MaxConnections %d, login failed.\n",
612e48354ceSNicholas Bellinger 				sess->sess_ops->MaxConnections);
613e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
614e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_ISID_ERROR);
615e48354ceSNicholas Bellinger 		return -1;
616e48354ceSNicholas Bellinger 	}
617e48354ceSNicholas Bellinger 
618e48354ceSNicholas Bellinger 	return 0;
619e48354ceSNicholas Bellinger }
620e48354ceSNicholas Bellinger 
621e48354ceSNicholas Bellinger static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
622e48354ceSNicholas Bellinger {
623e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
62403aa2070SNicholas Bellinger 	/*
6253fc6a642SColin Ian King 	 * FIXME: Unsolicited NopIN support for ISER
62603aa2070SNicholas Bellinger 	 */
62703aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
62803aa2070SNicholas Bellinger 		return;
629e48354ceSNicholas Bellinger 
630e48354ceSNicholas Bellinger 	if (!sess->sess_ops->SessionType)
631e48354ceSNicholas Bellinger 		iscsit_start_nopin_timer(conn);
632e48354ceSNicholas Bellinger }
633e48354ceSNicholas Bellinger 
634e5419865SNicholas Bellinger int iscsit_start_kthreads(struct iscsi_conn *conn)
63588dcd2daSNicholas Bellinger {
63688dcd2daSNicholas Bellinger 	int ret = 0;
63788dcd2daSNicholas Bellinger 
63888dcd2daSNicholas Bellinger 	spin_lock(&iscsit_global->ts_bitmap_lock);
63988dcd2daSNicholas Bellinger 	conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
64088dcd2daSNicholas Bellinger 					ISCSIT_BITMAP_BITS, get_order(1));
64188dcd2daSNicholas Bellinger 	spin_unlock(&iscsit_global->ts_bitmap_lock);
64288dcd2daSNicholas Bellinger 
64388dcd2daSNicholas Bellinger 	if (conn->bitmap_id < 0) {
64488dcd2daSNicholas Bellinger 		pr_err("bitmap_find_free_region() failed for"
64588dcd2daSNicholas Bellinger 		       " iscsit_start_kthreads()\n");
64688dcd2daSNicholas Bellinger 		return -ENOMEM;
64788dcd2daSNicholas Bellinger 	}
64888dcd2daSNicholas Bellinger 
64988dcd2daSNicholas Bellinger 	conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
65088dcd2daSNicholas Bellinger 				      "%s", ISCSI_TX_THREAD_NAME);
65188dcd2daSNicholas Bellinger 	if (IS_ERR(conn->tx_thread)) {
65288dcd2daSNicholas Bellinger 		pr_err("Unable to start iscsi_target_tx_thread\n");
65388dcd2daSNicholas Bellinger 		ret = PTR_ERR(conn->tx_thread);
65488dcd2daSNicholas Bellinger 		goto out_bitmap;
65588dcd2daSNicholas Bellinger 	}
65688dcd2daSNicholas Bellinger 	conn->tx_thread_active = true;
65788dcd2daSNicholas Bellinger 
65888dcd2daSNicholas Bellinger 	conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
65988dcd2daSNicholas Bellinger 				      "%s", ISCSI_RX_THREAD_NAME);
66088dcd2daSNicholas Bellinger 	if (IS_ERR(conn->rx_thread)) {
66188dcd2daSNicholas Bellinger 		pr_err("Unable to start iscsi_target_rx_thread\n");
66288dcd2daSNicholas Bellinger 		ret = PTR_ERR(conn->rx_thread);
66388dcd2daSNicholas Bellinger 		goto out_tx;
66488dcd2daSNicholas Bellinger 	}
66588dcd2daSNicholas Bellinger 	conn->rx_thread_active = true;
66688dcd2daSNicholas Bellinger 
66788dcd2daSNicholas Bellinger 	return 0;
66888dcd2daSNicholas Bellinger out_tx:
669e5419865SNicholas Bellinger 	send_sig(SIGINT, conn->tx_thread, 1);
67088dcd2daSNicholas Bellinger 	kthread_stop(conn->tx_thread);
67188dcd2daSNicholas Bellinger 	conn->tx_thread_active = false;
67288dcd2daSNicholas Bellinger out_bitmap:
67388dcd2daSNicholas Bellinger 	spin_lock(&iscsit_global->ts_bitmap_lock);
67488dcd2daSNicholas Bellinger 	bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
67588dcd2daSNicholas Bellinger 			      get_order(1));
67688dcd2daSNicholas Bellinger 	spin_unlock(&iscsit_global->ts_bitmap_lock);
67788dcd2daSNicholas Bellinger 	return ret;
67888dcd2daSNicholas Bellinger }
67988dcd2daSNicholas Bellinger 
680e5419865SNicholas Bellinger void iscsi_post_login_handler(
681e48354ceSNicholas Bellinger 	struct iscsi_np *np,
682e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
683e48354ceSNicholas Bellinger 	u8 zero_tsih)
684e48354ceSNicholas Bellinger {
685e48354ceSNicholas Bellinger 	int stop_timer = 0;
686e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
687e48354ceSNicholas Bellinger 	struct se_session *se_sess = sess->se_sess;
68860bfcf8eSAndy Grover 	struct iscsi_portal_group *tpg = sess->tpg;
689e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
690e48354ceSNicholas Bellinger 
691e48354ceSNicholas Bellinger 	iscsit_inc_conn_usage_count(conn);
692e48354ceSNicholas Bellinger 
693e48354ceSNicholas Bellinger 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
694e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_ACCEPT);
695e48354ceSNicholas Bellinger 
696e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
697e48354ceSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
698e48354ceSNicholas Bellinger 
699e48354ceSNicholas Bellinger 	iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
700e48354ceSNicholas Bellinger 	/*
701e48354ceSNicholas Bellinger 	 * SCSI Initiator -> SCSI Target Port Mapping
702e48354ceSNicholas Bellinger 	 */
703e48354ceSNicholas Bellinger 	if (!zero_tsih) {
704e48354ceSNicholas Bellinger 		iscsi_set_session_parameters(sess->sess_ops,
705e48354ceSNicholas Bellinger 				conn->param_list, 0);
706e48354ceSNicholas Bellinger 		iscsi_release_param_list(conn->param_list);
707e48354ceSNicholas Bellinger 		conn->param_list = NULL;
708e48354ceSNicholas Bellinger 
709e48354ceSNicholas Bellinger 		spin_lock_bh(&sess->conn_lock);
710e48354ceSNicholas Bellinger 		atomic_set(&sess->session_continuation, 0);
711e48354ceSNicholas Bellinger 		if (sess->session_state == TARG_SESS_STATE_FAILED) {
712e48354ceSNicholas Bellinger 			pr_debug("Moving to"
713e48354ceSNicholas Bellinger 					" TARG_SESS_STATE_LOGGED_IN.\n");
714e48354ceSNicholas Bellinger 			sess->session_state = TARG_SESS_STATE_LOGGED_IN;
715e48354ceSNicholas Bellinger 			stop_timer = 1;
716e48354ceSNicholas Bellinger 		}
717e48354ceSNicholas Bellinger 
718dc58f760SAndy Grover 		pr_debug("iSCSI Login successful on CID: %hu from %pISpc to"
719dc58f760SAndy Grover 			" %pISpc,%hu\n", conn->cid, &conn->login_sockaddr,
72069d75574SAndy Grover 			&conn->local_sockaddr, tpg->tpgt);
721e48354ceSNicholas Bellinger 
722e48354ceSNicholas Bellinger 		list_add_tail(&conn->conn_list, &sess->sess_conn_list);
723e48354ceSNicholas Bellinger 		atomic_inc(&sess->nconn);
724e48354ceSNicholas Bellinger 		pr_debug("Incremented iSCSI Connection count to %hu"
725e48354ceSNicholas Bellinger 			" from node: %s\n", atomic_read(&sess->nconn),
726e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
727e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
728e48354ceSNicholas Bellinger 
72988dcd2daSNicholas Bellinger 		iscsi_post_login_start_timers(conn);
730e48354ceSNicholas Bellinger 		/*
731e48354ceSNicholas Bellinger 		 * Determine CPU mask to ensure connection's RX and TX kthreads
732e48354ceSNicholas Bellinger 		 * are scheduled on the same CPU.
733e48354ceSNicholas Bellinger 		 */
734e48354ceSNicholas Bellinger 		iscsit_thread_get_cpumask(conn);
735e48354ceSNicholas Bellinger 		conn->conn_rx_reset_cpumask = 1;
736e48354ceSNicholas Bellinger 		conn->conn_tx_reset_cpumask = 1;
737e5419865SNicholas Bellinger 		/*
738e5419865SNicholas Bellinger 		 * Wakeup the sleeping iscsi_target_rx_thread() now that
739e5419865SNicholas Bellinger 		 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
740e5419865SNicholas Bellinger 		 */
741e5419865SNicholas Bellinger 		complete(&conn->rx_login_comp);
742e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn);
743e5419865SNicholas Bellinger 
744e48354ceSNicholas Bellinger 		if (stop_timer) {
745e48354ceSNicholas Bellinger 			spin_lock_bh(&se_tpg->session_lock);
746e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess);
747e48354ceSNicholas Bellinger 			spin_unlock_bh(&se_tpg->session_lock);
748e48354ceSNicholas Bellinger 		}
749e48354ceSNicholas Bellinger 		iscsit_dec_session_usage_count(sess);
750e5419865SNicholas Bellinger 		return;
751e48354ceSNicholas Bellinger 	}
752e48354ceSNicholas Bellinger 
753e48354ceSNicholas Bellinger 	iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
754e48354ceSNicholas Bellinger 	iscsi_release_param_list(conn->param_list);
755e48354ceSNicholas Bellinger 	conn->param_list = NULL;
756e48354ceSNicholas Bellinger 
757e48354ceSNicholas Bellinger 	iscsit_determine_maxcmdsn(sess);
758e48354ceSNicholas Bellinger 
759e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
760e48354ceSNicholas Bellinger 	__transport_register_session(&sess->tpg->tpg_se_tpg,
7618359cf43SJörn Engel 			se_sess->se_node_acl, se_sess, sess);
762e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
763e48354ceSNicholas Bellinger 	sess->session_state = TARG_SESS_STATE_LOGGED_IN;
764e48354ceSNicholas Bellinger 
765dc58f760SAndy Grover 	pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n",
766dc58f760SAndy Grover 		conn->cid, &conn->login_sockaddr, &conn->local_sockaddr,
7672f9bc894SNicholas Bellinger 		tpg->tpgt);
768e48354ceSNicholas Bellinger 
769e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
770e48354ceSNicholas Bellinger 	list_add_tail(&conn->conn_list, &sess->sess_conn_list);
771e48354ceSNicholas Bellinger 	atomic_inc(&sess->nconn);
772e48354ceSNicholas Bellinger 	pr_debug("Incremented iSCSI Connection count to %hu from node:"
773e48354ceSNicholas Bellinger 		" %s\n", atomic_read(&sess->nconn),
774e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
775e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
776e48354ceSNicholas Bellinger 
777e48354ceSNicholas Bellinger 	sess->sid = tpg->sid++;
778e48354ceSNicholas Bellinger 	if (!sess->sid)
779e48354ceSNicholas Bellinger 		sess->sid = tpg->sid++;
780e48354ceSNicholas Bellinger 	pr_debug("Established iSCSI session from node: %s\n",
781e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
782e48354ceSNicholas Bellinger 
783e48354ceSNicholas Bellinger 	tpg->nsessions++;
784e48354ceSNicholas Bellinger 	if (tpg->tpg_tiqn)
785e48354ceSNicholas Bellinger 		tpg->tpg_tiqn->tiqn_nsessions++;
786e48354ceSNicholas Bellinger 
787e48354ceSNicholas Bellinger 	pr_debug("Incremented number of active iSCSI sessions to %u on"
788e48354ceSNicholas Bellinger 		" iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
789e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
790e48354ceSNicholas Bellinger 
791e48354ceSNicholas Bellinger 	iscsi_post_login_start_timers(conn);
792e48354ceSNicholas Bellinger 	/*
793e48354ceSNicholas Bellinger 	 * Determine CPU mask to ensure connection's RX and TX kthreads
794e48354ceSNicholas Bellinger 	 * are scheduled on the same CPU.
795e48354ceSNicholas Bellinger 	 */
796e48354ceSNicholas Bellinger 	iscsit_thread_get_cpumask(conn);
797e48354ceSNicholas Bellinger 	conn->conn_rx_reset_cpumask = 1;
798e48354ceSNicholas Bellinger 	conn->conn_tx_reset_cpumask = 1;
799e5419865SNicholas Bellinger 	/*
800e5419865SNicholas Bellinger 	 * Wakeup the sleeping iscsi_target_rx_thread() now that
801e5419865SNicholas Bellinger 	 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
802e5419865SNicholas Bellinger 	 */
803e5419865SNicholas Bellinger 	complete(&conn->rx_login_comp);
804e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
805e48354ceSNicholas Bellinger }
806e48354ceSNicholas Bellinger 
807f7c9564aSKees Cook void iscsi_handle_login_thread_timeout(struct timer_list *t)
808e48354ceSNicholas Bellinger {
809f7c9564aSKees Cook 	struct iscsi_np *np = from_timer(np, t, np_login_timer);
810e48354ceSNicholas Bellinger 
811e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
81269d75574SAndy Grover 	pr_err("iSCSI Login timeout on Network Portal %pISpc\n",
81369d75574SAndy Grover 			&np->np_sockaddr);
814e48354ceSNicholas Bellinger 
815e48354ceSNicholas Bellinger 	if (np->np_login_timer_flags & ISCSI_TF_STOP) {
816e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
817e48354ceSNicholas Bellinger 		return;
818e48354ceSNicholas Bellinger 	}
819e48354ceSNicholas Bellinger 
820e48354ceSNicholas Bellinger 	if (np->np_thread)
821e48354ceSNicholas Bellinger 		send_sig(SIGINT, np->np_thread, 1);
822e48354ceSNicholas Bellinger 
823e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
824e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
825e48354ceSNicholas Bellinger }
826e48354ceSNicholas Bellinger 
827e48354ceSNicholas Bellinger static void iscsi_start_login_thread_timer(struct iscsi_np *np)
828e48354ceSNicholas Bellinger {
829e48354ceSNicholas Bellinger 	/*
830e48354ceSNicholas Bellinger 	 * This used the TA_LOGIN_TIMEOUT constant because at this
831e48354ceSNicholas Bellinger 	 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
832e48354ceSNicholas Bellinger 	 */
833e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
834e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_STOP;
835e48354ceSNicholas Bellinger 	np->np_login_timer_flags |= ISCSI_TF_RUNNING;
8368a47aa9dSBart Van Assche 	mod_timer(&np->np_login_timer, jiffies + TA_LOGIN_TIMEOUT * HZ);
837e48354ceSNicholas Bellinger 
838e48354ceSNicholas Bellinger 	pr_debug("Added timeout timer to iSCSI login request for"
839e48354ceSNicholas Bellinger 			" %u seconds.\n", TA_LOGIN_TIMEOUT);
840e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
841e48354ceSNicholas Bellinger }
842e48354ceSNicholas Bellinger 
843e48354ceSNicholas Bellinger static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
844e48354ceSNicholas Bellinger {
845e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
846e48354ceSNicholas Bellinger 	if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
847e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
848e48354ceSNicholas Bellinger 		return;
849e48354ceSNicholas Bellinger 	}
850e48354ceSNicholas Bellinger 	np->np_login_timer_flags |= ISCSI_TF_STOP;
851e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
852e48354ceSNicholas Bellinger 
853e48354ceSNicholas Bellinger 	del_timer_sync(&np->np_login_timer);
854e48354ceSNicholas Bellinger 
855e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
856e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
857e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
858e48354ceSNicholas Bellinger }
859e48354ceSNicholas Bellinger 
860baa4d64bSNicholas Bellinger int iscsit_setup_np(
861e48354ceSNicholas Bellinger 	struct iscsi_np *np,
86213a3cf08SAndy Grover 	struct sockaddr_storage *sockaddr)
863e48354ceSNicholas Bellinger {
864baa4d64bSNicholas Bellinger 	struct socket *sock = NULL;
865837f6452SNicholas Bellinger 	int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
866e48354ceSNicholas Bellinger 
867e48354ceSNicholas Bellinger 	switch (np->np_network_transport) {
868e48354ceSNicholas Bellinger 	case ISCSI_TCP:
869e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_TCP;
870e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_STREAM;
871e48354ceSNicholas Bellinger 		break;
872e48354ceSNicholas Bellinger 	case ISCSI_SCTP_TCP:
873e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_SCTP;
874e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_STREAM;
875e48354ceSNicholas Bellinger 		break;
876e48354ceSNicholas Bellinger 	case ISCSI_SCTP_UDP:
877e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_SCTP;
878e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_SEQPACKET;
879e48354ceSNicholas Bellinger 		break;
880e48354ceSNicholas Bellinger 	default:
881e48354ceSNicholas Bellinger 		pr_err("Unsupported network_transport: %d\n",
882e48354ceSNicholas Bellinger 				np->np_network_transport);
883e48354ceSNicholas Bellinger 		return -EINVAL;
884e48354ceSNicholas Bellinger 	}
885e48354ceSNicholas Bellinger 
886e48354ceSNicholas Bellinger 	ret = sock_create(sockaddr->ss_family, np->np_sock_type,
887e48354ceSNicholas Bellinger 			np->np_ip_proto, &sock);
888e48354ceSNicholas Bellinger 	if (ret < 0) {
889e48354ceSNicholas Bellinger 		pr_err("sock_create() failed.\n");
890e48354ceSNicholas Bellinger 		return ret;
891e48354ceSNicholas Bellinger 	}
892e48354ceSNicholas Bellinger 	np->np_socket = sock;
893e48354ceSNicholas Bellinger 	/*
894e48354ceSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
895e48354ceSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
896e48354ceSNicholas Bellinger 	 */
8978359cf43SJörn Engel 	memcpy(&np->np_sockaddr, sockaddr,
89813a3cf08SAndy Grover 			sizeof(struct sockaddr_storage));
899e48354ceSNicholas Bellinger 
900e48354ceSNicholas Bellinger 	if (sockaddr->ss_family == AF_INET6)
901e48354ceSNicholas Bellinger 		len = sizeof(struct sockaddr_in6);
902e48354ceSNicholas Bellinger 	else
903e48354ceSNicholas Bellinger 		len = sizeof(struct sockaddr_in);
904e48354ceSNicholas Bellinger 	/*
905e48354ceSNicholas Bellinger 	 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
906e48354ceSNicholas Bellinger 	 */
9078359cf43SJörn Engel 	/* FIXME: Someone please explain why this is endian-safe */
908e48354ceSNicholas Bellinger 	opt = 1;
909e48354ceSNicholas Bellinger 	if (np->np_network_transport == ISCSI_TCP) {
910e48354ceSNicholas Bellinger 		ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
911e48354ceSNicholas Bellinger 				(char *)&opt, sizeof(opt));
912e48354ceSNicholas Bellinger 		if (ret < 0) {
913e48354ceSNicholas Bellinger 			pr_err("kernel_setsockopt() for TCP_NODELAY"
914e48354ceSNicholas Bellinger 				" failed: %d\n", ret);
915e48354ceSNicholas Bellinger 			goto fail;
916e48354ceSNicholas Bellinger 		}
917e48354ceSNicholas Bellinger 	}
918e48354ceSNicholas Bellinger 
9198359cf43SJörn Engel 	/* FIXME: Someone please explain why this is endian-safe */
920e48354ceSNicholas Bellinger 	ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
921e48354ceSNicholas Bellinger 			(char *)&opt, sizeof(opt));
922e48354ceSNicholas Bellinger 	if (ret < 0) {
923e48354ceSNicholas Bellinger 		pr_err("kernel_setsockopt() for SO_REUSEADDR"
924e48354ceSNicholas Bellinger 			" failed\n");
925e48354ceSNicholas Bellinger 		goto fail;
926e48354ceSNicholas Bellinger 	}
927e48354ceSNicholas Bellinger 
9289f9ef6d3SDax Kelson 	ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
9299f9ef6d3SDax Kelson 			(char *)&opt, sizeof(opt));
9309f9ef6d3SDax Kelson 	if (ret < 0) {
9319f9ef6d3SDax Kelson 		pr_err("kernel_setsockopt() for IP_FREEBIND"
9329f9ef6d3SDax Kelson 			" failed\n");
9339f9ef6d3SDax Kelson 		goto fail;
9349f9ef6d3SDax Kelson 	}
9359f9ef6d3SDax Kelson 
936e48354ceSNicholas Bellinger 	ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
937e48354ceSNicholas Bellinger 	if (ret < 0) {
938e48354ceSNicholas Bellinger 		pr_err("kernel_bind() failed: %d\n", ret);
939e48354ceSNicholas Bellinger 		goto fail;
940e48354ceSNicholas Bellinger 	}
941e48354ceSNicholas Bellinger 
942e48354ceSNicholas Bellinger 	ret = kernel_listen(sock, backlog);
943e48354ceSNicholas Bellinger 	if (ret != 0) {
944e48354ceSNicholas Bellinger 		pr_err("kernel_listen() failed: %d\n", ret);
945e48354ceSNicholas Bellinger 		goto fail;
946e48354ceSNicholas Bellinger 	}
947e48354ceSNicholas Bellinger 
948e48354ceSNicholas Bellinger 	return 0;
949e48354ceSNicholas Bellinger fail:
950e48354ceSNicholas Bellinger 	np->np_socket = NULL;
951e48354ceSNicholas Bellinger 	sock_release(sock);
952e48354ceSNicholas Bellinger 	return ret;
953e48354ceSNicholas Bellinger }
954e48354ceSNicholas Bellinger 
955baa4d64bSNicholas Bellinger int iscsi_target_setup_login_socket(
956baa4d64bSNicholas Bellinger 	struct iscsi_np *np,
95713a3cf08SAndy Grover 	struct sockaddr_storage *sockaddr)
958baa4d64bSNicholas Bellinger {
959baa4d64bSNicholas Bellinger 	struct iscsit_transport *t;
960baa4d64bSNicholas Bellinger 	int rc;
961baa4d64bSNicholas Bellinger 
962baa4d64bSNicholas Bellinger 	t = iscsit_get_transport(np->np_network_transport);
963baa4d64bSNicholas Bellinger 	if (!t)
964baa4d64bSNicholas Bellinger 		return -EINVAL;
965baa4d64bSNicholas Bellinger 
966baa4d64bSNicholas Bellinger 	rc = t->iscsit_setup_np(np, sockaddr);
967baa4d64bSNicholas Bellinger 	if (rc < 0) {
968baa4d64bSNicholas Bellinger 		iscsit_put_transport(t);
969baa4d64bSNicholas Bellinger 		return rc;
970baa4d64bSNicholas Bellinger 	}
971baa4d64bSNicholas Bellinger 
972baa4d64bSNicholas Bellinger 	np->np_transport = t;
97314f4b54fSSagi Grimberg 	np->enabled = true;
974baa4d64bSNicholas Bellinger 	return 0;
975baa4d64bSNicholas Bellinger }
976baa4d64bSNicholas Bellinger 
977baa4d64bSNicholas Bellinger int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
978baa4d64bSNicholas Bellinger {
979baa4d64bSNicholas Bellinger 	struct socket *new_sock, *sock = np->np_socket;
980baa4d64bSNicholas Bellinger 	struct sockaddr_in sock_in;
981baa4d64bSNicholas Bellinger 	struct sockaddr_in6 sock_in6;
9829b2c45d4SDenys Vlasenko 	int rc;
983baa4d64bSNicholas Bellinger 
984baa4d64bSNicholas Bellinger 	rc = kernel_accept(sock, &new_sock, 0);
985baa4d64bSNicholas Bellinger 	if (rc < 0)
986baa4d64bSNicholas Bellinger 		return rc;
987baa4d64bSNicholas Bellinger 
988baa4d64bSNicholas Bellinger 	conn->sock = new_sock;
989baa4d64bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
990baa4d64bSNicholas Bellinger 
991baa4d64bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
992baa4d64bSNicholas Bellinger 		memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
993baa4d64bSNicholas Bellinger 
994baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
9959b2c45d4SDenys Vlasenko 				(struct sockaddr *)&sock_in6, 1);
9969b2c45d4SDenys Vlasenko 		if (rc >= 0) {
997dc58f760SAndy Grover 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
998dc58f760SAndy Grover 				memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6));
999dc58f760SAndy Grover 			} else {
1000dc58f760SAndy Grover 				/* Pretend to be an ipv4 socket */
1001dc58f760SAndy Grover 				sock_in.sin_family = AF_INET;
1002dc58f760SAndy Grover 				sock_in.sin_port = sock_in6.sin6_port;
1003dc58f760SAndy Grover 				memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1004dc58f760SAndy Grover 				memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1005dc58f760SAndy Grover 			}
1006baa4d64bSNicholas Bellinger 		}
1007baa4d64bSNicholas Bellinger 
1008baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
10099b2c45d4SDenys Vlasenko 				(struct sockaddr *)&sock_in6, 0);
10109b2c45d4SDenys Vlasenko 		if (rc >= 0) {
101169d75574SAndy Grover 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
101269d75574SAndy Grover 				memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
101369d75574SAndy Grover 			} else {
101469d75574SAndy Grover 				/* Pretend to be an ipv4 socket */
101569d75574SAndy Grover 				sock_in.sin_family = AF_INET;
101669d75574SAndy Grover 				sock_in.sin_port = sock_in6.sin6_port;
101769d75574SAndy Grover 				memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
101869d75574SAndy Grover 				memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
101969d75574SAndy Grover 			}
1020baa4d64bSNicholas Bellinger 		}
1021baa4d64bSNicholas Bellinger 	} else {
1022baa4d64bSNicholas Bellinger 		memset(&sock_in, 0, sizeof(struct sockaddr_in));
1023baa4d64bSNicholas Bellinger 
1024baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
10259b2c45d4SDenys Vlasenko 				(struct sockaddr *)&sock_in, 1);
10269b2c45d4SDenys Vlasenko 		if (rc >= 0)
1027dc58f760SAndy Grover 			memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1028baa4d64bSNicholas Bellinger 
1029baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
10309b2c45d4SDenys Vlasenko 				(struct sockaddr *)&sock_in, 0);
10319b2c45d4SDenys Vlasenko 		if (rc >= 0)
103269d75574SAndy Grover 			memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1033baa4d64bSNicholas Bellinger 	}
1034baa4d64bSNicholas Bellinger 
1035baa4d64bSNicholas Bellinger 	return 0;
1036baa4d64bSNicholas Bellinger }
1037baa4d64bSNicholas Bellinger 
1038baa4d64bSNicholas Bellinger int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1039baa4d64bSNicholas Bellinger {
1040baa4d64bSNicholas Bellinger 	struct iscsi_login_req *login_req;
1041baa4d64bSNicholas Bellinger 	u32 padding = 0, payload_length;
1042baa4d64bSNicholas Bellinger 
1043baa4d64bSNicholas Bellinger 	if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1044baa4d64bSNicholas Bellinger 		return -1;
1045baa4d64bSNicholas Bellinger 
1046baa4d64bSNicholas Bellinger 	login_req = (struct iscsi_login_req *)login->req;
1047baa4d64bSNicholas Bellinger 	payload_length	= ntoh24(login_req->dlength);
1048baa4d64bSNicholas Bellinger 	padding = ((-payload_length) & 3);
1049baa4d64bSNicholas Bellinger 
1050baa4d64bSNicholas Bellinger 	pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1051baa4d64bSNicholas Bellinger 		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1052baa4d64bSNicholas Bellinger 		login_req->flags, login_req->itt, login_req->cmdsn,
1053baa4d64bSNicholas Bellinger 		login_req->exp_statsn, login_req->cid, payload_length);
1054baa4d64bSNicholas Bellinger 	/*
1055baa4d64bSNicholas Bellinger 	 * Setup the initial iscsi_login values from the leading
1056baa4d64bSNicholas Bellinger 	 * login request PDU.
1057baa4d64bSNicholas Bellinger 	 */
1058baa4d64bSNicholas Bellinger 	if (login->first_request) {
1059baa4d64bSNicholas Bellinger 		login_req = (struct iscsi_login_req *)login->req;
1060baa4d64bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
10613e1c81a9SNicholas Bellinger 		login->current_stage	= ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1062baa4d64bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1063baa4d64bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1064baa4d64bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1065baa4d64bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1066baa4d64bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1067baa4d64bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1068baa4d64bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1069baa4d64bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1070baa4d64bSNicholas Bellinger 	}
1071baa4d64bSNicholas Bellinger 
1072baa4d64bSNicholas Bellinger 	if (iscsi_target_check_login_request(conn, login) < 0)
1073baa4d64bSNicholas Bellinger 		return -1;
1074baa4d64bSNicholas Bellinger 
1075baa4d64bSNicholas Bellinger 	memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1076baa4d64bSNicholas Bellinger 	if (iscsi_login_rx_data(conn, login->req_buf,
1077baa4d64bSNicholas Bellinger 				payload_length + padding) < 0)
1078baa4d64bSNicholas Bellinger 		return -1;
1079baa4d64bSNicholas Bellinger 
1080baa4d64bSNicholas Bellinger 	return 0;
1081baa4d64bSNicholas Bellinger }
1082baa4d64bSNicholas Bellinger 
1083baa4d64bSNicholas Bellinger int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1084baa4d64bSNicholas Bellinger 			u32 length)
1085baa4d64bSNicholas Bellinger {
1086baa4d64bSNicholas Bellinger 	if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1087baa4d64bSNicholas Bellinger 		return -1;
1088baa4d64bSNicholas Bellinger 
1089baa4d64bSNicholas Bellinger 	return 0;
1090baa4d64bSNicholas Bellinger }
1091baa4d64bSNicholas Bellinger 
1092baa4d64bSNicholas Bellinger static int
1093baa4d64bSNicholas Bellinger iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1094baa4d64bSNicholas Bellinger {
1095baa4d64bSNicholas Bellinger 	int rc;
1096baa4d64bSNicholas Bellinger 
1097baa4d64bSNicholas Bellinger 	if (!t->owner) {
1098baa4d64bSNicholas Bellinger 		conn->conn_transport = t;
1099baa4d64bSNicholas Bellinger 		return 0;
1100baa4d64bSNicholas Bellinger 	}
1101baa4d64bSNicholas Bellinger 
1102baa4d64bSNicholas Bellinger 	rc = try_module_get(t->owner);
1103baa4d64bSNicholas Bellinger 	if (!rc) {
1104baa4d64bSNicholas Bellinger 		pr_err("try_module_get() failed for %s\n", t->name);
1105baa4d64bSNicholas Bellinger 		return -EINVAL;
1106baa4d64bSNicholas Bellinger 	}
1107baa4d64bSNicholas Bellinger 
1108baa4d64bSNicholas Bellinger 	conn->conn_transport = t;
1109baa4d64bSNicholas Bellinger 	return 0;
1110baa4d64bSNicholas Bellinger }
1111baa4d64bSNicholas Bellinger 
111205a86e78SMike Christie static struct iscsi_conn *iscsit_alloc_conn(struct iscsi_np *np)
111305a86e78SMike Christie {
111405a86e78SMike Christie 	struct iscsi_conn *conn;
111505a86e78SMike Christie 
111605a86e78SMike Christie 	conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
111705a86e78SMike Christie 	if (!conn) {
111805a86e78SMike Christie 		pr_err("Could not allocate memory for new connection\n");
111905a86e78SMike Christie 		return NULL;
112005a86e78SMike Christie 	}
112105a86e78SMike Christie 	pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
112205a86e78SMike Christie 	conn->conn_state = TARG_CONN_STATE_FREE;
112305a86e78SMike Christie 
112405a86e78SMike Christie 	init_waitqueue_head(&conn->queues_wq);
112505a86e78SMike Christie 	INIT_LIST_HEAD(&conn->conn_list);
112605a86e78SMike Christie 	INIT_LIST_HEAD(&conn->conn_cmd_list);
112705a86e78SMike Christie 	INIT_LIST_HEAD(&conn->immed_queue_list);
112805a86e78SMike Christie 	INIT_LIST_HEAD(&conn->response_queue_list);
112905a86e78SMike Christie 	init_completion(&conn->conn_post_wait_comp);
113005a86e78SMike Christie 	init_completion(&conn->conn_wait_comp);
113105a86e78SMike Christie 	init_completion(&conn->conn_wait_rcfr_comp);
113205a86e78SMike Christie 	init_completion(&conn->conn_waiting_on_uc_comp);
113305a86e78SMike Christie 	init_completion(&conn->conn_logout_comp);
113405a86e78SMike Christie 	init_completion(&conn->rx_half_close_comp);
113505a86e78SMike Christie 	init_completion(&conn->tx_half_close_comp);
113605a86e78SMike Christie 	init_completion(&conn->rx_login_comp);
113705a86e78SMike Christie 	spin_lock_init(&conn->cmd_lock);
113805a86e78SMike Christie 	spin_lock_init(&conn->conn_usage_lock);
113905a86e78SMike Christie 	spin_lock_init(&conn->immed_queue_lock);
114005a86e78SMike Christie 	spin_lock_init(&conn->nopin_timer_lock);
114105a86e78SMike Christie 	spin_lock_init(&conn->response_queue_lock);
114205a86e78SMike Christie 	spin_lock_init(&conn->state_lock);
114305a86e78SMike Christie 
114405a86e78SMike Christie 	timer_setup(&conn->nopin_response_timer,
114505a86e78SMike Christie 		    iscsit_handle_nopin_response_timeout, 0);
114605a86e78SMike Christie 	timer_setup(&conn->nopin_timer, iscsit_handle_nopin_timeout, 0);
114705a86e78SMike Christie 
114805a86e78SMike Christie 	if (iscsit_conn_set_transport(conn, np->np_transport) < 0)
114905a86e78SMike Christie 		goto free_conn;
115005a86e78SMike Christie 
115105a86e78SMike Christie 	conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
115205a86e78SMike Christie 	if (!conn->conn_ops) {
115305a86e78SMike Christie 		pr_err("Unable to allocate memory for struct iscsi_conn_ops.\n");
115405a86e78SMike Christie 		goto put_transport;
115505a86e78SMike Christie 	}
115605a86e78SMike Christie 
115705a86e78SMike Christie 	if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
115805a86e78SMike Christie 		pr_err("Unable to allocate conn->conn_cpumask\n");
1159f55d0b40Stangwenji 		goto free_conn_ops;
116005a86e78SMike Christie 	}
116105a86e78SMike Christie 
116205a86e78SMike Christie 	return conn;
116305a86e78SMike Christie 
1164f55d0b40Stangwenji free_conn_ops:
1165f55d0b40Stangwenji 	kfree(conn->conn_ops);
116605a86e78SMike Christie put_transport:
116705a86e78SMike Christie 	iscsit_put_transport(conn->conn_transport);
116805a86e78SMike Christie free_conn:
116905a86e78SMike Christie 	kfree(conn);
117005a86e78SMike Christie 	return NULL;
117105a86e78SMike Christie }
117205a86e78SMike Christie 
117305a86e78SMike Christie void iscsit_free_conn(struct iscsi_conn *conn)
117405a86e78SMike Christie {
117505a86e78SMike Christie 	free_cpumask_var(conn->conn_cpumask);
117605a86e78SMike Christie 	kfree(conn->conn_ops);
117705a86e78SMike Christie 	iscsit_put_transport(conn->conn_transport);
117805a86e78SMike Christie 	kfree(conn);
117905a86e78SMike Christie }
118005a86e78SMike Christie 
1181a91eb7d9SNicholas Bellinger void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1182a91eb7d9SNicholas Bellinger 		struct iscsi_np *np, bool zero_tsih, bool new_sess)
1183a91eb7d9SNicholas Bellinger {
11840bcc297eSChristophe Vu-Brugier 	if (!new_sess)
1185a91eb7d9SNicholas Bellinger 		goto old_sess_out;
1186a91eb7d9SNicholas Bellinger 
1187a91eb7d9SNicholas Bellinger 	pr_err("iSCSI Login negotiation failed.\n");
1188a91eb7d9SNicholas Bellinger 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1189a91eb7d9SNicholas Bellinger 				   ISCSI_LOGIN_STATUS_INIT_ERR);
1190a91eb7d9SNicholas Bellinger 	if (!zero_tsih || !conn->sess)
1191a91eb7d9SNicholas Bellinger 		goto old_sess_out;
119226abc916SMike Christie 
1193a91eb7d9SNicholas Bellinger 	transport_free_session(conn->sess->se_sess);
119431ff0ceeSMatthew Wilcox 	ida_free(&sess_ida, conn->sess->session_index);
1195a91eb7d9SNicholas Bellinger 	kfree(conn->sess->sess_ops);
1196a91eb7d9SNicholas Bellinger 	kfree(conn->sess);
1197a0b3b9b2SSagi Grimberg 	conn->sess = NULL;
1198a91eb7d9SNicholas Bellinger 
1199a91eb7d9SNicholas Bellinger old_sess_out:
1200a91eb7d9SNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1201a91eb7d9SNicholas Bellinger 	/*
1202a91eb7d9SNicholas Bellinger 	 * If login negotiation fails check if the Time2Retain timer
1203a91eb7d9SNicholas Bellinger 	 * needs to be restarted.
1204a91eb7d9SNicholas Bellinger 	 */
1205a91eb7d9SNicholas Bellinger 	if (!zero_tsih && conn->sess) {
1206a91eb7d9SNicholas Bellinger 		spin_lock_bh(&conn->sess->conn_lock);
1207a91eb7d9SNicholas Bellinger 		if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1208a91eb7d9SNicholas Bellinger 			struct se_portal_group *se_tpg =
120960bfcf8eSAndy Grover 					&conn->tpg->tpg_se_tpg;
1210a91eb7d9SNicholas Bellinger 
1211a91eb7d9SNicholas Bellinger 			atomic_set(&conn->sess->session_continuation, 0);
1212a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&conn->sess->conn_lock);
1213a91eb7d9SNicholas Bellinger 			spin_lock_bh(&se_tpg->session_lock);
1214a91eb7d9SNicholas Bellinger 			iscsit_start_time2retain_handler(conn->sess);
1215a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&se_tpg->session_lock);
1216a91eb7d9SNicholas Bellinger 		} else
1217a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&conn->sess->conn_lock);
1218a91eb7d9SNicholas Bellinger 		iscsit_dec_session_usage_count(conn->sess);
1219a91eb7d9SNicholas Bellinger 	}
1220a91eb7d9SNicholas Bellinger 
122169110e3cSHerbert Xu 	ahash_request_free(conn->conn_tx_hash);
122269110e3cSHerbert Xu 	if (conn->conn_rx_hash) {
122369110e3cSHerbert Xu 		struct crypto_ahash *tfm;
122469110e3cSHerbert Xu 
122569110e3cSHerbert Xu 		tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
122669110e3cSHerbert Xu 		ahash_request_free(conn->conn_rx_hash);
122769110e3cSHerbert Xu 		crypto_free_ahash(tfm);
122869110e3cSHerbert Xu 	}
1229a91eb7d9SNicholas Bellinger 
1230a91eb7d9SNicholas Bellinger 	if (conn->param_list) {
1231a91eb7d9SNicholas Bellinger 		iscsi_release_param_list(conn->param_list);
1232a91eb7d9SNicholas Bellinger 		conn->param_list = NULL;
1233a91eb7d9SNicholas Bellinger 	}
1234a91eb7d9SNicholas Bellinger 	iscsi_target_nego_release(conn);
1235a91eb7d9SNicholas Bellinger 
1236a91eb7d9SNicholas Bellinger 	if (conn->sock) {
1237a91eb7d9SNicholas Bellinger 		sock_release(conn->sock);
1238a91eb7d9SNicholas Bellinger 		conn->sock = NULL;
1239a91eb7d9SNicholas Bellinger 	}
1240a91eb7d9SNicholas Bellinger 
1241954f2372SSagi Grimberg 	if (conn->conn_transport->iscsit_wait_conn)
1242954f2372SSagi Grimberg 		conn->conn_transport->iscsit_wait_conn(conn);
1243954f2372SSagi Grimberg 
1244a91eb7d9SNicholas Bellinger 	if (conn->conn_transport->iscsit_free_conn)
1245a91eb7d9SNicholas Bellinger 		conn->conn_transport->iscsit_free_conn(conn);
1246a91eb7d9SNicholas Bellinger 
124705a86e78SMike Christie 	iscsit_free_conn(conn);
1248a91eb7d9SNicholas Bellinger }
1249a91eb7d9SNicholas Bellinger 
1250e48354ceSNicholas Bellinger static int __iscsi_target_login_thread(struct iscsi_np *np)
1251e48354ceSNicholas Bellinger {
1252baa4d64bSNicholas Bellinger 	u8 *buffer, zero_tsih = 0;
125381a9c5e7SMikulas Patocka 	int ret = 0, rc;
1254e48354ceSNicholas Bellinger 	struct iscsi_conn *conn = NULL;
1255e48354ceSNicholas Bellinger 	struct iscsi_login *login;
1256e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = NULL;
1257e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu;
1258a91eb7d9SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np;
1259a91eb7d9SNicholas Bellinger 	bool new_sess = false;
1260e48354ceSNicholas Bellinger 
1261e48354ceSNicholas Bellinger 	flush_signals(current);
1262e48354ceSNicholas Bellinger 
1263e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1264978d13d6SNicholas Bellinger 	if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1265e48354ceSNicholas Bellinger 		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1266978d13d6SNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
1267e48354ceSNicholas Bellinger 		complete(&np->np_restart_comp);
1268978d13d6SNicholas Bellinger 		return 1;
126981a9c5e7SMikulas Patocka 	} else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
127081a9c5e7SMikulas Patocka 		spin_unlock_bh(&np->np_thread_lock);
127181a9c5e7SMikulas Patocka 		goto exit;
1272e48354ceSNicholas Bellinger 	} else {
1273e48354ceSNicholas Bellinger 		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1274e48354ceSNicholas Bellinger 	}
1275e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1276e48354ceSNicholas Bellinger 
127705a86e78SMike Christie 	conn = iscsit_alloc_conn(np);
1278e48354ceSNicholas Bellinger 	if (!conn) {
1279e48354ceSNicholas Bellinger 		/* Get another socket */
1280e48354ceSNicholas Bellinger 		return 1;
1281e48354ceSNicholas Bellinger 	}
1282e48354ceSNicholas Bellinger 
1283baa4d64bSNicholas Bellinger 	rc = np->np_transport->iscsit_accept_np(np, conn);
1284baa4d64bSNicholas Bellinger 	if (rc == -ENOSYS) {
1285baa4d64bSNicholas Bellinger 		complete(&np->np_restart_comp);
128605a86e78SMike Christie 		iscsit_free_conn(conn);
1287baa4d64bSNicholas Bellinger 		goto exit;
1288baa4d64bSNicholas Bellinger 	} else if (rc < 0) {
1289baa4d64bSNicholas Bellinger 		spin_lock_bh(&np->np_thread_lock);
1290978d13d6SNicholas Bellinger 		if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1291978d13d6SNicholas Bellinger 			np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1292baa4d64bSNicholas Bellinger 			spin_unlock_bh(&np->np_thread_lock);
1293baa4d64bSNicholas Bellinger 			complete(&np->np_restart_comp);
129405a86e78SMike Christie 			iscsit_free_conn(conn);
1295baa4d64bSNicholas Bellinger 			/* Get another socket */
1296baa4d64bSNicholas Bellinger 			return 1;
1297baa4d64bSNicholas Bellinger 		}
1298baa4d64bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
129905a86e78SMike Christie 		iscsit_free_conn(conn);
130005a86e78SMike Christie 		return 1;
1301e48354ceSNicholas Bellinger 	}
1302e48354ceSNicholas Bellinger 	/*
1303e48354ceSNicholas Bellinger 	 * Perform the remaining iSCSI connection initialization items..
1304e48354ceSNicholas Bellinger 	 */
1305baa4d64bSNicholas Bellinger 	login = iscsi_login_init_conn(conn);
1306baa4d64bSNicholas Bellinger 	if (!login) {
1307e48354ceSNicholas Bellinger 		goto new_sess_out;
1308e48354ceSNicholas Bellinger 	}
1309e48354ceSNicholas Bellinger 
1310baa4d64bSNicholas Bellinger 	iscsi_start_login_thread_timer(np);
1311e48354ceSNicholas Bellinger 
1312baa4d64bSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1313baa4d64bSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_XPT_UP;
1314baa4d64bSNicholas Bellinger 	/*
1315baa4d64bSNicholas Bellinger 	 * This will process the first login request + payload..
1316baa4d64bSNicholas Bellinger 	 */
1317baa4d64bSNicholas Bellinger 	rc = np->np_transport->iscsit_get_login_rx(conn, login);
1318baa4d64bSNicholas Bellinger 	if (rc == 1)
1319baa4d64bSNicholas Bellinger 		return 1;
1320baa4d64bSNicholas Bellinger 	else if (rc < 0)
1321baa4d64bSNicholas Bellinger 		goto new_sess_out;
1322baa4d64bSNicholas Bellinger 
1323baa4d64bSNicholas Bellinger 	buffer = &login->req[0];
1324e48354ceSNicholas Bellinger 	pdu = (struct iscsi_login_req *)buffer;
1325e48354ceSNicholas Bellinger 	/*
1326e48354ceSNicholas Bellinger 	 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1327e48354ceSNicholas Bellinger 	 * when Status-Class != 0.
1328e48354ceSNicholas Bellinger 	*/
1329e48354ceSNicholas Bellinger 	conn->login_itt	= pdu->itt;
1330e48354ceSNicholas Bellinger 
1331e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1332e48354ceSNicholas Bellinger 	if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1333e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
133469d75574SAndy Grover 		pr_err("iSCSI Network Portal on %pISpc currently not"
133569d75574SAndy Grover 			" active.\n", &np->np_sockaddr);
1336e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1337e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1338e48354ceSNicholas Bellinger 		goto new_sess_out;
1339e48354ceSNicholas Bellinger 	}
1340e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1341e48354ceSNicholas Bellinger 
1342e48354ceSNicholas Bellinger 	conn->network_transport = np->np_network_transport;
1343e48354ceSNicholas Bellinger 
1344dc58f760SAndy Grover 	pr_debug("Received iSCSI login request from %pISpc on %s Network"
1345dc58f760SAndy Grover 		" Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name,
134669d75574SAndy Grover 		&conn->local_sockaddr);
1347e48354ceSNicholas Bellinger 
1348e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1349e48354ceSNicholas Bellinger 	conn->conn_state	= TARG_CONN_STATE_IN_LOGIN;
1350e48354ceSNicholas Bellinger 
1351e48354ceSNicholas Bellinger 	if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1352e48354ceSNicholas Bellinger 			pdu->min_version) < 0)
1353e48354ceSNicholas Bellinger 		goto new_sess_out;
1354e48354ceSNicholas Bellinger 
1355e48354ceSNicholas Bellinger 	zero_tsih = (pdu->tsih == 0x0000);
1356ee1b1b9cSAndy Grover 	if (zero_tsih) {
1357e48354ceSNicholas Bellinger 		/*
1358e48354ceSNicholas Bellinger 		 * This is the leading connection of a new session.
1359e48354ceSNicholas Bellinger 		 * We wait until after authentication to check for
1360e48354ceSNicholas Bellinger 		 * session reinstatement.
1361e48354ceSNicholas Bellinger 		 */
1362e48354ceSNicholas Bellinger 		if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1363e48354ceSNicholas Bellinger 			goto new_sess_out;
1364e48354ceSNicholas Bellinger 	} else {
1365e48354ceSNicholas Bellinger 		/*
1366e48354ceSNicholas Bellinger 		 * Add a new connection to an existing session.
1367e48354ceSNicholas Bellinger 		 * We check for a non-existant session in
1368e48354ceSNicholas Bellinger 		 * iscsi_login_non_zero_tsih_s2() below based
1369e48354ceSNicholas Bellinger 		 * on ISID/TSIH, but wait until after authentication
1370e48354ceSNicholas Bellinger 		 * to check for connection reinstatement, etc.
1371e48354ceSNicholas Bellinger 		 */
1372e48354ceSNicholas Bellinger 		if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1373e48354ceSNicholas Bellinger 			goto new_sess_out;
1374e48354ceSNicholas Bellinger 	}
1375e48354ceSNicholas Bellinger 	/*
1376baa4d64bSNicholas Bellinger 	 * SessionType: Discovery
1377baa4d64bSNicholas Bellinger 	 *
1378baa4d64bSNicholas Bellinger 	 * 	Locates Default Portal
1379baa4d64bSNicholas Bellinger 	 *
1380baa4d64bSNicholas Bellinger 	 * SessionType: Normal
1381baa4d64bSNicholas Bellinger 	 *
1382baa4d64bSNicholas Bellinger 	 * 	Locates Target Portal from NP -> Target IQN
1383e48354ceSNicholas Bellinger 	 */
1384baa4d64bSNicholas Bellinger 	rc = iscsi_target_locate_portal(np, conn, login);
1385baa4d64bSNicholas Bellinger 	if (rc < 0) {
1386e48354ceSNicholas Bellinger 		tpg = conn->tpg;
1387e48354ceSNicholas Bellinger 		goto new_sess_out;
1388e48354ceSNicholas Bellinger 	}
1389a91eb7d9SNicholas Bellinger 	login->zero_tsih = zero_tsih;
1390e48354ceSNicholas Bellinger 
13918abc718dSFeng Li 	if (conn->sess)
139223a548eeSSagi Grimberg 		conn->sess->se_sess->sup_prot_ops =
139323a548eeSSagi Grimberg 			conn->conn_transport->iscsit_get_sup_prot_ops(conn);
139423a548eeSSagi Grimberg 
1395e48354ceSNicholas Bellinger 	tpg = conn->tpg;
1396e48354ceSNicholas Bellinger 	if (!tpg) {
1397e48354ceSNicholas Bellinger 		pr_err("Unable to locate struct iscsi_conn->tpg\n");
1398e48354ceSNicholas Bellinger 		goto new_sess_out;
1399e48354ceSNicholas Bellinger 	}
1400e48354ceSNicholas Bellinger 
1401e48354ceSNicholas Bellinger 	if (zero_tsih) {
1402baa4d64bSNicholas Bellinger 		if (iscsi_login_zero_tsih_s2(conn) < 0)
1403e48354ceSNicholas Bellinger 			goto new_sess_out;
1404e48354ceSNicholas Bellinger 	} else {
1405baa4d64bSNicholas Bellinger 		if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1406e48354ceSNicholas Bellinger 			goto old_sess_out;
1407e48354ceSNicholas Bellinger 	}
1408e48354ceSNicholas Bellinger 
140942bc3e57SVarun Prakash 	if (conn->conn_transport->iscsit_validate_params) {
141042bc3e57SVarun Prakash 		ret = conn->conn_transport->iscsit_validate_params(conn);
141142bc3e57SVarun Prakash 		if (ret < 0) {
141242bc3e57SVarun Prakash 			if (zero_tsih)
141342bc3e57SVarun Prakash 				goto new_sess_out;
141442bc3e57SVarun Prakash 			else
141542bc3e57SVarun Prakash 				goto old_sess_out;
141642bc3e57SVarun Prakash 		}
141742bc3e57SVarun Prakash 	}
141842bc3e57SVarun Prakash 
1419a91eb7d9SNicholas Bellinger 	ret = iscsi_target_start_negotiation(login, conn);
1420a91eb7d9SNicholas Bellinger 	if (ret < 0)
1421e48354ceSNicholas Bellinger 		goto new_sess_out;
1422e48354ceSNicholas Bellinger 
1423e48354ceSNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1424e48354ceSNicholas Bellinger 
1425a91eb7d9SNicholas Bellinger 	if (ret == 1) {
1426a91eb7d9SNicholas Bellinger 		tpg_np = conn->tpg_np;
1427e48354ceSNicholas Bellinger 
1428e5419865SNicholas Bellinger 		iscsi_post_login_handler(np, conn, zero_tsih);
1429a91eb7d9SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
1430a91eb7d9SNicholas Bellinger 	}
1431a91eb7d9SNicholas Bellinger 
1432e48354ceSNicholas Bellinger 	tpg = NULL;
1433a91eb7d9SNicholas Bellinger 	tpg_np = NULL;
1434e48354ceSNicholas Bellinger 	/* Get another socket */
1435e48354ceSNicholas Bellinger 	return 1;
1436e48354ceSNicholas Bellinger 
1437e48354ceSNicholas Bellinger new_sess_out:
1438a91eb7d9SNicholas Bellinger 	new_sess = true;
1439e48354ceSNicholas Bellinger old_sess_out:
1440a91eb7d9SNicholas Bellinger 	tpg_np = conn->tpg_np;
1441a91eb7d9SNicholas Bellinger 	iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1442a91eb7d9SNicholas Bellinger 	new_sess = false;
1443e48354ceSNicholas Bellinger 
1444e48354ceSNicholas Bellinger 	if (tpg) {
1445a91eb7d9SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
1446e48354ceSNicholas Bellinger 		tpg = NULL;
1447a91eb7d9SNicholas Bellinger 		tpg_np = NULL;
1448e48354ceSNicholas Bellinger 	}
1449e48354ceSNicholas Bellinger 
1450e48354ceSNicholas Bellinger 	return 1;
145181a9c5e7SMikulas Patocka 
1452baa4d64bSNicholas Bellinger exit:
1453e48354ceSNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1454e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1455e48354ceSNicholas Bellinger 	np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1456e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1457baa4d64bSNicholas Bellinger 
1458e48354ceSNicholas Bellinger 	return 0;
1459e48354ceSNicholas Bellinger }
1460e48354ceSNicholas Bellinger 
1461e48354ceSNicholas Bellinger int iscsi_target_login_thread(void *arg)
1462e48354ceSNicholas Bellinger {
14638359cf43SJörn Engel 	struct iscsi_np *np = arg;
1464e48354ceSNicholas Bellinger 	int ret;
1465e48354ceSNicholas Bellinger 
1466e48354ceSNicholas Bellinger 	allow_signal(SIGINT);
1467e48354ceSNicholas Bellinger 
146881a9c5e7SMikulas Patocka 	while (1) {
1469e48354ceSNicholas Bellinger 		ret = __iscsi_target_login_thread(np);
1470e48354ceSNicholas Bellinger 		/*
1471e48354ceSNicholas Bellinger 		 * We break and exit here unless another sock_accept() call
1472e48354ceSNicholas Bellinger 		 * is expected.
1473e48354ceSNicholas Bellinger 		 */
1474e48354ceSNicholas Bellinger 		if (ret != 1)
1475e48354ceSNicholas Bellinger 			break;
1476e48354ceSNicholas Bellinger 	}
1477e48354ceSNicholas Bellinger 
14785e0cf5e6SJiang Yi 	while (!kthread_should_stop()) {
14795e0cf5e6SJiang Yi 		msleep(100);
14805e0cf5e6SJiang Yi 	}
14815e0cf5e6SJiang Yi 
1482e48354ceSNicholas Bellinger 	return 0;
1483e48354ceSNicholas Bellinger }
1484