Revert "PM QoS: Use spinlock in the per-device PM QoS constraints code"
[firefly-linux-kernel-4.4.55.git] / drivers / target / iscsi / iscsi_target_login.c
1 /*******************************************************************************
2  * This file contains the login functions used by the iSCSI Target driver.
3  *
4  * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
5  *
6  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
7  *
8  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  ******************************************************************************/
20
21 #include <linux/string.h>
22 #include <linux/kthread.h>
23 #include <linux/crypto.h>
24 #include <linux/idr.h>
25 #include <scsi/iscsi_proto.h>
26 #include <target/target_core_base.h>
27 #include <target/target_core_fabric.h>
28
29 #include "iscsi_target_core.h"
30 #include "iscsi_target_tq.h"
31 #include "iscsi_target_device.h"
32 #include "iscsi_target_nego.h"
33 #include "iscsi_target_erl0.h"
34 #include "iscsi_target_erl2.h"
35 #include "iscsi_target_login.h"
36 #include "iscsi_target_stat.h"
37 #include "iscsi_target_tpg.h"
38 #include "iscsi_target_util.h"
39 #include "iscsi_target.h"
40 #include "iscsi_target_parameters.h"
41
42 extern struct idr sess_idr;
43 extern struct mutex auth_id_lock;
44 extern spinlock_t sess_idr_lock;
45
46 static int iscsi_login_init_conn(struct iscsi_conn *conn)
47 {
48         INIT_LIST_HEAD(&conn->conn_list);
49         INIT_LIST_HEAD(&conn->conn_cmd_list);
50         INIT_LIST_HEAD(&conn->immed_queue_list);
51         INIT_LIST_HEAD(&conn->response_queue_list);
52         init_completion(&conn->conn_post_wait_comp);
53         init_completion(&conn->conn_wait_comp);
54         init_completion(&conn->conn_wait_rcfr_comp);
55         init_completion(&conn->conn_waiting_on_uc_comp);
56         init_completion(&conn->conn_logout_comp);
57         init_completion(&conn->rx_half_close_comp);
58         init_completion(&conn->tx_half_close_comp);
59         spin_lock_init(&conn->cmd_lock);
60         spin_lock_init(&conn->conn_usage_lock);
61         spin_lock_init(&conn->immed_queue_lock);
62         spin_lock_init(&conn->nopin_timer_lock);
63         spin_lock_init(&conn->response_queue_lock);
64         spin_lock_init(&conn->state_lock);
65
66         if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
67                 pr_err("Unable to allocate conn->conn_cpumask\n");
68                 return -ENOMEM;
69         }
70
71         return 0;
72 }
73
74 /*
75  * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
76  * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
77  */
78 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
79 {
80         /*
81          * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
82          * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
83          * to software 1x8 byte slicing from crc32c.ko
84          */
85         conn->conn_rx_hash.flags = 0;
86         conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
87                                                 CRYPTO_ALG_ASYNC);
88         if (IS_ERR(conn->conn_rx_hash.tfm)) {
89                 pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
90                 return -ENOMEM;
91         }
92
93         conn->conn_tx_hash.flags = 0;
94         conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
95                                                 CRYPTO_ALG_ASYNC);
96         if (IS_ERR(conn->conn_tx_hash.tfm)) {
97                 pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
98                 crypto_free_hash(conn->conn_rx_hash.tfm);
99                 return -ENOMEM;
100         }
101
102         return 0;
103 }
104
105 static int iscsi_login_check_initiator_version(
106         struct iscsi_conn *conn,
107         u8 version_max,
108         u8 version_min)
109 {
110         if ((version_max != 0x00) || (version_min != 0x00)) {
111                 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
112                         " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
113                         version_min, version_max);
114                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
115                                 ISCSI_LOGIN_STATUS_NO_VERSION);
116                 return -1;
117         }
118
119         return 0;
120 }
121
122 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
123 {
124         int sessiontype;
125         struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
126         struct iscsi_portal_group *tpg = conn->tpg;
127         struct iscsi_session *sess = NULL, *sess_p = NULL;
128         struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
129         struct se_session *se_sess, *se_sess_tmp;
130
131         initiatorname_param = iscsi_find_param_from_key(
132                         INITIATORNAME, conn->param_list);
133         if (!initiatorname_param)
134                 return -1;
135
136         sessiontype_param = iscsi_find_param_from_key(
137                         SESSIONTYPE, conn->param_list);
138         if (!sessiontype_param)
139                 return -1;
140
141         sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
142
143         spin_lock_bh(&se_tpg->session_lock);
144         list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
145                         sess_list) {
146
147                 sess_p = se_sess->fabric_sess_ptr;
148                 spin_lock(&sess_p->conn_lock);
149                 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
150                     atomic_read(&sess_p->session_logout) ||
151                     (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
152                         spin_unlock(&sess_p->conn_lock);
153                         continue;
154                 }
155                 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
156                    (!strcmp(sess_p->sess_ops->InitiatorName,
157                             initiatorname_param->value) &&
158                    (sess_p->sess_ops->SessionType == sessiontype))) {
159                         atomic_set(&sess_p->session_reinstatement, 1);
160                         spin_unlock(&sess_p->conn_lock);
161                         iscsit_inc_session_usage_count(sess_p);
162                         iscsit_stop_time2retain_timer(sess_p);
163                         sess = sess_p;
164                         break;
165                 }
166                 spin_unlock(&sess_p->conn_lock);
167         }
168         spin_unlock_bh(&se_tpg->session_lock);
169         /*
170          * If the Time2Retain handler has expired, the session is already gone.
171          */
172         if (!sess)
173                 return 0;
174
175         pr_debug("%s iSCSI Session SID %u is still active for %s,"
176                 " preforming session reinstatement.\n", (sessiontype) ?
177                 "Discovery" : "Normal", sess->sid,
178                 sess->sess_ops->InitiatorName);
179
180         spin_lock_bh(&sess->conn_lock);
181         if (sess->session_state == TARG_SESS_STATE_FAILED) {
182                 spin_unlock_bh(&sess->conn_lock);
183                 iscsit_dec_session_usage_count(sess);
184                 target_put_session(sess->se_sess);
185                 return 0;
186         }
187         spin_unlock_bh(&sess->conn_lock);
188
189         iscsit_stop_session(sess, 1, 1);
190         iscsit_dec_session_usage_count(sess);
191
192         target_put_session(sess->se_sess);
193         return 0;
194 }
195
196 static void iscsi_login_set_conn_values(
197         struct iscsi_session *sess,
198         struct iscsi_conn *conn,
199         u16 cid)
200 {
201         conn->sess              = sess;
202         conn->cid               = cid;
203         /*
204          * Generate a random Status sequence number (statsn) for the new
205          * iSCSI connection.
206          */
207         get_random_bytes(&conn->stat_sn, sizeof(u32));
208
209         mutex_lock(&auth_id_lock);
210         conn->auth_id           = iscsit_global->auth_id++;
211         mutex_unlock(&auth_id_lock);
212 }
213
214 /*
215  *      This is the leading connection of a new session,
216  *      or session reinstatement.
217  */
218 static int iscsi_login_zero_tsih_s1(
219         struct iscsi_conn *conn,
220         unsigned char *buf)
221 {
222         struct iscsi_session *sess = NULL;
223         struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
224
225         sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
226         if (!sess) {
227                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
228                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
229                 pr_err("Could not allocate memory for session\n");
230                 return -ENOMEM;
231         }
232
233         iscsi_login_set_conn_values(sess, conn, pdu->cid);
234         sess->init_task_tag     = pdu->itt;
235         memcpy(&sess->isid, pdu->isid, 6);
236         sess->exp_cmd_sn        = pdu->cmdsn;
237         INIT_LIST_HEAD(&sess->sess_conn_list);
238         INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
239         INIT_LIST_HEAD(&sess->cr_active_list);
240         INIT_LIST_HEAD(&sess->cr_inactive_list);
241         init_completion(&sess->async_msg_comp);
242         init_completion(&sess->reinstatement_comp);
243         init_completion(&sess->session_wait_comp);
244         init_completion(&sess->session_waiting_on_uc_comp);
245         mutex_init(&sess->cmdsn_mutex);
246         spin_lock_init(&sess->conn_lock);
247         spin_lock_init(&sess->cr_a_lock);
248         spin_lock_init(&sess->cr_i_lock);
249         spin_lock_init(&sess->session_usage_lock);
250         spin_lock_init(&sess->ttt_lock);
251
252         if (!idr_pre_get(&sess_idr, GFP_KERNEL)) {
253                 pr_err("idr_pre_get() for sess_idr failed\n");
254                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
255                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
256                 kfree(sess);
257                 return -ENOMEM;
258         }
259         spin_lock(&sess_idr_lock);
260         idr_get_new(&sess_idr, NULL, &sess->session_index);
261         spin_unlock(&sess_idr_lock);
262
263         sess->creation_time = get_jiffies_64();
264         spin_lock_init(&sess->session_stats_lock);
265         /*
266          * The FFP CmdSN window values will be allocated from the TPG's
267          * Initiator Node's ACL once the login has been successfully completed.
268          */
269         sess->max_cmd_sn        = pdu->cmdsn;
270
271         sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
272         if (!sess->sess_ops) {
273                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
274                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
275                 pr_err("Unable to allocate memory for"
276                                 " struct iscsi_sess_ops.\n");
277                 kfree(sess);
278                 return -ENOMEM;
279         }
280
281         sess->se_sess = transport_init_session();
282         if (IS_ERR(sess->se_sess)) {
283                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
284                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
285                 kfree(sess);
286                 return -ENOMEM;
287         }
288
289         return 0;
290 }
291
292 static int iscsi_login_zero_tsih_s2(
293         struct iscsi_conn *conn)
294 {
295         struct iscsi_node_attrib *na;
296         struct iscsi_session *sess = conn->sess;
297         unsigned char buf[32];
298
299         sess->tpg = conn->tpg;
300
301         /*
302          * Assign a new TPG Session Handle.  Note this is protected with
303          * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
304          */
305         sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
306         if (!sess->tsih)
307                 sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
308
309         /*
310          * Create the default params from user defined values..
311          */
312         if (iscsi_copy_param_list(&conn->param_list,
313                                 ISCSI_TPG_C(conn)->param_list, 1) < 0) {
314                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
315                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
316                 return -1;
317         }
318
319         iscsi_set_keys_to_negotiate(0, conn->param_list);
320
321         if (sess->sess_ops->SessionType)
322                 return iscsi_set_keys_irrelevant_for_discovery(
323                                 conn->param_list);
324
325         na = iscsit_tpg_get_node_attrib(sess);
326
327         /*
328          * Need to send TargetPortalGroupTag back in first login response
329          * on any iSCSI connection where the Initiator provides TargetName.
330          * See 5.3.1.  Login Phase Start
331          *
332          * In our case, we have already located the struct iscsi_tiqn at this point.
333          */
334         memset(buf, 0, 32);
335         sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
336         if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
337                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
338                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
339                 return -1;
340         }
341
342         /*
343          * Workaround for Initiators that have broken connection recovery logic.
344          *
345          * "We would really like to get rid of this." Linux-iSCSI.org team
346          */
347         memset(buf, 0, 32);
348         sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl);
349         if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
350                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
351                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
352                 return -1;
353         }
354
355         if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
356                 return -1;
357
358         return 0;
359 }
360
361 /*
362  * Remove PSTATE_NEGOTIATE for the four FIM related keys.
363  * The Initiator node will be able to enable FIM by proposing them itself.
364  */
365 int iscsi_login_disable_FIM_keys(
366         struct iscsi_param_list *param_list,
367         struct iscsi_conn *conn)
368 {
369         struct iscsi_param *param;
370
371         param = iscsi_find_param_from_key("OFMarker", param_list);
372         if (!param) {
373                 pr_err("iscsi_find_param_from_key() for"
374                                 " OFMarker failed\n");
375                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
376                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
377                 return -1;
378         }
379         param->state &= ~PSTATE_NEGOTIATE;
380
381         param = iscsi_find_param_from_key("OFMarkInt", param_list);
382         if (!param) {
383                 pr_err("iscsi_find_param_from_key() for"
384                                 " IFMarker failed\n");
385                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
386                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
387                 return -1;
388         }
389         param->state &= ~PSTATE_NEGOTIATE;
390
391         param = iscsi_find_param_from_key("IFMarker", param_list);
392         if (!param) {
393                 pr_err("iscsi_find_param_from_key() for"
394                                 " IFMarker failed\n");
395                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
396                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
397                 return -1;
398         }
399         param->state &= ~PSTATE_NEGOTIATE;
400
401         param = iscsi_find_param_from_key("IFMarkInt", param_list);
402         if (!param) {
403                 pr_err("iscsi_find_param_from_key() for"
404                                 " IFMarker failed\n");
405                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
406                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
407                 return -1;
408         }
409         param->state &= ~PSTATE_NEGOTIATE;
410
411         return 0;
412 }
413
414 static int iscsi_login_non_zero_tsih_s1(
415         struct iscsi_conn *conn,
416         unsigned char *buf)
417 {
418         struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
419
420         iscsi_login_set_conn_values(NULL, conn, pdu->cid);
421         return 0;
422 }
423
424 /*
425  *      Add a new connection to an existing session.
426  */
427 static int iscsi_login_non_zero_tsih_s2(
428         struct iscsi_conn *conn,
429         unsigned char *buf)
430 {
431         struct iscsi_portal_group *tpg = conn->tpg;
432         struct iscsi_session *sess = NULL, *sess_p = NULL;
433         struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
434         struct se_session *se_sess, *se_sess_tmp;
435         struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
436
437         spin_lock_bh(&se_tpg->session_lock);
438         list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
439                         sess_list) {
440
441                 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
442                 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
443                     atomic_read(&sess_p->session_logout) ||
444                    (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
445                         continue;
446                 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
447                      (sess_p->tsih == pdu->tsih)) {
448                         iscsit_inc_session_usage_count(sess_p);
449                         iscsit_stop_time2retain_timer(sess_p);
450                         sess = sess_p;
451                         break;
452                 }
453         }
454         spin_unlock_bh(&se_tpg->session_lock);
455
456         /*
457          * If the Time2Retain handler has expired, the session is already gone.
458          */
459         if (!sess) {
460                 pr_err("Initiator attempting to add a connection to"
461                         " a non-existent session, rejecting iSCSI Login.\n");
462                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
463                                 ISCSI_LOGIN_STATUS_NO_SESSION);
464                 return -1;
465         }
466
467         /*
468          * Stop the Time2Retain timer if this is a failed session, we restart
469          * the timer if the login is not successful.
470          */
471         spin_lock_bh(&sess->conn_lock);
472         if (sess->session_state == TARG_SESS_STATE_FAILED)
473                 atomic_set(&sess->session_continuation, 1);
474         spin_unlock_bh(&sess->conn_lock);
475
476         iscsi_login_set_conn_values(sess, conn, pdu->cid);
477
478         if (iscsi_copy_param_list(&conn->param_list,
479                         ISCSI_TPG_C(conn)->param_list, 0) < 0) {
480                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
481                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
482                 return -1;
483         }
484
485         iscsi_set_keys_to_negotiate(0, conn->param_list);
486         /*
487          * Need to send TargetPortalGroupTag back in first login response
488          * on any iSCSI connection where the Initiator provides TargetName.
489          * See 5.3.1.  Login Phase Start
490          *
491          * In our case, we have already located the struct iscsi_tiqn at this point.
492          */
493         memset(buf, 0, 32);
494         sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
495         if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
496                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
497                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
498                 return -1;
499         }
500
501         return iscsi_login_disable_FIM_keys(conn->param_list, conn);
502 }
503
504 int iscsi_login_post_auth_non_zero_tsih(
505         struct iscsi_conn *conn,
506         u16 cid,
507         u32 exp_statsn)
508 {
509         struct iscsi_conn *conn_ptr = NULL;
510         struct iscsi_conn_recovery *cr = NULL;
511         struct iscsi_session *sess = conn->sess;
512
513         /*
514          * By following item 5 in the login table,  if we have found
515          * an existing ISID and a valid/existing TSIH and an existing
516          * CID we do connection reinstatement.  Currently we dont not
517          * support it so we send back an non-zero status class to the
518          * initiator and release the new connection.
519          */
520         conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
521         if (conn_ptr) {
522                 pr_err("Connection exists with CID %hu for %s,"
523                         " performing connection reinstatement.\n",
524                         conn_ptr->cid, sess->sess_ops->InitiatorName);
525
526                 iscsit_connection_reinstatement_rcfr(conn_ptr);
527                 iscsit_dec_conn_usage_count(conn_ptr);
528         }
529
530         /*
531          * Check for any connection recovery entires containing CID.
532          * We use the original ExpStatSN sent in the first login request
533          * to acknowledge commands for the failed connection.
534          *
535          * Also note that an explict logout may have already been sent,
536          * but the response may not be sent due to additional connection
537          * loss.
538          */
539         if (sess->sess_ops->ErrorRecoveryLevel == 2) {
540                 cr = iscsit_get_inactive_connection_recovery_entry(
541                                 sess, cid);
542                 if (cr) {
543                         pr_debug("Performing implicit logout"
544                                 " for connection recovery on CID: %hu\n",
545                                         conn->cid);
546                         iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
547                 }
548         }
549
550         /*
551          * Else we follow item 4 from the login table in that we have
552          * found an existing ISID and a valid/existing TSIH and a new
553          * CID we go ahead and continue to add a new connection to the
554          * session.
555          */
556         pr_debug("Adding CID %hu to existing session for %s.\n",
557                         cid, sess->sess_ops->InitiatorName);
558
559         if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
560                 pr_err("Adding additional connection to this session"
561                         " would exceed MaxConnections %d, login failed.\n",
562                                 sess->sess_ops->MaxConnections);
563                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
564                                 ISCSI_LOGIN_STATUS_ISID_ERROR);
565                 return -1;
566         }
567
568         return 0;
569 }
570
571 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
572 {
573         struct iscsi_session *sess = conn->sess;
574
575         if (!sess->sess_ops->SessionType)
576                 iscsit_start_nopin_timer(conn);
577 }
578
579 static int iscsi_post_login_handler(
580         struct iscsi_np *np,
581         struct iscsi_conn *conn,
582         u8 zero_tsih)
583 {
584         int stop_timer = 0;
585         struct iscsi_session *sess = conn->sess;
586         struct se_session *se_sess = sess->se_sess;
587         struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
588         struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
589         struct iscsi_thread_set *ts;
590
591         iscsit_inc_conn_usage_count(conn);
592
593         iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
594                         ISCSI_LOGIN_STATUS_ACCEPT);
595
596         pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
597         conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
598
599         iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
600         iscsit_set_sync_and_steering_values(conn);
601         /*
602          * SCSI Initiator -> SCSI Target Port Mapping
603          */
604         ts = iscsi_get_thread_set();
605         if (!zero_tsih) {
606                 iscsi_set_session_parameters(sess->sess_ops,
607                                 conn->param_list, 0);
608                 iscsi_release_param_list(conn->param_list);
609                 conn->param_list = NULL;
610
611                 spin_lock_bh(&sess->conn_lock);
612                 atomic_set(&sess->session_continuation, 0);
613                 if (sess->session_state == TARG_SESS_STATE_FAILED) {
614                         pr_debug("Moving to"
615                                         " TARG_SESS_STATE_LOGGED_IN.\n");
616                         sess->session_state = TARG_SESS_STATE_LOGGED_IN;
617                         stop_timer = 1;
618                 }
619
620                 pr_debug("iSCSI Login successful on CID: %hu from %s to"
621                         " %s:%hu,%hu\n", conn->cid, conn->login_ip,
622                         conn->local_ip, conn->local_port, tpg->tpgt);
623
624                 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
625                 atomic_inc(&sess->nconn);
626                 pr_debug("Incremented iSCSI Connection count to %hu"
627                         " from node: %s\n", atomic_read(&sess->nconn),
628                         sess->sess_ops->InitiatorName);
629                 spin_unlock_bh(&sess->conn_lock);
630
631                 iscsi_post_login_start_timers(conn);
632                 iscsi_activate_thread_set(conn, ts);
633                 /*
634                  * Determine CPU mask to ensure connection's RX and TX kthreads
635                  * are scheduled on the same CPU.
636                  */
637                 iscsit_thread_get_cpumask(conn);
638                 conn->conn_rx_reset_cpumask = 1;
639                 conn->conn_tx_reset_cpumask = 1;
640
641                 iscsit_dec_conn_usage_count(conn);
642                 if (stop_timer) {
643                         spin_lock_bh(&se_tpg->session_lock);
644                         iscsit_stop_time2retain_timer(sess);
645                         spin_unlock_bh(&se_tpg->session_lock);
646                 }
647                 iscsit_dec_session_usage_count(sess);
648                 return 0;
649         }
650
651         iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
652         iscsi_release_param_list(conn->param_list);
653         conn->param_list = NULL;
654
655         iscsit_determine_maxcmdsn(sess);
656
657         spin_lock_bh(&se_tpg->session_lock);
658         __transport_register_session(&sess->tpg->tpg_se_tpg,
659                         se_sess->se_node_acl, se_sess, sess);
660         pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
661         sess->session_state = TARG_SESS_STATE_LOGGED_IN;
662
663         pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
664                 conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
665                 tpg->tpgt);
666
667         spin_lock_bh(&sess->conn_lock);
668         list_add_tail(&conn->conn_list, &sess->sess_conn_list);
669         atomic_inc(&sess->nconn);
670         pr_debug("Incremented iSCSI Connection count to %hu from node:"
671                 " %s\n", atomic_read(&sess->nconn),
672                 sess->sess_ops->InitiatorName);
673         spin_unlock_bh(&sess->conn_lock);
674
675         sess->sid = tpg->sid++;
676         if (!sess->sid)
677                 sess->sid = tpg->sid++;
678         pr_debug("Established iSCSI session from node: %s\n",
679                         sess->sess_ops->InitiatorName);
680
681         tpg->nsessions++;
682         if (tpg->tpg_tiqn)
683                 tpg->tpg_tiqn->tiqn_nsessions++;
684
685         pr_debug("Incremented number of active iSCSI sessions to %u on"
686                 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
687         spin_unlock_bh(&se_tpg->session_lock);
688
689         iscsi_post_login_start_timers(conn);
690         iscsi_activate_thread_set(conn, ts);
691         /*
692          * Determine CPU mask to ensure connection's RX and TX kthreads
693          * are scheduled on the same CPU.
694          */
695         iscsit_thread_get_cpumask(conn);
696         conn->conn_rx_reset_cpumask = 1;
697         conn->conn_tx_reset_cpumask = 1;
698
699         iscsit_dec_conn_usage_count(conn);
700
701         return 0;
702 }
703
704 static void iscsi_handle_login_thread_timeout(unsigned long data)
705 {
706         struct iscsi_np *np = (struct iscsi_np *) data;
707
708         spin_lock_bh(&np->np_thread_lock);
709         pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
710                         np->np_ip, np->np_port);
711
712         if (np->np_login_timer_flags & ISCSI_TF_STOP) {
713                 spin_unlock_bh(&np->np_thread_lock);
714                 return;
715         }
716
717         if (np->np_thread)
718                 send_sig(SIGINT, np->np_thread, 1);
719
720         np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
721         spin_unlock_bh(&np->np_thread_lock);
722 }
723
724 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
725 {
726         /*
727          * This used the TA_LOGIN_TIMEOUT constant because at this
728          * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
729          */
730         spin_lock_bh(&np->np_thread_lock);
731         init_timer(&np->np_login_timer);
732         np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
733         np->np_login_timer.data = (unsigned long)np;
734         np->np_login_timer.function = iscsi_handle_login_thread_timeout;
735         np->np_login_timer_flags &= ~ISCSI_TF_STOP;
736         np->np_login_timer_flags |= ISCSI_TF_RUNNING;
737         add_timer(&np->np_login_timer);
738
739         pr_debug("Added timeout timer to iSCSI login request for"
740                         " %u seconds.\n", TA_LOGIN_TIMEOUT);
741         spin_unlock_bh(&np->np_thread_lock);
742 }
743
744 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
745 {
746         spin_lock_bh(&np->np_thread_lock);
747         if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
748                 spin_unlock_bh(&np->np_thread_lock);
749                 return;
750         }
751         np->np_login_timer_flags |= ISCSI_TF_STOP;
752         spin_unlock_bh(&np->np_thread_lock);
753
754         del_timer_sync(&np->np_login_timer);
755
756         spin_lock_bh(&np->np_thread_lock);
757         np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
758         spin_unlock_bh(&np->np_thread_lock);
759 }
760
761 int iscsi_target_setup_login_socket(
762         struct iscsi_np *np,
763         struct __kernel_sockaddr_storage *sockaddr)
764 {
765         struct socket *sock;
766         int backlog = 5, ret, opt = 0, len;
767
768         switch (np->np_network_transport) {
769         case ISCSI_TCP:
770                 np->np_ip_proto = IPPROTO_TCP;
771                 np->np_sock_type = SOCK_STREAM;
772                 break;
773         case ISCSI_SCTP_TCP:
774                 np->np_ip_proto = IPPROTO_SCTP;
775                 np->np_sock_type = SOCK_STREAM;
776                 break;
777         case ISCSI_SCTP_UDP:
778                 np->np_ip_proto = IPPROTO_SCTP;
779                 np->np_sock_type = SOCK_SEQPACKET;
780                 break;
781         case ISCSI_IWARP_TCP:
782         case ISCSI_IWARP_SCTP:
783         case ISCSI_INFINIBAND:
784         default:
785                 pr_err("Unsupported network_transport: %d\n",
786                                 np->np_network_transport);
787                 return -EINVAL;
788         }
789
790         ret = sock_create(sockaddr->ss_family, np->np_sock_type,
791                         np->np_ip_proto, &sock);
792         if (ret < 0) {
793                 pr_err("sock_create() failed.\n");
794                 return ret;
795         }
796         np->np_socket = sock;
797         /*
798          * Setup the np->np_sockaddr from the passed sockaddr setup
799          * in iscsi_target_configfs.c code..
800          */
801         memcpy(&np->np_sockaddr, sockaddr,
802                         sizeof(struct __kernel_sockaddr_storage));
803
804         if (sockaddr->ss_family == AF_INET6)
805                 len = sizeof(struct sockaddr_in6);
806         else
807                 len = sizeof(struct sockaddr_in);
808         /*
809          * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
810          */
811         /* FIXME: Someone please explain why this is endian-safe */
812         opt = 1;
813         if (np->np_network_transport == ISCSI_TCP) {
814                 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
815                                 (char *)&opt, sizeof(opt));
816                 if (ret < 0) {
817                         pr_err("kernel_setsockopt() for TCP_NODELAY"
818                                 " failed: %d\n", ret);
819                         goto fail;
820                 }
821         }
822
823         /* FIXME: Someone please explain why this is endian-safe */
824         ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
825                         (char *)&opt, sizeof(opt));
826         if (ret < 0) {
827                 pr_err("kernel_setsockopt() for SO_REUSEADDR"
828                         " failed\n");
829                 goto fail;
830         }
831
832         ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
833                         (char *)&opt, sizeof(opt));
834         if (ret < 0) {
835                 pr_err("kernel_setsockopt() for IP_FREEBIND"
836                         " failed\n");
837                 goto fail;
838         }
839
840         ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
841         if (ret < 0) {
842                 pr_err("kernel_bind() failed: %d\n", ret);
843                 goto fail;
844         }
845
846         ret = kernel_listen(sock, backlog);
847         if (ret != 0) {
848                 pr_err("kernel_listen() failed: %d\n", ret);
849                 goto fail;
850         }
851
852         return 0;
853
854 fail:
855         np->np_socket = NULL;
856         if (sock)
857                 sock_release(sock);
858         return ret;
859 }
860
861 static int __iscsi_target_login_thread(struct iscsi_np *np)
862 {
863         u8 buffer[ISCSI_HDR_LEN], iscsi_opcode, zero_tsih = 0;
864         int err, ret = 0, stop;
865         struct iscsi_conn *conn = NULL;
866         struct iscsi_login *login;
867         struct iscsi_portal_group *tpg = NULL;
868         struct socket *new_sock, *sock;
869         struct kvec iov;
870         struct iscsi_login_req *pdu;
871         struct sockaddr_in sock_in;
872         struct sockaddr_in6 sock_in6;
873
874         flush_signals(current);
875         sock = np->np_socket;
876
877         spin_lock_bh(&np->np_thread_lock);
878         if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
879                 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
880                 complete(&np->np_restart_comp);
881         } else {
882                 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
883         }
884         spin_unlock_bh(&np->np_thread_lock);
885
886         if (kernel_accept(sock, &new_sock, 0) < 0) {
887                 spin_lock_bh(&np->np_thread_lock);
888                 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
889                         spin_unlock_bh(&np->np_thread_lock);
890                         complete(&np->np_restart_comp);
891                         /* Get another socket */
892                         return 1;
893                 }
894                 spin_unlock_bh(&np->np_thread_lock);
895                 goto out;
896         }
897         iscsi_start_login_thread_timer(np);
898
899         conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
900         if (!conn) {
901                 pr_err("Could not allocate memory for"
902                         " new connection\n");
903                 sock_release(new_sock);
904                 /* Get another socket */
905                 return 1;
906         }
907
908         pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
909         conn->conn_state = TARG_CONN_STATE_FREE;
910         conn->sock = new_sock;
911
912         pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
913         conn->conn_state = TARG_CONN_STATE_XPT_UP;
914
915         /*
916          * Allocate conn->conn_ops early as a failure calling
917          * iscsit_tx_login_rsp() below will call tx_data().
918          */
919         conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
920         if (!conn->conn_ops) {
921                 pr_err("Unable to allocate memory for"
922                         " struct iscsi_conn_ops.\n");
923                 goto new_sess_out;
924         }
925         /*
926          * Perform the remaining iSCSI connection initialization items..
927          */
928         if (iscsi_login_init_conn(conn) < 0)
929                 goto new_sess_out;
930
931         memset(buffer, 0, ISCSI_HDR_LEN);
932         memset(&iov, 0, sizeof(struct kvec));
933         iov.iov_base    = buffer;
934         iov.iov_len     = ISCSI_HDR_LEN;
935
936         if (rx_data(conn, &iov, 1, ISCSI_HDR_LEN) <= 0) {
937                 pr_err("rx_data() returned an error.\n");
938                 goto new_sess_out;
939         }
940
941         iscsi_opcode = (buffer[0] & ISCSI_OPCODE_MASK);
942         if (!(iscsi_opcode & ISCSI_OP_LOGIN)) {
943                 pr_err("First opcode is not login request,"
944                         " failing login request.\n");
945                 goto new_sess_out;
946         }
947
948         pdu                     = (struct iscsi_login_req *) buffer;
949         pdu->cid                = be16_to_cpu(pdu->cid);
950         pdu->tsih               = be16_to_cpu(pdu->tsih);
951         pdu->itt                = be32_to_cpu(pdu->itt);
952         pdu->cmdsn              = be32_to_cpu(pdu->cmdsn);
953         pdu->exp_statsn         = be32_to_cpu(pdu->exp_statsn);
954         /*
955          * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
956          * when Status-Class != 0.
957         */
958         conn->login_itt         = pdu->itt;
959
960         spin_lock_bh(&np->np_thread_lock);
961         if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
962                 spin_unlock_bh(&np->np_thread_lock);
963                 pr_err("iSCSI Network Portal on %s:%hu currently not"
964                         " active.\n", np->np_ip, np->np_port);
965                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
966                                 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
967                 goto new_sess_out;
968         }
969         spin_unlock_bh(&np->np_thread_lock);
970
971         if (np->np_sockaddr.ss_family == AF_INET6) {
972                 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
973
974                 if (conn->sock->ops->getname(conn->sock,
975                                 (struct sockaddr *)&sock_in6, &err, 1) < 0) {
976                         pr_err("sock_ops->getname() failed.\n");
977                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
978                                         ISCSI_LOGIN_STATUS_TARGET_ERROR);
979                         goto new_sess_out;
980                 }
981                 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
982                                 &sock_in6.sin6_addr.in6_u);
983                 conn->login_port = ntohs(sock_in6.sin6_port);
984
985                 if (conn->sock->ops->getname(conn->sock,
986                                 (struct sockaddr *)&sock_in6, &err, 0) < 0) {
987                         pr_err("sock_ops->getname() failed.\n");
988                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
989                                         ISCSI_LOGIN_STATUS_TARGET_ERROR);
990                         goto new_sess_out;
991                 }
992                 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
993                                 &sock_in6.sin6_addr.in6_u);
994                 conn->local_port = ntohs(sock_in6.sin6_port);
995
996         } else {
997                 memset(&sock_in, 0, sizeof(struct sockaddr_in));
998
999                 if (conn->sock->ops->getname(conn->sock,
1000                                 (struct sockaddr *)&sock_in, &err, 1) < 0) {
1001                         pr_err("sock_ops->getname() failed.\n");
1002                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1003                                         ISCSI_LOGIN_STATUS_TARGET_ERROR);
1004                         goto new_sess_out;
1005                 }
1006                 sprintf(conn->login_ip, "%pI4", &sock_in.sin_addr.s_addr);
1007                 conn->login_port = ntohs(sock_in.sin_port);
1008
1009                 if (conn->sock->ops->getname(conn->sock,
1010                                 (struct sockaddr *)&sock_in, &err, 0) < 0) {
1011                         pr_err("sock_ops->getname() failed.\n");
1012                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1013                                         ISCSI_LOGIN_STATUS_TARGET_ERROR);
1014                         goto new_sess_out;
1015                 }
1016                 sprintf(conn->local_ip, "%pI4", &sock_in.sin_addr.s_addr);
1017                 conn->local_port = ntohs(sock_in.sin_port);
1018         }
1019
1020         conn->network_transport = np->np_network_transport;
1021
1022         pr_debug("Received iSCSI login request from %s on %s Network"
1023                         " Portal %s:%hu\n", conn->login_ip,
1024                 (conn->network_transport == ISCSI_TCP) ? "TCP" : "SCTP",
1025                         conn->local_ip, conn->local_port);
1026
1027         pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1028         conn->conn_state        = TARG_CONN_STATE_IN_LOGIN;
1029
1030         if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1031                         pdu->min_version) < 0)
1032                 goto new_sess_out;
1033
1034         zero_tsih = (pdu->tsih == 0x0000);
1035         if (zero_tsih) {
1036                 /*
1037                  * This is the leading connection of a new session.
1038                  * We wait until after authentication to check for
1039                  * session reinstatement.
1040                  */
1041                 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1042                         goto new_sess_out;
1043         } else {
1044                 /*
1045                  * Add a new connection to an existing session.
1046                  * We check for a non-existant session in
1047                  * iscsi_login_non_zero_tsih_s2() below based
1048                  * on ISID/TSIH, but wait until after authentication
1049                  * to check for connection reinstatement, etc.
1050                  */
1051                 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1052                         goto new_sess_out;
1053         }
1054
1055         /*
1056          * This will process the first login request, and call
1057          * iscsi_target_locate_portal(), and return a valid struct iscsi_login.
1058          */
1059         login = iscsi_target_init_negotiation(np, conn, buffer);
1060         if (!login) {
1061                 tpg = conn->tpg;
1062                 goto new_sess_out;
1063         }
1064
1065         tpg = conn->tpg;
1066         if (!tpg) {
1067                 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1068                 goto new_sess_out;
1069         }
1070
1071         if (zero_tsih) {
1072                 if (iscsi_login_zero_tsih_s2(conn) < 0) {
1073                         iscsi_target_nego_release(login, conn);
1074                         goto new_sess_out;
1075                 }
1076         } else {
1077                 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0) {
1078                         iscsi_target_nego_release(login, conn);
1079                         goto old_sess_out;
1080                 }
1081         }
1082
1083         if (iscsi_target_start_negotiation(login, conn) < 0)
1084                 goto new_sess_out;
1085
1086         if (!conn->sess) {
1087                 pr_err("struct iscsi_conn session pointer is NULL!\n");
1088                 goto new_sess_out;
1089         }
1090
1091         iscsi_stop_login_thread_timer(np);
1092
1093         if (signal_pending(current))
1094                 goto new_sess_out;
1095
1096         ret = iscsi_post_login_handler(np, conn, zero_tsih);
1097
1098         if (ret < 0)
1099                 goto new_sess_out;
1100
1101         iscsit_deaccess_np(np, tpg);
1102         tpg = NULL;
1103         /* Get another socket */
1104         return 1;
1105
1106 new_sess_out:
1107         pr_err("iSCSI Login negotiation failed.\n");
1108         iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1109                                   ISCSI_LOGIN_STATUS_INIT_ERR);
1110         if (!zero_tsih || !conn->sess)
1111                 goto old_sess_out;
1112         if (conn->sess->se_sess)
1113                 transport_free_session(conn->sess->se_sess);
1114         if (conn->sess->session_index != 0) {
1115                 spin_lock_bh(&sess_idr_lock);
1116                 idr_remove(&sess_idr, conn->sess->session_index);
1117                 spin_unlock_bh(&sess_idr_lock);
1118         }
1119         if (conn->sess->sess_ops)
1120                 kfree(conn->sess->sess_ops);
1121         if (conn->sess)
1122                 kfree(conn->sess);
1123 old_sess_out:
1124         iscsi_stop_login_thread_timer(np);
1125         /*
1126          * If login negotiation fails check if the Time2Retain timer
1127          * needs to be restarted.
1128          */
1129         if (!zero_tsih && conn->sess) {
1130                 spin_lock_bh(&conn->sess->conn_lock);
1131                 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1132                         struct se_portal_group *se_tpg =
1133                                         &ISCSI_TPG_C(conn)->tpg_se_tpg;
1134
1135                         atomic_set(&conn->sess->session_continuation, 0);
1136                         spin_unlock_bh(&conn->sess->conn_lock);
1137                         spin_lock_bh(&se_tpg->session_lock);
1138                         iscsit_start_time2retain_handler(conn->sess);
1139                         spin_unlock_bh(&se_tpg->session_lock);
1140                 } else
1141                         spin_unlock_bh(&conn->sess->conn_lock);
1142                 iscsit_dec_session_usage_count(conn->sess);
1143         }
1144
1145         if (!IS_ERR(conn->conn_rx_hash.tfm))
1146                 crypto_free_hash(conn->conn_rx_hash.tfm);
1147         if (!IS_ERR(conn->conn_tx_hash.tfm))
1148                 crypto_free_hash(conn->conn_tx_hash.tfm);
1149
1150         if (conn->conn_cpumask)
1151                 free_cpumask_var(conn->conn_cpumask);
1152
1153         kfree(conn->conn_ops);
1154
1155         if (conn->param_list) {
1156                 iscsi_release_param_list(conn->param_list);
1157                 conn->param_list = NULL;
1158         }
1159         if (conn->sock)
1160                 sock_release(conn->sock);
1161         kfree(conn);
1162
1163         if (tpg) {
1164                 iscsit_deaccess_np(np, tpg);
1165                 tpg = NULL;
1166         }
1167
1168 out:
1169         stop = kthread_should_stop();
1170         if (!stop && signal_pending(current)) {
1171                 spin_lock_bh(&np->np_thread_lock);
1172                 stop = (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN);
1173                 spin_unlock_bh(&np->np_thread_lock);
1174         }
1175         /* Wait for another socket.. */
1176         if (!stop)
1177                 return 1;
1178
1179         iscsi_stop_login_thread_timer(np);
1180         spin_lock_bh(&np->np_thread_lock);
1181         np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1182         spin_unlock_bh(&np->np_thread_lock);
1183         return 0;
1184 }
1185
1186 int iscsi_target_login_thread(void *arg)
1187 {
1188         struct iscsi_np *np = arg;
1189         int ret;
1190
1191         allow_signal(SIGINT);
1192
1193         while (!kthread_should_stop()) {
1194                 ret = __iscsi_target_login_thread(np);
1195                 /*
1196                  * We break and exit here unless another sock_accept() call
1197                  * is expected.
1198                  */
1199                 if (ret != 1)
1200                         break;
1201         }
1202
1203         return 0;
1204 }