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