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 
19e48354ceSNicholas Bellinger #include <linux/string.h>
20e48354ceSNicholas Bellinger #include <linux/kthread.h>
21e48354ceSNicholas Bellinger #include <linux/crypto.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 
27e48354ceSNicholas Bellinger #include "iscsi_target_core.h"
28e48354ceSNicholas Bellinger #include "iscsi_target_tq.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_stat.h"
35e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
36e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
37e48354ceSNicholas Bellinger #include "iscsi_target.h"
38e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
39e48354ceSNicholas Bellinger 
40baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
41baa4d64bSNicholas Bellinger 
42baa4d64bSNicholas Bellinger static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
43e48354ceSNicholas Bellinger {
44baa4d64bSNicholas Bellinger 	struct iscsi_login *login;
45baa4d64bSNicholas Bellinger 
46baa4d64bSNicholas Bellinger 	login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
47baa4d64bSNicholas Bellinger 	if (!login) {
48baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for struct iscsi_login.\n");
49baa4d64bSNicholas Bellinger 		return NULL;
50baa4d64bSNicholas Bellinger 	}
51a91eb7d9SNicholas Bellinger 	conn->login = login;
52baa4d64bSNicholas Bellinger 	login->conn = conn;
53baa4d64bSNicholas Bellinger 	login->first_request = 1;
54baa4d64bSNicholas Bellinger 
55baa4d64bSNicholas Bellinger 	login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
56baa4d64bSNicholas Bellinger 	if (!login->req_buf) {
57baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for response buffer.\n");
58baa4d64bSNicholas Bellinger 		goto out_login;
59baa4d64bSNicholas Bellinger 	}
60baa4d64bSNicholas Bellinger 
61baa4d64bSNicholas Bellinger 	login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
62baa4d64bSNicholas Bellinger 	if (!login->rsp_buf) {
63baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for request buffer.\n");
64baa4d64bSNicholas Bellinger 		goto out_req_buf;
65baa4d64bSNicholas Bellinger 	}
66baa4d64bSNicholas Bellinger 
67baa4d64bSNicholas Bellinger 	conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
68baa4d64bSNicholas Bellinger 	if (!conn->conn_ops) {
69baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for"
70baa4d64bSNicholas Bellinger 			" struct iscsi_conn_ops.\n");
71baa4d64bSNicholas Bellinger 		goto out_rsp_buf;
72baa4d64bSNicholas Bellinger 	}
73baa4d64bSNicholas Bellinger 
74d5627acbSRoland Dreier 	init_waitqueue_head(&conn->queues_wq);
75e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->conn_list);
76e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->conn_cmd_list);
77e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->immed_queue_list);
78e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->response_queue_list);
79e48354ceSNicholas Bellinger 	init_completion(&conn->conn_post_wait_comp);
80e48354ceSNicholas Bellinger 	init_completion(&conn->conn_wait_comp);
81e48354ceSNicholas Bellinger 	init_completion(&conn->conn_wait_rcfr_comp);
82e48354ceSNicholas Bellinger 	init_completion(&conn->conn_waiting_on_uc_comp);
83e48354ceSNicholas Bellinger 	init_completion(&conn->conn_logout_comp);
84e48354ceSNicholas Bellinger 	init_completion(&conn->rx_half_close_comp);
85e48354ceSNicholas Bellinger 	init_completion(&conn->tx_half_close_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 {
118e48354ceSNicholas Bellinger 	/*
119e48354ceSNicholas Bellinger 	 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
120e48354ceSNicholas Bellinger 	 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
121e48354ceSNicholas Bellinger 	 * to software 1x8 byte slicing from crc32c.ko
122e48354ceSNicholas Bellinger 	 */
123e48354ceSNicholas Bellinger 	conn->conn_rx_hash.flags = 0;
124e48354ceSNicholas Bellinger 	conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
125e48354ceSNicholas Bellinger 						CRYPTO_ALG_ASYNC);
126e48354ceSNicholas Bellinger 	if (IS_ERR(conn->conn_rx_hash.tfm)) {
127e48354ceSNicholas Bellinger 		pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
128e48354ceSNicholas Bellinger 		return -ENOMEM;
129e48354ceSNicholas Bellinger 	}
130e48354ceSNicholas Bellinger 
131e48354ceSNicholas Bellinger 	conn->conn_tx_hash.flags = 0;
132e48354ceSNicholas Bellinger 	conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
133e48354ceSNicholas Bellinger 						CRYPTO_ALG_ASYNC);
134e48354ceSNicholas Bellinger 	if (IS_ERR(conn->conn_tx_hash.tfm)) {
135e48354ceSNicholas Bellinger 		pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
136e48354ceSNicholas Bellinger 		crypto_free_hash(conn->conn_rx_hash.tfm);
137e48354ceSNicholas Bellinger 		return -ENOMEM;
138e48354ceSNicholas Bellinger 	}
139e48354ceSNicholas Bellinger 
140e48354ceSNicholas Bellinger 	return 0;
141e48354ceSNicholas Bellinger }
142e48354ceSNicholas Bellinger 
143e48354ceSNicholas Bellinger static int iscsi_login_check_initiator_version(
144e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
145e48354ceSNicholas Bellinger 	u8 version_max,
146e48354ceSNicholas Bellinger 	u8 version_min)
147e48354ceSNicholas Bellinger {
148e48354ceSNicholas Bellinger 	if ((version_max != 0x00) || (version_min != 0x00)) {
149e48354ceSNicholas Bellinger 		pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
150e48354ceSNicholas Bellinger 			" version Min/Max 0x%02x/0x%02x, rejecting login.\n",
151e48354ceSNicholas Bellinger 			version_min, version_max);
152e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
153e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_VERSION);
154e48354ceSNicholas Bellinger 		return -1;
155e48354ceSNicholas Bellinger 	}
156e48354ceSNicholas Bellinger 
157e48354ceSNicholas Bellinger 	return 0;
158e48354ceSNicholas Bellinger }
159e48354ceSNicholas Bellinger 
160e48354ceSNicholas Bellinger int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
161e48354ceSNicholas Bellinger {
162e48354ceSNicholas Bellinger 	int sessiontype;
163e48354ceSNicholas Bellinger 	struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
164e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
165e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL, *sess_p = NULL;
166e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
167e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
168e48354ceSNicholas Bellinger 
169e48354ceSNicholas Bellinger 	initiatorname_param = iscsi_find_param_from_key(
170e48354ceSNicholas Bellinger 			INITIATORNAME, conn->param_list);
171e48354ceSNicholas Bellinger 	sessiontype_param = iscsi_find_param_from_key(
172e48354ceSNicholas Bellinger 			SESSIONTYPE, conn->param_list);
1731c5c12c6SRoland Dreier 	if (!initiatorname_param || !sessiontype_param) {
1741c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1751c5c12c6SRoland Dreier 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
176e48354ceSNicholas Bellinger 		return -1;
1771c5c12c6SRoland Dreier 	}
178e48354ceSNicholas Bellinger 
179e48354ceSNicholas Bellinger 	sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
180e48354ceSNicholas Bellinger 
181e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
182e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
183e48354ceSNicholas Bellinger 			sess_list) {
184e48354ceSNicholas Bellinger 
1858359cf43SJörn Engel 		sess_p = se_sess->fabric_sess_ptr;
186e48354ceSNicholas Bellinger 		spin_lock(&sess_p->conn_lock);
187e48354ceSNicholas Bellinger 		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
188e48354ceSNicholas Bellinger 		    atomic_read(&sess_p->session_logout) ||
189e48354ceSNicholas Bellinger 		    (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
190e48354ceSNicholas Bellinger 			spin_unlock(&sess_p->conn_lock);
191e48354ceSNicholas Bellinger 			continue;
192e48354ceSNicholas Bellinger 		}
1938359cf43SJörn Engel 		if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
1948359cf43SJörn Engel 		   (!strcmp(sess_p->sess_ops->InitiatorName,
1958359cf43SJörn Engel 			    initiatorname_param->value) &&
196e48354ceSNicholas Bellinger 		   (sess_p->sess_ops->SessionType == sessiontype))) {
197e48354ceSNicholas Bellinger 			atomic_set(&sess_p->session_reinstatement, 1);
198e48354ceSNicholas Bellinger 			spin_unlock(&sess_p->conn_lock);
199e48354ceSNicholas Bellinger 			iscsit_inc_session_usage_count(sess_p);
200e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess_p);
201e48354ceSNicholas Bellinger 			sess = sess_p;
202e48354ceSNicholas Bellinger 			break;
203e48354ceSNicholas Bellinger 		}
204e48354ceSNicholas Bellinger 		spin_unlock(&sess_p->conn_lock);
205e48354ceSNicholas Bellinger 	}
206e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
207e48354ceSNicholas Bellinger 	/*
208e48354ceSNicholas Bellinger 	 * If the Time2Retain handler has expired, the session is already gone.
209e48354ceSNicholas Bellinger 	 */
210e48354ceSNicholas Bellinger 	if (!sess)
211e48354ceSNicholas Bellinger 		return 0;
212e48354ceSNicholas Bellinger 
213e48354ceSNicholas Bellinger 	pr_debug("%s iSCSI Session SID %u is still active for %s,"
214e48354ceSNicholas Bellinger 		" preforming session reinstatement.\n", (sessiontype) ?
215e48354ceSNicholas Bellinger 		"Discovery" : "Normal", sess->sid,
216e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
217e48354ceSNicholas Bellinger 
218e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
219e48354ceSNicholas Bellinger 	if (sess->session_state == TARG_SESS_STATE_FAILED) {
220e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
221e48354ceSNicholas Bellinger 		iscsit_dec_session_usage_count(sess);
22299367f01SNicholas Bellinger 		target_put_session(sess->se_sess);
22399367f01SNicholas Bellinger 		return 0;
224e48354ceSNicholas Bellinger 	}
225e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
226e48354ceSNicholas Bellinger 
227e48354ceSNicholas Bellinger 	iscsit_stop_session(sess, 1, 1);
228e48354ceSNicholas Bellinger 	iscsit_dec_session_usage_count(sess);
229e48354ceSNicholas Bellinger 
23099367f01SNicholas Bellinger 	target_put_session(sess->se_sess);
23199367f01SNicholas Bellinger 	return 0;
232e48354ceSNicholas Bellinger }
233e48354ceSNicholas Bellinger 
234e48354ceSNicholas Bellinger static void iscsi_login_set_conn_values(
235e48354ceSNicholas Bellinger 	struct iscsi_session *sess,
236e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
23750e5c87dSChristoph Hellwig 	__be16 cid)
238e48354ceSNicholas Bellinger {
239e48354ceSNicholas Bellinger 	conn->sess		= sess;
24050e5c87dSChristoph Hellwig 	conn->cid		= be16_to_cpu(cid);
241e48354ceSNicholas Bellinger 	/*
242e48354ceSNicholas Bellinger 	 * Generate a random Status sequence number (statsn) for the new
243e48354ceSNicholas Bellinger 	 * iSCSI connection.
244e48354ceSNicholas Bellinger 	 */
245e48354ceSNicholas Bellinger 	get_random_bytes(&conn->stat_sn, sizeof(u32));
246e48354ceSNicholas Bellinger 
247e48354ceSNicholas Bellinger 	mutex_lock(&auth_id_lock);
248e48354ceSNicholas Bellinger 	conn->auth_id		= iscsit_global->auth_id++;
249e48354ceSNicholas Bellinger 	mutex_unlock(&auth_id_lock);
250e48354ceSNicholas Bellinger }
251e48354ceSNicholas Bellinger 
252e48354ceSNicholas Bellinger /*
253e48354ceSNicholas Bellinger  *	This is the leading connection of a new session,
254e48354ceSNicholas Bellinger  *	or session reinstatement.
255e48354ceSNicholas Bellinger  */
256e48354ceSNicholas Bellinger static int iscsi_login_zero_tsih_s1(
257e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
258e48354ceSNicholas Bellinger 	unsigned char *buf)
259e48354ceSNicholas Bellinger {
260e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL;
261e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
262e70beee7SNicholas Bellinger 	enum target_prot_op sup_pro_ops;
26313b5533aSBenjamin Wang 	int ret;
264e48354ceSNicholas Bellinger 
265e48354ceSNicholas Bellinger 	sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
266e48354ceSNicholas Bellinger 	if (!sess) {
267e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
268e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
269e48354ceSNicholas Bellinger 		pr_err("Could not allocate memory for session\n");
2700957627aSNicholas Bellinger 		return -ENOMEM;
271e48354ceSNicholas Bellinger 	}
272e48354ceSNicholas Bellinger 
273e48354ceSNicholas Bellinger 	iscsi_login_set_conn_values(sess, conn, pdu->cid);
274e48354ceSNicholas Bellinger 	sess->init_task_tag	= pdu->itt;
2758359cf43SJörn Engel 	memcpy(&sess->isid, pdu->isid, 6);
27650e5c87dSChristoph Hellwig 	sess->exp_cmd_sn	= be32_to_cpu(pdu->cmdsn);
277e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->sess_conn_list);
278e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
279e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->cr_active_list);
280e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->cr_inactive_list);
281e48354ceSNicholas Bellinger 	init_completion(&sess->async_msg_comp);
282e48354ceSNicholas Bellinger 	init_completion(&sess->reinstatement_comp);
283e48354ceSNicholas Bellinger 	init_completion(&sess->session_wait_comp);
284e48354ceSNicholas Bellinger 	init_completion(&sess->session_waiting_on_uc_comp);
285e48354ceSNicholas Bellinger 	mutex_init(&sess->cmdsn_mutex);
286e48354ceSNicholas Bellinger 	spin_lock_init(&sess->conn_lock);
287e48354ceSNicholas Bellinger 	spin_lock_init(&sess->cr_a_lock);
288e48354ceSNicholas Bellinger 	spin_lock_init(&sess->cr_i_lock);
289e48354ceSNicholas Bellinger 	spin_lock_init(&sess->session_usage_lock);
290e48354ceSNicholas Bellinger 	spin_lock_init(&sess->ttt_lock);
291e48354ceSNicholas Bellinger 
292c9365bd0STejun Heo 	idr_preload(GFP_KERNEL);
293998866b0SRoland Dreier 	spin_lock_bh(&sess_idr_lock);
294c9365bd0STejun Heo 	ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
295c9365bd0STejun Heo 	if (ret >= 0)
296c9365bd0STejun Heo 		sess->session_index = ret;
297998866b0SRoland Dreier 	spin_unlock_bh(&sess_idr_lock);
298c9365bd0STejun Heo 	idr_preload_end();
299e48354ceSNicholas Bellinger 
30013b5533aSBenjamin Wang 	if (ret < 0) {
301c9365bd0STejun Heo 		pr_err("idr_alloc() for sess_idr failed\n");
30213b5533aSBenjamin Wang 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
30313b5533aSBenjamin Wang 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
30413b5533aSBenjamin Wang 		kfree(sess);
30513b5533aSBenjamin Wang 		return -ENOMEM;
30613b5533aSBenjamin Wang 	}
30713b5533aSBenjamin Wang 
308e48354ceSNicholas Bellinger 	sess->creation_time = get_jiffies_64();
309e48354ceSNicholas Bellinger 	/*
310e48354ceSNicholas Bellinger 	 * The FFP CmdSN window values will be allocated from the TPG's
311e48354ceSNicholas Bellinger 	 * Initiator Node's ACL once the login has been successfully completed.
312e48354ceSNicholas Bellinger 	 */
31350e5c87dSChristoph Hellwig 	sess->max_cmd_sn	= be32_to_cpu(pdu->cmdsn);
314e48354ceSNicholas Bellinger 
315e48354ceSNicholas Bellinger 	sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
316e48354ceSNicholas Bellinger 	if (!sess->sess_ops) {
317e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
318e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
319e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for"
320e48354ceSNicholas Bellinger 				" struct iscsi_sess_ops.\n");
3210957627aSNicholas Bellinger 		kfree(sess);
3220957627aSNicholas Bellinger 		return -ENOMEM;
323e48354ceSNicholas Bellinger 	}
324e70beee7SNicholas Bellinger 	sup_pro_ops = conn->conn_transport->iscsit_get_sup_prot_ops(conn);
325e48354ceSNicholas Bellinger 
326e70beee7SNicholas Bellinger 	sess->se_sess = transport_init_session(sup_pro_ops);
3270957627aSNicholas Bellinger 	if (IS_ERR(sess->se_sess)) {
328e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
329e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
3300957627aSNicholas Bellinger 		kfree(sess);
3310957627aSNicholas Bellinger 		return -ENOMEM;
332e48354ceSNicholas Bellinger 	}
333e48354ceSNicholas Bellinger 
334e48354ceSNicholas Bellinger 	return 0;
335e48354ceSNicholas Bellinger }
336e48354ceSNicholas Bellinger 
337e48354ceSNicholas Bellinger static int iscsi_login_zero_tsih_s2(
338e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
339e48354ceSNicholas Bellinger {
340e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na;
341e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
342e48354ceSNicholas Bellinger 	unsigned char buf[32];
34303aa2070SNicholas Bellinger 	bool iser = false;
344e48354ceSNicholas Bellinger 
345e48354ceSNicholas Bellinger 	sess->tpg = conn->tpg;
346e48354ceSNicholas Bellinger 
347e48354ceSNicholas Bellinger 	/*
348e48354ceSNicholas Bellinger 	 * Assign a new TPG Session Handle.  Note this is protected with
349e48354ceSNicholas Bellinger 	 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
350e48354ceSNicholas Bellinger 	 */
35160bfcf8eSAndy Grover 	sess->tsih = ++sess->tpg->ntsih;
352e48354ceSNicholas Bellinger 	if (!sess->tsih)
35360bfcf8eSAndy Grover 		sess->tsih = ++sess->tpg->ntsih;
354e48354ceSNicholas Bellinger 
355e48354ceSNicholas Bellinger 	/*
356e48354ceSNicholas Bellinger 	 * Create the default params from user defined values..
357e48354ceSNicholas Bellinger 	 */
358e48354ceSNicholas Bellinger 	if (iscsi_copy_param_list(&conn->param_list,
35960bfcf8eSAndy Grover 				conn->tpg->param_list, 1) < 0) {
360e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
361e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
362e48354ceSNicholas Bellinger 		return -1;
363e48354ceSNicholas Bellinger 	}
364e48354ceSNicholas Bellinger 
36503aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
36603aa2070SNicholas Bellinger 		iser = true;
36703aa2070SNicholas Bellinger 
36803aa2070SNicholas Bellinger 	iscsi_set_keys_to_negotiate(conn->param_list, iser);
369e48354ceSNicholas Bellinger 
370e48354ceSNicholas Bellinger 	if (sess->sess_ops->SessionType)
371e48354ceSNicholas Bellinger 		return iscsi_set_keys_irrelevant_for_discovery(
372e48354ceSNicholas Bellinger 				conn->param_list);
373e48354ceSNicholas Bellinger 
374e48354ceSNicholas Bellinger 	na = iscsit_tpg_get_node_attrib(sess);
375e48354ceSNicholas Bellinger 
376e48354ceSNicholas Bellinger 	/*
377e48354ceSNicholas Bellinger 	 * Need to send TargetPortalGroupTag back in first login response
378e48354ceSNicholas Bellinger 	 * on any iSCSI connection where the Initiator provides TargetName.
379e48354ceSNicholas Bellinger 	 * See 5.3.1.  Login Phase Start
380e48354ceSNicholas Bellinger 	 *
381e48354ceSNicholas Bellinger 	 * In our case, we have already located the struct iscsi_tiqn at this point.
382e48354ceSNicholas Bellinger 	 */
383e48354ceSNicholas Bellinger 	memset(buf, 0, 32);
38460bfcf8eSAndy Grover 	sprintf(buf, "TargetPortalGroupTag=%hu", sess->tpg->tpgt);
385e48354ceSNicholas Bellinger 	if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
386e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
387e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
388e48354ceSNicholas Bellinger 		return -1;
389e48354ceSNicholas Bellinger 	}
390e48354ceSNicholas Bellinger 
391e48354ceSNicholas Bellinger 	/*
392e48354ceSNicholas Bellinger 	 * Workaround for Initiators that have broken connection recovery logic.
393e48354ceSNicholas Bellinger 	 *
394e48354ceSNicholas Bellinger 	 * "We would really like to get rid of this." Linux-iSCSI.org team
395e48354ceSNicholas Bellinger 	 */
396e48354ceSNicholas Bellinger 	memset(buf, 0, 32);
397e48354ceSNicholas Bellinger 	sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl);
398e48354ceSNicholas Bellinger 	if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
399e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
400e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
401e48354ceSNicholas Bellinger 		return -1;
402e48354ceSNicholas Bellinger 	}
403e48354ceSNicholas Bellinger 
404e48354ceSNicholas Bellinger 	if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
405e48354ceSNicholas Bellinger 		return -1;
40603aa2070SNicholas Bellinger 	/*
40703aa2070SNicholas Bellinger 	 * Set RDMAExtensions=Yes by default for iSER enabled network portals
40803aa2070SNicholas Bellinger 	 */
40903aa2070SNicholas Bellinger 	if (iser) {
41003aa2070SNicholas Bellinger 		struct iscsi_param *param;
41103aa2070SNicholas Bellinger 		unsigned long mrdsl, off;
41203aa2070SNicholas Bellinger 		int rc;
41303aa2070SNicholas Bellinger 
41403aa2070SNicholas Bellinger 		sprintf(buf, "RDMAExtensions=Yes");
41503aa2070SNicholas Bellinger 		if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
41603aa2070SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
41703aa2070SNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
41803aa2070SNicholas Bellinger 			return -1;
41903aa2070SNicholas Bellinger 		}
42003aa2070SNicholas Bellinger 		/*
42103aa2070SNicholas Bellinger 		 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
42203aa2070SNicholas Bellinger 		 * Immediate Data + Unsolicitied Data-OUT if necessary..
42303aa2070SNicholas Bellinger 		 */
42403aa2070SNicholas Bellinger 		param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
42503aa2070SNicholas Bellinger 						  conn->param_list);
42603aa2070SNicholas Bellinger 		if (!param) {
42703aa2070SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
42803aa2070SNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
42903aa2070SNicholas Bellinger 			return -1;
43003aa2070SNicholas Bellinger 		}
43157103d7fSJingoo Han 		rc = kstrtoul(param->value, 0, &mrdsl);
43203aa2070SNicholas Bellinger 		if (rc < 0) {
43303aa2070SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
43403aa2070SNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
43503aa2070SNicholas Bellinger 			return -1;
43603aa2070SNicholas Bellinger 		}
43703aa2070SNicholas Bellinger 		off = mrdsl % PAGE_SIZE;
43803aa2070SNicholas Bellinger 		if (!off)
43952d0aa79SNicholas Bellinger 			goto check_prot;
44003aa2070SNicholas Bellinger 
44103aa2070SNicholas Bellinger 		if (mrdsl < PAGE_SIZE)
44203aa2070SNicholas Bellinger 			mrdsl = PAGE_SIZE;
44303aa2070SNicholas Bellinger 		else
44403aa2070SNicholas Bellinger 			mrdsl -= off;
44503aa2070SNicholas Bellinger 
44603aa2070SNicholas Bellinger 		pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
44703aa2070SNicholas Bellinger 			" to PAGE_SIZE\n", mrdsl);
44803aa2070SNicholas Bellinger 
44903aa2070SNicholas Bellinger 		sprintf(buf, "MaxRecvDataSegmentLength=%lu\n", mrdsl);
45003aa2070SNicholas Bellinger 		if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
45103aa2070SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
45203aa2070SNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
45303aa2070SNicholas Bellinger 			return -1;
45403aa2070SNicholas Bellinger 		}
45552d0aa79SNicholas Bellinger 		/*
45652d0aa79SNicholas Bellinger 		 * ISER currently requires that ImmediateData + Unsolicited
45752d0aa79SNicholas Bellinger 		 * Data be disabled when protection / signature MRs are enabled.
45852d0aa79SNicholas Bellinger 		 */
45952d0aa79SNicholas Bellinger check_prot:
46052d0aa79SNicholas Bellinger 		if (sess->se_sess->sup_prot_ops &
46152d0aa79SNicholas Bellinger 		   (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
46252d0aa79SNicholas Bellinger 		    TARGET_PROT_DOUT_INSERT)) {
46352d0aa79SNicholas Bellinger 
46452d0aa79SNicholas Bellinger 			sprintf(buf, "ImmediateData=No");
46552d0aa79SNicholas Bellinger 			if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
46652d0aa79SNicholas Bellinger 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
46752d0aa79SNicholas Bellinger 						    ISCSI_LOGIN_STATUS_NO_RESOURCES);
46852d0aa79SNicholas Bellinger 				return -1;
46952d0aa79SNicholas Bellinger 			}
47052d0aa79SNicholas Bellinger 
47152d0aa79SNicholas Bellinger 			sprintf(buf, "InitialR2T=Yes");
47252d0aa79SNicholas Bellinger 			if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
47352d0aa79SNicholas Bellinger 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
47452d0aa79SNicholas Bellinger 						    ISCSI_LOGIN_STATUS_NO_RESOURCES);
47552d0aa79SNicholas Bellinger 				return -1;
47652d0aa79SNicholas Bellinger 			}
47752d0aa79SNicholas Bellinger 			pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
47852d0aa79SNicholas Bellinger 				 " T10-PI enabled ISER session\n");
47952d0aa79SNicholas Bellinger 		}
48003aa2070SNicholas Bellinger 	}
481e48354ceSNicholas Bellinger 
482e48354ceSNicholas Bellinger 	return 0;
483e48354ceSNicholas Bellinger }
484e48354ceSNicholas Bellinger 
485e48354ceSNicholas Bellinger /*
486e48354ceSNicholas Bellinger  * Remove PSTATE_NEGOTIATE for the four FIM related keys.
487e48354ceSNicholas Bellinger  * The Initiator node will be able to enable FIM by proposing them itself.
488e48354ceSNicholas Bellinger  */
489e48354ceSNicholas Bellinger int iscsi_login_disable_FIM_keys(
490e48354ceSNicholas Bellinger 	struct iscsi_param_list *param_list,
491e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
492e48354ceSNicholas Bellinger {
493e48354ceSNicholas Bellinger 	struct iscsi_param *param;
494e48354ceSNicholas Bellinger 
495e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key("OFMarker", param_list);
496e48354ceSNicholas Bellinger 	if (!param) {
497e48354ceSNicholas Bellinger 		pr_err("iscsi_find_param_from_key() for"
498e48354ceSNicholas Bellinger 				" OFMarker failed\n");
499e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
500e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
501e48354ceSNicholas Bellinger 		return -1;
502e48354ceSNicholas Bellinger 	}
503e48354ceSNicholas Bellinger 	param->state &= ~PSTATE_NEGOTIATE;
504e48354ceSNicholas Bellinger 
505e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key("OFMarkInt", param_list);
506e48354ceSNicholas Bellinger 	if (!param) {
507e48354ceSNicholas Bellinger 		pr_err("iscsi_find_param_from_key() for"
508e48354ceSNicholas Bellinger 				" IFMarker failed\n");
509e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
510e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
511e48354ceSNicholas Bellinger 		return -1;
512e48354ceSNicholas Bellinger 	}
513e48354ceSNicholas Bellinger 	param->state &= ~PSTATE_NEGOTIATE;
514e48354ceSNicholas Bellinger 
515e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key("IFMarker", param_list);
516e48354ceSNicholas Bellinger 	if (!param) {
517e48354ceSNicholas Bellinger 		pr_err("iscsi_find_param_from_key() for"
518e48354ceSNicholas Bellinger 				" IFMarker failed\n");
519e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
520e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
521e48354ceSNicholas Bellinger 		return -1;
522e48354ceSNicholas Bellinger 	}
523e48354ceSNicholas Bellinger 	param->state &= ~PSTATE_NEGOTIATE;
524e48354ceSNicholas Bellinger 
525e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key("IFMarkInt", param_list);
526e48354ceSNicholas Bellinger 	if (!param) {
527e48354ceSNicholas Bellinger 		pr_err("iscsi_find_param_from_key() for"
528e48354ceSNicholas Bellinger 				" IFMarker failed\n");
529e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
530e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
531e48354ceSNicholas Bellinger 		return -1;
532e48354ceSNicholas Bellinger 	}
533e48354ceSNicholas Bellinger 	param->state &= ~PSTATE_NEGOTIATE;
534e48354ceSNicholas Bellinger 
535e48354ceSNicholas Bellinger 	return 0;
536e48354ceSNicholas Bellinger }
537e48354ceSNicholas Bellinger 
538e48354ceSNicholas Bellinger static int iscsi_login_non_zero_tsih_s1(
539e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
540e48354ceSNicholas Bellinger 	unsigned char *buf)
541e48354ceSNicholas Bellinger {
542e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
543e48354ceSNicholas Bellinger 
544e48354ceSNicholas Bellinger 	iscsi_login_set_conn_values(NULL, conn, pdu->cid);
545e48354ceSNicholas Bellinger 	return 0;
546e48354ceSNicholas Bellinger }
547e48354ceSNicholas Bellinger 
548e48354ceSNicholas Bellinger /*
549e48354ceSNicholas Bellinger  *	Add a new connection to an existing session.
550e48354ceSNicholas Bellinger  */
551e48354ceSNicholas Bellinger static int iscsi_login_non_zero_tsih_s2(
552e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
553e48354ceSNicholas Bellinger 	unsigned char *buf)
554e48354ceSNicholas Bellinger {
555e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
556e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL, *sess_p = NULL;
557e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
558e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
559e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
56003aa2070SNicholas Bellinger 	bool iser = false;
561e48354ceSNicholas Bellinger 
562e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
563e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
564e48354ceSNicholas Bellinger 			sess_list) {
565e48354ceSNicholas Bellinger 
566e48354ceSNicholas Bellinger 		sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
567e48354ceSNicholas Bellinger 		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
568e48354ceSNicholas Bellinger 		    atomic_read(&sess_p->session_logout) ||
569e48354ceSNicholas Bellinger 		   (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
570e48354ceSNicholas Bellinger 			continue;
5718359cf43SJörn Engel 		if (!memcmp(sess_p->isid, pdu->isid, 6) &&
57250e5c87dSChristoph Hellwig 		     (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
573e48354ceSNicholas Bellinger 			iscsit_inc_session_usage_count(sess_p);
574e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess_p);
575e48354ceSNicholas Bellinger 			sess = sess_p;
576e48354ceSNicholas Bellinger 			break;
577e48354ceSNicholas Bellinger 		}
578e48354ceSNicholas Bellinger 	}
579e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
580e48354ceSNicholas Bellinger 
581e48354ceSNicholas Bellinger 	/*
582e48354ceSNicholas Bellinger 	 * If the Time2Retain handler has expired, the session is already gone.
583e48354ceSNicholas Bellinger 	 */
584e48354ceSNicholas Bellinger 	if (!sess) {
585e48354ceSNicholas Bellinger 		pr_err("Initiator attempting to add a connection to"
586e48354ceSNicholas Bellinger 			" a non-existent session, rejecting iSCSI Login.\n");
587e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
588e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_SESSION);
589e48354ceSNicholas Bellinger 		return -1;
590e48354ceSNicholas Bellinger 	}
591e48354ceSNicholas Bellinger 
592e48354ceSNicholas Bellinger 	/*
593e48354ceSNicholas Bellinger 	 * Stop the Time2Retain timer if this is a failed session, we restart
594e48354ceSNicholas Bellinger 	 * the timer if the login is not successful.
595e48354ceSNicholas Bellinger 	 */
596e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
597e48354ceSNicholas Bellinger 	if (sess->session_state == TARG_SESS_STATE_FAILED)
598e48354ceSNicholas Bellinger 		atomic_set(&sess->session_continuation, 1);
599e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
600e48354ceSNicholas Bellinger 
601e48354ceSNicholas Bellinger 	iscsi_login_set_conn_values(sess, conn, pdu->cid);
602e48354ceSNicholas Bellinger 
603e48354ceSNicholas Bellinger 	if (iscsi_copy_param_list(&conn->param_list,
60460bfcf8eSAndy Grover 			conn->tpg->param_list, 0) < 0) {
605e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
606e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
607e48354ceSNicholas Bellinger 		return -1;
608e48354ceSNicholas Bellinger 	}
609e48354ceSNicholas Bellinger 
61003aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
61103aa2070SNicholas Bellinger 		iser = true;
61203aa2070SNicholas Bellinger 
61303aa2070SNicholas Bellinger 	iscsi_set_keys_to_negotiate(conn->param_list, iser);
614e48354ceSNicholas Bellinger 	/*
615e48354ceSNicholas Bellinger 	 * Need to send TargetPortalGroupTag back in first login response
616e48354ceSNicholas Bellinger 	 * on any iSCSI connection where the Initiator provides TargetName.
617e48354ceSNicholas Bellinger 	 * See 5.3.1.  Login Phase Start
618e48354ceSNicholas Bellinger 	 *
619e48354ceSNicholas Bellinger 	 * In our case, we have already located the struct iscsi_tiqn at this point.
620e48354ceSNicholas Bellinger 	 */
621e48354ceSNicholas Bellinger 	memset(buf, 0, 32);
62260bfcf8eSAndy Grover 	sprintf(buf, "TargetPortalGroupTag=%hu", sess->tpg->tpgt);
623e48354ceSNicholas Bellinger 	if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
624e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
625e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
626e48354ceSNicholas Bellinger 		return -1;
627e48354ceSNicholas Bellinger 	}
628e48354ceSNicholas Bellinger 
629e48354ceSNicholas Bellinger 	return iscsi_login_disable_FIM_keys(conn->param_list, conn);
630e48354ceSNicholas Bellinger }
631e48354ceSNicholas Bellinger 
632e48354ceSNicholas Bellinger int iscsi_login_post_auth_non_zero_tsih(
633e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
634e48354ceSNicholas Bellinger 	u16 cid,
635e48354ceSNicholas Bellinger 	u32 exp_statsn)
636e48354ceSNicholas Bellinger {
637e48354ceSNicholas Bellinger 	struct iscsi_conn *conn_ptr = NULL;
638e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr = NULL;
639e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
640e48354ceSNicholas Bellinger 
641e48354ceSNicholas Bellinger 	/*
642e48354ceSNicholas Bellinger 	 * By following item 5 in the login table,  if we have found
643e48354ceSNicholas Bellinger 	 * an existing ISID and a valid/existing TSIH and an existing
644e48354ceSNicholas Bellinger 	 * CID we do connection reinstatement.  Currently we dont not
645e48354ceSNicholas Bellinger 	 * support it so we send back an non-zero status class to the
646e48354ceSNicholas Bellinger 	 * initiator and release the new connection.
647e48354ceSNicholas Bellinger 	 */
648e48354ceSNicholas Bellinger 	conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
649ee1b1b9cSAndy Grover 	if (conn_ptr) {
650e48354ceSNicholas Bellinger 		pr_err("Connection exists with CID %hu for %s,"
651e48354ceSNicholas Bellinger 			" performing connection reinstatement.\n",
652e48354ceSNicholas Bellinger 			conn_ptr->cid, sess->sess_ops->InitiatorName);
653e48354ceSNicholas Bellinger 
654e48354ceSNicholas Bellinger 		iscsit_connection_reinstatement_rcfr(conn_ptr);
655e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn_ptr);
656e48354ceSNicholas Bellinger 	}
657e48354ceSNicholas Bellinger 
658e48354ceSNicholas Bellinger 	/*
659e48354ceSNicholas Bellinger 	 * Check for any connection recovery entires containing CID.
660e48354ceSNicholas Bellinger 	 * We use the original ExpStatSN sent in the first login request
661e48354ceSNicholas Bellinger 	 * to acknowledge commands for the failed connection.
662e48354ceSNicholas Bellinger 	 *
663e48354ceSNicholas Bellinger 	 * Also note that an explict logout may have already been sent,
664e48354ceSNicholas Bellinger 	 * but the response may not be sent due to additional connection
665e48354ceSNicholas Bellinger 	 * loss.
666e48354ceSNicholas Bellinger 	 */
667e48354ceSNicholas Bellinger 	if (sess->sess_ops->ErrorRecoveryLevel == 2) {
668e48354ceSNicholas Bellinger 		cr = iscsit_get_inactive_connection_recovery_entry(
669e48354ceSNicholas Bellinger 				sess, cid);
670ee1b1b9cSAndy Grover 		if (cr) {
671e48354ceSNicholas Bellinger 			pr_debug("Performing implicit logout"
672e48354ceSNicholas Bellinger 				" for connection recovery on CID: %hu\n",
673e48354ceSNicholas Bellinger 					conn->cid);
674e48354ceSNicholas Bellinger 			iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
675e48354ceSNicholas Bellinger 		}
676e48354ceSNicholas Bellinger 	}
677e48354ceSNicholas Bellinger 
678e48354ceSNicholas Bellinger 	/*
679e48354ceSNicholas Bellinger 	 * Else we follow item 4 from the login table in that we have
680e48354ceSNicholas Bellinger 	 * found an existing ISID and a valid/existing TSIH and a new
681e48354ceSNicholas Bellinger 	 * CID we go ahead and continue to add a new connection to the
682e48354ceSNicholas Bellinger 	 * session.
683e48354ceSNicholas Bellinger 	 */
684e48354ceSNicholas Bellinger 	pr_debug("Adding CID %hu to existing session for %s.\n",
685e48354ceSNicholas Bellinger 			cid, sess->sess_ops->InitiatorName);
686e48354ceSNicholas Bellinger 
687e48354ceSNicholas Bellinger 	if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
688e48354ceSNicholas Bellinger 		pr_err("Adding additional connection to this session"
689e48354ceSNicholas Bellinger 			" would exceed MaxConnections %d, login failed.\n",
690e48354ceSNicholas Bellinger 				sess->sess_ops->MaxConnections);
691e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
692e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_ISID_ERROR);
693e48354ceSNicholas Bellinger 		return -1;
694e48354ceSNicholas Bellinger 	}
695e48354ceSNicholas Bellinger 
696e48354ceSNicholas Bellinger 	return 0;
697e48354ceSNicholas Bellinger }
698e48354ceSNicholas Bellinger 
699e48354ceSNicholas Bellinger static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
700e48354ceSNicholas Bellinger {
701e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
70203aa2070SNicholas Bellinger 	/*
70303aa2070SNicholas Bellinger 	 * FIXME: Unsolicitied NopIN support for ISER
70403aa2070SNicholas Bellinger 	 */
70503aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
70603aa2070SNicholas Bellinger 		return;
707e48354ceSNicholas Bellinger 
708e48354ceSNicholas Bellinger 	if (!sess->sess_ops->SessionType)
709e48354ceSNicholas Bellinger 		iscsit_start_nopin_timer(conn);
710e48354ceSNicholas Bellinger }
711e48354ceSNicholas Bellinger 
712a91eb7d9SNicholas Bellinger int iscsi_post_login_handler(
713e48354ceSNicholas Bellinger 	struct iscsi_np *np,
714e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
715e48354ceSNicholas Bellinger 	u8 zero_tsih)
716e48354ceSNicholas Bellinger {
717e48354ceSNicholas Bellinger 	int stop_timer = 0;
718e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
719e48354ceSNicholas Bellinger 	struct se_session *se_sess = sess->se_sess;
72060bfcf8eSAndy Grover 	struct iscsi_portal_group *tpg = sess->tpg;
721e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
722e48354ceSNicholas Bellinger 	struct iscsi_thread_set *ts;
723e48354ceSNicholas Bellinger 
724e48354ceSNicholas Bellinger 	iscsit_inc_conn_usage_count(conn);
725e48354ceSNicholas Bellinger 
726e48354ceSNicholas Bellinger 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
727e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_ACCEPT);
728e48354ceSNicholas Bellinger 
729e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
730e48354ceSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
731e48354ceSNicholas Bellinger 
732e48354ceSNicholas Bellinger 	iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
733e48354ceSNicholas Bellinger 	iscsit_set_sync_and_steering_values(conn);
734e48354ceSNicholas Bellinger 	/*
735e48354ceSNicholas Bellinger 	 * SCSI Initiator -> SCSI Target Port Mapping
736e48354ceSNicholas Bellinger 	 */
737e48354ceSNicholas Bellinger 	ts = iscsi_get_thread_set();
738e48354ceSNicholas Bellinger 	if (!zero_tsih) {
739e48354ceSNicholas Bellinger 		iscsi_set_session_parameters(sess->sess_ops,
740e48354ceSNicholas Bellinger 				conn->param_list, 0);
741e48354ceSNicholas Bellinger 		iscsi_release_param_list(conn->param_list);
742e48354ceSNicholas Bellinger 		conn->param_list = NULL;
743e48354ceSNicholas Bellinger 
744e48354ceSNicholas Bellinger 		spin_lock_bh(&sess->conn_lock);
745e48354ceSNicholas Bellinger 		atomic_set(&sess->session_continuation, 0);
746e48354ceSNicholas Bellinger 		if (sess->session_state == TARG_SESS_STATE_FAILED) {
747e48354ceSNicholas Bellinger 			pr_debug("Moving to"
748e48354ceSNicholas Bellinger 					" TARG_SESS_STATE_LOGGED_IN.\n");
749e48354ceSNicholas Bellinger 			sess->session_state = TARG_SESS_STATE_LOGGED_IN;
750e48354ceSNicholas Bellinger 			stop_timer = 1;
751e48354ceSNicholas Bellinger 		}
752e48354ceSNicholas Bellinger 
753e48354ceSNicholas Bellinger 		pr_debug("iSCSI Login successful on CID: %hu from %s to"
7542f9bc894SNicholas Bellinger 			" %s:%hu,%hu\n", conn->cid, conn->login_ip,
7552f9bc894SNicholas Bellinger 			conn->local_ip, conn->local_port, tpg->tpgt);
756e48354ceSNicholas Bellinger 
757e48354ceSNicholas Bellinger 		list_add_tail(&conn->conn_list, &sess->sess_conn_list);
758e48354ceSNicholas Bellinger 		atomic_inc(&sess->nconn);
759e48354ceSNicholas Bellinger 		pr_debug("Incremented iSCSI Connection count to %hu"
760e48354ceSNicholas Bellinger 			" from node: %s\n", atomic_read(&sess->nconn),
761e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
762e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
763e48354ceSNicholas Bellinger 
764e48354ceSNicholas Bellinger 		iscsi_post_login_start_timers(conn);
765baa4d64bSNicholas Bellinger 
766e48354ceSNicholas Bellinger 		iscsi_activate_thread_set(conn, ts);
767e48354ceSNicholas Bellinger 		/*
768e48354ceSNicholas Bellinger 		 * Determine CPU mask to ensure connection's RX and TX kthreads
769e48354ceSNicholas Bellinger 		 * are scheduled on the same CPU.
770e48354ceSNicholas Bellinger 		 */
771e48354ceSNicholas Bellinger 		iscsit_thread_get_cpumask(conn);
772e48354ceSNicholas Bellinger 		conn->conn_rx_reset_cpumask = 1;
773e48354ceSNicholas Bellinger 		conn->conn_tx_reset_cpumask = 1;
774e48354ceSNicholas Bellinger 
775e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn);
776e48354ceSNicholas Bellinger 		if (stop_timer) {
777e48354ceSNicholas Bellinger 			spin_lock_bh(&se_tpg->session_lock);
778e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess);
779e48354ceSNicholas Bellinger 			spin_unlock_bh(&se_tpg->session_lock);
780e48354ceSNicholas Bellinger 		}
781e48354ceSNicholas Bellinger 		iscsit_dec_session_usage_count(sess);
782e48354ceSNicholas Bellinger 		return 0;
783e48354ceSNicholas Bellinger 	}
784e48354ceSNicholas Bellinger 
785e48354ceSNicholas Bellinger 	iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
786e48354ceSNicholas Bellinger 	iscsi_release_param_list(conn->param_list);
787e48354ceSNicholas Bellinger 	conn->param_list = NULL;
788e48354ceSNicholas Bellinger 
789e48354ceSNicholas Bellinger 	iscsit_determine_maxcmdsn(sess);
790e48354ceSNicholas Bellinger 
791e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
792e48354ceSNicholas Bellinger 	__transport_register_session(&sess->tpg->tpg_se_tpg,
7938359cf43SJörn Engel 			se_sess->se_node_acl, se_sess, sess);
794e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
795e48354ceSNicholas Bellinger 	sess->session_state = TARG_SESS_STATE_LOGGED_IN;
796e48354ceSNicholas Bellinger 
797e48354ceSNicholas Bellinger 	pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
7982f9bc894SNicholas Bellinger 		conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
7992f9bc894SNicholas Bellinger 		tpg->tpgt);
800e48354ceSNicholas Bellinger 
801e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
802e48354ceSNicholas Bellinger 	list_add_tail(&conn->conn_list, &sess->sess_conn_list);
803e48354ceSNicholas Bellinger 	atomic_inc(&sess->nconn);
804e48354ceSNicholas Bellinger 	pr_debug("Incremented iSCSI Connection count to %hu from node:"
805e48354ceSNicholas Bellinger 		" %s\n", atomic_read(&sess->nconn),
806e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
807e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
808e48354ceSNicholas Bellinger 
809e48354ceSNicholas Bellinger 	sess->sid = tpg->sid++;
810e48354ceSNicholas Bellinger 	if (!sess->sid)
811e48354ceSNicholas Bellinger 		sess->sid = tpg->sid++;
812e48354ceSNicholas Bellinger 	pr_debug("Established iSCSI session from node: %s\n",
813e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
814e48354ceSNicholas Bellinger 
815e48354ceSNicholas Bellinger 	tpg->nsessions++;
816e48354ceSNicholas Bellinger 	if (tpg->tpg_tiqn)
817e48354ceSNicholas Bellinger 		tpg->tpg_tiqn->tiqn_nsessions++;
818e48354ceSNicholas Bellinger 
819e48354ceSNicholas Bellinger 	pr_debug("Incremented number of active iSCSI sessions to %u on"
820e48354ceSNicholas Bellinger 		" iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
821e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
822e48354ceSNicholas Bellinger 
823e48354ceSNicholas Bellinger 	iscsi_post_login_start_timers(conn);
824e48354ceSNicholas Bellinger 	iscsi_activate_thread_set(conn, ts);
825e48354ceSNicholas Bellinger 	/*
826e48354ceSNicholas Bellinger 	 * Determine CPU mask to ensure connection's RX and TX kthreads
827e48354ceSNicholas Bellinger 	 * are scheduled on the same CPU.
828e48354ceSNicholas Bellinger 	 */
829e48354ceSNicholas Bellinger 	iscsit_thread_get_cpumask(conn);
830e48354ceSNicholas Bellinger 	conn->conn_rx_reset_cpumask = 1;
831e48354ceSNicholas Bellinger 	conn->conn_tx_reset_cpumask = 1;
832e48354ceSNicholas Bellinger 
833e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
834e48354ceSNicholas Bellinger 
835e48354ceSNicholas Bellinger 	return 0;
836e48354ceSNicholas Bellinger }
837e48354ceSNicholas Bellinger 
838e48354ceSNicholas Bellinger static void iscsi_handle_login_thread_timeout(unsigned long data)
839e48354ceSNicholas Bellinger {
840e48354ceSNicholas Bellinger 	struct iscsi_np *np = (struct iscsi_np *) data;
841e48354ceSNicholas Bellinger 
842e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
843e48354ceSNicholas Bellinger 	pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
844e48354ceSNicholas Bellinger 			np->np_ip, np->np_port);
845e48354ceSNicholas Bellinger 
846e48354ceSNicholas Bellinger 	if (np->np_login_timer_flags & ISCSI_TF_STOP) {
847e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
848e48354ceSNicholas Bellinger 		return;
849e48354ceSNicholas Bellinger 	}
850e48354ceSNicholas Bellinger 
851e48354ceSNicholas Bellinger 	if (np->np_thread)
852e48354ceSNicholas Bellinger 		send_sig(SIGINT, np->np_thread, 1);
853e48354ceSNicholas Bellinger 
854e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
855e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
856e48354ceSNicholas Bellinger }
857e48354ceSNicholas Bellinger 
858e48354ceSNicholas Bellinger static void iscsi_start_login_thread_timer(struct iscsi_np *np)
859e48354ceSNicholas Bellinger {
860e48354ceSNicholas Bellinger 	/*
861e48354ceSNicholas Bellinger 	 * This used the TA_LOGIN_TIMEOUT constant because at this
862e48354ceSNicholas Bellinger 	 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
863e48354ceSNicholas Bellinger 	 */
864e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
865e48354ceSNicholas Bellinger 	init_timer(&np->np_login_timer);
866e48354ceSNicholas Bellinger 	np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
867e48354ceSNicholas Bellinger 	np->np_login_timer.data = (unsigned long)np;
868e48354ceSNicholas Bellinger 	np->np_login_timer.function = iscsi_handle_login_thread_timeout;
869e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_STOP;
870e48354ceSNicholas Bellinger 	np->np_login_timer_flags |= ISCSI_TF_RUNNING;
871e48354ceSNicholas Bellinger 	add_timer(&np->np_login_timer);
872e48354ceSNicholas Bellinger 
873e48354ceSNicholas Bellinger 	pr_debug("Added timeout timer to iSCSI login request for"
874e48354ceSNicholas Bellinger 			" %u seconds.\n", TA_LOGIN_TIMEOUT);
875e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
876e48354ceSNicholas Bellinger }
877e48354ceSNicholas Bellinger 
878e48354ceSNicholas Bellinger static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
879e48354ceSNicholas Bellinger {
880e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
881e48354ceSNicholas Bellinger 	if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
882e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
883e48354ceSNicholas Bellinger 		return;
884e48354ceSNicholas Bellinger 	}
885e48354ceSNicholas Bellinger 	np->np_login_timer_flags |= ISCSI_TF_STOP;
886e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
887e48354ceSNicholas Bellinger 
888e48354ceSNicholas Bellinger 	del_timer_sync(&np->np_login_timer);
889e48354ceSNicholas Bellinger 
890e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
891e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
892e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
893e48354ceSNicholas Bellinger }
894e48354ceSNicholas Bellinger 
895baa4d64bSNicholas Bellinger int iscsit_setup_np(
896e48354ceSNicholas Bellinger 	struct iscsi_np *np,
897e48354ceSNicholas Bellinger 	struct __kernel_sockaddr_storage *sockaddr)
898e48354ceSNicholas Bellinger {
899baa4d64bSNicholas Bellinger 	struct socket *sock = NULL;
900837f6452SNicholas Bellinger 	int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
901e48354ceSNicholas Bellinger 
902e48354ceSNicholas Bellinger 	switch (np->np_network_transport) {
903e48354ceSNicholas Bellinger 	case ISCSI_TCP:
904e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_TCP;
905e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_STREAM;
906e48354ceSNicholas Bellinger 		break;
907e48354ceSNicholas Bellinger 	case ISCSI_SCTP_TCP:
908e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_SCTP;
909e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_STREAM;
910e48354ceSNicholas Bellinger 		break;
911e48354ceSNicholas Bellinger 	case ISCSI_SCTP_UDP:
912e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_SCTP;
913e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_SEQPACKET;
914e48354ceSNicholas Bellinger 		break;
915e48354ceSNicholas Bellinger 	default:
916e48354ceSNicholas Bellinger 		pr_err("Unsupported network_transport: %d\n",
917e48354ceSNicholas Bellinger 				np->np_network_transport);
918e48354ceSNicholas Bellinger 		return -EINVAL;
919e48354ceSNicholas Bellinger 	}
920e48354ceSNicholas Bellinger 
921baa4d64bSNicholas Bellinger 	np->np_ip_proto = IPPROTO_TCP;
922baa4d64bSNicholas Bellinger 	np->np_sock_type = SOCK_STREAM;
923baa4d64bSNicholas Bellinger 
924e48354ceSNicholas Bellinger 	ret = sock_create(sockaddr->ss_family, np->np_sock_type,
925e48354ceSNicholas Bellinger 			np->np_ip_proto, &sock);
926e48354ceSNicholas Bellinger 	if (ret < 0) {
927e48354ceSNicholas Bellinger 		pr_err("sock_create() failed.\n");
928e48354ceSNicholas Bellinger 		return ret;
929e48354ceSNicholas Bellinger 	}
930e48354ceSNicholas Bellinger 	np->np_socket = sock;
931e48354ceSNicholas Bellinger 	/*
932e48354ceSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
933e48354ceSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
934e48354ceSNicholas Bellinger 	 */
9358359cf43SJörn Engel 	memcpy(&np->np_sockaddr, sockaddr,
936e48354ceSNicholas Bellinger 			sizeof(struct __kernel_sockaddr_storage));
937e48354ceSNicholas Bellinger 
938e48354ceSNicholas Bellinger 	if (sockaddr->ss_family == AF_INET6)
939e48354ceSNicholas Bellinger 		len = sizeof(struct sockaddr_in6);
940e48354ceSNicholas Bellinger 	else
941e48354ceSNicholas Bellinger 		len = sizeof(struct sockaddr_in);
942e48354ceSNicholas Bellinger 	/*
943e48354ceSNicholas Bellinger 	 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
944e48354ceSNicholas Bellinger 	 */
9458359cf43SJörn Engel 	/* FIXME: Someone please explain why this is endian-safe */
946e48354ceSNicholas Bellinger 	opt = 1;
947e48354ceSNicholas Bellinger 	if (np->np_network_transport == ISCSI_TCP) {
948e48354ceSNicholas Bellinger 		ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
949e48354ceSNicholas Bellinger 				(char *)&opt, sizeof(opt));
950e48354ceSNicholas Bellinger 		if (ret < 0) {
951e48354ceSNicholas Bellinger 			pr_err("kernel_setsockopt() for TCP_NODELAY"
952e48354ceSNicholas Bellinger 				" failed: %d\n", ret);
953e48354ceSNicholas Bellinger 			goto fail;
954e48354ceSNicholas Bellinger 		}
955e48354ceSNicholas Bellinger 	}
956e48354ceSNicholas Bellinger 
9578359cf43SJörn Engel 	/* FIXME: Someone please explain why this is endian-safe */
958e48354ceSNicholas Bellinger 	ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
959e48354ceSNicholas Bellinger 			(char *)&opt, sizeof(opt));
960e48354ceSNicholas Bellinger 	if (ret < 0) {
961e48354ceSNicholas Bellinger 		pr_err("kernel_setsockopt() for SO_REUSEADDR"
962e48354ceSNicholas Bellinger 			" failed\n");
963e48354ceSNicholas Bellinger 		goto fail;
964e48354ceSNicholas Bellinger 	}
965e48354ceSNicholas Bellinger 
9669f9ef6d3SDax Kelson 	ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
9679f9ef6d3SDax Kelson 			(char *)&opt, sizeof(opt));
9689f9ef6d3SDax Kelson 	if (ret < 0) {
9699f9ef6d3SDax Kelson 		pr_err("kernel_setsockopt() for IP_FREEBIND"
9709f9ef6d3SDax Kelson 			" failed\n");
9719f9ef6d3SDax Kelson 		goto fail;
9729f9ef6d3SDax Kelson 	}
9739f9ef6d3SDax Kelson 
974e48354ceSNicholas Bellinger 	ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
975e48354ceSNicholas Bellinger 	if (ret < 0) {
976e48354ceSNicholas Bellinger 		pr_err("kernel_bind() failed: %d\n", ret);
977e48354ceSNicholas Bellinger 		goto fail;
978e48354ceSNicholas Bellinger 	}
979e48354ceSNicholas Bellinger 
980e48354ceSNicholas Bellinger 	ret = kernel_listen(sock, backlog);
981e48354ceSNicholas Bellinger 	if (ret != 0) {
982e48354ceSNicholas Bellinger 		pr_err("kernel_listen() failed: %d\n", ret);
983e48354ceSNicholas Bellinger 		goto fail;
984e48354ceSNicholas Bellinger 	}
985e48354ceSNicholas Bellinger 
986e48354ceSNicholas Bellinger 	return 0;
987e48354ceSNicholas Bellinger fail:
988e48354ceSNicholas Bellinger 	np->np_socket = NULL;
989bf6932f4SAl Viro 	if (sock)
990e48354ceSNicholas Bellinger 		sock_release(sock);
991e48354ceSNicholas Bellinger 	return ret;
992e48354ceSNicholas Bellinger }
993e48354ceSNicholas Bellinger 
994baa4d64bSNicholas Bellinger int iscsi_target_setup_login_socket(
995baa4d64bSNicholas Bellinger 	struct iscsi_np *np,
996baa4d64bSNicholas Bellinger 	struct __kernel_sockaddr_storage *sockaddr)
997baa4d64bSNicholas Bellinger {
998baa4d64bSNicholas Bellinger 	struct iscsit_transport *t;
999baa4d64bSNicholas Bellinger 	int rc;
1000baa4d64bSNicholas Bellinger 
1001baa4d64bSNicholas Bellinger 	t = iscsit_get_transport(np->np_network_transport);
1002baa4d64bSNicholas Bellinger 	if (!t)
1003baa4d64bSNicholas Bellinger 		return -EINVAL;
1004baa4d64bSNicholas Bellinger 
1005baa4d64bSNicholas Bellinger 	rc = t->iscsit_setup_np(np, sockaddr);
1006baa4d64bSNicholas Bellinger 	if (rc < 0) {
1007baa4d64bSNicholas Bellinger 		iscsit_put_transport(t);
1008baa4d64bSNicholas Bellinger 		return rc;
1009baa4d64bSNicholas Bellinger 	}
1010baa4d64bSNicholas Bellinger 
1011baa4d64bSNicholas Bellinger 	np->np_transport = t;
101214f4b54fSSagi Grimberg 	np->enabled = true;
1013baa4d64bSNicholas Bellinger 	return 0;
1014baa4d64bSNicholas Bellinger }
1015baa4d64bSNicholas Bellinger 
1016baa4d64bSNicholas Bellinger int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
1017baa4d64bSNicholas Bellinger {
1018baa4d64bSNicholas Bellinger 	struct socket *new_sock, *sock = np->np_socket;
1019baa4d64bSNicholas Bellinger 	struct sockaddr_in sock_in;
1020baa4d64bSNicholas Bellinger 	struct sockaddr_in6 sock_in6;
1021baa4d64bSNicholas Bellinger 	int rc, err;
1022baa4d64bSNicholas Bellinger 
1023baa4d64bSNicholas Bellinger 	rc = kernel_accept(sock, &new_sock, 0);
1024baa4d64bSNicholas Bellinger 	if (rc < 0)
1025baa4d64bSNicholas Bellinger 		return rc;
1026baa4d64bSNicholas Bellinger 
1027baa4d64bSNicholas Bellinger 	conn->sock = new_sock;
1028baa4d64bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
1029baa4d64bSNicholas Bellinger 
1030baa4d64bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
1031baa4d64bSNicholas Bellinger 		memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1032baa4d64bSNicholas Bellinger 
1033baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
1034baa4d64bSNicholas Bellinger 				(struct sockaddr *)&sock_in6, &err, 1);
1035baa4d64bSNicholas Bellinger 		if (!rc) {
1036dfecf611SChris Leech 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1037dfecf611SChris Leech 				snprintf(conn->login_ip, sizeof(conn->login_ip), "[%pI6c]",
1038baa4d64bSNicholas Bellinger 					&sock_in6.sin6_addr.in6_u);
1039dfecf611SChris Leech 			else
1040dfecf611SChris Leech 				snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI4",
1041dfecf611SChris Leech 					&sock_in6.sin6_addr.s6_addr32[3]);
1042baa4d64bSNicholas Bellinger 			conn->login_port = ntohs(sock_in6.sin6_port);
1043baa4d64bSNicholas Bellinger 		}
1044baa4d64bSNicholas Bellinger 
1045baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
1046baa4d64bSNicholas Bellinger 				(struct sockaddr *)&sock_in6, &err, 0);
1047baa4d64bSNicholas Bellinger 		if (!rc) {
1048dfecf611SChris Leech 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1049dfecf611SChris Leech 				snprintf(conn->local_ip, sizeof(conn->local_ip), "[%pI6c]",
1050baa4d64bSNicholas Bellinger 					&sock_in6.sin6_addr.in6_u);
1051dfecf611SChris Leech 			else
1052dfecf611SChris Leech 				snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI4",
1053dfecf611SChris Leech 					&sock_in6.sin6_addr.s6_addr32[3]);
1054baa4d64bSNicholas Bellinger 			conn->local_port = ntohs(sock_in6.sin6_port);
1055baa4d64bSNicholas Bellinger 		}
1056baa4d64bSNicholas Bellinger 	} else {
1057baa4d64bSNicholas Bellinger 		memset(&sock_in, 0, sizeof(struct sockaddr_in));
1058baa4d64bSNicholas Bellinger 
1059baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
1060baa4d64bSNicholas Bellinger 				(struct sockaddr *)&sock_in, &err, 1);
1061baa4d64bSNicholas Bellinger 		if (!rc) {
1062baa4d64bSNicholas Bellinger 			sprintf(conn->login_ip, "%pI4",
1063baa4d64bSNicholas Bellinger 					&sock_in.sin_addr.s_addr);
1064baa4d64bSNicholas Bellinger 			conn->login_port = ntohs(sock_in.sin_port);
1065baa4d64bSNicholas Bellinger 		}
1066baa4d64bSNicholas Bellinger 
1067baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
1068baa4d64bSNicholas Bellinger 				(struct sockaddr *)&sock_in, &err, 0);
1069baa4d64bSNicholas Bellinger 		if (!rc) {
1070baa4d64bSNicholas Bellinger 			sprintf(conn->local_ip, "%pI4",
1071baa4d64bSNicholas Bellinger 					&sock_in.sin_addr.s_addr);
1072baa4d64bSNicholas Bellinger 			conn->local_port = ntohs(sock_in.sin_port);
1073baa4d64bSNicholas Bellinger 		}
1074baa4d64bSNicholas Bellinger 	}
1075baa4d64bSNicholas Bellinger 
1076baa4d64bSNicholas Bellinger 	return 0;
1077baa4d64bSNicholas Bellinger }
1078baa4d64bSNicholas Bellinger 
1079baa4d64bSNicholas Bellinger int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1080baa4d64bSNicholas Bellinger {
1081baa4d64bSNicholas Bellinger 	struct iscsi_login_req *login_req;
1082baa4d64bSNicholas Bellinger 	u32 padding = 0, payload_length;
1083baa4d64bSNicholas Bellinger 
1084baa4d64bSNicholas Bellinger 	if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1085baa4d64bSNicholas Bellinger 		return -1;
1086baa4d64bSNicholas Bellinger 
1087baa4d64bSNicholas Bellinger 	login_req = (struct iscsi_login_req *)login->req;
1088baa4d64bSNicholas Bellinger 	payload_length	= ntoh24(login_req->dlength);
1089baa4d64bSNicholas Bellinger 	padding = ((-payload_length) & 3);
1090baa4d64bSNicholas Bellinger 
1091baa4d64bSNicholas Bellinger 	pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1092baa4d64bSNicholas Bellinger 		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1093baa4d64bSNicholas Bellinger 		login_req->flags, login_req->itt, login_req->cmdsn,
1094baa4d64bSNicholas Bellinger 		login_req->exp_statsn, login_req->cid, payload_length);
1095baa4d64bSNicholas Bellinger 	/*
1096baa4d64bSNicholas Bellinger 	 * Setup the initial iscsi_login values from the leading
1097baa4d64bSNicholas Bellinger 	 * login request PDU.
1098baa4d64bSNicholas Bellinger 	 */
1099baa4d64bSNicholas Bellinger 	if (login->first_request) {
1100baa4d64bSNicholas Bellinger 		login_req = (struct iscsi_login_req *)login->req;
1101baa4d64bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
11023e1c81a9SNicholas Bellinger 		login->current_stage	= ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1103baa4d64bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1104baa4d64bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1105baa4d64bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1106baa4d64bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1107baa4d64bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1108baa4d64bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1109baa4d64bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1110baa4d64bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1111baa4d64bSNicholas Bellinger 	}
1112baa4d64bSNicholas Bellinger 
1113baa4d64bSNicholas Bellinger 	if (iscsi_target_check_login_request(conn, login) < 0)
1114baa4d64bSNicholas Bellinger 		return -1;
1115baa4d64bSNicholas Bellinger 
1116baa4d64bSNicholas Bellinger 	memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1117baa4d64bSNicholas Bellinger 	if (iscsi_login_rx_data(conn, login->req_buf,
1118baa4d64bSNicholas Bellinger 				payload_length + padding) < 0)
1119baa4d64bSNicholas Bellinger 		return -1;
1120baa4d64bSNicholas Bellinger 
1121baa4d64bSNicholas Bellinger 	return 0;
1122baa4d64bSNicholas Bellinger }
1123baa4d64bSNicholas Bellinger 
1124baa4d64bSNicholas Bellinger int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1125baa4d64bSNicholas Bellinger 			u32 length)
1126baa4d64bSNicholas Bellinger {
1127baa4d64bSNicholas Bellinger 	if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1128baa4d64bSNicholas Bellinger 		return -1;
1129baa4d64bSNicholas Bellinger 
1130baa4d64bSNicholas Bellinger 	return 0;
1131baa4d64bSNicholas Bellinger }
1132baa4d64bSNicholas Bellinger 
1133baa4d64bSNicholas Bellinger static int
1134baa4d64bSNicholas Bellinger iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1135baa4d64bSNicholas Bellinger {
1136baa4d64bSNicholas Bellinger 	int rc;
1137baa4d64bSNicholas Bellinger 
1138baa4d64bSNicholas Bellinger 	if (!t->owner) {
1139baa4d64bSNicholas Bellinger 		conn->conn_transport = t;
1140baa4d64bSNicholas Bellinger 		return 0;
1141baa4d64bSNicholas Bellinger 	}
1142baa4d64bSNicholas Bellinger 
1143baa4d64bSNicholas Bellinger 	rc = try_module_get(t->owner);
1144baa4d64bSNicholas Bellinger 	if (!rc) {
1145baa4d64bSNicholas Bellinger 		pr_err("try_module_get() failed for %s\n", t->name);
1146baa4d64bSNicholas Bellinger 		return -EINVAL;
1147baa4d64bSNicholas Bellinger 	}
1148baa4d64bSNicholas Bellinger 
1149baa4d64bSNicholas Bellinger 	conn->conn_transport = t;
1150baa4d64bSNicholas Bellinger 	return 0;
1151baa4d64bSNicholas Bellinger }
1152baa4d64bSNicholas Bellinger 
1153a91eb7d9SNicholas Bellinger void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1154a91eb7d9SNicholas Bellinger 		struct iscsi_np *np, bool zero_tsih, bool new_sess)
1155a91eb7d9SNicholas Bellinger {
11560bcc297eSChristophe Vu-Brugier 	if (!new_sess)
1157a91eb7d9SNicholas Bellinger 		goto old_sess_out;
1158a91eb7d9SNicholas Bellinger 
1159a91eb7d9SNicholas Bellinger 	pr_err("iSCSI Login negotiation failed.\n");
1160a91eb7d9SNicholas Bellinger 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1161a91eb7d9SNicholas Bellinger 				   ISCSI_LOGIN_STATUS_INIT_ERR);
1162a91eb7d9SNicholas Bellinger 	if (!zero_tsih || !conn->sess)
1163a91eb7d9SNicholas Bellinger 		goto old_sess_out;
1164a91eb7d9SNicholas Bellinger 	if (conn->sess->se_sess)
1165a91eb7d9SNicholas Bellinger 		transport_free_session(conn->sess->se_sess);
1166a91eb7d9SNicholas Bellinger 	if (conn->sess->session_index != 0) {
1167a91eb7d9SNicholas Bellinger 		spin_lock_bh(&sess_idr_lock);
1168a91eb7d9SNicholas Bellinger 		idr_remove(&sess_idr, conn->sess->session_index);
1169a91eb7d9SNicholas Bellinger 		spin_unlock_bh(&sess_idr_lock);
1170a91eb7d9SNicholas Bellinger 	}
1171a91eb7d9SNicholas Bellinger 	kfree(conn->sess->sess_ops);
1172a91eb7d9SNicholas Bellinger 	kfree(conn->sess);
1173a91eb7d9SNicholas Bellinger 
1174a91eb7d9SNicholas Bellinger old_sess_out:
1175a91eb7d9SNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1176a91eb7d9SNicholas Bellinger 	/*
1177a91eb7d9SNicholas Bellinger 	 * If login negotiation fails check if the Time2Retain timer
1178a91eb7d9SNicholas Bellinger 	 * needs to be restarted.
1179a91eb7d9SNicholas Bellinger 	 */
1180a91eb7d9SNicholas Bellinger 	if (!zero_tsih && conn->sess) {
1181a91eb7d9SNicholas Bellinger 		spin_lock_bh(&conn->sess->conn_lock);
1182a91eb7d9SNicholas Bellinger 		if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1183a91eb7d9SNicholas Bellinger 			struct se_portal_group *se_tpg =
118460bfcf8eSAndy Grover 					&conn->tpg->tpg_se_tpg;
1185a91eb7d9SNicholas Bellinger 
1186a91eb7d9SNicholas Bellinger 			atomic_set(&conn->sess->session_continuation, 0);
1187a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&conn->sess->conn_lock);
1188a91eb7d9SNicholas Bellinger 			spin_lock_bh(&se_tpg->session_lock);
1189a91eb7d9SNicholas Bellinger 			iscsit_start_time2retain_handler(conn->sess);
1190a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&se_tpg->session_lock);
1191a91eb7d9SNicholas Bellinger 		} else
1192a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&conn->sess->conn_lock);
1193a91eb7d9SNicholas Bellinger 		iscsit_dec_session_usage_count(conn->sess);
1194a91eb7d9SNicholas Bellinger 	}
1195a91eb7d9SNicholas Bellinger 
1196a91eb7d9SNicholas Bellinger 	if (!IS_ERR(conn->conn_rx_hash.tfm))
1197a91eb7d9SNicholas Bellinger 		crypto_free_hash(conn->conn_rx_hash.tfm);
1198a91eb7d9SNicholas Bellinger 	if (!IS_ERR(conn->conn_tx_hash.tfm))
1199a91eb7d9SNicholas Bellinger 		crypto_free_hash(conn->conn_tx_hash.tfm);
1200a91eb7d9SNicholas Bellinger 
1201a91eb7d9SNicholas Bellinger 	if (conn->conn_cpumask)
1202a91eb7d9SNicholas Bellinger 		free_cpumask_var(conn->conn_cpumask);
1203a91eb7d9SNicholas Bellinger 
1204a91eb7d9SNicholas Bellinger 	kfree(conn->conn_ops);
1205a91eb7d9SNicholas Bellinger 
1206a91eb7d9SNicholas Bellinger 	if (conn->param_list) {
1207a91eb7d9SNicholas Bellinger 		iscsi_release_param_list(conn->param_list);
1208a91eb7d9SNicholas Bellinger 		conn->param_list = NULL;
1209a91eb7d9SNicholas Bellinger 	}
1210a91eb7d9SNicholas Bellinger 	iscsi_target_nego_release(conn);
1211a91eb7d9SNicholas Bellinger 
1212a91eb7d9SNicholas Bellinger 	if (conn->sock) {
1213a91eb7d9SNicholas Bellinger 		sock_release(conn->sock);
1214a91eb7d9SNicholas Bellinger 		conn->sock = NULL;
1215a91eb7d9SNicholas Bellinger 	}
1216a91eb7d9SNicholas Bellinger 
1217a91eb7d9SNicholas Bellinger 	if (conn->conn_transport->iscsit_free_conn)
1218a91eb7d9SNicholas Bellinger 		conn->conn_transport->iscsit_free_conn(conn);
1219a91eb7d9SNicholas Bellinger 
1220a91eb7d9SNicholas Bellinger 	iscsit_put_transport(conn->conn_transport);
1221a91eb7d9SNicholas Bellinger 	kfree(conn);
1222a91eb7d9SNicholas Bellinger }
1223a91eb7d9SNicholas Bellinger 
1224e48354ceSNicholas Bellinger static int __iscsi_target_login_thread(struct iscsi_np *np)
1225e48354ceSNicholas Bellinger {
1226baa4d64bSNicholas Bellinger 	u8 *buffer, zero_tsih = 0;
1227baa4d64bSNicholas Bellinger 	int ret = 0, rc, stop;
1228e48354ceSNicholas Bellinger 	struct iscsi_conn *conn = NULL;
1229e48354ceSNicholas Bellinger 	struct iscsi_login *login;
1230e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = NULL;
1231e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu;
1232a91eb7d9SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np;
1233a91eb7d9SNicholas Bellinger 	bool new_sess = false;
1234e48354ceSNicholas Bellinger 
1235e48354ceSNicholas Bellinger 	flush_signals(current);
1236e48354ceSNicholas Bellinger 
1237e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1238e48354ceSNicholas Bellinger 	if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1239e48354ceSNicholas Bellinger 		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1240e48354ceSNicholas Bellinger 		complete(&np->np_restart_comp);
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;
1276c9a03c12SNicholas Bellinger 			if (ret == -ENODEV)
1277baa4d64bSNicholas Bellinger 				goto out;
1278baa4d64bSNicholas Bellinger 			/* Get another socket */
1279baa4d64bSNicholas Bellinger 			return 1;
1280baa4d64bSNicholas Bellinger 		}
1281baa4d64bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
1282baa4d64bSNicholas Bellinger 		iscsit_put_transport(conn->conn_transport);
1283baa4d64bSNicholas Bellinger 		kfree(conn);
1284baa4d64bSNicholas Bellinger 		conn = NULL;
1285baa4d64bSNicholas Bellinger 		goto out;
1286e48354ceSNicholas Bellinger 	}
1287e48354ceSNicholas Bellinger 	/*
1288e48354ceSNicholas Bellinger 	 * Perform the remaining iSCSI connection initialization items..
1289e48354ceSNicholas Bellinger 	 */
1290baa4d64bSNicholas Bellinger 	login = iscsi_login_init_conn(conn);
1291baa4d64bSNicholas Bellinger 	if (!login) {
1292e48354ceSNicholas Bellinger 		goto new_sess_out;
1293e48354ceSNicholas Bellinger 	}
1294e48354ceSNicholas Bellinger 
1295baa4d64bSNicholas Bellinger 	iscsi_start_login_thread_timer(np);
1296e48354ceSNicholas Bellinger 
1297baa4d64bSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1298baa4d64bSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_XPT_UP;
1299baa4d64bSNicholas Bellinger 	/*
1300baa4d64bSNicholas Bellinger 	 * This will process the first login request + payload..
1301baa4d64bSNicholas Bellinger 	 */
1302baa4d64bSNicholas Bellinger 	rc = np->np_transport->iscsit_get_login_rx(conn, login);
1303baa4d64bSNicholas Bellinger 	if (rc == 1)
1304baa4d64bSNicholas Bellinger 		return 1;
1305baa4d64bSNicholas Bellinger 	else if (rc < 0)
1306baa4d64bSNicholas Bellinger 		goto new_sess_out;
1307baa4d64bSNicholas Bellinger 
1308baa4d64bSNicholas Bellinger 	buffer = &login->req[0];
1309e48354ceSNicholas Bellinger 	pdu = (struct iscsi_login_req *)buffer;
1310e48354ceSNicholas Bellinger 	/*
1311e48354ceSNicholas Bellinger 	 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1312e48354ceSNicholas Bellinger 	 * when Status-Class != 0.
1313e48354ceSNicholas Bellinger 	*/
1314e48354ceSNicholas Bellinger 	conn->login_itt	= pdu->itt;
1315e48354ceSNicholas Bellinger 
1316e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1317e48354ceSNicholas Bellinger 	if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1318e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
1319e48354ceSNicholas Bellinger 		pr_err("iSCSI Network Portal on %s:%hu currently not"
1320e48354ceSNicholas Bellinger 			" active.\n", np->np_ip, np->np_port);
1321e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1322e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1323e48354ceSNicholas Bellinger 		goto new_sess_out;
1324e48354ceSNicholas Bellinger 	}
1325e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1326e48354ceSNicholas Bellinger 
1327e48354ceSNicholas Bellinger 	conn->network_transport = np->np_network_transport;
1328e48354ceSNicholas Bellinger 
1329e48354ceSNicholas Bellinger 	pr_debug("Received iSCSI login request from %s on %s Network"
1330baa4d64bSNicholas Bellinger 		" Portal %s:%hu\n", conn->login_ip, np->np_transport->name,
13312f9bc894SNicholas Bellinger 		conn->local_ip, conn->local_port);
1332e48354ceSNicholas Bellinger 
1333e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1334e48354ceSNicholas Bellinger 	conn->conn_state	= TARG_CONN_STATE_IN_LOGIN;
1335e48354ceSNicholas Bellinger 
1336e48354ceSNicholas Bellinger 	if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1337e48354ceSNicholas Bellinger 			pdu->min_version) < 0)
1338e48354ceSNicholas Bellinger 		goto new_sess_out;
1339e48354ceSNicholas Bellinger 
1340e48354ceSNicholas Bellinger 	zero_tsih = (pdu->tsih == 0x0000);
1341ee1b1b9cSAndy Grover 	if (zero_tsih) {
1342e48354ceSNicholas Bellinger 		/*
1343e48354ceSNicholas Bellinger 		 * This is the leading connection of a new session.
1344e48354ceSNicholas Bellinger 		 * We wait until after authentication to check for
1345e48354ceSNicholas Bellinger 		 * session reinstatement.
1346e48354ceSNicholas Bellinger 		 */
1347e48354ceSNicholas Bellinger 		if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1348e48354ceSNicholas Bellinger 			goto new_sess_out;
1349e48354ceSNicholas Bellinger 	} else {
1350e48354ceSNicholas Bellinger 		/*
1351e48354ceSNicholas Bellinger 		 * Add a new connection to an existing session.
1352e48354ceSNicholas Bellinger 		 * We check for a non-existant session in
1353e48354ceSNicholas Bellinger 		 * iscsi_login_non_zero_tsih_s2() below based
1354e48354ceSNicholas Bellinger 		 * on ISID/TSIH, but wait until after authentication
1355e48354ceSNicholas Bellinger 		 * to check for connection reinstatement, etc.
1356e48354ceSNicholas Bellinger 		 */
1357e48354ceSNicholas Bellinger 		if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1358e48354ceSNicholas Bellinger 			goto new_sess_out;
1359e48354ceSNicholas Bellinger 	}
1360e48354ceSNicholas Bellinger 	/*
1361baa4d64bSNicholas Bellinger 	 * SessionType: Discovery
1362baa4d64bSNicholas Bellinger 	 *
1363baa4d64bSNicholas Bellinger 	 * 	Locates Default Portal
1364baa4d64bSNicholas Bellinger 	 *
1365baa4d64bSNicholas Bellinger 	 * SessionType: Normal
1366baa4d64bSNicholas Bellinger 	 *
1367baa4d64bSNicholas Bellinger 	 * 	Locates Target Portal from NP -> Target IQN
1368e48354ceSNicholas Bellinger 	 */
1369baa4d64bSNicholas Bellinger 	rc = iscsi_target_locate_portal(np, conn, login);
1370baa4d64bSNicholas Bellinger 	if (rc < 0) {
1371e48354ceSNicholas Bellinger 		tpg = conn->tpg;
1372e48354ceSNicholas Bellinger 		goto new_sess_out;
1373e48354ceSNicholas Bellinger 	}
1374a91eb7d9SNicholas Bellinger 	login->zero_tsih = zero_tsih;
1375e48354ceSNicholas Bellinger 
1376e48354ceSNicholas Bellinger 	tpg = conn->tpg;
1377e48354ceSNicholas Bellinger 	if (!tpg) {
1378e48354ceSNicholas Bellinger 		pr_err("Unable to locate struct iscsi_conn->tpg\n");
1379e48354ceSNicholas Bellinger 		goto new_sess_out;
1380e48354ceSNicholas Bellinger 	}
1381e48354ceSNicholas Bellinger 
1382e48354ceSNicholas Bellinger 	if (zero_tsih) {
1383baa4d64bSNicholas Bellinger 		if (iscsi_login_zero_tsih_s2(conn) < 0)
1384e48354ceSNicholas Bellinger 			goto new_sess_out;
1385e48354ceSNicholas Bellinger 	} else {
1386baa4d64bSNicholas Bellinger 		if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1387e48354ceSNicholas Bellinger 			goto old_sess_out;
1388e48354ceSNicholas Bellinger 	}
1389e48354ceSNicholas Bellinger 
1390a91eb7d9SNicholas Bellinger 	ret = iscsi_target_start_negotiation(login, conn);
1391a91eb7d9SNicholas Bellinger 	if (ret < 0)
1392e48354ceSNicholas Bellinger 		goto new_sess_out;
1393e48354ceSNicholas Bellinger 
1394e48354ceSNicholas Bellinger 	if (!conn->sess) {
1395e48354ceSNicholas Bellinger 		pr_err("struct iscsi_conn session pointer is NULL!\n");
1396e48354ceSNicholas Bellinger 		goto new_sess_out;
1397e48354ceSNicholas Bellinger 	}
1398e48354ceSNicholas Bellinger 
1399e48354ceSNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1400e48354ceSNicholas Bellinger 
1401e48354ceSNicholas Bellinger 	if (signal_pending(current))
1402e48354ceSNicholas Bellinger 		goto new_sess_out;
1403e48354ceSNicholas Bellinger 
1404a91eb7d9SNicholas Bellinger 	if (ret == 1) {
1405a91eb7d9SNicholas Bellinger 		tpg_np = conn->tpg_np;
1406e48354ceSNicholas Bellinger 
1407a91eb7d9SNicholas Bellinger 		ret = iscsi_post_login_handler(np, conn, zero_tsih);
1408e48354ceSNicholas Bellinger 		if (ret < 0)
1409e48354ceSNicholas Bellinger 			goto new_sess_out;
1410e48354ceSNicholas Bellinger 
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 	stop = kthread_should_stop();
1434e48354ceSNicholas Bellinger 	/* Wait for another socket.. */
1435e48354ceSNicholas Bellinger 	if (!stop)
1436e48354ceSNicholas Bellinger 		return 1;
1437baa4d64bSNicholas Bellinger exit:
1438e48354ceSNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1439e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1440e48354ceSNicholas Bellinger 	np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1441e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1442baa4d64bSNicholas Bellinger 
1443e48354ceSNicholas Bellinger 	return 0;
1444e48354ceSNicholas Bellinger }
1445e48354ceSNicholas Bellinger 
1446e48354ceSNicholas Bellinger int iscsi_target_login_thread(void *arg)
1447e48354ceSNicholas Bellinger {
14488359cf43SJörn Engel 	struct iscsi_np *np = arg;
1449e48354ceSNicholas Bellinger 	int ret;
1450e48354ceSNicholas Bellinger 
1451e48354ceSNicholas Bellinger 	allow_signal(SIGINT);
1452e48354ceSNicholas Bellinger 
1453e48354ceSNicholas Bellinger 	while (!kthread_should_stop()) {
1454e48354ceSNicholas Bellinger 		ret = __iscsi_target_login_thread(np);
1455e48354ceSNicholas Bellinger 		/*
1456e48354ceSNicholas Bellinger 		 * We break and exit here unless another sock_accept() call
1457e48354ceSNicholas Bellinger 		 * is expected.
1458e48354ceSNicholas Bellinger 		 */
1459e48354ceSNicholas Bellinger 		if (ret != 1)
1460e48354ceSNicholas Bellinger 			break;
1461e48354ceSNicholas Bellinger 	}
1462e48354ceSNicholas Bellinger 
1463e48354ceSNicholas Bellinger 	return 0;
1464e48354ceSNicholas Bellinger }
1465