Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * This file contains main functions related to the iSCSI Target Core Driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * (c) Copyright 2007-2013 Datera, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <scsi/scsi_proto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <scsi/iscsi_proto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <scsi/scsi_tcq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <target/target_core_base.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <target/target_core_fabric.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <target/iscsi/iscsi_target_core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include "iscsi_target_parameters.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include "iscsi_target_seq_pdu_list.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include "iscsi_target_datain_values.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include "iscsi_target_erl0.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include "iscsi_target_erl1.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include "iscsi_target_erl2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include "iscsi_target_login.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include "iscsi_target_tmr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include "iscsi_target_tpg.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include "iscsi_target_util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include "iscsi_target.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include "iscsi_target_device.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include <target/iscsi/iscsi_target_stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <target/iscsi/iscsi_transport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) static LIST_HEAD(g_tiqn_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) static LIST_HEAD(g_np_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) static DEFINE_SPINLOCK(tiqn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) static DEFINE_MUTEX(np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) static struct idr tiqn_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) DEFINE_IDA(sess_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) struct mutex auth_id_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) struct iscsit_global *iscsit_global;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) struct kmem_cache *lio_qr_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) struct kmem_cache *lio_dr_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) struct kmem_cache *lio_ooo_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) struct kmem_cache *lio_r2t_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) static int iscsit_handle_immediate_data(struct iscsi_cmd *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 			struct iscsi_scsi_req *, u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	struct iscsi_tiqn *tiqn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	spin_lock(&tiqn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 		if (!strcmp(tiqn->tiqn, buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 			spin_lock(&tiqn->tiqn_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 			if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 				tiqn->tiqn_access_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 				spin_unlock(&tiqn->tiqn_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 				spin_unlock(&tiqn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 				return tiqn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 			spin_unlock(&tiqn->tiqn_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	spin_unlock(&tiqn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	spin_lock(&tiqn->tiqn_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 		tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 		spin_unlock(&tiqn->tiqn_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	spin_unlock(&tiqn->tiqn_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	spin_lock(&tiqn->tiqn_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	tiqn->tiqn_access_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	spin_unlock(&tiqn->tiqn_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  * Note that IQN formatting is expected to be done in userspace, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  * no explict IQN format checks are done here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	struct iscsi_tiqn *tiqn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	if (strlen(buf) >= ISCSI_IQN_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		pr_err("Target IQN exceeds %d bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 				ISCSI_IQN_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	tiqn = kzalloc(sizeof(*tiqn), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	if (!tiqn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	sprintf(tiqn->tiqn, "%s", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	INIT_LIST_HEAD(&tiqn->tiqn_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	spin_lock_init(&tiqn->tiqn_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	spin_lock_init(&tiqn->tiqn_tpg_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	spin_lock_init(&tiqn->sess_err_stats.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	spin_lock_init(&tiqn->login_stats.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	spin_lock_init(&tiqn->logout_stats.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	tiqn->tiqn_state = TIQN_STATE_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	idr_preload(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	spin_lock(&tiqn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 		spin_unlock(&tiqn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		idr_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		kfree(tiqn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	tiqn->tiqn_index = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	spin_unlock(&tiqn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	idr_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	return tiqn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	 * Wait for accesses to said struct iscsi_tiqn to end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	spin_lock(&tiqn->tiqn_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	while (tiqn->tiqn_access_count != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		spin_unlock(&tiqn->tiqn_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		msleep(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		spin_lock(&tiqn->tiqn_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	spin_unlock(&tiqn->tiqn_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	 * while holding tiqn->tiqn_state_lock.  This means that all subsequent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	 * attempts to access this struct iscsi_tiqn will fail from both transport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	 * fabric and control code paths.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		pr_err("iscsit_set_tiqn_shutdown() failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	iscsit_wait_for_tiqn(tiqn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	spin_lock(&tiqn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	list_del(&tiqn->tiqn_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	idr_remove(&tiqn_idr, tiqn->tiqn_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	spin_unlock(&tiqn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 			tiqn->tiqn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	kfree(tiqn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	 * Determine if the network portal is accepting storage traffic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	spin_lock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		spin_unlock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	spin_unlock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	 * Determine if the portal group is accepting storage traffic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	spin_lock_bh(&tpg->tpg_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	if (tpg->tpg_state != TPG_STATE_ACTIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		spin_unlock_bh(&tpg->tpg_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	spin_unlock_bh(&tpg->tpg_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	 * Here we serialize access across the TIQN+TPG Tuple.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	ret = down_interruptible(&tpg->np_login_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	spin_lock_bh(&tpg->tpg_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	if (tpg->tpg_state != TPG_STATE_ACTIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		spin_unlock_bh(&tpg->tpg_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		up(&tpg->np_login_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	spin_unlock_bh(&tpg->tpg_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) void iscsit_login_kref_put(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	struct iscsi_tpg_np *tpg_np = container_of(kref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 				struct iscsi_tpg_np, tpg_np_kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	complete(&tpg_np->tpg_np_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		       struct iscsi_tpg_np *tpg_np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	up(&tpg->np_login_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	if (tpg_np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	if (tiqn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		iscsit_put_tiqn_for_login(tiqn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) bool iscsit_check_np_match(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	struct sockaddr_storage *sockaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	struct iscsi_np *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	int network_transport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	struct sockaddr_in *sock_in, *sock_in_e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	struct sockaddr_in6 *sock_in6, *sock_in6_e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	bool ip_match = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	u16 port, port_e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	if (sockaddr->ss_family == AF_INET6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		sock_in6 = (struct sockaddr_in6 *)sockaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		if (!memcmp(&sock_in6->sin6_addr.in6_u,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 			    &sock_in6_e->sin6_addr.in6_u,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 			    sizeof(struct in6_addr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 			ip_match = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		port = ntohs(sock_in6->sin6_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		port_e = ntohs(sock_in6_e->sin6_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		sock_in = (struct sockaddr_in *)sockaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 			ip_match = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		port = ntohs(sock_in->sin_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		port_e = ntohs(sock_in_e->sin_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	if (ip_match && (port_e == port) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	    (np->np_network_transport == network_transport))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) static struct iscsi_np *iscsit_get_np(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	struct sockaddr_storage *sockaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	int network_transport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	struct iscsi_np *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	bool match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	lockdep_assert_held(&np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	list_for_each_entry(np, &g_np_list, np_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		spin_lock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 			spin_unlock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		match = iscsit_check_np_match(sockaddr, np, network_transport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		if (match) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 			 * Increment the np_exports reference count now to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 			 * prevent iscsit_del_np() below from being called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			 * while iscsi_tpg_add_network_portal() is called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 			np->np_exports++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 			spin_unlock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 			return np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		spin_unlock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) struct iscsi_np *iscsit_add_np(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	struct sockaddr_storage *sockaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	int network_transport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	struct iscsi_np *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	mutex_lock(&np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	 * Locate the existing struct iscsi_np if already active..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	np = iscsit_get_np(sockaddr, network_transport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	if (np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		mutex_unlock(&np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		return np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	np = kzalloc(sizeof(*np), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	if (!np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		mutex_unlock(&np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	np->np_flags |= NPF_IP_NETWORK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	np->np_network_transport = network_transport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	spin_lock_init(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	init_completion(&np->np_restart_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	INIT_LIST_HEAD(&np->np_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	timer_setup(&np->np_login_timer, iscsi_handle_login_thread_timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	ret = iscsi_target_setup_login_socket(np, sockaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		kfree(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		mutex_unlock(&np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	if (IS_ERR(np->np_thread)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		pr_err("Unable to create kthread: iscsi_np\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		ret = PTR_ERR(np->np_thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		kfree(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		mutex_unlock(&np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	 * Increment the np_exports reference count now to prevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	 * iscsit_del_np() below from being run while a new call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	 * active.  We don't need to hold np->np_thread_lock at this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	 * point because iscsi_np has not been added to g_np_list yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	np->np_exports = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	list_add_tail(&np->np_list, &g_np_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	mutex_unlock(&np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		&np->np_sockaddr, np->np_transport->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	return np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) int iscsit_reset_np_thread(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	struct iscsi_np *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	struct iscsi_tpg_np *tpg_np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	struct iscsi_portal_group *tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	bool shutdown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	spin_lock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		spin_unlock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	np->np_thread_state = ISCSI_NP_THREAD_RESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	atomic_inc(&np->np_reset_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	if (np->np_thread) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		spin_unlock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		send_sig(SIGINT, np->np_thread, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		wait_for_completion(&np->np_restart_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		spin_lock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	spin_unlock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	if (tpg_np && shutdown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		wait_for_completion(&tpg_np->tpg_np_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) static void iscsit_free_np(struct iscsi_np *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	if (np->np_socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		sock_release(np->np_socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) int iscsit_del_np(struct iscsi_np *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	spin_lock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	np->np_exports--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	if (np->np_exports) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		np->enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		spin_unlock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	spin_unlock_bh(&np->np_thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	if (np->np_thread) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		 * We need to send the signal to wakeup Linux/Net
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		 * which may be sleeping in sock_accept()..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		send_sig(SIGINT, np->np_thread, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		kthread_stop(np->np_thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		np->np_thread = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	np->np_transport->iscsit_free_np(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	mutex_lock(&np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	list_del(&np->np_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	mutex_unlock(&np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		&np->np_sockaddr, np->np_transport->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	iscsit_put_transport(np->np_transport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	kfree(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) static void iscsit_get_rx_pdu(struct iscsi_conn *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	return iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) EXPORT_SYMBOL(iscsit_queue_rsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	spin_lock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	if (!list_empty(&cmd->i_conn_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		list_del_init(&cmd->i_conn_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	spin_unlock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	__iscsit_free_cmd(cmd, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) EXPORT_SYMBOL(iscsit_aborted_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 				      u32, u32, const void *, void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) iscsit_xmit_nondatain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			  const void *data_buf, u32 data_buf_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	struct kvec *iov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	u32 niov = 0, tx_size = ISCSI_HDR_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	iov = &cmd->iov_misc[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	iov[niov].iov_base	= cmd->pdu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	iov[niov++].iov_len	= ISCSI_HDR_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	if (conn->conn_ops->HeaderDigest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 					  ISCSI_HDR_LEN, 0, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 					  header_digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		iov[0].iov_len += ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		tx_size += ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		pr_debug("Attaching CRC32C HeaderDigest"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 			 " to opcode 0x%x 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			 hdr->opcode, *header_digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	if (data_buf_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		u32 padding = ((-data_buf_len) & 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		iov[niov].iov_base	= (void *)data_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		iov[niov++].iov_len	= data_buf_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		tx_size += data_buf_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		if (padding != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 			iov[niov].iov_base = &cmd->pad_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 			iov[niov++].iov_len = padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 			tx_size += padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 			pr_debug("Attaching %u additional"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 				 " padding bytes.\n", padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		if (conn->conn_ops->DataDigest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 			iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 						  data_buf, data_buf_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 						  padding, &cmd->pad_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 						  &cmd->data_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 			iov[niov].iov_base = &cmd->data_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			iov[niov++].iov_len = ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 			tx_size += ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 			pr_debug("Attached DataDigest for %u"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 				 " bytes opcode 0x%x, CRC 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 				 data_buf_len, hdr->opcode, cmd->data_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	cmd->iov_misc_count = niov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	cmd->tx_size = tx_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	ret = iscsit_send_tx_data(cmd, conn, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		iscsit_tx_thread_wait_for_tcp(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) static int iscsit_map_iovec(struct iscsi_cmd *cmd, struct kvec *iov, int nvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 			    u32 data_offset, u32 data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) static void iscsit_unmap_iovec(struct iscsi_cmd *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsi_cmd *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 				    u32, u32, u32, u8 *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) iscsit_xmit_datain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		       const struct iscsi_datain *datain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	struct kvec *iov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	u32 iov_count = 0, tx_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	int ret, iov_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	iov = &cmd->iov_data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	iov[iov_count].iov_base	= cmd->pdu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	iov[iov_count++].iov_len = ISCSI_HDR_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	tx_size += ISCSI_HDR_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	if (conn->conn_ops->HeaderDigest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 					  ISCSI_HDR_LEN, 0, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 					  header_digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		iov[0].iov_len += ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		tx_size += ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 			 *header_digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[iov_count],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 				   cmd->orig_iov_data_count - (iov_count + 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 				   datain->offset, datain->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	if (iov_ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	iov_count += iov_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	tx_size += datain->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	cmd->padding = ((-datain->length) & 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	if (cmd->padding) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		iov[iov_count].iov_base		= cmd->pad_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		iov[iov_count++].iov_len	= cmd->padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		tx_size += cmd->padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		pr_debug("Attaching %u padding bytes\n", cmd->padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	if (conn->conn_ops->DataDigest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 							 cmd, datain->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 							 datain->length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 							 cmd->padding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 							 cmd->pad_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		iov[iov_count].iov_base	= &cmd->data_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		iov[iov_count++].iov_len = ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		tx_size += ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 			 datain->length + cmd->padding, cmd->data_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	cmd->iov_data_count = iov_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	cmd->tx_size = tx_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	ret = iscsit_fe_sendpage_sg(cmd, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	iscsit_unmap_iovec(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		iscsit_tx_thread_wait_for_tcp(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) static int iscsit_xmit_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			   struct iscsi_datain_req *dr, const void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 			   u32 buf_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	if (dr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		return iscsit_xmit_datain_pdu(conn, cmd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	return TARGET_PROT_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) static struct iscsit_transport iscsi_target_transport = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	.name			= "iSCSI/TCP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	.transport_type		= ISCSI_TCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	.rdma_shutdown		= false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	.owner			= NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	.iscsit_setup_np	= iscsit_setup_np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	.iscsit_accept_np	= iscsit_accept_np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	.iscsit_free_np		= iscsit_free_np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	.iscsit_get_login_rx	= iscsit_get_login_rx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	.iscsit_put_login_tx	= iscsit_put_login_tx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	.iscsit_get_dataout	= iscsit_build_r2ts_for_cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	.iscsit_immediate_queue	= iscsit_immediate_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	.iscsit_response_queue	= iscsit_response_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	.iscsit_queue_data_in	= iscsit_queue_rsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	.iscsit_queue_status	= iscsit_queue_rsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	.iscsit_aborted_task	= iscsit_aborted_task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	.iscsit_xmit_pdu	= iscsit_xmit_pdu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	.iscsit_get_rx_pdu	= iscsit_get_rx_pdu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	.iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) static int __init iscsi_target_init_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	int ret = 0, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	iscsit_global = kzalloc(sizeof(*iscsit_global), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	if (!iscsit_global)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	spin_lock_init(&iscsit_global->ts_bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	mutex_init(&auth_id_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	idr_init(&tiqn_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	ret = target_register_template(&iscsi_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	iscsit_global->ts_bitmap = vzalloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	if (!iscsit_global->ts_bitmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		goto configfs_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	lio_qr_cache = kmem_cache_create("lio_qr_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 			sizeof(struct iscsi_queue_req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 			__alignof__(struct iscsi_queue_req), 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	if (!lio_qr_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		pr_err("Unable to kmem_cache_create() for"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 				" lio_qr_cache\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		goto bitmap_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	lio_dr_cache = kmem_cache_create("lio_dr_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 			sizeof(struct iscsi_datain_req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 			__alignof__(struct iscsi_datain_req), 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	if (!lio_dr_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		pr_err("Unable to kmem_cache_create() for"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 				" lio_dr_cache\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		goto qr_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 			sizeof(struct iscsi_ooo_cmdsn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			__alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	if (!lio_ooo_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		pr_err("Unable to kmem_cache_create() for"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 				" lio_ooo_cache\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		goto dr_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 			sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 			0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	if (!lio_r2t_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		pr_err("Unable to kmem_cache_create() for"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 				" lio_r2t_cache\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		goto ooo_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	iscsit_register_transport(&iscsi_target_transport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	if (iscsit_load_discovery_tpg() < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		goto r2t_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) r2t_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	iscsit_unregister_transport(&iscsi_target_transport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	kmem_cache_destroy(lio_r2t_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) ooo_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	kmem_cache_destroy(lio_ooo_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) dr_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	kmem_cache_destroy(lio_dr_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) qr_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	kmem_cache_destroy(lio_qr_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) bitmap_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	vfree(iscsit_global->ts_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) configfs_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	/* XXX: this probably wants it to be it's own unwind step.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	if (iscsit_global->discovery_tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	target_unregister_template(&iscsi_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	kfree(iscsit_global);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) static void __exit iscsi_target_cleanup_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	iscsit_release_discovery_tpg();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	iscsit_unregister_transport(&iscsi_target_transport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	kmem_cache_destroy(lio_qr_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	kmem_cache_destroy(lio_dr_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	kmem_cache_destroy(lio_ooo_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	kmem_cache_destroy(lio_r2t_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	 * Shutdown discovery sessions and disable discovery TPG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	if (iscsit_global->discovery_tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	target_unregister_template(&iscsi_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	vfree(iscsit_global->ts_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	kfree(iscsit_global);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) int iscsit_add_reject(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	struct iscsi_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	u8 reason,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	struct iscsi_cmd *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	cmd->iscsi_opcode = ISCSI_OP_REJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	cmd->reject_reason = reason;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	if (!cmd->buf_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		pr_err("Unable to allocate memory for cmd->buf_ptr\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		iscsit_free_cmd(cmd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	spin_lock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	spin_unlock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	cmd->i_state = ISTATE_SEND_REJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) EXPORT_SYMBOL(iscsit_add_reject);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) static int iscsit_add_reject_from_cmd(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	u8 reason,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	bool add_to_conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	struct iscsi_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	const bool do_put = cmd->se_cmd.se_tfo != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (!cmd->conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 				cmd->init_task_tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	conn = cmd->conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	cmd->iscsi_opcode = ISCSI_OP_REJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	cmd->reject_reason = reason;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	if (!cmd->buf_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		pr_err("Unable to allocate memory for cmd->buf_ptr\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		iscsit_free_cmd(cmd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	if (add_to_conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		spin_lock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		spin_unlock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	cmd->i_state = ISTATE_SEND_REJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	 * Perform the kref_put now if se_cmd has already been setup by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	 * scsit_setup_scsi_cmd()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	if (do_put) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		target_put_sess_cmd(&cmd->se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 				 unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) EXPORT_SYMBOL(iscsit_reject_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879)  * Map some portion of the allocated scatterlist to an iovec, suitable for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880)  * kernel sockets to copy data in/out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) static int iscsit_map_iovec(struct iscsi_cmd *cmd, struct kvec *iov, int nvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			    u32 data_offset, u32 data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	u32 i = 0, orig_data_length = data_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	unsigned int page_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	 * We know each entry in t_data_sg contains a page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	u32 ent = data_offset / PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	if (!data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	if (ent >= cmd->se_cmd.t_data_nents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		pr_err("Initial page entry out-of-bounds\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		goto overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	sg = &cmd->se_cmd.t_data_sg[ent];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	page_off = (data_offset % PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	cmd->first_data_sg = sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	cmd->first_data_sg_off = page_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	while (data_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		u32 cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		if (WARN_ON_ONCE(!sg || i >= nvec))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 			goto overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		cur_len = min_t(u32, data_length, sg->length - page_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		iov[i].iov_len = cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		data_length -= cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		page_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		sg = sg_next(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	cmd->kmapped_nents = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) overflow:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	pr_err("offset %d + length %d overflow; %d/%d; sg-list:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	       data_offset, orig_data_length, i, nvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	for_each_sg(cmd->se_cmd.t_data_sg, sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		    cmd->se_cmd.t_data_nents, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		pr_err("[%d] off %d len %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		       i, sg->offset, sg->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	sg = cmd->first_data_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	for (i = 0; i < cmd->kmapped_nents; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		kunmap(sg_page(&sg[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	LIST_HEAD(ack_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	struct iscsi_cmd *cmd, *cmd_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	conn->exp_statsn = exp_statsn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	if (conn->sess->sess_ops->RDMAExtensions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	spin_lock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		spin_lock(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		if ((cmd->i_state == ISTATE_SENT_STATUS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		    iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 			cmd->i_state = ISTATE_REMOVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			spin_unlock(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 			list_move_tail(&cmd->i_conn_node, &ack_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		spin_unlock(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	spin_unlock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		list_del_init(&cmd->i_conn_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		iscsit_free_cmd(cmd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	iov_count += ISCSI_IOV_DATA_BUFFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	cmd->iov_data = kcalloc(iov_count, sizeof(*cmd->iov_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	if (!cmd->iov_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	cmd->orig_iov_data_count = iov_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 			  unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	int data_direction, payload_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	struct iscsi_scsi_req *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	int iscsi_task_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	int sam_task_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	atomic_long_inc(&conn->sess->cmd_pdus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	hdr			= (struct iscsi_scsi_req *) buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	payload_length		= ntoh24(hdr->dlength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	/* FIXME; Add checks for AdditionalHeaderSegment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	    !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 				" not set. Bad iSCSI Initiator.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 					     ISCSI_REASON_BOOKMARK_INVALID, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	     (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		 * From RFC-3720 Section 10.3.1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		 * "Either or both of R and W MAY be 1 when either the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		 *  Expected Data Transfer Length and/or Bidirectional Read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		 *  Expected Data Transfer Length are 0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		 * For this case, go ahead and clear the unnecssary bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		 * to avoid any confusion with ->data_direction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		hdr->flags &= ~ISCSI_FLAG_CMD_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 			" set when Expected Data Transfer Length is 0 for"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 			" CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	    !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 			" MUST be set if Expected Data Transfer Length is not 0."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			" Bad iSCSI Initiator\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 					     ISCSI_REASON_BOOKMARK_INVALID, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	    (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		pr_err("Bidirectional operations not supported!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 					     ISCSI_REASON_BOOKMARK_INVALID, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		pr_err("Illegally set Immediate Bit in iSCSI Initiator"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 				" Scsi Command PDU.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 					     ISCSI_REASON_BOOKMARK_INVALID, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	if (payload_length && !conn->sess->sess_ops->ImmediateData) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		pr_err("ImmediateData=No but DataSegmentLength=%u,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 			" protocol error.\n", payload_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 					     ISCSI_REASON_PROTOCOL_ERROR, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	if ((be32_to_cpu(hdr->data_length) == payload_length) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	    (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		pr_err("Expected Data Transfer Length and Length of"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 			" Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 			" bit is not set protocol error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 					     ISCSI_REASON_PROTOCOL_ERROR, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	if (payload_length > be32_to_cpu(hdr->data_length)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		pr_err("DataSegmentLength: %u is greater than"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 			" EDTL: %u, protocol error.\n", payload_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 				hdr->data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 					     ISCSI_REASON_PROTOCOL_ERROR, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		pr_err("DataSegmentLength: %u is greater than"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 			" MaxXmitDataSegmentLength: %u, protocol error.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 			payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 					     ISCSI_REASON_PROTOCOL_ERROR, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		pr_err("DataSegmentLength: %u is greater than"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 			" FirstBurstLength: %u, protocol error.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 			payload_length, conn->sess->sess_ops->FirstBurstLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 					     ISCSI_REASON_BOOKMARK_INVALID, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 			 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 			  DMA_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	cmd->data_direction = data_direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	 * Figure out the SAM Task Attribute for the incoming SCSI CDB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	    (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		sam_task_attr = TCM_SIMPLE_TAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		sam_task_attr = TCM_ORDERED_TAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		sam_task_attr = TCM_HEAD_TAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	else if (iscsi_task_attr == ISCSI_ATTR_ACA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		sam_task_attr = TCM_ACA_TAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 			" TCM_SIMPLE_TAG\n", iscsi_task_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		sam_task_attr = TCM_SIMPLE_TAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	cmd->iscsi_opcode	= ISCSI_OP_SCSI_CMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	cmd->i_state		= ISTATE_NEW_CMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	cmd->immediate_data	= (payload_length) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	cmd->unsolicited_data	= ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 				     (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	if (cmd->unsolicited_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	if (hdr->flags & ISCSI_FLAG_CMD_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		cmd->targ_xfer_tag = 0xFFFFFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	cmd->cmd_sn		= be32_to_cpu(hdr->cmdsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	cmd->exp_stat_sn	= be32_to_cpu(hdr->exp_statsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	cmd->first_burst_len	= payload_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	if (!conn->sess->sess_ops->RDMAExtensions &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	     cmd->data_direction == DMA_FROM_DEVICE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		struct iscsi_datain_req *dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		dr = iscsit_allocate_datain_req();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		if (!dr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 			return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 					ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		iscsit_attach_datain_req(cmd, dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 			conn->sess->se_sess, be32_to_cpu(hdr->data_length),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 			cmd->data_direction, sam_task_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 			cmd->sense_buffer + 2, scsilun_to_int(&hdr->lun));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		" ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	target_get_sess_cmd(&cmd->se_cmd, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	cmd->sense_reason = target_cmd_init_cdb(&cmd->se_cmd, hdr->cdb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	if (cmd->sense_reason) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 			return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		goto attach_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	if (cmd->sense_reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		goto attach_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	cmd->sense_reason = target_cmd_parse_cdb(&cmd->se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	if (cmd->sense_reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		goto attach_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) attach_cmd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	spin_lock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	spin_unlock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	 * Check if we need to delay processing because of ALUA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	 * Active/NonOptimized primary access state..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	core_alua_check_nonop_delay(&cmd->se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) void iscsit_set_unsolicited_dataout(struct iscsi_cmd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	iscsit_set_dataout_sequence_values(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	spin_lock_bh(&cmd->dataout_timeout_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	iscsit_start_dataout_timer(cmd, cmd->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	spin_unlock_bh(&cmd->dataout_timeout_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) EXPORT_SYMBOL(iscsit_set_unsolicited_dataout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 			    struct iscsi_scsi_req *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	int cmdsn_ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	 * the Immediate Bit is not set, and no Immediate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	 * Data is attached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	 * A PDU/CmdSN carrying Immediate Data can only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	 * be processed after the DataCRC has passed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	 * If the DataCRC fails, the CmdSN MUST NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	 * be acknowledged. (See below)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	if (!cmd->immediate_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 					(unsigned char *)hdr, hdr->cmdsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 			target_put_sess_cmd(&cmd->se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	 * If no Immediate Data is attached, it's OK to return now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	if (!cmd->immediate_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		if (!cmd->sense_reason && cmd->unsolicited_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 			iscsit_set_unsolicited_dataout(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		if (!cmd->sense_reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 		target_put_sess_cmd(&cmd->se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	 * Early CHECK_CONDITIONs with ImmediateData never make it to command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	 * execution.  These exceptions are processed in CmdSN order using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	if (cmd->sense_reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	 * Call directly into transport_generic_new_cmd() to perform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	 * the backend memory allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	if (cmd->sense_reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) EXPORT_SYMBOL(iscsit_process_scsi_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 			  bool dump_payload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	if (dump_payload) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		u32 length = min(cmd->se_cmd.data_length - cmd->write_data_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 				 cmd->first_burst_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		pr_debug("Dumping min(%d - %d, %d) = %d bytes of immediate data\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 			 cmd->se_cmd.data_length, cmd->write_data_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 			 cmd->first_burst_len, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		rc = iscsit_dump_data_payload(cmd->conn, length, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		pr_debug("Finished dumping immediate data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 			immed_ret = IMMEDIATE_DATA_CANNOT_RECOVER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		immed_ret = iscsit_handle_immediate_data(cmd, hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 							 cmd->first_burst_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		 * A PDU/CmdSN carrying Immediate Data passed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		 * DataCRC, check against ExpCmdSN/MaxCmdSN if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		 * Immediate Bit is not set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 					(unsigned char *)hdr, hdr->cmdsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 			target_put_sess_cmd(&cmd->se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		} else if (cmd->unsolicited_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 			iscsit_set_unsolicited_dataout(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	} else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		 * Immediate Data failed DataCRC and ERL>=1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		 * silently drop this PDU and let the initiator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		 * plug the CmdSN gap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		 * FIXME: Send Unsolicited NOPIN with reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		 * TTT here to help the initiator figure out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		 * the missing CmdSN, although they should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		 * intelligent enough to determine the missing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		 * CmdSN and issue a retry to plug the sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		cmd->i_state = ISTATE_REMOVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 		iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	} else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 			   unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	int rc, immed_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	bool dump_payload = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	 * Allocation iovecs needed for struct socket operations for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	 * traditional iSCSI block I/O.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	if (iscsit_allocate_iovecs(cmd) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		return iscsit_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	immed_data = cmd->immediate_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	else if (rc > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		dump_payload = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	if (!immed_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	return iscsit_get_immediate_data(cmd, hdr, dump_payload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) static u32 iscsit_do_crypto_hash_sg(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	struct ahash_request *hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	u32 data_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	u32 data_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	u32 padding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	u8 *pad_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	u32 data_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	unsigned int page_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	crypto_ahash_init(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	sg = cmd->first_data_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	page_off = cmd->first_data_sg_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	if (data_length && page_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		struct scatterlist first_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		u32 len = min_t(u32, data_length, sg->length - page_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		sg_init_table(&first_sg, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		sg_set_page(&first_sg, sg_page(sg), len, sg->offset + page_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		ahash_request_set_crypt(hash, &first_sg, NULL, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		crypto_ahash_update(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		data_length -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		sg = sg_next(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	while (data_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 		u32 cur_len = min_t(u32, data_length, sg->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		ahash_request_set_crypt(hash, sg, NULL, cur_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		crypto_ahash_update(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		data_length -= cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		/* iscsit_map_iovec has already checked for invalid sg pointers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 		sg = sg_next(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	if (padding) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		struct scatterlist pad_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		sg_init_one(&pad_sg, pad_bytes, padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 					padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		crypto_ahash_finup(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		crypto_ahash_final(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	return data_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) static void iscsit_do_crypto_hash_buf(struct ahash_request *hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	const void *buf, u32 payload_length, u32 padding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	const void *pad_bytes, void *data_crc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	struct scatterlist sg[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	sg_init_table(sg, ARRAY_SIZE(sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	sg_set_buf(sg, buf, payload_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	if (padding)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 		sg_set_buf(sg + 1, pad_bytes, padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	crypto_ahash_digest(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) __iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 			   struct iscsi_cmd *cmd, u32 payload_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 			   bool *success)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	struct iscsi_data *hdr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	struct se_cmd *se_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	/* iSCSI write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	atomic_long_add(payload_length, &conn->sess->rx_data_octets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		" DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		payload_length, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		pr_err("Command ITT: 0x%08x received DataOUT after"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 			" last DataOUT received, dumping payload\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 			cmd->init_task_tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 		return iscsit_dump_data_payload(conn, payload_length, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	if (cmd->data_direction != DMA_TO_DEVICE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		pr_err("Command ITT: 0x%08x received DataOUT for a"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 			" NON-WRITE command.\n", cmd->init_task_tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		return iscsit_dump_data_payload(conn, payload_length, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	se_cmd = &cmd->se_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	iscsit_mod_dataout_timer(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		pr_err("DataOut Offset: %u, Length %u greater than iSCSI Command EDTL %u, protocol error.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		       be32_to_cpu(hdr->offset), payload_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		       cmd->se_cmd.data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	if (cmd->unsolicited_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		int dump_unsolicited_data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		if (conn->sess->sess_ops->InitialR2T) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 			pr_err("Received unexpected unsolicited data"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 				" while InitialR2T=Yes, protocol error.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 			transport_send_check_condition_and_sense(&cmd->se_cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 					TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		 * Special case for dealing with Unsolicited DataOUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		 * and Unsupported SAM WRITE Opcodes and SE resource allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		 * failures;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 		/* Something's amiss if we're not in WRITE_PENDING state... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 		if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 			dump_unsolicited_data = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 		if (dump_unsolicited_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 			 * Check if a delayed TASK_ABORTED status needs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 			 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 			 * received with the unsolicited data out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 			if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 				iscsit_stop_dataout_timer(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 			return iscsit_dump_data_payload(conn, payload_length, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 		 * For the normal solicited data path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		 * Check for a delayed TASK_ABORTED status and dump any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		 * incoming data out payload if one exists.  Also, when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		 * data out sequence, we decrement outstanding_r2ts.  Once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 		 * outstanding_r2ts reaches zero, go ahead and send the delayed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 		 * TASK_ABORTED status.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 		if (se_cmd->transport_state & CMD_T_ABORTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 			if (hdr->flags & ISCSI_FLAG_CMD_FINAL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 			    --cmd->outstanding_r2ts < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 				iscsit_stop_dataout_timer(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 			return iscsit_dump_data_payload(conn, payload_length, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	 * within-command recovery checks before receiving the payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	rc = iscsit_check_pre_dataout(cmd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	else if (rc == DATAOUT_CANNOT_RECOVER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	*success = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) EXPORT_SYMBOL(__iscsit_check_dataout_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 			 struct iscsi_cmd **out_cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	struct iscsi_data *hdr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	struct iscsi_cmd *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	u32 payload_length = ntoh24(hdr->dlength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	bool success = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	if (!payload_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		pr_err_ratelimited("DataSegmentLength: %u is greater than"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 			" MaxXmitDataSegmentLength: %u\n", payload_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 			conn->conn_ops->MaxXmitDataSegmentLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, payload_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	rc = __iscsit_check_dataout_hdr(conn, buf, cmd, payload_length, &success);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	if (success)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		*out_cmd = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) EXPORT_SYMBOL(iscsit_check_dataout_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 		   struct iscsi_data *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	struct kvec *iov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	u32 payload_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	int iov_ret, data_crc_failed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	payload_length = min_t(u32, cmd->se_cmd.data_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 			       ntoh24(hdr->dlength));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	rx_size += payload_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	iov = &cmd->iov_data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	iov_ret = iscsit_map_iovec(cmd, iov, cmd->orig_iov_data_count - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 				   be32_to_cpu(hdr->offset), payload_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	if (iov_ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	iov_count += iov_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	padding = ((-payload_length) & 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	if (padding != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		iov[iov_count].iov_base	= cmd->pad_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 		iov[iov_count++].iov_len = padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 		rx_size += padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 		pr_debug("Receiving %u padding bytes.\n", padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	if (conn->conn_ops->DataDigest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 		iov[iov_count].iov_base = &checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		iov[iov_count++].iov_len = ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 		rx_size += ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	WARN_ON_ONCE(iov_count > cmd->orig_iov_data_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	iscsit_unmap_iovec(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	if (rx_got != rx_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	if (conn->conn_ops->DataDigest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		u32 data_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 						    be32_to_cpu(hdr->offset),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 						    payload_length, padding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 						    cmd->pad_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		if (checksum != data_crc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 			pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 				" DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 				" does not match computed 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 				hdr->itt, hdr->offset, payload_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 				hdr->datasn, checksum, data_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 			data_crc_failed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 			pr_debug("Got CRC32C DataDigest 0x%08x for"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 				" %u bytes of Data Out\n", checksum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 				payload_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	return data_crc_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 			     bool data_crc_failed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	struct iscsi_conn *conn = cmd->conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	int rc, ooo_cmdsn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	 * Increment post receive data and CRC values or perform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	 * within-command recovery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	else if (rc == DATAOUT_SEND_R2T) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 		iscsit_set_dataout_sequence_values(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	} else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		 * Handle extra special case for out of order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		 * Unsolicited Data Out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		spin_lock_bh(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		spin_unlock_bh(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		iscsit_stop_dataout_timer(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		if (ooo_cmdsn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 		target_execute_cmd(&cmd->se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	} else /* DATAOUT_CANNOT_RECOVER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) EXPORT_SYMBOL(iscsit_check_dataout_payload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	struct iscsi_cmd *cmd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	bool data_crc_failed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	else if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	rc = iscsit_get_dataout(conn, cmd, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	else if (rc > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 		data_crc_failed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 			 struct iscsi_nopout *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	u32 payload_length = ntoh24(hdr->dlength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 			return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 						 (unsigned char *)hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 		
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 		return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 					 (unsigned char *)hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 		pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 			" not set, protocol error.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 			return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 						 (unsigned char *)hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 					 (unsigned char *)hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 			" greater than MaxXmitDataSegmentLength: %u, protocol"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 			" error.\n", payload_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 			conn->conn_ops->MaxXmitDataSegmentLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 			return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 						 (unsigned char *)hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 					 (unsigned char *)hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 		hdr->itt == RESERVED_ITT ? "Response" : "Request",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 		hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 		payload_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	 * This is not a response to a Unsolicited NopIN, which means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	 * it can either be a NOPOUT ping request (with a valid ITT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	 * Either way, make sure we allocate an struct iscsi_cmd, as both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	 * can contain ping data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		cmd->iscsi_opcode	= ISCSI_OP_NOOP_OUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		cmd->i_state		= ISTATE_SEND_NOPIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 		cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 						1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 		conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		cmd->targ_xfer_tag	= 0xFFFFFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 		cmd->cmd_sn		= be32_to_cpu(hdr->cmdsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		cmd->exp_stat_sn	= be32_to_cpu(hdr->exp_statsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 		cmd->data_direction	= DMA_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) EXPORT_SYMBOL(iscsit_setup_nop_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 			   struct iscsi_nopout *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	struct iscsi_cmd *cmd_p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	int cmdsn_ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	 * Initiator is expecting a NopIN ping reply..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	if (hdr->itt != RESERVED_ITT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 		if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 			return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 						(unsigned char *)hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 		spin_lock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 		list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 		spin_unlock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 			iscsit_add_cmd_to_response_queue(cmd, conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 							 cmd->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 				(unsigned char *)hdr, hdr->cmdsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808)                 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	 * This was a response to a unsolicited NOPIN ping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 		cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		if (!cmd_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 		iscsit_stop_nopin_response_timer(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		cmd_p->i_state = ISTATE_REMOVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 		iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 		iscsit_start_nopin_timer(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	 * Otherwise, initiator is not expecting a NOPIN is response.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	 * Just ignore for now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	if (cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		iscsit_free_cmd(cmd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)         return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) EXPORT_SYMBOL(iscsit_process_nop_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 				 unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	unsigned char *ping_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	struct kvec *iov = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	u32 payload_length = ntoh24(hdr->dlength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	ret = iscsit_setup_nop_out(conn, cmd, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	 * Handle NOP-OUT payload for traditional iSCSI sockets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		u32 checksum, data_crc, padding = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 		int niov = 0, rx_got, rx_size = payload_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 		ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 		if (!ping_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 			ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 		iov = &cmd->iov_misc[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 		iov[niov].iov_base	= ping_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 		iov[niov++].iov_len	= payload_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		padding = ((-payload_length) & 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 		if (padding != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 			pr_debug("Receiving %u additional bytes"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 				" for padding.\n", padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 			iov[niov].iov_base	= &cmd->pad_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 			iov[niov++].iov_len	= padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 			rx_size += padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 		if (conn->conn_ops->DataDigest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 			iov[niov].iov_base	= &checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 			iov[niov++].iov_len	= ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 			rx_size += ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 		WARN_ON_ONCE(niov > ARRAY_SIZE(cmd->iov_misc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 		rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		if (rx_got != rx_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 			ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 		if (conn->conn_ops->DataDigest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 			iscsit_do_crypto_hash_buf(conn->conn_rx_hash, ping_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 						  payload_length, padding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 						  cmd->pad_bytes, &data_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 			if (checksum != data_crc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 				pr_err("Ping data CRC32C DataDigest"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 				" 0x%08x does not match computed 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 					checksum, data_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 				if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 					pr_err("Unable to recover from"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 					" NOPOUT Ping DataCRC failure while in"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 						" ERL=0.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 					ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 					/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 					 * Silently drop this PDU and let the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 					 * initiator plug the CmdSN gap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 					pr_debug("Dropping NOPOUT"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 					" Command CmdSN: 0x%08x due to"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 					" DataCRC error.\n", hdr->cmdsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 					ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 				pr_debug("Got CRC32C DataDigest"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 				" 0x%08x for %u bytes of ping data.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 					checksum, payload_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 		ping_data[payload_length] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 		 * Attach ping data to struct iscsi_cmd->buf_ptr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		cmd->buf_ptr = ping_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 		cmd->buf_ptr_size = payload_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 		pr_debug("Got %u bytes of NOPOUT ping"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 			" data.\n", payload_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 		pr_debug("Ping Data: \"%s\"\n", ping_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	return iscsit_process_nop_out(conn, cmd, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	if (cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 		iscsit_free_cmd(cmd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	kfree(ping_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) static enum tcm_tmreq_table iscsit_convert_tmf(u8 iscsi_tmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 	switch (iscsi_tmf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	case ISCSI_TM_FUNC_ABORT_TASK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 		return TMR_ABORT_TASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	case ISCSI_TM_FUNC_ABORT_TASK_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 		return TMR_ABORT_TASK_SET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	case ISCSI_TM_FUNC_CLEAR_ACA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 		return TMR_CLEAR_ACA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	case ISCSI_TM_FUNC_CLEAR_TASK_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 		return TMR_CLEAR_TASK_SET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 		return TMR_LUN_RESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 		return TMR_TARGET_WARM_RESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	case ISCSI_TM_FUNC_TARGET_COLD_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 		return TMR_TARGET_COLD_RESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 		return TMR_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 			   unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	struct se_tmr_req *se_tmr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	struct iscsi_tmr_req *tmr_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	struct iscsi_tm *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	int out_of_order_cmdsn = 0, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	u8 function, tcm_function = TMR_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	hdr			= (struct iscsi_tm *) buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	function = hdr->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 		" 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 		" 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 		hdr->rtt, hdr->refcmdsn, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	    ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	     hdr->rtt != RESERVED_ITT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 		pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		hdr->rtt = RESERVED_ITT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 			!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 		pr_err("Task Management Request TASK_REASSIGN not"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 			" issued as immediate command, bad iSCSI Initiator"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 				"implementation\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 		return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 					     ISCSI_REASON_PROTOCOL_ERROR, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 	if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	    be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	cmd->data_direction = DMA_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	cmd->tmr_req = kzalloc(sizeof(*cmd->tmr_req), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	if (!cmd->tmr_req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 					     ISCSI_REASON_BOOKMARK_NO_RESOURCES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 					     buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 	transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 			      conn->sess->se_sess, 0, DMA_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 			      TCM_SIMPLE_TAG, cmd->sense_buffer + 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 			      scsilun_to_int(&hdr->lun));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	target_get_sess_cmd(&cmd->se_cmd, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 	 * TASK_REASSIGN for ERL=2 / connection stays inside of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	 * LIO-Target $FABRIC_MOD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 	if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 		tcm_function = iscsit_convert_tmf(function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 		if (tcm_function == TMR_UNKNOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 			pr_err("Unknown iSCSI TMR Function:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 			       " 0x%02x\n", function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 			return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, tcm_function,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 				 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	cmd->iscsi_opcode	= ISCSI_OP_SCSI_TMFUNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	cmd->i_state		= ISTATE_SEND_TASKMGTRSP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 	cmd->init_task_tag	= hdr->itt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 	cmd->targ_xfer_tag	= 0xFFFFFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	cmd->cmd_sn		= be32_to_cpu(hdr->cmdsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	cmd->exp_stat_sn	= be32_to_cpu(hdr->exp_statsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	se_tmr			= cmd->se_cmd.se_tmr_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	tmr_req			= cmd->tmr_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 	 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 		ret = transport_lookup_tmr_lun(&cmd->se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 			se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 			goto attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	switch (function) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 	case ISCSI_TM_FUNC_ABORT_TASK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 		se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 		if (se_tmr->response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 			goto attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	case ISCSI_TM_FUNC_ABORT_TASK_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	case ISCSI_TM_FUNC_CLEAR_ACA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	case ISCSI_TM_FUNC_CLEAR_TASK_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 		if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 			se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 			goto attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	case ISCSI_TM_FUNC_TARGET_COLD_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 		if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 			se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 			goto attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	case ISCSI_TM_FUNC_TASK_REASSIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 		se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 		 * Perform sanity checks on the ExpDataSN only if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 		 * TASK_REASSIGN was successful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 		if (se_tmr->response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 		if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 			return iscsit_add_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 					ISCSI_REASON_BOOKMARK_INVALID, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 		pr_err("Unknown TMR function: 0x%02x, protocol"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 			" error.\n", function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 		se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 		goto attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	    (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 		se_tmr->call_transport = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) attach:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	spin_lock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	spin_unlock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 		int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 		if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 			out_of_order_cmdsn = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 		} else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 			target_put_sess_cmd(&cmd->se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 		} else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	 * Found the referenced task, send to transport for processing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	if (se_tmr->call_transport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 		return transport_generic_handle_tmr(&cmd->se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	 * Could not find the referenced LUN, task, or Task Management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	 * command not authorized or supported.  Change state and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 	 * let the tx_thread send the response.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	 * For connection recovery, this is also the default action for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	 * TMR TASK_REASSIGN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 	target_put_sess_cmd(&cmd->se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) /* #warning FIXME: Support Text Command parameters besides SendTargets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 		      struct iscsi_text *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 	u32 payload_length = ntoh24(hdr->dlength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 		pr_err("Unable to accept text parameter length: %u"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 			"greater than MaxXmitDataSegmentLength %u.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 		       payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 		return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 					 (unsigned char *)hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 	if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	     (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 		pr_err("Multi sequence text commands currently not supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 		return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 					(unsigned char *)hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 	pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 		" ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 		hdr->exp_statsn, payload_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	cmd->iscsi_opcode	= ISCSI_OP_TEXT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	cmd->i_state		= ISTATE_SEND_TEXTRSP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 	cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	conn->sess->init_task_tag = cmd->init_task_tag  = hdr->itt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	cmd->targ_xfer_tag	= 0xFFFFFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 	cmd->cmd_sn		= be32_to_cpu(hdr->cmdsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 	cmd->exp_stat_sn	= be32_to_cpu(hdr->exp_statsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	cmd->data_direction	= DMA_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 	kfree(cmd->text_in_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 	cmd->text_in_ptr	= NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) EXPORT_SYMBOL(iscsit_setup_text_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 			struct iscsi_text *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 	unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	int cmdsn_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	if (!text_in) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 		cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 		if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 			pr_err("Unable to locate text_in buffer for sendtargets"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 			       " discovery\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 			goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 		goto empty_sendtargets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 	if (strncmp("SendTargets=", text_in, 12) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 		pr_err("Received Text Data that is not"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 			" SendTargets, cannot continue.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 		goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 	/* '=' confirmed in strncmp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	text_ptr = strchr(text_in, '=');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	BUG_ON(!text_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	if (!strncmp("=All", text_ptr, 5)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 		cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	} else if (!strncmp("=iqn.", text_ptr, 5) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 		   !strncmp("=eui.", text_ptr, 5)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 		cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 		pr_err("Unable to locate valid SendTargets%s value\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 		       text_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 		goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	spin_lock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	spin_unlock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) empty_sendtargets:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 				(unsigned char *)hdr, hdr->cmdsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 	return iscsit_execute_cmd(cmd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) reject:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 				 (unsigned char *)hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) EXPORT_SYMBOL(iscsit_process_text_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 		       unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	struct iscsi_text *hdr = (struct iscsi_text *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	char *text_in = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 	u32 payload_length = ntoh24(hdr->dlength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	int rx_size, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 	rx_size = payload_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 	if (payload_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 		u32 checksum = 0, data_crc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 		u32 padding = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		int niov = 0, rx_got;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 		struct kvec iov[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 		rx_size = ALIGN(payload_length, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 		text_in = kzalloc(rx_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 		if (!text_in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 			goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 		cmd->text_in_ptr = text_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 		memset(iov, 0, sizeof(iov));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 		iov[niov].iov_base	= text_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 		iov[niov++].iov_len	= rx_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 		padding = rx_size - payload_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 		if (padding)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 			pr_debug("Receiving %u additional bytes"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 					" for padding.\n", padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 		if (conn->conn_ops->DataDigest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 			iov[niov].iov_base	= &checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 			iov[niov++].iov_len	= ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 			rx_size += ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 		WARN_ON_ONCE(niov > ARRAY_SIZE(iov));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 		rx_got = rx_data(conn, &iov[0], niov, rx_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 		if (rx_got != rx_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 			goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 		if (conn->conn_ops->DataDigest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 			iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 						  text_in, rx_size, 0, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 						  &data_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 			if (checksum != data_crc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 				pr_err("Text data CRC32C DataDigest"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 					" 0x%08x does not match computed"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 					" 0x%08x\n", checksum, data_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 				if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 					pr_err("Unable to recover from"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 					" Text Data digest failure while in"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 						" ERL=0.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 					goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 					/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 					 * Silently drop this PDU and let the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 					 * initiator plug the CmdSN gap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 					pr_debug("Dropping Text"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 					" Command CmdSN: 0x%08x due to"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 					" DataCRC error.\n", hdr->cmdsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 					kfree(text_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 					return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 				pr_debug("Got CRC32C DataDigest"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 					" 0x%08x for %u bytes of text data.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 						checksum, payload_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 		text_in[payload_length - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 		pr_debug("Successfully read %d bytes of text"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 				" data.\n", payload_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	return iscsit_process_text_cmd(conn, cmd, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) reject:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	kfree(cmd->text_in_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	cmd->text_in_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	struct iscsi_conn *conn_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	struct iscsi_session *sess = conn->sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 	pr_debug("Received logout request CLOSESESSION on CID: %hu"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 		" for SID: %u.\n", conn->cid, conn->sess->sid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 	atomic_set(&sess->session_logout, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	atomic_set(&conn->conn_logout_remove, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	iscsit_inc_conn_usage_count(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 	iscsit_inc_session_usage_count(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 	spin_lock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 		if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 		pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 		conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 	struct iscsi_conn *l_conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 	struct iscsi_session *sess = conn->sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 	pr_debug("Received logout request CLOSECONNECTION for CID:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 		" %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 	 * A Logout Request with a CLOSECONNECTION reason code for a CID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 	 * can arrive on a connection with a differing CID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	if (conn->cid == cmd->logout_cid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 		spin_lock_bh(&conn->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 		pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 		conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 		atomic_set(&conn->conn_logout_remove, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 		conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 		iscsit_inc_conn_usage_count(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 		spin_unlock_bh(&conn->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 		 * Handle all different cid CLOSECONNECTION requests in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 		 * iscsit_logout_post_handler_diffcid() as to give enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 		 * time for any non immediate command's CmdSN to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 		 * acknowledged on the connection in question.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 		 * Here we simply make sure the CID is still around.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 		l_conn = iscsit_get_conn_from_cid(sess,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 				cmd->logout_cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 		if (!l_conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 			cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 			iscsit_add_cmd_to_response_queue(cmd, conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 					cmd->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 		iscsit_dec_conn_usage_count(l_conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 	struct iscsi_session *sess = conn->sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 	pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 		" CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 	if (sess->sess_ops->ErrorRecoveryLevel != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 		pr_err("Received Logout Request REMOVECONNFORRECOVERY"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 			" while ERL!=2.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 		cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 		iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	if (conn->cid == cmd->logout_cid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 		pr_err("Received Logout Request REMOVECONNFORRECOVERY"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 			" with CID: %hu on CID: %hu, implementation error.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 				cmd->logout_cid, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 		cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 		iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 			unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 	int cmdsn_ret, logout_remove = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	u8 reason_code = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 	struct iscsi_logout *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 	hdr			= (struct iscsi_logout *) buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 	reason_code		= (hdr->flags & 0x7f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 	if (tiqn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 		spin_lock(&tiqn->logout_stats.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 		if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 			tiqn->logout_stats.normal_logouts++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 			tiqn->logout_stats.abnormal_logouts++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 		spin_unlock(&tiqn->logout_stats.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 	pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 		" ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 		hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 		hdr->cid, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 		pr_err("Received logout request on connection that"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 			" is not in logged in state, ignoring request.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 		iscsit_free_cmd(cmd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	cmd->iscsi_opcode       = ISCSI_OP_LOGOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	cmd->i_state            = ISTATE_SEND_LOGOUTRSP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 	cmd->immediate_cmd      = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 	conn->sess->init_task_tag = cmd->init_task_tag  = hdr->itt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	cmd->targ_xfer_tag      = 0xFFFFFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	cmd->cmd_sn             = be32_to_cpu(hdr->cmdsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	cmd->exp_stat_sn        = be32_to_cpu(hdr->exp_statsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	cmd->logout_cid         = be16_to_cpu(hdr->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	cmd->logout_reason      = reason_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	cmd->data_direction     = DMA_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	 * We need to sleep in these cases (by returning 1) until the Logout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	 * Response gets sent in the tx thread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	   ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	    be16_to_cpu(hdr->cid) == conn->cid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 		logout_remove = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 	spin_lock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 	spin_unlock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 		iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 	 * Immediate commands are executed, well, immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 	 * Non-Immediate Logout Commands are executed in CmdSN order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	if (cmd->immediate_cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 		int ret = iscsit_execute_cmd(cmd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 		if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 			logout_remove = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 		else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	return logout_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) EXPORT_SYMBOL(iscsit_handle_logout_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) int iscsit_handle_snack(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 	struct iscsi_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 	unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	struct iscsi_snack *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	hdr			= (struct iscsi_snack *) buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	hdr->flags		&= ~ISCSI_FLAG_CMD_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 	pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 		" 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 		" CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 			hdr->begrun, hdr->runlength, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 	if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 		pr_err("Initiator sent SNACK request while in"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 			" ErrorRecoveryLevel=0.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 		return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 					 buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 	 * SNACK_DATA and SNACK_R2T are both 0,  so check which function to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	 * call from inside iscsi_send_recovery_datain_or_r2t().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 	switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 		return iscsit_handle_recovery_datain_or_r2t(conn, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 			hdr->itt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 			be32_to_cpu(hdr->ttt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 			be32_to_cpu(hdr->begrun),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 			be32_to_cpu(hdr->runlength));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 	case ISCSI_FLAG_SNACK_TYPE_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 		return iscsit_handle_status_snack(conn, hdr->itt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 			be32_to_cpu(hdr->ttt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 			be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 		return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 			be32_to_cpu(hdr->begrun),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 			be32_to_cpu(hdr->runlength));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 	case ISCSI_FLAG_SNACK_TYPE_RDATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 		/* FIXME: Support R-Data SNACK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 		pr_err("R-Data SNACK Not Supported.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 		return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 					 buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 		pr_err("Unknown SNACK type 0x%02x, protocol"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 			" error.\n", hdr->flags & 0x0f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 		return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 					 buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) EXPORT_SYMBOL(iscsit_handle_snack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 	if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 	    (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 		wait_for_completion_interruptible_timeout(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 					&conn->rx_half_close_comp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 					ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) static int iscsit_handle_immediate_data(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 	struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	struct iscsi_scsi_req *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 	u32 length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	int iov_ret, rx_got = 0, rx_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 	u32 checksum, iov_count = 0, padding = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	struct iscsi_conn *conn = cmd->conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 	struct kvec *iov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	void *overflow_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 	BUG_ON(cmd->write_data_done > cmd->se_cmd.data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 	rx_size = min(cmd->se_cmd.data_length - cmd->write_data_done, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 	iov_ret = iscsit_map_iovec(cmd, cmd->iov_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 				   cmd->orig_iov_data_count - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 				   cmd->write_data_done, rx_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	if (iov_ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 		return IMMEDIATE_DATA_CANNOT_RECOVER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 	iov_count = iov_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 	iov = &cmd->iov_data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 	if (rx_size < length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 		 * Special case: length of immediate data exceeds the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 		 * buffer size derived from the CDB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 		overflow_buf = kmalloc(length - rx_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 		if (!overflow_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 			iscsit_unmap_iovec(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 			return IMMEDIATE_DATA_CANNOT_RECOVER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 		cmd->overflow_buf = overflow_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 		iov[iov_count].iov_base = overflow_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 		iov[iov_count].iov_len = length - rx_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 		iov_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 		rx_size = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 	padding = ((-length) & 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 	if (padding != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 		iov[iov_count].iov_base	= cmd->pad_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 		iov[iov_count++].iov_len = padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 		rx_size += padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 	if (conn->conn_ops->DataDigest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 		iov[iov_count].iov_base		= &checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 		iov[iov_count++].iov_len	= ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 		rx_size += ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 	WARN_ON_ONCE(iov_count > cmd->orig_iov_data_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 	rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 	iscsit_unmap_iovec(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 	if (rx_got != rx_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 		iscsit_rx_thread_wait_for_tcp(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 		return IMMEDIATE_DATA_CANNOT_RECOVER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 	if (conn->conn_ops->DataDigest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 		u32 data_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 		data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 						    cmd->write_data_done, length, padding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 						    cmd->pad_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 		if (checksum != data_crc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 			pr_err("ImmediateData CRC32C DataDigest 0x%08x"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 				" does not match computed 0x%08x\n", checksum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 				data_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 			if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 				pr_err("Unable to recover from"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 					" Immediate Data digest failure while"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 					" in ERL=0.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 				iscsit_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 						ISCSI_REASON_DATA_DIGEST_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 						(unsigned char *)hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 				return IMMEDIATE_DATA_CANNOT_RECOVER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 				iscsit_reject_cmd(cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 						ISCSI_REASON_DATA_DIGEST_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 						(unsigned char *)hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 				return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 			pr_debug("Got CRC32C DataDigest 0x%08x for"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 				" %u bytes of Immediate Data\n", checksum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 				length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	cmd->write_data_done += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 		spin_lock_bh(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 		spin_unlock_bh(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 	return IMMEDIATE_DATA_NORMAL_OPERATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	with active network interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 	struct iscsi_cmd *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 	struct iscsi_conn *conn_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 	lockdep_assert_held(&conn->sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 	 * Only send a Asynchronous Message on connections whos network
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 	 * interface is still functional.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 	list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 		if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 			iscsit_inc_conn_usage_count(conn_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 	if (!found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 	cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 	if (!cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 		iscsit_dec_conn_usage_count(conn_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 	cmd->logout_cid = conn->cid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	cmd->i_state = ISTATE_SEND_ASYNCMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 	spin_lock_bh(&conn_p->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 	list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 	spin_unlock_bh(&conn_p->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 	iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 	iscsit_dec_conn_usage_count(conn_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) static int iscsit_send_conn_drop_async_message(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 	struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 	struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 	struct iscsi_async *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 	cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 	hdr			= (struct iscsi_async *) cmd->pdu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 	hdr->opcode		= ISCSI_OP_ASYNC_EVENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 	hdr->flags		= ISCSI_FLAG_CMD_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 	cmd->init_task_tag	= RESERVED_ITT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 	cmd->targ_xfer_tag	= 0xFFFFFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 	cmd->stat_sn		= conn->stat_sn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 	hdr->async_event	= ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 	hdr->param1		= cpu_to_be16(cmd->logout_cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 	hdr->param2		= cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 	hdr->param3		= cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 	pr_debug("Sending Connection Dropped Async Message StatSN:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 		" 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 			cmd->logout_cid, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 	if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 	    (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 		wait_for_completion_interruptible_timeout(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 					&conn->tx_half_close_comp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 					ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 			struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 			bool set_statsn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 	hdr->opcode		= ISCSI_OP_SCSI_DATA_IN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 	hdr->flags		= datain->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 	if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 		if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 			hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 			hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 		} else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 			hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 			hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 	hton24(hdr->dlength, datain->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 	if (hdr->flags & ISCSI_FLAG_DATA_ACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 		int_to_scsilun(cmd->se_cmd.orig_fe_lun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 				(struct scsi_lun *)&hdr->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 		put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 	hdr->itt		= cmd->init_task_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 	if (hdr->flags & ISCSI_FLAG_DATA_ACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 		hdr->ttt		= cpu_to_be32(cmd->targ_xfer_tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 		hdr->ttt		= cpu_to_be32(0xFFFFFFFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 	if (set_statsn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 		hdr->statsn		= cpu_to_be32(cmd->stat_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 		hdr->statsn		= cpu_to_be32(0xFFFFFFFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	hdr->datasn		= cpu_to_be32(datain->data_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 	hdr->offset		= cpu_to_be32(datain->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 	pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 		" DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 		cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 		ntohl(hdr->offset), datain->length, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) EXPORT_SYMBOL(iscsit_build_datain_pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 	struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 	struct iscsi_datain datain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 	struct iscsi_datain_req *dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 	int eodr = 0, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 	bool set_statsn = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 	memset(&datain, 0, sizeof(struct iscsi_datain));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 	dr = iscsit_get_datain_values(cmd, &datain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 	if (!dr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 		pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 				cmd->init_task_tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 	 * Be paranoid and double check the logic for now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 	if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 		pr_err("Command ITT: 0x%08x, datain.offset: %u and"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 			" datain.length: %u exceeds cmd->data_length: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 			cmd->init_task_tag, datain.offset, datain.length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 			cmd->se_cmd.data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 	atomic_long_add(datain.length, &conn->sess->tx_data_octets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 	 * Special case for successfully execution w/ both DATAIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 	 * and Sense Data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 	if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 	    (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 		datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 		if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 		    (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 			iscsit_increment_maxcmdsn(cmd, conn->sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 			cmd->stat_sn = conn->stat_sn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 			set_statsn = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 		} else if (dr->dr_complete ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 			   DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 			set_statsn = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 	iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 	ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 	if (dr->dr_complete) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 		eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 				2 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 		iscsit_free_datain_req(cmd, dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 	return eodr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 			struct iscsi_logout_rsp *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 	struct iscsi_conn *logout_conn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 	struct iscsi_conn_recovery *cr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 	struct iscsi_session *sess = conn->sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 	 * The actual shutting down of Sessions and/or Connections
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 	 * for CLOSESESSION and CLOSECONNECTION Logout Requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 	 * is done in scsi_logout_post_handler().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 	switch (cmd->logout_reason) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 	case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 		pr_debug("iSCSI session logout successful, setting"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 			" logout response to ISCSI_LOGOUT_SUCCESS.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 		cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 	case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 		if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 		 * For CLOSECONNECTION logout requests carrying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 		 * a matching logout CID -> local CID, the reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 		 * for the local CID will have been incremented in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 		 * iscsi_logout_closeconnection().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 		 * For CLOSECONNECTION logout requests carrying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 		 * a different CID than the connection it arrived
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 		 * on, the connection responding to cmd->logout_cid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 		 * is stopped in iscsit_logout_post_handler_diffcid().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 		pr_debug("iSCSI CID: %hu logout on CID: %hu"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 			" successful.\n", cmd->logout_cid, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 		cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 	case ISCSI_LOGOUT_REASON_RECOVERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 		if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 		    (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 		 * If the connection is still active from our point of view
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 		 * force connection recovery to occur.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 		logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 				cmd->logout_cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 		if (logout_conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 			iscsit_connection_reinstatement_rcfr(logout_conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 			iscsit_dec_conn_usage_count(logout_conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 		cr = iscsit_get_inactive_connection_recovery_entry(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 				conn->sess, cmd->logout_cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 		if (!cr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 			pr_err("Unable to locate CID: %hu for"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 			" REMOVECONNFORRECOVERY Logout Request.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 				cmd->logout_cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 			cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 		iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 		pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 			" for recovery for CID: %hu on CID: %hu successful.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 				cmd->logout_cid, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 		cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 		pr_err("Unknown cmd->logout_reason: 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 				cmd->logout_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 	hdr->opcode		= ISCSI_OP_LOGOUT_RSP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 	hdr->response		= cmd->logout_response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 	hdr->itt		= cmd->init_task_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 	cmd->stat_sn		= conn->stat_sn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 	iscsit_increment_maxcmdsn(cmd, conn->sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 	pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 		" 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 		cmd->init_task_tag, cmd->stat_sn, hdr->response,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 		cmd->logout_cid, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) EXPORT_SYMBOL(iscsit_build_logout_rsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 	rc = iscsit_build_logout_rsp(cmd, conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 			(struct iscsi_logout_rsp *)&cmd->pdu[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 		       struct iscsi_nopin *hdr, bool nopout_response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 	hdr->opcode		= ISCSI_OP_NOOP_IN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001)         hton24(hdr->dlength, cmd->buf_ptr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 	if (nopout_response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 		put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 	hdr->itt		= cmd->init_task_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 	hdr->ttt		= cpu_to_be32(cmd->targ_xfer_tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 	cmd->stat_sn		= (nopout_response) ? conn->stat_sn++ :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 				  conn->stat_sn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 	if (nopout_response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 		iscsit_increment_maxcmdsn(cmd, conn->sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 	pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 		" StatSN: 0x%08x, Length %u\n", (nopout_response) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 		"Solicited" : "Unsolicited", cmd->init_task_tag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 		cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) EXPORT_SYMBOL(iscsit_build_nopin_rsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024)  *	Unsolicited NOPIN, either requesting a response or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) static int iscsit_send_unsolicited_nopin(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 	struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 	struct iscsi_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 	int want_response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 	struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 	iscsit_build_nopin_rsp(cmd, conn, hdr, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 	pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 		" 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 	ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 	spin_lock_bh(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 	cmd->i_state = want_response ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 		ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 	spin_unlock_bh(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 	struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 	iscsit_build_nopin_rsp(cmd, conn, hdr, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 	 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 	 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 	pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 						     cmd->buf_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 						     cmd->buf_ptr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) static int iscsit_send_r2t(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 	struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 	struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 	struct iscsi_r2t *r2t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 	struct iscsi_r2t_rsp *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 	r2t = iscsit_get_r2t_from_list(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 	if (!r2t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 	hdr			= (struct iscsi_r2t_rsp *) cmd->pdu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 	memset(hdr, 0, ISCSI_HDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 	hdr->opcode		= ISCSI_OP_R2T;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 	int_to_scsilun(cmd->se_cmd.orig_fe_lun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 			(struct scsi_lun *)&hdr->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 	hdr->itt		= cmd->init_task_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 	if (conn->conn_transport->iscsit_get_r2t_ttt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 		conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 		r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 	hdr->ttt		= cpu_to_be32(r2t->targ_xfer_tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 	hdr->statsn		= cpu_to_be32(conn->stat_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 	hdr->r2tsn		= cpu_to_be32(r2t->r2t_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 	hdr->data_offset	= cpu_to_be32(r2t->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 	hdr->data_length	= cpu_to_be32(r2t->xfer_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 	pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 		" 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 		(!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 		r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 			r2t->offset, r2t->xfer_len, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 	spin_lock_bh(&cmd->r2t_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 	r2t->sent_r2t = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 	spin_unlock_bh(&cmd->r2t_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 	ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 	spin_lock_bh(&cmd->dataout_timeout_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 	iscsit_start_dataout_timer(cmd, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 	spin_unlock_bh(&cmd->dataout_timeout_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123)  *	@recovery: If called from iscsi_task_reassign_complete_write() for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124)  *		connection recovery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) int iscsit_build_r2ts_for_cmd(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 	struct iscsi_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) 	struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 	bool recovery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 	int first_r2t = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 	u32 offset = 0, xfer_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 	spin_lock_bh(&cmd->r2t_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 	if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 		spin_unlock_bh(&cmd->r2t_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 	if (conn->sess->sess_ops->DataSequenceInOrder &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) 	    !recovery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 		cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 	while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 		if (conn->sess->sess_ops->DataSequenceInOrder) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 			offset = cmd->r2t_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 			if (first_r2t && recovery) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 				int new_data_end = offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 					conn->sess->sess_ops->MaxBurstLength -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 					cmd->next_burst_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 				if (new_data_end > cmd->se_cmd.data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 					xfer_len = cmd->se_cmd.data_length - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 					xfer_len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 						conn->sess->sess_ops->MaxBurstLength -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) 						cmd->next_burst_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) 				int new_data_end = offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 					conn->sess->sess_ops->MaxBurstLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 				if (new_data_end > cmd->se_cmd.data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 					xfer_len = cmd->se_cmd.data_length - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 					xfer_len = conn->sess->sess_ops->MaxBurstLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 			if ((s32)xfer_len < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 				cmd->cmd_flags |= ICF_SENT_LAST_R2T;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) 			cmd->r2t_offset += xfer_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) 			if (cmd->r2t_offset == cmd->se_cmd.data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) 				cmd->cmd_flags |= ICF_SENT_LAST_R2T;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) 			struct iscsi_seq *seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 			seq = iscsit_get_seq_holder_for_r2t(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) 			if (!seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 				spin_unlock_bh(&cmd->r2t_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) 			offset = seq->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) 			xfer_len = seq->xfer_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) 			if (cmd->seq_send_order == cmd->seq_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 				cmd->cmd_flags |= ICF_SENT_LAST_R2T;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 		cmd->outstanding_r2ts++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 		first_r2t = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 		if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 			spin_unlock_bh(&cmd->r2t_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 		if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) 	spin_unlock_bh(&cmd->r2t_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 			bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 	if (inc_stat_sn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 		cmd->stat_sn = conn->stat_sn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 	atomic_long_inc(&conn->sess->rsp_pdus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 	memset(hdr, 0, ISCSI_HDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 	hdr->opcode		= ISCSI_OP_SCSI_CMD_RSP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 	if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 		hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 		hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 	} else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 		hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 		hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 	hdr->response		= cmd->iscsi_response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 	hdr->cmd_status		= cmd->se_cmd.scsi_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) 	hdr->itt		= cmd->init_task_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 	iscsit_increment_maxcmdsn(cmd, conn->sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) 	pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) 		" Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) 		cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) 		cmd->se_cmd.scsi_status, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) EXPORT_SYMBOL(iscsit_build_rsp_pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 	bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) 	void *data_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 	u32 padding = 0, data_buf_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 	iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 	 * Attach SENSE DATA payload to iSCSI Response PDU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 	if (cmd->se_cmd.sense_buffer &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) 	   ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 		cmd->se_cmd.scsi_sense_length += sizeof (__be16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 		padding		= -(cmd->se_cmd.scsi_sense_length) & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 		data_buf = cmd->sense_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 		data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 		if (padding) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 			memset(cmd->sense_buffer +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 				cmd->se_cmd.scsi_sense_length, 0, padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 			pr_debug("Adding %u bytes of padding to"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 				" SENSE.\n", padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 		pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) 				" Response PDU\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 				cmd->se_cmd.scsi_sense_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 						     data_buf_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) 	switch (se_tmr->response) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 	case TMR_FUNCTION_COMPLETE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) 		return ISCSI_TMF_RSP_COMPLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) 	case TMR_TASK_DOES_NOT_EXIST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) 		return ISCSI_TMF_RSP_NO_TASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 	case TMR_LUN_DOES_NOT_EXIST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) 		return ISCSI_TMF_RSP_NO_LUN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) 	case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) 		return ISCSI_TMF_RSP_NOT_SUPPORTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) 	case TMR_FUNCTION_REJECTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 		return ISCSI_TMF_RSP_REJECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 			  struct iscsi_tm_rsp *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 	struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 	hdr->opcode		= ISCSI_OP_SCSI_TMFUNC_RSP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) 	hdr->flags		= ISCSI_FLAG_CMD_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) 	hdr->response		= iscsit_convert_tcm_tmr_rsp(se_tmr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) 	hdr->itt		= cmd->init_task_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 	cmd->stat_sn		= conn->stat_sn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) 	iscsit_increment_maxcmdsn(cmd, conn->sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) 	pr_debug("Built Task Management Response ITT: 0x%08x,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) 		" StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) 		cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) 	struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 	iscsit_build_task_mgt_rsp(cmd, conn, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) #define SENDTARGETS_BUF_LIMIT 32768U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 				  enum iscsit_transport_type network_transport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) 				  int skip_bytes, bool *completed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) 	char *payload = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) 	struct iscsi_conn *conn = cmd->conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) 	struct iscsi_portal_group *tpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) 	struct iscsi_tiqn *tiqn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 	struct iscsi_tpg_np *tpg_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) 	int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 	int target_name_printed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) 	unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) 	unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) 	bool active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 	buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) 			 SENDTARGETS_BUF_LIMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) 	payload = kzalloc(buffer_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 	if (!payload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) 	 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) 	 * explicit case..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 	if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 		text_ptr = strchr(text_in, '=');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 		if (!text_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) 			pr_err("Unable to locate '=' string in text_in:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 			       " %s\n", text_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 			kfree(payload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) 		 * Skip over '=' character..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) 		text_ptr += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) 	spin_lock(&tiqn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) 	list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 		if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) 		     strcmp(tiqn->tiqn, text_ptr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) 		target_name_printed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) 		spin_lock(&tiqn->tiqn_tpg_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 		list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) 			/* If demo_mode_discovery=0 and generate_node_acls=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 			 * (demo mode dislabed) do not return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 			 * TargetName+TargetAddress unless a NodeACL exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) 			if ((tpg->tpg_attrib.generate_node_acls == 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) 			    (tpg->tpg_attrib.demo_mode_discovery == 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 			    (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) 				cmd->conn->sess->sess_ops->InitiatorName))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) 			spin_lock(&tpg->tpg_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) 			active = (tpg->tpg_state == TPG_STATE_ACTIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) 			spin_unlock(&tpg->tpg_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) 			if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) 			spin_lock(&tpg->tpg_np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 			list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) 						tpg_np_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 				struct iscsi_np *np = tpg_np->tpg_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 				struct sockaddr_storage *sockaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) 				if (np->np_network_transport != network_transport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) 				if (!target_name_printed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) 					len = sprintf(buf, "TargetName=%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) 						      tiqn->tiqn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) 					len += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 					if ((len + payload_len) > buffer_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) 						spin_unlock(&tpg->tpg_np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 						spin_unlock(&tiqn->tiqn_tpg_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) 						end_of_buf = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) 						goto eob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) 					if (skip_bytes && len <= skip_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) 						skip_bytes -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) 					} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) 						memcpy(payload + payload_len, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 						payload_len += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) 						target_name_printed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) 						if (len > skip_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) 							skip_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) 				if (inet_addr_is_any((struct sockaddr *)&np->np_sockaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) 					sockaddr = &conn->local_sockaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 					sockaddr = &np->np_sockaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 				len = sprintf(buf, "TargetAddress="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 					      "%pISpc,%hu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 					      sockaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 					      tpg->tpgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) 				len += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) 				if ((len + payload_len) > buffer_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) 					spin_unlock(&tpg->tpg_np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 					spin_unlock(&tiqn->tiqn_tpg_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) 					end_of_buf = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) 					goto eob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) 				if (skip_bytes && len <= skip_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) 					skip_bytes -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 					memcpy(payload + payload_len, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 					payload_len += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 					if (len > skip_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 						skip_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 			spin_unlock(&tpg->tpg_np_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 		spin_unlock(&tiqn->tiqn_tpg_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) eob:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) 		if (end_of_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) 			*completed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 		if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) 	spin_unlock(&tiqn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) 	cmd->buf_ptr = payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) 	return payload_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 		      struct iscsi_text_rsp *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) 		      enum iscsit_transport_type network_transport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 	int text_length, padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) 	bool completed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 	text_length = iscsit_build_sendtargets_response(cmd, network_transport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 							cmd->read_data_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 							&completed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 	if (text_length < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) 		return text_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) 	if (completed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 		hdr->flags = ISCSI_FLAG_CMD_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) 		hdr->flags = ISCSI_FLAG_TEXT_CONTINUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 		cmd->read_data_done += text_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 		if (cmd->targ_xfer_tag == 0xFFFFFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) 			cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 	hdr->opcode = ISCSI_OP_TEXT_RSP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 	padding = ((-text_length) & 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 	hton24(hdr->dlength, text_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) 	hdr->itt = cmd->init_task_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 	hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) 	cmd->stat_sn = conn->stat_sn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 	hdr->statsn = cpu_to_be32(cmd->stat_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 	iscsit_increment_maxcmdsn(cmd, conn->sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 	 * Reset maxcmdsn_inc in multi-part text payload exchanges to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 	 * correctly increment MaxCmdSN for each response answering a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 	 * non immediate text request with a valid CmdSN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) 	cmd->maxcmdsn_inc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) 	hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) 	hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) 	pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) 		" Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) 		cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) 		!!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) 		!!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) 	return text_length + padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) EXPORT_SYMBOL(iscsit_build_text_rsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) static int iscsit_send_text_rsp(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) 	struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) 	struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) 	struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) 	int text_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) 	text_length = iscsit_build_text_rsp(cmd, conn, hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) 				conn->conn_transport->transport_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 	if (text_length < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) 		return text_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) 						     cmd->buf_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) 						     text_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 		    struct iscsi_reject *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 	hdr->opcode		= ISCSI_OP_REJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) 	hdr->reason		= cmd->reject_reason;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 	hton24(hdr->dlength, ISCSI_HDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 	hdr->ffffffff		= cpu_to_be32(0xffffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) 	cmd->stat_sn		= conn->stat_sn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) EXPORT_SYMBOL(iscsit_build_reject);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) static int iscsit_send_reject(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 	struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) 	struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) 	struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) 	iscsit_build_reject(cmd, conn, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) 	pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) 		" CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) 						     cmd->buf_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) 						     ISCSI_HDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) 	int ord, cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) 	 * bitmap_id is assigned from iscsit_global->ts_bitmap from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) 	 * within iscsit_start_kthreads()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) 	 * Here we use bitmap_id to determine which CPU that this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) 	 * iSCSI connection's RX/TX threads will be scheduled to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 	 * execute upon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 	ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) 		if (ord-- == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) 			cpumask_set_cpu(cpu, conn->conn_cpumask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) 	 * This should never be reached..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) 	cpumask_setall(conn->conn_cpumask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) 	case ISTATE_SEND_R2T:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) 		ret = iscsit_send_r2t(cmd, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) 	case ISTATE_REMOVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) 		spin_lock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) 		list_del_init(&cmd->i_conn_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 		spin_unlock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) 		iscsit_free_cmd(cmd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) 		iscsit_mod_nopin_response_timer(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) 		ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 	case ISTATE_SEND_NOPIN_NO_RESPONSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) 		ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 		pr_err("Unknown Opcode: 0x%02x ITT:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 		       " 0x%08x, i_state: %d on CID: %hu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) 		       cmd->iscsi_opcode, cmd->init_task_tag, state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 		       conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) EXPORT_SYMBOL(iscsit_immediate_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) iscsit_handle_immediate_queue(struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 	struct iscsit_transport *t = conn->conn_transport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 	struct iscsi_queue_req *qr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 	struct iscsi_cmd *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 	u8 state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 	while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 		atomic_set(&conn->check_immediate_queue, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 		cmd = qr->cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 		state = qr->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 		kmem_cache_free(lio_qr_cache, qr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 		ret = t->iscsit_immediate_queue(conn, cmd, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) check_rsp_state:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) 	case ISTATE_SEND_DATAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) 		ret = iscsit_send_datain(cmd, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) 		else if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) 			/* more drs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) 			goto check_rsp_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) 		else if (ret == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) 			/* all done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) 			spin_lock_bh(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) 			cmd->i_state = ISTATE_SENT_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) 			spin_unlock_bh(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) 			if (atomic_read(&conn->check_immediate_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) 				return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) 		} else if (ret == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 			/* Still must send status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) 			   SCF_TRANSPORT_TASK_SENSE was set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) 			spin_lock_bh(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) 			cmd->i_state = ISTATE_SEND_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) 			spin_unlock_bh(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) 			state = ISTATE_SEND_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) 			goto check_rsp_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) 	case ISTATE_SEND_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) 	case ISTATE_SEND_STATUS_RECOVERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) 		ret = iscsit_send_response(cmd, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) 	case ISTATE_SEND_LOGOUTRSP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) 		ret = iscsit_send_logout(cmd, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 	case ISTATE_SEND_ASYNCMSG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) 		ret = iscsit_send_conn_drop_async_message(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 			cmd, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 	case ISTATE_SEND_NOPIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) 		ret = iscsit_send_nopin(cmd, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) 	case ISTATE_SEND_REJECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) 		ret = iscsit_send_reject(cmd, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) 	case ISTATE_SEND_TASKMGTRSP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) 		ret = iscsit_send_task_mgt_rsp(cmd, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) 		if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) 		ret = iscsit_tmr_post_handler(cmd, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) 		if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) 			iscsit_fall_back_to_erl0(conn->sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) 	case ISTATE_SEND_TEXTRSP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) 		ret = iscsit_send_text_rsp(cmd, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) 		pr_err("Unknown Opcode: 0x%02x ITT:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) 		       " 0x%08x, i_state: %d on CID: %hu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) 		       cmd->iscsi_opcode, cmd->init_task_tag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) 		       state, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 	case ISTATE_SEND_LOGOUTRSP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) 		if (!iscsit_logout_post_handler(cmd, conn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) 			return -ECONNRESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) 	case ISTATE_SEND_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) 	case ISTATE_SEND_ASYNCMSG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) 	case ISTATE_SEND_NOPIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) 	case ISTATE_SEND_STATUS_RECOVERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) 	case ISTATE_SEND_TEXTRSP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) 	case ISTATE_SEND_TASKMGTRSP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) 	case ISTATE_SEND_REJECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) 		spin_lock_bh(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) 		cmd->i_state = ISTATE_SENT_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) 		spin_unlock_bh(&cmd->istate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) 		pr_err("Unknown Opcode: 0x%02x ITT:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) 		       " 0x%08x, i_state: %d on CID: %hu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) 		       cmd->iscsi_opcode, cmd->init_task_tag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) 		       cmd->i_state, conn->cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) 	if (atomic_read(&conn->check_immediate_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) EXPORT_SYMBOL(iscsit_response_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) static int iscsit_handle_response_queue(struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) 	struct iscsit_transport *t = conn->conn_transport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) 	struct iscsi_queue_req *qr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) 	struct iscsi_cmd *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) 	u8 state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) 	while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) 		cmd = qr->cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) 		state = qr->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) 		kmem_cache_free(lio_qr_cache, qr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) 		ret = t->iscsit_response_queue(conn, cmd, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) 		if (ret == 1 || ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) int iscsi_target_tx_thread(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) 	struct iscsi_conn *conn = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) 	bool conn_freed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) 	 * Allow ourselves to be interrupted by SIGINT so that a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) 	 * connection recovery / failure event can be triggered externally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) 	allow_signal(SIGINT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) 	while (!kthread_should_stop()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) 		 * Ensure that both TX and RX per connection kthreads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) 		 * are scheduled to run on the same CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) 		iscsit_thread_check_cpumask(conn, current, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) 		wait_event_interruptible(conn->queues_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) 					 !iscsit_conn_all_queues_empty(conn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) 		if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) 			goto transport_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) get_immediate:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) 		ret = iscsit_handle_immediate_queue(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) 			goto transport_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) 		ret = iscsit_handle_response_queue(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) 		if (ret == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) 			goto get_immediate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) 		} else if (ret == -ECONNRESET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) 			conn_freed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) 		} else if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) 			goto transport_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) transport_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) 	 * Avoid the normal connection failure code-path if this connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) 	 * is still within LOGIN mode, and iscsi_np process context is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) 	 * responsible for cleaning up the early connection failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) 	if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) 		iscsit_take_action_for_connection_exit(conn, &conn_freed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) 	if (!conn_freed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) 		while (!kthread_should_stop()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) 			msleep(100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) 	struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) 	struct iscsi_cmd *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) 	switch (hdr->opcode & ISCSI_OPCODE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) 	case ISCSI_OP_SCSI_CMD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) 		cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) 		if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) 			goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) 		ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) 	case ISCSI_OP_SCSI_DATA_OUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) 		ret = iscsit_handle_data_out(conn, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) 	case ISCSI_OP_NOOP_OUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) 		cmd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) 		if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) 			cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) 			if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) 				goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) 		ret = iscsit_handle_nop_out(conn, cmd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) 	case ISCSI_OP_SCSI_TMFUNC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) 		cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) 		if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) 			goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) 		ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) 	case ISCSI_OP_TEXT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) 		if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) 			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) 			if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) 				goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) 			cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) 			if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) 				goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) 		ret = iscsit_handle_text_cmd(conn, cmd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) 	case ISCSI_OP_LOGOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) 		cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) 		if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) 			goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) 		ret = iscsit_handle_logout_cmd(conn, cmd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) 		if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) 			wait_for_completion_timeout(&conn->conn_logout_comp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) 					SECONDS_FOR_LOGOUT_COMP * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) 	case ISCSI_OP_SNACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) 		ret = iscsit_handle_snack(conn, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) 		pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) 		if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) 			pr_err("Cannot recover from unknown"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) 			" opcode while ERL=0, closing iSCSI connection.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) 		pr_err("Unable to recover from unknown opcode while OFMarker=No,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) 		       " closing iSCSI connection.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) 		ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) reject:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) 	return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) 	spin_lock_bh(&conn->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) 	ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) 	spin_unlock_bh(&conn->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) static void iscsit_get_rx_pdu(struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) 	u8 *buffer, opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) 	u32 checksum = 0, digest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) 	struct kvec iov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) 	buffer = kcalloc(ISCSI_HDR_LEN, sizeof(*buffer), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) 	while (!kthread_should_stop()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) 		 * Ensure that both TX and RX per connection kthreads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) 		 * are scheduled to run on the same CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) 		iscsit_thread_check_cpumask(conn, current, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) 		memset(&iov, 0, sizeof(struct kvec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) 		iov.iov_base	= buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) 		iov.iov_len	= ISCSI_HDR_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) 		ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) 		if (ret != ISCSI_HDR_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) 			iscsit_rx_thread_wait_for_tcp(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) 		if (conn->conn_ops->HeaderDigest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) 			iov.iov_base	= &digest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) 			iov.iov_len	= ISCSI_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) 			ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) 			if (ret != ISCSI_CRC_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) 				iscsit_rx_thread_wait_for_tcp(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) 			iscsit_do_crypto_hash_buf(conn->conn_rx_hash, buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) 						  ISCSI_HDR_LEN, 0, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) 						  &checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) 			if (digest != checksum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) 				pr_err("HeaderDigest CRC32C failed,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) 					" received 0x%08x, computed 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) 					digest, checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) 				 * Set the PDU to 0xff so it will intentionally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) 				 * hit default in the switch below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) 				memset(buffer, 0xff, ISCSI_HDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) 				atomic_long_inc(&conn->sess->conn_digest_errors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) 				pr_debug("Got HeaderDigest CRC32C"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) 						" 0x%08x\n", checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) 		if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) 		opcode = buffer[0] & ISCSI_OPCODE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) 		if (conn->sess->sess_ops->SessionType &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) 		   ((!(opcode & ISCSI_OP_TEXT)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) 		    (!(opcode & ISCSI_OP_LOGOUT)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) 			pr_err("Received illegal iSCSI Opcode: 0x%02x"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) 			" while in Discovery Session, rejecting.\n", opcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) 			iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) 					  buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) 		ret = iscsi_target_rx_opcode(conn, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) int iscsi_target_rx_thread(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) 	struct iscsi_conn *conn = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) 	bool conn_freed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) 	 * Allow ourselves to be interrupted by SIGINT so that a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) 	 * connection recovery / failure event can be triggered externally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) 	allow_signal(SIGINT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) 	 * Wait for iscsi_post_login_handler() to complete before allowing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) 	 * incoming iscsi/tcp socket I/O, and/or failing the connection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) 	rc = wait_for_completion_interruptible(&conn->rx_login_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) 	if (rc < 0 || iscsi_target_check_conn_state(conn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) 	if (!conn->conn_transport->iscsit_get_rx_pdu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) 	conn->conn_transport->iscsit_get_rx_pdu(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) 	if (!signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) 		atomic_set(&conn->transport_failed, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) 	iscsit_take_action_for_connection_exit(conn, &conn_freed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) 	if (!conn_freed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) 		while (!kthread_should_stop()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) 			msleep(100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) 	LIST_HEAD(tmp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) 	struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) 	struct iscsi_session *sess = conn->sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) 	 * We expect this function to only ever be called from either RX or TX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) 	 * thread context via iscsit_close_connection() once the other context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) 	 * has been reset -> returned sleeping pre-handler state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) 	spin_lock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) 	list_splice_init(&conn->conn_cmd_list, &tmp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) 	list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) 		struct se_cmd *se_cmd = &cmd->se_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) 		if (se_cmd->se_tfo != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) 			spin_lock_irq(&se_cmd->t_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) 			if (se_cmd->transport_state & CMD_T_ABORTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) 				 * LIO's abort path owns the cleanup for this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) 				 * so put it back on the list and let
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) 				 * aborted_task handle it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) 				list_move_tail(&cmd->i_conn_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) 					       &conn->conn_cmd_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) 				se_cmd->transport_state |= CMD_T_FABRIC_STOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) 			spin_unlock_irq(&se_cmd->t_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) 	spin_unlock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) 	list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) 		list_del_init(&cmd->i_conn_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) 		iscsit_increment_maxcmdsn(cmd, sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) 		iscsit_free_cmd(cmd, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) static void iscsit_stop_timers_for_cmds(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) 	struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) 	struct iscsi_cmd *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) 	spin_lock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) 		if (cmd->data_direction == DMA_TO_DEVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) 			iscsit_stop_dataout_timer(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) 	spin_unlock_bh(&conn->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) int iscsit_close_connection(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) 	struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) 	int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) 	struct iscsi_session	*sess = conn->sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) 	pr_debug("Closing iSCSI connection CID %hu on SID:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) 		" %u\n", conn->cid, sess->sid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) 	 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) 	 * case just in case the RX Thread in iscsi_target_rx_opcode() is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) 	 * sleeping and the logout response never got sent because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) 	 * connection failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) 	 * However for iser-target, isert_wait4logout() is using conn_logout_comp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) 	 * to signal logout response TX interrupt completion.  Go ahead and skip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) 	 * this for iser since isert_rx_opcode() does not wait on logout failure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) 	 * and to avoid iscsi_conn pointer dereference in iser-target code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) 	if (!conn->conn_transport->rdma_shutdown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) 		complete(&conn->conn_logout_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) 	if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) 		if (conn->tx_thread &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) 		    cmpxchg(&conn->tx_thread_active, true, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) 			send_sig(SIGINT, conn->tx_thread, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) 			kthread_stop(conn->tx_thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) 	} else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) 		if (conn->rx_thread &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) 		    cmpxchg(&conn->rx_thread_active, true, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) 			send_sig(SIGINT, conn->rx_thread, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) 			kthread_stop(conn->rx_thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) 	spin_lock(&iscsit_global->ts_bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) 	bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) 			      get_order(1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) 	spin_unlock(&iscsit_global->ts_bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) 	iscsit_stop_timers_for_cmds(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) 	iscsit_stop_nopin_response_timer(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) 	iscsit_stop_nopin_timer(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) 	if (conn->conn_transport->iscsit_wait_conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) 		conn->conn_transport->iscsit_wait_conn(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) 	 * During Connection recovery drop unacknowledged out of order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) 	 * commands for this connection, and prepare the other commands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) 	 * for reallegiance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) 	 * During normal operation clear the out of order commands (but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) 	 * do not free the struct iscsi_ooo_cmdsn's) and release all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) 	 * struct iscsi_cmds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) 	if (atomic_read(&conn->connection_recovery)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) 		iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) 		iscsit_prepare_cmds_for_reallegiance(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) 		iscsit_clear_ooo_cmdsns_for_conn(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) 		iscsit_release_commands_from_conn(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) 	iscsit_free_queue_reqs_for_conn(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) 	 * Handle decrementing session or connection usage count if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) 	 * a logout response was not able to be sent because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) 	 * connection failed.  Fall back to Session Recovery here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) 	if (atomic_read(&conn->conn_logout_remove)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) 		if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) 			iscsit_dec_conn_usage_count(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) 			iscsit_dec_session_usage_count(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) 		if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) 			iscsit_dec_conn_usage_count(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) 		atomic_set(&conn->conn_logout_remove, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) 		atomic_set(&sess->session_reinstatement, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) 		atomic_set(&sess->session_fall_back_to_erl0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) 	spin_lock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) 	list_del(&conn->conn_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) 	 * Attempt to let the Initiator know this connection failed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) 	 * sending an Connection Dropped Async Message on another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) 	 * active connection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) 	if (atomic_read(&conn->connection_recovery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) 		iscsit_build_conn_drop_async_message(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) 	spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) 	 * If connection reinstatement is being performed on this connection,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) 	 * up the connection reinstatement semaphore that is being blocked on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) 	 * in iscsit_cause_connection_reinstatement().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) 	spin_lock_bh(&conn->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) 	if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) 		spin_unlock_bh(&conn->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) 		complete(&conn->conn_wait_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) 		wait_for_completion(&conn->conn_post_wait_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) 		spin_lock_bh(&conn->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) 	 * If connection reinstatement is being performed on this connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) 	 * by receiving a REMOVECONNFORRECOVERY logout request, up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) 	 * connection wait rcfr semaphore that is being blocked on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) 	 * an iscsit_connection_reinstatement_rcfr().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) 	if (atomic_read(&conn->connection_wait_rcfr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) 		spin_unlock_bh(&conn->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) 		complete(&conn->conn_wait_rcfr_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) 		wait_for_completion(&conn->conn_post_wait_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) 		spin_lock_bh(&conn->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) 	atomic_set(&conn->connection_reinstatement, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) 	spin_unlock_bh(&conn->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253) 	 * If any other processes are accessing this connection pointer we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) 	 * must wait until they have completed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) 	iscsit_check_conn_usage_count(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) 	ahash_request_free(conn->conn_tx_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) 	if (conn->conn_rx_hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) 		struct crypto_ahash *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) 		tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) 		ahash_request_free(conn->conn_rx_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) 		crypto_free_ahash(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) 	if (conn->sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268) 		sock_release(conn->sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) 	if (conn->conn_transport->iscsit_free_conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) 		conn->conn_transport->iscsit_free_conn(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) 	pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) 	conn->conn_state = TARG_CONN_STATE_FREE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) 	iscsit_free_conn(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) 	spin_lock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) 	atomic_dec(&sess->nconn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) 	pr_debug("Decremented iSCSI connection count to %hu from node:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) 		" %s\n", atomic_read(&sess->nconn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) 		sess->sess_ops->InitiatorName);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) 	 * Make sure that if one connection fails in an non ERL=2 iSCSI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) 	 * Session that they all fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) 	if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) 	     !atomic_read(&sess->session_logout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) 		atomic_set(&sess->session_fall_back_to_erl0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) 	 * If this was not the last connection in the session, and we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) 	 * performing session reinstatement or falling back to ERL=0, call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) 	 * iscsit_stop_session() without sleeping to shutdown the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) 	 * active connections.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) 	if (atomic_read(&sess->nconn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) 		if (!atomic_read(&sess->session_reinstatement) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) 		    !atomic_read(&sess->session_fall_back_to_erl0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) 			spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) 		if (!atomic_read(&sess->session_stop_active)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) 			atomic_set(&sess->session_stop_active, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) 			spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) 			iscsit_stop_session(sess, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) 		spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) 	 * If this was the last connection in the session and one of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) 	 * following is occurring:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) 	 * Session Reinstatement is not being performed, and are falling back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) 	 * to ERL=0 call iscsit_close_session().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) 	 * Session Logout was requested.  iscsit_close_session() will be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) 	 * elsewhere.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) 	 * Session Continuation is not being performed, start the Time2Retain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) 	 * handler and check if sleep_on_sess_wait_sem is active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) 	if (!atomic_read(&sess->session_reinstatement) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326) 	     atomic_read(&sess->session_fall_back_to_erl0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) 		spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) 		complete_all(&sess->session_wait_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) 		iscsit_close_session(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) 	} else if (atomic_read(&sess->session_logout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) 		pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) 		sess->session_state = TARG_SESS_STATE_FREE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) 		if (atomic_read(&sess->session_close)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) 			spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) 			complete_all(&sess->session_wait_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) 			iscsit_close_session(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) 			spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) 		pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) 		sess->session_state = TARG_SESS_STATE_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) 		if (!atomic_read(&sess->session_continuation))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) 			iscsit_start_time2retain_handler(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) 		if (atomic_read(&sess->session_close)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) 			spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) 			complete_all(&sess->session_wait_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) 			iscsit_close_session(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) 			spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365)  * If the iSCSI Session for the iSCSI Initiator Node exists,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366)  * forcefully shutdown the iSCSI NEXUS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368) int iscsit_close_session(struct iscsi_session *sess)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) 	struct iscsi_portal_group *tpg = sess->tpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) 	if (atomic_read(&sess->nconn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) 		pr_err("%d connection(s) still exist for iSCSI session"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) 			" to %s\n", atomic_read(&sess->nconn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) 			sess->sess_ops->InitiatorName);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) 	spin_lock_bh(&se_tpg->session_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381) 	atomic_set(&sess->session_logout, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) 	atomic_set(&sess->session_reinstatement, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) 	iscsit_stop_time2retain_timer(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) 	spin_unlock_bh(&se_tpg->session_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) 	 * transport_deregister_session_configfs() will clear the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) 	 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) 	 * can be setting it again with __transport_register_session() in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) 	 * iscsi_post_login_handler() again after the iscsit_stop_session()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) 	 * completes in iscsi_np context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) 	transport_deregister_session_configfs(sess->se_sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) 	 * If any other processes are accessing this session pointer we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) 	 * wait until they have completed.  If we are in an interrupt (the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) 	 * time2retain handler) and contain and active session usage count we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) 	 * restart the timer and exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) 	if (!in_interrupt()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402) 		iscsit_check_session_usage_count(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) 		if (iscsit_check_session_usage_count(sess) == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) 			atomic_set(&sess->session_logout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) 			iscsit_start_time2retain_handler(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) 	transport_deregister_session(sess->se_sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) 	if (sess->sess_ops->ErrorRecoveryLevel == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) 		iscsit_free_connection_recovery_entries(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) 	iscsit_free_all_ooo_cmdsns(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) 	spin_lock_bh(&se_tpg->session_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) 	pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) 	sess->session_state = TARG_SESS_STATE_FREE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) 	pr_debug("Released iSCSI session from node: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422) 			sess->sess_ops->InitiatorName);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) 	tpg->nsessions--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) 	if (tpg->tpg_tiqn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) 		tpg->tpg_tiqn->tiqn_nsessions--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) 	pr_debug("Decremented number of active iSCSI Sessions on"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) 		" iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) 	ida_free(&sess_ida, sess->session_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) 	kfree(sess->sess_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) 	sess->sess_ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433) 	spin_unlock_bh(&se_tpg->session_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) 	kfree(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) static void iscsit_logout_post_handler_closesession(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) 	struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) 	struct iscsi_session *sess = conn->sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443) 	int sleep = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) 	 * Traditional iscsi/tcp will invoke this logic from TX thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) 	 * context during session logout, so clear tx_thread_active and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) 	 * sleep if iscsit_close_connection() has not already occured.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449) 	 * Since iser-target invokes this logic from it's own workqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) 	 * always sleep waiting for RX/TX thread shutdown to complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) 	 * within iscsit_close_connection().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) 	if (!conn->conn_transport->rdma_shutdown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) 		sleep = cmpxchg(&conn->tx_thread_active, true, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) 		if (!sleep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) 	atomic_set(&conn->conn_logout_remove, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) 	complete(&conn->conn_logout_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) 	iscsit_dec_conn_usage_count(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) 	atomic_set(&sess->session_close, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) 	iscsit_stop_session(sess, sleep, sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465) 	iscsit_dec_session_usage_count(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468) static void iscsit_logout_post_handler_samecid(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) 	struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) 	int sleep = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) 	if (!conn->conn_transport->rdma_shutdown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474) 		sleep = cmpxchg(&conn->tx_thread_active, true, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475) 		if (!sleep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479) 	atomic_set(&conn->conn_logout_remove, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480) 	complete(&conn->conn_logout_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) 	iscsit_cause_connection_reinstatement(conn, sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) 	iscsit_dec_conn_usage_count(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486) static void iscsit_logout_post_handler_diffcid(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487) 	struct iscsi_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488) 	u16 cid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490) 	struct iscsi_conn *l_conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491) 	struct iscsi_session *sess = conn->sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) 	bool conn_found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) 	if (!sess)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) 	spin_lock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498) 	list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499) 		if (l_conn->cid == cid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500) 			iscsit_inc_conn_usage_count(l_conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501) 			conn_found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505) 	spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507) 	if (!conn_found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510) 	if (l_conn->sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) 		l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513) 	spin_lock_bh(&l_conn->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) 	pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515) 	l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516) 	spin_unlock_bh(&l_conn->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518) 	iscsit_cause_connection_reinstatement(l_conn, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519) 	iscsit_dec_conn_usage_count(l_conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523)  *	Return of 0 causes the TX thread to restart.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525) int iscsit_logout_post_handler(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526) 	struct iscsi_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527) 	struct iscsi_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531) 	switch (cmd->logout_reason) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532) 	case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533) 		switch (cmd->logout_response) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) 		case ISCSI_LOGOUT_SUCCESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535) 		case ISCSI_LOGOUT_CLEANUP_FAILED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4537) 			iscsit_logout_post_handler_closesession(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4538) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4539) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541) 	case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) 		if (conn->cid == cmd->logout_cid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543) 			switch (cmd->logout_response) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544) 			case ISCSI_LOGOUT_SUCCESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545) 			case ISCSI_LOGOUT_CLEANUP_FAILED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) 				iscsit_logout_post_handler_samecid(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) 			switch (cmd->logout_response) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552) 			case ISCSI_LOGOUT_SUCCESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) 				iscsit_logout_post_handler_diffcid(conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554) 					cmd->logout_cid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) 			case ISCSI_LOGOUT_CID_NOT_FOUND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557) 			case ISCSI_LOGOUT_CLEANUP_FAILED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564) 	case ISCSI_LOGOUT_REASON_RECOVERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565) 		switch (cmd->logout_response) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) 		case ISCSI_LOGOUT_SUCCESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4567) 		case ISCSI_LOGOUT_CID_NOT_FOUND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4568) 		case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4569) 		case ISCSI_LOGOUT_CLEANUP_FAILED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581) EXPORT_SYMBOL(iscsit_logout_post_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583) void iscsit_fail_session(struct iscsi_session *sess)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) 	struct iscsi_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587) 	spin_lock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588) 	list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) 		pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) 		conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) 	spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594) 	pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595) 	sess->session_state = TARG_SESS_STATE_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598) void iscsit_stop_session(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599) 	struct iscsi_session *sess,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600) 	int session_sleep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601) 	int connection_sleep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603) 	u16 conn_count = atomic_read(&sess->nconn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604) 	struct iscsi_conn *conn, *conn_tmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) 	int is_last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) 	spin_lock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) 	if (connection_sleep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) 		list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611) 				conn_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612) 			if (conn_count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615) 			if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616) 				is_last = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618) 				iscsit_inc_conn_usage_count(conn_tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619) 				is_last = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621) 			iscsit_inc_conn_usage_count(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623) 			spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624) 			iscsit_cause_connection_reinstatement(conn, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625) 			spin_lock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627) 			iscsit_dec_conn_usage_count(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628) 			if (is_last == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629) 				iscsit_dec_conn_usage_count(conn_tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630) 			conn_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633) 		list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) 			iscsit_cause_connection_reinstatement(conn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) 	if (session_sleep && atomic_read(&sess->nconn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) 		spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639) 		wait_for_completion(&sess->session_wait_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) 		spin_unlock_bh(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) 	struct iscsi_session *sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648) 	struct se_session *se_sess, *se_sess_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649) 	LIST_HEAD(free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650) 	int session_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652) 	spin_lock_bh(&se_tpg->session_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653) 	if (tpg->nsessions && !force) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654) 		spin_unlock_bh(&se_tpg->session_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658) 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659) 			sess_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660) 		sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662) 		spin_lock(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) 		if (atomic_read(&sess->session_fall_back_to_erl0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664) 		    atomic_read(&sess->session_logout) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665) 		    atomic_read(&sess->session_close) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666) 		    (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) 			spin_unlock(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670) 		iscsit_inc_session_usage_count(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671) 		atomic_set(&sess->session_reinstatement, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672) 		atomic_set(&sess->session_fall_back_to_erl0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673) 		atomic_set(&sess->session_close, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) 		spin_unlock(&sess->conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676) 		list_move_tail(&se_sess->sess_list, &free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678) 	spin_unlock_bh(&se_tpg->session_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680) 	list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) 		sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683) 		list_del_init(&se_sess->sess_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) 		iscsit_stop_session(sess, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) 		iscsit_dec_session_usage_count(sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) 		session_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689) 	pr_debug("Released %d iSCSI Session(s) from Target Portal"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690) 			" Group: %hu\n", session_count, tpg->tpgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694) MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) MODULE_VERSION("4.1.x");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696) MODULE_AUTHOR("nab@Linux-iSCSI.org");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699) module_init(iscsi_target_init_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700) module_exit(iscsi_target_cleanup_module);