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