1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e48354ceSNicholas Bellinger /*******************************************************************************
3e48354ceSNicholas Bellinger  * This file contains main functions related to the iSCSI Target Core Driver.
4e48354ceSNicholas Bellinger  *
54c76251eSNicholas Bellinger  * (c) Copyright 2007-2013 Datera, Inc.
6e48354ceSNicholas Bellinger  *
7e48354ceSNicholas Bellinger  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8e48354ceSNicholas Bellinger  *
9e48354ceSNicholas Bellinger  ******************************************************************************/
10e48354ceSNicholas Bellinger 
1169110e3cSHerbert Xu #include <crypto/hash.h>
12e48354ceSNicholas Bellinger #include <linux/string.h>
13e48354ceSNicholas Bellinger #include <linux/kthread.h>
14e48354ceSNicholas Bellinger #include <linux/completion.h>
15827509e3SPaul Gortmaker #include <linux/module.h>
165538d294SDavid S. Miller #include <linux/vmalloc.h>
1740401530SAl Viro #include <linux/idr.h>
188dcf07beSBart Van Assche #include <linux/delay.h>
193f07c014SIngo Molnar #include <linux/sched/signal.h>
20e48354ceSNicholas Bellinger #include <asm/unaligned.h>
217bfca0cfSSagi Grimberg #include <linux/inet.h>
228dcf07beSBart Van Assche #include <net/ipv6.h>
23ba929992SBart Van Assche #include <scsi/scsi_proto.h>
24e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
25d28b1169SAndy Grover #include <scsi/scsi_tcq.h>
26e48354ceSNicholas Bellinger #include <target/target_core_base.h>
27c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
28e48354ceSNicholas Bellinger 
29*ea87981aSDmitry Bogdanov #include <target/target_core_backend.h>
3067f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h>
31e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
32e48354ceSNicholas Bellinger #include "iscsi_target_seq_pdu_list.h"
33e48354ceSNicholas Bellinger #include "iscsi_target_datain_values.h"
34e48354ceSNicholas Bellinger #include "iscsi_target_erl0.h"
35e48354ceSNicholas Bellinger #include "iscsi_target_erl1.h"
36e48354ceSNicholas Bellinger #include "iscsi_target_erl2.h"
37e48354ceSNicholas Bellinger #include "iscsi_target_login.h"
38e48354ceSNicholas Bellinger #include "iscsi_target_tmr.h"
39e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
40e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
41e48354ceSNicholas Bellinger #include "iscsi_target.h"
42e48354ceSNicholas Bellinger #include "iscsi_target_device.h"
4367f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_stat.h>
44e48354ceSNicholas Bellinger 
45baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
46baa4d64bSNicholas Bellinger 
47e48354ceSNicholas Bellinger static LIST_HEAD(g_tiqn_list);
48e48354ceSNicholas Bellinger static LIST_HEAD(g_np_list);
49e48354ceSNicholas Bellinger static DEFINE_SPINLOCK(tiqn_lock);
50ee291e63SAndy Grover static DEFINE_MUTEX(np_lock);
51e48354ceSNicholas Bellinger 
52e48354ceSNicholas Bellinger static struct idr tiqn_idr;
5331ff0ceeSMatthew Wilcox DEFINE_IDA(sess_ida);
54e48354ceSNicholas Bellinger struct mutex auth_id_lock;
55e48354ceSNicholas Bellinger 
56e48354ceSNicholas Bellinger struct iscsit_global *iscsit_global;
57e48354ceSNicholas Bellinger 
58e48354ceSNicholas Bellinger struct kmem_cache *lio_qr_cache;
59e48354ceSNicholas Bellinger struct kmem_cache *lio_dr_cache;
60e48354ceSNicholas Bellinger struct kmem_cache *lio_ooo_cache;
61e48354ceSNicholas Bellinger struct kmem_cache *lio_r2t_cache;
62e48354ceSNicholas Bellinger 
6366cd9d4eSMax Gurtovoy static int iscsit_handle_immediate_data(struct iscsit_cmd *,
642ec5a8c1SNicholas Bellinger 			struct iscsi_scsi_req *, u32);
65e48354ceSNicholas Bellinger 
iscsit_get_tiqn_for_login(unsigned char * buf)66e48354ceSNicholas Bellinger struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
67e48354ceSNicholas Bellinger {
68e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn = NULL;
69e48354ceSNicholas Bellinger 
70e48354ceSNicholas Bellinger 	spin_lock(&tiqn_lock);
71e48354ceSNicholas Bellinger 	list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
72e48354ceSNicholas Bellinger 		if (!strcmp(tiqn->tiqn, buf)) {
73e48354ceSNicholas Bellinger 
74e48354ceSNicholas Bellinger 			spin_lock(&tiqn->tiqn_state_lock);
75e48354ceSNicholas Bellinger 			if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
76e48354ceSNicholas Bellinger 				tiqn->tiqn_access_count++;
77e48354ceSNicholas Bellinger 				spin_unlock(&tiqn->tiqn_state_lock);
78e48354ceSNicholas Bellinger 				spin_unlock(&tiqn_lock);
79e48354ceSNicholas Bellinger 				return tiqn;
80e48354ceSNicholas Bellinger 			}
81e48354ceSNicholas Bellinger 			spin_unlock(&tiqn->tiqn_state_lock);
82e48354ceSNicholas Bellinger 		}
83e48354ceSNicholas Bellinger 	}
84e48354ceSNicholas Bellinger 	spin_unlock(&tiqn_lock);
85e48354ceSNicholas Bellinger 
86e48354ceSNicholas Bellinger 	return NULL;
87e48354ceSNicholas Bellinger }
88e48354ceSNicholas Bellinger 
iscsit_set_tiqn_shutdown(struct iscsi_tiqn * tiqn)89e48354ceSNicholas Bellinger static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
90e48354ceSNicholas Bellinger {
91e48354ceSNicholas Bellinger 	spin_lock(&tiqn->tiqn_state_lock);
92e48354ceSNicholas Bellinger 	if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
93e48354ceSNicholas Bellinger 		tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
94e48354ceSNicholas Bellinger 		spin_unlock(&tiqn->tiqn_state_lock);
95e48354ceSNicholas Bellinger 		return 0;
96e48354ceSNicholas Bellinger 	}
97e48354ceSNicholas Bellinger 	spin_unlock(&tiqn->tiqn_state_lock);
98e48354ceSNicholas Bellinger 
99e48354ceSNicholas Bellinger 	return -1;
100e48354ceSNicholas Bellinger }
101e48354ceSNicholas Bellinger 
iscsit_put_tiqn_for_login(struct iscsi_tiqn * tiqn)102e48354ceSNicholas Bellinger void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
103e48354ceSNicholas Bellinger {
104e48354ceSNicholas Bellinger 	spin_lock(&tiqn->tiqn_state_lock);
105e48354ceSNicholas Bellinger 	tiqn->tiqn_access_count--;
106e48354ceSNicholas Bellinger 	spin_unlock(&tiqn->tiqn_state_lock);
107e48354ceSNicholas Bellinger }
108e48354ceSNicholas Bellinger 
109e48354ceSNicholas Bellinger /*
110e48354ceSNicholas Bellinger  * Note that IQN formatting is expected to be done in userspace, and
111e48354ceSNicholas Bellinger  * no explict IQN format checks are done here.
112e48354ceSNicholas Bellinger  */
iscsit_add_tiqn(unsigned char * buf)113e48354ceSNicholas Bellinger struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
114e48354ceSNicholas Bellinger {
115e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn = NULL;
116e48354ceSNicholas Bellinger 	int ret;
117e48354ceSNicholas Bellinger 
1188f50c7f5SDan Carpenter 	if (strlen(buf) >= ISCSI_IQN_LEN) {
119e48354ceSNicholas Bellinger 		pr_err("Target IQN exceeds %d bytes\n",
120e48354ceSNicholas Bellinger 				ISCSI_IQN_LEN);
121e48354ceSNicholas Bellinger 		return ERR_PTR(-EINVAL);
122e48354ceSNicholas Bellinger 	}
123e48354ceSNicholas Bellinger 
1243829f381SMarkus Elfring 	tiqn = kzalloc(sizeof(*tiqn), GFP_KERNEL);
125c46e22f1SMarkus Elfring 	if (!tiqn)
126e48354ceSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
127e48354ceSNicholas Bellinger 
128e48354ceSNicholas Bellinger 	sprintf(tiqn->tiqn, "%s", buf);
129e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&tiqn->tiqn_list);
130e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
131e48354ceSNicholas Bellinger 	spin_lock_init(&tiqn->tiqn_state_lock);
132e48354ceSNicholas Bellinger 	spin_lock_init(&tiqn->tiqn_tpg_lock);
133e48354ceSNicholas Bellinger 	spin_lock_init(&tiqn->sess_err_stats.lock);
134e48354ceSNicholas Bellinger 	spin_lock_init(&tiqn->login_stats.lock);
135e48354ceSNicholas Bellinger 	spin_lock_init(&tiqn->logout_stats.lock);
136e48354ceSNicholas Bellinger 
137e48354ceSNicholas Bellinger 	tiqn->tiqn_state = TIQN_STATE_ACTIVE;
138e48354ceSNicholas Bellinger 
139c9365bd0STejun Heo 	idr_preload(GFP_KERNEL);
140e48354ceSNicholas Bellinger 	spin_lock(&tiqn_lock);
141c9365bd0STejun Heo 
142c9365bd0STejun Heo 	ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
143e48354ceSNicholas Bellinger 	if (ret < 0) {
144c9365bd0STejun Heo 		pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
145e48354ceSNicholas Bellinger 		spin_unlock(&tiqn_lock);
146c9365bd0STejun Heo 		idr_preload_end();
147e48354ceSNicholas Bellinger 		kfree(tiqn);
148e48354ceSNicholas Bellinger 		return ERR_PTR(ret);
149e48354ceSNicholas Bellinger 	}
150c9365bd0STejun Heo 	tiqn->tiqn_index = ret;
151e48354ceSNicholas Bellinger 	list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
152c9365bd0STejun Heo 
153e48354ceSNicholas Bellinger 	spin_unlock(&tiqn_lock);
154c9365bd0STejun Heo 	idr_preload_end();
155e48354ceSNicholas Bellinger 
156e48354ceSNicholas Bellinger 	pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
157e48354ceSNicholas Bellinger 
158e48354ceSNicholas Bellinger 	return tiqn;
159e48354ceSNicholas Bellinger 
160e48354ceSNicholas Bellinger }
161e48354ceSNicholas Bellinger 
iscsit_wait_for_tiqn(struct iscsi_tiqn * tiqn)162e48354ceSNicholas Bellinger static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
163e48354ceSNicholas Bellinger {
164e48354ceSNicholas Bellinger 	/*
165e48354ceSNicholas Bellinger 	 * Wait for accesses to said struct iscsi_tiqn to end.
166e48354ceSNicholas Bellinger 	 */
167e48354ceSNicholas Bellinger 	spin_lock(&tiqn->tiqn_state_lock);
168e48354ceSNicholas Bellinger 	while (tiqn->tiqn_access_count != 0) {
169e48354ceSNicholas Bellinger 		spin_unlock(&tiqn->tiqn_state_lock);
170e48354ceSNicholas Bellinger 		msleep(10);
171e48354ceSNicholas Bellinger 		spin_lock(&tiqn->tiqn_state_lock);
172e48354ceSNicholas Bellinger 	}
173e48354ceSNicholas Bellinger 	spin_unlock(&tiqn->tiqn_state_lock);
174e48354ceSNicholas Bellinger }
175e48354ceSNicholas Bellinger 
iscsit_del_tiqn(struct iscsi_tiqn * tiqn)176e48354ceSNicholas Bellinger void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
177e48354ceSNicholas Bellinger {
178e48354ceSNicholas Bellinger 	/*
179e48354ceSNicholas Bellinger 	 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
180e48354ceSNicholas Bellinger 	 * while holding tiqn->tiqn_state_lock.  This means that all subsequent
181e48354ceSNicholas Bellinger 	 * attempts to access this struct iscsi_tiqn will fail from both transport
182e48354ceSNicholas Bellinger 	 * fabric and control code paths.
183e48354ceSNicholas Bellinger 	 */
184e48354ceSNicholas Bellinger 	if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
185e48354ceSNicholas Bellinger 		pr_err("iscsit_set_tiqn_shutdown() failed\n");
186e48354ceSNicholas Bellinger 		return;
187e48354ceSNicholas Bellinger 	}
188e48354ceSNicholas Bellinger 
189e48354ceSNicholas Bellinger 	iscsit_wait_for_tiqn(tiqn);
190e48354ceSNicholas Bellinger 
191e48354ceSNicholas Bellinger 	spin_lock(&tiqn_lock);
192e48354ceSNicholas Bellinger 	list_del(&tiqn->tiqn_list);
193e48354ceSNicholas Bellinger 	idr_remove(&tiqn_idr, tiqn->tiqn_index);
194e48354ceSNicholas Bellinger 	spin_unlock(&tiqn_lock);
195e48354ceSNicholas Bellinger 
196e48354ceSNicholas Bellinger 	pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
197e48354ceSNicholas Bellinger 			tiqn->tiqn);
198e48354ceSNicholas Bellinger 	kfree(tiqn);
199e48354ceSNicholas Bellinger }
200e48354ceSNicholas Bellinger 
iscsit_access_np(struct iscsi_np * np,struct iscsi_portal_group * tpg)201e48354ceSNicholas Bellinger int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
202e48354ceSNicholas Bellinger {
203e48354ceSNicholas Bellinger 	int ret;
204e48354ceSNicholas Bellinger 	/*
205e48354ceSNicholas Bellinger 	 * Determine if the network portal is accepting storage traffic.
206e48354ceSNicholas Bellinger 	 */
207e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
208e48354ceSNicholas Bellinger 	if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
209e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
210e48354ceSNicholas Bellinger 		return -1;
211e48354ceSNicholas Bellinger 	}
212e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
213e48354ceSNicholas Bellinger 	/*
214e48354ceSNicholas Bellinger 	 * Determine if the portal group is accepting storage traffic.
215e48354ceSNicholas Bellinger 	 */
216e48354ceSNicholas Bellinger 	spin_lock_bh(&tpg->tpg_state_lock);
217e48354ceSNicholas Bellinger 	if (tpg->tpg_state != TPG_STATE_ACTIVE) {
218e48354ceSNicholas Bellinger 		spin_unlock_bh(&tpg->tpg_state_lock);
219e48354ceSNicholas Bellinger 		return -1;
220e48354ceSNicholas Bellinger 	}
221e48354ceSNicholas Bellinger 	spin_unlock_bh(&tpg->tpg_state_lock);
222e48354ceSNicholas Bellinger 
223e48354ceSNicholas Bellinger 	/*
224e48354ceSNicholas Bellinger 	 * Here we serialize access across the TIQN+TPG Tuple.
225e48354ceSNicholas Bellinger 	 */
226a91eb7d9SNicholas Bellinger 	ret = down_interruptible(&tpg->np_login_sem);
227ee7619f2SNicholas Bellinger 	if (ret != 0)
228e48354ceSNicholas Bellinger 		return -1;
229e48354ceSNicholas Bellinger 
230a91eb7d9SNicholas Bellinger 	spin_lock_bh(&tpg->tpg_state_lock);
231a91eb7d9SNicholas Bellinger 	if (tpg->tpg_state != TPG_STATE_ACTIVE) {
232a91eb7d9SNicholas Bellinger 		spin_unlock_bh(&tpg->tpg_state_lock);
233a91eb7d9SNicholas Bellinger 		up(&tpg->np_login_sem);
234a91eb7d9SNicholas Bellinger 		return -1;
235a91eb7d9SNicholas Bellinger 	}
236a91eb7d9SNicholas Bellinger 	spin_unlock_bh(&tpg->tpg_state_lock);
237e48354ceSNicholas Bellinger 
238e48354ceSNicholas Bellinger 	return 0;
239e48354ceSNicholas Bellinger }
240e48354ceSNicholas Bellinger 
iscsit_login_kref_put(struct kref * kref)241a91eb7d9SNicholas Bellinger void iscsit_login_kref_put(struct kref *kref)
242a91eb7d9SNicholas Bellinger {
243a91eb7d9SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = container_of(kref,
244a91eb7d9SNicholas Bellinger 				struct iscsi_tpg_np, tpg_np_kref);
245a91eb7d9SNicholas Bellinger 
246a91eb7d9SNicholas Bellinger 	complete(&tpg_np->tpg_np_comp);
247a91eb7d9SNicholas Bellinger }
248a91eb7d9SNicholas Bellinger 
iscsit_deaccess_np(struct iscsi_np * np,struct iscsi_portal_group * tpg,struct iscsi_tpg_np * tpg_np)249a91eb7d9SNicholas Bellinger int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
250a91eb7d9SNicholas Bellinger 		       struct iscsi_tpg_np *tpg_np)
251e48354ceSNicholas Bellinger {
252e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
253e48354ceSNicholas Bellinger 
254a91eb7d9SNicholas Bellinger 	up(&tpg->np_login_sem);
255e48354ceSNicholas Bellinger 
256a91eb7d9SNicholas Bellinger 	if (tpg_np)
257a91eb7d9SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
258e48354ceSNicholas Bellinger 
259e48354ceSNicholas Bellinger 	if (tiqn)
260e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
261e48354ceSNicholas Bellinger 
262e48354ceSNicholas Bellinger 	return 0;
263e48354ceSNicholas Bellinger }
264e48354ceSNicholas Bellinger 
iscsit_check_np_match(struct sockaddr_storage * sockaddr,struct iscsi_np * np,int network_transport)26505b96892SNicholas Bellinger bool iscsit_check_np_match(
26613a3cf08SAndy Grover 	struct sockaddr_storage *sockaddr,
26705b96892SNicholas Bellinger 	struct iscsi_np *np,
268e48354ceSNicholas Bellinger 	int network_transport)
269e48354ceSNicholas Bellinger {
270e48354ceSNicholas Bellinger 	struct sockaddr_in *sock_in, *sock_in_e;
271e48354ceSNicholas Bellinger 	struct sockaddr_in6 *sock_in6, *sock_in6_e;
27205b96892SNicholas Bellinger 	bool ip_match = false;
27369d75574SAndy Grover 	u16 port, port_e;
274e48354ceSNicholas Bellinger 
27505b96892SNicholas Bellinger 	if (sockaddr->ss_family == AF_INET6) {
27605b96892SNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)sockaddr;
27705b96892SNicholas Bellinger 		sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
27805b96892SNicholas Bellinger 
27905b96892SNicholas Bellinger 		if (!memcmp(&sock_in6->sin6_addr.in6_u,
28005b96892SNicholas Bellinger 			    &sock_in6_e->sin6_addr.in6_u,
28105b96892SNicholas Bellinger 			    sizeof(struct in6_addr)))
28205b96892SNicholas Bellinger 			ip_match = true;
28305b96892SNicholas Bellinger 
28405b96892SNicholas Bellinger 		port = ntohs(sock_in6->sin6_port);
28569d75574SAndy Grover 		port_e = ntohs(sock_in6_e->sin6_port);
28605b96892SNicholas Bellinger 	} else {
28705b96892SNicholas Bellinger 		sock_in = (struct sockaddr_in *)sockaddr;
28805b96892SNicholas Bellinger 		sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
28905b96892SNicholas Bellinger 
29005b96892SNicholas Bellinger 		if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
29105b96892SNicholas Bellinger 			ip_match = true;
29205b96892SNicholas Bellinger 
29305b96892SNicholas Bellinger 		port = ntohs(sock_in->sin_port);
29469d75574SAndy Grover 		port_e = ntohs(sock_in_e->sin_port);
29505b96892SNicholas Bellinger 	}
29605b96892SNicholas Bellinger 
29769d75574SAndy Grover 	if (ip_match && (port_e == port) &&
29805b96892SNicholas Bellinger 	    (np->np_network_transport == network_transport))
29905b96892SNicholas Bellinger 		return true;
30005b96892SNicholas Bellinger 
30105b96892SNicholas Bellinger 	return false;
30205b96892SNicholas Bellinger }
30305b96892SNicholas Bellinger 
iscsit_get_np(struct sockaddr_storage * sockaddr,int network_transport)30405b96892SNicholas Bellinger static struct iscsi_np *iscsit_get_np(
30513a3cf08SAndy Grover 	struct sockaddr_storage *sockaddr,
30605b96892SNicholas Bellinger 	int network_transport)
30705b96892SNicholas Bellinger {
30805b96892SNicholas Bellinger 	struct iscsi_np *np;
30905b96892SNicholas Bellinger 	bool match;
31005b96892SNicholas Bellinger 
311618baaf7SBart Van Assche 	lockdep_assert_held(&np_lock);
312618baaf7SBart Van Assche 
313e48354ceSNicholas Bellinger 	list_for_each_entry(np, &g_np_list, np_list) {
314ee291e63SAndy Grover 		spin_lock_bh(&np->np_thread_lock);
315e48354ceSNicholas Bellinger 		if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
316ee291e63SAndy Grover 			spin_unlock_bh(&np->np_thread_lock);
317e48354ceSNicholas Bellinger 			continue;
318e48354ceSNicholas Bellinger 		}
319e48354ceSNicholas Bellinger 
32005b96892SNicholas Bellinger 		match = iscsit_check_np_match(sockaddr, np, network_transport);
3210bcc297eSChristophe Vu-Brugier 		if (match) {
322e48354ceSNicholas Bellinger 			/*
323e48354ceSNicholas Bellinger 			 * Increment the np_exports reference count now to
324e48354ceSNicholas Bellinger 			 * prevent iscsit_del_np() below from being called
325e48354ceSNicholas Bellinger 			 * while iscsi_tpg_add_network_portal() is called.
326e48354ceSNicholas Bellinger 			 */
327e48354ceSNicholas Bellinger 			np->np_exports++;
328ee291e63SAndy Grover 			spin_unlock_bh(&np->np_thread_lock);
329e48354ceSNicholas Bellinger 			return np;
330e48354ceSNicholas Bellinger 		}
331ee291e63SAndy Grover 		spin_unlock_bh(&np->np_thread_lock);
332e48354ceSNicholas Bellinger 	}
333e48354ceSNicholas Bellinger 
334e48354ceSNicholas Bellinger 	return NULL;
335e48354ceSNicholas Bellinger }
336e48354ceSNicholas Bellinger 
iscsit_add_np(struct sockaddr_storage * sockaddr,int network_transport)337e48354ceSNicholas Bellinger struct iscsi_np *iscsit_add_np(
33813a3cf08SAndy Grover 	struct sockaddr_storage *sockaddr,
339e48354ceSNicholas Bellinger 	int network_transport)
340e48354ceSNicholas Bellinger {
341e48354ceSNicholas Bellinger 	struct iscsi_np *np;
342e48354ceSNicholas Bellinger 	int ret;
343ee291e63SAndy Grover 
344ee291e63SAndy Grover 	mutex_lock(&np_lock);
345ee291e63SAndy Grover 
346e48354ceSNicholas Bellinger 	/*
347e48354ceSNicholas Bellinger 	 * Locate the existing struct iscsi_np if already active..
348e48354ceSNicholas Bellinger 	 */
349e48354ceSNicholas Bellinger 	np = iscsit_get_np(sockaddr, network_transport);
350ee291e63SAndy Grover 	if (np) {
351ee291e63SAndy Grover 		mutex_unlock(&np_lock);
352e48354ceSNicholas Bellinger 		return np;
353ee291e63SAndy Grover 	}
354e48354ceSNicholas Bellinger 
3553829f381SMarkus Elfring 	np = kzalloc(sizeof(*np), GFP_KERNEL);
356e48354ceSNicholas Bellinger 	if (!np) {
357ee291e63SAndy Grover 		mutex_unlock(&np_lock);
358e48354ceSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
359e48354ceSNicholas Bellinger 	}
360e48354ceSNicholas Bellinger 
361e48354ceSNicholas Bellinger 	np->np_flags |= NPF_IP_NETWORK;
362e48354ceSNicholas Bellinger 	np->np_network_transport = network_transport;
363e48354ceSNicholas Bellinger 	spin_lock_init(&np->np_thread_lock);
364e48354ceSNicholas Bellinger 	init_completion(&np->np_restart_comp);
365e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&np->np_list);
366e48354ceSNicholas Bellinger 
367e48354ceSNicholas Bellinger 	ret = iscsi_target_setup_login_socket(np, sockaddr);
368e48354ceSNicholas Bellinger 	if (ret != 0) {
369e48354ceSNicholas Bellinger 		kfree(np);
370ee291e63SAndy Grover 		mutex_unlock(&np_lock);
371e48354ceSNicholas Bellinger 		return ERR_PTR(ret);
372e48354ceSNicholas Bellinger 	}
373e48354ceSNicholas Bellinger 
374e48354ceSNicholas Bellinger 	np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
375e48354ceSNicholas Bellinger 	if (IS_ERR(np->np_thread)) {
376e48354ceSNicholas Bellinger 		pr_err("Unable to create kthread: iscsi_np\n");
377e48354ceSNicholas Bellinger 		ret = PTR_ERR(np->np_thread);
378e48354ceSNicholas Bellinger 		kfree(np);
379ee291e63SAndy Grover 		mutex_unlock(&np_lock);
380e48354ceSNicholas Bellinger 		return ERR_PTR(ret);
381e48354ceSNicholas Bellinger 	}
382e48354ceSNicholas Bellinger 	/*
383e48354ceSNicholas Bellinger 	 * Increment the np_exports reference count now to prevent
384e48354ceSNicholas Bellinger 	 * iscsit_del_np() below from being run while a new call to
385e48354ceSNicholas Bellinger 	 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
386e48354ceSNicholas Bellinger 	 * active.  We don't need to hold np->np_thread_lock at this
387e48354ceSNicholas Bellinger 	 * point because iscsi_np has not been added to g_np_list yet.
388e48354ceSNicholas Bellinger 	 */
389e48354ceSNicholas Bellinger 	np->np_exports = 1;
390ee291e63SAndy Grover 	np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
391e48354ceSNicholas Bellinger 
392e48354ceSNicholas Bellinger 	list_add_tail(&np->np_list, &g_np_list);
393ee291e63SAndy Grover 	mutex_unlock(&np_lock);
394e48354ceSNicholas Bellinger 
39569d75574SAndy Grover 	pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
39669d75574SAndy Grover 		&np->np_sockaddr, np->np_transport->name);
397e48354ceSNicholas Bellinger 
398e48354ceSNicholas Bellinger 	return np;
399e48354ceSNicholas Bellinger }
400e48354ceSNicholas Bellinger 
iscsit_reset_np_thread(struct iscsi_np * np,struct iscsi_tpg_np * tpg_np,struct iscsi_portal_group * tpg,bool shutdown)401e48354ceSNicholas Bellinger int iscsit_reset_np_thread(
402e48354ceSNicholas Bellinger 	struct iscsi_np *np,
403e48354ceSNicholas Bellinger 	struct iscsi_tpg_np *tpg_np,
404a91eb7d9SNicholas Bellinger 	struct iscsi_portal_group *tpg,
405a91eb7d9SNicholas Bellinger 	bool shutdown)
406e48354ceSNicholas Bellinger {
407e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
408e48354ceSNicholas Bellinger 	if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
409e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
410e48354ceSNicholas Bellinger 		return 0;
411e48354ceSNicholas Bellinger 	}
412e48354ceSNicholas Bellinger 	np->np_thread_state = ISCSI_NP_THREAD_RESET;
413978d13d6SNicholas Bellinger 	atomic_inc(&np->np_reset_count);
414e48354ceSNicholas Bellinger 
415e48354ceSNicholas Bellinger 	if (np->np_thread) {
416e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
417e48354ceSNicholas Bellinger 		send_sig(SIGINT, np->np_thread, 1);
418e48354ceSNicholas Bellinger 		wait_for_completion(&np->np_restart_comp);
419e48354ceSNicholas Bellinger 		spin_lock_bh(&np->np_thread_lock);
420e48354ceSNicholas Bellinger 	}
421e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
422e48354ceSNicholas Bellinger 
423a91eb7d9SNicholas Bellinger 	if (tpg_np && shutdown) {
424a91eb7d9SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
425a91eb7d9SNicholas Bellinger 
426a91eb7d9SNicholas Bellinger 		wait_for_completion(&tpg_np->tpg_np_comp);
427a91eb7d9SNicholas Bellinger 	}
428a91eb7d9SNicholas Bellinger 
429e48354ceSNicholas Bellinger 	return 0;
430e48354ceSNicholas Bellinger }
431e48354ceSNicholas Bellinger 
iscsit_free_np(struct iscsi_np * np)432baa4d64bSNicholas Bellinger static void iscsit_free_np(struct iscsi_np *np)
433e48354ceSNicholas Bellinger {
434bf6932f4SAl Viro 	if (np->np_socket)
435e48354ceSNicholas Bellinger 		sock_release(np->np_socket);
436e48354ceSNicholas Bellinger }
437e48354ceSNicholas Bellinger 
iscsit_del_np(struct iscsi_np * np)438e48354ceSNicholas Bellinger int iscsit_del_np(struct iscsi_np *np)
439e48354ceSNicholas Bellinger {
440e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
441e48354ceSNicholas Bellinger 	np->np_exports--;
442e48354ceSNicholas Bellinger 	if (np->np_exports) {
4432363d196SNicholas Bellinger 		np->enabled = true;
444e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
445e48354ceSNicholas Bellinger 		return 0;
446e48354ceSNicholas Bellinger 	}
447e48354ceSNicholas Bellinger 	np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
448e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
449e48354ceSNicholas Bellinger 
450e48354ceSNicholas Bellinger 	if (np->np_thread) {
451e48354ceSNicholas Bellinger 		/*
452e48354ceSNicholas Bellinger 		 * We need to send the signal to wakeup Linux/Net
453e48354ceSNicholas Bellinger 		 * which may be sleeping in sock_accept()..
454e48354ceSNicholas Bellinger 		 */
455e48354ceSNicholas Bellinger 		send_sig(SIGINT, np->np_thread, 1);
456e48354ceSNicholas Bellinger 		kthread_stop(np->np_thread);
457db6077fdSNicholas Bellinger 		np->np_thread = NULL;
458e48354ceSNicholas Bellinger 	}
459baa4d64bSNicholas Bellinger 
460baa4d64bSNicholas Bellinger 	np->np_transport->iscsit_free_np(np);
461e48354ceSNicholas Bellinger 
462ee291e63SAndy Grover 	mutex_lock(&np_lock);
463e48354ceSNicholas Bellinger 	list_del(&np->np_list);
464ee291e63SAndy Grover 	mutex_unlock(&np_lock);
465e48354ceSNicholas Bellinger 
46669d75574SAndy Grover 	pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
46769d75574SAndy Grover 		&np->np_sockaddr, np->np_transport->name);
468e48354ceSNicholas Bellinger 
469baa4d64bSNicholas Bellinger 	iscsit_put_transport(np->np_transport);
470e48354ceSNicholas Bellinger 	kfree(np);
471e48354ceSNicholas Bellinger 	return 0;
472e48354ceSNicholas Bellinger }
473e48354ceSNicholas Bellinger 
474be36d683SMax Gurtovoy static void iscsit_get_rx_pdu(struct iscsit_conn *);
4752ec5a8c1SNicholas Bellinger 
iscsit_queue_rsp(struct iscsit_conn * conn,struct iscsit_cmd * cmd)476be36d683SMax Gurtovoy int iscsit_queue_rsp(struct iscsit_conn *conn, struct iscsit_cmd *cmd)
4772ec5a8c1SNicholas Bellinger {
478a4467018SNicholas Bellinger 	return iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
4792ec5a8c1SNicholas Bellinger }
480d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_queue_rsp);
4812ec5a8c1SNicholas Bellinger 
iscsit_aborted_task(struct iscsit_conn * conn,struct iscsit_cmd * cmd)482be36d683SMax Gurtovoy void iscsit_aborted_task(struct iscsit_conn *conn, struct iscsit_cmd *cmd)
483131e6abcSNicholas Bellinger {
484131e6abcSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
485f3619935SMike Christie 	if (!list_empty(&cmd->i_conn_node))
486131e6abcSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
487131e6abcSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
488131e6abcSNicholas Bellinger 
4894412a671SBart Van Assche 	__iscsit_free_cmd(cmd, true);
490131e6abcSNicholas Bellinger }
491d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_aborted_task);
492131e6abcSNicholas Bellinger 
4932854bb23SVarun Prakash static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
494e1dfb21fSBart Van Assche 				      u32, u32, const void *, void *);
495be36d683SMax Gurtovoy static void iscsit_tx_thread_wait_for_tcp(struct iscsit_conn *);
4962854bb23SVarun Prakash 
4972854bb23SVarun Prakash static int
iscsit_xmit_nondatain_pdu(struct iscsit_conn * conn,struct iscsit_cmd * cmd,const void * data_buf,u32 data_buf_len)498be36d683SMax Gurtovoy iscsit_xmit_nondatain_pdu(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
4992854bb23SVarun Prakash 			  const void *data_buf, u32 data_buf_len)
5002854bb23SVarun Prakash {
5012854bb23SVarun Prakash 	struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu;
5022854bb23SVarun Prakash 	struct kvec *iov;
5032854bb23SVarun Prakash 	u32 niov = 0, tx_size = ISCSI_HDR_LEN;
5042854bb23SVarun Prakash 	int ret;
5052854bb23SVarun Prakash 
5062854bb23SVarun Prakash 	iov = &cmd->iov_misc[0];
5072854bb23SVarun Prakash 	iov[niov].iov_base	= cmd->pdu;
5082854bb23SVarun Prakash 	iov[niov++].iov_len	= ISCSI_HDR_LEN;
5092854bb23SVarun Prakash 
5102854bb23SVarun Prakash 	if (conn->conn_ops->HeaderDigest) {
5112854bb23SVarun Prakash 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
5122854bb23SVarun Prakash 
5132854bb23SVarun Prakash 		iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
5142854bb23SVarun Prakash 					  ISCSI_HDR_LEN, 0, NULL,
515e1dfb21fSBart Van Assche 					  header_digest);
5162854bb23SVarun Prakash 
5172854bb23SVarun Prakash 		iov[0].iov_len += ISCSI_CRC_LEN;
5182854bb23SVarun Prakash 		tx_size += ISCSI_CRC_LEN;
5192854bb23SVarun Prakash 		pr_debug("Attaching CRC32C HeaderDigest"
5202854bb23SVarun Prakash 			 " to opcode 0x%x 0x%08x\n",
5212854bb23SVarun Prakash 			 hdr->opcode, *header_digest);
5222854bb23SVarun Prakash 	}
5232854bb23SVarun Prakash 
5242854bb23SVarun Prakash 	if (data_buf_len) {
5252854bb23SVarun Prakash 		u32 padding = ((-data_buf_len) & 3);
5262854bb23SVarun Prakash 
5272854bb23SVarun Prakash 		iov[niov].iov_base	= (void *)data_buf;
5282854bb23SVarun Prakash 		iov[niov++].iov_len	= data_buf_len;
5292854bb23SVarun Prakash 		tx_size += data_buf_len;
5302854bb23SVarun Prakash 
5312854bb23SVarun Prakash 		if (padding != 0) {
5322854bb23SVarun Prakash 			iov[niov].iov_base = &cmd->pad_bytes;
5332854bb23SVarun Prakash 			iov[niov++].iov_len = padding;
5342854bb23SVarun Prakash 			tx_size += padding;
5352854bb23SVarun Prakash 			pr_debug("Attaching %u additional"
5362854bb23SVarun Prakash 				 " padding bytes.\n", padding);
5372854bb23SVarun Prakash 		}
5382854bb23SVarun Prakash 
5392854bb23SVarun Prakash 		if (conn->conn_ops->DataDigest) {
5402854bb23SVarun Prakash 			iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
5412854bb23SVarun Prakash 						  data_buf, data_buf_len,
542e1dfb21fSBart Van Assche 						  padding, &cmd->pad_bytes,
543e1dfb21fSBart Van Assche 						  &cmd->data_crc);
5442854bb23SVarun Prakash 
5452854bb23SVarun Prakash 			iov[niov].iov_base = &cmd->data_crc;
5462854bb23SVarun Prakash 			iov[niov++].iov_len = ISCSI_CRC_LEN;
5472854bb23SVarun Prakash 			tx_size += ISCSI_CRC_LEN;
5482854bb23SVarun Prakash 			pr_debug("Attached DataDigest for %u"
5492854bb23SVarun Prakash 				 " bytes opcode 0x%x, CRC 0x%08x\n",
5502854bb23SVarun Prakash 				 data_buf_len, hdr->opcode, cmd->data_crc);
5512854bb23SVarun Prakash 		}
5522854bb23SVarun Prakash 	}
5532854bb23SVarun Prakash 
5542854bb23SVarun Prakash 	cmd->iov_misc_count = niov;
5552854bb23SVarun Prakash 	cmd->tx_size = tx_size;
5562854bb23SVarun Prakash 
5572854bb23SVarun Prakash 	ret = iscsit_send_tx_data(cmd, conn, 1);
5582854bb23SVarun Prakash 	if (ret < 0) {
5592854bb23SVarun Prakash 		iscsit_tx_thread_wait_for_tcp(conn);
5602854bb23SVarun Prakash 		return ret;
5612854bb23SVarun Prakash 	}
5622854bb23SVarun Prakash 
5632854bb23SVarun Prakash 	return 0;
5642854bb23SVarun Prakash }
5652854bb23SVarun Prakash 
56666cd9d4eSMax Gurtovoy static int iscsit_map_iovec(struct iscsit_cmd *cmd, struct kvec *iov, int nvec,
5672e39f1c9SBart Van Assche 			    u32 data_offset, u32 data_length);
56866cd9d4eSMax Gurtovoy static void iscsit_unmap_iovec(struct iscsit_cmd *);
56966cd9d4eSMax Gurtovoy static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsit_cmd *,
5702854bb23SVarun Prakash 				    u32, u32, u32, u8 *);
5712854bb23SVarun Prakash static int
iscsit_xmit_datain_pdu(struct iscsit_conn * conn,struct iscsit_cmd * cmd,const struct iscsi_datain * datain)572be36d683SMax Gurtovoy iscsit_xmit_datain_pdu(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
5732854bb23SVarun Prakash 		       const struct iscsi_datain *datain)
5742854bb23SVarun Prakash {
5752854bb23SVarun Prakash 	struct kvec *iov;
5762854bb23SVarun Prakash 	u32 iov_count = 0, tx_size = 0;
5772854bb23SVarun Prakash 	int ret, iov_ret;
5782854bb23SVarun Prakash 
5792854bb23SVarun Prakash 	iov = &cmd->iov_data[0];
5802854bb23SVarun Prakash 	iov[iov_count].iov_base	= cmd->pdu;
5812854bb23SVarun Prakash 	iov[iov_count++].iov_len = ISCSI_HDR_LEN;
5822854bb23SVarun Prakash 	tx_size += ISCSI_HDR_LEN;
5832854bb23SVarun Prakash 
5842854bb23SVarun Prakash 	if (conn->conn_ops->HeaderDigest) {
5852854bb23SVarun Prakash 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
5862854bb23SVarun Prakash 
5872854bb23SVarun Prakash 		iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
5882854bb23SVarun Prakash 					  ISCSI_HDR_LEN, 0, NULL,
589e1dfb21fSBart Van Assche 					  header_digest);
5902854bb23SVarun Prakash 
5912854bb23SVarun Prakash 		iov[0].iov_len += ISCSI_CRC_LEN;
5922854bb23SVarun Prakash 		tx_size += ISCSI_CRC_LEN;
5932854bb23SVarun Prakash 
5942854bb23SVarun Prakash 		pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
5952854bb23SVarun Prakash 			 *header_digest);
5962854bb23SVarun Prakash 	}
5972854bb23SVarun Prakash 
5982e39f1c9SBart Van Assche 	iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[iov_count],
5992e39f1c9SBart Van Assche 				   cmd->orig_iov_data_count - (iov_count + 2),
6002854bb23SVarun Prakash 				   datain->offset, datain->length);
6012854bb23SVarun Prakash 	if (iov_ret < 0)
6022854bb23SVarun Prakash 		return -1;
6032854bb23SVarun Prakash 
6042854bb23SVarun Prakash 	iov_count += iov_ret;
6052854bb23SVarun Prakash 	tx_size += datain->length;
6062854bb23SVarun Prakash 
6072854bb23SVarun Prakash 	cmd->padding = ((-datain->length) & 3);
6082854bb23SVarun Prakash 	if (cmd->padding) {
6092854bb23SVarun Prakash 		iov[iov_count].iov_base		= cmd->pad_bytes;
6102854bb23SVarun Prakash 		iov[iov_count++].iov_len	= cmd->padding;
6112854bb23SVarun Prakash 		tx_size += cmd->padding;
6122854bb23SVarun Prakash 
6132854bb23SVarun Prakash 		pr_debug("Attaching %u padding bytes\n", cmd->padding);
6142854bb23SVarun Prakash 	}
6152854bb23SVarun Prakash 
6162854bb23SVarun Prakash 	if (conn->conn_ops->DataDigest) {
6172854bb23SVarun Prakash 		cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
6182854bb23SVarun Prakash 							 cmd, datain->offset,
6192854bb23SVarun Prakash 							 datain->length,
6202854bb23SVarun Prakash 							 cmd->padding,
6212854bb23SVarun Prakash 							 cmd->pad_bytes);
6222854bb23SVarun Prakash 
6232854bb23SVarun Prakash 		iov[iov_count].iov_base	= &cmd->data_crc;
6242854bb23SVarun Prakash 		iov[iov_count++].iov_len = ISCSI_CRC_LEN;
6252854bb23SVarun Prakash 		tx_size += ISCSI_CRC_LEN;
6262854bb23SVarun Prakash 
6272854bb23SVarun Prakash 		pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
6282854bb23SVarun Prakash 			 datain->length + cmd->padding, cmd->data_crc);
6292854bb23SVarun Prakash 	}
6302854bb23SVarun Prakash 
6312854bb23SVarun Prakash 	cmd->iov_data_count = iov_count;
6322854bb23SVarun Prakash 	cmd->tx_size = tx_size;
6332854bb23SVarun Prakash 
6342854bb23SVarun Prakash 	ret = iscsit_fe_sendpage_sg(cmd, conn);
6352854bb23SVarun Prakash 
6362854bb23SVarun Prakash 	iscsit_unmap_iovec(cmd);
6372854bb23SVarun Prakash 
6382854bb23SVarun Prakash 	if (ret < 0) {
6392854bb23SVarun Prakash 		iscsit_tx_thread_wait_for_tcp(conn);
6402854bb23SVarun Prakash 		return ret;
6412854bb23SVarun Prakash 	}
6422854bb23SVarun Prakash 
6432854bb23SVarun Prakash 	return 0;
6442854bb23SVarun Prakash }
6452854bb23SVarun Prakash 
iscsit_xmit_pdu(struct iscsit_conn * conn,struct iscsit_cmd * cmd,struct iscsi_datain_req * dr,const void * buf,u32 buf_len)646be36d683SMax Gurtovoy static int iscsit_xmit_pdu(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
6472854bb23SVarun Prakash 			   struct iscsi_datain_req *dr, const void *buf,
6482854bb23SVarun Prakash 			   u32 buf_len)
6492854bb23SVarun Prakash {
6502854bb23SVarun Prakash 	if (dr)
6512854bb23SVarun Prakash 		return iscsit_xmit_datain_pdu(conn, cmd, buf);
6522854bb23SVarun Prakash 	else
6532854bb23SVarun Prakash 		return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len);
6542854bb23SVarun Prakash }
6552854bb23SVarun Prakash 
iscsit_get_sup_prot_ops(struct iscsit_conn * conn)656be36d683SMax Gurtovoy static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsit_conn *conn)
657e70beee7SNicholas Bellinger {
658e70beee7SNicholas Bellinger 	return TARGET_PROT_NORMAL;
659e70beee7SNicholas Bellinger }
660e70beee7SNicholas Bellinger 
661baa4d64bSNicholas Bellinger static struct iscsit_transport iscsi_target_transport = {
662baa4d64bSNicholas Bellinger 	.name			= "iSCSI/TCP",
663baa4d64bSNicholas Bellinger 	.transport_type		= ISCSI_TCP,
664bd027d85SNicholas Bellinger 	.rdma_shutdown		= false,
665baa4d64bSNicholas Bellinger 	.owner			= NULL,
666baa4d64bSNicholas Bellinger 	.iscsit_setup_np	= iscsit_setup_np,
667baa4d64bSNicholas Bellinger 	.iscsit_accept_np	= iscsit_accept_np,
668baa4d64bSNicholas Bellinger 	.iscsit_free_np		= iscsit_free_np,
669baa4d64bSNicholas Bellinger 	.iscsit_get_login_rx	= iscsit_get_login_rx,
670baa4d64bSNicholas Bellinger 	.iscsit_put_login_tx	= iscsit_put_login_tx,
6713e1c81a9SNicholas Bellinger 	.iscsit_get_dataout	= iscsit_build_r2ts_for_cmd,
6722ec5a8c1SNicholas Bellinger 	.iscsit_immediate_queue	= iscsit_immediate_queue,
6732ec5a8c1SNicholas Bellinger 	.iscsit_response_queue	= iscsit_response_queue,
6742ec5a8c1SNicholas Bellinger 	.iscsit_queue_data_in	= iscsit_queue_rsp,
6752ec5a8c1SNicholas Bellinger 	.iscsit_queue_status	= iscsit_queue_rsp,
676131e6abcSNicholas Bellinger 	.iscsit_aborted_task	= iscsit_aborted_task,
6772854bb23SVarun Prakash 	.iscsit_xmit_pdu	= iscsit_xmit_pdu,
678e8205ccaSVarun Prakash 	.iscsit_get_rx_pdu	= iscsit_get_rx_pdu,
679e70beee7SNicholas Bellinger 	.iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
680baa4d64bSNicholas Bellinger };
681baa4d64bSNicholas Bellinger 
iscsi_target_init_module(void)682e48354ceSNicholas Bellinger static int __init iscsi_target_init_module(void)
683e48354ceSNicholas Bellinger {
68488dcd2daSNicholas Bellinger 	int ret = 0, size;
685e48354ceSNicholas Bellinger 
686e48354ceSNicholas Bellinger 	pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
6873829f381SMarkus Elfring 	iscsit_global = kzalloc(sizeof(*iscsit_global), GFP_KERNEL);
688c46e22f1SMarkus Elfring 	if (!iscsit_global)
689e48354ceSNicholas Bellinger 		return -1;
690c46e22f1SMarkus Elfring 
69188dcd2daSNicholas Bellinger 	spin_lock_init(&iscsit_global->ts_bitmap_lock);
692e48354ceSNicholas Bellinger 	mutex_init(&auth_id_lock);
693e48354ceSNicholas Bellinger 	idr_init(&tiqn_idr);
694e48354ceSNicholas Bellinger 
6959ac8928eSChristoph Hellwig 	ret = target_register_template(&iscsi_ops);
6969ac8928eSChristoph Hellwig 	if (ret)
697e48354ceSNicholas Bellinger 		goto out;
698e48354ceSNicholas Bellinger 
69988dcd2daSNicholas Bellinger 	size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
70088dcd2daSNicholas Bellinger 	iscsit_global->ts_bitmap = vzalloc(size);
701c46e22f1SMarkus Elfring 	if (!iscsit_global->ts_bitmap)
702e48354ceSNicholas Bellinger 		goto configfs_out;
703e48354ceSNicholas Bellinger 
704d72d827fSMingzhe Zou 	if (!zalloc_cpumask_var(&iscsit_global->allowed_cpumask, GFP_KERNEL)) {
705d72d827fSMingzhe Zou 		pr_err("Unable to allocate iscsit_global->allowed_cpumask\n");
706d72d827fSMingzhe Zou 		goto bitmap_out;
707d72d827fSMingzhe Zou 	}
708d72d827fSMingzhe Zou 	cpumask_setall(iscsit_global->allowed_cpumask);
709d72d827fSMingzhe Zou 
710e48354ceSNicholas Bellinger 	lio_qr_cache = kmem_cache_create("lio_qr_cache",
711e48354ceSNicholas Bellinger 			sizeof(struct iscsi_queue_req),
712e48354ceSNicholas Bellinger 			__alignof__(struct iscsi_queue_req), 0, NULL);
713e48354ceSNicholas Bellinger 	if (!lio_qr_cache) {
714621a4367SLeo Zhang 		pr_err("Unable to kmem_cache_create() for"
715e48354ceSNicholas Bellinger 				" lio_qr_cache\n");
716d72d827fSMingzhe Zou 		goto cpumask_out;
717e48354ceSNicholas Bellinger 	}
718e48354ceSNicholas Bellinger 
719e48354ceSNicholas Bellinger 	lio_dr_cache = kmem_cache_create("lio_dr_cache",
720e48354ceSNicholas Bellinger 			sizeof(struct iscsi_datain_req),
721e48354ceSNicholas Bellinger 			__alignof__(struct iscsi_datain_req), 0, NULL);
722e48354ceSNicholas Bellinger 	if (!lio_dr_cache) {
723e48354ceSNicholas Bellinger 		pr_err("Unable to kmem_cache_create() for"
724e48354ceSNicholas Bellinger 				" lio_dr_cache\n");
725e48354ceSNicholas Bellinger 		goto qr_out;
726e48354ceSNicholas Bellinger 	}
727e48354ceSNicholas Bellinger 
728e48354ceSNicholas Bellinger 	lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
729e48354ceSNicholas Bellinger 			sizeof(struct iscsi_ooo_cmdsn),
730e48354ceSNicholas Bellinger 			__alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
731e48354ceSNicholas Bellinger 	if (!lio_ooo_cache) {
732e48354ceSNicholas Bellinger 		pr_err("Unable to kmem_cache_create() for"
733e48354ceSNicholas Bellinger 				" lio_ooo_cache\n");
734e48354ceSNicholas Bellinger 		goto dr_out;
735e48354ceSNicholas Bellinger 	}
736e48354ceSNicholas Bellinger 
737e48354ceSNicholas Bellinger 	lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
738e48354ceSNicholas Bellinger 			sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
739e48354ceSNicholas Bellinger 			0, NULL);
740e48354ceSNicholas Bellinger 	if (!lio_r2t_cache) {
741e48354ceSNicholas Bellinger 		pr_err("Unable to kmem_cache_create() for"
742e48354ceSNicholas Bellinger 				" lio_r2t_cache\n");
743e48354ceSNicholas Bellinger 		goto ooo_out;
744e48354ceSNicholas Bellinger 	}
745e48354ceSNicholas Bellinger 
746baa4d64bSNicholas Bellinger 	iscsit_register_transport(&iscsi_target_transport);
747baa4d64bSNicholas Bellinger 
748e48354ceSNicholas Bellinger 	if (iscsit_load_discovery_tpg() < 0)
749e48354ceSNicholas Bellinger 		goto r2t_out;
750e48354ceSNicholas Bellinger 
751e48354ceSNicholas Bellinger 	return ret;
752e48354ceSNicholas Bellinger r2t_out:
7537f2c53bbSLino Sanfilippo 	iscsit_unregister_transport(&iscsi_target_transport);
754e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_r2t_cache);
755e48354ceSNicholas Bellinger ooo_out:
756e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_ooo_cache);
757e48354ceSNicholas Bellinger dr_out:
758e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_dr_cache);
759e48354ceSNicholas Bellinger qr_out:
760e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_qr_cache);
761d72d827fSMingzhe Zou cpumask_out:
762d72d827fSMingzhe Zou 	free_cpumask_var(iscsit_global->allowed_cpumask);
76388dcd2daSNicholas Bellinger bitmap_out:
76488dcd2daSNicholas Bellinger 	vfree(iscsit_global->ts_bitmap);
765e48354ceSNicholas Bellinger configfs_out:
7669ac8928eSChristoph Hellwig 	/* XXX: this probably wants it to be it's own unwind step.. */
7679ac8928eSChristoph Hellwig 	if (iscsit_global->discovery_tpg)
7689ac8928eSChristoph Hellwig 		iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
7699ac8928eSChristoph Hellwig 	target_unregister_template(&iscsi_ops);
770e48354ceSNicholas Bellinger out:
771e48354ceSNicholas Bellinger 	kfree(iscsit_global);
772e48354ceSNicholas Bellinger 	return -ENOMEM;
773e48354ceSNicholas Bellinger }
774e48354ceSNicholas Bellinger 
iscsi_target_cleanup_module(void)775e48354ceSNicholas Bellinger static void __exit iscsi_target_cleanup_module(void)
776e48354ceSNicholas Bellinger {
777e48354ceSNicholas Bellinger 	iscsit_release_discovery_tpg();
778baa4d64bSNicholas Bellinger 	iscsit_unregister_transport(&iscsi_target_transport);
779e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_qr_cache);
780e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_dr_cache);
781e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_ooo_cache);
782e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_r2t_cache);
783e48354ceSNicholas Bellinger 
7849ac8928eSChristoph Hellwig 	/*
7859ac8928eSChristoph Hellwig 	 * Shutdown discovery sessions and disable discovery TPG
7869ac8928eSChristoph Hellwig 	 */
7879ac8928eSChristoph Hellwig 	if (iscsit_global->discovery_tpg)
7889ac8928eSChristoph Hellwig 		iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
789e48354ceSNicholas Bellinger 
7909ac8928eSChristoph Hellwig 	target_unregister_template(&iscsi_ops);
791e48354ceSNicholas Bellinger 
792d72d827fSMingzhe Zou 	free_cpumask_var(iscsit_global->allowed_cpumask);
79388dcd2daSNicholas Bellinger 	vfree(iscsit_global->ts_bitmap);
794e48354ceSNicholas Bellinger 	kfree(iscsit_global);
795e48354ceSNicholas Bellinger }
796e48354ceSNicholas Bellinger 
iscsit_add_reject(struct iscsit_conn * conn,u8 reason,unsigned char * buf)797d2faaefbSVarun Prakash int iscsit_add_reject(
798be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
799e48354ceSNicholas Bellinger 	u8 reason,
800ba159914SNicholas Bellinger 	unsigned char *buf)
801e48354ceSNicholas Bellinger {
80266cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
803e48354ceSNicholas Bellinger 
804676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
805e48354ceSNicholas Bellinger 	if (!cmd)
806e48354ceSNicholas Bellinger 		return -1;
807e48354ceSNicholas Bellinger 
808e48354ceSNicholas Bellinger 	cmd->iscsi_opcode = ISCSI_OP_REJECT;
809ba159914SNicholas Bellinger 	cmd->reject_reason = reason;
810e48354ceSNicholas Bellinger 
8111c3d5794SThomas Meyer 	cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
812e48354ceSNicholas Bellinger 	if (!cmd->buf_ptr) {
813e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for cmd->buf_ptr\n");
814aafc9d15SNicholas Bellinger 		iscsit_free_cmd(cmd, false);
815e48354ceSNicholas Bellinger 		return -1;
816e48354ceSNicholas Bellinger 	}
817e48354ceSNicholas Bellinger 
818e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
8192fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
820e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
821e48354ceSNicholas Bellinger 
822e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_REJECT;
823e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
824e48354ceSNicholas Bellinger 
825e48354ceSNicholas Bellinger 	return -1;
826e48354ceSNicholas Bellinger }
827d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_add_reject);
828e48354ceSNicholas Bellinger 
iscsit_add_reject_from_cmd(struct iscsit_cmd * cmd,u8 reason,bool add_to_conn,unsigned char * buf)829ba159914SNicholas Bellinger static int iscsit_add_reject_from_cmd(
83066cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
831e48354ceSNicholas Bellinger 	u8 reason,
832ba159914SNicholas Bellinger 	bool add_to_conn,
833ba159914SNicholas Bellinger 	unsigned char *buf)
834e48354ceSNicholas Bellinger {
835be36d683SMax Gurtovoy 	struct iscsit_conn *conn;
836cfe2b621SBart Van Assche 	const bool do_put = cmd->se_cmd.se_tfo != NULL;
837e48354ceSNicholas Bellinger 
838e48354ceSNicholas Bellinger 	if (!cmd->conn) {
839e48354ceSNicholas Bellinger 		pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
840e48354ceSNicholas Bellinger 				cmd->init_task_tag);
841e48354ceSNicholas Bellinger 		return -1;
842e48354ceSNicholas Bellinger 	}
843e48354ceSNicholas Bellinger 	conn = cmd->conn;
844e48354ceSNicholas Bellinger 
845e48354ceSNicholas Bellinger 	cmd->iscsi_opcode = ISCSI_OP_REJECT;
846ba159914SNicholas Bellinger 	cmd->reject_reason = reason;
847e48354ceSNicholas Bellinger 
8481c3d5794SThomas Meyer 	cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
849e48354ceSNicholas Bellinger 	if (!cmd->buf_ptr) {
850e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for cmd->buf_ptr\n");
851aafc9d15SNicholas Bellinger 		iscsit_free_cmd(cmd, false);
852e48354ceSNicholas Bellinger 		return -1;
853e48354ceSNicholas Bellinger 	}
854e48354ceSNicholas Bellinger 
855e48354ceSNicholas Bellinger 	if (add_to_conn) {
856e48354ceSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
8572fbb471eSAndy Grover 		list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
858e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
859e48354ceSNicholas Bellinger 	}
860e48354ceSNicholas Bellinger 
861e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_REJECT;
862e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
8633e1c81a9SNicholas Bellinger 	/*
8643e1c81a9SNicholas Bellinger 	 * Perform the kref_put now if se_cmd has already been setup by
8653e1c81a9SNicholas Bellinger 	 * scsit_setup_scsi_cmd()
8663e1c81a9SNicholas Bellinger 	 */
867cfe2b621SBart Van Assche 	if (do_put) {
8683e1c81a9SNicholas Bellinger 		pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
869afc16604SBart Van Assche 		target_put_sess_cmd(&cmd->se_cmd);
8703e1c81a9SNicholas Bellinger 	}
871e48354ceSNicholas Bellinger 	return -1;
872e48354ceSNicholas Bellinger }
873ba159914SNicholas Bellinger 
iscsit_add_reject_cmd(struct iscsit_cmd * cmd,u8 reason,unsigned char * buf)87466cd9d4eSMax Gurtovoy static int iscsit_add_reject_cmd(struct iscsit_cmd *cmd, u8 reason,
875ba159914SNicholas Bellinger 				 unsigned char *buf)
876ba159914SNicholas Bellinger {
877ba159914SNicholas Bellinger 	return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
878ba159914SNicholas Bellinger }
879ba159914SNicholas Bellinger 
iscsit_reject_cmd(struct iscsit_cmd * cmd,u8 reason,unsigned char * buf)88066cd9d4eSMax Gurtovoy int iscsit_reject_cmd(struct iscsit_cmd *cmd, u8 reason, unsigned char *buf)
881ba159914SNicholas Bellinger {
882ba159914SNicholas Bellinger 	return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
883ba159914SNicholas Bellinger }
884d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_reject_cmd);
885e48354ceSNicholas Bellinger 
886e48354ceSNicholas Bellinger /*
887e48354ceSNicholas Bellinger  * Map some portion of the allocated scatterlist to an iovec, suitable for
888bfb79eacSAndy Grover  * kernel sockets to copy data in/out.
889e48354ceSNicholas Bellinger  */
iscsit_map_iovec(struct iscsit_cmd * cmd,struct kvec * iov,int nvec,u32 data_offset,u32 data_length)89066cd9d4eSMax Gurtovoy static int iscsit_map_iovec(struct iscsit_cmd *cmd, struct kvec *iov, int nvec,
8912e39f1c9SBart Van Assche 			    u32 data_offset, u32 data_length)
892e48354ceSNicholas Bellinger {
8932e39f1c9SBart Van Assche 	u32 i = 0, orig_data_length = data_length;
894e48354ceSNicholas Bellinger 	struct scatterlist *sg;
895e48354ceSNicholas Bellinger 	unsigned int page_off;
896e48354ceSNicholas Bellinger 
897e48354ceSNicholas Bellinger 	/*
898bfb79eacSAndy Grover 	 * We know each entry in t_data_sg contains a page.
899e48354ceSNicholas Bellinger 	 */
9002b16509cSImran Haider 	u32 ent = data_offset / PAGE_SIZE;
9012b16509cSImran Haider 
9022e39f1c9SBart Van Assche 	if (!data_length)
9032e39f1c9SBart Van Assche 		return 0;
9042e39f1c9SBart Van Assche 
9052b16509cSImran Haider 	if (ent >= cmd->se_cmd.t_data_nents) {
9062b16509cSImran Haider 		pr_err("Initial page entry out-of-bounds\n");
9072e39f1c9SBart Van Assche 		goto overflow;
9082b16509cSImran Haider 	}
9092b16509cSImran Haider 
9102b16509cSImran Haider 	sg = &cmd->se_cmd.t_data_sg[ent];
911e48354ceSNicholas Bellinger 	page_off = (data_offset % PAGE_SIZE);
912e48354ceSNicholas Bellinger 
913e48354ceSNicholas Bellinger 	cmd->first_data_sg = sg;
914e48354ceSNicholas Bellinger 	cmd->first_data_sg_off = page_off;
915e48354ceSNicholas Bellinger 
916e48354ceSNicholas Bellinger 	while (data_length) {
9172e39f1c9SBart Van Assche 		u32 cur_len;
9182e39f1c9SBart Van Assche 
9192e39f1c9SBart Van Assche 		if (WARN_ON_ONCE(!sg || i >= nvec))
9202e39f1c9SBart Van Assche 			goto overflow;
9212e39f1c9SBart Van Assche 
9222e39f1c9SBart Van Assche 		cur_len = min_t(u32, data_length, sg->length - page_off);
923e48354ceSNicholas Bellinger 
924e48354ceSNicholas Bellinger 		iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
925e48354ceSNicholas Bellinger 		iov[i].iov_len = cur_len;
926e48354ceSNicholas Bellinger 
927e48354ceSNicholas Bellinger 		data_length -= cur_len;
928e48354ceSNicholas Bellinger 		page_off = 0;
929e48354ceSNicholas Bellinger 		sg = sg_next(sg);
930e48354ceSNicholas Bellinger 		i++;
931e48354ceSNicholas Bellinger 	}
932e48354ceSNicholas Bellinger 
933e48354ceSNicholas Bellinger 	cmd->kmapped_nents = i;
934e48354ceSNicholas Bellinger 
935e48354ceSNicholas Bellinger 	return i;
9362e39f1c9SBart Van Assche 
9372e39f1c9SBart Van Assche overflow:
9382e39f1c9SBart Van Assche 	pr_err("offset %d + length %d overflow; %d/%d; sg-list:\n",
9392e39f1c9SBart Van Assche 	       data_offset, orig_data_length, i, nvec);
9402e39f1c9SBart Van Assche 	for_each_sg(cmd->se_cmd.t_data_sg, sg,
9412e39f1c9SBart Van Assche 		    cmd->se_cmd.t_data_nents, i) {
9422e39f1c9SBart Van Assche 		pr_err("[%d] off %d len %d\n",
9432e39f1c9SBart Van Assche 		       i, sg->offset, sg->length);
9442e39f1c9SBart Van Assche 	}
9452e39f1c9SBart Van Assche 	return -1;
946e48354ceSNicholas Bellinger }
947e48354ceSNicholas Bellinger 
iscsit_unmap_iovec(struct iscsit_cmd * cmd)94866cd9d4eSMax Gurtovoy static void iscsit_unmap_iovec(struct iscsit_cmd *cmd)
949e48354ceSNicholas Bellinger {
950e48354ceSNicholas Bellinger 	u32 i;
951e48354ceSNicholas Bellinger 	struct scatterlist *sg;
952e48354ceSNicholas Bellinger 
953e48354ceSNicholas Bellinger 	sg = cmd->first_data_sg;
954e48354ceSNicholas Bellinger 
955e48354ceSNicholas Bellinger 	for (i = 0; i < cmd->kmapped_nents; i++)
956e48354ceSNicholas Bellinger 		kunmap(sg_page(&sg[i]));
957e48354ceSNicholas Bellinger }
958e48354ceSNicholas Bellinger 
iscsit_ack_from_expstatsn(struct iscsit_conn * conn,u32 exp_statsn)959be36d683SMax Gurtovoy static void iscsit_ack_from_expstatsn(struct iscsit_conn *conn, u32 exp_statsn)
960e48354ceSNicholas Bellinger {
961f56cbbb4SNicholas Bellinger 	LIST_HEAD(ack_list);
96266cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd, *cmd_p;
963e48354ceSNicholas Bellinger 
964e48354ceSNicholas Bellinger 	conn->exp_statsn = exp_statsn;
965e48354ceSNicholas Bellinger 
9663e1c81a9SNicholas Bellinger 	if (conn->sess->sess_ops->RDMAExtensions)
9673e1c81a9SNicholas Bellinger 		return;
9683e1c81a9SNicholas Bellinger 
969e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
970f56cbbb4SNicholas Bellinger 	list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
971e48354ceSNicholas Bellinger 		spin_lock(&cmd->istate_lock);
972e48354ceSNicholas Bellinger 		if ((cmd->i_state == ISTATE_SENT_STATUS) &&
97364c13330SSteve Hodgson 		    iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
974e48354ceSNicholas Bellinger 			cmd->i_state = ISTATE_REMOVE;
975e48354ceSNicholas Bellinger 			spin_unlock(&cmd->istate_lock);
976f56cbbb4SNicholas Bellinger 			list_move_tail(&cmd->i_conn_node, &ack_list);
977e48354ceSNicholas Bellinger 			continue;
978e48354ceSNicholas Bellinger 		}
979e48354ceSNicholas Bellinger 		spin_unlock(&cmd->istate_lock);
980e48354ceSNicholas Bellinger 	}
981e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
982f56cbbb4SNicholas Bellinger 
983f56cbbb4SNicholas Bellinger 	list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
9845159d763SNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
985f56cbbb4SNicholas Bellinger 		iscsit_free_cmd(cmd, false);
986f56cbbb4SNicholas Bellinger 	}
987e48354ceSNicholas Bellinger }
988e48354ceSNicholas Bellinger 
iscsit_allocate_iovecs(struct iscsit_cmd * cmd)98966cd9d4eSMax Gurtovoy static int iscsit_allocate_iovecs(struct iscsit_cmd *cmd)
990e48354ceSNicholas Bellinger {
991f80e8ed3SNicholas Bellinger 	u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
992e48354ceSNicholas Bellinger 
993c0427f15SChristoph Hellwig 	iov_count += ISCSI_IOV_DATA_BUFFER;
994f1725110SMarkus Elfring 	cmd->iov_data = kcalloc(iov_count, sizeof(*cmd->iov_data), GFP_KERNEL);
995c46e22f1SMarkus Elfring 	if (!cmd->iov_data)
996e48354ceSNicholas Bellinger 		return -ENOMEM;
997e48354ceSNicholas Bellinger 
998e48354ceSNicholas Bellinger 	cmd->orig_iov_data_count = iov_count;
999e48354ceSNicholas Bellinger 	return 0;
1000e48354ceSNicholas Bellinger }
1001e48354ceSNicholas Bellinger 
iscsit_setup_scsi_cmd(struct iscsit_conn * conn,struct iscsit_cmd * cmd,unsigned char * buf)1002be36d683SMax Gurtovoy int iscsit_setup_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
1003e48354ceSNicholas Bellinger 			  unsigned char *buf)
1004e48354ceSNicholas Bellinger {
10053e1c81a9SNicholas Bellinger 	int data_direction, payload_length;
10068f1f7d29SDmitry Bogdanov 	struct iscsi_ecdb_ahdr *ecdb_ahdr;
1007e48354ceSNicholas Bellinger 	struct iscsi_scsi_req *hdr;
1008d28b1169SAndy Grover 	int iscsi_task_attr;
10098f1f7d29SDmitry Bogdanov 	unsigned char *cdb;
1010d28b1169SAndy Grover 	int sam_task_attr;
1011e48354ceSNicholas Bellinger 
101204f3b31bSNicholas Bellinger 	atomic_long_inc(&conn->sess->cmd_pdus);
1013e48354ceSNicholas Bellinger 
1014e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_scsi_req *) buf;
1015e48354ceSNicholas Bellinger 	payload_length		= ntoh24(hdr->dlength);
1016e48354ceSNicholas Bellinger 
1017e48354ceSNicholas Bellinger 	/* FIXME; Add checks for AdditionalHeaderSegment */
1018e48354ceSNicholas Bellinger 
1019e48354ceSNicholas Bellinger 	if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
1020e48354ceSNicholas Bellinger 	    !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1021e48354ceSNicholas Bellinger 		pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1022e48354ceSNicholas Bellinger 				" not set. Bad iSCSI Initiator.\n");
1023ba159914SNicholas Bellinger 		return iscsit_add_reject_cmd(cmd,
1024ba159914SNicholas Bellinger 					     ISCSI_REASON_BOOKMARK_INVALID, buf);
1025e48354ceSNicholas Bellinger 	}
1026e48354ceSNicholas Bellinger 
1027e48354ceSNicholas Bellinger 	if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
1028e48354ceSNicholas Bellinger 	     (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
1029e48354ceSNicholas Bellinger 		/*
10304454b66cSNicholas Bellinger 		 * From RFC-3720 Section 10.3.1:
10314454b66cSNicholas Bellinger 		 *
10324454b66cSNicholas Bellinger 		 * "Either or both of R and W MAY be 1 when either the
10334454b66cSNicholas Bellinger 		 *  Expected Data Transfer Length and/or Bidirectional Read
10344454b66cSNicholas Bellinger 		 *  Expected Data Transfer Length are 0"
10354454b66cSNicholas Bellinger 		 *
10364454b66cSNicholas Bellinger 		 * For this case, go ahead and clear the unnecssary bits
10374454b66cSNicholas Bellinger 		 * to avoid any confusion with ->data_direction.
1038e48354ceSNicholas Bellinger 		 */
1039e48354ceSNicholas Bellinger 		hdr->flags &= ~ISCSI_FLAG_CMD_READ;
1040e48354ceSNicholas Bellinger 		hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
1041e48354ceSNicholas Bellinger 
10424454b66cSNicholas Bellinger 		pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
1043e48354ceSNicholas Bellinger 			" set when Expected Data Transfer Length is 0 for"
10444454b66cSNicholas Bellinger 			" CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
1045e48354ceSNicholas Bellinger 	}
1046e48354ceSNicholas Bellinger 
1047e48354ceSNicholas Bellinger 	if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
1048e48354ceSNicholas Bellinger 	    !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
1049e48354ceSNicholas Bellinger 		pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1050e48354ceSNicholas Bellinger 			" MUST be set if Expected Data Transfer Length is not 0."
1051e48354ceSNicholas Bellinger 			" Bad iSCSI Initiator\n");
1052ba159914SNicholas Bellinger 		return iscsit_add_reject_cmd(cmd,
1053ba159914SNicholas Bellinger 					     ISCSI_REASON_BOOKMARK_INVALID, buf);
1054e48354ceSNicholas Bellinger 	}
1055e48354ceSNicholas Bellinger 
1056e48354ceSNicholas Bellinger 	if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
1057e48354ceSNicholas Bellinger 	    (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
1058e48354ceSNicholas Bellinger 		pr_err("Bidirectional operations not supported!\n");
1059ba159914SNicholas Bellinger 		return iscsit_add_reject_cmd(cmd,
1060ba159914SNicholas Bellinger 					     ISCSI_REASON_BOOKMARK_INVALID, buf);
1061e48354ceSNicholas Bellinger 	}
1062e48354ceSNicholas Bellinger 
1063e48354ceSNicholas Bellinger 	if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1064e48354ceSNicholas Bellinger 		pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1065e48354ceSNicholas Bellinger 				" Scsi Command PDU.\n");
1066ba159914SNicholas Bellinger 		return iscsit_add_reject_cmd(cmd,
1067ba159914SNicholas Bellinger 					     ISCSI_REASON_BOOKMARK_INVALID, buf);
1068e48354ceSNicholas Bellinger 	}
1069e48354ceSNicholas Bellinger 
1070e48354ceSNicholas Bellinger 	if (payload_length && !conn->sess->sess_ops->ImmediateData) {
1071e48354ceSNicholas Bellinger 		pr_err("ImmediateData=No but DataSegmentLength=%u,"
1072e48354ceSNicholas Bellinger 			" protocol error.\n", payload_length);
1073ba159914SNicholas Bellinger 		return iscsit_add_reject_cmd(cmd,
1074ba159914SNicholas Bellinger 					     ISCSI_REASON_PROTOCOL_ERROR, buf);
1075e48354ceSNicholas Bellinger 	}
1076e48354ceSNicholas Bellinger 
107750e5c87dSChristoph Hellwig 	if ((be32_to_cpu(hdr->data_length) == payload_length) &&
1078e48354ceSNicholas Bellinger 	    (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
1079e48354ceSNicholas Bellinger 		pr_err("Expected Data Transfer Length and Length of"
1080e48354ceSNicholas Bellinger 			" Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1081e48354ceSNicholas Bellinger 			" bit is not set protocol error\n");
1082ba159914SNicholas Bellinger 		return iscsit_add_reject_cmd(cmd,
1083ba159914SNicholas Bellinger 					     ISCSI_REASON_PROTOCOL_ERROR, buf);
1084e48354ceSNicholas Bellinger 	}
1085e48354ceSNicholas Bellinger 
108650e5c87dSChristoph Hellwig 	if (payload_length > be32_to_cpu(hdr->data_length)) {
1087e48354ceSNicholas Bellinger 		pr_err("DataSegmentLength: %u is greater than"
1088e48354ceSNicholas Bellinger 			" EDTL: %u, protocol error.\n", payload_length,
1089e48354ceSNicholas Bellinger 				hdr->data_length);
1090ba159914SNicholas Bellinger 		return iscsit_add_reject_cmd(cmd,
1091ba159914SNicholas Bellinger 					     ISCSI_REASON_PROTOCOL_ERROR, buf);
1092e48354ceSNicholas Bellinger 	}
1093e48354ceSNicholas Bellinger 
109421f5aa7eSNicholas Bellinger 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1095e48354ceSNicholas Bellinger 		pr_err("DataSegmentLength: %u is greater than"
109621f5aa7eSNicholas Bellinger 			" MaxXmitDataSegmentLength: %u, protocol error.\n",
109721f5aa7eSNicholas Bellinger 			payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
1098ba159914SNicholas Bellinger 		return iscsit_add_reject_cmd(cmd,
1099ba159914SNicholas Bellinger 					     ISCSI_REASON_PROTOCOL_ERROR, buf);
1100e48354ceSNicholas Bellinger 	}
1101e48354ceSNicholas Bellinger 
1102e48354ceSNicholas Bellinger 	if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
1103e48354ceSNicholas Bellinger 		pr_err("DataSegmentLength: %u is greater than"
1104e48354ceSNicholas Bellinger 			" FirstBurstLength: %u, protocol error.\n",
1105e48354ceSNicholas Bellinger 			payload_length, conn->sess->sess_ops->FirstBurstLength);
1106ba159914SNicholas Bellinger 		return iscsit_add_reject_cmd(cmd,
1107ba159914SNicholas Bellinger 					     ISCSI_REASON_BOOKMARK_INVALID, buf);
1108e48354ceSNicholas Bellinger 	}
1109e48354ceSNicholas Bellinger 
11108f1f7d29SDmitry Bogdanov 	cdb = hdr->cdb;
11118f1f7d29SDmitry Bogdanov 
11128f1f7d29SDmitry Bogdanov 	if (hdr->hlength) {
11138f1f7d29SDmitry Bogdanov 		ecdb_ahdr = (struct iscsi_ecdb_ahdr *) (hdr + 1);
11148f1f7d29SDmitry Bogdanov 		if (ecdb_ahdr->ahstype != ISCSI_AHSTYPE_CDB) {
11158f1f7d29SDmitry Bogdanov 			pr_err("Additional Header Segment type %d not supported!\n",
11168f1f7d29SDmitry Bogdanov 			       ecdb_ahdr->ahstype);
11178f1f7d29SDmitry Bogdanov 			return iscsit_add_reject_cmd(cmd,
11188f1f7d29SDmitry Bogdanov 				ISCSI_REASON_CMD_NOT_SUPPORTED, buf);
11198f1f7d29SDmitry Bogdanov 		}
11208f1f7d29SDmitry Bogdanov 
11218f1f7d29SDmitry Bogdanov 		cdb = kmalloc(be16_to_cpu(ecdb_ahdr->ahslength) + 15,
11228f1f7d29SDmitry Bogdanov 			      GFP_KERNEL);
11238f1f7d29SDmitry Bogdanov 		if (cdb == NULL)
11248f1f7d29SDmitry Bogdanov 			return iscsit_add_reject_cmd(cmd,
11258f1f7d29SDmitry Bogdanov 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
11268f1f7d29SDmitry Bogdanov 		memcpy(cdb, hdr->cdb, ISCSI_CDB_SIZE);
11278f1f7d29SDmitry Bogdanov 		memcpy(cdb + ISCSI_CDB_SIZE, ecdb_ahdr->ecdb,
11288f1f7d29SDmitry Bogdanov 		       be16_to_cpu(ecdb_ahdr->ahslength) - 1);
11298f1f7d29SDmitry Bogdanov 	}
11308f1f7d29SDmitry Bogdanov 
1131e48354ceSNicholas Bellinger 	data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
1132e48354ceSNicholas Bellinger 			 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
1133e48354ceSNicholas Bellinger 			  DMA_NONE;
1134e48354ceSNicholas Bellinger 
1135d28b1169SAndy Grover 	cmd->data_direction = data_direction;
1136d28b1169SAndy Grover 	iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
1137d28b1169SAndy Grover 	/*
1138d28b1169SAndy Grover 	 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1139d28b1169SAndy Grover 	 */
1140d28b1169SAndy Grover 	if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
1141d28b1169SAndy Grover 	    (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
114268d81f40SChristoph Hellwig 		sam_task_attr = TCM_SIMPLE_TAG;
1143d28b1169SAndy Grover 	else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
114468d81f40SChristoph Hellwig 		sam_task_attr = TCM_ORDERED_TAG;
1145d28b1169SAndy Grover 	else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
114668d81f40SChristoph Hellwig 		sam_task_attr = TCM_HEAD_TAG;
1147d28b1169SAndy Grover 	else if (iscsi_task_attr == ISCSI_ATTR_ACA)
114868d81f40SChristoph Hellwig 		sam_task_attr = TCM_ACA_TAG;
1149d28b1169SAndy Grover 	else {
1150d28b1169SAndy Grover 		pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
115168d81f40SChristoph Hellwig 			" TCM_SIMPLE_TAG\n", iscsi_task_attr);
115268d81f40SChristoph Hellwig 		sam_task_attr = TCM_SIMPLE_TAG;
1153d28b1169SAndy Grover 	}
1154d28b1169SAndy Grover 
1155e48354ceSNicholas Bellinger 	cmd->iscsi_opcode	= ISCSI_OP_SCSI_CMD;
1156e48354ceSNicholas Bellinger 	cmd->i_state		= ISTATE_NEW_CMD;
1157e48354ceSNicholas Bellinger 	cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1158e48354ceSNicholas Bellinger 	cmd->immediate_data	= (payload_length) ? 1 : 0;
1159e48354ceSNicholas Bellinger 	cmd->unsolicited_data	= ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
1160e48354ceSNicholas Bellinger 				     (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
1161e48354ceSNicholas Bellinger 	if (cmd->unsolicited_data)
1162e48354ceSNicholas Bellinger 		cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
1163e48354ceSNicholas Bellinger 
1164e48354ceSNicholas Bellinger 	conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
11659547308bSAlexei Potashnik 	if (hdr->flags & ISCSI_FLAG_CMD_READ)
1166c1e34b64SSagi Grimberg 		cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
11679547308bSAlexei Potashnik 	else
1168e48354ceSNicholas Bellinger 		cmd->targ_xfer_tag = 0xFFFFFFFF;
116950e5c87dSChristoph Hellwig 	cmd->cmd_sn		= be32_to_cpu(hdr->cmdsn);
117050e5c87dSChristoph Hellwig 	cmd->exp_stat_sn	= be32_to_cpu(hdr->exp_statsn);
1171e48354ceSNicholas Bellinger 	cmd->first_burst_len	= payload_length;
1172e48354ceSNicholas Bellinger 
11733e1c81a9SNicholas Bellinger 	if (!conn->sess->sess_ops->RDMAExtensions &&
11743e1c81a9SNicholas Bellinger 	     cmd->data_direction == DMA_FROM_DEVICE) {
1175e48354ceSNicholas Bellinger 		struct iscsi_datain_req *dr;
1176e48354ceSNicholas Bellinger 
1177e48354ceSNicholas Bellinger 		dr = iscsit_allocate_datain_req();
11788f1f7d29SDmitry Bogdanov 		if (!dr) {
11798f1f7d29SDmitry Bogdanov 			if (cdb != hdr->cdb)
11808f1f7d29SDmitry Bogdanov 				kfree(cdb);
1181ba159914SNicholas Bellinger 			return iscsit_add_reject_cmd(cmd,
1182ba159914SNicholas Bellinger 					ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
11838f1f7d29SDmitry Bogdanov 		}
1184e48354ceSNicholas Bellinger 
1185e48354ceSNicholas Bellinger 		iscsit_attach_datain_req(cmd, dr);
1186e48354ceSNicholas Bellinger 	}
1187e48354ceSNicholas Bellinger 
1188e48354ceSNicholas Bellinger 	/*
1189065ca1e4SAndy Grover 	 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1190065ca1e4SAndy Grover 	 */
1191a78b7136SMike Christie 	__target_init_cmd(&cmd->se_cmd, &iscsi_ops,
119250e5c87dSChristoph Hellwig 			  conn->sess->se_sess, be32_to_cpu(hdr->data_length),
119350e5c87dSChristoph Hellwig 			  cmd->data_direction, sam_task_attr,
11948e288be8SMike Christie 			  cmd->sense_buffer + 2, scsilun_to_int(&hdr->lun),
11956d256beeSMike Christie 			  conn->cmd_cnt);
1196065ca1e4SAndy Grover 
1197065ca1e4SAndy Grover 	pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1198065ca1e4SAndy Grover 		" ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
11993e1c81a9SNicholas Bellinger 		hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
12003e1c81a9SNicholas Bellinger 		conn->cid);
12013e1c81a9SNicholas Bellinger 
1202807b9515SBart Van Assche 	target_get_sess_cmd(&cmd->se_cmd, true);
1203065ca1e4SAndy Grover 
12040352c3d3SRoman Bolshakov 	cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
12058f1f7d29SDmitry Bogdanov 	cmd->sense_reason = target_cmd_init_cdb(&cmd->se_cmd, cdb,
120608694199SMike Christie 						GFP_KERNEL);
120784b20b80SMartin K. Petersen 
12088f1f7d29SDmitry Bogdanov 	if (cdb != hdr->cdb)
12098f1f7d29SDmitry Bogdanov 		kfree(cdb);
12108f1f7d29SDmitry Bogdanov 
1211de103c93SChristoph Hellwig 	if (cmd->sense_reason) {
1212de103c93SChristoph Hellwig 		if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
1213ba159914SNicholas Bellinger 			return iscsit_add_reject_cmd(cmd,
1214ba159914SNicholas Bellinger 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1215de103c93SChristoph Hellwig 		}
1216de103c93SChristoph Hellwig 
1217de103c93SChristoph Hellwig 		goto attach_cmd;
1218de103c93SChristoph Hellwig 	}
1219de103c93SChristoph Hellwig 
12209e95fb80SSudhakar Panneerselvam 	cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd);
12219e95fb80SSudhakar Panneerselvam 	if (cmd->sense_reason)
12229e95fb80SSudhakar Panneerselvam 		goto attach_cmd;
12239e95fb80SSudhakar Panneerselvam 
1224987db587SSudhakar Panneerselvam 	cmd->sense_reason = target_cmd_parse_cdb(&cmd->se_cmd);
12259e95fb80SSudhakar Panneerselvam 	if (cmd->sense_reason)
12269e95fb80SSudhakar Panneerselvam 		goto attach_cmd;
12279e95fb80SSudhakar Panneerselvam 
1228de103c93SChristoph Hellwig 	if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
1229ba159914SNicholas Bellinger 		return iscsit_add_reject_cmd(cmd,
1230ba159914SNicholas Bellinger 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1231e48354ceSNicholas Bellinger 	}
1232e48354ceSNicholas Bellinger 
1233e48354ceSNicholas Bellinger attach_cmd:
1234e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
12352fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1236e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
1237e48354ceSNicholas Bellinger 	/*
1238e48354ceSNicholas Bellinger 	 * Check if we need to delay processing because of ALUA
1239e48354ceSNicholas Bellinger 	 * Active/NonOptimized primary access state..
1240e48354ceSNicholas Bellinger 	 */
1241e48354ceSNicholas Bellinger 	core_alua_check_nonop_delay(&cmd->se_cmd);
1242bfb79eacSAndy Grover 
12433e1c81a9SNicholas Bellinger 	return 0;
1244de103c93SChristoph Hellwig }
12453e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
1246de103c93SChristoph Hellwig 
iscsit_set_unsolicited_dataout(struct iscsit_cmd * cmd)124766cd9d4eSMax Gurtovoy void iscsit_set_unsolicited_dataout(struct iscsit_cmd *cmd)
12483e1c81a9SNicholas Bellinger {
12493e1c81a9SNicholas Bellinger 	iscsit_set_dataout_sequence_values(cmd);
12503e1c81a9SNicholas Bellinger 
12513e1c81a9SNicholas Bellinger 	spin_lock_bh(&cmd->dataout_timeout_lock);
12523e1c81a9SNicholas Bellinger 	iscsit_start_dataout_timer(cmd, cmd->conn);
12533e1c81a9SNicholas Bellinger 	spin_unlock_bh(&cmd->dataout_timeout_lock);
12543e1c81a9SNicholas Bellinger }
12550300b114SBart Van Assche EXPORT_SYMBOL(iscsit_set_unsolicited_dataout);
12563e1c81a9SNicholas Bellinger 
iscsit_process_scsi_cmd(struct iscsit_conn * conn,struct iscsit_cmd * cmd,struct iscsi_scsi_req * hdr)1257be36d683SMax Gurtovoy int iscsit_process_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
12583e1c81a9SNicholas Bellinger 			    struct iscsi_scsi_req *hdr)
12593e1c81a9SNicholas Bellinger {
12603e1c81a9SNicholas Bellinger 	int cmdsn_ret = 0;
1261e48354ceSNicholas Bellinger 	/*
1262e48354ceSNicholas Bellinger 	 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1263e48354ceSNicholas Bellinger 	 * the Immediate Bit is not set, and no Immediate
1264e48354ceSNicholas Bellinger 	 * Data is attached.
1265e48354ceSNicholas Bellinger 	 *
1266e48354ceSNicholas Bellinger 	 * A PDU/CmdSN carrying Immediate Data can only
1267e48354ceSNicholas Bellinger 	 * be processed after the DataCRC has passed.
1268e48354ceSNicholas Bellinger 	 * If the DataCRC fails, the CmdSN MUST NOT
1269e48354ceSNicholas Bellinger 	 * be acknowledged. (See below)
1270e48354ceSNicholas Bellinger 	 */
1271e48354ceSNicholas Bellinger 	if (!cmd->immediate_data) {
1272561bf158SNicholas Bellinger 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1273561bf158SNicholas Bellinger 					(unsigned char *)hdr, hdr->cmdsn);
1274561bf158SNicholas Bellinger 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1275561bf158SNicholas Bellinger 			return -1;
1276561bf158SNicholas Bellinger 		else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1277afc16604SBart Van Assche 			target_put_sess_cmd(&cmd->se_cmd);
12783e1c81a9SNicholas Bellinger 			return 0;
12793e1c81a9SNicholas Bellinger 		}
1280e48354ceSNicholas Bellinger 	}
1281e48354ceSNicholas Bellinger 
128250e5c87dSChristoph Hellwig 	iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1283e48354ceSNicholas Bellinger 
1284e48354ceSNicholas Bellinger 	/*
1285e48354ceSNicholas Bellinger 	 * If no Immediate Data is attached, it's OK to return now.
1286e48354ceSNicholas Bellinger 	 */
1287e48354ceSNicholas Bellinger 	if (!cmd->immediate_data) {
12883e1c81a9SNicholas Bellinger 		if (!cmd->sense_reason && cmd->unsolicited_data)
12890300b114SBart Van Assche 			iscsit_set_unsolicited_dataout(cmd);
12903e1c81a9SNicholas Bellinger 		if (!cmd->sense_reason)
12913e1c81a9SNicholas Bellinger 			return 0;
1292e48354ceSNicholas Bellinger 
1293afc16604SBart Van Assche 		target_put_sess_cmd(&cmd->se_cmd);
1294e48354ceSNicholas Bellinger 		return 0;
1295e48354ceSNicholas Bellinger 	}
1296e48354ceSNicholas Bellinger 
1297e48354ceSNicholas Bellinger 	/*
12983e1c81a9SNicholas Bellinger 	 * Early CHECK_CONDITIONs with ImmediateData never make it to command
12993e1c81a9SNicholas Bellinger 	 * execution.  These exceptions are processed in CmdSN order using
13003e1c81a9SNicholas Bellinger 	 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1301e48354ceSNicholas Bellinger 	 */
13028fa4011eSBart Van Assche 	if (cmd->sense_reason)
13033e1c81a9SNicholas Bellinger 		return 1;
1304e48354ceSNicholas Bellinger 	/*
1305e48354ceSNicholas Bellinger 	 * Call directly into transport_generic_new_cmd() to perform
1306e48354ceSNicholas Bellinger 	 * the backend memory allocation.
1307e48354ceSNicholas Bellinger 	 */
1308de103c93SChristoph Hellwig 	cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
1309561bf158SNicholas Bellinger 	if (cmd->sense_reason)
13103e1c81a9SNicholas Bellinger 		return 1;
1311e48354ceSNicholas Bellinger 
13123e1c81a9SNicholas Bellinger 	return 0;
13133e1c81a9SNicholas Bellinger }
13143e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_process_scsi_cmd);
13153e1c81a9SNicholas Bellinger 
13163e1c81a9SNicholas Bellinger static int
iscsit_get_immediate_data(struct iscsit_cmd * cmd,struct iscsi_scsi_req * hdr,bool dump_payload)131766cd9d4eSMax Gurtovoy iscsit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
13183e1c81a9SNicholas Bellinger 			  bool dump_payload)
13193e1c81a9SNicholas Bellinger {
13203e1c81a9SNicholas Bellinger 	int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
13214b3766ecSBart Van Assche 	int rc;
13224b3766ecSBart Van Assche 
13233e1c81a9SNicholas Bellinger 	/*
13243e1c81a9SNicholas Bellinger 	 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
13253e1c81a9SNicholas Bellinger 	 */
13264b3766ecSBart Van Assche 	if (dump_payload) {
13274b3766ecSBart Van Assche 		u32 length = min(cmd->se_cmd.data_length - cmd->write_data_done,
13284b3766ecSBart Van Assche 				 cmd->first_burst_len);
13293e1c81a9SNicholas Bellinger 
13304b3766ecSBart Van Assche 		pr_debug("Dumping min(%d - %d, %d) = %d bytes of immediate data\n",
13314b3766ecSBart Van Assche 			 cmd->se_cmd.data_length, cmd->write_data_done,
13324b3766ecSBart Van Assche 			 cmd->first_burst_len, length);
13334b3766ecSBart Van Assche 		rc = iscsit_dump_data_payload(cmd->conn, length, 1);
13344b3766ecSBart Van Assche 		pr_debug("Finished dumping immediate data\n");
13354b3766ecSBart Van Assche 		if (rc < 0)
13364b3766ecSBart Van Assche 			immed_ret = IMMEDIATE_DATA_CANNOT_RECOVER;
13374b3766ecSBart Van Assche 	} else {
13383e1c81a9SNicholas Bellinger 		immed_ret = iscsit_handle_immediate_data(cmd, hdr,
13393e1c81a9SNicholas Bellinger 							 cmd->first_burst_len);
13404b3766ecSBart Van Assche 	}
13414b3766ecSBart Van Assche 
1342e48354ceSNicholas Bellinger 	if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1343e48354ceSNicholas Bellinger 		/*
1344e48354ceSNicholas Bellinger 		 * A PDU/CmdSN carrying Immediate Data passed
1345e48354ceSNicholas Bellinger 		 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1346e48354ceSNicholas Bellinger 		 * Immediate Bit is not set.
1347e48354ceSNicholas Bellinger 		 */
1348561bf158SNicholas Bellinger 		cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1349561bf158SNicholas Bellinger 					(unsigned char *)hdr, hdr->cmdsn);
13509d86a2beSNicholas Bellinger 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1351561bf158SNicholas Bellinger 			return -1;
1352e48354ceSNicholas Bellinger 
13539d86a2beSNicholas Bellinger 		if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1354afc16604SBart Van Assche 			target_put_sess_cmd(&cmd->se_cmd);
13554b3766ecSBart Van Assche 
13564b3766ecSBart Van Assche 			return 0;
13573e1c81a9SNicholas Bellinger 		} else if (cmd->unsolicited_data)
13580300b114SBart Van Assche 			iscsit_set_unsolicited_dataout(cmd);
1359e48354ceSNicholas Bellinger 
1360e48354ceSNicholas Bellinger 	} else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1361e48354ceSNicholas Bellinger 		/*
1362e48354ceSNicholas Bellinger 		 * Immediate Data failed DataCRC and ERL>=1,
1363e48354ceSNicholas Bellinger 		 * silently drop this PDU and let the initiator
1364e48354ceSNicholas Bellinger 		 * plug the CmdSN gap.
1365e48354ceSNicholas Bellinger 		 *
1366e48354ceSNicholas Bellinger 		 * FIXME: Send Unsolicited NOPIN with reserved
1367e48354ceSNicholas Bellinger 		 * TTT here to help the initiator figure out
1368e48354ceSNicholas Bellinger 		 * the missing CmdSN, although they should be
1369e48354ceSNicholas Bellinger 		 * intelligent enough to determine the missing
1370e48354ceSNicholas Bellinger 		 * CmdSN and issue a retry to plug the sequence.
1371e48354ceSNicholas Bellinger 		 */
1372e48354ceSNicholas Bellinger 		cmd->i_state = ISTATE_REMOVE;
13733e1c81a9SNicholas Bellinger 		iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
1374e48354ceSNicholas Bellinger 	} else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1375e48354ceSNicholas Bellinger 		return -1;
1376e48354ceSNicholas Bellinger 
1377e48354ceSNicholas Bellinger 	return 0;
1378e48354ceSNicholas Bellinger }
1379e48354ceSNicholas Bellinger 
13803e1c81a9SNicholas Bellinger static int
iscsit_handle_scsi_cmd(struct iscsit_conn * conn,struct iscsit_cmd * cmd,unsigned char * buf)1381be36d683SMax Gurtovoy iscsit_handle_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
13823e1c81a9SNicholas Bellinger 			   unsigned char *buf)
13833e1c81a9SNicholas Bellinger {
13843e1c81a9SNicholas Bellinger 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
13853e1c81a9SNicholas Bellinger 	int rc, immed_data;
13863e1c81a9SNicholas Bellinger 	bool dump_payload = false;
13873e1c81a9SNicholas Bellinger 
13883e1c81a9SNicholas Bellinger 	rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
13893e1c81a9SNicholas Bellinger 	if (rc < 0)
1390561bf158SNicholas Bellinger 		return 0;
13913e1c81a9SNicholas Bellinger 	/*
13923e1c81a9SNicholas Bellinger 	 * Allocation iovecs needed for struct socket operations for
13933e1c81a9SNicholas Bellinger 	 * traditional iSCSI block I/O.
13943e1c81a9SNicholas Bellinger 	 */
13953e1c81a9SNicholas Bellinger 	if (iscsit_allocate_iovecs(cmd) < 0) {
1396b815fc12SMike Christie 		return iscsit_reject_cmd(cmd,
1397ba159914SNicholas Bellinger 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
13983e1c81a9SNicholas Bellinger 	}
13993e1c81a9SNicholas Bellinger 	immed_data = cmd->immediate_data;
14003e1c81a9SNicholas Bellinger 
14013e1c81a9SNicholas Bellinger 	rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
14023e1c81a9SNicholas Bellinger 	if (rc < 0)
14033e1c81a9SNicholas Bellinger 		return rc;
14043e1c81a9SNicholas Bellinger 	else if (rc > 0)
14053e1c81a9SNicholas Bellinger 		dump_payload = true;
14063e1c81a9SNicholas Bellinger 
14073e1c81a9SNicholas Bellinger 	if (!immed_data)
14083e1c81a9SNicholas Bellinger 		return 0;
14093e1c81a9SNicholas Bellinger 
14103e1c81a9SNicholas Bellinger 	return iscsit_get_immediate_data(cmd, hdr, dump_payload);
14113e1c81a9SNicholas Bellinger }
14123e1c81a9SNicholas Bellinger 
iscsit_do_crypto_hash_sg(struct ahash_request * hash,struct iscsit_cmd * cmd,u32 data_offset,u32 data_length,u32 padding,u8 * pad_bytes)1413e48354ceSNicholas Bellinger static u32 iscsit_do_crypto_hash_sg(
141469110e3cSHerbert Xu 	struct ahash_request *hash,
141566cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
1416e48354ceSNicholas Bellinger 	u32 data_offset,
1417e48354ceSNicholas Bellinger 	u32 data_length,
1418e48354ceSNicholas Bellinger 	u32 padding,
1419e48354ceSNicholas Bellinger 	u8 *pad_bytes)
1420e48354ceSNicholas Bellinger {
1421e48354ceSNicholas Bellinger 	u32 data_crc;
1422e48354ceSNicholas Bellinger 	struct scatterlist *sg;
1423e48354ceSNicholas Bellinger 	unsigned int page_off;
1424e48354ceSNicholas Bellinger 
142569110e3cSHerbert Xu 	crypto_ahash_init(hash);
1426e48354ceSNicholas Bellinger 
1427e48354ceSNicholas Bellinger 	sg = cmd->first_data_sg;
1428e48354ceSNicholas Bellinger 	page_off = cmd->first_data_sg_off;
1429e48354ceSNicholas Bellinger 
14305528d031SVarun Prakash 	if (data_length && page_off) {
14315528d031SVarun Prakash 		struct scatterlist first_sg;
14325528d031SVarun Prakash 		u32 len = min_t(u32, data_length, sg->length - page_off);
14335528d031SVarun Prakash 
14345528d031SVarun Prakash 		sg_init_table(&first_sg, 1);
14355528d031SVarun Prakash 		sg_set_page(&first_sg, sg_page(sg), len, sg->offset + page_off);
14365528d031SVarun Prakash 
14375528d031SVarun Prakash 		ahash_request_set_crypt(hash, &first_sg, NULL, len);
14385528d031SVarun Prakash 		crypto_ahash_update(hash);
14395528d031SVarun Prakash 
14405528d031SVarun Prakash 		data_length -= len;
14415528d031SVarun Prakash 		sg = sg_next(sg);
14425528d031SVarun Prakash 	}
14435528d031SVarun Prakash 
1444e48354ceSNicholas Bellinger 	while (data_length) {
14455528d031SVarun Prakash 		u32 cur_len = min_t(u32, data_length, sg->length);
1446e48354ceSNicholas Bellinger 
144769110e3cSHerbert Xu 		ahash_request_set_crypt(hash, sg, NULL, cur_len);
144869110e3cSHerbert Xu 		crypto_ahash_update(hash);
1449e48354ceSNicholas Bellinger 
1450e48354ceSNicholas Bellinger 		data_length -= cur_len;
1451aa75679cSAlexei Potashnik 		/* iscsit_map_iovec has already checked for invalid sg pointers */
1452aa75679cSAlexei Potashnik 		sg = sg_next(sg);
1453e48354ceSNicholas Bellinger 	}
1454e48354ceSNicholas Bellinger 
1455e48354ceSNicholas Bellinger 	if (padding) {
1456e48354ceSNicholas Bellinger 		struct scatterlist pad_sg;
1457e48354ceSNicholas Bellinger 
1458e48354ceSNicholas Bellinger 		sg_init_one(&pad_sg, pad_bytes, padding);
145969110e3cSHerbert Xu 		ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
146069110e3cSHerbert Xu 					padding);
146169110e3cSHerbert Xu 		crypto_ahash_finup(hash);
146269110e3cSHerbert Xu 	} else {
146369110e3cSHerbert Xu 		ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
146469110e3cSHerbert Xu 		crypto_ahash_final(hash);
1465e48354ceSNicholas Bellinger 	}
1466e48354ceSNicholas Bellinger 
1467e48354ceSNicholas Bellinger 	return data_crc;
1468e48354ceSNicholas Bellinger }
1469e48354ceSNicholas Bellinger 
iscsit_do_crypto_hash_buf(struct ahash_request * hash,const void * buf,u32 payload_length,u32 padding,const void * pad_bytes,void * data_crc)1470e1dfb21fSBart Van Assche static void iscsit_do_crypto_hash_buf(struct ahash_request *hash,
1471e1dfb21fSBart Van Assche 	const void *buf, u32 payload_length, u32 padding,
1472e1dfb21fSBart Van Assche 	const void *pad_bytes, void *data_crc)
1473e48354ceSNicholas Bellinger {
147469110e3cSHerbert Xu 	struct scatterlist sg[2];
1475e48354ceSNicholas Bellinger 
147669110e3cSHerbert Xu 	sg_init_table(sg, ARRAY_SIZE(sg));
147769110e3cSHerbert Xu 	sg_set_buf(sg, buf, payload_length);
1478679fcae4SLaura Abbott 	if (padding)
147969110e3cSHerbert Xu 		sg_set_buf(sg + 1, pad_bytes, padding);
1480e48354ceSNicholas Bellinger 
148169110e3cSHerbert Xu 	ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
1482e48354ceSNicholas Bellinger 
148369110e3cSHerbert Xu 	crypto_ahash_digest(hash);
1484e48354ceSNicholas Bellinger }
1485e48354ceSNicholas Bellinger 
14863e1c81a9SNicholas Bellinger int
__iscsit_check_dataout_hdr(struct iscsit_conn * conn,void * buf,struct iscsit_cmd * cmd,u32 payload_length,bool * success)1487be36d683SMax Gurtovoy __iscsit_check_dataout_hdr(struct iscsit_conn *conn, void *buf,
148866cd9d4eSMax Gurtovoy 			   struct iscsit_cmd *cmd, u32 payload_length,
14899a584bf9SVarun Prakash 			   bool *success)
1490e48354ceSNicholas Bellinger {
14919a584bf9SVarun Prakash 	struct iscsi_data *hdr = buf;
1492e48354ceSNicholas Bellinger 	struct se_cmd *se_cmd;
14933e1c81a9SNicholas Bellinger 	int rc;
1494e48354ceSNicholas Bellinger 
1495e48354ceSNicholas Bellinger 	/* iSCSI write */
149604f3b31bSNicholas Bellinger 	atomic_long_add(payload_length, &conn->sess->rx_data_octets);
1497e48354ceSNicholas Bellinger 
1498e48354ceSNicholas Bellinger 	pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1499e48354ceSNicholas Bellinger 		" DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
15003e1c81a9SNicholas Bellinger 		hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
1501e48354ceSNicholas Bellinger 		payload_length, conn->cid);
1502e48354ceSNicholas Bellinger 
1503e48354ceSNicholas Bellinger 	if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1504e48354ceSNicholas Bellinger 		pr_err("Command ITT: 0x%08x received DataOUT after"
1505e48354ceSNicholas Bellinger 			" last DataOUT received, dumping payload\n",
1506e48354ceSNicholas Bellinger 			cmd->init_task_tag);
1507e48354ceSNicholas Bellinger 		return iscsit_dump_data_payload(conn, payload_length, 1);
1508e48354ceSNicholas Bellinger 	}
1509e48354ceSNicholas Bellinger 
1510e48354ceSNicholas Bellinger 	if (cmd->data_direction != DMA_TO_DEVICE) {
1511e48354ceSNicholas Bellinger 		pr_err("Command ITT: 0x%08x received DataOUT for a"
1512e48354ceSNicholas Bellinger 			" NON-WRITE command.\n", cmd->init_task_tag);
151397c99b47SNicholas Bellinger 		return iscsit_dump_data_payload(conn, payload_length, 1);
1514e48354ceSNicholas Bellinger 	}
1515e48354ceSNicholas Bellinger 	se_cmd = &cmd->se_cmd;
1516e48354ceSNicholas Bellinger 	iscsit_mod_dataout_timer(cmd);
1517e48354ceSNicholas Bellinger 
151850e5c87dSChristoph Hellwig 	if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
1519de3493aeSBart Van Assche 		pr_err("DataOut Offset: %u, Length %u greater than iSCSI Command EDTL %u, protocol error.\n",
1520de3493aeSBart Van Assche 		       be32_to_cpu(hdr->offset), payload_length,
1521de3493aeSBart Van Assche 		       cmd->se_cmd.data_length);
1522ba159914SNicholas Bellinger 		return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
1523e48354ceSNicholas Bellinger 	}
1524e48354ceSNicholas Bellinger 
1525e48354ceSNicholas Bellinger 	if (cmd->unsolicited_data) {
1526e48354ceSNicholas Bellinger 		int dump_unsolicited_data = 0;
1527e48354ceSNicholas Bellinger 
1528e48354ceSNicholas Bellinger 		if (conn->sess->sess_ops->InitialR2T) {
1529e48354ceSNicholas Bellinger 			pr_err("Received unexpected unsolicited data"
1530e48354ceSNicholas Bellinger 				" while InitialR2T=Yes, protocol error.\n");
1531e48354ceSNicholas Bellinger 			transport_send_check_condition_and_sense(&cmd->se_cmd,
1532e48354ceSNicholas Bellinger 					TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1533e48354ceSNicholas Bellinger 			return -1;
1534e48354ceSNicholas Bellinger 		}
1535e48354ceSNicholas Bellinger 		/*
1536e48354ceSNicholas Bellinger 		 * Special case for dealing with Unsolicited DataOUT
1537e48354ceSNicholas Bellinger 		 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1538e48354ceSNicholas Bellinger 		 * failures;
1539e48354ceSNicholas Bellinger 		 */
1540e48354ceSNicholas Bellinger 
1541e48354ceSNicholas Bellinger 		/* Something's amiss if we're not in WRITE_PENDING state... */
1542e48354ceSNicholas Bellinger 		WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1543de103c93SChristoph Hellwig 		if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
1544e48354ceSNicholas Bellinger 			dump_unsolicited_data = 1;
1545e48354ceSNicholas Bellinger 
1546e48354ceSNicholas Bellinger 		if (dump_unsolicited_data) {
1547e48354ceSNicholas Bellinger 			/*
1548e48354ceSNicholas Bellinger 			 * Check if a delayed TASK_ABORTED status needs to
1549e48354ceSNicholas Bellinger 			 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
15505a342521SBart Van Assche 			 * received with the unsolicited data out.
1551e48354ceSNicholas Bellinger 			 */
1552e48354ceSNicholas Bellinger 			if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1553e48354ceSNicholas Bellinger 				iscsit_stop_dataout_timer(cmd);
1554e48354ceSNicholas Bellinger 
1555e48354ceSNicholas Bellinger 			return iscsit_dump_data_payload(conn, payload_length, 1);
1556e48354ceSNicholas Bellinger 		}
1557e48354ceSNicholas Bellinger 	} else {
1558e48354ceSNicholas Bellinger 		/*
1559e48354ceSNicholas Bellinger 		 * For the normal solicited data path:
1560e48354ceSNicholas Bellinger 		 *
1561e48354ceSNicholas Bellinger 		 * Check for a delayed TASK_ABORTED status and dump any
1562e48354ceSNicholas Bellinger 		 * incoming data out payload if one exists.  Also, when the
1563e48354ceSNicholas Bellinger 		 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1564e48354ceSNicholas Bellinger 		 * data out sequence, we decrement outstanding_r2ts.  Once
1565e48354ceSNicholas Bellinger 		 * outstanding_r2ts reaches zero, go ahead and send the delayed
1566e48354ceSNicholas Bellinger 		 * TASK_ABORTED status.
1567e48354ceSNicholas Bellinger 		 */
15687d680f3bSChristoph Hellwig 		if (se_cmd->transport_state & CMD_T_ABORTED) {
1569aaa00cc9SBart Van Assche 			if (hdr->flags & ISCSI_FLAG_CMD_FINAL &&
1570aaa00cc9SBart Van Assche 			    --cmd->outstanding_r2ts < 1)
1571e48354ceSNicholas Bellinger 				iscsit_stop_dataout_timer(cmd);
1572e48354ceSNicholas Bellinger 
1573e48354ceSNicholas Bellinger 			return iscsit_dump_data_payload(conn, payload_length, 1);
1574e48354ceSNicholas Bellinger 		}
1575e48354ceSNicholas Bellinger 	}
1576e48354ceSNicholas Bellinger 	/*
15770d5efb8aSBart Van Assche 	 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1578e48354ceSNicholas Bellinger 	 * within-command recovery checks before receiving the payload.
1579e48354ceSNicholas Bellinger 	 */
15803e1c81a9SNicholas Bellinger 	rc = iscsit_check_pre_dataout(cmd, buf);
15813e1c81a9SNicholas Bellinger 	if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
1582e48354ceSNicholas Bellinger 		return 0;
15833e1c81a9SNicholas Bellinger 	else if (rc == DATAOUT_CANNOT_RECOVER)
1584e48354ceSNicholas Bellinger 		return -1;
15859a584bf9SVarun Prakash 	*success = true;
15863e1c81a9SNicholas Bellinger 	return 0;
15873e1c81a9SNicholas Bellinger }
15889a584bf9SVarun Prakash EXPORT_SYMBOL(__iscsit_check_dataout_hdr);
15899a584bf9SVarun Prakash 
15909a584bf9SVarun Prakash int
iscsit_check_dataout_hdr(struct iscsit_conn * conn,void * buf,struct iscsit_cmd ** out_cmd)1591be36d683SMax Gurtovoy iscsit_check_dataout_hdr(struct iscsit_conn *conn, void *buf,
159266cd9d4eSMax Gurtovoy 			 struct iscsit_cmd **out_cmd)
15939a584bf9SVarun Prakash {
15949a584bf9SVarun Prakash 	struct iscsi_data *hdr = buf;
159566cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
15969a584bf9SVarun Prakash 	u32 payload_length = ntoh24(hdr->dlength);
15979a584bf9SVarun Prakash 	int rc;
15989a584bf9SVarun Prakash 	bool success = false;
15999a584bf9SVarun Prakash 
16009a584bf9SVarun Prakash 	if (!payload_length) {
16019a584bf9SVarun Prakash 		pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
16029a584bf9SVarun Prakash 		return 0;
16039a584bf9SVarun Prakash 	}
16049a584bf9SVarun Prakash 
16059a584bf9SVarun Prakash 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
16069a584bf9SVarun Prakash 		pr_err_ratelimited("DataSegmentLength: %u is greater than"
16079a584bf9SVarun Prakash 			" MaxXmitDataSegmentLength: %u\n", payload_length,
16089a584bf9SVarun Prakash 			conn->conn_ops->MaxXmitDataSegmentLength);
16099a584bf9SVarun Prakash 		return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buf);
16109a584bf9SVarun Prakash 	}
16119a584bf9SVarun Prakash 
16129a584bf9SVarun Prakash 	cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, payload_length);
16139a584bf9SVarun Prakash 	if (!cmd)
16149a584bf9SVarun Prakash 		return 0;
16159a584bf9SVarun Prakash 
16169a584bf9SVarun Prakash 	rc = __iscsit_check_dataout_hdr(conn, buf, cmd, payload_length, &success);
16179a584bf9SVarun Prakash 
16189a584bf9SVarun Prakash 	if (success)
16199a584bf9SVarun Prakash 		*out_cmd = cmd;
16209a584bf9SVarun Prakash 
16219a584bf9SVarun Prakash 	return rc;
16229a584bf9SVarun Prakash }
16233e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_check_dataout_hdr);
16243e1c81a9SNicholas Bellinger 
16253e1c81a9SNicholas Bellinger static int
iscsit_get_dataout(struct iscsit_conn * conn,struct iscsit_cmd * cmd,struct iscsi_data * hdr)1626be36d683SMax Gurtovoy iscsit_get_dataout(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
16273e1c81a9SNicholas Bellinger 		   struct iscsi_data *hdr)
16283e1c81a9SNicholas Bellinger {
16293e1c81a9SNicholas Bellinger 	struct kvec *iov;
16303e1c81a9SNicholas Bellinger 	u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
16310ca650c1SBart Van Assche 	u32 payload_length;
16323e1c81a9SNicholas Bellinger 	int iov_ret, data_crc_failed = 0;
16333e1c81a9SNicholas Bellinger 
16340ca650c1SBart Van Assche 	payload_length = min_t(u32, cmd->se_cmd.data_length,
16350ca650c1SBart Van Assche 			       ntoh24(hdr->dlength));
1636e48354ceSNicholas Bellinger 	rx_size += payload_length;
1637e48354ceSNicholas Bellinger 	iov = &cmd->iov_data[0];
1638e48354ceSNicholas Bellinger 
16392e39f1c9SBart Van Assche 	iov_ret = iscsit_map_iovec(cmd, iov, cmd->orig_iov_data_count - 2,
16402e39f1c9SBart Van Assche 				   be32_to_cpu(hdr->offset), payload_length);
1641e48354ceSNicholas Bellinger 	if (iov_ret < 0)
1642e48354ceSNicholas Bellinger 		return -1;
1643e48354ceSNicholas Bellinger 
1644e48354ceSNicholas Bellinger 	iov_count += iov_ret;
1645e48354ceSNicholas Bellinger 
1646e48354ceSNicholas Bellinger 	padding = ((-payload_length) & 3);
1647e48354ceSNicholas Bellinger 	if (padding != 0) {
1648e48354ceSNicholas Bellinger 		iov[iov_count].iov_base	= cmd->pad_bytes;
1649e48354ceSNicholas Bellinger 		iov[iov_count++].iov_len = padding;
1650e48354ceSNicholas Bellinger 		rx_size += padding;
1651e48354ceSNicholas Bellinger 		pr_debug("Receiving %u padding bytes.\n", padding);
1652e48354ceSNicholas Bellinger 	}
1653e48354ceSNicholas Bellinger 
1654e48354ceSNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
1655e48354ceSNicholas Bellinger 		iov[iov_count].iov_base = &checksum;
1656e48354ceSNicholas Bellinger 		iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1657e48354ceSNicholas Bellinger 		rx_size += ISCSI_CRC_LEN;
1658e48354ceSNicholas Bellinger 	}
1659e48354ceSNicholas Bellinger 
16602e39f1c9SBart Van Assche 	WARN_ON_ONCE(iov_count > cmd->orig_iov_data_count);
1661e48354ceSNicholas Bellinger 	rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1662e48354ceSNicholas Bellinger 
1663e48354ceSNicholas Bellinger 	iscsit_unmap_iovec(cmd);
1664e48354ceSNicholas Bellinger 
1665e48354ceSNicholas Bellinger 	if (rx_got != rx_size)
1666e48354ceSNicholas Bellinger 		return -1;
1667e48354ceSNicholas Bellinger 
1668e48354ceSNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
1669e48354ceSNicholas Bellinger 		u32 data_crc;
1670e48354ceSNicholas Bellinger 
167169110e3cSHerbert Xu 		data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
167250e5c87dSChristoph Hellwig 						    be32_to_cpu(hdr->offset),
167350e5c87dSChristoph Hellwig 						    payload_length, padding,
1674e48354ceSNicholas Bellinger 						    cmd->pad_bytes);
1675e48354ceSNicholas Bellinger 
1676e48354ceSNicholas Bellinger 		if (checksum != data_crc) {
1677e48354ceSNicholas Bellinger 			pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1678e48354ceSNicholas Bellinger 				" DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1679e48354ceSNicholas Bellinger 				" does not match computed 0x%08x\n",
1680e48354ceSNicholas Bellinger 				hdr->itt, hdr->offset, payload_length,
1681e48354ceSNicholas Bellinger 				hdr->datasn, checksum, data_crc);
1682e48354ceSNicholas Bellinger 			data_crc_failed = 1;
1683e48354ceSNicholas Bellinger 		} else {
1684e48354ceSNicholas Bellinger 			pr_debug("Got CRC32C DataDigest 0x%08x for"
1685e48354ceSNicholas Bellinger 				" %u bytes of Data Out\n", checksum,
1686e48354ceSNicholas Bellinger 				payload_length);
1687e48354ceSNicholas Bellinger 		}
1688e48354ceSNicholas Bellinger 	}
16893e1c81a9SNicholas Bellinger 
16903e1c81a9SNicholas Bellinger 	return data_crc_failed;
16913e1c81a9SNicholas Bellinger }
16923e1c81a9SNicholas Bellinger 
16933e1c81a9SNicholas Bellinger int
iscsit_check_dataout_payload(struct iscsit_cmd * cmd,struct iscsi_data * hdr,bool data_crc_failed)169466cd9d4eSMax Gurtovoy iscsit_check_dataout_payload(struct iscsit_cmd *cmd, struct iscsi_data *hdr,
16953e1c81a9SNicholas Bellinger 			     bool data_crc_failed)
16963e1c81a9SNicholas Bellinger {
1697be36d683SMax Gurtovoy 	struct iscsit_conn *conn = cmd->conn;
16983e1c81a9SNicholas Bellinger 	int rc, ooo_cmdsn;
1699e48354ceSNicholas Bellinger 	/*
1700e48354ceSNicholas Bellinger 	 * Increment post receive data and CRC values or perform
1701e48354ceSNicholas Bellinger 	 * within-command recovery.
1702e48354ceSNicholas Bellinger 	 */
17033e1c81a9SNicholas Bellinger 	rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
17043e1c81a9SNicholas Bellinger 	if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
1705e48354ceSNicholas Bellinger 		return 0;
17063e1c81a9SNicholas Bellinger 	else if (rc == DATAOUT_SEND_R2T) {
1707e48354ceSNicholas Bellinger 		iscsit_set_dataout_sequence_values(cmd);
17083e1c81a9SNicholas Bellinger 		conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
17093e1c81a9SNicholas Bellinger 	} else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
1710e48354ceSNicholas Bellinger 		/*
1711e48354ceSNicholas Bellinger 		 * Handle extra special case for out of order
1712e48354ceSNicholas Bellinger 		 * Unsolicited Data Out.
1713e48354ceSNicholas Bellinger 		 */
1714e48354ceSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1715e48354ceSNicholas Bellinger 		ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1716e48354ceSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1717e48354ceSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1718e48354ceSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1719e48354ceSNicholas Bellinger 
1720e48354ceSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
172167441b68SChristoph Hellwig 		if (ooo_cmdsn)
172267441b68SChristoph Hellwig 			return 0;
172367441b68SChristoph Hellwig 		target_execute_cmd(&cmd->se_cmd);
172467441b68SChristoph Hellwig 		return 0;
1725e48354ceSNicholas Bellinger 	} else /* DATAOUT_CANNOT_RECOVER */
1726e48354ceSNicholas Bellinger 		return -1;
1727e48354ceSNicholas Bellinger 
1728e48354ceSNicholas Bellinger 	return 0;
1729e48354ceSNicholas Bellinger }
17303e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_check_dataout_payload);
1731e48354ceSNicholas Bellinger 
iscsit_handle_data_out(struct iscsit_conn * conn,unsigned char * buf)1732be36d683SMax Gurtovoy static int iscsit_handle_data_out(struct iscsit_conn *conn, unsigned char *buf)
17333e1c81a9SNicholas Bellinger {
173466cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd = NULL;
17353e1c81a9SNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *)buf;
17363e1c81a9SNicholas Bellinger 	int rc;
17373e1c81a9SNicholas Bellinger 	bool data_crc_failed = false;
17383e1c81a9SNicholas Bellinger 
17393e1c81a9SNicholas Bellinger 	rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
17403e1c81a9SNicholas Bellinger 	if (rc < 0)
1741561bf158SNicholas Bellinger 		return 0;
17423e1c81a9SNicholas Bellinger 	else if (!cmd)
17433e1c81a9SNicholas Bellinger 		return 0;
17443e1c81a9SNicholas Bellinger 
17453e1c81a9SNicholas Bellinger 	rc = iscsit_get_dataout(conn, cmd, hdr);
17463e1c81a9SNicholas Bellinger 	if (rc < 0)
17473e1c81a9SNicholas Bellinger 		return rc;
17483e1c81a9SNicholas Bellinger 	else if (rc > 0)
17493e1c81a9SNicholas Bellinger 		data_crc_failed = true;
17503e1c81a9SNicholas Bellinger 
17513e1c81a9SNicholas Bellinger 	return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
17523e1c81a9SNicholas Bellinger }
17533e1c81a9SNicholas Bellinger 
iscsit_setup_nop_out(struct iscsit_conn * conn,struct iscsit_cmd * cmd,struct iscsi_nopout * hdr)1754be36d683SMax Gurtovoy int iscsit_setup_nop_out(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
1755778de368SNicholas Bellinger 			 struct iscsi_nopout *hdr)
1756e48354ceSNicholas Bellinger {
1757778de368SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1758e48354ceSNicholas Bellinger 
1759a3662605SArshad Hussain 	if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1760a3662605SArshad Hussain 		pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1761a3662605SArshad Hussain 		if (!cmd)
1762a3662605SArshad Hussain 			return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1763a3662605SArshad Hussain 						 (unsigned char *)hdr);
1764a3662605SArshad Hussain 
1765a3662605SArshad Hussain 		return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1766a3662605SArshad Hussain 					 (unsigned char *)hdr);
1767a3662605SArshad Hussain 	}
1768a3662605SArshad Hussain 
176966c7db68SChristoph Hellwig 	if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1770e48354ceSNicholas Bellinger 		pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1771e48354ceSNicholas Bellinger 			" not set, protocol error.\n");
177228aaa950SNicholas Bellinger 		if (!cmd)
177328aaa950SNicholas Bellinger 			return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
177428aaa950SNicholas Bellinger 						 (unsigned char *)hdr);
177528aaa950SNicholas Bellinger 
1776ba159914SNicholas Bellinger 		return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1777ba159914SNicholas Bellinger 					 (unsigned char *)hdr);
1778e48354ceSNicholas Bellinger 	}
1779e48354ceSNicholas Bellinger 
178021f5aa7eSNicholas Bellinger 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1781e48354ceSNicholas Bellinger 		pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
178221f5aa7eSNicholas Bellinger 			" greater than MaxXmitDataSegmentLength: %u, protocol"
1783e48354ceSNicholas Bellinger 			" error.\n", payload_length,
178421f5aa7eSNicholas Bellinger 			conn->conn_ops->MaxXmitDataSegmentLength);
178528aaa950SNicholas Bellinger 		if (!cmd)
178628aaa950SNicholas Bellinger 			return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
178728aaa950SNicholas Bellinger 						 (unsigned char *)hdr);
178828aaa950SNicholas Bellinger 
1789ba159914SNicholas Bellinger 		return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1790ba159914SNicholas Bellinger 					 (unsigned char *)hdr);
1791e48354ceSNicholas Bellinger 	}
1792e48354ceSNicholas Bellinger 
17933e1c81a9SNicholas Bellinger 	pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1794e48354ceSNicholas Bellinger 		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
179566c7db68SChristoph Hellwig 		hdr->itt == RESERVED_ITT ? "Response" : "Request",
1796e48354ceSNicholas Bellinger 		hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1797e48354ceSNicholas Bellinger 		payload_length);
1798e48354ceSNicholas Bellinger 	/*
1799e48354ceSNicholas Bellinger 	 * This is not a response to a Unsolicited NopIN, which means
1800e48354ceSNicholas Bellinger 	 * it can either be a NOPOUT ping request (with a valid ITT),
1801e48354ceSNicholas Bellinger 	 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
180266cd9d4eSMax Gurtovoy 	 * Either way, make sure we allocate an struct iscsit_cmd, as both
1803e48354ceSNicholas Bellinger 	 * can contain ping data.
1804e48354ceSNicholas Bellinger 	 */
180550e5c87dSChristoph Hellwig 	if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1806e48354ceSNicholas Bellinger 		cmd->iscsi_opcode	= ISCSI_OP_NOOP_OUT;
1807e48354ceSNicholas Bellinger 		cmd->i_state		= ISTATE_SEND_NOPIN;
1808e48354ceSNicholas Bellinger 		cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1809e48354ceSNicholas Bellinger 						1 : 0);
1810e48354ceSNicholas Bellinger 		conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1811e48354ceSNicholas Bellinger 		cmd->targ_xfer_tag	= 0xFFFFFFFF;
181250e5c87dSChristoph Hellwig 		cmd->cmd_sn		= be32_to_cpu(hdr->cmdsn);
181350e5c87dSChristoph Hellwig 		cmd->exp_stat_sn	= be32_to_cpu(hdr->exp_statsn);
1814e48354ceSNicholas Bellinger 		cmd->data_direction	= DMA_NONE;
1815e48354ceSNicholas Bellinger 	}
1816e48354ceSNicholas Bellinger 
1817778de368SNicholas Bellinger 	return 0;
1818778de368SNicholas Bellinger }
1819778de368SNicholas Bellinger EXPORT_SYMBOL(iscsit_setup_nop_out);
1820778de368SNicholas Bellinger 
iscsit_process_nop_out(struct iscsit_conn * conn,struct iscsit_cmd * cmd,struct iscsi_nopout * hdr)1821be36d683SMax Gurtovoy int iscsit_process_nop_out(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
1822778de368SNicholas Bellinger 			   struct iscsi_nopout *hdr)
1823778de368SNicholas Bellinger {
182466cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd_p = NULL;
1825778de368SNicholas Bellinger 	int cmdsn_ret = 0;
1826778de368SNicholas Bellinger 	/*
1827778de368SNicholas Bellinger 	 * Initiator is expecting a NopIN ping reply..
1828778de368SNicholas Bellinger 	 */
1829778de368SNicholas Bellinger 	if (hdr->itt != RESERVED_ITT) {
18307cbfcc95SNicholas Bellinger 		if (!cmd)
18317cbfcc95SNicholas Bellinger 			return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
18327cbfcc95SNicholas Bellinger 						(unsigned char *)hdr);
1833778de368SNicholas Bellinger 
1834778de368SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
1835778de368SNicholas Bellinger 		list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1836778de368SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1837778de368SNicholas Bellinger 
1838778de368SNicholas Bellinger 		iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1839778de368SNicholas Bellinger 
1840778de368SNicholas Bellinger 		if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1841778de368SNicholas Bellinger 			iscsit_add_cmd_to_response_queue(cmd, conn,
1842778de368SNicholas Bellinger 							 cmd->i_state);
1843778de368SNicholas Bellinger 			return 0;
1844778de368SNicholas Bellinger 		}
1845778de368SNicholas Bellinger 
1846561bf158SNicholas Bellinger 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1847561bf158SNicholas Bellinger 				(unsigned char *)hdr, hdr->cmdsn);
1848778de368SNicholas Bellinger                 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1849778de368SNicholas Bellinger 			return 0;
1850778de368SNicholas Bellinger 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1851ba159914SNicholas Bellinger 			return -1;
1852778de368SNicholas Bellinger 
1853778de368SNicholas Bellinger 		return 0;
1854778de368SNicholas Bellinger 	}
1855778de368SNicholas Bellinger 	/*
1856778de368SNicholas Bellinger 	 * This was a response to a unsolicited NOPIN ping.
1857778de368SNicholas Bellinger 	 */
1858778de368SNicholas Bellinger 	if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1859778de368SNicholas Bellinger 		cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1860778de368SNicholas Bellinger 		if (!cmd_p)
1861778de368SNicholas Bellinger 			return -EINVAL;
1862778de368SNicholas Bellinger 
1863778de368SNicholas Bellinger 		iscsit_stop_nopin_response_timer(conn);
1864778de368SNicholas Bellinger 
1865778de368SNicholas Bellinger 		cmd_p->i_state = ISTATE_REMOVE;
1866778de368SNicholas Bellinger 		iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1867778de368SNicholas Bellinger 
1868778de368SNicholas Bellinger 		iscsit_start_nopin_timer(conn);
1869778de368SNicholas Bellinger 		return 0;
1870778de368SNicholas Bellinger 	}
1871778de368SNicholas Bellinger 	/*
1872778de368SNicholas Bellinger 	 * Otherwise, initiator is not expecting a NOPIN is response.
1873778de368SNicholas Bellinger 	 * Just ignore for now.
1874778de368SNicholas Bellinger 	 */
18751a40f0a3SVarun Prakash 
18761a40f0a3SVarun Prakash 	if (cmd)
18771a40f0a3SVarun Prakash 		iscsit_free_cmd(cmd, false);
18781a40f0a3SVarun Prakash 
1879778de368SNicholas Bellinger         return 0;
1880778de368SNicholas Bellinger }
1881778de368SNicholas Bellinger EXPORT_SYMBOL(iscsit_process_nop_out);
1882778de368SNicholas Bellinger 
iscsit_handle_nop_out(struct iscsit_conn * conn,struct iscsit_cmd * cmd,unsigned char * buf)1883be36d683SMax Gurtovoy static int iscsit_handle_nop_out(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
1884778de368SNicholas Bellinger 				 unsigned char *buf)
1885778de368SNicholas Bellinger {
1886778de368SNicholas Bellinger 	unsigned char *ping_data = NULL;
1887778de368SNicholas Bellinger 	struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1888778de368SNicholas Bellinger 	struct kvec *iov = NULL;
1889778de368SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
1890778de368SNicholas Bellinger 	int ret;
1891778de368SNicholas Bellinger 
1892778de368SNicholas Bellinger 	ret = iscsit_setup_nop_out(conn, cmd, hdr);
1893778de368SNicholas Bellinger 	if (ret < 0)
1894561bf158SNicholas Bellinger 		return 0;
1895778de368SNicholas Bellinger 	/*
1896778de368SNicholas Bellinger 	 * Handle NOP-OUT payload for traditional iSCSI sockets
1897778de368SNicholas Bellinger 	 */
189850e5c87dSChristoph Hellwig 	if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1899778de368SNicholas Bellinger 		u32 checksum, data_crc, padding = 0;
1900778de368SNicholas Bellinger 		int niov = 0, rx_got, rx_size = payload_length;
1901778de368SNicholas Bellinger 
1902e48354ceSNicholas Bellinger 		ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1903e48354ceSNicholas Bellinger 		if (!ping_data) {
1904e48354ceSNicholas Bellinger 			ret = -1;
1905e48354ceSNicholas Bellinger 			goto out;
1906e48354ceSNicholas Bellinger 		}
1907e48354ceSNicholas Bellinger 
1908e48354ceSNicholas Bellinger 		iov = &cmd->iov_misc[0];
1909e48354ceSNicholas Bellinger 		iov[niov].iov_base	= ping_data;
1910e48354ceSNicholas Bellinger 		iov[niov++].iov_len	= payload_length;
1911e48354ceSNicholas Bellinger 
1912e48354ceSNicholas Bellinger 		padding = ((-payload_length) & 3);
1913e48354ceSNicholas Bellinger 		if (padding != 0) {
1914e48354ceSNicholas Bellinger 			pr_debug("Receiving %u additional bytes"
1915e48354ceSNicholas Bellinger 				" for padding.\n", padding);
1916e48354ceSNicholas Bellinger 			iov[niov].iov_base	= &cmd->pad_bytes;
1917e48354ceSNicholas Bellinger 			iov[niov++].iov_len	= padding;
1918e48354ceSNicholas Bellinger 			rx_size += padding;
1919e48354ceSNicholas Bellinger 		}
1920e48354ceSNicholas Bellinger 		if (conn->conn_ops->DataDigest) {
1921e48354ceSNicholas Bellinger 			iov[niov].iov_base	= &checksum;
1922e48354ceSNicholas Bellinger 			iov[niov++].iov_len	= ISCSI_CRC_LEN;
1923e48354ceSNicholas Bellinger 			rx_size += ISCSI_CRC_LEN;
1924e48354ceSNicholas Bellinger 		}
1925e48354ceSNicholas Bellinger 
19262e39f1c9SBart Van Assche 		WARN_ON_ONCE(niov > ARRAY_SIZE(cmd->iov_misc));
1927e48354ceSNicholas Bellinger 		rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1928e48354ceSNicholas Bellinger 		if (rx_got != rx_size) {
1929e48354ceSNicholas Bellinger 			ret = -1;
1930e48354ceSNicholas Bellinger 			goto out;
1931e48354ceSNicholas Bellinger 		}
1932e48354ceSNicholas Bellinger 
1933e48354ceSNicholas Bellinger 		if (conn->conn_ops->DataDigest) {
1934e1dfb21fSBart Van Assche 			iscsit_do_crypto_hash_buf(conn->conn_rx_hash, ping_data,
1935e1dfb21fSBart Van Assche 						  payload_length, padding,
1936e1dfb21fSBart Van Assche 						  cmd->pad_bytes, &data_crc);
1937e48354ceSNicholas Bellinger 
1938e48354ceSNicholas Bellinger 			if (checksum != data_crc) {
1939e48354ceSNicholas Bellinger 				pr_err("Ping data CRC32C DataDigest"
1940e48354ceSNicholas Bellinger 				" 0x%08x does not match computed 0x%08x\n",
1941e48354ceSNicholas Bellinger 					checksum, data_crc);
1942e48354ceSNicholas Bellinger 				if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1943e48354ceSNicholas Bellinger 					pr_err("Unable to recover from"
1944e48354ceSNicholas Bellinger 					" NOPOUT Ping DataCRC failure while in"
1945e48354ceSNicholas Bellinger 						" ERL=0.\n");
1946e48354ceSNicholas Bellinger 					ret = -1;
1947e48354ceSNicholas Bellinger 					goto out;
1948e48354ceSNicholas Bellinger 				} else {
1949e48354ceSNicholas Bellinger 					/*
1950e48354ceSNicholas Bellinger 					 * Silently drop this PDU and let the
1951e48354ceSNicholas Bellinger 					 * initiator plug the CmdSN gap.
1952e48354ceSNicholas Bellinger 					 */
1953e48354ceSNicholas Bellinger 					pr_debug("Dropping NOPOUT"
1954e48354ceSNicholas Bellinger 					" Command CmdSN: 0x%08x due to"
1955e48354ceSNicholas Bellinger 					" DataCRC error.\n", hdr->cmdsn);
1956e48354ceSNicholas Bellinger 					ret = 0;
1957e48354ceSNicholas Bellinger 					goto out;
1958e48354ceSNicholas Bellinger 				}
1959e48354ceSNicholas Bellinger 			} else {
1960e48354ceSNicholas Bellinger 				pr_debug("Got CRC32C DataDigest"
1961e48354ceSNicholas Bellinger 				" 0x%08x for %u bytes of ping data.\n",
1962e48354ceSNicholas Bellinger 					checksum, payload_length);
1963e48354ceSNicholas Bellinger 			}
1964e48354ceSNicholas Bellinger 		}
1965e48354ceSNicholas Bellinger 
1966e48354ceSNicholas Bellinger 		ping_data[payload_length] = '\0';
1967e48354ceSNicholas Bellinger 		/*
196866cd9d4eSMax Gurtovoy 		 * Attach ping data to struct iscsit_cmd->buf_ptr.
1969e48354ceSNicholas Bellinger 		 */
19708359cf43SJörn Engel 		cmd->buf_ptr = ping_data;
1971e48354ceSNicholas Bellinger 		cmd->buf_ptr_size = payload_length;
1972e48354ceSNicholas Bellinger 
1973e48354ceSNicholas Bellinger 		pr_debug("Got %u bytes of NOPOUT ping"
1974e48354ceSNicholas Bellinger 			" data.\n", payload_length);
1975e48354ceSNicholas Bellinger 		pr_debug("Ping Data: \"%s\"\n", ping_data);
1976e48354ceSNicholas Bellinger 	}
1977e48354ceSNicholas Bellinger 
1978778de368SNicholas Bellinger 	return iscsit_process_nop_out(conn, cmd, hdr);
1979e48354ceSNicholas Bellinger out:
1980e48354ceSNicholas Bellinger 	if (cmd)
1981aafc9d15SNicholas Bellinger 		iscsit_free_cmd(cmd, false);
1982778de368SNicholas Bellinger 
1983e48354ceSNicholas Bellinger 	kfree(ping_data);
1984e48354ceSNicholas Bellinger 	return ret;
1985e48354ceSNicholas Bellinger }
1986e48354ceSNicholas Bellinger 
iscsit_convert_tmf(u8 iscsi_tmf)1987e381fe9eSBart Van Assche static enum tcm_tmreq_table iscsit_convert_tmf(u8 iscsi_tmf)
1988e381fe9eSBart Van Assche {
1989e381fe9eSBart Van Assche 	switch (iscsi_tmf) {
1990e381fe9eSBart Van Assche 	case ISCSI_TM_FUNC_ABORT_TASK:
1991e381fe9eSBart Van Assche 		return TMR_ABORT_TASK;
1992e381fe9eSBart Van Assche 	case ISCSI_TM_FUNC_ABORT_TASK_SET:
1993e381fe9eSBart Van Assche 		return TMR_ABORT_TASK_SET;
1994e381fe9eSBart Van Assche 	case ISCSI_TM_FUNC_CLEAR_ACA:
1995e381fe9eSBart Van Assche 		return TMR_CLEAR_ACA;
1996e381fe9eSBart Van Assche 	case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1997e381fe9eSBart Van Assche 		return TMR_CLEAR_TASK_SET;
1998e381fe9eSBart Van Assche 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1999e381fe9eSBart Van Assche 		return TMR_LUN_RESET;
2000e381fe9eSBart Van Assche 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2001e381fe9eSBart Van Assche 		return TMR_TARGET_WARM_RESET;
2002e381fe9eSBart Van Assche 	case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2003e381fe9eSBart Van Assche 		return TMR_TARGET_COLD_RESET;
2004e381fe9eSBart Van Assche 	default:
2005e381fe9eSBart Van Assche 		return TMR_UNKNOWN;
2006e381fe9eSBart Van Assche 	}
2007e381fe9eSBart Van Assche }
2008e381fe9eSBart Van Assche 
20093e1c81a9SNicholas Bellinger int
iscsit_handle_task_mgt_cmd(struct iscsit_conn * conn,struct iscsit_cmd * cmd,unsigned char * buf)2010be36d683SMax Gurtovoy iscsit_handle_task_mgt_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
2011e48354ceSNicholas Bellinger 			   unsigned char *buf)
2012e48354ceSNicholas Bellinger {
2013e48354ceSNicholas Bellinger 	struct se_tmr_req *se_tmr;
2014e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req;
2015e48354ceSNicholas Bellinger 	struct iscsi_tm *hdr;
2016186a9647SNicholas Bellinger 	int out_of_order_cmdsn = 0, ret;
201759b6986dSBart Van Assche 	u8 function, tcm_function = TMR_UNKNOWN;
2018e48354ceSNicholas Bellinger 
2019e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_tm *) buf;
2020e48354ceSNicholas Bellinger 	hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
2021e48354ceSNicholas Bellinger 	function = hdr->flags;
2022e48354ceSNicholas Bellinger 
2023e48354ceSNicholas Bellinger 	pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
2024e48354ceSNicholas Bellinger 		" 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
2025e48354ceSNicholas Bellinger 		" 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
2026e48354ceSNicholas Bellinger 		hdr->rtt, hdr->refcmdsn, conn->cid);
2027e48354ceSNicholas Bellinger 
2028e48354ceSNicholas Bellinger 	if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
2029e48354ceSNicholas Bellinger 	    ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
203066c7db68SChristoph Hellwig 	     hdr->rtt != RESERVED_ITT)) {
2031e48354ceSNicholas Bellinger 		pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
203266c7db68SChristoph Hellwig 		hdr->rtt = RESERVED_ITT;
2033e48354ceSNicholas Bellinger 	}
2034e48354ceSNicholas Bellinger 
2035e48354ceSNicholas Bellinger 	if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
2036e48354ceSNicholas Bellinger 			!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2037e48354ceSNicholas Bellinger 		pr_err("Task Management Request TASK_REASSIGN not"
2038e48354ceSNicholas Bellinger 			" issued as immediate command, bad iSCSI Initiator"
2039e48354ceSNicholas Bellinger 				"implementation\n");
2040ba159914SNicholas Bellinger 		return iscsit_add_reject_cmd(cmd,
2041ba159914SNicholas Bellinger 					     ISCSI_REASON_PROTOCOL_ERROR, buf);
2042e48354ceSNicholas Bellinger 	}
2043e48354ceSNicholas Bellinger 	if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
204450e5c87dSChristoph Hellwig 	    be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
204550e5c87dSChristoph Hellwig 		hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
2046e48354ceSNicholas Bellinger 
2047d28b1169SAndy Grover 	cmd->data_direction = DMA_NONE;
20483829f381SMarkus Elfring 	cmd->tmr_req = kzalloc(sizeof(*cmd->tmr_req), GFP_KERNEL);
2049ae072726SNicholas Bellinger 	if (!cmd->tmr_req) {
2050ba159914SNicholas Bellinger 		return iscsit_add_reject_cmd(cmd,
2051d28b1169SAndy Grover 					     ISCSI_REASON_BOOKMARK_NO_RESOURCES,
2052ba159914SNicholas Bellinger 					     buf);
2053ae072726SNicholas Bellinger 	}
2054ae072726SNicholas Bellinger 
2055a78b7136SMike Christie 	__target_init_cmd(&cmd->se_cmd, &iscsi_ops,
2056ae072726SNicholas Bellinger 			  conn->sess->se_sess, 0, DMA_NONE,
2057a36840d8SSudhakar Panneerselvam 			  TCM_SIMPLE_TAG, cmd->sense_buffer + 2,
20588e288be8SMike Christie 			  scsilun_to_int(&hdr->lun),
20596d256beeSMike Christie 			  conn->cmd_cnt);
2060ae072726SNicholas Bellinger 
2061807b9515SBart Van Assche 	target_get_sess_cmd(&cmd->se_cmd, true);
2062d28b1169SAndy Grover 
2063d28b1169SAndy Grover 	/*
2064d28b1169SAndy Grover 	 * TASK_REASSIGN for ERL=2 / connection stays inside of
2065d28b1169SAndy Grover 	 * LIO-Target $FABRIC_MOD
2066d28b1169SAndy Grover 	 */
2067d28b1169SAndy Grover 	if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
2068e381fe9eSBart Van Assche 		tcm_function = iscsit_convert_tmf(function);
2069e381fe9eSBart Van Assche 		if (tcm_function == TMR_UNKNOWN) {
2070d28b1169SAndy Grover 			pr_err("Unknown iSCSI TMR Function:"
2071d28b1169SAndy Grover 			       " 0x%02x\n", function);
2072ba159914SNicholas Bellinger 			return iscsit_add_reject_cmd(cmd,
2073ba159914SNicholas Bellinger 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
2074d28b1169SAndy Grover 		}
207559b6986dSBart Van Assche 	}
207659b6986dSBart Van Assche 	ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, tcm_function,
207759b6986dSBart Van Assche 				 GFP_KERNEL);
2078d28b1169SAndy Grover 	if (ret < 0)
2079ba159914SNicholas Bellinger 		return iscsit_add_reject_cmd(cmd,
2080ba159914SNicholas Bellinger 				ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
2081d28b1169SAndy Grover 
2082d28b1169SAndy Grover 	cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
2083d28b1169SAndy Grover 
2084e48354ceSNicholas Bellinger 	cmd->iscsi_opcode	= ISCSI_OP_SCSI_TMFUNC;
2085e48354ceSNicholas Bellinger 	cmd->i_state		= ISTATE_SEND_TASKMGTRSP;
2086e48354ceSNicholas Bellinger 	cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2087e48354ceSNicholas Bellinger 	cmd->init_task_tag	= hdr->itt;
2088e48354ceSNicholas Bellinger 	cmd->targ_xfer_tag	= 0xFFFFFFFF;
208950e5c87dSChristoph Hellwig 	cmd->cmd_sn		= be32_to_cpu(hdr->cmdsn);
209050e5c87dSChristoph Hellwig 	cmd->exp_stat_sn	= be32_to_cpu(hdr->exp_statsn);
2091e48354ceSNicholas Bellinger 	se_tmr			= cmd->se_cmd.se_tmr_req;
2092e48354ceSNicholas Bellinger 	tmr_req			= cmd->tmr_req;
2093e48354ceSNicholas Bellinger 	/*
2094e48354ceSNicholas Bellinger 	 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2095e48354ceSNicholas Bellinger 	 */
2096e48354ceSNicholas Bellinger 	if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
2097a36840d8SSudhakar Panneerselvam 		ret = transport_lookup_tmr_lun(&cmd->se_cmd);
2098e48354ceSNicholas Bellinger 		if (ret < 0) {
2099e48354ceSNicholas Bellinger 			se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2100e48354ceSNicholas Bellinger 			goto attach;
2101e48354ceSNicholas Bellinger 		}
2102e48354ceSNicholas Bellinger 	}
2103e48354ceSNicholas Bellinger 
2104e48354ceSNicholas Bellinger 	switch (function) {
2105e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_ABORT_TASK:
2106e48354ceSNicholas Bellinger 		se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
2107de103c93SChristoph Hellwig 		if (se_tmr->response)
2108e48354ceSNicholas Bellinger 			goto attach;
2109e48354ceSNicholas Bellinger 		break;
2110e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_ABORT_TASK_SET:
2111e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_CLEAR_ACA:
2112e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2113e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2114e48354ceSNicholas Bellinger 		break;
2115e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2116e48354ceSNicholas Bellinger 		if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
2117e48354ceSNicholas Bellinger 			se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2118e48354ceSNicholas Bellinger 			goto attach;
2119e48354ceSNicholas Bellinger 		}
2120e48354ceSNicholas Bellinger 		break;
2121e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2122e48354ceSNicholas Bellinger 		if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
2123e48354ceSNicholas Bellinger 			se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2124e48354ceSNicholas Bellinger 			goto attach;
2125e48354ceSNicholas Bellinger 		}
2126e48354ceSNicholas Bellinger 		break;
2127e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_TASK_REASSIGN:
2128e48354ceSNicholas Bellinger 		se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2129e48354ceSNicholas Bellinger 		/*
2130e48354ceSNicholas Bellinger 		 * Perform sanity checks on the ExpDataSN only if the
2131e48354ceSNicholas Bellinger 		 * TASK_REASSIGN was successful.
2132e48354ceSNicholas Bellinger 		 */
2133de103c93SChristoph Hellwig 		if (se_tmr->response)
2134e48354ceSNicholas Bellinger 			break;
2135e48354ceSNicholas Bellinger 
2136e48354ceSNicholas Bellinger 		if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
2137ba159914SNicholas Bellinger 			return iscsit_add_reject_cmd(cmd,
2138ba159914SNicholas Bellinger 					ISCSI_REASON_BOOKMARK_INVALID, buf);
2139e48354ceSNicholas Bellinger 		break;
2140e48354ceSNicholas Bellinger 	default:
2141e48354ceSNicholas Bellinger 		pr_err("Unknown TMR function: 0x%02x, protocol"
2142e48354ceSNicholas Bellinger 			" error.\n", function);
2143e48354ceSNicholas Bellinger 		se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2144e48354ceSNicholas Bellinger 		goto attach;
2145e48354ceSNicholas Bellinger 	}
2146e48354ceSNicholas Bellinger 
2147e48354ceSNicholas Bellinger 	if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2148e48354ceSNicholas Bellinger 	    (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2149e48354ceSNicholas Bellinger 		se_tmr->call_transport = 1;
2150e48354ceSNicholas Bellinger attach:
2151e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
21522fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2153e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
2154e48354ceSNicholas Bellinger 
2155e48354ceSNicholas Bellinger 	if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2156561bf158SNicholas Bellinger 		int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
21573fc9fb13SNicholas Bellinger 		if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP) {
2158e48354ceSNicholas Bellinger 			out_of_order_cmdsn = 1;
21593fc9fb13SNicholas Bellinger 		} else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
21603fc9fb13SNicholas Bellinger 			target_put_sess_cmd(&cmd->se_cmd);
2161e48354ceSNicholas Bellinger 			return 0;
21623fc9fb13SNicholas Bellinger 		} else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
2163ba159914SNicholas Bellinger 			return -1;
2164e48354ceSNicholas Bellinger 		}
21653fc9fb13SNicholas Bellinger 	}
216650e5c87dSChristoph Hellwig 	iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2167e48354ceSNicholas Bellinger 
21685a4c8666SNicholas Bellinger 	if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
2169e48354ceSNicholas Bellinger 		return 0;
2170e48354ceSNicholas Bellinger 	/*
2171e48354ceSNicholas Bellinger 	 * Found the referenced task, send to transport for processing.
2172e48354ceSNicholas Bellinger 	 */
2173e48354ceSNicholas Bellinger 	if (se_tmr->call_transport)
2174e48354ceSNicholas Bellinger 		return transport_generic_handle_tmr(&cmd->se_cmd);
2175e48354ceSNicholas Bellinger 
2176e48354ceSNicholas Bellinger 	/*
2177e48354ceSNicholas Bellinger 	 * Could not find the referenced LUN, task, or Task Management
2178e48354ceSNicholas Bellinger 	 * command not authorized or supported.  Change state and
2179e48354ceSNicholas Bellinger 	 * let the tx_thread send the response.
2180e48354ceSNicholas Bellinger 	 *
2181e48354ceSNicholas Bellinger 	 * For connection recovery, this is also the default action for
2182e48354ceSNicholas Bellinger 	 * TMR TASK_REASSIGN.
2183e48354ceSNicholas Bellinger 	 */
2184e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2185ae072726SNicholas Bellinger 	target_put_sess_cmd(&cmd->se_cmd);
2186e48354ceSNicholas Bellinger 	return 0;
2187e48354ceSNicholas Bellinger }
21883e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
2189e48354ceSNicholas Bellinger 
2190e48354ceSNicholas Bellinger /* #warning FIXME: Support Text Command parameters besides SendTargets */
219164534aa7SNicholas Bellinger int
iscsit_setup_text_cmd(struct iscsit_conn * conn,struct iscsit_cmd * cmd,struct iscsi_text * hdr)2192be36d683SMax Gurtovoy iscsit_setup_text_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
219364534aa7SNicholas Bellinger 		      struct iscsi_text *hdr)
2194e48354ceSNicholas Bellinger {
219564534aa7SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
2196e48354ceSNicholas Bellinger 
219721f5aa7eSNicholas Bellinger 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
2198e48354ceSNicholas Bellinger 		pr_err("Unable to accept text parameter length: %u"
219921f5aa7eSNicholas Bellinger 			"greater than MaxXmitDataSegmentLength %u.\n",
220021f5aa7eSNicholas Bellinger 		       payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
2201ba159914SNicholas Bellinger 		return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2202ba159914SNicholas Bellinger 					 (unsigned char *)hdr);
2203e48354ceSNicholas Bellinger 	}
2204e48354ceSNicholas Bellinger 
2205122f8afcSNicholas Bellinger 	if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2206122f8afcSNicholas Bellinger 	     (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2207122f8afcSNicholas Bellinger 		pr_err("Multi sequence text commands currently not supported\n");
2208122f8afcSNicholas Bellinger 		return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2209122f8afcSNicholas Bellinger 					(unsigned char *)hdr);
2210122f8afcSNicholas Bellinger 	}
2211122f8afcSNicholas Bellinger 
2212e48354ceSNicholas Bellinger 	pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2213e48354ceSNicholas Bellinger 		" ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2214e48354ceSNicholas Bellinger 		hdr->exp_statsn, payload_length);
2215e48354ceSNicholas Bellinger 
221664534aa7SNicholas Bellinger 	cmd->iscsi_opcode	= ISCSI_OP_TEXT;
221764534aa7SNicholas Bellinger 	cmd->i_state		= ISTATE_SEND_TEXTRSP;
221864534aa7SNicholas Bellinger 	cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
221964534aa7SNicholas Bellinger 	conn->sess->init_task_tag = cmd->init_task_tag  = hdr->itt;
222064534aa7SNicholas Bellinger 	cmd->targ_xfer_tag	= 0xFFFFFFFF;
222164534aa7SNicholas Bellinger 	cmd->cmd_sn		= be32_to_cpu(hdr->cmdsn);
222264534aa7SNicholas Bellinger 	cmd->exp_stat_sn	= be32_to_cpu(hdr->exp_statsn);
222364534aa7SNicholas Bellinger 	cmd->data_direction	= DMA_NONE;
2224ea8dc5b4SVarun Prakash 	kfree(cmd->text_in_ptr);
2225e4f4e801SSagi Grimberg 	cmd->text_in_ptr	= NULL;
222664534aa7SNicholas Bellinger 
222764534aa7SNicholas Bellinger 	return 0;
222864534aa7SNicholas Bellinger }
222964534aa7SNicholas Bellinger EXPORT_SYMBOL(iscsit_setup_text_cmd);
223064534aa7SNicholas Bellinger 
223164534aa7SNicholas Bellinger int
iscsit_process_text_cmd(struct iscsit_conn * conn,struct iscsit_cmd * cmd,struct iscsi_text * hdr)2232be36d683SMax Gurtovoy iscsit_process_text_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
223364534aa7SNicholas Bellinger 			struct iscsi_text *hdr)
223464534aa7SNicholas Bellinger {
22359864ca9dSNicholas Bellinger 	unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
223664534aa7SNicholas Bellinger 	int cmdsn_ret;
223764534aa7SNicholas Bellinger 
22389864ca9dSNicholas Bellinger 	if (!text_in) {
2239e4f4e801SSagi Grimberg 		cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2240e4f4e801SSagi Grimberg 		if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
22419864ca9dSNicholas Bellinger 			pr_err("Unable to locate text_in buffer for sendtargets"
22429864ca9dSNicholas Bellinger 			       " discovery\n");
22439864ca9dSNicholas Bellinger 			goto reject;
22449864ca9dSNicholas Bellinger 		}
2245e4f4e801SSagi Grimberg 		goto empty_sendtargets;
2246e4f4e801SSagi Grimberg 	}
224795f8f6a9SDavid Disseldorp 	if (strncmp("SendTargets=", text_in, 12) != 0) {
22489864ca9dSNicholas Bellinger 		pr_err("Received Text Data that is not"
22499864ca9dSNicholas Bellinger 			" SendTargets, cannot continue.\n");
22509864ca9dSNicholas Bellinger 		goto reject;
22519864ca9dSNicholas Bellinger 	}
225295f8f6a9SDavid Disseldorp 	/* '=' confirmed in strncmp */
22539864ca9dSNicholas Bellinger 	text_ptr = strchr(text_in, '=');
225495f8f6a9SDavid Disseldorp 	BUG_ON(!text_ptr);
225595f8f6a9SDavid Disseldorp 	if (!strncmp("=All", text_ptr, 5)) {
22568060b8ddSAndy Grover 		cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
22576665889cSNicholas Bellinger 	} else if (!strncmp("=iqn.", text_ptr, 5) ||
22586665889cSNicholas Bellinger 		   !strncmp("=eui.", text_ptr, 5)) {
22598060b8ddSAndy Grover 		cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
22609864ca9dSNicholas Bellinger 	} else {
226195f8f6a9SDavid Disseldorp 		pr_err("Unable to locate valid SendTargets%s value\n",
226295f8f6a9SDavid Disseldorp 		       text_ptr);
22639864ca9dSNicholas Bellinger 		goto reject;
22649864ca9dSNicholas Bellinger 	}
22659864ca9dSNicholas Bellinger 
226664534aa7SNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
226764534aa7SNicholas Bellinger 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
226864534aa7SNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
226964534aa7SNicholas Bellinger 
2270e4f4e801SSagi Grimberg empty_sendtargets:
227164534aa7SNicholas Bellinger 	iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
227264534aa7SNicholas Bellinger 
227364534aa7SNicholas Bellinger 	if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2274561bf158SNicholas Bellinger 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2275561bf158SNicholas Bellinger 				(unsigned char *)hdr, hdr->cmdsn);
227664534aa7SNicholas Bellinger 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2277ba159914SNicholas Bellinger 			return -1;
2278ba159914SNicholas Bellinger 
227964534aa7SNicholas Bellinger 		return 0;
228064534aa7SNicholas Bellinger 	}
228164534aa7SNicholas Bellinger 
228264534aa7SNicholas Bellinger 	return iscsit_execute_cmd(cmd, 0);
22839864ca9dSNicholas Bellinger 
22849864ca9dSNicholas Bellinger reject:
2285ba159914SNicholas Bellinger 	return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2286ba159914SNicholas Bellinger 				 (unsigned char *)hdr);
228764534aa7SNicholas Bellinger }
228864534aa7SNicholas Bellinger EXPORT_SYMBOL(iscsit_process_text_cmd);
228964534aa7SNicholas Bellinger 
229064534aa7SNicholas Bellinger static int
iscsit_handle_text_cmd(struct iscsit_conn * conn,struct iscsit_cmd * cmd,unsigned char * buf)2291be36d683SMax Gurtovoy iscsit_handle_text_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
229264534aa7SNicholas Bellinger 		       unsigned char *buf)
229364534aa7SNicholas Bellinger {
229464534aa7SNicholas Bellinger 	struct iscsi_text *hdr = (struct iscsi_text *)buf;
229564534aa7SNicholas Bellinger 	char *text_in = NULL;
229664534aa7SNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
229764534aa7SNicholas Bellinger 	int rx_size, rc;
229864534aa7SNicholas Bellinger 
229964534aa7SNicholas Bellinger 	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
230064534aa7SNicholas Bellinger 	if (rc < 0)
2301561bf158SNicholas Bellinger 		return 0;
230264534aa7SNicholas Bellinger 
230364534aa7SNicholas Bellinger 	rx_size = payload_length;
230464534aa7SNicholas Bellinger 	if (payload_length) {
230564534aa7SNicholas Bellinger 		u32 checksum = 0, data_crc = 0;
230647eefdedSBart Van Assche 		u32 padding = 0;
230764534aa7SNicholas Bellinger 		int niov = 0, rx_got;
230847eefdedSBart Van Assche 		struct kvec iov[2];
230964534aa7SNicholas Bellinger 
231047eefdedSBart Van Assche 		rx_size = ALIGN(payload_length, 4);
231147eefdedSBart Van Assche 		text_in = kzalloc(rx_size, GFP_KERNEL);
2312c46e22f1SMarkus Elfring 		if (!text_in)
231364534aa7SNicholas Bellinger 			goto reject;
2314c46e22f1SMarkus Elfring 
23159864ca9dSNicholas Bellinger 		cmd->text_in_ptr = text_in;
2316e48354ceSNicholas Bellinger 
231747eefdedSBart Van Assche 		memset(iov, 0, sizeof(iov));
2318e48354ceSNicholas Bellinger 		iov[niov].iov_base	= text_in;
231947eefdedSBart Van Assche 		iov[niov++].iov_len	= rx_size;
2320e48354ceSNicholas Bellinger 
232147eefdedSBart Van Assche 		padding = rx_size - payload_length;
232247eefdedSBart Van Assche 		if (padding)
2323e48354ceSNicholas Bellinger 			pr_debug("Receiving %u additional bytes"
2324e48354ceSNicholas Bellinger 					" for padding.\n", padding);
2325e48354ceSNicholas Bellinger 		if (conn->conn_ops->DataDigest) {
2326e48354ceSNicholas Bellinger 			iov[niov].iov_base	= &checksum;
2327e48354ceSNicholas Bellinger 			iov[niov++].iov_len	= ISCSI_CRC_LEN;
2328e48354ceSNicholas Bellinger 			rx_size += ISCSI_CRC_LEN;
2329e48354ceSNicholas Bellinger 		}
2330e48354ceSNicholas Bellinger 
23312e39f1c9SBart Van Assche 		WARN_ON_ONCE(niov > ARRAY_SIZE(iov));
2332e48354ceSNicholas Bellinger 		rx_got = rx_data(conn, &iov[0], niov, rx_size);
233364534aa7SNicholas Bellinger 		if (rx_got != rx_size)
233464534aa7SNicholas Bellinger 			goto reject;
2335e48354ceSNicholas Bellinger 
2336e48354ceSNicholas Bellinger 		if (conn->conn_ops->DataDigest) {
233747eefdedSBart Van Assche 			iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
233847eefdedSBart Van Assche 						  text_in, rx_size, 0, NULL,
233947eefdedSBart Van Assche 						  &data_crc);
2340e48354ceSNicholas Bellinger 
2341e48354ceSNicholas Bellinger 			if (checksum != data_crc) {
2342e48354ceSNicholas Bellinger 				pr_err("Text data CRC32C DataDigest"
2343e48354ceSNicholas Bellinger 					" 0x%08x does not match computed"
2344e48354ceSNicholas Bellinger 					" 0x%08x\n", checksum, data_crc);
2345e48354ceSNicholas Bellinger 				if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2346e48354ceSNicholas Bellinger 					pr_err("Unable to recover from"
2347e48354ceSNicholas Bellinger 					" Text Data digest failure while in"
2348e48354ceSNicholas Bellinger 						" ERL=0.\n");
234964534aa7SNicholas Bellinger 					goto reject;
2350e48354ceSNicholas Bellinger 				} else {
2351e48354ceSNicholas Bellinger 					/*
2352e48354ceSNicholas Bellinger 					 * Silently drop this PDU and let the
2353e48354ceSNicholas Bellinger 					 * initiator plug the CmdSN gap.
2354e48354ceSNicholas Bellinger 					 */
2355e48354ceSNicholas Bellinger 					pr_debug("Dropping Text"
2356e48354ceSNicholas Bellinger 					" Command CmdSN: 0x%08x due to"
2357e48354ceSNicholas Bellinger 					" DataCRC error.\n", hdr->cmdsn);
2358e48354ceSNicholas Bellinger 					kfree(text_in);
2359e48354ceSNicholas Bellinger 					return 0;
2360e48354ceSNicholas Bellinger 				}
2361e48354ceSNicholas Bellinger 			} else {
2362e48354ceSNicholas Bellinger 				pr_debug("Got CRC32C DataDigest"
2363e48354ceSNicholas Bellinger 					" 0x%08x for %u bytes of text data.\n",
236464534aa7SNicholas Bellinger 						checksum, payload_length);
2365e48354ceSNicholas Bellinger 			}
2366e48354ceSNicholas Bellinger 		}
236764534aa7SNicholas Bellinger 		text_in[payload_length - 1] = '\0';
2368e48354ceSNicholas Bellinger 		pr_debug("Successfully read %d bytes of text"
236964534aa7SNicholas Bellinger 				" data.\n", payload_length);
2370e48354ceSNicholas Bellinger 	}
2371e48354ceSNicholas Bellinger 
237264534aa7SNicholas Bellinger 	return iscsit_process_text_cmd(conn, cmd, hdr);
2373e48354ceSNicholas Bellinger 
237464534aa7SNicholas Bellinger reject:
23759864ca9dSNicholas Bellinger 	kfree(cmd->text_in_ptr);
23769864ca9dSNicholas Bellinger 	cmd->text_in_ptr = NULL;
2377ba159914SNicholas Bellinger 	return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
2378e48354ceSNicholas Bellinger }
2379e48354ceSNicholas Bellinger 
iscsit_logout_closesession(struct iscsit_cmd * cmd,struct iscsit_conn * conn)2380be36d683SMax Gurtovoy int iscsit_logout_closesession(struct iscsit_cmd *cmd, struct iscsit_conn *conn)
2381e48354ceSNicholas Bellinger {
2382be36d683SMax Gurtovoy 	struct iscsit_conn *conn_p;
23830873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
2384e48354ceSNicholas Bellinger 
2385e48354ceSNicholas Bellinger 	pr_debug("Received logout request CLOSESESSION on CID: %hu"
2386e48354ceSNicholas Bellinger 		" for SID: %u.\n", conn->cid, conn->sess->sid);
2387e48354ceSNicholas Bellinger 
2388e48354ceSNicholas Bellinger 	atomic_set(&sess->session_logout, 1);
2389e48354ceSNicholas Bellinger 	atomic_set(&conn->conn_logout_remove, 1);
2390e48354ceSNicholas Bellinger 	conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2391e48354ceSNicholas Bellinger 
2392e48354ceSNicholas Bellinger 	iscsit_inc_conn_usage_count(conn);
2393e48354ceSNicholas Bellinger 	iscsit_inc_session_usage_count(sess);
2394e48354ceSNicholas Bellinger 
2395e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
2396e48354ceSNicholas Bellinger 	list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2397e48354ceSNicholas Bellinger 		if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2398e48354ceSNicholas Bellinger 			continue;
2399e48354ceSNicholas Bellinger 
2400e48354ceSNicholas Bellinger 		pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2401e48354ceSNicholas Bellinger 		conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2402e48354ceSNicholas Bellinger 	}
2403e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
2404e48354ceSNicholas Bellinger 
2405e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2406e48354ceSNicholas Bellinger 
2407e48354ceSNicholas Bellinger 	return 0;
2408e48354ceSNicholas Bellinger }
2409e48354ceSNicholas Bellinger 
iscsit_logout_closeconnection(struct iscsit_cmd * cmd,struct iscsit_conn * conn)2410be36d683SMax Gurtovoy int iscsit_logout_closeconnection(struct iscsit_cmd *cmd, struct iscsit_conn *conn)
2411e48354ceSNicholas Bellinger {
2412be36d683SMax Gurtovoy 	struct iscsit_conn *l_conn;
24130873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
2414e48354ceSNicholas Bellinger 
2415e48354ceSNicholas Bellinger 	pr_debug("Received logout request CLOSECONNECTION for CID:"
2416e48354ceSNicholas Bellinger 		" %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2417e48354ceSNicholas Bellinger 
2418e48354ceSNicholas Bellinger 	/*
2419e48354ceSNicholas Bellinger 	 * A Logout Request with a CLOSECONNECTION reason code for a CID
2420e48354ceSNicholas Bellinger 	 * can arrive on a connection with a differing CID.
2421e48354ceSNicholas Bellinger 	 */
2422e48354ceSNicholas Bellinger 	if (conn->cid == cmd->logout_cid) {
2423e48354ceSNicholas Bellinger 		spin_lock_bh(&conn->state_lock);
2424e48354ceSNicholas Bellinger 		pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2425e48354ceSNicholas Bellinger 		conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2426e48354ceSNicholas Bellinger 
2427e48354ceSNicholas Bellinger 		atomic_set(&conn->conn_logout_remove, 1);
2428e48354ceSNicholas Bellinger 		conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2429e48354ceSNicholas Bellinger 		iscsit_inc_conn_usage_count(conn);
2430e48354ceSNicholas Bellinger 
2431e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->state_lock);
2432e48354ceSNicholas Bellinger 	} else {
2433e48354ceSNicholas Bellinger 		/*
2434e48354ceSNicholas Bellinger 		 * Handle all different cid CLOSECONNECTION requests in
2435e48354ceSNicholas Bellinger 		 * iscsit_logout_post_handler_diffcid() as to give enough
2436e48354ceSNicholas Bellinger 		 * time for any non immediate command's CmdSN to be
2437e48354ceSNicholas Bellinger 		 * acknowledged on the connection in question.
2438e48354ceSNicholas Bellinger 		 *
2439e48354ceSNicholas Bellinger 		 * Here we simply make sure the CID is still around.
2440e48354ceSNicholas Bellinger 		 */
2441e48354ceSNicholas Bellinger 		l_conn = iscsit_get_conn_from_cid(sess,
2442e48354ceSNicholas Bellinger 				cmd->logout_cid);
2443e48354ceSNicholas Bellinger 		if (!l_conn) {
2444e48354ceSNicholas Bellinger 			cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2445e48354ceSNicholas Bellinger 			iscsit_add_cmd_to_response_queue(cmd, conn,
2446e48354ceSNicholas Bellinger 					cmd->i_state);
2447e48354ceSNicholas Bellinger 			return 0;
2448e48354ceSNicholas Bellinger 		}
2449e48354ceSNicholas Bellinger 
2450e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(l_conn);
2451e48354ceSNicholas Bellinger 	}
2452e48354ceSNicholas Bellinger 
2453e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2454e48354ceSNicholas Bellinger 
2455e48354ceSNicholas Bellinger 	return 0;
2456e48354ceSNicholas Bellinger }
2457e48354ceSNicholas Bellinger 
iscsit_logout_removeconnforrecovery(struct iscsit_cmd * cmd,struct iscsit_conn * conn)2458be36d683SMax Gurtovoy int iscsit_logout_removeconnforrecovery(struct iscsit_cmd *cmd, struct iscsit_conn *conn)
2459e48354ceSNicholas Bellinger {
24600873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
2461e48354ceSNicholas Bellinger 
2462e48354ceSNicholas Bellinger 	pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2463e48354ceSNicholas Bellinger 		" CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2464e48354ceSNicholas Bellinger 
2465e48354ceSNicholas Bellinger 	if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2466e48354ceSNicholas Bellinger 		pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2467e48354ceSNicholas Bellinger 			" while ERL!=2.\n");
2468e48354ceSNicholas Bellinger 		cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2469e48354ceSNicholas Bellinger 		iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2470e48354ceSNicholas Bellinger 		return 0;
2471e48354ceSNicholas Bellinger 	}
2472e48354ceSNicholas Bellinger 
2473e48354ceSNicholas Bellinger 	if (conn->cid == cmd->logout_cid) {
2474e48354ceSNicholas Bellinger 		pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2475e48354ceSNicholas Bellinger 			" with CID: %hu on CID: %hu, implementation error.\n",
2476e48354ceSNicholas Bellinger 				cmd->logout_cid, conn->cid);
2477e48354ceSNicholas Bellinger 		cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2478e48354ceSNicholas Bellinger 		iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2479e48354ceSNicholas Bellinger 		return 0;
2480e48354ceSNicholas Bellinger 	}
2481e48354ceSNicholas Bellinger 
2482e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2483e48354ceSNicholas Bellinger 
2484e48354ceSNicholas Bellinger 	return 0;
2485e48354ceSNicholas Bellinger }
2486e48354ceSNicholas Bellinger 
24873e1c81a9SNicholas Bellinger int
iscsit_handle_logout_cmd(struct iscsit_conn * conn,struct iscsit_cmd * cmd,unsigned char * buf)2488be36d683SMax Gurtovoy iscsit_handle_logout_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
2489e48354ceSNicholas Bellinger 			unsigned char *buf)
2490e48354ceSNicholas Bellinger {
2491e48354ceSNicholas Bellinger 	int cmdsn_ret, logout_remove = 0;
2492e48354ceSNicholas Bellinger 	u8 reason_code = 0;
2493e48354ceSNicholas Bellinger 	struct iscsi_logout *hdr;
2494e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2495e48354ceSNicholas Bellinger 
2496e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_logout *) buf;
2497e48354ceSNicholas Bellinger 	reason_code		= (hdr->flags & 0x7f);
2498e48354ceSNicholas Bellinger 
2499e48354ceSNicholas Bellinger 	if (tiqn) {
2500e48354ceSNicholas Bellinger 		spin_lock(&tiqn->logout_stats.lock);
2501e48354ceSNicholas Bellinger 		if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2502e48354ceSNicholas Bellinger 			tiqn->logout_stats.normal_logouts++;
2503e48354ceSNicholas Bellinger 		else
2504e48354ceSNicholas Bellinger 			tiqn->logout_stats.abnormal_logouts++;
2505e48354ceSNicholas Bellinger 		spin_unlock(&tiqn->logout_stats.lock);
2506e48354ceSNicholas Bellinger 	}
2507e48354ceSNicholas Bellinger 
2508e48354ceSNicholas Bellinger 	pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2509e48354ceSNicholas Bellinger 		" ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2510e48354ceSNicholas Bellinger 		hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2511e48354ceSNicholas Bellinger 		hdr->cid, conn->cid);
2512e48354ceSNicholas Bellinger 
2513e48354ceSNicholas Bellinger 	if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2514e48354ceSNicholas Bellinger 		pr_err("Received logout request on connection that"
2515e48354ceSNicholas Bellinger 			" is not in logged in state, ignoring request.\n");
2516aafc9d15SNicholas Bellinger 		iscsit_free_cmd(cmd, false);
2517e48354ceSNicholas Bellinger 		return 0;
2518e48354ceSNicholas Bellinger 	}
2519e48354ceSNicholas Bellinger 
2520e48354ceSNicholas Bellinger 	cmd->iscsi_opcode       = ISCSI_OP_LOGOUT;
2521e48354ceSNicholas Bellinger 	cmd->i_state            = ISTATE_SEND_LOGOUTRSP;
2522e48354ceSNicholas Bellinger 	cmd->immediate_cmd      = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2523e48354ceSNicholas Bellinger 	conn->sess->init_task_tag = cmd->init_task_tag  = hdr->itt;
2524e48354ceSNicholas Bellinger 	cmd->targ_xfer_tag      = 0xFFFFFFFF;
252550e5c87dSChristoph Hellwig 	cmd->cmd_sn             = be32_to_cpu(hdr->cmdsn);
252650e5c87dSChristoph Hellwig 	cmd->exp_stat_sn        = be32_to_cpu(hdr->exp_statsn);
252750e5c87dSChristoph Hellwig 	cmd->logout_cid         = be16_to_cpu(hdr->cid);
2528e48354ceSNicholas Bellinger 	cmd->logout_reason      = reason_code;
2529e48354ceSNicholas Bellinger 	cmd->data_direction     = DMA_NONE;
2530e48354ceSNicholas Bellinger 
2531e48354ceSNicholas Bellinger 	/*
2532e48354ceSNicholas Bellinger 	 * We need to sleep in these cases (by returning 1) until the Logout
2533e48354ceSNicholas Bellinger 	 * Response gets sent in the tx thread.
2534e48354ceSNicholas Bellinger 	 */
2535e48354ceSNicholas Bellinger 	if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2536e48354ceSNicholas Bellinger 	   ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
253750e5c87dSChristoph Hellwig 	    be16_to_cpu(hdr->cid) == conn->cid))
2538e48354ceSNicholas Bellinger 		logout_remove = 1;
2539e48354ceSNicholas Bellinger 
2540e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
25412fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2542e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
2543e48354ceSNicholas Bellinger 
2544e48354ceSNicholas Bellinger 	if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
254550e5c87dSChristoph Hellwig 		iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2546e48354ceSNicholas Bellinger 
2547e48354ceSNicholas Bellinger 	/*
2548e48354ceSNicholas Bellinger 	 * Immediate commands are executed, well, immediately.
2549e48354ceSNicholas Bellinger 	 * Non-Immediate Logout Commands are executed in CmdSN order.
2550e48354ceSNicholas Bellinger 	 */
2551c6037cc5SAndy Grover 	if (cmd->immediate_cmd) {
2552e48354ceSNicholas Bellinger 		int ret = iscsit_execute_cmd(cmd, 0);
2553e48354ceSNicholas Bellinger 
2554e48354ceSNicholas Bellinger 		if (ret < 0)
2555e48354ceSNicholas Bellinger 			return ret;
2556e48354ceSNicholas Bellinger 	} else {
2557561bf158SNicholas Bellinger 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
2558ba159914SNicholas Bellinger 		if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
2559e48354ceSNicholas Bellinger 			logout_remove = 0;
2560ba159914SNicholas Bellinger 		else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2561ba159914SNicholas Bellinger 			return -1;
2562e48354ceSNicholas Bellinger 	}
2563e48354ceSNicholas Bellinger 
2564e48354ceSNicholas Bellinger 	return logout_remove;
2565e48354ceSNicholas Bellinger }
25663e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_handle_logout_cmd);
2567e48354ceSNicholas Bellinger 
iscsit_handle_snack(struct iscsit_conn * conn,unsigned char * buf)2568d2faaefbSVarun Prakash int iscsit_handle_snack(
2569be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
2570e48354ceSNicholas Bellinger 	unsigned char *buf)
2571e48354ceSNicholas Bellinger {
2572e48354ceSNicholas Bellinger 	struct iscsi_snack *hdr;
2573e48354ceSNicholas Bellinger 
2574e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_snack *) buf;
2575e48354ceSNicholas Bellinger 	hdr->flags		&= ~ISCSI_FLAG_CMD_FINAL;
2576e48354ceSNicholas Bellinger 
2577e48354ceSNicholas Bellinger 	pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2578e48354ceSNicholas Bellinger 		" 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2579e48354ceSNicholas Bellinger 		" CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2580e48354ceSNicholas Bellinger 			hdr->begrun, hdr->runlength, conn->cid);
2581e48354ceSNicholas Bellinger 
2582e48354ceSNicholas Bellinger 	if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2583e48354ceSNicholas Bellinger 		pr_err("Initiator sent SNACK request while in"
2584e48354ceSNicholas Bellinger 			" ErrorRecoveryLevel=0.\n");
2585ba159914SNicholas Bellinger 		return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2586ba159914SNicholas Bellinger 					 buf);
2587e48354ceSNicholas Bellinger 	}
2588e48354ceSNicholas Bellinger 	/*
2589e48354ceSNicholas Bellinger 	 * SNACK_DATA and SNACK_R2T are both 0,  so check which function to
2590e48354ceSNicholas Bellinger 	 * call from inside iscsi_send_recovery_datain_or_r2t().
2591e48354ceSNicholas Bellinger 	 */
2592e48354ceSNicholas Bellinger 	switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2593e48354ceSNicholas Bellinger 	case 0:
2594e48354ceSNicholas Bellinger 		return iscsit_handle_recovery_datain_or_r2t(conn, buf,
259550e5c87dSChristoph Hellwig 			hdr->itt,
259650e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->ttt),
259750e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->begrun),
259850e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->runlength));
2599e48354ceSNicholas Bellinger 	case ISCSI_FLAG_SNACK_TYPE_STATUS:
260050e5c87dSChristoph Hellwig 		return iscsit_handle_status_snack(conn, hdr->itt,
260150e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->ttt),
260250e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
2603e48354ceSNicholas Bellinger 	case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
260450e5c87dSChristoph Hellwig 		return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
260550e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->begrun),
260650e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->runlength));
2607e48354ceSNicholas Bellinger 	case ISCSI_FLAG_SNACK_TYPE_RDATA:
2608e48354ceSNicholas Bellinger 		/* FIXME: Support R-Data SNACK */
2609e48354ceSNicholas Bellinger 		pr_err("R-Data SNACK Not Supported.\n");
2610ba159914SNicholas Bellinger 		return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2611ba159914SNicholas Bellinger 					 buf);
2612e48354ceSNicholas Bellinger 	default:
2613e48354ceSNicholas Bellinger 		pr_err("Unknown SNACK type 0x%02x, protocol"
2614e48354ceSNicholas Bellinger 			" error.\n", hdr->flags & 0x0f);
2615ba159914SNicholas Bellinger 		return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2616ba159914SNicholas Bellinger 					 buf);
2617e48354ceSNicholas Bellinger 	}
2618e48354ceSNicholas Bellinger 
2619e48354ceSNicholas Bellinger 	return 0;
2620e48354ceSNicholas Bellinger }
2621d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_handle_snack);
2622e48354ceSNicholas Bellinger 
iscsit_rx_thread_wait_for_tcp(struct iscsit_conn * conn)2623be36d683SMax Gurtovoy static void iscsit_rx_thread_wait_for_tcp(struct iscsit_conn *conn)
2624e48354ceSNicholas Bellinger {
2625e48354ceSNicholas Bellinger 	if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2626e48354ceSNicholas Bellinger 	    (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2627e48354ceSNicholas Bellinger 		wait_for_completion_interruptible_timeout(
2628e48354ceSNicholas Bellinger 					&conn->rx_half_close_comp,
2629e48354ceSNicholas Bellinger 					ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2630e48354ceSNicholas Bellinger 	}
2631e48354ceSNicholas Bellinger }
2632e48354ceSNicholas Bellinger 
iscsit_handle_immediate_data(struct iscsit_cmd * cmd,struct iscsi_scsi_req * hdr,u32 length)2633e48354ceSNicholas Bellinger static int iscsit_handle_immediate_data(
263466cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
26353e1c81a9SNicholas Bellinger 	struct iscsi_scsi_req *hdr,
2636e48354ceSNicholas Bellinger 	u32 length)
2637e48354ceSNicholas Bellinger {
2638e48354ceSNicholas Bellinger 	int iov_ret, rx_got = 0, rx_size = 0;
2639e48354ceSNicholas Bellinger 	u32 checksum, iov_count = 0, padding = 0;
2640be36d683SMax Gurtovoy 	struct iscsit_conn *conn = cmd->conn;
2641e48354ceSNicholas Bellinger 	struct kvec *iov;
26420ca650c1SBart Van Assche 	void *overflow_buf = NULL;
2643e48354ceSNicholas Bellinger 
26440ca650c1SBart Van Assche 	BUG_ON(cmd->write_data_done > cmd->se_cmd.data_length);
26450ca650c1SBart Van Assche 	rx_size = min(cmd->se_cmd.data_length - cmd->write_data_done, length);
26462e39f1c9SBart Van Assche 	iov_ret = iscsit_map_iovec(cmd, cmd->iov_data,
26472e39f1c9SBart Van Assche 				   cmd->orig_iov_data_count - 2,
26482e39f1c9SBart Van Assche 				   cmd->write_data_done, rx_size);
2649e48354ceSNicholas Bellinger 	if (iov_ret < 0)
2650e48354ceSNicholas Bellinger 		return IMMEDIATE_DATA_CANNOT_RECOVER;
2651e48354ceSNicholas Bellinger 
2652e48354ceSNicholas Bellinger 	iov_count = iov_ret;
2653e48354ceSNicholas Bellinger 	iov = &cmd->iov_data[0];
26540ca650c1SBart Van Assche 	if (rx_size < length) {
26550ca650c1SBart Van Assche 		/*
26560ca650c1SBart Van Assche 		 * Special case: length of immediate data exceeds the data
26570ca650c1SBart Van Assche 		 * buffer size derived from the CDB.
26580ca650c1SBart Van Assche 		 */
26590ca650c1SBart Van Assche 		overflow_buf = kmalloc(length - rx_size, GFP_KERNEL);
26600ca650c1SBart Van Assche 		if (!overflow_buf) {
26610ca650c1SBart Van Assche 			iscsit_unmap_iovec(cmd);
26620ca650c1SBart Van Assche 			return IMMEDIATE_DATA_CANNOT_RECOVER;
26630ca650c1SBart Van Assche 		}
26640ca650c1SBart Van Assche 		cmd->overflow_buf = overflow_buf;
26650ca650c1SBart Van Assche 		iov[iov_count].iov_base = overflow_buf;
26660ca650c1SBart Van Assche 		iov[iov_count].iov_len = length - rx_size;
26670ca650c1SBart Van Assche 		iov_count++;
26680ca650c1SBart Van Assche 		rx_size = length;
26690ca650c1SBart Van Assche 	}
2670e48354ceSNicholas Bellinger 
2671e48354ceSNicholas Bellinger 	padding = ((-length) & 3);
2672e48354ceSNicholas Bellinger 	if (padding != 0) {
2673e48354ceSNicholas Bellinger 		iov[iov_count].iov_base	= cmd->pad_bytes;
2674e48354ceSNicholas Bellinger 		iov[iov_count++].iov_len = padding;
2675e48354ceSNicholas Bellinger 		rx_size += padding;
2676e48354ceSNicholas Bellinger 	}
2677e48354ceSNicholas Bellinger 
2678e48354ceSNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
2679e48354ceSNicholas Bellinger 		iov[iov_count].iov_base		= &checksum;
2680e48354ceSNicholas Bellinger 		iov[iov_count++].iov_len	= ISCSI_CRC_LEN;
2681e48354ceSNicholas Bellinger 		rx_size += ISCSI_CRC_LEN;
2682e48354ceSNicholas Bellinger 	}
2683e48354ceSNicholas Bellinger 
26842e39f1c9SBart Van Assche 	WARN_ON_ONCE(iov_count > cmd->orig_iov_data_count);
2685e48354ceSNicholas Bellinger 	rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2686e48354ceSNicholas Bellinger 
2687e48354ceSNicholas Bellinger 	iscsit_unmap_iovec(cmd);
2688e48354ceSNicholas Bellinger 
2689e48354ceSNicholas Bellinger 	if (rx_got != rx_size) {
2690e48354ceSNicholas Bellinger 		iscsit_rx_thread_wait_for_tcp(conn);
2691e48354ceSNicholas Bellinger 		return IMMEDIATE_DATA_CANNOT_RECOVER;
2692e48354ceSNicholas Bellinger 	}
2693e48354ceSNicholas Bellinger 
2694e48354ceSNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
2695e48354ceSNicholas Bellinger 		u32 data_crc;
2696e48354ceSNicholas Bellinger 
269769110e3cSHerbert Xu 		data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
2698e48354ceSNicholas Bellinger 						    cmd->write_data_done, length, padding,
2699e48354ceSNicholas Bellinger 						    cmd->pad_bytes);
2700e48354ceSNicholas Bellinger 
2701e48354ceSNicholas Bellinger 		if (checksum != data_crc) {
2702e48354ceSNicholas Bellinger 			pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2703e48354ceSNicholas Bellinger 				" does not match computed 0x%08x\n", checksum,
2704e48354ceSNicholas Bellinger 				data_crc);
2705e48354ceSNicholas Bellinger 
2706e48354ceSNicholas Bellinger 			if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2707e48354ceSNicholas Bellinger 				pr_err("Unable to recover from"
2708e48354ceSNicholas Bellinger 					" Immediate Data digest failure while"
2709e48354ceSNicholas Bellinger 					" in ERL=0.\n");
2710ba159914SNicholas Bellinger 				iscsit_reject_cmd(cmd,
2711e48354ceSNicholas Bellinger 						ISCSI_REASON_DATA_DIGEST_ERROR,
2712ba159914SNicholas Bellinger 						(unsigned char *)hdr);
2713e48354ceSNicholas Bellinger 				return IMMEDIATE_DATA_CANNOT_RECOVER;
2714e48354ceSNicholas Bellinger 			} else {
2715ba159914SNicholas Bellinger 				iscsit_reject_cmd(cmd,
2716e48354ceSNicholas Bellinger 						ISCSI_REASON_DATA_DIGEST_ERROR,
2717ba159914SNicholas Bellinger 						(unsigned char *)hdr);
2718e48354ceSNicholas Bellinger 				return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2719e48354ceSNicholas Bellinger 			}
2720e48354ceSNicholas Bellinger 		} else {
2721e48354ceSNicholas Bellinger 			pr_debug("Got CRC32C DataDigest 0x%08x for"
2722e48354ceSNicholas Bellinger 				" %u bytes of Immediate Data\n", checksum,
2723e48354ceSNicholas Bellinger 				length);
2724e48354ceSNicholas Bellinger 		}
2725e48354ceSNicholas Bellinger 	}
2726e48354ceSNicholas Bellinger 
2727e48354ceSNicholas Bellinger 	cmd->write_data_done += length;
2728e48354ceSNicholas Bellinger 
2729ebf1d95cSAndy Grover 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
2730e48354ceSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
2731e48354ceSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2732e48354ceSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2733e48354ceSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
2734e48354ceSNicholas Bellinger 	}
2735e48354ceSNicholas Bellinger 
2736e48354ceSNicholas Bellinger 	return IMMEDIATE_DATA_NORMAL_OPERATION;
2737e48354ceSNicholas Bellinger }
2738e48354ceSNicholas Bellinger 
2739e48354ceSNicholas Bellinger /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2740e48354ceSNicholas Bellinger 	with active network interface */
iscsit_build_conn_drop_async_message(struct iscsit_conn * conn)2741be36d683SMax Gurtovoy static void iscsit_build_conn_drop_async_message(struct iscsit_conn *conn)
2742e48354ceSNicholas Bellinger {
274366cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
2744be36d683SMax Gurtovoy 	struct iscsit_conn *conn_p;
2745d444edc6SNicholas Bellinger 	bool found = false;
2746e48354ceSNicholas Bellinger 
2747618baaf7SBart Van Assche 	lockdep_assert_held(&conn->sess->conn_lock);
2748618baaf7SBart Van Assche 
2749e48354ceSNicholas Bellinger 	/*
2750e48354ceSNicholas Bellinger 	 * Only send a Asynchronous Message on connections whos network
2751e48354ceSNicholas Bellinger 	 * interface is still functional.
2752e48354ceSNicholas Bellinger 	 */
2753e48354ceSNicholas Bellinger 	list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2754e48354ceSNicholas Bellinger 		if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2755e48354ceSNicholas Bellinger 			iscsit_inc_conn_usage_count(conn_p);
2756d444edc6SNicholas Bellinger 			found = true;
2757e48354ceSNicholas Bellinger 			break;
2758e48354ceSNicholas Bellinger 		}
2759e48354ceSNicholas Bellinger 	}
2760e48354ceSNicholas Bellinger 
2761d444edc6SNicholas Bellinger 	if (!found)
2762e48354ceSNicholas Bellinger 		return;
2763e48354ceSNicholas Bellinger 
2764676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
2765e48354ceSNicholas Bellinger 	if (!cmd) {
2766e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn_p);
2767e48354ceSNicholas Bellinger 		return;
2768e48354ceSNicholas Bellinger 	}
2769e48354ceSNicholas Bellinger 
2770e48354ceSNicholas Bellinger 	cmd->logout_cid = conn->cid;
2771e48354ceSNicholas Bellinger 	cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2772e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_ASYNCMSG;
2773e48354ceSNicholas Bellinger 
2774e48354ceSNicholas Bellinger 	spin_lock_bh(&conn_p->cmd_lock);
27752fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
2776e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn_p->cmd_lock);
2777e48354ceSNicholas Bellinger 
2778e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2779e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn_p);
2780e48354ceSNicholas Bellinger }
2781e48354ceSNicholas Bellinger 
iscsit_send_conn_drop_async_message(struct iscsit_cmd * cmd,struct iscsit_conn * conn)2782e48354ceSNicholas Bellinger static int iscsit_send_conn_drop_async_message(
278366cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
2784be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
2785e48354ceSNicholas Bellinger {
2786e48354ceSNicholas Bellinger 	struct iscsi_async *hdr;
2787e48354ceSNicholas Bellinger 
2788e48354ceSNicholas Bellinger 	cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2789e48354ceSNicholas Bellinger 
2790e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_async *) cmd->pdu;
2791e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_ASYNC_EVENT;
2792e48354ceSNicholas Bellinger 	hdr->flags		= ISCSI_FLAG_CMD_FINAL;
279366c7db68SChristoph Hellwig 	cmd->init_task_tag	= RESERVED_ITT;
2794e48354ceSNicholas Bellinger 	cmd->targ_xfer_tag	= 0xFFFFFFFF;
2795e48354ceSNicholas Bellinger 	put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2796e48354ceSNicholas Bellinger 	cmd->stat_sn		= conn->stat_sn++;
2797e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
2798e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
2799109e2381SRoland Dreier 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2800e48354ceSNicholas Bellinger 	hdr->async_event	= ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2801e48354ceSNicholas Bellinger 	hdr->param1		= cpu_to_be16(cmd->logout_cid);
2802e48354ceSNicholas Bellinger 	hdr->param2		= cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2803e48354ceSNicholas Bellinger 	hdr->param3		= cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2804e48354ceSNicholas Bellinger 
2805e48354ceSNicholas Bellinger 	pr_debug("Sending Connection Dropped Async Message StatSN:"
2806e48354ceSNicholas Bellinger 		" 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2807e48354ceSNicholas Bellinger 			cmd->logout_cid, conn->cid);
28082854bb23SVarun Prakash 
28092854bb23SVarun Prakash 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
2810e48354ceSNicholas Bellinger }
2811e48354ceSNicholas Bellinger 
iscsit_tx_thread_wait_for_tcp(struct iscsit_conn * conn)2812be36d683SMax Gurtovoy static void iscsit_tx_thread_wait_for_tcp(struct iscsit_conn *conn)
28136f3c0e69SAndy Grover {
28146f3c0e69SAndy Grover 	if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
28156f3c0e69SAndy Grover 	    (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
28166f3c0e69SAndy Grover 		wait_for_completion_interruptible_timeout(
28176f3c0e69SAndy Grover 					&conn->tx_half_close_comp,
28186f3c0e69SAndy Grover 					ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
28196f3c0e69SAndy Grover 	}
28206f3c0e69SAndy Grover }
28216f3c0e69SAndy Grover 
2822d2faaefbSVarun Prakash void
iscsit_build_datain_pdu(struct iscsit_cmd * cmd,struct iscsit_conn * conn,struct iscsi_datain * datain,struct iscsi_data_rsp * hdr,bool set_statsn)2823be36d683SMax Gurtovoy iscsit_build_datain_pdu(struct iscsit_cmd *cmd, struct iscsit_conn *conn,
28242ec5a8c1SNicholas Bellinger 			struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
28252ec5a8c1SNicholas Bellinger 			bool set_statsn)
2826e48354ceSNicholas Bellinger {
28272ec5a8c1SNicholas Bellinger 	hdr->opcode		= ISCSI_OP_SCSI_DATA_IN;
28282ec5a8c1SNicholas Bellinger 	hdr->flags		= datain->flags;
28292ec5a8c1SNicholas Bellinger 	if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
28302ec5a8c1SNicholas Bellinger 		if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
28312ec5a8c1SNicholas Bellinger 			hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
28322ec5a8c1SNicholas Bellinger 			hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
28332ec5a8c1SNicholas Bellinger 		} else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
28342ec5a8c1SNicholas Bellinger 			hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
28352ec5a8c1SNicholas Bellinger 			hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
28362ec5a8c1SNicholas Bellinger 		}
28372ec5a8c1SNicholas Bellinger 	}
28382ec5a8c1SNicholas Bellinger 	hton24(hdr->dlength, datain->length);
28392ec5a8c1SNicholas Bellinger 	if (hdr->flags & ISCSI_FLAG_DATA_ACK)
28402ec5a8c1SNicholas Bellinger 		int_to_scsilun(cmd->se_cmd.orig_fe_lun,
28412ec5a8c1SNicholas Bellinger 				(struct scsi_lun *)&hdr->lun);
28422ec5a8c1SNicholas Bellinger 	else
28432ec5a8c1SNicholas Bellinger 		put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
28442ec5a8c1SNicholas Bellinger 
28452ec5a8c1SNicholas Bellinger 	hdr->itt		= cmd->init_task_tag;
28462ec5a8c1SNicholas Bellinger 
28472ec5a8c1SNicholas Bellinger 	if (hdr->flags & ISCSI_FLAG_DATA_ACK)
28482ec5a8c1SNicholas Bellinger 		hdr->ttt		= cpu_to_be32(cmd->targ_xfer_tag);
28492ec5a8c1SNicholas Bellinger 	else
28502ec5a8c1SNicholas Bellinger 		hdr->ttt		= cpu_to_be32(0xFFFFFFFF);
28512ec5a8c1SNicholas Bellinger 	if (set_statsn)
28522ec5a8c1SNicholas Bellinger 		hdr->statsn		= cpu_to_be32(cmd->stat_sn);
28532ec5a8c1SNicholas Bellinger 	else
28542ec5a8c1SNicholas Bellinger 		hdr->statsn		= cpu_to_be32(0xFFFFFFFF);
28552ec5a8c1SNicholas Bellinger 
28562ec5a8c1SNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
2857109e2381SRoland Dreier 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
28582ec5a8c1SNicholas Bellinger 	hdr->datasn		= cpu_to_be32(datain->data_sn);
28592ec5a8c1SNicholas Bellinger 	hdr->offset		= cpu_to_be32(datain->offset);
28602ec5a8c1SNicholas Bellinger 
28612ec5a8c1SNicholas Bellinger 	pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
28622ec5a8c1SNicholas Bellinger 		" DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
28632ec5a8c1SNicholas Bellinger 		cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
28642ec5a8c1SNicholas Bellinger 		ntohl(hdr->offset), datain->length, conn->cid);
28652ec5a8c1SNicholas Bellinger }
2866d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_build_datain_pdu);
28672ec5a8c1SNicholas Bellinger 
iscsit_send_datain(struct iscsit_cmd * cmd,struct iscsit_conn * conn)2868be36d683SMax Gurtovoy static int iscsit_send_datain(struct iscsit_cmd *cmd, struct iscsit_conn *conn)
28692ec5a8c1SNicholas Bellinger {
28702ec5a8c1SNicholas Bellinger 	struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
2871e48354ceSNicholas Bellinger 	struct iscsi_datain datain;
2872e48354ceSNicholas Bellinger 	struct iscsi_datain_req *dr;
28732854bb23SVarun Prakash 	int eodr = 0, ret;
28742ec5a8c1SNicholas Bellinger 	bool set_statsn = false;
2875e48354ceSNicholas Bellinger 
2876e48354ceSNicholas Bellinger 	memset(&datain, 0, sizeof(struct iscsi_datain));
2877e48354ceSNicholas Bellinger 	dr = iscsit_get_datain_values(cmd, &datain);
2878e48354ceSNicholas Bellinger 	if (!dr) {
2879e48354ceSNicholas Bellinger 		pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2880e48354ceSNicholas Bellinger 				cmd->init_task_tag);
2881e48354ceSNicholas Bellinger 		return -1;
2882e48354ceSNicholas Bellinger 	}
2883e48354ceSNicholas Bellinger 	/*
2884e48354ceSNicholas Bellinger 	 * Be paranoid and double check the logic for now.
2885e48354ceSNicholas Bellinger 	 */
2886ebf1d95cSAndy Grover 	if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
2887e48354ceSNicholas Bellinger 		pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2888e48354ceSNicholas Bellinger 			" datain.length: %u exceeds cmd->data_length: %u\n",
2889e48354ceSNicholas Bellinger 			cmd->init_task_tag, datain.offset, datain.length,
2890ebf1d95cSAndy Grover 			cmd->se_cmd.data_length);
2891e48354ceSNicholas Bellinger 		return -1;
2892e48354ceSNicholas Bellinger 	}
2893e48354ceSNicholas Bellinger 
289404f3b31bSNicholas Bellinger 	atomic_long_add(datain.length, &conn->sess->tx_data_octets);
2895e48354ceSNicholas Bellinger 	/*
2896e48354ceSNicholas Bellinger 	 * Special case for successfully execution w/ both DATAIN
2897e48354ceSNicholas Bellinger 	 * and Sense Data.
2898e48354ceSNicholas Bellinger 	 */
2899e48354ceSNicholas Bellinger 	if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2900e48354ceSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2901e48354ceSNicholas Bellinger 		datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2902e48354ceSNicholas Bellinger 	else {
2903e48354ceSNicholas Bellinger 		if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2904e48354ceSNicholas Bellinger 		    (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2905e48354ceSNicholas Bellinger 			iscsit_increment_maxcmdsn(cmd, conn->sess);
2906e48354ceSNicholas Bellinger 			cmd->stat_sn = conn->stat_sn++;
29072ec5a8c1SNicholas Bellinger 			set_statsn = true;
2908e48354ceSNicholas Bellinger 		} else if (dr->dr_complete ==
2909e48354ceSNicholas Bellinger 			   DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
29102ec5a8c1SNicholas Bellinger 			set_statsn = true;
2911e48354ceSNicholas Bellinger 	}
2912e48354ceSNicholas Bellinger 
29132ec5a8c1SNicholas Bellinger 	iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
2914e48354ceSNicholas Bellinger 
29152854bb23SVarun Prakash 	ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
29162854bb23SVarun Prakash 	if (ret < 0)
29176f3c0e69SAndy Grover 		return ret;
29186f3c0e69SAndy Grover 
2919e48354ceSNicholas Bellinger 	if (dr->dr_complete) {
29206f3c0e69SAndy Grover 		eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
2921e48354ceSNicholas Bellinger 				2 : 1;
2922e48354ceSNicholas Bellinger 		iscsit_free_datain_req(cmd, dr);
2923e48354ceSNicholas Bellinger 	}
2924e48354ceSNicholas Bellinger 
29256f3c0e69SAndy Grover 	return eodr;
2926e48354ceSNicholas Bellinger }
2927e48354ceSNicholas Bellinger 
29282ec5a8c1SNicholas Bellinger int
iscsit_build_logout_rsp(struct iscsit_cmd * cmd,struct iscsit_conn * conn,struct iscsi_logout_rsp * hdr)2929be36d683SMax Gurtovoy iscsit_build_logout_rsp(struct iscsit_cmd *cmd, struct iscsit_conn *conn,
29302ec5a8c1SNicholas Bellinger 			struct iscsi_logout_rsp *hdr)
2931e48354ceSNicholas Bellinger {
2932be36d683SMax Gurtovoy 	struct iscsit_conn *logout_conn = NULL;
2933e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr = NULL;
29340873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
2935e48354ceSNicholas Bellinger 	/*
2936e48354ceSNicholas Bellinger 	 * The actual shutting down of Sessions and/or Connections
2937e48354ceSNicholas Bellinger 	 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2938e48354ceSNicholas Bellinger 	 * is done in scsi_logout_post_handler().
2939e48354ceSNicholas Bellinger 	 */
2940e48354ceSNicholas Bellinger 	switch (cmd->logout_reason) {
2941e48354ceSNicholas Bellinger 	case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2942e48354ceSNicholas Bellinger 		pr_debug("iSCSI session logout successful, setting"
2943e48354ceSNicholas Bellinger 			" logout response to ISCSI_LOGOUT_SUCCESS.\n");
2944e48354ceSNicholas Bellinger 		cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2945e48354ceSNicholas Bellinger 		break;
2946e48354ceSNicholas Bellinger 	case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2947e48354ceSNicholas Bellinger 		if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2948e48354ceSNicholas Bellinger 			break;
2949e48354ceSNicholas Bellinger 		/*
2950e48354ceSNicholas Bellinger 		 * For CLOSECONNECTION logout requests carrying
2951e48354ceSNicholas Bellinger 		 * a matching logout CID -> local CID, the reference
2952e48354ceSNicholas Bellinger 		 * for the local CID will have been incremented in
2953e48354ceSNicholas Bellinger 		 * iscsi_logout_closeconnection().
2954e48354ceSNicholas Bellinger 		 *
2955e48354ceSNicholas Bellinger 		 * For CLOSECONNECTION logout requests carrying
2956e48354ceSNicholas Bellinger 		 * a different CID than the connection it arrived
2957e48354ceSNicholas Bellinger 		 * on, the connection responding to cmd->logout_cid
2958e48354ceSNicholas Bellinger 		 * is stopped in iscsit_logout_post_handler_diffcid().
2959e48354ceSNicholas Bellinger 		 */
2960e48354ceSNicholas Bellinger 
2961e48354ceSNicholas Bellinger 		pr_debug("iSCSI CID: %hu logout on CID: %hu"
2962e48354ceSNicholas Bellinger 			" successful.\n", cmd->logout_cid, conn->cid);
2963e48354ceSNicholas Bellinger 		cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2964e48354ceSNicholas Bellinger 		break;
2965e48354ceSNicholas Bellinger 	case ISCSI_LOGOUT_REASON_RECOVERY:
2966e48354ceSNicholas Bellinger 		if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2967e48354ceSNicholas Bellinger 		    (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2968e48354ceSNicholas Bellinger 			break;
2969e48354ceSNicholas Bellinger 		/*
2970e48354ceSNicholas Bellinger 		 * If the connection is still active from our point of view
2971e48354ceSNicholas Bellinger 		 * force connection recovery to occur.
2972e48354ceSNicholas Bellinger 		 */
2973e48354ceSNicholas Bellinger 		logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2974e48354ceSNicholas Bellinger 				cmd->logout_cid);
2975ee1b1b9cSAndy Grover 		if (logout_conn) {
2976e48354ceSNicholas Bellinger 			iscsit_connection_reinstatement_rcfr(logout_conn);
2977e48354ceSNicholas Bellinger 			iscsit_dec_conn_usage_count(logout_conn);
2978e48354ceSNicholas Bellinger 		}
2979e48354ceSNicholas Bellinger 
2980e48354ceSNicholas Bellinger 		cr = iscsit_get_inactive_connection_recovery_entry(
2981e48354ceSNicholas Bellinger 				conn->sess, cmd->logout_cid);
2982e48354ceSNicholas Bellinger 		if (!cr) {
2983e48354ceSNicholas Bellinger 			pr_err("Unable to locate CID: %hu for"
2984e48354ceSNicholas Bellinger 			" REMOVECONNFORRECOVERY Logout Request.\n",
2985e48354ceSNicholas Bellinger 				cmd->logout_cid);
2986e48354ceSNicholas Bellinger 			cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2987e48354ceSNicholas Bellinger 			break;
2988e48354ceSNicholas Bellinger 		}
2989e48354ceSNicholas Bellinger 
2990e48354ceSNicholas Bellinger 		iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2991e48354ceSNicholas Bellinger 
2992e48354ceSNicholas Bellinger 		pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2993e48354ceSNicholas Bellinger 			" for recovery for CID: %hu on CID: %hu successful.\n",
2994e48354ceSNicholas Bellinger 				cmd->logout_cid, conn->cid);
2995e48354ceSNicholas Bellinger 		cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2996e48354ceSNicholas Bellinger 		break;
2997e48354ceSNicholas Bellinger 	default:
2998e48354ceSNicholas Bellinger 		pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2999e48354ceSNicholas Bellinger 				cmd->logout_reason);
3000e48354ceSNicholas Bellinger 		return -1;
3001e48354ceSNicholas Bellinger 	}
3002e48354ceSNicholas Bellinger 
3003e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_LOGOUT_RSP;
3004e48354ceSNicholas Bellinger 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
3005e48354ceSNicholas Bellinger 	hdr->response		= cmd->logout_response;
300666c7db68SChristoph Hellwig 	hdr->itt		= cmd->init_task_tag;
3007e48354ceSNicholas Bellinger 	cmd->stat_sn		= conn->stat_sn++;
3008e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
3009e48354ceSNicholas Bellinger 
3010e48354ceSNicholas Bellinger 	iscsit_increment_maxcmdsn(cmd, conn->sess);
3011e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
3012109e2381SRoland Dreier 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3013e48354ceSNicholas Bellinger 
30142ec5a8c1SNicholas Bellinger 	pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
30152ec5a8c1SNicholas Bellinger 		" 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
30162ec5a8c1SNicholas Bellinger 		cmd->init_task_tag, cmd->stat_sn, hdr->response,
30172ec5a8c1SNicholas Bellinger 		cmd->logout_cid, conn->cid);
30182ec5a8c1SNicholas Bellinger 
30192ec5a8c1SNicholas Bellinger 	return 0;
30202ec5a8c1SNicholas Bellinger }
30212ec5a8c1SNicholas Bellinger EXPORT_SYMBOL(iscsit_build_logout_rsp);
30222ec5a8c1SNicholas Bellinger 
30232ec5a8c1SNicholas Bellinger static int
iscsit_send_logout(struct iscsit_cmd * cmd,struct iscsit_conn * conn)3024be36d683SMax Gurtovoy iscsit_send_logout(struct iscsit_cmd *cmd, struct iscsit_conn *conn)
30252ec5a8c1SNicholas Bellinger {
30262854bb23SVarun Prakash 	int rc;
30272ec5a8c1SNicholas Bellinger 
30282ec5a8c1SNicholas Bellinger 	rc = iscsit_build_logout_rsp(cmd, conn,
30292ec5a8c1SNicholas Bellinger 			(struct iscsi_logout_rsp *)&cmd->pdu[0]);
30302ec5a8c1SNicholas Bellinger 	if (rc < 0)
30312ec5a8c1SNicholas Bellinger 		return rc;
30322ec5a8c1SNicholas Bellinger 
30332854bb23SVarun Prakash 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3034e48354ceSNicholas Bellinger }
3035e48354ceSNicholas Bellinger 
30362ec5a8c1SNicholas Bellinger void
iscsit_build_nopin_rsp(struct iscsit_cmd * cmd,struct iscsit_conn * conn,struct iscsi_nopin * hdr,bool nopout_response)3037be36d683SMax Gurtovoy iscsit_build_nopin_rsp(struct iscsit_cmd *cmd, struct iscsit_conn *conn,
30382ec5a8c1SNicholas Bellinger 		       struct iscsi_nopin *hdr, bool nopout_response)
30392ec5a8c1SNicholas Bellinger {
30402ec5a8c1SNicholas Bellinger 	hdr->opcode		= ISCSI_OP_NOOP_IN;
30412ec5a8c1SNicholas Bellinger 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
30422ec5a8c1SNicholas Bellinger         hton24(hdr->dlength, cmd->buf_ptr_size);
30432ec5a8c1SNicholas Bellinger 	if (nopout_response)
30442ec5a8c1SNicholas Bellinger 		put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
30452ec5a8c1SNicholas Bellinger 	hdr->itt		= cmd->init_task_tag;
30462ec5a8c1SNicholas Bellinger 	hdr->ttt		= cpu_to_be32(cmd->targ_xfer_tag);
30472ec5a8c1SNicholas Bellinger 	cmd->stat_sn		= (nopout_response) ? conn->stat_sn++ :
30482ec5a8c1SNicholas Bellinger 				  conn->stat_sn;
30492ec5a8c1SNicholas Bellinger 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
30502ec5a8c1SNicholas Bellinger 
30512ec5a8c1SNicholas Bellinger 	if (nopout_response)
30522ec5a8c1SNicholas Bellinger 		iscsit_increment_maxcmdsn(cmd, conn->sess);
30532ec5a8c1SNicholas Bellinger 
30542ec5a8c1SNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
3055109e2381SRoland Dreier 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
30562ec5a8c1SNicholas Bellinger 
30572ec5a8c1SNicholas Bellinger 	pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
30582ec5a8c1SNicholas Bellinger 		" StatSN: 0x%08x, Length %u\n", (nopout_response) ?
30593fc6a642SColin Ian King 		"Solicited" : "Unsolicited", cmd->init_task_tag,
30602ec5a8c1SNicholas Bellinger 		cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
30612ec5a8c1SNicholas Bellinger }
30622ec5a8c1SNicholas Bellinger EXPORT_SYMBOL(iscsit_build_nopin_rsp);
30632ec5a8c1SNicholas Bellinger 
3064e48354ceSNicholas Bellinger /*
3065e48354ceSNicholas Bellinger  *	Unsolicited NOPIN, either requesting a response or not.
3066e48354ceSNicholas Bellinger  */
iscsit_send_unsolicited_nopin(struct iscsit_cmd * cmd,struct iscsit_conn * conn,int want_response)3067e48354ceSNicholas Bellinger static int iscsit_send_unsolicited_nopin(
306866cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
3069be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
3070e48354ceSNicholas Bellinger 	int want_response)
3071e48354ceSNicholas Bellinger {
30722ec5a8c1SNicholas Bellinger 	struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
30732854bb23SVarun Prakash 	int ret;
3074e48354ceSNicholas Bellinger 
30752ec5a8c1SNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, hdr, false);
3076e48354ceSNicholas Bellinger 
3077e48354ceSNicholas Bellinger 	pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
3078e48354ceSNicholas Bellinger 		" 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
3079e48354ceSNicholas Bellinger 
30802854bb23SVarun Prakash 	ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
30812854bb23SVarun Prakash 	if (ret < 0)
30826f3c0e69SAndy Grover 		return ret;
30836f3c0e69SAndy Grover 
30846f3c0e69SAndy Grover 	spin_lock_bh(&cmd->istate_lock);
30856f3c0e69SAndy Grover 	cmd->i_state = want_response ?
30866f3c0e69SAndy Grover 		ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
30876f3c0e69SAndy Grover 	spin_unlock_bh(&cmd->istate_lock);
30886f3c0e69SAndy Grover 
3089e48354ceSNicholas Bellinger 	return 0;
3090e48354ceSNicholas Bellinger }
3091e48354ceSNicholas Bellinger 
30922ec5a8c1SNicholas Bellinger static int
iscsit_send_nopin(struct iscsit_cmd * cmd,struct iscsit_conn * conn)3093be36d683SMax Gurtovoy iscsit_send_nopin(struct iscsit_cmd *cmd, struct iscsit_conn *conn)
3094e48354ceSNicholas Bellinger {
30952ec5a8c1SNicholas Bellinger 	struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
30962ec5a8c1SNicholas Bellinger 
30972ec5a8c1SNicholas Bellinger 	iscsit_build_nopin_rsp(cmd, conn, hdr, true);
3098e48354ceSNicholas Bellinger 
3099e48354ceSNicholas Bellinger 	/*
310066cd9d4eSMax Gurtovoy 	 * NOPOUT Ping Data is attached to struct iscsit_cmd->buf_ptr.
310166cd9d4eSMax Gurtovoy 	 * NOPOUT DataSegmentLength is at struct iscsit_cmd->buf_ptr_size.
3102e48354ceSNicholas Bellinger 	 */
31032854bb23SVarun Prakash 	pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
3104e48354ceSNicholas Bellinger 
31052854bb23SVarun Prakash 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
31062854bb23SVarun Prakash 						     cmd->buf_ptr,
31072854bb23SVarun Prakash 						     cmd->buf_ptr_size);
3108e48354ceSNicholas Bellinger }
3109e48354ceSNicholas Bellinger 
iscsit_send_r2t(struct iscsit_cmd * cmd,struct iscsit_conn * conn)31106f3c0e69SAndy Grover static int iscsit_send_r2t(
311166cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
3112be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
3113e48354ceSNicholas Bellinger {
3114e48354ceSNicholas Bellinger 	struct iscsi_r2t *r2t;
3115e48354ceSNicholas Bellinger 	struct iscsi_r2t_rsp *hdr;
31166f3c0e69SAndy Grover 	int ret;
3117e48354ceSNicholas Bellinger 
3118e48354ceSNicholas Bellinger 	r2t = iscsit_get_r2t_from_list(cmd);
3119e48354ceSNicholas Bellinger 	if (!r2t)
3120e48354ceSNicholas Bellinger 		return -1;
3121e48354ceSNicholas Bellinger 
3122e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_r2t_rsp *) cmd->pdu;
3123e48354ceSNicholas Bellinger 	memset(hdr, 0, ISCSI_HDR_LEN);
3124e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_R2T;
3125e48354ceSNicholas Bellinger 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
3126e48354ceSNicholas Bellinger 	int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3127e48354ceSNicholas Bellinger 			(struct scsi_lun *)&hdr->lun);
312866c7db68SChristoph Hellwig 	hdr->itt		= cmd->init_task_tag;
31298567270dSVarun Prakash 	if (conn->conn_transport->iscsit_get_r2t_ttt)
31308567270dSVarun Prakash 		conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
31318567270dSVarun Prakash 	else
3132c1e34b64SSagi Grimberg 		r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
3133e48354ceSNicholas Bellinger 	hdr->ttt		= cpu_to_be32(r2t->targ_xfer_tag);
3134e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(conn->stat_sn);
3135e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
3136109e2381SRoland Dreier 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3137e48354ceSNicholas Bellinger 	hdr->r2tsn		= cpu_to_be32(r2t->r2t_sn);
3138e48354ceSNicholas Bellinger 	hdr->data_offset	= cpu_to_be32(r2t->offset);
3139e48354ceSNicholas Bellinger 	hdr->data_length	= cpu_to_be32(r2t->xfer_len);
3140e48354ceSNicholas Bellinger 
3141e48354ceSNicholas Bellinger 	pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3142e48354ceSNicholas Bellinger 		" 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3143e48354ceSNicholas Bellinger 		(!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3144e48354ceSNicholas Bellinger 		r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3145e48354ceSNicholas Bellinger 			r2t->offset, r2t->xfer_len, conn->cid);
3146e48354ceSNicholas Bellinger 
3147e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
3148e48354ceSNicholas Bellinger 	r2t->sent_r2t = 1;
3149e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
3150e48354ceSNicholas Bellinger 
31512854bb23SVarun Prakash 	ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
31526f3c0e69SAndy Grover 	if (ret < 0) {
31536f3c0e69SAndy Grover 		return ret;
31546f3c0e69SAndy Grover 	}
31556f3c0e69SAndy Grover 
31566f3c0e69SAndy Grover 	spin_lock_bh(&cmd->dataout_timeout_lock);
31576f3c0e69SAndy Grover 	iscsit_start_dataout_timer(cmd, conn);
31586f3c0e69SAndy Grover 	spin_unlock_bh(&cmd->dataout_timeout_lock);
31596f3c0e69SAndy Grover 
3160e48354ceSNicholas Bellinger 	return 0;
3161e48354ceSNicholas Bellinger }
3162e48354ceSNicholas Bellinger 
3163e48354ceSNicholas Bellinger /*
31648b1e1244SAndy Grover  *	@recovery: If called from iscsi_task_reassign_complete_write() for
3165e48354ceSNicholas Bellinger  *		connection recovery.
3166e48354ceSNicholas Bellinger  */
iscsit_build_r2ts_for_cmd(struct iscsit_conn * conn,struct iscsit_cmd * cmd,bool recovery)3167e48354ceSNicholas Bellinger int iscsit_build_r2ts_for_cmd(
3168be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
316966cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
31708b1e1244SAndy Grover 	bool recovery)
3171e48354ceSNicholas Bellinger {
3172e48354ceSNicholas Bellinger 	int first_r2t = 1;
3173e48354ceSNicholas Bellinger 	u32 offset = 0, xfer_len = 0;
3174e48354ceSNicholas Bellinger 
3175e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
3176e48354ceSNicholas Bellinger 	if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3177e48354ceSNicholas Bellinger 		spin_unlock_bh(&cmd->r2t_lock);
3178e48354ceSNicholas Bellinger 		return 0;
3179e48354ceSNicholas Bellinger 	}
3180e48354ceSNicholas Bellinger 
31818b1e1244SAndy Grover 	if (conn->sess->sess_ops->DataSequenceInOrder &&
31828b1e1244SAndy Grover 	    !recovery)
3183c6037cc5SAndy Grover 		cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
3184e48354ceSNicholas Bellinger 
3185e48354ceSNicholas Bellinger 	while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3186e48354ceSNicholas Bellinger 		if (conn->sess->sess_ops->DataSequenceInOrder) {
3187e48354ceSNicholas Bellinger 			offset = cmd->r2t_offset;
3188e48354ceSNicholas Bellinger 
31898b1e1244SAndy Grover 			if (first_r2t && recovery) {
31908b1e1244SAndy Grover 				int new_data_end = offset +
31918b1e1244SAndy Grover 					conn->sess->sess_ops->MaxBurstLength -
31928b1e1244SAndy Grover 					cmd->next_burst_len;
31938b1e1244SAndy Grover 
3194ebf1d95cSAndy Grover 				if (new_data_end > cmd->se_cmd.data_length)
3195ebf1d95cSAndy Grover 					xfer_len = cmd->se_cmd.data_length - offset;
31968b1e1244SAndy Grover 				else
31978b1e1244SAndy Grover 					xfer_len =
31988b1e1244SAndy Grover 						conn->sess->sess_ops->MaxBurstLength -
31998b1e1244SAndy Grover 						cmd->next_burst_len;
3200e48354ceSNicholas Bellinger 			} else {
32018b1e1244SAndy Grover 				int new_data_end = offset +
3202e48354ceSNicholas Bellinger 					conn->sess->sess_ops->MaxBurstLength;
32038b1e1244SAndy Grover 
3204ebf1d95cSAndy Grover 				if (new_data_end > cmd->se_cmd.data_length)
3205ebf1d95cSAndy Grover 					xfer_len = cmd->se_cmd.data_length - offset;
32068b1e1244SAndy Grover 				else
32078b1e1244SAndy Grover 					xfer_len = conn->sess->sess_ops->MaxBurstLength;
3208e48354ceSNicholas Bellinger 			}
320996e8e26dSBart Van Assche 
321096e8e26dSBart Van Assche 			if ((s32)xfer_len < 0) {
321196e8e26dSBart Van Assche 				cmd->cmd_flags |= ICF_SENT_LAST_R2T;
321296e8e26dSBart Van Assche 				break;
321396e8e26dSBart Van Assche 			}
321496e8e26dSBart Van Assche 
3215e48354ceSNicholas Bellinger 			cmd->r2t_offset += xfer_len;
3216e48354ceSNicholas Bellinger 
3217ebf1d95cSAndy Grover 			if (cmd->r2t_offset == cmd->se_cmd.data_length)
3218e48354ceSNicholas Bellinger 				cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3219e48354ceSNicholas Bellinger 		} else {
3220e48354ceSNicholas Bellinger 			struct iscsi_seq *seq;
3221e48354ceSNicholas Bellinger 
3222e48354ceSNicholas Bellinger 			seq = iscsit_get_seq_holder_for_r2t(cmd);
3223e48354ceSNicholas Bellinger 			if (!seq) {
3224e48354ceSNicholas Bellinger 				spin_unlock_bh(&cmd->r2t_lock);
3225e48354ceSNicholas Bellinger 				return -1;
3226e48354ceSNicholas Bellinger 			}
3227e48354ceSNicholas Bellinger 
3228e48354ceSNicholas Bellinger 			offset = seq->offset;
3229e48354ceSNicholas Bellinger 			xfer_len = seq->xfer_len;
3230e48354ceSNicholas Bellinger 
3231e48354ceSNicholas Bellinger 			if (cmd->seq_send_order == cmd->seq_count)
3232e48354ceSNicholas Bellinger 				cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3233e48354ceSNicholas Bellinger 		}
3234e48354ceSNicholas Bellinger 		cmd->outstanding_r2ts++;
3235e48354ceSNicholas Bellinger 		first_r2t = 0;
3236e48354ceSNicholas Bellinger 
3237e48354ceSNicholas Bellinger 		if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3238e48354ceSNicholas Bellinger 			spin_unlock_bh(&cmd->r2t_lock);
3239e48354ceSNicholas Bellinger 			return -1;
3240e48354ceSNicholas Bellinger 		}
3241e48354ceSNicholas Bellinger 
3242e48354ceSNicholas Bellinger 		if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3243e48354ceSNicholas Bellinger 			break;
3244e48354ceSNicholas Bellinger 	}
3245e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
3246e48354ceSNicholas Bellinger 
3247e48354ceSNicholas Bellinger 	return 0;
3248e48354ceSNicholas Bellinger }
3249d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
3250e48354ceSNicholas Bellinger 
iscsit_build_rsp_pdu(struct iscsit_cmd * cmd,struct iscsit_conn * conn,bool inc_stat_sn,struct iscsi_scsi_rsp * hdr)3251be36d683SMax Gurtovoy void iscsit_build_rsp_pdu(struct iscsit_cmd *cmd, struct iscsit_conn *conn,
32522ec5a8c1SNicholas Bellinger 			bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
3253e48354ceSNicholas Bellinger {
32542ec5a8c1SNicholas Bellinger 	if (inc_stat_sn)
3255e48354ceSNicholas Bellinger 		cmd->stat_sn = conn->stat_sn++;
3256e48354ceSNicholas Bellinger 
325704f3b31bSNicholas Bellinger 	atomic_long_inc(&conn->sess->rsp_pdus);
3258e48354ceSNicholas Bellinger 
3259e48354ceSNicholas Bellinger 	memset(hdr, 0, ISCSI_HDR_LEN);
3260e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_SCSI_CMD_RSP;
3261e48354ceSNicholas Bellinger 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
3262e48354ceSNicholas Bellinger 	if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3263e48354ceSNicholas Bellinger 		hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
32647e46cf02SNicholas Bellinger 		hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3265e48354ceSNicholas Bellinger 	} else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3266e48354ceSNicholas Bellinger 		hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
32677e46cf02SNicholas Bellinger 		hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3268e48354ceSNicholas Bellinger 	}
3269e48354ceSNicholas Bellinger 	hdr->response		= cmd->iscsi_response;
3270e48354ceSNicholas Bellinger 	hdr->cmd_status		= cmd->se_cmd.scsi_status;
327166c7db68SChristoph Hellwig 	hdr->itt		= cmd->init_task_tag;
3272e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
3273e48354ceSNicholas Bellinger 
3274e48354ceSNicholas Bellinger 	iscsit_increment_maxcmdsn(cmd, conn->sess);
3275e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
3276109e2381SRoland Dreier 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3277e48354ceSNicholas Bellinger 
32782ec5a8c1SNicholas Bellinger 	pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
32792ec5a8c1SNicholas Bellinger 		" Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
32802ec5a8c1SNicholas Bellinger 		cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
32812ec5a8c1SNicholas Bellinger 		cmd->se_cmd.scsi_status, conn->cid);
32822ec5a8c1SNicholas Bellinger }
32832ec5a8c1SNicholas Bellinger EXPORT_SYMBOL(iscsit_build_rsp_pdu);
32842ec5a8c1SNicholas Bellinger 
iscsit_send_response(struct iscsit_cmd * cmd,struct iscsit_conn * conn)3285be36d683SMax Gurtovoy static int iscsit_send_response(struct iscsit_cmd *cmd, struct iscsit_conn *conn)
32862ec5a8c1SNicholas Bellinger {
32872ec5a8c1SNicholas Bellinger 	struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
32882ec5a8c1SNicholas Bellinger 	bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
32892854bb23SVarun Prakash 	void *data_buf = NULL;
32902854bb23SVarun Prakash 	u32 padding = 0, data_buf_len = 0;
32912ec5a8c1SNicholas Bellinger 
32922ec5a8c1SNicholas Bellinger 	iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
32932ec5a8c1SNicholas Bellinger 
3294e48354ceSNicholas Bellinger 	/*
3295e48354ceSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
3296e48354ceSNicholas Bellinger 	 */
3297e48354ceSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
3298e48354ceSNicholas Bellinger 	   ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3299e48354ceSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
33009c58b7ddSRoland Dreier 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
33019c58b7ddSRoland Dreier 		cmd->se_cmd.scsi_sense_length += sizeof (__be16);
33029c58b7ddSRoland Dreier 
3303e48354ceSNicholas Bellinger 		padding		= -(cmd->se_cmd.scsi_sense_length) & 3;
330450e5c87dSChristoph Hellwig 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
33052854bb23SVarun Prakash 		data_buf = cmd->sense_buffer;
33062854bb23SVarun Prakash 		data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
3307e48354ceSNicholas Bellinger 
3308e48354ceSNicholas Bellinger 		if (padding) {
33099c58b7ddSRoland Dreier 			memset(cmd->sense_buffer +
3310e48354ceSNicholas Bellinger 				cmd->se_cmd.scsi_sense_length, 0, padding);
3311e48354ceSNicholas Bellinger 			pr_debug("Adding %u bytes of padding to"
3312e48354ceSNicholas Bellinger 				" SENSE.\n", padding);
3313e48354ceSNicholas Bellinger 		}
3314e48354ceSNicholas Bellinger 
3315e48354ceSNicholas Bellinger 		pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3316e48354ceSNicholas Bellinger 				" Response PDU\n",
3317e48354ceSNicholas Bellinger 				cmd->se_cmd.scsi_sense_length);
3318e48354ceSNicholas Bellinger 	}
3319e48354ceSNicholas Bellinger 
33202854bb23SVarun Prakash 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
33212854bb23SVarun Prakash 						     data_buf_len);
3322e48354ceSNicholas Bellinger }
3323e48354ceSNicholas Bellinger 
iscsit_convert_tcm_tmr_rsp(struct se_tmr_req * se_tmr)3324e48354ceSNicholas Bellinger static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3325e48354ceSNicholas Bellinger {
3326e48354ceSNicholas Bellinger 	switch (se_tmr->response) {
3327e48354ceSNicholas Bellinger 	case TMR_FUNCTION_COMPLETE:
3328e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_COMPLETE;
3329e48354ceSNicholas Bellinger 	case TMR_TASK_DOES_NOT_EXIST:
3330e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_NO_TASK;
3331e48354ceSNicholas Bellinger 	case TMR_LUN_DOES_NOT_EXIST:
3332e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_NO_LUN;
3333e48354ceSNicholas Bellinger 	case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3334e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_NOT_SUPPORTED;
3335e48354ceSNicholas Bellinger 	case TMR_FUNCTION_REJECTED:
3336e48354ceSNicholas Bellinger 	default:
3337e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_REJECTED;
3338e48354ceSNicholas Bellinger 	}
3339e48354ceSNicholas Bellinger }
3340e48354ceSNicholas Bellinger 
33412ec5a8c1SNicholas Bellinger void
iscsit_build_task_mgt_rsp(struct iscsit_cmd * cmd,struct iscsit_conn * conn,struct iscsi_tm_rsp * hdr)3342be36d683SMax Gurtovoy iscsit_build_task_mgt_rsp(struct iscsit_cmd *cmd, struct iscsit_conn *conn,
33432ec5a8c1SNicholas Bellinger 			  struct iscsi_tm_rsp *hdr)
3344e48354ceSNicholas Bellinger {
3345e48354ceSNicholas Bellinger 	struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3346e48354ceSNicholas Bellinger 
3347e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_SCSI_TMFUNC_RSP;
33487ae0b103SNicholas Bellinger 	hdr->flags		= ISCSI_FLAG_CMD_FINAL;
3349e48354ceSNicholas Bellinger 	hdr->response		= iscsit_convert_tcm_tmr_rsp(se_tmr);
335066c7db68SChristoph Hellwig 	hdr->itt		= cmd->init_task_tag;
3351e48354ceSNicholas Bellinger 	cmd->stat_sn		= conn->stat_sn++;
3352e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
3353e48354ceSNicholas Bellinger 
3354e48354ceSNicholas Bellinger 	iscsit_increment_maxcmdsn(cmd, conn->sess);
3355e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
3356109e2381SRoland Dreier 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3357e48354ceSNicholas Bellinger 
33582ec5a8c1SNicholas Bellinger 	pr_debug("Built Task Management Response ITT: 0x%08x,"
33592ec5a8c1SNicholas Bellinger 		" StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
33602ec5a8c1SNicholas Bellinger 		cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
33612ec5a8c1SNicholas Bellinger }
33622ec5a8c1SNicholas Bellinger EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
33632ec5a8c1SNicholas Bellinger 
33642ec5a8c1SNicholas Bellinger static int
iscsit_send_task_mgt_rsp(struct iscsit_cmd * cmd,struct iscsit_conn * conn)3365be36d683SMax Gurtovoy iscsit_send_task_mgt_rsp(struct iscsit_cmd *cmd, struct iscsit_conn *conn)
33662ec5a8c1SNicholas Bellinger {
33672ec5a8c1SNicholas Bellinger 	struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
33682ec5a8c1SNicholas Bellinger 
33692ec5a8c1SNicholas Bellinger 	iscsit_build_task_mgt_rsp(cmd, conn, hdr);
33702ec5a8c1SNicholas Bellinger 
33712854bb23SVarun Prakash 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3372e48354ceSNicholas Bellinger }
3373e48354ceSNicholas Bellinger 
33748b1e1244SAndy Grover #define SENDTARGETS_BUF_LIMIT 32768U
33758b1e1244SAndy Grover 
337622c7aaa5SSagi Grimberg static int
iscsit_build_sendtargets_response(struct iscsit_cmd * cmd,enum iscsit_transport_type network_transport,int skip_bytes,bool * completed)337766cd9d4eSMax Gurtovoy iscsit_build_sendtargets_response(struct iscsit_cmd *cmd,
3378e4f4e801SSagi Grimberg 				  enum iscsit_transport_type network_transport,
3379e4f4e801SSagi Grimberg 				  int skip_bytes, bool *completed)
3380e48354ceSNicholas Bellinger {
3381e48354ceSNicholas Bellinger 	char *payload = NULL;
3382be36d683SMax Gurtovoy 	struct iscsit_conn *conn = cmd->conn;
3383e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg;
3384e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
3385e48354ceSNicholas Bellinger 	struct iscsi_tpg_np *tpg_np;
3386e48354ceSNicholas Bellinger 	int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
33872dd1d53fSThomas Glanzmann 	int target_name_printed;
33888b1e1244SAndy Grover 	unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
33896665889cSNicholas Bellinger 	unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
3390a6415cddSDavid Disseldorp 	bool active;
3391e48354ceSNicholas Bellinger 
3392be7dcfb6SSagi Grimberg 	buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
33938b1e1244SAndy Grover 			 SENDTARGETS_BUF_LIMIT);
3394e48354ceSNicholas Bellinger 
3395e48354ceSNicholas Bellinger 	payload = kzalloc(buffer_len, GFP_KERNEL);
3396c46e22f1SMarkus Elfring 	if (!payload)
3397e48354ceSNicholas Bellinger 		return -ENOMEM;
3398c46e22f1SMarkus Elfring 
33996665889cSNicholas Bellinger 	/*
34008060b8ddSAndy Grover 	 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
34016665889cSNicholas Bellinger 	 * explicit case..
34026665889cSNicholas Bellinger 	 */
34038060b8ddSAndy Grover 	if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
34046665889cSNicholas Bellinger 		text_ptr = strchr(text_in, '=');
34056665889cSNicholas Bellinger 		if (!text_ptr) {
34066665889cSNicholas Bellinger 			pr_err("Unable to locate '=' string in text_in:"
34076665889cSNicholas Bellinger 			       " %s\n", text_in);
34084f45d320SDan Carpenter 			kfree(payload);
34096665889cSNicholas Bellinger 			return -EINVAL;
34106665889cSNicholas Bellinger 		}
34116665889cSNicholas Bellinger 		/*
34126665889cSNicholas Bellinger 		 * Skip over '=' character..
34136665889cSNicholas Bellinger 		 */
34146665889cSNicholas Bellinger 		text_ptr += 1;
34156665889cSNicholas Bellinger 	}
3416e48354ceSNicholas Bellinger 
3417e48354ceSNicholas Bellinger 	spin_lock(&tiqn_lock);
3418e48354ceSNicholas Bellinger 	list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
34198060b8ddSAndy Grover 		if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
34206665889cSNicholas Bellinger 		     strcmp(tiqn->tiqn, text_ptr)) {
34216665889cSNicholas Bellinger 			continue;
34226665889cSNicholas Bellinger 		}
34236665889cSNicholas Bellinger 
34242dd1d53fSThomas Glanzmann 		target_name_printed = 0;
3425e48354ceSNicholas Bellinger 
3426e48354ceSNicholas Bellinger 		spin_lock(&tiqn->tiqn_tpg_lock);
3427e48354ceSNicholas Bellinger 		list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3428e48354ceSNicholas Bellinger 
34292dd1d53fSThomas Glanzmann 			/* If demo_mode_discovery=0 and generate_node_acls=0
34302dd1d53fSThomas Glanzmann 			 * (demo mode dislabed) do not return
34312dd1d53fSThomas Glanzmann 			 * TargetName+TargetAddress unless a NodeACL exists.
34322dd1d53fSThomas Glanzmann 			 */
34332dd1d53fSThomas Glanzmann 
34342dd1d53fSThomas Glanzmann 			if ((tpg->tpg_attrib.generate_node_acls == 0) &&
34352dd1d53fSThomas Glanzmann 			    (tpg->tpg_attrib.demo_mode_discovery == 0) &&
343621aaa23bSNicholas Bellinger 			    (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
34372dd1d53fSThomas Glanzmann 				cmd->conn->sess->sess_ops->InitiatorName))) {
34382dd1d53fSThomas Glanzmann 				continue;
34392dd1d53fSThomas Glanzmann 			}
34402dd1d53fSThomas Glanzmann 
3441e48354ceSNicholas Bellinger 			spin_lock(&tpg->tpg_state_lock);
3442a6415cddSDavid Disseldorp 			active = (tpg->tpg_state == TPG_STATE_ACTIVE);
3443e48354ceSNicholas Bellinger 			spin_unlock(&tpg->tpg_state_lock);
3444a6415cddSDavid Disseldorp 
3445a6415cddSDavid Disseldorp 			if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3446e48354ceSNicholas Bellinger 				continue;
3447e48354ceSNicholas Bellinger 
3448e48354ceSNicholas Bellinger 			spin_lock(&tpg->tpg_np_lock);
3449e48354ceSNicholas Bellinger 			list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3450e48354ceSNicholas Bellinger 						tpg_np_list) {
34512f9bc894SNicholas Bellinger 				struct iscsi_np *np = tpg_np->tpg_np;
345213a3cf08SAndy Grover 				struct sockaddr_storage *sockaddr;
34532f9bc894SNicholas Bellinger 
345422c7aaa5SSagi Grimberg 				if (np->np_network_transport != network_transport)
345522c7aaa5SSagi Grimberg 					continue;
345622c7aaa5SSagi Grimberg 
34572dd1d53fSThomas Glanzmann 				if (!target_name_printed) {
34582dd1d53fSThomas Glanzmann 					len = sprintf(buf, "TargetName=%s",
34592dd1d53fSThomas Glanzmann 						      tiqn->tiqn);
34602dd1d53fSThomas Glanzmann 					len += 1;
34612dd1d53fSThomas Glanzmann 
34622dd1d53fSThomas Glanzmann 					if ((len + payload_len) > buffer_len) {
34632dd1d53fSThomas Glanzmann 						spin_unlock(&tpg->tpg_np_lock);
34642dd1d53fSThomas Glanzmann 						spin_unlock(&tiqn->tiqn_tpg_lock);
34652dd1d53fSThomas Glanzmann 						end_of_buf = 1;
34662dd1d53fSThomas Glanzmann 						goto eob;
34672dd1d53fSThomas Glanzmann 					}
3468e4f4e801SSagi Grimberg 
3469e4f4e801SSagi Grimberg 					if (skip_bytes && len <= skip_bytes) {
3470e4f4e801SSagi Grimberg 						skip_bytes -= len;
3471e4f4e801SSagi Grimberg 					} else {
34722dd1d53fSThomas Glanzmann 						memcpy(payload + payload_len, buf, len);
34732dd1d53fSThomas Glanzmann 						payload_len += len;
34742dd1d53fSThomas Glanzmann 						target_name_printed = 1;
3475e4f4e801SSagi Grimberg 						if (len > skip_bytes)
3476e4f4e801SSagi Grimberg 							skip_bytes = 0;
3477e4f4e801SSagi Grimberg 					}
34782dd1d53fSThomas Glanzmann 				}
34792dd1d53fSThomas Glanzmann 
34807bfca0cfSSagi Grimberg 				if (inet_addr_is_any((struct sockaddr *)&np->np_sockaddr))
348169d75574SAndy Grover 					sockaddr = &conn->local_sockaddr;
34821997e625SAndy Grover 				else
348369d75574SAndy Grover 					sockaddr = &np->np_sockaddr;
34841997e625SAndy Grover 
348576c28f1fSAndy Grover 				len = sprintf(buf, "TargetAddress="
348676c28f1fSAndy Grover 					      "%pISpc,%hu",
348769d75574SAndy Grover 					      sockaddr,
3488e48354ceSNicholas Bellinger 					      tpg->tpgt);
3489e48354ceSNicholas Bellinger 				len += 1;
3490e48354ceSNicholas Bellinger 
3491e48354ceSNicholas Bellinger 				if ((len + payload_len) > buffer_len) {
3492e48354ceSNicholas Bellinger 					spin_unlock(&tpg->tpg_np_lock);
3493e48354ceSNicholas Bellinger 					spin_unlock(&tiqn->tiqn_tpg_lock);
3494e48354ceSNicholas Bellinger 					end_of_buf = 1;
3495e48354ceSNicholas Bellinger 					goto eob;
3496e48354ceSNicholas Bellinger 				}
3497e4f4e801SSagi Grimberg 
3498e4f4e801SSagi Grimberg 				if (skip_bytes && len <= skip_bytes) {
3499e4f4e801SSagi Grimberg 					skip_bytes -= len;
3500e4f4e801SSagi Grimberg 				} else {
35018359cf43SJörn Engel 					memcpy(payload + payload_len, buf, len);
3502e48354ceSNicholas Bellinger 					payload_len += len;
3503e4f4e801SSagi Grimberg 					if (len > skip_bytes)
3504e4f4e801SSagi Grimberg 						skip_bytes = 0;
3505e4f4e801SSagi Grimberg 				}
3506e48354ceSNicholas Bellinger 			}
3507e48354ceSNicholas Bellinger 			spin_unlock(&tpg->tpg_np_lock);
3508e48354ceSNicholas Bellinger 		}
3509e48354ceSNicholas Bellinger 		spin_unlock(&tiqn->tiqn_tpg_lock);
3510e48354ceSNicholas Bellinger eob:
3511e4f4e801SSagi Grimberg 		if (end_of_buf) {
3512e4f4e801SSagi Grimberg 			*completed = false;
3513e48354ceSNicholas Bellinger 			break;
3514e4f4e801SSagi Grimberg 		}
35156665889cSNicholas Bellinger 
35168060b8ddSAndy Grover 		if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
35176665889cSNicholas Bellinger 			break;
3518e48354ceSNicholas Bellinger 	}
3519e48354ceSNicholas Bellinger 	spin_unlock(&tiqn_lock);
3520e48354ceSNicholas Bellinger 
3521e48354ceSNicholas Bellinger 	cmd->buf_ptr = payload;
3522e48354ceSNicholas Bellinger 
3523e48354ceSNicholas Bellinger 	return payload_len;
3524e48354ceSNicholas Bellinger }
3525e48354ceSNicholas Bellinger 
3526889c8a68SNicholas Bellinger int
iscsit_build_text_rsp(struct iscsit_cmd * cmd,struct iscsit_conn * conn,struct iscsi_text_rsp * hdr,enum iscsit_transport_type network_transport)3527be36d683SMax Gurtovoy iscsit_build_text_rsp(struct iscsit_cmd *cmd, struct iscsit_conn *conn,
352822c7aaa5SSagi Grimberg 		      struct iscsi_text_rsp *hdr,
352922c7aaa5SSagi Grimberg 		      enum iscsit_transport_type network_transport)
3530e48354ceSNicholas Bellinger {
3531889c8a68SNicholas Bellinger 	int text_length, padding;
3532e4f4e801SSagi Grimberg 	bool completed = true;
3533e48354ceSNicholas Bellinger 
3534e4f4e801SSagi Grimberg 	text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3535e4f4e801SSagi Grimberg 							cmd->read_data_done,
3536e4f4e801SSagi Grimberg 							&completed);
3537e48354ceSNicholas Bellinger 	if (text_length < 0)
3538e48354ceSNicholas Bellinger 		return text_length;
3539e48354ceSNicholas Bellinger 
3540e4f4e801SSagi Grimberg 	if (completed) {
3541310d40a9SVarun Prakash 		hdr->flags = ISCSI_FLAG_CMD_FINAL;
3542e4f4e801SSagi Grimberg 	} else {
3543310d40a9SVarun Prakash 		hdr->flags = ISCSI_FLAG_TEXT_CONTINUE;
3544e4f4e801SSagi Grimberg 		cmd->read_data_done += text_length;
3545e4f4e801SSagi Grimberg 		if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3546e4f4e801SSagi Grimberg 			cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3547e4f4e801SSagi Grimberg 	}
3548e4f4e801SSagi Grimberg 	hdr->opcode = ISCSI_OP_TEXT_RSP;
3549889c8a68SNicholas Bellinger 	padding = ((-text_length) & 3);
3550e48354ceSNicholas Bellinger 	hton24(hdr->dlength, text_length);
355166c7db68SChristoph Hellwig 	hdr->itt = cmd->init_task_tag;
3552e48354ceSNicholas Bellinger 	hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3553e48354ceSNicholas Bellinger 	cmd->stat_sn = conn->stat_sn++;
3554e48354ceSNicholas Bellinger 	hdr->statsn = cpu_to_be32(cmd->stat_sn);
3555e48354ceSNicholas Bellinger 
3556e48354ceSNicholas Bellinger 	iscsit_increment_maxcmdsn(cmd, conn->sess);
3557e4f4e801SSagi Grimberg 	/*
3558e4f4e801SSagi Grimberg 	 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3559e4f4e801SSagi Grimberg 	 * correctly increment MaxCmdSN for each response answering a
3560e4f4e801SSagi Grimberg 	 * non immediate text request with a valid CmdSN.
3561e4f4e801SSagi Grimberg 	 */
3562e4f4e801SSagi Grimberg 	cmd->maxcmdsn_inc = 0;
3563e48354ceSNicholas Bellinger 	hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3564109e2381SRoland Dreier 	hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3565e48354ceSNicholas Bellinger 
3566e4f4e801SSagi Grimberg 	pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3567e4f4e801SSagi Grimberg 		" Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3568e4f4e801SSagi Grimberg 		cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3569e4f4e801SSagi Grimberg 		!!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3570e4f4e801SSagi Grimberg 		!!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
3571e48354ceSNicholas Bellinger 
3572889c8a68SNicholas Bellinger 	return text_length + padding;
3573889c8a68SNicholas Bellinger }
3574889c8a68SNicholas Bellinger EXPORT_SYMBOL(iscsit_build_text_rsp);
3575889c8a68SNicholas Bellinger 
iscsit_send_text_rsp(struct iscsit_cmd * cmd,struct iscsit_conn * conn)3576889c8a68SNicholas Bellinger static int iscsit_send_text_rsp(
357766cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
3578be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
3579889c8a68SNicholas Bellinger {
3580889c8a68SNicholas Bellinger 	struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
35812854bb23SVarun Prakash 	int text_length;
3582889c8a68SNicholas Bellinger 
3583864e504aSVarun Prakash 	text_length = iscsit_build_text_rsp(cmd, conn, hdr,
3584864e504aSVarun Prakash 				conn->conn_transport->transport_type);
35852854bb23SVarun Prakash 	if (text_length < 0)
35862854bb23SVarun Prakash 		return text_length;
3587889c8a68SNicholas Bellinger 
35882854bb23SVarun Prakash 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
35892854bb23SVarun Prakash 						     cmd->buf_ptr,
35902854bb23SVarun Prakash 						     text_length);
3591e48354ceSNicholas Bellinger }
3592e48354ceSNicholas Bellinger 
35932ec5a8c1SNicholas Bellinger void
iscsit_build_reject(struct iscsit_cmd * cmd,struct iscsit_conn * conn,struct iscsi_reject * hdr)3594be36d683SMax Gurtovoy iscsit_build_reject(struct iscsit_cmd *cmd, struct iscsit_conn *conn,
35952ec5a8c1SNicholas Bellinger 		    struct iscsi_reject *hdr)
3596e48354ceSNicholas Bellinger {
3597e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_REJECT;
3598ba159914SNicholas Bellinger 	hdr->reason		= cmd->reject_reason;
3599e48354ceSNicholas Bellinger 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
3600e48354ceSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
360150e5c87dSChristoph Hellwig 	hdr->ffffffff		= cpu_to_be32(0xffffffff);
3602e48354ceSNicholas Bellinger 	cmd->stat_sn		= conn->stat_sn++;
3603e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
3604e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
3605109e2381SRoland Dreier 	hdr->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3606e48354ceSNicholas Bellinger 
36072ec5a8c1SNicholas Bellinger }
36082ec5a8c1SNicholas Bellinger EXPORT_SYMBOL(iscsit_build_reject);
3609e48354ceSNicholas Bellinger 
iscsit_send_reject(struct iscsit_cmd * cmd,struct iscsit_conn * conn)36102ec5a8c1SNicholas Bellinger static int iscsit_send_reject(
361166cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
3612be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
36132ec5a8c1SNicholas Bellinger {
3614bfbdb31dSNicholas Bellinger 	struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
36152ec5a8c1SNicholas Bellinger 
3616bfbdb31dSNicholas Bellinger 	iscsit_build_reject(cmd, conn, hdr);
36172ec5a8c1SNicholas Bellinger 
3618e48354ceSNicholas Bellinger 	pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3619e48354ceSNicholas Bellinger 		" CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3620e48354ceSNicholas Bellinger 
36212854bb23SVarun Prakash 	return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
36222854bb23SVarun Prakash 						     cmd->buf_ptr,
36232854bb23SVarun Prakash 						     ISCSI_HDR_LEN);
3624e48354ceSNicholas Bellinger }
3625e48354ceSNicholas Bellinger 
iscsit_thread_get_cpumask(struct iscsit_conn * conn)3626be36d683SMax Gurtovoy void iscsit_thread_get_cpumask(struct iscsit_conn *conn)
3627e48354ceSNicholas Bellinger {
3628e48354ceSNicholas Bellinger 	int ord, cpu;
3629525f447fSMingzhe Zou 	cpumask_var_t conn_allowed_cpumask;
3630d72d827fSMingzhe Zou 
3631e48354ceSNicholas Bellinger 	/*
363288dcd2daSNicholas Bellinger 	 * bitmap_id is assigned from iscsit_global->ts_bitmap from
363388dcd2daSNicholas Bellinger 	 * within iscsit_start_kthreads()
3634e48354ceSNicholas Bellinger 	 *
363588dcd2daSNicholas Bellinger 	 * Here we use bitmap_id to determine which CPU that this
363688dcd2daSNicholas Bellinger 	 * iSCSI connection's RX/TX threads will be scheduled to
3637e48354ceSNicholas Bellinger 	 * execute upon.
3638e48354ceSNicholas Bellinger 	 */
3639525f447fSMingzhe Zou 	if (!zalloc_cpumask_var(&conn_allowed_cpumask, GFP_KERNEL)) {
3640525f447fSMingzhe Zou 		ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
3641525f447fSMingzhe Zou 		for_each_online_cpu(cpu) {
3642e48354ceSNicholas Bellinger 			if (ord-- == 0) {
3643e48354ceSNicholas Bellinger 				cpumask_set_cpu(cpu, conn->conn_cpumask);
3644e48354ceSNicholas Bellinger 				return;
3645e48354ceSNicholas Bellinger 			}
3646e48354ceSNicholas Bellinger 		}
3647525f447fSMingzhe Zou 	} else {
3648525f447fSMingzhe Zou 		cpumask_and(conn_allowed_cpumask, iscsit_global->allowed_cpumask,
3649525f447fSMingzhe Zou 			cpu_online_mask);
3650525f447fSMingzhe Zou 
3651525f447fSMingzhe Zou 		cpumask_clear(conn->conn_cpumask);
3652525f447fSMingzhe Zou 		ord = conn->bitmap_id % cpumask_weight(conn_allowed_cpumask);
3653525f447fSMingzhe Zou 		for_each_cpu(cpu, conn_allowed_cpumask) {
3654525f447fSMingzhe Zou 			if (ord-- == 0) {
3655525f447fSMingzhe Zou 				cpumask_set_cpu(cpu, conn->conn_cpumask);
3656525f447fSMingzhe Zou 				free_cpumask_var(conn_allowed_cpumask);
3657525f447fSMingzhe Zou 				return;
3658525f447fSMingzhe Zou 			}
3659525f447fSMingzhe Zou 		}
3660525f447fSMingzhe Zou 		free_cpumask_var(conn_allowed_cpumask);
3661525f447fSMingzhe Zou 	}
3662e48354ceSNicholas Bellinger 	/*
3663e48354ceSNicholas Bellinger 	 * This should never be reached..
3664e48354ceSNicholas Bellinger 	 */
3665e48354ceSNicholas Bellinger 	dump_stack();
3666e48354ceSNicholas Bellinger 	cpumask_setall(conn->conn_cpumask);
3667e48354ceSNicholas Bellinger }
3668e48354ceSNicholas Bellinger 
iscsit_thread_reschedule(struct iscsit_conn * conn)3669be36d683SMax Gurtovoy static void iscsit_thread_reschedule(struct iscsit_conn *conn)
3670d72d827fSMingzhe Zou {
3671d72d827fSMingzhe Zou 	/*
3672d72d827fSMingzhe Zou 	 * If iscsit_global->allowed_cpumask modified, reschedule iSCSI
3673d72d827fSMingzhe Zou 	 * connection's RX/TX threads update conn->allowed_cpumask.
3674d72d827fSMingzhe Zou 	 */
3675d72d827fSMingzhe Zou 	if (!cpumask_equal(iscsit_global->allowed_cpumask,
3676d72d827fSMingzhe Zou 			   conn->allowed_cpumask)) {
3677d72d827fSMingzhe Zou 		iscsit_thread_get_cpumask(conn);
3678d72d827fSMingzhe Zou 		conn->conn_tx_reset_cpumask = 1;
3679d72d827fSMingzhe Zou 		conn->conn_rx_reset_cpumask = 1;
3680d72d827fSMingzhe Zou 		cpumask_copy(conn->allowed_cpumask,
3681d72d827fSMingzhe Zou 			     iscsit_global->allowed_cpumask);
3682d72d827fSMingzhe Zou 	}
3683d72d827fSMingzhe Zou }
3684d72d827fSMingzhe Zou 
iscsit_thread_check_cpumask(struct iscsit_conn * conn,struct task_struct * p,int mode)3685d72d827fSMingzhe Zou void iscsit_thread_check_cpumask(
3686be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
3687d72d827fSMingzhe Zou 	struct task_struct *p,
3688d72d827fSMingzhe Zou 	int mode)
3689d72d827fSMingzhe Zou {
3690d72d827fSMingzhe Zou 	/*
3691d72d827fSMingzhe Zou 	 * The TX and RX threads maybe call iscsit_thread_check_cpumask()
3692d72d827fSMingzhe Zou 	 * at the same time. The RX thread might be faster and return from
3693d72d827fSMingzhe Zou 	 * iscsit_thread_reschedule() with conn_rx_reset_cpumask set to 0.
3694d72d827fSMingzhe Zou 	 * Then the TX thread sets it back to 1.
3695d72d827fSMingzhe Zou 	 * The next time the RX thread loops, it sees conn_rx_reset_cpumask
3696d72d827fSMingzhe Zou 	 * set to 1 and calls set_cpus_allowed_ptr() again and set it to 0.
3697d72d827fSMingzhe Zou 	 */
3698d72d827fSMingzhe Zou 	iscsit_thread_reschedule(conn);
3699d72d827fSMingzhe Zou 
3700d72d827fSMingzhe Zou 	/*
3701d72d827fSMingzhe Zou 	 * mode == 1 signals iscsi_target_tx_thread() usage.
3702d72d827fSMingzhe Zou 	 * mode == 0 signals iscsi_target_rx_thread() usage.
3703d72d827fSMingzhe Zou 	 */
3704d72d827fSMingzhe Zou 	if (mode == 1) {
3705d72d827fSMingzhe Zou 		if (!conn->conn_tx_reset_cpumask)
3706d72d827fSMingzhe Zou 			return;
3707d72d827fSMingzhe Zou 	} else {
3708d72d827fSMingzhe Zou 		if (!conn->conn_rx_reset_cpumask)
3709d72d827fSMingzhe Zou 			return;
3710d72d827fSMingzhe Zou 	}
3711d72d827fSMingzhe Zou 
3712d72d827fSMingzhe Zou 	/*
3713d72d827fSMingzhe Zou 	 * Update the CPU mask for this single kthread so that
3714d72d827fSMingzhe Zou 	 * both TX and RX kthreads are scheduled to run on the
3715d72d827fSMingzhe Zou 	 * same CPU.
3716d72d827fSMingzhe Zou 	 */
3717d72d827fSMingzhe Zou 	set_cpus_allowed_ptr(p, conn->conn_cpumask);
3718d72d827fSMingzhe Zou 	if (mode == 1)
3719d72d827fSMingzhe Zou 		conn->conn_tx_reset_cpumask = 0;
3720d72d827fSMingzhe Zou 	else
3721d72d827fSMingzhe Zou 		conn->conn_rx_reset_cpumask = 0;
3722d72d827fSMingzhe Zou }
3723d72d827fSMingzhe Zou EXPORT_SYMBOL(iscsit_thread_check_cpumask);
3724d72d827fSMingzhe Zou 
3725d2faaefbSVarun Prakash int
iscsit_immediate_queue(struct iscsit_conn * conn,struct iscsit_cmd * cmd,int state)3726be36d683SMax Gurtovoy iscsit_immediate_queue(struct iscsit_conn *conn, struct iscsit_cmd *cmd, int state)
3727e48354ceSNicholas Bellinger {
37282ec5a8c1SNicholas Bellinger 	int ret;
37292ec5a8c1SNicholas Bellinger 
37302ec5a8c1SNicholas Bellinger 	switch (state) {
37312ec5a8c1SNicholas Bellinger 	case ISTATE_SEND_R2T:
37322ec5a8c1SNicholas Bellinger 		ret = iscsit_send_r2t(cmd, conn);
37332ec5a8c1SNicholas Bellinger 		if (ret < 0)
37342ec5a8c1SNicholas Bellinger 			goto err;
37352ec5a8c1SNicholas Bellinger 		break;
37362ec5a8c1SNicholas Bellinger 	case ISTATE_REMOVE:
37372ec5a8c1SNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
37385159d763SNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
37392ec5a8c1SNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
37402ec5a8c1SNicholas Bellinger 
3741aafc9d15SNicholas Bellinger 		iscsit_free_cmd(cmd, false);
37422ec5a8c1SNicholas Bellinger 		break;
37432ec5a8c1SNicholas Bellinger 	case ISTATE_SEND_NOPIN_WANT_RESPONSE:
37442ec5a8c1SNicholas Bellinger 		iscsit_mod_nopin_response_timer(conn);
37452ec5a8c1SNicholas Bellinger 		ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
37462ec5a8c1SNicholas Bellinger 		if (ret < 0)
37472ec5a8c1SNicholas Bellinger 			goto err;
37482ec5a8c1SNicholas Bellinger 		break;
37492ec5a8c1SNicholas Bellinger 	case ISTATE_SEND_NOPIN_NO_RESPONSE:
37502ec5a8c1SNicholas Bellinger 		ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
37512ec5a8c1SNicholas Bellinger 		if (ret < 0)
37522ec5a8c1SNicholas Bellinger 			goto err;
37532ec5a8c1SNicholas Bellinger 		break;
37542ec5a8c1SNicholas Bellinger 	default:
37552ec5a8c1SNicholas Bellinger 		pr_err("Unknown Opcode: 0x%02x ITT:"
37562ec5a8c1SNicholas Bellinger 		       " 0x%08x, i_state: %d on CID: %hu\n",
37572ec5a8c1SNicholas Bellinger 		       cmd->iscsi_opcode, cmd->init_task_tag, state,
37582ec5a8c1SNicholas Bellinger 		       conn->cid);
37592ec5a8c1SNicholas Bellinger 		goto err;
37602ec5a8c1SNicholas Bellinger 	}
37612ec5a8c1SNicholas Bellinger 
37622ec5a8c1SNicholas Bellinger 	return 0;
37632ec5a8c1SNicholas Bellinger 
37642ec5a8c1SNicholas Bellinger err:
37652ec5a8c1SNicholas Bellinger 	return -1;
37662ec5a8c1SNicholas Bellinger }
3767d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_immediate_queue);
37682ec5a8c1SNicholas Bellinger 
37692ec5a8c1SNicholas Bellinger static int
iscsit_handle_immediate_queue(struct iscsit_conn * conn)3770be36d683SMax Gurtovoy iscsit_handle_immediate_queue(struct iscsit_conn *conn)
37712ec5a8c1SNicholas Bellinger {
37722ec5a8c1SNicholas Bellinger 	struct iscsit_transport *t = conn->conn_transport;
37736f3c0e69SAndy Grover 	struct iscsi_queue_req *qr;
377466cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
3775e48354ceSNicholas Bellinger 	u8 state;
37766f3c0e69SAndy Grover 	int ret;
3777e48354ceSNicholas Bellinger 
3778c6037cc5SAndy Grover 	while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3779e48354ceSNicholas Bellinger 		atomic_set(&conn->check_immediate_queue, 0);
3780e48354ceSNicholas Bellinger 		cmd = qr->cmd;
3781e48354ceSNicholas Bellinger 		state = qr->state;
3782e48354ceSNicholas Bellinger 		kmem_cache_free(lio_qr_cache, qr);
3783e48354ceSNicholas Bellinger 
37842ec5a8c1SNicholas Bellinger 		ret = t->iscsit_immediate_queue(conn, cmd, state);
37856f3c0e69SAndy Grover 		if (ret < 0)
37862ec5a8c1SNicholas Bellinger 			return ret;
3787e48354ceSNicholas Bellinger 	}
3788e48354ceSNicholas Bellinger 
37896f3c0e69SAndy Grover 	return 0;
3790e48354ceSNicholas Bellinger }
37916f3c0e69SAndy Grover 
3792d2faaefbSVarun Prakash int
iscsit_response_queue(struct iscsit_conn * conn,struct iscsit_cmd * cmd,int state)3793be36d683SMax Gurtovoy iscsit_response_queue(struct iscsit_conn *conn, struct iscsit_cmd *cmd, int state)
37946f3c0e69SAndy Grover {
37956f3c0e69SAndy Grover 	int ret;
3796e48354ceSNicholas Bellinger 
3797e48354ceSNicholas Bellinger check_rsp_state:
3798e48354ceSNicholas Bellinger 	switch (state) {
3799e48354ceSNicholas Bellinger 	case ISTATE_SEND_DATAIN:
38002ec5a8c1SNicholas Bellinger 		ret = iscsit_send_datain(cmd, conn);
38016f3c0e69SAndy Grover 		if (ret < 0)
38026f3c0e69SAndy Grover 			goto err;
38036f3c0e69SAndy Grover 		else if (!ret)
38046f3c0e69SAndy Grover 			/* more drs */
38056f3c0e69SAndy Grover 			goto check_rsp_state;
38066f3c0e69SAndy Grover 		else if (ret == 1) {
38076f3c0e69SAndy Grover 			/* all done */
38086f3c0e69SAndy Grover 			spin_lock_bh(&cmd->istate_lock);
38096f3c0e69SAndy Grover 			cmd->i_state = ISTATE_SENT_STATUS;
3810e48354ceSNicholas Bellinger 			spin_unlock_bh(&cmd->istate_lock);
3811fd3a9025SNicholas Bellinger 
3812fd3a9025SNicholas Bellinger 			if (atomic_read(&conn->check_immediate_queue))
3813fd3a9025SNicholas Bellinger 				return 1;
3814fd3a9025SNicholas Bellinger 
38152ec5a8c1SNicholas Bellinger 			return 0;
38166f3c0e69SAndy Grover 		} else if (ret == 2) {
38176f3c0e69SAndy Grover 			/* Still must send status,
38186f3c0e69SAndy Grover 			   SCF_TRANSPORT_TASK_SENSE was set */
38196f3c0e69SAndy Grover 			spin_lock_bh(&cmd->istate_lock);
38206f3c0e69SAndy Grover 			cmd->i_state = ISTATE_SEND_STATUS;
38216f3c0e69SAndy Grover 			spin_unlock_bh(&cmd->istate_lock);
38226f3c0e69SAndy Grover 			state = ISTATE_SEND_STATUS;
38236f3c0e69SAndy Grover 			goto check_rsp_state;
38246f3c0e69SAndy Grover 		}
38256f3c0e69SAndy Grover 
3826e48354ceSNicholas Bellinger 		break;
3827e48354ceSNicholas Bellinger 	case ISTATE_SEND_STATUS:
3828e48354ceSNicholas Bellinger 	case ISTATE_SEND_STATUS_RECOVERY:
38292ec5a8c1SNicholas Bellinger 		ret = iscsit_send_response(cmd, conn);
3830e48354ceSNicholas Bellinger 		break;
3831e48354ceSNicholas Bellinger 	case ISTATE_SEND_LOGOUTRSP:
38322ec5a8c1SNicholas Bellinger 		ret = iscsit_send_logout(cmd, conn);
3833e48354ceSNicholas Bellinger 		break;
3834e48354ceSNicholas Bellinger 	case ISTATE_SEND_ASYNCMSG:
3835e48354ceSNicholas Bellinger 		ret = iscsit_send_conn_drop_async_message(
3836e48354ceSNicholas Bellinger 			cmd, conn);
3837e48354ceSNicholas Bellinger 		break;
3838e48354ceSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
38392ec5a8c1SNicholas Bellinger 		ret = iscsit_send_nopin(cmd, conn);
3840e48354ceSNicholas Bellinger 		break;
3841e48354ceSNicholas Bellinger 	case ISTATE_SEND_REJECT:
3842e48354ceSNicholas Bellinger 		ret = iscsit_send_reject(cmd, conn);
3843e48354ceSNicholas Bellinger 		break;
3844e48354ceSNicholas Bellinger 	case ISTATE_SEND_TASKMGTRSP:
3845e48354ceSNicholas Bellinger 		ret = iscsit_send_task_mgt_rsp(cmd, conn);
3846e48354ceSNicholas Bellinger 		if (ret != 0)
3847e48354ceSNicholas Bellinger 			break;
3848e48354ceSNicholas Bellinger 		ret = iscsit_tmr_post_handler(cmd, conn);
3849e48354ceSNicholas Bellinger 		if (ret != 0)
3850e48354ceSNicholas Bellinger 			iscsit_fall_back_to_erl0(conn->sess);
3851e48354ceSNicholas Bellinger 		break;
3852e48354ceSNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
3853e48354ceSNicholas Bellinger 		ret = iscsit_send_text_rsp(cmd, conn);
3854e48354ceSNicholas Bellinger 		break;
3855e48354ceSNicholas Bellinger 	default:
3856e48354ceSNicholas Bellinger 		pr_err("Unknown Opcode: 0x%02x ITT:"
3857e48354ceSNicholas Bellinger 		       " 0x%08x, i_state: %d on CID: %hu\n",
3858e48354ceSNicholas Bellinger 		       cmd->iscsi_opcode, cmd->init_task_tag,
3859e48354ceSNicholas Bellinger 		       state, conn->cid);
38606f3c0e69SAndy Grover 		goto err;
3861e48354ceSNicholas Bellinger 	}
3862c6037cc5SAndy Grover 	if (ret < 0)
38636f3c0e69SAndy Grover 		goto err;
3864e48354ceSNicholas Bellinger 
3865e48354ceSNicholas Bellinger 	switch (state) {
38666f3c0e69SAndy Grover 	case ISTATE_SEND_LOGOUTRSP:
38676f3c0e69SAndy Grover 		if (!iscsit_logout_post_handler(cmd, conn))
386888dcd2daSNicholas Bellinger 			return -ECONNRESET;
3869df561f66SGustavo A. R. Silva 		fallthrough;
3870e48354ceSNicholas Bellinger 	case ISTATE_SEND_STATUS:
3871e48354ceSNicholas Bellinger 	case ISTATE_SEND_ASYNCMSG:
3872e48354ceSNicholas Bellinger 	case ISTATE_SEND_NOPIN:
3873e48354ceSNicholas Bellinger 	case ISTATE_SEND_STATUS_RECOVERY:
3874e48354ceSNicholas Bellinger 	case ISTATE_SEND_TEXTRSP:
38756f3c0e69SAndy Grover 	case ISTATE_SEND_TASKMGTRSP:
3876ba159914SNicholas Bellinger 	case ISTATE_SEND_REJECT:
38776f3c0e69SAndy Grover 		spin_lock_bh(&cmd->istate_lock);
38786f3c0e69SAndy Grover 		cmd->i_state = ISTATE_SENT_STATUS;
38796f3c0e69SAndy Grover 		spin_unlock_bh(&cmd->istate_lock);
3880e48354ceSNicholas Bellinger 		break;
3881e48354ceSNicholas Bellinger 	default:
3882e48354ceSNicholas Bellinger 		pr_err("Unknown Opcode: 0x%02x ITT:"
3883e48354ceSNicholas Bellinger 		       " 0x%08x, i_state: %d on CID: %hu\n",
3884e48354ceSNicholas Bellinger 		       cmd->iscsi_opcode, cmd->init_task_tag,
3885e48354ceSNicholas Bellinger 		       cmd->i_state, conn->cid);
38866f3c0e69SAndy Grover 		goto err;
3887e48354ceSNicholas Bellinger 	}
3888e48354ceSNicholas Bellinger 
3889e48354ceSNicholas Bellinger 	if (atomic_read(&conn->check_immediate_queue))
3890fd3a9025SNicholas Bellinger 		return 1;
38916f3c0e69SAndy Grover 
38926f3c0e69SAndy Grover 	return 0;
38936f3c0e69SAndy Grover 
38946f3c0e69SAndy Grover err:
38956f3c0e69SAndy Grover 	return -1;
38966f3c0e69SAndy Grover }
3897d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_response_queue);
38986f3c0e69SAndy Grover 
iscsit_handle_response_queue(struct iscsit_conn * conn)3899be36d683SMax Gurtovoy static int iscsit_handle_response_queue(struct iscsit_conn *conn)
39002ec5a8c1SNicholas Bellinger {
39012ec5a8c1SNicholas Bellinger 	struct iscsit_transport *t = conn->conn_transport;
39022ec5a8c1SNicholas Bellinger 	struct iscsi_queue_req *qr;
390366cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
39042ec5a8c1SNicholas Bellinger 	u8 state;
39052ec5a8c1SNicholas Bellinger 	int ret;
39062ec5a8c1SNicholas Bellinger 
39072ec5a8c1SNicholas Bellinger 	while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
39082ec5a8c1SNicholas Bellinger 		cmd = qr->cmd;
39092ec5a8c1SNicholas Bellinger 		state = qr->state;
39102ec5a8c1SNicholas Bellinger 		kmem_cache_free(lio_qr_cache, qr);
39112ec5a8c1SNicholas Bellinger 
39122ec5a8c1SNicholas Bellinger 		ret = t->iscsit_response_queue(conn, cmd, state);
39132ec5a8c1SNicholas Bellinger 		if (ret == 1 || ret < 0)
39142ec5a8c1SNicholas Bellinger 			return ret;
39152ec5a8c1SNicholas Bellinger 	}
39162ec5a8c1SNicholas Bellinger 
39172ec5a8c1SNicholas Bellinger 	return 0;
39182ec5a8c1SNicholas Bellinger }
39192ec5a8c1SNicholas Bellinger 
iscsi_target_tx_thread(void * arg)39206f3c0e69SAndy Grover int iscsi_target_tx_thread(void *arg)
39216f3c0e69SAndy Grover {
39226f3c0e69SAndy Grover 	int ret = 0;
3923be36d683SMax Gurtovoy 	struct iscsit_conn *conn = arg;
39245e0cf5e6SJiang Yi 	bool conn_freed = false;
39255e0cf5e6SJiang Yi 
39266f3c0e69SAndy Grover 	/*
39276f3c0e69SAndy Grover 	 * Allow ourselves to be interrupted by SIGINT so that a
39286f3c0e69SAndy Grover 	 * connection recovery / failure event can be triggered externally.
39296f3c0e69SAndy Grover 	 */
39306f3c0e69SAndy Grover 	allow_signal(SIGINT);
39316f3c0e69SAndy Grover 
39326f3c0e69SAndy Grover 	while (!kthread_should_stop()) {
39336f3c0e69SAndy Grover 		/*
39346f3c0e69SAndy Grover 		 * Ensure that both TX and RX per connection kthreads
39356f3c0e69SAndy Grover 		 * are scheduled to run on the same CPU.
39366f3c0e69SAndy Grover 		 */
39376f3c0e69SAndy Grover 		iscsit_thread_check_cpumask(conn, current, 1);
39386f3c0e69SAndy Grover 
3939d5627acbSRoland Dreier 		wait_event_interruptible(conn->queues_wq,
394088dcd2daSNicholas Bellinger 					 !iscsit_conn_all_queues_empty(conn));
39416f3c0e69SAndy Grover 
394288dcd2daSNicholas Bellinger 		if (signal_pending(current))
39436f3c0e69SAndy Grover 			goto transport_err;
39446f3c0e69SAndy Grover 
3945fd3a9025SNicholas Bellinger get_immediate:
39462ec5a8c1SNicholas Bellinger 		ret = iscsit_handle_immediate_queue(conn);
39476f3c0e69SAndy Grover 		if (ret < 0)
39486f3c0e69SAndy Grover 			goto transport_err;
39496f3c0e69SAndy Grover 
39502ec5a8c1SNicholas Bellinger 		ret = iscsit_handle_response_queue(conn);
39515e0cf5e6SJiang Yi 		if (ret == 1) {
3952fd3a9025SNicholas Bellinger 			goto get_immediate;
39535e0cf5e6SJiang Yi 		} else if (ret == -ECONNRESET) {
39545e0cf5e6SJiang Yi 			conn_freed = true;
395588dcd2daSNicholas Bellinger 			goto out;
39565e0cf5e6SJiang Yi 		} else if (ret < 0) {
39576f3c0e69SAndy Grover 			goto transport_err;
3958e48354ceSNicholas Bellinger 		}
39595e0cf5e6SJiang Yi 	}
3960e48354ceSNicholas Bellinger 
3961e48354ceSNicholas Bellinger transport_err:
3962e5419865SNicholas Bellinger 	/*
3963e5419865SNicholas Bellinger 	 * Avoid the normal connection failure code-path if this connection
3964e5419865SNicholas Bellinger 	 * is still within LOGIN mode, and iscsi_np process context is
3965e5419865SNicholas Bellinger 	 * responsible for cleaning up the early connection failure.
3966e5419865SNicholas Bellinger 	 */
3967e5419865SNicholas Bellinger 	if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
39685e0cf5e6SJiang Yi 		iscsit_take_action_for_connection_exit(conn, &conn_freed);
3969e48354ceSNicholas Bellinger out:
39705e0cf5e6SJiang Yi 	if (!conn_freed) {
39715e0cf5e6SJiang Yi 		while (!kthread_should_stop()) {
39725e0cf5e6SJiang Yi 			msleep(100);
39735e0cf5e6SJiang Yi 		}
39745e0cf5e6SJiang Yi 	}
3975e48354ceSNicholas Bellinger 	return 0;
3976e48354ceSNicholas Bellinger }
3977e48354ceSNicholas Bellinger 
iscsi_target_rx_opcode(struct iscsit_conn * conn,unsigned char * buf)3978be36d683SMax Gurtovoy static int iscsi_target_rx_opcode(struct iscsit_conn *conn, unsigned char *buf)
39793e1c81a9SNicholas Bellinger {
39803e1c81a9SNicholas Bellinger 	struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
398166cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
39823e1c81a9SNicholas Bellinger 	int ret = 0;
39833e1c81a9SNicholas Bellinger 
39843e1c81a9SNicholas Bellinger 	switch (hdr->opcode & ISCSI_OPCODE_MASK) {
39853e1c81a9SNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
3986676687c6SNicholas Bellinger 		cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
39873e1c81a9SNicholas Bellinger 		if (!cmd)
3988ba159914SNicholas Bellinger 			goto reject;
39893e1c81a9SNicholas Bellinger 
39903e1c81a9SNicholas Bellinger 		ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
39913e1c81a9SNicholas Bellinger 		break;
39923e1c81a9SNicholas Bellinger 	case ISCSI_OP_SCSI_DATA_OUT:
39933e1c81a9SNicholas Bellinger 		ret = iscsit_handle_data_out(conn, buf);
39943e1c81a9SNicholas Bellinger 		break;
39953e1c81a9SNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
39963e1c81a9SNicholas Bellinger 		cmd = NULL;
39973e1c81a9SNicholas Bellinger 		if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
3998676687c6SNicholas Bellinger 			cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
39993e1c81a9SNicholas Bellinger 			if (!cmd)
4000ba159914SNicholas Bellinger 				goto reject;
40013e1c81a9SNicholas Bellinger 		}
40023e1c81a9SNicholas Bellinger 		ret = iscsit_handle_nop_out(conn, cmd, buf);
40033e1c81a9SNicholas Bellinger 		break;
40043e1c81a9SNicholas Bellinger 	case ISCSI_OP_SCSI_TMFUNC:
4005676687c6SNicholas Bellinger 		cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
40063e1c81a9SNicholas Bellinger 		if (!cmd)
4007ba159914SNicholas Bellinger 			goto reject;
40083e1c81a9SNicholas Bellinger 
40093e1c81a9SNicholas Bellinger 		ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
40103e1c81a9SNicholas Bellinger 		break;
40113e1c81a9SNicholas Bellinger 	case ISCSI_OP_TEXT:
4012e4f4e801SSagi Grimberg 		if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
4013e4f4e801SSagi Grimberg 			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
4014e4f4e801SSagi Grimberg 			if (!cmd)
4015e4f4e801SSagi Grimberg 				goto reject;
4016e4f4e801SSagi Grimberg 		} else {
4017676687c6SNicholas Bellinger 			cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
401864534aa7SNicholas Bellinger 			if (!cmd)
4019ba159914SNicholas Bellinger 				goto reject;
4020e4f4e801SSagi Grimberg 		}
402164534aa7SNicholas Bellinger 
402264534aa7SNicholas Bellinger 		ret = iscsit_handle_text_cmd(conn, cmd, buf);
40233e1c81a9SNicholas Bellinger 		break;
40243e1c81a9SNicholas Bellinger 	case ISCSI_OP_LOGOUT:
4025676687c6SNicholas Bellinger 		cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
40263e1c81a9SNicholas Bellinger 		if (!cmd)
4027ba159914SNicholas Bellinger 			goto reject;
40283e1c81a9SNicholas Bellinger 
40293e1c81a9SNicholas Bellinger 		ret = iscsit_handle_logout_cmd(conn, cmd, buf);
40303e1c81a9SNicholas Bellinger 		if (ret > 0)
40313e1c81a9SNicholas Bellinger 			wait_for_completion_timeout(&conn->conn_logout_comp,
40323e1c81a9SNicholas Bellinger 					SECONDS_FOR_LOGOUT_COMP * HZ);
40333e1c81a9SNicholas Bellinger 		break;
40343e1c81a9SNicholas Bellinger 	case ISCSI_OP_SNACK:
40353e1c81a9SNicholas Bellinger 		ret = iscsit_handle_snack(conn, buf);
40363e1c81a9SNicholas Bellinger 		break;
40373e1c81a9SNicholas Bellinger 	default:
40383e1c81a9SNicholas Bellinger 		pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
40393e1c81a9SNicholas Bellinger 		if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
40403e1c81a9SNicholas Bellinger 			pr_err("Cannot recover from unknown"
40413e1c81a9SNicholas Bellinger 			" opcode while ERL=0, closing iSCSI connection.\n");
40423e1c81a9SNicholas Bellinger 			return -1;
40433e1c81a9SNicholas Bellinger 		}
4044c04a6091SChristophe Vu-Brugier 		pr_err("Unable to recover from unknown opcode while OFMarker=No,"
4045c04a6091SChristophe Vu-Brugier 		       " closing iSCSI connection.\n");
4046c04a6091SChristophe Vu-Brugier 		ret = -1;
40473e1c81a9SNicholas Bellinger 		break;
40483e1c81a9SNicholas Bellinger 	}
40493e1c81a9SNicholas Bellinger 
40503e1c81a9SNicholas Bellinger 	return ret;
4051ba159914SNicholas Bellinger reject:
4052ba159914SNicholas Bellinger 	return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
40533e1c81a9SNicholas Bellinger }
40543e1c81a9SNicholas Bellinger 
iscsi_target_check_conn_state(struct iscsit_conn * conn)4055be36d683SMax Gurtovoy static bool iscsi_target_check_conn_state(struct iscsit_conn *conn)
4056ca82c2bdSNicholas Bellinger {
4057ca82c2bdSNicholas Bellinger 	bool ret;
4058ca82c2bdSNicholas Bellinger 
4059ca82c2bdSNicholas Bellinger 	spin_lock_bh(&conn->state_lock);
4060ca82c2bdSNicholas Bellinger 	ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
4061ca82c2bdSNicholas Bellinger 	spin_unlock_bh(&conn->state_lock);
4062ca82c2bdSNicholas Bellinger 
4063ca82c2bdSNicholas Bellinger 	return ret;
4064ca82c2bdSNicholas Bellinger }
4065ca82c2bdSNicholas Bellinger 
iscsit_get_rx_pdu(struct iscsit_conn * conn)4066be36d683SMax Gurtovoy static void iscsit_get_rx_pdu(struct iscsit_conn *conn)
4067e48354ceSNicholas Bellinger {
4068e8205ccaSVarun Prakash 	int ret;
40698f1f7d29SDmitry Bogdanov 	u8 *buffer, *tmp_buf, opcode;
4070e48354ceSNicholas Bellinger 	u32 checksum = 0, digest = 0;
40718f1f7d29SDmitry Bogdanov 	struct iscsi_hdr *hdr;
4072e48354ceSNicholas Bellinger 	struct kvec iov;
40733e1c81a9SNicholas Bellinger 
4074679fcae4SLaura Abbott 	buffer = kcalloc(ISCSI_HDR_LEN, sizeof(*buffer), GFP_KERNEL);
4075679fcae4SLaura Abbott 	if (!buffer)
4076679fcae4SLaura Abbott 		return;
4077679fcae4SLaura Abbott 
4078e48354ceSNicholas Bellinger 	while (!kthread_should_stop()) {
4079e48354ceSNicholas Bellinger 		/*
4080e48354ceSNicholas Bellinger 		 * Ensure that both TX and RX per connection kthreads
4081e48354ceSNicholas Bellinger 		 * are scheduled to run on the same CPU.
4082e48354ceSNicholas Bellinger 		 */
4083e48354ceSNicholas Bellinger 		iscsit_thread_check_cpumask(conn, current, 0);
4084e48354ceSNicholas Bellinger 
4085e48354ceSNicholas Bellinger 		memset(&iov, 0, sizeof(struct kvec));
4086e48354ceSNicholas Bellinger 
4087e48354ceSNicholas Bellinger 		iov.iov_base	= buffer;
4088e48354ceSNicholas Bellinger 		iov.iov_len	= ISCSI_HDR_LEN;
4089e48354ceSNicholas Bellinger 
4090e48354ceSNicholas Bellinger 		ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4091e48354ceSNicholas Bellinger 		if (ret != ISCSI_HDR_LEN) {
4092e48354ceSNicholas Bellinger 			iscsit_rx_thread_wait_for_tcp(conn);
4093679fcae4SLaura Abbott 			break;
4094e48354ceSNicholas Bellinger 		}
4095e48354ceSNicholas Bellinger 
40968f1f7d29SDmitry Bogdanov 		hdr = (struct iscsi_hdr *) buffer;
40978f1f7d29SDmitry Bogdanov 		if (hdr->hlength) {
40988f1f7d29SDmitry Bogdanov 			iov.iov_len = hdr->hlength * 4;
40998f1f7d29SDmitry Bogdanov 			tmp_buf = krealloc(buffer,
41008f1f7d29SDmitry Bogdanov 					  ISCSI_HDR_LEN + iov.iov_len,
41018f1f7d29SDmitry Bogdanov 					  GFP_KERNEL);
41028f1f7d29SDmitry Bogdanov 			if (!tmp_buf)
41038f1f7d29SDmitry Bogdanov 				break;
41048f1f7d29SDmitry Bogdanov 
41058f1f7d29SDmitry Bogdanov 			buffer = tmp_buf;
41068f1f7d29SDmitry Bogdanov 			iov.iov_base = &buffer[ISCSI_HDR_LEN];
41078f1f7d29SDmitry Bogdanov 
41088f1f7d29SDmitry Bogdanov 			ret = rx_data(conn, &iov, 1, iov.iov_len);
41098f1f7d29SDmitry Bogdanov 			if (ret != iov.iov_len) {
41108f1f7d29SDmitry Bogdanov 				iscsit_rx_thread_wait_for_tcp(conn);
41118f1f7d29SDmitry Bogdanov 				break;
41128f1f7d29SDmitry Bogdanov 			}
41138f1f7d29SDmitry Bogdanov 		}
41148f1f7d29SDmitry Bogdanov 
4115e48354ceSNicholas Bellinger 		if (conn->conn_ops->HeaderDigest) {
4116e48354ceSNicholas Bellinger 			iov.iov_base	= &digest;
4117e48354ceSNicholas Bellinger 			iov.iov_len	= ISCSI_CRC_LEN;
4118e48354ceSNicholas Bellinger 
4119e48354ceSNicholas Bellinger 			ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4120e48354ceSNicholas Bellinger 			if (ret != ISCSI_CRC_LEN) {
4121e48354ceSNicholas Bellinger 				iscsit_rx_thread_wait_for_tcp(conn);
4122679fcae4SLaura Abbott 				break;
4123e48354ceSNicholas Bellinger 			}
4124e48354ceSNicholas Bellinger 
4125e1dfb21fSBart Van Assche 			iscsit_do_crypto_hash_buf(conn->conn_rx_hash, buffer,
4126e1dfb21fSBart Van Assche 						  ISCSI_HDR_LEN, 0, NULL,
4127e1dfb21fSBart Van Assche 						  &checksum);
4128e48354ceSNicholas Bellinger 
4129e48354ceSNicholas Bellinger 			if (digest != checksum) {
4130e48354ceSNicholas Bellinger 				pr_err("HeaderDigest CRC32C failed,"
4131e48354ceSNicholas Bellinger 					" received 0x%08x, computed 0x%08x\n",
4132e48354ceSNicholas Bellinger 					digest, checksum);
4133e48354ceSNicholas Bellinger 				/*
4134e48354ceSNicholas Bellinger 				 * Set the PDU to 0xff so it will intentionally
4135e48354ceSNicholas Bellinger 				 * hit default in the switch below.
4136e48354ceSNicholas Bellinger 				 */
4137e48354ceSNicholas Bellinger 				memset(buffer, 0xff, ISCSI_HDR_LEN);
413804f3b31bSNicholas Bellinger 				atomic_long_inc(&conn->sess->conn_digest_errors);
4139e48354ceSNicholas Bellinger 			} else {
4140e48354ceSNicholas Bellinger 				pr_debug("Got HeaderDigest CRC32C"
4141e48354ceSNicholas Bellinger 						" 0x%08x\n", checksum);
4142e48354ceSNicholas Bellinger 			}
4143e48354ceSNicholas Bellinger 		}
4144e48354ceSNicholas Bellinger 
4145e48354ceSNicholas Bellinger 		if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4146679fcae4SLaura Abbott 			break;
4147e48354ceSNicholas Bellinger 
4148e48354ceSNicholas Bellinger 		opcode = buffer[0] & ISCSI_OPCODE_MASK;
4149e48354ceSNicholas Bellinger 
4150e48354ceSNicholas Bellinger 		if (conn->sess->sess_ops->SessionType &&
4151e48354ceSNicholas Bellinger 		   ((!(opcode & ISCSI_OP_TEXT)) ||
4152e48354ceSNicholas Bellinger 		    (!(opcode & ISCSI_OP_LOGOUT)))) {
4153e48354ceSNicholas Bellinger 			pr_err("Received illegal iSCSI Opcode: 0x%02x"
4154e48354ceSNicholas Bellinger 			" while in Discovery Session, rejecting.\n", opcode);
4155ba159914SNicholas Bellinger 			iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4156ba159914SNicholas Bellinger 					  buffer);
4157679fcae4SLaura Abbott 			break;
4158e48354ceSNicholas Bellinger 		}
4159e48354ceSNicholas Bellinger 
41603e1c81a9SNicholas Bellinger 		ret = iscsi_target_rx_opcode(conn, buffer);
41613e1c81a9SNicholas Bellinger 		if (ret < 0)
4162679fcae4SLaura Abbott 			break;
4163e8205ccaSVarun Prakash 	}
4164679fcae4SLaura Abbott 
4165679fcae4SLaura Abbott 	kfree(buffer);
4166e48354ceSNicholas Bellinger }
4167e48354ceSNicholas Bellinger 
iscsi_target_rx_thread(void * arg)4168e8205ccaSVarun Prakash int iscsi_target_rx_thread(void *arg)
4169e8205ccaSVarun Prakash {
4170e8205ccaSVarun Prakash 	int rc;
4171be36d683SMax Gurtovoy 	struct iscsit_conn *conn = arg;
41725e0cf5e6SJiang Yi 	bool conn_freed = false;
4173e8205ccaSVarun Prakash 
4174e8205ccaSVarun Prakash 	/*
4175e8205ccaSVarun Prakash 	 * Allow ourselves to be interrupted by SIGINT so that a
4176e8205ccaSVarun Prakash 	 * connection recovery / failure event can be triggered externally.
4177e8205ccaSVarun Prakash 	 */
4178e8205ccaSVarun Prakash 	allow_signal(SIGINT);
4179e8205ccaSVarun Prakash 	/*
4180e8205ccaSVarun Prakash 	 * Wait for iscsi_post_login_handler() to complete before allowing
4181e8205ccaSVarun Prakash 	 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4182e8205ccaSVarun Prakash 	 */
4183e8205ccaSVarun Prakash 	rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4184e8205ccaSVarun Prakash 	if (rc < 0 || iscsi_target_check_conn_state(conn))
41855e0cf5e6SJiang Yi 		goto out;
4186e8205ccaSVarun Prakash 
4187e8205ccaSVarun Prakash 	if (!conn->conn_transport->iscsit_get_rx_pdu)
4188e8205ccaSVarun Prakash 		return 0;
4189e8205ccaSVarun Prakash 
4190e8205ccaSVarun Prakash 	conn->conn_transport->iscsit_get_rx_pdu(conn);
4191e8205ccaSVarun Prakash 
4192e48354ceSNicholas Bellinger 	if (!signal_pending(current))
4193e48354ceSNicholas Bellinger 		atomic_set(&conn->transport_failed, 1);
41945e0cf5e6SJiang Yi 	iscsit_take_action_for_connection_exit(conn, &conn_freed);
41955e0cf5e6SJiang Yi 
41965e0cf5e6SJiang Yi out:
41975e0cf5e6SJiang Yi 	if (!conn_freed) {
41985e0cf5e6SJiang Yi 		while (!kthread_should_stop()) {
41995e0cf5e6SJiang Yi 			msleep(100);
42005e0cf5e6SJiang Yi 		}
42015e0cf5e6SJiang Yi 	}
42025e0cf5e6SJiang Yi 
4203e48354ceSNicholas Bellinger 	return 0;
4204e48354ceSNicholas Bellinger }
4205e48354ceSNicholas Bellinger 
iscsit_release_commands_from_conn(struct iscsit_conn * conn)4206be36d683SMax Gurtovoy static void iscsit_release_commands_from_conn(struct iscsit_conn *conn)
4207e48354ceSNicholas Bellinger {
4208064cdd2dSNicholas Bellinger 	LIST_HEAD(tmp_list);
420966cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd = NULL, *cmd_tmp = NULL;
42100873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
4211e48354ceSNicholas Bellinger 	/*
4212e48354ceSNicholas Bellinger 	 * We expect this function to only ever be called from either RX or TX
4213e48354ceSNicholas Bellinger 	 * thread context via iscsit_close_connection() once the other context
4214e48354ceSNicholas Bellinger 	 * has been reset -> returned sleeping pre-handler state.
4215e48354ceSNicholas Bellinger 	 */
4216e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
4217064cdd2dSNicholas Bellinger 	list_splice_init(&conn->conn_cmd_list, &tmp_list);
4218e48354ceSNicholas Bellinger 
4219f3619935SMike Christie 	list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4220064cdd2dSNicholas Bellinger 		struct se_cmd *se_cmd = &cmd->se_cmd;
4221e48354ceSNicholas Bellinger 
4222cc79da30SMike Christie 		if (!se_cmd->se_tfo)
4223cc79da30SMike Christie 			continue;
4224cc79da30SMike Christie 
422532e36bfbSBart Van Assche 		spin_lock_irq(&se_cmd->t_state_lock);
4226f3619935SMike Christie 		if (se_cmd->transport_state & CMD_T_ABORTED) {
4227cc79da30SMike Christie 			if (!(se_cmd->transport_state & CMD_T_TAS))
4228f3619935SMike Christie 				/*
4229f3619935SMike Christie 				 * LIO's abort path owns the cleanup for this,
4230f3619935SMike Christie 				 * so put it back on the list and let
4231f3619935SMike Christie 				 * aborted_task handle it.
4232f3619935SMike Christie 				 */
4233f3619935SMike Christie 				list_move_tail(&cmd->i_conn_node,
4234f3619935SMike Christie 					       &conn->conn_cmd_list);
4235f3619935SMike Christie 		} else {
4236064cdd2dSNicholas Bellinger 			se_cmd->transport_state |= CMD_T_FABRIC_STOP;
4237f3619935SMike Christie 		}
4238*ea87981aSDmitry Bogdanov 
4239*ea87981aSDmitry Bogdanov 		if (cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
4240*ea87981aSDmitry Bogdanov 			/*
4241*ea87981aSDmitry Bogdanov 			 * We never submitted the cmd to LIO core, so we have
4242*ea87981aSDmitry Bogdanov 			 * to tell LIO to perform the completion process.
4243*ea87981aSDmitry Bogdanov 			 */
4244*ea87981aSDmitry Bogdanov 			spin_unlock_irq(&se_cmd->t_state_lock);
4245*ea87981aSDmitry Bogdanov 			target_complete_cmd(&cmd->se_cmd, SAM_STAT_TASK_ABORTED);
4246*ea87981aSDmitry Bogdanov 			continue;
4247*ea87981aSDmitry Bogdanov 		}
424832e36bfbSBart Van Assche 		spin_unlock_irq(&se_cmd->t_state_lock);
4249064cdd2dSNicholas Bellinger 	}
4250e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
4251064cdd2dSNicholas Bellinger 
4252064cdd2dSNicholas Bellinger 	list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4253064cdd2dSNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
4254064cdd2dSNicholas Bellinger 
4255064cdd2dSNicholas Bellinger 		iscsit_increment_maxcmdsn(cmd, sess);
4256064cdd2dSNicholas Bellinger 		iscsit_free_cmd(cmd, true);
4257064cdd2dSNicholas Bellinger 
4258064cdd2dSNicholas Bellinger 	}
4259395cee83SMike Christie 
4260395cee83SMike Christie 	/*
4261395cee83SMike Christie 	 * Wait on commands that were cleaned up via the aborted_task path.
4262395cee83SMike Christie 	 * LLDs that implement iscsit_wait_conn will already have waited for
4263395cee83SMike Christie 	 * commands.
4264395cee83SMike Christie 	 */
4265395cee83SMike Christie 	if (!conn->conn_transport->iscsit_wait_conn) {
4266395cee83SMike Christie 		target_stop_cmd_counter(conn->cmd_cnt);
4267395cee83SMike Christie 		target_wait_for_cmds(conn->cmd_cnt);
4268395cee83SMike Christie 	}
4269e48354ceSNicholas Bellinger }
4270e48354ceSNicholas Bellinger 
iscsit_stop_timers_for_cmds(struct iscsit_conn * conn)4271e48354ceSNicholas Bellinger static void iscsit_stop_timers_for_cmds(
4272be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
4273e48354ceSNicholas Bellinger {
427466cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
4275e48354ceSNicholas Bellinger 
4276e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
42772fbb471eSAndy Grover 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
4278e48354ceSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE)
4279e48354ceSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
4280e48354ceSNicholas Bellinger 	}
4281e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
4282e48354ceSNicholas Bellinger }
4283e48354ceSNicholas Bellinger 
iscsit_close_connection(struct iscsit_conn * conn)4284e48354ceSNicholas Bellinger int iscsit_close_connection(
4285be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
4286e48354ceSNicholas Bellinger {
4287e48354ceSNicholas Bellinger 	int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
42880873fe44SMax Gurtovoy 	struct iscsit_session	*sess = conn->sess;
4289e48354ceSNicholas Bellinger 
4290e48354ceSNicholas Bellinger 	pr_debug("Closing iSCSI connection CID %hu on SID:"
4291e48354ceSNicholas Bellinger 		" %u\n", conn->cid, sess->sid);
4292e48354ceSNicholas Bellinger 	/*
4293b4869ee9SVarun Prakash 	 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4294b4869ee9SVarun Prakash 	 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4295b4869ee9SVarun Prakash 	 * sleeping and the logout response never got sent because the
4296b4869ee9SVarun Prakash 	 * connection failed.
4297f068fbc8SNicholas Bellinger 	 *
4298f068fbc8SNicholas Bellinger 	 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4299f068fbc8SNicholas Bellinger 	 * to signal logout response TX interrupt completion.  Go ahead and skip
4300f068fbc8SNicholas Bellinger 	 * this for iser since isert_rx_opcode() does not wait on logout failure,
4301be36d683SMax Gurtovoy 	 * and to avoid iscsit_conn pointer dereference in iser-target code.
4302e48354ceSNicholas Bellinger 	 */
4303bd027d85SNicholas Bellinger 	if (!conn->conn_transport->rdma_shutdown)
4304e48354ceSNicholas Bellinger 		complete(&conn->conn_logout_comp);
4305e48354ceSNicholas Bellinger 
430688dcd2daSNicholas Bellinger 	if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
430788dcd2daSNicholas Bellinger 		if (conn->tx_thread &&
430888dcd2daSNicholas Bellinger 		    cmpxchg(&conn->tx_thread_active, true, false)) {
430988dcd2daSNicholas Bellinger 			send_sig(SIGINT, conn->tx_thread, 1);
431088dcd2daSNicholas Bellinger 			kthread_stop(conn->tx_thread);
431188dcd2daSNicholas Bellinger 		}
431288dcd2daSNicholas Bellinger 	} else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
431388dcd2daSNicholas Bellinger 		if (conn->rx_thread &&
431488dcd2daSNicholas Bellinger 		    cmpxchg(&conn->rx_thread_active, true, false)) {
431588dcd2daSNicholas Bellinger 			send_sig(SIGINT, conn->rx_thread, 1);
431688dcd2daSNicholas Bellinger 			kthread_stop(conn->rx_thread);
431788dcd2daSNicholas Bellinger 		}
431888dcd2daSNicholas Bellinger 	}
431988dcd2daSNicholas Bellinger 
432088dcd2daSNicholas Bellinger 	spin_lock(&iscsit_global->ts_bitmap_lock);
432188dcd2daSNicholas Bellinger 	bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
432288dcd2daSNicholas Bellinger 			      get_order(1));
432388dcd2daSNicholas Bellinger 	spin_unlock(&iscsit_global->ts_bitmap_lock);
4324e48354ceSNicholas Bellinger 
4325e48354ceSNicholas Bellinger 	iscsit_stop_timers_for_cmds(conn);
4326e48354ceSNicholas Bellinger 	iscsit_stop_nopin_response_timer(conn);
4327e48354ceSNicholas Bellinger 	iscsit_stop_nopin_timer(conn);
4328defd8848SNicholas Bellinger 
432976261adaSBart Van Assche 	if (conn->conn_transport->iscsit_wait_conn)
433076261adaSBart Van Assche 		conn->conn_transport->iscsit_wait_conn(conn);
433176261adaSBart Van Assche 
4332e48354ceSNicholas Bellinger 	/*
4333e48354ceSNicholas Bellinger 	 * During Connection recovery drop unacknowledged out of order
4334e48354ceSNicholas Bellinger 	 * commands for this connection, and prepare the other commands
433553c561dcSBart Van Assche 	 * for reallegiance.
4336e48354ceSNicholas Bellinger 	 *
4337e48354ceSNicholas Bellinger 	 * During normal operation clear the out of order commands (but
4338e48354ceSNicholas Bellinger 	 * do not free the struct iscsi_ooo_cmdsn's) and release all
433966cd9d4eSMax Gurtovoy 	 * struct iscsit_cmds.
4340e48354ceSNicholas Bellinger 	 */
4341e48354ceSNicholas Bellinger 	if (atomic_read(&conn->connection_recovery)) {
4342e48354ceSNicholas Bellinger 		iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
434353c561dcSBart Van Assche 		iscsit_prepare_cmds_for_reallegiance(conn);
4344e48354ceSNicholas Bellinger 	} else {
4345e48354ceSNicholas Bellinger 		iscsit_clear_ooo_cmdsns_for_conn(conn);
4346e48354ceSNicholas Bellinger 		iscsit_release_commands_from_conn(conn);
4347e48354ceSNicholas Bellinger 	}
4348bbc05048SNicholas Bellinger 	iscsit_free_queue_reqs_for_conn(conn);
4349e48354ceSNicholas Bellinger 
4350e48354ceSNicholas Bellinger 	/*
4351e48354ceSNicholas Bellinger 	 * Handle decrementing session or connection usage count if
4352e48354ceSNicholas Bellinger 	 * a logout response was not able to be sent because the
4353e48354ceSNicholas Bellinger 	 * connection failed.  Fall back to Session Recovery here.
4354e48354ceSNicholas Bellinger 	 */
4355e48354ceSNicholas Bellinger 	if (atomic_read(&conn->conn_logout_remove)) {
4356e48354ceSNicholas Bellinger 		if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4357e48354ceSNicholas Bellinger 			iscsit_dec_conn_usage_count(conn);
4358e48354ceSNicholas Bellinger 			iscsit_dec_session_usage_count(sess);
4359e48354ceSNicholas Bellinger 		}
4360e48354ceSNicholas Bellinger 		if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4361e48354ceSNicholas Bellinger 			iscsit_dec_conn_usage_count(conn);
4362e48354ceSNicholas Bellinger 
4363e48354ceSNicholas Bellinger 		atomic_set(&conn->conn_logout_remove, 0);
4364e48354ceSNicholas Bellinger 		atomic_set(&sess->session_reinstatement, 0);
4365e48354ceSNicholas Bellinger 		atomic_set(&sess->session_fall_back_to_erl0, 1);
4366e48354ceSNicholas Bellinger 	}
4367e48354ceSNicholas Bellinger 
4368e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
4369e48354ceSNicholas Bellinger 	list_del(&conn->conn_list);
4370e48354ceSNicholas Bellinger 
4371e48354ceSNicholas Bellinger 	/*
4372e48354ceSNicholas Bellinger 	 * Attempt to let the Initiator know this connection failed by
4373e48354ceSNicholas Bellinger 	 * sending an Connection Dropped Async Message on another
4374e48354ceSNicholas Bellinger 	 * active connection.
4375e48354ceSNicholas Bellinger 	 */
4376e48354ceSNicholas Bellinger 	if (atomic_read(&conn->connection_recovery))
4377e48354ceSNicholas Bellinger 		iscsit_build_conn_drop_async_message(conn);
4378e48354ceSNicholas Bellinger 
4379e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
4380e48354ceSNicholas Bellinger 
4381e48354ceSNicholas Bellinger 	/*
4382e48354ceSNicholas Bellinger 	 * If connection reinstatement is being performed on this connection,
4383e48354ceSNicholas Bellinger 	 * up the connection reinstatement semaphore that is being blocked on
4384e48354ceSNicholas Bellinger 	 * in iscsit_cause_connection_reinstatement().
4385e48354ceSNicholas Bellinger 	 */
4386e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->state_lock);
4387e48354ceSNicholas Bellinger 	if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4388e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->state_lock);
4389e48354ceSNicholas Bellinger 		complete(&conn->conn_wait_comp);
4390e48354ceSNicholas Bellinger 		wait_for_completion(&conn->conn_post_wait_comp);
4391e48354ceSNicholas Bellinger 		spin_lock_bh(&conn->state_lock);
4392e48354ceSNicholas Bellinger 	}
4393e48354ceSNicholas Bellinger 
4394e48354ceSNicholas Bellinger 	/*
4395e48354ceSNicholas Bellinger 	 * If connection reinstatement is being performed on this connection
4396e48354ceSNicholas Bellinger 	 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4397e48354ceSNicholas Bellinger 	 * connection wait rcfr semaphore that is being blocked on
4398e48354ceSNicholas Bellinger 	 * an iscsit_connection_reinstatement_rcfr().
4399e48354ceSNicholas Bellinger 	 */
4400e48354ceSNicholas Bellinger 	if (atomic_read(&conn->connection_wait_rcfr)) {
4401e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->state_lock);
4402e48354ceSNicholas Bellinger 		complete(&conn->conn_wait_rcfr_comp);
4403e48354ceSNicholas Bellinger 		wait_for_completion(&conn->conn_post_wait_comp);
4404e48354ceSNicholas Bellinger 		spin_lock_bh(&conn->state_lock);
4405e48354ceSNicholas Bellinger 	}
4406e48354ceSNicholas Bellinger 	atomic_set(&conn->connection_reinstatement, 1);
4407e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->state_lock);
4408e48354ceSNicholas Bellinger 
4409e48354ceSNicholas Bellinger 	/*
4410e48354ceSNicholas Bellinger 	 * If any other processes are accessing this connection pointer we
4411e48354ceSNicholas Bellinger 	 * must wait until they have completed.
4412e48354ceSNicholas Bellinger 	 */
4413e48354ceSNicholas Bellinger 	iscsit_check_conn_usage_count(conn);
4414e48354ceSNicholas Bellinger 
441569110e3cSHerbert Xu 	ahash_request_free(conn->conn_tx_hash);
441669110e3cSHerbert Xu 	if (conn->conn_rx_hash) {
441769110e3cSHerbert Xu 		struct crypto_ahash *tfm;
441869110e3cSHerbert Xu 
441969110e3cSHerbert Xu 		tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
442069110e3cSHerbert Xu 		ahash_request_free(conn->conn_rx_hash);
442169110e3cSHerbert Xu 		crypto_free_ahash(tfm);
442269110e3cSHerbert Xu 	}
4423e48354ceSNicholas Bellinger 
4424bf6932f4SAl Viro 	if (conn->sock)
4425e48354ceSNicholas Bellinger 		sock_release(conn->sock);
4426baa4d64bSNicholas Bellinger 
4427baa4d64bSNicholas Bellinger 	if (conn->conn_transport->iscsit_free_conn)
4428baa4d64bSNicholas Bellinger 		conn->conn_transport->iscsit_free_conn(conn);
4429baa4d64bSNicholas Bellinger 
4430e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4431e48354ceSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_FREE;
443205a86e78SMike Christie 	iscsit_free_conn(conn);
4433e48354ceSNicholas Bellinger 
4434e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
4435e48354ceSNicholas Bellinger 	atomic_dec(&sess->nconn);
443671b25693SJustin Stitt 	pr_debug("Decremented iSCSI connection count to %d from node:"
4437e48354ceSNicholas Bellinger 		" %s\n", atomic_read(&sess->nconn),
4438e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
4439e48354ceSNicholas Bellinger 	/*
4440e48354ceSNicholas Bellinger 	 * Make sure that if one connection fails in an non ERL=2 iSCSI
4441e48354ceSNicholas Bellinger 	 * Session that they all fail.
4442e48354ceSNicholas Bellinger 	 */
4443e48354ceSNicholas Bellinger 	if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4444e48354ceSNicholas Bellinger 	     !atomic_read(&sess->session_logout))
4445e48354ceSNicholas Bellinger 		atomic_set(&sess->session_fall_back_to_erl0, 1);
4446e48354ceSNicholas Bellinger 
4447e48354ceSNicholas Bellinger 	/*
4448e48354ceSNicholas Bellinger 	 * If this was not the last connection in the session, and we are
4449e48354ceSNicholas Bellinger 	 * performing session reinstatement or falling back to ERL=0, call
4450e48354ceSNicholas Bellinger 	 * iscsit_stop_session() without sleeping to shutdown the other
4451e48354ceSNicholas Bellinger 	 * active connections.
4452e48354ceSNicholas Bellinger 	 */
4453e48354ceSNicholas Bellinger 	if (atomic_read(&sess->nconn)) {
4454e48354ceSNicholas Bellinger 		if (!atomic_read(&sess->session_reinstatement) &&
4455e48354ceSNicholas Bellinger 		    !atomic_read(&sess->session_fall_back_to_erl0)) {
4456e48354ceSNicholas Bellinger 			spin_unlock_bh(&sess->conn_lock);
4457e48354ceSNicholas Bellinger 			return 0;
4458e48354ceSNicholas Bellinger 		}
4459e48354ceSNicholas Bellinger 		if (!atomic_read(&sess->session_stop_active)) {
4460e48354ceSNicholas Bellinger 			atomic_set(&sess->session_stop_active, 1);
4461e48354ceSNicholas Bellinger 			spin_unlock_bh(&sess->conn_lock);
4462e48354ceSNicholas Bellinger 			iscsit_stop_session(sess, 0, 0);
4463e48354ceSNicholas Bellinger 			return 0;
4464e48354ceSNicholas Bellinger 		}
4465e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
4466e48354ceSNicholas Bellinger 		return 0;
4467e48354ceSNicholas Bellinger 	}
4468e48354ceSNicholas Bellinger 
4469e48354ceSNicholas Bellinger 	/*
4470e48354ceSNicholas Bellinger 	 * If this was the last connection in the session and one of the
4471e48354ceSNicholas Bellinger 	 * following is occurring:
4472e48354ceSNicholas Bellinger 	 *
4473e48354ceSNicholas Bellinger 	 * Session Reinstatement is not being performed, and are falling back
4474e48354ceSNicholas Bellinger 	 * to ERL=0 call iscsit_close_session().
4475e48354ceSNicholas Bellinger 	 *
4476e48354ceSNicholas Bellinger 	 * Session Logout was requested.  iscsit_close_session() will be called
4477e48354ceSNicholas Bellinger 	 * elsewhere.
4478e48354ceSNicholas Bellinger 	 *
4479e48354ceSNicholas Bellinger 	 * Session Continuation is not being performed, start the Time2Retain
4480e48354ceSNicholas Bellinger 	 * handler and check if sleep_on_sess_wait_sem is active.
4481e48354ceSNicholas Bellinger 	 */
4482e48354ceSNicholas Bellinger 	if (!atomic_read(&sess->session_reinstatement) &&
4483e48354ceSNicholas Bellinger 	     atomic_read(&sess->session_fall_back_to_erl0)) {
4484e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
448557c46e9fSMaurizio Lombardi 		complete_all(&sess->session_wait_comp);
448643367548SSebastian Andrzej Siewior 		iscsit_close_session(sess, true);
4487e48354ceSNicholas Bellinger 
4488e48354ceSNicholas Bellinger 		return 0;
4489e48354ceSNicholas Bellinger 	} else if (atomic_read(&sess->session_logout)) {
4490e48354ceSNicholas Bellinger 		pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4491e48354ceSNicholas Bellinger 		sess->session_state = TARG_SESS_STATE_FREE;
4492e48354ceSNicholas Bellinger 
449357c46e9fSMaurizio Lombardi 		if (atomic_read(&sess->session_close)) {
449457c46e9fSMaurizio Lombardi 			spin_unlock_bh(&sess->conn_lock);
449557c46e9fSMaurizio Lombardi 			complete_all(&sess->session_wait_comp);
449643367548SSebastian Andrzej Siewior 			iscsit_close_session(sess, true);
449757c46e9fSMaurizio Lombardi 		} else {
449857c46e9fSMaurizio Lombardi 			spin_unlock_bh(&sess->conn_lock);
449957c46e9fSMaurizio Lombardi 		}
4500e48354ceSNicholas Bellinger 
4501e48354ceSNicholas Bellinger 		return 0;
4502e48354ceSNicholas Bellinger 	} else {
4503e48354ceSNicholas Bellinger 		pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4504e48354ceSNicholas Bellinger 		sess->session_state = TARG_SESS_STATE_FAILED;
4505e48354ceSNicholas Bellinger 
450657c46e9fSMaurizio Lombardi 		if (!atomic_read(&sess->session_continuation))
4507e48354ceSNicholas Bellinger 			iscsit_start_time2retain_handler(sess);
4508e48354ceSNicholas Bellinger 
450957c46e9fSMaurizio Lombardi 		if (atomic_read(&sess->session_close)) {
451057c46e9fSMaurizio Lombardi 			spin_unlock_bh(&sess->conn_lock);
451157c46e9fSMaurizio Lombardi 			complete_all(&sess->session_wait_comp);
451243367548SSebastian Andrzej Siewior 			iscsit_close_session(sess, true);
451357c46e9fSMaurizio Lombardi 		} else {
451457c46e9fSMaurizio Lombardi 			spin_unlock_bh(&sess->conn_lock);
451557c46e9fSMaurizio Lombardi 		}
4516e48354ceSNicholas Bellinger 
4517e48354ceSNicholas Bellinger 		return 0;
4518e48354ceSNicholas Bellinger 	}
4519e48354ceSNicholas Bellinger }
4520e48354ceSNicholas Bellinger 
452144f33d0fSChristoph Hellwig /*
452244f33d0fSChristoph Hellwig  * If the iSCSI Session for the iSCSI Initiator Node exists,
452344f33d0fSChristoph Hellwig  * forcefully shutdown the iSCSI NEXUS.
452444f33d0fSChristoph Hellwig  */
iscsit_close_session(struct iscsit_session * sess,bool can_sleep)45250873fe44SMax Gurtovoy int iscsit_close_session(struct iscsit_session *sess, bool can_sleep)
4526e48354ceSNicholas Bellinger {
452760bfcf8eSAndy Grover 	struct iscsi_portal_group *tpg = sess->tpg;
4528e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4529e48354ceSNicholas Bellinger 
4530e48354ceSNicholas Bellinger 	if (atomic_read(&sess->nconn)) {
4531e48354ceSNicholas Bellinger 		pr_err("%d connection(s) still exist for iSCSI session"
4532e48354ceSNicholas Bellinger 			" to %s\n", atomic_read(&sess->nconn),
4533e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
4534e48354ceSNicholas Bellinger 		BUG();
4535e48354ceSNicholas Bellinger 	}
4536e48354ceSNicholas Bellinger 
4537e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
4538e48354ceSNicholas Bellinger 	atomic_set(&sess->session_logout, 1);
4539e48354ceSNicholas Bellinger 	atomic_set(&sess->session_reinstatement, 1);
4540e48354ceSNicholas Bellinger 	iscsit_stop_time2retain_timer(sess);
4541e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
4542e48354ceSNicholas Bellinger 
4543d8990b5aSDmitry Bogdanov 	if (sess->sess_ops->ErrorRecoveryLevel == 2)
4544d8990b5aSDmitry Bogdanov 		iscsit_free_connection_recovery_entries(sess);
4545d8990b5aSDmitry Bogdanov 
4546e48354ceSNicholas Bellinger 	/*
4547e48354ceSNicholas Bellinger 	 * transport_deregister_session_configfs() will clear the
4548e48354ceSNicholas Bellinger 	 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4549e48354ceSNicholas Bellinger 	 * can be setting it again with __transport_register_session() in
4550e48354ceSNicholas Bellinger 	 * iscsi_post_login_handler() again after the iscsit_stop_session()
4551e48354ceSNicholas Bellinger 	 * completes in iscsi_np context.
4552e48354ceSNicholas Bellinger 	 */
4553e48354ceSNicholas Bellinger 	transport_deregister_session_configfs(sess->se_sess);
4554e48354ceSNicholas Bellinger 
4555e48354ceSNicholas Bellinger 	/*
4556e48354ceSNicholas Bellinger 	 * If any other processes are accessing this session pointer we must
4557e48354ceSNicholas Bellinger 	 * wait until they have completed.  If we are in an interrupt (the
4558e48354ceSNicholas Bellinger 	 * time2retain handler) and contain and active session usage count we
4559e48354ceSNicholas Bellinger 	 * restart the timer and exit.
4560e48354ceSNicholas Bellinger 	 */
4561f88a10f8SSebastian Andrzej Siewior 	if (iscsit_check_session_usage_count(sess, can_sleep)) {
4562e48354ceSNicholas Bellinger 		atomic_set(&sess->session_logout, 0);
4563e48354ceSNicholas Bellinger 		iscsit_start_time2retain_handler(sess);
4564e48354ceSNicholas Bellinger 		return 0;
4565e48354ceSNicholas Bellinger 	}
4566e48354ceSNicholas Bellinger 
4567e48354ceSNicholas Bellinger 	transport_deregister_session(sess->se_sess);
4568e48354ceSNicholas Bellinger 
4569e48354ceSNicholas Bellinger 	iscsit_free_all_ooo_cmdsns(sess);
4570e48354ceSNicholas Bellinger 
4571e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
4572e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4573e48354ceSNicholas Bellinger 	sess->session_state = TARG_SESS_STATE_FREE;
4574e48354ceSNicholas Bellinger 	pr_debug("Released iSCSI session from node: %s\n",
4575e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
4576e48354ceSNicholas Bellinger 	tpg->nsessions--;
4577e48354ceSNicholas Bellinger 	if (tpg->tpg_tiqn)
4578e48354ceSNicholas Bellinger 		tpg->tpg_tiqn->tiqn_nsessions--;
4579e48354ceSNicholas Bellinger 
4580e48354ceSNicholas Bellinger 	pr_debug("Decremented number of active iSCSI Sessions on"
4581e48354ceSNicholas Bellinger 		" iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4582e48354ceSNicholas Bellinger 
458331ff0ceeSMatthew Wilcox 	ida_free(&sess_ida, sess->session_index);
4584e48354ceSNicholas Bellinger 	kfree(sess->sess_ops);
4585e48354ceSNicholas Bellinger 	sess->sess_ops = NULL;
4586e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
4587e48354ceSNicholas Bellinger 
4588e48354ceSNicholas Bellinger 	kfree(sess);
4589e48354ceSNicholas Bellinger 	return 0;
4590e48354ceSNicholas Bellinger }
4591e48354ceSNicholas Bellinger 
iscsit_logout_post_handler_closesession(struct iscsit_conn * conn)4592e48354ceSNicholas Bellinger static void iscsit_logout_post_handler_closesession(
4593be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
4594e48354ceSNicholas Bellinger {
45950873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
4596007d038bSNicholas Bellinger 	int sleep = 1;
4597007d038bSNicholas Bellinger 	/*
4598007d038bSNicholas Bellinger 	 * Traditional iscsi/tcp will invoke this logic from TX thread
4599007d038bSNicholas Bellinger 	 * context during session logout, so clear tx_thread_active and
4600007d038bSNicholas Bellinger 	 * sleep if iscsit_close_connection() has not already occured.
4601007d038bSNicholas Bellinger 	 *
4602007d038bSNicholas Bellinger 	 * Since iser-target invokes this logic from it's own workqueue,
4603007d038bSNicholas Bellinger 	 * always sleep waiting for RX/TX thread shutdown to complete
4604007d038bSNicholas Bellinger 	 * within iscsit_close_connection().
4605007d038bSNicholas Bellinger 	 */
4606105fa2f4SNicholas Bellinger 	if (!conn->conn_transport->rdma_shutdown) {
4607007d038bSNicholas Bellinger 		sleep = cmpxchg(&conn->tx_thread_active, true, false);
4608105fa2f4SNicholas Bellinger 		if (!sleep)
4609105fa2f4SNicholas Bellinger 			return;
4610105fa2f4SNicholas Bellinger 	}
4611e48354ceSNicholas Bellinger 
4612e48354ceSNicholas Bellinger 	atomic_set(&conn->conn_logout_remove, 0);
4613e48354ceSNicholas Bellinger 	complete(&conn->conn_logout_comp);
4614e48354ceSNicholas Bellinger 
4615e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
461657c46e9fSMaurizio Lombardi 	atomic_set(&sess->session_close, 1);
461788dcd2daSNicholas Bellinger 	iscsit_stop_session(sess, sleep, sleep);
4618e48354ceSNicholas Bellinger 	iscsit_dec_session_usage_count(sess);
4619e48354ceSNicholas Bellinger }
4620e48354ceSNicholas Bellinger 
iscsit_logout_post_handler_samecid(struct iscsit_conn * conn)4621e48354ceSNicholas Bellinger static void iscsit_logout_post_handler_samecid(
4622be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
4623e48354ceSNicholas Bellinger {
4624007d038bSNicholas Bellinger 	int sleep = 1;
4625007d038bSNicholas Bellinger 
4626105fa2f4SNicholas Bellinger 	if (!conn->conn_transport->rdma_shutdown) {
4627007d038bSNicholas Bellinger 		sleep = cmpxchg(&conn->tx_thread_active, true, false);
4628105fa2f4SNicholas Bellinger 		if (!sleep)
4629105fa2f4SNicholas Bellinger 			return;
4630105fa2f4SNicholas Bellinger 	}
4631e48354ceSNicholas Bellinger 
4632e48354ceSNicholas Bellinger 	atomic_set(&conn->conn_logout_remove, 0);
4633e48354ceSNicholas Bellinger 	complete(&conn->conn_logout_comp);
4634e48354ceSNicholas Bellinger 
463588dcd2daSNicholas Bellinger 	iscsit_cause_connection_reinstatement(conn, sleep);
4636e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
4637e48354ceSNicholas Bellinger }
4638e48354ceSNicholas Bellinger 
iscsit_logout_post_handler_diffcid(struct iscsit_conn * conn,u16 cid)4639e48354ceSNicholas Bellinger static void iscsit_logout_post_handler_diffcid(
4640be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
4641e48354ceSNicholas Bellinger 	u16 cid)
4642e48354ceSNicholas Bellinger {
4643be36d683SMax Gurtovoy 	struct iscsit_conn *l_conn;
46440873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
4645b53b0d99SNicholas Bellinger 	bool conn_found = false;
4646e48354ceSNicholas Bellinger 
4647e48354ceSNicholas Bellinger 	if (!sess)
4648e48354ceSNicholas Bellinger 		return;
4649e48354ceSNicholas Bellinger 
4650e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
4651e48354ceSNicholas Bellinger 	list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4652e48354ceSNicholas Bellinger 		if (l_conn->cid == cid) {
4653e48354ceSNicholas Bellinger 			iscsit_inc_conn_usage_count(l_conn);
4654b53b0d99SNicholas Bellinger 			conn_found = true;
4655e48354ceSNicholas Bellinger 			break;
4656e48354ceSNicholas Bellinger 		}
4657e48354ceSNicholas Bellinger 	}
4658e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
4659e48354ceSNicholas Bellinger 
4660b53b0d99SNicholas Bellinger 	if (!conn_found)
4661e48354ceSNicholas Bellinger 		return;
4662e48354ceSNicholas Bellinger 
4663e48354ceSNicholas Bellinger 	if (l_conn->sock)
4664e48354ceSNicholas Bellinger 		l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4665e48354ceSNicholas Bellinger 
4666e48354ceSNicholas Bellinger 	spin_lock_bh(&l_conn->state_lock);
4667e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4668e48354ceSNicholas Bellinger 	l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4669e48354ceSNicholas Bellinger 	spin_unlock_bh(&l_conn->state_lock);
4670e48354ceSNicholas Bellinger 
4671e48354ceSNicholas Bellinger 	iscsit_cause_connection_reinstatement(l_conn, 1);
4672e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(l_conn);
4673e48354ceSNicholas Bellinger }
4674e48354ceSNicholas Bellinger 
4675e48354ceSNicholas Bellinger /*
4676e48354ceSNicholas Bellinger  *	Return of 0 causes the TX thread to restart.
4677e48354ceSNicholas Bellinger  */
iscsit_logout_post_handler(struct iscsit_cmd * cmd,struct iscsit_conn * conn)46782ec5a8c1SNicholas Bellinger int iscsit_logout_post_handler(
467966cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
4680be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
4681e48354ceSNicholas Bellinger {
4682e48354ceSNicholas Bellinger 	int ret = 0;
4683e48354ceSNicholas Bellinger 
4684e48354ceSNicholas Bellinger 	switch (cmd->logout_reason) {
4685e48354ceSNicholas Bellinger 	case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4686e48354ceSNicholas Bellinger 		switch (cmd->logout_response) {
4687e48354ceSNicholas Bellinger 		case ISCSI_LOGOUT_SUCCESS:
4688e48354ceSNicholas Bellinger 		case ISCSI_LOGOUT_CLEANUP_FAILED:
4689e48354ceSNicholas Bellinger 		default:
4690e48354ceSNicholas Bellinger 			iscsit_logout_post_handler_closesession(conn);
4691e48354ceSNicholas Bellinger 			break;
4692e48354ceSNicholas Bellinger 		}
4693e48354ceSNicholas Bellinger 		break;
4694e48354ceSNicholas Bellinger 	case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4695e48354ceSNicholas Bellinger 		if (conn->cid == cmd->logout_cid) {
4696e48354ceSNicholas Bellinger 			switch (cmd->logout_response) {
4697e48354ceSNicholas Bellinger 			case ISCSI_LOGOUT_SUCCESS:
4698e48354ceSNicholas Bellinger 			case ISCSI_LOGOUT_CLEANUP_FAILED:
4699e48354ceSNicholas Bellinger 			default:
4700e48354ceSNicholas Bellinger 				iscsit_logout_post_handler_samecid(conn);
4701e48354ceSNicholas Bellinger 				break;
4702e48354ceSNicholas Bellinger 			}
4703e48354ceSNicholas Bellinger 		} else {
4704e48354ceSNicholas Bellinger 			switch (cmd->logout_response) {
4705e48354ceSNicholas Bellinger 			case ISCSI_LOGOUT_SUCCESS:
4706e48354ceSNicholas Bellinger 				iscsit_logout_post_handler_diffcid(conn,
4707e48354ceSNicholas Bellinger 					cmd->logout_cid);
4708e48354ceSNicholas Bellinger 				break;
4709e48354ceSNicholas Bellinger 			case ISCSI_LOGOUT_CID_NOT_FOUND:
4710e48354ceSNicholas Bellinger 			case ISCSI_LOGOUT_CLEANUP_FAILED:
4711e48354ceSNicholas Bellinger 			default:
4712e48354ceSNicholas Bellinger 				break;
4713e48354ceSNicholas Bellinger 			}
4714e48354ceSNicholas Bellinger 			ret = 1;
4715e48354ceSNicholas Bellinger 		}
4716e48354ceSNicholas Bellinger 		break;
4717e48354ceSNicholas Bellinger 	case ISCSI_LOGOUT_REASON_RECOVERY:
4718e48354ceSNicholas Bellinger 		switch (cmd->logout_response) {
4719e48354ceSNicholas Bellinger 		case ISCSI_LOGOUT_SUCCESS:
4720e48354ceSNicholas Bellinger 		case ISCSI_LOGOUT_CID_NOT_FOUND:
4721e48354ceSNicholas Bellinger 		case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4722e48354ceSNicholas Bellinger 		case ISCSI_LOGOUT_CLEANUP_FAILED:
4723e48354ceSNicholas Bellinger 		default:
4724e48354ceSNicholas Bellinger 			break;
4725e48354ceSNicholas Bellinger 		}
4726e48354ceSNicholas Bellinger 		ret = 1;
4727e48354ceSNicholas Bellinger 		break;
4728e48354ceSNicholas Bellinger 	default:
4729e48354ceSNicholas Bellinger 		break;
4730e48354ceSNicholas Bellinger 
4731e48354ceSNicholas Bellinger 	}
4732e48354ceSNicholas Bellinger 	return ret;
4733e48354ceSNicholas Bellinger }
47342ec5a8c1SNicholas Bellinger EXPORT_SYMBOL(iscsit_logout_post_handler);
4735e48354ceSNicholas Bellinger 
iscsit_fail_session(struct iscsit_session * sess)47360873fe44SMax Gurtovoy void iscsit_fail_session(struct iscsit_session *sess)
4737e48354ceSNicholas Bellinger {
4738be36d683SMax Gurtovoy 	struct iscsit_conn *conn;
4739e48354ceSNicholas Bellinger 
4740e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
4741e48354ceSNicholas Bellinger 	list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4742e48354ceSNicholas Bellinger 		pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4743e48354ceSNicholas Bellinger 		conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4744e48354ceSNicholas Bellinger 	}
4745e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
4746e48354ceSNicholas Bellinger 
4747e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4748e48354ceSNicholas Bellinger 	sess->session_state = TARG_SESS_STATE_FAILED;
4749e48354ceSNicholas Bellinger }
4750e48354ceSNicholas Bellinger 
iscsit_stop_session(struct iscsit_session * sess,int session_sleep,int connection_sleep)4751e48354ceSNicholas Bellinger void iscsit_stop_session(
47520873fe44SMax Gurtovoy 	struct iscsit_session *sess,
4753e48354ceSNicholas Bellinger 	int session_sleep,
4754e48354ceSNicholas Bellinger 	int connection_sleep)
4755e48354ceSNicholas Bellinger {
4756e48354ceSNicholas Bellinger 	u16 conn_count = atomic_read(&sess->nconn);
4757be36d683SMax Gurtovoy 	struct iscsit_conn *conn, *conn_tmp = NULL;
4758e48354ceSNicholas Bellinger 	int is_last;
4759e48354ceSNicholas Bellinger 
4760e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
4761e48354ceSNicholas Bellinger 
4762e48354ceSNicholas Bellinger 	if (connection_sleep) {
4763e48354ceSNicholas Bellinger 		list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4764e48354ceSNicholas Bellinger 				conn_list) {
4765e48354ceSNicholas Bellinger 			if (conn_count == 0)
4766e48354ceSNicholas Bellinger 				break;
4767e48354ceSNicholas Bellinger 
4768e48354ceSNicholas Bellinger 			if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4769e48354ceSNicholas Bellinger 				is_last = 1;
4770e48354ceSNicholas Bellinger 			} else {
4771e48354ceSNicholas Bellinger 				iscsit_inc_conn_usage_count(conn_tmp);
4772e48354ceSNicholas Bellinger 				is_last = 0;
4773e48354ceSNicholas Bellinger 			}
4774e48354ceSNicholas Bellinger 			iscsit_inc_conn_usage_count(conn);
4775e48354ceSNicholas Bellinger 
4776e48354ceSNicholas Bellinger 			spin_unlock_bh(&sess->conn_lock);
4777e48354ceSNicholas Bellinger 			iscsit_cause_connection_reinstatement(conn, 1);
4778e48354ceSNicholas Bellinger 			spin_lock_bh(&sess->conn_lock);
4779e48354ceSNicholas Bellinger 
4780e48354ceSNicholas Bellinger 			iscsit_dec_conn_usage_count(conn);
4781e48354ceSNicholas Bellinger 			if (is_last == 0)
4782e48354ceSNicholas Bellinger 				iscsit_dec_conn_usage_count(conn_tmp);
4783e48354ceSNicholas Bellinger 			conn_count--;
4784e48354ceSNicholas Bellinger 		}
4785e48354ceSNicholas Bellinger 	} else {
4786e48354ceSNicholas Bellinger 		list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4787e48354ceSNicholas Bellinger 			iscsit_cause_connection_reinstatement(conn, 0);
4788e48354ceSNicholas Bellinger 	}
4789e48354ceSNicholas Bellinger 
4790e48354ceSNicholas Bellinger 	if (session_sleep && atomic_read(&sess->nconn)) {
4791e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
4792e48354ceSNicholas Bellinger 		wait_for_completion(&sess->session_wait_comp);
4793e48354ceSNicholas Bellinger 	} else
4794e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
4795e48354ceSNicholas Bellinger }
4796e48354ceSNicholas Bellinger 
iscsit_release_sessions_for_tpg(struct iscsi_portal_group * tpg,int force)4797e48354ceSNicholas Bellinger int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4798e48354ceSNicholas Bellinger {
47990873fe44SMax Gurtovoy 	struct iscsit_session *sess;
4800e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4801e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
4802417c20a9SNicholas Bellinger 	LIST_HEAD(free_list);
4803e48354ceSNicholas Bellinger 	int session_count = 0;
4804e48354ceSNicholas Bellinger 
4805e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
4806e48354ceSNicholas Bellinger 	if (tpg->nsessions && !force) {
4807e48354ceSNicholas Bellinger 		spin_unlock_bh(&se_tpg->session_lock);
4808e48354ceSNicholas Bellinger 		return -1;
4809e48354ceSNicholas Bellinger 	}
4810e48354ceSNicholas Bellinger 
4811e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4812e48354ceSNicholas Bellinger 			sess_list) {
48130873fe44SMax Gurtovoy 		sess = (struct iscsit_session *)se_sess->fabric_sess_ptr;
4814e48354ceSNicholas Bellinger 
4815e48354ceSNicholas Bellinger 		spin_lock(&sess->conn_lock);
4816e48354ceSNicholas Bellinger 		if (atomic_read(&sess->session_fall_back_to_erl0) ||
4817e48354ceSNicholas Bellinger 		    atomic_read(&sess->session_logout) ||
481857c46e9fSMaurizio Lombardi 		    atomic_read(&sess->session_close) ||
4819e48354ceSNicholas Bellinger 		    (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4820e48354ceSNicholas Bellinger 			spin_unlock(&sess->conn_lock);
4821e48354ceSNicholas Bellinger 			continue;
4822e48354ceSNicholas Bellinger 		}
482357c46e9fSMaurizio Lombardi 		iscsit_inc_session_usage_count(sess);
4824e48354ceSNicholas Bellinger 		atomic_set(&sess->session_reinstatement, 1);
4825197b806aSNicholas Bellinger 		atomic_set(&sess->session_fall_back_to_erl0, 1);
482657c46e9fSMaurizio Lombardi 		atomic_set(&sess->session_close, 1);
4827e48354ceSNicholas Bellinger 		spin_unlock(&sess->conn_lock);
4828e48354ceSNicholas Bellinger 
4829417c20a9SNicholas Bellinger 		list_move_tail(&se_sess->sess_list, &free_list);
4830e48354ceSNicholas Bellinger 	}
4831e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
4832e48354ceSNicholas Bellinger 
4833417c20a9SNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
48340873fe44SMax Gurtovoy 		sess = (struct iscsit_session *)se_sess->fabric_sess_ptr;
4835417c20a9SNicholas Bellinger 
483657c46e9fSMaurizio Lombardi 		list_del_init(&se_sess->sess_list);
4837e49a7d99SMaurizio Lombardi 		iscsit_stop_session(sess, 1, 1);
483857c46e9fSMaurizio Lombardi 		iscsit_dec_session_usage_count(sess);
4839417c20a9SNicholas Bellinger 		session_count++;
4840417c20a9SNicholas Bellinger 	}
4841417c20a9SNicholas Bellinger 
4842e48354ceSNicholas Bellinger 	pr_debug("Released %d iSCSI Session(s) from Target Portal"
4843e48354ceSNicholas Bellinger 			" Group: %hu\n", session_count, tpg->tpgt);
4844e48354ceSNicholas Bellinger 	return 0;
4845e48354ceSNicholas Bellinger }
4846e48354ceSNicholas Bellinger 
4847e48354ceSNicholas Bellinger MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4848e48354ceSNicholas Bellinger MODULE_VERSION("4.1.x");
4849e48354ceSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
4850e48354ceSNicholas Bellinger MODULE_LICENSE("GPL");
4851e48354ceSNicholas Bellinger 
4852e48354ceSNicholas Bellinger module_init(iscsi_target_init_module);
4853e48354ceSNicholas Bellinger module_exit(iscsi_target_cleanup_module);
4854