1e48354ceSNicholas Bellinger /*******************************************************************************
2e48354ceSNicholas Bellinger  * This file contains main functions related to the iSCSI Target Core Driver.
3e48354ceSNicholas Bellinger  *
4e48354ceSNicholas Bellinger  * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
5e48354ceSNicholas Bellinger  *
6e48354ceSNicholas Bellinger  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
7e48354ceSNicholas Bellinger  *
8e48354ceSNicholas Bellinger  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9e48354ceSNicholas Bellinger  *
10e48354ceSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
11e48354ceSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
12e48354ceSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
13e48354ceSNicholas Bellinger  * (at your option) any later version.
14e48354ceSNicholas Bellinger  *
15e48354ceSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
16e48354ceSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17e48354ceSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18e48354ceSNicholas Bellinger  * GNU General Public License for more details.
19e48354ceSNicholas Bellinger  ******************************************************************************/
20e48354ceSNicholas Bellinger 
21e48354ceSNicholas Bellinger #include <linux/string.h>
22e48354ceSNicholas Bellinger #include <linux/kthread.h>
23e48354ceSNicholas Bellinger #include <linux/crypto.h>
24e48354ceSNicholas Bellinger #include <linux/completion.h>
25827509e3SPaul Gortmaker #include <linux/module.h>
2640401530SAl Viro #include <linux/idr.h>
27e48354ceSNicholas Bellinger #include <asm/unaligned.h>
28e48354ceSNicholas Bellinger #include <scsi/scsi_device.h>
29e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
30d28b1169SAndy Grover #include <scsi/scsi_tcq.h>
31e48354ceSNicholas Bellinger #include <target/target_core_base.h>
32c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
33d28b1169SAndy Grover #include <target/target_core_configfs.h>
34e48354ceSNicholas Bellinger 
35e48354ceSNicholas Bellinger #include "iscsi_target_core.h"
36e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
37e48354ceSNicholas Bellinger #include "iscsi_target_seq_pdu_list.h"
38e48354ceSNicholas Bellinger #include "iscsi_target_tq.h"
39e48354ceSNicholas Bellinger #include "iscsi_target_configfs.h"
40e48354ceSNicholas Bellinger #include "iscsi_target_datain_values.h"
41e48354ceSNicholas Bellinger #include "iscsi_target_erl0.h"
42e48354ceSNicholas Bellinger #include "iscsi_target_erl1.h"
43e48354ceSNicholas Bellinger #include "iscsi_target_erl2.h"
44e48354ceSNicholas Bellinger #include "iscsi_target_login.h"
45e48354ceSNicholas Bellinger #include "iscsi_target_tmr.h"
46e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
47e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
48e48354ceSNicholas Bellinger #include "iscsi_target.h"
49e48354ceSNicholas Bellinger #include "iscsi_target_device.h"
50e48354ceSNicholas Bellinger #include "iscsi_target_stat.h"
51e48354ceSNicholas Bellinger 
52baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
53baa4d64bSNicholas Bellinger 
54e48354ceSNicholas Bellinger static LIST_HEAD(g_tiqn_list);
55e48354ceSNicholas Bellinger static LIST_HEAD(g_np_list);
56e48354ceSNicholas Bellinger static DEFINE_SPINLOCK(tiqn_lock);
57e48354ceSNicholas Bellinger static DEFINE_SPINLOCK(np_lock);
58e48354ceSNicholas Bellinger 
59e48354ceSNicholas Bellinger static struct idr tiqn_idr;
60e48354ceSNicholas Bellinger struct idr sess_idr;
61e48354ceSNicholas Bellinger struct mutex auth_id_lock;
62e48354ceSNicholas Bellinger spinlock_t sess_idr_lock;
63e48354ceSNicholas Bellinger 
64e48354ceSNicholas Bellinger struct iscsit_global *iscsit_global;
65e48354ceSNicholas Bellinger 
66e48354ceSNicholas Bellinger struct kmem_cache *lio_cmd_cache;
67e48354ceSNicholas Bellinger struct kmem_cache *lio_qr_cache;
68e48354ceSNicholas Bellinger struct kmem_cache *lio_dr_cache;
69e48354ceSNicholas Bellinger struct kmem_cache *lio_ooo_cache;
70e48354ceSNicholas Bellinger struct kmem_cache *lio_r2t_cache;
71e48354ceSNicholas Bellinger 
72e48354ceSNicholas Bellinger static int iscsit_handle_immediate_data(struct iscsi_cmd *,
73e48354ceSNicholas Bellinger 			unsigned char *buf, u32);
74e48354ceSNicholas Bellinger static int iscsit_logout_post_handler(struct iscsi_cmd *, struct iscsi_conn *);
75e48354ceSNicholas Bellinger 
76e48354ceSNicholas Bellinger struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
77e48354ceSNicholas Bellinger {
78e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn = NULL;
79e48354ceSNicholas Bellinger 
80e48354ceSNicholas Bellinger 	spin_lock(&tiqn_lock);
81e48354ceSNicholas Bellinger 	list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
82e48354ceSNicholas Bellinger 		if (!strcmp(tiqn->tiqn, buf)) {
83e48354ceSNicholas Bellinger 
84e48354ceSNicholas Bellinger 			spin_lock(&tiqn->tiqn_state_lock);
85e48354ceSNicholas Bellinger 			if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
86e48354ceSNicholas Bellinger 				tiqn->tiqn_access_count++;
87e48354ceSNicholas Bellinger 				spin_unlock(&tiqn->tiqn_state_lock);
88e48354ceSNicholas Bellinger 				spin_unlock(&tiqn_lock);
89e48354ceSNicholas Bellinger 				return tiqn;
90e48354ceSNicholas Bellinger 			}
91e48354ceSNicholas Bellinger 			spin_unlock(&tiqn->tiqn_state_lock);
92e48354ceSNicholas Bellinger 		}
93e48354ceSNicholas Bellinger 	}
94e48354ceSNicholas Bellinger 	spin_unlock(&tiqn_lock);
95e48354ceSNicholas Bellinger 
96e48354ceSNicholas Bellinger 	return NULL;
97e48354ceSNicholas Bellinger }
98e48354ceSNicholas Bellinger 
99e48354ceSNicholas Bellinger static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
100e48354ceSNicholas Bellinger {
101e48354ceSNicholas Bellinger 	spin_lock(&tiqn->tiqn_state_lock);
102e48354ceSNicholas Bellinger 	if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
103e48354ceSNicholas Bellinger 		tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
104e48354ceSNicholas Bellinger 		spin_unlock(&tiqn->tiqn_state_lock);
105e48354ceSNicholas Bellinger 		return 0;
106e48354ceSNicholas Bellinger 	}
107e48354ceSNicholas Bellinger 	spin_unlock(&tiqn->tiqn_state_lock);
108e48354ceSNicholas Bellinger 
109e48354ceSNicholas Bellinger 	return -1;
110e48354ceSNicholas Bellinger }
111e48354ceSNicholas Bellinger 
112e48354ceSNicholas Bellinger void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
113e48354ceSNicholas Bellinger {
114e48354ceSNicholas Bellinger 	spin_lock(&tiqn->tiqn_state_lock);
115e48354ceSNicholas Bellinger 	tiqn->tiqn_access_count--;
116e48354ceSNicholas Bellinger 	spin_unlock(&tiqn->tiqn_state_lock);
117e48354ceSNicholas Bellinger }
118e48354ceSNicholas Bellinger 
119e48354ceSNicholas Bellinger /*
120e48354ceSNicholas Bellinger  * Note that IQN formatting is expected to be done in userspace, and
121e48354ceSNicholas Bellinger  * no explict IQN format checks are done here.
122e48354ceSNicholas Bellinger  */
123e48354ceSNicholas Bellinger struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
124e48354ceSNicholas Bellinger {
125e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn = NULL;
126e48354ceSNicholas Bellinger 	int ret;
127e48354ceSNicholas Bellinger 
1288f50c7f5SDan Carpenter 	if (strlen(buf) >= ISCSI_IQN_LEN) {
129e48354ceSNicholas Bellinger 		pr_err("Target IQN exceeds %d bytes\n",
130e48354ceSNicholas Bellinger 				ISCSI_IQN_LEN);
131e48354ceSNicholas Bellinger 		return ERR_PTR(-EINVAL);
132e48354ceSNicholas Bellinger 	}
133e48354ceSNicholas Bellinger 
134e48354ceSNicholas Bellinger 	tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
135e48354ceSNicholas Bellinger 	if (!tiqn) {
136e48354ceSNicholas Bellinger 		pr_err("Unable to allocate struct iscsi_tiqn\n");
137e48354ceSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
138e48354ceSNicholas Bellinger 	}
139e48354ceSNicholas Bellinger 
140e48354ceSNicholas Bellinger 	sprintf(tiqn->tiqn, "%s", buf);
141e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&tiqn->tiqn_list);
142e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
143e48354ceSNicholas Bellinger 	spin_lock_init(&tiqn->tiqn_state_lock);
144e48354ceSNicholas Bellinger 	spin_lock_init(&tiqn->tiqn_tpg_lock);
145e48354ceSNicholas Bellinger 	spin_lock_init(&tiqn->sess_err_stats.lock);
146e48354ceSNicholas Bellinger 	spin_lock_init(&tiqn->login_stats.lock);
147e48354ceSNicholas Bellinger 	spin_lock_init(&tiqn->logout_stats.lock);
148e48354ceSNicholas Bellinger 
149e48354ceSNicholas Bellinger 	tiqn->tiqn_state = TIQN_STATE_ACTIVE;
150e48354ceSNicholas Bellinger 
151c9365bd0STejun Heo 	idr_preload(GFP_KERNEL);
152e48354ceSNicholas Bellinger 	spin_lock(&tiqn_lock);
153c9365bd0STejun Heo 
154c9365bd0STejun Heo 	ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
155e48354ceSNicholas Bellinger 	if (ret < 0) {
156c9365bd0STejun Heo 		pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
157e48354ceSNicholas Bellinger 		spin_unlock(&tiqn_lock);
158c9365bd0STejun Heo 		idr_preload_end();
159e48354ceSNicholas Bellinger 		kfree(tiqn);
160e48354ceSNicholas Bellinger 		return ERR_PTR(ret);
161e48354ceSNicholas Bellinger 	}
162c9365bd0STejun Heo 	tiqn->tiqn_index = ret;
163e48354ceSNicholas Bellinger 	list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
164c9365bd0STejun Heo 
165e48354ceSNicholas Bellinger 	spin_unlock(&tiqn_lock);
166c9365bd0STejun Heo 	idr_preload_end();
167e48354ceSNicholas Bellinger 
168e48354ceSNicholas Bellinger 	pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
169e48354ceSNicholas Bellinger 
170e48354ceSNicholas Bellinger 	return tiqn;
171e48354ceSNicholas Bellinger 
172e48354ceSNicholas Bellinger }
173e48354ceSNicholas Bellinger 
174e48354ceSNicholas Bellinger static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
175e48354ceSNicholas Bellinger {
176e48354ceSNicholas Bellinger 	/*
177e48354ceSNicholas Bellinger 	 * Wait for accesses to said struct iscsi_tiqn to end.
178e48354ceSNicholas Bellinger 	 */
179e48354ceSNicholas Bellinger 	spin_lock(&tiqn->tiqn_state_lock);
180e48354ceSNicholas Bellinger 	while (tiqn->tiqn_access_count != 0) {
181e48354ceSNicholas Bellinger 		spin_unlock(&tiqn->tiqn_state_lock);
182e48354ceSNicholas Bellinger 		msleep(10);
183e48354ceSNicholas Bellinger 		spin_lock(&tiqn->tiqn_state_lock);
184e48354ceSNicholas Bellinger 	}
185e48354ceSNicholas Bellinger 	spin_unlock(&tiqn->tiqn_state_lock);
186e48354ceSNicholas Bellinger }
187e48354ceSNicholas Bellinger 
188e48354ceSNicholas Bellinger void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
189e48354ceSNicholas Bellinger {
190e48354ceSNicholas Bellinger 	/*
191e48354ceSNicholas Bellinger 	 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
192e48354ceSNicholas Bellinger 	 * while holding tiqn->tiqn_state_lock.  This means that all subsequent
193e48354ceSNicholas Bellinger 	 * attempts to access this struct iscsi_tiqn will fail from both transport
194e48354ceSNicholas Bellinger 	 * fabric and control code paths.
195e48354ceSNicholas Bellinger 	 */
196e48354ceSNicholas Bellinger 	if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
197e48354ceSNicholas Bellinger 		pr_err("iscsit_set_tiqn_shutdown() failed\n");
198e48354ceSNicholas Bellinger 		return;
199e48354ceSNicholas Bellinger 	}
200e48354ceSNicholas Bellinger 
201e48354ceSNicholas Bellinger 	iscsit_wait_for_tiqn(tiqn);
202e48354ceSNicholas Bellinger 
203e48354ceSNicholas Bellinger 	spin_lock(&tiqn_lock);
204e48354ceSNicholas Bellinger 	list_del(&tiqn->tiqn_list);
205e48354ceSNicholas Bellinger 	idr_remove(&tiqn_idr, tiqn->tiqn_index);
206e48354ceSNicholas Bellinger 	spin_unlock(&tiqn_lock);
207e48354ceSNicholas Bellinger 
208e48354ceSNicholas Bellinger 	pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
209e48354ceSNicholas Bellinger 			tiqn->tiqn);
210e48354ceSNicholas Bellinger 	kfree(tiqn);
211e48354ceSNicholas Bellinger }
212e48354ceSNicholas Bellinger 
213e48354ceSNicholas Bellinger int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
214e48354ceSNicholas Bellinger {
215e48354ceSNicholas Bellinger 	int ret;
216e48354ceSNicholas Bellinger 	/*
217e48354ceSNicholas Bellinger 	 * Determine if the network portal is accepting storage traffic.
218e48354ceSNicholas Bellinger 	 */
219e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
220e48354ceSNicholas Bellinger 	if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
221e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
222e48354ceSNicholas Bellinger 		return -1;
223e48354ceSNicholas Bellinger 	}
224e48354ceSNicholas Bellinger 	if (np->np_login_tpg) {
225e48354ceSNicholas Bellinger 		pr_err("np->np_login_tpg() is not NULL!\n");
226e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
227e48354ceSNicholas Bellinger 		return -1;
228e48354ceSNicholas Bellinger 	}
229e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
230e48354ceSNicholas Bellinger 	/*
231e48354ceSNicholas Bellinger 	 * Determine if the portal group is accepting storage traffic.
232e48354ceSNicholas Bellinger 	 */
233e48354ceSNicholas Bellinger 	spin_lock_bh(&tpg->tpg_state_lock);
234e48354ceSNicholas Bellinger 	if (tpg->tpg_state != TPG_STATE_ACTIVE) {
235e48354ceSNicholas Bellinger 		spin_unlock_bh(&tpg->tpg_state_lock);
236e48354ceSNicholas Bellinger 		return -1;
237e48354ceSNicholas Bellinger 	}
238e48354ceSNicholas Bellinger 	spin_unlock_bh(&tpg->tpg_state_lock);
239e48354ceSNicholas Bellinger 
240e48354ceSNicholas Bellinger 	/*
241e48354ceSNicholas Bellinger 	 * Here we serialize access across the TIQN+TPG Tuple.
242e48354ceSNicholas Bellinger 	 */
243e48354ceSNicholas Bellinger 	ret = mutex_lock_interruptible(&tpg->np_login_lock);
244e48354ceSNicholas Bellinger 	if ((ret != 0) || signal_pending(current))
245e48354ceSNicholas Bellinger 		return -1;
246e48354ceSNicholas Bellinger 
247e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
248e48354ceSNicholas Bellinger 	np->np_login_tpg = tpg;
249e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
250e48354ceSNicholas Bellinger 
251e48354ceSNicholas Bellinger 	return 0;
252e48354ceSNicholas Bellinger }
253e48354ceSNicholas Bellinger 
254e48354ceSNicholas Bellinger int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
255e48354ceSNicholas Bellinger {
256e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
257e48354ceSNicholas Bellinger 
258e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
259e48354ceSNicholas Bellinger 	np->np_login_tpg = NULL;
260e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
261e48354ceSNicholas Bellinger 
262e48354ceSNicholas Bellinger 	mutex_unlock(&tpg->np_login_lock);
263e48354ceSNicholas Bellinger 
264e48354ceSNicholas Bellinger 	if (tiqn)
265e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
266e48354ceSNicholas Bellinger 
267e48354ceSNicholas Bellinger 	return 0;
268e48354ceSNicholas Bellinger }
269e48354ceSNicholas Bellinger 
27005b96892SNicholas Bellinger bool iscsit_check_np_match(
271e48354ceSNicholas Bellinger 	struct __kernel_sockaddr_storage *sockaddr,
27205b96892SNicholas Bellinger 	struct iscsi_np *np,
273e48354ceSNicholas Bellinger 	int network_transport)
274e48354ceSNicholas Bellinger {
275e48354ceSNicholas Bellinger 	struct sockaddr_in *sock_in, *sock_in_e;
276e48354ceSNicholas Bellinger 	struct sockaddr_in6 *sock_in6, *sock_in6_e;
27705b96892SNicholas Bellinger 	bool ip_match = false;
278e48354ceSNicholas Bellinger 	u16 port;
279e48354ceSNicholas Bellinger 
28005b96892SNicholas Bellinger 	if (sockaddr->ss_family == AF_INET6) {
28105b96892SNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)sockaddr;
28205b96892SNicholas Bellinger 		sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
28305b96892SNicholas Bellinger 
28405b96892SNicholas Bellinger 		if (!memcmp(&sock_in6->sin6_addr.in6_u,
28505b96892SNicholas Bellinger 			    &sock_in6_e->sin6_addr.in6_u,
28605b96892SNicholas Bellinger 			    sizeof(struct in6_addr)))
28705b96892SNicholas Bellinger 			ip_match = true;
28805b96892SNicholas Bellinger 
28905b96892SNicholas Bellinger 		port = ntohs(sock_in6->sin6_port);
29005b96892SNicholas Bellinger 	} else {
29105b96892SNicholas Bellinger 		sock_in = (struct sockaddr_in *)sockaddr;
29205b96892SNicholas Bellinger 		sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
29305b96892SNicholas Bellinger 
29405b96892SNicholas Bellinger 		if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
29505b96892SNicholas Bellinger 			ip_match = true;
29605b96892SNicholas Bellinger 
29705b96892SNicholas Bellinger 		port = ntohs(sock_in->sin_port);
29805b96892SNicholas Bellinger 	}
29905b96892SNicholas Bellinger 
30005b96892SNicholas Bellinger 	if ((ip_match == true) && (np->np_port == port) &&
30105b96892SNicholas Bellinger 	    (np->np_network_transport == network_transport))
30205b96892SNicholas Bellinger 		return true;
30305b96892SNicholas Bellinger 
30405b96892SNicholas Bellinger 	return false;
30505b96892SNicholas Bellinger }
30605b96892SNicholas Bellinger 
30705b96892SNicholas Bellinger static struct iscsi_np *iscsit_get_np(
30805b96892SNicholas Bellinger 	struct __kernel_sockaddr_storage *sockaddr,
30905b96892SNicholas Bellinger 	int network_transport)
31005b96892SNicholas Bellinger {
31105b96892SNicholas Bellinger 	struct iscsi_np *np;
31205b96892SNicholas Bellinger 	bool match;
31305b96892SNicholas Bellinger 
314e48354ceSNicholas Bellinger 	spin_lock_bh(&np_lock);
315e48354ceSNicholas Bellinger 	list_for_each_entry(np, &g_np_list, np_list) {
316e48354ceSNicholas Bellinger 		spin_lock(&np->np_thread_lock);
317e48354ceSNicholas Bellinger 		if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
318e48354ceSNicholas Bellinger 			spin_unlock(&np->np_thread_lock);
319e48354ceSNicholas Bellinger 			continue;
320e48354ceSNicholas Bellinger 		}
321e48354ceSNicholas Bellinger 
32205b96892SNicholas Bellinger 		match = iscsit_check_np_match(sockaddr, np, network_transport);
32305b96892SNicholas Bellinger 		if (match == true) {
324e48354ceSNicholas Bellinger 			/*
325e48354ceSNicholas Bellinger 			 * Increment the np_exports reference count now to
326e48354ceSNicholas Bellinger 			 * prevent iscsit_del_np() below from being called
327e48354ceSNicholas Bellinger 			 * while iscsi_tpg_add_network_portal() is called.
328e48354ceSNicholas Bellinger 			 */
329e48354ceSNicholas Bellinger 			np->np_exports++;
330e48354ceSNicholas Bellinger 			spin_unlock(&np->np_thread_lock);
331e48354ceSNicholas Bellinger 			spin_unlock_bh(&np_lock);
332e48354ceSNicholas Bellinger 			return np;
333e48354ceSNicholas Bellinger 		}
334e48354ceSNicholas Bellinger 		spin_unlock(&np->np_thread_lock);
335e48354ceSNicholas Bellinger 	}
336e48354ceSNicholas Bellinger 	spin_unlock_bh(&np_lock);
337e48354ceSNicholas Bellinger 
338e48354ceSNicholas Bellinger 	return NULL;
339e48354ceSNicholas Bellinger }
340e48354ceSNicholas Bellinger 
341e48354ceSNicholas Bellinger struct iscsi_np *iscsit_add_np(
342e48354ceSNicholas Bellinger 	struct __kernel_sockaddr_storage *sockaddr,
343e48354ceSNicholas Bellinger 	char *ip_str,
344e48354ceSNicholas Bellinger 	int network_transport)
345e48354ceSNicholas Bellinger {
346e48354ceSNicholas Bellinger 	struct sockaddr_in *sock_in;
347e48354ceSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
348e48354ceSNicholas Bellinger 	struct iscsi_np *np;
349e48354ceSNicholas Bellinger 	int ret;
350e48354ceSNicholas Bellinger 	/*
351e48354ceSNicholas Bellinger 	 * Locate the existing struct iscsi_np if already active..
352e48354ceSNicholas Bellinger 	 */
353e48354ceSNicholas Bellinger 	np = iscsit_get_np(sockaddr, network_transport);
354e48354ceSNicholas Bellinger 	if (np)
355e48354ceSNicholas Bellinger 		return np;
356e48354ceSNicholas Bellinger 
357e48354ceSNicholas Bellinger 	np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
358e48354ceSNicholas Bellinger 	if (!np) {
359e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for struct iscsi_np\n");
360e48354ceSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
361e48354ceSNicholas Bellinger 	}
362e48354ceSNicholas Bellinger 
363e48354ceSNicholas Bellinger 	np->np_flags |= NPF_IP_NETWORK;
364e48354ceSNicholas Bellinger 	if (sockaddr->ss_family == AF_INET6) {
365e48354ceSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)sockaddr;
366e48354ceSNicholas Bellinger 		snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str);
367e48354ceSNicholas Bellinger 		np->np_port = ntohs(sock_in6->sin6_port);
368e48354ceSNicholas Bellinger 	} else {
369e48354ceSNicholas Bellinger 		sock_in = (struct sockaddr_in *)sockaddr;
370e48354ceSNicholas Bellinger 		sprintf(np->np_ip, "%s", ip_str);
371e48354ceSNicholas Bellinger 		np->np_port = ntohs(sock_in->sin_port);
372e48354ceSNicholas Bellinger 	}
373e48354ceSNicholas Bellinger 
374e48354ceSNicholas Bellinger 	np->np_network_transport = network_transport;
375e48354ceSNicholas Bellinger 	spin_lock_init(&np->np_thread_lock);
376e48354ceSNicholas Bellinger 	init_completion(&np->np_restart_comp);
377e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&np->np_list);
378e48354ceSNicholas Bellinger 
379e48354ceSNicholas Bellinger 	ret = iscsi_target_setup_login_socket(np, sockaddr);
380e48354ceSNicholas Bellinger 	if (ret != 0) {
381e48354ceSNicholas Bellinger 		kfree(np);
382e48354ceSNicholas Bellinger 		return ERR_PTR(ret);
383e48354ceSNicholas Bellinger 	}
384e48354ceSNicholas Bellinger 
385e48354ceSNicholas Bellinger 	np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
386e48354ceSNicholas Bellinger 	if (IS_ERR(np->np_thread)) {
387e48354ceSNicholas Bellinger 		pr_err("Unable to create kthread: iscsi_np\n");
388e48354ceSNicholas Bellinger 		ret = PTR_ERR(np->np_thread);
389e48354ceSNicholas Bellinger 		kfree(np);
390e48354ceSNicholas Bellinger 		return ERR_PTR(ret);
391e48354ceSNicholas Bellinger 	}
392e48354ceSNicholas Bellinger 	/*
393e48354ceSNicholas Bellinger 	 * Increment the np_exports reference count now to prevent
394e48354ceSNicholas Bellinger 	 * iscsit_del_np() below from being run while a new call to
395e48354ceSNicholas Bellinger 	 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
396e48354ceSNicholas Bellinger 	 * active.  We don't need to hold np->np_thread_lock at this
397e48354ceSNicholas Bellinger 	 * point because iscsi_np has not been added to g_np_list yet.
398e48354ceSNicholas Bellinger 	 */
399e48354ceSNicholas Bellinger 	np->np_exports = 1;
400e48354ceSNicholas Bellinger 
401e48354ceSNicholas Bellinger 	spin_lock_bh(&np_lock);
402e48354ceSNicholas Bellinger 	list_add_tail(&np->np_list, &g_np_list);
403e48354ceSNicholas Bellinger 	spin_unlock_bh(&np_lock);
404e48354ceSNicholas Bellinger 
405e48354ceSNicholas Bellinger 	pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
406baa4d64bSNicholas Bellinger 		np->np_ip, np->np_port, np->np_transport->name);
407e48354ceSNicholas Bellinger 
408e48354ceSNicholas Bellinger 	return np;
409e48354ceSNicholas Bellinger }
410e48354ceSNicholas Bellinger 
411e48354ceSNicholas Bellinger int iscsit_reset_np_thread(
412e48354ceSNicholas Bellinger 	struct iscsi_np *np,
413e48354ceSNicholas Bellinger 	struct iscsi_tpg_np *tpg_np,
414e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg)
415e48354ceSNicholas Bellinger {
416e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
417e48354ceSNicholas Bellinger 	if (tpg && tpg_np) {
418e48354ceSNicholas Bellinger 		/*
419e48354ceSNicholas Bellinger 		 * The reset operation need only be performed when the
420e48354ceSNicholas Bellinger 		 * passed struct iscsi_portal_group has a login in progress
421e48354ceSNicholas Bellinger 		 * to one of the network portals.
422e48354ceSNicholas Bellinger 		 */
423e48354ceSNicholas Bellinger 		if (tpg_np->tpg_np->np_login_tpg != tpg) {
424e48354ceSNicholas Bellinger 			spin_unlock_bh(&np->np_thread_lock);
425e48354ceSNicholas Bellinger 			return 0;
426e48354ceSNicholas Bellinger 		}
427e48354ceSNicholas Bellinger 	}
428e48354ceSNicholas Bellinger 	if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
429e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
430e48354ceSNicholas Bellinger 		return 0;
431e48354ceSNicholas Bellinger 	}
432e48354ceSNicholas Bellinger 	np->np_thread_state = ISCSI_NP_THREAD_RESET;
433e48354ceSNicholas Bellinger 
434e48354ceSNicholas Bellinger 	if (np->np_thread) {
435e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
436e48354ceSNicholas Bellinger 		send_sig(SIGINT, np->np_thread, 1);
437e48354ceSNicholas Bellinger 		wait_for_completion(&np->np_restart_comp);
438e48354ceSNicholas Bellinger 		spin_lock_bh(&np->np_thread_lock);
439e48354ceSNicholas Bellinger 	}
440e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
441e48354ceSNicholas Bellinger 
442e48354ceSNicholas Bellinger 	return 0;
443e48354ceSNicholas Bellinger }
444e48354ceSNicholas Bellinger 
445baa4d64bSNicholas Bellinger static void iscsit_free_np(struct iscsi_np *np)
446e48354ceSNicholas Bellinger {
447bf6932f4SAl Viro 	if (np->np_socket)
448e48354ceSNicholas Bellinger 		sock_release(np->np_socket);
449e48354ceSNicholas Bellinger }
450e48354ceSNicholas Bellinger 
451e48354ceSNicholas Bellinger int iscsit_del_np(struct iscsi_np *np)
452e48354ceSNicholas Bellinger {
453e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
454e48354ceSNicholas Bellinger 	np->np_exports--;
455e48354ceSNicholas Bellinger 	if (np->np_exports) {
456e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
457e48354ceSNicholas Bellinger 		return 0;
458e48354ceSNicholas Bellinger 	}
459e48354ceSNicholas Bellinger 	np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
460e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
461e48354ceSNicholas Bellinger 
462e48354ceSNicholas Bellinger 	if (np->np_thread) {
463e48354ceSNicholas Bellinger 		/*
464e48354ceSNicholas Bellinger 		 * We need to send the signal to wakeup Linux/Net
465e48354ceSNicholas Bellinger 		 * which may be sleeping in sock_accept()..
466e48354ceSNicholas Bellinger 		 */
467e48354ceSNicholas Bellinger 		send_sig(SIGINT, np->np_thread, 1);
468e48354ceSNicholas Bellinger 		kthread_stop(np->np_thread);
469e48354ceSNicholas Bellinger 	}
470baa4d64bSNicholas Bellinger 
471baa4d64bSNicholas Bellinger 	np->np_transport->iscsit_free_np(np);
472e48354ceSNicholas Bellinger 
473e48354ceSNicholas Bellinger 	spin_lock_bh(&np_lock);
474e48354ceSNicholas Bellinger 	list_del(&np->np_list);
475e48354ceSNicholas Bellinger 	spin_unlock_bh(&np_lock);
476e48354ceSNicholas Bellinger 
477e48354ceSNicholas Bellinger 	pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
478baa4d64bSNicholas Bellinger 		np->np_ip, np->np_port, np->np_transport->name);
479e48354ceSNicholas Bellinger 
480baa4d64bSNicholas Bellinger 	iscsit_put_transport(np->np_transport);
481e48354ceSNicholas Bellinger 	kfree(np);
482e48354ceSNicholas Bellinger 	return 0;
483e48354ceSNicholas Bellinger }
484e48354ceSNicholas Bellinger 
485baa4d64bSNicholas Bellinger static struct iscsit_transport iscsi_target_transport = {
486baa4d64bSNicholas Bellinger 	.name			= "iSCSI/TCP",
487baa4d64bSNicholas Bellinger 	.transport_type		= ISCSI_TCP,
488baa4d64bSNicholas Bellinger 	.owner			= NULL,
489baa4d64bSNicholas Bellinger 	.iscsit_setup_np	= iscsit_setup_np,
490baa4d64bSNicholas Bellinger 	.iscsit_accept_np	= iscsit_accept_np,
491baa4d64bSNicholas Bellinger 	.iscsit_free_np		= iscsit_free_np,
492baa4d64bSNicholas Bellinger 	.iscsit_get_login_rx	= iscsit_get_login_rx,
493baa4d64bSNicholas Bellinger 	.iscsit_put_login_tx	= iscsit_put_login_tx,
494baa4d64bSNicholas Bellinger };
495baa4d64bSNicholas Bellinger 
496e48354ceSNicholas Bellinger static int __init iscsi_target_init_module(void)
497e48354ceSNicholas Bellinger {
498e48354ceSNicholas Bellinger 	int ret = 0;
499e48354ceSNicholas Bellinger 
500e48354ceSNicholas Bellinger 	pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
501e48354ceSNicholas Bellinger 
502e48354ceSNicholas Bellinger 	iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
503e48354ceSNicholas Bellinger 	if (!iscsit_global) {
504e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for iscsit_global\n");
505e48354ceSNicholas Bellinger 		return -1;
506e48354ceSNicholas Bellinger 	}
507e48354ceSNicholas Bellinger 	mutex_init(&auth_id_lock);
508e48354ceSNicholas Bellinger 	spin_lock_init(&sess_idr_lock);
509e48354ceSNicholas Bellinger 	idr_init(&tiqn_idr);
510e48354ceSNicholas Bellinger 	idr_init(&sess_idr);
511e48354ceSNicholas Bellinger 
512e48354ceSNicholas Bellinger 	ret = iscsi_target_register_configfs();
513e48354ceSNicholas Bellinger 	if (ret < 0)
514e48354ceSNicholas Bellinger 		goto out;
515e48354ceSNicholas Bellinger 
516e48354ceSNicholas Bellinger 	ret = iscsi_thread_set_init();
517e48354ceSNicholas Bellinger 	if (ret < 0)
518e48354ceSNicholas Bellinger 		goto configfs_out;
519e48354ceSNicholas Bellinger 
520e48354ceSNicholas Bellinger 	if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
521e48354ceSNicholas Bellinger 			TARGET_THREAD_SET_COUNT) {
522e48354ceSNicholas Bellinger 		pr_err("iscsi_allocate_thread_sets() returned"
523e48354ceSNicholas Bellinger 			" unexpected value!\n");
524e48354ceSNicholas Bellinger 		goto ts_out1;
525e48354ceSNicholas Bellinger 	}
526e48354ceSNicholas Bellinger 
527e48354ceSNicholas Bellinger 	lio_cmd_cache = kmem_cache_create("lio_cmd_cache",
528e48354ceSNicholas Bellinger 			sizeof(struct iscsi_cmd), __alignof__(struct iscsi_cmd),
529e48354ceSNicholas Bellinger 			0, NULL);
530e48354ceSNicholas Bellinger 	if (!lio_cmd_cache) {
531e48354ceSNicholas Bellinger 		pr_err("Unable to kmem_cache_create() for"
532e48354ceSNicholas Bellinger 				" lio_cmd_cache\n");
533e48354ceSNicholas Bellinger 		goto ts_out2;
534e48354ceSNicholas Bellinger 	}
535e48354ceSNicholas Bellinger 
536e48354ceSNicholas Bellinger 	lio_qr_cache = kmem_cache_create("lio_qr_cache",
537e48354ceSNicholas Bellinger 			sizeof(struct iscsi_queue_req),
538e48354ceSNicholas Bellinger 			__alignof__(struct iscsi_queue_req), 0, NULL);
539e48354ceSNicholas Bellinger 	if (!lio_qr_cache) {
540e48354ceSNicholas Bellinger 		pr_err("nable to kmem_cache_create() for"
541e48354ceSNicholas Bellinger 				" lio_qr_cache\n");
542e48354ceSNicholas Bellinger 		goto cmd_out;
543e48354ceSNicholas Bellinger 	}
544e48354ceSNicholas Bellinger 
545e48354ceSNicholas Bellinger 	lio_dr_cache = kmem_cache_create("lio_dr_cache",
546e48354ceSNicholas Bellinger 			sizeof(struct iscsi_datain_req),
547e48354ceSNicholas Bellinger 			__alignof__(struct iscsi_datain_req), 0, NULL);
548e48354ceSNicholas Bellinger 	if (!lio_dr_cache) {
549e48354ceSNicholas Bellinger 		pr_err("Unable to kmem_cache_create() for"
550e48354ceSNicholas Bellinger 				" lio_dr_cache\n");
551e48354ceSNicholas Bellinger 		goto qr_out;
552e48354ceSNicholas Bellinger 	}
553e48354ceSNicholas Bellinger 
554e48354ceSNicholas Bellinger 	lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
555e48354ceSNicholas Bellinger 			sizeof(struct iscsi_ooo_cmdsn),
556e48354ceSNicholas Bellinger 			__alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
557e48354ceSNicholas Bellinger 	if (!lio_ooo_cache) {
558e48354ceSNicholas Bellinger 		pr_err("Unable to kmem_cache_create() for"
559e48354ceSNicholas Bellinger 				" lio_ooo_cache\n");
560e48354ceSNicholas Bellinger 		goto dr_out;
561e48354ceSNicholas Bellinger 	}
562e48354ceSNicholas Bellinger 
563e48354ceSNicholas Bellinger 	lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
564e48354ceSNicholas Bellinger 			sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
565e48354ceSNicholas Bellinger 			0, NULL);
566e48354ceSNicholas Bellinger 	if (!lio_r2t_cache) {
567e48354ceSNicholas Bellinger 		pr_err("Unable to kmem_cache_create() for"
568e48354ceSNicholas Bellinger 				" lio_r2t_cache\n");
569e48354ceSNicholas Bellinger 		goto ooo_out;
570e48354ceSNicholas Bellinger 	}
571e48354ceSNicholas Bellinger 
572baa4d64bSNicholas Bellinger 	iscsit_register_transport(&iscsi_target_transport);
573baa4d64bSNicholas Bellinger 
574e48354ceSNicholas Bellinger 	if (iscsit_load_discovery_tpg() < 0)
575e48354ceSNicholas Bellinger 		goto r2t_out;
576e48354ceSNicholas Bellinger 
577e48354ceSNicholas Bellinger 	return ret;
578e48354ceSNicholas Bellinger r2t_out:
579e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_r2t_cache);
580e48354ceSNicholas Bellinger ooo_out:
581e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_ooo_cache);
582e48354ceSNicholas Bellinger dr_out:
583e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_dr_cache);
584e48354ceSNicholas Bellinger qr_out:
585e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_qr_cache);
586e48354ceSNicholas Bellinger cmd_out:
587e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_cmd_cache);
588e48354ceSNicholas Bellinger ts_out2:
589e48354ceSNicholas Bellinger 	iscsi_deallocate_thread_sets();
590e48354ceSNicholas Bellinger ts_out1:
591e48354ceSNicholas Bellinger 	iscsi_thread_set_free();
592e48354ceSNicholas Bellinger configfs_out:
593e48354ceSNicholas Bellinger 	iscsi_target_deregister_configfs();
594e48354ceSNicholas Bellinger out:
595e48354ceSNicholas Bellinger 	kfree(iscsit_global);
596e48354ceSNicholas Bellinger 	return -ENOMEM;
597e48354ceSNicholas Bellinger }
598e48354ceSNicholas Bellinger 
599e48354ceSNicholas Bellinger static void __exit iscsi_target_cleanup_module(void)
600e48354ceSNicholas Bellinger {
601e48354ceSNicholas Bellinger 	iscsi_deallocate_thread_sets();
602e48354ceSNicholas Bellinger 	iscsi_thread_set_free();
603e48354ceSNicholas Bellinger 	iscsit_release_discovery_tpg();
604baa4d64bSNicholas Bellinger 	iscsit_unregister_transport(&iscsi_target_transport);
605e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_cmd_cache);
606e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_qr_cache);
607e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_dr_cache);
608e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_ooo_cache);
609e48354ceSNicholas Bellinger 	kmem_cache_destroy(lio_r2t_cache);
610e48354ceSNicholas Bellinger 
611e48354ceSNicholas Bellinger 	iscsi_target_deregister_configfs();
612e48354ceSNicholas Bellinger 
613e48354ceSNicholas Bellinger 	kfree(iscsit_global);
614e48354ceSNicholas Bellinger }
615e48354ceSNicholas Bellinger 
6168b1e1244SAndy Grover static int iscsit_add_reject(
617e48354ceSNicholas Bellinger 	u8 reason,
618e48354ceSNicholas Bellinger 	int fail_conn,
619e48354ceSNicholas Bellinger 	unsigned char *buf,
620e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
621e48354ceSNicholas Bellinger {
622e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd;
623e48354ceSNicholas Bellinger 	struct iscsi_reject *hdr;
624e48354ceSNicholas Bellinger 	int ret;
625e48354ceSNicholas Bellinger 
626e48354ceSNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
627e48354ceSNicholas Bellinger 	if (!cmd)
628e48354ceSNicholas Bellinger 		return -1;
629e48354ceSNicholas Bellinger 
630e48354ceSNicholas Bellinger 	cmd->iscsi_opcode = ISCSI_OP_REJECT;
631e48354ceSNicholas Bellinger 	if (fail_conn)
632e48354ceSNicholas Bellinger 		cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
633e48354ceSNicholas Bellinger 
634e48354ceSNicholas Bellinger 	hdr	= (struct iscsi_reject *) cmd->pdu;
635e48354ceSNicholas Bellinger 	hdr->reason = reason;
636e48354ceSNicholas Bellinger 
6371c3d5794SThomas Meyer 	cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
638e48354ceSNicholas Bellinger 	if (!cmd->buf_ptr) {
639e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for cmd->buf_ptr\n");
640e48354ceSNicholas Bellinger 		iscsit_release_cmd(cmd);
641e48354ceSNicholas Bellinger 		return -1;
642e48354ceSNicholas Bellinger 	}
643e48354ceSNicholas Bellinger 
644e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
6452fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
646e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
647e48354ceSNicholas Bellinger 
648e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_REJECT;
649e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
650e48354ceSNicholas Bellinger 
651e48354ceSNicholas Bellinger 	ret = wait_for_completion_interruptible(&cmd->reject_comp);
652e48354ceSNicholas Bellinger 	if (ret != 0)
653e48354ceSNicholas Bellinger 		return -1;
654e48354ceSNicholas Bellinger 
655e48354ceSNicholas Bellinger 	return (!fail_conn) ? 0 : -1;
656e48354ceSNicholas Bellinger }
657e48354ceSNicholas Bellinger 
658e48354ceSNicholas Bellinger int iscsit_add_reject_from_cmd(
659e48354ceSNicholas Bellinger 	u8 reason,
660e48354ceSNicholas Bellinger 	int fail_conn,
661e48354ceSNicholas Bellinger 	int add_to_conn,
662e48354ceSNicholas Bellinger 	unsigned char *buf,
663e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd)
664e48354ceSNicholas Bellinger {
665e48354ceSNicholas Bellinger 	struct iscsi_conn *conn;
666e48354ceSNicholas Bellinger 	struct iscsi_reject *hdr;
667e48354ceSNicholas Bellinger 	int ret;
668e48354ceSNicholas Bellinger 
669e48354ceSNicholas Bellinger 	if (!cmd->conn) {
670e48354ceSNicholas Bellinger 		pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
671e48354ceSNicholas Bellinger 				cmd->init_task_tag);
672e48354ceSNicholas Bellinger 		return -1;
673e48354ceSNicholas Bellinger 	}
674e48354ceSNicholas Bellinger 	conn = cmd->conn;
675e48354ceSNicholas Bellinger 
676e48354ceSNicholas Bellinger 	cmd->iscsi_opcode = ISCSI_OP_REJECT;
677e48354ceSNicholas Bellinger 	if (fail_conn)
678e48354ceSNicholas Bellinger 		cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
679e48354ceSNicholas Bellinger 
680e48354ceSNicholas Bellinger 	hdr	= (struct iscsi_reject *) cmd->pdu;
681e48354ceSNicholas Bellinger 	hdr->reason = reason;
682e48354ceSNicholas Bellinger 
6831c3d5794SThomas Meyer 	cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
684e48354ceSNicholas Bellinger 	if (!cmd->buf_ptr) {
685e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for cmd->buf_ptr\n");
686e48354ceSNicholas Bellinger 		iscsit_release_cmd(cmd);
687e48354ceSNicholas Bellinger 		return -1;
688e48354ceSNicholas Bellinger 	}
689e48354ceSNicholas Bellinger 
690e48354ceSNicholas Bellinger 	if (add_to_conn) {
691e48354ceSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
6922fbb471eSAndy Grover 		list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
693e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
694e48354ceSNicholas Bellinger 	}
695e48354ceSNicholas Bellinger 
696e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_REJECT;
697e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
698e48354ceSNicholas Bellinger 
699e48354ceSNicholas Bellinger 	ret = wait_for_completion_interruptible(&cmd->reject_comp);
700e48354ceSNicholas Bellinger 	if (ret != 0)
701e48354ceSNicholas Bellinger 		return -1;
702e48354ceSNicholas Bellinger 
703e48354ceSNicholas Bellinger 	return (!fail_conn) ? 0 : -1;
704e48354ceSNicholas Bellinger }
705e48354ceSNicholas Bellinger 
706e48354ceSNicholas Bellinger /*
707e48354ceSNicholas Bellinger  * Map some portion of the allocated scatterlist to an iovec, suitable for
708bfb79eacSAndy Grover  * kernel sockets to copy data in/out.
709e48354ceSNicholas Bellinger  */
710e48354ceSNicholas Bellinger static int iscsit_map_iovec(
711e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
712e48354ceSNicholas Bellinger 	struct kvec *iov,
713e48354ceSNicholas Bellinger 	u32 data_offset,
714e48354ceSNicholas Bellinger 	u32 data_length)
715e48354ceSNicholas Bellinger {
716e48354ceSNicholas Bellinger 	u32 i = 0;
717e48354ceSNicholas Bellinger 	struct scatterlist *sg;
718e48354ceSNicholas Bellinger 	unsigned int page_off;
719e48354ceSNicholas Bellinger 
720e48354ceSNicholas Bellinger 	/*
721bfb79eacSAndy Grover 	 * We know each entry in t_data_sg contains a page.
722e48354ceSNicholas Bellinger 	 */
723bfb79eacSAndy Grover 	sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
724e48354ceSNicholas Bellinger 	page_off = (data_offset % PAGE_SIZE);
725e48354ceSNicholas Bellinger 
726e48354ceSNicholas Bellinger 	cmd->first_data_sg = sg;
727e48354ceSNicholas Bellinger 	cmd->first_data_sg_off = page_off;
728e48354ceSNicholas Bellinger 
729e48354ceSNicholas Bellinger 	while (data_length) {
730e48354ceSNicholas Bellinger 		u32 cur_len = min_t(u32, data_length, sg->length - page_off);
731e48354ceSNicholas Bellinger 
732e48354ceSNicholas Bellinger 		iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
733e48354ceSNicholas Bellinger 		iov[i].iov_len = cur_len;
734e48354ceSNicholas Bellinger 
735e48354ceSNicholas Bellinger 		data_length -= cur_len;
736e48354ceSNicholas Bellinger 		page_off = 0;
737e48354ceSNicholas Bellinger 		sg = sg_next(sg);
738e48354ceSNicholas Bellinger 		i++;
739e48354ceSNicholas Bellinger 	}
740e48354ceSNicholas Bellinger 
741e48354ceSNicholas Bellinger 	cmd->kmapped_nents = i;
742e48354ceSNicholas Bellinger 
743e48354ceSNicholas Bellinger 	return i;
744e48354ceSNicholas Bellinger }
745e48354ceSNicholas Bellinger 
746e48354ceSNicholas Bellinger static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
747e48354ceSNicholas Bellinger {
748e48354ceSNicholas Bellinger 	u32 i;
749e48354ceSNicholas Bellinger 	struct scatterlist *sg;
750e48354ceSNicholas Bellinger 
751e48354ceSNicholas Bellinger 	sg = cmd->first_data_sg;
752e48354ceSNicholas Bellinger 
753e48354ceSNicholas Bellinger 	for (i = 0; i < cmd->kmapped_nents; i++)
754e48354ceSNicholas Bellinger 		kunmap(sg_page(&sg[i]));
755e48354ceSNicholas Bellinger }
756e48354ceSNicholas Bellinger 
757e48354ceSNicholas Bellinger static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
758e48354ceSNicholas Bellinger {
759e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd;
760e48354ceSNicholas Bellinger 
761e48354ceSNicholas Bellinger 	conn->exp_statsn = exp_statsn;
762e48354ceSNicholas Bellinger 
763e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
7642fbb471eSAndy Grover 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
765e48354ceSNicholas Bellinger 		spin_lock(&cmd->istate_lock);
766e48354ceSNicholas Bellinger 		if ((cmd->i_state == ISTATE_SENT_STATUS) &&
76764c13330SSteve Hodgson 		    iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
768e48354ceSNicholas Bellinger 			cmd->i_state = ISTATE_REMOVE;
769e48354ceSNicholas Bellinger 			spin_unlock(&cmd->istate_lock);
770e48354ceSNicholas Bellinger 			iscsit_add_cmd_to_immediate_queue(cmd, conn,
771e48354ceSNicholas Bellinger 						cmd->i_state);
772e48354ceSNicholas Bellinger 			continue;
773e48354ceSNicholas Bellinger 		}
774e48354ceSNicholas Bellinger 		spin_unlock(&cmd->istate_lock);
775e48354ceSNicholas Bellinger 	}
776e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
777e48354ceSNicholas Bellinger }
778e48354ceSNicholas Bellinger 
779e48354ceSNicholas Bellinger static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
780e48354ceSNicholas Bellinger {
781f80e8ed3SNicholas Bellinger 	u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
782e48354ceSNicholas Bellinger 
783c0427f15SChristoph Hellwig 	iov_count += ISCSI_IOV_DATA_BUFFER;
784e48354ceSNicholas Bellinger 
785e48354ceSNicholas Bellinger 	cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
786e48354ceSNicholas Bellinger 	if (!cmd->iov_data) {
787e48354ceSNicholas Bellinger 		pr_err("Unable to allocate cmd->iov_data\n");
788e48354ceSNicholas Bellinger 		return -ENOMEM;
789e48354ceSNicholas Bellinger 	}
790e48354ceSNicholas Bellinger 
791e48354ceSNicholas Bellinger 	cmd->orig_iov_data_count = iov_count;
792e48354ceSNicholas Bellinger 	return 0;
793e48354ceSNicholas Bellinger }
794e48354ceSNicholas Bellinger 
795e48354ceSNicholas Bellinger static int iscsit_handle_scsi_cmd(
796e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
797e48354ceSNicholas Bellinger 	unsigned char *buf)
798e48354ceSNicholas Bellinger {
799de103c93SChristoph Hellwig 	int data_direction, payload_length, cmdsn_ret = 0, immed_ret;
800e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
801e48354ceSNicholas Bellinger 	struct iscsi_scsi_req *hdr;
802d28b1169SAndy Grover 	int iscsi_task_attr;
803d28b1169SAndy Grover 	int sam_task_attr;
804e48354ceSNicholas Bellinger 
805e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->sess->session_stats_lock);
806e48354ceSNicholas Bellinger 	conn->sess->cmd_pdus++;
807e48354ceSNicholas Bellinger 	if (conn->sess->se_sess->se_node_acl) {
808e48354ceSNicholas Bellinger 		spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
809e48354ceSNicholas Bellinger 		conn->sess->se_sess->se_node_acl->num_cmds++;
810e48354ceSNicholas Bellinger 		spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
811e48354ceSNicholas Bellinger 	}
812e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->sess->session_stats_lock);
813e48354ceSNicholas Bellinger 
814e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_scsi_req *) buf;
815e48354ceSNicholas Bellinger 	payload_length		= ntoh24(hdr->dlength);
816e48354ceSNicholas Bellinger 
817e48354ceSNicholas Bellinger 	/* FIXME; Add checks for AdditionalHeaderSegment */
818e48354ceSNicholas Bellinger 
819e48354ceSNicholas Bellinger 	if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
820e48354ceSNicholas Bellinger 	    !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
821e48354ceSNicholas Bellinger 		pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
822e48354ceSNicholas Bellinger 				" not set. Bad iSCSI Initiator.\n");
823e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
824e48354ceSNicholas Bellinger 				buf, conn);
825e48354ceSNicholas Bellinger 	}
826e48354ceSNicholas Bellinger 
827e48354ceSNicholas Bellinger 	if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
828e48354ceSNicholas Bellinger 	     (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
829e48354ceSNicholas Bellinger 		/*
830e48354ceSNicholas Bellinger 		 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
831e48354ceSNicholas Bellinger 		 * that adds support for RESERVE/RELEASE.  There is a bug
832e48354ceSNicholas Bellinger 		 * add with this new functionality that sets R/W bits when
833e48354ceSNicholas Bellinger 		 * neither CDB carries any READ or WRITE datapayloads.
834e48354ceSNicholas Bellinger 		 */
835e48354ceSNicholas Bellinger 		if ((hdr->cdb[0] == 0x16) || (hdr->cdb[0] == 0x17)) {
836e48354ceSNicholas Bellinger 			hdr->flags &= ~ISCSI_FLAG_CMD_READ;
837e48354ceSNicholas Bellinger 			hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
838e48354ceSNicholas Bellinger 			goto done;
839e48354ceSNicholas Bellinger 		}
840e48354ceSNicholas Bellinger 
841e48354ceSNicholas Bellinger 		pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
842e48354ceSNicholas Bellinger 			" set when Expected Data Transfer Length is 0 for"
843e48354ceSNicholas Bellinger 			" CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr->cdb[0]);
844e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
845e48354ceSNicholas Bellinger 				buf, conn);
846e48354ceSNicholas Bellinger 	}
847e48354ceSNicholas Bellinger done:
848e48354ceSNicholas Bellinger 
849e48354ceSNicholas Bellinger 	if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
850e48354ceSNicholas Bellinger 	    !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
851e48354ceSNicholas Bellinger 		pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
852e48354ceSNicholas Bellinger 			" MUST be set if Expected Data Transfer Length is not 0."
853e48354ceSNicholas Bellinger 			" Bad iSCSI Initiator\n");
854e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
855e48354ceSNicholas Bellinger 				buf, conn);
856e48354ceSNicholas Bellinger 	}
857e48354ceSNicholas Bellinger 
858e48354ceSNicholas Bellinger 	if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
859e48354ceSNicholas Bellinger 	    (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
860e48354ceSNicholas Bellinger 		pr_err("Bidirectional operations not supported!\n");
861e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
862e48354ceSNicholas Bellinger 				buf, conn);
863e48354ceSNicholas Bellinger 	}
864e48354ceSNicholas Bellinger 
865e48354ceSNicholas Bellinger 	if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
866e48354ceSNicholas Bellinger 		pr_err("Illegally set Immediate Bit in iSCSI Initiator"
867e48354ceSNicholas Bellinger 				" Scsi Command PDU.\n");
868e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
869e48354ceSNicholas Bellinger 				buf, conn);
870e48354ceSNicholas Bellinger 	}
871e48354ceSNicholas Bellinger 
872e48354ceSNicholas Bellinger 	if (payload_length && !conn->sess->sess_ops->ImmediateData) {
873e48354ceSNicholas Bellinger 		pr_err("ImmediateData=No but DataSegmentLength=%u,"
874e48354ceSNicholas Bellinger 			" protocol error.\n", payload_length);
875e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
876e48354ceSNicholas Bellinger 				buf, conn);
877e48354ceSNicholas Bellinger 	}
878e48354ceSNicholas Bellinger 
87950e5c87dSChristoph Hellwig 	if ((be32_to_cpu(hdr->data_length )== payload_length) &&
880e48354ceSNicholas Bellinger 	    (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
881e48354ceSNicholas Bellinger 		pr_err("Expected Data Transfer Length and Length of"
882e48354ceSNicholas Bellinger 			" Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
883e48354ceSNicholas Bellinger 			" bit is not set protocol error\n");
884e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
885e48354ceSNicholas Bellinger 				buf, conn);
886e48354ceSNicholas Bellinger 	}
887e48354ceSNicholas Bellinger 
88850e5c87dSChristoph Hellwig 	if (payload_length > be32_to_cpu(hdr->data_length)) {
889e48354ceSNicholas Bellinger 		pr_err("DataSegmentLength: %u is greater than"
890e48354ceSNicholas Bellinger 			" EDTL: %u, protocol error.\n", payload_length,
891e48354ceSNicholas Bellinger 				hdr->data_length);
892e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
893e48354ceSNicholas Bellinger 				buf, conn);
894e48354ceSNicholas Bellinger 	}
895e48354ceSNicholas Bellinger 
89621f5aa7eSNicholas Bellinger 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
897e48354ceSNicholas Bellinger 		pr_err("DataSegmentLength: %u is greater than"
89821f5aa7eSNicholas Bellinger 			" MaxXmitDataSegmentLength: %u, protocol error.\n",
89921f5aa7eSNicholas Bellinger 			payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
900e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
901e48354ceSNicholas Bellinger 				buf, conn);
902e48354ceSNicholas Bellinger 	}
903e48354ceSNicholas Bellinger 
904e48354ceSNicholas Bellinger 	if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
905e48354ceSNicholas Bellinger 		pr_err("DataSegmentLength: %u is greater than"
906e48354ceSNicholas Bellinger 			" FirstBurstLength: %u, protocol error.\n",
907e48354ceSNicholas Bellinger 			payload_length, conn->sess->sess_ops->FirstBurstLength);
908e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
909e48354ceSNicholas Bellinger 					buf, conn);
910e48354ceSNicholas Bellinger 	}
911e48354ceSNicholas Bellinger 
912e48354ceSNicholas Bellinger 	data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
913e48354ceSNicholas Bellinger 			 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
914e48354ceSNicholas Bellinger 			  DMA_NONE;
915e48354ceSNicholas Bellinger 
916d28b1169SAndy Grover 	cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
917e48354ceSNicholas Bellinger 	if (!cmd)
918e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
919e48354ceSNicholas Bellinger 					 buf, conn);
920e48354ceSNicholas Bellinger 
921d28b1169SAndy Grover 	cmd->data_direction = data_direction;
922d28b1169SAndy Grover 	iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
923d28b1169SAndy Grover 	/*
924d28b1169SAndy Grover 	 * Figure out the SAM Task Attribute for the incoming SCSI CDB
925d28b1169SAndy Grover 	 */
926d28b1169SAndy Grover 	if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
927d28b1169SAndy Grover 	    (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
928d28b1169SAndy Grover 		sam_task_attr = MSG_SIMPLE_TAG;
929d28b1169SAndy Grover 	else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
930d28b1169SAndy Grover 		sam_task_attr = MSG_ORDERED_TAG;
931d28b1169SAndy Grover 	else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
932d28b1169SAndy Grover 		sam_task_attr = MSG_HEAD_TAG;
933d28b1169SAndy Grover 	else if (iscsi_task_attr == ISCSI_ATTR_ACA)
934d28b1169SAndy Grover 		sam_task_attr = MSG_ACA_TAG;
935d28b1169SAndy Grover 	else {
936d28b1169SAndy Grover 		pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
937d28b1169SAndy Grover 			" MSG_SIMPLE_TAG\n", iscsi_task_attr);
938d28b1169SAndy Grover 		sam_task_attr = MSG_SIMPLE_TAG;
939d28b1169SAndy Grover 	}
940d28b1169SAndy Grover 
941e48354ceSNicholas Bellinger 	cmd->iscsi_opcode	= ISCSI_OP_SCSI_CMD;
942e48354ceSNicholas Bellinger 	cmd->i_state		= ISTATE_NEW_CMD;
943e48354ceSNicholas Bellinger 	cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
944e48354ceSNicholas Bellinger 	cmd->immediate_data	= (payload_length) ? 1 : 0;
945e48354ceSNicholas Bellinger 	cmd->unsolicited_data	= ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
946e48354ceSNicholas Bellinger 				     (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
947e48354ceSNicholas Bellinger 	if (cmd->unsolicited_data)
948e48354ceSNicholas Bellinger 		cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
949e48354ceSNicholas Bellinger 
950e48354ceSNicholas Bellinger 	conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
951e48354ceSNicholas Bellinger 	if (hdr->flags & ISCSI_FLAG_CMD_READ) {
952e48354ceSNicholas Bellinger 		spin_lock_bh(&conn->sess->ttt_lock);
953e48354ceSNicholas Bellinger 		cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
954e48354ceSNicholas Bellinger 		if (cmd->targ_xfer_tag == 0xFFFFFFFF)
955e48354ceSNicholas Bellinger 			cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
956e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->sess->ttt_lock);
957e48354ceSNicholas Bellinger 	} else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
958e48354ceSNicholas Bellinger 		cmd->targ_xfer_tag = 0xFFFFFFFF;
95950e5c87dSChristoph Hellwig 	cmd->cmd_sn		= be32_to_cpu(hdr->cmdsn);
96050e5c87dSChristoph Hellwig 	cmd->exp_stat_sn	= be32_to_cpu(hdr->exp_statsn);
961e48354ceSNicholas Bellinger 	cmd->first_burst_len	= payload_length;
962e48354ceSNicholas Bellinger 
963e48354ceSNicholas Bellinger 	if (cmd->data_direction == DMA_FROM_DEVICE) {
964e48354ceSNicholas Bellinger 		struct iscsi_datain_req *dr;
965e48354ceSNicholas Bellinger 
966e48354ceSNicholas Bellinger 		dr = iscsit_allocate_datain_req();
967e48354ceSNicholas Bellinger 		if (!dr)
968e48354ceSNicholas Bellinger 			return iscsit_add_reject_from_cmd(
969e48354ceSNicholas Bellinger 					ISCSI_REASON_BOOKMARK_NO_RESOURCES,
970e48354ceSNicholas Bellinger 					1, 1, buf, cmd);
971e48354ceSNicholas Bellinger 
972e48354ceSNicholas Bellinger 		iscsit_attach_datain_req(cmd, dr);
973e48354ceSNicholas Bellinger 	}
974e48354ceSNicholas Bellinger 
975e48354ceSNicholas Bellinger 	/*
976065ca1e4SAndy Grover 	 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
977065ca1e4SAndy Grover 	 */
978065ca1e4SAndy Grover 	transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
97950e5c87dSChristoph Hellwig 			conn->sess->se_sess, be32_to_cpu(hdr->data_length),
98050e5c87dSChristoph Hellwig 			cmd->data_direction, sam_task_attr,
98150e5c87dSChristoph Hellwig 			cmd->sense_buffer + 2);
982065ca1e4SAndy Grover 
983065ca1e4SAndy Grover 	pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
984065ca1e4SAndy Grover 		" ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
985065ca1e4SAndy Grover 		hdr->cmdsn, hdr->data_length, payload_length, conn->cid);
986065ca1e4SAndy Grover 
987de103c93SChristoph Hellwig 	cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
9884f26998aSAndy Grover 						     scsilun_to_int(&hdr->lun));
989de103c93SChristoph Hellwig 	if (cmd->sense_reason)
990e48354ceSNicholas Bellinger 		goto attach_cmd;
991a12f41f8SAndy Grover 
992de103c93SChristoph Hellwig 	cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
993de103c93SChristoph Hellwig 	if (cmd->sense_reason) {
994de103c93SChristoph Hellwig 		if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
995e48354ceSNicholas Bellinger 			return iscsit_add_reject_from_cmd(
996e48354ceSNicholas Bellinger 					ISCSI_REASON_BOOKMARK_NO_RESOURCES,
997e48354ceSNicholas Bellinger 					1, 1, buf, cmd);
998de103c93SChristoph Hellwig 		}
999de103c93SChristoph Hellwig 
1000de103c93SChristoph Hellwig 		goto attach_cmd;
1001de103c93SChristoph Hellwig 	}
1002de103c93SChristoph Hellwig 
1003de103c93SChristoph Hellwig 	if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
1004e48354ceSNicholas Bellinger 		return iscsit_add_reject_from_cmd(
1005e48354ceSNicholas Bellinger 			ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1006e48354ceSNicholas Bellinger 			1, 1, buf, cmd);
1007e48354ceSNicholas Bellinger 	}
1008e48354ceSNicholas Bellinger 
1009e48354ceSNicholas Bellinger attach_cmd:
1010e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
10112fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1012e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
1013e48354ceSNicholas Bellinger 	/*
1014e48354ceSNicholas Bellinger 	 * Check if we need to delay processing because of ALUA
1015e48354ceSNicholas Bellinger 	 * Active/NonOptimized primary access state..
1016e48354ceSNicholas Bellinger 	 */
1017e48354ceSNicholas Bellinger 	core_alua_check_nonop_delay(&cmd->se_cmd);
1018bfb79eacSAndy Grover 
1019de103c93SChristoph Hellwig 	if (iscsit_allocate_iovecs(cmd) < 0) {
1020e48354ceSNicholas Bellinger 		return iscsit_add_reject_from_cmd(
1021e48354ceSNicholas Bellinger 				ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1022cd931ee6SNicholas Bellinger 				1, 0, buf, cmd);
1023de103c93SChristoph Hellwig 	}
1024de103c93SChristoph Hellwig 
1025e48354ceSNicholas Bellinger 	/*
1026e48354ceSNicholas Bellinger 	 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1027e48354ceSNicholas Bellinger 	 * the Immediate Bit is not set, and no Immediate
1028e48354ceSNicholas Bellinger 	 * Data is attached.
1029e48354ceSNicholas Bellinger 	 *
1030e48354ceSNicholas Bellinger 	 * A PDU/CmdSN carrying Immediate Data can only
1031e48354ceSNicholas Bellinger 	 * be processed after the DataCRC has passed.
1032e48354ceSNicholas Bellinger 	 * If the DataCRC fails, the CmdSN MUST NOT
1033e48354ceSNicholas Bellinger 	 * be acknowledged. (See below)
1034e48354ceSNicholas Bellinger 	 */
1035e48354ceSNicholas Bellinger 	if (!cmd->immediate_data) {
1036e48354ceSNicholas Bellinger 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
10377e32da55SNicholas Bellinger 		if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
10387e32da55SNicholas Bellinger 			return 0;
10397e32da55SNicholas Bellinger 		else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1040e48354ceSNicholas Bellinger 			return iscsit_add_reject_from_cmd(
1041e48354ceSNicholas Bellinger 				ISCSI_REASON_PROTOCOL_ERROR,
1042e48354ceSNicholas Bellinger 				1, 0, buf, cmd);
1043e48354ceSNicholas Bellinger 	}
1044e48354ceSNicholas Bellinger 
104550e5c87dSChristoph Hellwig 	iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1046e48354ceSNicholas Bellinger 
1047e48354ceSNicholas Bellinger 	/*
1048e48354ceSNicholas Bellinger 	 * If no Immediate Data is attached, it's OK to return now.
1049e48354ceSNicholas Bellinger 	 */
1050e48354ceSNicholas Bellinger 	if (!cmd->immediate_data) {
1051de103c93SChristoph Hellwig 		if (!cmd->sense_reason && cmd->unsolicited_data) {
1052e48354ceSNicholas Bellinger 			iscsit_set_dataout_sequence_values(cmd);
1053e48354ceSNicholas Bellinger 
1054e48354ceSNicholas Bellinger 			spin_lock_bh(&cmd->dataout_timeout_lock);
1055e48354ceSNicholas Bellinger 			iscsit_start_dataout_timer(cmd, cmd->conn);
1056e48354ceSNicholas Bellinger 			spin_unlock_bh(&cmd->dataout_timeout_lock);
1057e48354ceSNicholas Bellinger 		}
1058e48354ceSNicholas Bellinger 
1059e48354ceSNicholas Bellinger 		return 0;
1060e48354ceSNicholas Bellinger 	}
1061e48354ceSNicholas Bellinger 
1062e48354ceSNicholas Bellinger 	/*
1063e48354ceSNicholas Bellinger 	 * Early CHECK_CONDITIONs never make it to the transport processing
1064e48354ceSNicholas Bellinger 	 * thread.  They are processed in CmdSN order by
1065e48354ceSNicholas Bellinger 	 * iscsit_check_received_cmdsn() below.
1066e48354ceSNicholas Bellinger 	 */
1067de103c93SChristoph Hellwig 	if (cmd->sense_reason) {
1068e48354ceSNicholas Bellinger 		immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1069e48354ceSNicholas Bellinger 		goto after_immediate_data;
1070e48354ceSNicholas Bellinger 	}
1071e48354ceSNicholas Bellinger 	/*
1072e48354ceSNicholas Bellinger 	 * Call directly into transport_generic_new_cmd() to perform
1073e48354ceSNicholas Bellinger 	 * the backend memory allocation.
1074e48354ceSNicholas Bellinger 	 */
1075de103c93SChristoph Hellwig 	cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
1076de103c93SChristoph Hellwig 	if (cmd->sense_reason) {
1077e48354ceSNicholas Bellinger 		immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1078e48354ceSNicholas Bellinger 		goto after_immediate_data;
1079e48354ceSNicholas Bellinger 	}
1080e48354ceSNicholas Bellinger 
1081e48354ceSNicholas Bellinger 	immed_ret = iscsit_handle_immediate_data(cmd, buf, payload_length);
1082e48354ceSNicholas Bellinger after_immediate_data:
1083e48354ceSNicholas Bellinger 	if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1084e48354ceSNicholas Bellinger 		/*
1085e48354ceSNicholas Bellinger 		 * A PDU/CmdSN carrying Immediate Data passed
1086e48354ceSNicholas Bellinger 		 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1087e48354ceSNicholas Bellinger 		 * Immediate Bit is not set.
1088e48354ceSNicholas Bellinger 		 */
1089e48354ceSNicholas Bellinger 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1090e48354ceSNicholas Bellinger 		/*
1091e48354ceSNicholas Bellinger 		 * Special case for Unsupported SAM WRITE Opcodes
1092e48354ceSNicholas Bellinger 		 * and ImmediateData=Yes.
1093e48354ceSNicholas Bellinger 		 */
1094de103c93SChristoph Hellwig 		if (cmd->sense_reason) {
1095e48354ceSNicholas Bellinger 			if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
1096e48354ceSNicholas Bellinger 				return -1;
1097e48354ceSNicholas Bellinger 		} else if (cmd->unsolicited_data) {
1098e48354ceSNicholas Bellinger 			iscsit_set_dataout_sequence_values(cmd);
1099e48354ceSNicholas Bellinger 
1100e48354ceSNicholas Bellinger 			spin_lock_bh(&cmd->dataout_timeout_lock);
1101e48354ceSNicholas Bellinger 			iscsit_start_dataout_timer(cmd, cmd->conn);
1102e48354ceSNicholas Bellinger 			spin_unlock_bh(&cmd->dataout_timeout_lock);
1103e48354ceSNicholas Bellinger 		}
1104e48354ceSNicholas Bellinger 
1105e48354ceSNicholas Bellinger 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1106e48354ceSNicholas Bellinger 			return iscsit_add_reject_from_cmd(
1107e48354ceSNicholas Bellinger 				ISCSI_REASON_PROTOCOL_ERROR,
1108e48354ceSNicholas Bellinger 				1, 0, buf, cmd);
1109e48354ceSNicholas Bellinger 
1110e48354ceSNicholas Bellinger 	} else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1111e48354ceSNicholas Bellinger 		/*
1112e48354ceSNicholas Bellinger 		 * Immediate Data failed DataCRC and ERL>=1,
1113e48354ceSNicholas Bellinger 		 * silently drop this PDU and let the initiator
1114e48354ceSNicholas Bellinger 		 * plug the CmdSN gap.
1115e48354ceSNicholas Bellinger 		 *
1116e48354ceSNicholas Bellinger 		 * FIXME: Send Unsolicited NOPIN with reserved
1117e48354ceSNicholas Bellinger 		 * TTT here to help the initiator figure out
1118e48354ceSNicholas Bellinger 		 * the missing CmdSN, although they should be
1119e48354ceSNicholas Bellinger 		 * intelligent enough to determine the missing
1120e48354ceSNicholas Bellinger 		 * CmdSN and issue a retry to plug the sequence.
1121e48354ceSNicholas Bellinger 		 */
1122e48354ceSNicholas Bellinger 		cmd->i_state = ISTATE_REMOVE;
1123e48354ceSNicholas Bellinger 		iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1124e48354ceSNicholas Bellinger 	} else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1125e48354ceSNicholas Bellinger 		return -1;
1126e48354ceSNicholas Bellinger 
1127e48354ceSNicholas Bellinger 	return 0;
1128e48354ceSNicholas Bellinger }
1129e48354ceSNicholas Bellinger 
1130e48354ceSNicholas Bellinger static u32 iscsit_do_crypto_hash_sg(
1131e48354ceSNicholas Bellinger 	struct hash_desc *hash,
1132e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
1133e48354ceSNicholas Bellinger 	u32 data_offset,
1134e48354ceSNicholas Bellinger 	u32 data_length,
1135e48354ceSNicholas Bellinger 	u32 padding,
1136e48354ceSNicholas Bellinger 	u8 *pad_bytes)
1137e48354ceSNicholas Bellinger {
1138e48354ceSNicholas Bellinger 	u32 data_crc;
1139e48354ceSNicholas Bellinger 	u32 i;
1140e48354ceSNicholas Bellinger 	struct scatterlist *sg;
1141e48354ceSNicholas Bellinger 	unsigned int page_off;
1142e48354ceSNicholas Bellinger 
1143e48354ceSNicholas Bellinger 	crypto_hash_init(hash);
1144e48354ceSNicholas Bellinger 
1145e48354ceSNicholas Bellinger 	sg = cmd->first_data_sg;
1146e48354ceSNicholas Bellinger 	page_off = cmd->first_data_sg_off;
1147e48354ceSNicholas Bellinger 
1148e48354ceSNicholas Bellinger 	i = 0;
1149e48354ceSNicholas Bellinger 	while (data_length) {
1150e48354ceSNicholas Bellinger 		u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1151e48354ceSNicholas Bellinger 
1152e48354ceSNicholas Bellinger 		crypto_hash_update(hash, &sg[i], cur_len);
1153e48354ceSNicholas Bellinger 
1154e48354ceSNicholas Bellinger 		data_length -= cur_len;
1155e48354ceSNicholas Bellinger 		page_off = 0;
1156e48354ceSNicholas Bellinger 		i++;
1157e48354ceSNicholas Bellinger 	}
1158e48354ceSNicholas Bellinger 
1159e48354ceSNicholas Bellinger 	if (padding) {
1160e48354ceSNicholas Bellinger 		struct scatterlist pad_sg;
1161e48354ceSNicholas Bellinger 
1162e48354ceSNicholas Bellinger 		sg_init_one(&pad_sg, pad_bytes, padding);
1163e48354ceSNicholas Bellinger 		crypto_hash_update(hash, &pad_sg, padding);
1164e48354ceSNicholas Bellinger 	}
1165e48354ceSNicholas Bellinger 	crypto_hash_final(hash, (u8 *) &data_crc);
1166e48354ceSNicholas Bellinger 
1167e48354ceSNicholas Bellinger 	return data_crc;
1168e48354ceSNicholas Bellinger }
1169e48354ceSNicholas Bellinger 
1170e48354ceSNicholas Bellinger static void iscsit_do_crypto_hash_buf(
1171e48354ceSNicholas Bellinger 	struct hash_desc *hash,
1172e48354ceSNicholas Bellinger 	unsigned char *buf,
1173e48354ceSNicholas Bellinger 	u32 payload_length,
1174e48354ceSNicholas Bellinger 	u32 padding,
1175e48354ceSNicholas Bellinger 	u8 *pad_bytes,
1176e48354ceSNicholas Bellinger 	u8 *data_crc)
1177e48354ceSNicholas Bellinger {
1178e48354ceSNicholas Bellinger 	struct scatterlist sg;
1179e48354ceSNicholas Bellinger 
1180e48354ceSNicholas Bellinger 	crypto_hash_init(hash);
1181e48354ceSNicholas Bellinger 
11828359cf43SJörn Engel 	sg_init_one(&sg, buf, payload_length);
1183e48354ceSNicholas Bellinger 	crypto_hash_update(hash, &sg, payload_length);
1184e48354ceSNicholas Bellinger 
1185e48354ceSNicholas Bellinger 	if (padding) {
1186e48354ceSNicholas Bellinger 		sg_init_one(&sg, pad_bytes, padding);
1187e48354ceSNicholas Bellinger 		crypto_hash_update(hash, &sg, padding);
1188e48354ceSNicholas Bellinger 	}
1189e48354ceSNicholas Bellinger 	crypto_hash_final(hash, data_crc);
1190e48354ceSNicholas Bellinger }
1191e48354ceSNicholas Bellinger 
1192e48354ceSNicholas Bellinger static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1193e48354ceSNicholas Bellinger {
1194e48354ceSNicholas Bellinger 	int iov_ret, ooo_cmdsn = 0, ret;
1195e48354ceSNicholas Bellinger 	u8 data_crc_failed = 0;
1196e48354ceSNicholas Bellinger 	u32 checksum, iov_count = 0, padding = 0, rx_got = 0;
1197e48354ceSNicholas Bellinger 	u32 rx_size = 0, payload_length;
1198e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1199e48354ceSNicholas Bellinger 	struct se_cmd *se_cmd;
1200e48354ceSNicholas Bellinger 	struct iscsi_data *hdr;
1201e48354ceSNicholas Bellinger 	struct kvec *iov;
1202e48354ceSNicholas Bellinger 	unsigned long flags;
1203e48354ceSNicholas Bellinger 
1204e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_data *) buf;
1205e48354ceSNicholas Bellinger 	payload_length		= ntoh24(hdr->dlength);
1206e48354ceSNicholas Bellinger 
1207e48354ceSNicholas Bellinger 	if (!payload_length) {
1208e48354ceSNicholas Bellinger 		pr_err("DataOUT payload is ZERO, protocol error.\n");
1209e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1210e48354ceSNicholas Bellinger 					buf, conn);
1211e48354ceSNicholas Bellinger 	}
1212e48354ceSNicholas Bellinger 
1213e48354ceSNicholas Bellinger 	/* iSCSI write */
1214e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->sess->session_stats_lock);
1215e48354ceSNicholas Bellinger 	conn->sess->rx_data_octets += payload_length;
1216e48354ceSNicholas Bellinger 	if (conn->sess->se_sess->se_node_acl) {
1217e48354ceSNicholas Bellinger 		spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
1218e48354ceSNicholas Bellinger 		conn->sess->se_sess->se_node_acl->write_bytes += payload_length;
1219e48354ceSNicholas Bellinger 		spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
1220e48354ceSNicholas Bellinger 	}
1221e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->sess->session_stats_lock);
1222e48354ceSNicholas Bellinger 
122321f5aa7eSNicholas Bellinger 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1224e48354ceSNicholas Bellinger 		pr_err("DataSegmentLength: %u is greater than"
122521f5aa7eSNicholas Bellinger 			" MaxXmitDataSegmentLength: %u\n", payload_length,
122621f5aa7eSNicholas Bellinger 			conn->conn_ops->MaxXmitDataSegmentLength);
1227e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1228e48354ceSNicholas Bellinger 					buf, conn);
1229e48354ceSNicholas Bellinger 	}
1230e48354ceSNicholas Bellinger 
1231e48354ceSNicholas Bellinger 	cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1232e48354ceSNicholas Bellinger 			payload_length);
1233e48354ceSNicholas Bellinger 	if (!cmd)
1234e48354ceSNicholas Bellinger 		return 0;
1235e48354ceSNicholas Bellinger 
1236e48354ceSNicholas Bellinger 	pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1237e48354ceSNicholas Bellinger 		" DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1238e48354ceSNicholas Bellinger 		hdr->itt, hdr->ttt, hdr->datasn, hdr->offset,
1239e48354ceSNicholas Bellinger 		payload_length, conn->cid);
1240e48354ceSNicholas Bellinger 
1241e48354ceSNicholas Bellinger 	if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1242e48354ceSNicholas Bellinger 		pr_err("Command ITT: 0x%08x received DataOUT after"
1243e48354ceSNicholas Bellinger 			" last DataOUT received, dumping payload\n",
1244e48354ceSNicholas Bellinger 			cmd->init_task_tag);
1245e48354ceSNicholas Bellinger 		return iscsit_dump_data_payload(conn, payload_length, 1);
1246e48354ceSNicholas Bellinger 	}
1247e48354ceSNicholas Bellinger 
1248e48354ceSNicholas Bellinger 	if (cmd->data_direction != DMA_TO_DEVICE) {
1249e48354ceSNicholas Bellinger 		pr_err("Command ITT: 0x%08x received DataOUT for a"
1250e48354ceSNicholas Bellinger 			" NON-WRITE command.\n", cmd->init_task_tag);
1251e48354ceSNicholas Bellinger 		return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
1252e48354ceSNicholas Bellinger 				1, 0, buf, cmd);
1253e48354ceSNicholas Bellinger 	}
1254e48354ceSNicholas Bellinger 	se_cmd = &cmd->se_cmd;
1255e48354ceSNicholas Bellinger 	iscsit_mod_dataout_timer(cmd);
1256e48354ceSNicholas Bellinger 
125750e5c87dSChristoph Hellwig 	if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
1258e48354ceSNicholas Bellinger 		pr_err("DataOut Offset: %u, Length %u greater than"
1259e48354ceSNicholas Bellinger 			" iSCSI Command EDTL %u, protocol error.\n",
1260ebf1d95cSAndy Grover 			hdr->offset, payload_length, cmd->se_cmd.data_length);
1261e48354ceSNicholas Bellinger 		return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
1262e48354ceSNicholas Bellinger 				1, 0, buf, cmd);
1263e48354ceSNicholas Bellinger 	}
1264e48354ceSNicholas Bellinger 
1265e48354ceSNicholas Bellinger 	if (cmd->unsolicited_data) {
1266e48354ceSNicholas Bellinger 		int dump_unsolicited_data = 0;
1267e48354ceSNicholas Bellinger 
1268e48354ceSNicholas Bellinger 		if (conn->sess->sess_ops->InitialR2T) {
1269e48354ceSNicholas Bellinger 			pr_err("Received unexpected unsolicited data"
1270e48354ceSNicholas Bellinger 				" while InitialR2T=Yes, protocol error.\n");
1271e48354ceSNicholas Bellinger 			transport_send_check_condition_and_sense(&cmd->se_cmd,
1272e48354ceSNicholas Bellinger 					TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1273e48354ceSNicholas Bellinger 			return -1;
1274e48354ceSNicholas Bellinger 		}
1275e48354ceSNicholas Bellinger 		/*
1276e48354ceSNicholas Bellinger 		 * Special case for dealing with Unsolicited DataOUT
1277e48354ceSNicholas Bellinger 		 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1278e48354ceSNicholas Bellinger 		 * failures;
1279e48354ceSNicholas Bellinger 		 */
1280e48354ceSNicholas Bellinger 
1281e48354ceSNicholas Bellinger 		/* Something's amiss if we're not in WRITE_PENDING state... */
1282e48354ceSNicholas Bellinger 		spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1283e48354ceSNicholas Bellinger 		WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1284e48354ceSNicholas Bellinger 		spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1285e48354ceSNicholas Bellinger 
1286e48354ceSNicholas Bellinger 		spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1287de103c93SChristoph Hellwig 		if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
1288e48354ceSNicholas Bellinger 			dump_unsolicited_data = 1;
1289e48354ceSNicholas Bellinger 		spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1290e48354ceSNicholas Bellinger 
1291e48354ceSNicholas Bellinger 		if (dump_unsolicited_data) {
1292e48354ceSNicholas Bellinger 			/*
1293e48354ceSNicholas Bellinger 			 * Check if a delayed TASK_ABORTED status needs to
1294e48354ceSNicholas Bellinger 			 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1295e48354ceSNicholas Bellinger 			 * received with the unsolicitied data out.
1296e48354ceSNicholas Bellinger 			 */
1297e48354ceSNicholas Bellinger 			if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1298e48354ceSNicholas Bellinger 				iscsit_stop_dataout_timer(cmd);
1299e48354ceSNicholas Bellinger 
1300e48354ceSNicholas Bellinger 			transport_check_aborted_status(se_cmd,
1301e48354ceSNicholas Bellinger 					(hdr->flags & ISCSI_FLAG_CMD_FINAL));
1302e48354ceSNicholas Bellinger 			return iscsit_dump_data_payload(conn, payload_length, 1);
1303e48354ceSNicholas Bellinger 		}
1304e48354ceSNicholas Bellinger 	} else {
1305e48354ceSNicholas Bellinger 		/*
1306e48354ceSNicholas Bellinger 		 * For the normal solicited data path:
1307e48354ceSNicholas Bellinger 		 *
1308e48354ceSNicholas Bellinger 		 * Check for a delayed TASK_ABORTED status and dump any
1309e48354ceSNicholas Bellinger 		 * incoming data out payload if one exists.  Also, when the
1310e48354ceSNicholas Bellinger 		 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1311e48354ceSNicholas Bellinger 		 * data out sequence, we decrement outstanding_r2ts.  Once
1312e48354ceSNicholas Bellinger 		 * outstanding_r2ts reaches zero, go ahead and send the delayed
1313e48354ceSNicholas Bellinger 		 * TASK_ABORTED status.
1314e48354ceSNicholas Bellinger 		 */
13157d680f3bSChristoph Hellwig 		if (se_cmd->transport_state & CMD_T_ABORTED) {
1316e48354ceSNicholas Bellinger 			if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1317e48354ceSNicholas Bellinger 				if (--cmd->outstanding_r2ts < 1) {
1318e48354ceSNicholas Bellinger 					iscsit_stop_dataout_timer(cmd);
1319e48354ceSNicholas Bellinger 					transport_check_aborted_status(
1320e48354ceSNicholas Bellinger 							se_cmd, 1);
1321e48354ceSNicholas Bellinger 				}
1322e48354ceSNicholas Bellinger 
1323e48354ceSNicholas Bellinger 			return iscsit_dump_data_payload(conn, payload_length, 1);
1324e48354ceSNicholas Bellinger 		}
1325e48354ceSNicholas Bellinger 	}
1326e48354ceSNicholas Bellinger 	/*
1327e48354ceSNicholas Bellinger 	 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1328e48354ceSNicholas Bellinger 	 * within-command recovery checks before receiving the payload.
1329e48354ceSNicholas Bellinger 	 */
1330e48354ceSNicholas Bellinger 	ret = iscsit_check_pre_dataout(cmd, buf);
1331e48354ceSNicholas Bellinger 	if (ret == DATAOUT_WITHIN_COMMAND_RECOVERY)
1332e48354ceSNicholas Bellinger 		return 0;
1333e48354ceSNicholas Bellinger 	else if (ret == DATAOUT_CANNOT_RECOVER)
1334e48354ceSNicholas Bellinger 		return -1;
1335e48354ceSNicholas Bellinger 
1336e48354ceSNicholas Bellinger 	rx_size += payload_length;
1337e48354ceSNicholas Bellinger 	iov = &cmd->iov_data[0];
1338e48354ceSNicholas Bellinger 
133950e5c87dSChristoph Hellwig 	iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
134050e5c87dSChristoph Hellwig 				   payload_length);
1341e48354ceSNicholas Bellinger 	if (iov_ret < 0)
1342e48354ceSNicholas Bellinger 		return -1;
1343e48354ceSNicholas Bellinger 
1344e48354ceSNicholas Bellinger 	iov_count += iov_ret;
1345e48354ceSNicholas Bellinger 
1346e48354ceSNicholas Bellinger 	padding = ((-payload_length) & 3);
1347e48354ceSNicholas Bellinger 	if (padding != 0) {
1348e48354ceSNicholas Bellinger 		iov[iov_count].iov_base	= cmd->pad_bytes;
1349e48354ceSNicholas Bellinger 		iov[iov_count++].iov_len = padding;
1350e48354ceSNicholas Bellinger 		rx_size += padding;
1351e48354ceSNicholas Bellinger 		pr_debug("Receiving %u padding bytes.\n", padding);
1352e48354ceSNicholas Bellinger 	}
1353e48354ceSNicholas Bellinger 
1354e48354ceSNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
1355e48354ceSNicholas Bellinger 		iov[iov_count].iov_base = &checksum;
1356e48354ceSNicholas Bellinger 		iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1357e48354ceSNicholas Bellinger 		rx_size += ISCSI_CRC_LEN;
1358e48354ceSNicholas Bellinger 	}
1359e48354ceSNicholas Bellinger 
1360e48354ceSNicholas Bellinger 	rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1361e48354ceSNicholas Bellinger 
1362e48354ceSNicholas Bellinger 	iscsit_unmap_iovec(cmd);
1363e48354ceSNicholas Bellinger 
1364e48354ceSNicholas Bellinger 	if (rx_got != rx_size)
1365e48354ceSNicholas Bellinger 		return -1;
1366e48354ceSNicholas Bellinger 
1367e48354ceSNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
1368e48354ceSNicholas Bellinger 		u32 data_crc;
1369e48354ceSNicholas Bellinger 
1370e48354ceSNicholas Bellinger 		data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
137150e5c87dSChristoph Hellwig 						    be32_to_cpu(hdr->offset),
137250e5c87dSChristoph Hellwig 						    payload_length, padding,
1373e48354ceSNicholas Bellinger 						    cmd->pad_bytes);
1374e48354ceSNicholas Bellinger 
1375e48354ceSNicholas Bellinger 		if (checksum != data_crc) {
1376e48354ceSNicholas Bellinger 			pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1377e48354ceSNicholas Bellinger 				" DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1378e48354ceSNicholas Bellinger 				" does not match computed 0x%08x\n",
1379e48354ceSNicholas Bellinger 				hdr->itt, hdr->offset, payload_length,
1380e48354ceSNicholas Bellinger 				hdr->datasn, checksum, data_crc);
1381e48354ceSNicholas Bellinger 			data_crc_failed = 1;
1382e48354ceSNicholas Bellinger 		} else {
1383e48354ceSNicholas Bellinger 			pr_debug("Got CRC32C DataDigest 0x%08x for"
1384e48354ceSNicholas Bellinger 				" %u bytes of Data Out\n", checksum,
1385e48354ceSNicholas Bellinger 				payload_length);
1386e48354ceSNicholas Bellinger 		}
1387e48354ceSNicholas Bellinger 	}
1388e48354ceSNicholas Bellinger 	/*
1389e48354ceSNicholas Bellinger 	 * Increment post receive data and CRC values or perform
1390e48354ceSNicholas Bellinger 	 * within-command recovery.
1391e48354ceSNicholas Bellinger 	 */
1392e48354ceSNicholas Bellinger 	ret = iscsit_check_post_dataout(cmd, buf, data_crc_failed);
1393e48354ceSNicholas Bellinger 	if ((ret == DATAOUT_NORMAL) || (ret == DATAOUT_WITHIN_COMMAND_RECOVERY))
1394e48354ceSNicholas Bellinger 		return 0;
1395e48354ceSNicholas Bellinger 	else if (ret == DATAOUT_SEND_R2T) {
1396e48354ceSNicholas Bellinger 		iscsit_set_dataout_sequence_values(cmd);
13978b1e1244SAndy Grover 		iscsit_build_r2ts_for_cmd(cmd, conn, false);
1398e48354ceSNicholas Bellinger 	} else if (ret == DATAOUT_SEND_TO_TRANSPORT) {
1399e48354ceSNicholas Bellinger 		/*
1400e48354ceSNicholas Bellinger 		 * Handle extra special case for out of order
1401e48354ceSNicholas Bellinger 		 * Unsolicited Data Out.
1402e48354ceSNicholas Bellinger 		 */
1403e48354ceSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
1404e48354ceSNicholas Bellinger 		ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1405e48354ceSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1406e48354ceSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1407e48354ceSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
1408e48354ceSNicholas Bellinger 
1409e48354ceSNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
141067441b68SChristoph Hellwig 		if (ooo_cmdsn)
141167441b68SChristoph Hellwig 			return 0;
141267441b68SChristoph Hellwig 		target_execute_cmd(&cmd->se_cmd);
141367441b68SChristoph Hellwig 		return 0;
1414e48354ceSNicholas Bellinger 	} else /* DATAOUT_CANNOT_RECOVER */
1415e48354ceSNicholas Bellinger 		return -1;
1416e48354ceSNicholas Bellinger 
1417e48354ceSNicholas Bellinger 	return 0;
1418e48354ceSNicholas Bellinger }
1419e48354ceSNicholas Bellinger 
1420e48354ceSNicholas Bellinger static int iscsit_handle_nop_out(
1421e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
1422e48354ceSNicholas Bellinger 	unsigned char *buf)
1423e48354ceSNicholas Bellinger {
1424e48354ceSNicholas Bellinger 	unsigned char *ping_data = NULL;
1425e48354ceSNicholas Bellinger 	int cmdsn_ret, niov = 0, ret = 0, rx_got, rx_size;
1426e48354ceSNicholas Bellinger 	u32 checksum, data_crc, padding = 0, payload_length;
1427e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
1428e48354ceSNicholas Bellinger 	struct kvec *iov = NULL;
1429e48354ceSNicholas Bellinger 	struct iscsi_nopout *hdr;
1430e48354ceSNicholas Bellinger 
1431e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_nopout *) buf;
1432e48354ceSNicholas Bellinger 	payload_length		= ntoh24(hdr->dlength);
1433e48354ceSNicholas Bellinger 
143466c7db68SChristoph Hellwig 	if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1435e48354ceSNicholas Bellinger 		pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1436e48354ceSNicholas Bellinger 			" not set, protocol error.\n");
1437e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1438e48354ceSNicholas Bellinger 					buf, conn);
1439e48354ceSNicholas Bellinger 	}
1440e48354ceSNicholas Bellinger 
144121f5aa7eSNicholas Bellinger 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1442e48354ceSNicholas Bellinger 		pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
144321f5aa7eSNicholas Bellinger 			" greater than MaxXmitDataSegmentLength: %u, protocol"
1444e48354ceSNicholas Bellinger 			" error.\n", payload_length,
144521f5aa7eSNicholas Bellinger 			conn->conn_ops->MaxXmitDataSegmentLength);
1446e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1447e48354ceSNicholas Bellinger 					buf, conn);
1448e48354ceSNicholas Bellinger 	}
1449e48354ceSNicholas Bellinger 
1450e48354ceSNicholas Bellinger 	pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%09x,"
1451e48354ceSNicholas Bellinger 		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
145266c7db68SChristoph Hellwig 		hdr->itt == RESERVED_ITT ? "Response" : "Request",
1453e48354ceSNicholas Bellinger 		hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1454e48354ceSNicholas Bellinger 		payload_length);
1455e48354ceSNicholas Bellinger 	/*
1456e48354ceSNicholas Bellinger 	 * This is not a response to a Unsolicited NopIN, which means
1457e48354ceSNicholas Bellinger 	 * it can either be a NOPOUT ping request (with a valid ITT),
1458e48354ceSNicholas Bellinger 	 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1459e48354ceSNicholas Bellinger 	 * Either way, make sure we allocate an struct iscsi_cmd, as both
1460e48354ceSNicholas Bellinger 	 * can contain ping data.
1461e48354ceSNicholas Bellinger 	 */
146250e5c87dSChristoph Hellwig 	if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1463e48354ceSNicholas Bellinger 		cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1464e48354ceSNicholas Bellinger 		if (!cmd)
1465e48354ceSNicholas Bellinger 			return iscsit_add_reject(
1466e48354ceSNicholas Bellinger 					ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1467e48354ceSNicholas Bellinger 					1, buf, conn);
1468e48354ceSNicholas Bellinger 
1469e48354ceSNicholas Bellinger 		cmd->iscsi_opcode	= ISCSI_OP_NOOP_OUT;
1470e48354ceSNicholas Bellinger 		cmd->i_state		= ISTATE_SEND_NOPIN;
1471e48354ceSNicholas Bellinger 		cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1472e48354ceSNicholas Bellinger 						1 : 0);
1473e48354ceSNicholas Bellinger 		conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1474e48354ceSNicholas Bellinger 		cmd->targ_xfer_tag	= 0xFFFFFFFF;
147550e5c87dSChristoph Hellwig 		cmd->cmd_sn		= be32_to_cpu(hdr->cmdsn);
147650e5c87dSChristoph Hellwig 		cmd->exp_stat_sn	= be32_to_cpu(hdr->exp_statsn);
1477e48354ceSNicholas Bellinger 		cmd->data_direction	= DMA_NONE;
1478e48354ceSNicholas Bellinger 	}
1479e48354ceSNicholas Bellinger 
148050e5c87dSChristoph Hellwig 	if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1481e48354ceSNicholas Bellinger 		rx_size = payload_length;
1482e48354ceSNicholas Bellinger 		ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1483e48354ceSNicholas Bellinger 		if (!ping_data) {
1484e48354ceSNicholas Bellinger 			pr_err("Unable to allocate memory for"
1485e48354ceSNicholas Bellinger 				" NOPOUT ping data.\n");
1486e48354ceSNicholas Bellinger 			ret = -1;
1487e48354ceSNicholas Bellinger 			goto out;
1488e48354ceSNicholas Bellinger 		}
1489e48354ceSNicholas Bellinger 
1490e48354ceSNicholas Bellinger 		iov = &cmd->iov_misc[0];
1491e48354ceSNicholas Bellinger 		iov[niov].iov_base	= ping_data;
1492e48354ceSNicholas Bellinger 		iov[niov++].iov_len	= payload_length;
1493e48354ceSNicholas Bellinger 
1494e48354ceSNicholas Bellinger 		padding = ((-payload_length) & 3);
1495e48354ceSNicholas Bellinger 		if (padding != 0) {
1496e48354ceSNicholas Bellinger 			pr_debug("Receiving %u additional bytes"
1497e48354ceSNicholas Bellinger 				" for padding.\n", padding);
1498e48354ceSNicholas Bellinger 			iov[niov].iov_base	= &cmd->pad_bytes;
1499e48354ceSNicholas Bellinger 			iov[niov++].iov_len	= padding;
1500e48354ceSNicholas Bellinger 			rx_size += padding;
1501e48354ceSNicholas Bellinger 		}
1502e48354ceSNicholas Bellinger 		if (conn->conn_ops->DataDigest) {
1503e48354ceSNicholas Bellinger 			iov[niov].iov_base	= &checksum;
1504e48354ceSNicholas Bellinger 			iov[niov++].iov_len	= ISCSI_CRC_LEN;
1505e48354ceSNicholas Bellinger 			rx_size += ISCSI_CRC_LEN;
1506e48354ceSNicholas Bellinger 		}
1507e48354ceSNicholas Bellinger 
1508e48354ceSNicholas Bellinger 		rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1509e48354ceSNicholas Bellinger 		if (rx_got != rx_size) {
1510e48354ceSNicholas Bellinger 			ret = -1;
1511e48354ceSNicholas Bellinger 			goto out;
1512e48354ceSNicholas Bellinger 		}
1513e48354ceSNicholas Bellinger 
1514e48354ceSNicholas Bellinger 		if (conn->conn_ops->DataDigest) {
1515e48354ceSNicholas Bellinger 			iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1516e48354ceSNicholas Bellinger 					ping_data, payload_length,
1517e48354ceSNicholas Bellinger 					padding, cmd->pad_bytes,
1518e48354ceSNicholas Bellinger 					(u8 *)&data_crc);
1519e48354ceSNicholas Bellinger 
1520e48354ceSNicholas Bellinger 			if (checksum != data_crc) {
1521e48354ceSNicholas Bellinger 				pr_err("Ping data CRC32C DataDigest"
1522e48354ceSNicholas Bellinger 				" 0x%08x does not match computed 0x%08x\n",
1523e48354ceSNicholas Bellinger 					checksum, data_crc);
1524e48354ceSNicholas Bellinger 				if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1525e48354ceSNicholas Bellinger 					pr_err("Unable to recover from"
1526e48354ceSNicholas Bellinger 					" NOPOUT Ping DataCRC failure while in"
1527e48354ceSNicholas Bellinger 						" ERL=0.\n");
1528e48354ceSNicholas Bellinger 					ret = -1;
1529e48354ceSNicholas Bellinger 					goto out;
1530e48354ceSNicholas Bellinger 				} else {
1531e48354ceSNicholas Bellinger 					/*
1532e48354ceSNicholas Bellinger 					 * Silently drop this PDU and let the
1533e48354ceSNicholas Bellinger 					 * initiator plug the CmdSN gap.
1534e48354ceSNicholas Bellinger 					 */
1535e48354ceSNicholas Bellinger 					pr_debug("Dropping NOPOUT"
1536e48354ceSNicholas Bellinger 					" Command CmdSN: 0x%08x due to"
1537e48354ceSNicholas Bellinger 					" DataCRC error.\n", hdr->cmdsn);
1538e48354ceSNicholas Bellinger 					ret = 0;
1539e48354ceSNicholas Bellinger 					goto out;
1540e48354ceSNicholas Bellinger 				}
1541e48354ceSNicholas Bellinger 			} else {
1542e48354ceSNicholas Bellinger 				pr_debug("Got CRC32C DataDigest"
1543e48354ceSNicholas Bellinger 				" 0x%08x for %u bytes of ping data.\n",
1544e48354ceSNicholas Bellinger 					checksum, payload_length);
1545e48354ceSNicholas Bellinger 			}
1546e48354ceSNicholas Bellinger 		}
1547e48354ceSNicholas Bellinger 
1548e48354ceSNicholas Bellinger 		ping_data[payload_length] = '\0';
1549e48354ceSNicholas Bellinger 		/*
1550e48354ceSNicholas Bellinger 		 * Attach ping data to struct iscsi_cmd->buf_ptr.
1551e48354ceSNicholas Bellinger 		 */
15528359cf43SJörn Engel 		cmd->buf_ptr = ping_data;
1553e48354ceSNicholas Bellinger 		cmd->buf_ptr_size = payload_length;
1554e48354ceSNicholas Bellinger 
1555e48354ceSNicholas Bellinger 		pr_debug("Got %u bytes of NOPOUT ping"
1556e48354ceSNicholas Bellinger 			" data.\n", payload_length);
1557e48354ceSNicholas Bellinger 		pr_debug("Ping Data: \"%s\"\n", ping_data);
1558e48354ceSNicholas Bellinger 	}
1559e48354ceSNicholas Bellinger 
156066c7db68SChristoph Hellwig 	if (hdr->itt != RESERVED_ITT) {
1561e48354ceSNicholas Bellinger 		if (!cmd) {
1562e48354ceSNicholas Bellinger 			pr_err("Checking CmdSN for NOPOUT,"
1563e48354ceSNicholas Bellinger 				" but cmd is NULL!\n");
1564e48354ceSNicholas Bellinger 			return -1;
1565e48354ceSNicholas Bellinger 		}
1566e48354ceSNicholas Bellinger 		/*
1567e48354ceSNicholas Bellinger 		 * Initiator is expecting a NopIN ping reply,
1568e48354ceSNicholas Bellinger 		 */
1569e48354ceSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
15702fbb471eSAndy Grover 		list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1571e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
1572e48354ceSNicholas Bellinger 
157350e5c87dSChristoph Hellwig 		iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1574e48354ceSNicholas Bellinger 
1575e48354ceSNicholas Bellinger 		if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1576e48354ceSNicholas Bellinger 			iscsit_add_cmd_to_response_queue(cmd, conn,
1577e48354ceSNicholas Bellinger 					cmd->i_state);
1578e48354ceSNicholas Bellinger 			return 0;
1579e48354ceSNicholas Bellinger 		}
1580e48354ceSNicholas Bellinger 
1581e48354ceSNicholas Bellinger 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1582e48354ceSNicholas Bellinger 		if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1583e48354ceSNicholas Bellinger 			ret = 0;
1584e48354ceSNicholas Bellinger 			goto ping_out;
1585e48354ceSNicholas Bellinger 		}
1586e48354ceSNicholas Bellinger 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1587e48354ceSNicholas Bellinger 			return iscsit_add_reject_from_cmd(
1588e48354ceSNicholas Bellinger 					ISCSI_REASON_PROTOCOL_ERROR,
1589e48354ceSNicholas Bellinger 					1, 0, buf, cmd);
1590e48354ceSNicholas Bellinger 
1591e48354ceSNicholas Bellinger 		return 0;
1592e48354ceSNicholas Bellinger 	}
1593e48354ceSNicholas Bellinger 
159450e5c87dSChristoph Hellwig 	if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1595e48354ceSNicholas Bellinger 		/*
1596e48354ceSNicholas Bellinger 		 * This was a response to a unsolicited NOPIN ping.
1597e48354ceSNicholas Bellinger 		 */
159850e5c87dSChristoph Hellwig 		cmd = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1599e48354ceSNicholas Bellinger 		if (!cmd)
1600e48354ceSNicholas Bellinger 			return -1;
1601e48354ceSNicholas Bellinger 
1602e48354ceSNicholas Bellinger 		iscsit_stop_nopin_response_timer(conn);
1603e48354ceSNicholas Bellinger 
1604e48354ceSNicholas Bellinger 		cmd->i_state = ISTATE_REMOVE;
1605e48354ceSNicholas Bellinger 		iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1606e48354ceSNicholas Bellinger 		iscsit_start_nopin_timer(conn);
1607e48354ceSNicholas Bellinger 	} else {
1608e48354ceSNicholas Bellinger 		/*
1609e48354ceSNicholas Bellinger 		 * Initiator is not expecting a NOPIN is response.
1610e48354ceSNicholas Bellinger 		 * Just ignore for now.
1611e48354ceSNicholas Bellinger 		 *
1612e48354ceSNicholas Bellinger 		 * iSCSI v19-91 10.18
1613e48354ceSNicholas Bellinger 		 * "A NOP-OUT may also be used to confirm a changed
1614e48354ceSNicholas Bellinger 		 *  ExpStatSN if another PDU will not be available
1615e48354ceSNicholas Bellinger 		 *  for a long time."
1616e48354ceSNicholas Bellinger 		 */
1617e48354ceSNicholas Bellinger 		ret = 0;
1618e48354ceSNicholas Bellinger 		goto out;
1619e48354ceSNicholas Bellinger 	}
1620e48354ceSNicholas Bellinger 
1621e48354ceSNicholas Bellinger 	return 0;
1622e48354ceSNicholas Bellinger out:
1623e48354ceSNicholas Bellinger 	if (cmd)
1624e48354ceSNicholas Bellinger 		iscsit_release_cmd(cmd);
1625e48354ceSNicholas Bellinger ping_out:
1626e48354ceSNicholas Bellinger 	kfree(ping_data);
1627e48354ceSNicholas Bellinger 	return ret;
1628e48354ceSNicholas Bellinger }
1629e48354ceSNicholas Bellinger 
1630e48354ceSNicholas Bellinger static int iscsit_handle_task_mgt_cmd(
1631e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
1632e48354ceSNicholas Bellinger 	unsigned char *buf)
1633e48354ceSNicholas Bellinger {
1634e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd;
1635e48354ceSNicholas Bellinger 	struct se_tmr_req *se_tmr;
1636e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req;
1637e48354ceSNicholas Bellinger 	struct iscsi_tm *hdr;
1638e48354ceSNicholas Bellinger 	int out_of_order_cmdsn = 0;
1639e48354ceSNicholas Bellinger 	int ret;
1640e48354ceSNicholas Bellinger 	u8 function;
1641e48354ceSNicholas Bellinger 
1642e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_tm *) buf;
1643e48354ceSNicholas Bellinger 	hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1644e48354ceSNicholas Bellinger 	function = hdr->flags;
1645e48354ceSNicholas Bellinger 
1646e48354ceSNicholas Bellinger 	pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1647e48354ceSNicholas Bellinger 		" 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1648e48354ceSNicholas Bellinger 		" 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1649e48354ceSNicholas Bellinger 		hdr->rtt, hdr->refcmdsn, conn->cid);
1650e48354ceSNicholas Bellinger 
1651e48354ceSNicholas Bellinger 	if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1652e48354ceSNicholas Bellinger 	    ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
165366c7db68SChristoph Hellwig 	     hdr->rtt != RESERVED_ITT)) {
1654e48354ceSNicholas Bellinger 		pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
165566c7db68SChristoph Hellwig 		hdr->rtt = RESERVED_ITT;
1656e48354ceSNicholas Bellinger 	}
1657e48354ceSNicholas Bellinger 
1658e48354ceSNicholas Bellinger 	if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1659e48354ceSNicholas Bellinger 			!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1660e48354ceSNicholas Bellinger 		pr_err("Task Management Request TASK_REASSIGN not"
1661e48354ceSNicholas Bellinger 			" issued as immediate command, bad iSCSI Initiator"
1662e48354ceSNicholas Bellinger 				"implementation\n");
1663e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1664e48354ceSNicholas Bellinger 					buf, conn);
1665e48354ceSNicholas Bellinger 	}
1666e48354ceSNicholas Bellinger 	if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
166750e5c87dSChristoph Hellwig 	    be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
166850e5c87dSChristoph Hellwig 		hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
1669e48354ceSNicholas Bellinger 
1670d28b1169SAndy Grover 	cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1671e48354ceSNicholas Bellinger 	if (!cmd)
1672e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1673e48354ceSNicholas Bellinger 					 1, buf, conn);
1674e48354ceSNicholas Bellinger 
1675d28b1169SAndy Grover 	cmd->data_direction = DMA_NONE;
1676d28b1169SAndy Grover 
1677d28b1169SAndy Grover 	cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1678d28b1169SAndy Grover 	if (!cmd->tmr_req) {
1679d28b1169SAndy Grover 		pr_err("Unable to allocate memory for"
1680d28b1169SAndy Grover 			" Task Management command!\n");
1681d28b1169SAndy Grover 		return iscsit_add_reject_from_cmd(
1682d28b1169SAndy Grover 			ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1683d28b1169SAndy Grover 			1, 1, buf, cmd);
1684d28b1169SAndy Grover 	}
1685d28b1169SAndy Grover 
1686d28b1169SAndy Grover 	/*
1687d28b1169SAndy Grover 	 * TASK_REASSIGN for ERL=2 / connection stays inside of
1688d28b1169SAndy Grover 	 * LIO-Target $FABRIC_MOD
1689d28b1169SAndy Grover 	 */
1690d28b1169SAndy Grover 	if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1691d28b1169SAndy Grover 
1692d28b1169SAndy Grover 		u8 tcm_function;
1693d28b1169SAndy Grover 		int ret;
1694d28b1169SAndy Grover 
1695d28b1169SAndy Grover 		transport_init_se_cmd(&cmd->se_cmd,
1696d28b1169SAndy Grover 				      &lio_target_fabric_configfs->tf_ops,
1697d28b1169SAndy Grover 				      conn->sess->se_sess, 0, DMA_NONE,
16989c58b7ddSRoland Dreier 				      MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
1699d28b1169SAndy Grover 
1700d28b1169SAndy Grover 		switch (function) {
1701d28b1169SAndy Grover 		case ISCSI_TM_FUNC_ABORT_TASK:
1702d28b1169SAndy Grover 			tcm_function = TMR_ABORT_TASK;
1703d28b1169SAndy Grover 			break;
1704d28b1169SAndy Grover 		case ISCSI_TM_FUNC_ABORT_TASK_SET:
1705d28b1169SAndy Grover 			tcm_function = TMR_ABORT_TASK_SET;
1706d28b1169SAndy Grover 			break;
1707d28b1169SAndy Grover 		case ISCSI_TM_FUNC_CLEAR_ACA:
1708d28b1169SAndy Grover 			tcm_function = TMR_CLEAR_ACA;
1709d28b1169SAndy Grover 			break;
1710d28b1169SAndy Grover 		case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1711d28b1169SAndy Grover 			tcm_function = TMR_CLEAR_TASK_SET;
1712d28b1169SAndy Grover 			break;
1713d28b1169SAndy Grover 		case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1714d28b1169SAndy Grover 			tcm_function = TMR_LUN_RESET;
1715d28b1169SAndy Grover 			break;
1716d28b1169SAndy Grover 		case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1717d28b1169SAndy Grover 			tcm_function = TMR_TARGET_WARM_RESET;
1718d28b1169SAndy Grover 			break;
1719d28b1169SAndy Grover 		case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1720d28b1169SAndy Grover 			tcm_function = TMR_TARGET_COLD_RESET;
1721d28b1169SAndy Grover 			break;
1722d28b1169SAndy Grover 		default:
1723d28b1169SAndy Grover 			pr_err("Unknown iSCSI TMR Function:"
1724d28b1169SAndy Grover 			       " 0x%02x\n", function);
1725d28b1169SAndy Grover 			return iscsit_add_reject_from_cmd(
1726d28b1169SAndy Grover 				ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1727d28b1169SAndy Grover 				1, 1, buf, cmd);
1728d28b1169SAndy Grover 		}
1729d28b1169SAndy Grover 
1730d28b1169SAndy Grover 		ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1731d28b1169SAndy Grover 					 tcm_function, GFP_KERNEL);
1732d28b1169SAndy Grover 		if (ret < 0)
1733d28b1169SAndy Grover 			return iscsit_add_reject_from_cmd(
1734d28b1169SAndy Grover 				ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1735d28b1169SAndy Grover 				1, 1, buf, cmd);
1736d28b1169SAndy Grover 
1737d28b1169SAndy Grover 		cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1738d28b1169SAndy Grover 	}
1739d28b1169SAndy Grover 
1740e48354ceSNicholas Bellinger 	cmd->iscsi_opcode	= ISCSI_OP_SCSI_TMFUNC;
1741e48354ceSNicholas Bellinger 	cmd->i_state		= ISTATE_SEND_TASKMGTRSP;
1742e48354ceSNicholas Bellinger 	cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1743e48354ceSNicholas Bellinger 	cmd->init_task_tag	= hdr->itt;
1744e48354ceSNicholas Bellinger 	cmd->targ_xfer_tag	= 0xFFFFFFFF;
174550e5c87dSChristoph Hellwig 	cmd->cmd_sn		= be32_to_cpu(hdr->cmdsn);
174650e5c87dSChristoph Hellwig 	cmd->exp_stat_sn	= be32_to_cpu(hdr->exp_statsn);
1747e48354ceSNicholas Bellinger 	se_tmr			= cmd->se_cmd.se_tmr_req;
1748e48354ceSNicholas Bellinger 	tmr_req			= cmd->tmr_req;
1749e48354ceSNicholas Bellinger 	/*
1750e48354ceSNicholas Bellinger 	 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1751e48354ceSNicholas Bellinger 	 */
1752e48354ceSNicholas Bellinger 	if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
17534f26998aSAndy Grover 		ret = transport_lookup_tmr_lun(&cmd->se_cmd,
17544f26998aSAndy Grover 					       scsilun_to_int(&hdr->lun));
1755e48354ceSNicholas Bellinger 		if (ret < 0) {
1756e48354ceSNicholas Bellinger 			se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1757e48354ceSNicholas Bellinger 			goto attach;
1758e48354ceSNicholas Bellinger 		}
1759e48354ceSNicholas Bellinger 	}
1760e48354ceSNicholas Bellinger 
1761e48354ceSNicholas Bellinger 	switch (function) {
1762e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_ABORT_TASK:
1763e48354ceSNicholas Bellinger 		se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
1764de103c93SChristoph Hellwig 		if (se_tmr->response)
1765e48354ceSNicholas Bellinger 			goto attach;
1766e48354ceSNicholas Bellinger 		break;
1767e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_ABORT_TASK_SET:
1768e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_CLEAR_ACA:
1769e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1770e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1771e48354ceSNicholas Bellinger 		break;
1772e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1773e48354ceSNicholas Bellinger 		if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
1774e48354ceSNicholas Bellinger 			se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1775e48354ceSNicholas Bellinger 			goto attach;
1776e48354ceSNicholas Bellinger 		}
1777e48354ceSNicholas Bellinger 		break;
1778e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1779e48354ceSNicholas Bellinger 		if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
1780e48354ceSNicholas Bellinger 			se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1781e48354ceSNicholas Bellinger 			goto attach;
1782e48354ceSNicholas Bellinger 		}
1783e48354ceSNicholas Bellinger 		break;
1784e48354ceSNicholas Bellinger 	case ISCSI_TM_FUNC_TASK_REASSIGN:
1785e48354ceSNicholas Bellinger 		se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1786e48354ceSNicholas Bellinger 		/*
1787e48354ceSNicholas Bellinger 		 * Perform sanity checks on the ExpDataSN only if the
1788e48354ceSNicholas Bellinger 		 * TASK_REASSIGN was successful.
1789e48354ceSNicholas Bellinger 		 */
1790de103c93SChristoph Hellwig 		if (se_tmr->response)
1791e48354ceSNicholas Bellinger 			break;
1792e48354ceSNicholas Bellinger 
1793e48354ceSNicholas Bellinger 		if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
1794e48354ceSNicholas Bellinger 			return iscsit_add_reject_from_cmd(
1795e48354ceSNicholas Bellinger 					ISCSI_REASON_BOOKMARK_INVALID, 1, 1,
1796e48354ceSNicholas Bellinger 					buf, cmd);
1797e48354ceSNicholas Bellinger 		break;
1798e48354ceSNicholas Bellinger 	default:
1799e48354ceSNicholas Bellinger 		pr_err("Unknown TMR function: 0x%02x, protocol"
1800e48354ceSNicholas Bellinger 			" error.\n", function);
1801e48354ceSNicholas Bellinger 		se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1802e48354ceSNicholas Bellinger 		goto attach;
1803e48354ceSNicholas Bellinger 	}
1804e48354ceSNicholas Bellinger 
1805e48354ceSNicholas Bellinger 	if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1806e48354ceSNicholas Bellinger 	    (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1807e48354ceSNicholas Bellinger 		se_tmr->call_transport = 1;
1808e48354ceSNicholas Bellinger attach:
1809e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
18102fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1811e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
1812e48354ceSNicholas Bellinger 
1813e48354ceSNicholas Bellinger 	if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1814e48354ceSNicholas Bellinger 		int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1815e48354ceSNicholas Bellinger 		if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1816e48354ceSNicholas Bellinger 			out_of_order_cmdsn = 1;
18175a4c8666SNicholas Bellinger 		else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1818e48354ceSNicholas Bellinger 			return 0;
18195a4c8666SNicholas Bellinger 		else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1820e48354ceSNicholas Bellinger 			return iscsit_add_reject_from_cmd(
1821e48354ceSNicholas Bellinger 					ISCSI_REASON_PROTOCOL_ERROR,
1822e48354ceSNicholas Bellinger 					1, 0, buf, cmd);
1823e48354ceSNicholas Bellinger 	}
182450e5c87dSChristoph Hellwig 	iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1825e48354ceSNicholas Bellinger 
18265a4c8666SNicholas Bellinger 	if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
1827e48354ceSNicholas Bellinger 		return 0;
1828e48354ceSNicholas Bellinger 	/*
1829e48354ceSNicholas Bellinger 	 * Found the referenced task, send to transport for processing.
1830e48354ceSNicholas Bellinger 	 */
1831e48354ceSNicholas Bellinger 	if (se_tmr->call_transport)
1832e48354ceSNicholas Bellinger 		return transport_generic_handle_tmr(&cmd->se_cmd);
1833e48354ceSNicholas Bellinger 
1834e48354ceSNicholas Bellinger 	/*
1835e48354ceSNicholas Bellinger 	 * Could not find the referenced LUN, task, or Task Management
1836e48354ceSNicholas Bellinger 	 * command not authorized or supported.  Change state and
1837e48354ceSNicholas Bellinger 	 * let the tx_thread send the response.
1838e48354ceSNicholas Bellinger 	 *
1839e48354ceSNicholas Bellinger 	 * For connection recovery, this is also the default action for
1840e48354ceSNicholas Bellinger 	 * TMR TASK_REASSIGN.
1841e48354ceSNicholas Bellinger 	 */
1842e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1843e48354ceSNicholas Bellinger 	return 0;
1844e48354ceSNicholas Bellinger }
1845e48354ceSNicholas Bellinger 
1846e48354ceSNicholas Bellinger /* #warning FIXME: Support Text Command parameters besides SendTargets */
1847e48354ceSNicholas Bellinger static int iscsit_handle_text_cmd(
1848e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
1849e48354ceSNicholas Bellinger 	unsigned char *buf)
1850e48354ceSNicholas Bellinger {
1851e48354ceSNicholas Bellinger 	char *text_ptr, *text_in;
1852e48354ceSNicholas Bellinger 	int cmdsn_ret, niov = 0, rx_got, rx_size;
1853e48354ceSNicholas Bellinger 	u32 checksum = 0, data_crc = 0, payload_length;
185476f1928eSNicholas Bellinger 	u32 padding = 0, pad_bytes = 0, text_length = 0;
1855e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd;
1856e48354ceSNicholas Bellinger 	struct kvec iov[3];
1857e48354ceSNicholas Bellinger 	struct iscsi_text *hdr;
1858e48354ceSNicholas Bellinger 
1859e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_text *) buf;
1860e48354ceSNicholas Bellinger 	payload_length		= ntoh24(hdr->dlength);
1861e48354ceSNicholas Bellinger 
186221f5aa7eSNicholas Bellinger 	if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1863e48354ceSNicholas Bellinger 		pr_err("Unable to accept text parameter length: %u"
186421f5aa7eSNicholas Bellinger 			"greater than MaxXmitDataSegmentLength %u.\n",
186521f5aa7eSNicholas Bellinger 		       payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
1866e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1867e48354ceSNicholas Bellinger 					buf, conn);
1868e48354ceSNicholas Bellinger 	}
1869e48354ceSNicholas Bellinger 
1870e48354ceSNicholas Bellinger 	pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1871e48354ceSNicholas Bellinger 		" ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1872e48354ceSNicholas Bellinger 		hdr->exp_statsn, payload_length);
1873e48354ceSNicholas Bellinger 
1874e48354ceSNicholas Bellinger 	rx_size = text_length = payload_length;
1875e48354ceSNicholas Bellinger 	if (text_length) {
1876e48354ceSNicholas Bellinger 		text_in = kzalloc(text_length, GFP_KERNEL);
1877e48354ceSNicholas Bellinger 		if (!text_in) {
1878e48354ceSNicholas Bellinger 			pr_err("Unable to allocate memory for"
1879e48354ceSNicholas Bellinger 				" incoming text parameters\n");
1880e48354ceSNicholas Bellinger 			return -1;
1881e48354ceSNicholas Bellinger 		}
1882e48354ceSNicholas Bellinger 
1883e48354ceSNicholas Bellinger 		memset(iov, 0, 3 * sizeof(struct kvec));
1884e48354ceSNicholas Bellinger 		iov[niov].iov_base	= text_in;
1885e48354ceSNicholas Bellinger 		iov[niov++].iov_len	= text_length;
1886e48354ceSNicholas Bellinger 
1887e48354ceSNicholas Bellinger 		padding = ((-payload_length) & 3);
1888e48354ceSNicholas Bellinger 		if (padding != 0) {
188976f1928eSNicholas Bellinger 			iov[niov].iov_base = &pad_bytes;
1890e48354ceSNicholas Bellinger 			iov[niov++].iov_len  = padding;
1891e48354ceSNicholas Bellinger 			rx_size += padding;
1892e48354ceSNicholas Bellinger 			pr_debug("Receiving %u additional bytes"
1893e48354ceSNicholas Bellinger 					" for padding.\n", padding);
1894e48354ceSNicholas Bellinger 		}
1895e48354ceSNicholas Bellinger 		if (conn->conn_ops->DataDigest) {
1896e48354ceSNicholas Bellinger 			iov[niov].iov_base	= &checksum;
1897e48354ceSNicholas Bellinger 			iov[niov++].iov_len	= ISCSI_CRC_LEN;
1898e48354ceSNicholas Bellinger 			rx_size += ISCSI_CRC_LEN;
1899e48354ceSNicholas Bellinger 		}
1900e48354ceSNicholas Bellinger 
1901e48354ceSNicholas Bellinger 		rx_got = rx_data(conn, &iov[0], niov, rx_size);
1902e48354ceSNicholas Bellinger 		if (rx_got != rx_size) {
1903e48354ceSNicholas Bellinger 			kfree(text_in);
1904e48354ceSNicholas Bellinger 			return -1;
1905e48354ceSNicholas Bellinger 		}
1906e48354ceSNicholas Bellinger 
1907e48354ceSNicholas Bellinger 		if (conn->conn_ops->DataDigest) {
1908e48354ceSNicholas Bellinger 			iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1909e48354ceSNicholas Bellinger 					text_in, text_length,
191076f1928eSNicholas Bellinger 					padding, (u8 *)&pad_bytes,
1911e48354ceSNicholas Bellinger 					(u8 *)&data_crc);
1912e48354ceSNicholas Bellinger 
1913e48354ceSNicholas Bellinger 			if (checksum != data_crc) {
1914e48354ceSNicholas Bellinger 				pr_err("Text data CRC32C DataDigest"
1915e48354ceSNicholas Bellinger 					" 0x%08x does not match computed"
1916e48354ceSNicholas Bellinger 					" 0x%08x\n", checksum, data_crc);
1917e48354ceSNicholas Bellinger 				if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1918e48354ceSNicholas Bellinger 					pr_err("Unable to recover from"
1919e48354ceSNicholas Bellinger 					" Text Data digest failure while in"
1920e48354ceSNicholas Bellinger 						" ERL=0.\n");
1921e48354ceSNicholas Bellinger 					kfree(text_in);
1922e48354ceSNicholas Bellinger 					return -1;
1923e48354ceSNicholas Bellinger 				} else {
1924e48354ceSNicholas Bellinger 					/*
1925e48354ceSNicholas Bellinger 					 * Silently drop this PDU and let the
1926e48354ceSNicholas Bellinger 					 * initiator plug the CmdSN gap.
1927e48354ceSNicholas Bellinger 					 */
1928e48354ceSNicholas Bellinger 					pr_debug("Dropping Text"
1929e48354ceSNicholas Bellinger 					" Command CmdSN: 0x%08x due to"
1930e48354ceSNicholas Bellinger 					" DataCRC error.\n", hdr->cmdsn);
1931e48354ceSNicholas Bellinger 					kfree(text_in);
1932e48354ceSNicholas Bellinger 					return 0;
1933e48354ceSNicholas Bellinger 				}
1934e48354ceSNicholas Bellinger 			} else {
1935e48354ceSNicholas Bellinger 				pr_debug("Got CRC32C DataDigest"
1936e48354ceSNicholas Bellinger 					" 0x%08x for %u bytes of text data.\n",
1937e48354ceSNicholas Bellinger 						checksum, text_length);
1938e48354ceSNicholas Bellinger 			}
1939e48354ceSNicholas Bellinger 		}
1940e48354ceSNicholas Bellinger 		text_in[text_length - 1] = '\0';
1941e48354ceSNicholas Bellinger 		pr_debug("Successfully read %d bytes of text"
1942e48354ceSNicholas Bellinger 				" data.\n", text_length);
1943e48354ceSNicholas Bellinger 
1944e48354ceSNicholas Bellinger 		if (strncmp("SendTargets", text_in, 11) != 0) {
1945e48354ceSNicholas Bellinger 			pr_err("Received Text Data that is not"
1946e48354ceSNicholas Bellinger 				" SendTargets, cannot continue.\n");
1947e48354ceSNicholas Bellinger 			kfree(text_in);
1948e48354ceSNicholas Bellinger 			return -1;
1949e48354ceSNicholas Bellinger 		}
1950e48354ceSNicholas Bellinger 		text_ptr = strchr(text_in, '=');
1951e48354ceSNicholas Bellinger 		if (!text_ptr) {
1952e48354ceSNicholas Bellinger 			pr_err("No \"=\" separator found in Text Data,"
1953e48354ceSNicholas Bellinger 				"  cannot continue.\n");
1954e48354ceSNicholas Bellinger 			kfree(text_in);
1955e48354ceSNicholas Bellinger 			return -1;
1956e48354ceSNicholas Bellinger 		}
1957e48354ceSNicholas Bellinger 		if (strncmp("=All", text_ptr, 4) != 0) {
1958e48354ceSNicholas Bellinger 			pr_err("Unable to locate All value for"
1959e48354ceSNicholas Bellinger 				" SendTargets key,  cannot continue.\n");
1960e48354ceSNicholas Bellinger 			kfree(text_in);
1961e48354ceSNicholas Bellinger 			return -1;
1962e48354ceSNicholas Bellinger 		}
1963e48354ceSNicholas Bellinger /*#warning Support SendTargets=(iSCSI Target Name/Nothing) values. */
1964e48354ceSNicholas Bellinger 		kfree(text_in);
1965e48354ceSNicholas Bellinger 	}
1966e48354ceSNicholas Bellinger 
1967e48354ceSNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1968e48354ceSNicholas Bellinger 	if (!cmd)
1969e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1970e48354ceSNicholas Bellinger 					1, buf, conn);
1971e48354ceSNicholas Bellinger 
1972e48354ceSNicholas Bellinger 	cmd->iscsi_opcode	= ISCSI_OP_TEXT;
1973e48354ceSNicholas Bellinger 	cmd->i_state		= ISTATE_SEND_TEXTRSP;
1974e48354ceSNicholas Bellinger 	cmd->immediate_cmd	= ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1975e48354ceSNicholas Bellinger 	conn->sess->init_task_tag = cmd->init_task_tag	= hdr->itt;
1976e48354ceSNicholas Bellinger 	cmd->targ_xfer_tag	= 0xFFFFFFFF;
197750e5c87dSChristoph Hellwig 	cmd->cmd_sn		= be32_to_cpu(hdr->cmdsn);
197850e5c87dSChristoph Hellwig 	cmd->exp_stat_sn	= be32_to_cpu(hdr->exp_statsn);
1979e48354ceSNicholas Bellinger 	cmd->data_direction	= DMA_NONE;
1980e48354ceSNicholas Bellinger 
1981e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
19822fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1983e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
1984e48354ceSNicholas Bellinger 
198550e5c87dSChristoph Hellwig 	iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1986e48354ceSNicholas Bellinger 
1987e48354ceSNicholas Bellinger 	if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1988e48354ceSNicholas Bellinger 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1989e48354ceSNicholas Bellinger 		if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1990e48354ceSNicholas Bellinger 			return iscsit_add_reject_from_cmd(
1991e48354ceSNicholas Bellinger 					ISCSI_REASON_PROTOCOL_ERROR,
1992e48354ceSNicholas Bellinger 					1, 0, buf, cmd);
1993e48354ceSNicholas Bellinger 
1994e48354ceSNicholas Bellinger 		return 0;
1995e48354ceSNicholas Bellinger 	}
1996e48354ceSNicholas Bellinger 
1997e48354ceSNicholas Bellinger 	return iscsit_execute_cmd(cmd, 0);
1998e48354ceSNicholas Bellinger }
1999e48354ceSNicholas Bellinger 
2000e48354ceSNicholas Bellinger int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2001e48354ceSNicholas Bellinger {
2002e48354ceSNicholas Bellinger 	struct iscsi_conn *conn_p;
2003e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
2004e48354ceSNicholas Bellinger 
2005e48354ceSNicholas Bellinger 	pr_debug("Received logout request CLOSESESSION on CID: %hu"
2006e48354ceSNicholas Bellinger 		" for SID: %u.\n", conn->cid, conn->sess->sid);
2007e48354ceSNicholas Bellinger 
2008e48354ceSNicholas Bellinger 	atomic_set(&sess->session_logout, 1);
2009e48354ceSNicholas Bellinger 	atomic_set(&conn->conn_logout_remove, 1);
2010e48354ceSNicholas Bellinger 	conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2011e48354ceSNicholas Bellinger 
2012e48354ceSNicholas Bellinger 	iscsit_inc_conn_usage_count(conn);
2013e48354ceSNicholas Bellinger 	iscsit_inc_session_usage_count(sess);
2014e48354ceSNicholas Bellinger 
2015e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
2016e48354ceSNicholas Bellinger 	list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2017e48354ceSNicholas Bellinger 		if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2018e48354ceSNicholas Bellinger 			continue;
2019e48354ceSNicholas Bellinger 
2020e48354ceSNicholas Bellinger 		pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2021e48354ceSNicholas Bellinger 		conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2022e48354ceSNicholas Bellinger 	}
2023e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
2024e48354ceSNicholas Bellinger 
2025e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2026e48354ceSNicholas Bellinger 
2027e48354ceSNicholas Bellinger 	return 0;
2028e48354ceSNicholas Bellinger }
2029e48354ceSNicholas Bellinger 
2030e48354ceSNicholas Bellinger int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2031e48354ceSNicholas Bellinger {
2032e48354ceSNicholas Bellinger 	struct iscsi_conn *l_conn;
2033e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
2034e48354ceSNicholas Bellinger 
2035e48354ceSNicholas Bellinger 	pr_debug("Received logout request CLOSECONNECTION for CID:"
2036e48354ceSNicholas Bellinger 		" %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2037e48354ceSNicholas Bellinger 
2038e48354ceSNicholas Bellinger 	/*
2039e48354ceSNicholas Bellinger 	 * A Logout Request with a CLOSECONNECTION reason code for a CID
2040e48354ceSNicholas Bellinger 	 * can arrive on a connection with a differing CID.
2041e48354ceSNicholas Bellinger 	 */
2042e48354ceSNicholas Bellinger 	if (conn->cid == cmd->logout_cid) {
2043e48354ceSNicholas Bellinger 		spin_lock_bh(&conn->state_lock);
2044e48354ceSNicholas Bellinger 		pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2045e48354ceSNicholas Bellinger 		conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2046e48354ceSNicholas Bellinger 
2047e48354ceSNicholas Bellinger 		atomic_set(&conn->conn_logout_remove, 1);
2048e48354ceSNicholas Bellinger 		conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2049e48354ceSNicholas Bellinger 		iscsit_inc_conn_usage_count(conn);
2050e48354ceSNicholas Bellinger 
2051e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->state_lock);
2052e48354ceSNicholas Bellinger 	} else {
2053e48354ceSNicholas Bellinger 		/*
2054e48354ceSNicholas Bellinger 		 * Handle all different cid CLOSECONNECTION requests in
2055e48354ceSNicholas Bellinger 		 * iscsit_logout_post_handler_diffcid() as to give enough
2056e48354ceSNicholas Bellinger 		 * time for any non immediate command's CmdSN to be
2057e48354ceSNicholas Bellinger 		 * acknowledged on the connection in question.
2058e48354ceSNicholas Bellinger 		 *
2059e48354ceSNicholas Bellinger 		 * Here we simply make sure the CID is still around.
2060e48354ceSNicholas Bellinger 		 */
2061e48354ceSNicholas Bellinger 		l_conn = iscsit_get_conn_from_cid(sess,
2062e48354ceSNicholas Bellinger 				cmd->logout_cid);
2063e48354ceSNicholas Bellinger 		if (!l_conn) {
2064e48354ceSNicholas Bellinger 			cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2065e48354ceSNicholas Bellinger 			iscsit_add_cmd_to_response_queue(cmd, conn,
2066e48354ceSNicholas Bellinger 					cmd->i_state);
2067e48354ceSNicholas Bellinger 			return 0;
2068e48354ceSNicholas Bellinger 		}
2069e48354ceSNicholas Bellinger 
2070e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(l_conn);
2071e48354ceSNicholas Bellinger 	}
2072e48354ceSNicholas Bellinger 
2073e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2074e48354ceSNicholas Bellinger 
2075e48354ceSNicholas Bellinger 	return 0;
2076e48354ceSNicholas Bellinger }
2077e48354ceSNicholas Bellinger 
2078e48354ceSNicholas Bellinger int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2079e48354ceSNicholas Bellinger {
2080e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
2081e48354ceSNicholas Bellinger 
2082e48354ceSNicholas Bellinger 	pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2083e48354ceSNicholas Bellinger 		" CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2084e48354ceSNicholas Bellinger 
2085e48354ceSNicholas Bellinger 	if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2086e48354ceSNicholas Bellinger 		pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2087e48354ceSNicholas Bellinger 			" while ERL!=2.\n");
2088e48354ceSNicholas Bellinger 		cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2089e48354ceSNicholas Bellinger 		iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2090e48354ceSNicholas Bellinger 		return 0;
2091e48354ceSNicholas Bellinger 	}
2092e48354ceSNicholas Bellinger 
2093e48354ceSNicholas Bellinger 	if (conn->cid == cmd->logout_cid) {
2094e48354ceSNicholas Bellinger 		pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2095e48354ceSNicholas Bellinger 			" with CID: %hu on CID: %hu, implementation error.\n",
2096e48354ceSNicholas Bellinger 				cmd->logout_cid, conn->cid);
2097e48354ceSNicholas Bellinger 		cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2098e48354ceSNicholas Bellinger 		iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2099e48354ceSNicholas Bellinger 		return 0;
2100e48354ceSNicholas Bellinger 	}
2101e48354ceSNicholas Bellinger 
2102e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2103e48354ceSNicholas Bellinger 
2104e48354ceSNicholas Bellinger 	return 0;
2105e48354ceSNicholas Bellinger }
2106e48354ceSNicholas Bellinger 
2107e48354ceSNicholas Bellinger static int iscsit_handle_logout_cmd(
2108e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
2109e48354ceSNicholas Bellinger 	unsigned char *buf)
2110e48354ceSNicholas Bellinger {
2111e48354ceSNicholas Bellinger 	int cmdsn_ret, logout_remove = 0;
2112e48354ceSNicholas Bellinger 	u8 reason_code = 0;
2113e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd;
2114e48354ceSNicholas Bellinger 	struct iscsi_logout *hdr;
2115e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2116e48354ceSNicholas Bellinger 
2117e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_logout *) buf;
2118e48354ceSNicholas Bellinger 	reason_code		= (hdr->flags & 0x7f);
2119e48354ceSNicholas Bellinger 
2120e48354ceSNicholas Bellinger 	if (tiqn) {
2121e48354ceSNicholas Bellinger 		spin_lock(&tiqn->logout_stats.lock);
2122e48354ceSNicholas Bellinger 		if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2123e48354ceSNicholas Bellinger 			tiqn->logout_stats.normal_logouts++;
2124e48354ceSNicholas Bellinger 		else
2125e48354ceSNicholas Bellinger 			tiqn->logout_stats.abnormal_logouts++;
2126e48354ceSNicholas Bellinger 		spin_unlock(&tiqn->logout_stats.lock);
2127e48354ceSNicholas Bellinger 	}
2128e48354ceSNicholas Bellinger 
2129e48354ceSNicholas Bellinger 	pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2130e48354ceSNicholas Bellinger 		" ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2131e48354ceSNicholas Bellinger 		hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2132e48354ceSNicholas Bellinger 		hdr->cid, conn->cid);
2133e48354ceSNicholas Bellinger 
2134e48354ceSNicholas Bellinger 	if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2135e48354ceSNicholas Bellinger 		pr_err("Received logout request on connection that"
2136e48354ceSNicholas Bellinger 			" is not in logged in state, ignoring request.\n");
2137e48354ceSNicholas Bellinger 		return 0;
2138e48354ceSNicholas Bellinger 	}
2139e48354ceSNicholas Bellinger 
2140e48354ceSNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
2141e48354ceSNicholas Bellinger 	if (!cmd)
2142e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
2143e48354ceSNicholas Bellinger 					buf, conn);
2144e48354ceSNicholas Bellinger 
2145e48354ceSNicholas Bellinger 	cmd->iscsi_opcode       = ISCSI_OP_LOGOUT;
2146e48354ceSNicholas Bellinger 	cmd->i_state            = ISTATE_SEND_LOGOUTRSP;
2147e48354ceSNicholas Bellinger 	cmd->immediate_cmd      = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2148e48354ceSNicholas Bellinger 	conn->sess->init_task_tag = cmd->init_task_tag  = hdr->itt;
2149e48354ceSNicholas Bellinger 	cmd->targ_xfer_tag      = 0xFFFFFFFF;
215050e5c87dSChristoph Hellwig 	cmd->cmd_sn             = be32_to_cpu(hdr->cmdsn);
215150e5c87dSChristoph Hellwig 	cmd->exp_stat_sn        = be32_to_cpu(hdr->exp_statsn);
215250e5c87dSChristoph Hellwig 	cmd->logout_cid         = be16_to_cpu(hdr->cid);
2153e48354ceSNicholas Bellinger 	cmd->logout_reason      = reason_code;
2154e48354ceSNicholas Bellinger 	cmd->data_direction     = DMA_NONE;
2155e48354ceSNicholas Bellinger 
2156e48354ceSNicholas Bellinger 	/*
2157e48354ceSNicholas Bellinger 	 * We need to sleep in these cases (by returning 1) until the Logout
2158e48354ceSNicholas Bellinger 	 * Response gets sent in the tx thread.
2159e48354ceSNicholas Bellinger 	 */
2160e48354ceSNicholas Bellinger 	if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2161e48354ceSNicholas Bellinger 	   ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
216250e5c87dSChristoph Hellwig 	    be16_to_cpu(hdr->cid) == conn->cid))
2163e48354ceSNicholas Bellinger 		logout_remove = 1;
2164e48354ceSNicholas Bellinger 
2165e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
21662fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2167e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
2168e48354ceSNicholas Bellinger 
2169e48354ceSNicholas Bellinger 	if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
217050e5c87dSChristoph Hellwig 		iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2171e48354ceSNicholas Bellinger 
2172e48354ceSNicholas Bellinger 	/*
2173e48354ceSNicholas Bellinger 	 * Immediate commands are executed, well, immediately.
2174e48354ceSNicholas Bellinger 	 * Non-Immediate Logout Commands are executed in CmdSN order.
2175e48354ceSNicholas Bellinger 	 */
2176c6037cc5SAndy Grover 	if (cmd->immediate_cmd) {
2177e48354ceSNicholas Bellinger 		int ret = iscsit_execute_cmd(cmd, 0);
2178e48354ceSNicholas Bellinger 
2179e48354ceSNicholas Bellinger 		if (ret < 0)
2180e48354ceSNicholas Bellinger 			return ret;
2181e48354ceSNicholas Bellinger 	} else {
2182e48354ceSNicholas Bellinger 		cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
2183e48354ceSNicholas Bellinger 		if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2184e48354ceSNicholas Bellinger 			logout_remove = 0;
2185e48354ceSNicholas Bellinger 		} else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
2186e48354ceSNicholas Bellinger 			return iscsit_add_reject_from_cmd(
2187e48354ceSNicholas Bellinger 				ISCSI_REASON_PROTOCOL_ERROR,
2188e48354ceSNicholas Bellinger 				1, 0, buf, cmd);
2189e48354ceSNicholas Bellinger 		}
2190e48354ceSNicholas Bellinger 	}
2191e48354ceSNicholas Bellinger 
2192e48354ceSNicholas Bellinger 	return logout_remove;
2193e48354ceSNicholas Bellinger }
2194e48354ceSNicholas Bellinger 
2195e48354ceSNicholas Bellinger static int iscsit_handle_snack(
2196e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
2197e48354ceSNicholas Bellinger 	unsigned char *buf)
2198e48354ceSNicholas Bellinger {
2199e48354ceSNicholas Bellinger 	struct iscsi_snack *hdr;
2200e48354ceSNicholas Bellinger 
2201e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_snack *) buf;
2202e48354ceSNicholas Bellinger 	hdr->flags		&= ~ISCSI_FLAG_CMD_FINAL;
2203e48354ceSNicholas Bellinger 
2204e48354ceSNicholas Bellinger 	pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2205e48354ceSNicholas Bellinger 		" 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2206e48354ceSNicholas Bellinger 		" CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2207e48354ceSNicholas Bellinger 			hdr->begrun, hdr->runlength, conn->cid);
2208e48354ceSNicholas Bellinger 
2209e48354ceSNicholas Bellinger 	if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2210e48354ceSNicholas Bellinger 		pr_err("Initiator sent SNACK request while in"
2211e48354ceSNicholas Bellinger 			" ErrorRecoveryLevel=0.\n");
2212e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2213e48354ceSNicholas Bellinger 					buf, conn);
2214e48354ceSNicholas Bellinger 	}
2215e48354ceSNicholas Bellinger 	/*
2216e48354ceSNicholas Bellinger 	 * SNACK_DATA and SNACK_R2T are both 0,  so check which function to
2217e48354ceSNicholas Bellinger 	 * call from inside iscsi_send_recovery_datain_or_r2t().
2218e48354ceSNicholas Bellinger 	 */
2219e48354ceSNicholas Bellinger 	switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2220e48354ceSNicholas Bellinger 	case 0:
2221e48354ceSNicholas Bellinger 		return iscsit_handle_recovery_datain_or_r2t(conn, buf,
222250e5c87dSChristoph Hellwig 			hdr->itt,
222350e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->ttt),
222450e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->begrun),
222550e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->runlength));
2226e48354ceSNicholas Bellinger 	case ISCSI_FLAG_SNACK_TYPE_STATUS:
222750e5c87dSChristoph Hellwig 		return iscsit_handle_status_snack(conn, hdr->itt,
222850e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->ttt),
222950e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
2230e48354ceSNicholas Bellinger 	case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
223150e5c87dSChristoph Hellwig 		return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
223250e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->begrun),
223350e5c87dSChristoph Hellwig 			be32_to_cpu(hdr->runlength));
2234e48354ceSNicholas Bellinger 	case ISCSI_FLAG_SNACK_TYPE_RDATA:
2235e48354ceSNicholas Bellinger 		/* FIXME: Support R-Data SNACK */
2236e48354ceSNicholas Bellinger 		pr_err("R-Data SNACK Not Supported.\n");
2237e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2238e48354ceSNicholas Bellinger 					buf, conn);
2239e48354ceSNicholas Bellinger 	default:
2240e48354ceSNicholas Bellinger 		pr_err("Unknown SNACK type 0x%02x, protocol"
2241e48354ceSNicholas Bellinger 			" error.\n", hdr->flags & 0x0f);
2242e48354ceSNicholas Bellinger 		return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2243e48354ceSNicholas Bellinger 					buf, conn);
2244e48354ceSNicholas Bellinger 	}
2245e48354ceSNicholas Bellinger 
2246e48354ceSNicholas Bellinger 	return 0;
2247e48354ceSNicholas Bellinger }
2248e48354ceSNicholas Bellinger 
2249e48354ceSNicholas Bellinger static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2250e48354ceSNicholas Bellinger {
2251e48354ceSNicholas Bellinger 	if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2252e48354ceSNicholas Bellinger 	    (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2253e48354ceSNicholas Bellinger 		wait_for_completion_interruptible_timeout(
2254e48354ceSNicholas Bellinger 					&conn->rx_half_close_comp,
2255e48354ceSNicholas Bellinger 					ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2256e48354ceSNicholas Bellinger 	}
2257e48354ceSNicholas Bellinger }
2258e48354ceSNicholas Bellinger 
2259e48354ceSNicholas Bellinger static int iscsit_handle_immediate_data(
2260e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
2261e48354ceSNicholas Bellinger 	unsigned char *buf,
2262e48354ceSNicholas Bellinger 	u32 length)
2263e48354ceSNicholas Bellinger {
2264e48354ceSNicholas Bellinger 	int iov_ret, rx_got = 0, rx_size = 0;
2265e48354ceSNicholas Bellinger 	u32 checksum, iov_count = 0, padding = 0;
2266e48354ceSNicholas Bellinger 	struct iscsi_conn *conn = cmd->conn;
2267e48354ceSNicholas Bellinger 	struct kvec *iov;
2268e48354ceSNicholas Bellinger 
2269e48354ceSNicholas Bellinger 	iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2270e48354ceSNicholas Bellinger 	if (iov_ret < 0)
2271e48354ceSNicholas Bellinger 		return IMMEDIATE_DATA_CANNOT_RECOVER;
2272e48354ceSNicholas Bellinger 
2273e48354ceSNicholas Bellinger 	rx_size = length;
2274e48354ceSNicholas Bellinger 	iov_count = iov_ret;
2275e48354ceSNicholas Bellinger 	iov = &cmd->iov_data[0];
2276e48354ceSNicholas Bellinger 
2277e48354ceSNicholas Bellinger 	padding = ((-length) & 3);
2278e48354ceSNicholas Bellinger 	if (padding != 0) {
2279e48354ceSNicholas Bellinger 		iov[iov_count].iov_base	= cmd->pad_bytes;
2280e48354ceSNicholas Bellinger 		iov[iov_count++].iov_len = padding;
2281e48354ceSNicholas Bellinger 		rx_size += padding;
2282e48354ceSNicholas Bellinger 	}
2283e48354ceSNicholas Bellinger 
2284e48354ceSNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
2285e48354ceSNicholas Bellinger 		iov[iov_count].iov_base		= &checksum;
2286e48354ceSNicholas Bellinger 		iov[iov_count++].iov_len	= ISCSI_CRC_LEN;
2287e48354ceSNicholas Bellinger 		rx_size += ISCSI_CRC_LEN;
2288e48354ceSNicholas Bellinger 	}
2289e48354ceSNicholas Bellinger 
2290e48354ceSNicholas Bellinger 	rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2291e48354ceSNicholas Bellinger 
2292e48354ceSNicholas Bellinger 	iscsit_unmap_iovec(cmd);
2293e48354ceSNicholas Bellinger 
2294e48354ceSNicholas Bellinger 	if (rx_got != rx_size) {
2295e48354ceSNicholas Bellinger 		iscsit_rx_thread_wait_for_tcp(conn);
2296e48354ceSNicholas Bellinger 		return IMMEDIATE_DATA_CANNOT_RECOVER;
2297e48354ceSNicholas Bellinger 	}
2298e48354ceSNicholas Bellinger 
2299e48354ceSNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
2300e48354ceSNicholas Bellinger 		u32 data_crc;
2301e48354ceSNicholas Bellinger 
2302e48354ceSNicholas Bellinger 		data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2303e48354ceSNicholas Bellinger 						    cmd->write_data_done, length, padding,
2304e48354ceSNicholas Bellinger 						    cmd->pad_bytes);
2305e48354ceSNicholas Bellinger 
2306e48354ceSNicholas Bellinger 		if (checksum != data_crc) {
2307e48354ceSNicholas Bellinger 			pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2308e48354ceSNicholas Bellinger 				" does not match computed 0x%08x\n", checksum,
2309e48354ceSNicholas Bellinger 				data_crc);
2310e48354ceSNicholas Bellinger 
2311e48354ceSNicholas Bellinger 			if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2312e48354ceSNicholas Bellinger 				pr_err("Unable to recover from"
2313e48354ceSNicholas Bellinger 					" Immediate Data digest failure while"
2314e48354ceSNicholas Bellinger 					" in ERL=0.\n");
2315e48354ceSNicholas Bellinger 				iscsit_add_reject_from_cmd(
2316e48354ceSNicholas Bellinger 						ISCSI_REASON_DATA_DIGEST_ERROR,
2317e48354ceSNicholas Bellinger 						1, 0, buf, cmd);
2318e48354ceSNicholas Bellinger 				return IMMEDIATE_DATA_CANNOT_RECOVER;
2319e48354ceSNicholas Bellinger 			} else {
2320e48354ceSNicholas Bellinger 				iscsit_add_reject_from_cmd(
2321e48354ceSNicholas Bellinger 						ISCSI_REASON_DATA_DIGEST_ERROR,
2322e48354ceSNicholas Bellinger 						0, 0, buf, cmd);
2323e48354ceSNicholas Bellinger 				return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2324e48354ceSNicholas Bellinger 			}
2325e48354ceSNicholas Bellinger 		} else {
2326e48354ceSNicholas Bellinger 			pr_debug("Got CRC32C DataDigest 0x%08x for"
2327e48354ceSNicholas Bellinger 				" %u bytes of Immediate Data\n", checksum,
2328e48354ceSNicholas Bellinger 				length);
2329e48354ceSNicholas Bellinger 		}
2330e48354ceSNicholas Bellinger 	}
2331e48354ceSNicholas Bellinger 
2332e48354ceSNicholas Bellinger 	cmd->write_data_done += length;
2333e48354ceSNicholas Bellinger 
2334ebf1d95cSAndy Grover 	if (cmd->write_data_done == cmd->se_cmd.data_length) {
2335e48354ceSNicholas Bellinger 		spin_lock_bh(&cmd->istate_lock);
2336e48354ceSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2337e48354ceSNicholas Bellinger 		cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2338e48354ceSNicholas Bellinger 		spin_unlock_bh(&cmd->istate_lock);
2339e48354ceSNicholas Bellinger 	}
2340e48354ceSNicholas Bellinger 
2341e48354ceSNicholas Bellinger 	return IMMEDIATE_DATA_NORMAL_OPERATION;
2342e48354ceSNicholas Bellinger }
2343e48354ceSNicholas Bellinger 
2344e48354ceSNicholas Bellinger /*
2345e48354ceSNicholas Bellinger  *	Called with sess->conn_lock held.
2346e48354ceSNicholas Bellinger  */
2347e48354ceSNicholas Bellinger /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2348e48354ceSNicholas Bellinger 	with active network interface */
2349e48354ceSNicholas Bellinger static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2350e48354ceSNicholas Bellinger {
2351e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd;
2352e48354ceSNicholas Bellinger 	struct iscsi_conn *conn_p;
2353e48354ceSNicholas Bellinger 
2354e48354ceSNicholas Bellinger 	/*
2355e48354ceSNicholas Bellinger 	 * Only send a Asynchronous Message on connections whos network
2356e48354ceSNicholas Bellinger 	 * interface is still functional.
2357e48354ceSNicholas Bellinger 	 */
2358e48354ceSNicholas Bellinger 	list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2359e48354ceSNicholas Bellinger 		if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2360e48354ceSNicholas Bellinger 			iscsit_inc_conn_usage_count(conn_p);
2361e48354ceSNicholas Bellinger 			break;
2362e48354ceSNicholas Bellinger 		}
2363e48354ceSNicholas Bellinger 	}
2364e48354ceSNicholas Bellinger 
2365e48354ceSNicholas Bellinger 	if (!conn_p)
2366e48354ceSNicholas Bellinger 		return;
2367e48354ceSNicholas Bellinger 
23683c989d76SWei Yongjun 	cmd = iscsit_allocate_cmd(conn_p, GFP_ATOMIC);
2369e48354ceSNicholas Bellinger 	if (!cmd) {
2370e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn_p);
2371e48354ceSNicholas Bellinger 		return;
2372e48354ceSNicholas Bellinger 	}
2373e48354ceSNicholas Bellinger 
2374e48354ceSNicholas Bellinger 	cmd->logout_cid = conn->cid;
2375e48354ceSNicholas Bellinger 	cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2376e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_ASYNCMSG;
2377e48354ceSNicholas Bellinger 
2378e48354ceSNicholas Bellinger 	spin_lock_bh(&conn_p->cmd_lock);
23792fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
2380e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn_p->cmd_lock);
2381e48354ceSNicholas Bellinger 
2382e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2383e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn_p);
2384e48354ceSNicholas Bellinger }
2385e48354ceSNicholas Bellinger 
2386e48354ceSNicholas Bellinger static int iscsit_send_conn_drop_async_message(
2387e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
2388e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
2389e48354ceSNicholas Bellinger {
2390e48354ceSNicholas Bellinger 	struct iscsi_async *hdr;
2391e48354ceSNicholas Bellinger 
2392e48354ceSNicholas Bellinger 	cmd->tx_size = ISCSI_HDR_LEN;
2393e48354ceSNicholas Bellinger 	cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2394e48354ceSNicholas Bellinger 
2395e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_async *) cmd->pdu;
2396e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_ASYNC_EVENT;
2397e48354ceSNicholas Bellinger 	hdr->flags		= ISCSI_FLAG_CMD_FINAL;
239866c7db68SChristoph Hellwig 	cmd->init_task_tag	= RESERVED_ITT;
2399e48354ceSNicholas Bellinger 	cmd->targ_xfer_tag	= 0xFFFFFFFF;
2400e48354ceSNicholas Bellinger 	put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2401e48354ceSNicholas Bellinger 	cmd->stat_sn		= conn->stat_sn++;
2402e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
2403e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
2404e48354ceSNicholas Bellinger 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn);
2405e48354ceSNicholas Bellinger 	hdr->async_event	= ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2406e48354ceSNicholas Bellinger 	hdr->param1		= cpu_to_be16(cmd->logout_cid);
2407e48354ceSNicholas Bellinger 	hdr->param2		= cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2408e48354ceSNicholas Bellinger 	hdr->param3		= cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2409e48354ceSNicholas Bellinger 
2410e48354ceSNicholas Bellinger 	if (conn->conn_ops->HeaderDigest) {
2411e48354ceSNicholas Bellinger 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2412e48354ceSNicholas Bellinger 
2413e48354ceSNicholas Bellinger 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2414e48354ceSNicholas Bellinger 				(unsigned char *)hdr, ISCSI_HDR_LEN,
2415e48354ceSNicholas Bellinger 				0, NULL, (u8 *)header_digest);
2416e48354ceSNicholas Bellinger 
2417e48354ceSNicholas Bellinger 		cmd->tx_size += ISCSI_CRC_LEN;
2418e48354ceSNicholas Bellinger 		pr_debug("Attaching CRC32C HeaderDigest to"
2419e48354ceSNicholas Bellinger 			" Async Message 0x%08x\n", *header_digest);
2420e48354ceSNicholas Bellinger 	}
2421e48354ceSNicholas Bellinger 
2422e48354ceSNicholas Bellinger 	cmd->iov_misc[0].iov_base	= cmd->pdu;
2423e48354ceSNicholas Bellinger 	cmd->iov_misc[0].iov_len	= cmd->tx_size;
2424e48354ceSNicholas Bellinger 	cmd->iov_misc_count		= 1;
2425e48354ceSNicholas Bellinger 
2426e48354ceSNicholas Bellinger 	pr_debug("Sending Connection Dropped Async Message StatSN:"
2427e48354ceSNicholas Bellinger 		" 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2428e48354ceSNicholas Bellinger 			cmd->logout_cid, conn->cid);
2429e48354ceSNicholas Bellinger 	return 0;
2430e48354ceSNicholas Bellinger }
2431e48354ceSNicholas Bellinger 
24326f3c0e69SAndy Grover static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
24336f3c0e69SAndy Grover {
24346f3c0e69SAndy Grover 	if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
24356f3c0e69SAndy Grover 	    (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
24366f3c0e69SAndy Grover 		wait_for_completion_interruptible_timeout(
24376f3c0e69SAndy Grover 					&conn->tx_half_close_comp,
24386f3c0e69SAndy Grover 					ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
24396f3c0e69SAndy Grover 	}
24406f3c0e69SAndy Grover }
24416f3c0e69SAndy Grover 
2442e48354ceSNicholas Bellinger static int iscsit_send_data_in(
2443e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
24446f3c0e69SAndy Grover 	struct iscsi_conn *conn)
2445e48354ceSNicholas Bellinger {
2446e48354ceSNicholas Bellinger 	int iov_ret = 0, set_statsn = 0;
2447e48354ceSNicholas Bellinger 	u32 iov_count = 0, tx_size = 0;
2448e48354ceSNicholas Bellinger 	struct iscsi_datain datain;
2449e48354ceSNicholas Bellinger 	struct iscsi_datain_req *dr;
2450e48354ceSNicholas Bellinger 	struct iscsi_data_rsp *hdr;
2451e48354ceSNicholas Bellinger 	struct kvec *iov;
24526f3c0e69SAndy Grover 	int eodr = 0;
24536f3c0e69SAndy Grover 	int ret;
2454e48354ceSNicholas Bellinger 
2455e48354ceSNicholas Bellinger 	memset(&datain, 0, sizeof(struct iscsi_datain));
2456e48354ceSNicholas Bellinger 	dr = iscsit_get_datain_values(cmd, &datain);
2457e48354ceSNicholas Bellinger 	if (!dr) {
2458e48354ceSNicholas Bellinger 		pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2459e48354ceSNicholas Bellinger 				cmd->init_task_tag);
2460e48354ceSNicholas Bellinger 		return -1;
2461e48354ceSNicholas Bellinger 	}
2462e48354ceSNicholas Bellinger 
2463e48354ceSNicholas Bellinger 	/*
2464e48354ceSNicholas Bellinger 	 * Be paranoid and double check the logic for now.
2465e48354ceSNicholas Bellinger 	 */
2466ebf1d95cSAndy Grover 	if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
2467e48354ceSNicholas Bellinger 		pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2468e48354ceSNicholas Bellinger 			" datain.length: %u exceeds cmd->data_length: %u\n",
2469e48354ceSNicholas Bellinger 			cmd->init_task_tag, datain.offset, datain.length,
2470ebf1d95cSAndy Grover 				cmd->se_cmd.data_length);
2471e48354ceSNicholas Bellinger 		return -1;
2472e48354ceSNicholas Bellinger 	}
2473e48354ceSNicholas Bellinger 
2474e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->sess->session_stats_lock);
2475e48354ceSNicholas Bellinger 	conn->sess->tx_data_octets += datain.length;
2476e48354ceSNicholas Bellinger 	if (conn->sess->se_sess->se_node_acl) {
2477e48354ceSNicholas Bellinger 		spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2478e48354ceSNicholas Bellinger 		conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2479e48354ceSNicholas Bellinger 		spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2480e48354ceSNicholas Bellinger 	}
2481e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->sess->session_stats_lock);
2482e48354ceSNicholas Bellinger 	/*
2483e48354ceSNicholas Bellinger 	 * Special case for successfully execution w/ both DATAIN
2484e48354ceSNicholas Bellinger 	 * and Sense Data.
2485e48354ceSNicholas Bellinger 	 */
2486e48354ceSNicholas Bellinger 	if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2487e48354ceSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2488e48354ceSNicholas Bellinger 		datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2489e48354ceSNicholas Bellinger 	else {
2490e48354ceSNicholas Bellinger 		if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2491e48354ceSNicholas Bellinger 		    (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2492e48354ceSNicholas Bellinger 			iscsit_increment_maxcmdsn(cmd, conn->sess);
2493e48354ceSNicholas Bellinger 			cmd->stat_sn = conn->stat_sn++;
2494e48354ceSNicholas Bellinger 			set_statsn = 1;
2495e48354ceSNicholas Bellinger 		} else if (dr->dr_complete ==
2496e48354ceSNicholas Bellinger 				DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2497e48354ceSNicholas Bellinger 			set_statsn = 1;
2498e48354ceSNicholas Bellinger 	}
2499e48354ceSNicholas Bellinger 
2500e48354ceSNicholas Bellinger 	hdr	= (struct iscsi_data_rsp *) cmd->pdu;
2501e48354ceSNicholas Bellinger 	memset(hdr, 0, ISCSI_HDR_LEN);
2502e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_SCSI_DATA_IN;
2503e48354ceSNicholas Bellinger 	hdr->flags		= datain.flags;
2504e48354ceSNicholas Bellinger 	if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2505e48354ceSNicholas Bellinger 		if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2506e48354ceSNicholas Bellinger 			hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
25077e46cf02SNicholas Bellinger 			hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2508e48354ceSNicholas Bellinger 		} else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2509e48354ceSNicholas Bellinger 			hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
25107e46cf02SNicholas Bellinger 			hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2511e48354ceSNicholas Bellinger 		}
2512e48354ceSNicholas Bellinger 	}
2513e48354ceSNicholas Bellinger 	hton24(hdr->dlength, datain.length);
2514e48354ceSNicholas Bellinger 	if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2515e48354ceSNicholas Bellinger 		int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2516e48354ceSNicholas Bellinger 				(struct scsi_lun *)&hdr->lun);
2517e48354ceSNicholas Bellinger 	else
2518e48354ceSNicholas Bellinger 		put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2519e48354ceSNicholas Bellinger 
252066c7db68SChristoph Hellwig 	hdr->itt		= cmd->init_task_tag;
252150e5c87dSChristoph Hellwig 
252250e5c87dSChristoph Hellwig 	if (hdr->flags & ISCSI_FLAG_DATA_ACK)
252350e5c87dSChristoph Hellwig 		hdr->ttt		= cpu_to_be32(cmd->targ_xfer_tag);
252450e5c87dSChristoph Hellwig 	else
252550e5c87dSChristoph Hellwig 		hdr->ttt		= cpu_to_be32(0xFFFFFFFF);
252650e5c87dSChristoph Hellwig 	if (set_statsn)
252750e5c87dSChristoph Hellwig 		hdr->statsn		= cpu_to_be32(cmd->stat_sn);
252850e5c87dSChristoph Hellwig 	else
252950e5c87dSChristoph Hellwig 		hdr->statsn		= cpu_to_be32(0xFFFFFFFF);
253050e5c87dSChristoph Hellwig 
2531e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
2532e48354ceSNicholas Bellinger 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn);
2533e48354ceSNicholas Bellinger 	hdr->datasn		= cpu_to_be32(datain.data_sn);
2534e48354ceSNicholas Bellinger 	hdr->offset		= cpu_to_be32(datain.offset);
2535e48354ceSNicholas Bellinger 
2536e48354ceSNicholas Bellinger 	iov = &cmd->iov_data[0];
2537e48354ceSNicholas Bellinger 	iov[iov_count].iov_base	= cmd->pdu;
2538e48354ceSNicholas Bellinger 	iov[iov_count++].iov_len	= ISCSI_HDR_LEN;
2539e48354ceSNicholas Bellinger 	tx_size += ISCSI_HDR_LEN;
2540e48354ceSNicholas Bellinger 
2541e48354ceSNicholas Bellinger 	if (conn->conn_ops->HeaderDigest) {
2542e48354ceSNicholas Bellinger 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2543e48354ceSNicholas Bellinger 
2544e48354ceSNicholas Bellinger 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2545e48354ceSNicholas Bellinger 				(unsigned char *)hdr, ISCSI_HDR_LEN,
2546e48354ceSNicholas Bellinger 				0, NULL, (u8 *)header_digest);
2547e48354ceSNicholas Bellinger 
2548e48354ceSNicholas Bellinger 		iov[0].iov_len += ISCSI_CRC_LEN;
2549e48354ceSNicholas Bellinger 		tx_size += ISCSI_CRC_LEN;
2550e48354ceSNicholas Bellinger 
2551e48354ceSNicholas Bellinger 		pr_debug("Attaching CRC32 HeaderDigest"
2552e48354ceSNicholas Bellinger 			" for DataIN PDU 0x%08x\n", *header_digest);
2553e48354ceSNicholas Bellinger 	}
2554e48354ceSNicholas Bellinger 
2555e48354ceSNicholas Bellinger 	iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1], datain.offset, datain.length);
2556e48354ceSNicholas Bellinger 	if (iov_ret < 0)
2557e48354ceSNicholas Bellinger 		return -1;
2558e48354ceSNicholas Bellinger 
2559e48354ceSNicholas Bellinger 	iov_count += iov_ret;
2560e48354ceSNicholas Bellinger 	tx_size += datain.length;
2561e48354ceSNicholas Bellinger 
2562e48354ceSNicholas Bellinger 	cmd->padding = ((-datain.length) & 3);
2563e48354ceSNicholas Bellinger 	if (cmd->padding) {
2564e48354ceSNicholas Bellinger 		iov[iov_count].iov_base		= cmd->pad_bytes;
2565e48354ceSNicholas Bellinger 		iov[iov_count++].iov_len	= cmd->padding;
2566e48354ceSNicholas Bellinger 		tx_size += cmd->padding;
2567e48354ceSNicholas Bellinger 
2568e48354ceSNicholas Bellinger 		pr_debug("Attaching %u padding bytes\n",
2569e48354ceSNicholas Bellinger 				cmd->padding);
2570e48354ceSNicholas Bellinger 	}
2571e48354ceSNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
2572e48354ceSNicholas Bellinger 		cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2573e48354ceSNicholas Bellinger 			 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2574e48354ceSNicholas Bellinger 
2575e48354ceSNicholas Bellinger 		iov[iov_count].iov_base	= &cmd->data_crc;
2576e48354ceSNicholas Bellinger 		iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2577e48354ceSNicholas Bellinger 		tx_size += ISCSI_CRC_LEN;
2578e48354ceSNicholas Bellinger 
2579e48354ceSNicholas Bellinger 		pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2580e48354ceSNicholas Bellinger 			" 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2581e48354ceSNicholas Bellinger 	}
2582e48354ceSNicholas Bellinger 
2583e48354ceSNicholas Bellinger 	cmd->iov_data_count = iov_count;
2584e48354ceSNicholas Bellinger 	cmd->tx_size = tx_size;
2585e48354ceSNicholas Bellinger 
2586e48354ceSNicholas Bellinger 	pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2587e48354ceSNicholas Bellinger 		" DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2588e48354ceSNicholas Bellinger 		cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2589e48354ceSNicholas Bellinger 		ntohl(hdr->offset), datain.length, conn->cid);
2590e48354ceSNicholas Bellinger 
25916f3c0e69SAndy Grover 	/* sendpage is preferred but can't insert markers */
25926f3c0e69SAndy Grover 	if (!conn->conn_ops->IFMarker)
25936f3c0e69SAndy Grover 		ret = iscsit_fe_sendpage_sg(cmd, conn);
25946f3c0e69SAndy Grover 	else
25956f3c0e69SAndy Grover 		ret = iscsit_send_tx_data(cmd, conn, 0);
25966f3c0e69SAndy Grover 
25976f3c0e69SAndy Grover 	iscsit_unmap_iovec(cmd);
25986f3c0e69SAndy Grover 
25996f3c0e69SAndy Grover 	if (ret < 0) {
26006f3c0e69SAndy Grover 		iscsit_tx_thread_wait_for_tcp(conn);
26016f3c0e69SAndy Grover 		return ret;
26026f3c0e69SAndy Grover 	}
26036f3c0e69SAndy Grover 
2604e48354ceSNicholas Bellinger 	if (dr->dr_complete) {
26056f3c0e69SAndy Grover 		eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
2606e48354ceSNicholas Bellinger 				2 : 1;
2607e48354ceSNicholas Bellinger 		iscsit_free_datain_req(cmd, dr);
2608e48354ceSNicholas Bellinger 	}
2609e48354ceSNicholas Bellinger 
26106f3c0e69SAndy Grover 	return eodr;
2611e48354ceSNicholas Bellinger }
2612e48354ceSNicholas Bellinger 
2613e48354ceSNicholas Bellinger static int iscsit_send_logout_response(
2614e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
2615e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
2616e48354ceSNicholas Bellinger {
2617e48354ceSNicholas Bellinger 	int niov = 0, tx_size;
2618e48354ceSNicholas Bellinger 	struct iscsi_conn *logout_conn = NULL;
2619e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr = NULL;
2620e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
2621e48354ceSNicholas Bellinger 	struct kvec *iov;
2622e48354ceSNicholas Bellinger 	struct iscsi_logout_rsp *hdr;
2623e48354ceSNicholas Bellinger 	/*
2624e48354ceSNicholas Bellinger 	 * The actual shutting down of Sessions and/or Connections
2625e48354ceSNicholas Bellinger 	 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2626e48354ceSNicholas Bellinger 	 * is done in scsi_logout_post_handler().
2627e48354ceSNicholas Bellinger 	 */
2628e48354ceSNicholas Bellinger 	switch (cmd->logout_reason) {
2629e48354ceSNicholas Bellinger 	case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2630e48354ceSNicholas Bellinger 		pr_debug("iSCSI session logout successful, setting"
2631e48354ceSNicholas Bellinger 			" logout response to ISCSI_LOGOUT_SUCCESS.\n");
2632e48354ceSNicholas Bellinger 		cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2633e48354ceSNicholas Bellinger 		break;
2634e48354ceSNicholas Bellinger 	case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2635e48354ceSNicholas Bellinger 		if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2636e48354ceSNicholas Bellinger 			break;
2637e48354ceSNicholas Bellinger 		/*
2638e48354ceSNicholas Bellinger 		 * For CLOSECONNECTION logout requests carrying
2639e48354ceSNicholas Bellinger 		 * a matching logout CID -> local CID, the reference
2640e48354ceSNicholas Bellinger 		 * for the local CID will have been incremented in
2641e48354ceSNicholas Bellinger 		 * iscsi_logout_closeconnection().
2642e48354ceSNicholas Bellinger 		 *
2643e48354ceSNicholas Bellinger 		 * For CLOSECONNECTION logout requests carrying
2644e48354ceSNicholas Bellinger 		 * a different CID than the connection it arrived
2645e48354ceSNicholas Bellinger 		 * on, the connection responding to cmd->logout_cid
2646e48354ceSNicholas Bellinger 		 * is stopped in iscsit_logout_post_handler_diffcid().
2647e48354ceSNicholas Bellinger 		 */
2648e48354ceSNicholas Bellinger 
2649e48354ceSNicholas Bellinger 		pr_debug("iSCSI CID: %hu logout on CID: %hu"
2650e48354ceSNicholas Bellinger 			" successful.\n", cmd->logout_cid, conn->cid);
2651e48354ceSNicholas Bellinger 		cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2652e48354ceSNicholas Bellinger 		break;
2653e48354ceSNicholas Bellinger 	case ISCSI_LOGOUT_REASON_RECOVERY:
2654e48354ceSNicholas Bellinger 		if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2655e48354ceSNicholas Bellinger 		    (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2656e48354ceSNicholas Bellinger 			break;
2657e48354ceSNicholas Bellinger 		/*
2658e48354ceSNicholas Bellinger 		 * If the connection is still active from our point of view
2659e48354ceSNicholas Bellinger 		 * force connection recovery to occur.
2660e48354ceSNicholas Bellinger 		 */
2661e48354ceSNicholas Bellinger 		logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2662e48354ceSNicholas Bellinger 				cmd->logout_cid);
2663ee1b1b9cSAndy Grover 		if (logout_conn) {
2664e48354ceSNicholas Bellinger 			iscsit_connection_reinstatement_rcfr(logout_conn);
2665e48354ceSNicholas Bellinger 			iscsit_dec_conn_usage_count(logout_conn);
2666e48354ceSNicholas Bellinger 		}
2667e48354ceSNicholas Bellinger 
2668e48354ceSNicholas Bellinger 		cr = iscsit_get_inactive_connection_recovery_entry(
2669e48354ceSNicholas Bellinger 				conn->sess, cmd->logout_cid);
2670e48354ceSNicholas Bellinger 		if (!cr) {
2671e48354ceSNicholas Bellinger 			pr_err("Unable to locate CID: %hu for"
2672e48354ceSNicholas Bellinger 			" REMOVECONNFORRECOVERY Logout Request.\n",
2673e48354ceSNicholas Bellinger 				cmd->logout_cid);
2674e48354ceSNicholas Bellinger 			cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2675e48354ceSNicholas Bellinger 			break;
2676e48354ceSNicholas Bellinger 		}
2677e48354ceSNicholas Bellinger 
2678e48354ceSNicholas Bellinger 		iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2679e48354ceSNicholas Bellinger 
2680e48354ceSNicholas Bellinger 		pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2681e48354ceSNicholas Bellinger 			" for recovery for CID: %hu on CID: %hu successful.\n",
2682e48354ceSNicholas Bellinger 				cmd->logout_cid, conn->cid);
2683e48354ceSNicholas Bellinger 		cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2684e48354ceSNicholas Bellinger 		break;
2685e48354ceSNicholas Bellinger 	default:
2686e48354ceSNicholas Bellinger 		pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2687e48354ceSNicholas Bellinger 				cmd->logout_reason);
2688e48354ceSNicholas Bellinger 		return -1;
2689e48354ceSNicholas Bellinger 	}
2690e48354ceSNicholas Bellinger 
2691e48354ceSNicholas Bellinger 	tx_size = ISCSI_HDR_LEN;
2692e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_logout_rsp *)cmd->pdu;
2693e48354ceSNicholas Bellinger 	memset(hdr, 0, ISCSI_HDR_LEN);
2694e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_LOGOUT_RSP;
2695e48354ceSNicholas Bellinger 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
2696e48354ceSNicholas Bellinger 	hdr->response		= cmd->logout_response;
269766c7db68SChristoph Hellwig 	hdr->itt		= cmd->init_task_tag;
2698e48354ceSNicholas Bellinger 	cmd->stat_sn		= conn->stat_sn++;
2699e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
2700e48354ceSNicholas Bellinger 
2701e48354ceSNicholas Bellinger 	iscsit_increment_maxcmdsn(cmd, conn->sess);
2702e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
2703e48354ceSNicholas Bellinger 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn);
2704e48354ceSNicholas Bellinger 
2705e48354ceSNicholas Bellinger 	iov = &cmd->iov_misc[0];
2706e48354ceSNicholas Bellinger 	iov[niov].iov_base	= cmd->pdu;
2707e48354ceSNicholas Bellinger 	iov[niov++].iov_len	= ISCSI_HDR_LEN;
2708e48354ceSNicholas Bellinger 
2709e48354ceSNicholas Bellinger 	if (conn->conn_ops->HeaderDigest) {
2710e48354ceSNicholas Bellinger 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2711e48354ceSNicholas Bellinger 
2712e48354ceSNicholas Bellinger 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2713e48354ceSNicholas Bellinger 				(unsigned char *)hdr, ISCSI_HDR_LEN,
2714e48354ceSNicholas Bellinger 				0, NULL, (u8 *)header_digest);
2715e48354ceSNicholas Bellinger 
2716e48354ceSNicholas Bellinger 		iov[0].iov_len += ISCSI_CRC_LEN;
2717e48354ceSNicholas Bellinger 		tx_size += ISCSI_CRC_LEN;
2718e48354ceSNicholas Bellinger 		pr_debug("Attaching CRC32C HeaderDigest to"
2719e48354ceSNicholas Bellinger 			" Logout Response 0x%08x\n", *header_digest);
2720e48354ceSNicholas Bellinger 	}
2721e48354ceSNicholas Bellinger 	cmd->iov_misc_count = niov;
2722e48354ceSNicholas Bellinger 	cmd->tx_size = tx_size;
2723e48354ceSNicholas Bellinger 
2724e48354ceSNicholas Bellinger 	pr_debug("Sending Logout Response ITT: 0x%08x StatSN:"
2725e48354ceSNicholas Bellinger 		" 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2726e48354ceSNicholas Bellinger 		cmd->init_task_tag, cmd->stat_sn, hdr->response,
2727e48354ceSNicholas Bellinger 		cmd->logout_cid, conn->cid);
2728e48354ceSNicholas Bellinger 
2729e48354ceSNicholas Bellinger 	return 0;
2730e48354ceSNicholas Bellinger }
2731e48354ceSNicholas Bellinger 
2732e48354ceSNicholas Bellinger /*
2733e48354ceSNicholas Bellinger  *	Unsolicited NOPIN, either requesting a response or not.
2734e48354ceSNicholas Bellinger  */
2735e48354ceSNicholas Bellinger static int iscsit_send_unsolicited_nopin(
2736e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
2737e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
2738e48354ceSNicholas Bellinger 	int want_response)
2739e48354ceSNicholas Bellinger {
2740e48354ceSNicholas Bellinger 	int tx_size = ISCSI_HDR_LEN;
2741e48354ceSNicholas Bellinger 	struct iscsi_nopin *hdr;
27426f3c0e69SAndy Grover 	int ret;
2743e48354ceSNicholas Bellinger 
2744e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_nopin *) cmd->pdu;
2745e48354ceSNicholas Bellinger 	memset(hdr, 0, ISCSI_HDR_LEN);
2746e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_NOOP_IN;
2747e48354ceSNicholas Bellinger 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
274866c7db68SChristoph Hellwig 	hdr->itt		= cmd->init_task_tag;
2749e48354ceSNicholas Bellinger 	hdr->ttt		= cpu_to_be32(cmd->targ_xfer_tag);
2750e48354ceSNicholas Bellinger 	cmd->stat_sn		= conn->stat_sn;
2751e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
2752e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
2753e48354ceSNicholas Bellinger 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn);
2754e48354ceSNicholas Bellinger 
2755e48354ceSNicholas Bellinger 	if (conn->conn_ops->HeaderDigest) {
2756e48354ceSNicholas Bellinger 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2757e48354ceSNicholas Bellinger 
2758e48354ceSNicholas Bellinger 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2759e48354ceSNicholas Bellinger 				(unsigned char *)hdr, ISCSI_HDR_LEN,
2760e48354ceSNicholas Bellinger 				0, NULL, (u8 *)header_digest);
2761e48354ceSNicholas Bellinger 
2762e48354ceSNicholas Bellinger 		tx_size += ISCSI_CRC_LEN;
2763e48354ceSNicholas Bellinger 		pr_debug("Attaching CRC32C HeaderDigest to"
2764e48354ceSNicholas Bellinger 			" NopIN 0x%08x\n", *header_digest);
2765e48354ceSNicholas Bellinger 	}
2766e48354ceSNicholas Bellinger 
2767e48354ceSNicholas Bellinger 	cmd->iov_misc[0].iov_base	= cmd->pdu;
2768e48354ceSNicholas Bellinger 	cmd->iov_misc[0].iov_len	= tx_size;
2769e48354ceSNicholas Bellinger 	cmd->iov_misc_count	= 1;
2770e48354ceSNicholas Bellinger 	cmd->tx_size		= tx_size;
2771e48354ceSNicholas Bellinger 
2772e48354ceSNicholas Bellinger 	pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2773e48354ceSNicholas Bellinger 		" 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2774e48354ceSNicholas Bellinger 
27756f3c0e69SAndy Grover 	ret = iscsit_send_tx_data(cmd, conn, 1);
27766f3c0e69SAndy Grover 	if (ret < 0) {
27776f3c0e69SAndy Grover 		iscsit_tx_thread_wait_for_tcp(conn);
27786f3c0e69SAndy Grover 		return ret;
27796f3c0e69SAndy Grover 	}
27806f3c0e69SAndy Grover 
27816f3c0e69SAndy Grover 	spin_lock_bh(&cmd->istate_lock);
27826f3c0e69SAndy Grover 	cmd->i_state = want_response ?
27836f3c0e69SAndy Grover 		ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
27846f3c0e69SAndy Grover 	spin_unlock_bh(&cmd->istate_lock);
27856f3c0e69SAndy Grover 
2786e48354ceSNicholas Bellinger 	return 0;
2787e48354ceSNicholas Bellinger }
2788e48354ceSNicholas Bellinger 
2789e48354ceSNicholas Bellinger static int iscsit_send_nopin_response(
2790e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
2791e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
2792e48354ceSNicholas Bellinger {
2793e48354ceSNicholas Bellinger 	int niov = 0, tx_size;
2794e48354ceSNicholas Bellinger 	u32 padding = 0;
2795e48354ceSNicholas Bellinger 	struct kvec *iov;
2796e48354ceSNicholas Bellinger 	struct iscsi_nopin *hdr;
2797e48354ceSNicholas Bellinger 
2798e48354ceSNicholas Bellinger 	tx_size = ISCSI_HDR_LEN;
2799e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_nopin *) cmd->pdu;
2800e48354ceSNicholas Bellinger 	memset(hdr, 0, ISCSI_HDR_LEN);
2801e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_NOOP_IN;
2802e48354ceSNicholas Bellinger 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
2803e48354ceSNicholas Bellinger 	hton24(hdr->dlength, cmd->buf_ptr_size);
2804e48354ceSNicholas Bellinger 	put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
280566c7db68SChristoph Hellwig 	hdr->itt		= cmd->init_task_tag;
2806e48354ceSNicholas Bellinger 	hdr->ttt		= cpu_to_be32(cmd->targ_xfer_tag);
2807e48354ceSNicholas Bellinger 	cmd->stat_sn		= conn->stat_sn++;
2808e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
2809e48354ceSNicholas Bellinger 
2810e48354ceSNicholas Bellinger 	iscsit_increment_maxcmdsn(cmd, conn->sess);
2811e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
2812e48354ceSNicholas Bellinger 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn);
2813e48354ceSNicholas Bellinger 
2814e48354ceSNicholas Bellinger 	iov = &cmd->iov_misc[0];
2815e48354ceSNicholas Bellinger 	iov[niov].iov_base	= cmd->pdu;
2816e48354ceSNicholas Bellinger 	iov[niov++].iov_len	= ISCSI_HDR_LEN;
2817e48354ceSNicholas Bellinger 
2818e48354ceSNicholas Bellinger 	if (conn->conn_ops->HeaderDigest) {
2819e48354ceSNicholas Bellinger 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2820e48354ceSNicholas Bellinger 
2821e48354ceSNicholas Bellinger 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2822e48354ceSNicholas Bellinger 				(unsigned char *)hdr, ISCSI_HDR_LEN,
2823e48354ceSNicholas Bellinger 				0, NULL, (u8 *)header_digest);
2824e48354ceSNicholas Bellinger 
2825e48354ceSNicholas Bellinger 		iov[0].iov_len += ISCSI_CRC_LEN;
2826e48354ceSNicholas Bellinger 		tx_size += ISCSI_CRC_LEN;
2827e48354ceSNicholas Bellinger 		pr_debug("Attaching CRC32C HeaderDigest"
2828e48354ceSNicholas Bellinger 			" to NopIn 0x%08x\n", *header_digest);
2829e48354ceSNicholas Bellinger 	}
2830e48354ceSNicholas Bellinger 
2831e48354ceSNicholas Bellinger 	/*
2832e48354ceSNicholas Bellinger 	 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2833e48354ceSNicholas Bellinger 	 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2834e48354ceSNicholas Bellinger 	 */
2835e48354ceSNicholas Bellinger 	if (cmd->buf_ptr_size) {
2836e48354ceSNicholas Bellinger 		iov[niov].iov_base	= cmd->buf_ptr;
2837e48354ceSNicholas Bellinger 		iov[niov++].iov_len	= cmd->buf_ptr_size;
2838e48354ceSNicholas Bellinger 		tx_size += cmd->buf_ptr_size;
2839e48354ceSNicholas Bellinger 
2840e48354ceSNicholas Bellinger 		pr_debug("Echoing back %u bytes of ping"
2841e48354ceSNicholas Bellinger 			" data.\n", cmd->buf_ptr_size);
2842e48354ceSNicholas Bellinger 
2843e48354ceSNicholas Bellinger 		padding = ((-cmd->buf_ptr_size) & 3);
2844e48354ceSNicholas Bellinger 		if (padding != 0) {
2845e48354ceSNicholas Bellinger 			iov[niov].iov_base = &cmd->pad_bytes;
2846e48354ceSNicholas Bellinger 			iov[niov++].iov_len = padding;
2847e48354ceSNicholas Bellinger 			tx_size += padding;
2848e48354ceSNicholas Bellinger 			pr_debug("Attaching %u additional"
2849e48354ceSNicholas Bellinger 				" padding bytes.\n", padding);
2850e48354ceSNicholas Bellinger 		}
2851e48354ceSNicholas Bellinger 		if (conn->conn_ops->DataDigest) {
2852e48354ceSNicholas Bellinger 			iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2853e48354ceSNicholas Bellinger 				cmd->buf_ptr, cmd->buf_ptr_size,
2854e48354ceSNicholas Bellinger 				padding, (u8 *)&cmd->pad_bytes,
2855e48354ceSNicholas Bellinger 				(u8 *)&cmd->data_crc);
2856e48354ceSNicholas Bellinger 
2857e48354ceSNicholas Bellinger 			iov[niov].iov_base = &cmd->data_crc;
2858e48354ceSNicholas Bellinger 			iov[niov++].iov_len = ISCSI_CRC_LEN;
2859e48354ceSNicholas Bellinger 			tx_size += ISCSI_CRC_LEN;
2860e48354ceSNicholas Bellinger 			pr_debug("Attached DataDigest for %u"
2861e48354ceSNicholas Bellinger 				" bytes of ping data, CRC 0x%08x\n",
2862e48354ceSNicholas Bellinger 				cmd->buf_ptr_size, cmd->data_crc);
2863e48354ceSNicholas Bellinger 		}
2864e48354ceSNicholas Bellinger 	}
2865e48354ceSNicholas Bellinger 
2866e48354ceSNicholas Bellinger 	cmd->iov_misc_count = niov;
2867e48354ceSNicholas Bellinger 	cmd->tx_size = tx_size;
2868e48354ceSNicholas Bellinger 
2869e48354ceSNicholas Bellinger 	pr_debug("Sending NOPIN Response ITT: 0x%08x, TTT:"
2870e48354ceSNicholas Bellinger 		" 0x%08x, StatSN: 0x%08x, Length %u\n", cmd->init_task_tag,
2871e48354ceSNicholas Bellinger 		cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2872e48354ceSNicholas Bellinger 
2873e48354ceSNicholas Bellinger 	return 0;
2874e48354ceSNicholas Bellinger }
2875e48354ceSNicholas Bellinger 
28766f3c0e69SAndy Grover static int iscsit_send_r2t(
2877e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
2878e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
2879e48354ceSNicholas Bellinger {
2880e48354ceSNicholas Bellinger 	int tx_size = 0;
2881e48354ceSNicholas Bellinger 	struct iscsi_r2t *r2t;
2882e48354ceSNicholas Bellinger 	struct iscsi_r2t_rsp *hdr;
28836f3c0e69SAndy Grover 	int ret;
2884e48354ceSNicholas Bellinger 
2885e48354ceSNicholas Bellinger 	r2t = iscsit_get_r2t_from_list(cmd);
2886e48354ceSNicholas Bellinger 	if (!r2t)
2887e48354ceSNicholas Bellinger 		return -1;
2888e48354ceSNicholas Bellinger 
2889e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_r2t_rsp *) cmd->pdu;
2890e48354ceSNicholas Bellinger 	memset(hdr, 0, ISCSI_HDR_LEN);
2891e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_R2T;
2892e48354ceSNicholas Bellinger 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
2893e48354ceSNicholas Bellinger 	int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2894e48354ceSNicholas Bellinger 			(struct scsi_lun *)&hdr->lun);
289566c7db68SChristoph Hellwig 	hdr->itt		= cmd->init_task_tag;
2896e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->sess->ttt_lock);
2897e48354ceSNicholas Bellinger 	r2t->targ_xfer_tag	= conn->sess->targ_xfer_tag++;
2898e48354ceSNicholas Bellinger 	if (r2t->targ_xfer_tag == 0xFFFFFFFF)
2899e48354ceSNicholas Bellinger 		r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2900e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->sess->ttt_lock);
2901e48354ceSNicholas Bellinger 	hdr->ttt		= cpu_to_be32(r2t->targ_xfer_tag);
2902e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(conn->stat_sn);
2903e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
2904e48354ceSNicholas Bellinger 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn);
2905e48354ceSNicholas Bellinger 	hdr->r2tsn		= cpu_to_be32(r2t->r2t_sn);
2906e48354ceSNicholas Bellinger 	hdr->data_offset	= cpu_to_be32(r2t->offset);
2907e48354ceSNicholas Bellinger 	hdr->data_length	= cpu_to_be32(r2t->xfer_len);
2908e48354ceSNicholas Bellinger 
2909e48354ceSNicholas Bellinger 	cmd->iov_misc[0].iov_base	= cmd->pdu;
2910e48354ceSNicholas Bellinger 	cmd->iov_misc[0].iov_len	= ISCSI_HDR_LEN;
2911e48354ceSNicholas Bellinger 	tx_size += ISCSI_HDR_LEN;
2912e48354ceSNicholas Bellinger 
2913e48354ceSNicholas Bellinger 	if (conn->conn_ops->HeaderDigest) {
2914e48354ceSNicholas Bellinger 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2915e48354ceSNicholas Bellinger 
2916e48354ceSNicholas Bellinger 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2917e48354ceSNicholas Bellinger 				(unsigned char *)hdr, ISCSI_HDR_LEN,
2918e48354ceSNicholas Bellinger 				0, NULL, (u8 *)header_digest);
2919e48354ceSNicholas Bellinger 
2920e48354ceSNicholas Bellinger 		cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
2921e48354ceSNicholas Bellinger 		tx_size += ISCSI_CRC_LEN;
2922e48354ceSNicholas Bellinger 		pr_debug("Attaching CRC32 HeaderDigest for R2T"
2923e48354ceSNicholas Bellinger 			" PDU 0x%08x\n", *header_digest);
2924e48354ceSNicholas Bellinger 	}
2925e48354ceSNicholas Bellinger 
2926e48354ceSNicholas Bellinger 	pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
2927e48354ceSNicholas Bellinger 		" 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
2928e48354ceSNicholas Bellinger 		(!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
2929e48354ceSNicholas Bellinger 		r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
2930e48354ceSNicholas Bellinger 			r2t->offset, r2t->xfer_len, conn->cid);
2931e48354ceSNicholas Bellinger 
2932e48354ceSNicholas Bellinger 	cmd->iov_misc_count = 1;
2933e48354ceSNicholas Bellinger 	cmd->tx_size = tx_size;
2934e48354ceSNicholas Bellinger 
2935e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
2936e48354ceSNicholas Bellinger 	r2t->sent_r2t = 1;
2937e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
2938e48354ceSNicholas Bellinger 
29396f3c0e69SAndy Grover 	ret = iscsit_send_tx_data(cmd, conn, 1);
29406f3c0e69SAndy Grover 	if (ret < 0) {
29416f3c0e69SAndy Grover 		iscsit_tx_thread_wait_for_tcp(conn);
29426f3c0e69SAndy Grover 		return ret;
29436f3c0e69SAndy Grover 	}
29446f3c0e69SAndy Grover 
29456f3c0e69SAndy Grover 	spin_lock_bh(&cmd->dataout_timeout_lock);
29466f3c0e69SAndy Grover 	iscsit_start_dataout_timer(cmd, conn);
29476f3c0e69SAndy Grover 	spin_unlock_bh(&cmd->dataout_timeout_lock);
29486f3c0e69SAndy Grover 
2949e48354ceSNicholas Bellinger 	return 0;
2950e48354ceSNicholas Bellinger }
2951e48354ceSNicholas Bellinger 
2952e48354ceSNicholas Bellinger /*
29538b1e1244SAndy Grover  *	@recovery: If called from iscsi_task_reassign_complete_write() for
2954e48354ceSNicholas Bellinger  *		connection recovery.
2955e48354ceSNicholas Bellinger  */
2956e48354ceSNicholas Bellinger int iscsit_build_r2ts_for_cmd(
2957e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
2958e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
29598b1e1244SAndy Grover 	bool recovery)
2960e48354ceSNicholas Bellinger {
2961e48354ceSNicholas Bellinger 	int first_r2t = 1;
2962e48354ceSNicholas Bellinger 	u32 offset = 0, xfer_len = 0;
2963e48354ceSNicholas Bellinger 
2964e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
2965e48354ceSNicholas Bellinger 	if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
2966e48354ceSNicholas Bellinger 		spin_unlock_bh(&cmd->r2t_lock);
2967e48354ceSNicholas Bellinger 		return 0;
2968e48354ceSNicholas Bellinger 	}
2969e48354ceSNicholas Bellinger 
29708b1e1244SAndy Grover 	if (conn->sess->sess_ops->DataSequenceInOrder &&
29718b1e1244SAndy Grover 	    !recovery)
2972c6037cc5SAndy Grover 		cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
2973e48354ceSNicholas Bellinger 
2974e48354ceSNicholas Bellinger 	while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
2975e48354ceSNicholas Bellinger 		if (conn->sess->sess_ops->DataSequenceInOrder) {
2976e48354ceSNicholas Bellinger 			offset = cmd->r2t_offset;
2977e48354ceSNicholas Bellinger 
29788b1e1244SAndy Grover 			if (first_r2t && recovery) {
29798b1e1244SAndy Grover 				int new_data_end = offset +
29808b1e1244SAndy Grover 					conn->sess->sess_ops->MaxBurstLength -
29818b1e1244SAndy Grover 					cmd->next_burst_len;
29828b1e1244SAndy Grover 
2983ebf1d95cSAndy Grover 				if (new_data_end > cmd->se_cmd.data_length)
2984ebf1d95cSAndy Grover 					xfer_len = cmd->se_cmd.data_length - offset;
29858b1e1244SAndy Grover 				else
29868b1e1244SAndy Grover 					xfer_len =
29878b1e1244SAndy Grover 						conn->sess->sess_ops->MaxBurstLength -
29888b1e1244SAndy Grover 						cmd->next_burst_len;
2989e48354ceSNicholas Bellinger 			} else {
29908b1e1244SAndy Grover 				int new_data_end = offset +
2991e48354ceSNicholas Bellinger 					conn->sess->sess_ops->MaxBurstLength;
29928b1e1244SAndy Grover 
2993ebf1d95cSAndy Grover 				if (new_data_end > cmd->se_cmd.data_length)
2994ebf1d95cSAndy Grover 					xfer_len = cmd->se_cmd.data_length - offset;
29958b1e1244SAndy Grover 				else
29968b1e1244SAndy Grover 					xfer_len = conn->sess->sess_ops->MaxBurstLength;
2997e48354ceSNicholas Bellinger 			}
2998e48354ceSNicholas Bellinger 			cmd->r2t_offset += xfer_len;
2999e48354ceSNicholas Bellinger 
3000ebf1d95cSAndy Grover 			if (cmd->r2t_offset == cmd->se_cmd.data_length)
3001e48354ceSNicholas Bellinger 				cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3002e48354ceSNicholas Bellinger 		} else {
3003e48354ceSNicholas Bellinger 			struct iscsi_seq *seq;
3004e48354ceSNicholas Bellinger 
3005e48354ceSNicholas Bellinger 			seq = iscsit_get_seq_holder_for_r2t(cmd);
3006e48354ceSNicholas Bellinger 			if (!seq) {
3007e48354ceSNicholas Bellinger 				spin_unlock_bh(&cmd->r2t_lock);
3008e48354ceSNicholas Bellinger 				return -1;
3009e48354ceSNicholas Bellinger 			}
3010e48354ceSNicholas Bellinger 
3011e48354ceSNicholas Bellinger 			offset = seq->offset;
3012e48354ceSNicholas Bellinger 			xfer_len = seq->xfer_len;
3013e48354ceSNicholas Bellinger 
3014e48354ceSNicholas Bellinger 			if (cmd->seq_send_order == cmd->seq_count)
3015e48354ceSNicholas Bellinger 				cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3016e48354ceSNicholas Bellinger 		}
3017e48354ceSNicholas Bellinger 		cmd->outstanding_r2ts++;
3018e48354ceSNicholas Bellinger 		first_r2t = 0;
3019e48354ceSNicholas Bellinger 
3020e48354ceSNicholas Bellinger 		if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3021e48354ceSNicholas Bellinger 			spin_unlock_bh(&cmd->r2t_lock);
3022e48354ceSNicholas Bellinger 			return -1;
3023e48354ceSNicholas Bellinger 		}
3024e48354ceSNicholas Bellinger 
3025e48354ceSNicholas Bellinger 		if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3026e48354ceSNicholas Bellinger 			break;
3027e48354ceSNicholas Bellinger 	}
3028e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
3029e48354ceSNicholas Bellinger 
3030e48354ceSNicholas Bellinger 	return 0;
3031e48354ceSNicholas Bellinger }
3032e48354ceSNicholas Bellinger 
3033e48354ceSNicholas Bellinger static int iscsit_send_status(
3034e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
3035e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
3036e48354ceSNicholas Bellinger {
3037e48354ceSNicholas Bellinger 	u8 iov_count = 0, recovery;
3038e48354ceSNicholas Bellinger 	u32 padding = 0, tx_size = 0;
3039e48354ceSNicholas Bellinger 	struct iscsi_scsi_rsp *hdr;
3040e48354ceSNicholas Bellinger 	struct kvec *iov;
3041e48354ceSNicholas Bellinger 
3042e48354ceSNicholas Bellinger 	recovery = (cmd->i_state != ISTATE_SEND_STATUS);
3043e48354ceSNicholas Bellinger 	if (!recovery)
3044e48354ceSNicholas Bellinger 		cmd->stat_sn = conn->stat_sn++;
3045e48354ceSNicholas Bellinger 
3046e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->sess->session_stats_lock);
3047e48354ceSNicholas Bellinger 	conn->sess->rsp_pdus++;
3048e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->sess->session_stats_lock);
3049e48354ceSNicholas Bellinger 
3050e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_scsi_rsp *) cmd->pdu;
3051e48354ceSNicholas Bellinger 	memset(hdr, 0, ISCSI_HDR_LEN);
3052e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_SCSI_CMD_RSP;
3053e48354ceSNicholas Bellinger 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
3054e48354ceSNicholas Bellinger 	if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3055e48354ceSNicholas Bellinger 		hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
30567e46cf02SNicholas Bellinger 		hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3057e48354ceSNicholas Bellinger 	} else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3058e48354ceSNicholas Bellinger 		hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
30597e46cf02SNicholas Bellinger 		hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3060e48354ceSNicholas Bellinger 	}
3061e48354ceSNicholas Bellinger 	hdr->response		= cmd->iscsi_response;
3062e48354ceSNicholas Bellinger 	hdr->cmd_status		= cmd->se_cmd.scsi_status;
306366c7db68SChristoph Hellwig 	hdr->itt		= cmd->init_task_tag;
3064e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
3065e48354ceSNicholas Bellinger 
3066e48354ceSNicholas Bellinger 	iscsit_increment_maxcmdsn(cmd, conn->sess);
3067e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
3068e48354ceSNicholas Bellinger 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn);
3069e48354ceSNicholas Bellinger 
3070e48354ceSNicholas Bellinger 	iov = &cmd->iov_misc[0];
3071e48354ceSNicholas Bellinger 	iov[iov_count].iov_base	= cmd->pdu;
3072e48354ceSNicholas Bellinger 	iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3073e48354ceSNicholas Bellinger 	tx_size += ISCSI_HDR_LEN;
3074e48354ceSNicholas Bellinger 
3075e48354ceSNicholas Bellinger 	/*
3076e48354ceSNicholas Bellinger 	 * Attach SENSE DATA payload to iSCSI Response PDU
3077e48354ceSNicholas Bellinger 	 */
3078e48354ceSNicholas Bellinger 	if (cmd->se_cmd.sense_buffer &&
3079e48354ceSNicholas Bellinger 	   ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3080e48354ceSNicholas Bellinger 	    (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
30819c58b7ddSRoland Dreier 		put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
30829c58b7ddSRoland Dreier 		cmd->se_cmd.scsi_sense_length += sizeof (__be16);
30839c58b7ddSRoland Dreier 
3084e48354ceSNicholas Bellinger 		padding		= -(cmd->se_cmd.scsi_sense_length) & 3;
308550e5c87dSChristoph Hellwig 		hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
30869c58b7ddSRoland Dreier 		iov[iov_count].iov_base	= cmd->sense_buffer;
3087e48354ceSNicholas Bellinger 		iov[iov_count++].iov_len =
3088e48354ceSNicholas Bellinger 				(cmd->se_cmd.scsi_sense_length + padding);
3089e48354ceSNicholas Bellinger 		tx_size += cmd->se_cmd.scsi_sense_length;
3090e48354ceSNicholas Bellinger 
3091e48354ceSNicholas Bellinger 		if (padding) {
30929c58b7ddSRoland Dreier 			memset(cmd->sense_buffer +
3093e48354ceSNicholas Bellinger 				cmd->se_cmd.scsi_sense_length, 0, padding);
3094e48354ceSNicholas Bellinger 			tx_size += padding;
3095e48354ceSNicholas Bellinger 			pr_debug("Adding %u bytes of padding to"
3096e48354ceSNicholas Bellinger 				" SENSE.\n", padding);
3097e48354ceSNicholas Bellinger 		}
3098e48354ceSNicholas Bellinger 
3099e48354ceSNicholas Bellinger 		if (conn->conn_ops->DataDigest) {
3100e48354ceSNicholas Bellinger 			iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
31019c58b7ddSRoland Dreier 				cmd->sense_buffer,
3102e48354ceSNicholas Bellinger 				(cmd->se_cmd.scsi_sense_length + padding),
3103e48354ceSNicholas Bellinger 				0, NULL, (u8 *)&cmd->data_crc);
3104e48354ceSNicholas Bellinger 
3105e48354ceSNicholas Bellinger 			iov[iov_count].iov_base    = &cmd->data_crc;
3106e48354ceSNicholas Bellinger 			iov[iov_count++].iov_len     = ISCSI_CRC_LEN;
3107e48354ceSNicholas Bellinger 			tx_size += ISCSI_CRC_LEN;
3108e48354ceSNicholas Bellinger 
3109e48354ceSNicholas Bellinger 			pr_debug("Attaching CRC32 DataDigest for"
3110e48354ceSNicholas Bellinger 				" SENSE, %u bytes CRC 0x%08x\n",
3111e48354ceSNicholas Bellinger 				(cmd->se_cmd.scsi_sense_length + padding),
3112e48354ceSNicholas Bellinger 				cmd->data_crc);
3113e48354ceSNicholas Bellinger 		}
3114e48354ceSNicholas Bellinger 
3115e48354ceSNicholas Bellinger 		pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3116e48354ceSNicholas Bellinger 				" Response PDU\n",
3117e48354ceSNicholas Bellinger 				cmd->se_cmd.scsi_sense_length);
3118e48354ceSNicholas Bellinger 	}
3119e48354ceSNicholas Bellinger 
3120e48354ceSNicholas Bellinger 	if (conn->conn_ops->HeaderDigest) {
3121e48354ceSNicholas Bellinger 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3122e48354ceSNicholas Bellinger 
3123e48354ceSNicholas Bellinger 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3124e48354ceSNicholas Bellinger 				(unsigned char *)hdr, ISCSI_HDR_LEN,
3125e48354ceSNicholas Bellinger 				0, NULL, (u8 *)header_digest);
3126e48354ceSNicholas Bellinger 
3127e48354ceSNicholas Bellinger 		iov[0].iov_len += ISCSI_CRC_LEN;
3128e48354ceSNicholas Bellinger 		tx_size += ISCSI_CRC_LEN;
3129e48354ceSNicholas Bellinger 		pr_debug("Attaching CRC32 HeaderDigest for Response"
3130e48354ceSNicholas Bellinger 				" PDU 0x%08x\n", *header_digest);
3131e48354ceSNicholas Bellinger 	}
3132e48354ceSNicholas Bellinger 
3133e48354ceSNicholas Bellinger 	cmd->iov_misc_count = iov_count;
3134e48354ceSNicholas Bellinger 	cmd->tx_size = tx_size;
3135e48354ceSNicholas Bellinger 
3136e48354ceSNicholas Bellinger 	pr_debug("Built %sSCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3137e48354ceSNicholas Bellinger 		" Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3138e48354ceSNicholas Bellinger 		(!recovery) ? "" : "Recovery ", cmd->init_task_tag,
3139e48354ceSNicholas Bellinger 		cmd->stat_sn, 0x00, cmd->se_cmd.scsi_status, conn->cid);
3140e48354ceSNicholas Bellinger 
3141e48354ceSNicholas Bellinger 	return 0;
3142e48354ceSNicholas Bellinger }
3143e48354ceSNicholas Bellinger 
3144e48354ceSNicholas Bellinger static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3145e48354ceSNicholas Bellinger {
3146e48354ceSNicholas Bellinger 	switch (se_tmr->response) {
3147e48354ceSNicholas Bellinger 	case TMR_FUNCTION_COMPLETE:
3148e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_COMPLETE;
3149e48354ceSNicholas Bellinger 	case TMR_TASK_DOES_NOT_EXIST:
3150e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_NO_TASK;
3151e48354ceSNicholas Bellinger 	case TMR_LUN_DOES_NOT_EXIST:
3152e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_NO_LUN;
3153e48354ceSNicholas Bellinger 	case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3154e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_NOT_SUPPORTED;
3155e48354ceSNicholas Bellinger 	case TMR_FUNCTION_AUTHORIZATION_FAILED:
3156e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_AUTH_FAILED;
3157e48354ceSNicholas Bellinger 	case TMR_FUNCTION_REJECTED:
3158e48354ceSNicholas Bellinger 	default:
3159e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_REJECTED;
3160e48354ceSNicholas Bellinger 	}
3161e48354ceSNicholas Bellinger }
3162e48354ceSNicholas Bellinger 
3163e48354ceSNicholas Bellinger static int iscsit_send_task_mgt_rsp(
3164e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
3165e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
3166e48354ceSNicholas Bellinger {
3167e48354ceSNicholas Bellinger 	struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3168e48354ceSNicholas Bellinger 	struct iscsi_tm_rsp *hdr;
3169e48354ceSNicholas Bellinger 	u32 tx_size = 0;
3170e48354ceSNicholas Bellinger 
3171e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_tm_rsp *) cmd->pdu;
3172e48354ceSNicholas Bellinger 	memset(hdr, 0, ISCSI_HDR_LEN);
3173e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_SCSI_TMFUNC_RSP;
31747ae0b103SNicholas Bellinger 	hdr->flags		= ISCSI_FLAG_CMD_FINAL;
3175e48354ceSNicholas Bellinger 	hdr->response		= iscsit_convert_tcm_tmr_rsp(se_tmr);
317666c7db68SChristoph Hellwig 	hdr->itt		= cmd->init_task_tag;
3177e48354ceSNicholas Bellinger 	cmd->stat_sn		= conn->stat_sn++;
3178e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
3179e48354ceSNicholas Bellinger 
3180e48354ceSNicholas Bellinger 	iscsit_increment_maxcmdsn(cmd, conn->sess);
3181e48354ceSNicholas Bellinger 	hdr->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
3182e48354ceSNicholas Bellinger 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn);
3183e48354ceSNicholas Bellinger 
3184e48354ceSNicholas Bellinger 	cmd->iov_misc[0].iov_base	= cmd->pdu;
3185e48354ceSNicholas Bellinger 	cmd->iov_misc[0].iov_len	= ISCSI_HDR_LEN;
3186e48354ceSNicholas Bellinger 	tx_size += ISCSI_HDR_LEN;
3187e48354ceSNicholas Bellinger 
3188e48354ceSNicholas Bellinger 	if (conn->conn_ops->HeaderDigest) {
3189e48354ceSNicholas Bellinger 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3190e48354ceSNicholas Bellinger 
3191e48354ceSNicholas Bellinger 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3192e48354ceSNicholas Bellinger 				(unsigned char *)hdr, ISCSI_HDR_LEN,
3193e48354ceSNicholas Bellinger 				0, NULL, (u8 *)header_digest);
3194e48354ceSNicholas Bellinger 
3195e48354ceSNicholas Bellinger 		cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3196e48354ceSNicholas Bellinger 		tx_size += ISCSI_CRC_LEN;
3197e48354ceSNicholas Bellinger 		pr_debug("Attaching CRC32 HeaderDigest for Task"
3198e48354ceSNicholas Bellinger 			" Mgmt Response PDU 0x%08x\n", *header_digest);
3199e48354ceSNicholas Bellinger 	}
3200e48354ceSNicholas Bellinger 
3201e48354ceSNicholas Bellinger 	cmd->iov_misc_count = 1;
3202e48354ceSNicholas Bellinger 	cmd->tx_size = tx_size;
3203e48354ceSNicholas Bellinger 
3204e48354ceSNicholas Bellinger 	pr_debug("Built Task Management Response ITT: 0x%08x,"
3205e48354ceSNicholas Bellinger 		" StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3206e48354ceSNicholas Bellinger 		cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3207e48354ceSNicholas Bellinger 
3208e48354ceSNicholas Bellinger 	return 0;
3209e48354ceSNicholas Bellinger }
3210e48354ceSNicholas Bellinger 
32112f9bc894SNicholas Bellinger static bool iscsit_check_inaddr_any(struct iscsi_np *np)
32122f9bc894SNicholas Bellinger {
32132f9bc894SNicholas Bellinger 	bool ret = false;
32142f9bc894SNicholas Bellinger 
32152f9bc894SNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
32162f9bc894SNicholas Bellinger 		const struct sockaddr_in6 sin6 = {
32172f9bc894SNicholas Bellinger 			.sin6_addr = IN6ADDR_ANY_INIT };
32182f9bc894SNicholas Bellinger 		struct sockaddr_in6 *sock_in6 =
32192f9bc894SNicholas Bellinger 			 (struct sockaddr_in6 *)&np->np_sockaddr;
32202f9bc894SNicholas Bellinger 
32212f9bc894SNicholas Bellinger 		if (!memcmp(sock_in6->sin6_addr.s6_addr,
32222f9bc894SNicholas Bellinger 				sin6.sin6_addr.s6_addr, 16))
32232f9bc894SNicholas Bellinger 			ret = true;
32242f9bc894SNicholas Bellinger 	} else {
32252f9bc894SNicholas Bellinger 		struct sockaddr_in * sock_in =
32262f9bc894SNicholas Bellinger 			(struct sockaddr_in *)&np->np_sockaddr;
32272f9bc894SNicholas Bellinger 
3228cea0b4ceSChristoph Hellwig 		if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
32292f9bc894SNicholas Bellinger 			ret = true;
32302f9bc894SNicholas Bellinger 	}
32312f9bc894SNicholas Bellinger 
32322f9bc894SNicholas Bellinger 	return ret;
32332f9bc894SNicholas Bellinger }
32342f9bc894SNicholas Bellinger 
32358b1e1244SAndy Grover #define SENDTARGETS_BUF_LIMIT 32768U
32368b1e1244SAndy Grover 
3237e48354ceSNicholas Bellinger static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3238e48354ceSNicholas Bellinger {
3239e48354ceSNicholas Bellinger 	char *payload = NULL;
3240e48354ceSNicholas Bellinger 	struct iscsi_conn *conn = cmd->conn;
3241e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg;
3242e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
3243e48354ceSNicholas Bellinger 	struct iscsi_tpg_np *tpg_np;
3244e48354ceSNicholas Bellinger 	int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
32458b1e1244SAndy Grover 	unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
3246e48354ceSNicholas Bellinger 
32478b1e1244SAndy Grover 	buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
32488b1e1244SAndy Grover 			 SENDTARGETS_BUF_LIMIT);
3249e48354ceSNicholas Bellinger 
3250e48354ceSNicholas Bellinger 	payload = kzalloc(buffer_len, GFP_KERNEL);
3251e48354ceSNicholas Bellinger 	if (!payload) {
3252e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for sendtargets"
3253e48354ceSNicholas Bellinger 				" response.\n");
3254e48354ceSNicholas Bellinger 		return -ENOMEM;
3255e48354ceSNicholas Bellinger 	}
3256e48354ceSNicholas Bellinger 
3257e48354ceSNicholas Bellinger 	spin_lock(&tiqn_lock);
3258e48354ceSNicholas Bellinger 	list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3259e48354ceSNicholas Bellinger 		len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3260e48354ceSNicholas Bellinger 		len += 1;
3261e48354ceSNicholas Bellinger 
3262e48354ceSNicholas Bellinger 		if ((len + payload_len) > buffer_len) {
3263e48354ceSNicholas Bellinger 			end_of_buf = 1;
3264e48354ceSNicholas Bellinger 			goto eob;
3265e48354ceSNicholas Bellinger 		}
32668359cf43SJörn Engel 		memcpy(payload + payload_len, buf, len);
3267e48354ceSNicholas Bellinger 		payload_len += len;
3268e48354ceSNicholas Bellinger 
3269e48354ceSNicholas Bellinger 		spin_lock(&tiqn->tiqn_tpg_lock);
3270e48354ceSNicholas Bellinger 		list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3271e48354ceSNicholas Bellinger 
3272e48354ceSNicholas Bellinger 			spin_lock(&tpg->tpg_state_lock);
3273e48354ceSNicholas Bellinger 			if ((tpg->tpg_state == TPG_STATE_FREE) ||
3274e48354ceSNicholas Bellinger 			    (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3275e48354ceSNicholas Bellinger 				spin_unlock(&tpg->tpg_state_lock);
3276e48354ceSNicholas Bellinger 				continue;
3277e48354ceSNicholas Bellinger 			}
3278e48354ceSNicholas Bellinger 			spin_unlock(&tpg->tpg_state_lock);
3279e48354ceSNicholas Bellinger 
3280e48354ceSNicholas Bellinger 			spin_lock(&tpg->tpg_np_lock);
3281e48354ceSNicholas Bellinger 			list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3282e48354ceSNicholas Bellinger 						tpg_np_list) {
32832f9bc894SNicholas Bellinger 				struct iscsi_np *np = tpg_np->tpg_np;
32842f9bc894SNicholas Bellinger 				bool inaddr_any = iscsit_check_inaddr_any(np);
32852f9bc894SNicholas Bellinger 
3286e48354ceSNicholas Bellinger 				len = sprintf(buf, "TargetAddress="
3287e48354ceSNicholas Bellinger 					"%s%s%s:%hu,%hu",
32882f9bc894SNicholas Bellinger 					(np->np_sockaddr.ss_family == AF_INET6) ?
32892f9bc894SNicholas Bellinger 					"[" : "", (inaddr_any == false) ?
32902f9bc894SNicholas Bellinger 						np->np_ip : conn->local_ip,
32912f9bc894SNicholas Bellinger 					(np->np_sockaddr.ss_family == AF_INET6) ?
32922f9bc894SNicholas Bellinger 					"]" : "", (inaddr_any == false) ?
32932f9bc894SNicholas Bellinger 						np->np_port : conn->local_port,
3294e48354ceSNicholas Bellinger 					tpg->tpgt);
3295e48354ceSNicholas Bellinger 				len += 1;
3296e48354ceSNicholas Bellinger 
3297e48354ceSNicholas Bellinger 				if ((len + payload_len) > buffer_len) {
3298e48354ceSNicholas Bellinger 					spin_unlock(&tpg->tpg_np_lock);
3299e48354ceSNicholas Bellinger 					spin_unlock(&tiqn->tiqn_tpg_lock);
3300e48354ceSNicholas Bellinger 					end_of_buf = 1;
3301e48354ceSNicholas Bellinger 					goto eob;
3302e48354ceSNicholas Bellinger 				}
33038359cf43SJörn Engel 				memcpy(payload + payload_len, buf, len);
3304e48354ceSNicholas Bellinger 				payload_len += len;
3305e48354ceSNicholas Bellinger 			}
3306e48354ceSNicholas Bellinger 			spin_unlock(&tpg->tpg_np_lock);
3307e48354ceSNicholas Bellinger 		}
3308e48354ceSNicholas Bellinger 		spin_unlock(&tiqn->tiqn_tpg_lock);
3309e48354ceSNicholas Bellinger eob:
3310e48354ceSNicholas Bellinger 		if (end_of_buf)
3311e48354ceSNicholas Bellinger 			break;
3312e48354ceSNicholas Bellinger 	}
3313e48354ceSNicholas Bellinger 	spin_unlock(&tiqn_lock);
3314e48354ceSNicholas Bellinger 
3315e48354ceSNicholas Bellinger 	cmd->buf_ptr = payload;
3316e48354ceSNicholas Bellinger 
3317e48354ceSNicholas Bellinger 	return payload_len;
3318e48354ceSNicholas Bellinger }
3319e48354ceSNicholas Bellinger 
3320e48354ceSNicholas Bellinger /*
3321e48354ceSNicholas Bellinger  *	FIXME: Add support for F_BIT and C_BIT when the length is longer than
3322e48354ceSNicholas Bellinger  *	MaxRecvDataSegmentLength.
3323e48354ceSNicholas Bellinger  */
3324e48354ceSNicholas Bellinger static int iscsit_send_text_rsp(
3325e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
3326e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
3327e48354ceSNicholas Bellinger {
3328e48354ceSNicholas Bellinger 	struct iscsi_text_rsp *hdr;
3329e48354ceSNicholas Bellinger 	struct kvec *iov;
3330e48354ceSNicholas Bellinger 	u32 padding = 0, tx_size = 0;
3331e48354ceSNicholas Bellinger 	int text_length, iov_count = 0;
3332e48354ceSNicholas Bellinger 
3333e48354ceSNicholas Bellinger 	text_length = iscsit_build_sendtargets_response(cmd);
3334e48354ceSNicholas Bellinger 	if (text_length < 0)
3335e48354ceSNicholas Bellinger 		return text_length;
3336e48354ceSNicholas Bellinger 
3337e48354ceSNicholas Bellinger 	padding = ((-text_length) & 3);
3338e48354ceSNicholas Bellinger 	if (padding != 0) {
3339e48354ceSNicholas Bellinger 		memset(cmd->buf_ptr + text_length, 0, padding);
3340e48354ceSNicholas Bellinger 		pr_debug("Attaching %u additional bytes for"
3341e48354ceSNicholas Bellinger 			" padding.\n", padding);
3342e48354ceSNicholas Bellinger 	}
3343e48354ceSNicholas Bellinger 
3344e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_text_rsp *) cmd->pdu;
3345e48354ceSNicholas Bellinger 	memset(hdr, 0, ISCSI_HDR_LEN);
3346e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_TEXT_RSP;
3347e48354ceSNicholas Bellinger 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
3348e48354ceSNicholas Bellinger 	hton24(hdr->dlength, text_length);
334966c7db68SChristoph Hellwig 	hdr->itt		= cmd->init_task_tag;
3350e48354ceSNicholas Bellinger 	hdr->ttt		= cpu_to_be32(cmd->targ_xfer_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);
3356e48354ceSNicholas Bellinger 	hdr->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn);
3357e48354ceSNicholas Bellinger 
3358e48354ceSNicholas Bellinger 	iov = &cmd->iov_misc[0];
3359e48354ceSNicholas Bellinger 
3360e48354ceSNicholas Bellinger 	iov[iov_count].iov_base = cmd->pdu;
3361e48354ceSNicholas Bellinger 	iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3362e48354ceSNicholas Bellinger 	iov[iov_count].iov_base	= cmd->buf_ptr;
3363e48354ceSNicholas Bellinger 	iov[iov_count++].iov_len = text_length + padding;
3364e48354ceSNicholas Bellinger 
3365e48354ceSNicholas Bellinger 	tx_size += (ISCSI_HDR_LEN + text_length + padding);
3366e48354ceSNicholas Bellinger 
3367e48354ceSNicholas Bellinger 	if (conn->conn_ops->HeaderDigest) {
3368e48354ceSNicholas Bellinger 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3369e48354ceSNicholas Bellinger 
3370e48354ceSNicholas Bellinger 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3371e48354ceSNicholas Bellinger 				(unsigned char *)hdr, ISCSI_HDR_LEN,
3372e48354ceSNicholas Bellinger 				0, NULL, (u8 *)header_digest);
3373e48354ceSNicholas Bellinger 
3374e48354ceSNicholas Bellinger 		iov[0].iov_len += ISCSI_CRC_LEN;
3375e48354ceSNicholas Bellinger 		tx_size += ISCSI_CRC_LEN;
3376e48354ceSNicholas Bellinger 		pr_debug("Attaching CRC32 HeaderDigest for"
3377e48354ceSNicholas Bellinger 			" Text Response PDU 0x%08x\n", *header_digest);
3378e48354ceSNicholas Bellinger 	}
3379e48354ceSNicholas Bellinger 
3380e48354ceSNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
3381e48354ceSNicholas Bellinger 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3382e48354ceSNicholas Bellinger 				cmd->buf_ptr, (text_length + padding),
3383e48354ceSNicholas Bellinger 				0, NULL, (u8 *)&cmd->data_crc);
3384e48354ceSNicholas Bellinger 
3385e48354ceSNicholas Bellinger 		iov[iov_count].iov_base	= &cmd->data_crc;
3386e48354ceSNicholas Bellinger 		iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3387e48354ceSNicholas Bellinger 		tx_size	+= ISCSI_CRC_LEN;
3388e48354ceSNicholas Bellinger 
3389e48354ceSNicholas Bellinger 		pr_debug("Attaching DataDigest for %u bytes of text"
3390e48354ceSNicholas Bellinger 			" data, CRC 0x%08x\n", (text_length + padding),
3391e48354ceSNicholas Bellinger 			cmd->data_crc);
3392e48354ceSNicholas Bellinger 	}
3393e48354ceSNicholas Bellinger 
3394e48354ceSNicholas Bellinger 	cmd->iov_misc_count = iov_count;
3395e48354ceSNicholas Bellinger 	cmd->tx_size = tx_size;
3396e48354ceSNicholas Bellinger 
3397e48354ceSNicholas Bellinger 	pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3398e48354ceSNicholas Bellinger 		" Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3399e48354ceSNicholas Bellinger 			text_length, conn->cid);
3400e48354ceSNicholas Bellinger 	return 0;
3401e48354ceSNicholas Bellinger }
3402e48354ceSNicholas Bellinger 
3403e48354ceSNicholas Bellinger static int iscsit_send_reject(
3404e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
3405e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
3406e48354ceSNicholas Bellinger {
3407e48354ceSNicholas Bellinger 	u32 iov_count = 0, tx_size = 0;
3408e48354ceSNicholas Bellinger 	struct iscsi_reject *hdr;
3409e48354ceSNicholas Bellinger 	struct kvec *iov;
3410e48354ceSNicholas Bellinger 
3411e48354ceSNicholas Bellinger 	hdr			= (struct iscsi_reject *) cmd->pdu;
3412e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_REJECT;
3413e48354ceSNicholas Bellinger 	hdr->flags		|= ISCSI_FLAG_CMD_FINAL;
3414e48354ceSNicholas Bellinger 	hton24(hdr->dlength, ISCSI_HDR_LEN);
341550e5c87dSChristoph Hellwig 	hdr->ffffffff		= cpu_to_be32(0xffffffff);
3416e48354ceSNicholas Bellinger 	cmd->stat_sn		= conn->stat_sn++;
3417e48354ceSNicholas Bellinger 	hdr->statsn		= cpu_to_be32(cmd->stat_sn);
3418e48354ceSNicholas Bellinger 	hdr->exp_cmdsn	= cpu_to_be32(conn->sess->exp_cmd_sn);
3419e48354ceSNicholas Bellinger 	hdr->max_cmdsn	= cpu_to_be32(conn->sess->max_cmd_sn);
3420e48354ceSNicholas Bellinger 
3421e48354ceSNicholas Bellinger 	iov = &cmd->iov_misc[0];
3422e48354ceSNicholas Bellinger 
3423e48354ceSNicholas Bellinger 	iov[iov_count].iov_base = cmd->pdu;
3424e48354ceSNicholas Bellinger 	iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3425e48354ceSNicholas Bellinger 	iov[iov_count].iov_base = cmd->buf_ptr;
3426e48354ceSNicholas Bellinger 	iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3427e48354ceSNicholas Bellinger 
3428e48354ceSNicholas Bellinger 	tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3429e48354ceSNicholas Bellinger 
3430e48354ceSNicholas Bellinger 	if (conn->conn_ops->HeaderDigest) {
3431e48354ceSNicholas Bellinger 		u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3432e48354ceSNicholas Bellinger 
3433e48354ceSNicholas Bellinger 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3434e48354ceSNicholas Bellinger 				(unsigned char *)hdr, ISCSI_HDR_LEN,
3435e48354ceSNicholas Bellinger 				0, NULL, (u8 *)header_digest);
3436e48354ceSNicholas Bellinger 
3437e48354ceSNicholas Bellinger 		iov[0].iov_len += ISCSI_CRC_LEN;
3438e48354ceSNicholas Bellinger 		tx_size += ISCSI_CRC_LEN;
3439e48354ceSNicholas Bellinger 		pr_debug("Attaching CRC32 HeaderDigest for"
3440e48354ceSNicholas Bellinger 			" REJECT PDU 0x%08x\n", *header_digest);
3441e48354ceSNicholas Bellinger 	}
3442e48354ceSNicholas Bellinger 
3443e48354ceSNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
3444e48354ceSNicholas Bellinger 		iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3445e48354ceSNicholas Bellinger 				(unsigned char *)cmd->buf_ptr, ISCSI_HDR_LEN,
3446e48354ceSNicholas Bellinger 				0, NULL, (u8 *)&cmd->data_crc);
3447e48354ceSNicholas Bellinger 
3448e48354ceSNicholas Bellinger 		iov[iov_count].iov_base = &cmd->data_crc;
3449e48354ceSNicholas Bellinger 		iov[iov_count++].iov_len  = ISCSI_CRC_LEN;
3450e48354ceSNicholas Bellinger 		tx_size += ISCSI_CRC_LEN;
3451e48354ceSNicholas Bellinger 		pr_debug("Attaching CRC32 DataDigest for REJECT"
3452e48354ceSNicholas Bellinger 				" PDU 0x%08x\n", cmd->data_crc);
3453e48354ceSNicholas Bellinger 	}
3454e48354ceSNicholas Bellinger 
3455e48354ceSNicholas Bellinger 	cmd->iov_misc_count = iov_count;
3456e48354ceSNicholas Bellinger 	cmd->tx_size = tx_size;
3457e48354ceSNicholas Bellinger 
3458e48354ceSNicholas Bellinger 	pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3459e48354ceSNicholas Bellinger 		" CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3460e48354ceSNicholas Bellinger 
3461e48354ceSNicholas Bellinger 	return 0;
3462e48354ceSNicholas Bellinger }
3463e48354ceSNicholas Bellinger 
3464e48354ceSNicholas Bellinger void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3465e48354ceSNicholas Bellinger {
3466e48354ceSNicholas Bellinger 	struct iscsi_thread_set *ts = conn->thread_set;
3467e48354ceSNicholas Bellinger 	int ord, cpu;
3468e48354ceSNicholas Bellinger 	/*
3469e48354ceSNicholas Bellinger 	 * thread_id is assigned from iscsit_global->ts_bitmap from
3470e48354ceSNicholas Bellinger 	 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3471e48354ceSNicholas Bellinger 	 *
3472e48354ceSNicholas Bellinger 	 * Here we use thread_id to determine which CPU that this
3473e48354ceSNicholas Bellinger 	 * iSCSI connection's iscsi_thread_set will be scheduled to
3474e48354ceSNicholas Bellinger 	 * execute upon.
3475e48354ceSNicholas Bellinger 	 */
3476e48354ceSNicholas Bellinger 	ord = ts->thread_id % cpumask_weight(cpu_online_mask);
3477e48354ceSNicholas Bellinger 	for_each_online_cpu(cpu) {
3478e48354ceSNicholas Bellinger 		if (ord-- == 0) {
3479e48354ceSNicholas Bellinger 			cpumask_set_cpu(cpu, conn->conn_cpumask);
3480e48354ceSNicholas Bellinger 			return;
3481e48354ceSNicholas Bellinger 		}
3482e48354ceSNicholas Bellinger 	}
3483e48354ceSNicholas Bellinger 	/*
3484e48354ceSNicholas Bellinger 	 * This should never be reached..
3485e48354ceSNicholas Bellinger 	 */
3486e48354ceSNicholas Bellinger 	dump_stack();
3487e48354ceSNicholas Bellinger 	cpumask_setall(conn->conn_cpumask);
3488e48354ceSNicholas Bellinger }
3489e48354ceSNicholas Bellinger 
3490e48354ceSNicholas Bellinger static inline void iscsit_thread_check_cpumask(
3491e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
3492e48354ceSNicholas Bellinger 	struct task_struct *p,
3493e48354ceSNicholas Bellinger 	int mode)
3494e48354ceSNicholas Bellinger {
3495e48354ceSNicholas Bellinger 	char buf[128];
3496e48354ceSNicholas Bellinger 	/*
3497e48354ceSNicholas Bellinger 	 * mode == 1 signals iscsi_target_tx_thread() usage.
3498e48354ceSNicholas Bellinger 	 * mode == 0 signals iscsi_target_rx_thread() usage.
3499e48354ceSNicholas Bellinger 	 */
3500e48354ceSNicholas Bellinger 	if (mode == 1) {
3501e48354ceSNicholas Bellinger 		if (!conn->conn_tx_reset_cpumask)
3502e48354ceSNicholas Bellinger 			return;
3503e48354ceSNicholas Bellinger 		conn->conn_tx_reset_cpumask = 0;
3504e48354ceSNicholas Bellinger 	} else {
3505e48354ceSNicholas Bellinger 		if (!conn->conn_rx_reset_cpumask)
3506e48354ceSNicholas Bellinger 			return;
3507e48354ceSNicholas Bellinger 		conn->conn_rx_reset_cpumask = 0;
3508e48354ceSNicholas Bellinger 	}
3509e48354ceSNicholas Bellinger 	/*
3510e48354ceSNicholas Bellinger 	 * Update the CPU mask for this single kthread so that
3511e48354ceSNicholas Bellinger 	 * both TX and RX kthreads are scheduled to run on the
3512e48354ceSNicholas Bellinger 	 * same CPU.
3513e48354ceSNicholas Bellinger 	 */
3514e48354ceSNicholas Bellinger 	memset(buf, 0, 128);
3515e48354ceSNicholas Bellinger 	cpumask_scnprintf(buf, 128, conn->conn_cpumask);
3516e48354ceSNicholas Bellinger 	set_cpus_allowed_ptr(p, conn->conn_cpumask);
3517e48354ceSNicholas Bellinger }
3518e48354ceSNicholas Bellinger 
35196f3c0e69SAndy Grover static int handle_immediate_queue(struct iscsi_conn *conn)
3520e48354ceSNicholas Bellinger {
35216f3c0e69SAndy Grover 	struct iscsi_queue_req *qr;
35226f3c0e69SAndy Grover 	struct iscsi_cmd *cmd;
3523e48354ceSNicholas Bellinger 	u8 state;
35246f3c0e69SAndy Grover 	int ret;
3525e48354ceSNicholas Bellinger 
3526c6037cc5SAndy Grover 	while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3527e48354ceSNicholas Bellinger 		atomic_set(&conn->check_immediate_queue, 0);
3528e48354ceSNicholas Bellinger 		cmd = qr->cmd;
3529e48354ceSNicholas Bellinger 		state = qr->state;
3530e48354ceSNicholas Bellinger 		kmem_cache_free(lio_qr_cache, qr);
3531e48354ceSNicholas Bellinger 
3532e48354ceSNicholas Bellinger 		switch (state) {
3533e48354ceSNicholas Bellinger 		case ISTATE_SEND_R2T:
3534e48354ceSNicholas Bellinger 			ret = iscsit_send_r2t(cmd, conn);
35356f3c0e69SAndy Grover 			if (ret < 0)
35366f3c0e69SAndy Grover 				goto err;
3537e48354ceSNicholas Bellinger 			break;
3538e48354ceSNicholas Bellinger 		case ISTATE_REMOVE:
3539e48354ceSNicholas Bellinger 			if (cmd->data_direction == DMA_TO_DEVICE)
3540e48354ceSNicholas Bellinger 				iscsit_stop_dataout_timer(cmd);
3541e48354ceSNicholas Bellinger 
3542e48354ceSNicholas Bellinger 			spin_lock_bh(&conn->cmd_lock);
35432fbb471eSAndy Grover 			list_del(&cmd->i_conn_node);
3544e48354ceSNicholas Bellinger 			spin_unlock_bh(&conn->cmd_lock);
3545d270190aSNicholas Bellinger 
3546d270190aSNicholas Bellinger 			iscsit_free_cmd(cmd);
3547c6037cc5SAndy Grover 			continue;
3548e48354ceSNicholas Bellinger 		case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3549e48354ceSNicholas Bellinger 			iscsit_mod_nopin_response_timer(conn);
3550e48354ceSNicholas Bellinger 			ret = iscsit_send_unsolicited_nopin(cmd,
3551e48354ceSNicholas Bellinger 							    conn, 1);
35526f3c0e69SAndy Grover 			if (ret < 0)
35536f3c0e69SAndy Grover 				goto err;
3554e48354ceSNicholas Bellinger 			break;
3555e48354ceSNicholas Bellinger 		case ISTATE_SEND_NOPIN_NO_RESPONSE:
3556e48354ceSNicholas Bellinger 			ret = iscsit_send_unsolicited_nopin(cmd,
3557e48354ceSNicholas Bellinger 							    conn, 0);
35586f3c0e69SAndy Grover 			if (ret < 0)
35596f3c0e69SAndy Grover 				goto err;
3560e48354ceSNicholas Bellinger 			break;
3561e48354ceSNicholas Bellinger 		default:
3562e48354ceSNicholas Bellinger 			pr_err("Unknown Opcode: 0x%02x ITT:"
3563e48354ceSNicholas Bellinger 			       " 0x%08x, i_state: %d on CID: %hu\n",
3564e48354ceSNicholas Bellinger 			       cmd->iscsi_opcode, cmd->init_task_tag, state,
3565e48354ceSNicholas Bellinger 			       conn->cid);
35666f3c0e69SAndy Grover 			goto err;
3567e48354ceSNicholas Bellinger 		}
3568e48354ceSNicholas Bellinger 	}
3569e48354ceSNicholas Bellinger 
35706f3c0e69SAndy Grover 	return 0;
35716f3c0e69SAndy Grover 
35726f3c0e69SAndy Grover err:
35736f3c0e69SAndy Grover 	return -1;
3574e48354ceSNicholas Bellinger }
35756f3c0e69SAndy Grover 
35766f3c0e69SAndy Grover static int handle_response_queue(struct iscsi_conn *conn)
35776f3c0e69SAndy Grover {
35786f3c0e69SAndy Grover 	struct iscsi_queue_req *qr;
35796f3c0e69SAndy Grover 	struct iscsi_cmd *cmd;
35806f3c0e69SAndy Grover 	u8 state;
35816f3c0e69SAndy Grover 	int ret;
3582e48354ceSNicholas Bellinger 
3583c6037cc5SAndy Grover 	while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3584e48354ceSNicholas Bellinger 		cmd = qr->cmd;
3585e48354ceSNicholas Bellinger 		state = qr->state;
3586e48354ceSNicholas Bellinger 		kmem_cache_free(lio_qr_cache, qr);
3587e48354ceSNicholas Bellinger 
3588e48354ceSNicholas Bellinger check_rsp_state:
3589e48354ceSNicholas Bellinger 		switch (state) {
3590e48354ceSNicholas Bellinger 		case ISTATE_SEND_DATAIN:
35916f3c0e69SAndy Grover 			ret = iscsit_send_data_in(cmd, conn);
35926f3c0e69SAndy Grover 			if (ret < 0)
35936f3c0e69SAndy Grover 				goto err;
35946f3c0e69SAndy Grover 			else if (!ret)
35956f3c0e69SAndy Grover 				/* more drs */
35966f3c0e69SAndy Grover 				goto check_rsp_state;
35976f3c0e69SAndy Grover 			else if (ret == 1) {
35986f3c0e69SAndy Grover 				/* all done */
35996f3c0e69SAndy Grover 				spin_lock_bh(&cmd->istate_lock);
36006f3c0e69SAndy Grover 				cmd->i_state = ISTATE_SENT_STATUS;
3601e48354ceSNicholas Bellinger 				spin_unlock_bh(&cmd->istate_lock);
3602fd3a9025SNicholas Bellinger 
3603fd3a9025SNicholas Bellinger 				if (atomic_read(&conn->check_immediate_queue))
3604fd3a9025SNicholas Bellinger 					return 1;
3605fd3a9025SNicholas Bellinger 
36066f3c0e69SAndy Grover 				continue;
36076f3c0e69SAndy Grover 			} else if (ret == 2) {
36086f3c0e69SAndy Grover 				/* Still must send status,
36096f3c0e69SAndy Grover 				   SCF_TRANSPORT_TASK_SENSE was set */
36106f3c0e69SAndy Grover 				spin_lock_bh(&cmd->istate_lock);
36116f3c0e69SAndy Grover 				cmd->i_state = ISTATE_SEND_STATUS;
36126f3c0e69SAndy Grover 				spin_unlock_bh(&cmd->istate_lock);
36136f3c0e69SAndy Grover 				state = ISTATE_SEND_STATUS;
36146f3c0e69SAndy Grover 				goto check_rsp_state;
36156f3c0e69SAndy Grover 			}
36166f3c0e69SAndy Grover 
3617e48354ceSNicholas Bellinger 			break;
3618e48354ceSNicholas Bellinger 		case ISTATE_SEND_STATUS:
3619e48354ceSNicholas Bellinger 		case ISTATE_SEND_STATUS_RECOVERY:
3620e48354ceSNicholas Bellinger 			ret = iscsit_send_status(cmd, conn);
3621e48354ceSNicholas Bellinger 			break;
3622e48354ceSNicholas Bellinger 		case ISTATE_SEND_LOGOUTRSP:
3623e48354ceSNicholas Bellinger 			ret = iscsit_send_logout_response(cmd, conn);
3624e48354ceSNicholas Bellinger 			break;
3625e48354ceSNicholas Bellinger 		case ISTATE_SEND_ASYNCMSG:
3626e48354ceSNicholas Bellinger 			ret = iscsit_send_conn_drop_async_message(
3627e48354ceSNicholas Bellinger 				cmd, conn);
3628e48354ceSNicholas Bellinger 			break;
3629e48354ceSNicholas Bellinger 		case ISTATE_SEND_NOPIN:
3630e48354ceSNicholas Bellinger 			ret = iscsit_send_nopin_response(cmd, conn);
3631e48354ceSNicholas Bellinger 			break;
3632e48354ceSNicholas Bellinger 		case ISTATE_SEND_REJECT:
3633e48354ceSNicholas Bellinger 			ret = iscsit_send_reject(cmd, conn);
3634e48354ceSNicholas Bellinger 			break;
3635e48354ceSNicholas Bellinger 		case ISTATE_SEND_TASKMGTRSP:
3636e48354ceSNicholas Bellinger 			ret = iscsit_send_task_mgt_rsp(cmd, conn);
3637e48354ceSNicholas Bellinger 			if (ret != 0)
3638e48354ceSNicholas Bellinger 				break;
3639e48354ceSNicholas Bellinger 			ret = iscsit_tmr_post_handler(cmd, conn);
3640e48354ceSNicholas Bellinger 			if (ret != 0)
3641e48354ceSNicholas Bellinger 				iscsit_fall_back_to_erl0(conn->sess);
3642e48354ceSNicholas Bellinger 			break;
3643e48354ceSNicholas Bellinger 		case ISTATE_SEND_TEXTRSP:
3644e48354ceSNicholas Bellinger 			ret = iscsit_send_text_rsp(cmd, conn);
3645e48354ceSNicholas Bellinger 			break;
3646e48354ceSNicholas Bellinger 		default:
3647e48354ceSNicholas Bellinger 			pr_err("Unknown Opcode: 0x%02x ITT:"
3648e48354ceSNicholas Bellinger 			       " 0x%08x, i_state: %d on CID: %hu\n",
3649e48354ceSNicholas Bellinger 			       cmd->iscsi_opcode, cmd->init_task_tag,
3650e48354ceSNicholas Bellinger 			       state, conn->cid);
36516f3c0e69SAndy Grover 			goto err;
3652e48354ceSNicholas Bellinger 		}
3653c6037cc5SAndy Grover 		if (ret < 0)
36546f3c0e69SAndy Grover 			goto err;
3655e48354ceSNicholas Bellinger 
36566f3c0e69SAndy Grover 		if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3657e48354ceSNicholas Bellinger 			iscsit_tx_thread_wait_for_tcp(conn);
3658e48354ceSNicholas Bellinger 			iscsit_unmap_iovec(cmd);
36596f3c0e69SAndy Grover 			goto err;
3660e48354ceSNicholas Bellinger 		}
3661e48354ceSNicholas Bellinger 		iscsit_unmap_iovec(cmd);
3662e48354ceSNicholas Bellinger 
3663e48354ceSNicholas Bellinger 		switch (state) {
36646f3c0e69SAndy Grover 		case ISTATE_SEND_LOGOUTRSP:
36656f3c0e69SAndy Grover 			if (!iscsit_logout_post_handler(cmd, conn))
36666f3c0e69SAndy Grover 				goto restart;
36676f3c0e69SAndy Grover 			/* fall through */
3668e48354ceSNicholas Bellinger 		case ISTATE_SEND_STATUS:
3669e48354ceSNicholas Bellinger 		case ISTATE_SEND_ASYNCMSG:
3670e48354ceSNicholas Bellinger 		case ISTATE_SEND_NOPIN:
3671e48354ceSNicholas Bellinger 		case ISTATE_SEND_STATUS_RECOVERY:
3672e48354ceSNicholas Bellinger 		case ISTATE_SEND_TEXTRSP:
36736f3c0e69SAndy Grover 		case ISTATE_SEND_TASKMGTRSP:
36746f3c0e69SAndy Grover 			spin_lock_bh(&cmd->istate_lock);
36756f3c0e69SAndy Grover 			cmd->i_state = ISTATE_SENT_STATUS;
36766f3c0e69SAndy Grover 			spin_unlock_bh(&cmd->istate_lock);
3677e48354ceSNicholas Bellinger 			break;
3678e48354ceSNicholas Bellinger 		case ISTATE_SEND_REJECT:
3679e48354ceSNicholas Bellinger 			if (cmd->cmd_flags & ICF_REJECT_FAIL_CONN) {
3680e48354ceSNicholas Bellinger 				cmd->cmd_flags &= ~ICF_REJECT_FAIL_CONN;
3681e48354ceSNicholas Bellinger 				complete(&cmd->reject_comp);
36826f3c0e69SAndy Grover 				goto err;
3683e48354ceSNicholas Bellinger 			}
3684e48354ceSNicholas Bellinger 			complete(&cmd->reject_comp);
3685e48354ceSNicholas Bellinger 			break;
3686e48354ceSNicholas Bellinger 		default:
3687e48354ceSNicholas Bellinger 			pr_err("Unknown Opcode: 0x%02x ITT:"
3688e48354ceSNicholas Bellinger 			       " 0x%08x, i_state: %d on CID: %hu\n",
3689e48354ceSNicholas Bellinger 			       cmd->iscsi_opcode, cmd->init_task_tag,
3690e48354ceSNicholas Bellinger 			       cmd->i_state, conn->cid);
36916f3c0e69SAndy Grover 			goto err;
3692e48354ceSNicholas Bellinger 		}
3693e48354ceSNicholas Bellinger 
3694e48354ceSNicholas Bellinger 		if (atomic_read(&conn->check_immediate_queue))
3695fd3a9025SNicholas Bellinger 			return 1;
3696c6037cc5SAndy Grover 	}
36976f3c0e69SAndy Grover 
36986f3c0e69SAndy Grover 	return 0;
36996f3c0e69SAndy Grover 
37006f3c0e69SAndy Grover err:
37016f3c0e69SAndy Grover 	return -1;
37026f3c0e69SAndy Grover restart:
37036f3c0e69SAndy Grover 	return -EAGAIN;
37046f3c0e69SAndy Grover }
37056f3c0e69SAndy Grover 
37066f3c0e69SAndy Grover int iscsi_target_tx_thread(void *arg)
37076f3c0e69SAndy Grover {
37086f3c0e69SAndy Grover 	int ret = 0;
37096f3c0e69SAndy Grover 	struct iscsi_conn *conn;
37106f3c0e69SAndy Grover 	struct iscsi_thread_set *ts = arg;
37116f3c0e69SAndy Grover 	/*
37126f3c0e69SAndy Grover 	 * Allow ourselves to be interrupted by SIGINT so that a
37136f3c0e69SAndy Grover 	 * connection recovery / failure event can be triggered externally.
37146f3c0e69SAndy Grover 	 */
37156f3c0e69SAndy Grover 	allow_signal(SIGINT);
37166f3c0e69SAndy Grover 
37176f3c0e69SAndy Grover restart:
37186f3c0e69SAndy Grover 	conn = iscsi_tx_thread_pre_handler(ts);
37196f3c0e69SAndy Grover 	if (!conn)
37206f3c0e69SAndy Grover 		goto out;
37216f3c0e69SAndy Grover 
37226f3c0e69SAndy Grover 	ret = 0;
37236f3c0e69SAndy Grover 
37246f3c0e69SAndy Grover 	while (!kthread_should_stop()) {
37256f3c0e69SAndy Grover 		/*
37266f3c0e69SAndy Grover 		 * Ensure that both TX and RX per connection kthreads
37276f3c0e69SAndy Grover 		 * are scheduled to run on the same CPU.
37286f3c0e69SAndy Grover 		 */
37296f3c0e69SAndy Grover 		iscsit_thread_check_cpumask(conn, current, 1);
37306f3c0e69SAndy Grover 
3731d5627acbSRoland Dreier 		wait_event_interruptible(conn->queues_wq,
3732d5627acbSRoland Dreier 					 !iscsit_conn_all_queues_empty(conn) ||
3733d5627acbSRoland Dreier 					 ts->status == ISCSI_THREAD_SET_RESET);
37346f3c0e69SAndy Grover 
37356f3c0e69SAndy Grover 		if ((ts->status == ISCSI_THREAD_SET_RESET) ||
37366f3c0e69SAndy Grover 		     signal_pending(current))
37376f3c0e69SAndy Grover 			goto transport_err;
37386f3c0e69SAndy Grover 
3739fd3a9025SNicholas Bellinger get_immediate:
37406f3c0e69SAndy Grover 		ret = handle_immediate_queue(conn);
37416f3c0e69SAndy Grover 		if (ret < 0)
37426f3c0e69SAndy Grover 			goto transport_err;
37436f3c0e69SAndy Grover 
37446f3c0e69SAndy Grover 		ret = handle_response_queue(conn);
3745fd3a9025SNicholas Bellinger 		if (ret == 1)
3746fd3a9025SNicholas Bellinger 			goto get_immediate;
3747fd3a9025SNicholas Bellinger 		else if (ret == -EAGAIN)
37486f3c0e69SAndy Grover 			goto restart;
37496f3c0e69SAndy Grover 		else if (ret < 0)
37506f3c0e69SAndy Grover 			goto transport_err;
3751e48354ceSNicholas Bellinger 	}
3752e48354ceSNicholas Bellinger 
3753e48354ceSNicholas Bellinger transport_err:
3754e48354ceSNicholas Bellinger 	iscsit_take_action_for_connection_exit(conn);
3755e48354ceSNicholas Bellinger 	goto restart;
3756e48354ceSNicholas Bellinger out:
3757e48354ceSNicholas Bellinger 	return 0;
3758e48354ceSNicholas Bellinger }
3759e48354ceSNicholas Bellinger 
3760e48354ceSNicholas Bellinger int iscsi_target_rx_thread(void *arg)
3761e48354ceSNicholas Bellinger {
3762e48354ceSNicholas Bellinger 	int ret;
3763e48354ceSNicholas Bellinger 	u8 buffer[ISCSI_HDR_LEN], opcode;
3764e48354ceSNicholas Bellinger 	u32 checksum = 0, digest = 0;
3765e48354ceSNicholas Bellinger 	struct iscsi_conn *conn = NULL;
37668359cf43SJörn Engel 	struct iscsi_thread_set *ts = arg;
3767e48354ceSNicholas Bellinger 	struct kvec iov;
3768e48354ceSNicholas Bellinger 	/*
3769e48354ceSNicholas Bellinger 	 * Allow ourselves to be interrupted by SIGINT so that a
3770e48354ceSNicholas Bellinger 	 * connection recovery / failure event can be triggered externally.
3771e48354ceSNicholas Bellinger 	 */
3772e48354ceSNicholas Bellinger 	allow_signal(SIGINT);
3773e48354ceSNicholas Bellinger 
3774e48354ceSNicholas Bellinger restart:
3775e48354ceSNicholas Bellinger 	conn = iscsi_rx_thread_pre_handler(ts);
3776e48354ceSNicholas Bellinger 	if (!conn)
3777e48354ceSNicholas Bellinger 		goto out;
3778e48354ceSNicholas Bellinger 
3779e48354ceSNicholas Bellinger 	while (!kthread_should_stop()) {
3780e48354ceSNicholas Bellinger 		/*
3781e48354ceSNicholas Bellinger 		 * Ensure that both TX and RX per connection kthreads
3782e48354ceSNicholas Bellinger 		 * are scheduled to run on the same CPU.
3783e48354ceSNicholas Bellinger 		 */
3784e48354ceSNicholas Bellinger 		iscsit_thread_check_cpumask(conn, current, 0);
3785e48354ceSNicholas Bellinger 
3786e48354ceSNicholas Bellinger 		memset(buffer, 0, ISCSI_HDR_LEN);
3787e48354ceSNicholas Bellinger 		memset(&iov, 0, sizeof(struct kvec));
3788e48354ceSNicholas Bellinger 
3789e48354ceSNicholas Bellinger 		iov.iov_base	= buffer;
3790e48354ceSNicholas Bellinger 		iov.iov_len	= ISCSI_HDR_LEN;
3791e48354ceSNicholas Bellinger 
3792e48354ceSNicholas Bellinger 		ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3793e48354ceSNicholas Bellinger 		if (ret != ISCSI_HDR_LEN) {
3794e48354ceSNicholas Bellinger 			iscsit_rx_thread_wait_for_tcp(conn);
3795e48354ceSNicholas Bellinger 			goto transport_err;
3796e48354ceSNicholas Bellinger 		}
3797e48354ceSNicholas Bellinger 
3798e48354ceSNicholas Bellinger 		/*
3799e48354ceSNicholas Bellinger 		 * Set conn->bad_hdr for use with REJECT PDUs.
3800e48354ceSNicholas Bellinger 		 */
3801e48354ceSNicholas Bellinger 		memcpy(&conn->bad_hdr, &buffer, ISCSI_HDR_LEN);
3802e48354ceSNicholas Bellinger 
3803e48354ceSNicholas Bellinger 		if (conn->conn_ops->HeaderDigest) {
3804e48354ceSNicholas Bellinger 			iov.iov_base	= &digest;
3805e48354ceSNicholas Bellinger 			iov.iov_len	= ISCSI_CRC_LEN;
3806e48354ceSNicholas Bellinger 
3807e48354ceSNicholas Bellinger 			ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3808e48354ceSNicholas Bellinger 			if (ret != ISCSI_CRC_LEN) {
3809e48354ceSNicholas Bellinger 				iscsit_rx_thread_wait_for_tcp(conn);
3810e48354ceSNicholas Bellinger 				goto transport_err;
3811e48354ceSNicholas Bellinger 			}
3812e48354ceSNicholas Bellinger 
3813e48354ceSNicholas Bellinger 			iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
3814e48354ceSNicholas Bellinger 					buffer, ISCSI_HDR_LEN,
3815e48354ceSNicholas Bellinger 					0, NULL, (u8 *)&checksum);
3816e48354ceSNicholas Bellinger 
3817e48354ceSNicholas Bellinger 			if (digest != checksum) {
3818e48354ceSNicholas Bellinger 				pr_err("HeaderDigest CRC32C failed,"
3819e48354ceSNicholas Bellinger 					" received 0x%08x, computed 0x%08x\n",
3820e48354ceSNicholas Bellinger 					digest, checksum);
3821e48354ceSNicholas Bellinger 				/*
3822e48354ceSNicholas Bellinger 				 * Set the PDU to 0xff so it will intentionally
3823e48354ceSNicholas Bellinger 				 * hit default in the switch below.
3824e48354ceSNicholas Bellinger 				 */
3825e48354ceSNicholas Bellinger 				memset(buffer, 0xff, ISCSI_HDR_LEN);
3826e48354ceSNicholas Bellinger 				spin_lock_bh(&conn->sess->session_stats_lock);
3827e48354ceSNicholas Bellinger 				conn->sess->conn_digest_errors++;
3828e48354ceSNicholas Bellinger 				spin_unlock_bh(&conn->sess->session_stats_lock);
3829e48354ceSNicholas Bellinger 			} else {
3830e48354ceSNicholas Bellinger 				pr_debug("Got HeaderDigest CRC32C"
3831e48354ceSNicholas Bellinger 						" 0x%08x\n", checksum);
3832e48354ceSNicholas Bellinger 			}
3833e48354ceSNicholas Bellinger 		}
3834e48354ceSNicholas Bellinger 
3835e48354ceSNicholas Bellinger 		if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
3836e48354ceSNicholas Bellinger 			goto transport_err;
3837e48354ceSNicholas Bellinger 
3838e48354ceSNicholas Bellinger 		opcode = buffer[0] & ISCSI_OPCODE_MASK;
3839e48354ceSNicholas Bellinger 
3840e48354ceSNicholas Bellinger 		if (conn->sess->sess_ops->SessionType &&
3841e48354ceSNicholas Bellinger 		   ((!(opcode & ISCSI_OP_TEXT)) ||
3842e48354ceSNicholas Bellinger 		    (!(opcode & ISCSI_OP_LOGOUT)))) {
3843e48354ceSNicholas Bellinger 			pr_err("Received illegal iSCSI Opcode: 0x%02x"
3844e48354ceSNicholas Bellinger 			" while in Discovery Session, rejecting.\n", opcode);
3845e48354ceSNicholas Bellinger 			iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
3846e48354ceSNicholas Bellinger 					buffer, conn);
3847e48354ceSNicholas Bellinger 			goto transport_err;
3848e48354ceSNicholas Bellinger 		}
3849e48354ceSNicholas Bellinger 
3850e48354ceSNicholas Bellinger 		switch (opcode) {
3851e48354ceSNicholas Bellinger 		case ISCSI_OP_SCSI_CMD:
3852e48354ceSNicholas Bellinger 			if (iscsit_handle_scsi_cmd(conn, buffer) < 0)
3853e48354ceSNicholas Bellinger 				goto transport_err;
3854e48354ceSNicholas Bellinger 			break;
3855e48354ceSNicholas Bellinger 		case ISCSI_OP_SCSI_DATA_OUT:
3856e48354ceSNicholas Bellinger 			if (iscsit_handle_data_out(conn, buffer) < 0)
3857e48354ceSNicholas Bellinger 				goto transport_err;
3858e48354ceSNicholas Bellinger 			break;
3859e48354ceSNicholas Bellinger 		case ISCSI_OP_NOOP_OUT:
3860e48354ceSNicholas Bellinger 			if (iscsit_handle_nop_out(conn, buffer) < 0)
3861e48354ceSNicholas Bellinger 				goto transport_err;
3862e48354ceSNicholas Bellinger 			break;
3863e48354ceSNicholas Bellinger 		case ISCSI_OP_SCSI_TMFUNC:
3864e48354ceSNicholas Bellinger 			if (iscsit_handle_task_mgt_cmd(conn, buffer) < 0)
3865e48354ceSNicholas Bellinger 				goto transport_err;
3866e48354ceSNicholas Bellinger 			break;
3867e48354ceSNicholas Bellinger 		case ISCSI_OP_TEXT:
3868e48354ceSNicholas Bellinger 			if (iscsit_handle_text_cmd(conn, buffer) < 0)
3869e48354ceSNicholas Bellinger 				goto transport_err;
3870e48354ceSNicholas Bellinger 			break;
3871e48354ceSNicholas Bellinger 		case ISCSI_OP_LOGOUT:
3872e48354ceSNicholas Bellinger 			ret = iscsit_handle_logout_cmd(conn, buffer);
3873e48354ceSNicholas Bellinger 			if (ret > 0) {
3874e48354ceSNicholas Bellinger 				wait_for_completion_timeout(&conn->conn_logout_comp,
3875e48354ceSNicholas Bellinger 						SECONDS_FOR_LOGOUT_COMP * HZ);
3876e48354ceSNicholas Bellinger 				goto transport_err;
3877e48354ceSNicholas Bellinger 			} else if (ret < 0)
3878e48354ceSNicholas Bellinger 				goto transport_err;
3879e48354ceSNicholas Bellinger 			break;
3880e48354ceSNicholas Bellinger 		case ISCSI_OP_SNACK:
3881e48354ceSNicholas Bellinger 			if (iscsit_handle_snack(conn, buffer) < 0)
3882e48354ceSNicholas Bellinger 				goto transport_err;
3883e48354ceSNicholas Bellinger 			break;
3884e48354ceSNicholas Bellinger 		default:
3885e48354ceSNicholas Bellinger 			pr_err("Got unknown iSCSI OpCode: 0x%02x\n",
3886e48354ceSNicholas Bellinger 					opcode);
3887e48354ceSNicholas Bellinger 			if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3888e48354ceSNicholas Bellinger 				pr_err("Cannot recover from unknown"
3889e48354ceSNicholas Bellinger 				" opcode while ERL=0, closing iSCSI connection"
3890e48354ceSNicholas Bellinger 				".\n");
3891e48354ceSNicholas Bellinger 				goto transport_err;
3892e48354ceSNicholas Bellinger 			}
3893e48354ceSNicholas Bellinger 			if (!conn->conn_ops->OFMarker) {
3894e48354ceSNicholas Bellinger 				pr_err("Unable to recover from unknown"
3895e48354ceSNicholas Bellinger 				" opcode while OFMarker=No, closing iSCSI"
3896e48354ceSNicholas Bellinger 					" connection.\n");
3897e48354ceSNicholas Bellinger 				goto transport_err;
3898e48354ceSNicholas Bellinger 			}
3899e48354ceSNicholas Bellinger 			if (iscsit_recover_from_unknown_opcode(conn) < 0) {
3900e48354ceSNicholas Bellinger 				pr_err("Unable to recover from unknown"
3901e48354ceSNicholas Bellinger 					" opcode, closing iSCSI connection.\n");
3902e48354ceSNicholas Bellinger 				goto transport_err;
3903e48354ceSNicholas Bellinger 			}
3904e48354ceSNicholas Bellinger 			break;
3905e48354ceSNicholas Bellinger 		}
3906e48354ceSNicholas Bellinger 	}
3907e48354ceSNicholas Bellinger 
3908e48354ceSNicholas Bellinger transport_err:
3909e48354ceSNicholas Bellinger 	if (!signal_pending(current))
3910e48354ceSNicholas Bellinger 		atomic_set(&conn->transport_failed, 1);
3911e48354ceSNicholas Bellinger 	iscsit_take_action_for_connection_exit(conn);
3912e48354ceSNicholas Bellinger 	goto restart;
3913e48354ceSNicholas Bellinger out:
3914e48354ceSNicholas Bellinger 	return 0;
3915e48354ceSNicholas Bellinger }
3916e48354ceSNicholas Bellinger 
3917e48354ceSNicholas Bellinger static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
3918e48354ceSNicholas Bellinger {
3919e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
3920e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
3921e48354ceSNicholas Bellinger 	/*
3922e48354ceSNicholas Bellinger 	 * We expect this function to only ever be called from either RX or TX
3923e48354ceSNicholas Bellinger 	 * thread context via iscsit_close_connection() once the other context
3924e48354ceSNicholas Bellinger 	 * has been reset -> returned sleeping pre-handler state.
3925e48354ceSNicholas Bellinger 	 */
3926e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
39272fbb471eSAndy Grover 	list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
3928e48354ceSNicholas Bellinger 
39292fbb471eSAndy Grover 		list_del(&cmd->i_conn_node);
3930e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
3931e48354ceSNicholas Bellinger 
3932e48354ceSNicholas Bellinger 		iscsit_increment_maxcmdsn(cmd, sess);
3933e48354ceSNicholas Bellinger 
3934d270190aSNicholas Bellinger 		iscsit_free_cmd(cmd);
3935e48354ceSNicholas Bellinger 
3936e48354ceSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
3937e48354ceSNicholas Bellinger 	}
3938e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
3939e48354ceSNicholas Bellinger }
3940e48354ceSNicholas Bellinger 
3941e48354ceSNicholas Bellinger static void iscsit_stop_timers_for_cmds(
3942e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
3943e48354ceSNicholas Bellinger {
3944e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd;
3945e48354ceSNicholas Bellinger 
3946e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
39472fbb471eSAndy Grover 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
3948e48354ceSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE)
3949e48354ceSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
3950e48354ceSNicholas Bellinger 	}
3951e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
3952e48354ceSNicholas Bellinger }
3953e48354ceSNicholas Bellinger 
3954e48354ceSNicholas Bellinger int iscsit_close_connection(
3955e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
3956e48354ceSNicholas Bellinger {
3957e48354ceSNicholas Bellinger 	int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
3958e48354ceSNicholas Bellinger 	struct iscsi_session	*sess = conn->sess;
3959e48354ceSNicholas Bellinger 
3960e48354ceSNicholas Bellinger 	pr_debug("Closing iSCSI connection CID %hu on SID:"
3961e48354ceSNicholas Bellinger 		" %u\n", conn->cid, sess->sid);
3962e48354ceSNicholas Bellinger 	/*
3963e48354ceSNicholas Bellinger 	 * Always up conn_logout_comp just in case the RX Thread is sleeping
3964e48354ceSNicholas Bellinger 	 * and the logout response never got sent because the connection
3965e48354ceSNicholas Bellinger 	 * failed.
3966e48354ceSNicholas Bellinger 	 */
3967e48354ceSNicholas Bellinger 	complete(&conn->conn_logout_comp);
3968e48354ceSNicholas Bellinger 
3969e48354ceSNicholas Bellinger 	iscsi_release_thread_set(conn);
3970e48354ceSNicholas Bellinger 
3971e48354ceSNicholas Bellinger 	iscsit_stop_timers_for_cmds(conn);
3972e48354ceSNicholas Bellinger 	iscsit_stop_nopin_response_timer(conn);
3973e48354ceSNicholas Bellinger 	iscsit_stop_nopin_timer(conn);
3974e48354ceSNicholas Bellinger 	iscsit_free_queue_reqs_for_conn(conn);
3975e48354ceSNicholas Bellinger 
3976e48354ceSNicholas Bellinger 	/*
3977e48354ceSNicholas Bellinger 	 * During Connection recovery drop unacknowledged out of order
3978e48354ceSNicholas Bellinger 	 * commands for this connection, and prepare the other commands
3979e48354ceSNicholas Bellinger 	 * for realligence.
3980e48354ceSNicholas Bellinger 	 *
3981e48354ceSNicholas Bellinger 	 * During normal operation clear the out of order commands (but
3982e48354ceSNicholas Bellinger 	 * do not free the struct iscsi_ooo_cmdsn's) and release all
3983e48354ceSNicholas Bellinger 	 * struct iscsi_cmds.
3984e48354ceSNicholas Bellinger 	 */
3985e48354ceSNicholas Bellinger 	if (atomic_read(&conn->connection_recovery)) {
3986e48354ceSNicholas Bellinger 		iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
3987e48354ceSNicholas Bellinger 		iscsit_prepare_cmds_for_realligance(conn);
3988e48354ceSNicholas Bellinger 	} else {
3989e48354ceSNicholas Bellinger 		iscsit_clear_ooo_cmdsns_for_conn(conn);
3990e48354ceSNicholas Bellinger 		iscsit_release_commands_from_conn(conn);
3991e48354ceSNicholas Bellinger 	}
3992e48354ceSNicholas Bellinger 
3993e48354ceSNicholas Bellinger 	/*
3994e48354ceSNicholas Bellinger 	 * Handle decrementing session or connection usage count if
3995e48354ceSNicholas Bellinger 	 * a logout response was not able to be sent because the
3996e48354ceSNicholas Bellinger 	 * connection failed.  Fall back to Session Recovery here.
3997e48354ceSNicholas Bellinger 	 */
3998e48354ceSNicholas Bellinger 	if (atomic_read(&conn->conn_logout_remove)) {
3999e48354ceSNicholas Bellinger 		if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4000e48354ceSNicholas Bellinger 			iscsit_dec_conn_usage_count(conn);
4001e48354ceSNicholas Bellinger 			iscsit_dec_session_usage_count(sess);
4002e48354ceSNicholas Bellinger 		}
4003e48354ceSNicholas Bellinger 		if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4004e48354ceSNicholas Bellinger 			iscsit_dec_conn_usage_count(conn);
4005e48354ceSNicholas Bellinger 
4006e48354ceSNicholas Bellinger 		atomic_set(&conn->conn_logout_remove, 0);
4007e48354ceSNicholas Bellinger 		atomic_set(&sess->session_reinstatement, 0);
4008e48354ceSNicholas Bellinger 		atomic_set(&sess->session_fall_back_to_erl0, 1);
4009e48354ceSNicholas Bellinger 	}
4010e48354ceSNicholas Bellinger 
4011e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
4012e48354ceSNicholas Bellinger 	list_del(&conn->conn_list);
4013e48354ceSNicholas Bellinger 
4014e48354ceSNicholas Bellinger 	/*
4015e48354ceSNicholas Bellinger 	 * Attempt to let the Initiator know this connection failed by
4016e48354ceSNicholas Bellinger 	 * sending an Connection Dropped Async Message on another
4017e48354ceSNicholas Bellinger 	 * active connection.
4018e48354ceSNicholas Bellinger 	 */
4019e48354ceSNicholas Bellinger 	if (atomic_read(&conn->connection_recovery))
4020e48354ceSNicholas Bellinger 		iscsit_build_conn_drop_async_message(conn);
4021e48354ceSNicholas Bellinger 
4022e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
4023e48354ceSNicholas Bellinger 
4024e48354ceSNicholas Bellinger 	/*
4025e48354ceSNicholas Bellinger 	 * If connection reinstatement is being performed on this connection,
4026e48354ceSNicholas Bellinger 	 * up the connection reinstatement semaphore that is being blocked on
4027e48354ceSNicholas Bellinger 	 * in iscsit_cause_connection_reinstatement().
4028e48354ceSNicholas Bellinger 	 */
4029e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->state_lock);
4030e48354ceSNicholas Bellinger 	if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4031e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->state_lock);
4032e48354ceSNicholas Bellinger 		complete(&conn->conn_wait_comp);
4033e48354ceSNicholas Bellinger 		wait_for_completion(&conn->conn_post_wait_comp);
4034e48354ceSNicholas Bellinger 		spin_lock_bh(&conn->state_lock);
4035e48354ceSNicholas Bellinger 	}
4036e48354ceSNicholas Bellinger 
4037e48354ceSNicholas Bellinger 	/*
4038e48354ceSNicholas Bellinger 	 * If connection reinstatement is being performed on this connection
4039e48354ceSNicholas Bellinger 	 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4040e48354ceSNicholas Bellinger 	 * connection wait rcfr semaphore that is being blocked on
4041e48354ceSNicholas Bellinger 	 * an iscsit_connection_reinstatement_rcfr().
4042e48354ceSNicholas Bellinger 	 */
4043e48354ceSNicholas Bellinger 	if (atomic_read(&conn->connection_wait_rcfr)) {
4044e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->state_lock);
4045e48354ceSNicholas Bellinger 		complete(&conn->conn_wait_rcfr_comp);
4046e48354ceSNicholas Bellinger 		wait_for_completion(&conn->conn_post_wait_comp);
4047e48354ceSNicholas Bellinger 		spin_lock_bh(&conn->state_lock);
4048e48354ceSNicholas Bellinger 	}
4049e48354ceSNicholas Bellinger 	atomic_set(&conn->connection_reinstatement, 1);
4050e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->state_lock);
4051e48354ceSNicholas Bellinger 
4052e48354ceSNicholas Bellinger 	/*
4053e48354ceSNicholas Bellinger 	 * If any other processes are accessing this connection pointer we
4054e48354ceSNicholas Bellinger 	 * must wait until they have completed.
4055e48354ceSNicholas Bellinger 	 */
4056e48354ceSNicholas Bellinger 	iscsit_check_conn_usage_count(conn);
4057e48354ceSNicholas Bellinger 
4058e48354ceSNicholas Bellinger 	if (conn->conn_rx_hash.tfm)
4059e48354ceSNicholas Bellinger 		crypto_free_hash(conn->conn_rx_hash.tfm);
4060e48354ceSNicholas Bellinger 	if (conn->conn_tx_hash.tfm)
4061e48354ceSNicholas Bellinger 		crypto_free_hash(conn->conn_tx_hash.tfm);
4062e48354ceSNicholas Bellinger 
4063e48354ceSNicholas Bellinger 	if (conn->conn_cpumask)
4064e48354ceSNicholas Bellinger 		free_cpumask_var(conn->conn_cpumask);
4065e48354ceSNicholas Bellinger 
4066e48354ceSNicholas Bellinger 	kfree(conn->conn_ops);
4067e48354ceSNicholas Bellinger 	conn->conn_ops = NULL;
4068e48354ceSNicholas Bellinger 
4069bf6932f4SAl Viro 	if (conn->sock)
4070e48354ceSNicholas Bellinger 		sock_release(conn->sock);
4071baa4d64bSNicholas Bellinger 
4072baa4d64bSNicholas Bellinger 	if (conn->conn_transport->iscsit_free_conn)
4073baa4d64bSNicholas Bellinger 		conn->conn_transport->iscsit_free_conn(conn);
4074baa4d64bSNicholas Bellinger 
4075baa4d64bSNicholas Bellinger 	iscsit_put_transport(conn->conn_transport);
4076baa4d64bSNicholas Bellinger 
4077e48354ceSNicholas Bellinger 	conn->thread_set = NULL;
4078e48354ceSNicholas Bellinger 
4079e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4080e48354ceSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_FREE;
4081e48354ceSNicholas Bellinger 	kfree(conn);
4082e48354ceSNicholas Bellinger 
4083e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
4084e48354ceSNicholas Bellinger 	atomic_dec(&sess->nconn);
4085e48354ceSNicholas Bellinger 	pr_debug("Decremented iSCSI connection count to %hu from node:"
4086e48354ceSNicholas Bellinger 		" %s\n", atomic_read(&sess->nconn),
4087e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
4088e48354ceSNicholas Bellinger 	/*
4089e48354ceSNicholas Bellinger 	 * Make sure that if one connection fails in an non ERL=2 iSCSI
4090e48354ceSNicholas Bellinger 	 * Session that they all fail.
4091e48354ceSNicholas Bellinger 	 */
4092e48354ceSNicholas Bellinger 	if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4093e48354ceSNicholas Bellinger 	     !atomic_read(&sess->session_logout))
4094e48354ceSNicholas Bellinger 		atomic_set(&sess->session_fall_back_to_erl0, 1);
4095e48354ceSNicholas Bellinger 
4096e48354ceSNicholas Bellinger 	/*
4097e48354ceSNicholas Bellinger 	 * If this was not the last connection in the session, and we are
4098e48354ceSNicholas Bellinger 	 * performing session reinstatement or falling back to ERL=0, call
4099e48354ceSNicholas Bellinger 	 * iscsit_stop_session() without sleeping to shutdown the other
4100e48354ceSNicholas Bellinger 	 * active connections.
4101e48354ceSNicholas Bellinger 	 */
4102e48354ceSNicholas Bellinger 	if (atomic_read(&sess->nconn)) {
4103e48354ceSNicholas Bellinger 		if (!atomic_read(&sess->session_reinstatement) &&
4104e48354ceSNicholas Bellinger 		    !atomic_read(&sess->session_fall_back_to_erl0)) {
4105e48354ceSNicholas Bellinger 			spin_unlock_bh(&sess->conn_lock);
4106e48354ceSNicholas Bellinger 			return 0;
4107e48354ceSNicholas Bellinger 		}
4108e48354ceSNicholas Bellinger 		if (!atomic_read(&sess->session_stop_active)) {
4109e48354ceSNicholas Bellinger 			atomic_set(&sess->session_stop_active, 1);
4110e48354ceSNicholas Bellinger 			spin_unlock_bh(&sess->conn_lock);
4111e48354ceSNicholas Bellinger 			iscsit_stop_session(sess, 0, 0);
4112e48354ceSNicholas Bellinger 			return 0;
4113e48354ceSNicholas Bellinger 		}
4114e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
4115e48354ceSNicholas Bellinger 		return 0;
4116e48354ceSNicholas Bellinger 	}
4117e48354ceSNicholas Bellinger 
4118e48354ceSNicholas Bellinger 	/*
4119e48354ceSNicholas Bellinger 	 * If this was the last connection in the session and one of the
4120e48354ceSNicholas Bellinger 	 * following is occurring:
4121e48354ceSNicholas Bellinger 	 *
4122e48354ceSNicholas Bellinger 	 * Session Reinstatement is not being performed, and are falling back
4123e48354ceSNicholas Bellinger 	 * to ERL=0 call iscsit_close_session().
4124e48354ceSNicholas Bellinger 	 *
4125e48354ceSNicholas Bellinger 	 * Session Logout was requested.  iscsit_close_session() will be called
4126e48354ceSNicholas Bellinger 	 * elsewhere.
4127e48354ceSNicholas Bellinger 	 *
4128e48354ceSNicholas Bellinger 	 * Session Continuation is not being performed, start the Time2Retain
4129e48354ceSNicholas Bellinger 	 * handler and check if sleep_on_sess_wait_sem is active.
4130e48354ceSNicholas Bellinger 	 */
4131e48354ceSNicholas Bellinger 	if (!atomic_read(&sess->session_reinstatement) &&
4132e48354ceSNicholas Bellinger 	     atomic_read(&sess->session_fall_back_to_erl0)) {
4133e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
413499367f01SNicholas Bellinger 		target_put_session(sess->se_sess);
4135e48354ceSNicholas Bellinger 
4136e48354ceSNicholas Bellinger 		return 0;
4137e48354ceSNicholas Bellinger 	} else if (atomic_read(&sess->session_logout)) {
4138e48354ceSNicholas Bellinger 		pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4139e48354ceSNicholas Bellinger 		sess->session_state = TARG_SESS_STATE_FREE;
4140e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
4141e48354ceSNicholas Bellinger 
4142e48354ceSNicholas Bellinger 		if (atomic_read(&sess->sleep_on_sess_wait_comp))
4143e48354ceSNicholas Bellinger 			complete(&sess->session_wait_comp);
4144e48354ceSNicholas Bellinger 
4145e48354ceSNicholas Bellinger 		return 0;
4146e48354ceSNicholas Bellinger 	} else {
4147e48354ceSNicholas Bellinger 		pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4148e48354ceSNicholas Bellinger 		sess->session_state = TARG_SESS_STATE_FAILED;
4149e48354ceSNicholas Bellinger 
4150e48354ceSNicholas Bellinger 		if (!atomic_read(&sess->session_continuation)) {
4151e48354ceSNicholas Bellinger 			spin_unlock_bh(&sess->conn_lock);
4152e48354ceSNicholas Bellinger 			iscsit_start_time2retain_handler(sess);
4153e48354ceSNicholas Bellinger 		} else
4154e48354ceSNicholas Bellinger 			spin_unlock_bh(&sess->conn_lock);
4155e48354ceSNicholas Bellinger 
4156e48354ceSNicholas Bellinger 		if (atomic_read(&sess->sleep_on_sess_wait_comp))
4157e48354ceSNicholas Bellinger 			complete(&sess->session_wait_comp);
4158e48354ceSNicholas Bellinger 
4159e48354ceSNicholas Bellinger 		return 0;
4160e48354ceSNicholas Bellinger 	}
4161e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
4162e48354ceSNicholas Bellinger 
4163e48354ceSNicholas Bellinger 	return 0;
4164e48354ceSNicholas Bellinger }
4165e48354ceSNicholas Bellinger 
4166e48354ceSNicholas Bellinger int iscsit_close_session(struct iscsi_session *sess)
4167e48354ceSNicholas Bellinger {
4168e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4169e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4170e48354ceSNicholas Bellinger 
4171e48354ceSNicholas Bellinger 	if (atomic_read(&sess->nconn)) {
4172e48354ceSNicholas Bellinger 		pr_err("%d connection(s) still exist for iSCSI session"
4173e48354ceSNicholas Bellinger 			" to %s\n", atomic_read(&sess->nconn),
4174e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
4175e48354ceSNicholas Bellinger 		BUG();
4176e48354ceSNicholas Bellinger 	}
4177e48354ceSNicholas Bellinger 
4178e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
4179e48354ceSNicholas Bellinger 	atomic_set(&sess->session_logout, 1);
4180e48354ceSNicholas Bellinger 	atomic_set(&sess->session_reinstatement, 1);
4181e48354ceSNicholas Bellinger 	iscsit_stop_time2retain_timer(sess);
4182e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
4183e48354ceSNicholas Bellinger 
4184e48354ceSNicholas Bellinger 	/*
4185e48354ceSNicholas Bellinger 	 * transport_deregister_session_configfs() will clear the
4186e48354ceSNicholas Bellinger 	 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4187e48354ceSNicholas Bellinger 	 * can be setting it again with __transport_register_session() in
4188e48354ceSNicholas Bellinger 	 * iscsi_post_login_handler() again after the iscsit_stop_session()
4189e48354ceSNicholas Bellinger 	 * completes in iscsi_np context.
4190e48354ceSNicholas Bellinger 	 */
4191e48354ceSNicholas Bellinger 	transport_deregister_session_configfs(sess->se_sess);
4192e48354ceSNicholas Bellinger 
4193e48354ceSNicholas Bellinger 	/*
4194e48354ceSNicholas Bellinger 	 * If any other processes are accessing this session pointer we must
4195e48354ceSNicholas Bellinger 	 * wait until they have completed.  If we are in an interrupt (the
4196e48354ceSNicholas Bellinger 	 * time2retain handler) and contain and active session usage count we
4197e48354ceSNicholas Bellinger 	 * restart the timer and exit.
4198e48354ceSNicholas Bellinger 	 */
4199e48354ceSNicholas Bellinger 	if (!in_interrupt()) {
4200e48354ceSNicholas Bellinger 		if (iscsit_check_session_usage_count(sess) == 1)
4201e48354ceSNicholas Bellinger 			iscsit_stop_session(sess, 1, 1);
4202e48354ceSNicholas Bellinger 	} else {
4203e48354ceSNicholas Bellinger 		if (iscsit_check_session_usage_count(sess) == 2) {
4204e48354ceSNicholas Bellinger 			atomic_set(&sess->session_logout, 0);
4205e48354ceSNicholas Bellinger 			iscsit_start_time2retain_handler(sess);
4206e48354ceSNicholas Bellinger 			return 0;
4207e48354ceSNicholas Bellinger 		}
4208e48354ceSNicholas Bellinger 	}
4209e48354ceSNicholas Bellinger 
4210e48354ceSNicholas Bellinger 	transport_deregister_session(sess->se_sess);
4211e48354ceSNicholas Bellinger 
4212e48354ceSNicholas Bellinger 	if (sess->sess_ops->ErrorRecoveryLevel == 2)
4213e48354ceSNicholas Bellinger 		iscsit_free_connection_recovery_entires(sess);
4214e48354ceSNicholas Bellinger 
4215e48354ceSNicholas Bellinger 	iscsit_free_all_ooo_cmdsns(sess);
4216e48354ceSNicholas Bellinger 
4217e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
4218e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4219e48354ceSNicholas Bellinger 	sess->session_state = TARG_SESS_STATE_FREE;
4220e48354ceSNicholas Bellinger 	pr_debug("Released iSCSI session from node: %s\n",
4221e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
4222e48354ceSNicholas Bellinger 	tpg->nsessions--;
4223e48354ceSNicholas Bellinger 	if (tpg->tpg_tiqn)
4224e48354ceSNicholas Bellinger 		tpg->tpg_tiqn->tiqn_nsessions--;
4225e48354ceSNicholas Bellinger 
4226e48354ceSNicholas Bellinger 	pr_debug("Decremented number of active iSCSI Sessions on"
4227e48354ceSNicholas Bellinger 		" iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4228e48354ceSNicholas Bellinger 
4229e48354ceSNicholas Bellinger 	spin_lock(&sess_idr_lock);
4230e48354ceSNicholas Bellinger 	idr_remove(&sess_idr, sess->session_index);
4231e48354ceSNicholas Bellinger 	spin_unlock(&sess_idr_lock);
4232e48354ceSNicholas Bellinger 
4233e48354ceSNicholas Bellinger 	kfree(sess->sess_ops);
4234e48354ceSNicholas Bellinger 	sess->sess_ops = NULL;
4235e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
4236e48354ceSNicholas Bellinger 
4237e48354ceSNicholas Bellinger 	kfree(sess);
4238e48354ceSNicholas Bellinger 	return 0;
4239e48354ceSNicholas Bellinger }
4240e48354ceSNicholas Bellinger 
4241e48354ceSNicholas Bellinger static void iscsit_logout_post_handler_closesession(
4242e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
4243e48354ceSNicholas Bellinger {
4244e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
4245e48354ceSNicholas Bellinger 
4246e48354ceSNicholas Bellinger 	iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4247e48354ceSNicholas Bellinger 	iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4248e48354ceSNicholas Bellinger 
4249e48354ceSNicholas Bellinger 	atomic_set(&conn->conn_logout_remove, 0);
4250e48354ceSNicholas Bellinger 	complete(&conn->conn_logout_comp);
4251e48354ceSNicholas Bellinger 
4252e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
4253e48354ceSNicholas Bellinger 	iscsit_stop_session(sess, 1, 1);
4254e48354ceSNicholas Bellinger 	iscsit_dec_session_usage_count(sess);
425599367f01SNicholas Bellinger 	target_put_session(sess->se_sess);
4256e48354ceSNicholas Bellinger }
4257e48354ceSNicholas Bellinger 
4258e48354ceSNicholas Bellinger static void iscsit_logout_post_handler_samecid(
4259e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
4260e48354ceSNicholas Bellinger {
4261e48354ceSNicholas Bellinger 	iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4262e48354ceSNicholas Bellinger 	iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4263e48354ceSNicholas Bellinger 
4264e48354ceSNicholas Bellinger 	atomic_set(&conn->conn_logout_remove, 0);
4265e48354ceSNicholas Bellinger 	complete(&conn->conn_logout_comp);
4266e48354ceSNicholas Bellinger 
4267e48354ceSNicholas Bellinger 	iscsit_cause_connection_reinstatement(conn, 1);
4268e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
4269e48354ceSNicholas Bellinger }
4270e48354ceSNicholas Bellinger 
4271e48354ceSNicholas Bellinger static void iscsit_logout_post_handler_diffcid(
4272e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
4273e48354ceSNicholas Bellinger 	u16 cid)
4274e48354ceSNicholas Bellinger {
4275e48354ceSNicholas Bellinger 	struct iscsi_conn *l_conn;
4276e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
4277e48354ceSNicholas Bellinger 
4278e48354ceSNicholas Bellinger 	if (!sess)
4279e48354ceSNicholas Bellinger 		return;
4280e48354ceSNicholas Bellinger 
4281e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
4282e48354ceSNicholas Bellinger 	list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4283e48354ceSNicholas Bellinger 		if (l_conn->cid == cid) {
4284e48354ceSNicholas Bellinger 			iscsit_inc_conn_usage_count(l_conn);
4285e48354ceSNicholas Bellinger 			break;
4286e48354ceSNicholas Bellinger 		}
4287e48354ceSNicholas Bellinger 	}
4288e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
4289e48354ceSNicholas Bellinger 
4290e48354ceSNicholas Bellinger 	if (!l_conn)
4291e48354ceSNicholas Bellinger 		return;
4292e48354ceSNicholas Bellinger 
4293e48354ceSNicholas Bellinger 	if (l_conn->sock)
4294e48354ceSNicholas Bellinger 		l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4295e48354ceSNicholas Bellinger 
4296e48354ceSNicholas Bellinger 	spin_lock_bh(&l_conn->state_lock);
4297e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4298e48354ceSNicholas Bellinger 	l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4299e48354ceSNicholas Bellinger 	spin_unlock_bh(&l_conn->state_lock);
4300e48354ceSNicholas Bellinger 
4301e48354ceSNicholas Bellinger 	iscsit_cause_connection_reinstatement(l_conn, 1);
4302e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(l_conn);
4303e48354ceSNicholas Bellinger }
4304e48354ceSNicholas Bellinger 
4305e48354ceSNicholas Bellinger /*
4306e48354ceSNicholas Bellinger  *	Return of 0 causes the TX thread to restart.
4307e48354ceSNicholas Bellinger  */
4308e48354ceSNicholas Bellinger static int iscsit_logout_post_handler(
4309e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
4310e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
4311e48354ceSNicholas Bellinger {
4312e48354ceSNicholas Bellinger 	int ret = 0;
4313e48354ceSNicholas Bellinger 
4314e48354ceSNicholas Bellinger 	switch (cmd->logout_reason) {
4315e48354ceSNicholas Bellinger 	case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4316e48354ceSNicholas Bellinger 		switch (cmd->logout_response) {
4317e48354ceSNicholas Bellinger 		case ISCSI_LOGOUT_SUCCESS:
4318e48354ceSNicholas Bellinger 		case ISCSI_LOGOUT_CLEANUP_FAILED:
4319e48354ceSNicholas Bellinger 		default:
4320e48354ceSNicholas Bellinger 			iscsit_logout_post_handler_closesession(conn);
4321e48354ceSNicholas Bellinger 			break;
4322e48354ceSNicholas Bellinger 		}
4323e48354ceSNicholas Bellinger 		ret = 0;
4324e48354ceSNicholas Bellinger 		break;
4325e48354ceSNicholas Bellinger 	case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4326e48354ceSNicholas Bellinger 		if (conn->cid == cmd->logout_cid) {
4327e48354ceSNicholas Bellinger 			switch (cmd->logout_response) {
4328e48354ceSNicholas Bellinger 			case ISCSI_LOGOUT_SUCCESS:
4329e48354ceSNicholas Bellinger 			case ISCSI_LOGOUT_CLEANUP_FAILED:
4330e48354ceSNicholas Bellinger 			default:
4331e48354ceSNicholas Bellinger 				iscsit_logout_post_handler_samecid(conn);
4332e48354ceSNicholas Bellinger 				break;
4333e48354ceSNicholas Bellinger 			}
4334e48354ceSNicholas Bellinger 			ret = 0;
4335e48354ceSNicholas Bellinger 		} else {
4336e48354ceSNicholas Bellinger 			switch (cmd->logout_response) {
4337e48354ceSNicholas Bellinger 			case ISCSI_LOGOUT_SUCCESS:
4338e48354ceSNicholas Bellinger 				iscsit_logout_post_handler_diffcid(conn,
4339e48354ceSNicholas Bellinger 					cmd->logout_cid);
4340e48354ceSNicholas Bellinger 				break;
4341e48354ceSNicholas Bellinger 			case ISCSI_LOGOUT_CID_NOT_FOUND:
4342e48354ceSNicholas Bellinger 			case ISCSI_LOGOUT_CLEANUP_FAILED:
4343e48354ceSNicholas Bellinger 			default:
4344e48354ceSNicholas Bellinger 				break;
4345e48354ceSNicholas Bellinger 			}
4346e48354ceSNicholas Bellinger 			ret = 1;
4347e48354ceSNicholas Bellinger 		}
4348e48354ceSNicholas Bellinger 		break;
4349e48354ceSNicholas Bellinger 	case ISCSI_LOGOUT_REASON_RECOVERY:
4350e48354ceSNicholas Bellinger 		switch (cmd->logout_response) {
4351e48354ceSNicholas Bellinger 		case ISCSI_LOGOUT_SUCCESS:
4352e48354ceSNicholas Bellinger 		case ISCSI_LOGOUT_CID_NOT_FOUND:
4353e48354ceSNicholas Bellinger 		case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4354e48354ceSNicholas Bellinger 		case ISCSI_LOGOUT_CLEANUP_FAILED:
4355e48354ceSNicholas Bellinger 		default:
4356e48354ceSNicholas Bellinger 			break;
4357e48354ceSNicholas Bellinger 		}
4358e48354ceSNicholas Bellinger 		ret = 1;
4359e48354ceSNicholas Bellinger 		break;
4360e48354ceSNicholas Bellinger 	default:
4361e48354ceSNicholas Bellinger 		break;
4362e48354ceSNicholas Bellinger 
4363e48354ceSNicholas Bellinger 	}
4364e48354ceSNicholas Bellinger 	return ret;
4365e48354ceSNicholas Bellinger }
4366e48354ceSNicholas Bellinger 
4367e48354ceSNicholas Bellinger void iscsit_fail_session(struct iscsi_session *sess)
4368e48354ceSNicholas Bellinger {
4369e48354ceSNicholas Bellinger 	struct iscsi_conn *conn;
4370e48354ceSNicholas Bellinger 
4371e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
4372e48354ceSNicholas Bellinger 	list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4373e48354ceSNicholas Bellinger 		pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4374e48354ceSNicholas Bellinger 		conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4375e48354ceSNicholas Bellinger 	}
4376e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
4377e48354ceSNicholas Bellinger 
4378e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4379e48354ceSNicholas Bellinger 	sess->session_state = TARG_SESS_STATE_FAILED;
4380e48354ceSNicholas Bellinger }
4381e48354ceSNicholas Bellinger 
4382e48354ceSNicholas Bellinger int iscsit_free_session(struct iscsi_session *sess)
4383e48354ceSNicholas Bellinger {
4384e48354ceSNicholas Bellinger 	u16 conn_count = atomic_read(&sess->nconn);
4385e48354ceSNicholas Bellinger 	struct iscsi_conn *conn, *conn_tmp = NULL;
4386e48354ceSNicholas Bellinger 	int is_last;
4387e48354ceSNicholas Bellinger 
4388e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
4389e48354ceSNicholas Bellinger 	atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4390e48354ceSNicholas Bellinger 
4391e48354ceSNicholas Bellinger 	list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4392e48354ceSNicholas Bellinger 			conn_list) {
4393e48354ceSNicholas Bellinger 		if (conn_count == 0)
4394e48354ceSNicholas Bellinger 			break;
4395e48354ceSNicholas Bellinger 
4396e48354ceSNicholas Bellinger 		if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4397e48354ceSNicholas Bellinger 			is_last = 1;
4398e48354ceSNicholas Bellinger 		} else {
4399e48354ceSNicholas Bellinger 			iscsit_inc_conn_usage_count(conn_tmp);
4400e48354ceSNicholas Bellinger 			is_last = 0;
4401e48354ceSNicholas Bellinger 		}
4402e48354ceSNicholas Bellinger 		iscsit_inc_conn_usage_count(conn);
4403e48354ceSNicholas Bellinger 
4404e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
4405e48354ceSNicholas Bellinger 		iscsit_cause_connection_reinstatement(conn, 1);
4406e48354ceSNicholas Bellinger 		spin_lock_bh(&sess->conn_lock);
4407e48354ceSNicholas Bellinger 
4408e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn);
4409e48354ceSNicholas Bellinger 		if (is_last == 0)
4410e48354ceSNicholas Bellinger 			iscsit_dec_conn_usage_count(conn_tmp);
4411e48354ceSNicholas Bellinger 
4412e48354ceSNicholas Bellinger 		conn_count--;
4413e48354ceSNicholas Bellinger 	}
4414e48354ceSNicholas Bellinger 
4415e48354ceSNicholas Bellinger 	if (atomic_read(&sess->nconn)) {
4416e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
4417e48354ceSNicholas Bellinger 		wait_for_completion(&sess->session_wait_comp);
4418e48354ceSNicholas Bellinger 	} else
4419e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
4420e48354ceSNicholas Bellinger 
442199367f01SNicholas Bellinger 	target_put_session(sess->se_sess);
4422e48354ceSNicholas Bellinger 	return 0;
4423e48354ceSNicholas Bellinger }
4424e48354ceSNicholas Bellinger 
4425e48354ceSNicholas Bellinger void iscsit_stop_session(
4426e48354ceSNicholas Bellinger 	struct iscsi_session *sess,
4427e48354ceSNicholas Bellinger 	int session_sleep,
4428e48354ceSNicholas Bellinger 	int connection_sleep)
4429e48354ceSNicholas Bellinger {
4430e48354ceSNicholas Bellinger 	u16 conn_count = atomic_read(&sess->nconn);
4431e48354ceSNicholas Bellinger 	struct iscsi_conn *conn, *conn_tmp = NULL;
4432e48354ceSNicholas Bellinger 	int is_last;
4433e48354ceSNicholas Bellinger 
4434e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
4435e48354ceSNicholas Bellinger 	if (session_sleep)
4436e48354ceSNicholas Bellinger 		atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4437e48354ceSNicholas Bellinger 
4438e48354ceSNicholas Bellinger 	if (connection_sleep) {
4439e48354ceSNicholas Bellinger 		list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4440e48354ceSNicholas Bellinger 				conn_list) {
4441e48354ceSNicholas Bellinger 			if (conn_count == 0)
4442e48354ceSNicholas Bellinger 				break;
4443e48354ceSNicholas Bellinger 
4444e48354ceSNicholas Bellinger 			if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4445e48354ceSNicholas Bellinger 				is_last = 1;
4446e48354ceSNicholas Bellinger 			} else {
4447e48354ceSNicholas Bellinger 				iscsit_inc_conn_usage_count(conn_tmp);
4448e48354ceSNicholas Bellinger 				is_last = 0;
4449e48354ceSNicholas Bellinger 			}
4450e48354ceSNicholas Bellinger 			iscsit_inc_conn_usage_count(conn);
4451e48354ceSNicholas Bellinger 
4452e48354ceSNicholas Bellinger 			spin_unlock_bh(&sess->conn_lock);
4453e48354ceSNicholas Bellinger 			iscsit_cause_connection_reinstatement(conn, 1);
4454e48354ceSNicholas Bellinger 			spin_lock_bh(&sess->conn_lock);
4455e48354ceSNicholas Bellinger 
4456e48354ceSNicholas Bellinger 			iscsit_dec_conn_usage_count(conn);
4457e48354ceSNicholas Bellinger 			if (is_last == 0)
4458e48354ceSNicholas Bellinger 				iscsit_dec_conn_usage_count(conn_tmp);
4459e48354ceSNicholas Bellinger 			conn_count--;
4460e48354ceSNicholas Bellinger 		}
4461e48354ceSNicholas Bellinger 	} else {
4462e48354ceSNicholas Bellinger 		list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4463e48354ceSNicholas Bellinger 			iscsit_cause_connection_reinstatement(conn, 0);
4464e48354ceSNicholas Bellinger 	}
4465e48354ceSNicholas Bellinger 
4466e48354ceSNicholas Bellinger 	if (session_sleep && atomic_read(&sess->nconn)) {
4467e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
4468e48354ceSNicholas Bellinger 		wait_for_completion(&sess->session_wait_comp);
4469e48354ceSNicholas Bellinger 	} else
4470e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
4471e48354ceSNicholas Bellinger }
4472e48354ceSNicholas Bellinger 
4473e48354ceSNicholas Bellinger int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4474e48354ceSNicholas Bellinger {
4475e48354ceSNicholas Bellinger 	struct iscsi_session *sess;
4476e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4477e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
4478e48354ceSNicholas Bellinger 	int session_count = 0;
4479e48354ceSNicholas Bellinger 
4480e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
4481e48354ceSNicholas Bellinger 	if (tpg->nsessions && !force) {
4482e48354ceSNicholas Bellinger 		spin_unlock_bh(&se_tpg->session_lock);
4483e48354ceSNicholas Bellinger 		return -1;
4484e48354ceSNicholas Bellinger 	}
4485e48354ceSNicholas Bellinger 
4486e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4487e48354ceSNicholas Bellinger 			sess_list) {
4488e48354ceSNicholas Bellinger 		sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4489e48354ceSNicholas Bellinger 
4490e48354ceSNicholas Bellinger 		spin_lock(&sess->conn_lock);
4491e48354ceSNicholas Bellinger 		if (atomic_read(&sess->session_fall_back_to_erl0) ||
4492e48354ceSNicholas Bellinger 		    atomic_read(&sess->session_logout) ||
4493e48354ceSNicholas Bellinger 		    (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4494e48354ceSNicholas Bellinger 			spin_unlock(&sess->conn_lock);
4495e48354ceSNicholas Bellinger 			continue;
4496e48354ceSNicholas Bellinger 		}
4497e48354ceSNicholas Bellinger 		atomic_set(&sess->session_reinstatement, 1);
4498e48354ceSNicholas Bellinger 		spin_unlock(&sess->conn_lock);
4499e48354ceSNicholas Bellinger 		spin_unlock_bh(&se_tpg->session_lock);
4500e48354ceSNicholas Bellinger 
4501e48354ceSNicholas Bellinger 		iscsit_free_session(sess);
4502e48354ceSNicholas Bellinger 		spin_lock_bh(&se_tpg->session_lock);
4503e48354ceSNicholas Bellinger 
4504e48354ceSNicholas Bellinger 		session_count++;
4505e48354ceSNicholas Bellinger 	}
4506e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
4507e48354ceSNicholas Bellinger 
4508e48354ceSNicholas Bellinger 	pr_debug("Released %d iSCSI Session(s) from Target Portal"
4509e48354ceSNicholas Bellinger 			" Group: %hu\n", session_count, tpg->tpgt);
4510e48354ceSNicholas Bellinger 	return 0;
4511e48354ceSNicholas Bellinger }
4512e48354ceSNicholas Bellinger 
4513e48354ceSNicholas Bellinger MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4514e48354ceSNicholas Bellinger MODULE_VERSION("4.1.x");
4515e48354ceSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
4516e48354ceSNicholas Bellinger MODULE_LICENSE("GPL");
4517e48354ceSNicholas Bellinger 
4518e48354ceSNicholas Bellinger module_init(iscsi_target_init_module);
4519e48354ceSNicholas Bellinger module_exit(iscsi_target_cleanup_module);
4520