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