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 <linux/sched/signal.h>
28 #include <asm/unaligned.h>
29 #include <linux/inet.h>
31 #include <scsi/scsi_proto.h>
32 #include <scsi/iscsi_proto.h>
33 #include <scsi/scsi_tcq.h>
34 #include <target/target_core_base.h>
35 #include <target/target_core_fabric.h>
37 #include <target/iscsi/iscsi_target_core.h>
38 #include "iscsi_target_parameters.h"
39 #include "iscsi_target_seq_pdu_list.h"
40 #include "iscsi_target_datain_values.h"
41 #include "iscsi_target_erl0.h"
42 #include "iscsi_target_erl1.h"
43 #include "iscsi_target_erl2.h"
44 #include "iscsi_target_login.h"
45 #include "iscsi_target_tmr.h"
46 #include "iscsi_target_tpg.h"
47 #include "iscsi_target_util.h"
48 #include "iscsi_target.h"
49 #include "iscsi_target_device.h"
50 #include <target/iscsi/iscsi_target_stat.h>
52 #include <target/iscsi/iscsi_transport.h>
54 static LIST_HEAD(g_tiqn_list
);
55 static LIST_HEAD(g_np_list
);
56 static DEFINE_SPINLOCK(tiqn_lock
);
57 static DEFINE_MUTEX(np_lock
);
59 static struct idr tiqn_idr
;
61 struct mutex auth_id_lock
;
63 struct iscsit_global
*iscsit_global
;
65 struct kmem_cache
*lio_qr_cache
;
66 struct kmem_cache
*lio_dr_cache
;
67 struct kmem_cache
*lio_ooo_cache
;
68 struct kmem_cache
*lio_r2t_cache
;
70 static int iscsit_handle_immediate_data(struct iscsi_cmd
*,
71 struct iscsi_scsi_req
*, u32
);
73 struct iscsi_tiqn
*iscsit_get_tiqn_for_login(unsigned char *buf
)
75 struct iscsi_tiqn
*tiqn
= NULL
;
77 spin_lock(&tiqn_lock
);
78 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
79 if (!strcmp(tiqn
->tiqn
, buf
)) {
81 spin_lock(&tiqn
->tiqn_state_lock
);
82 if (tiqn
->tiqn_state
== TIQN_STATE_ACTIVE
) {
83 tiqn
->tiqn_access_count
++;
84 spin_unlock(&tiqn
->tiqn_state_lock
);
85 spin_unlock(&tiqn_lock
);
88 spin_unlock(&tiqn
->tiqn_state_lock
);
91 spin_unlock(&tiqn_lock
);
96 static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn
*tiqn
)
98 spin_lock(&tiqn
->tiqn_state_lock
);
99 if (tiqn
->tiqn_state
== TIQN_STATE_ACTIVE
) {
100 tiqn
->tiqn_state
= TIQN_STATE_SHUTDOWN
;
101 spin_unlock(&tiqn
->tiqn_state_lock
);
104 spin_unlock(&tiqn
->tiqn_state_lock
);
109 void iscsit_put_tiqn_for_login(struct iscsi_tiqn
*tiqn
)
111 spin_lock(&tiqn
->tiqn_state_lock
);
112 tiqn
->tiqn_access_count
--;
113 spin_unlock(&tiqn
->tiqn_state_lock
);
117 * Note that IQN formatting is expected to be done in userspace, and
118 * no explict IQN format checks are done here.
120 struct iscsi_tiqn
*iscsit_add_tiqn(unsigned char *buf
)
122 struct iscsi_tiqn
*tiqn
= NULL
;
125 if (strlen(buf
) >= ISCSI_IQN_LEN
) {
126 pr_err("Target IQN exceeds %d bytes\n",
128 return ERR_PTR(-EINVAL
);
131 tiqn
= kzalloc(sizeof(*tiqn
), GFP_KERNEL
);
133 return ERR_PTR(-ENOMEM
);
135 sprintf(tiqn
->tiqn
, "%s", buf
);
136 INIT_LIST_HEAD(&tiqn
->tiqn_list
);
137 INIT_LIST_HEAD(&tiqn
->tiqn_tpg_list
);
138 spin_lock_init(&tiqn
->tiqn_state_lock
);
139 spin_lock_init(&tiqn
->tiqn_tpg_lock
);
140 spin_lock_init(&tiqn
->sess_err_stats
.lock
);
141 spin_lock_init(&tiqn
->login_stats
.lock
);
142 spin_lock_init(&tiqn
->logout_stats
.lock
);
144 tiqn
->tiqn_state
= TIQN_STATE_ACTIVE
;
146 idr_preload(GFP_KERNEL
);
147 spin_lock(&tiqn_lock
);
149 ret
= idr_alloc(&tiqn_idr
, NULL
, 0, 0, GFP_NOWAIT
);
151 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
152 spin_unlock(&tiqn_lock
);
157 tiqn
->tiqn_index
= ret
;
158 list_add_tail(&tiqn
->tiqn_list
, &g_tiqn_list
);
160 spin_unlock(&tiqn_lock
);
163 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn
->tiqn
);
169 static void iscsit_wait_for_tiqn(struct iscsi_tiqn
*tiqn
)
172 * Wait for accesses to said struct iscsi_tiqn to end.
174 spin_lock(&tiqn
->tiqn_state_lock
);
175 while (tiqn
->tiqn_access_count
!= 0) {
176 spin_unlock(&tiqn
->tiqn_state_lock
);
178 spin_lock(&tiqn
->tiqn_state_lock
);
180 spin_unlock(&tiqn
->tiqn_state_lock
);
183 void iscsit_del_tiqn(struct iscsi_tiqn
*tiqn
)
186 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
187 * while holding tiqn->tiqn_state_lock. This means that all subsequent
188 * attempts to access this struct iscsi_tiqn will fail from both transport
189 * fabric and control code paths.
191 if (iscsit_set_tiqn_shutdown(tiqn
) < 0) {
192 pr_err("iscsit_set_tiqn_shutdown() failed\n");
196 iscsit_wait_for_tiqn(tiqn
);
198 spin_lock(&tiqn_lock
);
199 list_del(&tiqn
->tiqn_list
);
200 idr_remove(&tiqn_idr
, tiqn
->tiqn_index
);
201 spin_unlock(&tiqn_lock
);
203 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
208 int iscsit_access_np(struct iscsi_np
*np
, struct iscsi_portal_group
*tpg
)
212 * Determine if the network portal is accepting storage traffic.
214 spin_lock_bh(&np
->np_thread_lock
);
215 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
216 spin_unlock_bh(&np
->np_thread_lock
);
219 spin_unlock_bh(&np
->np_thread_lock
);
221 * Determine if the portal group is accepting storage traffic.
223 spin_lock_bh(&tpg
->tpg_state_lock
);
224 if (tpg
->tpg_state
!= TPG_STATE_ACTIVE
) {
225 spin_unlock_bh(&tpg
->tpg_state_lock
);
228 spin_unlock_bh(&tpg
->tpg_state_lock
);
231 * Here we serialize access across the TIQN+TPG Tuple.
233 ret
= down_interruptible(&tpg
->np_login_sem
);
237 spin_lock_bh(&tpg
->tpg_state_lock
);
238 if (tpg
->tpg_state
!= TPG_STATE_ACTIVE
) {
239 spin_unlock_bh(&tpg
->tpg_state_lock
);
240 up(&tpg
->np_login_sem
);
243 spin_unlock_bh(&tpg
->tpg_state_lock
);
248 void iscsit_login_kref_put(struct kref
*kref
)
250 struct iscsi_tpg_np
*tpg_np
= container_of(kref
,
251 struct iscsi_tpg_np
, tpg_np_kref
);
253 complete(&tpg_np
->tpg_np_comp
);
256 int iscsit_deaccess_np(struct iscsi_np
*np
, struct iscsi_portal_group
*tpg
,
257 struct iscsi_tpg_np
*tpg_np
)
259 struct iscsi_tiqn
*tiqn
= tpg
->tpg_tiqn
;
261 up(&tpg
->np_login_sem
);
264 kref_put(&tpg_np
->tpg_np_kref
, iscsit_login_kref_put
);
267 iscsit_put_tiqn_for_login(tiqn
);
272 bool iscsit_check_np_match(
273 struct sockaddr_storage
*sockaddr
,
275 int network_transport
)
277 struct sockaddr_in
*sock_in
, *sock_in_e
;
278 struct sockaddr_in6
*sock_in6
, *sock_in6_e
;
279 bool ip_match
= false;
282 if (sockaddr
->ss_family
== AF_INET6
) {
283 sock_in6
= (struct sockaddr_in6
*)sockaddr
;
284 sock_in6_e
= (struct sockaddr_in6
*)&np
->np_sockaddr
;
286 if (!memcmp(&sock_in6
->sin6_addr
.in6_u
,
287 &sock_in6_e
->sin6_addr
.in6_u
,
288 sizeof(struct in6_addr
)))
291 port
= ntohs(sock_in6
->sin6_port
);
292 port_e
= ntohs(sock_in6_e
->sin6_port
);
294 sock_in
= (struct sockaddr_in
*)sockaddr
;
295 sock_in_e
= (struct sockaddr_in
*)&np
->np_sockaddr
;
297 if (sock_in
->sin_addr
.s_addr
== sock_in_e
->sin_addr
.s_addr
)
300 port
= ntohs(sock_in
->sin_port
);
301 port_e
= ntohs(sock_in_e
->sin_port
);
304 if (ip_match
&& (port_e
== port
) &&
305 (np
->np_network_transport
== network_transport
))
311 static struct iscsi_np
*iscsit_get_np(
312 struct sockaddr_storage
*sockaddr
,
313 int network_transport
)
318 lockdep_assert_held(&np_lock
);
320 list_for_each_entry(np
, &g_np_list
, np_list
) {
321 spin_lock_bh(&np
->np_thread_lock
);
322 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
323 spin_unlock_bh(&np
->np_thread_lock
);
327 match
= iscsit_check_np_match(sockaddr
, np
, network_transport
);
330 * Increment the np_exports reference count now to
331 * prevent iscsit_del_np() below from being called
332 * while iscsi_tpg_add_network_portal() is called.
335 spin_unlock_bh(&np
->np_thread_lock
);
338 spin_unlock_bh(&np
->np_thread_lock
);
344 struct iscsi_np
*iscsit_add_np(
345 struct sockaddr_storage
*sockaddr
,
346 int network_transport
)
351 mutex_lock(&np_lock
);
354 * Locate the existing struct iscsi_np if already active..
356 np
= iscsit_get_np(sockaddr
, network_transport
);
358 mutex_unlock(&np_lock
);
362 np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
364 mutex_unlock(&np_lock
);
365 return ERR_PTR(-ENOMEM
);
368 np
->np_flags
|= NPF_IP_NETWORK
;
369 np
->np_network_transport
= network_transport
;
370 spin_lock_init(&np
->np_thread_lock
);
371 init_completion(&np
->np_restart_comp
);
372 INIT_LIST_HEAD(&np
->np_list
);
374 timer_setup(&np
->np_login_timer
, iscsi_handle_login_thread_timeout
, 0);
376 ret
= iscsi_target_setup_login_socket(np
, sockaddr
);
379 mutex_unlock(&np_lock
);
383 np
->np_thread
= kthread_run(iscsi_target_login_thread
, np
, "iscsi_np");
384 if (IS_ERR(np
->np_thread
)) {
385 pr_err("Unable to create kthread: iscsi_np\n");
386 ret
= PTR_ERR(np
->np_thread
);
388 mutex_unlock(&np_lock
);
392 * Increment the np_exports reference count now to prevent
393 * iscsit_del_np() below from being run while a new call to
394 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
395 * active. We don't need to hold np->np_thread_lock at this
396 * point because iscsi_np has not been added to g_np_list yet.
399 np
->np_thread_state
= ISCSI_NP_THREAD_ACTIVE
;
401 list_add_tail(&np
->np_list
, &g_np_list
);
402 mutex_unlock(&np_lock
);
404 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
405 &np
->np_sockaddr
, np
->np_transport
->name
);
410 int iscsit_reset_np_thread(
412 struct iscsi_tpg_np
*tpg_np
,
413 struct iscsi_portal_group
*tpg
,
416 spin_lock_bh(&np
->np_thread_lock
);
417 if (np
->np_thread_state
== ISCSI_NP_THREAD_INACTIVE
) {
418 spin_unlock_bh(&np
->np_thread_lock
);
421 np
->np_thread_state
= ISCSI_NP_THREAD_RESET
;
422 atomic_inc(&np
->np_reset_count
);
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 return iscsit_add_cmd_to_response_queue(cmd
, cmd
->conn
, cmd
->i_state
);
489 EXPORT_SYMBOL(iscsit_queue_rsp
);
491 void iscsit_aborted_task(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
)
493 spin_lock_bh(&conn
->cmd_lock
);
494 if (!list_empty(&cmd
->i_conn_node
) &&
495 !(cmd
->se_cmd
.transport_state
& CMD_T_FABRIC_STOP
))
496 list_del_init(&cmd
->i_conn_node
);
497 spin_unlock_bh(&conn
->cmd_lock
);
499 __iscsit_free_cmd(cmd
, true);
501 EXPORT_SYMBOL(iscsit_aborted_task
);
503 static void iscsit_do_crypto_hash_buf(struct ahash_request
*, const void *,
504 u32
, u32
, const void *, void *);
505 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn
*);
508 iscsit_xmit_nondatain_pdu(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
509 const void *data_buf
, u32 data_buf_len
)
511 struct iscsi_hdr
*hdr
= (struct iscsi_hdr
*)cmd
->pdu
;
513 u32 niov
= 0, tx_size
= ISCSI_HDR_LEN
;
516 iov
= &cmd
->iov_misc
[0];
517 iov
[niov
].iov_base
= cmd
->pdu
;
518 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
520 if (conn
->conn_ops
->HeaderDigest
) {
521 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
523 iscsit_do_crypto_hash_buf(conn
->conn_tx_hash
, hdr
,
524 ISCSI_HDR_LEN
, 0, NULL
,
527 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
528 tx_size
+= ISCSI_CRC_LEN
;
529 pr_debug("Attaching CRC32C HeaderDigest"
530 " to opcode 0x%x 0x%08x\n",
531 hdr
->opcode
, *header_digest
);
535 u32 padding
= ((-data_buf_len
) & 3);
537 iov
[niov
].iov_base
= (void *)data_buf
;
538 iov
[niov
++].iov_len
= data_buf_len
;
539 tx_size
+= data_buf_len
;
542 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
543 iov
[niov
++].iov_len
= padding
;
545 pr_debug("Attaching %u additional"
546 " padding bytes.\n", padding
);
549 if (conn
->conn_ops
->DataDigest
) {
550 iscsit_do_crypto_hash_buf(conn
->conn_tx_hash
,
551 data_buf
, data_buf_len
,
552 padding
, &cmd
->pad_bytes
,
555 iov
[niov
].iov_base
= &cmd
->data_crc
;
556 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
557 tx_size
+= ISCSI_CRC_LEN
;
558 pr_debug("Attached DataDigest for %u"
559 " bytes opcode 0x%x, CRC 0x%08x\n",
560 data_buf_len
, hdr
->opcode
, cmd
->data_crc
);
564 cmd
->iov_misc_count
= niov
;
565 cmd
->tx_size
= tx_size
;
567 ret
= iscsit_send_tx_data(cmd
, conn
, 1);
569 iscsit_tx_thread_wait_for_tcp(conn
);
576 static int iscsit_map_iovec(struct iscsi_cmd
*, struct kvec
*, u32
, u32
);
577 static void iscsit_unmap_iovec(struct iscsi_cmd
*);
578 static u32
iscsit_do_crypto_hash_sg(struct ahash_request
*, struct iscsi_cmd
*,
579 u32
, u32
, u32
, u8
*);
581 iscsit_xmit_datain_pdu(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
582 const struct iscsi_datain
*datain
)
585 u32 iov_count
= 0, tx_size
= 0;
588 iov
= &cmd
->iov_data
[0];
589 iov
[iov_count
].iov_base
= cmd
->pdu
;
590 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
591 tx_size
+= ISCSI_HDR_LEN
;
593 if (conn
->conn_ops
->HeaderDigest
) {
594 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
596 iscsit_do_crypto_hash_buf(conn
->conn_tx_hash
, cmd
->pdu
,
597 ISCSI_HDR_LEN
, 0, NULL
,
600 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
601 tx_size
+= ISCSI_CRC_LEN
;
603 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
607 iov_ret
= iscsit_map_iovec(cmd
, &cmd
->iov_data
[1],
608 datain
->offset
, datain
->length
);
612 iov_count
+= iov_ret
;
613 tx_size
+= datain
->length
;
615 cmd
->padding
= ((-datain
->length
) & 3);
617 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
618 iov
[iov_count
++].iov_len
= cmd
->padding
;
619 tx_size
+= cmd
->padding
;
621 pr_debug("Attaching %u padding bytes\n", cmd
->padding
);
624 if (conn
->conn_ops
->DataDigest
) {
625 cmd
->data_crc
= iscsit_do_crypto_hash_sg(conn
->conn_tx_hash
,
631 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
632 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
633 tx_size
+= ISCSI_CRC_LEN
;
635 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
636 datain
->length
+ cmd
->padding
, cmd
->data_crc
);
639 cmd
->iov_data_count
= iov_count
;
640 cmd
->tx_size
= tx_size
;
642 ret
= iscsit_fe_sendpage_sg(cmd
, conn
);
644 iscsit_unmap_iovec(cmd
);
647 iscsit_tx_thread_wait_for_tcp(conn
);
654 static int iscsit_xmit_pdu(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
655 struct iscsi_datain_req
*dr
, const void *buf
,
659 return iscsit_xmit_datain_pdu(conn
, cmd
, buf
);
661 return iscsit_xmit_nondatain_pdu(conn
, cmd
, buf
, buf_len
);
664 static enum target_prot_op
iscsit_get_sup_prot_ops(struct iscsi_conn
*conn
)
666 return TARGET_PROT_NORMAL
;
669 static struct iscsit_transport iscsi_target_transport
= {
671 .transport_type
= ISCSI_TCP
,
672 .rdma_shutdown
= false,
674 .iscsit_setup_np
= iscsit_setup_np
,
675 .iscsit_accept_np
= iscsit_accept_np
,
676 .iscsit_free_np
= iscsit_free_np
,
677 .iscsit_get_login_rx
= iscsit_get_login_rx
,
678 .iscsit_put_login_tx
= iscsit_put_login_tx
,
679 .iscsit_get_dataout
= iscsit_build_r2ts_for_cmd
,
680 .iscsit_immediate_queue
= iscsit_immediate_queue
,
681 .iscsit_response_queue
= iscsit_response_queue
,
682 .iscsit_queue_data_in
= iscsit_queue_rsp
,
683 .iscsit_queue_status
= iscsit_queue_rsp
,
684 .iscsit_aborted_task
= iscsit_aborted_task
,
685 .iscsit_xmit_pdu
= iscsit_xmit_pdu
,
686 .iscsit_get_rx_pdu
= iscsit_get_rx_pdu
,
687 .iscsit_get_sup_prot_ops
= iscsit_get_sup_prot_ops
,
690 static int __init
iscsi_target_init_module(void)
694 pr_debug("iSCSI-Target "ISCSIT_VERSION
"\n");
695 iscsit_global
= kzalloc(sizeof(*iscsit_global
), GFP_KERNEL
);
699 spin_lock_init(&iscsit_global
->ts_bitmap_lock
);
700 mutex_init(&auth_id_lock
);
703 ret
= target_register_template(&iscsi_ops
);
707 size
= BITS_TO_LONGS(ISCSIT_BITMAP_BITS
) * sizeof(long);
708 iscsit_global
->ts_bitmap
= vzalloc(size
);
709 if (!iscsit_global
->ts_bitmap
)
712 lio_qr_cache
= kmem_cache_create("lio_qr_cache",
713 sizeof(struct iscsi_queue_req
),
714 __alignof__(struct iscsi_queue_req
), 0, NULL
);
716 pr_err("Unable to kmem_cache_create() for"
721 lio_dr_cache
= kmem_cache_create("lio_dr_cache",
722 sizeof(struct iscsi_datain_req
),
723 __alignof__(struct iscsi_datain_req
), 0, NULL
);
725 pr_err("Unable to kmem_cache_create() for"
730 lio_ooo_cache
= kmem_cache_create("lio_ooo_cache",
731 sizeof(struct iscsi_ooo_cmdsn
),
732 __alignof__(struct iscsi_ooo_cmdsn
), 0, NULL
);
733 if (!lio_ooo_cache
) {
734 pr_err("Unable to kmem_cache_create() for"
739 lio_r2t_cache
= kmem_cache_create("lio_r2t_cache",
740 sizeof(struct iscsi_r2t
), __alignof__(struct iscsi_r2t
),
742 if (!lio_r2t_cache
) {
743 pr_err("Unable to kmem_cache_create() for"
748 iscsit_register_transport(&iscsi_target_transport
);
750 if (iscsit_load_discovery_tpg() < 0)
755 iscsit_unregister_transport(&iscsi_target_transport
);
756 kmem_cache_destroy(lio_r2t_cache
);
758 kmem_cache_destroy(lio_ooo_cache
);
760 kmem_cache_destroy(lio_dr_cache
);
762 kmem_cache_destroy(lio_qr_cache
);
764 vfree(iscsit_global
->ts_bitmap
);
766 /* XXX: this probably wants it to be it's own unwind step.. */
767 if (iscsit_global
->discovery_tpg
)
768 iscsit_tpg_disable_portal_group(iscsit_global
->discovery_tpg
, 1);
769 target_unregister_template(&iscsi_ops
);
771 kfree(iscsit_global
);
775 static void __exit
iscsi_target_cleanup_module(void)
777 iscsit_release_discovery_tpg();
778 iscsit_unregister_transport(&iscsi_target_transport
);
779 kmem_cache_destroy(lio_qr_cache
);
780 kmem_cache_destroy(lio_dr_cache
);
781 kmem_cache_destroy(lio_ooo_cache
);
782 kmem_cache_destroy(lio_r2t_cache
);
785 * Shutdown discovery sessions and disable discovery TPG
787 if (iscsit_global
->discovery_tpg
)
788 iscsit_tpg_disable_portal_group(iscsit_global
->discovery_tpg
, 1);
790 target_unregister_template(&iscsi_ops
);
792 vfree(iscsit_global
->ts_bitmap
);
793 kfree(iscsit_global
);
796 int iscsit_add_reject(
797 struct iscsi_conn
*conn
,
801 struct iscsi_cmd
*cmd
;
803 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
807 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
808 cmd
->reject_reason
= reason
;
810 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
812 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
813 iscsit_free_cmd(cmd
, false);
817 spin_lock_bh(&conn
->cmd_lock
);
818 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
819 spin_unlock_bh(&conn
->cmd_lock
);
821 cmd
->i_state
= ISTATE_SEND_REJECT
;
822 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
826 EXPORT_SYMBOL(iscsit_add_reject
);
828 static int iscsit_add_reject_from_cmd(
829 struct iscsi_cmd
*cmd
,
834 struct iscsi_conn
*conn
;
835 const bool do_put
= cmd
->se_cmd
.se_tfo
!= NULL
;
838 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
844 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
845 cmd
->reject_reason
= reason
;
847 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
849 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
850 iscsit_free_cmd(cmd
, false);
855 spin_lock_bh(&conn
->cmd_lock
);
856 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
857 spin_unlock_bh(&conn
->cmd_lock
);
860 cmd
->i_state
= ISTATE_SEND_REJECT
;
861 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
863 * Perform the kref_put now if se_cmd has already been setup by
864 * scsit_setup_scsi_cmd()
867 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
868 target_put_sess_cmd(&cmd
->se_cmd
);
873 static int iscsit_add_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
,
876 return iscsit_add_reject_from_cmd(cmd
, reason
, true, buf
);
879 int iscsit_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
, unsigned char *buf
)
881 return iscsit_add_reject_from_cmd(cmd
, reason
, false, buf
);
883 EXPORT_SYMBOL(iscsit_reject_cmd
);
886 * Map some portion of the allocated scatterlist to an iovec, suitable for
887 * kernel sockets to copy data in/out.
889 static int iscsit_map_iovec(
890 struct iscsi_cmd
*cmd
,
896 struct scatterlist
*sg
;
897 unsigned int page_off
;
900 * We know each entry in t_data_sg contains a page.
902 u32 ent
= data_offset
/ PAGE_SIZE
;
904 if (ent
>= cmd
->se_cmd
.t_data_nents
) {
905 pr_err("Initial page entry out-of-bounds\n");
909 sg
= &cmd
->se_cmd
.t_data_sg
[ent
];
910 page_off
= (data_offset
% PAGE_SIZE
);
912 cmd
->first_data_sg
= sg
;
913 cmd
->first_data_sg_off
= page_off
;
915 while (data_length
) {
916 u32 cur_len
= min_t(u32
, data_length
, sg
->length
- page_off
);
918 iov
[i
].iov_base
= kmap(sg_page(sg
)) + sg
->offset
+ page_off
;
919 iov
[i
].iov_len
= cur_len
;
921 data_length
-= cur_len
;
927 cmd
->kmapped_nents
= i
;
932 static void iscsit_unmap_iovec(struct iscsi_cmd
*cmd
)
935 struct scatterlist
*sg
;
937 sg
= cmd
->first_data_sg
;
939 for (i
= 0; i
< cmd
->kmapped_nents
; i
++)
940 kunmap(sg_page(&sg
[i
]));
943 static void iscsit_ack_from_expstatsn(struct iscsi_conn
*conn
, u32 exp_statsn
)
946 struct iscsi_cmd
*cmd
, *cmd_p
;
948 conn
->exp_statsn
= exp_statsn
;
950 if (conn
->sess
->sess_ops
->RDMAExtensions
)
953 spin_lock_bh(&conn
->cmd_lock
);
954 list_for_each_entry_safe(cmd
, cmd_p
, &conn
->conn_cmd_list
, i_conn_node
) {
955 spin_lock(&cmd
->istate_lock
);
956 if ((cmd
->i_state
== ISTATE_SENT_STATUS
) &&
957 iscsi_sna_lt(cmd
->stat_sn
, exp_statsn
)) {
958 cmd
->i_state
= ISTATE_REMOVE
;
959 spin_unlock(&cmd
->istate_lock
);
960 list_move_tail(&cmd
->i_conn_node
, &ack_list
);
963 spin_unlock(&cmd
->istate_lock
);
965 spin_unlock_bh(&conn
->cmd_lock
);
967 list_for_each_entry_safe(cmd
, cmd_p
, &ack_list
, i_conn_node
) {
968 list_del_init(&cmd
->i_conn_node
);
969 iscsit_free_cmd(cmd
, false);
973 static int iscsit_allocate_iovecs(struct iscsi_cmd
*cmd
)
975 u32 iov_count
= max(1UL, DIV_ROUND_UP(cmd
->se_cmd
.data_length
, PAGE_SIZE
));
977 iov_count
+= ISCSI_IOV_DATA_BUFFER
;
978 cmd
->iov_data
= kcalloc(iov_count
, sizeof(*cmd
->iov_data
), GFP_KERNEL
);
982 cmd
->orig_iov_data_count
= iov_count
;
986 int iscsit_setup_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
989 int data_direction
, payload_length
;
990 struct iscsi_scsi_req
*hdr
;
994 atomic_long_inc(&conn
->sess
->cmd_pdus
);
996 hdr
= (struct iscsi_scsi_req
*) buf
;
997 payload_length
= ntoh24(hdr
->dlength
);
999 /* FIXME; Add checks for AdditionalHeaderSegment */
1001 if (!(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) &&
1002 !(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
1003 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1004 " not set. Bad iSCSI Initiator.\n");
1005 return iscsit_add_reject_cmd(cmd
,
1006 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1009 if (((hdr
->flags
& ISCSI_FLAG_CMD_READ
) ||
1010 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) && !hdr
->data_length
) {
1012 * From RFC-3720 Section 10.3.1:
1014 * "Either or both of R and W MAY be 1 when either the
1015 * Expected Data Transfer Length and/or Bidirectional Read
1016 * Expected Data Transfer Length are 0"
1018 * For this case, go ahead and clear the unnecssary bits
1019 * to avoid any confusion with ->data_direction.
1021 hdr
->flags
&= ~ISCSI_FLAG_CMD_READ
;
1022 hdr
->flags
&= ~ISCSI_FLAG_CMD_WRITE
;
1024 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
1025 " set when Expected Data Transfer Length is 0 for"
1026 " CDB: 0x%02x, Fixing up flags\n", hdr
->cdb
[0]);
1029 if (!(hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
1030 !(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) && (hdr
->data_length
!= 0)) {
1031 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1032 " MUST be set if Expected Data Transfer Length is not 0."
1033 " Bad iSCSI Initiator\n");
1034 return iscsit_add_reject_cmd(cmd
,
1035 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1038 if ((hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
1039 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) {
1040 pr_err("Bidirectional operations not supported!\n");
1041 return iscsit_add_reject_cmd(cmd
,
1042 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1045 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
1046 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1047 " Scsi Command PDU.\n");
1048 return iscsit_add_reject_cmd(cmd
,
1049 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1052 if (payload_length
&& !conn
->sess
->sess_ops
->ImmediateData
) {
1053 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1054 " protocol error.\n", payload_length
);
1055 return iscsit_add_reject_cmd(cmd
,
1056 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1059 if ((be32_to_cpu(hdr
->data_length
) == payload_length
) &&
1060 (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
))) {
1061 pr_err("Expected Data Transfer Length and Length of"
1062 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1063 " bit is not set protocol error\n");
1064 return iscsit_add_reject_cmd(cmd
,
1065 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1068 if (payload_length
> be32_to_cpu(hdr
->data_length
)) {
1069 pr_err("DataSegmentLength: %u is greater than"
1070 " EDTL: %u, protocol error.\n", payload_length
,
1072 return iscsit_add_reject_cmd(cmd
,
1073 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1076 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1077 pr_err("DataSegmentLength: %u is greater than"
1078 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1079 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
1080 return iscsit_add_reject_cmd(cmd
,
1081 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1084 if (payload_length
> conn
->sess
->sess_ops
->FirstBurstLength
) {
1085 pr_err("DataSegmentLength: %u is greater than"
1086 " FirstBurstLength: %u, protocol error.\n",
1087 payload_length
, conn
->sess
->sess_ops
->FirstBurstLength
);
1088 return iscsit_add_reject_cmd(cmd
,
1089 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1092 data_direction
= (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) ? DMA_TO_DEVICE
:
1093 (hdr
->flags
& ISCSI_FLAG_CMD_READ
) ? DMA_FROM_DEVICE
:
1096 cmd
->data_direction
= data_direction
;
1097 iscsi_task_attr
= hdr
->flags
& ISCSI_FLAG_CMD_ATTR_MASK
;
1099 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1101 if ((iscsi_task_attr
== ISCSI_ATTR_UNTAGGED
) ||
1102 (iscsi_task_attr
== ISCSI_ATTR_SIMPLE
))
1103 sam_task_attr
= TCM_SIMPLE_TAG
;
1104 else if (iscsi_task_attr
== ISCSI_ATTR_ORDERED
)
1105 sam_task_attr
= TCM_ORDERED_TAG
;
1106 else if (iscsi_task_attr
== ISCSI_ATTR_HEAD_OF_QUEUE
)
1107 sam_task_attr
= TCM_HEAD_TAG
;
1108 else if (iscsi_task_attr
== ISCSI_ATTR_ACA
)
1109 sam_task_attr
= TCM_ACA_TAG
;
1111 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
1112 " TCM_SIMPLE_TAG\n", iscsi_task_attr
);
1113 sam_task_attr
= TCM_SIMPLE_TAG
;
1116 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_CMD
;
1117 cmd
->i_state
= ISTATE_NEW_CMD
;
1118 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1119 cmd
->immediate_data
= (payload_length
) ? 1 : 0;
1120 cmd
->unsolicited_data
= ((!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) &&
1121 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) ? 1 : 0);
1122 if (cmd
->unsolicited_data
)
1123 cmd
->cmd_flags
|= ICF_NON_IMMEDIATE_UNSOLICITED_DATA
;
1125 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1126 if (hdr
->flags
& ISCSI_FLAG_CMD_READ
)
1127 cmd
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
1129 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1130 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1131 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1132 cmd
->first_burst_len
= payload_length
;
1134 if (!conn
->sess
->sess_ops
->RDMAExtensions
&&
1135 cmd
->data_direction
== DMA_FROM_DEVICE
) {
1136 struct iscsi_datain_req
*dr
;
1138 dr
= iscsit_allocate_datain_req();
1140 return iscsit_add_reject_cmd(cmd
,
1141 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1143 iscsit_attach_datain_req(cmd
, dr
);
1147 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1149 transport_init_se_cmd(&cmd
->se_cmd
, &iscsi_ops
,
1150 conn
->sess
->se_sess
, be32_to_cpu(hdr
->data_length
),
1151 cmd
->data_direction
, sam_task_attr
,
1152 cmd
->sense_buffer
+ 2);
1154 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1155 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr
->itt
,
1156 hdr
->cmdsn
, be32_to_cpu(hdr
->data_length
), payload_length
,
1159 target_get_sess_cmd(&cmd
->se_cmd
, true);
1161 cmd
->sense_reason
= transport_lookup_cmd_lun(&cmd
->se_cmd
,
1162 scsilun_to_int(&hdr
->lun
));
1163 if (cmd
->sense_reason
)
1166 /* only used for printks or comparing with ->ref_task_tag */
1167 cmd
->se_cmd
.tag
= (__force u32
)cmd
->init_task_tag
;
1168 cmd
->sense_reason
= target_setup_cmd_from_cdb(&cmd
->se_cmd
, hdr
->cdb
);
1169 if (cmd
->sense_reason
) {
1170 if (cmd
->sense_reason
== TCM_OUT_OF_RESOURCES
) {
1171 return iscsit_add_reject_cmd(cmd
,
1172 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1178 if (iscsit_build_pdu_and_seq_lists(cmd
, payload_length
) < 0) {
1179 return iscsit_add_reject_cmd(cmd
,
1180 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1184 spin_lock_bh(&conn
->cmd_lock
);
1185 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1186 spin_unlock_bh(&conn
->cmd_lock
);
1188 * Check if we need to delay processing because of ALUA
1189 * Active/NonOptimized primary access state..
1191 core_alua_check_nonop_delay(&cmd
->se_cmd
);
1195 EXPORT_SYMBOL(iscsit_setup_scsi_cmd
);
1197 void iscsit_set_unsolicited_dataout(struct iscsi_cmd
*cmd
)
1199 iscsit_set_dataout_sequence_values(cmd
);
1201 spin_lock_bh(&cmd
->dataout_timeout_lock
);
1202 iscsit_start_dataout_timer(cmd
, cmd
->conn
);
1203 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
1205 EXPORT_SYMBOL(iscsit_set_unsolicited_dataout
);
1207 int iscsit_process_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1208 struct iscsi_scsi_req
*hdr
)
1212 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1213 * the Immediate Bit is not set, and no Immediate
1216 * A PDU/CmdSN carrying Immediate Data can only
1217 * be processed after the DataCRC has passed.
1218 * If the DataCRC fails, the CmdSN MUST NOT
1219 * be acknowledged. (See below)
1221 if (!cmd
->immediate_data
) {
1222 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1223 (unsigned char *)hdr
, hdr
->cmdsn
);
1224 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1226 else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1227 target_put_sess_cmd(&cmd
->se_cmd
);
1232 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1235 * If no Immediate Data is attached, it's OK to return now.
1237 if (!cmd
->immediate_data
) {
1238 if (!cmd
->sense_reason
&& cmd
->unsolicited_data
)
1239 iscsit_set_unsolicited_dataout(cmd
);
1240 if (!cmd
->sense_reason
)
1243 target_put_sess_cmd(&cmd
->se_cmd
);
1248 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1249 * execution. These exceptions are processed in CmdSN order using
1250 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1252 if (cmd
->sense_reason
)
1255 * Call directly into transport_generic_new_cmd() to perform
1256 * the backend memory allocation.
1258 cmd
->sense_reason
= transport_generic_new_cmd(&cmd
->se_cmd
);
1259 if (cmd
->sense_reason
)
1264 EXPORT_SYMBOL(iscsit_process_scsi_cmd
);
1267 iscsit_get_immediate_data(struct iscsi_cmd
*cmd
, struct iscsi_scsi_req
*hdr
,
1270 int cmdsn_ret
= 0, immed_ret
= IMMEDIATE_DATA_NORMAL_OPERATION
;
1272 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1275 goto after_immediate_data
;
1277 * Check for underflow case where both EDTL and immediate data payload
1278 * exceeds what is presented by CDB's TRANSFER LENGTH, and what has
1279 * already been set in target_cmd_size_check() as se_cmd->data_length.
1281 * For this special case, fail the command and dump the immediate data
1284 if (cmd
->first_burst_len
> cmd
->se_cmd
.data_length
) {
1285 cmd
->sense_reason
= TCM_INVALID_CDB_FIELD
;
1286 goto after_immediate_data
;
1289 immed_ret
= iscsit_handle_immediate_data(cmd
, hdr
,
1290 cmd
->first_burst_len
);
1291 after_immediate_data
:
1292 if (immed_ret
== IMMEDIATE_DATA_NORMAL_OPERATION
) {
1294 * A PDU/CmdSN carrying Immediate Data passed
1295 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1296 * Immediate Bit is not set.
1298 cmdsn_ret
= iscsit_sequence_cmd(cmd
->conn
, cmd
,
1299 (unsigned char *)hdr
, hdr
->cmdsn
);
1300 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1303 if (cmd
->sense_reason
|| cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1306 rc
= iscsit_dump_data_payload(cmd
->conn
,
1307 cmd
->first_burst_len
, 1);
1308 target_put_sess_cmd(&cmd
->se_cmd
);
1310 } else if (cmd
->unsolicited_data
)
1311 iscsit_set_unsolicited_dataout(cmd
);
1313 } else if (immed_ret
== IMMEDIATE_DATA_ERL1_CRC_FAILURE
) {
1315 * Immediate Data failed DataCRC and ERL>=1,
1316 * silently drop this PDU and let the initiator
1317 * plug the CmdSN gap.
1319 * FIXME: Send Unsolicited NOPIN with reserved
1320 * TTT here to help the initiator figure out
1321 * the missing CmdSN, although they should be
1322 * intelligent enough to determine the missing
1323 * CmdSN and issue a retry to plug the sequence.
1325 cmd
->i_state
= ISTATE_REMOVE
;
1326 iscsit_add_cmd_to_immediate_queue(cmd
, cmd
->conn
, cmd
->i_state
);
1327 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1334 iscsit_handle_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1337 struct iscsi_scsi_req
*hdr
= (struct iscsi_scsi_req
*)buf
;
1339 bool dump_payload
= false;
1341 rc
= iscsit_setup_scsi_cmd(conn
, cmd
, buf
);
1345 * Allocation iovecs needed for struct socket operations for
1346 * traditional iSCSI block I/O.
1348 if (iscsit_allocate_iovecs(cmd
) < 0) {
1349 return iscsit_reject_cmd(cmd
,
1350 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1352 immed_data
= cmd
->immediate_data
;
1354 rc
= iscsit_process_scsi_cmd(conn
, cmd
, hdr
);
1358 dump_payload
= true;
1363 return iscsit_get_immediate_data(cmd
, hdr
, dump_payload
);
1366 static u32
iscsit_do_crypto_hash_sg(
1367 struct ahash_request
*hash
,
1368 struct iscsi_cmd
*cmd
,
1375 struct scatterlist
*sg
;
1376 unsigned int page_off
;
1378 crypto_ahash_init(hash
);
1380 sg
= cmd
->first_data_sg
;
1381 page_off
= cmd
->first_data_sg_off
;
1383 while (data_length
) {
1384 u32 cur_len
= min_t(u32
, data_length
, (sg
->length
- page_off
));
1386 ahash_request_set_crypt(hash
, sg
, NULL
, cur_len
);
1387 crypto_ahash_update(hash
);
1389 data_length
-= cur_len
;
1391 /* iscsit_map_iovec has already checked for invalid sg pointers */
1396 struct scatterlist pad_sg
;
1398 sg_init_one(&pad_sg
, pad_bytes
, padding
);
1399 ahash_request_set_crypt(hash
, &pad_sg
, (u8
*)&data_crc
,
1401 crypto_ahash_finup(hash
);
1403 ahash_request_set_crypt(hash
, NULL
, (u8
*)&data_crc
, 0);
1404 crypto_ahash_final(hash
);
1410 static void iscsit_do_crypto_hash_buf(struct ahash_request
*hash
,
1411 const void *buf
, u32 payload_length
, u32 padding
,
1412 const void *pad_bytes
, void *data_crc
)
1414 struct scatterlist sg
[2];
1416 sg_init_table(sg
, ARRAY_SIZE(sg
));
1417 sg_set_buf(sg
, buf
, payload_length
);
1419 sg_set_buf(sg
+ 1, pad_bytes
, padding
);
1421 ahash_request_set_crypt(hash
, sg
, data_crc
, payload_length
+ padding
);
1423 crypto_ahash_digest(hash
);
1427 __iscsit_check_dataout_hdr(struct iscsi_conn
*conn
, void *buf
,
1428 struct iscsi_cmd
*cmd
, u32 payload_length
,
1431 struct iscsi_data
*hdr
= buf
;
1432 struct se_cmd
*se_cmd
;
1436 atomic_long_add(payload_length
, &conn
->sess
->rx_data_octets
);
1438 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1439 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1440 hdr
->itt
, hdr
->ttt
, hdr
->datasn
, ntohl(hdr
->offset
),
1441 payload_length
, conn
->cid
);
1443 if (cmd
->cmd_flags
& ICF_GOT_LAST_DATAOUT
) {
1444 pr_err("Command ITT: 0x%08x received DataOUT after"
1445 " last DataOUT received, dumping payload\n",
1446 cmd
->init_task_tag
);
1447 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1450 if (cmd
->data_direction
!= DMA_TO_DEVICE
) {
1451 pr_err("Command ITT: 0x%08x received DataOUT for a"
1452 " NON-WRITE command.\n", cmd
->init_task_tag
);
1453 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1455 se_cmd
= &cmd
->se_cmd
;
1456 iscsit_mod_dataout_timer(cmd
);
1458 if ((be32_to_cpu(hdr
->offset
) + payload_length
) > cmd
->se_cmd
.data_length
) {
1459 pr_err("DataOut Offset: %u, Length %u greater than iSCSI Command EDTL %u, protocol error.\n",
1460 be32_to_cpu(hdr
->offset
), payload_length
,
1461 cmd
->se_cmd
.data_length
);
1462 return iscsit_reject_cmd(cmd
, ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1465 if (cmd
->unsolicited_data
) {
1466 int dump_unsolicited_data
= 0;
1468 if (conn
->sess
->sess_ops
->InitialR2T
) {
1469 pr_err("Received unexpected unsolicited data"
1470 " while InitialR2T=Yes, protocol error.\n");
1471 transport_send_check_condition_and_sense(&cmd
->se_cmd
,
1472 TCM_UNEXPECTED_UNSOLICITED_DATA
, 0);
1476 * Special case for dealing with Unsolicited DataOUT
1477 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1481 /* Something's amiss if we're not in WRITE_PENDING state... */
1482 WARN_ON(se_cmd
->t_state
!= TRANSPORT_WRITE_PENDING
);
1483 if (!(se_cmd
->se_cmd_flags
& SCF_SUPPORTED_SAM_OPCODE
))
1484 dump_unsolicited_data
= 1;
1486 if (dump_unsolicited_data
) {
1488 * Check if a delayed TASK_ABORTED status needs to
1489 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1490 * received with the unsolicited data out.
1492 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1493 iscsit_stop_dataout_timer(cmd
);
1495 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1499 * For the normal solicited data path:
1501 * Check for a delayed TASK_ABORTED status and dump any
1502 * incoming data out payload if one exists. Also, when the
1503 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1504 * data out sequence, we decrement outstanding_r2ts. Once
1505 * outstanding_r2ts reaches zero, go ahead and send the delayed
1506 * TASK_ABORTED status.
1508 if (se_cmd
->transport_state
& CMD_T_ABORTED
) {
1509 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
&&
1510 --cmd
->outstanding_r2ts
< 1)
1511 iscsit_stop_dataout_timer(cmd
);
1513 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1517 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1518 * within-command recovery checks before receiving the payload.
1520 rc
= iscsit_check_pre_dataout(cmd
, buf
);
1521 if (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
)
1523 else if (rc
== DATAOUT_CANNOT_RECOVER
)
1528 EXPORT_SYMBOL(__iscsit_check_dataout_hdr
);
1531 iscsit_check_dataout_hdr(struct iscsi_conn
*conn
, void *buf
,
1532 struct iscsi_cmd
**out_cmd
)
1534 struct iscsi_data
*hdr
= buf
;
1535 struct iscsi_cmd
*cmd
;
1536 u32 payload_length
= ntoh24(hdr
->dlength
);
1538 bool success
= false;
1540 if (!payload_length
) {
1541 pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
1545 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1546 pr_err_ratelimited("DataSegmentLength: %u is greater than"
1547 " MaxXmitDataSegmentLength: %u\n", payload_length
,
1548 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1549 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1552 cmd
= iscsit_find_cmd_from_itt_or_dump(conn
, hdr
->itt
, payload_length
);
1556 rc
= __iscsit_check_dataout_hdr(conn
, buf
, cmd
, payload_length
, &success
);
1563 EXPORT_SYMBOL(iscsit_check_dataout_hdr
);
1566 iscsit_get_dataout(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1567 struct iscsi_data
*hdr
)
1570 u32 checksum
, iov_count
= 0, padding
= 0, rx_got
= 0, rx_size
= 0;
1571 u32 payload_length
= ntoh24(hdr
->dlength
);
1572 int iov_ret
, data_crc_failed
= 0;
1574 rx_size
+= payload_length
;
1575 iov
= &cmd
->iov_data
[0];
1577 iov_ret
= iscsit_map_iovec(cmd
, iov
, be32_to_cpu(hdr
->offset
),
1582 iov_count
+= iov_ret
;
1584 padding
= ((-payload_length
) & 3);
1586 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
1587 iov
[iov_count
++].iov_len
= padding
;
1589 pr_debug("Receiving %u padding bytes.\n", padding
);
1592 if (conn
->conn_ops
->DataDigest
) {
1593 iov
[iov_count
].iov_base
= &checksum
;
1594 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
1595 rx_size
+= ISCSI_CRC_LEN
;
1598 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
1600 iscsit_unmap_iovec(cmd
);
1602 if (rx_got
!= rx_size
)
1605 if (conn
->conn_ops
->DataDigest
) {
1608 data_crc
= iscsit_do_crypto_hash_sg(conn
->conn_rx_hash
, cmd
,
1609 be32_to_cpu(hdr
->offset
),
1610 payload_length
, padding
,
1613 if (checksum
!= data_crc
) {
1614 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1615 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1616 " does not match computed 0x%08x\n",
1617 hdr
->itt
, hdr
->offset
, payload_length
,
1618 hdr
->datasn
, checksum
, data_crc
);
1619 data_crc_failed
= 1;
1621 pr_debug("Got CRC32C DataDigest 0x%08x for"
1622 " %u bytes of Data Out\n", checksum
,
1627 return data_crc_failed
;
1631 iscsit_check_dataout_payload(struct iscsi_cmd
*cmd
, struct iscsi_data
*hdr
,
1632 bool data_crc_failed
)
1634 struct iscsi_conn
*conn
= cmd
->conn
;
1637 * Increment post receive data and CRC values or perform
1638 * within-command recovery.
1640 rc
= iscsit_check_post_dataout(cmd
, (unsigned char *)hdr
, data_crc_failed
);
1641 if ((rc
== DATAOUT_NORMAL
) || (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
))
1643 else if (rc
== DATAOUT_SEND_R2T
) {
1644 iscsit_set_dataout_sequence_values(cmd
);
1645 conn
->conn_transport
->iscsit_get_dataout(conn
, cmd
, false);
1646 } else if (rc
== DATAOUT_SEND_TO_TRANSPORT
) {
1648 * Handle extra special case for out of order
1649 * Unsolicited Data Out.
1651 spin_lock_bh(&cmd
->istate_lock
);
1652 ooo_cmdsn
= (cmd
->cmd_flags
& ICF_OOO_CMDSN
);
1653 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
1654 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
1655 spin_unlock_bh(&cmd
->istate_lock
);
1657 iscsit_stop_dataout_timer(cmd
);
1660 target_execute_cmd(&cmd
->se_cmd
);
1662 } else /* DATAOUT_CANNOT_RECOVER */
1667 EXPORT_SYMBOL(iscsit_check_dataout_payload
);
1669 static int iscsit_handle_data_out(struct iscsi_conn
*conn
, unsigned char *buf
)
1671 struct iscsi_cmd
*cmd
= NULL
;
1672 struct iscsi_data
*hdr
= (struct iscsi_data
*)buf
;
1674 bool data_crc_failed
= false;
1676 rc
= iscsit_check_dataout_hdr(conn
, buf
, &cmd
);
1682 rc
= iscsit_get_dataout(conn
, cmd
, hdr
);
1686 data_crc_failed
= true;
1688 return iscsit_check_dataout_payload(cmd
, hdr
, data_crc_failed
);
1691 int iscsit_setup_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1692 struct iscsi_nopout
*hdr
)
1694 u32 payload_length
= ntoh24(hdr
->dlength
);
1696 if (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
1697 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1699 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1700 (unsigned char *)hdr
);
1702 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1703 (unsigned char *)hdr
);
1706 if (hdr
->itt
== RESERVED_ITT
&& !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1707 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1708 " not set, protocol error.\n");
1710 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1711 (unsigned char *)hdr
);
1713 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1714 (unsigned char *)hdr
);
1717 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1718 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1719 " greater than MaxXmitDataSegmentLength: %u, protocol"
1720 " error.\n", payload_length
,
1721 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1723 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1724 (unsigned char *)hdr
);
1726 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1727 (unsigned char *)hdr
);
1730 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1731 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1732 hdr
->itt
== RESERVED_ITT
? "Response" : "Request",
1733 hdr
->itt
, hdr
->ttt
, hdr
->cmdsn
, hdr
->exp_statsn
,
1736 * This is not a response to a Unsolicited NopIN, which means
1737 * it can either be a NOPOUT ping request (with a valid ITT),
1738 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1739 * Either way, make sure we allocate an struct iscsi_cmd, as both
1740 * can contain ping data.
1742 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1743 cmd
->iscsi_opcode
= ISCSI_OP_NOOP_OUT
;
1744 cmd
->i_state
= ISTATE_SEND_NOPIN
;
1745 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ?
1747 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1748 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1749 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1750 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1751 cmd
->data_direction
= DMA_NONE
;
1756 EXPORT_SYMBOL(iscsit_setup_nop_out
);
1758 int iscsit_process_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1759 struct iscsi_nopout
*hdr
)
1761 struct iscsi_cmd
*cmd_p
= NULL
;
1764 * Initiator is expecting a NopIN ping reply..
1766 if (hdr
->itt
!= RESERVED_ITT
) {
1768 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1769 (unsigned char *)hdr
);
1771 spin_lock_bh(&conn
->cmd_lock
);
1772 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1773 spin_unlock_bh(&conn
->cmd_lock
);
1775 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1777 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
1778 iscsit_add_cmd_to_response_queue(cmd
, conn
,
1783 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1784 (unsigned char *)hdr
, hdr
->cmdsn
);
1785 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
1787 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1793 * This was a response to a unsolicited NOPIN ping.
1795 if (hdr
->ttt
!= cpu_to_be32(0xFFFFFFFF)) {
1796 cmd_p
= iscsit_find_cmd_from_ttt(conn
, be32_to_cpu(hdr
->ttt
));
1800 iscsit_stop_nopin_response_timer(conn
);
1802 cmd_p
->i_state
= ISTATE_REMOVE
;
1803 iscsit_add_cmd_to_immediate_queue(cmd_p
, conn
, cmd_p
->i_state
);
1805 iscsit_start_nopin_timer(conn
);
1809 * Otherwise, initiator is not expecting a NOPIN is response.
1810 * Just ignore for now.
1814 iscsit_free_cmd(cmd
, false);
1818 EXPORT_SYMBOL(iscsit_process_nop_out
);
1820 static int iscsit_handle_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1823 unsigned char *ping_data
= NULL
;
1824 struct iscsi_nopout
*hdr
= (struct iscsi_nopout
*)buf
;
1825 struct kvec
*iov
= NULL
;
1826 u32 payload_length
= ntoh24(hdr
->dlength
);
1829 ret
= iscsit_setup_nop_out(conn
, cmd
, hdr
);
1833 * Handle NOP-OUT payload for traditional iSCSI sockets
1835 if (payload_length
&& hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1836 u32 checksum
, data_crc
, padding
= 0;
1837 int niov
= 0, rx_got
, rx_size
= payload_length
;
1839 ping_data
= kzalloc(payload_length
+ 1, GFP_KERNEL
);
1845 iov
= &cmd
->iov_misc
[0];
1846 iov
[niov
].iov_base
= ping_data
;
1847 iov
[niov
++].iov_len
= payload_length
;
1849 padding
= ((-payload_length
) & 3);
1851 pr_debug("Receiving %u additional bytes"
1852 " for padding.\n", padding
);
1853 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
1854 iov
[niov
++].iov_len
= padding
;
1857 if (conn
->conn_ops
->DataDigest
) {
1858 iov
[niov
].iov_base
= &checksum
;
1859 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
1860 rx_size
+= ISCSI_CRC_LEN
;
1863 rx_got
= rx_data(conn
, &cmd
->iov_misc
[0], niov
, rx_size
);
1864 if (rx_got
!= rx_size
) {
1869 if (conn
->conn_ops
->DataDigest
) {
1870 iscsit_do_crypto_hash_buf(conn
->conn_rx_hash
, ping_data
,
1871 payload_length
, padding
,
1872 cmd
->pad_bytes
, &data_crc
);
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);
1923 static enum tcm_tmreq_table
iscsit_convert_tmf(u8 iscsi_tmf
)
1925 switch (iscsi_tmf
) {
1926 case ISCSI_TM_FUNC_ABORT_TASK
:
1927 return TMR_ABORT_TASK
;
1928 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
1929 return TMR_ABORT_TASK_SET
;
1930 case ISCSI_TM_FUNC_CLEAR_ACA
:
1931 return TMR_CLEAR_ACA
;
1932 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
1933 return TMR_CLEAR_TASK_SET
;
1934 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
1935 return TMR_LUN_RESET
;
1936 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
1937 return TMR_TARGET_WARM_RESET
;
1938 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
1939 return TMR_TARGET_COLD_RESET
;
1946 iscsit_handle_task_mgt_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1949 struct se_tmr_req
*se_tmr
;
1950 struct iscsi_tmr_req
*tmr_req
;
1951 struct iscsi_tm
*hdr
;
1952 int out_of_order_cmdsn
= 0, ret
;
1953 u8 function
, tcm_function
= TMR_UNKNOWN
;
1955 hdr
= (struct iscsi_tm
*) buf
;
1956 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
1957 function
= hdr
->flags
;
1959 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1960 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1961 " 0x%08x, CID: %hu\n", hdr
->itt
, hdr
->cmdsn
, function
,
1962 hdr
->rtt
, hdr
->refcmdsn
, conn
->cid
);
1964 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1965 ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1966 hdr
->rtt
!= RESERVED_ITT
)) {
1967 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1968 hdr
->rtt
= RESERVED_ITT
;
1971 if ((function
== ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1972 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1973 pr_err("Task Management Request TASK_REASSIGN not"
1974 " issued as immediate command, bad iSCSI Initiator"
1975 "implementation\n");
1976 return iscsit_add_reject_cmd(cmd
,
1977 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1979 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1980 be32_to_cpu(hdr
->refcmdsn
) != ISCSI_RESERVED_TAG
)
1981 hdr
->refcmdsn
= cpu_to_be32(ISCSI_RESERVED_TAG
);
1983 cmd
->data_direction
= DMA_NONE
;
1984 cmd
->tmr_req
= kzalloc(sizeof(*cmd
->tmr_req
), GFP_KERNEL
);
1985 if (!cmd
->tmr_req
) {
1986 return iscsit_add_reject_cmd(cmd
,
1987 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1991 transport_init_se_cmd(&cmd
->se_cmd
, &iscsi_ops
,
1992 conn
->sess
->se_sess
, 0, DMA_NONE
,
1993 TCM_SIMPLE_TAG
, cmd
->sense_buffer
+ 2);
1995 target_get_sess_cmd(&cmd
->se_cmd
, true);
1998 * TASK_REASSIGN for ERL=2 / connection stays inside of
1999 * LIO-Target $FABRIC_MOD
2001 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
2002 tcm_function
= iscsit_convert_tmf(function
);
2003 if (tcm_function
== TMR_UNKNOWN
) {
2004 pr_err("Unknown iSCSI TMR Function:"
2005 " 0x%02x\n", function
);
2006 return iscsit_add_reject_cmd(cmd
,
2007 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
2010 ret
= core_tmr_alloc_req(&cmd
->se_cmd
, cmd
->tmr_req
, tcm_function
,
2013 return iscsit_add_reject_cmd(cmd
,
2014 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
2016 cmd
->tmr_req
->se_tmr_req
= cmd
->se_cmd
.se_tmr_req
;
2018 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_TMFUNC
;
2019 cmd
->i_state
= ISTATE_SEND_TASKMGTRSP
;
2020 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2021 cmd
->init_task_tag
= hdr
->itt
;
2022 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2023 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
2024 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
2025 se_tmr
= cmd
->se_cmd
.se_tmr_req
;
2026 tmr_req
= cmd
->tmr_req
;
2028 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2030 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
2031 ret
= transport_lookup_tmr_lun(&cmd
->se_cmd
,
2032 scsilun_to_int(&hdr
->lun
));
2034 se_tmr
->response
= ISCSI_TMF_RSP_NO_LUN
;
2040 case ISCSI_TM_FUNC_ABORT_TASK
:
2041 se_tmr
->response
= iscsit_tmr_abort_task(cmd
, buf
);
2042 if (se_tmr
->response
)
2045 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
2046 case ISCSI_TM_FUNC_CLEAR_ACA
:
2047 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
2048 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
2050 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
2051 if (iscsit_tmr_task_warm_reset(conn
, tmr_req
, buf
) < 0) {
2052 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
2056 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
2057 if (iscsit_tmr_task_cold_reset(conn
, tmr_req
, buf
) < 0) {
2058 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
2062 case ISCSI_TM_FUNC_TASK_REASSIGN
:
2063 se_tmr
->response
= iscsit_tmr_task_reassign(cmd
, buf
);
2065 * Perform sanity checks on the ExpDataSN only if the
2066 * TASK_REASSIGN was successful.
2068 if (se_tmr
->response
)
2071 if (iscsit_check_task_reassign_expdatasn(tmr_req
, conn
) < 0)
2072 return iscsit_add_reject_cmd(cmd
,
2073 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
2076 pr_err("Unknown TMR function: 0x%02x, protocol"
2077 " error.\n", function
);
2078 se_tmr
->response
= ISCSI_TMF_RSP_NOT_SUPPORTED
;
2082 if ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
2083 (se_tmr
->response
== ISCSI_TMF_RSP_COMPLETE
))
2084 se_tmr
->call_transport
= 1;
2086 spin_lock_bh(&conn
->cmd_lock
);
2087 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2088 spin_unlock_bh(&conn
->cmd_lock
);
2090 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
2091 int cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
2092 if (cmdsn_ret
== CMDSN_HIGHER_THAN_EXP
) {
2093 out_of_order_cmdsn
= 1;
2094 } else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
2095 target_put_sess_cmd(&cmd
->se_cmd
);
2097 } else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
) {
2101 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2103 if (out_of_order_cmdsn
|| !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
))
2106 * Found the referenced task, send to transport for processing.
2108 if (se_tmr
->call_transport
)
2109 return transport_generic_handle_tmr(&cmd
->se_cmd
);
2112 * Could not find the referenced LUN, task, or Task Management
2113 * command not authorized or supported. Change state and
2114 * let the tx_thread send the response.
2116 * For connection recovery, this is also the default action for
2117 * TMR TASK_REASSIGN.
2119 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2120 target_put_sess_cmd(&cmd
->se_cmd
);
2123 EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd
);
2125 /* #warning FIXME: Support Text Command parameters besides SendTargets */
2127 iscsit_setup_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2128 struct iscsi_text
*hdr
)
2130 u32 payload_length
= ntoh24(hdr
->dlength
);
2132 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
2133 pr_err("Unable to accept text parameter length: %u"
2134 "greater than MaxXmitDataSegmentLength %u.\n",
2135 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
2136 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
2137 (unsigned char *)hdr
);
2140 if (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) ||
2141 (hdr
->flags
& ISCSI_FLAG_TEXT_CONTINUE
)) {
2142 pr_err("Multi sequence text commands currently not supported\n");
2143 return iscsit_reject_cmd(cmd
, ISCSI_REASON_CMD_NOT_SUPPORTED
,
2144 (unsigned char *)hdr
);
2147 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2148 " ExpStatSN: 0x%08x, Length: %u\n", hdr
->itt
, hdr
->cmdsn
,
2149 hdr
->exp_statsn
, payload_length
);
2151 cmd
->iscsi_opcode
= ISCSI_OP_TEXT
;
2152 cmd
->i_state
= ISTATE_SEND_TEXTRSP
;
2153 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2154 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
2155 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2156 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
2157 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
2158 cmd
->data_direction
= DMA_NONE
;
2159 kfree(cmd
->text_in_ptr
);
2160 cmd
->text_in_ptr
= NULL
;
2164 EXPORT_SYMBOL(iscsit_setup_text_cmd
);
2167 iscsit_process_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2168 struct iscsi_text
*hdr
)
2170 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
;
2174 cmd
->targ_xfer_tag
= be32_to_cpu(hdr
->ttt
);
2175 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF) {
2176 pr_err("Unable to locate text_in buffer for sendtargets"
2180 goto empty_sendtargets
;
2182 if (strncmp("SendTargets", text_in
, 11) != 0) {
2183 pr_err("Received Text Data that is not"
2184 " SendTargets, cannot continue.\n");
2187 text_ptr
= strchr(text_in
, '=');
2189 pr_err("No \"=\" separator found in Text Data,"
2190 " cannot continue.\n");
2193 if (!strncmp("=All", text_ptr
, 4)) {
2194 cmd
->cmd_flags
|= ICF_SENDTARGETS_ALL
;
2195 } else if (!strncmp("=iqn.", text_ptr
, 5) ||
2196 !strncmp("=eui.", text_ptr
, 5)) {
2197 cmd
->cmd_flags
|= ICF_SENDTARGETS_SINGLE
;
2199 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr
);
2203 spin_lock_bh(&conn
->cmd_lock
);
2204 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2205 spin_unlock_bh(&conn
->cmd_lock
);
2208 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2210 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
2211 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
2212 (unsigned char *)hdr
, hdr
->cmdsn
);
2213 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2219 return iscsit_execute_cmd(cmd
, 0);
2222 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
2223 (unsigned char *)hdr
);
2225 EXPORT_SYMBOL(iscsit_process_text_cmd
);
2228 iscsit_handle_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2231 struct iscsi_text
*hdr
= (struct iscsi_text
*)buf
;
2232 char *text_in
= NULL
;
2233 u32 payload_length
= ntoh24(hdr
->dlength
);
2236 rc
= iscsit_setup_text_cmd(conn
, cmd
, hdr
);
2240 rx_size
= payload_length
;
2241 if (payload_length
) {
2242 u32 checksum
= 0, data_crc
= 0;
2244 int niov
= 0, rx_got
;
2247 rx_size
= ALIGN(payload_length
, 4);
2248 text_in
= kzalloc(rx_size
, GFP_KERNEL
);
2252 cmd
->text_in_ptr
= text_in
;
2254 memset(iov
, 0, sizeof(iov
));
2255 iov
[niov
].iov_base
= text_in
;
2256 iov
[niov
++].iov_len
= rx_size
;
2258 padding
= rx_size
- payload_length
;
2260 pr_debug("Receiving %u additional bytes"
2261 " for padding.\n", padding
);
2262 if (conn
->conn_ops
->DataDigest
) {
2263 iov
[niov
].iov_base
= &checksum
;
2264 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
2265 rx_size
+= ISCSI_CRC_LEN
;
2268 rx_got
= rx_data(conn
, &iov
[0], niov
, rx_size
);
2269 if (rx_got
!= rx_size
)
2272 if (conn
->conn_ops
->DataDigest
) {
2273 iscsit_do_crypto_hash_buf(conn
->conn_rx_hash
,
2274 text_in
, rx_size
, 0, NULL
,
2277 if (checksum
!= data_crc
) {
2278 pr_err("Text data CRC32C DataDigest"
2279 " 0x%08x does not match computed"
2280 " 0x%08x\n", checksum
, data_crc
);
2281 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2282 pr_err("Unable to recover from"
2283 " Text Data digest failure while in"
2288 * Silently drop this PDU and let the
2289 * initiator plug the CmdSN gap.
2291 pr_debug("Dropping Text"
2292 " Command CmdSN: 0x%08x due to"
2293 " DataCRC error.\n", hdr
->cmdsn
);
2298 pr_debug("Got CRC32C DataDigest"
2299 " 0x%08x for %u bytes of text data.\n",
2300 checksum
, payload_length
);
2303 text_in
[payload_length
- 1] = '\0';
2304 pr_debug("Successfully read %d bytes of text"
2305 " data.\n", payload_length
);
2308 return iscsit_process_text_cmd(conn
, cmd
, hdr
);
2311 kfree(cmd
->text_in_ptr
);
2312 cmd
->text_in_ptr
= NULL
;
2313 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
, buf
);
2316 int iscsit_logout_closesession(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2318 struct iscsi_conn
*conn_p
;
2319 struct iscsi_session
*sess
= conn
->sess
;
2321 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2322 " for SID: %u.\n", conn
->cid
, conn
->sess
->sid
);
2324 atomic_set(&sess
->session_logout
, 1);
2325 atomic_set(&conn
->conn_logout_remove
, 1);
2326 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_SESSION
;
2328 iscsit_inc_conn_usage_count(conn
);
2329 iscsit_inc_session_usage_count(sess
);
2331 spin_lock_bh(&sess
->conn_lock
);
2332 list_for_each_entry(conn_p
, &sess
->sess_conn_list
, conn_list
) {
2333 if (conn_p
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
)
2336 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2337 conn_p
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2339 spin_unlock_bh(&sess
->conn_lock
);
2341 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2346 int iscsit_logout_closeconnection(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2348 struct iscsi_conn
*l_conn
;
2349 struct iscsi_session
*sess
= conn
->sess
;
2351 pr_debug("Received logout request CLOSECONNECTION for CID:"
2352 " %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2355 * A Logout Request with a CLOSECONNECTION reason code for a CID
2356 * can arrive on a connection with a differing CID.
2358 if (conn
->cid
== cmd
->logout_cid
) {
2359 spin_lock_bh(&conn
->state_lock
);
2360 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2361 conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2363 atomic_set(&conn
->conn_logout_remove
, 1);
2364 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
;
2365 iscsit_inc_conn_usage_count(conn
);
2367 spin_unlock_bh(&conn
->state_lock
);
2370 * Handle all different cid CLOSECONNECTION requests in
2371 * iscsit_logout_post_handler_diffcid() as to give enough
2372 * time for any non immediate command's CmdSN to be
2373 * acknowledged on the connection in question.
2375 * Here we simply make sure the CID is still around.
2377 l_conn
= iscsit_get_conn_from_cid(sess
,
2380 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2381 iscsit_add_cmd_to_response_queue(cmd
, conn
,
2386 iscsit_dec_conn_usage_count(l_conn
);
2389 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2394 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2396 struct iscsi_session
*sess
= conn
->sess
;
2398 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2399 " CID: %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2401 if (sess
->sess_ops
->ErrorRecoveryLevel
!= 2) {
2402 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2403 " while ERL!=2.\n");
2404 cmd
->logout_response
= ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
;
2405 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2409 if (conn
->cid
== cmd
->logout_cid
) {
2410 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2411 " with CID: %hu on CID: %hu, implementation error.\n",
2412 cmd
->logout_cid
, conn
->cid
);
2413 cmd
->logout_response
= ISCSI_LOGOUT_CLEANUP_FAILED
;
2414 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2418 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2424 iscsit_handle_logout_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2427 int cmdsn_ret
, logout_remove
= 0;
2429 struct iscsi_logout
*hdr
;
2430 struct iscsi_tiqn
*tiqn
= iscsit_snmp_get_tiqn(conn
);
2432 hdr
= (struct iscsi_logout
*) buf
;
2433 reason_code
= (hdr
->flags
& 0x7f);
2436 spin_lock(&tiqn
->logout_stats
.lock
);
2437 if (reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
)
2438 tiqn
->logout_stats
.normal_logouts
++;
2440 tiqn
->logout_stats
.abnormal_logouts
++;
2441 spin_unlock(&tiqn
->logout_stats
.lock
);
2444 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2445 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2446 hdr
->itt
, hdr
->cmdsn
, hdr
->exp_statsn
, reason_code
,
2447 hdr
->cid
, conn
->cid
);
2449 if (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
) {
2450 pr_err("Received logout request on connection that"
2451 " is not in logged in state, ignoring request.\n");
2452 iscsit_free_cmd(cmd
, false);
2456 cmd
->iscsi_opcode
= ISCSI_OP_LOGOUT
;
2457 cmd
->i_state
= ISTATE_SEND_LOGOUTRSP
;
2458 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2459 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
2460 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2461 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
2462 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
2463 cmd
->logout_cid
= be16_to_cpu(hdr
->cid
);
2464 cmd
->logout_reason
= reason_code
;
2465 cmd
->data_direction
= DMA_NONE
;
2468 * We need to sleep in these cases (by returning 1) until the Logout
2469 * Response gets sent in the tx thread.
2471 if ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) ||
2472 ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
) &&
2473 be16_to_cpu(hdr
->cid
) == conn
->cid
))
2476 spin_lock_bh(&conn
->cmd_lock
);
2477 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2478 spin_unlock_bh(&conn
->cmd_lock
);
2480 if (reason_code
!= ISCSI_LOGOUT_REASON_RECOVERY
)
2481 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2484 * Immediate commands are executed, well, immediately.
2485 * Non-Immediate Logout Commands are executed in CmdSN order.
2487 if (cmd
->immediate_cmd
) {
2488 int ret
= iscsit_execute_cmd(cmd
, 0);
2493 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
2494 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
2496 else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2500 return logout_remove
;
2502 EXPORT_SYMBOL(iscsit_handle_logout_cmd
);
2504 int iscsit_handle_snack(
2505 struct iscsi_conn
*conn
,
2508 struct iscsi_snack
*hdr
;
2510 hdr
= (struct iscsi_snack
*) buf
;
2511 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
2513 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2514 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2515 " CID: %hu\n", hdr
->itt
, hdr
->exp_statsn
, hdr
->flags
,
2516 hdr
->begrun
, hdr
->runlength
, conn
->cid
);
2518 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2519 pr_err("Initiator sent SNACK request while in"
2520 " ErrorRecoveryLevel=0.\n");
2521 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2525 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2526 * call from inside iscsi_send_recovery_datain_or_r2t().
2528 switch (hdr
->flags
& ISCSI_FLAG_SNACK_TYPE_MASK
) {
2530 return iscsit_handle_recovery_datain_or_r2t(conn
, buf
,
2532 be32_to_cpu(hdr
->ttt
),
2533 be32_to_cpu(hdr
->begrun
),
2534 be32_to_cpu(hdr
->runlength
));
2535 case ISCSI_FLAG_SNACK_TYPE_STATUS
:
2536 return iscsit_handle_status_snack(conn
, hdr
->itt
,
2537 be32_to_cpu(hdr
->ttt
),
2538 be32_to_cpu(hdr
->begrun
), be32_to_cpu(hdr
->runlength
));
2539 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK
:
2540 return iscsit_handle_data_ack(conn
, be32_to_cpu(hdr
->ttt
),
2541 be32_to_cpu(hdr
->begrun
),
2542 be32_to_cpu(hdr
->runlength
));
2543 case ISCSI_FLAG_SNACK_TYPE_RDATA
:
2544 /* FIXME: Support R-Data SNACK */
2545 pr_err("R-Data SNACK Not Supported.\n");
2546 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2549 pr_err("Unknown SNACK type 0x%02x, protocol"
2550 " error.\n", hdr
->flags
& 0x0f);
2551 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2557 EXPORT_SYMBOL(iscsit_handle_snack
);
2559 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2561 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2562 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2563 wait_for_completion_interruptible_timeout(
2564 &conn
->rx_half_close_comp
,
2565 ISCSI_RX_THREAD_TCP_TIMEOUT
* HZ
);
2569 static int iscsit_handle_immediate_data(
2570 struct iscsi_cmd
*cmd
,
2571 struct iscsi_scsi_req
*hdr
,
2574 int iov_ret
, rx_got
= 0, rx_size
= 0;
2575 u32 checksum
, iov_count
= 0, padding
= 0;
2576 struct iscsi_conn
*conn
= cmd
->conn
;
2579 iov_ret
= iscsit_map_iovec(cmd
, cmd
->iov_data
, cmd
->write_data_done
, length
);
2581 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2584 iov_count
= iov_ret
;
2585 iov
= &cmd
->iov_data
[0];
2587 padding
= ((-length
) & 3);
2589 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2590 iov
[iov_count
++].iov_len
= padding
;
2594 if (conn
->conn_ops
->DataDigest
) {
2595 iov
[iov_count
].iov_base
= &checksum
;
2596 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2597 rx_size
+= ISCSI_CRC_LEN
;
2600 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
2602 iscsit_unmap_iovec(cmd
);
2604 if (rx_got
!= rx_size
) {
2605 iscsit_rx_thread_wait_for_tcp(conn
);
2606 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2609 if (conn
->conn_ops
->DataDigest
) {
2612 data_crc
= iscsit_do_crypto_hash_sg(conn
->conn_rx_hash
, cmd
,
2613 cmd
->write_data_done
, length
, padding
,
2616 if (checksum
!= data_crc
) {
2617 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2618 " does not match computed 0x%08x\n", checksum
,
2621 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2622 pr_err("Unable to recover from"
2623 " Immediate Data digest failure while"
2625 iscsit_reject_cmd(cmd
,
2626 ISCSI_REASON_DATA_DIGEST_ERROR
,
2627 (unsigned char *)hdr
);
2628 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2630 iscsit_reject_cmd(cmd
,
2631 ISCSI_REASON_DATA_DIGEST_ERROR
,
2632 (unsigned char *)hdr
);
2633 return IMMEDIATE_DATA_ERL1_CRC_FAILURE
;
2636 pr_debug("Got CRC32C DataDigest 0x%08x for"
2637 " %u bytes of Immediate Data\n", checksum
,
2642 cmd
->write_data_done
+= length
;
2644 if (cmd
->write_data_done
== cmd
->se_cmd
.data_length
) {
2645 spin_lock_bh(&cmd
->istate_lock
);
2646 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
2647 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
2648 spin_unlock_bh(&cmd
->istate_lock
);
2651 return IMMEDIATE_DATA_NORMAL_OPERATION
;
2654 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2655 with active network interface */
2656 static void iscsit_build_conn_drop_async_message(struct iscsi_conn
*conn
)
2658 struct iscsi_cmd
*cmd
;
2659 struct iscsi_conn
*conn_p
;
2662 lockdep_assert_held(&conn
->sess
->conn_lock
);
2665 * Only send a Asynchronous Message on connections whos network
2666 * interface is still functional.
2668 list_for_each_entry(conn_p
, &conn
->sess
->sess_conn_list
, conn_list
) {
2669 if (conn_p
->conn_state
== TARG_CONN_STATE_LOGGED_IN
) {
2670 iscsit_inc_conn_usage_count(conn_p
);
2679 cmd
= iscsit_allocate_cmd(conn_p
, TASK_RUNNING
);
2681 iscsit_dec_conn_usage_count(conn_p
);
2685 cmd
->logout_cid
= conn
->cid
;
2686 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2687 cmd
->i_state
= ISTATE_SEND_ASYNCMSG
;
2689 spin_lock_bh(&conn_p
->cmd_lock
);
2690 list_add_tail(&cmd
->i_conn_node
, &conn_p
->conn_cmd_list
);
2691 spin_unlock_bh(&conn_p
->cmd_lock
);
2693 iscsit_add_cmd_to_response_queue(cmd
, conn_p
, cmd
->i_state
);
2694 iscsit_dec_conn_usage_count(conn_p
);
2697 static int iscsit_send_conn_drop_async_message(
2698 struct iscsi_cmd
*cmd
,
2699 struct iscsi_conn
*conn
)
2701 struct iscsi_async
*hdr
;
2703 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2705 hdr
= (struct iscsi_async
*) cmd
->pdu
;
2706 hdr
->opcode
= ISCSI_OP_ASYNC_EVENT
;
2707 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
2708 cmd
->init_task_tag
= RESERVED_ITT
;
2709 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2710 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->rsvd4
[0]);
2711 cmd
->stat_sn
= conn
->stat_sn
++;
2712 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2713 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2714 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2715 hdr
->async_event
= ISCSI_ASYNC_MSG_DROPPING_CONNECTION
;
2716 hdr
->param1
= cpu_to_be16(cmd
->logout_cid
);
2717 hdr
->param2
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Wait
);
2718 hdr
->param3
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Retain
);
2720 pr_debug("Sending Connection Dropped Async Message StatSN:"
2721 " 0x%08x, for CID: %hu on CID: %hu\n", cmd
->stat_sn
,
2722 cmd
->logout_cid
, conn
->cid
);
2724 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
2727 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2729 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2730 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2731 wait_for_completion_interruptible_timeout(
2732 &conn
->tx_half_close_comp
,
2733 ISCSI_TX_THREAD_TCP_TIMEOUT
* HZ
);
2738 iscsit_build_datain_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2739 struct iscsi_datain
*datain
, struct iscsi_data_rsp
*hdr
,
2742 hdr
->opcode
= ISCSI_OP_SCSI_DATA_IN
;
2743 hdr
->flags
= datain
->flags
;
2744 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
2745 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
2746 hdr
->flags
|= ISCSI_FLAG_DATA_OVERFLOW
;
2747 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2748 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
2749 hdr
->flags
|= ISCSI_FLAG_DATA_UNDERFLOW
;
2750 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2753 hton24(hdr
->dlength
, datain
->length
);
2754 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2755 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
2756 (struct scsi_lun
*)&hdr
->lun
);
2758 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2760 hdr
->itt
= cmd
->init_task_tag
;
2762 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2763 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2765 hdr
->ttt
= cpu_to_be32(0xFFFFFFFF);
2767 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2769 hdr
->statsn
= cpu_to_be32(0xFFFFFFFF);
2771 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2772 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2773 hdr
->datasn
= cpu_to_be32(datain
->data_sn
);
2774 hdr
->offset
= cpu_to_be32(datain
->offset
);
2776 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2777 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2778 cmd
->init_task_tag
, ntohl(hdr
->statsn
), ntohl(hdr
->datasn
),
2779 ntohl(hdr
->offset
), datain
->length
, conn
->cid
);
2781 EXPORT_SYMBOL(iscsit_build_datain_pdu
);
2783 static int iscsit_send_datain(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2785 struct iscsi_data_rsp
*hdr
= (struct iscsi_data_rsp
*)&cmd
->pdu
[0];
2786 struct iscsi_datain datain
;
2787 struct iscsi_datain_req
*dr
;
2789 bool set_statsn
= false;
2791 memset(&datain
, 0, sizeof(struct iscsi_datain
));
2792 dr
= iscsit_get_datain_values(cmd
, &datain
);
2794 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2795 cmd
->init_task_tag
);
2799 * Be paranoid and double check the logic for now.
2801 if ((datain
.offset
+ datain
.length
) > cmd
->se_cmd
.data_length
) {
2802 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2803 " datain.length: %u exceeds cmd->data_length: %u\n",
2804 cmd
->init_task_tag
, datain
.offset
, datain
.length
,
2805 cmd
->se_cmd
.data_length
);
2809 atomic_long_add(datain
.length
, &conn
->sess
->tx_data_octets
);
2811 * Special case for successfully execution w/ both DATAIN
2814 if ((datain
.flags
& ISCSI_FLAG_DATA_STATUS
) &&
2815 (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
))
2816 datain
.flags
&= ~ISCSI_FLAG_DATA_STATUS
;
2818 if ((dr
->dr_complete
== DATAIN_COMPLETE_NORMAL
) ||
2819 (dr
->dr_complete
== DATAIN_COMPLETE_CONNECTION_RECOVERY
)) {
2820 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2821 cmd
->stat_sn
= conn
->stat_sn
++;
2823 } else if (dr
->dr_complete
==
2824 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY
)
2828 iscsit_build_datain_pdu(cmd
, conn
, &datain
, hdr
, set_statsn
);
2830 ret
= conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, dr
, &datain
, 0);
2834 if (dr
->dr_complete
) {
2835 eodr
= (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ?
2837 iscsit_free_datain_req(cmd
, dr
);
2844 iscsit_build_logout_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2845 struct iscsi_logout_rsp
*hdr
)
2847 struct iscsi_conn
*logout_conn
= NULL
;
2848 struct iscsi_conn_recovery
*cr
= NULL
;
2849 struct iscsi_session
*sess
= conn
->sess
;
2851 * The actual shutting down of Sessions and/or Connections
2852 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2853 * is done in scsi_logout_post_handler().
2855 switch (cmd
->logout_reason
) {
2856 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
2857 pr_debug("iSCSI session logout successful, setting"
2858 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2859 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2861 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
2862 if (cmd
->logout_response
== ISCSI_LOGOUT_CID_NOT_FOUND
)
2865 * For CLOSECONNECTION logout requests carrying
2866 * a matching logout CID -> local CID, the reference
2867 * for the local CID will have been incremented in
2868 * iscsi_logout_closeconnection().
2870 * For CLOSECONNECTION logout requests carrying
2871 * a different CID than the connection it arrived
2872 * on, the connection responding to cmd->logout_cid
2873 * is stopped in iscsit_logout_post_handler_diffcid().
2876 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2877 " successful.\n", cmd
->logout_cid
, conn
->cid
);
2878 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2880 case ISCSI_LOGOUT_REASON_RECOVERY
:
2881 if ((cmd
->logout_response
== ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
) ||
2882 (cmd
->logout_response
== ISCSI_LOGOUT_CLEANUP_FAILED
))
2885 * If the connection is still active from our point of view
2886 * force connection recovery to occur.
2888 logout_conn
= iscsit_get_conn_from_cid_rcfr(sess
,
2891 iscsit_connection_reinstatement_rcfr(logout_conn
);
2892 iscsit_dec_conn_usage_count(logout_conn
);
2895 cr
= iscsit_get_inactive_connection_recovery_entry(
2896 conn
->sess
, cmd
->logout_cid
);
2898 pr_err("Unable to locate CID: %hu for"
2899 " REMOVECONNFORRECOVERY Logout Request.\n",
2901 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2905 iscsit_discard_cr_cmds_by_expstatsn(cr
, cmd
->exp_stat_sn
);
2907 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2908 " for recovery for CID: %hu on CID: %hu successful.\n",
2909 cmd
->logout_cid
, conn
->cid
);
2910 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2913 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2914 cmd
->logout_reason
);
2918 hdr
->opcode
= ISCSI_OP_LOGOUT_RSP
;
2919 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2920 hdr
->response
= cmd
->logout_response
;
2921 hdr
->itt
= cmd
->init_task_tag
;
2922 cmd
->stat_sn
= conn
->stat_sn
++;
2923 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2925 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2926 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2927 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2929 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2930 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2931 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
,
2932 cmd
->logout_cid
, conn
->cid
);
2936 EXPORT_SYMBOL(iscsit_build_logout_rsp
);
2939 iscsit_send_logout(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2943 rc
= iscsit_build_logout_rsp(cmd
, conn
,
2944 (struct iscsi_logout_rsp
*)&cmd
->pdu
[0]);
2948 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
2952 iscsit_build_nopin_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2953 struct iscsi_nopin
*hdr
, bool nopout_response
)
2955 hdr
->opcode
= ISCSI_OP_NOOP_IN
;
2956 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2957 hton24(hdr
->dlength
, cmd
->buf_ptr_size
);
2958 if (nopout_response
)
2959 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2960 hdr
->itt
= cmd
->init_task_tag
;
2961 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2962 cmd
->stat_sn
= (nopout_response
) ? conn
->stat_sn
++ :
2964 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2966 if (nopout_response
)
2967 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2969 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2970 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2972 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2973 " StatSN: 0x%08x, Length %u\n", (nopout_response
) ?
2974 "Solicited" : "Unsolicited", cmd
->init_task_tag
,
2975 cmd
->targ_xfer_tag
, cmd
->stat_sn
, cmd
->buf_ptr_size
);
2977 EXPORT_SYMBOL(iscsit_build_nopin_rsp
);
2980 * Unsolicited NOPIN, either requesting a response or not.
2982 static int iscsit_send_unsolicited_nopin(
2983 struct iscsi_cmd
*cmd
,
2984 struct iscsi_conn
*conn
,
2987 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
2990 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, false);
2992 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2993 " 0x%08x CID: %hu\n", hdr
->ttt
, cmd
->stat_sn
, conn
->cid
);
2995 ret
= conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
2999 spin_lock_bh(&cmd
->istate_lock
);
3000 cmd
->i_state
= want_response
?
3001 ISTATE_SENT_NOPIN_WANT_RESPONSE
: ISTATE_SENT_STATUS
;
3002 spin_unlock_bh(&cmd
->istate_lock
);
3008 iscsit_send_nopin(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3010 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
3012 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, true);
3015 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3016 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3018 pr_debug("Echoing back %u bytes of ping data.\n", cmd
->buf_ptr_size
);
3020 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
,
3025 static int iscsit_send_r2t(
3026 struct iscsi_cmd
*cmd
,
3027 struct iscsi_conn
*conn
)
3029 struct iscsi_r2t
*r2t
;
3030 struct iscsi_r2t_rsp
*hdr
;
3033 r2t
= iscsit_get_r2t_from_list(cmd
);
3037 hdr
= (struct iscsi_r2t_rsp
*) cmd
->pdu
;
3038 memset(hdr
, 0, ISCSI_HDR_LEN
);
3039 hdr
->opcode
= ISCSI_OP_R2T
;
3040 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3041 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
3042 (struct scsi_lun
*)&hdr
->lun
);
3043 hdr
->itt
= cmd
->init_task_tag
;
3044 if (conn
->conn_transport
->iscsit_get_r2t_ttt
)
3045 conn
->conn_transport
->iscsit_get_r2t_ttt(conn
, cmd
, r2t
);
3047 r2t
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
3048 hdr
->ttt
= cpu_to_be32(r2t
->targ_xfer_tag
);
3049 hdr
->statsn
= cpu_to_be32(conn
->stat_sn
);
3050 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3051 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3052 hdr
->r2tsn
= cpu_to_be32(r2t
->r2t_sn
);
3053 hdr
->data_offset
= cpu_to_be32(r2t
->offset
);
3054 hdr
->data_length
= cpu_to_be32(r2t
->xfer_len
);
3056 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3057 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3058 (!r2t
->recovery_r2t
) ? "" : "Recovery ", cmd
->init_task_tag
,
3059 r2t
->targ_xfer_tag
, ntohl(hdr
->statsn
), r2t
->r2t_sn
,
3060 r2t
->offset
, r2t
->xfer_len
, conn
->cid
);
3062 spin_lock_bh(&cmd
->r2t_lock
);
3064 spin_unlock_bh(&cmd
->r2t_lock
);
3066 ret
= conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
3071 spin_lock_bh(&cmd
->dataout_timeout_lock
);
3072 iscsit_start_dataout_timer(cmd
, conn
);
3073 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
3079 * @recovery: If called from iscsi_task_reassign_complete_write() for
3080 * connection recovery.
3082 int iscsit_build_r2ts_for_cmd(
3083 struct iscsi_conn
*conn
,
3084 struct iscsi_cmd
*cmd
,
3088 u32 offset
= 0, xfer_len
= 0;
3090 spin_lock_bh(&cmd
->r2t_lock
);
3091 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
) {
3092 spin_unlock_bh(&cmd
->r2t_lock
);
3096 if (conn
->sess
->sess_ops
->DataSequenceInOrder
&&
3098 cmd
->r2t_offset
= max(cmd
->r2t_offset
, cmd
->write_data_done
);
3100 while (cmd
->outstanding_r2ts
< conn
->sess
->sess_ops
->MaxOutstandingR2T
) {
3101 if (conn
->sess
->sess_ops
->DataSequenceInOrder
) {
3102 offset
= cmd
->r2t_offset
;
3104 if (first_r2t
&& recovery
) {
3105 int new_data_end
= offset
+
3106 conn
->sess
->sess_ops
->MaxBurstLength
-
3107 cmd
->next_burst_len
;
3109 if (new_data_end
> cmd
->se_cmd
.data_length
)
3110 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3113 conn
->sess
->sess_ops
->MaxBurstLength
-
3114 cmd
->next_burst_len
;
3116 int new_data_end
= offset
+
3117 conn
->sess
->sess_ops
->MaxBurstLength
;
3119 if (new_data_end
> cmd
->se_cmd
.data_length
)
3120 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3122 xfer_len
= conn
->sess
->sess_ops
->MaxBurstLength
;
3124 cmd
->r2t_offset
+= xfer_len
;
3126 if (cmd
->r2t_offset
== cmd
->se_cmd
.data_length
)
3127 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3129 struct iscsi_seq
*seq
;
3131 seq
= iscsit_get_seq_holder_for_r2t(cmd
);
3133 spin_unlock_bh(&cmd
->r2t_lock
);
3137 offset
= seq
->offset
;
3138 xfer_len
= seq
->xfer_len
;
3140 if (cmd
->seq_send_order
== cmd
->seq_count
)
3141 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3143 cmd
->outstanding_r2ts
++;
3146 if (iscsit_add_r2t_to_list(cmd
, offset
, xfer_len
, 0, 0) < 0) {
3147 spin_unlock_bh(&cmd
->r2t_lock
);
3151 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
)
3154 spin_unlock_bh(&cmd
->r2t_lock
);
3158 EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd
);
3160 void iscsit_build_rsp_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3161 bool inc_stat_sn
, struct iscsi_scsi_rsp
*hdr
)
3164 cmd
->stat_sn
= conn
->stat_sn
++;
3166 atomic_long_inc(&conn
->sess
->rsp_pdus
);
3168 memset(hdr
, 0, ISCSI_HDR_LEN
);
3169 hdr
->opcode
= ISCSI_OP_SCSI_CMD_RSP
;
3170 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3171 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
3172 hdr
->flags
|= ISCSI_FLAG_CMD_OVERFLOW
;
3173 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3174 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
3175 hdr
->flags
|= ISCSI_FLAG_CMD_UNDERFLOW
;
3176 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3178 hdr
->response
= cmd
->iscsi_response
;
3179 hdr
->cmd_status
= cmd
->se_cmd
.scsi_status
;
3180 hdr
->itt
= cmd
->init_task_tag
;
3181 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3183 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3184 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3185 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3187 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3188 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3189 cmd
->init_task_tag
, cmd
->stat_sn
, cmd
->se_cmd
.scsi_status
,
3190 cmd
->se_cmd
.scsi_status
, conn
->cid
);
3192 EXPORT_SYMBOL(iscsit_build_rsp_pdu
);
3194 static int iscsit_send_response(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3196 struct iscsi_scsi_rsp
*hdr
= (struct iscsi_scsi_rsp
*)&cmd
->pdu
[0];
3197 bool inc_stat_sn
= (cmd
->i_state
== ISTATE_SEND_STATUS
);
3198 void *data_buf
= NULL
;
3199 u32 padding
= 0, data_buf_len
= 0;
3201 iscsit_build_rsp_pdu(cmd
, conn
, inc_stat_sn
, hdr
);
3204 * Attach SENSE DATA payload to iSCSI Response PDU
3206 if (cmd
->se_cmd
.sense_buffer
&&
3207 ((cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
3208 (cmd
->se_cmd
.se_cmd_flags
& SCF_EMULATED_TASK_SENSE
))) {
3209 put_unaligned_be16(cmd
->se_cmd
.scsi_sense_length
, cmd
->sense_buffer
);
3210 cmd
->se_cmd
.scsi_sense_length
+= sizeof (__be16
);
3212 padding
= -(cmd
->se_cmd
.scsi_sense_length
) & 3;
3213 hton24(hdr
->dlength
, (u32
)cmd
->se_cmd
.scsi_sense_length
);
3214 data_buf
= cmd
->sense_buffer
;
3215 data_buf_len
= cmd
->se_cmd
.scsi_sense_length
+ padding
;
3218 memset(cmd
->sense_buffer
+
3219 cmd
->se_cmd
.scsi_sense_length
, 0, padding
);
3220 pr_debug("Adding %u bytes of padding to"
3221 " SENSE.\n", padding
);
3224 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3226 cmd
->se_cmd
.scsi_sense_length
);
3229 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, data_buf
,
3233 static u8
iscsit_convert_tcm_tmr_rsp(struct se_tmr_req
*se_tmr
)
3235 switch (se_tmr
->response
) {
3236 case TMR_FUNCTION_COMPLETE
:
3237 return ISCSI_TMF_RSP_COMPLETE
;
3238 case TMR_TASK_DOES_NOT_EXIST
:
3239 return ISCSI_TMF_RSP_NO_TASK
;
3240 case TMR_LUN_DOES_NOT_EXIST
:
3241 return ISCSI_TMF_RSP_NO_LUN
;
3242 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
:
3243 return ISCSI_TMF_RSP_NOT_SUPPORTED
;
3244 case TMR_FUNCTION_REJECTED
:
3246 return ISCSI_TMF_RSP_REJECTED
;
3251 iscsit_build_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3252 struct iscsi_tm_rsp
*hdr
)
3254 struct se_tmr_req
*se_tmr
= cmd
->se_cmd
.se_tmr_req
;
3256 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC_RSP
;
3257 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
3258 hdr
->response
= iscsit_convert_tcm_tmr_rsp(se_tmr
);
3259 hdr
->itt
= cmd
->init_task_tag
;
3260 cmd
->stat_sn
= conn
->stat_sn
++;
3261 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3263 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3264 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3265 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3267 pr_debug("Built Task Management Response ITT: 0x%08x,"
3268 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3269 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
, conn
->cid
);
3271 EXPORT_SYMBOL(iscsit_build_task_mgt_rsp
);
3274 iscsit_send_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3276 struct iscsi_tm_rsp
*hdr
= (struct iscsi_tm_rsp
*)&cmd
->pdu
[0];
3278 iscsit_build_task_mgt_rsp(cmd
, conn
, hdr
);
3280 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
3283 #define SENDTARGETS_BUF_LIMIT 32768U
3286 iscsit_build_sendtargets_response(struct iscsi_cmd
*cmd
,
3287 enum iscsit_transport_type network_transport
,
3288 int skip_bytes
, bool *completed
)
3290 char *payload
= NULL
;
3291 struct iscsi_conn
*conn
= cmd
->conn
;
3292 struct iscsi_portal_group
*tpg
;
3293 struct iscsi_tiqn
*tiqn
;
3294 struct iscsi_tpg_np
*tpg_np
;
3295 int buffer_len
, end_of_buf
= 0, len
= 0, payload_len
= 0;
3296 int target_name_printed
;
3297 unsigned char buf
[ISCSI_IQN_LEN
+12]; /* iqn + "TargetName=" + \0 */
3298 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
= NULL
;
3301 buffer_len
= min(conn
->conn_ops
->MaxRecvDataSegmentLength
,
3302 SENDTARGETS_BUF_LIMIT
);
3304 payload
= kzalloc(buffer_len
, GFP_KERNEL
);
3309 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
3312 if (cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
) {
3313 text_ptr
= strchr(text_in
, '=');
3315 pr_err("Unable to locate '=' string in text_in:"
3321 * Skip over '=' character..
3326 spin_lock(&tiqn_lock
);
3327 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
3328 if ((cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
) &&
3329 strcmp(tiqn
->tiqn
, text_ptr
)) {
3333 target_name_printed
= 0;
3335 spin_lock(&tiqn
->tiqn_tpg_lock
);
3336 list_for_each_entry(tpg
, &tiqn
->tiqn_tpg_list
, tpg_list
) {
3338 /* If demo_mode_discovery=0 and generate_node_acls=0
3339 * (demo mode dislabed) do not return
3340 * TargetName+TargetAddress unless a NodeACL exists.
3343 if ((tpg
->tpg_attrib
.generate_node_acls
== 0) &&
3344 (tpg
->tpg_attrib
.demo_mode_discovery
== 0) &&
3345 (!target_tpg_has_node_acl(&tpg
->tpg_se_tpg
,
3346 cmd
->conn
->sess
->sess_ops
->InitiatorName
))) {
3350 spin_lock(&tpg
->tpg_state_lock
);
3351 active
= (tpg
->tpg_state
== TPG_STATE_ACTIVE
);
3352 spin_unlock(&tpg
->tpg_state_lock
);
3354 if (!active
&& tpg
->tpg_attrib
.tpg_enabled_sendtargets
)
3357 spin_lock(&tpg
->tpg_np_lock
);
3358 list_for_each_entry(tpg_np
, &tpg
->tpg_gnp_list
,
3360 struct iscsi_np
*np
= tpg_np
->tpg_np
;
3361 struct sockaddr_storage
*sockaddr
;
3363 if (np
->np_network_transport
!= network_transport
)
3366 if (!target_name_printed
) {
3367 len
= sprintf(buf
, "TargetName=%s",
3371 if ((len
+ payload_len
) > buffer_len
) {
3372 spin_unlock(&tpg
->tpg_np_lock
);
3373 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3378 if (skip_bytes
&& len
<= skip_bytes
) {
3381 memcpy(payload
+ payload_len
, buf
, len
);
3383 target_name_printed
= 1;
3384 if (len
> skip_bytes
)
3389 if (inet_addr_is_any((struct sockaddr
*)&np
->np_sockaddr
))
3390 sockaddr
= &conn
->local_sockaddr
;
3392 sockaddr
= &np
->np_sockaddr
;
3394 len
= sprintf(buf
, "TargetAddress="
3400 if ((len
+ payload_len
) > buffer_len
) {
3401 spin_unlock(&tpg
->tpg_np_lock
);
3402 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3407 if (skip_bytes
&& len
<= skip_bytes
) {
3410 memcpy(payload
+ payload_len
, buf
, len
);
3412 if (len
> skip_bytes
)
3416 spin_unlock(&tpg
->tpg_np_lock
);
3418 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3425 if (cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
)
3428 spin_unlock(&tiqn_lock
);
3430 cmd
->buf_ptr
= payload
;
3436 iscsit_build_text_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3437 struct iscsi_text_rsp
*hdr
,
3438 enum iscsit_transport_type network_transport
)
3440 int text_length
, padding
;
3441 bool completed
= true;
3443 text_length
= iscsit_build_sendtargets_response(cmd
, network_transport
,
3444 cmd
->read_data_done
,
3446 if (text_length
< 0)
3450 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
3452 hdr
->flags
= ISCSI_FLAG_TEXT_CONTINUE
;
3453 cmd
->read_data_done
+= text_length
;
3454 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF)
3455 cmd
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
3457 hdr
->opcode
= ISCSI_OP_TEXT_RSP
;
3458 padding
= ((-text_length
) & 3);
3459 hton24(hdr
->dlength
, text_length
);
3460 hdr
->itt
= cmd
->init_task_tag
;
3461 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
3462 cmd
->stat_sn
= conn
->stat_sn
++;
3463 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3465 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3467 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3468 * correctly increment MaxCmdSN for each response answering a
3469 * non immediate text request with a valid CmdSN.
3471 cmd
->maxcmdsn_inc
= 0;
3472 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3473 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3475 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3476 " Length: %u, CID: %hu F: %d C: %d\n", cmd
->init_task_tag
,
3477 cmd
->targ_xfer_tag
, cmd
->stat_sn
, text_length
, conn
->cid
,
3478 !!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
),
3479 !!(hdr
->flags
& ISCSI_FLAG_TEXT_CONTINUE
));
3481 return text_length
+ padding
;
3483 EXPORT_SYMBOL(iscsit_build_text_rsp
);
3485 static int iscsit_send_text_rsp(
3486 struct iscsi_cmd
*cmd
,
3487 struct iscsi_conn
*conn
)
3489 struct iscsi_text_rsp
*hdr
= (struct iscsi_text_rsp
*)cmd
->pdu
;
3492 text_length
= iscsit_build_text_rsp(cmd
, conn
, hdr
,
3493 conn
->conn_transport
->transport_type
);
3494 if (text_length
< 0)
3497 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
,
3503 iscsit_build_reject(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3504 struct iscsi_reject
*hdr
)
3506 hdr
->opcode
= ISCSI_OP_REJECT
;
3507 hdr
->reason
= cmd
->reject_reason
;
3508 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3509 hton24(hdr
->dlength
, ISCSI_HDR_LEN
);
3510 hdr
->ffffffff
= cpu_to_be32(0xffffffff);
3511 cmd
->stat_sn
= conn
->stat_sn
++;
3512 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3513 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3514 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3517 EXPORT_SYMBOL(iscsit_build_reject
);
3519 static int iscsit_send_reject(
3520 struct iscsi_cmd
*cmd
,
3521 struct iscsi_conn
*conn
)
3523 struct iscsi_reject
*hdr
= (struct iscsi_reject
*)&cmd
->pdu
[0];
3525 iscsit_build_reject(cmd
, conn
, hdr
);
3527 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3528 " CID: %hu\n", ntohl(hdr
->statsn
), hdr
->reason
, conn
->cid
);
3530 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
,
3535 void iscsit_thread_get_cpumask(struct iscsi_conn
*conn
)
3539 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3540 * within iscsit_start_kthreads()
3542 * Here we use bitmap_id to determine which CPU that this
3543 * iSCSI connection's RX/TX threads will be scheduled to
3546 ord
= conn
->bitmap_id
% cpumask_weight(cpu_online_mask
);
3547 for_each_online_cpu(cpu
) {
3549 cpumask_set_cpu(cpu
, conn
->conn_cpumask
);
3554 * This should never be reached..
3557 cpumask_setall(conn
->conn_cpumask
);
3561 iscsit_immediate_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3566 case ISTATE_SEND_R2T
:
3567 ret
= iscsit_send_r2t(cmd
, conn
);
3572 spin_lock_bh(&conn
->cmd_lock
);
3573 list_del_init(&cmd
->i_conn_node
);
3574 spin_unlock_bh(&conn
->cmd_lock
);
3576 iscsit_free_cmd(cmd
, false);
3578 case ISTATE_SEND_NOPIN_WANT_RESPONSE
:
3579 iscsit_mod_nopin_response_timer(conn
);
3580 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 1);
3584 case ISTATE_SEND_NOPIN_NO_RESPONSE
:
3585 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 0);
3590 pr_err("Unknown Opcode: 0x%02x ITT:"
3591 " 0x%08x, i_state: %d on CID: %hu\n",
3592 cmd
->iscsi_opcode
, cmd
->init_task_tag
, state
,
3602 EXPORT_SYMBOL(iscsit_immediate_queue
);
3605 iscsit_handle_immediate_queue(struct iscsi_conn
*conn
)
3607 struct iscsit_transport
*t
= conn
->conn_transport
;
3608 struct iscsi_queue_req
*qr
;
3609 struct iscsi_cmd
*cmd
;
3613 while ((qr
= iscsit_get_cmd_from_immediate_queue(conn
))) {
3614 atomic_set(&conn
->check_immediate_queue
, 0);
3617 kmem_cache_free(lio_qr_cache
, qr
);
3619 ret
= t
->iscsit_immediate_queue(conn
, cmd
, state
);
3628 iscsit_response_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3634 case ISTATE_SEND_DATAIN
:
3635 ret
= iscsit_send_datain(cmd
, conn
);
3640 goto check_rsp_state
;
3641 else if (ret
== 1) {
3643 spin_lock_bh(&cmd
->istate_lock
);
3644 cmd
->i_state
= ISTATE_SENT_STATUS
;
3645 spin_unlock_bh(&cmd
->istate_lock
);
3647 if (atomic_read(&conn
->check_immediate_queue
))
3651 } else if (ret
== 2) {
3652 /* Still must send status,
3653 SCF_TRANSPORT_TASK_SENSE was set */
3654 spin_lock_bh(&cmd
->istate_lock
);
3655 cmd
->i_state
= ISTATE_SEND_STATUS
;
3656 spin_unlock_bh(&cmd
->istate_lock
);
3657 state
= ISTATE_SEND_STATUS
;
3658 goto check_rsp_state
;
3662 case ISTATE_SEND_STATUS
:
3663 case ISTATE_SEND_STATUS_RECOVERY
:
3664 ret
= iscsit_send_response(cmd
, conn
);
3666 case ISTATE_SEND_LOGOUTRSP
:
3667 ret
= iscsit_send_logout(cmd
, conn
);
3669 case ISTATE_SEND_ASYNCMSG
:
3670 ret
= iscsit_send_conn_drop_async_message(
3673 case ISTATE_SEND_NOPIN
:
3674 ret
= iscsit_send_nopin(cmd
, conn
);
3676 case ISTATE_SEND_REJECT
:
3677 ret
= iscsit_send_reject(cmd
, conn
);
3679 case ISTATE_SEND_TASKMGTRSP
:
3680 ret
= iscsit_send_task_mgt_rsp(cmd
, conn
);
3683 ret
= iscsit_tmr_post_handler(cmd
, conn
);
3685 iscsit_fall_back_to_erl0(conn
->sess
);
3687 case ISTATE_SEND_TEXTRSP
:
3688 ret
= iscsit_send_text_rsp(cmd
, conn
);
3691 pr_err("Unknown Opcode: 0x%02x ITT:"
3692 " 0x%08x, i_state: %d on CID: %hu\n",
3693 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3701 case ISTATE_SEND_LOGOUTRSP
:
3702 if (!iscsit_logout_post_handler(cmd
, conn
))
3705 case ISTATE_SEND_STATUS
:
3706 case ISTATE_SEND_ASYNCMSG
:
3707 case ISTATE_SEND_NOPIN
:
3708 case ISTATE_SEND_STATUS_RECOVERY
:
3709 case ISTATE_SEND_TEXTRSP
:
3710 case ISTATE_SEND_TASKMGTRSP
:
3711 case ISTATE_SEND_REJECT
:
3712 spin_lock_bh(&cmd
->istate_lock
);
3713 cmd
->i_state
= ISTATE_SENT_STATUS
;
3714 spin_unlock_bh(&cmd
->istate_lock
);
3717 pr_err("Unknown Opcode: 0x%02x ITT:"
3718 " 0x%08x, i_state: %d on CID: %hu\n",
3719 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3720 cmd
->i_state
, conn
->cid
);
3724 if (atomic_read(&conn
->check_immediate_queue
))
3732 EXPORT_SYMBOL(iscsit_response_queue
);
3734 static int iscsit_handle_response_queue(struct iscsi_conn
*conn
)
3736 struct iscsit_transport
*t
= conn
->conn_transport
;
3737 struct iscsi_queue_req
*qr
;
3738 struct iscsi_cmd
*cmd
;
3742 while ((qr
= iscsit_get_cmd_from_response_queue(conn
))) {
3745 kmem_cache_free(lio_qr_cache
, qr
);
3747 ret
= t
->iscsit_response_queue(conn
, cmd
, state
);
3748 if (ret
== 1 || ret
< 0)
3755 int iscsi_target_tx_thread(void *arg
)
3758 struct iscsi_conn
*conn
= arg
;
3759 bool conn_freed
= false;
3762 * Allow ourselves to be interrupted by SIGINT so that a
3763 * connection recovery / failure event can be triggered externally.
3765 allow_signal(SIGINT
);
3767 while (!kthread_should_stop()) {
3769 * Ensure that both TX and RX per connection kthreads
3770 * are scheduled to run on the same CPU.
3772 iscsit_thread_check_cpumask(conn
, current
, 1);
3774 wait_event_interruptible(conn
->queues_wq
,
3775 !iscsit_conn_all_queues_empty(conn
));
3777 if (signal_pending(current
))
3781 ret
= iscsit_handle_immediate_queue(conn
);
3785 ret
= iscsit_handle_response_queue(conn
);
3788 } else if (ret
== -ECONNRESET
) {
3791 } else if (ret
< 0) {
3798 * Avoid the normal connection failure code-path if this connection
3799 * is still within LOGIN mode, and iscsi_np process context is
3800 * responsible for cleaning up the early connection failure.
3802 if (conn
->conn_state
!= TARG_CONN_STATE_IN_LOGIN
)
3803 iscsit_take_action_for_connection_exit(conn
, &conn_freed
);
3806 while (!kthread_should_stop()) {
3813 static int iscsi_target_rx_opcode(struct iscsi_conn
*conn
, unsigned char *buf
)
3815 struct iscsi_hdr
*hdr
= (struct iscsi_hdr
*)buf
;
3816 struct iscsi_cmd
*cmd
;
3819 switch (hdr
->opcode
& ISCSI_OPCODE_MASK
) {
3820 case ISCSI_OP_SCSI_CMD
:
3821 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3825 ret
= iscsit_handle_scsi_cmd(conn
, cmd
, buf
);
3827 case ISCSI_OP_SCSI_DATA_OUT
:
3828 ret
= iscsit_handle_data_out(conn
, buf
);
3830 case ISCSI_OP_NOOP_OUT
:
3832 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
3833 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3837 ret
= iscsit_handle_nop_out(conn
, cmd
, buf
);
3839 case ISCSI_OP_SCSI_TMFUNC
:
3840 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3844 ret
= iscsit_handle_task_mgt_cmd(conn
, cmd
, buf
);
3847 if (hdr
->ttt
!= cpu_to_be32(0xFFFFFFFF)) {
3848 cmd
= iscsit_find_cmd_from_itt(conn
, hdr
->itt
);
3852 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3857 ret
= iscsit_handle_text_cmd(conn
, cmd
, buf
);
3859 case ISCSI_OP_LOGOUT
:
3860 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3864 ret
= iscsit_handle_logout_cmd(conn
, cmd
, buf
);
3866 wait_for_completion_timeout(&conn
->conn_logout_comp
,
3867 SECONDS_FOR_LOGOUT_COMP
* HZ
);
3869 case ISCSI_OP_SNACK
:
3870 ret
= iscsit_handle_snack(conn
, buf
);
3873 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr
->opcode
);
3874 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
3875 pr_err("Cannot recover from unknown"
3876 " opcode while ERL=0, closing iSCSI connection.\n");
3879 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3880 " closing iSCSI connection.\n");
3887 return iscsit_add_reject(conn
, ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
3890 static bool iscsi_target_check_conn_state(struct iscsi_conn
*conn
)
3894 spin_lock_bh(&conn
->state_lock
);
3895 ret
= (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
);
3896 spin_unlock_bh(&conn
->state_lock
);
3901 static void iscsit_get_rx_pdu(struct iscsi_conn
*conn
)
3905 u32 checksum
= 0, digest
= 0;
3908 buffer
= kcalloc(ISCSI_HDR_LEN
, sizeof(*buffer
), GFP_KERNEL
);
3912 while (!kthread_should_stop()) {
3914 * Ensure that both TX and RX per connection kthreads
3915 * are scheduled to run on the same CPU.
3917 iscsit_thread_check_cpumask(conn
, current
, 0);
3919 memset(&iov
, 0, sizeof(struct kvec
));
3921 iov
.iov_base
= buffer
;
3922 iov
.iov_len
= ISCSI_HDR_LEN
;
3924 ret
= rx_data(conn
, &iov
, 1, ISCSI_HDR_LEN
);
3925 if (ret
!= ISCSI_HDR_LEN
) {
3926 iscsit_rx_thread_wait_for_tcp(conn
);
3930 if (conn
->conn_ops
->HeaderDigest
) {
3931 iov
.iov_base
= &digest
;
3932 iov
.iov_len
= ISCSI_CRC_LEN
;
3934 ret
= rx_data(conn
, &iov
, 1, ISCSI_CRC_LEN
);
3935 if (ret
!= ISCSI_CRC_LEN
) {
3936 iscsit_rx_thread_wait_for_tcp(conn
);
3940 iscsit_do_crypto_hash_buf(conn
->conn_rx_hash
, buffer
,
3941 ISCSI_HDR_LEN
, 0, NULL
,
3944 if (digest
!= checksum
) {
3945 pr_err("HeaderDigest CRC32C failed,"
3946 " received 0x%08x, computed 0x%08x\n",
3949 * Set the PDU to 0xff so it will intentionally
3950 * hit default in the switch below.
3952 memset(buffer
, 0xff, ISCSI_HDR_LEN
);
3953 atomic_long_inc(&conn
->sess
->conn_digest_errors
);
3955 pr_debug("Got HeaderDigest CRC32C"
3956 " 0x%08x\n", checksum
);
3960 if (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
)
3963 opcode
= buffer
[0] & ISCSI_OPCODE_MASK
;
3965 if (conn
->sess
->sess_ops
->SessionType
&&
3966 ((!(opcode
& ISCSI_OP_TEXT
)) ||
3967 (!(opcode
& ISCSI_OP_LOGOUT
)))) {
3968 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3969 " while in Discovery Session, rejecting.\n", opcode
);
3970 iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
3975 ret
= iscsi_target_rx_opcode(conn
, buffer
);
3983 int iscsi_target_rx_thread(void *arg
)
3986 struct iscsi_conn
*conn
= arg
;
3987 bool conn_freed
= false;
3990 * Allow ourselves to be interrupted by SIGINT so that a
3991 * connection recovery / failure event can be triggered externally.
3993 allow_signal(SIGINT
);
3995 * Wait for iscsi_post_login_handler() to complete before allowing
3996 * incoming iscsi/tcp socket I/O, and/or failing the connection.
3998 rc
= wait_for_completion_interruptible(&conn
->rx_login_comp
);
3999 if (rc
< 0 || iscsi_target_check_conn_state(conn
))
4002 if (!conn
->conn_transport
->iscsit_get_rx_pdu
)
4005 conn
->conn_transport
->iscsit_get_rx_pdu(conn
);
4007 if (!signal_pending(current
))
4008 atomic_set(&conn
->transport_failed
, 1);
4009 iscsit_take_action_for_connection_exit(conn
, &conn_freed
);
4013 while (!kthread_should_stop()) {
4021 static void iscsit_release_commands_from_conn(struct iscsi_conn
*conn
)
4023 LIST_HEAD(tmp_list
);
4024 struct iscsi_cmd
*cmd
= NULL
, *cmd_tmp
= NULL
;
4025 struct iscsi_session
*sess
= conn
->sess
;
4027 * We expect this function to only ever be called from either RX or TX
4028 * thread context via iscsit_close_connection() once the other context
4029 * has been reset -> returned sleeping pre-handler state.
4031 spin_lock_bh(&conn
->cmd_lock
);
4032 list_splice_init(&conn
->conn_cmd_list
, &tmp_list
);
4034 list_for_each_entry(cmd
, &tmp_list
, i_conn_node
) {
4035 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
4037 if (se_cmd
->se_tfo
!= NULL
) {
4038 spin_lock_irq(&se_cmd
->t_state_lock
);
4039 se_cmd
->transport_state
|= CMD_T_FABRIC_STOP
;
4040 spin_unlock_irq(&se_cmd
->t_state_lock
);
4043 spin_unlock_bh(&conn
->cmd_lock
);
4045 list_for_each_entry_safe(cmd
, cmd_tmp
, &tmp_list
, i_conn_node
) {
4046 list_del_init(&cmd
->i_conn_node
);
4048 iscsit_increment_maxcmdsn(cmd
, sess
);
4049 iscsit_free_cmd(cmd
, true);
4054 static void iscsit_stop_timers_for_cmds(
4055 struct iscsi_conn
*conn
)
4057 struct iscsi_cmd
*cmd
;
4059 spin_lock_bh(&conn
->cmd_lock
);
4060 list_for_each_entry(cmd
, &conn
->conn_cmd_list
, i_conn_node
) {
4061 if (cmd
->data_direction
== DMA_TO_DEVICE
)
4062 iscsit_stop_dataout_timer(cmd
);
4064 spin_unlock_bh(&conn
->cmd_lock
);
4067 int iscsit_close_connection(
4068 struct iscsi_conn
*conn
)
4070 int conn_logout
= (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
);
4071 struct iscsi_session
*sess
= conn
->sess
;
4073 pr_debug("Closing iSCSI connection CID %hu on SID:"
4074 " %u\n", conn
->cid
, sess
->sid
);
4076 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4077 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4078 * sleeping and the logout response never got sent because the
4079 * connection failed.
4081 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4082 * to signal logout response TX interrupt completion. Go ahead and skip
4083 * this for iser since isert_rx_opcode() does not wait on logout failure,
4084 * and to avoid iscsi_conn pointer dereference in iser-target code.
4086 if (!conn
->conn_transport
->rdma_shutdown
)
4087 complete(&conn
->conn_logout_comp
);
4089 if (!strcmp(current
->comm
, ISCSI_RX_THREAD_NAME
)) {
4090 if (conn
->tx_thread
&&
4091 cmpxchg(&conn
->tx_thread_active
, true, false)) {
4092 send_sig(SIGINT
, conn
->tx_thread
, 1);
4093 kthread_stop(conn
->tx_thread
);
4095 } else if (!strcmp(current
->comm
, ISCSI_TX_THREAD_NAME
)) {
4096 if (conn
->rx_thread
&&
4097 cmpxchg(&conn
->rx_thread_active
, true, false)) {
4098 send_sig(SIGINT
, conn
->rx_thread
, 1);
4099 kthread_stop(conn
->rx_thread
);
4103 spin_lock(&iscsit_global
->ts_bitmap_lock
);
4104 bitmap_release_region(iscsit_global
->ts_bitmap
, conn
->bitmap_id
,
4106 spin_unlock(&iscsit_global
->ts_bitmap_lock
);
4108 iscsit_stop_timers_for_cmds(conn
);
4109 iscsit_stop_nopin_response_timer(conn
);
4110 iscsit_stop_nopin_timer(conn
);
4112 if (conn
->conn_transport
->iscsit_wait_conn
)
4113 conn
->conn_transport
->iscsit_wait_conn(conn
);
4116 * During Connection recovery drop unacknowledged out of order
4117 * commands for this connection, and prepare the other commands
4120 * During normal operation clear the out of order commands (but
4121 * do not free the struct iscsi_ooo_cmdsn's) and release all
4122 * struct iscsi_cmds.
4124 if (atomic_read(&conn
->connection_recovery
)) {
4125 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn
);
4126 iscsit_prepare_cmds_for_reallegiance(conn
);
4128 iscsit_clear_ooo_cmdsns_for_conn(conn
);
4129 iscsit_release_commands_from_conn(conn
);
4131 iscsit_free_queue_reqs_for_conn(conn
);
4134 * Handle decrementing session or connection usage count if
4135 * a logout response was not able to be sent because the
4136 * connection failed. Fall back to Session Recovery here.
4138 if (atomic_read(&conn
->conn_logout_remove
)) {
4139 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) {
4140 iscsit_dec_conn_usage_count(conn
);
4141 iscsit_dec_session_usage_count(sess
);
4143 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
)
4144 iscsit_dec_conn_usage_count(conn
);
4146 atomic_set(&conn
->conn_logout_remove
, 0);
4147 atomic_set(&sess
->session_reinstatement
, 0);
4148 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4151 spin_lock_bh(&sess
->conn_lock
);
4152 list_del(&conn
->conn_list
);
4155 * Attempt to let the Initiator know this connection failed by
4156 * sending an Connection Dropped Async Message on another
4157 * active connection.
4159 if (atomic_read(&conn
->connection_recovery
))
4160 iscsit_build_conn_drop_async_message(conn
);
4162 spin_unlock_bh(&sess
->conn_lock
);
4165 * If connection reinstatement is being performed on this connection,
4166 * up the connection reinstatement semaphore that is being blocked on
4167 * in iscsit_cause_connection_reinstatement().
4169 spin_lock_bh(&conn
->state_lock
);
4170 if (atomic_read(&conn
->sleep_on_conn_wait_comp
)) {
4171 spin_unlock_bh(&conn
->state_lock
);
4172 complete(&conn
->conn_wait_comp
);
4173 wait_for_completion(&conn
->conn_post_wait_comp
);
4174 spin_lock_bh(&conn
->state_lock
);
4178 * If connection reinstatement is being performed on this connection
4179 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4180 * connection wait rcfr semaphore that is being blocked on
4181 * an iscsit_connection_reinstatement_rcfr().
4183 if (atomic_read(&conn
->connection_wait_rcfr
)) {
4184 spin_unlock_bh(&conn
->state_lock
);
4185 complete(&conn
->conn_wait_rcfr_comp
);
4186 wait_for_completion(&conn
->conn_post_wait_comp
);
4187 spin_lock_bh(&conn
->state_lock
);
4189 atomic_set(&conn
->connection_reinstatement
, 1);
4190 spin_unlock_bh(&conn
->state_lock
);
4193 * If any other processes are accessing this connection pointer we
4194 * must wait until they have completed.
4196 iscsit_check_conn_usage_count(conn
);
4198 ahash_request_free(conn
->conn_tx_hash
);
4199 if (conn
->conn_rx_hash
) {
4200 struct crypto_ahash
*tfm
;
4202 tfm
= crypto_ahash_reqtfm(conn
->conn_rx_hash
);
4203 ahash_request_free(conn
->conn_rx_hash
);
4204 crypto_free_ahash(tfm
);
4208 sock_release(conn
->sock
);
4210 if (conn
->conn_transport
->iscsit_free_conn
)
4211 conn
->conn_transport
->iscsit_free_conn(conn
);
4213 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4214 conn
->conn_state
= TARG_CONN_STATE_FREE
;
4215 iscsit_free_conn(conn
);
4217 spin_lock_bh(&sess
->conn_lock
);
4218 atomic_dec(&sess
->nconn
);
4219 pr_debug("Decremented iSCSI connection count to %hu from node:"
4220 " %s\n", atomic_read(&sess
->nconn
),
4221 sess
->sess_ops
->InitiatorName
);
4223 * Make sure that if one connection fails in an non ERL=2 iSCSI
4224 * Session that they all fail.
4226 if ((sess
->sess_ops
->ErrorRecoveryLevel
!= 2) && !conn_logout
&&
4227 !atomic_read(&sess
->session_logout
))
4228 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4231 * If this was not the last connection in the session, and we are
4232 * performing session reinstatement or falling back to ERL=0, call
4233 * iscsit_stop_session() without sleeping to shutdown the other
4234 * active connections.
4236 if (atomic_read(&sess
->nconn
)) {
4237 if (!atomic_read(&sess
->session_reinstatement
) &&
4238 !atomic_read(&sess
->session_fall_back_to_erl0
)) {
4239 spin_unlock_bh(&sess
->conn_lock
);
4242 if (!atomic_read(&sess
->session_stop_active
)) {
4243 atomic_set(&sess
->session_stop_active
, 1);
4244 spin_unlock_bh(&sess
->conn_lock
);
4245 iscsit_stop_session(sess
, 0, 0);
4248 spin_unlock_bh(&sess
->conn_lock
);
4253 * If this was the last connection in the session and one of the
4254 * following is occurring:
4256 * Session Reinstatement is not being performed, and are falling back
4257 * to ERL=0 call iscsit_close_session().
4259 * Session Logout was requested. iscsit_close_session() will be called
4262 * Session Continuation is not being performed, start the Time2Retain
4263 * handler and check if sleep_on_sess_wait_sem is active.
4265 if (!atomic_read(&sess
->session_reinstatement
) &&
4266 atomic_read(&sess
->session_fall_back_to_erl0
)) {
4267 spin_unlock_bh(&sess
->conn_lock
);
4268 iscsit_close_session(sess
);
4271 } else if (atomic_read(&sess
->session_logout
)) {
4272 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4273 sess
->session_state
= TARG_SESS_STATE_FREE
;
4274 spin_unlock_bh(&sess
->conn_lock
);
4276 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4277 complete(&sess
->session_wait_comp
);
4281 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4282 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4284 if (!atomic_read(&sess
->session_continuation
)) {
4285 spin_unlock_bh(&sess
->conn_lock
);
4286 iscsit_start_time2retain_handler(sess
);
4288 spin_unlock_bh(&sess
->conn_lock
);
4290 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4291 complete(&sess
->session_wait_comp
);
4298 * If the iSCSI Session for the iSCSI Initiator Node exists,
4299 * forcefully shutdown the iSCSI NEXUS.
4301 int iscsit_close_session(struct iscsi_session
*sess
)
4303 struct iscsi_portal_group
*tpg
= sess
->tpg
;
4304 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4306 if (atomic_read(&sess
->nconn
)) {
4307 pr_err("%d connection(s) still exist for iSCSI session"
4308 " to %s\n", atomic_read(&sess
->nconn
),
4309 sess
->sess_ops
->InitiatorName
);
4313 spin_lock_bh(&se_tpg
->session_lock
);
4314 atomic_set(&sess
->session_logout
, 1);
4315 atomic_set(&sess
->session_reinstatement
, 1);
4316 iscsit_stop_time2retain_timer(sess
);
4317 spin_unlock_bh(&se_tpg
->session_lock
);
4320 * transport_deregister_session_configfs() will clear the
4321 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4322 * can be setting it again with __transport_register_session() in
4323 * iscsi_post_login_handler() again after the iscsit_stop_session()
4324 * completes in iscsi_np context.
4326 transport_deregister_session_configfs(sess
->se_sess
);
4329 * If any other processes are accessing this session pointer we must
4330 * wait until they have completed. If we are in an interrupt (the
4331 * time2retain handler) and contain and active session usage count we
4332 * restart the timer and exit.
4334 if (!in_interrupt()) {
4335 if (iscsit_check_session_usage_count(sess
) == 1)
4336 iscsit_stop_session(sess
, 1, 1);
4338 if (iscsit_check_session_usage_count(sess
) == 2) {
4339 atomic_set(&sess
->session_logout
, 0);
4340 iscsit_start_time2retain_handler(sess
);
4345 transport_deregister_session(sess
->se_sess
);
4347 if (sess
->sess_ops
->ErrorRecoveryLevel
== 2)
4348 iscsit_free_connection_recovery_entries(sess
);
4350 iscsit_free_all_ooo_cmdsns(sess
);
4352 spin_lock_bh(&se_tpg
->session_lock
);
4353 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4354 sess
->session_state
= TARG_SESS_STATE_FREE
;
4355 pr_debug("Released iSCSI session from node: %s\n",
4356 sess
->sess_ops
->InitiatorName
);
4359 tpg
->tpg_tiqn
->tiqn_nsessions
--;
4361 pr_debug("Decremented number of active iSCSI Sessions on"
4362 " iSCSI TPG: %hu to %u\n", tpg
->tpgt
, tpg
->nsessions
);
4364 ida_free(&sess_ida
, sess
->session_index
);
4365 kfree(sess
->sess_ops
);
4366 sess
->sess_ops
= NULL
;
4367 spin_unlock_bh(&se_tpg
->session_lock
);
4373 static void iscsit_logout_post_handler_closesession(
4374 struct iscsi_conn
*conn
)
4376 struct iscsi_session
*sess
= conn
->sess
;
4379 * Traditional iscsi/tcp will invoke this logic from TX thread
4380 * context during session logout, so clear tx_thread_active and
4381 * sleep if iscsit_close_connection() has not already occured.
4383 * Since iser-target invokes this logic from it's own workqueue,
4384 * always sleep waiting for RX/TX thread shutdown to complete
4385 * within iscsit_close_connection().
4387 if (!conn
->conn_transport
->rdma_shutdown
) {
4388 sleep
= cmpxchg(&conn
->tx_thread_active
, true, false);
4393 atomic_set(&conn
->conn_logout_remove
, 0);
4394 complete(&conn
->conn_logout_comp
);
4396 iscsit_dec_conn_usage_count(conn
);
4397 iscsit_stop_session(sess
, sleep
, sleep
);
4398 iscsit_dec_session_usage_count(sess
);
4399 iscsit_close_session(sess
);
4402 static void iscsit_logout_post_handler_samecid(
4403 struct iscsi_conn
*conn
)
4407 if (!conn
->conn_transport
->rdma_shutdown
) {
4408 sleep
= cmpxchg(&conn
->tx_thread_active
, true, false);
4413 atomic_set(&conn
->conn_logout_remove
, 0);
4414 complete(&conn
->conn_logout_comp
);
4416 iscsit_cause_connection_reinstatement(conn
, sleep
);
4417 iscsit_dec_conn_usage_count(conn
);
4420 static void iscsit_logout_post_handler_diffcid(
4421 struct iscsi_conn
*conn
,
4424 struct iscsi_conn
*l_conn
;
4425 struct iscsi_session
*sess
= conn
->sess
;
4426 bool conn_found
= false;
4431 spin_lock_bh(&sess
->conn_lock
);
4432 list_for_each_entry(l_conn
, &sess
->sess_conn_list
, conn_list
) {
4433 if (l_conn
->cid
== cid
) {
4434 iscsit_inc_conn_usage_count(l_conn
);
4439 spin_unlock_bh(&sess
->conn_lock
);
4445 l_conn
->sock
->ops
->shutdown(l_conn
->sock
, RCV_SHUTDOWN
);
4447 spin_lock_bh(&l_conn
->state_lock
);
4448 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4449 l_conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
4450 spin_unlock_bh(&l_conn
->state_lock
);
4452 iscsit_cause_connection_reinstatement(l_conn
, 1);
4453 iscsit_dec_conn_usage_count(l_conn
);
4457 * Return of 0 causes the TX thread to restart.
4459 int iscsit_logout_post_handler(
4460 struct iscsi_cmd
*cmd
,
4461 struct iscsi_conn
*conn
)
4465 switch (cmd
->logout_reason
) {
4466 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
4467 switch (cmd
->logout_response
) {
4468 case ISCSI_LOGOUT_SUCCESS
:
4469 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4471 iscsit_logout_post_handler_closesession(conn
);
4476 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
4477 if (conn
->cid
== cmd
->logout_cid
) {
4478 switch (cmd
->logout_response
) {
4479 case ISCSI_LOGOUT_SUCCESS
:
4480 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4482 iscsit_logout_post_handler_samecid(conn
);
4487 switch (cmd
->logout_response
) {
4488 case ISCSI_LOGOUT_SUCCESS
:
4489 iscsit_logout_post_handler_diffcid(conn
,
4492 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4493 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4500 case ISCSI_LOGOUT_REASON_RECOVERY
:
4501 switch (cmd
->logout_response
) {
4502 case ISCSI_LOGOUT_SUCCESS
:
4503 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4504 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
:
4505 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4517 EXPORT_SYMBOL(iscsit_logout_post_handler
);
4519 void iscsit_fail_session(struct iscsi_session
*sess
)
4521 struct iscsi_conn
*conn
;
4523 spin_lock_bh(&sess
->conn_lock
);
4524 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
) {
4525 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4526 conn
->conn_state
= TARG_CONN_STATE_CLEANUP_WAIT
;
4528 spin_unlock_bh(&sess
->conn_lock
);
4530 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4531 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4534 int iscsit_free_session(struct iscsi_session
*sess
)
4536 u16 conn_count
= atomic_read(&sess
->nconn
);
4537 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4540 spin_lock_bh(&sess
->conn_lock
);
4541 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4543 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4545 if (conn_count
== 0)
4548 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4551 iscsit_inc_conn_usage_count(conn_tmp
);
4554 iscsit_inc_conn_usage_count(conn
);
4556 spin_unlock_bh(&sess
->conn_lock
);
4557 iscsit_cause_connection_reinstatement(conn
, 1);
4558 spin_lock_bh(&sess
->conn_lock
);
4560 iscsit_dec_conn_usage_count(conn
);
4562 iscsit_dec_conn_usage_count(conn_tmp
);
4567 if (atomic_read(&sess
->nconn
)) {
4568 spin_unlock_bh(&sess
->conn_lock
);
4569 wait_for_completion(&sess
->session_wait_comp
);
4571 spin_unlock_bh(&sess
->conn_lock
);
4573 iscsit_close_session(sess
);
4577 void iscsit_stop_session(
4578 struct iscsi_session
*sess
,
4580 int connection_sleep
)
4582 u16 conn_count
= atomic_read(&sess
->nconn
);
4583 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4586 spin_lock_bh(&sess
->conn_lock
);
4588 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4590 if (connection_sleep
) {
4591 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4593 if (conn_count
== 0)
4596 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4599 iscsit_inc_conn_usage_count(conn_tmp
);
4602 iscsit_inc_conn_usage_count(conn
);
4604 spin_unlock_bh(&sess
->conn_lock
);
4605 iscsit_cause_connection_reinstatement(conn
, 1);
4606 spin_lock_bh(&sess
->conn_lock
);
4608 iscsit_dec_conn_usage_count(conn
);
4610 iscsit_dec_conn_usage_count(conn_tmp
);
4614 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
)
4615 iscsit_cause_connection_reinstatement(conn
, 0);
4618 if (session_sleep
&& atomic_read(&sess
->nconn
)) {
4619 spin_unlock_bh(&sess
->conn_lock
);
4620 wait_for_completion(&sess
->session_wait_comp
);
4622 spin_unlock_bh(&sess
->conn_lock
);
4625 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group
*tpg
, int force
)
4627 struct iscsi_session
*sess
;
4628 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4629 struct se_session
*se_sess
, *se_sess_tmp
;
4630 LIST_HEAD(free_list
);
4631 int session_count
= 0;
4633 spin_lock_bh(&se_tpg
->session_lock
);
4634 if (tpg
->nsessions
&& !force
) {
4635 spin_unlock_bh(&se_tpg
->session_lock
);
4639 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &se_tpg
->tpg_sess_list
,
4641 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4643 spin_lock(&sess
->conn_lock
);
4644 if (atomic_read(&sess
->session_fall_back_to_erl0
) ||
4645 atomic_read(&sess
->session_logout
) ||
4646 (sess
->time2retain_timer_flags
& ISCSI_TF_EXPIRED
)) {
4647 spin_unlock(&sess
->conn_lock
);
4650 atomic_set(&sess
->session_reinstatement
, 1);
4651 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4652 spin_unlock(&sess
->conn_lock
);
4654 list_move_tail(&se_sess
->sess_list
, &free_list
);
4656 spin_unlock_bh(&se_tpg
->session_lock
);
4658 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &free_list
, sess_list
) {
4659 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4661 iscsit_free_session(sess
);
4665 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4666 " Group: %hu\n", session_count
, tpg
->tpgt
);
4670 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4671 MODULE_VERSION("4.1.x");
4672 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4673 MODULE_LICENSE("GPL");
4675 module_init(iscsi_target_init_module
);
4676 module_exit(iscsi_target_cleanup_module
);