1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * This file contains main functions related to the iSCSI Target Core Driver.
5 * (c) Copyright 2007-2013 Datera, Inc.
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9 ******************************************************************************/
11 #include <crypto/hash.h>
12 #include <linux/string.h>
13 #include <linux/kthread.h>
14 #include <linux/completion.h>
15 #include <linux/module.h>
16 #include <linux/vmalloc.h>
17 #include <linux/idr.h>
18 #include <linux/delay.h>
19 #include <linux/sched/signal.h>
20 #include <asm/unaligned.h>
21 #include <linux/inet.h>
23 #include <scsi/scsi_proto.h>
24 #include <scsi/iscsi_proto.h>
25 #include <scsi/scsi_tcq.h>
26 #include <target/target_core_base.h>
27 #include <target/target_core_fabric.h>
29 #include <target/iscsi/iscsi_target_core.h>
30 #include "iscsi_target_parameters.h"
31 #include "iscsi_target_seq_pdu_list.h"
32 #include "iscsi_target_datain_values.h"
33 #include "iscsi_target_erl0.h"
34 #include "iscsi_target_erl1.h"
35 #include "iscsi_target_erl2.h"
36 #include "iscsi_target_login.h"
37 #include "iscsi_target_tmr.h"
38 #include "iscsi_target_tpg.h"
39 #include "iscsi_target_util.h"
40 #include "iscsi_target.h"
41 #include "iscsi_target_device.h"
42 #include <target/iscsi/iscsi_target_stat.h>
44 #include <target/iscsi/iscsi_transport.h>
46 static LIST_HEAD(g_tiqn_list);
47 static LIST_HEAD(g_np_list);
48 static DEFINE_SPINLOCK(tiqn_lock);
49 static DEFINE_MUTEX(np_lock);
51 static struct idr tiqn_idr;
53 struct mutex auth_id_lock;
55 struct iscsit_global *iscsit_global;
57 struct kmem_cache *lio_qr_cache;
58 struct kmem_cache *lio_dr_cache;
59 struct kmem_cache *lio_ooo_cache;
60 struct kmem_cache *lio_r2t_cache;
62 static int iscsit_handle_immediate_data(struct iscsi_cmd *,
63 struct iscsi_scsi_req *, u32);
65 struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
67 struct iscsi_tiqn *tiqn = NULL;
69 spin_lock(&tiqn_lock);
70 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
71 if (!strcmp(tiqn->tiqn, buf)) {
73 spin_lock(&tiqn->tiqn_state_lock);
74 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
75 tiqn->tiqn_access_count++;
76 spin_unlock(&tiqn->tiqn_state_lock);
77 spin_unlock(&tiqn_lock);
80 spin_unlock(&tiqn->tiqn_state_lock);
83 spin_unlock(&tiqn_lock);
88 static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
90 spin_lock(&tiqn->tiqn_state_lock);
91 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
92 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
93 spin_unlock(&tiqn->tiqn_state_lock);
96 spin_unlock(&tiqn->tiqn_state_lock);
101 void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
103 spin_lock(&tiqn->tiqn_state_lock);
104 tiqn->tiqn_access_count--;
105 spin_unlock(&tiqn->tiqn_state_lock);
109 * Note that IQN formatting is expected to be done in userspace, and
110 * no explict IQN format checks are done here.
112 struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
114 struct iscsi_tiqn *tiqn = NULL;
117 if (strlen(buf) >= ISCSI_IQN_LEN) {
118 pr_err("Target IQN exceeds %d bytes\n",
120 return ERR_PTR(-EINVAL);
123 tiqn = kzalloc(sizeof(*tiqn), GFP_KERNEL);
125 return ERR_PTR(-ENOMEM);
127 sprintf(tiqn->tiqn, "%s", buf);
128 INIT_LIST_HEAD(&tiqn->tiqn_list);
129 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
130 spin_lock_init(&tiqn->tiqn_state_lock);
131 spin_lock_init(&tiqn->tiqn_tpg_lock);
132 spin_lock_init(&tiqn->sess_err_stats.lock);
133 spin_lock_init(&tiqn->login_stats.lock);
134 spin_lock_init(&tiqn->logout_stats.lock);
136 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
138 idr_preload(GFP_KERNEL);
139 spin_lock(&tiqn_lock);
141 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
143 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
144 spin_unlock(&tiqn_lock);
149 tiqn->tiqn_index = ret;
150 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
152 spin_unlock(&tiqn_lock);
155 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
161 static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
164 * Wait for accesses to said struct iscsi_tiqn to end.
166 spin_lock(&tiqn->tiqn_state_lock);
167 while (tiqn->tiqn_access_count != 0) {
168 spin_unlock(&tiqn->tiqn_state_lock);
170 spin_lock(&tiqn->tiqn_state_lock);
172 spin_unlock(&tiqn->tiqn_state_lock);
175 void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
178 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
179 * while holding tiqn->tiqn_state_lock. This means that all subsequent
180 * attempts to access this struct iscsi_tiqn will fail from both transport
181 * fabric and control code paths.
183 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
184 pr_err("iscsit_set_tiqn_shutdown() failed\n");
188 iscsit_wait_for_tiqn(tiqn);
190 spin_lock(&tiqn_lock);
191 list_del(&tiqn->tiqn_list);
192 idr_remove(&tiqn_idr, tiqn->tiqn_index);
193 spin_unlock(&tiqn_lock);
195 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
200 int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
204 * Determine if the network portal is accepting storage traffic.
206 spin_lock_bh(&np->np_thread_lock);
207 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
208 spin_unlock_bh(&np->np_thread_lock);
211 spin_unlock_bh(&np->np_thread_lock);
213 * Determine if the portal group is accepting storage traffic.
215 spin_lock_bh(&tpg->tpg_state_lock);
216 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
217 spin_unlock_bh(&tpg->tpg_state_lock);
220 spin_unlock_bh(&tpg->tpg_state_lock);
223 * Here we serialize access across the TIQN+TPG Tuple.
225 ret = down_interruptible(&tpg->np_login_sem);
229 spin_lock_bh(&tpg->tpg_state_lock);
230 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
231 spin_unlock_bh(&tpg->tpg_state_lock);
232 up(&tpg->np_login_sem);
235 spin_unlock_bh(&tpg->tpg_state_lock);
240 void iscsit_login_kref_put(struct kref *kref)
242 struct iscsi_tpg_np *tpg_np = container_of(kref,
243 struct iscsi_tpg_np, tpg_np_kref);
245 complete(&tpg_np->tpg_np_comp);
248 int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
249 struct iscsi_tpg_np *tpg_np)
251 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
253 up(&tpg->np_login_sem);
256 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
259 iscsit_put_tiqn_for_login(tiqn);
264 bool iscsit_check_np_match(
265 struct sockaddr_storage *sockaddr,
267 int network_transport)
269 struct sockaddr_in *sock_in, *sock_in_e;
270 struct sockaddr_in6 *sock_in6, *sock_in6_e;
271 bool ip_match = false;
274 if (sockaddr->ss_family == AF_INET6) {
275 sock_in6 = (struct sockaddr_in6 *)sockaddr;
276 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
278 if (!memcmp(&sock_in6->sin6_addr.in6_u,
279 &sock_in6_e->sin6_addr.in6_u,
280 sizeof(struct in6_addr)))
283 port = ntohs(sock_in6->sin6_port);
284 port_e = ntohs(sock_in6_e->sin6_port);
286 sock_in = (struct sockaddr_in *)sockaddr;
287 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
289 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
292 port = ntohs(sock_in->sin_port);
293 port_e = ntohs(sock_in_e->sin_port);
296 if (ip_match && (port_e == port) &&
297 (np->np_network_transport == network_transport))
303 static struct iscsi_np *iscsit_get_np(
304 struct sockaddr_storage *sockaddr,
305 int network_transport)
310 lockdep_assert_held(&np_lock);
312 list_for_each_entry(np, &g_np_list, np_list) {
313 spin_lock_bh(&np->np_thread_lock);
314 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
315 spin_unlock_bh(&np->np_thread_lock);
319 match = iscsit_check_np_match(sockaddr, np, network_transport);
322 * Increment the np_exports reference count now to
323 * prevent iscsit_del_np() below from being called
324 * while iscsi_tpg_add_network_portal() is called.
327 spin_unlock_bh(&np->np_thread_lock);
330 spin_unlock_bh(&np->np_thread_lock);
336 struct iscsi_np *iscsit_add_np(
337 struct sockaddr_storage *sockaddr,
338 int network_transport)
343 mutex_lock(&np_lock);
346 * Locate the existing struct iscsi_np if already active..
348 np = iscsit_get_np(sockaddr, network_transport);
350 mutex_unlock(&np_lock);
354 np = kzalloc(sizeof(*np), GFP_KERNEL);
356 mutex_unlock(&np_lock);
357 return ERR_PTR(-ENOMEM);
360 np->np_flags |= NPF_IP_NETWORK;
361 np->np_network_transport = network_transport;
362 spin_lock_init(&np->np_thread_lock);
363 init_completion(&np->np_restart_comp);
364 INIT_LIST_HEAD(&np->np_list);
366 timer_setup(&np->np_login_timer, iscsi_handle_login_thread_timeout, 0);
368 ret = iscsi_target_setup_login_socket(np, sockaddr);
371 mutex_unlock(&np_lock);
375 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
376 if (IS_ERR(np->np_thread)) {
377 pr_err("Unable to create kthread: iscsi_np\n");
378 ret = PTR_ERR(np->np_thread);
380 mutex_unlock(&np_lock);
384 * Increment the np_exports reference count now to prevent
385 * iscsit_del_np() below from being run while a new call to
386 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
387 * active. We don't need to hold np->np_thread_lock at this
388 * point because iscsi_np has not been added to g_np_list yet.
391 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
393 list_add_tail(&np->np_list, &g_np_list);
394 mutex_unlock(&np_lock);
396 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
397 &np->np_sockaddr, np->np_transport->name);
402 int iscsit_reset_np_thread(
404 struct iscsi_tpg_np *tpg_np,
405 struct iscsi_portal_group *tpg,
408 spin_lock_bh(&np->np_thread_lock);
409 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
410 spin_unlock_bh(&np->np_thread_lock);
413 np->np_thread_state = ISCSI_NP_THREAD_RESET;
414 atomic_inc(&np->np_reset_count);
417 spin_unlock_bh(&np->np_thread_lock);
418 send_sig(SIGINT, np->np_thread, 1);
419 wait_for_completion(&np->np_restart_comp);
420 spin_lock_bh(&np->np_thread_lock);
422 spin_unlock_bh(&np->np_thread_lock);
424 if (tpg_np && shutdown) {
425 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
427 wait_for_completion(&tpg_np->tpg_np_comp);
433 static void iscsit_free_np(struct iscsi_np *np)
436 sock_release(np->np_socket);
439 int iscsit_del_np(struct iscsi_np *np)
441 spin_lock_bh(&np->np_thread_lock);
443 if (np->np_exports) {
445 spin_unlock_bh(&np->np_thread_lock);
448 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
449 spin_unlock_bh(&np->np_thread_lock);
453 * We need to send the signal to wakeup Linux/Net
454 * which may be sleeping in sock_accept()..
456 send_sig(SIGINT, np->np_thread, 1);
457 kthread_stop(np->np_thread);
458 np->np_thread = NULL;
461 np->np_transport->iscsit_free_np(np);
463 mutex_lock(&np_lock);
464 list_del(&np->np_list);
465 mutex_unlock(&np_lock);
467 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
468 &np->np_sockaddr, np->np_transport->name);
470 iscsit_put_transport(np->np_transport);
475 static void iscsit_get_rx_pdu(struct iscsi_conn *);
477 int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
479 return iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
481 EXPORT_SYMBOL(iscsit_queue_rsp);
483 void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
485 spin_lock_bh(&conn->cmd_lock);
486 if (!list_empty(&cmd->i_conn_node))
487 list_del_init(&cmd->i_conn_node);
488 spin_unlock_bh(&conn->cmd_lock);
490 __iscsit_free_cmd(cmd, true);
492 EXPORT_SYMBOL(iscsit_aborted_task);
494 static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
495 u32, u32, const void *, void *);
496 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *);
499 iscsit_xmit_nondatain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
500 const void *data_buf, u32 data_buf_len)
502 struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu;
504 u32 niov = 0, tx_size = ISCSI_HDR_LEN;
507 iov = &cmd->iov_misc[0];
508 iov[niov].iov_base = cmd->pdu;
509 iov[niov++].iov_len = ISCSI_HDR_LEN;
511 if (conn->conn_ops->HeaderDigest) {
512 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
514 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
515 ISCSI_HDR_LEN, 0, NULL,
518 iov[0].iov_len += ISCSI_CRC_LEN;
519 tx_size += ISCSI_CRC_LEN;
520 pr_debug("Attaching CRC32C HeaderDigest"
521 " to opcode 0x%x 0x%08x\n",
522 hdr->opcode, *header_digest);
526 u32 padding = ((-data_buf_len) & 3);
528 iov[niov].iov_base = (void *)data_buf;
529 iov[niov++].iov_len = data_buf_len;
530 tx_size += data_buf_len;
533 iov[niov].iov_base = &cmd->pad_bytes;
534 iov[niov++].iov_len = padding;
536 pr_debug("Attaching %u additional"
537 " padding bytes.\n", padding);
540 if (conn->conn_ops->DataDigest) {
541 iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
542 data_buf, data_buf_len,
543 padding, &cmd->pad_bytes,
546 iov[niov].iov_base = &cmd->data_crc;
547 iov[niov++].iov_len = ISCSI_CRC_LEN;
548 tx_size += ISCSI_CRC_LEN;
549 pr_debug("Attached DataDigest for %u"
550 " bytes opcode 0x%x, CRC 0x%08x\n",
551 data_buf_len, hdr->opcode, cmd->data_crc);
555 cmd->iov_misc_count = niov;
556 cmd->tx_size = tx_size;
558 ret = iscsit_send_tx_data(cmd, conn, 1);
560 iscsit_tx_thread_wait_for_tcp(conn);
567 static int iscsit_map_iovec(struct iscsi_cmd *cmd, struct kvec *iov, int nvec,
568 u32 data_offset, u32 data_length);
569 static void iscsit_unmap_iovec(struct iscsi_cmd *);
570 static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsi_cmd *,
571 u32, u32, u32, u8 *);
573 iscsit_xmit_datain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
574 const struct iscsi_datain *datain)
577 u32 iov_count = 0, tx_size = 0;
580 iov = &cmd->iov_data[0];
581 iov[iov_count].iov_base = cmd->pdu;
582 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
583 tx_size += ISCSI_HDR_LEN;
585 if (conn->conn_ops->HeaderDigest) {
586 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
588 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
589 ISCSI_HDR_LEN, 0, NULL,
592 iov[0].iov_len += ISCSI_CRC_LEN;
593 tx_size += ISCSI_CRC_LEN;
595 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
599 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[iov_count],
600 cmd->orig_iov_data_count - (iov_count + 2),
601 datain->offset, datain->length);
605 iov_count += iov_ret;
606 tx_size += datain->length;
608 cmd->padding = ((-datain->length) & 3);
610 iov[iov_count].iov_base = cmd->pad_bytes;
611 iov[iov_count++].iov_len = cmd->padding;
612 tx_size += cmd->padding;
614 pr_debug("Attaching %u padding bytes\n", cmd->padding);
617 if (conn->conn_ops->DataDigest) {
618 cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
624 iov[iov_count].iov_base = &cmd->data_crc;
625 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
626 tx_size += ISCSI_CRC_LEN;
628 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
629 datain->length + cmd->padding, cmd->data_crc);
632 cmd->iov_data_count = iov_count;
633 cmd->tx_size = tx_size;
635 ret = iscsit_fe_sendpage_sg(cmd, conn);
637 iscsit_unmap_iovec(cmd);
640 iscsit_tx_thread_wait_for_tcp(conn);
647 static int iscsit_xmit_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
648 struct iscsi_datain_req *dr, const void *buf,
652 return iscsit_xmit_datain_pdu(conn, cmd, buf);
654 return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len);
657 static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
659 return TARGET_PROT_NORMAL;
662 static struct iscsit_transport iscsi_target_transport = {
664 .transport_type = ISCSI_TCP,
665 .rdma_shutdown = false,
667 .iscsit_setup_np = iscsit_setup_np,
668 .iscsit_accept_np = iscsit_accept_np,
669 .iscsit_free_np = iscsit_free_np,
670 .iscsit_get_login_rx = iscsit_get_login_rx,
671 .iscsit_put_login_tx = iscsit_put_login_tx,
672 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
673 .iscsit_immediate_queue = iscsit_immediate_queue,
674 .iscsit_response_queue = iscsit_response_queue,
675 .iscsit_queue_data_in = iscsit_queue_rsp,
676 .iscsit_queue_status = iscsit_queue_rsp,
677 .iscsit_aborted_task = iscsit_aborted_task,
678 .iscsit_xmit_pdu = iscsit_xmit_pdu,
679 .iscsit_get_rx_pdu = iscsit_get_rx_pdu,
680 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
683 static int __init iscsi_target_init_module(void)
687 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
688 iscsit_global = kzalloc(sizeof(*iscsit_global), GFP_KERNEL);
692 spin_lock_init(&iscsit_global->ts_bitmap_lock);
693 mutex_init(&auth_id_lock);
696 ret = target_register_template(&iscsi_ops);
700 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
701 iscsit_global->ts_bitmap = vzalloc(size);
702 if (!iscsit_global->ts_bitmap)
705 lio_qr_cache = kmem_cache_create("lio_qr_cache",
706 sizeof(struct iscsi_queue_req),
707 __alignof__(struct iscsi_queue_req), 0, NULL);
709 pr_err("Unable to kmem_cache_create() for"
714 lio_dr_cache = kmem_cache_create("lio_dr_cache",
715 sizeof(struct iscsi_datain_req),
716 __alignof__(struct iscsi_datain_req), 0, NULL);
718 pr_err("Unable to kmem_cache_create() for"
723 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
724 sizeof(struct iscsi_ooo_cmdsn),
725 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
726 if (!lio_ooo_cache) {
727 pr_err("Unable to kmem_cache_create() for"
732 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
733 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
735 if (!lio_r2t_cache) {
736 pr_err("Unable to kmem_cache_create() for"
741 iscsit_register_transport(&iscsi_target_transport);
743 if (iscsit_load_discovery_tpg() < 0)
748 iscsit_unregister_transport(&iscsi_target_transport);
749 kmem_cache_destroy(lio_r2t_cache);
751 kmem_cache_destroy(lio_ooo_cache);
753 kmem_cache_destroy(lio_dr_cache);
755 kmem_cache_destroy(lio_qr_cache);
757 vfree(iscsit_global->ts_bitmap);
759 /* XXX: this probably wants it to be it's own unwind step.. */
760 if (iscsit_global->discovery_tpg)
761 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
762 target_unregister_template(&iscsi_ops);
764 kfree(iscsit_global);
768 static void __exit iscsi_target_cleanup_module(void)
770 iscsit_release_discovery_tpg();
771 iscsit_unregister_transport(&iscsi_target_transport);
772 kmem_cache_destroy(lio_qr_cache);
773 kmem_cache_destroy(lio_dr_cache);
774 kmem_cache_destroy(lio_ooo_cache);
775 kmem_cache_destroy(lio_r2t_cache);
778 * Shutdown discovery sessions and disable discovery TPG
780 if (iscsit_global->discovery_tpg)
781 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
783 target_unregister_template(&iscsi_ops);
785 vfree(iscsit_global->ts_bitmap);
786 kfree(iscsit_global);
789 int iscsit_add_reject(
790 struct iscsi_conn *conn,
794 struct iscsi_cmd *cmd;
796 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
800 cmd->iscsi_opcode = ISCSI_OP_REJECT;
801 cmd->reject_reason = reason;
803 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
805 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
806 iscsit_free_cmd(cmd, false);
810 spin_lock_bh(&conn->cmd_lock);
811 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
812 spin_unlock_bh(&conn->cmd_lock);
814 cmd->i_state = ISTATE_SEND_REJECT;
815 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
819 EXPORT_SYMBOL(iscsit_add_reject);
821 static int iscsit_add_reject_from_cmd(
822 struct iscsi_cmd *cmd,
827 struct iscsi_conn *conn;
828 const bool do_put = cmd->se_cmd.se_tfo != NULL;
831 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
837 cmd->iscsi_opcode = ISCSI_OP_REJECT;
838 cmd->reject_reason = reason;
840 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
842 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
843 iscsit_free_cmd(cmd, false);
848 spin_lock_bh(&conn->cmd_lock);
849 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
850 spin_unlock_bh(&conn->cmd_lock);
853 cmd->i_state = ISTATE_SEND_REJECT;
854 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
856 * Perform the kref_put now if se_cmd has already been setup by
857 * scsit_setup_scsi_cmd()
860 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
861 target_put_sess_cmd(&cmd->se_cmd);
866 static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
869 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
872 int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
874 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
876 EXPORT_SYMBOL(iscsit_reject_cmd);
879 * Map some portion of the allocated scatterlist to an iovec, suitable for
880 * kernel sockets to copy data in/out.
882 static int iscsit_map_iovec(struct iscsi_cmd *cmd, struct kvec *iov, int nvec,
883 u32 data_offset, u32 data_length)
885 u32 i = 0, orig_data_length = data_length;
886 struct scatterlist *sg;
887 unsigned int page_off;
890 * We know each entry in t_data_sg contains a page.
892 u32 ent = data_offset / PAGE_SIZE;
897 if (ent >= cmd->se_cmd.t_data_nents) {
898 pr_err("Initial page entry out-of-bounds\n");
902 sg = &cmd->se_cmd.t_data_sg[ent];
903 page_off = (data_offset % PAGE_SIZE);
905 cmd->first_data_sg = sg;
906 cmd->first_data_sg_off = page_off;
908 while (data_length) {
911 if (WARN_ON_ONCE(!sg || i >= nvec))
914 cur_len = min_t(u32, data_length, sg->length - page_off);
916 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
917 iov[i].iov_len = cur_len;
919 data_length -= cur_len;
925 cmd->kmapped_nents = i;
930 pr_err("offset %d + length %d overflow; %d/%d; sg-list:\n",
931 data_offset, orig_data_length, i, nvec);
932 for_each_sg(cmd->se_cmd.t_data_sg, sg,
933 cmd->se_cmd.t_data_nents, i) {
934 pr_err("[%d] off %d len %d\n",
935 i, sg->offset, sg->length);
940 static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
943 struct scatterlist *sg;
945 sg = cmd->first_data_sg;
947 for (i = 0; i < cmd->kmapped_nents; i++)
948 kunmap(sg_page(&sg[i]));
951 static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
954 struct iscsi_cmd *cmd, *cmd_p;
956 conn->exp_statsn = exp_statsn;
958 if (conn->sess->sess_ops->RDMAExtensions)
961 spin_lock_bh(&conn->cmd_lock);
962 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
963 spin_lock(&cmd->istate_lock);
964 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
965 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
966 cmd->i_state = ISTATE_REMOVE;
967 spin_unlock(&cmd->istate_lock);
968 list_move_tail(&cmd->i_conn_node, &ack_list);
971 spin_unlock(&cmd->istate_lock);
973 spin_unlock_bh(&conn->cmd_lock);
975 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
976 list_del_init(&cmd->i_conn_node);
977 iscsit_free_cmd(cmd, false);
981 static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
983 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
985 iov_count += ISCSI_IOV_DATA_BUFFER;
986 cmd->iov_data = kcalloc(iov_count, sizeof(*cmd->iov_data), GFP_KERNEL);
990 cmd->orig_iov_data_count = iov_count;
994 int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
997 int data_direction, payload_length;
998 struct iscsi_scsi_req *hdr;
1002 atomic_long_inc(&conn->sess->cmd_pdus);
1004 hdr = (struct iscsi_scsi_req *) buf;
1005 payload_length = ntoh24(hdr->dlength);
1007 /* FIXME; Add checks for AdditionalHeaderSegment */
1009 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
1010 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1011 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1012 " not set. Bad iSCSI Initiator.\n");
1013 return iscsit_add_reject_cmd(cmd,
1014 ISCSI_REASON_BOOKMARK_INVALID, buf);
1017 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
1018 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
1020 * From RFC-3720 Section 10.3.1:
1022 * "Either or both of R and W MAY be 1 when either the
1023 * Expected Data Transfer Length and/or Bidirectional Read
1024 * Expected Data Transfer Length are 0"
1026 * For this case, go ahead and clear the unnecssary bits
1027 * to avoid any confusion with ->data_direction.
1029 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
1030 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
1032 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
1033 " set when Expected Data Transfer Length is 0 for"
1034 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
1037 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
1038 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
1039 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1040 " MUST be set if Expected Data Transfer Length is not 0."
1041 " Bad iSCSI Initiator\n");
1042 return iscsit_add_reject_cmd(cmd,
1043 ISCSI_REASON_BOOKMARK_INVALID, buf);
1046 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
1047 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
1048 pr_err("Bidirectional operations not supported!\n");
1049 return iscsit_add_reject_cmd(cmd,
1050 ISCSI_REASON_BOOKMARK_INVALID, buf);
1053 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1054 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1055 " Scsi Command PDU.\n");
1056 return iscsit_add_reject_cmd(cmd,
1057 ISCSI_REASON_BOOKMARK_INVALID, buf);
1060 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
1061 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1062 " protocol error.\n", payload_length);
1063 return iscsit_add_reject_cmd(cmd,
1064 ISCSI_REASON_PROTOCOL_ERROR, buf);
1067 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
1068 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
1069 pr_err("Expected Data Transfer Length and Length of"
1070 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1071 " bit is not set protocol error\n");
1072 return iscsit_add_reject_cmd(cmd,
1073 ISCSI_REASON_PROTOCOL_ERROR, buf);
1076 if (payload_length > be32_to_cpu(hdr->data_length)) {
1077 pr_err("DataSegmentLength: %u is greater than"
1078 " EDTL: %u, protocol error.\n", payload_length,
1080 return iscsit_add_reject_cmd(cmd,
1081 ISCSI_REASON_PROTOCOL_ERROR, buf);
1084 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1085 pr_err("DataSegmentLength: %u is greater than"
1086 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1087 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
1088 return iscsit_add_reject_cmd(cmd,
1089 ISCSI_REASON_PROTOCOL_ERROR, buf);
1092 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
1093 pr_err("DataSegmentLength: %u is greater than"
1094 " FirstBurstLength: %u, protocol error.\n",
1095 payload_length, conn->sess->sess_ops->FirstBurstLength);
1096 return iscsit_add_reject_cmd(cmd,
1097 ISCSI_REASON_BOOKMARK_INVALID, buf);
1100 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
1101 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
1104 cmd->data_direction = data_direction;
1105 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
1107 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1109 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
1110 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
1111 sam_task_attr = TCM_SIMPLE_TAG;
1112 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
1113 sam_task_attr = TCM_ORDERED_TAG;
1114 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
1115 sam_task_attr = TCM_HEAD_TAG;
1116 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
1117 sam_task_attr = TCM_ACA_TAG;
1119 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
1120 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
1121 sam_task_attr = TCM_SIMPLE_TAG;
1124 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
1125 cmd->i_state = ISTATE_NEW_CMD;
1126 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1127 cmd->immediate_data = (payload_length) ? 1 : 0;
1128 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
1129 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
1130 if (cmd->unsolicited_data)
1131 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
1133 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1134 if (hdr->flags & ISCSI_FLAG_CMD_READ)
1135 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
1137 cmd->targ_xfer_tag = 0xFFFFFFFF;
1138 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1139 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1140 cmd->first_burst_len = payload_length;
1142 if (!conn->sess->sess_ops->RDMAExtensions &&
1143 cmd->data_direction == DMA_FROM_DEVICE) {
1144 struct iscsi_datain_req *dr;
1146 dr = iscsit_allocate_datain_req();
1148 return iscsit_add_reject_cmd(cmd,
1149 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1151 iscsit_attach_datain_req(cmd, dr);
1155 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1157 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
1158 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
1159 cmd->data_direction, sam_task_attr,
1160 cmd->sense_buffer + 2, scsilun_to_int(&hdr->lun));
1162 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1163 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
1164 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1167 target_get_sess_cmd(&cmd->se_cmd, true);
1169 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
1170 cmd->sense_reason = target_cmd_init_cdb(&cmd->se_cmd, hdr->cdb);
1171 if (cmd->sense_reason) {
1172 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
1173 return iscsit_add_reject_cmd(cmd,
1174 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1180 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd);
1181 if (cmd->sense_reason)
1184 cmd->sense_reason = target_cmd_parse_cdb(&cmd->se_cmd);
1185 if (cmd->sense_reason)
1188 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
1189 return iscsit_add_reject_cmd(cmd,
1190 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1194 spin_lock_bh(&conn->cmd_lock);
1195 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1196 spin_unlock_bh(&conn->cmd_lock);
1198 * Check if we need to delay processing because of ALUA
1199 * Active/NonOptimized primary access state..
1201 core_alua_check_nonop_delay(&cmd->se_cmd);
1205 EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
1207 void iscsit_set_unsolicited_dataout(struct iscsi_cmd *cmd)
1209 iscsit_set_dataout_sequence_values(cmd);
1211 spin_lock_bh(&cmd->dataout_timeout_lock);
1212 iscsit_start_dataout_timer(cmd, cmd->conn);
1213 spin_unlock_bh(&cmd->dataout_timeout_lock);
1215 EXPORT_SYMBOL(iscsit_set_unsolicited_dataout);
1217 int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1218 struct iscsi_scsi_req *hdr)
1222 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1223 * the Immediate Bit is not set, and no Immediate
1226 * A PDU/CmdSN carrying Immediate Data can only
1227 * be processed after the DataCRC has passed.
1228 * If the DataCRC fails, the CmdSN MUST NOT
1229 * be acknowledged. (See below)
1231 if (!cmd->immediate_data) {
1232 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1233 (unsigned char *)hdr, hdr->cmdsn);
1234 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1236 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1237 target_put_sess_cmd(&cmd->se_cmd);
1242 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1245 * If no Immediate Data is attached, it's OK to return now.
1247 if (!cmd->immediate_data) {
1248 if (!cmd->sense_reason && cmd->unsolicited_data)
1249 iscsit_set_unsolicited_dataout(cmd);
1250 if (!cmd->sense_reason)
1253 target_put_sess_cmd(&cmd->se_cmd);
1258 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1259 * execution. These exceptions are processed in CmdSN order using
1260 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1262 if (cmd->sense_reason)
1265 * Call directly into transport_generic_new_cmd() to perform
1266 * the backend memory allocation.
1268 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
1269 if (cmd->sense_reason)
1274 EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1277 iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1280 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1284 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1287 u32 length = min(cmd->se_cmd.data_length - cmd->write_data_done,
1288 cmd->first_burst_len);
1290 pr_debug("Dumping min(%d - %d, %d) = %d bytes of immediate data\n",
1291 cmd->se_cmd.data_length, cmd->write_data_done,
1292 cmd->first_burst_len, length);
1293 rc = iscsit_dump_data_payload(cmd->conn, length, 1);
1294 pr_debug("Finished dumping immediate data\n");
1296 immed_ret = IMMEDIATE_DATA_CANNOT_RECOVER;
1298 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1299 cmd->first_burst_len);
1302 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1304 * A PDU/CmdSN carrying Immediate Data passed
1305 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1306 * Immediate Bit is not set.
1308 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1309 (unsigned char *)hdr, hdr->cmdsn);
1310 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1313 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1314 target_put_sess_cmd(&cmd->se_cmd);
1317 } else if (cmd->unsolicited_data)
1318 iscsit_set_unsolicited_dataout(cmd);
1320 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1322 * Immediate Data failed DataCRC and ERL>=1,
1323 * silently drop this PDU and let the initiator
1324 * plug the CmdSN gap.
1326 * FIXME: Send Unsolicited NOPIN with reserved
1327 * TTT here to help the initiator figure out
1328 * the missing CmdSN, although they should be
1329 * intelligent enough to determine the missing
1330 * CmdSN and issue a retry to plug the sequence.
1332 cmd->i_state = ISTATE_REMOVE;
1333 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
1334 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1341 iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1344 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1346 bool dump_payload = false;
1348 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1352 * Allocation iovecs needed for struct socket operations for
1353 * traditional iSCSI block I/O.
1355 if (iscsit_allocate_iovecs(cmd) < 0) {
1356 return iscsit_reject_cmd(cmd,
1357 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1359 immed_data = cmd->immediate_data;
1361 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1365 dump_payload = true;
1370 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1373 static u32 iscsit_do_crypto_hash_sg(
1374 struct ahash_request *hash,
1375 struct iscsi_cmd *cmd,
1382 struct scatterlist *sg;
1383 unsigned int page_off;
1385 crypto_ahash_init(hash);
1387 sg = cmd->first_data_sg;
1388 page_off = cmd->first_data_sg_off;
1390 if (data_length && page_off) {
1391 struct scatterlist first_sg;
1392 u32 len = min_t(u32, data_length, sg->length - page_off);
1394 sg_init_table(&first_sg, 1);
1395 sg_set_page(&first_sg, sg_page(sg), len, sg->offset + page_off);
1397 ahash_request_set_crypt(hash, &first_sg, NULL, len);
1398 crypto_ahash_update(hash);
1404 while (data_length) {
1405 u32 cur_len = min_t(u32, data_length, sg->length);
1407 ahash_request_set_crypt(hash, sg, NULL, cur_len);
1408 crypto_ahash_update(hash);
1410 data_length -= cur_len;
1411 /* iscsit_map_iovec has already checked for invalid sg pointers */
1416 struct scatterlist pad_sg;
1418 sg_init_one(&pad_sg, pad_bytes, padding);
1419 ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
1421 crypto_ahash_finup(hash);
1423 ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
1424 crypto_ahash_final(hash);
1430 static void iscsit_do_crypto_hash_buf(struct ahash_request *hash,
1431 const void *buf, u32 payload_length, u32 padding,
1432 const void *pad_bytes, void *data_crc)
1434 struct scatterlist sg[2];
1436 sg_init_table(sg, ARRAY_SIZE(sg));
1437 sg_set_buf(sg, buf, payload_length);
1439 sg_set_buf(sg + 1, pad_bytes, padding);
1441 ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
1443 crypto_ahash_digest(hash);
1447 __iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1448 struct iscsi_cmd *cmd, u32 payload_length,
1451 struct iscsi_data *hdr = buf;
1452 struct se_cmd *se_cmd;
1456 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
1458 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1459 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1460 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
1461 payload_length, conn->cid);
1463 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1464 pr_err("Command ITT: 0x%08x received DataOUT after"
1465 " last DataOUT received, dumping payload\n",
1466 cmd->init_task_tag);
1467 return iscsit_dump_data_payload(conn, payload_length, 1);
1470 if (cmd->data_direction != DMA_TO_DEVICE) {
1471 pr_err("Command ITT: 0x%08x received DataOUT for a"
1472 " NON-WRITE command.\n", cmd->init_task_tag);
1473 return iscsit_dump_data_payload(conn, payload_length, 1);
1475 se_cmd = &cmd->se_cmd;
1476 iscsit_mod_dataout_timer(cmd);
1478 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
1479 pr_err("DataOut Offset: %u, Length %u greater than iSCSI Command EDTL %u, protocol error.\n",
1480 be32_to_cpu(hdr->offset), payload_length,
1481 cmd->se_cmd.data_length);
1482 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
1485 if (cmd->unsolicited_data) {
1486 int dump_unsolicited_data = 0;
1488 if (conn->sess->sess_ops->InitialR2T) {
1489 pr_err("Received unexpected unsolicited data"
1490 " while InitialR2T=Yes, protocol error.\n");
1491 transport_send_check_condition_and_sense(&cmd->se_cmd,
1492 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1496 * Special case for dealing with Unsolicited DataOUT
1497 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1501 /* Something's amiss if we're not in WRITE_PENDING state... */
1502 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1503 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
1504 dump_unsolicited_data = 1;
1506 if (dump_unsolicited_data) {
1508 * Check if a delayed TASK_ABORTED status needs to
1509 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1510 * received with the unsolicited data out.
1512 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1513 iscsit_stop_dataout_timer(cmd);
1515 return iscsit_dump_data_payload(conn, payload_length, 1);
1519 * For the normal solicited data path:
1521 * Check for a delayed TASK_ABORTED status and dump any
1522 * incoming data out payload if one exists. Also, when the
1523 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1524 * data out sequence, we decrement outstanding_r2ts. Once
1525 * outstanding_r2ts reaches zero, go ahead and send the delayed
1526 * TASK_ABORTED status.
1528 if (se_cmd->transport_state & CMD_T_ABORTED) {
1529 if (hdr->flags & ISCSI_FLAG_CMD_FINAL &&
1530 --cmd->outstanding_r2ts < 1)
1531 iscsit_stop_dataout_timer(cmd);
1533 return iscsit_dump_data_payload(conn, payload_length, 1);
1537 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1538 * within-command recovery checks before receiving the payload.
1540 rc = iscsit_check_pre_dataout(cmd, buf);
1541 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
1543 else if (rc == DATAOUT_CANNOT_RECOVER)
1548 EXPORT_SYMBOL(__iscsit_check_dataout_hdr);
1551 iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1552 struct iscsi_cmd **out_cmd)
1554 struct iscsi_data *hdr = buf;
1555 struct iscsi_cmd *cmd;
1556 u32 payload_length = ntoh24(hdr->dlength);
1558 bool success = false;
1560 if (!payload_length) {
1561 pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
1565 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1566 pr_err_ratelimited("DataSegmentLength: %u is greater than"
1567 " MaxXmitDataSegmentLength: %u\n", payload_length,
1568 conn->conn_ops->MaxXmitDataSegmentLength);
1569 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buf);
1572 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, payload_length);
1576 rc = __iscsit_check_dataout_hdr(conn, buf, cmd, payload_length, &success);
1583 EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1586 iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1587 struct iscsi_data *hdr)
1590 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1592 int iov_ret, data_crc_failed = 0;
1594 payload_length = min_t(u32, cmd->se_cmd.data_length,
1595 ntoh24(hdr->dlength));
1596 rx_size += payload_length;
1597 iov = &cmd->iov_data[0];
1599 iov_ret = iscsit_map_iovec(cmd, iov, cmd->orig_iov_data_count - 2,
1600 be32_to_cpu(hdr->offset), payload_length);
1604 iov_count += iov_ret;
1606 padding = ((-payload_length) & 3);
1608 iov[iov_count].iov_base = cmd->pad_bytes;
1609 iov[iov_count++].iov_len = padding;
1611 pr_debug("Receiving %u padding bytes.\n", padding);
1614 if (conn->conn_ops->DataDigest) {
1615 iov[iov_count].iov_base = &checksum;
1616 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1617 rx_size += ISCSI_CRC_LEN;
1620 WARN_ON_ONCE(iov_count > cmd->orig_iov_data_count);
1621 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1623 iscsit_unmap_iovec(cmd);
1625 if (rx_got != rx_size)
1628 if (conn->conn_ops->DataDigest) {
1631 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
1632 be32_to_cpu(hdr->offset),
1633 payload_length, padding,
1636 if (checksum != data_crc) {
1637 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1638 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1639 " does not match computed 0x%08x\n",
1640 hdr->itt, hdr->offset, payload_length,
1641 hdr->datasn, checksum, data_crc);
1642 data_crc_failed = 1;
1644 pr_debug("Got CRC32C DataDigest 0x%08x for"
1645 " %u bytes of Data Out\n", checksum,
1650 return data_crc_failed;
1654 iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1655 bool data_crc_failed)
1657 struct iscsi_conn *conn = cmd->conn;
1660 * Increment post receive data and CRC values or perform
1661 * within-command recovery.
1663 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1664 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
1666 else if (rc == DATAOUT_SEND_R2T) {
1667 iscsit_set_dataout_sequence_values(cmd);
1668 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1669 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
1671 * Handle extra special case for out of order
1672 * Unsolicited Data Out.
1674 spin_lock_bh(&cmd->istate_lock);
1675 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1676 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1677 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1678 spin_unlock_bh(&cmd->istate_lock);
1680 iscsit_stop_dataout_timer(cmd);
1683 target_execute_cmd(&cmd->se_cmd);
1685 } else /* DATAOUT_CANNOT_RECOVER */
1690 EXPORT_SYMBOL(iscsit_check_dataout_payload);
1692 static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1694 struct iscsi_cmd *cmd = NULL;
1695 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1697 bool data_crc_failed = false;
1699 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1705 rc = iscsit_get_dataout(conn, cmd, hdr);
1709 data_crc_failed = true;
1711 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1714 int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1715 struct iscsi_nopout *hdr)
1717 u32 payload_length = ntoh24(hdr->dlength);
1719 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1720 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1722 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1723 (unsigned char *)hdr);
1725 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1726 (unsigned char *)hdr);
1729 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1730 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1731 " not set, protocol error.\n");
1733 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1734 (unsigned char *)hdr);
1736 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1737 (unsigned char *)hdr);
1740 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1741 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1742 " greater than MaxXmitDataSegmentLength: %u, protocol"
1743 " error.\n", payload_length,
1744 conn->conn_ops->MaxXmitDataSegmentLength);
1746 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1747 (unsigned char *)hdr);
1749 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1750 (unsigned char *)hdr);
1753 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1754 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1755 hdr->itt == RESERVED_ITT ? "Response" : "Request",
1756 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1759 * This is not a response to a Unsolicited NopIN, which means
1760 * it can either be a NOPOUT ping request (with a valid ITT),
1761 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1762 * Either way, make sure we allocate an struct iscsi_cmd, as both
1763 * can contain ping data.
1765 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1766 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1767 cmd->i_state = ISTATE_SEND_NOPIN;
1768 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1770 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1771 cmd->targ_xfer_tag = 0xFFFFFFFF;
1772 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1773 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1774 cmd->data_direction = DMA_NONE;
1779 EXPORT_SYMBOL(iscsit_setup_nop_out);
1781 int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1782 struct iscsi_nopout *hdr)
1784 struct iscsi_cmd *cmd_p = NULL;
1787 * Initiator is expecting a NopIN ping reply..
1789 if (hdr->itt != RESERVED_ITT) {
1791 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1792 (unsigned char *)hdr);
1794 spin_lock_bh(&conn->cmd_lock);
1795 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1796 spin_unlock_bh(&conn->cmd_lock);
1798 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1800 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1801 iscsit_add_cmd_to_response_queue(cmd, conn,
1806 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1807 (unsigned char *)hdr, hdr->cmdsn);
1808 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1810 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1816 * This was a response to a unsolicited NOPIN ping.
1818 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1819 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1823 iscsit_stop_nopin_response_timer(conn);
1825 cmd_p->i_state = ISTATE_REMOVE;
1826 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1828 iscsit_start_nopin_timer(conn);
1832 * Otherwise, initiator is not expecting a NOPIN is response.
1833 * Just ignore for now.
1837 iscsit_free_cmd(cmd, false);
1841 EXPORT_SYMBOL(iscsit_process_nop_out);
1843 static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1846 unsigned char *ping_data = NULL;
1847 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1848 struct kvec *iov = NULL;
1849 u32 payload_length = ntoh24(hdr->dlength);
1852 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1856 * Handle NOP-OUT payload for traditional iSCSI sockets
1858 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1859 u32 checksum, data_crc, padding = 0;
1860 int niov = 0, rx_got, rx_size = payload_length;
1862 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1868 iov = &cmd->iov_misc[0];
1869 iov[niov].iov_base = ping_data;
1870 iov[niov++].iov_len = payload_length;
1872 padding = ((-payload_length) & 3);
1874 pr_debug("Receiving %u additional bytes"
1875 " for padding.\n", padding);
1876 iov[niov].iov_base = &cmd->pad_bytes;
1877 iov[niov++].iov_len = padding;
1880 if (conn->conn_ops->DataDigest) {
1881 iov[niov].iov_base = &checksum;
1882 iov[niov++].iov_len = ISCSI_CRC_LEN;
1883 rx_size += ISCSI_CRC_LEN;
1886 WARN_ON_ONCE(niov > ARRAY_SIZE(cmd->iov_misc));
1887 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1888 if (rx_got != rx_size) {
1893 if (conn->conn_ops->DataDigest) {
1894 iscsit_do_crypto_hash_buf(conn->conn_rx_hash, ping_data,
1895 payload_length, padding,
1896 cmd->pad_bytes, &data_crc);
1898 if (checksum != data_crc) {
1899 pr_err("Ping data CRC32C DataDigest"
1900 " 0x%08x does not match computed 0x%08x\n",
1901 checksum, data_crc);
1902 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1903 pr_err("Unable to recover from"
1904 " NOPOUT Ping DataCRC failure while in"
1910 * Silently drop this PDU and let the
1911 * initiator plug the CmdSN gap.
1913 pr_debug("Dropping NOPOUT"
1914 " Command CmdSN: 0x%08x due to"
1915 " DataCRC error.\n", hdr->cmdsn);
1920 pr_debug("Got CRC32C DataDigest"
1921 " 0x%08x for %u bytes of ping data.\n",
1922 checksum, payload_length);
1926 ping_data[payload_length] = '\0';
1928 * Attach ping data to struct iscsi_cmd->buf_ptr.
1930 cmd->buf_ptr = ping_data;
1931 cmd->buf_ptr_size = payload_length;
1933 pr_debug("Got %u bytes of NOPOUT ping"
1934 " data.\n", payload_length);
1935 pr_debug("Ping Data: \"%s\"\n", ping_data);
1938 return iscsit_process_nop_out(conn, cmd, hdr);
1941 iscsit_free_cmd(cmd, false);
1947 static enum tcm_tmreq_table iscsit_convert_tmf(u8 iscsi_tmf)
1949 switch (iscsi_tmf) {
1950 case ISCSI_TM_FUNC_ABORT_TASK:
1951 return TMR_ABORT_TASK;
1952 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1953 return TMR_ABORT_TASK_SET;
1954 case ISCSI_TM_FUNC_CLEAR_ACA:
1955 return TMR_CLEAR_ACA;
1956 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1957 return TMR_CLEAR_TASK_SET;
1958 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1959 return TMR_LUN_RESET;
1960 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1961 return TMR_TARGET_WARM_RESET;
1962 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1963 return TMR_TARGET_COLD_RESET;
1970 iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1973 struct se_tmr_req *se_tmr;
1974 struct iscsi_tmr_req *tmr_req;
1975 struct iscsi_tm *hdr;
1976 int out_of_order_cmdsn = 0, ret;
1977 u8 function, tcm_function = TMR_UNKNOWN;
1979 hdr = (struct iscsi_tm *) buf;
1980 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1981 function = hdr->flags;
1983 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1984 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1985 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1986 hdr->rtt, hdr->refcmdsn, conn->cid);
1988 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1989 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1990 hdr->rtt != RESERVED_ITT)) {
1991 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1992 hdr->rtt = RESERVED_ITT;
1995 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1996 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1997 pr_err("Task Management Request TASK_REASSIGN not"
1998 " issued as immediate command, bad iSCSI Initiator"
1999 "implementation\n");
2000 return iscsit_add_reject_cmd(cmd,
2001 ISCSI_REASON_PROTOCOL_ERROR, buf);
2003 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
2004 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
2005 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
2007 cmd->data_direction = DMA_NONE;
2008 cmd->tmr_req = kzalloc(sizeof(*cmd->tmr_req), GFP_KERNEL);
2009 if (!cmd->tmr_req) {
2010 return iscsit_add_reject_cmd(cmd,
2011 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
2015 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
2016 conn->sess->se_sess, 0, DMA_NONE,
2017 TCM_SIMPLE_TAG, cmd->sense_buffer + 2,
2018 scsilun_to_int(&hdr->lun));
2020 target_get_sess_cmd(&cmd->se_cmd, true);
2023 * TASK_REASSIGN for ERL=2 / connection stays inside of
2024 * LIO-Target $FABRIC_MOD
2026 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
2027 tcm_function = iscsit_convert_tmf(function);
2028 if (tcm_function == TMR_UNKNOWN) {
2029 pr_err("Unknown iSCSI TMR Function:"
2030 " 0x%02x\n", function);
2031 return iscsit_add_reject_cmd(cmd,
2032 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
2035 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, tcm_function,
2038 return iscsit_add_reject_cmd(cmd,
2039 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
2041 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
2043 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
2044 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
2045 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2046 cmd->init_task_tag = hdr->itt;
2047 cmd->targ_xfer_tag = 0xFFFFFFFF;
2048 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2049 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2050 se_tmr = cmd->se_cmd.se_tmr_req;
2051 tmr_req = cmd->tmr_req;
2053 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2055 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
2056 ret = transport_lookup_tmr_lun(&cmd->se_cmd);
2058 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2064 case ISCSI_TM_FUNC_ABORT_TASK:
2065 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
2066 if (se_tmr->response)
2069 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2070 case ISCSI_TM_FUNC_CLEAR_ACA:
2071 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2072 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2074 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2075 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
2076 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2080 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2081 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
2082 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2086 case ISCSI_TM_FUNC_TASK_REASSIGN:
2087 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2089 * Perform sanity checks on the ExpDataSN only if the
2090 * TASK_REASSIGN was successful.
2092 if (se_tmr->response)
2095 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
2096 return iscsit_add_reject_cmd(cmd,
2097 ISCSI_REASON_BOOKMARK_INVALID, buf);
2100 pr_err("Unknown TMR function: 0x%02x, protocol"
2101 " error.\n", function);
2102 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2106 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2107 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2108 se_tmr->call_transport = 1;
2110 spin_lock_bh(&conn->cmd_lock);
2111 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2112 spin_unlock_bh(&conn->cmd_lock);
2114 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2115 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
2116 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP) {
2117 out_of_order_cmdsn = 1;
2118 } else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2119 target_put_sess_cmd(&cmd->se_cmd);
2121 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
2125 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2127 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
2130 * Found the referenced task, send to transport for processing.
2132 if (se_tmr->call_transport)
2133 return transport_generic_handle_tmr(&cmd->se_cmd);
2136 * Could not find the referenced LUN, task, or Task Management
2137 * command not authorized or supported. Change state and
2138 * let the tx_thread send the response.
2140 * For connection recovery, this is also the default action for
2141 * TMR TASK_REASSIGN.
2143 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2144 target_put_sess_cmd(&cmd->se_cmd);
2147 EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
2149 /* #warning FIXME: Support Text Command parameters besides SendTargets */
2151 iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2152 struct iscsi_text *hdr)
2154 u32 payload_length = ntoh24(hdr->dlength);
2156 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
2157 pr_err("Unable to accept text parameter length: %u"
2158 "greater than MaxXmitDataSegmentLength %u.\n",
2159 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
2160 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2161 (unsigned char *)hdr);
2164 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2165 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2166 pr_err("Multi sequence text commands currently not supported\n");
2167 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2168 (unsigned char *)hdr);
2171 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2172 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2173 hdr->exp_statsn, payload_length);
2175 cmd->iscsi_opcode = ISCSI_OP_TEXT;
2176 cmd->i_state = ISTATE_SEND_TEXTRSP;
2177 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2178 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2179 cmd->targ_xfer_tag = 0xFFFFFFFF;
2180 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2181 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2182 cmd->data_direction = DMA_NONE;
2183 kfree(cmd->text_in_ptr);
2184 cmd->text_in_ptr = NULL;
2188 EXPORT_SYMBOL(iscsit_setup_text_cmd);
2191 iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2192 struct iscsi_text *hdr)
2194 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
2198 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2199 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2200 pr_err("Unable to locate text_in buffer for sendtargets"
2204 goto empty_sendtargets;
2206 if (strncmp("SendTargets=", text_in, 12) != 0) {
2207 pr_err("Received Text Data that is not"
2208 " SendTargets, cannot continue.\n");
2211 /* '=' confirmed in strncmp */
2212 text_ptr = strchr(text_in, '=');
2214 if (!strncmp("=All", text_ptr, 5)) {
2215 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
2216 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2217 !strncmp("=eui.", text_ptr, 5)) {
2218 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
2220 pr_err("Unable to locate valid SendTargets%s value\n",
2225 spin_lock_bh(&conn->cmd_lock);
2226 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2227 spin_unlock_bh(&conn->cmd_lock);
2230 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2232 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2233 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2234 (unsigned char *)hdr, hdr->cmdsn);
2235 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2241 return iscsit_execute_cmd(cmd, 0);
2244 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2245 (unsigned char *)hdr);
2247 EXPORT_SYMBOL(iscsit_process_text_cmd);
2250 iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2253 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2254 char *text_in = NULL;
2255 u32 payload_length = ntoh24(hdr->dlength);
2258 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2262 rx_size = payload_length;
2263 if (payload_length) {
2264 u32 checksum = 0, data_crc = 0;
2266 int niov = 0, rx_got;
2269 rx_size = ALIGN(payload_length, 4);
2270 text_in = kzalloc(rx_size, GFP_KERNEL);
2274 cmd->text_in_ptr = text_in;
2276 memset(iov, 0, sizeof(iov));
2277 iov[niov].iov_base = text_in;
2278 iov[niov++].iov_len = rx_size;
2280 padding = rx_size - payload_length;
2282 pr_debug("Receiving %u additional bytes"
2283 " for padding.\n", padding);
2284 if (conn->conn_ops->DataDigest) {
2285 iov[niov].iov_base = &checksum;
2286 iov[niov++].iov_len = ISCSI_CRC_LEN;
2287 rx_size += ISCSI_CRC_LEN;
2290 WARN_ON_ONCE(niov > ARRAY_SIZE(iov));
2291 rx_got = rx_data(conn, &iov[0], niov, rx_size);
2292 if (rx_got != rx_size)
2295 if (conn->conn_ops->DataDigest) {
2296 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
2297 text_in, rx_size, 0, NULL,
2300 if (checksum != data_crc) {
2301 pr_err("Text data CRC32C DataDigest"
2302 " 0x%08x does not match computed"
2303 " 0x%08x\n", checksum, data_crc);
2304 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2305 pr_err("Unable to recover from"
2306 " Text Data digest failure while in"
2311 * Silently drop this PDU and let the
2312 * initiator plug the CmdSN gap.
2314 pr_debug("Dropping Text"
2315 " Command CmdSN: 0x%08x due to"
2316 " DataCRC error.\n", hdr->cmdsn);
2321 pr_debug("Got CRC32C DataDigest"
2322 " 0x%08x for %u bytes of text data.\n",
2323 checksum, payload_length);
2326 text_in[payload_length - 1] = '\0';
2327 pr_debug("Successfully read %d bytes of text"
2328 " data.\n", payload_length);
2331 return iscsit_process_text_cmd(conn, cmd, hdr);
2334 kfree(cmd->text_in_ptr);
2335 cmd->text_in_ptr = NULL;
2336 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
2339 int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2341 struct iscsi_conn *conn_p;
2342 struct iscsi_session *sess = conn->sess;
2344 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2345 " for SID: %u.\n", conn->cid, conn->sess->sid);
2347 atomic_set(&sess->session_logout, 1);
2348 atomic_set(&conn->conn_logout_remove, 1);
2349 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2351 iscsit_inc_conn_usage_count(conn);
2352 iscsit_inc_session_usage_count(sess);
2354 spin_lock_bh(&sess->conn_lock);
2355 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2356 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2359 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2360 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2362 spin_unlock_bh(&sess->conn_lock);
2364 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2369 int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2371 struct iscsi_conn *l_conn;
2372 struct iscsi_session *sess = conn->sess;
2374 pr_debug("Received logout request CLOSECONNECTION for CID:"
2375 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2378 * A Logout Request with a CLOSECONNECTION reason code for a CID
2379 * can arrive on a connection with a differing CID.
2381 if (conn->cid == cmd->logout_cid) {
2382 spin_lock_bh(&conn->state_lock);
2383 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2384 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2386 atomic_set(&conn->conn_logout_remove, 1);
2387 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2388 iscsit_inc_conn_usage_count(conn);
2390 spin_unlock_bh(&conn->state_lock);
2393 * Handle all different cid CLOSECONNECTION requests in
2394 * iscsit_logout_post_handler_diffcid() as to give enough
2395 * time for any non immediate command's CmdSN to be
2396 * acknowledged on the connection in question.
2398 * Here we simply make sure the CID is still around.
2400 l_conn = iscsit_get_conn_from_cid(sess,
2403 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2404 iscsit_add_cmd_to_response_queue(cmd, conn,
2409 iscsit_dec_conn_usage_count(l_conn);
2412 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2417 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2419 struct iscsi_session *sess = conn->sess;
2421 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2422 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2424 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2425 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2426 " while ERL!=2.\n");
2427 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2428 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2432 if (conn->cid == cmd->logout_cid) {
2433 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2434 " with CID: %hu on CID: %hu, implementation error.\n",
2435 cmd->logout_cid, conn->cid);
2436 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2437 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2441 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2447 iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2450 int cmdsn_ret, logout_remove = 0;
2452 struct iscsi_logout *hdr;
2453 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2455 hdr = (struct iscsi_logout *) buf;
2456 reason_code = (hdr->flags & 0x7f);
2459 spin_lock(&tiqn->logout_stats.lock);
2460 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2461 tiqn->logout_stats.normal_logouts++;
2463 tiqn->logout_stats.abnormal_logouts++;
2464 spin_unlock(&tiqn->logout_stats.lock);
2467 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2468 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2469 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2470 hdr->cid, conn->cid);
2472 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2473 pr_err("Received logout request on connection that"
2474 " is not in logged in state, ignoring request.\n");
2475 iscsit_free_cmd(cmd, false);
2479 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2480 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2481 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2482 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2483 cmd->targ_xfer_tag = 0xFFFFFFFF;
2484 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2485 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2486 cmd->logout_cid = be16_to_cpu(hdr->cid);
2487 cmd->logout_reason = reason_code;
2488 cmd->data_direction = DMA_NONE;
2491 * We need to sleep in these cases (by returning 1) until the Logout
2492 * Response gets sent in the tx thread.
2494 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2495 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
2496 be16_to_cpu(hdr->cid) == conn->cid))
2499 spin_lock_bh(&conn->cmd_lock);
2500 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2501 spin_unlock_bh(&conn->cmd_lock);
2503 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
2504 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2507 * Immediate commands are executed, well, immediately.
2508 * Non-Immediate Logout Commands are executed in CmdSN order.
2510 if (cmd->immediate_cmd) {
2511 int ret = iscsit_execute_cmd(cmd, 0);
2516 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
2517 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
2519 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2523 return logout_remove;
2525 EXPORT_SYMBOL(iscsit_handle_logout_cmd);
2527 int iscsit_handle_snack(
2528 struct iscsi_conn *conn,
2531 struct iscsi_snack *hdr;
2533 hdr = (struct iscsi_snack *) buf;
2534 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
2536 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2537 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2538 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2539 hdr->begrun, hdr->runlength, conn->cid);
2541 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2542 pr_err("Initiator sent SNACK request while in"
2543 " ErrorRecoveryLevel=0.\n");
2544 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2548 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2549 * call from inside iscsi_send_recovery_datain_or_r2t().
2551 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2553 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
2555 be32_to_cpu(hdr->ttt),
2556 be32_to_cpu(hdr->begrun),
2557 be32_to_cpu(hdr->runlength));
2558 case ISCSI_FLAG_SNACK_TYPE_STATUS:
2559 return iscsit_handle_status_snack(conn, hdr->itt,
2560 be32_to_cpu(hdr->ttt),
2561 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
2562 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
2563 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2564 be32_to_cpu(hdr->begrun),
2565 be32_to_cpu(hdr->runlength));
2566 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2567 /* FIXME: Support R-Data SNACK */
2568 pr_err("R-Data SNACK Not Supported.\n");
2569 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2572 pr_err("Unknown SNACK type 0x%02x, protocol"
2573 " error.\n", hdr->flags & 0x0f);
2574 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2580 EXPORT_SYMBOL(iscsit_handle_snack);
2582 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2584 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2585 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2586 wait_for_completion_interruptible_timeout(
2587 &conn->rx_half_close_comp,
2588 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2592 static int iscsit_handle_immediate_data(
2593 struct iscsi_cmd *cmd,
2594 struct iscsi_scsi_req *hdr,
2597 int iov_ret, rx_got = 0, rx_size = 0;
2598 u32 checksum, iov_count = 0, padding = 0;
2599 struct iscsi_conn *conn = cmd->conn;
2601 void *overflow_buf = NULL;
2603 BUG_ON(cmd->write_data_done > cmd->se_cmd.data_length);
2604 rx_size = min(cmd->se_cmd.data_length - cmd->write_data_done, length);
2605 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data,
2606 cmd->orig_iov_data_count - 2,
2607 cmd->write_data_done, rx_size);
2609 return IMMEDIATE_DATA_CANNOT_RECOVER;
2611 iov_count = iov_ret;
2612 iov = &cmd->iov_data[0];
2613 if (rx_size < length) {
2615 * Special case: length of immediate data exceeds the data
2616 * buffer size derived from the CDB.
2618 overflow_buf = kmalloc(length - rx_size, GFP_KERNEL);
2619 if (!overflow_buf) {
2620 iscsit_unmap_iovec(cmd);
2621 return IMMEDIATE_DATA_CANNOT_RECOVER;
2623 cmd->overflow_buf = overflow_buf;
2624 iov[iov_count].iov_base = overflow_buf;
2625 iov[iov_count].iov_len = length - rx_size;
2630 padding = ((-length) & 3);
2632 iov[iov_count].iov_base = cmd->pad_bytes;
2633 iov[iov_count++].iov_len = padding;
2637 if (conn->conn_ops->DataDigest) {
2638 iov[iov_count].iov_base = &checksum;
2639 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2640 rx_size += ISCSI_CRC_LEN;
2643 WARN_ON_ONCE(iov_count > cmd->orig_iov_data_count);
2644 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2646 iscsit_unmap_iovec(cmd);
2648 if (rx_got != rx_size) {
2649 iscsit_rx_thread_wait_for_tcp(conn);
2650 return IMMEDIATE_DATA_CANNOT_RECOVER;
2653 if (conn->conn_ops->DataDigest) {
2656 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
2657 cmd->write_data_done, length, padding,
2660 if (checksum != data_crc) {
2661 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2662 " does not match computed 0x%08x\n", checksum,
2665 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2666 pr_err("Unable to recover from"
2667 " Immediate Data digest failure while"
2669 iscsit_reject_cmd(cmd,
2670 ISCSI_REASON_DATA_DIGEST_ERROR,
2671 (unsigned char *)hdr);
2672 return IMMEDIATE_DATA_CANNOT_RECOVER;
2674 iscsit_reject_cmd(cmd,
2675 ISCSI_REASON_DATA_DIGEST_ERROR,
2676 (unsigned char *)hdr);
2677 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2680 pr_debug("Got CRC32C DataDigest 0x%08x for"
2681 " %u bytes of Immediate Data\n", checksum,
2686 cmd->write_data_done += length;
2688 if (cmd->write_data_done == cmd->se_cmd.data_length) {
2689 spin_lock_bh(&cmd->istate_lock);
2690 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2691 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2692 spin_unlock_bh(&cmd->istate_lock);
2695 return IMMEDIATE_DATA_NORMAL_OPERATION;
2698 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2699 with active network interface */
2700 static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2702 struct iscsi_cmd *cmd;
2703 struct iscsi_conn *conn_p;
2706 lockdep_assert_held(&conn->sess->conn_lock);
2709 * Only send a Asynchronous Message on connections whos network
2710 * interface is still functional.
2712 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2713 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2714 iscsit_inc_conn_usage_count(conn_p);
2723 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
2725 iscsit_dec_conn_usage_count(conn_p);
2729 cmd->logout_cid = conn->cid;
2730 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2731 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2733 spin_lock_bh(&conn_p->cmd_lock);
2734 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
2735 spin_unlock_bh(&conn_p->cmd_lock);
2737 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2738 iscsit_dec_conn_usage_count(conn_p);
2741 static int iscsit_send_conn_drop_async_message(
2742 struct iscsi_cmd *cmd,
2743 struct iscsi_conn *conn)
2745 struct iscsi_async *hdr;
2747 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2749 hdr = (struct iscsi_async *) cmd->pdu;
2750 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2751 hdr->flags = ISCSI_FLAG_CMD_FINAL;
2752 cmd->init_task_tag = RESERVED_ITT;
2753 cmd->targ_xfer_tag = 0xFFFFFFFF;
2754 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2755 cmd->stat_sn = conn->stat_sn++;
2756 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2757 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2758 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2759 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2760 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2761 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2762 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2764 pr_debug("Sending Connection Dropped Async Message StatSN:"
2765 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2766 cmd->logout_cid, conn->cid);
2768 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
2771 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2773 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2774 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2775 wait_for_completion_interruptible_timeout(
2776 &conn->tx_half_close_comp,
2777 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2782 iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2783 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2786 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2787 hdr->flags = datain->flags;
2788 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2789 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2790 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2791 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2792 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2793 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2794 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2797 hton24(hdr->dlength, datain->length);
2798 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2799 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2800 (struct scsi_lun *)&hdr->lun);
2802 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2804 hdr->itt = cmd->init_task_tag;
2806 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2807 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2809 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2811 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2813 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2815 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2816 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2817 hdr->datasn = cpu_to_be32(datain->data_sn);
2818 hdr->offset = cpu_to_be32(datain->offset);
2820 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2821 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2822 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2823 ntohl(hdr->offset), datain->length, conn->cid);
2825 EXPORT_SYMBOL(iscsit_build_datain_pdu);
2827 static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2829 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
2830 struct iscsi_datain datain;
2831 struct iscsi_datain_req *dr;
2833 bool set_statsn = false;
2835 memset(&datain, 0, sizeof(struct iscsi_datain));
2836 dr = iscsit_get_datain_values(cmd, &datain);
2838 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2839 cmd->init_task_tag);
2843 * Be paranoid and double check the logic for now.
2845 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
2846 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2847 " datain.length: %u exceeds cmd->data_length: %u\n",
2848 cmd->init_task_tag, datain.offset, datain.length,
2849 cmd->se_cmd.data_length);
2853 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
2855 * Special case for successfully execution w/ both DATAIN
2858 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2859 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2860 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2862 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2863 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2864 iscsit_increment_maxcmdsn(cmd, conn->sess);
2865 cmd->stat_sn = conn->stat_sn++;
2867 } else if (dr->dr_complete ==
2868 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2872 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
2874 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
2878 if (dr->dr_complete) {
2879 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
2881 iscsit_free_datain_req(cmd, dr);
2888 iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2889 struct iscsi_logout_rsp *hdr)
2891 struct iscsi_conn *logout_conn = NULL;
2892 struct iscsi_conn_recovery *cr = NULL;
2893 struct iscsi_session *sess = conn->sess;
2895 * The actual shutting down of Sessions and/or Connections
2896 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2897 * is done in scsi_logout_post_handler().
2899 switch (cmd->logout_reason) {
2900 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2901 pr_debug("iSCSI session logout successful, setting"
2902 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2903 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2905 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2906 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2909 * For CLOSECONNECTION logout requests carrying
2910 * a matching logout CID -> local CID, the reference
2911 * for the local CID will have been incremented in
2912 * iscsi_logout_closeconnection().
2914 * For CLOSECONNECTION logout requests carrying
2915 * a different CID than the connection it arrived
2916 * on, the connection responding to cmd->logout_cid
2917 * is stopped in iscsit_logout_post_handler_diffcid().
2920 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2921 " successful.\n", cmd->logout_cid, conn->cid);
2922 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2924 case ISCSI_LOGOUT_REASON_RECOVERY:
2925 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2926 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2929 * If the connection is still active from our point of view
2930 * force connection recovery to occur.
2932 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2935 iscsit_connection_reinstatement_rcfr(logout_conn);
2936 iscsit_dec_conn_usage_count(logout_conn);
2939 cr = iscsit_get_inactive_connection_recovery_entry(
2940 conn->sess, cmd->logout_cid);
2942 pr_err("Unable to locate CID: %hu for"
2943 " REMOVECONNFORRECOVERY Logout Request.\n",
2945 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2949 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2951 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2952 " for recovery for CID: %hu on CID: %hu successful.\n",
2953 cmd->logout_cid, conn->cid);
2954 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2957 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2958 cmd->logout_reason);
2962 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2963 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2964 hdr->response = cmd->logout_response;
2965 hdr->itt = cmd->init_task_tag;
2966 cmd->stat_sn = conn->stat_sn++;
2967 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2969 iscsit_increment_maxcmdsn(cmd, conn->sess);
2970 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2971 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2973 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2974 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2975 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2976 cmd->logout_cid, conn->cid);
2980 EXPORT_SYMBOL(iscsit_build_logout_rsp);
2983 iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2987 rc = iscsit_build_logout_rsp(cmd, conn,
2988 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2992 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
2996 iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2997 struct iscsi_nopin *hdr, bool nopout_response)
2999 hdr->opcode = ISCSI_OP_NOOP_IN;
3000 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3001 hton24(hdr->dlength, cmd->buf_ptr_size);
3002 if (nopout_response)
3003 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
3004 hdr->itt = cmd->init_task_tag;
3005 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3006 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
3008 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3010 if (nopout_response)
3011 iscsit_increment_maxcmdsn(cmd, conn->sess);
3013 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3014 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3016 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
3017 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
3018 "Solicited" : "Unsolicited", cmd->init_task_tag,
3019 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
3021 EXPORT_SYMBOL(iscsit_build_nopin_rsp);
3024 * Unsolicited NOPIN, either requesting a response or not.
3026 static int iscsit_send_unsolicited_nopin(
3027 struct iscsi_cmd *cmd,
3028 struct iscsi_conn *conn,
3031 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
3034 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
3036 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
3037 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
3039 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3043 spin_lock_bh(&cmd->istate_lock);
3044 cmd->i_state = want_response ?
3045 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
3046 spin_unlock_bh(&cmd->istate_lock);
3052 iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3054 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
3056 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
3059 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3060 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3062 pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
3064 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3069 static int iscsit_send_r2t(
3070 struct iscsi_cmd *cmd,
3071 struct iscsi_conn *conn)
3073 struct iscsi_r2t *r2t;
3074 struct iscsi_r2t_rsp *hdr;
3077 r2t = iscsit_get_r2t_from_list(cmd);
3081 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3082 memset(hdr, 0, ISCSI_HDR_LEN);
3083 hdr->opcode = ISCSI_OP_R2T;
3084 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3085 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3086 (struct scsi_lun *)&hdr->lun);
3087 hdr->itt = cmd->init_task_tag;
3088 if (conn->conn_transport->iscsit_get_r2t_ttt)
3089 conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
3091 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
3092 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3093 hdr->statsn = cpu_to_be32(conn->stat_sn);
3094 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3095 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3096 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3097 hdr->data_offset = cpu_to_be32(r2t->offset);
3098 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3100 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3101 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3102 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3103 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3104 r2t->offset, r2t->xfer_len, conn->cid);
3106 spin_lock_bh(&cmd->r2t_lock);
3108 spin_unlock_bh(&cmd->r2t_lock);
3110 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3115 spin_lock_bh(&cmd->dataout_timeout_lock);
3116 iscsit_start_dataout_timer(cmd, conn);
3117 spin_unlock_bh(&cmd->dataout_timeout_lock);
3123 * @recovery: If called from iscsi_task_reassign_complete_write() for
3124 * connection recovery.
3126 int iscsit_build_r2ts_for_cmd(
3127 struct iscsi_conn *conn,
3128 struct iscsi_cmd *cmd,
3132 u32 offset = 0, xfer_len = 0;
3134 spin_lock_bh(&cmd->r2t_lock);
3135 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3136 spin_unlock_bh(&cmd->r2t_lock);
3140 if (conn->sess->sess_ops->DataSequenceInOrder &&
3142 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
3144 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3145 if (conn->sess->sess_ops->DataSequenceInOrder) {
3146 offset = cmd->r2t_offset;
3148 if (first_r2t && recovery) {
3149 int new_data_end = offset +
3150 conn->sess->sess_ops->MaxBurstLength -
3151 cmd->next_burst_len;
3153 if (new_data_end > cmd->se_cmd.data_length)
3154 xfer_len = cmd->se_cmd.data_length - offset;
3157 conn->sess->sess_ops->MaxBurstLength -
3158 cmd->next_burst_len;
3160 int new_data_end = offset +
3161 conn->sess->sess_ops->MaxBurstLength;
3163 if (new_data_end > cmd->se_cmd.data_length)
3164 xfer_len = cmd->se_cmd.data_length - offset;
3166 xfer_len = conn->sess->sess_ops->MaxBurstLength;
3169 if ((s32)xfer_len < 0) {
3170 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3174 cmd->r2t_offset += xfer_len;
3176 if (cmd->r2t_offset == cmd->se_cmd.data_length)
3177 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3179 struct iscsi_seq *seq;
3181 seq = iscsit_get_seq_holder_for_r2t(cmd);
3183 spin_unlock_bh(&cmd->r2t_lock);
3187 offset = seq->offset;
3188 xfer_len = seq->xfer_len;
3190 if (cmd->seq_send_order == cmd->seq_count)
3191 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3193 cmd->outstanding_r2ts++;
3196 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3197 spin_unlock_bh(&cmd->r2t_lock);
3201 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3204 spin_unlock_bh(&cmd->r2t_lock);
3208 EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
3210 void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3211 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
3214 cmd->stat_sn = conn->stat_sn++;
3216 atomic_long_inc(&conn->sess->rsp_pdus);
3218 memset(hdr, 0, ISCSI_HDR_LEN);
3219 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3220 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3221 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3222 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
3223 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3224 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3225 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
3226 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3228 hdr->response = cmd->iscsi_response;
3229 hdr->cmd_status = cmd->se_cmd.scsi_status;
3230 hdr->itt = cmd->init_task_tag;
3231 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3233 iscsit_increment_maxcmdsn(cmd, conn->sess);
3234 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3235 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3237 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3238 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3239 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3240 cmd->se_cmd.scsi_status, conn->cid);
3242 EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3244 static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3246 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3247 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3248 void *data_buf = NULL;
3249 u32 padding = 0, data_buf_len = 0;
3251 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3254 * Attach SENSE DATA payload to iSCSI Response PDU
3256 if (cmd->se_cmd.sense_buffer &&
3257 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3258 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
3259 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3260 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3262 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
3263 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
3264 data_buf = cmd->sense_buffer;
3265 data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
3268 memset(cmd->sense_buffer +
3269 cmd->se_cmd.scsi_sense_length, 0, padding);
3270 pr_debug("Adding %u bytes of padding to"
3271 " SENSE.\n", padding);
3274 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3276 cmd->se_cmd.scsi_sense_length);
3279 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
3283 static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3285 switch (se_tmr->response) {
3286 case TMR_FUNCTION_COMPLETE:
3287 return ISCSI_TMF_RSP_COMPLETE;
3288 case TMR_TASK_DOES_NOT_EXIST:
3289 return ISCSI_TMF_RSP_NO_TASK;
3290 case TMR_LUN_DOES_NOT_EXIST:
3291 return ISCSI_TMF_RSP_NO_LUN;
3292 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3293 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3294 case TMR_FUNCTION_REJECTED:
3296 return ISCSI_TMF_RSP_REJECTED;
3301 iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3302 struct iscsi_tm_rsp *hdr)
3304 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3306 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
3307 hdr->flags = ISCSI_FLAG_CMD_FINAL;
3308 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
3309 hdr->itt = cmd->init_task_tag;
3310 cmd->stat_sn = conn->stat_sn++;
3311 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3313 iscsit_increment_maxcmdsn(cmd, conn->sess);
3314 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3315 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3317 pr_debug("Built Task Management Response ITT: 0x%08x,"
3318 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3319 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3321 EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3324 iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3326 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3328 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3330 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3333 #define SENDTARGETS_BUF_LIMIT 32768U
3336 iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
3337 enum iscsit_transport_type network_transport,
3338 int skip_bytes, bool *completed)
3340 char *payload = NULL;
3341 struct iscsi_conn *conn = cmd->conn;
3342 struct iscsi_portal_group *tpg;
3343 struct iscsi_tiqn *tiqn;
3344 struct iscsi_tpg_np *tpg_np;
3345 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
3346 int target_name_printed;
3347 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
3348 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
3351 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
3352 SENDTARGETS_BUF_LIMIT);
3354 payload = kzalloc(buffer_len, GFP_KERNEL);
3359 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
3362 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
3363 text_ptr = strchr(text_in, '=');
3365 pr_err("Unable to locate '=' string in text_in:"
3371 * Skip over '=' character..
3376 spin_lock(&tiqn_lock);
3377 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3378 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
3379 strcmp(tiqn->tiqn, text_ptr)) {
3383 target_name_printed = 0;
3385 spin_lock(&tiqn->tiqn_tpg_lock);
3386 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3388 /* If demo_mode_discovery=0 and generate_node_acls=0
3389 * (demo mode dislabed) do not return
3390 * TargetName+TargetAddress unless a NodeACL exists.
3393 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3394 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3395 (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
3396 cmd->conn->sess->sess_ops->InitiatorName))) {
3400 spin_lock(&tpg->tpg_state_lock);
3401 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
3402 spin_unlock(&tpg->tpg_state_lock);
3404 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3407 spin_lock(&tpg->tpg_np_lock);
3408 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3410 struct iscsi_np *np = tpg_np->tpg_np;
3411 struct sockaddr_storage *sockaddr;
3413 if (np->np_network_transport != network_transport)
3416 if (!target_name_printed) {
3417 len = sprintf(buf, "TargetName=%s",
3421 if ((len + payload_len) > buffer_len) {
3422 spin_unlock(&tpg->tpg_np_lock);
3423 spin_unlock(&tiqn->tiqn_tpg_lock);
3428 if (skip_bytes && len <= skip_bytes) {
3431 memcpy(payload + payload_len, buf, len);
3433 target_name_printed = 1;
3434 if (len > skip_bytes)
3439 if (inet_addr_is_any((struct sockaddr *)&np->np_sockaddr))
3440 sockaddr = &conn->local_sockaddr;
3442 sockaddr = &np->np_sockaddr;
3444 len = sprintf(buf, "TargetAddress="
3450 if ((len + payload_len) > buffer_len) {
3451 spin_unlock(&tpg->tpg_np_lock);
3452 spin_unlock(&tiqn->tiqn_tpg_lock);
3457 if (skip_bytes && len <= skip_bytes) {
3460 memcpy(payload + payload_len, buf, len);
3462 if (len > skip_bytes)
3466 spin_unlock(&tpg->tpg_np_lock);
3468 spin_unlock(&tiqn->tiqn_tpg_lock);
3475 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
3478 spin_unlock(&tiqn_lock);
3480 cmd->buf_ptr = payload;
3486 iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3487 struct iscsi_text_rsp *hdr,
3488 enum iscsit_transport_type network_transport)
3490 int text_length, padding;
3491 bool completed = true;
3493 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3494 cmd->read_data_done,
3496 if (text_length < 0)
3500 hdr->flags = ISCSI_FLAG_CMD_FINAL;
3502 hdr->flags = ISCSI_FLAG_TEXT_CONTINUE;
3503 cmd->read_data_done += text_length;
3504 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3505 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3507 hdr->opcode = ISCSI_OP_TEXT_RSP;
3508 padding = ((-text_length) & 3);
3509 hton24(hdr->dlength, text_length);
3510 hdr->itt = cmd->init_task_tag;
3511 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3512 cmd->stat_sn = conn->stat_sn++;
3513 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3515 iscsit_increment_maxcmdsn(cmd, conn->sess);
3517 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3518 * correctly increment MaxCmdSN for each response answering a
3519 * non immediate text request with a valid CmdSN.
3521 cmd->maxcmdsn_inc = 0;
3522 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3523 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3525 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3526 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3527 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3528 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3529 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
3531 return text_length + padding;
3533 EXPORT_SYMBOL(iscsit_build_text_rsp);
3535 static int iscsit_send_text_rsp(
3536 struct iscsi_cmd *cmd,
3537 struct iscsi_conn *conn)
3539 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
3542 text_length = iscsit_build_text_rsp(cmd, conn, hdr,
3543 conn->conn_transport->transport_type);
3544 if (text_length < 0)
3547 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3553 iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3554 struct iscsi_reject *hdr)
3556 hdr->opcode = ISCSI_OP_REJECT;
3557 hdr->reason = cmd->reject_reason;
3558 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3559 hton24(hdr->dlength, ISCSI_HDR_LEN);
3560 hdr->ffffffff = cpu_to_be32(0xffffffff);
3561 cmd->stat_sn = conn->stat_sn++;
3562 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3563 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3564 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3567 EXPORT_SYMBOL(iscsit_build_reject);
3569 static int iscsit_send_reject(
3570 struct iscsi_cmd *cmd,
3571 struct iscsi_conn *conn)
3573 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
3575 iscsit_build_reject(cmd, conn, hdr);
3577 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3578 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3580 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3585 void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3589 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3590 * within iscsit_start_kthreads()
3592 * Here we use bitmap_id to determine which CPU that this
3593 * iSCSI connection's RX/TX threads will be scheduled to
3596 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
3597 for_each_online_cpu(cpu) {
3599 cpumask_set_cpu(cpu, conn->conn_cpumask);
3604 * This should never be reached..
3607 cpumask_setall(conn->conn_cpumask);
3611 iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3616 case ISTATE_SEND_R2T:
3617 ret = iscsit_send_r2t(cmd, conn);
3622 spin_lock_bh(&conn->cmd_lock);
3623 list_del_init(&cmd->i_conn_node);
3624 spin_unlock_bh(&conn->cmd_lock);
3626 iscsit_free_cmd(cmd, false);
3628 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3629 iscsit_mod_nopin_response_timer(conn);
3630 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3634 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3635 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3640 pr_err("Unknown Opcode: 0x%02x ITT:"
3641 " 0x%08x, i_state: %d on CID: %hu\n",
3642 cmd->iscsi_opcode, cmd->init_task_tag, state,
3652 EXPORT_SYMBOL(iscsit_immediate_queue);
3655 iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3657 struct iscsit_transport *t = conn->conn_transport;
3658 struct iscsi_queue_req *qr;
3659 struct iscsi_cmd *cmd;
3663 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3664 atomic_set(&conn->check_immediate_queue, 0);
3667 kmem_cache_free(lio_qr_cache, qr);
3669 ret = t->iscsit_immediate_queue(conn, cmd, state);
3678 iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3684 case ISTATE_SEND_DATAIN:
3685 ret = iscsit_send_datain(cmd, conn);
3690 goto check_rsp_state;
3691 else if (ret == 1) {
3693 spin_lock_bh(&cmd->istate_lock);
3694 cmd->i_state = ISTATE_SENT_STATUS;
3695 spin_unlock_bh(&cmd->istate_lock);
3697 if (atomic_read(&conn->check_immediate_queue))
3701 } else if (ret == 2) {
3702 /* Still must send status,
3703 SCF_TRANSPORT_TASK_SENSE was set */
3704 spin_lock_bh(&cmd->istate_lock);
3705 cmd->i_state = ISTATE_SEND_STATUS;
3706 spin_unlock_bh(&cmd->istate_lock);
3707 state = ISTATE_SEND_STATUS;
3708 goto check_rsp_state;
3712 case ISTATE_SEND_STATUS:
3713 case ISTATE_SEND_STATUS_RECOVERY:
3714 ret = iscsit_send_response(cmd, conn);
3716 case ISTATE_SEND_LOGOUTRSP:
3717 ret = iscsit_send_logout(cmd, conn);
3719 case ISTATE_SEND_ASYNCMSG:
3720 ret = iscsit_send_conn_drop_async_message(
3723 case ISTATE_SEND_NOPIN:
3724 ret = iscsit_send_nopin(cmd, conn);
3726 case ISTATE_SEND_REJECT:
3727 ret = iscsit_send_reject(cmd, conn);
3729 case ISTATE_SEND_TASKMGTRSP:
3730 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3733 ret = iscsit_tmr_post_handler(cmd, conn);
3735 iscsit_fall_back_to_erl0(conn->sess);
3737 case ISTATE_SEND_TEXTRSP:
3738 ret = iscsit_send_text_rsp(cmd, conn);
3741 pr_err("Unknown Opcode: 0x%02x ITT:"
3742 " 0x%08x, i_state: %d on CID: %hu\n",
3743 cmd->iscsi_opcode, cmd->init_task_tag,
3751 case ISTATE_SEND_LOGOUTRSP:
3752 if (!iscsit_logout_post_handler(cmd, conn))
3755 case ISTATE_SEND_STATUS:
3756 case ISTATE_SEND_ASYNCMSG:
3757 case ISTATE_SEND_NOPIN:
3758 case ISTATE_SEND_STATUS_RECOVERY:
3759 case ISTATE_SEND_TEXTRSP:
3760 case ISTATE_SEND_TASKMGTRSP:
3761 case ISTATE_SEND_REJECT:
3762 spin_lock_bh(&cmd->istate_lock);
3763 cmd->i_state = ISTATE_SENT_STATUS;
3764 spin_unlock_bh(&cmd->istate_lock);
3767 pr_err("Unknown Opcode: 0x%02x ITT:"
3768 " 0x%08x, i_state: %d on CID: %hu\n",
3769 cmd->iscsi_opcode, cmd->init_task_tag,
3770 cmd->i_state, conn->cid);
3774 if (atomic_read(&conn->check_immediate_queue))
3782 EXPORT_SYMBOL(iscsit_response_queue);
3784 static int iscsit_handle_response_queue(struct iscsi_conn *conn)
3786 struct iscsit_transport *t = conn->conn_transport;
3787 struct iscsi_queue_req *qr;
3788 struct iscsi_cmd *cmd;
3792 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3795 kmem_cache_free(lio_qr_cache, qr);
3797 ret = t->iscsit_response_queue(conn, cmd, state);
3798 if (ret == 1 || ret < 0)
3805 int iscsi_target_tx_thread(void *arg)
3808 struct iscsi_conn *conn = arg;
3809 bool conn_freed = false;
3812 * Allow ourselves to be interrupted by SIGINT so that a
3813 * connection recovery / failure event can be triggered externally.
3815 allow_signal(SIGINT);
3817 while (!kthread_should_stop()) {
3819 * Ensure that both TX and RX per connection kthreads
3820 * are scheduled to run on the same CPU.
3822 iscsit_thread_check_cpumask(conn, current, 1);
3824 wait_event_interruptible(conn->queues_wq,
3825 !iscsit_conn_all_queues_empty(conn));
3827 if (signal_pending(current))
3831 ret = iscsit_handle_immediate_queue(conn);
3835 ret = iscsit_handle_response_queue(conn);
3838 } else if (ret == -ECONNRESET) {
3841 } else if (ret < 0) {
3848 * Avoid the normal connection failure code-path if this connection
3849 * is still within LOGIN mode, and iscsi_np process context is
3850 * responsible for cleaning up the early connection failure.
3852 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
3853 iscsit_take_action_for_connection_exit(conn, &conn_freed);
3856 while (!kthread_should_stop()) {
3863 static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3865 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3866 struct iscsi_cmd *cmd;
3869 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3870 case ISCSI_OP_SCSI_CMD:
3871 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3875 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3877 case ISCSI_OP_SCSI_DATA_OUT:
3878 ret = iscsit_handle_data_out(conn, buf);
3880 case ISCSI_OP_NOOP_OUT:
3882 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
3883 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3887 ret = iscsit_handle_nop_out(conn, cmd, buf);
3889 case ISCSI_OP_SCSI_TMFUNC:
3890 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3894 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3897 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
3898 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
3902 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3907 ret = iscsit_handle_text_cmd(conn, cmd, buf);
3909 case ISCSI_OP_LOGOUT:
3910 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3914 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3916 wait_for_completion_timeout(&conn->conn_logout_comp,
3917 SECONDS_FOR_LOGOUT_COMP * HZ);
3919 case ISCSI_OP_SNACK:
3920 ret = iscsit_handle_snack(conn, buf);
3923 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3924 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3925 pr_err("Cannot recover from unknown"
3926 " opcode while ERL=0, closing iSCSI connection.\n");
3929 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3930 " closing iSCSI connection.\n");
3937 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
3940 static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
3944 spin_lock_bh(&conn->state_lock);
3945 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
3946 spin_unlock_bh(&conn->state_lock);
3951 static void iscsit_get_rx_pdu(struct iscsi_conn *conn)
3955 u32 checksum = 0, digest = 0;
3958 buffer = kcalloc(ISCSI_HDR_LEN, sizeof(*buffer), GFP_KERNEL);
3962 while (!kthread_should_stop()) {
3964 * Ensure that both TX and RX per connection kthreads
3965 * are scheduled to run on the same CPU.
3967 iscsit_thread_check_cpumask(conn, current, 0);
3969 memset(&iov, 0, sizeof(struct kvec));
3971 iov.iov_base = buffer;
3972 iov.iov_len = ISCSI_HDR_LEN;
3974 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3975 if (ret != ISCSI_HDR_LEN) {
3976 iscsit_rx_thread_wait_for_tcp(conn);
3980 if (conn->conn_ops->HeaderDigest) {
3981 iov.iov_base = &digest;
3982 iov.iov_len = ISCSI_CRC_LEN;
3984 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3985 if (ret != ISCSI_CRC_LEN) {
3986 iscsit_rx_thread_wait_for_tcp(conn);
3990 iscsit_do_crypto_hash_buf(conn->conn_rx_hash, buffer,
3991 ISCSI_HDR_LEN, 0, NULL,
3994 if (digest != checksum) {
3995 pr_err("HeaderDigest CRC32C failed,"
3996 " received 0x%08x, computed 0x%08x\n",
3999 * Set the PDU to 0xff so it will intentionally
4000 * hit default in the switch below.
4002 memset(buffer, 0xff, ISCSI_HDR_LEN);
4003 atomic_long_inc(&conn->sess->conn_digest_errors);
4005 pr_debug("Got HeaderDigest CRC32C"
4006 " 0x%08x\n", checksum);
4010 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4013 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4015 if (conn->sess->sess_ops->SessionType &&
4016 ((!(opcode & ISCSI_OP_TEXT)) ||
4017 (!(opcode & ISCSI_OP_LOGOUT)))) {
4018 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4019 " while in Discovery Session, rejecting.\n", opcode);
4020 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4025 ret = iscsi_target_rx_opcode(conn, buffer);
4033 int iscsi_target_rx_thread(void *arg)
4036 struct iscsi_conn *conn = arg;
4037 bool conn_freed = false;
4040 * Allow ourselves to be interrupted by SIGINT so that a
4041 * connection recovery / failure event can be triggered externally.
4043 allow_signal(SIGINT);
4045 * Wait for iscsi_post_login_handler() to complete before allowing
4046 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4048 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4049 if (rc < 0 || iscsi_target_check_conn_state(conn))
4052 if (!conn->conn_transport->iscsit_get_rx_pdu)
4055 conn->conn_transport->iscsit_get_rx_pdu(conn);
4057 if (!signal_pending(current))
4058 atomic_set(&conn->transport_failed, 1);
4059 iscsit_take_action_for_connection_exit(conn, &conn_freed);
4063 while (!kthread_should_stop()) {
4071 static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4073 LIST_HEAD(tmp_list);
4074 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4075 struct iscsi_session *sess = conn->sess;
4077 * We expect this function to only ever be called from either RX or TX
4078 * thread context via iscsit_close_connection() once the other context
4079 * has been reset -> returned sleeping pre-handler state.
4081 spin_lock_bh(&conn->cmd_lock);
4082 list_splice_init(&conn->conn_cmd_list, &tmp_list);
4084 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4085 struct se_cmd *se_cmd = &cmd->se_cmd;
4087 if (!se_cmd->se_tfo)
4090 spin_lock_irq(&se_cmd->t_state_lock);
4091 if (se_cmd->transport_state & CMD_T_ABORTED) {
4092 if (!(se_cmd->transport_state & CMD_T_TAS))
4094 * LIO's abort path owns the cleanup for this,
4095 * so put it back on the list and let
4096 * aborted_task handle it.
4098 list_move_tail(&cmd->i_conn_node,
4099 &conn->conn_cmd_list);
4101 se_cmd->transport_state |= CMD_T_FABRIC_STOP;
4103 spin_unlock_irq(&se_cmd->t_state_lock);
4105 spin_unlock_bh(&conn->cmd_lock);
4107 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4108 list_del_init(&cmd->i_conn_node);
4110 iscsit_increment_maxcmdsn(cmd, sess);
4111 iscsit_free_cmd(cmd, true);
4116 static void iscsit_stop_timers_for_cmds(
4117 struct iscsi_conn *conn)
4119 struct iscsi_cmd *cmd;
4121 spin_lock_bh(&conn->cmd_lock);
4122 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
4123 if (cmd->data_direction == DMA_TO_DEVICE)
4124 iscsit_stop_dataout_timer(cmd);
4126 spin_unlock_bh(&conn->cmd_lock);
4129 int iscsit_close_connection(
4130 struct iscsi_conn *conn)
4132 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4133 struct iscsi_session *sess = conn->sess;
4135 pr_debug("Closing iSCSI connection CID %hu on SID:"
4136 " %u\n", conn->cid, sess->sid);
4138 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4139 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4140 * sleeping and the logout response never got sent because the
4141 * connection failed.
4143 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4144 * to signal logout response TX interrupt completion. Go ahead and skip
4145 * this for iser since isert_rx_opcode() does not wait on logout failure,
4146 * and to avoid iscsi_conn pointer dereference in iser-target code.
4148 if (!conn->conn_transport->rdma_shutdown)
4149 complete(&conn->conn_logout_comp);
4151 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4152 if (conn->tx_thread &&
4153 cmpxchg(&conn->tx_thread_active, true, false)) {
4154 send_sig(SIGINT, conn->tx_thread, 1);
4155 kthread_stop(conn->tx_thread);
4157 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4158 if (conn->rx_thread &&
4159 cmpxchg(&conn->rx_thread_active, true, false)) {
4160 send_sig(SIGINT, conn->rx_thread, 1);
4161 kthread_stop(conn->rx_thread);
4165 spin_lock(&iscsit_global->ts_bitmap_lock);
4166 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4168 spin_unlock(&iscsit_global->ts_bitmap_lock);
4170 iscsit_stop_timers_for_cmds(conn);
4171 iscsit_stop_nopin_response_timer(conn);
4172 iscsit_stop_nopin_timer(conn);
4174 if (conn->conn_transport->iscsit_wait_conn)
4175 conn->conn_transport->iscsit_wait_conn(conn);
4178 * During Connection recovery drop unacknowledged out of order
4179 * commands for this connection, and prepare the other commands
4182 * During normal operation clear the out of order commands (but
4183 * do not free the struct iscsi_ooo_cmdsn's) and release all
4184 * struct iscsi_cmds.
4186 if (atomic_read(&conn->connection_recovery)) {
4187 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4188 iscsit_prepare_cmds_for_reallegiance(conn);
4190 iscsit_clear_ooo_cmdsns_for_conn(conn);
4191 iscsit_release_commands_from_conn(conn);
4193 iscsit_free_queue_reqs_for_conn(conn);
4196 * Handle decrementing session or connection usage count if
4197 * a logout response was not able to be sent because the
4198 * connection failed. Fall back to Session Recovery here.
4200 if (atomic_read(&conn->conn_logout_remove)) {
4201 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4202 iscsit_dec_conn_usage_count(conn);
4203 iscsit_dec_session_usage_count(sess);
4205 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4206 iscsit_dec_conn_usage_count(conn);
4208 atomic_set(&conn->conn_logout_remove, 0);
4209 atomic_set(&sess->session_reinstatement, 0);
4210 atomic_set(&sess->session_fall_back_to_erl0, 1);
4213 spin_lock_bh(&sess->conn_lock);
4214 list_del(&conn->conn_list);
4217 * Attempt to let the Initiator know this connection failed by
4218 * sending an Connection Dropped Async Message on another
4219 * active connection.
4221 if (atomic_read(&conn->connection_recovery))
4222 iscsit_build_conn_drop_async_message(conn);
4224 spin_unlock_bh(&sess->conn_lock);
4227 * If connection reinstatement is being performed on this connection,
4228 * up the connection reinstatement semaphore that is being blocked on
4229 * in iscsit_cause_connection_reinstatement().
4231 spin_lock_bh(&conn->state_lock);
4232 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4233 spin_unlock_bh(&conn->state_lock);
4234 complete(&conn->conn_wait_comp);
4235 wait_for_completion(&conn->conn_post_wait_comp);
4236 spin_lock_bh(&conn->state_lock);
4240 * If connection reinstatement is being performed on this connection
4241 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4242 * connection wait rcfr semaphore that is being blocked on
4243 * an iscsit_connection_reinstatement_rcfr().
4245 if (atomic_read(&conn->connection_wait_rcfr)) {
4246 spin_unlock_bh(&conn->state_lock);
4247 complete(&conn->conn_wait_rcfr_comp);
4248 wait_for_completion(&conn->conn_post_wait_comp);
4249 spin_lock_bh(&conn->state_lock);
4251 atomic_set(&conn->connection_reinstatement, 1);
4252 spin_unlock_bh(&conn->state_lock);
4255 * If any other processes are accessing this connection pointer we
4256 * must wait until they have completed.
4258 iscsit_check_conn_usage_count(conn);
4260 ahash_request_free(conn->conn_tx_hash);
4261 if (conn->conn_rx_hash) {
4262 struct crypto_ahash *tfm;
4264 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
4265 ahash_request_free(conn->conn_rx_hash);
4266 crypto_free_ahash(tfm);
4270 sock_release(conn->sock);
4272 if (conn->conn_transport->iscsit_free_conn)
4273 conn->conn_transport->iscsit_free_conn(conn);
4275 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4276 conn->conn_state = TARG_CONN_STATE_FREE;
4277 iscsit_free_conn(conn);
4279 spin_lock_bh(&sess->conn_lock);
4280 atomic_dec(&sess->nconn);
4281 pr_debug("Decremented iSCSI connection count to %hu from node:"
4282 " %s\n", atomic_read(&sess->nconn),
4283 sess->sess_ops->InitiatorName);
4285 * Make sure that if one connection fails in an non ERL=2 iSCSI
4286 * Session that they all fail.
4288 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4289 !atomic_read(&sess->session_logout))
4290 atomic_set(&sess->session_fall_back_to_erl0, 1);
4293 * If this was not the last connection in the session, and we are
4294 * performing session reinstatement or falling back to ERL=0, call
4295 * iscsit_stop_session() without sleeping to shutdown the other
4296 * active connections.
4298 if (atomic_read(&sess->nconn)) {
4299 if (!atomic_read(&sess->session_reinstatement) &&
4300 !atomic_read(&sess->session_fall_back_to_erl0)) {
4301 spin_unlock_bh(&sess->conn_lock);
4304 if (!atomic_read(&sess->session_stop_active)) {
4305 atomic_set(&sess->session_stop_active, 1);
4306 spin_unlock_bh(&sess->conn_lock);
4307 iscsit_stop_session(sess, 0, 0);
4310 spin_unlock_bh(&sess->conn_lock);
4315 * If this was the last connection in the session and one of the
4316 * following is occurring:
4318 * Session Reinstatement is not being performed, and are falling back
4319 * to ERL=0 call iscsit_close_session().
4321 * Session Logout was requested. iscsit_close_session() will be called
4324 * Session Continuation is not being performed, start the Time2Retain
4325 * handler and check if sleep_on_sess_wait_sem is active.
4327 if (!atomic_read(&sess->session_reinstatement) &&
4328 atomic_read(&sess->session_fall_back_to_erl0)) {
4329 spin_unlock_bh(&sess->conn_lock);
4330 complete_all(&sess->session_wait_comp);
4331 iscsit_close_session(sess);
4334 } else if (atomic_read(&sess->session_logout)) {
4335 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4336 sess->session_state = TARG_SESS_STATE_FREE;
4338 if (atomic_read(&sess->session_close)) {
4339 spin_unlock_bh(&sess->conn_lock);
4340 complete_all(&sess->session_wait_comp);
4341 iscsit_close_session(sess);
4343 spin_unlock_bh(&sess->conn_lock);
4348 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4349 sess->session_state = TARG_SESS_STATE_FAILED;
4351 if (!atomic_read(&sess->session_continuation))
4352 iscsit_start_time2retain_handler(sess);
4354 if (atomic_read(&sess->session_close)) {
4355 spin_unlock_bh(&sess->conn_lock);
4356 complete_all(&sess->session_wait_comp);
4357 iscsit_close_session(sess);
4359 spin_unlock_bh(&sess->conn_lock);
4367 * If the iSCSI Session for the iSCSI Initiator Node exists,
4368 * forcefully shutdown the iSCSI NEXUS.
4370 int iscsit_close_session(struct iscsi_session *sess)
4372 struct iscsi_portal_group *tpg = sess->tpg;
4373 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4375 if (atomic_read(&sess->nconn)) {
4376 pr_err("%d connection(s) still exist for iSCSI session"
4377 " to %s\n", atomic_read(&sess->nconn),
4378 sess->sess_ops->InitiatorName);
4382 spin_lock_bh(&se_tpg->session_lock);
4383 atomic_set(&sess->session_logout, 1);
4384 atomic_set(&sess->session_reinstatement, 1);
4385 iscsit_stop_time2retain_timer(sess);
4386 spin_unlock_bh(&se_tpg->session_lock);
4388 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4389 iscsit_free_connection_recovery_entries(sess);
4392 * transport_deregister_session_configfs() will clear the
4393 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4394 * can be setting it again with __transport_register_session() in
4395 * iscsi_post_login_handler() again after the iscsit_stop_session()
4396 * completes in iscsi_np context.
4398 transport_deregister_session_configfs(sess->se_sess);
4401 * If any other processes are accessing this session pointer we must
4402 * wait until they have completed. If we are in an interrupt (the
4403 * time2retain handler) and contain and active session usage count we
4404 * restart the timer and exit.
4406 if (!in_interrupt()) {
4407 iscsit_check_session_usage_count(sess);
4409 if (iscsit_check_session_usage_count(sess) == 2) {
4410 atomic_set(&sess->session_logout, 0);
4411 iscsit_start_time2retain_handler(sess);
4416 transport_deregister_session(sess->se_sess);
4418 iscsit_free_all_ooo_cmdsns(sess);
4420 spin_lock_bh(&se_tpg->session_lock);
4421 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4422 sess->session_state = TARG_SESS_STATE_FREE;
4423 pr_debug("Released iSCSI session from node: %s\n",
4424 sess->sess_ops->InitiatorName);
4427 tpg->tpg_tiqn->tiqn_nsessions--;
4429 pr_debug("Decremented number of active iSCSI Sessions on"
4430 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4432 ida_free(&sess_ida, sess->session_index);
4433 kfree(sess->sess_ops);
4434 sess->sess_ops = NULL;
4435 spin_unlock_bh(&se_tpg->session_lock);
4441 static void iscsit_logout_post_handler_closesession(
4442 struct iscsi_conn *conn)
4444 struct iscsi_session *sess = conn->sess;
4447 * Traditional iscsi/tcp will invoke this logic from TX thread
4448 * context during session logout, so clear tx_thread_active and
4449 * sleep if iscsit_close_connection() has not already occured.
4451 * Since iser-target invokes this logic from it's own workqueue,
4452 * always sleep waiting for RX/TX thread shutdown to complete
4453 * within iscsit_close_connection().
4455 if (!conn->conn_transport->rdma_shutdown) {
4456 sleep = cmpxchg(&conn->tx_thread_active, true, false);
4461 atomic_set(&conn->conn_logout_remove, 0);
4462 complete(&conn->conn_logout_comp);
4464 iscsit_dec_conn_usage_count(conn);
4465 atomic_set(&sess->session_close, 1);
4466 iscsit_stop_session(sess, sleep, sleep);
4467 iscsit_dec_session_usage_count(sess);
4470 static void iscsit_logout_post_handler_samecid(
4471 struct iscsi_conn *conn)
4475 if (!conn->conn_transport->rdma_shutdown) {
4476 sleep = cmpxchg(&conn->tx_thread_active, true, false);
4481 atomic_set(&conn->conn_logout_remove, 0);
4482 complete(&conn->conn_logout_comp);
4484 iscsit_cause_connection_reinstatement(conn, sleep);
4485 iscsit_dec_conn_usage_count(conn);
4488 static void iscsit_logout_post_handler_diffcid(
4489 struct iscsi_conn *conn,
4492 struct iscsi_conn *l_conn;
4493 struct iscsi_session *sess = conn->sess;
4494 bool conn_found = false;
4499 spin_lock_bh(&sess->conn_lock);
4500 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4501 if (l_conn->cid == cid) {
4502 iscsit_inc_conn_usage_count(l_conn);
4507 spin_unlock_bh(&sess->conn_lock);
4513 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4515 spin_lock_bh(&l_conn->state_lock);
4516 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4517 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4518 spin_unlock_bh(&l_conn->state_lock);
4520 iscsit_cause_connection_reinstatement(l_conn, 1);
4521 iscsit_dec_conn_usage_count(l_conn);
4525 * Return of 0 causes the TX thread to restart.
4527 int iscsit_logout_post_handler(
4528 struct iscsi_cmd *cmd,
4529 struct iscsi_conn *conn)
4533 switch (cmd->logout_reason) {
4534 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4535 switch (cmd->logout_response) {
4536 case ISCSI_LOGOUT_SUCCESS:
4537 case ISCSI_LOGOUT_CLEANUP_FAILED:
4539 iscsit_logout_post_handler_closesession(conn);
4543 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4544 if (conn->cid == cmd->logout_cid) {
4545 switch (cmd->logout_response) {
4546 case ISCSI_LOGOUT_SUCCESS:
4547 case ISCSI_LOGOUT_CLEANUP_FAILED:
4549 iscsit_logout_post_handler_samecid(conn);
4553 switch (cmd->logout_response) {
4554 case ISCSI_LOGOUT_SUCCESS:
4555 iscsit_logout_post_handler_diffcid(conn,
4558 case ISCSI_LOGOUT_CID_NOT_FOUND:
4559 case ISCSI_LOGOUT_CLEANUP_FAILED:
4566 case ISCSI_LOGOUT_REASON_RECOVERY:
4567 switch (cmd->logout_response) {
4568 case ISCSI_LOGOUT_SUCCESS:
4569 case ISCSI_LOGOUT_CID_NOT_FOUND:
4570 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4571 case ISCSI_LOGOUT_CLEANUP_FAILED:
4583 EXPORT_SYMBOL(iscsit_logout_post_handler);
4585 void iscsit_fail_session(struct iscsi_session *sess)
4587 struct iscsi_conn *conn;
4589 spin_lock_bh(&sess->conn_lock);
4590 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4591 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4592 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4594 spin_unlock_bh(&sess->conn_lock);
4596 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4597 sess->session_state = TARG_SESS_STATE_FAILED;
4600 void iscsit_stop_session(
4601 struct iscsi_session *sess,
4603 int connection_sleep)
4605 u16 conn_count = atomic_read(&sess->nconn);
4606 struct iscsi_conn *conn, *conn_tmp = NULL;
4609 spin_lock_bh(&sess->conn_lock);
4611 if (connection_sleep) {
4612 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4614 if (conn_count == 0)
4617 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4620 iscsit_inc_conn_usage_count(conn_tmp);
4623 iscsit_inc_conn_usage_count(conn);
4625 spin_unlock_bh(&sess->conn_lock);
4626 iscsit_cause_connection_reinstatement(conn, 1);
4627 spin_lock_bh(&sess->conn_lock);
4629 iscsit_dec_conn_usage_count(conn);
4631 iscsit_dec_conn_usage_count(conn_tmp);
4635 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4636 iscsit_cause_connection_reinstatement(conn, 0);
4639 if (session_sleep && atomic_read(&sess->nconn)) {
4640 spin_unlock_bh(&sess->conn_lock);
4641 wait_for_completion(&sess->session_wait_comp);
4643 spin_unlock_bh(&sess->conn_lock);
4646 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4648 struct iscsi_session *sess;
4649 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4650 struct se_session *se_sess, *se_sess_tmp;
4651 LIST_HEAD(free_list);
4652 int session_count = 0;
4654 spin_lock_bh(&se_tpg->session_lock);
4655 if (tpg->nsessions && !force) {
4656 spin_unlock_bh(&se_tpg->session_lock);
4660 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4662 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4664 spin_lock(&sess->conn_lock);
4665 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4666 atomic_read(&sess->session_logout) ||
4667 atomic_read(&sess->session_close) ||
4668 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4669 spin_unlock(&sess->conn_lock);
4672 iscsit_inc_session_usage_count(sess);
4673 atomic_set(&sess->session_reinstatement, 1);
4674 atomic_set(&sess->session_fall_back_to_erl0, 1);
4675 atomic_set(&sess->session_close, 1);
4676 spin_unlock(&sess->conn_lock);
4678 list_move_tail(&se_sess->sess_list, &free_list);
4680 spin_unlock_bh(&se_tpg->session_lock);
4682 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4683 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4685 list_del_init(&se_sess->sess_list);
4686 iscsit_stop_session(sess, 1, 1);
4687 iscsit_dec_session_usage_count(sess);
4691 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4692 " Group: %hu\n", session_count, tpg->tpgt);
4696 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4697 MODULE_VERSION("4.1.x");
4698 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4699 MODULE_LICENSE("GPL");
4701 module_init(iscsi_target_init_module);
4702 module_exit(iscsi_target_cleanup_module);