spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / drivers / target / iscsi / iscsi_target_login.c
blob38cb7ce8469ed36084eb739bd7336f4376f03cbd
1 /*******************************************************************************
2 * This file contains the login functions used by the iSCSI Target driver.
4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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 ******************************************************************************/
21 #include <linux/string.h>
22 #include <linux/kthread.h>
23 #include <linux/crypto.h>
24 #include <scsi/iscsi_proto.h>
25 #include <target/target_core_base.h>
26 #include <target/target_core_fabric.h>
28 #include "iscsi_target_core.h"
29 #include "iscsi_target_tq.h"
30 #include "iscsi_target_device.h"
31 #include "iscsi_target_nego.h"
32 #include "iscsi_target_erl0.h"
33 #include "iscsi_target_erl2.h"
34 #include "iscsi_target_login.h"
35 #include "iscsi_target_stat.h"
36 #include "iscsi_target_tpg.h"
37 #include "iscsi_target_util.h"
38 #include "iscsi_target.h"
39 #include "iscsi_target_parameters.h"
41 extern struct idr sess_idr;
42 extern struct mutex auth_id_lock;
43 extern spinlock_t sess_idr_lock;
45 static int iscsi_login_init_conn(struct iscsi_conn *conn)
47 INIT_LIST_HEAD(&conn->conn_list);
48 INIT_LIST_HEAD(&conn->conn_cmd_list);
49 INIT_LIST_HEAD(&conn->immed_queue_list);
50 INIT_LIST_HEAD(&conn->response_queue_list);
51 init_completion(&conn->conn_post_wait_comp);
52 init_completion(&conn->conn_wait_comp);
53 init_completion(&conn->conn_wait_rcfr_comp);
54 init_completion(&conn->conn_waiting_on_uc_comp);
55 init_completion(&conn->conn_logout_comp);
56 init_completion(&conn->rx_half_close_comp);
57 init_completion(&conn->tx_half_close_comp);
58 spin_lock_init(&conn->cmd_lock);
59 spin_lock_init(&conn->conn_usage_lock);
60 spin_lock_init(&conn->immed_queue_lock);
61 spin_lock_init(&conn->nopin_timer_lock);
62 spin_lock_init(&conn->response_queue_lock);
63 spin_lock_init(&conn->state_lock);
65 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
66 pr_err("Unable to allocate conn->conn_cpumask\n");
67 return -ENOMEM;
70 return 0;
74 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
75 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
77 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
80 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
81 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
82 * to software 1x8 byte slicing from crc32c.ko
84 conn->conn_rx_hash.flags = 0;
85 conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
86 CRYPTO_ALG_ASYNC);
87 if (IS_ERR(conn->conn_rx_hash.tfm)) {
88 pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
89 return -ENOMEM;
92 conn->conn_tx_hash.flags = 0;
93 conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
94 CRYPTO_ALG_ASYNC);
95 if (IS_ERR(conn->conn_tx_hash.tfm)) {
96 pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
97 crypto_free_hash(conn->conn_rx_hash.tfm);
98 return -ENOMEM;
101 return 0;
104 static int iscsi_login_check_initiator_version(
105 struct iscsi_conn *conn,
106 u8 version_max,
107 u8 version_min)
109 if ((version_max != 0x00) || (version_min != 0x00)) {
110 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
111 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
112 version_min, version_max);
113 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
114 ISCSI_LOGIN_STATUS_NO_VERSION);
115 return -1;
118 return 0;
121 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
123 int sessiontype;
124 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
125 struct iscsi_portal_group *tpg = conn->tpg;
126 struct iscsi_session *sess = NULL, *sess_p = NULL;
127 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
128 struct se_session *se_sess, *se_sess_tmp;
130 initiatorname_param = iscsi_find_param_from_key(
131 INITIATORNAME, conn->param_list);
132 if (!initiatorname_param)
133 return -1;
135 sessiontype_param = iscsi_find_param_from_key(
136 SESSIONTYPE, conn->param_list);
137 if (!sessiontype_param)
138 return -1;
140 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
142 spin_lock_bh(&se_tpg->session_lock);
143 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
144 sess_list) {
146 sess_p = se_sess->fabric_sess_ptr;
147 spin_lock(&sess_p->conn_lock);
148 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
149 atomic_read(&sess_p->session_logout) ||
150 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
151 spin_unlock(&sess_p->conn_lock);
152 continue;
154 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
155 (!strcmp(sess_p->sess_ops->InitiatorName,
156 initiatorname_param->value) &&
157 (sess_p->sess_ops->SessionType == sessiontype))) {
158 atomic_set(&sess_p->session_reinstatement, 1);
159 spin_unlock(&sess_p->conn_lock);
160 iscsit_inc_session_usage_count(sess_p);
161 iscsit_stop_time2retain_timer(sess_p);
162 sess = sess_p;
163 break;
165 spin_unlock(&sess_p->conn_lock);
167 spin_unlock_bh(&se_tpg->session_lock);
169 * If the Time2Retain handler has expired, the session is already gone.
171 if (!sess)
172 return 0;
174 pr_debug("%s iSCSI Session SID %u is still active for %s,"
175 " preforming session reinstatement.\n", (sessiontype) ?
176 "Discovery" : "Normal", sess->sid,
177 sess->sess_ops->InitiatorName);
179 spin_lock_bh(&sess->conn_lock);
180 if (sess->session_state == TARG_SESS_STATE_FAILED) {
181 spin_unlock_bh(&sess->conn_lock);
182 iscsit_dec_session_usage_count(sess);
183 return iscsit_close_session(sess);
185 spin_unlock_bh(&sess->conn_lock);
187 iscsit_stop_session(sess, 1, 1);
188 iscsit_dec_session_usage_count(sess);
190 return iscsit_close_session(sess);
193 static void iscsi_login_set_conn_values(
194 struct iscsi_session *sess,
195 struct iscsi_conn *conn,
196 u16 cid)
198 conn->sess = sess;
199 conn->cid = cid;
201 * Generate a random Status sequence number (statsn) for the new
202 * iSCSI connection.
204 get_random_bytes(&conn->stat_sn, sizeof(u32));
206 mutex_lock(&auth_id_lock);
207 conn->auth_id = iscsit_global->auth_id++;
208 mutex_unlock(&auth_id_lock);
212 * This is the leading connection of a new session,
213 * or session reinstatement.
215 static int iscsi_login_zero_tsih_s1(
216 struct iscsi_conn *conn,
217 unsigned char *buf)
219 struct iscsi_session *sess = NULL;
220 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
222 sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
223 if (!sess) {
224 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
225 ISCSI_LOGIN_STATUS_NO_RESOURCES);
226 pr_err("Could not allocate memory for session\n");
227 return -ENOMEM;
230 iscsi_login_set_conn_values(sess, conn, pdu->cid);
231 sess->init_task_tag = pdu->itt;
232 memcpy(&sess->isid, pdu->isid, 6);
233 sess->exp_cmd_sn = pdu->cmdsn;
234 INIT_LIST_HEAD(&sess->sess_conn_list);
235 INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
236 INIT_LIST_HEAD(&sess->cr_active_list);
237 INIT_LIST_HEAD(&sess->cr_inactive_list);
238 init_completion(&sess->async_msg_comp);
239 init_completion(&sess->reinstatement_comp);
240 init_completion(&sess->session_wait_comp);
241 init_completion(&sess->session_waiting_on_uc_comp);
242 mutex_init(&sess->cmdsn_mutex);
243 spin_lock_init(&sess->conn_lock);
244 spin_lock_init(&sess->cr_a_lock);
245 spin_lock_init(&sess->cr_i_lock);
246 spin_lock_init(&sess->session_usage_lock);
247 spin_lock_init(&sess->ttt_lock);
249 if (!idr_pre_get(&sess_idr, GFP_KERNEL)) {
250 pr_err("idr_pre_get() for sess_idr failed\n");
251 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
252 ISCSI_LOGIN_STATUS_NO_RESOURCES);
253 kfree(sess);
254 return -ENOMEM;
256 spin_lock(&sess_idr_lock);
257 idr_get_new(&sess_idr, NULL, &sess->session_index);
258 spin_unlock(&sess_idr_lock);
260 sess->creation_time = get_jiffies_64();
261 spin_lock_init(&sess->session_stats_lock);
263 * The FFP CmdSN window values will be allocated from the TPG's
264 * Initiator Node's ACL once the login has been successfully completed.
266 sess->max_cmd_sn = pdu->cmdsn;
268 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
269 if (!sess->sess_ops) {
270 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
271 ISCSI_LOGIN_STATUS_NO_RESOURCES);
272 pr_err("Unable to allocate memory for"
273 " struct iscsi_sess_ops.\n");
274 kfree(sess);
275 return -ENOMEM;
278 sess->se_sess = transport_init_session();
279 if (IS_ERR(sess->se_sess)) {
280 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
281 ISCSI_LOGIN_STATUS_NO_RESOURCES);
282 kfree(sess);
283 return -ENOMEM;
286 return 0;
289 static int iscsi_login_zero_tsih_s2(
290 struct iscsi_conn *conn)
292 struct iscsi_node_attrib *na;
293 struct iscsi_session *sess = conn->sess;
294 unsigned char buf[32];
296 sess->tpg = conn->tpg;
299 * Assign a new TPG Session Handle. Note this is protected with
300 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
302 sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
303 if (!sess->tsih)
304 sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
307 * Create the default params from user defined values..
309 if (iscsi_copy_param_list(&conn->param_list,
310 ISCSI_TPG_C(conn)->param_list, 1) < 0) {
311 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
312 ISCSI_LOGIN_STATUS_NO_RESOURCES);
313 return -1;
316 iscsi_set_keys_to_negotiate(0, conn->param_list);
318 if (sess->sess_ops->SessionType)
319 return iscsi_set_keys_irrelevant_for_discovery(
320 conn->param_list);
322 na = iscsit_tpg_get_node_attrib(sess);
325 * Need to send TargetPortalGroupTag back in first login response
326 * on any iSCSI connection where the Initiator provides TargetName.
327 * See 5.3.1. Login Phase Start
329 * In our case, we have already located the struct iscsi_tiqn at this point.
331 memset(buf, 0, 32);
332 sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
333 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
334 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
335 ISCSI_LOGIN_STATUS_NO_RESOURCES);
336 return -1;
340 * Workaround for Initiators that have broken connection recovery logic.
342 * "We would really like to get rid of this." Linux-iSCSI.org team
344 memset(buf, 0, 32);
345 sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl);
346 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
347 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
348 ISCSI_LOGIN_STATUS_NO_RESOURCES);
349 return -1;
352 if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
353 return -1;
355 return 0;
359 * Remove PSTATE_NEGOTIATE for the four FIM related keys.
360 * The Initiator node will be able to enable FIM by proposing them itself.
362 int iscsi_login_disable_FIM_keys(
363 struct iscsi_param_list *param_list,
364 struct iscsi_conn *conn)
366 struct iscsi_param *param;
368 param = iscsi_find_param_from_key("OFMarker", param_list);
369 if (!param) {
370 pr_err("iscsi_find_param_from_key() for"
371 " OFMarker failed\n");
372 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
373 ISCSI_LOGIN_STATUS_NO_RESOURCES);
374 return -1;
376 param->state &= ~PSTATE_NEGOTIATE;
378 param = iscsi_find_param_from_key("OFMarkInt", param_list);
379 if (!param) {
380 pr_err("iscsi_find_param_from_key() for"
381 " IFMarker failed\n");
382 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
383 ISCSI_LOGIN_STATUS_NO_RESOURCES);
384 return -1;
386 param->state &= ~PSTATE_NEGOTIATE;
388 param = iscsi_find_param_from_key("IFMarker", param_list);
389 if (!param) {
390 pr_err("iscsi_find_param_from_key() for"
391 " IFMarker failed\n");
392 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
393 ISCSI_LOGIN_STATUS_NO_RESOURCES);
394 return -1;
396 param->state &= ~PSTATE_NEGOTIATE;
398 param = iscsi_find_param_from_key("IFMarkInt", param_list);
399 if (!param) {
400 pr_err("iscsi_find_param_from_key() for"
401 " IFMarker failed\n");
402 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
403 ISCSI_LOGIN_STATUS_NO_RESOURCES);
404 return -1;
406 param->state &= ~PSTATE_NEGOTIATE;
408 return 0;
411 static int iscsi_login_non_zero_tsih_s1(
412 struct iscsi_conn *conn,
413 unsigned char *buf)
415 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
417 iscsi_login_set_conn_values(NULL, conn, pdu->cid);
418 return 0;
422 * Add a new connection to an existing session.
424 static int iscsi_login_non_zero_tsih_s2(
425 struct iscsi_conn *conn,
426 unsigned char *buf)
428 struct iscsi_portal_group *tpg = conn->tpg;
429 struct iscsi_session *sess = NULL, *sess_p = NULL;
430 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
431 struct se_session *se_sess, *se_sess_tmp;
432 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
434 spin_lock_bh(&se_tpg->session_lock);
435 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
436 sess_list) {
438 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
439 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
440 atomic_read(&sess_p->session_logout) ||
441 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
442 continue;
443 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
444 (sess_p->tsih == pdu->tsih)) {
445 iscsit_inc_session_usage_count(sess_p);
446 iscsit_stop_time2retain_timer(sess_p);
447 sess = sess_p;
448 break;
451 spin_unlock_bh(&se_tpg->session_lock);
454 * If the Time2Retain handler has expired, the session is already gone.
456 if (!sess) {
457 pr_err("Initiator attempting to add a connection to"
458 " a non-existent session, rejecting iSCSI Login.\n");
459 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
460 ISCSI_LOGIN_STATUS_NO_SESSION);
461 return -1;
465 * Stop the Time2Retain timer if this is a failed session, we restart
466 * the timer if the login is not successful.
468 spin_lock_bh(&sess->conn_lock);
469 if (sess->session_state == TARG_SESS_STATE_FAILED)
470 atomic_set(&sess->session_continuation, 1);
471 spin_unlock_bh(&sess->conn_lock);
473 iscsi_login_set_conn_values(sess, conn, pdu->cid);
475 if (iscsi_copy_param_list(&conn->param_list,
476 ISCSI_TPG_C(conn)->param_list, 0) < 0) {
477 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
478 ISCSI_LOGIN_STATUS_NO_RESOURCES);
479 return -1;
482 iscsi_set_keys_to_negotiate(0, conn->param_list);
484 * Need to send TargetPortalGroupTag back in first login response
485 * on any iSCSI connection where the Initiator provides TargetName.
486 * See 5.3.1. Login Phase Start
488 * In our case, we have already located the struct iscsi_tiqn at this point.
490 memset(buf, 0, 32);
491 sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
492 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
493 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
494 ISCSI_LOGIN_STATUS_NO_RESOURCES);
495 return -1;
498 return iscsi_login_disable_FIM_keys(conn->param_list, conn);
501 int iscsi_login_post_auth_non_zero_tsih(
502 struct iscsi_conn *conn,
503 u16 cid,
504 u32 exp_statsn)
506 struct iscsi_conn *conn_ptr = NULL;
507 struct iscsi_conn_recovery *cr = NULL;
508 struct iscsi_session *sess = conn->sess;
511 * By following item 5 in the login table, if we have found
512 * an existing ISID and a valid/existing TSIH and an existing
513 * CID we do connection reinstatement. Currently we dont not
514 * support it so we send back an non-zero status class to the
515 * initiator and release the new connection.
517 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
518 if ((conn_ptr)) {
519 pr_err("Connection exists with CID %hu for %s,"
520 " performing connection reinstatement.\n",
521 conn_ptr->cid, sess->sess_ops->InitiatorName);
523 iscsit_connection_reinstatement_rcfr(conn_ptr);
524 iscsit_dec_conn_usage_count(conn_ptr);
528 * Check for any connection recovery entires containing CID.
529 * We use the original ExpStatSN sent in the first login request
530 * to acknowledge commands for the failed connection.
532 * Also note that an explict logout may have already been sent,
533 * but the response may not be sent due to additional connection
534 * loss.
536 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
537 cr = iscsit_get_inactive_connection_recovery_entry(
538 sess, cid);
539 if ((cr)) {
540 pr_debug("Performing implicit logout"
541 " for connection recovery on CID: %hu\n",
542 conn->cid);
543 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
548 * Else we follow item 4 from the login table in that we have
549 * found an existing ISID and a valid/existing TSIH and a new
550 * CID we go ahead and continue to add a new connection to the
551 * session.
553 pr_debug("Adding CID %hu to existing session for %s.\n",
554 cid, sess->sess_ops->InitiatorName);
556 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
557 pr_err("Adding additional connection to this session"
558 " would exceed MaxConnections %d, login failed.\n",
559 sess->sess_ops->MaxConnections);
560 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
561 ISCSI_LOGIN_STATUS_ISID_ERROR);
562 return -1;
565 return 0;
568 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
570 struct iscsi_session *sess = conn->sess;
572 if (!sess->sess_ops->SessionType)
573 iscsit_start_nopin_timer(conn);
576 static int iscsi_post_login_handler(
577 struct iscsi_np *np,
578 struct iscsi_conn *conn,
579 u8 zero_tsih)
581 int stop_timer = 0;
582 struct iscsi_session *sess = conn->sess;
583 struct se_session *se_sess = sess->se_sess;
584 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
585 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
586 struct iscsi_thread_set *ts;
588 iscsit_inc_conn_usage_count(conn);
590 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
591 ISCSI_LOGIN_STATUS_ACCEPT);
593 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
594 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
596 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
597 iscsit_set_sync_and_steering_values(conn);
599 * SCSI Initiator -> SCSI Target Port Mapping
601 ts = iscsi_get_thread_set();
602 if (!zero_tsih) {
603 iscsi_set_session_parameters(sess->sess_ops,
604 conn->param_list, 0);
605 iscsi_release_param_list(conn->param_list);
606 conn->param_list = NULL;
608 spin_lock_bh(&sess->conn_lock);
609 atomic_set(&sess->session_continuation, 0);
610 if (sess->session_state == TARG_SESS_STATE_FAILED) {
611 pr_debug("Moving to"
612 " TARG_SESS_STATE_LOGGED_IN.\n");
613 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
614 stop_timer = 1;
617 pr_debug("iSCSI Login successful on CID: %hu from %s to"
618 " %s:%hu,%hu\n", conn->cid, conn->login_ip,
619 conn->local_ip, conn->local_port, tpg->tpgt);
621 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
622 atomic_inc(&sess->nconn);
623 pr_debug("Incremented iSCSI Connection count to %hu"
624 " from node: %s\n", atomic_read(&sess->nconn),
625 sess->sess_ops->InitiatorName);
626 spin_unlock_bh(&sess->conn_lock);
628 iscsi_post_login_start_timers(conn);
629 iscsi_activate_thread_set(conn, ts);
631 * Determine CPU mask to ensure connection's RX and TX kthreads
632 * are scheduled on the same CPU.
634 iscsit_thread_get_cpumask(conn);
635 conn->conn_rx_reset_cpumask = 1;
636 conn->conn_tx_reset_cpumask = 1;
638 iscsit_dec_conn_usage_count(conn);
639 if (stop_timer) {
640 spin_lock_bh(&se_tpg->session_lock);
641 iscsit_stop_time2retain_timer(sess);
642 spin_unlock_bh(&se_tpg->session_lock);
644 iscsit_dec_session_usage_count(sess);
645 return 0;
648 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
649 iscsi_release_param_list(conn->param_list);
650 conn->param_list = NULL;
652 iscsit_determine_maxcmdsn(sess);
654 spin_lock_bh(&se_tpg->session_lock);
655 __transport_register_session(&sess->tpg->tpg_se_tpg,
656 se_sess->se_node_acl, se_sess, sess);
657 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
658 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
660 pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
661 conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
662 tpg->tpgt);
664 spin_lock_bh(&sess->conn_lock);
665 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
666 atomic_inc(&sess->nconn);
667 pr_debug("Incremented iSCSI Connection count to %hu from node:"
668 " %s\n", atomic_read(&sess->nconn),
669 sess->sess_ops->InitiatorName);
670 spin_unlock_bh(&sess->conn_lock);
672 sess->sid = tpg->sid++;
673 if (!sess->sid)
674 sess->sid = tpg->sid++;
675 pr_debug("Established iSCSI session from node: %s\n",
676 sess->sess_ops->InitiatorName);
678 tpg->nsessions++;
679 if (tpg->tpg_tiqn)
680 tpg->tpg_tiqn->tiqn_nsessions++;
682 pr_debug("Incremented number of active iSCSI sessions to %u on"
683 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
684 spin_unlock_bh(&se_tpg->session_lock);
686 iscsi_post_login_start_timers(conn);
687 iscsi_activate_thread_set(conn, ts);
689 * Determine CPU mask to ensure connection's RX and TX kthreads
690 * are scheduled on the same CPU.
692 iscsit_thread_get_cpumask(conn);
693 conn->conn_rx_reset_cpumask = 1;
694 conn->conn_tx_reset_cpumask = 1;
696 iscsit_dec_conn_usage_count(conn);
698 return 0;
701 static void iscsi_handle_login_thread_timeout(unsigned long data)
703 struct iscsi_np *np = (struct iscsi_np *) data;
705 spin_lock_bh(&np->np_thread_lock);
706 pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
707 np->np_ip, np->np_port);
709 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
710 spin_unlock_bh(&np->np_thread_lock);
711 return;
714 if (np->np_thread)
715 send_sig(SIGINT, np->np_thread, 1);
717 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
718 spin_unlock_bh(&np->np_thread_lock);
721 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
724 * This used the TA_LOGIN_TIMEOUT constant because at this
725 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
727 spin_lock_bh(&np->np_thread_lock);
728 init_timer(&np->np_login_timer);
729 np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
730 np->np_login_timer.data = (unsigned long)np;
731 np->np_login_timer.function = iscsi_handle_login_thread_timeout;
732 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
733 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
734 add_timer(&np->np_login_timer);
736 pr_debug("Added timeout timer to iSCSI login request for"
737 " %u seconds.\n", TA_LOGIN_TIMEOUT);
738 spin_unlock_bh(&np->np_thread_lock);
741 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
743 spin_lock_bh(&np->np_thread_lock);
744 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
745 spin_unlock_bh(&np->np_thread_lock);
746 return;
748 np->np_login_timer_flags |= ISCSI_TF_STOP;
749 spin_unlock_bh(&np->np_thread_lock);
751 del_timer_sync(&np->np_login_timer);
753 spin_lock_bh(&np->np_thread_lock);
754 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
755 spin_unlock_bh(&np->np_thread_lock);
758 int iscsi_target_setup_login_socket(
759 struct iscsi_np *np,
760 struct __kernel_sockaddr_storage *sockaddr)
762 struct socket *sock;
763 int backlog = 5, ret, opt = 0, len;
765 switch (np->np_network_transport) {
766 case ISCSI_TCP:
767 np->np_ip_proto = IPPROTO_TCP;
768 np->np_sock_type = SOCK_STREAM;
769 break;
770 case ISCSI_SCTP_TCP:
771 np->np_ip_proto = IPPROTO_SCTP;
772 np->np_sock_type = SOCK_STREAM;
773 break;
774 case ISCSI_SCTP_UDP:
775 np->np_ip_proto = IPPROTO_SCTP;
776 np->np_sock_type = SOCK_SEQPACKET;
777 break;
778 case ISCSI_IWARP_TCP:
779 case ISCSI_IWARP_SCTP:
780 case ISCSI_INFINIBAND:
781 default:
782 pr_err("Unsupported network_transport: %d\n",
783 np->np_network_transport);
784 return -EINVAL;
787 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
788 np->np_ip_proto, &sock);
789 if (ret < 0) {
790 pr_err("sock_create() failed.\n");
791 return ret;
793 np->np_socket = sock;
795 * The SCTP stack needs struct socket->file.
797 if ((np->np_network_transport == ISCSI_SCTP_TCP) ||
798 (np->np_network_transport == ISCSI_SCTP_UDP)) {
799 if (!sock->file) {
800 sock->file = kzalloc(sizeof(struct file), GFP_KERNEL);
801 if (!sock->file) {
802 pr_err("Unable to allocate struct"
803 " file for SCTP\n");
804 ret = -ENOMEM;
805 goto fail;
807 np->np_flags |= NPF_SCTP_STRUCT_FILE;
811 * Setup the np->np_sockaddr from the passed sockaddr setup
812 * in iscsi_target_configfs.c code..
814 memcpy(&np->np_sockaddr, sockaddr,
815 sizeof(struct __kernel_sockaddr_storage));
817 if (sockaddr->ss_family == AF_INET6)
818 len = sizeof(struct sockaddr_in6);
819 else
820 len = sizeof(struct sockaddr_in);
822 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
824 /* FIXME: Someone please explain why this is endian-safe */
825 opt = 1;
826 if (np->np_network_transport == ISCSI_TCP) {
827 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
828 (char *)&opt, sizeof(opt));
829 if (ret < 0) {
830 pr_err("kernel_setsockopt() for TCP_NODELAY"
831 " failed: %d\n", ret);
832 goto fail;
836 /* FIXME: Someone please explain why this is endian-safe */
837 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
838 (char *)&opt, sizeof(opt));
839 if (ret < 0) {
840 pr_err("kernel_setsockopt() for SO_REUSEADDR"
841 " failed\n");
842 goto fail;
845 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
846 (char *)&opt, sizeof(opt));
847 if (ret < 0) {
848 pr_err("kernel_setsockopt() for IP_FREEBIND"
849 " failed\n");
850 goto fail;
853 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
854 if (ret < 0) {
855 pr_err("kernel_bind() failed: %d\n", ret);
856 goto fail;
859 ret = kernel_listen(sock, backlog);
860 if (ret != 0) {
861 pr_err("kernel_listen() failed: %d\n", ret);
862 goto fail;
865 return 0;
867 fail:
868 np->np_socket = NULL;
869 if (sock) {
870 if (np->np_flags & NPF_SCTP_STRUCT_FILE) {
871 kfree(sock->file);
872 sock->file = NULL;
875 sock_release(sock);
877 return ret;
880 static int __iscsi_target_login_thread(struct iscsi_np *np)
882 u8 buffer[ISCSI_HDR_LEN], iscsi_opcode, zero_tsih = 0;
883 int err, ret = 0, ip_proto, sock_type, set_sctp_conn_flag, stop;
884 struct iscsi_conn *conn = NULL;
885 struct iscsi_login *login;
886 struct iscsi_portal_group *tpg = NULL;
887 struct socket *new_sock, *sock;
888 struct kvec iov;
889 struct iscsi_login_req *pdu;
890 struct sockaddr_in sock_in;
891 struct sockaddr_in6 sock_in6;
893 flush_signals(current);
894 set_sctp_conn_flag = 0;
895 sock = np->np_socket;
896 ip_proto = np->np_ip_proto;
897 sock_type = np->np_sock_type;
899 spin_lock_bh(&np->np_thread_lock);
900 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
901 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
902 complete(&np->np_restart_comp);
903 } else {
904 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
906 spin_unlock_bh(&np->np_thread_lock);
908 if (kernel_accept(sock, &new_sock, 0) < 0) {
909 spin_lock_bh(&np->np_thread_lock);
910 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
911 spin_unlock_bh(&np->np_thread_lock);
912 complete(&np->np_restart_comp);
913 /* Get another socket */
914 return 1;
916 spin_unlock_bh(&np->np_thread_lock);
917 goto out;
920 * The SCTP stack needs struct socket->file.
922 if ((np->np_network_transport == ISCSI_SCTP_TCP) ||
923 (np->np_network_transport == ISCSI_SCTP_UDP)) {
924 if (!new_sock->file) {
925 new_sock->file = kzalloc(
926 sizeof(struct file), GFP_KERNEL);
927 if (!new_sock->file) {
928 pr_err("Unable to allocate struct"
929 " file for SCTP\n");
930 sock_release(new_sock);
931 /* Get another socket */
932 return 1;
934 set_sctp_conn_flag = 1;
938 iscsi_start_login_thread_timer(np);
940 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
941 if (!conn) {
942 pr_err("Could not allocate memory for"
943 " new connection\n");
944 if (set_sctp_conn_flag) {
945 kfree(new_sock->file);
946 new_sock->file = NULL;
948 sock_release(new_sock);
949 /* Get another socket */
950 return 1;
953 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
954 conn->conn_state = TARG_CONN_STATE_FREE;
955 conn->sock = new_sock;
957 if (set_sctp_conn_flag)
958 conn->conn_flags |= CONNFLAG_SCTP_STRUCT_FILE;
960 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
961 conn->conn_state = TARG_CONN_STATE_XPT_UP;
964 * Allocate conn->conn_ops early as a failure calling
965 * iscsit_tx_login_rsp() below will call tx_data().
967 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
968 if (!conn->conn_ops) {
969 pr_err("Unable to allocate memory for"
970 " struct iscsi_conn_ops.\n");
971 goto new_sess_out;
974 * Perform the remaining iSCSI connection initialization items..
976 if (iscsi_login_init_conn(conn) < 0)
977 goto new_sess_out;
979 memset(buffer, 0, ISCSI_HDR_LEN);
980 memset(&iov, 0, sizeof(struct kvec));
981 iov.iov_base = buffer;
982 iov.iov_len = ISCSI_HDR_LEN;
984 if (rx_data(conn, &iov, 1, ISCSI_HDR_LEN) <= 0) {
985 pr_err("rx_data() returned an error.\n");
986 goto new_sess_out;
989 iscsi_opcode = (buffer[0] & ISCSI_OPCODE_MASK);
990 if (!(iscsi_opcode & ISCSI_OP_LOGIN)) {
991 pr_err("First opcode is not login request,"
992 " failing login request.\n");
993 goto new_sess_out;
996 pdu = (struct iscsi_login_req *) buffer;
997 pdu->cid = be16_to_cpu(pdu->cid);
998 pdu->tsih = be16_to_cpu(pdu->tsih);
999 pdu->itt = be32_to_cpu(pdu->itt);
1000 pdu->cmdsn = be32_to_cpu(pdu->cmdsn);
1001 pdu->exp_statsn = be32_to_cpu(pdu->exp_statsn);
1003 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1004 * when Status-Class != 0.
1006 conn->login_itt = pdu->itt;
1008 spin_lock_bh(&np->np_thread_lock);
1009 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1010 spin_unlock_bh(&np->np_thread_lock);
1011 pr_err("iSCSI Network Portal on %s:%hu currently not"
1012 " active.\n", np->np_ip, np->np_port);
1013 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1014 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1015 goto new_sess_out;
1017 spin_unlock_bh(&np->np_thread_lock);
1019 if (np->np_sockaddr.ss_family == AF_INET6) {
1020 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1022 if (conn->sock->ops->getname(conn->sock,
1023 (struct sockaddr *)&sock_in6, &err, 1) < 0) {
1024 pr_err("sock_ops->getname() failed.\n");
1025 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1026 ISCSI_LOGIN_STATUS_TARGET_ERROR);
1027 goto new_sess_out;
1029 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
1030 &sock_in6.sin6_addr.in6_u);
1031 conn->login_port = ntohs(sock_in6.sin6_port);
1033 if (conn->sock->ops->getname(conn->sock,
1034 (struct sockaddr *)&sock_in6, &err, 0) < 0) {
1035 pr_err("sock_ops->getname() failed.\n");
1036 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1037 ISCSI_LOGIN_STATUS_TARGET_ERROR);
1038 goto new_sess_out;
1040 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
1041 &sock_in6.sin6_addr.in6_u);
1042 conn->local_port = ntohs(sock_in6.sin6_port);
1044 } else {
1045 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1047 if (conn->sock->ops->getname(conn->sock,
1048 (struct sockaddr *)&sock_in, &err, 1) < 0) {
1049 pr_err("sock_ops->getname() failed.\n");
1050 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1051 ISCSI_LOGIN_STATUS_TARGET_ERROR);
1052 goto new_sess_out;
1054 sprintf(conn->login_ip, "%pI4", &sock_in.sin_addr.s_addr);
1055 conn->login_port = ntohs(sock_in.sin_port);
1057 if (conn->sock->ops->getname(conn->sock,
1058 (struct sockaddr *)&sock_in, &err, 0) < 0) {
1059 pr_err("sock_ops->getname() failed.\n");
1060 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1061 ISCSI_LOGIN_STATUS_TARGET_ERROR);
1062 goto new_sess_out;
1064 sprintf(conn->local_ip, "%pI4", &sock_in.sin_addr.s_addr);
1065 conn->local_port = ntohs(sock_in.sin_port);
1068 conn->network_transport = np->np_network_transport;
1070 pr_debug("Received iSCSI login request from %s on %s Network"
1071 " Portal %s:%hu\n", conn->login_ip,
1072 (conn->network_transport == ISCSI_TCP) ? "TCP" : "SCTP",
1073 conn->local_ip, conn->local_port);
1075 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1076 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1078 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1079 pdu->min_version) < 0)
1080 goto new_sess_out;
1082 zero_tsih = (pdu->tsih == 0x0000);
1083 if ((zero_tsih)) {
1085 * This is the leading connection of a new session.
1086 * We wait until after authentication to check for
1087 * session reinstatement.
1089 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1090 goto new_sess_out;
1091 } else {
1093 * Add a new connection to an existing session.
1094 * We check for a non-existant session in
1095 * iscsi_login_non_zero_tsih_s2() below based
1096 * on ISID/TSIH, but wait until after authentication
1097 * to check for connection reinstatement, etc.
1099 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1100 goto new_sess_out;
1104 * This will process the first login request, and call
1105 * iscsi_target_locate_portal(), and return a valid struct iscsi_login.
1107 login = iscsi_target_init_negotiation(np, conn, buffer);
1108 if (!login) {
1109 tpg = conn->tpg;
1110 goto new_sess_out;
1113 tpg = conn->tpg;
1114 if (!tpg) {
1115 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1116 goto new_sess_out;
1119 if (zero_tsih) {
1120 if (iscsi_login_zero_tsih_s2(conn) < 0) {
1121 iscsi_target_nego_release(login, conn);
1122 goto new_sess_out;
1124 } else {
1125 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0) {
1126 iscsi_target_nego_release(login, conn);
1127 goto old_sess_out;
1131 if (iscsi_target_start_negotiation(login, conn) < 0)
1132 goto new_sess_out;
1134 if (!conn->sess) {
1135 pr_err("struct iscsi_conn session pointer is NULL!\n");
1136 goto new_sess_out;
1139 iscsi_stop_login_thread_timer(np);
1141 if (signal_pending(current))
1142 goto new_sess_out;
1144 ret = iscsi_post_login_handler(np, conn, zero_tsih);
1146 if (ret < 0)
1147 goto new_sess_out;
1149 iscsit_deaccess_np(np, tpg);
1150 tpg = NULL;
1151 /* Get another socket */
1152 return 1;
1154 new_sess_out:
1155 pr_err("iSCSI Login negotiation failed.\n");
1156 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1157 ISCSI_LOGIN_STATUS_INIT_ERR);
1158 if (!zero_tsih || !conn->sess)
1159 goto old_sess_out;
1160 if (conn->sess->se_sess)
1161 transport_free_session(conn->sess->se_sess);
1162 if (conn->sess->session_index != 0) {
1163 spin_lock_bh(&sess_idr_lock);
1164 idr_remove(&sess_idr, conn->sess->session_index);
1165 spin_unlock_bh(&sess_idr_lock);
1167 if (conn->sess->sess_ops)
1168 kfree(conn->sess->sess_ops);
1169 if (conn->sess)
1170 kfree(conn->sess);
1171 old_sess_out:
1172 iscsi_stop_login_thread_timer(np);
1174 * If login negotiation fails check if the Time2Retain timer
1175 * needs to be restarted.
1177 if (!zero_tsih && conn->sess) {
1178 spin_lock_bh(&conn->sess->conn_lock);
1179 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1180 struct se_portal_group *se_tpg =
1181 &ISCSI_TPG_C(conn)->tpg_se_tpg;
1183 atomic_set(&conn->sess->session_continuation, 0);
1184 spin_unlock_bh(&conn->sess->conn_lock);
1185 spin_lock_bh(&se_tpg->session_lock);
1186 iscsit_start_time2retain_handler(conn->sess);
1187 spin_unlock_bh(&se_tpg->session_lock);
1188 } else
1189 spin_unlock_bh(&conn->sess->conn_lock);
1190 iscsit_dec_session_usage_count(conn->sess);
1193 if (!IS_ERR(conn->conn_rx_hash.tfm))
1194 crypto_free_hash(conn->conn_rx_hash.tfm);
1195 if (!IS_ERR(conn->conn_tx_hash.tfm))
1196 crypto_free_hash(conn->conn_tx_hash.tfm);
1198 if (conn->conn_cpumask)
1199 free_cpumask_var(conn->conn_cpumask);
1201 kfree(conn->conn_ops);
1203 if (conn->param_list) {
1204 iscsi_release_param_list(conn->param_list);
1205 conn->param_list = NULL;
1207 if (conn->sock) {
1208 if (conn->conn_flags & CONNFLAG_SCTP_STRUCT_FILE) {
1209 kfree(conn->sock->file);
1210 conn->sock->file = NULL;
1212 sock_release(conn->sock);
1214 kfree(conn);
1216 if (tpg) {
1217 iscsit_deaccess_np(np, tpg);
1218 tpg = NULL;
1221 out:
1222 stop = kthread_should_stop();
1223 if (!stop && signal_pending(current)) {
1224 spin_lock_bh(&np->np_thread_lock);
1225 stop = (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN);
1226 spin_unlock_bh(&np->np_thread_lock);
1228 /* Wait for another socket.. */
1229 if (!stop)
1230 return 1;
1232 iscsi_stop_login_thread_timer(np);
1233 spin_lock_bh(&np->np_thread_lock);
1234 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1235 spin_unlock_bh(&np->np_thread_lock);
1236 return 0;
1239 int iscsi_target_login_thread(void *arg)
1241 struct iscsi_np *np = arg;
1242 int ret;
1244 allow_signal(SIGINT);
1246 while (!kthread_should_stop()) {
1247 ret = __iscsi_target_login_thread(np);
1249 * We break and exit here unless another sock_accept() call
1250 * is expected.
1252 if (ret != 1)
1253 break;
1256 return 0;