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>
20e48354ceSNicholas Bellinger #include <linux/string.h>
21e48354ceSNicholas Bellinger #include <linux/kthread.h>
2240401530SAl Viro #include <linux/idr.h>
23e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
24e48354ceSNicholas Bellinger #include <target/target_core_base.h>
25c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
26e48354ceSNicholas Bellinger 
2767f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h>
2867f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_stat.h>
29e48354ceSNicholas Bellinger #include "iscsi_target_device.h"
30e48354ceSNicholas Bellinger #include "iscsi_target_nego.h"
31e48354ceSNicholas Bellinger #include "iscsi_target_erl0.h"
32e48354ceSNicholas Bellinger #include "iscsi_target_erl2.h"
33e48354ceSNicholas Bellinger #include "iscsi_target_login.h"
34e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
35e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
36e48354ceSNicholas Bellinger #include "iscsi_target.h"
37e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
38e48354ceSNicholas Bellinger 
39baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
40baa4d64bSNicholas Bellinger 
41baa4d64bSNicholas Bellinger static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
42e48354ceSNicholas Bellinger {
43baa4d64bSNicholas Bellinger 	struct iscsi_login *login;
44baa4d64bSNicholas Bellinger 
45baa4d64bSNicholas Bellinger 	login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
46baa4d64bSNicholas Bellinger 	if (!login) {
47baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for struct iscsi_login.\n");
48baa4d64bSNicholas Bellinger 		return NULL;
49baa4d64bSNicholas Bellinger 	}
50a91eb7d9SNicholas Bellinger 	conn->login = login;
51baa4d64bSNicholas Bellinger 	login->conn = conn;
52baa4d64bSNicholas Bellinger 	login->first_request = 1;
53baa4d64bSNicholas Bellinger 
54baa4d64bSNicholas Bellinger 	login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
55baa4d64bSNicholas Bellinger 	if (!login->req_buf) {
56baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for response buffer.\n");
57baa4d64bSNicholas Bellinger 		goto out_login;
58baa4d64bSNicholas Bellinger 	}
59baa4d64bSNicholas Bellinger 
60baa4d64bSNicholas Bellinger 	login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
61baa4d64bSNicholas Bellinger 	if (!login->rsp_buf) {
62baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for request buffer.\n");
63baa4d64bSNicholas Bellinger 		goto out_req_buf;
64baa4d64bSNicholas Bellinger 	}
65baa4d64bSNicholas Bellinger 
66baa4d64bSNicholas Bellinger 	conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
67baa4d64bSNicholas Bellinger 	if (!conn->conn_ops) {
68baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for"
69baa4d64bSNicholas Bellinger 			" struct iscsi_conn_ops.\n");
70baa4d64bSNicholas Bellinger 		goto out_rsp_buf;
71baa4d64bSNicholas Bellinger 	}
72baa4d64bSNicholas Bellinger 
73d5627acbSRoland Dreier 	init_waitqueue_head(&conn->queues_wq);
74e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->conn_list);
75e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->conn_cmd_list);
76e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->immed_queue_list);
77e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->response_queue_list);
78e48354ceSNicholas Bellinger 	init_completion(&conn->conn_post_wait_comp);
79e48354ceSNicholas Bellinger 	init_completion(&conn->conn_wait_comp);
80e48354ceSNicholas Bellinger 	init_completion(&conn->conn_wait_rcfr_comp);
81e48354ceSNicholas Bellinger 	init_completion(&conn->conn_waiting_on_uc_comp);
82e48354ceSNicholas Bellinger 	init_completion(&conn->conn_logout_comp);
83e48354ceSNicholas Bellinger 	init_completion(&conn->rx_half_close_comp);
84e48354ceSNicholas Bellinger 	init_completion(&conn->tx_half_close_comp);
85e5419865SNicholas Bellinger 	init_completion(&conn->rx_login_comp);
86e48354ceSNicholas Bellinger 	spin_lock_init(&conn->cmd_lock);
87e48354ceSNicholas Bellinger 	spin_lock_init(&conn->conn_usage_lock);
88e48354ceSNicholas Bellinger 	spin_lock_init(&conn->immed_queue_lock);
89e48354ceSNicholas Bellinger 	spin_lock_init(&conn->nopin_timer_lock);
90e48354ceSNicholas Bellinger 	spin_lock_init(&conn->response_queue_lock);
91e48354ceSNicholas Bellinger 	spin_lock_init(&conn->state_lock);
92e48354ceSNicholas Bellinger 
93e48354ceSNicholas Bellinger 	if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
94e48354ceSNicholas Bellinger 		pr_err("Unable to allocate conn->conn_cpumask\n");
95baa4d64bSNicholas Bellinger 		goto out_conn_ops;
96e48354ceSNicholas Bellinger 	}
97baa4d64bSNicholas Bellinger 	conn->conn_login = login;
98e48354ceSNicholas Bellinger 
99baa4d64bSNicholas Bellinger 	return login;
100baa4d64bSNicholas Bellinger 
101baa4d64bSNicholas Bellinger out_conn_ops:
102baa4d64bSNicholas Bellinger 	kfree(conn->conn_ops);
103baa4d64bSNicholas Bellinger out_rsp_buf:
104baa4d64bSNicholas Bellinger 	kfree(login->rsp_buf);
105baa4d64bSNicholas Bellinger out_req_buf:
106baa4d64bSNicholas Bellinger 	kfree(login->req_buf);
107baa4d64bSNicholas Bellinger out_login:
108baa4d64bSNicholas Bellinger 	kfree(login);
109baa4d64bSNicholas Bellinger 	return NULL;
110e48354ceSNicholas Bellinger }
111e48354ceSNicholas Bellinger 
112e48354ceSNicholas Bellinger /*
113e48354ceSNicholas Bellinger  * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
114e48354ceSNicholas Bellinger  * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
115e48354ceSNicholas Bellinger  */
116e48354ceSNicholas Bellinger int iscsi_login_setup_crypto(struct iscsi_conn *conn)
117e48354ceSNicholas Bellinger {
11869110e3cSHerbert Xu 	struct crypto_ahash *tfm;
11969110e3cSHerbert Xu 
120e48354ceSNicholas Bellinger 	/*
121e48354ceSNicholas Bellinger 	 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
122e48354ceSNicholas Bellinger 	 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
123e48354ceSNicholas Bellinger 	 * to software 1x8 byte slicing from crc32c.ko
124e48354ceSNicholas Bellinger 	 */
12569110e3cSHerbert Xu 	tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
12669110e3cSHerbert Xu 	if (IS_ERR(tfm)) {
12769110e3cSHerbert Xu 		pr_err("crypto_alloc_ahash() failed\n");
128e48354ceSNicholas Bellinger 		return -ENOMEM;
129e48354ceSNicholas Bellinger 	}
130e48354ceSNicholas Bellinger 
13169110e3cSHerbert Xu 	conn->conn_rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
13269110e3cSHerbert Xu 	if (!conn->conn_rx_hash) {
13369110e3cSHerbert Xu 		pr_err("ahash_request_alloc() failed for conn_rx_hash\n");
13469110e3cSHerbert Xu 		crypto_free_ahash(tfm);
135e48354ceSNicholas Bellinger 		return -ENOMEM;
136e48354ceSNicholas Bellinger 	}
13769110e3cSHerbert Xu 	ahash_request_set_callback(conn->conn_rx_hash, 0, NULL, NULL);
13869110e3cSHerbert Xu 
13969110e3cSHerbert Xu 	conn->conn_tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
14069110e3cSHerbert Xu 	if (!conn->conn_tx_hash) {
14169110e3cSHerbert Xu 		pr_err("ahash_request_alloc() failed for conn_tx_hash\n");
14269110e3cSHerbert Xu 		ahash_request_free(conn->conn_rx_hash);
14369110e3cSHerbert Xu 		conn->conn_rx_hash = NULL;
14469110e3cSHerbert Xu 		crypto_free_ahash(tfm);
14569110e3cSHerbert Xu 		return -ENOMEM;
14669110e3cSHerbert Xu 	}
14769110e3cSHerbert Xu 	ahash_request_set_callback(conn->conn_tx_hash, 0, NULL, NULL);
148e48354ceSNicholas Bellinger 
149e48354ceSNicholas Bellinger 	return 0;
150e48354ceSNicholas Bellinger }
151e48354ceSNicholas Bellinger 
152e48354ceSNicholas Bellinger static int iscsi_login_check_initiator_version(
153e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
154e48354ceSNicholas Bellinger 	u8 version_max,
155e48354ceSNicholas Bellinger 	u8 version_min)
156e48354ceSNicholas Bellinger {
157e48354ceSNicholas Bellinger 	if ((version_max != 0x00) || (version_min != 0x00)) {
158e48354ceSNicholas Bellinger 		pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
159e48354ceSNicholas Bellinger 			" version Min/Max 0x%02x/0x%02x, rejecting login.\n",
160e48354ceSNicholas Bellinger 			version_min, version_max);
161e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
162e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_VERSION);
163e48354ceSNicholas Bellinger 		return -1;
164e48354ceSNicholas Bellinger 	}
165e48354ceSNicholas Bellinger 
166e48354ceSNicholas Bellinger 	return 0;
167e48354ceSNicholas Bellinger }
168e48354ceSNicholas Bellinger 
169e48354ceSNicholas Bellinger int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
170e48354ceSNicholas Bellinger {
171e48354ceSNicholas Bellinger 	int sessiontype;
172e48354ceSNicholas Bellinger 	struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
173e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
174e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL, *sess_p = NULL;
175e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
176e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
177e48354ceSNicholas Bellinger 
178e48354ceSNicholas Bellinger 	initiatorname_param = iscsi_find_param_from_key(
179e48354ceSNicholas Bellinger 			INITIATORNAME, conn->param_list);
180e48354ceSNicholas Bellinger 	sessiontype_param = iscsi_find_param_from_key(
181e48354ceSNicholas Bellinger 			SESSIONTYPE, conn->param_list);
1821c5c12c6SRoland Dreier 	if (!initiatorname_param || !sessiontype_param) {
1831c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1841c5c12c6SRoland Dreier 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
185e48354ceSNicholas Bellinger 		return -1;
1861c5c12c6SRoland Dreier 	}
187e48354ceSNicholas Bellinger 
188e48354ceSNicholas Bellinger 	sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
189e48354ceSNicholas Bellinger 
190e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
191e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
192e48354ceSNicholas Bellinger 			sess_list) {
193e48354ceSNicholas Bellinger 
1948359cf43SJörn Engel 		sess_p = se_sess->fabric_sess_ptr;
195e48354ceSNicholas Bellinger 		spin_lock(&sess_p->conn_lock);
196e48354ceSNicholas Bellinger 		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
197e48354ceSNicholas Bellinger 		    atomic_read(&sess_p->session_logout) ||
198e48354ceSNicholas Bellinger 		    (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
199e48354ceSNicholas Bellinger 			spin_unlock(&sess_p->conn_lock);
200e48354ceSNicholas Bellinger 			continue;
201e48354ceSNicholas Bellinger 		}
2028359cf43SJörn Engel 		if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
2038359cf43SJörn Engel 		   (!strcmp(sess_p->sess_ops->InitiatorName,
2048359cf43SJörn Engel 			    initiatorname_param->value) &&
205e48354ceSNicholas Bellinger 		   (sess_p->sess_ops->SessionType == sessiontype))) {
206e48354ceSNicholas Bellinger 			atomic_set(&sess_p->session_reinstatement, 1);
207e48354ceSNicholas Bellinger 			spin_unlock(&sess_p->conn_lock);
208e48354ceSNicholas Bellinger 			iscsit_inc_session_usage_count(sess_p);
209e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess_p);
210e48354ceSNicholas Bellinger 			sess = sess_p;
211e48354ceSNicholas Bellinger 			break;
212e48354ceSNicholas Bellinger 		}
213e48354ceSNicholas Bellinger 		spin_unlock(&sess_p->conn_lock);
214e48354ceSNicholas Bellinger 	}
215e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
216e48354ceSNicholas Bellinger 	/*
217e48354ceSNicholas Bellinger 	 * If the Time2Retain handler has expired, the session is already gone.
218e48354ceSNicholas Bellinger 	 */
219e48354ceSNicholas Bellinger 	if (!sess)
220e48354ceSNicholas Bellinger 		return 0;
221e48354ceSNicholas Bellinger 
222e48354ceSNicholas Bellinger 	pr_debug("%s iSCSI Session SID %u is still active for %s,"
223e48354ceSNicholas Bellinger 		" preforming session reinstatement.\n", (sessiontype) ?
224e48354ceSNicholas Bellinger 		"Discovery" : "Normal", sess->sid,
225e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
226e48354ceSNicholas Bellinger 
227e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
228e48354ceSNicholas Bellinger 	if (sess->session_state == TARG_SESS_STATE_FAILED) {
229e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
230e48354ceSNicholas Bellinger 		iscsit_dec_session_usage_count(sess);
23199367f01SNicholas Bellinger 		target_put_session(sess->se_sess);
23299367f01SNicholas Bellinger 		return 0;
233e48354ceSNicholas Bellinger 	}
234e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
235e48354ceSNicholas Bellinger 
236e48354ceSNicholas Bellinger 	iscsit_stop_session(sess, 1, 1);
237e48354ceSNicholas Bellinger 	iscsit_dec_session_usage_count(sess);
238e48354ceSNicholas Bellinger 
23999367f01SNicholas Bellinger 	target_put_session(sess->se_sess);
24099367f01SNicholas Bellinger 	return 0;
241e48354ceSNicholas Bellinger }
242e48354ceSNicholas Bellinger 
243e48354ceSNicholas Bellinger static void iscsi_login_set_conn_values(
244e48354ceSNicholas Bellinger 	struct iscsi_session *sess,
245e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
24650e5c87dSChristoph Hellwig 	__be16 cid)
247e48354ceSNicholas Bellinger {
248e48354ceSNicholas Bellinger 	conn->sess		= sess;
24950e5c87dSChristoph Hellwig 	conn->cid		= be16_to_cpu(cid);
250e48354ceSNicholas Bellinger 	/*
251e48354ceSNicholas Bellinger 	 * Generate a random Status sequence number (statsn) for the new
252e48354ceSNicholas Bellinger 	 * iSCSI connection.
253e48354ceSNicholas Bellinger 	 */
254e48354ceSNicholas Bellinger 	get_random_bytes(&conn->stat_sn, sizeof(u32));
255e48354ceSNicholas Bellinger 
256e48354ceSNicholas Bellinger 	mutex_lock(&auth_id_lock);
257e48354ceSNicholas Bellinger 	conn->auth_id		= iscsit_global->auth_id++;
258e48354ceSNicholas Bellinger 	mutex_unlock(&auth_id_lock);
259e48354ceSNicholas Bellinger }
260e48354ceSNicholas Bellinger 
261d2faaefbSVarun Prakash __printf(2, 3) int iscsi_change_param_sprintf(
26279d59d08SRoland Dreier 	struct iscsi_conn *conn,
26379d59d08SRoland Dreier 	const char *fmt, ...)
26479d59d08SRoland Dreier {
26579d59d08SRoland Dreier 	va_list args;
26679d59d08SRoland Dreier 	unsigned char buf[64];
26779d59d08SRoland Dreier 
26879d59d08SRoland Dreier 	memset(buf, 0, sizeof buf);
26979d59d08SRoland Dreier 
27079d59d08SRoland Dreier 	va_start(args, fmt);
27179d59d08SRoland Dreier 	vsnprintf(buf, sizeof buf, fmt, args);
27279d59d08SRoland Dreier 	va_end(args);
27379d59d08SRoland Dreier 
27479d59d08SRoland Dreier 	if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
27579d59d08SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
27679d59d08SRoland Dreier 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
27779d59d08SRoland Dreier 		return -1;
27879d59d08SRoland Dreier 	}
27979d59d08SRoland Dreier 
28079d59d08SRoland Dreier 	return 0;
28179d59d08SRoland Dreier }
282d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsi_change_param_sprintf);
28379d59d08SRoland Dreier 
284e48354ceSNicholas Bellinger /*
285e48354ceSNicholas Bellinger  *	This is the leading connection of a new session,
286e48354ceSNicholas Bellinger  *	or session reinstatement.
287e48354ceSNicholas Bellinger  */
288e48354ceSNicholas Bellinger static int iscsi_login_zero_tsih_s1(
289e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
290e48354ceSNicholas Bellinger 	unsigned char *buf)
291e48354ceSNicholas Bellinger {
292e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL;
293e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
29413b5533aSBenjamin Wang 	int ret;
295e48354ceSNicholas Bellinger 
296e48354ceSNicholas Bellinger 	sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
297e48354ceSNicholas Bellinger 	if (!sess) {
298e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
299e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
300e48354ceSNicholas Bellinger 		pr_err("Could not allocate memory for session\n");
3010957627aSNicholas Bellinger 		return -ENOMEM;
302e48354ceSNicholas Bellinger 	}
303e48354ceSNicholas Bellinger 
304e48354ceSNicholas Bellinger 	iscsi_login_set_conn_values(sess, conn, pdu->cid);
305e48354ceSNicholas Bellinger 	sess->init_task_tag	= pdu->itt;
3068359cf43SJörn Engel 	memcpy(&sess->isid, pdu->isid, 6);
30750e5c87dSChristoph Hellwig 	sess->exp_cmd_sn	= be32_to_cpu(pdu->cmdsn);
308e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->sess_conn_list);
309e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
310e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->cr_active_list);
311e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->cr_inactive_list);
312e48354ceSNicholas Bellinger 	init_completion(&sess->async_msg_comp);
313e48354ceSNicholas Bellinger 	init_completion(&sess->reinstatement_comp);
314e48354ceSNicholas Bellinger 	init_completion(&sess->session_wait_comp);
315e48354ceSNicholas Bellinger 	init_completion(&sess->session_waiting_on_uc_comp);
316e48354ceSNicholas Bellinger 	mutex_init(&sess->cmdsn_mutex);
317e48354ceSNicholas Bellinger 	spin_lock_init(&sess->conn_lock);
318e48354ceSNicholas Bellinger 	spin_lock_init(&sess->cr_a_lock);
319e48354ceSNicholas Bellinger 	spin_lock_init(&sess->cr_i_lock);
320e48354ceSNicholas Bellinger 	spin_lock_init(&sess->session_usage_lock);
321e48354ceSNicholas Bellinger 	spin_lock_init(&sess->ttt_lock);
322e48354ceSNicholas Bellinger 
323c9365bd0STejun Heo 	idr_preload(GFP_KERNEL);
324998866b0SRoland Dreier 	spin_lock_bh(&sess_idr_lock);
325c9365bd0STejun Heo 	ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
326c9365bd0STejun Heo 	if (ret >= 0)
327c9365bd0STejun Heo 		sess->session_index = ret;
328998866b0SRoland Dreier 	spin_unlock_bh(&sess_idr_lock);
329c9365bd0STejun Heo 	idr_preload_end();
330e48354ceSNicholas Bellinger 
33113b5533aSBenjamin Wang 	if (ret < 0) {
332c9365bd0STejun Heo 		pr_err("idr_alloc() for sess_idr failed\n");
33313b5533aSBenjamin Wang 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
33413b5533aSBenjamin Wang 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
33513b5533aSBenjamin Wang 		kfree(sess);
33613b5533aSBenjamin Wang 		return -ENOMEM;
33713b5533aSBenjamin Wang 	}
33813b5533aSBenjamin Wang 
339e48354ceSNicholas Bellinger 	sess->creation_time = get_jiffies_64();
340e48354ceSNicholas Bellinger 	/*
341e48354ceSNicholas Bellinger 	 * The FFP CmdSN window values will be allocated from the TPG's
342e48354ceSNicholas Bellinger 	 * Initiator Node's ACL once the login has been successfully completed.
343e48354ceSNicholas Bellinger 	 */
344109e2381SRoland Dreier 	atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn));
345e48354ceSNicholas Bellinger 
346e48354ceSNicholas Bellinger 	sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
347e48354ceSNicholas Bellinger 	if (!sess->sess_ops) {
348e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
349e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
350e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for"
351e48354ceSNicholas Bellinger 				" struct iscsi_sess_ops.\n");
3520957627aSNicholas Bellinger 		kfree(sess);
3530957627aSNicholas Bellinger 		return -ENOMEM;
354e48354ceSNicholas Bellinger 	}
355e48354ceSNicholas Bellinger 
35623a548eeSSagi Grimberg 	sess->se_sess = transport_init_session(TARGET_PROT_NORMAL);
3570957627aSNicholas Bellinger 	if (IS_ERR(sess->se_sess)) {
358e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
359e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
360a928d28dSEvgenii Lepikhin 		kfree(sess->sess_ops);
3610957627aSNicholas Bellinger 		kfree(sess);
3620957627aSNicholas Bellinger 		return -ENOMEM;
363e48354ceSNicholas Bellinger 	}
364e48354ceSNicholas Bellinger 
365e48354ceSNicholas Bellinger 	return 0;
366e48354ceSNicholas Bellinger }
367e48354ceSNicholas Bellinger 
368e48354ceSNicholas Bellinger static int iscsi_login_zero_tsih_s2(
369e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
370e48354ceSNicholas Bellinger {
371e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na;
372e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
37303aa2070SNicholas Bellinger 	bool iser = false;
374e48354ceSNicholas Bellinger 
375e48354ceSNicholas Bellinger 	sess->tpg = conn->tpg;
376e48354ceSNicholas Bellinger 
377e48354ceSNicholas Bellinger 	/*
378e48354ceSNicholas Bellinger 	 * Assign a new TPG Session Handle.  Note this is protected with
379e48354ceSNicholas Bellinger 	 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
380e48354ceSNicholas Bellinger 	 */
38160bfcf8eSAndy Grover 	sess->tsih = ++sess->tpg->ntsih;
382e48354ceSNicholas Bellinger 	if (!sess->tsih)
38360bfcf8eSAndy Grover 		sess->tsih = ++sess->tpg->ntsih;
384e48354ceSNicholas Bellinger 
385e48354ceSNicholas Bellinger 	/*
386e48354ceSNicholas Bellinger 	 * Create the default params from user defined values..
387e48354ceSNicholas Bellinger 	 */
388e48354ceSNicholas Bellinger 	if (iscsi_copy_param_list(&conn->param_list,
38960bfcf8eSAndy Grover 				conn->tpg->param_list, 1) < 0) {
390e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
391e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
392e48354ceSNicholas Bellinger 		return -1;
393e48354ceSNicholas Bellinger 	}
394e48354ceSNicholas Bellinger 
39503aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
39603aa2070SNicholas Bellinger 		iser = true;
39703aa2070SNicholas Bellinger 
39803aa2070SNicholas Bellinger 	iscsi_set_keys_to_negotiate(conn->param_list, iser);
399e48354ceSNicholas Bellinger 
400e48354ceSNicholas Bellinger 	if (sess->sess_ops->SessionType)
401e48354ceSNicholas Bellinger 		return iscsi_set_keys_irrelevant_for_discovery(
402e48354ceSNicholas Bellinger 				conn->param_list);
403e48354ceSNicholas Bellinger 
404e48354ceSNicholas Bellinger 	na = iscsit_tpg_get_node_attrib(sess);
405e48354ceSNicholas Bellinger 
406e48354ceSNicholas Bellinger 	/*
407e48354ceSNicholas Bellinger 	 * Need to send TargetPortalGroupTag back in first login response
408e48354ceSNicholas Bellinger 	 * on any iSCSI connection where the Initiator provides TargetName.
409e48354ceSNicholas Bellinger 	 * See 5.3.1.  Login Phase Start
410e48354ceSNicholas Bellinger 	 *
411e48354ceSNicholas Bellinger 	 * In our case, we have already located the struct iscsi_tiqn at this point.
412e48354ceSNicholas Bellinger 	 */
41379d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
414e48354ceSNicholas Bellinger 		return -1;
415e48354ceSNicholas Bellinger 
416e48354ceSNicholas Bellinger 	/*
417e48354ceSNicholas Bellinger 	 * Workaround for Initiators that have broken connection recovery logic.
418e48354ceSNicholas Bellinger 	 *
419e48354ceSNicholas Bellinger 	 * "We would really like to get rid of this." Linux-iSCSI.org team
420e48354ceSNicholas Bellinger 	 */
42179d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
422e48354ceSNicholas Bellinger 		return -1;
423e48354ceSNicholas Bellinger 
42403aa2070SNicholas Bellinger 	/*
42503aa2070SNicholas Bellinger 	 * Set RDMAExtensions=Yes by default for iSER enabled network portals
42603aa2070SNicholas Bellinger 	 */
42703aa2070SNicholas Bellinger 	if (iser) {
42803aa2070SNicholas Bellinger 		struct iscsi_param *param;
42903aa2070SNicholas Bellinger 		unsigned long mrdsl, off;
43003aa2070SNicholas Bellinger 		int rc;
43103aa2070SNicholas Bellinger 
43279d59d08SRoland Dreier 		if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
43303aa2070SNicholas Bellinger 			return -1;
43479d59d08SRoland Dreier 
43503aa2070SNicholas Bellinger 		/*
43603aa2070SNicholas Bellinger 		 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
43703aa2070SNicholas Bellinger 		 * Immediate Data + Unsolicitied Data-OUT if necessary..
43803aa2070SNicholas Bellinger 		 */
43903aa2070SNicholas Bellinger 		param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
44003aa2070SNicholas Bellinger 						  conn->param_list);
44103aa2070SNicholas Bellinger 		if (!param) {
44203aa2070SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
44303aa2070SNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
44403aa2070SNicholas Bellinger 			return -1;
44503aa2070SNicholas Bellinger 		}
44657103d7fSJingoo Han 		rc = kstrtoul(param->value, 0, &mrdsl);
44703aa2070SNicholas Bellinger 		if (rc < 0) {
44803aa2070SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
44903aa2070SNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
45003aa2070SNicholas Bellinger 			return -1;
45103aa2070SNicholas Bellinger 		}
45203aa2070SNicholas Bellinger 		off = mrdsl % PAGE_SIZE;
45303aa2070SNicholas Bellinger 		if (!off)
45452d0aa79SNicholas Bellinger 			goto check_prot;
45503aa2070SNicholas Bellinger 
45603aa2070SNicholas Bellinger 		if (mrdsl < PAGE_SIZE)
45703aa2070SNicholas Bellinger 			mrdsl = PAGE_SIZE;
45803aa2070SNicholas Bellinger 		else
45903aa2070SNicholas Bellinger 			mrdsl -= off;
46003aa2070SNicholas Bellinger 
46103aa2070SNicholas Bellinger 		pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
46203aa2070SNicholas Bellinger 			" to PAGE_SIZE\n", mrdsl);
46303aa2070SNicholas Bellinger 
46479d59d08SRoland Dreier 		if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
46503aa2070SNicholas Bellinger 			return -1;
46652d0aa79SNicholas Bellinger 		/*
46752d0aa79SNicholas Bellinger 		 * ISER currently requires that ImmediateData + Unsolicited
46852d0aa79SNicholas Bellinger 		 * Data be disabled when protection / signature MRs are enabled.
46952d0aa79SNicholas Bellinger 		 */
47052d0aa79SNicholas Bellinger check_prot:
47152d0aa79SNicholas Bellinger 		if (sess->se_sess->sup_prot_ops &
47252d0aa79SNicholas Bellinger 		   (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
47352d0aa79SNicholas Bellinger 		    TARGET_PROT_DOUT_INSERT)) {
47452d0aa79SNicholas Bellinger 
47579d59d08SRoland Dreier 			if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
47652d0aa79SNicholas Bellinger 				return -1;
47752d0aa79SNicholas Bellinger 
47879d59d08SRoland Dreier 			if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
47952d0aa79SNicholas Bellinger 				return -1;
48079d59d08SRoland Dreier 
48152d0aa79SNicholas Bellinger 			pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
48252d0aa79SNicholas Bellinger 				 " T10-PI enabled ISER session\n");
48352d0aa79SNicholas Bellinger 		}
48403aa2070SNicholas Bellinger 	}
485e48354ceSNicholas Bellinger 
486e48354ceSNicholas Bellinger 	return 0;
487e48354ceSNicholas Bellinger }
488e48354ceSNicholas Bellinger 
489e48354ceSNicholas Bellinger static int iscsi_login_non_zero_tsih_s1(
490e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
491e48354ceSNicholas Bellinger 	unsigned char *buf)
492e48354ceSNicholas Bellinger {
493e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
494e48354ceSNicholas Bellinger 
495e48354ceSNicholas Bellinger 	iscsi_login_set_conn_values(NULL, conn, pdu->cid);
496e48354ceSNicholas Bellinger 	return 0;
497e48354ceSNicholas Bellinger }
498e48354ceSNicholas Bellinger 
499e48354ceSNicholas Bellinger /*
500e48354ceSNicholas Bellinger  *	Add a new connection to an existing session.
501e48354ceSNicholas Bellinger  */
502e48354ceSNicholas Bellinger static int iscsi_login_non_zero_tsih_s2(
503e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
504e48354ceSNicholas Bellinger 	unsigned char *buf)
505e48354ceSNicholas Bellinger {
506e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
507e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL, *sess_p = NULL;
508e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
509e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
510e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
51103aa2070SNicholas Bellinger 	bool iser = false;
512e48354ceSNicholas Bellinger 
513e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
514e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
515e48354ceSNicholas Bellinger 			sess_list) {
516e48354ceSNicholas Bellinger 
517e48354ceSNicholas Bellinger 		sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
518e48354ceSNicholas Bellinger 		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
519e48354ceSNicholas Bellinger 		    atomic_read(&sess_p->session_logout) ||
520e48354ceSNicholas Bellinger 		   (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
521e48354ceSNicholas Bellinger 			continue;
5228359cf43SJörn Engel 		if (!memcmp(sess_p->isid, pdu->isid, 6) &&
52350e5c87dSChristoph Hellwig 		     (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
524e48354ceSNicholas Bellinger 			iscsit_inc_session_usage_count(sess_p);
525e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess_p);
526e48354ceSNicholas Bellinger 			sess = sess_p;
527e48354ceSNicholas Bellinger 			break;
528e48354ceSNicholas Bellinger 		}
529e48354ceSNicholas Bellinger 	}
530e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
531e48354ceSNicholas Bellinger 
532e48354ceSNicholas Bellinger 	/*
533e48354ceSNicholas Bellinger 	 * If the Time2Retain handler has expired, the session is already gone.
534e48354ceSNicholas Bellinger 	 */
535e48354ceSNicholas Bellinger 	if (!sess) {
536e48354ceSNicholas Bellinger 		pr_err("Initiator attempting to add a connection to"
537e48354ceSNicholas Bellinger 			" a non-existent session, rejecting iSCSI Login.\n");
538e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
539e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_SESSION);
540e48354ceSNicholas Bellinger 		return -1;
541e48354ceSNicholas Bellinger 	}
542e48354ceSNicholas Bellinger 
543e48354ceSNicholas Bellinger 	/*
544e48354ceSNicholas Bellinger 	 * Stop the Time2Retain timer if this is a failed session, we restart
545e48354ceSNicholas Bellinger 	 * the timer if the login is not successful.
546e48354ceSNicholas Bellinger 	 */
547e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
548e48354ceSNicholas Bellinger 	if (sess->session_state == TARG_SESS_STATE_FAILED)
549e48354ceSNicholas Bellinger 		atomic_set(&sess->session_continuation, 1);
550e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
551e48354ceSNicholas Bellinger 
552e48354ceSNicholas Bellinger 	iscsi_login_set_conn_values(sess, conn, pdu->cid);
553e48354ceSNicholas Bellinger 
554e48354ceSNicholas Bellinger 	if (iscsi_copy_param_list(&conn->param_list,
55560bfcf8eSAndy Grover 			conn->tpg->param_list, 0) < 0) {
556e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
557e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
558e48354ceSNicholas Bellinger 		return -1;
559e48354ceSNicholas Bellinger 	}
560e48354ceSNicholas Bellinger 
56103aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
56203aa2070SNicholas Bellinger 		iser = true;
56303aa2070SNicholas Bellinger 
56403aa2070SNicholas Bellinger 	iscsi_set_keys_to_negotiate(conn->param_list, iser);
565e48354ceSNicholas Bellinger 	/*
566e48354ceSNicholas Bellinger 	 * Need to send TargetPortalGroupTag back in first login response
567e48354ceSNicholas Bellinger 	 * on any iSCSI connection where the Initiator provides TargetName.
568e48354ceSNicholas Bellinger 	 * See 5.3.1.  Login Phase Start
569e48354ceSNicholas Bellinger 	 *
570e48354ceSNicholas Bellinger 	 * In our case, we have already located the struct iscsi_tiqn at this point.
571e48354ceSNicholas Bellinger 	 */
57279d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
573e48354ceSNicholas Bellinger 		return -1;
574e48354ceSNicholas Bellinger 
575c04a6091SChristophe Vu-Brugier 	return 0;
576e48354ceSNicholas Bellinger }
577e48354ceSNicholas Bellinger 
578e48354ceSNicholas Bellinger int iscsi_login_post_auth_non_zero_tsih(
579e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
580e48354ceSNicholas Bellinger 	u16 cid,
581e48354ceSNicholas Bellinger 	u32 exp_statsn)
582e48354ceSNicholas Bellinger {
583e48354ceSNicholas Bellinger 	struct iscsi_conn *conn_ptr = NULL;
584e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr = NULL;
585e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
586e48354ceSNicholas Bellinger 
587e48354ceSNicholas Bellinger 	/*
588e48354ceSNicholas Bellinger 	 * By following item 5 in the login table,  if we have found
589e48354ceSNicholas Bellinger 	 * an existing ISID and a valid/existing TSIH and an existing
590e48354ceSNicholas Bellinger 	 * CID we do connection reinstatement.  Currently we dont not
591e48354ceSNicholas Bellinger 	 * support it so we send back an non-zero status class to the
592e48354ceSNicholas Bellinger 	 * initiator and release the new connection.
593e48354ceSNicholas Bellinger 	 */
594e48354ceSNicholas Bellinger 	conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
595ee1b1b9cSAndy Grover 	if (conn_ptr) {
596e48354ceSNicholas Bellinger 		pr_err("Connection exists with CID %hu for %s,"
597e48354ceSNicholas Bellinger 			" performing connection reinstatement.\n",
598e48354ceSNicholas Bellinger 			conn_ptr->cid, sess->sess_ops->InitiatorName);
599e48354ceSNicholas Bellinger 
600e48354ceSNicholas Bellinger 		iscsit_connection_reinstatement_rcfr(conn_ptr);
601e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn_ptr);
602e48354ceSNicholas Bellinger 	}
603e48354ceSNicholas Bellinger 
604e48354ceSNicholas Bellinger 	/*
605e48354ceSNicholas Bellinger 	 * Check for any connection recovery entires containing CID.
606e48354ceSNicholas Bellinger 	 * We use the original ExpStatSN sent in the first login request
607e48354ceSNicholas Bellinger 	 * to acknowledge commands for the failed connection.
608e48354ceSNicholas Bellinger 	 *
609e48354ceSNicholas Bellinger 	 * Also note that an explict logout may have already been sent,
610e48354ceSNicholas Bellinger 	 * but the response may not be sent due to additional connection
611e48354ceSNicholas Bellinger 	 * loss.
612e48354ceSNicholas Bellinger 	 */
613e48354ceSNicholas Bellinger 	if (sess->sess_ops->ErrorRecoveryLevel == 2) {
614e48354ceSNicholas Bellinger 		cr = iscsit_get_inactive_connection_recovery_entry(
615e48354ceSNicholas Bellinger 				sess, cid);
616ee1b1b9cSAndy Grover 		if (cr) {
617e48354ceSNicholas Bellinger 			pr_debug("Performing implicit logout"
618e48354ceSNicholas Bellinger 				" for connection recovery on CID: %hu\n",
619e48354ceSNicholas Bellinger 					conn->cid);
620e48354ceSNicholas Bellinger 			iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
621e48354ceSNicholas Bellinger 		}
622e48354ceSNicholas Bellinger 	}
623e48354ceSNicholas Bellinger 
624e48354ceSNicholas Bellinger 	/*
625e48354ceSNicholas Bellinger 	 * Else we follow item 4 from the login table in that we have
626e48354ceSNicholas Bellinger 	 * found an existing ISID and a valid/existing TSIH and a new
627e48354ceSNicholas Bellinger 	 * CID we go ahead and continue to add a new connection to the
628e48354ceSNicholas Bellinger 	 * session.
629e48354ceSNicholas Bellinger 	 */
630e48354ceSNicholas Bellinger 	pr_debug("Adding CID %hu to existing session for %s.\n",
631e48354ceSNicholas Bellinger 			cid, sess->sess_ops->InitiatorName);
632e48354ceSNicholas Bellinger 
633e48354ceSNicholas Bellinger 	if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
634e48354ceSNicholas Bellinger 		pr_err("Adding additional connection to this session"
635e48354ceSNicholas Bellinger 			" would exceed MaxConnections %d, login failed.\n",
636e48354ceSNicholas Bellinger 				sess->sess_ops->MaxConnections);
637e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
638e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_ISID_ERROR);
639e48354ceSNicholas Bellinger 		return -1;
640e48354ceSNicholas Bellinger 	}
641e48354ceSNicholas Bellinger 
642e48354ceSNicholas Bellinger 	return 0;
643e48354ceSNicholas Bellinger }
644e48354ceSNicholas Bellinger 
645e48354ceSNicholas Bellinger static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
646e48354ceSNicholas Bellinger {
647e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
64803aa2070SNicholas Bellinger 	/*
64903aa2070SNicholas Bellinger 	 * FIXME: Unsolicitied NopIN support for ISER
65003aa2070SNicholas Bellinger 	 */
65103aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
65203aa2070SNicholas Bellinger 		return;
653e48354ceSNicholas Bellinger 
654e48354ceSNicholas Bellinger 	if (!sess->sess_ops->SessionType)
655e48354ceSNicholas Bellinger 		iscsit_start_nopin_timer(conn);
656e48354ceSNicholas Bellinger }
657e48354ceSNicholas Bellinger 
658e5419865SNicholas Bellinger int iscsit_start_kthreads(struct iscsi_conn *conn)
65988dcd2daSNicholas Bellinger {
66088dcd2daSNicholas Bellinger 	int ret = 0;
66188dcd2daSNicholas Bellinger 
66288dcd2daSNicholas Bellinger 	spin_lock(&iscsit_global->ts_bitmap_lock);
66388dcd2daSNicholas Bellinger 	conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
66488dcd2daSNicholas Bellinger 					ISCSIT_BITMAP_BITS, get_order(1));
66588dcd2daSNicholas Bellinger 	spin_unlock(&iscsit_global->ts_bitmap_lock);
66688dcd2daSNicholas Bellinger 
66788dcd2daSNicholas Bellinger 	if (conn->bitmap_id < 0) {
66888dcd2daSNicholas Bellinger 		pr_err("bitmap_find_free_region() failed for"
66988dcd2daSNicholas Bellinger 		       " iscsit_start_kthreads()\n");
67088dcd2daSNicholas Bellinger 		return -ENOMEM;
67188dcd2daSNicholas Bellinger 	}
67288dcd2daSNicholas Bellinger 
67388dcd2daSNicholas Bellinger 	conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
67488dcd2daSNicholas Bellinger 				      "%s", ISCSI_TX_THREAD_NAME);
67588dcd2daSNicholas Bellinger 	if (IS_ERR(conn->tx_thread)) {
67688dcd2daSNicholas Bellinger 		pr_err("Unable to start iscsi_target_tx_thread\n");
67788dcd2daSNicholas Bellinger 		ret = PTR_ERR(conn->tx_thread);
67888dcd2daSNicholas Bellinger 		goto out_bitmap;
67988dcd2daSNicholas Bellinger 	}
68088dcd2daSNicholas Bellinger 	conn->tx_thread_active = true;
68188dcd2daSNicholas Bellinger 
68288dcd2daSNicholas Bellinger 	conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
68388dcd2daSNicholas Bellinger 				      "%s", ISCSI_RX_THREAD_NAME);
68488dcd2daSNicholas Bellinger 	if (IS_ERR(conn->rx_thread)) {
68588dcd2daSNicholas Bellinger 		pr_err("Unable to start iscsi_target_rx_thread\n");
68688dcd2daSNicholas Bellinger 		ret = PTR_ERR(conn->rx_thread);
68788dcd2daSNicholas Bellinger 		goto out_tx;
68888dcd2daSNicholas Bellinger 	}
68988dcd2daSNicholas Bellinger 	conn->rx_thread_active = true;
69088dcd2daSNicholas Bellinger 
69188dcd2daSNicholas Bellinger 	return 0;
69288dcd2daSNicholas Bellinger out_tx:
693e5419865SNicholas Bellinger 	send_sig(SIGINT, conn->tx_thread, 1);
69488dcd2daSNicholas Bellinger 	kthread_stop(conn->tx_thread);
69588dcd2daSNicholas Bellinger 	conn->tx_thread_active = false;
69688dcd2daSNicholas Bellinger out_bitmap:
69788dcd2daSNicholas Bellinger 	spin_lock(&iscsit_global->ts_bitmap_lock);
69888dcd2daSNicholas Bellinger 	bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
69988dcd2daSNicholas Bellinger 			      get_order(1));
70088dcd2daSNicholas Bellinger 	spin_unlock(&iscsit_global->ts_bitmap_lock);
70188dcd2daSNicholas Bellinger 	return ret;
70288dcd2daSNicholas Bellinger }
70388dcd2daSNicholas Bellinger 
704e5419865SNicholas Bellinger void iscsi_post_login_handler(
705e48354ceSNicholas Bellinger 	struct iscsi_np *np,
706e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
707e48354ceSNicholas Bellinger 	u8 zero_tsih)
708e48354ceSNicholas Bellinger {
709e48354ceSNicholas Bellinger 	int stop_timer = 0;
710e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
711e48354ceSNicholas Bellinger 	struct se_session *se_sess = sess->se_sess;
71260bfcf8eSAndy Grover 	struct iscsi_portal_group *tpg = sess->tpg;
713e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
714e48354ceSNicholas Bellinger 
715e48354ceSNicholas Bellinger 	iscsit_inc_conn_usage_count(conn);
716e48354ceSNicholas Bellinger 
717e48354ceSNicholas Bellinger 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
718e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_ACCEPT);
719e48354ceSNicholas Bellinger 
720e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
721e48354ceSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
722e48354ceSNicholas Bellinger 
723e48354ceSNicholas Bellinger 	iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
724e48354ceSNicholas Bellinger 	/*
725e48354ceSNicholas Bellinger 	 * SCSI Initiator -> SCSI Target Port Mapping
726e48354ceSNicholas Bellinger 	 */
727e48354ceSNicholas Bellinger 	if (!zero_tsih) {
728e48354ceSNicholas Bellinger 		iscsi_set_session_parameters(sess->sess_ops,
729e48354ceSNicholas Bellinger 				conn->param_list, 0);
730e48354ceSNicholas Bellinger 		iscsi_release_param_list(conn->param_list);
731e48354ceSNicholas Bellinger 		conn->param_list = NULL;
732e48354ceSNicholas Bellinger 
733e48354ceSNicholas Bellinger 		spin_lock_bh(&sess->conn_lock);
734e48354ceSNicholas Bellinger 		atomic_set(&sess->session_continuation, 0);
735e48354ceSNicholas Bellinger 		if (sess->session_state == TARG_SESS_STATE_FAILED) {
736e48354ceSNicholas Bellinger 			pr_debug("Moving to"
737e48354ceSNicholas Bellinger 					" TARG_SESS_STATE_LOGGED_IN.\n");
738e48354ceSNicholas Bellinger 			sess->session_state = TARG_SESS_STATE_LOGGED_IN;
739e48354ceSNicholas Bellinger 			stop_timer = 1;
740e48354ceSNicholas Bellinger 		}
741e48354ceSNicholas Bellinger 
742dc58f760SAndy Grover 		pr_debug("iSCSI Login successful on CID: %hu from %pISpc to"
743dc58f760SAndy Grover 			" %pISpc,%hu\n", conn->cid, &conn->login_sockaddr,
74469d75574SAndy Grover 			&conn->local_sockaddr, tpg->tpgt);
745e48354ceSNicholas Bellinger 
746e48354ceSNicholas Bellinger 		list_add_tail(&conn->conn_list, &sess->sess_conn_list);
747e48354ceSNicholas Bellinger 		atomic_inc(&sess->nconn);
748e48354ceSNicholas Bellinger 		pr_debug("Incremented iSCSI Connection count to %hu"
749e48354ceSNicholas Bellinger 			" from node: %s\n", atomic_read(&sess->nconn),
750e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
751e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
752e48354ceSNicholas Bellinger 
75388dcd2daSNicholas Bellinger 		iscsi_post_login_start_timers(conn);
754e48354ceSNicholas Bellinger 		/*
755e48354ceSNicholas Bellinger 		 * Determine CPU mask to ensure connection's RX and TX kthreads
756e48354ceSNicholas Bellinger 		 * are scheduled on the same CPU.
757e48354ceSNicholas Bellinger 		 */
758e48354ceSNicholas Bellinger 		iscsit_thread_get_cpumask(conn);
759e48354ceSNicholas Bellinger 		conn->conn_rx_reset_cpumask = 1;
760e48354ceSNicholas Bellinger 		conn->conn_tx_reset_cpumask = 1;
761e5419865SNicholas Bellinger 		/*
762e5419865SNicholas Bellinger 		 * Wakeup the sleeping iscsi_target_rx_thread() now that
763e5419865SNicholas Bellinger 		 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
764e5419865SNicholas Bellinger 		 */
765e5419865SNicholas Bellinger 		complete(&conn->rx_login_comp);
766e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn);
767e5419865SNicholas Bellinger 
768e48354ceSNicholas Bellinger 		if (stop_timer) {
769e48354ceSNicholas Bellinger 			spin_lock_bh(&se_tpg->session_lock);
770e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess);
771e48354ceSNicholas Bellinger 			spin_unlock_bh(&se_tpg->session_lock);
772e48354ceSNicholas Bellinger 		}
773e48354ceSNicholas Bellinger 		iscsit_dec_session_usage_count(sess);
774e5419865SNicholas Bellinger 		return;
775e48354ceSNicholas Bellinger 	}
776e48354ceSNicholas Bellinger 
777e48354ceSNicholas Bellinger 	iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
778e48354ceSNicholas Bellinger 	iscsi_release_param_list(conn->param_list);
779e48354ceSNicholas Bellinger 	conn->param_list = NULL;
780e48354ceSNicholas Bellinger 
781e48354ceSNicholas Bellinger 	iscsit_determine_maxcmdsn(sess);
782e48354ceSNicholas Bellinger 
783e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
784e48354ceSNicholas Bellinger 	__transport_register_session(&sess->tpg->tpg_se_tpg,
7858359cf43SJörn Engel 			se_sess->se_node_acl, se_sess, sess);
786e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
787e48354ceSNicholas Bellinger 	sess->session_state = TARG_SESS_STATE_LOGGED_IN;
788e48354ceSNicholas Bellinger 
789dc58f760SAndy Grover 	pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n",
790dc58f760SAndy Grover 		conn->cid, &conn->login_sockaddr, &conn->local_sockaddr,
7912f9bc894SNicholas Bellinger 		tpg->tpgt);
792e48354ceSNicholas Bellinger 
793e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
794e48354ceSNicholas Bellinger 	list_add_tail(&conn->conn_list, &sess->sess_conn_list);
795e48354ceSNicholas Bellinger 	atomic_inc(&sess->nconn);
796e48354ceSNicholas Bellinger 	pr_debug("Incremented iSCSI Connection count to %hu from node:"
797e48354ceSNicholas Bellinger 		" %s\n", atomic_read(&sess->nconn),
798e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
799e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
800e48354ceSNicholas Bellinger 
801e48354ceSNicholas Bellinger 	sess->sid = tpg->sid++;
802e48354ceSNicholas Bellinger 	if (!sess->sid)
803e48354ceSNicholas Bellinger 		sess->sid = tpg->sid++;
804e48354ceSNicholas Bellinger 	pr_debug("Established iSCSI session from node: %s\n",
805e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
806e48354ceSNicholas Bellinger 
807e48354ceSNicholas Bellinger 	tpg->nsessions++;
808e48354ceSNicholas Bellinger 	if (tpg->tpg_tiqn)
809e48354ceSNicholas Bellinger 		tpg->tpg_tiqn->tiqn_nsessions++;
810e48354ceSNicholas Bellinger 
811e48354ceSNicholas Bellinger 	pr_debug("Incremented number of active iSCSI sessions to %u on"
812e48354ceSNicholas Bellinger 		" iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
813e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
814e48354ceSNicholas Bellinger 
815e48354ceSNicholas Bellinger 	iscsi_post_login_start_timers(conn);
816e48354ceSNicholas Bellinger 	/*
817e48354ceSNicholas Bellinger 	 * Determine CPU mask to ensure connection's RX and TX kthreads
818e48354ceSNicholas Bellinger 	 * are scheduled on the same CPU.
819e48354ceSNicholas Bellinger 	 */
820e48354ceSNicholas Bellinger 	iscsit_thread_get_cpumask(conn);
821e48354ceSNicholas Bellinger 	conn->conn_rx_reset_cpumask = 1;
822e48354ceSNicholas Bellinger 	conn->conn_tx_reset_cpumask = 1;
823e5419865SNicholas Bellinger 	/*
824e5419865SNicholas Bellinger 	 * Wakeup the sleeping iscsi_target_rx_thread() now that
825e5419865SNicholas Bellinger 	 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
826e5419865SNicholas Bellinger 	 */
827e5419865SNicholas Bellinger 	complete(&conn->rx_login_comp);
828e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
829e48354ceSNicholas Bellinger }
830e48354ceSNicholas Bellinger 
831e48354ceSNicholas Bellinger static void iscsi_handle_login_thread_timeout(unsigned long data)
832e48354ceSNicholas Bellinger {
833e48354ceSNicholas Bellinger 	struct iscsi_np *np = (struct iscsi_np *) data;
834e48354ceSNicholas Bellinger 
835e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
83669d75574SAndy Grover 	pr_err("iSCSI Login timeout on Network Portal %pISpc\n",
83769d75574SAndy Grover 			&np->np_sockaddr);
838e48354ceSNicholas Bellinger 
839e48354ceSNicholas Bellinger 	if (np->np_login_timer_flags & ISCSI_TF_STOP) {
840e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
841e48354ceSNicholas Bellinger 		return;
842e48354ceSNicholas Bellinger 	}
843e48354ceSNicholas Bellinger 
844e48354ceSNicholas Bellinger 	if (np->np_thread)
845e48354ceSNicholas Bellinger 		send_sig(SIGINT, np->np_thread, 1);
846e48354ceSNicholas Bellinger 
847e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
848e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
849e48354ceSNicholas Bellinger }
850e48354ceSNicholas Bellinger 
851e48354ceSNicholas Bellinger static void iscsi_start_login_thread_timer(struct iscsi_np *np)
852e48354ceSNicholas Bellinger {
853e48354ceSNicholas Bellinger 	/*
854e48354ceSNicholas Bellinger 	 * This used the TA_LOGIN_TIMEOUT constant because at this
855e48354ceSNicholas Bellinger 	 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
856e48354ceSNicholas Bellinger 	 */
857e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
858e48354ceSNicholas Bellinger 	init_timer(&np->np_login_timer);
859e48354ceSNicholas Bellinger 	np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
860e48354ceSNicholas Bellinger 	np->np_login_timer.data = (unsigned long)np;
861e48354ceSNicholas Bellinger 	np->np_login_timer.function = iscsi_handle_login_thread_timeout;
862e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_STOP;
863e48354ceSNicholas Bellinger 	np->np_login_timer_flags |= ISCSI_TF_RUNNING;
864e48354ceSNicholas Bellinger 	add_timer(&np->np_login_timer);
865e48354ceSNicholas Bellinger 
866e48354ceSNicholas Bellinger 	pr_debug("Added timeout timer to iSCSI login request for"
867e48354ceSNicholas Bellinger 			" %u seconds.\n", TA_LOGIN_TIMEOUT);
868e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
869e48354ceSNicholas Bellinger }
870e48354ceSNicholas Bellinger 
871e48354ceSNicholas Bellinger static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
872e48354ceSNicholas Bellinger {
873e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
874e48354ceSNicholas Bellinger 	if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
875e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
876e48354ceSNicholas Bellinger 		return;
877e48354ceSNicholas Bellinger 	}
878e48354ceSNicholas Bellinger 	np->np_login_timer_flags |= ISCSI_TF_STOP;
879e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
880e48354ceSNicholas Bellinger 
881e48354ceSNicholas Bellinger 	del_timer_sync(&np->np_login_timer);
882e48354ceSNicholas Bellinger 
883e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
884e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
885e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
886e48354ceSNicholas Bellinger }
887e48354ceSNicholas Bellinger 
888baa4d64bSNicholas Bellinger int iscsit_setup_np(
889e48354ceSNicholas Bellinger 	struct iscsi_np *np,
89013a3cf08SAndy Grover 	struct sockaddr_storage *sockaddr)
891e48354ceSNicholas Bellinger {
892baa4d64bSNicholas Bellinger 	struct socket *sock = NULL;
893837f6452SNicholas Bellinger 	int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
894e48354ceSNicholas Bellinger 
895e48354ceSNicholas Bellinger 	switch (np->np_network_transport) {
896e48354ceSNicholas Bellinger 	case ISCSI_TCP:
897e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_TCP;
898e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_STREAM;
899e48354ceSNicholas Bellinger 		break;
900e48354ceSNicholas Bellinger 	case ISCSI_SCTP_TCP:
901e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_SCTP;
902e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_STREAM;
903e48354ceSNicholas Bellinger 		break;
904e48354ceSNicholas Bellinger 	case ISCSI_SCTP_UDP:
905e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_SCTP;
906e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_SEQPACKET;
907e48354ceSNicholas Bellinger 		break;
908e48354ceSNicholas Bellinger 	default:
909e48354ceSNicholas Bellinger 		pr_err("Unsupported network_transport: %d\n",
910e48354ceSNicholas Bellinger 				np->np_network_transport);
911e48354ceSNicholas Bellinger 		return -EINVAL;
912e48354ceSNicholas Bellinger 	}
913e48354ceSNicholas Bellinger 
914baa4d64bSNicholas Bellinger 	np->np_ip_proto = IPPROTO_TCP;
915baa4d64bSNicholas Bellinger 	np->np_sock_type = SOCK_STREAM;
916baa4d64bSNicholas Bellinger 
917e48354ceSNicholas Bellinger 	ret = sock_create(sockaddr->ss_family, np->np_sock_type,
918e48354ceSNicholas Bellinger 			np->np_ip_proto, &sock);
919e48354ceSNicholas Bellinger 	if (ret < 0) {
920e48354ceSNicholas Bellinger 		pr_err("sock_create() failed.\n");
921e48354ceSNicholas Bellinger 		return ret;
922e48354ceSNicholas Bellinger 	}
923e48354ceSNicholas Bellinger 	np->np_socket = sock;
924e48354ceSNicholas Bellinger 	/*
925e48354ceSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
926e48354ceSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
927e48354ceSNicholas Bellinger 	 */
9288359cf43SJörn Engel 	memcpy(&np->np_sockaddr, sockaddr,
92913a3cf08SAndy Grover 			sizeof(struct sockaddr_storage));
930e48354ceSNicholas Bellinger 
931e48354ceSNicholas Bellinger 	if (sockaddr->ss_family == AF_INET6)
932e48354ceSNicholas Bellinger 		len = sizeof(struct sockaddr_in6);
933e48354ceSNicholas Bellinger 	else
934e48354ceSNicholas Bellinger 		len = sizeof(struct sockaddr_in);
935e48354ceSNicholas Bellinger 	/*
936e48354ceSNicholas Bellinger 	 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
937e48354ceSNicholas Bellinger 	 */
9388359cf43SJörn Engel 	/* FIXME: Someone please explain why this is endian-safe */
939e48354ceSNicholas Bellinger 	opt = 1;
940e48354ceSNicholas Bellinger 	if (np->np_network_transport == ISCSI_TCP) {
941e48354ceSNicholas Bellinger 		ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
942e48354ceSNicholas Bellinger 				(char *)&opt, sizeof(opt));
943e48354ceSNicholas Bellinger 		if (ret < 0) {
944e48354ceSNicholas Bellinger 			pr_err("kernel_setsockopt() for TCP_NODELAY"
945e48354ceSNicholas Bellinger 				" failed: %d\n", ret);
946e48354ceSNicholas Bellinger 			goto fail;
947e48354ceSNicholas Bellinger 		}
948e48354ceSNicholas Bellinger 	}
949e48354ceSNicholas Bellinger 
9508359cf43SJörn Engel 	/* FIXME: Someone please explain why this is endian-safe */
951e48354ceSNicholas Bellinger 	ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
952e48354ceSNicholas Bellinger 			(char *)&opt, sizeof(opt));
953e48354ceSNicholas Bellinger 	if (ret < 0) {
954e48354ceSNicholas Bellinger 		pr_err("kernel_setsockopt() for SO_REUSEADDR"
955e48354ceSNicholas Bellinger 			" failed\n");
956e48354ceSNicholas Bellinger 		goto fail;
957e48354ceSNicholas Bellinger 	}
958e48354ceSNicholas Bellinger 
9599f9ef6d3SDax Kelson 	ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
9609f9ef6d3SDax Kelson 			(char *)&opt, sizeof(opt));
9619f9ef6d3SDax Kelson 	if (ret < 0) {
9629f9ef6d3SDax Kelson 		pr_err("kernel_setsockopt() for IP_FREEBIND"
9639f9ef6d3SDax Kelson 			" failed\n");
9649f9ef6d3SDax Kelson 		goto fail;
9659f9ef6d3SDax Kelson 	}
9669f9ef6d3SDax Kelson 
967e48354ceSNicholas Bellinger 	ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
968e48354ceSNicholas Bellinger 	if (ret < 0) {
969e48354ceSNicholas Bellinger 		pr_err("kernel_bind() failed: %d\n", ret);
970e48354ceSNicholas Bellinger 		goto fail;
971e48354ceSNicholas Bellinger 	}
972e48354ceSNicholas Bellinger 
973e48354ceSNicholas Bellinger 	ret = kernel_listen(sock, backlog);
974e48354ceSNicholas Bellinger 	if (ret != 0) {
975e48354ceSNicholas Bellinger 		pr_err("kernel_listen() failed: %d\n", ret);
976e48354ceSNicholas Bellinger 		goto fail;
977e48354ceSNicholas Bellinger 	}
978e48354ceSNicholas Bellinger 
979e48354ceSNicholas Bellinger 	return 0;
980e48354ceSNicholas Bellinger fail:
981e48354ceSNicholas Bellinger 	np->np_socket = NULL;
982e48354ceSNicholas Bellinger 	sock_release(sock);
983e48354ceSNicholas Bellinger 	return ret;
984e48354ceSNicholas Bellinger }
985e48354ceSNicholas Bellinger 
986baa4d64bSNicholas Bellinger int iscsi_target_setup_login_socket(
987baa4d64bSNicholas Bellinger 	struct iscsi_np *np,
98813a3cf08SAndy Grover 	struct sockaddr_storage *sockaddr)
989baa4d64bSNicholas Bellinger {
990baa4d64bSNicholas Bellinger 	struct iscsit_transport *t;
991baa4d64bSNicholas Bellinger 	int rc;
992baa4d64bSNicholas Bellinger 
993baa4d64bSNicholas Bellinger 	t = iscsit_get_transport(np->np_network_transport);
994baa4d64bSNicholas Bellinger 	if (!t)
995baa4d64bSNicholas Bellinger 		return -EINVAL;
996baa4d64bSNicholas Bellinger 
997baa4d64bSNicholas Bellinger 	rc = t->iscsit_setup_np(np, sockaddr);
998baa4d64bSNicholas Bellinger 	if (rc < 0) {
999baa4d64bSNicholas Bellinger 		iscsit_put_transport(t);
1000baa4d64bSNicholas Bellinger 		return rc;
1001baa4d64bSNicholas Bellinger 	}
1002baa4d64bSNicholas Bellinger 
1003baa4d64bSNicholas Bellinger 	np->np_transport = t;
100414f4b54fSSagi Grimberg 	np->enabled = true;
1005baa4d64bSNicholas Bellinger 	return 0;
1006baa4d64bSNicholas Bellinger }
1007baa4d64bSNicholas Bellinger 
1008baa4d64bSNicholas Bellinger int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
1009baa4d64bSNicholas Bellinger {
1010baa4d64bSNicholas Bellinger 	struct socket *new_sock, *sock = np->np_socket;
1011baa4d64bSNicholas Bellinger 	struct sockaddr_in sock_in;
1012baa4d64bSNicholas Bellinger 	struct sockaddr_in6 sock_in6;
1013baa4d64bSNicholas Bellinger 	int rc, err;
1014baa4d64bSNicholas Bellinger 
1015baa4d64bSNicholas Bellinger 	rc = kernel_accept(sock, &new_sock, 0);
1016baa4d64bSNicholas Bellinger 	if (rc < 0)
1017baa4d64bSNicholas Bellinger 		return rc;
1018baa4d64bSNicholas Bellinger 
1019baa4d64bSNicholas Bellinger 	conn->sock = new_sock;
1020baa4d64bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
1021baa4d64bSNicholas Bellinger 
1022baa4d64bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
1023baa4d64bSNicholas Bellinger 		memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1024baa4d64bSNicholas Bellinger 
1025baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
1026baa4d64bSNicholas Bellinger 				(struct sockaddr *)&sock_in6, &err, 1);
1027baa4d64bSNicholas Bellinger 		if (!rc) {
1028dc58f760SAndy Grover 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
1029dc58f760SAndy Grover 				memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6));
1030dc58f760SAndy Grover 			} else {
1031dc58f760SAndy Grover 				/* Pretend to be an ipv4 socket */
1032dc58f760SAndy Grover 				sock_in.sin_family = AF_INET;
1033dc58f760SAndy Grover 				sock_in.sin_port = sock_in6.sin6_port;
1034dc58f760SAndy Grover 				memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1035dc58f760SAndy Grover 				memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1036dc58f760SAndy Grover 			}
1037baa4d64bSNicholas Bellinger 		}
1038baa4d64bSNicholas Bellinger 
1039baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
1040baa4d64bSNicholas Bellinger 				(struct sockaddr *)&sock_in6, &err, 0);
1041baa4d64bSNicholas Bellinger 		if (!rc) {
104269d75574SAndy Grover 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
104369d75574SAndy Grover 				memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
104469d75574SAndy Grover 			} else {
104569d75574SAndy Grover 				/* Pretend to be an ipv4 socket */
104669d75574SAndy Grover 				sock_in.sin_family = AF_INET;
104769d75574SAndy Grover 				sock_in.sin_port = sock_in6.sin6_port;
104869d75574SAndy Grover 				memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
104969d75574SAndy Grover 				memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
105069d75574SAndy Grover 			}
1051baa4d64bSNicholas Bellinger 		}
1052baa4d64bSNicholas Bellinger 	} else {
1053baa4d64bSNicholas Bellinger 		memset(&sock_in, 0, sizeof(struct sockaddr_in));
1054baa4d64bSNicholas Bellinger 
1055baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
1056baa4d64bSNicholas Bellinger 				(struct sockaddr *)&sock_in, &err, 1);
1057dc58f760SAndy Grover 		if (!rc)
1058dc58f760SAndy Grover 			memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1059baa4d64bSNicholas Bellinger 
1060baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
1061baa4d64bSNicholas Bellinger 				(struct sockaddr *)&sock_in, &err, 0);
106269d75574SAndy Grover 		if (!rc)
106369d75574SAndy Grover 			memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1064baa4d64bSNicholas Bellinger 	}
1065baa4d64bSNicholas Bellinger 
1066baa4d64bSNicholas Bellinger 	return 0;
1067baa4d64bSNicholas Bellinger }
1068baa4d64bSNicholas Bellinger 
1069baa4d64bSNicholas Bellinger int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1070baa4d64bSNicholas Bellinger {
1071baa4d64bSNicholas Bellinger 	struct iscsi_login_req *login_req;
1072baa4d64bSNicholas Bellinger 	u32 padding = 0, payload_length;
1073baa4d64bSNicholas Bellinger 
1074baa4d64bSNicholas Bellinger 	if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1075baa4d64bSNicholas Bellinger 		return -1;
1076baa4d64bSNicholas Bellinger 
1077baa4d64bSNicholas Bellinger 	login_req = (struct iscsi_login_req *)login->req;
1078baa4d64bSNicholas Bellinger 	payload_length	= ntoh24(login_req->dlength);
1079baa4d64bSNicholas Bellinger 	padding = ((-payload_length) & 3);
1080baa4d64bSNicholas Bellinger 
1081baa4d64bSNicholas Bellinger 	pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1082baa4d64bSNicholas Bellinger 		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1083baa4d64bSNicholas Bellinger 		login_req->flags, login_req->itt, login_req->cmdsn,
1084baa4d64bSNicholas Bellinger 		login_req->exp_statsn, login_req->cid, payload_length);
1085baa4d64bSNicholas Bellinger 	/*
1086baa4d64bSNicholas Bellinger 	 * Setup the initial iscsi_login values from the leading
1087baa4d64bSNicholas Bellinger 	 * login request PDU.
1088baa4d64bSNicholas Bellinger 	 */
1089baa4d64bSNicholas Bellinger 	if (login->first_request) {
1090baa4d64bSNicholas Bellinger 		login_req = (struct iscsi_login_req *)login->req;
1091baa4d64bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
10923e1c81a9SNicholas Bellinger 		login->current_stage	= ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1093baa4d64bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1094baa4d64bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1095baa4d64bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1096baa4d64bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1097baa4d64bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1098baa4d64bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1099baa4d64bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1100baa4d64bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1101baa4d64bSNicholas Bellinger 	}
1102baa4d64bSNicholas Bellinger 
1103baa4d64bSNicholas Bellinger 	if (iscsi_target_check_login_request(conn, login) < 0)
1104baa4d64bSNicholas Bellinger 		return -1;
1105baa4d64bSNicholas Bellinger 
1106baa4d64bSNicholas Bellinger 	memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1107baa4d64bSNicholas Bellinger 	if (iscsi_login_rx_data(conn, login->req_buf,
1108baa4d64bSNicholas Bellinger 				payload_length + padding) < 0)
1109baa4d64bSNicholas Bellinger 		return -1;
1110baa4d64bSNicholas Bellinger 
1111baa4d64bSNicholas Bellinger 	return 0;
1112baa4d64bSNicholas Bellinger }
1113baa4d64bSNicholas Bellinger 
1114baa4d64bSNicholas Bellinger int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1115baa4d64bSNicholas Bellinger 			u32 length)
1116baa4d64bSNicholas Bellinger {
1117baa4d64bSNicholas Bellinger 	if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1118baa4d64bSNicholas Bellinger 		return -1;
1119baa4d64bSNicholas Bellinger 
1120baa4d64bSNicholas Bellinger 	return 0;
1121baa4d64bSNicholas Bellinger }
1122baa4d64bSNicholas Bellinger 
1123baa4d64bSNicholas Bellinger static int
1124baa4d64bSNicholas Bellinger iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1125baa4d64bSNicholas Bellinger {
1126baa4d64bSNicholas Bellinger 	int rc;
1127baa4d64bSNicholas Bellinger 
1128baa4d64bSNicholas Bellinger 	if (!t->owner) {
1129baa4d64bSNicholas Bellinger 		conn->conn_transport = t;
1130baa4d64bSNicholas Bellinger 		return 0;
1131baa4d64bSNicholas Bellinger 	}
1132baa4d64bSNicholas Bellinger 
1133baa4d64bSNicholas Bellinger 	rc = try_module_get(t->owner);
1134baa4d64bSNicholas Bellinger 	if (!rc) {
1135baa4d64bSNicholas Bellinger 		pr_err("try_module_get() failed for %s\n", t->name);
1136baa4d64bSNicholas Bellinger 		return -EINVAL;
1137baa4d64bSNicholas Bellinger 	}
1138baa4d64bSNicholas Bellinger 
1139baa4d64bSNicholas Bellinger 	conn->conn_transport = t;
1140baa4d64bSNicholas Bellinger 	return 0;
1141baa4d64bSNicholas Bellinger }
1142baa4d64bSNicholas Bellinger 
1143a91eb7d9SNicholas Bellinger void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1144a91eb7d9SNicholas Bellinger 		struct iscsi_np *np, bool zero_tsih, bool new_sess)
1145a91eb7d9SNicholas Bellinger {
11460bcc297eSChristophe Vu-Brugier 	if (!new_sess)
1147a91eb7d9SNicholas Bellinger 		goto old_sess_out;
1148a91eb7d9SNicholas Bellinger 
1149a91eb7d9SNicholas Bellinger 	pr_err("iSCSI Login negotiation failed.\n");
1150a91eb7d9SNicholas Bellinger 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1151a91eb7d9SNicholas Bellinger 				   ISCSI_LOGIN_STATUS_INIT_ERR);
1152a91eb7d9SNicholas Bellinger 	if (!zero_tsih || !conn->sess)
1153a91eb7d9SNicholas Bellinger 		goto old_sess_out;
1154a91eb7d9SNicholas Bellinger 	if (conn->sess->se_sess)
1155a91eb7d9SNicholas Bellinger 		transport_free_session(conn->sess->se_sess);
1156a91eb7d9SNicholas Bellinger 	if (conn->sess->session_index != 0) {
1157a91eb7d9SNicholas Bellinger 		spin_lock_bh(&sess_idr_lock);
1158a91eb7d9SNicholas Bellinger 		idr_remove(&sess_idr, conn->sess->session_index);
1159a91eb7d9SNicholas Bellinger 		spin_unlock_bh(&sess_idr_lock);
1160a91eb7d9SNicholas Bellinger 	}
1161a91eb7d9SNicholas Bellinger 	kfree(conn->sess->sess_ops);
1162a91eb7d9SNicholas Bellinger 	kfree(conn->sess);
1163a0b3b9b2SSagi Grimberg 	conn->sess = NULL;
1164a91eb7d9SNicholas Bellinger 
1165a91eb7d9SNicholas Bellinger old_sess_out:
1166a91eb7d9SNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1167a91eb7d9SNicholas Bellinger 	/*
1168a91eb7d9SNicholas Bellinger 	 * If login negotiation fails check if the Time2Retain timer
1169a91eb7d9SNicholas Bellinger 	 * needs to be restarted.
1170a91eb7d9SNicholas Bellinger 	 */
1171a91eb7d9SNicholas Bellinger 	if (!zero_tsih && conn->sess) {
1172a91eb7d9SNicholas Bellinger 		spin_lock_bh(&conn->sess->conn_lock);
1173a91eb7d9SNicholas Bellinger 		if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1174a91eb7d9SNicholas Bellinger 			struct se_portal_group *se_tpg =
117560bfcf8eSAndy Grover 					&conn->tpg->tpg_se_tpg;
1176a91eb7d9SNicholas Bellinger 
1177a91eb7d9SNicholas Bellinger 			atomic_set(&conn->sess->session_continuation, 0);
1178a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&conn->sess->conn_lock);
1179a91eb7d9SNicholas Bellinger 			spin_lock_bh(&se_tpg->session_lock);
1180a91eb7d9SNicholas Bellinger 			iscsit_start_time2retain_handler(conn->sess);
1181a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&se_tpg->session_lock);
1182a91eb7d9SNicholas Bellinger 		} else
1183a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&conn->sess->conn_lock);
1184a91eb7d9SNicholas Bellinger 		iscsit_dec_session_usage_count(conn->sess);
1185a91eb7d9SNicholas Bellinger 	}
1186a91eb7d9SNicholas Bellinger 
118769110e3cSHerbert Xu 	ahash_request_free(conn->conn_tx_hash);
118869110e3cSHerbert Xu 	if (conn->conn_rx_hash) {
118969110e3cSHerbert Xu 		struct crypto_ahash *tfm;
119069110e3cSHerbert Xu 
119169110e3cSHerbert Xu 		tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
119269110e3cSHerbert Xu 		ahash_request_free(conn->conn_rx_hash);
119369110e3cSHerbert Xu 		crypto_free_ahash(tfm);
119469110e3cSHerbert Xu 	}
1195a91eb7d9SNicholas Bellinger 
1196a91eb7d9SNicholas Bellinger 	free_cpumask_var(conn->conn_cpumask);
1197a91eb7d9SNicholas Bellinger 
1198a91eb7d9SNicholas Bellinger 	kfree(conn->conn_ops);
1199a91eb7d9SNicholas Bellinger 
1200a91eb7d9SNicholas Bellinger 	if (conn->param_list) {
1201a91eb7d9SNicholas Bellinger 		iscsi_release_param_list(conn->param_list);
1202a91eb7d9SNicholas Bellinger 		conn->param_list = NULL;
1203a91eb7d9SNicholas Bellinger 	}
1204a91eb7d9SNicholas Bellinger 	iscsi_target_nego_release(conn);
1205a91eb7d9SNicholas Bellinger 
1206a91eb7d9SNicholas Bellinger 	if (conn->sock) {
1207a91eb7d9SNicholas Bellinger 		sock_release(conn->sock);
1208a91eb7d9SNicholas Bellinger 		conn->sock = NULL;
1209a91eb7d9SNicholas Bellinger 	}
1210a91eb7d9SNicholas Bellinger 
1211954f2372SSagi Grimberg 	if (conn->conn_transport->iscsit_wait_conn)
1212954f2372SSagi Grimberg 		conn->conn_transport->iscsit_wait_conn(conn);
1213954f2372SSagi Grimberg 
1214a91eb7d9SNicholas Bellinger 	if (conn->conn_transport->iscsit_free_conn)
1215a91eb7d9SNicholas Bellinger 		conn->conn_transport->iscsit_free_conn(conn);
1216a91eb7d9SNicholas Bellinger 
1217a91eb7d9SNicholas Bellinger 	iscsit_put_transport(conn->conn_transport);
1218a91eb7d9SNicholas Bellinger 	kfree(conn);
1219a91eb7d9SNicholas Bellinger }
1220a91eb7d9SNicholas Bellinger 
1221e48354ceSNicholas Bellinger static int __iscsi_target_login_thread(struct iscsi_np *np)
1222e48354ceSNicholas Bellinger {
1223baa4d64bSNicholas Bellinger 	u8 *buffer, zero_tsih = 0;
122481a9c5e7SMikulas Patocka 	int ret = 0, rc;
1225e48354ceSNicholas Bellinger 	struct iscsi_conn *conn = NULL;
1226e48354ceSNicholas Bellinger 	struct iscsi_login *login;
1227e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = NULL;
1228e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu;
1229a91eb7d9SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np;
1230a91eb7d9SNicholas Bellinger 	bool new_sess = false;
1231e48354ceSNicholas Bellinger 
1232e48354ceSNicholas Bellinger 	flush_signals(current);
1233e48354ceSNicholas Bellinger 
1234e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1235e48354ceSNicholas Bellinger 	if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1236e48354ceSNicholas Bellinger 		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1237e48354ceSNicholas Bellinger 		complete(&np->np_restart_comp);
123881a9c5e7SMikulas Patocka 	} else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
123981a9c5e7SMikulas Patocka 		spin_unlock_bh(&np->np_thread_lock);
124081a9c5e7SMikulas Patocka 		goto exit;
1241e48354ceSNicholas Bellinger 	} else {
1242e48354ceSNicholas Bellinger 		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1243e48354ceSNicholas Bellinger 	}
1244e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1245e48354ceSNicholas Bellinger 
1246e48354ceSNicholas Bellinger 	conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1247e48354ceSNicholas Bellinger 	if (!conn) {
1248e48354ceSNicholas Bellinger 		pr_err("Could not allocate memory for"
1249e48354ceSNicholas Bellinger 			" new connection\n");
1250e48354ceSNicholas Bellinger 		/* Get another socket */
1251e48354ceSNicholas Bellinger 		return 1;
1252e48354ceSNicholas Bellinger 	}
1253e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1254e48354ceSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_FREE;
1255e48354ceSNicholas Bellinger 
1256baa4d64bSNicholas Bellinger 	if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1257baa4d64bSNicholas Bellinger 		kfree(conn);
1258baa4d64bSNicholas Bellinger 		return 1;
1259baa4d64bSNicholas Bellinger 	}
1260e48354ceSNicholas Bellinger 
1261baa4d64bSNicholas Bellinger 	rc = np->np_transport->iscsit_accept_np(np, conn);
1262baa4d64bSNicholas Bellinger 	if (rc == -ENOSYS) {
1263baa4d64bSNicholas Bellinger 		complete(&np->np_restart_comp);
1264baa4d64bSNicholas Bellinger 		iscsit_put_transport(conn->conn_transport);
1265baa4d64bSNicholas Bellinger 		kfree(conn);
1266baa4d64bSNicholas Bellinger 		conn = NULL;
1267baa4d64bSNicholas Bellinger 		goto exit;
1268baa4d64bSNicholas Bellinger 	} else if (rc < 0) {
1269baa4d64bSNicholas Bellinger 		spin_lock_bh(&np->np_thread_lock);
1270baa4d64bSNicholas Bellinger 		if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1271baa4d64bSNicholas Bellinger 			spin_unlock_bh(&np->np_thread_lock);
1272baa4d64bSNicholas Bellinger 			complete(&np->np_restart_comp);
1273baa4d64bSNicholas Bellinger 			iscsit_put_transport(conn->conn_transport);
1274baa4d64bSNicholas Bellinger 			kfree(conn);
1275baa4d64bSNicholas Bellinger 			conn = NULL;
1276baa4d64bSNicholas Bellinger 			/* Get another socket */
1277baa4d64bSNicholas Bellinger 			return 1;
1278baa4d64bSNicholas Bellinger 		}
1279baa4d64bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
1280baa4d64bSNicholas Bellinger 		iscsit_put_transport(conn->conn_transport);
1281baa4d64bSNicholas Bellinger 		kfree(conn);
1282baa4d64bSNicholas Bellinger 		conn = NULL;
1283baa4d64bSNicholas Bellinger 		goto out;
1284e48354ceSNicholas Bellinger 	}
1285e48354ceSNicholas Bellinger 	/*
1286e48354ceSNicholas Bellinger 	 * Perform the remaining iSCSI connection initialization items..
1287e48354ceSNicholas Bellinger 	 */
1288baa4d64bSNicholas Bellinger 	login = iscsi_login_init_conn(conn);
1289baa4d64bSNicholas Bellinger 	if (!login) {
1290e48354ceSNicholas Bellinger 		goto new_sess_out;
1291e48354ceSNicholas Bellinger 	}
1292e48354ceSNicholas Bellinger 
1293baa4d64bSNicholas Bellinger 	iscsi_start_login_thread_timer(np);
1294e48354ceSNicholas Bellinger 
1295baa4d64bSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1296baa4d64bSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_XPT_UP;
1297baa4d64bSNicholas Bellinger 	/*
1298baa4d64bSNicholas Bellinger 	 * This will process the first login request + payload..
1299baa4d64bSNicholas Bellinger 	 */
1300baa4d64bSNicholas Bellinger 	rc = np->np_transport->iscsit_get_login_rx(conn, login);
1301baa4d64bSNicholas Bellinger 	if (rc == 1)
1302baa4d64bSNicholas Bellinger 		return 1;
1303baa4d64bSNicholas Bellinger 	else if (rc < 0)
1304baa4d64bSNicholas Bellinger 		goto new_sess_out;
1305baa4d64bSNicholas Bellinger 
1306baa4d64bSNicholas Bellinger 	buffer = &login->req[0];
1307e48354ceSNicholas Bellinger 	pdu = (struct iscsi_login_req *)buffer;
1308e48354ceSNicholas Bellinger 	/*
1309e48354ceSNicholas Bellinger 	 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1310e48354ceSNicholas Bellinger 	 * when Status-Class != 0.
1311e48354ceSNicholas Bellinger 	*/
1312e48354ceSNicholas Bellinger 	conn->login_itt	= pdu->itt;
1313e48354ceSNicholas Bellinger 
1314e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1315e48354ceSNicholas Bellinger 	if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1316e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
131769d75574SAndy Grover 		pr_err("iSCSI Network Portal on %pISpc currently not"
131869d75574SAndy Grover 			" active.\n", &np->np_sockaddr);
1319e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1320e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1321e48354ceSNicholas Bellinger 		goto new_sess_out;
1322e48354ceSNicholas Bellinger 	}
1323e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1324e48354ceSNicholas Bellinger 
1325e48354ceSNicholas Bellinger 	conn->network_transport = np->np_network_transport;
1326e48354ceSNicholas Bellinger 
1327dc58f760SAndy Grover 	pr_debug("Received iSCSI login request from %pISpc on %s Network"
1328dc58f760SAndy Grover 		" Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name,
132969d75574SAndy Grover 		&conn->local_sockaddr);
1330e48354ceSNicholas Bellinger 
1331e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1332e48354ceSNicholas Bellinger 	conn->conn_state	= TARG_CONN_STATE_IN_LOGIN;
1333e48354ceSNicholas Bellinger 
1334e48354ceSNicholas Bellinger 	if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1335e48354ceSNicholas Bellinger 			pdu->min_version) < 0)
1336e48354ceSNicholas Bellinger 		goto new_sess_out;
1337e48354ceSNicholas Bellinger 
1338e48354ceSNicholas Bellinger 	zero_tsih = (pdu->tsih == 0x0000);
1339ee1b1b9cSAndy Grover 	if (zero_tsih) {
1340e48354ceSNicholas Bellinger 		/*
1341e48354ceSNicholas Bellinger 		 * This is the leading connection of a new session.
1342e48354ceSNicholas Bellinger 		 * We wait until after authentication to check for
1343e48354ceSNicholas Bellinger 		 * session reinstatement.
1344e48354ceSNicholas Bellinger 		 */
1345e48354ceSNicholas Bellinger 		if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1346e48354ceSNicholas Bellinger 			goto new_sess_out;
1347e48354ceSNicholas Bellinger 	} else {
1348e48354ceSNicholas Bellinger 		/*
1349e48354ceSNicholas Bellinger 		 * Add a new connection to an existing session.
1350e48354ceSNicholas Bellinger 		 * We check for a non-existant session in
1351e48354ceSNicholas Bellinger 		 * iscsi_login_non_zero_tsih_s2() below based
1352e48354ceSNicholas Bellinger 		 * on ISID/TSIH, but wait until after authentication
1353e48354ceSNicholas Bellinger 		 * to check for connection reinstatement, etc.
1354e48354ceSNicholas Bellinger 		 */
1355e48354ceSNicholas Bellinger 		if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1356e48354ceSNicholas Bellinger 			goto new_sess_out;
1357e48354ceSNicholas Bellinger 	}
1358e48354ceSNicholas Bellinger 	/*
1359baa4d64bSNicholas Bellinger 	 * SessionType: Discovery
1360baa4d64bSNicholas Bellinger 	 *
1361baa4d64bSNicholas Bellinger 	 * 	Locates Default Portal
1362baa4d64bSNicholas Bellinger 	 *
1363baa4d64bSNicholas Bellinger 	 * SessionType: Normal
1364baa4d64bSNicholas Bellinger 	 *
1365baa4d64bSNicholas Bellinger 	 * 	Locates Target Portal from NP -> Target IQN
1366e48354ceSNicholas Bellinger 	 */
1367baa4d64bSNicholas Bellinger 	rc = iscsi_target_locate_portal(np, conn, login);
1368baa4d64bSNicholas Bellinger 	if (rc < 0) {
1369e48354ceSNicholas Bellinger 		tpg = conn->tpg;
1370e48354ceSNicholas Bellinger 		goto new_sess_out;
1371e48354ceSNicholas Bellinger 	}
1372a91eb7d9SNicholas Bellinger 	login->zero_tsih = zero_tsih;
1373e48354ceSNicholas Bellinger 
137423a548eeSSagi Grimberg 	conn->sess->se_sess->sup_prot_ops =
137523a548eeSSagi Grimberg 		conn->conn_transport->iscsit_get_sup_prot_ops(conn);
137623a548eeSSagi Grimberg 
1377e48354ceSNicholas Bellinger 	tpg = conn->tpg;
1378e48354ceSNicholas Bellinger 	if (!tpg) {
1379e48354ceSNicholas Bellinger 		pr_err("Unable to locate struct iscsi_conn->tpg\n");
1380e48354ceSNicholas Bellinger 		goto new_sess_out;
1381e48354ceSNicholas Bellinger 	}
1382e48354ceSNicholas Bellinger 
1383e48354ceSNicholas Bellinger 	if (zero_tsih) {
1384baa4d64bSNicholas Bellinger 		if (iscsi_login_zero_tsih_s2(conn) < 0)
1385e48354ceSNicholas Bellinger 			goto new_sess_out;
1386e48354ceSNicholas Bellinger 	} else {
1387baa4d64bSNicholas Bellinger 		if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1388e48354ceSNicholas Bellinger 			goto old_sess_out;
1389e48354ceSNicholas Bellinger 	}
1390e48354ceSNicholas Bellinger 
139142bc3e57SVarun Prakash 	if (conn->conn_transport->iscsit_validate_params) {
139242bc3e57SVarun Prakash 		ret = conn->conn_transport->iscsit_validate_params(conn);
139342bc3e57SVarun Prakash 		if (ret < 0) {
139442bc3e57SVarun Prakash 			if (zero_tsih)
139542bc3e57SVarun Prakash 				goto new_sess_out;
139642bc3e57SVarun Prakash 			else
139742bc3e57SVarun Prakash 				goto old_sess_out;
139842bc3e57SVarun Prakash 		}
139942bc3e57SVarun Prakash 	}
140042bc3e57SVarun Prakash 
1401a91eb7d9SNicholas Bellinger 	ret = iscsi_target_start_negotiation(login, conn);
1402a91eb7d9SNicholas Bellinger 	if (ret < 0)
1403e48354ceSNicholas Bellinger 		goto new_sess_out;
1404e48354ceSNicholas Bellinger 
1405e48354ceSNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1406e48354ceSNicholas Bellinger 
1407a91eb7d9SNicholas Bellinger 	if (ret == 1) {
1408a91eb7d9SNicholas Bellinger 		tpg_np = conn->tpg_np;
1409e48354ceSNicholas Bellinger 
1410e5419865SNicholas Bellinger 		iscsi_post_login_handler(np, conn, zero_tsih);
1411a91eb7d9SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
1412a91eb7d9SNicholas Bellinger 	}
1413a91eb7d9SNicholas Bellinger 
1414e48354ceSNicholas Bellinger 	tpg = NULL;
1415a91eb7d9SNicholas Bellinger 	tpg_np = NULL;
1416e48354ceSNicholas Bellinger 	/* Get another socket */
1417e48354ceSNicholas Bellinger 	return 1;
1418e48354ceSNicholas Bellinger 
1419e48354ceSNicholas Bellinger new_sess_out:
1420a91eb7d9SNicholas Bellinger 	new_sess = true;
1421e48354ceSNicholas Bellinger old_sess_out:
1422a91eb7d9SNicholas Bellinger 	tpg_np = conn->tpg_np;
1423a91eb7d9SNicholas Bellinger 	iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1424a91eb7d9SNicholas Bellinger 	new_sess = false;
1425e48354ceSNicholas Bellinger 
1426e48354ceSNicholas Bellinger 	if (tpg) {
1427a91eb7d9SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
1428e48354ceSNicholas Bellinger 		tpg = NULL;
1429a91eb7d9SNicholas Bellinger 		tpg_np = NULL;
1430e48354ceSNicholas Bellinger 	}
1431e48354ceSNicholas Bellinger 
1432e48354ceSNicholas Bellinger out:
1433e48354ceSNicholas Bellinger 	return 1;
143481a9c5e7SMikulas Patocka 
1435baa4d64bSNicholas Bellinger exit:
1436e48354ceSNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1437e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1438e48354ceSNicholas Bellinger 	np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1439e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1440baa4d64bSNicholas Bellinger 
1441e48354ceSNicholas Bellinger 	return 0;
1442e48354ceSNicholas Bellinger }
1443e48354ceSNicholas Bellinger 
1444e48354ceSNicholas Bellinger int iscsi_target_login_thread(void *arg)
1445e48354ceSNicholas Bellinger {
14468359cf43SJörn Engel 	struct iscsi_np *np = arg;
1447e48354ceSNicholas Bellinger 	int ret;
1448e48354ceSNicholas Bellinger 
1449e48354ceSNicholas Bellinger 	allow_signal(SIGINT);
1450e48354ceSNicholas Bellinger 
145181a9c5e7SMikulas Patocka 	while (1) {
1452e48354ceSNicholas Bellinger 		ret = __iscsi_target_login_thread(np);
1453e48354ceSNicholas Bellinger 		/*
1454e48354ceSNicholas Bellinger 		 * We break and exit here unless another sock_accept() call
1455e48354ceSNicholas Bellinger 		 * is expected.
1456e48354ceSNicholas Bellinger 		 */
1457e48354ceSNicholas Bellinger 		if (ret != 1)
1458e48354ceSNicholas Bellinger 			break;
1459e48354ceSNicholas Bellinger 	}
1460e48354ceSNicholas Bellinger 
1461e48354ceSNicholas Bellinger 	return 0;
1462e48354ceSNicholas Bellinger }
1463