iw_cxgb4: gracefully handle unknown CQE status errors
[linux-2.6-block.git] / drivers / target / iscsi / iscsi_target_login.c
CommitLineData
e48354ce
NB
1/*******************************************************************************
2 * This file contains the login functions used by the iSCSI Target driver.
3 *
4c76251e 4 * (c) Copyright 2007-2013 Datera, Inc.
e48354ce
NB
5 *
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
18
19#include <linux/string.h>
20#include <linux/kthread.h>
21#include <linux/crypto.h>
40401530 22#include <linux/idr.h>
e48354ce
NB
23#include <scsi/iscsi_proto.h>
24#include <target/target_core_base.h>
c4795fb2 25#include <target/target_core_fabric.h>
e48354ce 26
67f091f2
SG
27#include <target/iscsi/iscsi_target_core.h>
28#include <target/iscsi/iscsi_target_stat.h>
e48354ce
NB
29#include "iscsi_target_device.h"
30#include "iscsi_target_nego.h"
31#include "iscsi_target_erl0.h"
32#include "iscsi_target_erl2.h"
33#include "iscsi_target_login.h"
e48354ce
NB
34#include "iscsi_target_tpg.h"
35#include "iscsi_target_util.h"
36#include "iscsi_target.h"
37#include "iscsi_target_parameters.h"
38
baa4d64b
NB
39#include <target/iscsi/iscsi_transport.h>
40
41static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
e48354ce 42{
baa4d64b
NB
43 struct iscsi_login *login;
44
45 login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
46 if (!login) {
47 pr_err("Unable to allocate memory for struct iscsi_login.\n");
48 return NULL;
49 }
a91eb7d9 50 conn->login = login;
baa4d64b
NB
51 login->conn = conn;
52 login->first_request = 1;
53
54 login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
55 if (!login->req_buf) {
56 pr_err("Unable to allocate memory for response buffer.\n");
57 goto out_login;
58 }
59
60 login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
61 if (!login->rsp_buf) {
62 pr_err("Unable to allocate memory for request buffer.\n");
63 goto out_req_buf;
64 }
65
66 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
67 if (!conn->conn_ops) {
68 pr_err("Unable to allocate memory for"
69 " struct iscsi_conn_ops.\n");
70 goto out_rsp_buf;
71 }
72
d5627acb 73 init_waitqueue_head(&conn->queues_wq);
e48354ce
NB
74 INIT_LIST_HEAD(&conn->conn_list);
75 INIT_LIST_HEAD(&conn->conn_cmd_list);
76 INIT_LIST_HEAD(&conn->immed_queue_list);
77 INIT_LIST_HEAD(&conn->response_queue_list);
78 init_completion(&conn->conn_post_wait_comp);
79 init_completion(&conn->conn_wait_comp);
80 init_completion(&conn->conn_wait_rcfr_comp);
81 init_completion(&conn->conn_waiting_on_uc_comp);
82 init_completion(&conn->conn_logout_comp);
83 init_completion(&conn->rx_half_close_comp);
84 init_completion(&conn->tx_half_close_comp);
85 spin_lock_init(&conn->cmd_lock);
86 spin_lock_init(&conn->conn_usage_lock);
87 spin_lock_init(&conn->immed_queue_lock);
88 spin_lock_init(&conn->nopin_timer_lock);
89 spin_lock_init(&conn->response_queue_lock);
90 spin_lock_init(&conn->state_lock);
91
92 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
93 pr_err("Unable to allocate conn->conn_cpumask\n");
baa4d64b 94 goto out_conn_ops;
e48354ce 95 }
baa4d64b 96 conn->conn_login = login;
e48354ce 97
baa4d64b
NB
98 return login;
99
100out_conn_ops:
101 kfree(conn->conn_ops);
102out_rsp_buf:
103 kfree(login->rsp_buf);
104out_req_buf:
105 kfree(login->req_buf);
106out_login:
107 kfree(login);
108 return NULL;
e48354ce
NB
109}
110
111/*
112 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
113 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
114 */
115int iscsi_login_setup_crypto(struct iscsi_conn *conn)
116{
117 /*
118 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
119 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
120 * to software 1x8 byte slicing from crc32c.ko
121 */
122 conn->conn_rx_hash.flags = 0;
123 conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
124 CRYPTO_ALG_ASYNC);
125 if (IS_ERR(conn->conn_rx_hash.tfm)) {
126 pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
127 return -ENOMEM;
128 }
129
130 conn->conn_tx_hash.flags = 0;
131 conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
132 CRYPTO_ALG_ASYNC);
133 if (IS_ERR(conn->conn_tx_hash.tfm)) {
134 pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
135 crypto_free_hash(conn->conn_rx_hash.tfm);
136 return -ENOMEM;
137 }
138
139 return 0;
140}
141
142static int iscsi_login_check_initiator_version(
143 struct iscsi_conn *conn,
144 u8 version_max,
145 u8 version_min)
146{
147 if ((version_max != 0x00) || (version_min != 0x00)) {
148 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
149 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
150 version_min, version_max);
151 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
152 ISCSI_LOGIN_STATUS_NO_VERSION);
153 return -1;
154 }
155
156 return 0;
157}
158
159int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
160{
161 int sessiontype;
162 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
163 struct iscsi_portal_group *tpg = conn->tpg;
164 struct iscsi_session *sess = NULL, *sess_p = NULL;
165 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
166 struct se_session *se_sess, *se_sess_tmp;
167
168 initiatorname_param = iscsi_find_param_from_key(
169 INITIATORNAME, conn->param_list);
e48354ce
NB
170 sessiontype_param = iscsi_find_param_from_key(
171 SESSIONTYPE, conn->param_list);
1c5c12c6
RD
172 if (!initiatorname_param || !sessiontype_param) {
173 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
174 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
e48354ce 175 return -1;
1c5c12c6 176 }
e48354ce
NB
177
178 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
179
180 spin_lock_bh(&se_tpg->session_lock);
181 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
182 sess_list) {
183
8359cf43 184 sess_p = se_sess->fabric_sess_ptr;
e48354ce
NB
185 spin_lock(&sess_p->conn_lock);
186 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
187 atomic_read(&sess_p->session_logout) ||
188 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
189 spin_unlock(&sess_p->conn_lock);
190 continue;
191 }
8359cf43
JE
192 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
193 (!strcmp(sess_p->sess_ops->InitiatorName,
194 initiatorname_param->value) &&
e48354ce
NB
195 (sess_p->sess_ops->SessionType == sessiontype))) {
196 atomic_set(&sess_p->session_reinstatement, 1);
197 spin_unlock(&sess_p->conn_lock);
198 iscsit_inc_session_usage_count(sess_p);
199 iscsit_stop_time2retain_timer(sess_p);
200 sess = sess_p;
201 break;
202 }
203 spin_unlock(&sess_p->conn_lock);
204 }
205 spin_unlock_bh(&se_tpg->session_lock);
206 /*
207 * If the Time2Retain handler has expired, the session is already gone.
208 */
209 if (!sess)
210 return 0;
211
212 pr_debug("%s iSCSI Session SID %u is still active for %s,"
213 " preforming session reinstatement.\n", (sessiontype) ?
214 "Discovery" : "Normal", sess->sid,
215 sess->sess_ops->InitiatorName);
216
217 spin_lock_bh(&sess->conn_lock);
218 if (sess->session_state == TARG_SESS_STATE_FAILED) {
219 spin_unlock_bh(&sess->conn_lock);
220 iscsit_dec_session_usage_count(sess);
99367f01
NB
221 target_put_session(sess->se_sess);
222 return 0;
e48354ce
NB
223 }
224 spin_unlock_bh(&sess->conn_lock);
225
226 iscsit_stop_session(sess, 1, 1);
227 iscsit_dec_session_usage_count(sess);
228
99367f01
NB
229 target_put_session(sess->se_sess);
230 return 0;
e48354ce
NB
231}
232
233static void iscsi_login_set_conn_values(
234 struct iscsi_session *sess,
235 struct iscsi_conn *conn,
50e5c87d 236 __be16 cid)
e48354ce
NB
237{
238 conn->sess = sess;
50e5c87d 239 conn->cid = be16_to_cpu(cid);
e48354ce
NB
240 /*
241 * Generate a random Status sequence number (statsn) for the new
242 * iSCSI connection.
243 */
244 get_random_bytes(&conn->stat_sn, sizeof(u32));
245
246 mutex_lock(&auth_id_lock);
247 conn->auth_id = iscsit_global->auth_id++;
248 mutex_unlock(&auth_id_lock);
249}
250
79d59d08
RD
251static __printf(2, 3) int iscsi_change_param_sprintf(
252 struct iscsi_conn *conn,
253 const char *fmt, ...)
254{
255 va_list args;
256 unsigned char buf[64];
257
258 memset(buf, 0, sizeof buf);
259
260 va_start(args, fmt);
261 vsnprintf(buf, sizeof buf, fmt, args);
262 va_end(args);
263
264 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
265 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
266 ISCSI_LOGIN_STATUS_NO_RESOURCES);
267 return -1;
268 }
269
270 return 0;
271}
272
e48354ce
NB
273/*
274 * This is the leading connection of a new session,
275 * or session reinstatement.
276 */
277static int iscsi_login_zero_tsih_s1(
278 struct iscsi_conn *conn,
279 unsigned char *buf)
280{
281 struct iscsi_session *sess = NULL;
282 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
13b5533a 283 int ret;
e48354ce
NB
284
285 sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
286 if (!sess) {
287 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
288 ISCSI_LOGIN_STATUS_NO_RESOURCES);
289 pr_err("Could not allocate memory for session\n");
0957627a 290 return -ENOMEM;
e48354ce
NB
291 }
292
293 iscsi_login_set_conn_values(sess, conn, pdu->cid);
294 sess->init_task_tag = pdu->itt;
8359cf43 295 memcpy(&sess->isid, pdu->isid, 6);
50e5c87d 296 sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
e48354ce
NB
297 INIT_LIST_HEAD(&sess->sess_conn_list);
298 INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
299 INIT_LIST_HEAD(&sess->cr_active_list);
300 INIT_LIST_HEAD(&sess->cr_inactive_list);
301 init_completion(&sess->async_msg_comp);
302 init_completion(&sess->reinstatement_comp);
303 init_completion(&sess->session_wait_comp);
304 init_completion(&sess->session_waiting_on_uc_comp);
305 mutex_init(&sess->cmdsn_mutex);
306 spin_lock_init(&sess->conn_lock);
307 spin_lock_init(&sess->cr_a_lock);
308 spin_lock_init(&sess->cr_i_lock);
309 spin_lock_init(&sess->session_usage_lock);
310 spin_lock_init(&sess->ttt_lock);
311
c9365bd0 312 idr_preload(GFP_KERNEL);
998866b0 313 spin_lock_bh(&sess_idr_lock);
c9365bd0
TH
314 ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
315 if (ret >= 0)
316 sess->session_index = ret;
998866b0 317 spin_unlock_bh(&sess_idr_lock);
c9365bd0 318 idr_preload_end();
e48354ce 319
13b5533a 320 if (ret < 0) {
c9365bd0 321 pr_err("idr_alloc() for sess_idr failed\n");
13b5533a
BW
322 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
323 ISCSI_LOGIN_STATUS_NO_RESOURCES);
324 kfree(sess);
325 return -ENOMEM;
326 }
327
e48354ce 328 sess->creation_time = get_jiffies_64();
e48354ce
NB
329 /*
330 * The FFP CmdSN window values will be allocated from the TPG's
331 * Initiator Node's ACL once the login has been successfully completed.
332 */
50e5c87d 333 sess->max_cmd_sn = be32_to_cpu(pdu->cmdsn);
e48354ce
NB
334
335 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
336 if (!sess->sess_ops) {
337 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
338 ISCSI_LOGIN_STATUS_NO_RESOURCES);
339 pr_err("Unable to allocate memory for"
340 " struct iscsi_sess_ops.\n");
0957627a
NB
341 kfree(sess);
342 return -ENOMEM;
e48354ce
NB
343 }
344
23a548ee 345 sess->se_sess = transport_init_session(TARGET_PROT_NORMAL);
0957627a 346 if (IS_ERR(sess->se_sess)) {
e48354ce
NB
347 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
348 ISCSI_LOGIN_STATUS_NO_RESOURCES);
a928d28d 349 kfree(sess->sess_ops);
0957627a
NB
350 kfree(sess);
351 return -ENOMEM;
e48354ce
NB
352 }
353
354 return 0;
355}
356
357static int iscsi_login_zero_tsih_s2(
358 struct iscsi_conn *conn)
359{
360 struct iscsi_node_attrib *na;
361 struct iscsi_session *sess = conn->sess;
03aa2070 362 bool iser = false;
e48354ce
NB
363
364 sess->tpg = conn->tpg;
365
366 /*
367 * Assign a new TPG Session Handle. Note this is protected with
368 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
369 */
60bfcf8e 370 sess->tsih = ++sess->tpg->ntsih;
e48354ce 371 if (!sess->tsih)
60bfcf8e 372 sess->tsih = ++sess->tpg->ntsih;
e48354ce
NB
373
374 /*
375 * Create the default params from user defined values..
376 */
377 if (iscsi_copy_param_list(&conn->param_list,
60bfcf8e 378 conn->tpg->param_list, 1) < 0) {
e48354ce
NB
379 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
380 ISCSI_LOGIN_STATUS_NO_RESOURCES);
381 return -1;
382 }
383
03aa2070
NB
384 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
385 iser = true;
386
387 iscsi_set_keys_to_negotiate(conn->param_list, iser);
e48354ce
NB
388
389 if (sess->sess_ops->SessionType)
390 return iscsi_set_keys_irrelevant_for_discovery(
391 conn->param_list);
392
393 na = iscsit_tpg_get_node_attrib(sess);
394
395 /*
396 * Need to send TargetPortalGroupTag back in first login response
397 * on any iSCSI connection where the Initiator provides TargetName.
398 * See 5.3.1. Login Phase Start
399 *
400 * In our case, we have already located the struct iscsi_tiqn at this point.
401 */
79d59d08 402 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
e48354ce 403 return -1;
e48354ce
NB
404
405 /*
406 * Workaround for Initiators that have broken connection recovery logic.
407 *
408 * "We would really like to get rid of this." Linux-iSCSI.org team
409 */
79d59d08 410 if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
e48354ce 411 return -1;
e48354ce 412
03aa2070
NB
413 /*
414 * Set RDMAExtensions=Yes by default for iSER enabled network portals
415 */
416 if (iser) {
417 struct iscsi_param *param;
418 unsigned long mrdsl, off;
419 int rc;
420
79d59d08 421 if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
03aa2070 422 return -1;
79d59d08 423
03aa2070
NB
424 /*
425 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
426 * Immediate Data + Unsolicitied Data-OUT if necessary..
427 */
428 param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
429 conn->param_list);
430 if (!param) {
431 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
432 ISCSI_LOGIN_STATUS_NO_RESOURCES);
433 return -1;
434 }
57103d7f 435 rc = kstrtoul(param->value, 0, &mrdsl);
03aa2070
NB
436 if (rc < 0) {
437 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
438 ISCSI_LOGIN_STATUS_NO_RESOURCES);
439 return -1;
440 }
441 off = mrdsl % PAGE_SIZE;
442 if (!off)
52d0aa79 443 goto check_prot;
03aa2070
NB
444
445 if (mrdsl < PAGE_SIZE)
446 mrdsl = PAGE_SIZE;
447 else
448 mrdsl -= off;
449
450 pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
451 " to PAGE_SIZE\n", mrdsl);
452
79d59d08 453 if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
03aa2070 454 return -1;
52d0aa79
NB
455 /*
456 * ISER currently requires that ImmediateData + Unsolicited
457 * Data be disabled when protection / signature MRs are enabled.
458 */
459check_prot:
460 if (sess->se_sess->sup_prot_ops &
461 (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
462 TARGET_PROT_DOUT_INSERT)) {
463
79d59d08 464 if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
52d0aa79 465 return -1;
52d0aa79 466
79d59d08 467 if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
52d0aa79 468 return -1;
79d59d08 469
52d0aa79
NB
470 pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
471 " T10-PI enabled ISER session\n");
472 }
03aa2070 473 }
e48354ce
NB
474
475 return 0;
476}
477
e48354ce
NB
478static int iscsi_login_non_zero_tsih_s1(
479 struct iscsi_conn *conn,
480 unsigned char *buf)
481{
482 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
483
484 iscsi_login_set_conn_values(NULL, conn, pdu->cid);
485 return 0;
486}
487
488/*
489 * Add a new connection to an existing session.
490 */
491static int iscsi_login_non_zero_tsih_s2(
492 struct iscsi_conn *conn,
493 unsigned char *buf)
494{
495 struct iscsi_portal_group *tpg = conn->tpg;
496 struct iscsi_session *sess = NULL, *sess_p = NULL;
497 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
498 struct se_session *se_sess, *se_sess_tmp;
499 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
03aa2070 500 bool iser = false;
e48354ce
NB
501
502 spin_lock_bh(&se_tpg->session_lock);
503 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
504 sess_list) {
505
506 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
507 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
508 atomic_read(&sess_p->session_logout) ||
509 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
510 continue;
8359cf43 511 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
50e5c87d 512 (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
e48354ce
NB
513 iscsit_inc_session_usage_count(sess_p);
514 iscsit_stop_time2retain_timer(sess_p);
515 sess = sess_p;
516 break;
517 }
518 }
519 spin_unlock_bh(&se_tpg->session_lock);
520
521 /*
522 * If the Time2Retain handler has expired, the session is already gone.
523 */
524 if (!sess) {
525 pr_err("Initiator attempting to add a connection to"
526 " a non-existent session, rejecting iSCSI Login.\n");
527 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
528 ISCSI_LOGIN_STATUS_NO_SESSION);
529 return -1;
530 }
531
532 /*
533 * Stop the Time2Retain timer if this is a failed session, we restart
534 * the timer if the login is not successful.
535 */
536 spin_lock_bh(&sess->conn_lock);
537 if (sess->session_state == TARG_SESS_STATE_FAILED)
538 atomic_set(&sess->session_continuation, 1);
539 spin_unlock_bh(&sess->conn_lock);
540
541 iscsi_login_set_conn_values(sess, conn, pdu->cid);
542
543 if (iscsi_copy_param_list(&conn->param_list,
60bfcf8e 544 conn->tpg->param_list, 0) < 0) {
e48354ce
NB
545 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
546 ISCSI_LOGIN_STATUS_NO_RESOURCES);
547 return -1;
548 }
549
03aa2070
NB
550 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
551 iser = true;
552
553 iscsi_set_keys_to_negotiate(conn->param_list, iser);
e48354ce
NB
554 /*
555 * Need to send TargetPortalGroupTag back in first login response
556 * on any iSCSI connection where the Initiator provides TargetName.
557 * See 5.3.1. Login Phase Start
558 *
559 * In our case, we have already located the struct iscsi_tiqn at this point.
560 */
79d59d08 561 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
e48354ce 562 return -1;
e48354ce 563
c04a6091 564 return 0;
e48354ce
NB
565}
566
567int iscsi_login_post_auth_non_zero_tsih(
568 struct iscsi_conn *conn,
569 u16 cid,
570 u32 exp_statsn)
571{
572 struct iscsi_conn *conn_ptr = NULL;
573 struct iscsi_conn_recovery *cr = NULL;
574 struct iscsi_session *sess = conn->sess;
575
576 /*
577 * By following item 5 in the login table, if we have found
578 * an existing ISID and a valid/existing TSIH and an existing
579 * CID we do connection reinstatement. Currently we dont not
580 * support it so we send back an non-zero status class to the
581 * initiator and release the new connection.
582 */
583 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
ee1b1b9c 584 if (conn_ptr) {
e48354ce
NB
585 pr_err("Connection exists with CID %hu for %s,"
586 " performing connection reinstatement.\n",
587 conn_ptr->cid, sess->sess_ops->InitiatorName);
588
589 iscsit_connection_reinstatement_rcfr(conn_ptr);
590 iscsit_dec_conn_usage_count(conn_ptr);
591 }
592
593 /*
594 * Check for any connection recovery entires containing CID.
595 * We use the original ExpStatSN sent in the first login request
596 * to acknowledge commands for the failed connection.
597 *
598 * Also note that an explict logout may have already been sent,
599 * but the response may not be sent due to additional connection
600 * loss.
601 */
602 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
603 cr = iscsit_get_inactive_connection_recovery_entry(
604 sess, cid);
ee1b1b9c 605 if (cr) {
e48354ce
NB
606 pr_debug("Performing implicit logout"
607 " for connection recovery on CID: %hu\n",
608 conn->cid);
609 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
610 }
611 }
612
613 /*
614 * Else we follow item 4 from the login table in that we have
615 * found an existing ISID and a valid/existing TSIH and a new
616 * CID we go ahead and continue to add a new connection to the
617 * session.
618 */
619 pr_debug("Adding CID %hu to existing session for %s.\n",
620 cid, sess->sess_ops->InitiatorName);
621
622 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
623 pr_err("Adding additional connection to this session"
624 " would exceed MaxConnections %d, login failed.\n",
625 sess->sess_ops->MaxConnections);
626 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
627 ISCSI_LOGIN_STATUS_ISID_ERROR);
628 return -1;
629 }
630
631 return 0;
632}
633
634static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
635{
636 struct iscsi_session *sess = conn->sess;
03aa2070
NB
637 /*
638 * FIXME: Unsolicitied NopIN support for ISER
639 */
640 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
641 return;
e48354ce
NB
642
643 if (!sess->sess_ops->SessionType)
644 iscsit_start_nopin_timer(conn);
645}
646
96494916 647static int iscsit_start_kthreads(struct iscsi_conn *conn)
88dcd2da
NB
648{
649 int ret = 0;
650
651 spin_lock(&iscsit_global->ts_bitmap_lock);
652 conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
653 ISCSIT_BITMAP_BITS, get_order(1));
654 spin_unlock(&iscsit_global->ts_bitmap_lock);
655
656 if (conn->bitmap_id < 0) {
657 pr_err("bitmap_find_free_region() failed for"
658 " iscsit_start_kthreads()\n");
659 return -ENOMEM;
660 }
661
662 conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
663 "%s", ISCSI_TX_THREAD_NAME);
664 if (IS_ERR(conn->tx_thread)) {
665 pr_err("Unable to start iscsi_target_tx_thread\n");
666 ret = PTR_ERR(conn->tx_thread);
667 goto out_bitmap;
668 }
669 conn->tx_thread_active = true;
670
671 conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
672 "%s", ISCSI_RX_THREAD_NAME);
673 if (IS_ERR(conn->rx_thread)) {
674 pr_err("Unable to start iscsi_target_rx_thread\n");
675 ret = PTR_ERR(conn->rx_thread);
676 goto out_tx;
677 }
678 conn->rx_thread_active = true;
679
680 return 0;
681out_tx:
682 kthread_stop(conn->tx_thread);
683 conn->tx_thread_active = false;
684out_bitmap:
685 spin_lock(&iscsit_global->ts_bitmap_lock);
686 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
687 get_order(1));
688 spin_unlock(&iscsit_global->ts_bitmap_lock);
689 return ret;
690}
691
a91eb7d9 692int iscsi_post_login_handler(
e48354ce
NB
693 struct iscsi_np *np,
694 struct iscsi_conn *conn,
695 u8 zero_tsih)
696{
697 int stop_timer = 0;
698 struct iscsi_session *sess = conn->sess;
699 struct se_session *se_sess = sess->se_sess;
60bfcf8e 700 struct iscsi_portal_group *tpg = sess->tpg;
e48354ce 701 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
88dcd2da 702 int rc;
e48354ce
NB
703
704 iscsit_inc_conn_usage_count(conn);
705
706 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
707 ISCSI_LOGIN_STATUS_ACCEPT);
708
709 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
710 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
711
712 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
e48354ce
NB
713 /*
714 * SCSI Initiator -> SCSI Target Port Mapping
715 */
e48354ce
NB
716 if (!zero_tsih) {
717 iscsi_set_session_parameters(sess->sess_ops,
718 conn->param_list, 0);
719 iscsi_release_param_list(conn->param_list);
720 conn->param_list = NULL;
721
722 spin_lock_bh(&sess->conn_lock);
723 atomic_set(&sess->session_continuation, 0);
724 if (sess->session_state == TARG_SESS_STATE_FAILED) {
725 pr_debug("Moving to"
726 " TARG_SESS_STATE_LOGGED_IN.\n");
727 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
728 stop_timer = 1;
729 }
730
731 pr_debug("iSCSI Login successful on CID: %hu from %s to"
2f9bc894
NB
732 " %s:%hu,%hu\n", conn->cid, conn->login_ip,
733 conn->local_ip, conn->local_port, tpg->tpgt);
e48354ce
NB
734
735 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
736 atomic_inc(&sess->nconn);
737 pr_debug("Incremented iSCSI Connection count to %hu"
738 " from node: %s\n", atomic_read(&sess->nconn),
739 sess->sess_ops->InitiatorName);
740 spin_unlock_bh(&sess->conn_lock);
741
88dcd2da
NB
742 rc = iscsit_start_kthreads(conn);
743 if (rc)
744 return rc;
baa4d64b 745
88dcd2da 746 iscsi_post_login_start_timers(conn);
e48354ce
NB
747 /*
748 * Determine CPU mask to ensure connection's RX and TX kthreads
749 * are scheduled on the same CPU.
750 */
751 iscsit_thread_get_cpumask(conn);
752 conn->conn_rx_reset_cpumask = 1;
753 conn->conn_tx_reset_cpumask = 1;
754
755 iscsit_dec_conn_usage_count(conn);
756 if (stop_timer) {
757 spin_lock_bh(&se_tpg->session_lock);
758 iscsit_stop_time2retain_timer(sess);
759 spin_unlock_bh(&se_tpg->session_lock);
760 }
761 iscsit_dec_session_usage_count(sess);
762 return 0;
763 }
764
765 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
766 iscsi_release_param_list(conn->param_list);
767 conn->param_list = NULL;
768
769 iscsit_determine_maxcmdsn(sess);
770
771 spin_lock_bh(&se_tpg->session_lock);
772 __transport_register_session(&sess->tpg->tpg_se_tpg,
8359cf43 773 se_sess->se_node_acl, se_sess, sess);
e48354ce
NB
774 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
775 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
776
777 pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
2f9bc894
NB
778 conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
779 tpg->tpgt);
e48354ce
NB
780
781 spin_lock_bh(&sess->conn_lock);
782 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
783 atomic_inc(&sess->nconn);
784 pr_debug("Incremented iSCSI Connection count to %hu from node:"
785 " %s\n", atomic_read(&sess->nconn),
786 sess->sess_ops->InitiatorName);
787 spin_unlock_bh(&sess->conn_lock);
788
789 sess->sid = tpg->sid++;
790 if (!sess->sid)
791 sess->sid = tpg->sid++;
792 pr_debug("Established iSCSI session from node: %s\n",
793 sess->sess_ops->InitiatorName);
794
795 tpg->nsessions++;
796 if (tpg->tpg_tiqn)
797 tpg->tpg_tiqn->tiqn_nsessions++;
798
799 pr_debug("Incremented number of active iSCSI sessions to %u on"
800 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
801 spin_unlock_bh(&se_tpg->session_lock);
802
88dcd2da
NB
803 rc = iscsit_start_kthreads(conn);
804 if (rc)
805 return rc;
806
e48354ce 807 iscsi_post_login_start_timers(conn);
e48354ce
NB
808 /*
809 * Determine CPU mask to ensure connection's RX and TX kthreads
810 * are scheduled on the same CPU.
811 */
812 iscsit_thread_get_cpumask(conn);
813 conn->conn_rx_reset_cpumask = 1;
814 conn->conn_tx_reset_cpumask = 1;
815
816 iscsit_dec_conn_usage_count(conn);
817
818 return 0;
819}
820
821static void iscsi_handle_login_thread_timeout(unsigned long data)
822{
823 struct iscsi_np *np = (struct iscsi_np *) data;
824
825 spin_lock_bh(&np->np_thread_lock);
826 pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
827 np->np_ip, np->np_port);
828
829 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
830 spin_unlock_bh(&np->np_thread_lock);
831 return;
832 }
833
834 if (np->np_thread)
835 send_sig(SIGINT, np->np_thread, 1);
836
837 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
838 spin_unlock_bh(&np->np_thread_lock);
839}
840
841static void iscsi_start_login_thread_timer(struct iscsi_np *np)
842{
843 /*
844 * This used the TA_LOGIN_TIMEOUT constant because at this
845 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
846 */
847 spin_lock_bh(&np->np_thread_lock);
848 init_timer(&np->np_login_timer);
849 np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
850 np->np_login_timer.data = (unsigned long)np;
851 np->np_login_timer.function = iscsi_handle_login_thread_timeout;
852 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
853 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
854 add_timer(&np->np_login_timer);
855
856 pr_debug("Added timeout timer to iSCSI login request for"
857 " %u seconds.\n", TA_LOGIN_TIMEOUT);
858 spin_unlock_bh(&np->np_thread_lock);
859}
860
861static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
862{
863 spin_lock_bh(&np->np_thread_lock);
864 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
865 spin_unlock_bh(&np->np_thread_lock);
866 return;
867 }
868 np->np_login_timer_flags |= ISCSI_TF_STOP;
869 spin_unlock_bh(&np->np_thread_lock);
870
871 del_timer_sync(&np->np_login_timer);
872
873 spin_lock_bh(&np->np_thread_lock);
874 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
875 spin_unlock_bh(&np->np_thread_lock);
876}
877
baa4d64b 878int iscsit_setup_np(
e48354ce
NB
879 struct iscsi_np *np,
880 struct __kernel_sockaddr_storage *sockaddr)
881{
baa4d64b 882 struct socket *sock = NULL;
837f6452 883 int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
e48354ce
NB
884
885 switch (np->np_network_transport) {
886 case ISCSI_TCP:
887 np->np_ip_proto = IPPROTO_TCP;
888 np->np_sock_type = SOCK_STREAM;
889 break;
890 case ISCSI_SCTP_TCP:
891 np->np_ip_proto = IPPROTO_SCTP;
892 np->np_sock_type = SOCK_STREAM;
893 break;
894 case ISCSI_SCTP_UDP:
895 np->np_ip_proto = IPPROTO_SCTP;
896 np->np_sock_type = SOCK_SEQPACKET;
897 break;
e48354ce
NB
898 default:
899 pr_err("Unsupported network_transport: %d\n",
900 np->np_network_transport);
901 return -EINVAL;
902 }
903
baa4d64b
NB
904 np->np_ip_proto = IPPROTO_TCP;
905 np->np_sock_type = SOCK_STREAM;
906
e48354ce
NB
907 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
908 np->np_ip_proto, &sock);
909 if (ret < 0) {
910 pr_err("sock_create() failed.\n");
911 return ret;
912 }
913 np->np_socket = sock;
e48354ce
NB
914 /*
915 * Setup the np->np_sockaddr from the passed sockaddr setup
916 * in iscsi_target_configfs.c code..
917 */
8359cf43 918 memcpy(&np->np_sockaddr, sockaddr,
e48354ce
NB
919 sizeof(struct __kernel_sockaddr_storage));
920
921 if (sockaddr->ss_family == AF_INET6)
922 len = sizeof(struct sockaddr_in6);
923 else
924 len = sizeof(struct sockaddr_in);
925 /*
926 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
927 */
8359cf43 928 /* FIXME: Someone please explain why this is endian-safe */
e48354ce
NB
929 opt = 1;
930 if (np->np_network_transport == ISCSI_TCP) {
931 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
932 (char *)&opt, sizeof(opt));
933 if (ret < 0) {
934 pr_err("kernel_setsockopt() for TCP_NODELAY"
935 " failed: %d\n", ret);
936 goto fail;
937 }
938 }
939
8359cf43 940 /* FIXME: Someone please explain why this is endian-safe */
e48354ce
NB
941 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
942 (char *)&opt, sizeof(opt));
943 if (ret < 0) {
944 pr_err("kernel_setsockopt() for SO_REUSEADDR"
945 " failed\n");
946 goto fail;
947 }
948
9f9ef6d3
DK
949 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
950 (char *)&opt, sizeof(opt));
951 if (ret < 0) {
952 pr_err("kernel_setsockopt() for IP_FREEBIND"
953 " failed\n");
954 goto fail;
955 }
956
e48354ce
NB
957 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
958 if (ret < 0) {
959 pr_err("kernel_bind() failed: %d\n", ret);
960 goto fail;
961 }
962
963 ret = kernel_listen(sock, backlog);
964 if (ret != 0) {
965 pr_err("kernel_listen() failed: %d\n", ret);
966 goto fail;
967 }
968
969 return 0;
e48354ce
NB
970fail:
971 np->np_socket = NULL;
94e16e9c 972 sock_release(sock);
e48354ce
NB
973 return ret;
974}
975
baa4d64b
NB
976int iscsi_target_setup_login_socket(
977 struct iscsi_np *np,
978 struct __kernel_sockaddr_storage *sockaddr)
979{
980 struct iscsit_transport *t;
981 int rc;
982
983 t = iscsit_get_transport(np->np_network_transport);
984 if (!t)
985 return -EINVAL;
986
987 rc = t->iscsit_setup_np(np, sockaddr);
988 if (rc < 0) {
989 iscsit_put_transport(t);
990 return rc;
991 }
992
993 np->np_transport = t;
14f4b54f 994 np->enabled = true;
baa4d64b
NB
995 return 0;
996}
997
998int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
999{
1000 struct socket *new_sock, *sock = np->np_socket;
1001 struct sockaddr_in sock_in;
1002 struct sockaddr_in6 sock_in6;
1003 int rc, err;
1004
1005 rc = kernel_accept(sock, &new_sock, 0);
1006 if (rc < 0)
1007 return rc;
1008
1009 conn->sock = new_sock;
1010 conn->login_family = np->np_sockaddr.ss_family;
baa4d64b
NB
1011
1012 if (np->np_sockaddr.ss_family == AF_INET6) {
1013 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1014
1015 rc = conn->sock->ops->getname(conn->sock,
1016 (struct sockaddr *)&sock_in6, &err, 1);
1017 if (!rc) {
dfecf611
CL
1018 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1019 snprintf(conn->login_ip, sizeof(conn->login_ip), "[%pI6c]",
1020 &sock_in6.sin6_addr.in6_u);
1021 else
1022 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI4",
1023 &sock_in6.sin6_addr.s6_addr32[3]);
baa4d64b
NB
1024 conn->login_port = ntohs(sock_in6.sin6_port);
1025 }
1026
1027 rc = conn->sock->ops->getname(conn->sock,
1028 (struct sockaddr *)&sock_in6, &err, 0);
1029 if (!rc) {
dfecf611
CL
1030 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1031 snprintf(conn->local_ip, sizeof(conn->local_ip), "[%pI6c]",
1032 &sock_in6.sin6_addr.in6_u);
1033 else
1034 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI4",
1035 &sock_in6.sin6_addr.s6_addr32[3]);
baa4d64b
NB
1036 conn->local_port = ntohs(sock_in6.sin6_port);
1037 }
1038 } else {
1039 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1040
1041 rc = conn->sock->ops->getname(conn->sock,
1042 (struct sockaddr *)&sock_in, &err, 1);
1043 if (!rc) {
1044 sprintf(conn->login_ip, "%pI4",
1045 &sock_in.sin_addr.s_addr);
1046 conn->login_port = ntohs(sock_in.sin_port);
1047 }
1048
1049 rc = conn->sock->ops->getname(conn->sock,
1050 (struct sockaddr *)&sock_in, &err, 0);
1051 if (!rc) {
1052 sprintf(conn->local_ip, "%pI4",
1053 &sock_in.sin_addr.s_addr);
1054 conn->local_port = ntohs(sock_in.sin_port);
1055 }
1056 }
1057
1058 return 0;
1059}
1060
1061int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1062{
1063 struct iscsi_login_req *login_req;
1064 u32 padding = 0, payload_length;
1065
1066 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1067 return -1;
1068
1069 login_req = (struct iscsi_login_req *)login->req;
1070 payload_length = ntoh24(login_req->dlength);
1071 padding = ((-payload_length) & 3);
1072
1073 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1074 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1075 login_req->flags, login_req->itt, login_req->cmdsn,
1076 login_req->exp_statsn, login_req->cid, payload_length);
1077 /*
1078 * Setup the initial iscsi_login values from the leading
1079 * login request PDU.
1080 */
1081 if (login->first_request) {
1082 login_req = (struct iscsi_login_req *)login->req;
1083 login->leading_connection = (!login_req->tsih) ? 1 : 0;
3e1c81a9 1084 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
baa4d64b
NB
1085 login->version_min = login_req->min_version;
1086 login->version_max = login_req->max_version;
1087 memcpy(login->isid, login_req->isid, 6);
1088 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1089 login->init_task_tag = login_req->itt;
1090 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1091 login->cid = be16_to_cpu(login_req->cid);
1092 login->tsih = be16_to_cpu(login_req->tsih);
1093 }
1094
1095 if (iscsi_target_check_login_request(conn, login) < 0)
1096 return -1;
1097
1098 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1099 if (iscsi_login_rx_data(conn, login->req_buf,
1100 payload_length + padding) < 0)
1101 return -1;
1102
1103 return 0;
1104}
1105
1106int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1107 u32 length)
1108{
1109 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1110 return -1;
1111
1112 return 0;
1113}
1114
1115static int
1116iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1117{
1118 int rc;
1119
1120 if (!t->owner) {
1121 conn->conn_transport = t;
1122 return 0;
1123 }
1124
1125 rc = try_module_get(t->owner);
1126 if (!rc) {
1127 pr_err("try_module_get() failed for %s\n", t->name);
1128 return -EINVAL;
1129 }
1130
1131 conn->conn_transport = t;
1132 return 0;
1133}
1134
a91eb7d9
NB
1135void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1136 struct iscsi_np *np, bool zero_tsih, bool new_sess)
1137{
0bcc297e 1138 if (!new_sess)
a91eb7d9
NB
1139 goto old_sess_out;
1140
1141 pr_err("iSCSI Login negotiation failed.\n");
1142 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1143 ISCSI_LOGIN_STATUS_INIT_ERR);
1144 if (!zero_tsih || !conn->sess)
1145 goto old_sess_out;
1146 if (conn->sess->se_sess)
1147 transport_free_session(conn->sess->se_sess);
1148 if (conn->sess->session_index != 0) {
1149 spin_lock_bh(&sess_idr_lock);
1150 idr_remove(&sess_idr, conn->sess->session_index);
1151 spin_unlock_bh(&sess_idr_lock);
1152 }
1153 kfree(conn->sess->sess_ops);
1154 kfree(conn->sess);
a0b3b9b2 1155 conn->sess = NULL;
a91eb7d9
NB
1156
1157old_sess_out:
1158 iscsi_stop_login_thread_timer(np);
1159 /*
1160 * If login negotiation fails check if the Time2Retain timer
1161 * needs to be restarted.
1162 */
1163 if (!zero_tsih && conn->sess) {
1164 spin_lock_bh(&conn->sess->conn_lock);
1165 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1166 struct se_portal_group *se_tpg =
60bfcf8e 1167 &conn->tpg->tpg_se_tpg;
a91eb7d9
NB
1168
1169 atomic_set(&conn->sess->session_continuation, 0);
1170 spin_unlock_bh(&conn->sess->conn_lock);
1171 spin_lock_bh(&se_tpg->session_lock);
1172 iscsit_start_time2retain_handler(conn->sess);
1173 spin_unlock_bh(&se_tpg->session_lock);
1174 } else
1175 spin_unlock_bh(&conn->sess->conn_lock);
1176 iscsit_dec_session_usage_count(conn->sess);
1177 }
1178
1179 if (!IS_ERR(conn->conn_rx_hash.tfm))
1180 crypto_free_hash(conn->conn_rx_hash.tfm);
1181 if (!IS_ERR(conn->conn_tx_hash.tfm))
1182 crypto_free_hash(conn->conn_tx_hash.tfm);
1183
fbecb659 1184 free_cpumask_var(conn->conn_cpumask);
a91eb7d9
NB
1185
1186 kfree(conn->conn_ops);
1187
1188 if (conn->param_list) {
1189 iscsi_release_param_list(conn->param_list);
1190 conn->param_list = NULL;
1191 }
1192 iscsi_target_nego_release(conn);
1193
1194 if (conn->sock) {
1195 sock_release(conn->sock);
1196 conn->sock = NULL;
1197 }
1198
954f2372
SG
1199 if (conn->conn_transport->iscsit_wait_conn)
1200 conn->conn_transport->iscsit_wait_conn(conn);
1201
a91eb7d9
NB
1202 if (conn->conn_transport->iscsit_free_conn)
1203 conn->conn_transport->iscsit_free_conn(conn);
1204
1205 iscsit_put_transport(conn->conn_transport);
1206 kfree(conn);
1207}
1208
e48354ce
NB
1209static int __iscsi_target_login_thread(struct iscsi_np *np)
1210{
baa4d64b 1211 u8 *buffer, zero_tsih = 0;
81a9c5e7 1212 int ret = 0, rc;
e48354ce
NB
1213 struct iscsi_conn *conn = NULL;
1214 struct iscsi_login *login;
1215 struct iscsi_portal_group *tpg = NULL;
e48354ce 1216 struct iscsi_login_req *pdu;
a91eb7d9
NB
1217 struct iscsi_tpg_np *tpg_np;
1218 bool new_sess = false;
e48354ce
NB
1219
1220 flush_signals(current);
e48354ce
NB
1221
1222 spin_lock_bh(&np->np_thread_lock);
1223 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1224 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1225 complete(&np->np_restart_comp);
81a9c5e7
MP
1226 } else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
1227 spin_unlock_bh(&np->np_thread_lock);
1228 goto exit;
e48354ce
NB
1229 } else {
1230 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1231 }
1232 spin_unlock_bh(&np->np_thread_lock);
1233
e48354ce
NB
1234 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1235 if (!conn) {
1236 pr_err("Could not allocate memory for"
1237 " new connection\n");
e48354ce
NB
1238 /* Get another socket */
1239 return 1;
1240 }
e48354ce
NB
1241 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1242 conn->conn_state = TARG_CONN_STATE_FREE;
e48354ce 1243
baa4d64b
NB
1244 if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1245 kfree(conn);
1246 return 1;
1247 }
e48354ce 1248
baa4d64b
NB
1249 rc = np->np_transport->iscsit_accept_np(np, conn);
1250 if (rc == -ENOSYS) {
1251 complete(&np->np_restart_comp);
1252 iscsit_put_transport(conn->conn_transport);
1253 kfree(conn);
1254 conn = NULL;
1255 goto exit;
1256 } else if (rc < 0) {
1257 spin_lock_bh(&np->np_thread_lock);
1258 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1259 spin_unlock_bh(&np->np_thread_lock);
1260 complete(&np->np_restart_comp);
c9a03c12
NB
1261 iscsit_put_transport(conn->conn_transport);
1262 kfree(conn);
1263 conn = NULL;
baa4d64b
NB
1264 /* Get another socket */
1265 return 1;
1266 }
1267 spin_unlock_bh(&np->np_thread_lock);
1268 iscsit_put_transport(conn->conn_transport);
1269 kfree(conn);
1270 conn = NULL;
1271 goto out;
e48354ce
NB
1272 }
1273 /*
1274 * Perform the remaining iSCSI connection initialization items..
1275 */
baa4d64b
NB
1276 login = iscsi_login_init_conn(conn);
1277 if (!login) {
e48354ce
NB
1278 goto new_sess_out;
1279 }
1280
baa4d64b 1281 iscsi_start_login_thread_timer(np);
e48354ce 1282
baa4d64b
NB
1283 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1284 conn->conn_state = TARG_CONN_STATE_XPT_UP;
1285 /*
1286 * This will process the first login request + payload..
1287 */
1288 rc = np->np_transport->iscsit_get_login_rx(conn, login);
1289 if (rc == 1)
1290 return 1;
1291 else if (rc < 0)
1292 goto new_sess_out;
66c7db68 1293
baa4d64b
NB
1294 buffer = &login->req[0];
1295 pdu = (struct iscsi_login_req *)buffer;
e48354ce
NB
1296 /*
1297 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1298 * when Status-Class != 0.
1299 */
baa4d64b 1300 conn->login_itt = pdu->itt;
e48354ce
NB
1301
1302 spin_lock_bh(&np->np_thread_lock);
1303 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1304 spin_unlock_bh(&np->np_thread_lock);
1305 pr_err("iSCSI Network Portal on %s:%hu currently not"
1306 " active.\n", np->np_ip, np->np_port);
1307 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1308 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1309 goto new_sess_out;
1310 }
1311 spin_unlock_bh(&np->np_thread_lock);
1312
e48354ce
NB
1313 conn->network_transport = np->np_network_transport;
1314
1315 pr_debug("Received iSCSI login request from %s on %s Network"
baa4d64b
NB
1316 " Portal %s:%hu\n", conn->login_ip, np->np_transport->name,
1317 conn->local_ip, conn->local_port);
e48354ce
NB
1318
1319 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1320 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1321
1322 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1323 pdu->min_version) < 0)
1324 goto new_sess_out;
1325
1326 zero_tsih = (pdu->tsih == 0x0000);
ee1b1b9c 1327 if (zero_tsih) {
e48354ce
NB
1328 /*
1329 * This is the leading connection of a new session.
1330 * We wait until after authentication to check for
1331 * session reinstatement.
1332 */
1333 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1334 goto new_sess_out;
1335 } else {
1336 /*
1337 * Add a new connection to an existing session.
1338 * We check for a non-existant session in
1339 * iscsi_login_non_zero_tsih_s2() below based
1340 * on ISID/TSIH, but wait until after authentication
1341 * to check for connection reinstatement, etc.
1342 */
1343 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1344 goto new_sess_out;
1345 }
e48354ce 1346 /*
baa4d64b
NB
1347 * SessionType: Discovery
1348 *
1349 * Locates Default Portal
1350 *
1351 * SessionType: Normal
1352 *
1353 * Locates Target Portal from NP -> Target IQN
e48354ce 1354 */
baa4d64b
NB
1355 rc = iscsi_target_locate_portal(np, conn, login);
1356 if (rc < 0) {
e48354ce
NB
1357 tpg = conn->tpg;
1358 goto new_sess_out;
1359 }
a91eb7d9 1360 login->zero_tsih = zero_tsih;
e48354ce 1361
23a548ee
SG
1362 conn->sess->se_sess->sup_prot_ops =
1363 conn->conn_transport->iscsit_get_sup_prot_ops(conn);
1364
e48354ce
NB
1365 tpg = conn->tpg;
1366 if (!tpg) {
1367 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1368 goto new_sess_out;
1369 }
1370
1371 if (zero_tsih) {
baa4d64b 1372 if (iscsi_login_zero_tsih_s2(conn) < 0)
e48354ce 1373 goto new_sess_out;
e48354ce 1374 } else {
baa4d64b 1375 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
e48354ce 1376 goto old_sess_out;
e48354ce
NB
1377 }
1378
a91eb7d9
NB
1379 ret = iscsi_target_start_negotiation(login, conn);
1380 if (ret < 0)
e48354ce
NB
1381 goto new_sess_out;
1382
1383 if (!conn->sess) {
1384 pr_err("struct iscsi_conn session pointer is NULL!\n");
1385 goto new_sess_out;
1386 }
1387
1388 iscsi_stop_login_thread_timer(np);
1389
1390 if (signal_pending(current))
1391 goto new_sess_out;
1392
a91eb7d9
NB
1393 if (ret == 1) {
1394 tpg_np = conn->tpg_np;
e48354ce 1395
a91eb7d9
NB
1396 ret = iscsi_post_login_handler(np, conn, zero_tsih);
1397 if (ret < 0)
1398 goto new_sess_out;
1399
1400 iscsit_deaccess_np(np, tpg, tpg_np);
1401 }
e48354ce 1402
e48354ce 1403 tpg = NULL;
a91eb7d9 1404 tpg_np = NULL;
e48354ce
NB
1405 /* Get another socket */
1406 return 1;
1407
1408new_sess_out:
a91eb7d9 1409 new_sess = true;
e48354ce 1410old_sess_out:
a91eb7d9
NB
1411 tpg_np = conn->tpg_np;
1412 iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1413 new_sess = false;
e48354ce
NB
1414
1415 if (tpg) {
a91eb7d9 1416 iscsit_deaccess_np(np, tpg, tpg_np);
e48354ce 1417 tpg = NULL;
a91eb7d9 1418 tpg_np = NULL;
e48354ce
NB
1419 }
1420
1421out:
81a9c5e7
MP
1422 return 1;
1423
baa4d64b 1424exit:
e48354ce
NB
1425 iscsi_stop_login_thread_timer(np);
1426 spin_lock_bh(&np->np_thread_lock);
1427 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1428 spin_unlock_bh(&np->np_thread_lock);
baa4d64b 1429
e48354ce
NB
1430 return 0;
1431}
1432
1433int iscsi_target_login_thread(void *arg)
1434{
8359cf43 1435 struct iscsi_np *np = arg;
e48354ce
NB
1436 int ret;
1437
1438 allow_signal(SIGINT);
1439
81a9c5e7 1440 while (1) {
e48354ce
NB
1441 ret = __iscsi_target_login_thread(np);
1442 /*
1443 * We break and exit here unless another sock_accept() call
1444 * is expected.
1445 */
1446 if (ret != 1)
1447 break;
1448 }
1449
1450 return 0;
1451}