1 /*******************************************************************************
2 * This file contains main functions related to the iSCSI Target Core Driver.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
19 #include <crypto/hash.h>
20 #include <linux/string.h>
21 #include <linux/kthread.h>
22 #include <linux/completion.h>
23 #include <linux/module.h>
24 #include <linux/vmalloc.h>
25 #include <linux/idr.h>
26 #include <linux/delay.h>
27 #include <asm/unaligned.h>
29 #include <scsi/scsi_proto.h>
30 #include <scsi/iscsi_proto.h>
31 #include <scsi/scsi_tcq.h>
32 #include <target/target_core_base.h>
33 #include <target/target_core_fabric.h>
35 #include <target/iscsi/iscsi_target_core.h>
36 #include "iscsi_target_parameters.h"
37 #include "iscsi_target_seq_pdu_list.h"
38 #include "iscsi_target_datain_values.h"
39 #include "iscsi_target_erl0.h"
40 #include "iscsi_target_erl1.h"
41 #include "iscsi_target_erl2.h"
42 #include "iscsi_target_login.h"
43 #include "iscsi_target_tmr.h"
44 #include "iscsi_target_tpg.h"
45 #include "iscsi_target_util.h"
46 #include "iscsi_target.h"
47 #include "iscsi_target_device.h"
48 #include <target/iscsi/iscsi_target_stat.h>
50 #include <target/iscsi/iscsi_transport.h>
52 static LIST_HEAD(g_tiqn_list);
53 static LIST_HEAD(g_np_list);
54 static DEFINE_SPINLOCK(tiqn_lock);
55 static DEFINE_MUTEX(np_lock);
57 static struct idr tiqn_idr;
59 struct mutex auth_id_lock;
60 spinlock_t sess_idr_lock;
62 struct iscsit_global *iscsit_global;
64 struct kmem_cache *lio_qr_cache;
65 struct kmem_cache *lio_dr_cache;
66 struct kmem_cache *lio_ooo_cache;
67 struct kmem_cache *lio_r2t_cache;
69 static int iscsit_handle_immediate_data(struct iscsi_cmd *,
70 struct iscsi_scsi_req *, u32);
72 struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
74 struct iscsi_tiqn *tiqn = NULL;
76 spin_lock(&tiqn_lock);
77 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
78 if (!strcmp(tiqn->tiqn, buf)) {
80 spin_lock(&tiqn->tiqn_state_lock);
81 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
82 tiqn->tiqn_access_count++;
83 spin_unlock(&tiqn->tiqn_state_lock);
84 spin_unlock(&tiqn_lock);
87 spin_unlock(&tiqn->tiqn_state_lock);
90 spin_unlock(&tiqn_lock);
95 static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
97 spin_lock(&tiqn->tiqn_state_lock);
98 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
99 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
100 spin_unlock(&tiqn->tiqn_state_lock);
103 spin_unlock(&tiqn->tiqn_state_lock);
108 void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
110 spin_lock(&tiqn->tiqn_state_lock);
111 tiqn->tiqn_access_count--;
112 spin_unlock(&tiqn->tiqn_state_lock);
116 * Note that IQN formatting is expected to be done in userspace, and
117 * no explict IQN format checks are done here.
119 struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
121 struct iscsi_tiqn *tiqn = NULL;
124 if (strlen(buf) >= ISCSI_IQN_LEN) {
125 pr_err("Target IQN exceeds %d bytes\n",
127 return ERR_PTR(-EINVAL);
130 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
132 pr_err("Unable to allocate struct iscsi_tiqn\n");
133 return ERR_PTR(-ENOMEM);
136 sprintf(tiqn->tiqn, "%s", buf);
137 INIT_LIST_HEAD(&tiqn->tiqn_list);
138 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
139 spin_lock_init(&tiqn->tiqn_state_lock);
140 spin_lock_init(&tiqn->tiqn_tpg_lock);
141 spin_lock_init(&tiqn->sess_err_stats.lock);
142 spin_lock_init(&tiqn->login_stats.lock);
143 spin_lock_init(&tiqn->logout_stats.lock);
145 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
147 idr_preload(GFP_KERNEL);
148 spin_lock(&tiqn_lock);
150 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
152 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
153 spin_unlock(&tiqn_lock);
158 tiqn->tiqn_index = ret;
159 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
161 spin_unlock(&tiqn_lock);
164 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
170 static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
173 * Wait for accesses to said struct iscsi_tiqn to end.
175 spin_lock(&tiqn->tiqn_state_lock);
176 while (tiqn->tiqn_access_count != 0) {
177 spin_unlock(&tiqn->tiqn_state_lock);
179 spin_lock(&tiqn->tiqn_state_lock);
181 spin_unlock(&tiqn->tiqn_state_lock);
184 void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
187 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
188 * while holding tiqn->tiqn_state_lock. This means that all subsequent
189 * attempts to access this struct iscsi_tiqn will fail from both transport
190 * fabric and control code paths.
192 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
193 pr_err("iscsit_set_tiqn_shutdown() failed\n");
197 iscsit_wait_for_tiqn(tiqn);
199 spin_lock(&tiqn_lock);
200 list_del(&tiqn->tiqn_list);
201 idr_remove(&tiqn_idr, tiqn->tiqn_index);
202 spin_unlock(&tiqn_lock);
204 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
209 int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
213 * Determine if the network portal is accepting storage traffic.
215 spin_lock_bh(&np->np_thread_lock);
216 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
217 spin_unlock_bh(&np->np_thread_lock);
220 spin_unlock_bh(&np->np_thread_lock);
222 * Determine if the portal group is accepting storage traffic.
224 spin_lock_bh(&tpg->tpg_state_lock);
225 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
226 spin_unlock_bh(&tpg->tpg_state_lock);
229 spin_unlock_bh(&tpg->tpg_state_lock);
232 * Here we serialize access across the TIQN+TPG Tuple.
234 ret = down_interruptible(&tpg->np_login_sem);
238 spin_lock_bh(&tpg->tpg_state_lock);
239 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
240 spin_unlock_bh(&tpg->tpg_state_lock);
241 up(&tpg->np_login_sem);
244 spin_unlock_bh(&tpg->tpg_state_lock);
249 void iscsit_login_kref_put(struct kref *kref)
251 struct iscsi_tpg_np *tpg_np = container_of(kref,
252 struct iscsi_tpg_np, tpg_np_kref);
254 complete(&tpg_np->tpg_np_comp);
257 int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
258 struct iscsi_tpg_np *tpg_np)
260 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
262 up(&tpg->np_login_sem);
265 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
268 iscsit_put_tiqn_for_login(tiqn);
273 bool iscsit_check_np_match(
274 struct sockaddr_storage *sockaddr,
276 int network_transport)
278 struct sockaddr_in *sock_in, *sock_in_e;
279 struct sockaddr_in6 *sock_in6, *sock_in6_e;
280 bool ip_match = false;
283 if (sockaddr->ss_family == AF_INET6) {
284 sock_in6 = (struct sockaddr_in6 *)sockaddr;
285 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
287 if (!memcmp(&sock_in6->sin6_addr.in6_u,
288 &sock_in6_e->sin6_addr.in6_u,
289 sizeof(struct in6_addr)))
292 port = ntohs(sock_in6->sin6_port);
293 port_e = ntohs(sock_in6_e->sin6_port);
295 sock_in = (struct sockaddr_in *)sockaddr;
296 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
298 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
301 port = ntohs(sock_in->sin_port);
302 port_e = ntohs(sock_in_e->sin_port);
305 if (ip_match && (port_e == port) &&
306 (np->np_network_transport == network_transport))
313 * Called with mutex np_lock held
315 static struct iscsi_np *iscsit_get_np(
316 struct sockaddr_storage *sockaddr,
317 int network_transport)
322 list_for_each_entry(np, &g_np_list, np_list) {
323 spin_lock_bh(&np->np_thread_lock);
324 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
325 spin_unlock_bh(&np->np_thread_lock);
329 match = iscsit_check_np_match(sockaddr, np, network_transport);
332 * Increment the np_exports reference count now to
333 * prevent iscsit_del_np() below from being called
334 * while iscsi_tpg_add_network_portal() is called.
337 spin_unlock_bh(&np->np_thread_lock);
340 spin_unlock_bh(&np->np_thread_lock);
346 struct iscsi_np *iscsit_add_np(
347 struct sockaddr_storage *sockaddr,
348 int network_transport)
353 mutex_lock(&np_lock);
356 * Locate the existing struct iscsi_np if already active..
358 np = iscsit_get_np(sockaddr, network_transport);
360 mutex_unlock(&np_lock);
364 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
366 pr_err("Unable to allocate memory for struct iscsi_np\n");
367 mutex_unlock(&np_lock);
368 return ERR_PTR(-ENOMEM);
371 np->np_flags |= NPF_IP_NETWORK;
372 np->np_network_transport = network_transport;
373 spin_lock_init(&np->np_thread_lock);
374 init_completion(&np->np_restart_comp);
375 INIT_LIST_HEAD(&np->np_list);
377 ret = iscsi_target_setup_login_socket(np, sockaddr);
380 mutex_unlock(&np_lock);
384 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
385 if (IS_ERR(np->np_thread)) {
386 pr_err("Unable to create kthread: iscsi_np\n");
387 ret = PTR_ERR(np->np_thread);
389 mutex_unlock(&np_lock);
393 * Increment the np_exports reference count now to prevent
394 * iscsit_del_np() below from being run while a new call to
395 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
396 * active. We don't need to hold np->np_thread_lock at this
397 * point because iscsi_np has not been added to g_np_list yet.
400 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
402 list_add_tail(&np->np_list, &g_np_list);
403 mutex_unlock(&np_lock);
405 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
406 &np->np_sockaddr, np->np_transport->name);
411 int iscsit_reset_np_thread(
413 struct iscsi_tpg_np *tpg_np,
414 struct iscsi_portal_group *tpg,
417 spin_lock_bh(&np->np_thread_lock);
418 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
419 spin_unlock_bh(&np->np_thread_lock);
422 np->np_thread_state = ISCSI_NP_THREAD_RESET;
425 spin_unlock_bh(&np->np_thread_lock);
426 send_sig(SIGINT, np->np_thread, 1);
427 wait_for_completion(&np->np_restart_comp);
428 spin_lock_bh(&np->np_thread_lock);
430 spin_unlock_bh(&np->np_thread_lock);
432 if (tpg_np && shutdown) {
433 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
435 wait_for_completion(&tpg_np->tpg_np_comp);
441 static void iscsit_free_np(struct iscsi_np *np)
444 sock_release(np->np_socket);
447 int iscsit_del_np(struct iscsi_np *np)
449 spin_lock_bh(&np->np_thread_lock);
451 if (np->np_exports) {
453 spin_unlock_bh(&np->np_thread_lock);
456 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
457 spin_unlock_bh(&np->np_thread_lock);
461 * We need to send the signal to wakeup Linux/Net
462 * which may be sleeping in sock_accept()..
464 send_sig(SIGINT, np->np_thread, 1);
465 kthread_stop(np->np_thread);
466 np->np_thread = NULL;
469 np->np_transport->iscsit_free_np(np);
471 mutex_lock(&np_lock);
472 list_del(&np->np_list);
473 mutex_unlock(&np_lock);
475 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
476 &np->np_sockaddr, np->np_transport->name);
478 iscsit_put_transport(np->np_transport);
483 static void iscsit_get_rx_pdu(struct iscsi_conn *);
485 int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
487 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
490 EXPORT_SYMBOL(iscsit_queue_rsp);
492 void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
494 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD);
496 spin_lock_bh(&conn->cmd_lock);
497 if (!list_empty(&cmd->i_conn_node) &&
498 !(cmd->se_cmd.transport_state & CMD_T_FABRIC_STOP))
499 list_del_init(&cmd->i_conn_node);
500 spin_unlock_bh(&conn->cmd_lock);
502 __iscsit_free_cmd(cmd, scsi_cmd, true);
504 EXPORT_SYMBOL(iscsit_aborted_task);
506 static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
507 u32, u32, u8 *, u8 *);
508 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *);
511 iscsit_xmit_nondatain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
512 const void *data_buf, u32 data_buf_len)
514 struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu;
516 u32 niov = 0, tx_size = ISCSI_HDR_LEN;
519 iov = &cmd->iov_misc[0];
520 iov[niov].iov_base = cmd->pdu;
521 iov[niov++].iov_len = ISCSI_HDR_LEN;
523 if (conn->conn_ops->HeaderDigest) {
524 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
526 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
527 ISCSI_HDR_LEN, 0, NULL,
528 (u8 *)header_digest);
530 iov[0].iov_len += ISCSI_CRC_LEN;
531 tx_size += ISCSI_CRC_LEN;
532 pr_debug("Attaching CRC32C HeaderDigest"
533 " to opcode 0x%x 0x%08x\n",
534 hdr->opcode, *header_digest);
538 u32 padding = ((-data_buf_len) & 3);
540 iov[niov].iov_base = (void *)data_buf;
541 iov[niov++].iov_len = data_buf_len;
542 tx_size += data_buf_len;
545 iov[niov].iov_base = &cmd->pad_bytes;
546 iov[niov++].iov_len = padding;
548 pr_debug("Attaching %u additional"
549 " padding bytes.\n", padding);
552 if (conn->conn_ops->DataDigest) {
553 iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
554 data_buf, data_buf_len,
556 (u8 *)&cmd->pad_bytes,
557 (u8 *)&cmd->data_crc);
559 iov[niov].iov_base = &cmd->data_crc;
560 iov[niov++].iov_len = ISCSI_CRC_LEN;
561 tx_size += ISCSI_CRC_LEN;
562 pr_debug("Attached DataDigest for %u"
563 " bytes opcode 0x%x, CRC 0x%08x\n",
564 data_buf_len, hdr->opcode, cmd->data_crc);
568 cmd->iov_misc_count = niov;
569 cmd->tx_size = tx_size;
571 ret = iscsit_send_tx_data(cmd, conn, 1);
573 iscsit_tx_thread_wait_for_tcp(conn);
580 static int iscsit_map_iovec(struct iscsi_cmd *, struct kvec *, u32, u32);
581 static void iscsit_unmap_iovec(struct iscsi_cmd *);
582 static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsi_cmd *,
583 u32, u32, u32, u8 *);
585 iscsit_xmit_datain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
586 const struct iscsi_datain *datain)
589 u32 iov_count = 0, tx_size = 0;
592 iov = &cmd->iov_data[0];
593 iov[iov_count].iov_base = cmd->pdu;
594 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
595 tx_size += ISCSI_HDR_LEN;
597 if (conn->conn_ops->HeaderDigest) {
598 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
600 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
601 ISCSI_HDR_LEN, 0, NULL,
602 (u8 *)header_digest);
604 iov[0].iov_len += ISCSI_CRC_LEN;
605 tx_size += ISCSI_CRC_LEN;
607 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
611 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
612 datain->offset, datain->length);
616 iov_count += iov_ret;
617 tx_size += datain->length;
619 cmd->padding = ((-datain->length) & 3);
621 iov[iov_count].iov_base = cmd->pad_bytes;
622 iov[iov_count++].iov_len = cmd->padding;
623 tx_size += cmd->padding;
625 pr_debug("Attaching %u padding bytes\n", cmd->padding);
628 if (conn->conn_ops->DataDigest) {
629 cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
635 iov[iov_count].iov_base = &cmd->data_crc;
636 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
637 tx_size += ISCSI_CRC_LEN;
639 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
640 datain->length + cmd->padding, cmd->data_crc);
643 cmd->iov_data_count = iov_count;
644 cmd->tx_size = tx_size;
646 ret = iscsit_fe_sendpage_sg(cmd, conn);
648 iscsit_unmap_iovec(cmd);
651 iscsit_tx_thread_wait_for_tcp(conn);
658 static int iscsit_xmit_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
659 struct iscsi_datain_req *dr, const void *buf,
663 return iscsit_xmit_datain_pdu(conn, cmd, buf);
665 return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len);
668 static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
670 return TARGET_PROT_NORMAL;
673 static struct iscsit_transport iscsi_target_transport = {
675 .transport_type = ISCSI_TCP,
676 .rdma_shutdown = false,
678 .iscsit_setup_np = iscsit_setup_np,
679 .iscsit_accept_np = iscsit_accept_np,
680 .iscsit_free_np = iscsit_free_np,
681 .iscsit_get_login_rx = iscsit_get_login_rx,
682 .iscsit_put_login_tx = iscsit_put_login_tx,
683 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
684 .iscsit_immediate_queue = iscsit_immediate_queue,
685 .iscsit_response_queue = iscsit_response_queue,
686 .iscsit_queue_data_in = iscsit_queue_rsp,
687 .iscsit_queue_status = iscsit_queue_rsp,
688 .iscsit_aborted_task = iscsit_aborted_task,
689 .iscsit_xmit_pdu = iscsit_xmit_pdu,
690 .iscsit_get_rx_pdu = iscsit_get_rx_pdu,
691 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
694 static int __init iscsi_target_init_module(void)
698 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
700 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
701 if (!iscsit_global) {
702 pr_err("Unable to allocate memory for iscsit_global\n");
705 spin_lock_init(&iscsit_global->ts_bitmap_lock);
706 mutex_init(&auth_id_lock);
707 spin_lock_init(&sess_idr_lock);
711 ret = target_register_template(&iscsi_ops);
715 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
716 iscsit_global->ts_bitmap = vzalloc(size);
717 if (!iscsit_global->ts_bitmap) {
718 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
722 lio_qr_cache = kmem_cache_create("lio_qr_cache",
723 sizeof(struct iscsi_queue_req),
724 __alignof__(struct iscsi_queue_req), 0, NULL);
726 pr_err("nable to kmem_cache_create() for"
731 lio_dr_cache = kmem_cache_create("lio_dr_cache",
732 sizeof(struct iscsi_datain_req),
733 __alignof__(struct iscsi_datain_req), 0, NULL);
735 pr_err("Unable to kmem_cache_create() for"
740 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
741 sizeof(struct iscsi_ooo_cmdsn),
742 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
743 if (!lio_ooo_cache) {
744 pr_err("Unable to kmem_cache_create() for"
749 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
750 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
752 if (!lio_r2t_cache) {
753 pr_err("Unable to kmem_cache_create() for"
758 iscsit_register_transport(&iscsi_target_transport);
760 if (iscsit_load_discovery_tpg() < 0)
765 iscsit_unregister_transport(&iscsi_target_transport);
766 kmem_cache_destroy(lio_r2t_cache);
768 kmem_cache_destroy(lio_ooo_cache);
770 kmem_cache_destroy(lio_dr_cache);
772 kmem_cache_destroy(lio_qr_cache);
774 vfree(iscsit_global->ts_bitmap);
776 /* XXX: this probably wants it to be it's own unwind step.. */
777 if (iscsit_global->discovery_tpg)
778 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
779 target_unregister_template(&iscsi_ops);
781 kfree(iscsit_global);
785 static void __exit iscsi_target_cleanup_module(void)
787 iscsit_release_discovery_tpg();
788 iscsit_unregister_transport(&iscsi_target_transport);
789 kmem_cache_destroy(lio_qr_cache);
790 kmem_cache_destroy(lio_dr_cache);
791 kmem_cache_destroy(lio_ooo_cache);
792 kmem_cache_destroy(lio_r2t_cache);
795 * Shutdown discovery sessions and disable discovery TPG
797 if (iscsit_global->discovery_tpg)
798 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
800 target_unregister_template(&iscsi_ops);
802 vfree(iscsit_global->ts_bitmap);
803 kfree(iscsit_global);
806 int iscsit_add_reject(
807 struct iscsi_conn *conn,
811 struct iscsi_cmd *cmd;
813 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
817 cmd->iscsi_opcode = ISCSI_OP_REJECT;
818 cmd->reject_reason = reason;
820 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
822 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
823 iscsit_free_cmd(cmd, false);
827 spin_lock_bh(&conn->cmd_lock);
828 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
829 spin_unlock_bh(&conn->cmd_lock);
831 cmd->i_state = ISTATE_SEND_REJECT;
832 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
836 EXPORT_SYMBOL(iscsit_add_reject);
838 static int iscsit_add_reject_from_cmd(
839 struct iscsi_cmd *cmd,
844 struct iscsi_conn *conn;
847 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
853 cmd->iscsi_opcode = ISCSI_OP_REJECT;
854 cmd->reject_reason = reason;
856 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
858 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
859 iscsit_free_cmd(cmd, false);
864 spin_lock_bh(&conn->cmd_lock);
865 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
866 spin_unlock_bh(&conn->cmd_lock);
869 cmd->i_state = ISTATE_SEND_REJECT;
870 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
872 * Perform the kref_put now if se_cmd has already been setup by
873 * scsit_setup_scsi_cmd()
875 if (cmd->se_cmd.se_tfo != NULL) {
876 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
877 target_put_sess_cmd(&cmd->se_cmd);
882 static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
885 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
888 int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
890 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
892 EXPORT_SYMBOL(iscsit_reject_cmd);
895 * Map some portion of the allocated scatterlist to an iovec, suitable for
896 * kernel sockets to copy data in/out.
898 static int iscsit_map_iovec(
899 struct iscsi_cmd *cmd,
905 struct scatterlist *sg;
906 unsigned int page_off;
909 * We know each entry in t_data_sg contains a page.
911 u32 ent = data_offset / PAGE_SIZE;
913 if (ent >= cmd->se_cmd.t_data_nents) {
914 pr_err("Initial page entry out-of-bounds\n");
918 sg = &cmd->se_cmd.t_data_sg[ent];
919 page_off = (data_offset % PAGE_SIZE);
921 cmd->first_data_sg = sg;
922 cmd->first_data_sg_off = page_off;
924 while (data_length) {
925 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
927 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
928 iov[i].iov_len = cur_len;
930 data_length -= cur_len;
936 cmd->kmapped_nents = i;
941 static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
944 struct scatterlist *sg;
946 sg = cmd->first_data_sg;
948 for (i = 0; i < cmd->kmapped_nents; i++)
949 kunmap(sg_page(&sg[i]));
952 static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
955 struct iscsi_cmd *cmd, *cmd_p;
957 conn->exp_statsn = exp_statsn;
959 if (conn->sess->sess_ops->RDMAExtensions)
962 spin_lock_bh(&conn->cmd_lock);
963 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
964 spin_lock(&cmd->istate_lock);
965 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
966 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
967 cmd->i_state = ISTATE_REMOVE;
968 spin_unlock(&cmd->istate_lock);
969 list_move_tail(&cmd->i_conn_node, &ack_list);
972 spin_unlock(&cmd->istate_lock);
974 spin_unlock_bh(&conn->cmd_lock);
976 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
977 list_del_init(&cmd->i_conn_node);
978 iscsit_free_cmd(cmd, false);
982 static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
984 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
986 iov_count += ISCSI_IOV_DATA_BUFFER;
988 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
989 if (!cmd->iov_data) {
990 pr_err("Unable to allocate cmd->iov_data\n");
994 cmd->orig_iov_data_count = iov_count;
998 int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1001 int data_direction, payload_length;
1002 struct iscsi_scsi_req *hdr;
1003 int iscsi_task_attr;
1006 atomic_long_inc(&conn->sess->cmd_pdus);
1008 hdr = (struct iscsi_scsi_req *) buf;
1009 payload_length = ntoh24(hdr->dlength);
1011 /* FIXME; Add checks for AdditionalHeaderSegment */
1013 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
1014 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1015 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1016 " not set. Bad iSCSI Initiator.\n");
1017 return iscsit_add_reject_cmd(cmd,
1018 ISCSI_REASON_BOOKMARK_INVALID, buf);
1021 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
1022 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
1024 * From RFC-3720 Section 10.3.1:
1026 * "Either or both of R and W MAY be 1 when either the
1027 * Expected Data Transfer Length and/or Bidirectional Read
1028 * Expected Data Transfer Length are 0"
1030 * For this case, go ahead and clear the unnecssary bits
1031 * to avoid any confusion with ->data_direction.
1033 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
1034 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
1036 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
1037 " set when Expected Data Transfer Length is 0 for"
1038 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
1041 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
1042 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
1043 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1044 " MUST be set if Expected Data Transfer Length is not 0."
1045 " Bad iSCSI Initiator\n");
1046 return iscsit_add_reject_cmd(cmd,
1047 ISCSI_REASON_BOOKMARK_INVALID, buf);
1050 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
1051 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
1052 pr_err("Bidirectional operations not supported!\n");
1053 return iscsit_add_reject_cmd(cmd,
1054 ISCSI_REASON_BOOKMARK_INVALID, buf);
1057 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1058 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1059 " Scsi Command PDU.\n");
1060 return iscsit_add_reject_cmd(cmd,
1061 ISCSI_REASON_BOOKMARK_INVALID, buf);
1064 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
1065 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1066 " protocol error.\n", payload_length);
1067 return iscsit_add_reject_cmd(cmd,
1068 ISCSI_REASON_PROTOCOL_ERROR, buf);
1071 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
1072 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
1073 pr_err("Expected Data Transfer Length and Length of"
1074 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1075 " bit is not set protocol error\n");
1076 return iscsit_add_reject_cmd(cmd,
1077 ISCSI_REASON_PROTOCOL_ERROR, buf);
1080 if (payload_length > be32_to_cpu(hdr->data_length)) {
1081 pr_err("DataSegmentLength: %u is greater than"
1082 " EDTL: %u, protocol error.\n", payload_length,
1084 return iscsit_add_reject_cmd(cmd,
1085 ISCSI_REASON_PROTOCOL_ERROR, buf);
1088 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1089 pr_err("DataSegmentLength: %u is greater than"
1090 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1091 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
1092 return iscsit_add_reject_cmd(cmd,
1093 ISCSI_REASON_PROTOCOL_ERROR, buf);
1096 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
1097 pr_err("DataSegmentLength: %u is greater than"
1098 " FirstBurstLength: %u, protocol error.\n",
1099 payload_length, conn->sess->sess_ops->FirstBurstLength);
1100 return iscsit_add_reject_cmd(cmd,
1101 ISCSI_REASON_BOOKMARK_INVALID, buf);
1104 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
1105 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
1108 cmd->data_direction = data_direction;
1109 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
1111 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1113 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
1114 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
1115 sam_task_attr = TCM_SIMPLE_TAG;
1116 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
1117 sam_task_attr = TCM_ORDERED_TAG;
1118 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
1119 sam_task_attr = TCM_HEAD_TAG;
1120 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
1121 sam_task_attr = TCM_ACA_TAG;
1123 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
1124 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
1125 sam_task_attr = TCM_SIMPLE_TAG;
1128 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
1129 cmd->i_state = ISTATE_NEW_CMD;
1130 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1131 cmd->immediate_data = (payload_length) ? 1 : 0;
1132 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
1133 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
1134 if (cmd->unsolicited_data)
1135 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
1137 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1138 if (hdr->flags & ISCSI_FLAG_CMD_READ)
1139 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
1141 cmd->targ_xfer_tag = 0xFFFFFFFF;
1142 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1143 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1144 cmd->first_burst_len = payload_length;
1146 if (!conn->sess->sess_ops->RDMAExtensions &&
1147 cmd->data_direction == DMA_FROM_DEVICE) {
1148 struct iscsi_datain_req *dr;
1150 dr = iscsit_allocate_datain_req();
1152 return iscsit_add_reject_cmd(cmd,
1153 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1155 iscsit_attach_datain_req(cmd, dr);
1159 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1161 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
1162 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
1163 cmd->data_direction, sam_task_attr,
1164 cmd->sense_buffer + 2);
1166 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1167 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
1168 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1171 target_get_sess_cmd(&cmd->se_cmd, true);
1173 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1174 scsilun_to_int(&hdr->lun));
1175 if (cmd->sense_reason)
1178 /* only used for printks or comparing with ->ref_task_tag */
1179 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
1180 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1181 if (cmd->sense_reason) {
1182 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
1183 return iscsit_add_reject_cmd(cmd,
1184 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1190 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
1191 return iscsit_add_reject_cmd(cmd,
1192 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1196 spin_lock_bh(&conn->cmd_lock);
1197 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1198 spin_unlock_bh(&conn->cmd_lock);
1200 * Check if we need to delay processing because of ALUA
1201 * Active/NonOptimized primary access state..
1203 core_alua_check_nonop_delay(&cmd->se_cmd);
1207 EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
1209 void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1211 iscsit_set_dataout_sequence_values(cmd);
1213 spin_lock_bh(&cmd->dataout_timeout_lock);
1214 iscsit_start_dataout_timer(cmd, cmd->conn);
1215 spin_unlock_bh(&cmd->dataout_timeout_lock);
1217 EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1219 int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1220 struct iscsi_scsi_req *hdr)
1224 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1225 * the Immediate Bit is not set, and no Immediate
1228 * A PDU/CmdSN carrying Immediate Data can only
1229 * be processed after the DataCRC has passed.
1230 * If the DataCRC fails, the CmdSN MUST NOT
1231 * be acknowledged. (See below)
1233 if (!cmd->immediate_data) {
1234 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1235 (unsigned char *)hdr, hdr->cmdsn);
1236 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1238 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1239 target_put_sess_cmd(&cmd->se_cmd);
1244 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1247 * If no Immediate Data is attached, it's OK to return now.
1249 if (!cmd->immediate_data) {
1250 if (!cmd->sense_reason && cmd->unsolicited_data)
1251 iscsit_set_unsoliticed_dataout(cmd);
1252 if (!cmd->sense_reason)
1255 target_put_sess_cmd(&cmd->se_cmd);
1260 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1261 * execution. These exceptions are processed in CmdSN order using
1262 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1264 if (cmd->sense_reason) {
1265 if (cmd->reject_reason)
1271 * Call directly into transport_generic_new_cmd() to perform
1272 * the backend memory allocation.
1274 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
1275 if (cmd->sense_reason)
1280 EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1283 iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1286 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1288 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1291 goto after_immediate_data;
1293 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1294 cmd->first_burst_len);
1295 after_immediate_data:
1296 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1298 * A PDU/CmdSN carrying Immediate Data passed
1299 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1300 * Immediate Bit is not set.
1302 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1303 (unsigned char *)hdr, hdr->cmdsn);
1304 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1307 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1310 rc = iscsit_dump_data_payload(cmd->conn,
1311 cmd->first_burst_len, 1);
1312 target_put_sess_cmd(&cmd->se_cmd);
1314 } else if (cmd->unsolicited_data)
1315 iscsit_set_unsoliticed_dataout(cmd);
1317 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1319 * Immediate Data failed DataCRC and ERL>=1,
1320 * silently drop this PDU and let the initiator
1321 * plug the CmdSN gap.
1323 * FIXME: Send Unsolicited NOPIN with reserved
1324 * TTT here to help the initiator figure out
1325 * the missing CmdSN, although they should be
1326 * intelligent enough to determine the missing
1327 * CmdSN and issue a retry to plug the sequence.
1329 cmd->i_state = ISTATE_REMOVE;
1330 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
1331 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1338 iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1341 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1343 bool dump_payload = false;
1345 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1349 * Allocation iovecs needed for struct socket operations for
1350 * traditional iSCSI block I/O.
1352 if (iscsit_allocate_iovecs(cmd) < 0) {
1353 return iscsit_reject_cmd(cmd,
1354 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1356 immed_data = cmd->immediate_data;
1358 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1362 dump_payload = true;
1367 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1370 static u32 iscsit_do_crypto_hash_sg(
1371 struct ahash_request *hash,
1372 struct iscsi_cmd *cmd,
1379 struct scatterlist *sg;
1380 unsigned int page_off;
1382 crypto_ahash_init(hash);
1384 sg = cmd->first_data_sg;
1385 page_off = cmd->first_data_sg_off;
1387 while (data_length) {
1388 u32 cur_len = min_t(u32, data_length, (sg->length - page_off));
1390 ahash_request_set_crypt(hash, sg, NULL, cur_len);
1391 crypto_ahash_update(hash);
1393 data_length -= cur_len;
1395 /* iscsit_map_iovec has already checked for invalid sg pointers */
1400 struct scatterlist pad_sg;
1402 sg_init_one(&pad_sg, pad_bytes, padding);
1403 ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
1405 crypto_ahash_finup(hash);
1407 ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
1408 crypto_ahash_final(hash);
1414 static void iscsit_do_crypto_hash_buf(
1415 struct ahash_request *hash,
1422 struct scatterlist sg[2];
1424 sg_init_table(sg, ARRAY_SIZE(sg));
1425 sg_set_buf(sg, buf, payload_length);
1426 sg_set_buf(sg + 1, pad_bytes, padding);
1428 ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
1430 crypto_ahash_digest(hash);
1434 iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1435 struct iscsi_cmd **out_cmd)
1437 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1438 struct iscsi_cmd *cmd = NULL;
1439 struct se_cmd *se_cmd;
1440 u32 payload_length = ntoh24(hdr->dlength);
1443 if (!payload_length) {
1444 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1449 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
1451 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1452 pr_err("DataSegmentLength: %u is greater than"
1453 " MaxXmitDataSegmentLength: %u\n", payload_length,
1454 conn->conn_ops->MaxXmitDataSegmentLength);
1455 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1459 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1464 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1465 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1466 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
1467 payload_length, conn->cid);
1469 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1470 pr_err("Command ITT: 0x%08x received DataOUT after"
1471 " last DataOUT received, dumping payload\n",
1472 cmd->init_task_tag);
1473 return iscsit_dump_data_payload(conn, payload_length, 1);
1476 if (cmd->data_direction != DMA_TO_DEVICE) {
1477 pr_err("Command ITT: 0x%08x received DataOUT for a"
1478 " NON-WRITE command.\n", cmd->init_task_tag);
1479 return iscsit_dump_data_payload(conn, payload_length, 1);
1481 se_cmd = &cmd->se_cmd;
1482 iscsit_mod_dataout_timer(cmd);
1484 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
1485 pr_err("DataOut Offset: %u, Length %u greater than"
1486 " iSCSI Command EDTL %u, protocol error.\n",
1487 hdr->offset, payload_length, cmd->se_cmd.data_length);
1488 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
1491 if (cmd->unsolicited_data) {
1492 int dump_unsolicited_data = 0;
1494 if (conn->sess->sess_ops->InitialR2T) {
1495 pr_err("Received unexpected unsolicited data"
1496 " while InitialR2T=Yes, protocol error.\n");
1497 transport_send_check_condition_and_sense(&cmd->se_cmd,
1498 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1502 * Special case for dealing with Unsolicited DataOUT
1503 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1507 /* Something's amiss if we're not in WRITE_PENDING state... */
1508 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1509 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
1510 dump_unsolicited_data = 1;
1512 if (dump_unsolicited_data) {
1514 * Check if a delayed TASK_ABORTED status needs to
1515 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1516 * received with the unsolicited data out.
1518 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1519 iscsit_stop_dataout_timer(cmd);
1521 transport_check_aborted_status(se_cmd,
1522 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1523 return iscsit_dump_data_payload(conn, payload_length, 1);
1527 * For the normal solicited data path:
1529 * Check for a delayed TASK_ABORTED status and dump any
1530 * incoming data out payload if one exists. Also, when the
1531 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1532 * data out sequence, we decrement outstanding_r2ts. Once
1533 * outstanding_r2ts reaches zero, go ahead and send the delayed
1534 * TASK_ABORTED status.
1536 if (se_cmd->transport_state & CMD_T_ABORTED) {
1537 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1538 if (--cmd->outstanding_r2ts < 1) {
1539 iscsit_stop_dataout_timer(cmd);
1540 transport_check_aborted_status(
1544 return iscsit_dump_data_payload(conn, payload_length, 1);
1548 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1549 * within-command recovery checks before receiving the payload.
1551 rc = iscsit_check_pre_dataout(cmd, buf);
1552 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
1554 else if (rc == DATAOUT_CANNOT_RECOVER)
1560 EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1563 iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1564 struct iscsi_data *hdr)
1567 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1568 u32 payload_length = ntoh24(hdr->dlength);
1569 int iov_ret, data_crc_failed = 0;
1571 rx_size += payload_length;
1572 iov = &cmd->iov_data[0];
1574 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1579 iov_count += iov_ret;
1581 padding = ((-payload_length) & 3);
1583 iov[iov_count].iov_base = cmd->pad_bytes;
1584 iov[iov_count++].iov_len = padding;
1586 pr_debug("Receiving %u padding bytes.\n", padding);
1589 if (conn->conn_ops->DataDigest) {
1590 iov[iov_count].iov_base = &checksum;
1591 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1592 rx_size += ISCSI_CRC_LEN;
1595 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1597 iscsit_unmap_iovec(cmd);
1599 if (rx_got != rx_size)
1602 if (conn->conn_ops->DataDigest) {
1605 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
1606 be32_to_cpu(hdr->offset),
1607 payload_length, padding,
1610 if (checksum != data_crc) {
1611 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1612 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1613 " does not match computed 0x%08x\n",
1614 hdr->itt, hdr->offset, payload_length,
1615 hdr->datasn, checksum, data_crc);
1616 data_crc_failed = 1;
1618 pr_debug("Got CRC32C DataDigest 0x%08x for"
1619 " %u bytes of Data Out\n", checksum,
1624 return data_crc_failed;
1628 iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1629 bool data_crc_failed)
1631 struct iscsi_conn *conn = cmd->conn;
1634 * Increment post receive data and CRC values or perform
1635 * within-command recovery.
1637 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1638 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
1640 else if (rc == DATAOUT_SEND_R2T) {
1641 iscsit_set_dataout_sequence_values(cmd);
1642 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1643 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
1645 * Handle extra special case for out of order
1646 * Unsolicited Data Out.
1648 spin_lock_bh(&cmd->istate_lock);
1649 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1650 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1651 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1652 spin_unlock_bh(&cmd->istate_lock);
1654 iscsit_stop_dataout_timer(cmd);
1657 target_execute_cmd(&cmd->se_cmd);
1659 } else /* DATAOUT_CANNOT_RECOVER */
1664 EXPORT_SYMBOL(iscsit_check_dataout_payload);
1666 static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1668 struct iscsi_cmd *cmd = NULL;
1669 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1671 bool data_crc_failed = false;
1673 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1679 rc = iscsit_get_dataout(conn, cmd, hdr);
1683 data_crc_failed = true;
1685 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1688 int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1689 struct iscsi_nopout *hdr)
1691 u32 payload_length = ntoh24(hdr->dlength);
1693 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1694 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1696 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1697 (unsigned char *)hdr);
1699 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1700 (unsigned char *)hdr);
1703 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1704 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1705 " not set, protocol error.\n");
1707 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1708 (unsigned char *)hdr);
1710 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1711 (unsigned char *)hdr);
1714 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1715 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1716 " greater than MaxXmitDataSegmentLength: %u, protocol"
1717 " error.\n", payload_length,
1718 conn->conn_ops->MaxXmitDataSegmentLength);
1720 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1721 (unsigned char *)hdr);
1723 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1724 (unsigned char *)hdr);
1727 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1728 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1729 hdr->itt == RESERVED_ITT ? "Response" : "Request",
1730 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1733 * This is not a response to a Unsolicited NopIN, which means
1734 * it can either be a NOPOUT ping request (with a valid ITT),
1735 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1736 * Either way, make sure we allocate an struct iscsi_cmd, as both
1737 * can contain ping data.
1739 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1740 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1741 cmd->i_state = ISTATE_SEND_NOPIN;
1742 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1744 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1745 cmd->targ_xfer_tag = 0xFFFFFFFF;
1746 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1747 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1748 cmd->data_direction = DMA_NONE;
1753 EXPORT_SYMBOL(iscsit_setup_nop_out);
1755 int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1756 struct iscsi_nopout *hdr)
1758 struct iscsi_cmd *cmd_p = NULL;
1761 * Initiator is expecting a NopIN ping reply..
1763 if (hdr->itt != RESERVED_ITT) {
1765 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1766 (unsigned char *)hdr);
1768 spin_lock_bh(&conn->cmd_lock);
1769 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1770 spin_unlock_bh(&conn->cmd_lock);
1772 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1774 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1775 iscsit_add_cmd_to_response_queue(cmd, conn,
1780 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1781 (unsigned char *)hdr, hdr->cmdsn);
1782 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1784 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1790 * This was a response to a unsolicited NOPIN ping.
1792 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1793 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1797 iscsit_stop_nopin_response_timer(conn);
1799 cmd_p->i_state = ISTATE_REMOVE;
1800 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1802 iscsit_start_nopin_timer(conn);
1806 * Otherwise, initiator is not expecting a NOPIN is response.
1807 * Just ignore for now.
1811 iscsit_free_cmd(cmd, false);
1815 EXPORT_SYMBOL(iscsit_process_nop_out);
1817 static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1820 unsigned char *ping_data = NULL;
1821 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1822 struct kvec *iov = NULL;
1823 u32 payload_length = ntoh24(hdr->dlength);
1826 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1830 * Handle NOP-OUT payload for traditional iSCSI sockets
1832 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1833 u32 checksum, data_crc, padding = 0;
1834 int niov = 0, rx_got, rx_size = payload_length;
1836 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1838 pr_err("Unable to allocate memory for"
1839 " NOPOUT ping data.\n");
1844 iov = &cmd->iov_misc[0];
1845 iov[niov].iov_base = ping_data;
1846 iov[niov++].iov_len = payload_length;
1848 padding = ((-payload_length) & 3);
1850 pr_debug("Receiving %u additional bytes"
1851 " for padding.\n", padding);
1852 iov[niov].iov_base = &cmd->pad_bytes;
1853 iov[niov++].iov_len = padding;
1856 if (conn->conn_ops->DataDigest) {
1857 iov[niov].iov_base = &checksum;
1858 iov[niov++].iov_len = ISCSI_CRC_LEN;
1859 rx_size += ISCSI_CRC_LEN;
1862 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1863 if (rx_got != rx_size) {
1868 if (conn->conn_ops->DataDigest) {
1869 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
1870 ping_data, payload_length,
1871 padding, cmd->pad_bytes,
1874 if (checksum != data_crc) {
1875 pr_err("Ping data CRC32C DataDigest"
1876 " 0x%08x does not match computed 0x%08x\n",
1877 checksum, data_crc);
1878 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1879 pr_err("Unable to recover from"
1880 " NOPOUT Ping DataCRC failure while in"
1886 * Silently drop this PDU and let the
1887 * initiator plug the CmdSN gap.
1889 pr_debug("Dropping NOPOUT"
1890 " Command CmdSN: 0x%08x due to"
1891 " DataCRC error.\n", hdr->cmdsn);
1896 pr_debug("Got CRC32C DataDigest"
1897 " 0x%08x for %u bytes of ping data.\n",
1898 checksum, payload_length);
1902 ping_data[payload_length] = '\0';
1904 * Attach ping data to struct iscsi_cmd->buf_ptr.
1906 cmd->buf_ptr = ping_data;
1907 cmd->buf_ptr_size = payload_length;
1909 pr_debug("Got %u bytes of NOPOUT ping"
1910 " data.\n", payload_length);
1911 pr_debug("Ping Data: \"%s\"\n", ping_data);
1914 return iscsit_process_nop_out(conn, cmd, hdr);
1917 iscsit_free_cmd(cmd, false);
1924 iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1927 struct se_tmr_req *se_tmr;
1928 struct iscsi_tmr_req *tmr_req;
1929 struct iscsi_tm *hdr;
1930 int out_of_order_cmdsn = 0, ret;
1931 bool sess_ref = false;
1934 hdr = (struct iscsi_tm *) buf;
1935 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1936 function = hdr->flags;
1938 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1939 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1940 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1941 hdr->rtt, hdr->refcmdsn, conn->cid);
1943 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1944 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1945 hdr->rtt != RESERVED_ITT)) {
1946 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1947 hdr->rtt = RESERVED_ITT;
1950 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1951 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1952 pr_err("Task Management Request TASK_REASSIGN not"
1953 " issued as immediate command, bad iSCSI Initiator"
1954 "implementation\n");
1955 return iscsit_add_reject_cmd(cmd,
1956 ISCSI_REASON_PROTOCOL_ERROR, buf);
1958 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1959 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1960 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
1962 cmd->data_direction = DMA_NONE;
1964 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1965 if (!cmd->tmr_req) {
1966 pr_err("Unable to allocate memory for"
1967 " Task Management command!\n");
1968 return iscsit_add_reject_cmd(cmd,
1969 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1974 * TASK_REASSIGN for ERL=2 / connection stays inside of
1975 * LIO-Target $FABRIC_MOD
1977 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1982 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
1983 conn->sess->se_sess, 0, DMA_NONE,
1984 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
1986 target_get_sess_cmd(&cmd->se_cmd, true);
1990 case ISCSI_TM_FUNC_ABORT_TASK:
1991 tcm_function = TMR_ABORT_TASK;
1993 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1994 tcm_function = TMR_ABORT_TASK_SET;
1996 case ISCSI_TM_FUNC_CLEAR_ACA:
1997 tcm_function = TMR_CLEAR_ACA;
1999 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2000 tcm_function = TMR_CLEAR_TASK_SET;
2002 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2003 tcm_function = TMR_LUN_RESET;
2005 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2006 tcm_function = TMR_TARGET_WARM_RESET;
2008 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2009 tcm_function = TMR_TARGET_COLD_RESET;
2012 pr_err("Unknown iSCSI TMR Function:"
2013 " 0x%02x\n", function);
2014 return iscsit_add_reject_cmd(cmd,
2015 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
2018 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
2019 tcm_function, GFP_KERNEL);
2021 return iscsit_add_reject_cmd(cmd,
2022 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
2024 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
2027 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
2028 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
2029 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2030 cmd->init_task_tag = hdr->itt;
2031 cmd->targ_xfer_tag = 0xFFFFFFFF;
2032 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2033 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2034 se_tmr = cmd->se_cmd.se_tmr_req;
2035 tmr_req = cmd->tmr_req;
2037 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2039 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
2040 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
2041 scsilun_to_int(&hdr->lun));
2043 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2049 case ISCSI_TM_FUNC_ABORT_TASK:
2050 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
2051 if (se_tmr->response)
2054 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2055 case ISCSI_TM_FUNC_CLEAR_ACA:
2056 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2057 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2059 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2060 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
2061 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2065 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2066 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
2067 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2071 case ISCSI_TM_FUNC_TASK_REASSIGN:
2072 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2074 * Perform sanity checks on the ExpDataSN only if the
2075 * TASK_REASSIGN was successful.
2077 if (se_tmr->response)
2080 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
2081 return iscsit_add_reject_cmd(cmd,
2082 ISCSI_REASON_BOOKMARK_INVALID, buf);
2085 pr_err("Unknown TMR function: 0x%02x, protocol"
2086 " error.\n", function);
2087 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2091 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2092 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2093 se_tmr->call_transport = 1;
2095 spin_lock_bh(&conn->cmd_lock);
2096 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2097 spin_unlock_bh(&conn->cmd_lock);
2099 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2100 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
2101 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
2102 out_of_order_cmdsn = 1;
2103 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
2105 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2108 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2110 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
2113 * Found the referenced task, send to transport for processing.
2115 if (se_tmr->call_transport)
2116 return transport_generic_handle_tmr(&cmd->se_cmd);
2119 * Could not find the referenced LUN, task, or Task Management
2120 * command not authorized or supported. Change state and
2121 * let the tx_thread send the response.
2123 * For connection recovery, this is also the default action for
2124 * TMR TASK_REASSIGN.
2127 pr_debug("Handle TMR, using sess_ref=true check\n");
2128 target_put_sess_cmd(&cmd->se_cmd);
2131 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2134 EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
2136 /* #warning FIXME: Support Text Command parameters besides SendTargets */
2138 iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2139 struct iscsi_text *hdr)
2141 u32 payload_length = ntoh24(hdr->dlength);
2143 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
2144 pr_err("Unable to accept text parameter length: %u"
2145 "greater than MaxXmitDataSegmentLength %u.\n",
2146 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
2147 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2148 (unsigned char *)hdr);
2151 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2152 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2153 pr_err("Multi sequence text commands currently not supported\n");
2154 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2155 (unsigned char *)hdr);
2158 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2159 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2160 hdr->exp_statsn, payload_length);
2162 cmd->iscsi_opcode = ISCSI_OP_TEXT;
2163 cmd->i_state = ISTATE_SEND_TEXTRSP;
2164 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2165 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2166 cmd->targ_xfer_tag = 0xFFFFFFFF;
2167 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2168 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2169 cmd->data_direction = DMA_NONE;
2170 cmd->text_in_ptr = NULL;
2174 EXPORT_SYMBOL(iscsit_setup_text_cmd);
2177 iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2178 struct iscsi_text *hdr)
2180 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
2184 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2185 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2186 pr_err("Unable to locate text_in buffer for sendtargets"
2190 goto empty_sendtargets;
2192 if (strncmp("SendTargets", text_in, 11) != 0) {
2193 pr_err("Received Text Data that is not"
2194 " SendTargets, cannot continue.\n");
2197 text_ptr = strchr(text_in, '=');
2199 pr_err("No \"=\" separator found in Text Data,"
2200 " cannot continue.\n");
2203 if (!strncmp("=All", text_ptr, 4)) {
2204 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
2205 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2206 !strncmp("=eui.", text_ptr, 5)) {
2207 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
2209 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2213 spin_lock_bh(&conn->cmd_lock);
2214 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2215 spin_unlock_bh(&conn->cmd_lock);
2218 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2220 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2221 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2222 (unsigned char *)hdr, hdr->cmdsn);
2223 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2229 return iscsit_execute_cmd(cmd, 0);
2232 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2233 (unsigned char *)hdr);
2235 EXPORT_SYMBOL(iscsit_process_text_cmd);
2238 iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2241 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2242 char *text_in = NULL;
2243 u32 payload_length = ntoh24(hdr->dlength);
2246 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2250 rx_size = payload_length;
2251 if (payload_length) {
2252 u32 checksum = 0, data_crc = 0;
2253 u32 padding = 0, pad_bytes = 0;
2254 int niov = 0, rx_got;
2257 text_in = kzalloc(payload_length, GFP_KERNEL);
2259 pr_err("Unable to allocate memory for"
2260 " incoming text parameters\n");
2263 cmd->text_in_ptr = text_in;
2265 memset(iov, 0, 3 * sizeof(struct kvec));
2266 iov[niov].iov_base = text_in;
2267 iov[niov++].iov_len = payload_length;
2269 padding = ((-payload_length) & 3);
2271 iov[niov].iov_base = &pad_bytes;
2272 iov[niov++].iov_len = padding;
2274 pr_debug("Receiving %u additional bytes"
2275 " for padding.\n", padding);
2277 if (conn->conn_ops->DataDigest) {
2278 iov[niov].iov_base = &checksum;
2279 iov[niov++].iov_len = ISCSI_CRC_LEN;
2280 rx_size += ISCSI_CRC_LEN;
2283 rx_got = rx_data(conn, &iov[0], niov, rx_size);
2284 if (rx_got != rx_size)
2287 if (conn->conn_ops->DataDigest) {
2288 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
2289 text_in, payload_length,
2290 padding, (u8 *)&pad_bytes,
2293 if (checksum != data_crc) {
2294 pr_err("Text data CRC32C DataDigest"
2295 " 0x%08x does not match computed"
2296 " 0x%08x\n", checksum, data_crc);
2297 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2298 pr_err("Unable to recover from"
2299 " Text Data digest failure while in"
2304 * Silently drop this PDU and let the
2305 * initiator plug the CmdSN gap.
2307 pr_debug("Dropping Text"
2308 " Command CmdSN: 0x%08x due to"
2309 " DataCRC error.\n", hdr->cmdsn);
2314 pr_debug("Got CRC32C DataDigest"
2315 " 0x%08x for %u bytes of text data.\n",
2316 checksum, payload_length);
2319 text_in[payload_length - 1] = '\0';
2320 pr_debug("Successfully read %d bytes of text"
2321 " data.\n", payload_length);
2324 return iscsit_process_text_cmd(conn, cmd, hdr);
2327 kfree(cmd->text_in_ptr);
2328 cmd->text_in_ptr = NULL;
2329 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
2332 int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2334 struct iscsi_conn *conn_p;
2335 struct iscsi_session *sess = conn->sess;
2337 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2338 " for SID: %u.\n", conn->cid, conn->sess->sid);
2340 atomic_set(&sess->session_logout, 1);
2341 atomic_set(&conn->conn_logout_remove, 1);
2342 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2344 iscsit_inc_conn_usage_count(conn);
2345 iscsit_inc_session_usage_count(sess);
2347 spin_lock_bh(&sess->conn_lock);
2348 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2349 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2352 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2353 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2355 spin_unlock_bh(&sess->conn_lock);
2357 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2362 int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2364 struct iscsi_conn *l_conn;
2365 struct iscsi_session *sess = conn->sess;
2367 pr_debug("Received logout request CLOSECONNECTION for CID:"
2368 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2371 * A Logout Request with a CLOSECONNECTION reason code for a CID
2372 * can arrive on a connection with a differing CID.
2374 if (conn->cid == cmd->logout_cid) {
2375 spin_lock_bh(&conn->state_lock);
2376 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2377 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2379 atomic_set(&conn->conn_logout_remove, 1);
2380 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2381 iscsit_inc_conn_usage_count(conn);
2383 spin_unlock_bh(&conn->state_lock);
2386 * Handle all different cid CLOSECONNECTION requests in
2387 * iscsit_logout_post_handler_diffcid() as to give enough
2388 * time for any non immediate command's CmdSN to be
2389 * acknowledged on the connection in question.
2391 * Here we simply make sure the CID is still around.
2393 l_conn = iscsit_get_conn_from_cid(sess,
2396 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2397 iscsit_add_cmd_to_response_queue(cmd, conn,
2402 iscsit_dec_conn_usage_count(l_conn);
2405 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2410 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2412 struct iscsi_session *sess = conn->sess;
2414 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2415 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2417 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2418 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2419 " while ERL!=2.\n");
2420 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2421 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2425 if (conn->cid == cmd->logout_cid) {
2426 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2427 " with CID: %hu on CID: %hu, implementation error.\n",
2428 cmd->logout_cid, conn->cid);
2429 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2430 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2434 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2440 iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2443 int cmdsn_ret, logout_remove = 0;
2445 struct iscsi_logout *hdr;
2446 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2448 hdr = (struct iscsi_logout *) buf;
2449 reason_code = (hdr->flags & 0x7f);
2452 spin_lock(&tiqn->logout_stats.lock);
2453 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2454 tiqn->logout_stats.normal_logouts++;
2456 tiqn->logout_stats.abnormal_logouts++;
2457 spin_unlock(&tiqn->logout_stats.lock);
2460 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2461 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2462 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2463 hdr->cid, conn->cid);
2465 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2466 pr_err("Received logout request on connection that"
2467 " is not in logged in state, ignoring request.\n");
2468 iscsit_free_cmd(cmd, false);
2472 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2473 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2474 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2475 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2476 cmd->targ_xfer_tag = 0xFFFFFFFF;
2477 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2478 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2479 cmd->logout_cid = be16_to_cpu(hdr->cid);
2480 cmd->logout_reason = reason_code;
2481 cmd->data_direction = DMA_NONE;
2484 * We need to sleep in these cases (by returning 1) until the Logout
2485 * Response gets sent in the tx thread.
2487 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2488 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
2489 be16_to_cpu(hdr->cid) == conn->cid))
2492 spin_lock_bh(&conn->cmd_lock);
2493 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2494 spin_unlock_bh(&conn->cmd_lock);
2496 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
2497 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2500 * Immediate commands are executed, well, immediately.
2501 * Non-Immediate Logout Commands are executed in CmdSN order.
2503 if (cmd->immediate_cmd) {
2504 int ret = iscsit_execute_cmd(cmd, 0);
2509 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
2510 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
2512 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2516 return logout_remove;
2518 EXPORT_SYMBOL(iscsit_handle_logout_cmd);
2520 int iscsit_handle_snack(
2521 struct iscsi_conn *conn,
2524 struct iscsi_snack *hdr;
2526 hdr = (struct iscsi_snack *) buf;
2527 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
2529 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2530 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2531 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2532 hdr->begrun, hdr->runlength, conn->cid);
2534 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2535 pr_err("Initiator sent SNACK request while in"
2536 " ErrorRecoveryLevel=0.\n");
2537 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2541 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2542 * call from inside iscsi_send_recovery_datain_or_r2t().
2544 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2546 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
2548 be32_to_cpu(hdr->ttt),
2549 be32_to_cpu(hdr->begrun),
2550 be32_to_cpu(hdr->runlength));
2551 case ISCSI_FLAG_SNACK_TYPE_STATUS:
2552 return iscsit_handle_status_snack(conn, hdr->itt,
2553 be32_to_cpu(hdr->ttt),
2554 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
2555 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
2556 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2557 be32_to_cpu(hdr->begrun),
2558 be32_to_cpu(hdr->runlength));
2559 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2560 /* FIXME: Support R-Data SNACK */
2561 pr_err("R-Data SNACK Not Supported.\n");
2562 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2565 pr_err("Unknown SNACK type 0x%02x, protocol"
2566 " error.\n", hdr->flags & 0x0f);
2567 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2573 EXPORT_SYMBOL(iscsit_handle_snack);
2575 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2577 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2578 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2579 wait_for_completion_interruptible_timeout(
2580 &conn->rx_half_close_comp,
2581 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2585 static int iscsit_handle_immediate_data(
2586 struct iscsi_cmd *cmd,
2587 struct iscsi_scsi_req *hdr,
2590 int iov_ret, rx_got = 0, rx_size = 0;
2591 u32 checksum, iov_count = 0, padding = 0;
2592 struct iscsi_conn *conn = cmd->conn;
2595 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2597 return IMMEDIATE_DATA_CANNOT_RECOVER;
2600 iov_count = iov_ret;
2601 iov = &cmd->iov_data[0];
2603 padding = ((-length) & 3);
2605 iov[iov_count].iov_base = cmd->pad_bytes;
2606 iov[iov_count++].iov_len = padding;
2610 if (conn->conn_ops->DataDigest) {
2611 iov[iov_count].iov_base = &checksum;
2612 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2613 rx_size += ISCSI_CRC_LEN;
2616 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2618 iscsit_unmap_iovec(cmd);
2620 if (rx_got != rx_size) {
2621 iscsit_rx_thread_wait_for_tcp(conn);
2622 return IMMEDIATE_DATA_CANNOT_RECOVER;
2625 if (conn->conn_ops->DataDigest) {
2628 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
2629 cmd->write_data_done, length, padding,
2632 if (checksum != data_crc) {
2633 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2634 " does not match computed 0x%08x\n", checksum,
2637 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2638 pr_err("Unable to recover from"
2639 " Immediate Data digest failure while"
2641 iscsit_reject_cmd(cmd,
2642 ISCSI_REASON_DATA_DIGEST_ERROR,
2643 (unsigned char *)hdr);
2644 return IMMEDIATE_DATA_CANNOT_RECOVER;
2646 iscsit_reject_cmd(cmd,
2647 ISCSI_REASON_DATA_DIGEST_ERROR,
2648 (unsigned char *)hdr);
2649 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2652 pr_debug("Got CRC32C DataDigest 0x%08x for"
2653 " %u bytes of Immediate Data\n", checksum,
2658 cmd->write_data_done += length;
2660 if (cmd->write_data_done == cmd->se_cmd.data_length) {
2661 spin_lock_bh(&cmd->istate_lock);
2662 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2663 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2664 spin_unlock_bh(&cmd->istate_lock);
2667 return IMMEDIATE_DATA_NORMAL_OPERATION;
2671 * Called with sess->conn_lock held.
2673 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2674 with active network interface */
2675 static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2677 struct iscsi_cmd *cmd;
2678 struct iscsi_conn *conn_p;
2682 * Only send a Asynchronous Message on connections whos network
2683 * interface is still functional.
2685 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2686 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2687 iscsit_inc_conn_usage_count(conn_p);
2696 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
2698 iscsit_dec_conn_usage_count(conn_p);
2702 cmd->logout_cid = conn->cid;
2703 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2704 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2706 spin_lock_bh(&conn_p->cmd_lock);
2707 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
2708 spin_unlock_bh(&conn_p->cmd_lock);
2710 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2711 iscsit_dec_conn_usage_count(conn_p);
2714 static int iscsit_send_conn_drop_async_message(
2715 struct iscsi_cmd *cmd,
2716 struct iscsi_conn *conn)
2718 struct iscsi_async *hdr;
2720 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2722 hdr = (struct iscsi_async *) cmd->pdu;
2723 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2724 hdr->flags = ISCSI_FLAG_CMD_FINAL;
2725 cmd->init_task_tag = RESERVED_ITT;
2726 cmd->targ_xfer_tag = 0xFFFFFFFF;
2727 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2728 cmd->stat_sn = conn->stat_sn++;
2729 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2730 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2731 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2732 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2733 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2734 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2735 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2737 pr_debug("Sending Connection Dropped Async Message StatSN:"
2738 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2739 cmd->logout_cid, conn->cid);
2741 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
2744 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2746 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2747 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2748 wait_for_completion_interruptible_timeout(
2749 &conn->tx_half_close_comp,
2750 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2755 iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2756 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2759 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2760 hdr->flags = datain->flags;
2761 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2762 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2763 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2764 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2765 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2766 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2767 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2770 hton24(hdr->dlength, datain->length);
2771 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2772 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2773 (struct scsi_lun *)&hdr->lun);
2775 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2777 hdr->itt = cmd->init_task_tag;
2779 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2780 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2782 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2784 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2786 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2788 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2789 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2790 hdr->datasn = cpu_to_be32(datain->data_sn);
2791 hdr->offset = cpu_to_be32(datain->offset);
2793 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2794 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2795 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2796 ntohl(hdr->offset), datain->length, conn->cid);
2798 EXPORT_SYMBOL(iscsit_build_datain_pdu);
2800 static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2802 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
2803 struct iscsi_datain datain;
2804 struct iscsi_datain_req *dr;
2806 bool set_statsn = false;
2808 memset(&datain, 0, sizeof(struct iscsi_datain));
2809 dr = iscsit_get_datain_values(cmd, &datain);
2811 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2812 cmd->init_task_tag);
2816 * Be paranoid and double check the logic for now.
2818 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
2819 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2820 " datain.length: %u exceeds cmd->data_length: %u\n",
2821 cmd->init_task_tag, datain.offset, datain.length,
2822 cmd->se_cmd.data_length);
2826 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
2828 * Special case for successfully execution w/ both DATAIN
2831 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2832 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2833 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2835 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2836 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2837 iscsit_increment_maxcmdsn(cmd, conn->sess);
2838 cmd->stat_sn = conn->stat_sn++;
2840 } else if (dr->dr_complete ==
2841 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2845 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
2847 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
2851 if (dr->dr_complete) {
2852 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
2854 iscsit_free_datain_req(cmd, dr);
2861 iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2862 struct iscsi_logout_rsp *hdr)
2864 struct iscsi_conn *logout_conn = NULL;
2865 struct iscsi_conn_recovery *cr = NULL;
2866 struct iscsi_session *sess = conn->sess;
2868 * The actual shutting down of Sessions and/or Connections
2869 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2870 * is done in scsi_logout_post_handler().
2872 switch (cmd->logout_reason) {
2873 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2874 pr_debug("iSCSI session logout successful, setting"
2875 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2876 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2878 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2879 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2882 * For CLOSECONNECTION logout requests carrying
2883 * a matching logout CID -> local CID, the reference
2884 * for the local CID will have been incremented in
2885 * iscsi_logout_closeconnection().
2887 * For CLOSECONNECTION logout requests carrying
2888 * a different CID than the connection it arrived
2889 * on, the connection responding to cmd->logout_cid
2890 * is stopped in iscsit_logout_post_handler_diffcid().
2893 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2894 " successful.\n", cmd->logout_cid, conn->cid);
2895 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2897 case ISCSI_LOGOUT_REASON_RECOVERY:
2898 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2899 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2902 * If the connection is still active from our point of view
2903 * force connection recovery to occur.
2905 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2908 iscsit_connection_reinstatement_rcfr(logout_conn);
2909 iscsit_dec_conn_usage_count(logout_conn);
2912 cr = iscsit_get_inactive_connection_recovery_entry(
2913 conn->sess, cmd->logout_cid);
2915 pr_err("Unable to locate CID: %hu for"
2916 " REMOVECONNFORRECOVERY Logout Request.\n",
2918 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2922 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2924 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2925 " for recovery for CID: %hu on CID: %hu successful.\n",
2926 cmd->logout_cid, conn->cid);
2927 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2930 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2931 cmd->logout_reason);
2935 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2936 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2937 hdr->response = cmd->logout_response;
2938 hdr->itt = cmd->init_task_tag;
2939 cmd->stat_sn = conn->stat_sn++;
2940 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2942 iscsit_increment_maxcmdsn(cmd, conn->sess);
2943 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2944 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2946 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2947 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2948 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2949 cmd->logout_cid, conn->cid);
2953 EXPORT_SYMBOL(iscsit_build_logout_rsp);
2956 iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2960 rc = iscsit_build_logout_rsp(cmd, conn,
2961 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2965 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
2969 iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2970 struct iscsi_nopin *hdr, bool nopout_response)
2972 hdr->opcode = ISCSI_OP_NOOP_IN;
2973 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2974 hton24(hdr->dlength, cmd->buf_ptr_size);
2975 if (nopout_response)
2976 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2977 hdr->itt = cmd->init_task_tag;
2978 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2979 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2981 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2983 if (nopout_response)
2984 iscsit_increment_maxcmdsn(cmd, conn->sess);
2986 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2987 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2989 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2990 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2991 "Solicited" : "Unsolicited", cmd->init_task_tag,
2992 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2994 EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2997 * Unsolicited NOPIN, either requesting a response or not.
2999 static int iscsit_send_unsolicited_nopin(
3000 struct iscsi_cmd *cmd,
3001 struct iscsi_conn *conn,
3004 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
3007 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
3009 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
3010 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
3012 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3016 spin_lock_bh(&cmd->istate_lock);
3017 cmd->i_state = want_response ?
3018 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
3019 spin_unlock_bh(&cmd->istate_lock);
3025 iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3027 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
3029 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
3032 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3033 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3035 pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
3037 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3042 static int iscsit_send_r2t(
3043 struct iscsi_cmd *cmd,
3044 struct iscsi_conn *conn)
3046 struct iscsi_r2t *r2t;
3047 struct iscsi_r2t_rsp *hdr;
3050 r2t = iscsit_get_r2t_from_list(cmd);
3054 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3055 memset(hdr, 0, ISCSI_HDR_LEN);
3056 hdr->opcode = ISCSI_OP_R2T;
3057 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3058 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3059 (struct scsi_lun *)&hdr->lun);
3060 hdr->itt = cmd->init_task_tag;
3061 if (conn->conn_transport->iscsit_get_r2t_ttt)
3062 conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
3064 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
3065 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3066 hdr->statsn = cpu_to_be32(conn->stat_sn);
3067 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3068 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3069 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3070 hdr->data_offset = cpu_to_be32(r2t->offset);
3071 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3073 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3074 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3075 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3076 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3077 r2t->offset, r2t->xfer_len, conn->cid);
3079 spin_lock_bh(&cmd->r2t_lock);
3081 spin_unlock_bh(&cmd->r2t_lock);
3083 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3088 spin_lock_bh(&cmd->dataout_timeout_lock);
3089 iscsit_start_dataout_timer(cmd, conn);
3090 spin_unlock_bh(&cmd->dataout_timeout_lock);
3096 * @recovery: If called from iscsi_task_reassign_complete_write() for
3097 * connection recovery.
3099 int iscsit_build_r2ts_for_cmd(
3100 struct iscsi_conn *conn,
3101 struct iscsi_cmd *cmd,
3105 u32 offset = 0, xfer_len = 0;
3107 spin_lock_bh(&cmd->r2t_lock);
3108 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3109 spin_unlock_bh(&cmd->r2t_lock);
3113 if (conn->sess->sess_ops->DataSequenceInOrder &&
3115 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
3117 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3118 if (conn->sess->sess_ops->DataSequenceInOrder) {
3119 offset = cmd->r2t_offset;
3121 if (first_r2t && recovery) {
3122 int new_data_end = offset +
3123 conn->sess->sess_ops->MaxBurstLength -
3124 cmd->next_burst_len;
3126 if (new_data_end > cmd->se_cmd.data_length)
3127 xfer_len = cmd->se_cmd.data_length - offset;
3130 conn->sess->sess_ops->MaxBurstLength -
3131 cmd->next_burst_len;
3133 int new_data_end = offset +
3134 conn->sess->sess_ops->MaxBurstLength;
3136 if (new_data_end > cmd->se_cmd.data_length)
3137 xfer_len = cmd->se_cmd.data_length - offset;
3139 xfer_len = conn->sess->sess_ops->MaxBurstLength;
3141 cmd->r2t_offset += xfer_len;
3143 if (cmd->r2t_offset == cmd->se_cmd.data_length)
3144 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3146 struct iscsi_seq *seq;
3148 seq = iscsit_get_seq_holder_for_r2t(cmd);
3150 spin_unlock_bh(&cmd->r2t_lock);
3154 offset = seq->offset;
3155 xfer_len = seq->xfer_len;
3157 if (cmd->seq_send_order == cmd->seq_count)
3158 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3160 cmd->outstanding_r2ts++;
3163 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3164 spin_unlock_bh(&cmd->r2t_lock);
3168 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3171 spin_unlock_bh(&cmd->r2t_lock);
3175 EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
3177 void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3178 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
3181 cmd->stat_sn = conn->stat_sn++;
3183 atomic_long_inc(&conn->sess->rsp_pdus);
3185 memset(hdr, 0, ISCSI_HDR_LEN);
3186 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3187 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3188 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3189 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
3190 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3191 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3192 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
3193 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3195 hdr->response = cmd->iscsi_response;
3196 hdr->cmd_status = cmd->se_cmd.scsi_status;
3197 hdr->itt = cmd->init_task_tag;
3198 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3200 iscsit_increment_maxcmdsn(cmd, conn->sess);
3201 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3202 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3204 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3205 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3206 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3207 cmd->se_cmd.scsi_status, conn->cid);
3209 EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3211 static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3213 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3214 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3215 void *data_buf = NULL;
3216 u32 padding = 0, data_buf_len = 0;
3218 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3221 * Attach SENSE DATA payload to iSCSI Response PDU
3223 if (cmd->se_cmd.sense_buffer &&
3224 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3225 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
3226 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3227 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3229 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
3230 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
3231 data_buf = cmd->sense_buffer;
3232 data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
3235 memset(cmd->sense_buffer +
3236 cmd->se_cmd.scsi_sense_length, 0, padding);
3237 pr_debug("Adding %u bytes of padding to"
3238 " SENSE.\n", padding);
3241 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3243 cmd->se_cmd.scsi_sense_length);
3246 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
3250 static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3252 switch (se_tmr->response) {
3253 case TMR_FUNCTION_COMPLETE:
3254 return ISCSI_TMF_RSP_COMPLETE;
3255 case TMR_TASK_DOES_NOT_EXIST:
3256 return ISCSI_TMF_RSP_NO_TASK;
3257 case TMR_LUN_DOES_NOT_EXIST:
3258 return ISCSI_TMF_RSP_NO_LUN;
3259 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3260 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3261 case TMR_FUNCTION_REJECTED:
3263 return ISCSI_TMF_RSP_REJECTED;
3268 iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3269 struct iscsi_tm_rsp *hdr)
3271 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3273 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
3274 hdr->flags = ISCSI_FLAG_CMD_FINAL;
3275 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
3276 hdr->itt = cmd->init_task_tag;
3277 cmd->stat_sn = conn->stat_sn++;
3278 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3280 iscsit_increment_maxcmdsn(cmd, conn->sess);
3281 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3282 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3284 pr_debug("Built Task Management Response ITT: 0x%08x,"
3285 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3286 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3288 EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3291 iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3293 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3295 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3297 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3300 static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3304 if (np->np_sockaddr.ss_family == AF_INET6) {
3305 const struct sockaddr_in6 sin6 = {
3306 .sin6_addr = IN6ADDR_ANY_INIT };
3307 struct sockaddr_in6 *sock_in6 =
3308 (struct sockaddr_in6 *)&np->np_sockaddr;
3310 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3311 sin6.sin6_addr.s6_addr, 16))
3314 struct sockaddr_in * sock_in =
3315 (struct sockaddr_in *)&np->np_sockaddr;
3317 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
3324 #define SENDTARGETS_BUF_LIMIT 32768U
3327 iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
3328 enum iscsit_transport_type network_transport,
3329 int skip_bytes, bool *completed)
3331 char *payload = NULL;
3332 struct iscsi_conn *conn = cmd->conn;
3333 struct iscsi_portal_group *tpg;
3334 struct iscsi_tiqn *tiqn;
3335 struct iscsi_tpg_np *tpg_np;
3336 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
3337 int target_name_printed;
3338 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
3339 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
3342 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
3343 SENDTARGETS_BUF_LIMIT);
3345 payload = kzalloc(buffer_len, GFP_KERNEL);
3347 pr_err("Unable to allocate memory for sendtargets"
3352 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
3355 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
3356 text_ptr = strchr(text_in, '=');
3358 pr_err("Unable to locate '=' string in text_in:"
3364 * Skip over '=' character..
3369 spin_lock(&tiqn_lock);
3370 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3371 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
3372 strcmp(tiqn->tiqn, text_ptr)) {
3376 target_name_printed = 0;
3378 spin_lock(&tiqn->tiqn_tpg_lock);
3379 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3381 /* If demo_mode_discovery=0 and generate_node_acls=0
3382 * (demo mode dislabed) do not return
3383 * TargetName+TargetAddress unless a NodeACL exists.
3386 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3387 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3388 (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
3389 cmd->conn->sess->sess_ops->InitiatorName))) {
3393 spin_lock(&tpg->tpg_state_lock);
3394 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
3395 spin_unlock(&tpg->tpg_state_lock);
3397 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3400 spin_lock(&tpg->tpg_np_lock);
3401 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3403 struct iscsi_np *np = tpg_np->tpg_np;
3404 bool inaddr_any = iscsit_check_inaddr_any(np);
3405 struct sockaddr_storage *sockaddr;
3407 if (np->np_network_transport != network_transport)
3410 if (!target_name_printed) {
3411 len = sprintf(buf, "TargetName=%s",
3415 if ((len + payload_len) > buffer_len) {
3416 spin_unlock(&tpg->tpg_np_lock);
3417 spin_unlock(&tiqn->tiqn_tpg_lock);
3422 if (skip_bytes && len <= skip_bytes) {
3425 memcpy(payload + payload_len, buf, len);
3427 target_name_printed = 1;
3428 if (len > skip_bytes)
3434 sockaddr = &conn->local_sockaddr;
3436 sockaddr = &np->np_sockaddr;
3438 len = sprintf(buf, "TargetAddress="
3444 if ((len + payload_len) > buffer_len) {
3445 spin_unlock(&tpg->tpg_np_lock);
3446 spin_unlock(&tiqn->tiqn_tpg_lock);
3451 if (skip_bytes && len <= skip_bytes) {
3454 memcpy(payload + payload_len, buf, len);
3456 if (len > skip_bytes)
3460 spin_unlock(&tpg->tpg_np_lock);
3462 spin_unlock(&tiqn->tiqn_tpg_lock);
3469 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
3472 spin_unlock(&tiqn_lock);
3474 cmd->buf_ptr = payload;
3480 iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3481 struct iscsi_text_rsp *hdr,
3482 enum iscsit_transport_type network_transport)
3484 int text_length, padding;
3485 bool completed = true;
3487 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3488 cmd->read_data_done,
3490 if (text_length < 0)
3494 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3496 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3497 cmd->read_data_done += text_length;
3498 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3499 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3501 hdr->opcode = ISCSI_OP_TEXT_RSP;
3502 padding = ((-text_length) & 3);
3503 hton24(hdr->dlength, text_length);
3504 hdr->itt = cmd->init_task_tag;
3505 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3506 cmd->stat_sn = conn->stat_sn++;
3507 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3509 iscsit_increment_maxcmdsn(cmd, conn->sess);
3511 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3512 * correctly increment MaxCmdSN for each response answering a
3513 * non immediate text request with a valid CmdSN.
3515 cmd->maxcmdsn_inc = 0;
3516 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3517 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3519 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3520 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3521 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3522 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3523 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
3525 return text_length + padding;
3527 EXPORT_SYMBOL(iscsit_build_text_rsp);
3529 static int iscsit_send_text_rsp(
3530 struct iscsi_cmd *cmd,
3531 struct iscsi_conn *conn)
3533 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
3536 text_length = iscsit_build_text_rsp(cmd, conn, hdr,
3537 conn->conn_transport->transport_type);
3538 if (text_length < 0)
3541 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3547 iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3548 struct iscsi_reject *hdr)
3550 hdr->opcode = ISCSI_OP_REJECT;
3551 hdr->reason = cmd->reject_reason;
3552 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3553 hton24(hdr->dlength, ISCSI_HDR_LEN);
3554 hdr->ffffffff = cpu_to_be32(0xffffffff);
3555 cmd->stat_sn = conn->stat_sn++;
3556 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3557 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3558 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3561 EXPORT_SYMBOL(iscsit_build_reject);
3563 static int iscsit_send_reject(
3564 struct iscsi_cmd *cmd,
3565 struct iscsi_conn *conn)
3567 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
3569 iscsit_build_reject(cmd, conn, hdr);
3571 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3572 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3574 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3579 void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3583 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3584 * within iscsit_start_kthreads()
3586 * Here we use bitmap_id to determine which CPU that this
3587 * iSCSI connection's RX/TX threads will be scheduled to
3590 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
3591 for_each_online_cpu(cpu) {
3593 cpumask_set_cpu(cpu, conn->conn_cpumask);
3598 * This should never be reached..
3601 cpumask_setall(conn->conn_cpumask);
3605 iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3610 case ISTATE_SEND_R2T:
3611 ret = iscsit_send_r2t(cmd, conn);
3616 spin_lock_bh(&conn->cmd_lock);
3617 list_del_init(&cmd->i_conn_node);
3618 spin_unlock_bh(&conn->cmd_lock);
3620 iscsit_free_cmd(cmd, false);
3622 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3623 iscsit_mod_nopin_response_timer(conn);
3624 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3628 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3629 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3634 pr_err("Unknown Opcode: 0x%02x ITT:"
3635 " 0x%08x, i_state: %d on CID: %hu\n",
3636 cmd->iscsi_opcode, cmd->init_task_tag, state,
3646 EXPORT_SYMBOL(iscsit_immediate_queue);
3649 iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3651 struct iscsit_transport *t = conn->conn_transport;
3652 struct iscsi_queue_req *qr;
3653 struct iscsi_cmd *cmd;
3657 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3658 atomic_set(&conn->check_immediate_queue, 0);
3661 kmem_cache_free(lio_qr_cache, qr);
3663 ret = t->iscsit_immediate_queue(conn, cmd, state);
3672 iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3678 case ISTATE_SEND_DATAIN:
3679 ret = iscsit_send_datain(cmd, conn);
3684 goto check_rsp_state;
3685 else if (ret == 1) {
3687 spin_lock_bh(&cmd->istate_lock);
3688 cmd->i_state = ISTATE_SENT_STATUS;
3689 spin_unlock_bh(&cmd->istate_lock);
3691 if (atomic_read(&conn->check_immediate_queue))
3695 } else if (ret == 2) {
3696 /* Still must send status,
3697 SCF_TRANSPORT_TASK_SENSE was set */
3698 spin_lock_bh(&cmd->istate_lock);
3699 cmd->i_state = ISTATE_SEND_STATUS;
3700 spin_unlock_bh(&cmd->istate_lock);
3701 state = ISTATE_SEND_STATUS;
3702 goto check_rsp_state;
3706 case ISTATE_SEND_STATUS:
3707 case ISTATE_SEND_STATUS_RECOVERY:
3708 ret = iscsit_send_response(cmd, conn);
3710 case ISTATE_SEND_LOGOUTRSP:
3711 ret = iscsit_send_logout(cmd, conn);
3713 case ISTATE_SEND_ASYNCMSG:
3714 ret = iscsit_send_conn_drop_async_message(
3717 case ISTATE_SEND_NOPIN:
3718 ret = iscsit_send_nopin(cmd, conn);
3720 case ISTATE_SEND_REJECT:
3721 ret = iscsit_send_reject(cmd, conn);
3723 case ISTATE_SEND_TASKMGTRSP:
3724 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3727 ret = iscsit_tmr_post_handler(cmd, conn);
3729 iscsit_fall_back_to_erl0(conn->sess);
3731 case ISTATE_SEND_TEXTRSP:
3732 ret = iscsit_send_text_rsp(cmd, conn);
3735 pr_err("Unknown Opcode: 0x%02x ITT:"
3736 " 0x%08x, i_state: %d on CID: %hu\n",
3737 cmd->iscsi_opcode, cmd->init_task_tag,
3745 case ISTATE_SEND_LOGOUTRSP:
3746 if (!iscsit_logout_post_handler(cmd, conn))
3749 case ISTATE_SEND_STATUS:
3750 case ISTATE_SEND_ASYNCMSG:
3751 case ISTATE_SEND_NOPIN:
3752 case ISTATE_SEND_STATUS_RECOVERY:
3753 case ISTATE_SEND_TEXTRSP:
3754 case ISTATE_SEND_TASKMGTRSP:
3755 case ISTATE_SEND_REJECT:
3756 spin_lock_bh(&cmd->istate_lock);
3757 cmd->i_state = ISTATE_SENT_STATUS;
3758 spin_unlock_bh(&cmd->istate_lock);
3761 pr_err("Unknown Opcode: 0x%02x ITT:"
3762 " 0x%08x, i_state: %d on CID: %hu\n",
3763 cmd->iscsi_opcode, cmd->init_task_tag,
3764 cmd->i_state, conn->cid);
3768 if (atomic_read(&conn->check_immediate_queue))
3776 EXPORT_SYMBOL(iscsit_response_queue);
3778 static int iscsit_handle_response_queue(struct iscsi_conn *conn)
3780 struct iscsit_transport *t = conn->conn_transport;
3781 struct iscsi_queue_req *qr;
3782 struct iscsi_cmd *cmd;
3786 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3789 kmem_cache_free(lio_qr_cache, qr);
3791 ret = t->iscsit_response_queue(conn, cmd, state);
3792 if (ret == 1 || ret < 0)
3799 int iscsi_target_tx_thread(void *arg)
3802 struct iscsi_conn *conn = arg;
3804 * Allow ourselves to be interrupted by SIGINT so that a
3805 * connection recovery / failure event can be triggered externally.
3807 allow_signal(SIGINT);
3809 while (!kthread_should_stop()) {
3811 * Ensure that both TX and RX per connection kthreads
3812 * are scheduled to run on the same CPU.
3814 iscsit_thread_check_cpumask(conn, current, 1);
3816 wait_event_interruptible(conn->queues_wq,
3817 !iscsit_conn_all_queues_empty(conn));
3819 if (signal_pending(current))
3823 ret = iscsit_handle_immediate_queue(conn);
3827 ret = iscsit_handle_response_queue(conn);
3830 else if (ret == -ECONNRESET)
3838 * Avoid the normal connection failure code-path if this connection
3839 * is still within LOGIN mode, and iscsi_np process context is
3840 * responsible for cleaning up the early connection failure.
3842 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
3843 iscsit_take_action_for_connection_exit(conn);
3848 static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3850 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3851 struct iscsi_cmd *cmd;
3854 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3855 case ISCSI_OP_SCSI_CMD:
3856 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3860 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3862 case ISCSI_OP_SCSI_DATA_OUT:
3863 ret = iscsit_handle_data_out(conn, buf);
3865 case ISCSI_OP_NOOP_OUT:
3867 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
3868 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3872 ret = iscsit_handle_nop_out(conn, cmd, buf);
3874 case ISCSI_OP_SCSI_TMFUNC:
3875 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3879 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3882 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
3883 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
3887 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3892 ret = iscsit_handle_text_cmd(conn, cmd, buf);
3894 case ISCSI_OP_LOGOUT:
3895 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3899 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3901 wait_for_completion_timeout(&conn->conn_logout_comp,
3902 SECONDS_FOR_LOGOUT_COMP * HZ);
3904 case ISCSI_OP_SNACK:
3905 ret = iscsit_handle_snack(conn, buf);
3908 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3909 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3910 pr_err("Cannot recover from unknown"
3911 " opcode while ERL=0, closing iSCSI connection.\n");
3914 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3915 " closing iSCSI connection.\n");
3922 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
3925 static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
3929 spin_lock_bh(&conn->state_lock);
3930 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
3931 spin_unlock_bh(&conn->state_lock);
3936 static void iscsit_get_rx_pdu(struct iscsi_conn *conn)
3939 u8 buffer[ISCSI_HDR_LEN], opcode;
3940 u32 checksum = 0, digest = 0;
3943 while (!kthread_should_stop()) {
3945 * Ensure that both TX and RX per connection kthreads
3946 * are scheduled to run on the same CPU.
3948 iscsit_thread_check_cpumask(conn, current, 0);
3950 memset(buffer, 0, ISCSI_HDR_LEN);
3951 memset(&iov, 0, sizeof(struct kvec));
3953 iov.iov_base = buffer;
3954 iov.iov_len = ISCSI_HDR_LEN;
3956 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3957 if (ret != ISCSI_HDR_LEN) {
3958 iscsit_rx_thread_wait_for_tcp(conn);
3962 if (conn->conn_ops->HeaderDigest) {
3963 iov.iov_base = &digest;
3964 iov.iov_len = ISCSI_CRC_LEN;
3966 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3967 if (ret != ISCSI_CRC_LEN) {
3968 iscsit_rx_thread_wait_for_tcp(conn);
3972 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
3973 buffer, ISCSI_HDR_LEN,
3974 0, NULL, (u8 *)&checksum);
3976 if (digest != checksum) {
3977 pr_err("HeaderDigest CRC32C failed,"
3978 " received 0x%08x, computed 0x%08x\n",
3981 * Set the PDU to 0xff so it will intentionally
3982 * hit default in the switch below.
3984 memset(buffer, 0xff, ISCSI_HDR_LEN);
3985 atomic_long_inc(&conn->sess->conn_digest_errors);
3987 pr_debug("Got HeaderDigest CRC32C"
3988 " 0x%08x\n", checksum);
3992 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
3995 opcode = buffer[0] & ISCSI_OPCODE_MASK;
3997 if (conn->sess->sess_ops->SessionType &&
3998 ((!(opcode & ISCSI_OP_TEXT)) ||
3999 (!(opcode & ISCSI_OP_LOGOUT)))) {
4000 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4001 " while in Discovery Session, rejecting.\n", opcode);
4002 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4007 ret = iscsi_target_rx_opcode(conn, buffer);
4013 int iscsi_target_rx_thread(void *arg)
4016 struct iscsi_conn *conn = arg;
4019 * Allow ourselves to be interrupted by SIGINT so that a
4020 * connection recovery / failure event can be triggered externally.
4022 allow_signal(SIGINT);
4024 * Wait for iscsi_post_login_handler() to complete before allowing
4025 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4027 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4028 if (rc < 0 || iscsi_target_check_conn_state(conn))
4031 if (!conn->conn_transport->iscsit_get_rx_pdu)
4034 conn->conn_transport->iscsit_get_rx_pdu(conn);
4036 if (!signal_pending(current))
4037 atomic_set(&conn->transport_failed, 1);
4038 iscsit_take_action_for_connection_exit(conn);
4042 static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4044 LIST_HEAD(tmp_list);
4045 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4046 struct iscsi_session *sess = conn->sess;
4048 * We expect this function to only ever be called from either RX or TX
4049 * thread context via iscsit_close_connection() once the other context
4050 * has been reset -> returned sleeping pre-handler state.
4052 spin_lock_bh(&conn->cmd_lock);
4053 list_splice_init(&conn->conn_cmd_list, &tmp_list);
4055 list_for_each_entry(cmd, &tmp_list, i_conn_node) {
4056 struct se_cmd *se_cmd = &cmd->se_cmd;
4058 if (se_cmd->se_tfo != NULL) {
4059 spin_lock(&se_cmd->t_state_lock);
4060 se_cmd->transport_state |= CMD_T_FABRIC_STOP;
4061 spin_unlock(&se_cmd->t_state_lock);
4064 spin_unlock_bh(&conn->cmd_lock);
4066 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4067 list_del_init(&cmd->i_conn_node);
4069 iscsit_increment_maxcmdsn(cmd, sess);
4070 iscsit_free_cmd(cmd, true);
4075 static void iscsit_stop_timers_for_cmds(
4076 struct iscsi_conn *conn)
4078 struct iscsi_cmd *cmd;
4080 spin_lock_bh(&conn->cmd_lock);
4081 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
4082 if (cmd->data_direction == DMA_TO_DEVICE)
4083 iscsit_stop_dataout_timer(cmd);
4085 spin_unlock_bh(&conn->cmd_lock);
4088 int iscsit_close_connection(
4089 struct iscsi_conn *conn)
4091 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4092 struct iscsi_session *sess = conn->sess;
4094 pr_debug("Closing iSCSI connection CID %hu on SID:"
4095 " %u\n", conn->cid, sess->sid);
4097 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4098 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4099 * sleeping and the logout response never got sent because the
4100 * connection failed.
4102 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4103 * to signal logout response TX interrupt completion. Go ahead and skip
4104 * this for iser since isert_rx_opcode() does not wait on logout failure,
4105 * and to avoid iscsi_conn pointer dereference in iser-target code.
4107 if (!conn->conn_transport->rdma_shutdown)
4108 complete(&conn->conn_logout_comp);
4110 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4111 if (conn->tx_thread &&
4112 cmpxchg(&conn->tx_thread_active, true, false)) {
4113 send_sig(SIGINT, conn->tx_thread, 1);
4114 kthread_stop(conn->tx_thread);
4116 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4117 if (conn->rx_thread &&
4118 cmpxchg(&conn->rx_thread_active, true, false)) {
4119 send_sig(SIGINT, conn->rx_thread, 1);
4120 kthread_stop(conn->rx_thread);
4124 spin_lock(&iscsit_global->ts_bitmap_lock);
4125 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4127 spin_unlock(&iscsit_global->ts_bitmap_lock);
4129 iscsit_stop_timers_for_cmds(conn);
4130 iscsit_stop_nopin_response_timer(conn);
4131 iscsit_stop_nopin_timer(conn);
4133 if (conn->conn_transport->iscsit_wait_conn)
4134 conn->conn_transport->iscsit_wait_conn(conn);
4137 * During Connection recovery drop unacknowledged out of order
4138 * commands for this connection, and prepare the other commands
4141 * During normal operation clear the out of order commands (but
4142 * do not free the struct iscsi_ooo_cmdsn's) and release all
4143 * struct iscsi_cmds.
4145 if (atomic_read(&conn->connection_recovery)) {
4146 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4147 iscsit_prepare_cmds_for_realligance(conn);
4149 iscsit_clear_ooo_cmdsns_for_conn(conn);
4150 iscsit_release_commands_from_conn(conn);
4152 iscsit_free_queue_reqs_for_conn(conn);
4155 * Handle decrementing session or connection usage count if
4156 * a logout response was not able to be sent because the
4157 * connection failed. Fall back to Session Recovery here.
4159 if (atomic_read(&conn->conn_logout_remove)) {
4160 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4161 iscsit_dec_conn_usage_count(conn);
4162 iscsit_dec_session_usage_count(sess);
4164 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4165 iscsit_dec_conn_usage_count(conn);
4167 atomic_set(&conn->conn_logout_remove, 0);
4168 atomic_set(&sess->session_reinstatement, 0);
4169 atomic_set(&sess->session_fall_back_to_erl0, 1);
4172 spin_lock_bh(&sess->conn_lock);
4173 list_del(&conn->conn_list);
4176 * Attempt to let the Initiator know this connection failed by
4177 * sending an Connection Dropped Async Message on another
4178 * active connection.
4180 if (atomic_read(&conn->connection_recovery))
4181 iscsit_build_conn_drop_async_message(conn);
4183 spin_unlock_bh(&sess->conn_lock);
4186 * If connection reinstatement is being performed on this connection,
4187 * up the connection reinstatement semaphore that is being blocked on
4188 * in iscsit_cause_connection_reinstatement().
4190 spin_lock_bh(&conn->state_lock);
4191 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4192 spin_unlock_bh(&conn->state_lock);
4193 complete(&conn->conn_wait_comp);
4194 wait_for_completion(&conn->conn_post_wait_comp);
4195 spin_lock_bh(&conn->state_lock);
4199 * If connection reinstatement is being performed on this connection
4200 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4201 * connection wait rcfr semaphore that is being blocked on
4202 * an iscsit_connection_reinstatement_rcfr().
4204 if (atomic_read(&conn->connection_wait_rcfr)) {
4205 spin_unlock_bh(&conn->state_lock);
4206 complete(&conn->conn_wait_rcfr_comp);
4207 wait_for_completion(&conn->conn_post_wait_comp);
4208 spin_lock_bh(&conn->state_lock);
4210 atomic_set(&conn->connection_reinstatement, 1);
4211 spin_unlock_bh(&conn->state_lock);
4214 * If any other processes are accessing this connection pointer we
4215 * must wait until they have completed.
4217 iscsit_check_conn_usage_count(conn);
4219 ahash_request_free(conn->conn_tx_hash);
4220 if (conn->conn_rx_hash) {
4221 struct crypto_ahash *tfm;
4223 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
4224 ahash_request_free(conn->conn_rx_hash);
4225 crypto_free_ahash(tfm);
4228 free_cpumask_var(conn->conn_cpumask);
4230 kfree(conn->conn_ops);
4231 conn->conn_ops = NULL;
4234 sock_release(conn->sock);
4236 if (conn->conn_transport->iscsit_free_conn)
4237 conn->conn_transport->iscsit_free_conn(conn);
4239 iscsit_put_transport(conn->conn_transport);
4241 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4242 conn->conn_state = TARG_CONN_STATE_FREE;
4245 spin_lock_bh(&sess->conn_lock);
4246 atomic_dec(&sess->nconn);
4247 pr_debug("Decremented iSCSI connection count to %hu from node:"
4248 " %s\n", atomic_read(&sess->nconn),
4249 sess->sess_ops->InitiatorName);
4251 * Make sure that if one connection fails in an non ERL=2 iSCSI
4252 * Session that they all fail.
4254 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4255 !atomic_read(&sess->session_logout))
4256 atomic_set(&sess->session_fall_back_to_erl0, 1);
4259 * If this was not the last connection in the session, and we are
4260 * performing session reinstatement or falling back to ERL=0, call
4261 * iscsit_stop_session() without sleeping to shutdown the other
4262 * active connections.
4264 if (atomic_read(&sess->nconn)) {
4265 if (!atomic_read(&sess->session_reinstatement) &&
4266 !atomic_read(&sess->session_fall_back_to_erl0)) {
4267 spin_unlock_bh(&sess->conn_lock);
4270 if (!atomic_read(&sess->session_stop_active)) {
4271 atomic_set(&sess->session_stop_active, 1);
4272 spin_unlock_bh(&sess->conn_lock);
4273 iscsit_stop_session(sess, 0, 0);
4276 spin_unlock_bh(&sess->conn_lock);
4281 * If this was the last connection in the session and one of the
4282 * following is occurring:
4284 * Session Reinstatement is not being performed, and are falling back
4285 * to ERL=0 call iscsit_close_session().
4287 * Session Logout was requested. iscsit_close_session() will be called
4290 * Session Continuation is not being performed, start the Time2Retain
4291 * handler and check if sleep_on_sess_wait_sem is active.
4293 if (!atomic_read(&sess->session_reinstatement) &&
4294 atomic_read(&sess->session_fall_back_to_erl0)) {
4295 spin_unlock_bh(&sess->conn_lock);
4296 iscsit_close_session(sess);
4299 } else if (atomic_read(&sess->session_logout)) {
4300 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4301 sess->session_state = TARG_SESS_STATE_FREE;
4302 spin_unlock_bh(&sess->conn_lock);
4304 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4305 complete(&sess->session_wait_comp);
4309 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4310 sess->session_state = TARG_SESS_STATE_FAILED;
4312 if (!atomic_read(&sess->session_continuation)) {
4313 spin_unlock_bh(&sess->conn_lock);
4314 iscsit_start_time2retain_handler(sess);
4316 spin_unlock_bh(&sess->conn_lock);
4318 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4319 complete(&sess->session_wait_comp);
4326 * If the iSCSI Session for the iSCSI Initiator Node exists,
4327 * forcefully shutdown the iSCSI NEXUS.
4329 int iscsit_close_session(struct iscsi_session *sess)
4331 struct iscsi_portal_group *tpg = sess->tpg;
4332 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4334 if (atomic_read(&sess->nconn)) {
4335 pr_err("%d connection(s) still exist for iSCSI session"
4336 " to %s\n", atomic_read(&sess->nconn),
4337 sess->sess_ops->InitiatorName);
4341 spin_lock_bh(&se_tpg->session_lock);
4342 atomic_set(&sess->session_logout, 1);
4343 atomic_set(&sess->session_reinstatement, 1);
4344 iscsit_stop_time2retain_timer(sess);
4345 spin_unlock_bh(&se_tpg->session_lock);
4348 * transport_deregister_session_configfs() will clear the
4349 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4350 * can be setting it again with __transport_register_session() in
4351 * iscsi_post_login_handler() again after the iscsit_stop_session()
4352 * completes in iscsi_np context.
4354 transport_deregister_session_configfs(sess->se_sess);
4357 * If any other processes are accessing this session pointer we must
4358 * wait until they have completed. If we are in an interrupt (the
4359 * time2retain handler) and contain and active session usage count we
4360 * restart the timer and exit.
4362 if (!in_interrupt()) {
4363 if (iscsit_check_session_usage_count(sess) == 1)
4364 iscsit_stop_session(sess, 1, 1);
4366 if (iscsit_check_session_usage_count(sess) == 2) {
4367 atomic_set(&sess->session_logout, 0);
4368 iscsit_start_time2retain_handler(sess);
4373 transport_deregister_session(sess->se_sess);
4375 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4376 iscsit_free_connection_recovery_entires(sess);
4378 iscsit_free_all_ooo_cmdsns(sess);
4380 spin_lock_bh(&se_tpg->session_lock);
4381 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4382 sess->session_state = TARG_SESS_STATE_FREE;
4383 pr_debug("Released iSCSI session from node: %s\n",
4384 sess->sess_ops->InitiatorName);
4387 tpg->tpg_tiqn->tiqn_nsessions--;
4389 pr_debug("Decremented number of active iSCSI Sessions on"
4390 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4392 spin_lock(&sess_idr_lock);
4393 idr_remove(&sess_idr, sess->session_index);
4394 spin_unlock(&sess_idr_lock);
4396 kfree(sess->sess_ops);
4397 sess->sess_ops = NULL;
4398 spin_unlock_bh(&se_tpg->session_lock);
4404 static void iscsit_logout_post_handler_closesession(
4405 struct iscsi_conn *conn)
4407 struct iscsi_session *sess = conn->sess;
4410 * Traditional iscsi/tcp will invoke this logic from TX thread
4411 * context during session logout, so clear tx_thread_active and
4412 * sleep if iscsit_close_connection() has not already occured.
4414 * Since iser-target invokes this logic from it's own workqueue,
4415 * always sleep waiting for RX/TX thread shutdown to complete
4416 * within iscsit_close_connection().
4418 if (!conn->conn_transport->rdma_shutdown)
4419 sleep = cmpxchg(&conn->tx_thread_active, true, false);
4421 atomic_set(&conn->conn_logout_remove, 0);
4422 complete(&conn->conn_logout_comp);
4424 iscsit_dec_conn_usage_count(conn);
4425 iscsit_stop_session(sess, sleep, sleep);
4426 iscsit_dec_session_usage_count(sess);
4427 iscsit_close_session(sess);
4430 static void iscsit_logout_post_handler_samecid(
4431 struct iscsi_conn *conn)
4435 if (!conn->conn_transport->rdma_shutdown)
4436 sleep = cmpxchg(&conn->tx_thread_active, true, false);
4438 atomic_set(&conn->conn_logout_remove, 0);
4439 complete(&conn->conn_logout_comp);
4441 iscsit_cause_connection_reinstatement(conn, sleep);
4442 iscsit_dec_conn_usage_count(conn);
4445 static void iscsit_logout_post_handler_diffcid(
4446 struct iscsi_conn *conn,
4449 struct iscsi_conn *l_conn;
4450 struct iscsi_session *sess = conn->sess;
4451 bool conn_found = false;
4456 spin_lock_bh(&sess->conn_lock);
4457 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4458 if (l_conn->cid == cid) {
4459 iscsit_inc_conn_usage_count(l_conn);
4464 spin_unlock_bh(&sess->conn_lock);
4470 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4472 spin_lock_bh(&l_conn->state_lock);
4473 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4474 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4475 spin_unlock_bh(&l_conn->state_lock);
4477 iscsit_cause_connection_reinstatement(l_conn, 1);
4478 iscsit_dec_conn_usage_count(l_conn);
4482 * Return of 0 causes the TX thread to restart.
4484 int iscsit_logout_post_handler(
4485 struct iscsi_cmd *cmd,
4486 struct iscsi_conn *conn)
4490 switch (cmd->logout_reason) {
4491 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4492 switch (cmd->logout_response) {
4493 case ISCSI_LOGOUT_SUCCESS:
4494 case ISCSI_LOGOUT_CLEANUP_FAILED:
4496 iscsit_logout_post_handler_closesession(conn);
4501 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4502 if (conn->cid == cmd->logout_cid) {
4503 switch (cmd->logout_response) {
4504 case ISCSI_LOGOUT_SUCCESS:
4505 case ISCSI_LOGOUT_CLEANUP_FAILED:
4507 iscsit_logout_post_handler_samecid(conn);
4512 switch (cmd->logout_response) {
4513 case ISCSI_LOGOUT_SUCCESS:
4514 iscsit_logout_post_handler_diffcid(conn,
4517 case ISCSI_LOGOUT_CID_NOT_FOUND:
4518 case ISCSI_LOGOUT_CLEANUP_FAILED:
4525 case ISCSI_LOGOUT_REASON_RECOVERY:
4526 switch (cmd->logout_response) {
4527 case ISCSI_LOGOUT_SUCCESS:
4528 case ISCSI_LOGOUT_CID_NOT_FOUND:
4529 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4530 case ISCSI_LOGOUT_CLEANUP_FAILED:
4542 EXPORT_SYMBOL(iscsit_logout_post_handler);
4544 void iscsit_fail_session(struct iscsi_session *sess)
4546 struct iscsi_conn *conn;
4548 spin_lock_bh(&sess->conn_lock);
4549 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4550 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4551 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4553 spin_unlock_bh(&sess->conn_lock);
4555 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4556 sess->session_state = TARG_SESS_STATE_FAILED;
4559 int iscsit_free_session(struct iscsi_session *sess)
4561 u16 conn_count = atomic_read(&sess->nconn);
4562 struct iscsi_conn *conn, *conn_tmp = NULL;
4565 spin_lock_bh(&sess->conn_lock);
4566 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4568 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4570 if (conn_count == 0)
4573 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4576 iscsit_inc_conn_usage_count(conn_tmp);
4579 iscsit_inc_conn_usage_count(conn);
4581 spin_unlock_bh(&sess->conn_lock);
4582 iscsit_cause_connection_reinstatement(conn, 1);
4583 spin_lock_bh(&sess->conn_lock);
4585 iscsit_dec_conn_usage_count(conn);
4587 iscsit_dec_conn_usage_count(conn_tmp);
4592 if (atomic_read(&sess->nconn)) {
4593 spin_unlock_bh(&sess->conn_lock);
4594 wait_for_completion(&sess->session_wait_comp);
4596 spin_unlock_bh(&sess->conn_lock);
4598 iscsit_close_session(sess);
4602 void iscsit_stop_session(
4603 struct iscsi_session *sess,
4605 int connection_sleep)
4607 u16 conn_count = atomic_read(&sess->nconn);
4608 struct iscsi_conn *conn, *conn_tmp = NULL;
4611 spin_lock_bh(&sess->conn_lock);
4613 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4615 if (connection_sleep) {
4616 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4618 if (conn_count == 0)
4621 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4624 iscsit_inc_conn_usage_count(conn_tmp);
4627 iscsit_inc_conn_usage_count(conn);
4629 spin_unlock_bh(&sess->conn_lock);
4630 iscsit_cause_connection_reinstatement(conn, 1);
4631 spin_lock_bh(&sess->conn_lock);
4633 iscsit_dec_conn_usage_count(conn);
4635 iscsit_dec_conn_usage_count(conn_tmp);
4639 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4640 iscsit_cause_connection_reinstatement(conn, 0);
4643 if (session_sleep && atomic_read(&sess->nconn)) {
4644 spin_unlock_bh(&sess->conn_lock);
4645 wait_for_completion(&sess->session_wait_comp);
4647 spin_unlock_bh(&sess->conn_lock);
4650 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4652 struct iscsi_session *sess;
4653 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4654 struct se_session *se_sess, *se_sess_tmp;
4655 LIST_HEAD(free_list);
4656 int session_count = 0;
4658 spin_lock_bh(&se_tpg->session_lock);
4659 if (tpg->nsessions && !force) {
4660 spin_unlock_bh(&se_tpg->session_lock);
4664 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4666 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4668 spin_lock(&sess->conn_lock);
4669 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4670 atomic_read(&sess->session_logout) ||
4671 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4672 spin_unlock(&sess->conn_lock);
4675 atomic_set(&sess->session_reinstatement, 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 iscsit_free_session(sess);
4689 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4690 " Group: %hu\n", session_count, tpg->tpgt);
4694 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4695 MODULE_VERSION("4.1.x");
4696 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4697 MODULE_LICENSE("GPL");
4699 module_init(iscsi_target_init_module);
4700 module_exit(iscsi_target_cleanup_module);