1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * This file contains main functions related to the iSCSI Target Core Driver.
5 * (c) Copyright 2007-2013 Datera, Inc.
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9 ******************************************************************************/
11 #include <crypto/hash.h>
12 #include <linux/string.h>
13 #include <linux/kthread.h>
14 #include <linux/completion.h>
15 #include <linux/module.h>
16 #include <linux/vmalloc.h>
17 #include <linux/idr.h>
18 #include <linux/delay.h>
19 #include <linux/sched/signal.h>
20 #include <asm/unaligned.h>
21 #include <linux/inet.h>
23 #include <scsi/scsi_proto.h>
24 #include <scsi/iscsi_proto.h>
25 #include <scsi/scsi_tcq.h>
26 #include <target/target_core_base.h>
27 #include <target/target_core_fabric.h>
29 #include <target/iscsi/iscsi_target_core.h>
30 #include "iscsi_target_parameters.h"
31 #include "iscsi_target_seq_pdu_list.h"
32 #include "iscsi_target_datain_values.h"
33 #include "iscsi_target_erl0.h"
34 #include "iscsi_target_erl1.h"
35 #include "iscsi_target_erl2.h"
36 #include "iscsi_target_login.h"
37 #include "iscsi_target_tmr.h"
38 #include "iscsi_target_tpg.h"
39 #include "iscsi_target_util.h"
40 #include "iscsi_target.h"
41 #include "iscsi_target_device.h"
42 #include <target/iscsi/iscsi_target_stat.h>
44 #include <target/iscsi/iscsi_transport.h>
46 static LIST_HEAD(g_tiqn_list
);
47 static LIST_HEAD(g_np_list
);
48 static DEFINE_SPINLOCK(tiqn_lock
);
49 static DEFINE_MUTEX(np_lock
);
51 static struct idr tiqn_idr
;
53 struct mutex auth_id_lock
;
55 struct iscsit_global
*iscsit_global
;
57 struct kmem_cache
*lio_qr_cache
;
58 struct kmem_cache
*lio_dr_cache
;
59 struct kmem_cache
*lio_ooo_cache
;
60 struct kmem_cache
*lio_r2t_cache
;
62 static int iscsit_handle_immediate_data(struct iscsi_cmd
*,
63 struct iscsi_scsi_req
*, u32
);
65 struct iscsi_tiqn
*iscsit_get_tiqn_for_login(unsigned char *buf
)
67 struct iscsi_tiqn
*tiqn
= NULL
;
69 spin_lock(&tiqn_lock
);
70 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
71 if (!strcmp(tiqn
->tiqn
, buf
)) {
73 spin_lock(&tiqn
->tiqn_state_lock
);
74 if (tiqn
->tiqn_state
== TIQN_STATE_ACTIVE
) {
75 tiqn
->tiqn_access_count
++;
76 spin_unlock(&tiqn
->tiqn_state_lock
);
77 spin_unlock(&tiqn_lock
);
80 spin_unlock(&tiqn
->tiqn_state_lock
);
83 spin_unlock(&tiqn_lock
);
88 static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn
*tiqn
)
90 spin_lock(&tiqn
->tiqn_state_lock
);
91 if (tiqn
->tiqn_state
== TIQN_STATE_ACTIVE
) {
92 tiqn
->tiqn_state
= TIQN_STATE_SHUTDOWN
;
93 spin_unlock(&tiqn
->tiqn_state_lock
);
96 spin_unlock(&tiqn
->tiqn_state_lock
);
101 void iscsit_put_tiqn_for_login(struct iscsi_tiqn
*tiqn
)
103 spin_lock(&tiqn
->tiqn_state_lock
);
104 tiqn
->tiqn_access_count
--;
105 spin_unlock(&tiqn
->tiqn_state_lock
);
109 * Note that IQN formatting is expected to be done in userspace, and
110 * no explict IQN format checks are done here.
112 struct iscsi_tiqn
*iscsit_add_tiqn(unsigned char *buf
)
114 struct iscsi_tiqn
*tiqn
= NULL
;
117 if (strlen(buf
) >= ISCSI_IQN_LEN
) {
118 pr_err("Target IQN exceeds %d bytes\n",
120 return ERR_PTR(-EINVAL
);
123 tiqn
= kzalloc(sizeof(*tiqn
), GFP_KERNEL
);
125 return ERR_PTR(-ENOMEM
);
127 sprintf(tiqn
->tiqn
, "%s", buf
);
128 INIT_LIST_HEAD(&tiqn
->tiqn_list
);
129 INIT_LIST_HEAD(&tiqn
->tiqn_tpg_list
);
130 spin_lock_init(&tiqn
->tiqn_state_lock
);
131 spin_lock_init(&tiqn
->tiqn_tpg_lock
);
132 spin_lock_init(&tiqn
->sess_err_stats
.lock
);
133 spin_lock_init(&tiqn
->login_stats
.lock
);
134 spin_lock_init(&tiqn
->logout_stats
.lock
);
136 tiqn
->tiqn_state
= TIQN_STATE_ACTIVE
;
138 idr_preload(GFP_KERNEL
);
139 spin_lock(&tiqn_lock
);
141 ret
= idr_alloc(&tiqn_idr
, NULL
, 0, 0, GFP_NOWAIT
);
143 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
144 spin_unlock(&tiqn_lock
);
149 tiqn
->tiqn_index
= ret
;
150 list_add_tail(&tiqn
->tiqn_list
, &g_tiqn_list
);
152 spin_unlock(&tiqn_lock
);
155 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn
->tiqn
);
161 static void iscsit_wait_for_tiqn(struct iscsi_tiqn
*tiqn
)
164 * Wait for accesses to said struct iscsi_tiqn to end.
166 spin_lock(&tiqn
->tiqn_state_lock
);
167 while (tiqn
->tiqn_access_count
!= 0) {
168 spin_unlock(&tiqn
->tiqn_state_lock
);
170 spin_lock(&tiqn
->tiqn_state_lock
);
172 spin_unlock(&tiqn
->tiqn_state_lock
);
175 void iscsit_del_tiqn(struct iscsi_tiqn
*tiqn
)
178 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
179 * while holding tiqn->tiqn_state_lock. This means that all subsequent
180 * attempts to access this struct iscsi_tiqn will fail from both transport
181 * fabric and control code paths.
183 if (iscsit_set_tiqn_shutdown(tiqn
) < 0) {
184 pr_err("iscsit_set_tiqn_shutdown() failed\n");
188 iscsit_wait_for_tiqn(tiqn
);
190 spin_lock(&tiqn_lock
);
191 list_del(&tiqn
->tiqn_list
);
192 idr_remove(&tiqn_idr
, tiqn
->tiqn_index
);
193 spin_unlock(&tiqn_lock
);
195 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
200 int iscsit_access_np(struct iscsi_np
*np
, struct iscsi_portal_group
*tpg
)
204 * Determine if the network portal is accepting storage traffic.
206 spin_lock_bh(&np
->np_thread_lock
);
207 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
208 spin_unlock_bh(&np
->np_thread_lock
);
211 spin_unlock_bh(&np
->np_thread_lock
);
213 * Determine if the portal group is accepting storage traffic.
215 spin_lock_bh(&tpg
->tpg_state_lock
);
216 if (tpg
->tpg_state
!= TPG_STATE_ACTIVE
) {
217 spin_unlock_bh(&tpg
->tpg_state_lock
);
220 spin_unlock_bh(&tpg
->tpg_state_lock
);
223 * Here we serialize access across the TIQN+TPG Tuple.
225 ret
= down_interruptible(&tpg
->np_login_sem
);
229 spin_lock_bh(&tpg
->tpg_state_lock
);
230 if (tpg
->tpg_state
!= TPG_STATE_ACTIVE
) {
231 spin_unlock_bh(&tpg
->tpg_state_lock
);
232 up(&tpg
->np_login_sem
);
235 spin_unlock_bh(&tpg
->tpg_state_lock
);
240 void iscsit_login_kref_put(struct kref
*kref
)
242 struct iscsi_tpg_np
*tpg_np
= container_of(kref
,
243 struct iscsi_tpg_np
, tpg_np_kref
);
245 complete(&tpg_np
->tpg_np_comp
);
248 int iscsit_deaccess_np(struct iscsi_np
*np
, struct iscsi_portal_group
*tpg
,
249 struct iscsi_tpg_np
*tpg_np
)
251 struct iscsi_tiqn
*tiqn
= tpg
->tpg_tiqn
;
253 up(&tpg
->np_login_sem
);
256 kref_put(&tpg_np
->tpg_np_kref
, iscsit_login_kref_put
);
259 iscsit_put_tiqn_for_login(tiqn
);
264 bool iscsit_check_np_match(
265 struct sockaddr_storage
*sockaddr
,
267 int network_transport
)
269 struct sockaddr_in
*sock_in
, *sock_in_e
;
270 struct sockaddr_in6
*sock_in6
, *sock_in6_e
;
271 bool ip_match
= false;
274 if (sockaddr
->ss_family
== AF_INET6
) {
275 sock_in6
= (struct sockaddr_in6
*)sockaddr
;
276 sock_in6_e
= (struct sockaddr_in6
*)&np
->np_sockaddr
;
278 if (!memcmp(&sock_in6
->sin6_addr
.in6_u
,
279 &sock_in6_e
->sin6_addr
.in6_u
,
280 sizeof(struct in6_addr
)))
283 port
= ntohs(sock_in6
->sin6_port
);
284 port_e
= ntohs(sock_in6_e
->sin6_port
);
286 sock_in
= (struct sockaddr_in
*)sockaddr
;
287 sock_in_e
= (struct sockaddr_in
*)&np
->np_sockaddr
;
289 if (sock_in
->sin_addr
.s_addr
== sock_in_e
->sin_addr
.s_addr
)
292 port
= ntohs(sock_in
->sin_port
);
293 port_e
= ntohs(sock_in_e
->sin_port
);
296 if (ip_match
&& (port_e
== port
) &&
297 (np
->np_network_transport
== network_transport
))
303 static struct iscsi_np
*iscsit_get_np(
304 struct sockaddr_storage
*sockaddr
,
305 int network_transport
)
310 lockdep_assert_held(&np_lock
);
312 list_for_each_entry(np
, &g_np_list
, np_list
) {
313 spin_lock_bh(&np
->np_thread_lock
);
314 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
315 spin_unlock_bh(&np
->np_thread_lock
);
319 match
= iscsit_check_np_match(sockaddr
, np
, network_transport
);
322 * Increment the np_exports reference count now to
323 * prevent iscsit_del_np() below from being called
324 * while iscsi_tpg_add_network_portal() is called.
327 spin_unlock_bh(&np
->np_thread_lock
);
330 spin_unlock_bh(&np
->np_thread_lock
);
336 struct iscsi_np
*iscsit_add_np(
337 struct sockaddr_storage
*sockaddr
,
338 int network_transport
)
343 mutex_lock(&np_lock
);
346 * Locate the existing struct iscsi_np if already active..
348 np
= iscsit_get_np(sockaddr
, network_transport
);
350 mutex_unlock(&np_lock
);
354 np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
356 mutex_unlock(&np_lock
);
357 return ERR_PTR(-ENOMEM
);
360 np
->np_flags
|= NPF_IP_NETWORK
;
361 np
->np_network_transport
= network_transport
;
362 spin_lock_init(&np
->np_thread_lock
);
363 init_completion(&np
->np_restart_comp
);
364 INIT_LIST_HEAD(&np
->np_list
);
366 timer_setup(&np
->np_login_timer
, iscsi_handle_login_thread_timeout
, 0);
368 ret
= iscsi_target_setup_login_socket(np
, sockaddr
);
371 mutex_unlock(&np_lock
);
375 np
->np_thread
= kthread_run(iscsi_target_login_thread
, np
, "iscsi_np");
376 if (IS_ERR(np
->np_thread
)) {
377 pr_err("Unable to create kthread: iscsi_np\n");
378 ret
= PTR_ERR(np
->np_thread
);
380 mutex_unlock(&np_lock
);
384 * Increment the np_exports reference count now to prevent
385 * iscsit_del_np() below from being run while a new call to
386 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
387 * active. We don't need to hold np->np_thread_lock at this
388 * point because iscsi_np has not been added to g_np_list yet.
391 np
->np_thread_state
= ISCSI_NP_THREAD_ACTIVE
;
393 list_add_tail(&np
->np_list
, &g_np_list
);
394 mutex_unlock(&np_lock
);
396 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
397 &np
->np_sockaddr
, np
->np_transport
->name
);
402 int iscsit_reset_np_thread(
404 struct iscsi_tpg_np
*tpg_np
,
405 struct iscsi_portal_group
*tpg
,
408 spin_lock_bh(&np
->np_thread_lock
);
409 if (np
->np_thread_state
== ISCSI_NP_THREAD_INACTIVE
) {
410 spin_unlock_bh(&np
->np_thread_lock
);
413 np
->np_thread_state
= ISCSI_NP_THREAD_RESET
;
414 atomic_inc(&np
->np_reset_count
);
417 spin_unlock_bh(&np
->np_thread_lock
);
418 send_sig(SIGINT
, np
->np_thread
, 1);
419 wait_for_completion(&np
->np_restart_comp
);
420 spin_lock_bh(&np
->np_thread_lock
);
422 spin_unlock_bh(&np
->np_thread_lock
);
424 if (tpg_np
&& shutdown
) {
425 kref_put(&tpg_np
->tpg_np_kref
, iscsit_login_kref_put
);
427 wait_for_completion(&tpg_np
->tpg_np_comp
);
433 static void iscsit_free_np(struct iscsi_np
*np
)
436 sock_release(np
->np_socket
);
439 int iscsit_del_np(struct iscsi_np
*np
)
441 spin_lock_bh(&np
->np_thread_lock
);
443 if (np
->np_exports
) {
445 spin_unlock_bh(&np
->np_thread_lock
);
448 np
->np_thread_state
= ISCSI_NP_THREAD_SHUTDOWN
;
449 spin_unlock_bh(&np
->np_thread_lock
);
453 * We need to send the signal to wakeup Linux/Net
454 * which may be sleeping in sock_accept()..
456 send_sig(SIGINT
, np
->np_thread
, 1);
457 kthread_stop(np
->np_thread
);
458 np
->np_thread
= NULL
;
461 np
->np_transport
->iscsit_free_np(np
);
463 mutex_lock(&np_lock
);
464 list_del(&np
->np_list
);
465 mutex_unlock(&np_lock
);
467 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
468 &np
->np_sockaddr
, np
->np_transport
->name
);
470 iscsit_put_transport(np
->np_transport
);
475 static void iscsit_get_rx_pdu(struct iscsi_conn
*);
477 int iscsit_queue_rsp(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
)
479 return iscsit_add_cmd_to_response_queue(cmd
, cmd
->conn
, cmd
->i_state
);
481 EXPORT_SYMBOL(iscsit_queue_rsp
);
483 void iscsit_aborted_task(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
)
485 spin_lock_bh(&conn
->cmd_lock
);
486 if (!list_empty(&cmd
->i_conn_node
))
487 list_del_init(&cmd
->i_conn_node
);
488 spin_unlock_bh(&conn
->cmd_lock
);
490 __iscsit_free_cmd(cmd
, true);
492 EXPORT_SYMBOL(iscsit_aborted_task
);
494 static void iscsit_do_crypto_hash_buf(struct ahash_request
*, const void *,
495 u32
, u32
, const void *, void *);
496 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn
*);
499 iscsit_xmit_nondatain_pdu(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
500 const void *data_buf
, u32 data_buf_len
)
502 struct iscsi_hdr
*hdr
= (struct iscsi_hdr
*)cmd
->pdu
;
504 u32 niov
= 0, tx_size
= ISCSI_HDR_LEN
;
507 iov
= &cmd
->iov_misc
[0];
508 iov
[niov
].iov_base
= cmd
->pdu
;
509 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
511 if (conn
->conn_ops
->HeaderDigest
) {
512 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
514 iscsit_do_crypto_hash_buf(conn
->conn_tx_hash
, hdr
,
515 ISCSI_HDR_LEN
, 0, NULL
,
518 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
519 tx_size
+= ISCSI_CRC_LEN
;
520 pr_debug("Attaching CRC32C HeaderDigest"
521 " to opcode 0x%x 0x%08x\n",
522 hdr
->opcode
, *header_digest
);
526 u32 padding
= ((-data_buf_len
) & 3);
528 iov
[niov
].iov_base
= (void *)data_buf
;
529 iov
[niov
++].iov_len
= data_buf_len
;
530 tx_size
+= data_buf_len
;
533 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
534 iov
[niov
++].iov_len
= padding
;
536 pr_debug("Attaching %u additional"
537 " padding bytes.\n", padding
);
540 if (conn
->conn_ops
->DataDigest
) {
541 iscsit_do_crypto_hash_buf(conn
->conn_tx_hash
,
542 data_buf
, data_buf_len
,
543 padding
, &cmd
->pad_bytes
,
546 iov
[niov
].iov_base
= &cmd
->data_crc
;
547 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
548 tx_size
+= ISCSI_CRC_LEN
;
549 pr_debug("Attached DataDigest for %u"
550 " bytes opcode 0x%x, CRC 0x%08x\n",
551 data_buf_len
, hdr
->opcode
, cmd
->data_crc
);
555 cmd
->iov_misc_count
= niov
;
556 cmd
->tx_size
= tx_size
;
558 ret
= iscsit_send_tx_data(cmd
, conn
, 1);
560 iscsit_tx_thread_wait_for_tcp(conn
);
567 static int iscsit_map_iovec(struct iscsi_cmd
*cmd
, struct kvec
*iov
, int nvec
,
568 u32 data_offset
, u32 data_length
);
569 static void iscsit_unmap_iovec(struct iscsi_cmd
*);
570 static u32
iscsit_do_crypto_hash_sg(struct ahash_request
*, struct iscsi_cmd
*,
571 u32
, u32
, u32
, u8
*);
573 iscsit_xmit_datain_pdu(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
574 const struct iscsi_datain
*datain
)
577 u32 iov_count
= 0, tx_size
= 0;
580 iov
= &cmd
->iov_data
[0];
581 iov
[iov_count
].iov_base
= cmd
->pdu
;
582 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
583 tx_size
+= ISCSI_HDR_LEN
;
585 if (conn
->conn_ops
->HeaderDigest
) {
586 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
588 iscsit_do_crypto_hash_buf(conn
->conn_tx_hash
, cmd
->pdu
,
589 ISCSI_HDR_LEN
, 0, NULL
,
592 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
593 tx_size
+= ISCSI_CRC_LEN
;
595 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
599 iov_ret
= iscsit_map_iovec(cmd
, &cmd
->iov_data
[iov_count
],
600 cmd
->orig_iov_data_count
- (iov_count
+ 2),
601 datain
->offset
, datain
->length
);
605 iov_count
+= iov_ret
;
606 tx_size
+= datain
->length
;
608 cmd
->padding
= ((-datain
->length
) & 3);
610 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
611 iov
[iov_count
++].iov_len
= cmd
->padding
;
612 tx_size
+= cmd
->padding
;
614 pr_debug("Attaching %u padding bytes\n", cmd
->padding
);
617 if (conn
->conn_ops
->DataDigest
) {
618 cmd
->data_crc
= iscsit_do_crypto_hash_sg(conn
->conn_tx_hash
,
624 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
625 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
626 tx_size
+= ISCSI_CRC_LEN
;
628 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
629 datain
->length
+ cmd
->padding
, cmd
->data_crc
);
632 cmd
->iov_data_count
= iov_count
;
633 cmd
->tx_size
= tx_size
;
635 ret
= iscsit_fe_sendpage_sg(cmd
, conn
);
637 iscsit_unmap_iovec(cmd
);
640 iscsit_tx_thread_wait_for_tcp(conn
);
647 static int iscsit_xmit_pdu(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
648 struct iscsi_datain_req
*dr
, const void *buf
,
652 return iscsit_xmit_datain_pdu(conn
, cmd
, buf
);
654 return iscsit_xmit_nondatain_pdu(conn
, cmd
, buf
, buf_len
);
657 static enum target_prot_op
iscsit_get_sup_prot_ops(struct iscsi_conn
*conn
)
659 return TARGET_PROT_NORMAL
;
662 static struct iscsit_transport iscsi_target_transport
= {
664 .transport_type
= ISCSI_TCP
,
665 .rdma_shutdown
= false,
667 .iscsit_setup_np
= iscsit_setup_np
,
668 .iscsit_accept_np
= iscsit_accept_np
,
669 .iscsit_free_np
= iscsit_free_np
,
670 .iscsit_get_login_rx
= iscsit_get_login_rx
,
671 .iscsit_put_login_tx
= iscsit_put_login_tx
,
672 .iscsit_get_dataout
= iscsit_build_r2ts_for_cmd
,
673 .iscsit_immediate_queue
= iscsit_immediate_queue
,
674 .iscsit_response_queue
= iscsit_response_queue
,
675 .iscsit_queue_data_in
= iscsit_queue_rsp
,
676 .iscsit_queue_status
= iscsit_queue_rsp
,
677 .iscsit_aborted_task
= iscsit_aborted_task
,
678 .iscsit_xmit_pdu
= iscsit_xmit_pdu
,
679 .iscsit_get_rx_pdu
= iscsit_get_rx_pdu
,
680 .iscsit_get_sup_prot_ops
= iscsit_get_sup_prot_ops
,
683 static int __init
iscsi_target_init_module(void)
687 pr_debug("iSCSI-Target "ISCSIT_VERSION
"\n");
688 iscsit_global
= kzalloc(sizeof(*iscsit_global
), GFP_KERNEL
);
692 spin_lock_init(&iscsit_global
->ts_bitmap_lock
);
693 mutex_init(&auth_id_lock
);
696 ret
= target_register_template(&iscsi_ops
);
700 size
= BITS_TO_LONGS(ISCSIT_BITMAP_BITS
) * sizeof(long);
701 iscsit_global
->ts_bitmap
= vzalloc(size
);
702 if (!iscsit_global
->ts_bitmap
)
705 lio_qr_cache
= kmem_cache_create("lio_qr_cache",
706 sizeof(struct iscsi_queue_req
),
707 __alignof__(struct iscsi_queue_req
), 0, NULL
);
709 pr_err("Unable to kmem_cache_create() for"
714 lio_dr_cache
= kmem_cache_create("lio_dr_cache",
715 sizeof(struct iscsi_datain_req
),
716 __alignof__(struct iscsi_datain_req
), 0, NULL
);
718 pr_err("Unable to kmem_cache_create() for"
723 lio_ooo_cache
= kmem_cache_create("lio_ooo_cache",
724 sizeof(struct iscsi_ooo_cmdsn
),
725 __alignof__(struct iscsi_ooo_cmdsn
), 0, NULL
);
726 if (!lio_ooo_cache
) {
727 pr_err("Unable to kmem_cache_create() for"
732 lio_r2t_cache
= kmem_cache_create("lio_r2t_cache",
733 sizeof(struct iscsi_r2t
), __alignof__(struct iscsi_r2t
),
735 if (!lio_r2t_cache
) {
736 pr_err("Unable to kmem_cache_create() for"
741 iscsit_register_transport(&iscsi_target_transport
);
743 if (iscsit_load_discovery_tpg() < 0)
748 iscsit_unregister_transport(&iscsi_target_transport
);
749 kmem_cache_destroy(lio_r2t_cache
);
751 kmem_cache_destroy(lio_ooo_cache
);
753 kmem_cache_destroy(lio_dr_cache
);
755 kmem_cache_destroy(lio_qr_cache
);
757 vfree(iscsit_global
->ts_bitmap
);
759 /* XXX: this probably wants it to be it's own unwind step.. */
760 if (iscsit_global
->discovery_tpg
)
761 iscsit_tpg_disable_portal_group(iscsit_global
->discovery_tpg
, 1);
762 target_unregister_template(&iscsi_ops
);
764 kfree(iscsit_global
);
768 static void __exit
iscsi_target_cleanup_module(void)
770 iscsit_release_discovery_tpg();
771 iscsit_unregister_transport(&iscsi_target_transport
);
772 kmem_cache_destroy(lio_qr_cache
);
773 kmem_cache_destroy(lio_dr_cache
);
774 kmem_cache_destroy(lio_ooo_cache
);
775 kmem_cache_destroy(lio_r2t_cache
);
778 * Shutdown discovery sessions and disable discovery TPG
780 if (iscsit_global
->discovery_tpg
)
781 iscsit_tpg_disable_portal_group(iscsit_global
->discovery_tpg
, 1);
783 target_unregister_template(&iscsi_ops
);
785 vfree(iscsit_global
->ts_bitmap
);
786 kfree(iscsit_global
);
789 int iscsit_add_reject(
790 struct iscsi_conn
*conn
,
794 struct iscsi_cmd
*cmd
;
796 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
800 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
801 cmd
->reject_reason
= reason
;
803 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
805 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
806 iscsit_free_cmd(cmd
, false);
810 spin_lock_bh(&conn
->cmd_lock
);
811 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
812 spin_unlock_bh(&conn
->cmd_lock
);
814 cmd
->i_state
= ISTATE_SEND_REJECT
;
815 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
819 EXPORT_SYMBOL(iscsit_add_reject
);
821 static int iscsit_add_reject_from_cmd(
822 struct iscsi_cmd
*cmd
,
827 struct iscsi_conn
*conn
;
828 const bool do_put
= cmd
->se_cmd
.se_tfo
!= NULL
;
831 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
837 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
838 cmd
->reject_reason
= reason
;
840 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
842 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
843 iscsit_free_cmd(cmd
, false);
848 spin_lock_bh(&conn
->cmd_lock
);
849 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
850 spin_unlock_bh(&conn
->cmd_lock
);
853 cmd
->i_state
= ISTATE_SEND_REJECT
;
854 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
856 * Perform the kref_put now if se_cmd has already been setup by
857 * scsit_setup_scsi_cmd()
860 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
861 target_put_sess_cmd(&cmd
->se_cmd
);
866 static int iscsit_add_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
,
869 return iscsit_add_reject_from_cmd(cmd
, reason
, true, buf
);
872 int iscsit_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
, unsigned char *buf
)
874 return iscsit_add_reject_from_cmd(cmd
, reason
, false, buf
);
876 EXPORT_SYMBOL(iscsit_reject_cmd
);
879 * Map some portion of the allocated scatterlist to an iovec, suitable for
880 * kernel sockets to copy data in/out.
882 static int iscsit_map_iovec(struct iscsi_cmd
*cmd
, struct kvec
*iov
, int nvec
,
883 u32 data_offset
, u32 data_length
)
885 u32 i
= 0, orig_data_length
= data_length
;
886 struct scatterlist
*sg
;
887 unsigned int page_off
;
890 * We know each entry in t_data_sg contains a page.
892 u32 ent
= data_offset
/ PAGE_SIZE
;
897 if (ent
>= cmd
->se_cmd
.t_data_nents
) {
898 pr_err("Initial page entry out-of-bounds\n");
902 sg
= &cmd
->se_cmd
.t_data_sg
[ent
];
903 page_off
= (data_offset
% PAGE_SIZE
);
905 cmd
->first_data_sg
= sg
;
906 cmd
->first_data_sg_off
= page_off
;
908 while (data_length
) {
911 if (WARN_ON_ONCE(!sg
|| i
>= nvec
))
914 cur_len
= min_t(u32
, data_length
, sg
->length
- page_off
);
916 iov
[i
].iov_base
= kmap(sg_page(sg
)) + sg
->offset
+ page_off
;
917 iov
[i
].iov_len
= cur_len
;
919 data_length
-= cur_len
;
925 cmd
->kmapped_nents
= i
;
930 pr_err("offset %d + length %d overflow; %d/%d; sg-list:\n",
931 data_offset
, orig_data_length
, i
, nvec
);
932 for_each_sg(cmd
->se_cmd
.t_data_sg
, sg
,
933 cmd
->se_cmd
.t_data_nents
, i
) {
934 pr_err("[%d] off %d len %d\n",
935 i
, sg
->offset
, sg
->length
);
940 static void iscsit_unmap_iovec(struct iscsi_cmd
*cmd
)
943 struct scatterlist
*sg
;
945 sg
= cmd
->first_data_sg
;
947 for (i
= 0; i
< cmd
->kmapped_nents
; i
++)
948 kunmap(sg_page(&sg
[i
]));
951 static void iscsit_ack_from_expstatsn(struct iscsi_conn
*conn
, u32 exp_statsn
)
954 struct iscsi_cmd
*cmd
, *cmd_p
;
956 conn
->exp_statsn
= exp_statsn
;
958 if (conn
->sess
->sess_ops
->RDMAExtensions
)
961 spin_lock_bh(&conn
->cmd_lock
);
962 list_for_each_entry_safe(cmd
, cmd_p
, &conn
->conn_cmd_list
, i_conn_node
) {
963 spin_lock(&cmd
->istate_lock
);
964 if ((cmd
->i_state
== ISTATE_SENT_STATUS
) &&
965 iscsi_sna_lt(cmd
->stat_sn
, exp_statsn
)) {
966 cmd
->i_state
= ISTATE_REMOVE
;
967 spin_unlock(&cmd
->istate_lock
);
968 list_move_tail(&cmd
->i_conn_node
, &ack_list
);
971 spin_unlock(&cmd
->istate_lock
);
973 spin_unlock_bh(&conn
->cmd_lock
);
975 list_for_each_entry_safe(cmd
, cmd_p
, &ack_list
, i_conn_node
) {
976 list_del_init(&cmd
->i_conn_node
);
977 iscsit_free_cmd(cmd
, false);
981 static int iscsit_allocate_iovecs(struct iscsi_cmd
*cmd
)
983 u32 iov_count
= max(1UL, DIV_ROUND_UP(cmd
->se_cmd
.data_length
, PAGE_SIZE
));
985 iov_count
+= ISCSI_IOV_DATA_BUFFER
;
986 cmd
->iov_data
= kcalloc(iov_count
, sizeof(*cmd
->iov_data
), GFP_KERNEL
);
990 cmd
->orig_iov_data_count
= iov_count
;
994 int iscsit_setup_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
997 int data_direction
, payload_length
;
998 struct iscsi_scsi_req
*hdr
;
1002 atomic_long_inc(&conn
->sess
->cmd_pdus
);
1004 hdr
= (struct iscsi_scsi_req
*) buf
;
1005 payload_length
= ntoh24(hdr
->dlength
);
1007 /* FIXME; Add checks for AdditionalHeaderSegment */
1009 if (!(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) &&
1010 !(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
1011 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1012 " not set. Bad iSCSI Initiator.\n");
1013 return iscsit_add_reject_cmd(cmd
,
1014 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1017 if (((hdr
->flags
& ISCSI_FLAG_CMD_READ
) ||
1018 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) && !hdr
->data_length
) {
1020 * From RFC-3720 Section 10.3.1:
1022 * "Either or both of R and W MAY be 1 when either the
1023 * Expected Data Transfer Length and/or Bidirectional Read
1024 * Expected Data Transfer Length are 0"
1026 * For this case, go ahead and clear the unnecssary bits
1027 * to avoid any confusion with ->data_direction.
1029 hdr
->flags
&= ~ISCSI_FLAG_CMD_READ
;
1030 hdr
->flags
&= ~ISCSI_FLAG_CMD_WRITE
;
1032 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
1033 " set when Expected Data Transfer Length is 0 for"
1034 " CDB: 0x%02x, Fixing up flags\n", hdr
->cdb
[0]);
1037 if (!(hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
1038 !(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) && (hdr
->data_length
!= 0)) {
1039 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1040 " MUST be set if Expected Data Transfer Length is not 0."
1041 " Bad iSCSI Initiator\n");
1042 return iscsit_add_reject_cmd(cmd
,
1043 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1046 if ((hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
1047 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) {
1048 pr_err("Bidirectional operations not supported!\n");
1049 return iscsit_add_reject_cmd(cmd
,
1050 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1053 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
1054 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1055 " Scsi Command PDU.\n");
1056 return iscsit_add_reject_cmd(cmd
,
1057 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1060 if (payload_length
&& !conn
->sess
->sess_ops
->ImmediateData
) {
1061 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1062 " protocol error.\n", payload_length
);
1063 return iscsit_add_reject_cmd(cmd
,
1064 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1067 if ((be32_to_cpu(hdr
->data_length
) == payload_length
) &&
1068 (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
))) {
1069 pr_err("Expected Data Transfer Length and Length of"
1070 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1071 " bit is not set protocol error\n");
1072 return iscsit_add_reject_cmd(cmd
,
1073 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1076 if (payload_length
> be32_to_cpu(hdr
->data_length
)) {
1077 pr_err("DataSegmentLength: %u is greater than"
1078 " EDTL: %u, protocol error.\n", payload_length
,
1080 return iscsit_add_reject_cmd(cmd
,
1081 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1084 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1085 pr_err("DataSegmentLength: %u is greater than"
1086 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1087 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
1088 return iscsit_add_reject_cmd(cmd
,
1089 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1092 if (payload_length
> conn
->sess
->sess_ops
->FirstBurstLength
) {
1093 pr_err("DataSegmentLength: %u is greater than"
1094 " FirstBurstLength: %u, protocol error.\n",
1095 payload_length
, conn
->sess
->sess_ops
->FirstBurstLength
);
1096 return iscsit_add_reject_cmd(cmd
,
1097 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1100 data_direction
= (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) ? DMA_TO_DEVICE
:
1101 (hdr
->flags
& ISCSI_FLAG_CMD_READ
) ? DMA_FROM_DEVICE
:
1104 cmd
->data_direction
= data_direction
;
1105 iscsi_task_attr
= hdr
->flags
& ISCSI_FLAG_CMD_ATTR_MASK
;
1107 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1109 if ((iscsi_task_attr
== ISCSI_ATTR_UNTAGGED
) ||
1110 (iscsi_task_attr
== ISCSI_ATTR_SIMPLE
))
1111 sam_task_attr
= TCM_SIMPLE_TAG
;
1112 else if (iscsi_task_attr
== ISCSI_ATTR_ORDERED
)
1113 sam_task_attr
= TCM_ORDERED_TAG
;
1114 else if (iscsi_task_attr
== ISCSI_ATTR_HEAD_OF_QUEUE
)
1115 sam_task_attr
= TCM_HEAD_TAG
;
1116 else if (iscsi_task_attr
== ISCSI_ATTR_ACA
)
1117 sam_task_attr
= TCM_ACA_TAG
;
1119 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
1120 " TCM_SIMPLE_TAG\n", iscsi_task_attr
);
1121 sam_task_attr
= TCM_SIMPLE_TAG
;
1124 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_CMD
;
1125 cmd
->i_state
= ISTATE_NEW_CMD
;
1126 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1127 cmd
->immediate_data
= (payload_length
) ? 1 : 0;
1128 cmd
->unsolicited_data
= ((!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) &&
1129 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) ? 1 : 0);
1130 if (cmd
->unsolicited_data
)
1131 cmd
->cmd_flags
|= ICF_NON_IMMEDIATE_UNSOLICITED_DATA
;
1133 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1134 if (hdr
->flags
& ISCSI_FLAG_CMD_READ
)
1135 cmd
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
1137 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1138 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1139 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1140 cmd
->first_burst_len
= payload_length
;
1142 if (!conn
->sess
->sess_ops
->RDMAExtensions
&&
1143 cmd
->data_direction
== DMA_FROM_DEVICE
) {
1144 struct iscsi_datain_req
*dr
;
1146 dr
= iscsit_allocate_datain_req();
1148 return iscsit_add_reject_cmd(cmd
,
1149 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1151 iscsit_attach_datain_req(cmd
, dr
);
1155 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1157 transport_init_se_cmd(&cmd
->se_cmd
, &iscsi_ops
,
1158 conn
->sess
->se_sess
, be32_to_cpu(hdr
->data_length
),
1159 cmd
->data_direction
, sam_task_attr
,
1160 cmd
->sense_buffer
+ 2, scsilun_to_int(&hdr
->lun
));
1162 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1163 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr
->itt
,
1164 hdr
->cmdsn
, be32_to_cpu(hdr
->data_length
), payload_length
,
1167 target_get_sess_cmd(&cmd
->se_cmd
, true);
1169 cmd
->sense_reason
= target_cmd_init_cdb(&cmd
->se_cmd
, hdr
->cdb
);
1170 if (cmd
->sense_reason
) {
1171 if (cmd
->sense_reason
== TCM_OUT_OF_RESOURCES
) {
1172 return iscsit_add_reject_cmd(cmd
,
1173 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1179 cmd
->sense_reason
= transport_lookup_cmd_lun(&cmd
->se_cmd
);
1180 if (cmd
->sense_reason
)
1183 /* only used for printks or comparing with ->ref_task_tag */
1184 cmd
->se_cmd
.tag
= (__force u32
)cmd
->init_task_tag
;
1185 cmd
->sense_reason
= target_cmd_parse_cdb(&cmd
->se_cmd
);
1186 if (cmd
->sense_reason
)
1189 if (iscsit_build_pdu_and_seq_lists(cmd
, payload_length
) < 0) {
1190 return iscsit_add_reject_cmd(cmd
,
1191 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1195 spin_lock_bh(&conn
->cmd_lock
);
1196 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1197 spin_unlock_bh(&conn
->cmd_lock
);
1199 * Check if we need to delay processing because of ALUA
1200 * Active/NonOptimized primary access state..
1202 core_alua_check_nonop_delay(&cmd
->se_cmd
);
1206 EXPORT_SYMBOL(iscsit_setup_scsi_cmd
);
1208 void iscsit_set_unsolicited_dataout(struct iscsi_cmd
*cmd
)
1210 iscsit_set_dataout_sequence_values(cmd
);
1212 spin_lock_bh(&cmd
->dataout_timeout_lock
);
1213 iscsit_start_dataout_timer(cmd
, cmd
->conn
);
1214 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
1216 EXPORT_SYMBOL(iscsit_set_unsolicited_dataout
);
1218 int iscsit_process_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1219 struct iscsi_scsi_req
*hdr
)
1223 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1224 * the Immediate Bit is not set, and no Immediate
1227 * A PDU/CmdSN carrying Immediate Data can only
1228 * be processed after the DataCRC has passed.
1229 * If the DataCRC fails, the CmdSN MUST NOT
1230 * be acknowledged. (See below)
1232 if (!cmd
->immediate_data
) {
1233 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1234 (unsigned char *)hdr
, hdr
->cmdsn
);
1235 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1237 else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1238 target_put_sess_cmd(&cmd
->se_cmd
);
1243 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1246 * If no Immediate Data is attached, it's OK to return now.
1248 if (!cmd
->immediate_data
) {
1249 if (!cmd
->sense_reason
&& cmd
->unsolicited_data
)
1250 iscsit_set_unsolicited_dataout(cmd
);
1251 if (!cmd
->sense_reason
)
1254 target_put_sess_cmd(&cmd
->se_cmd
);
1259 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1260 * execution. These exceptions are processed in CmdSN order using
1261 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1263 if (cmd
->sense_reason
)
1266 * Call directly into transport_generic_new_cmd() to perform
1267 * the backend memory allocation.
1269 cmd
->sense_reason
= transport_generic_new_cmd(&cmd
->se_cmd
);
1270 if (cmd
->sense_reason
)
1275 EXPORT_SYMBOL(iscsit_process_scsi_cmd
);
1278 iscsit_get_immediate_data(struct iscsi_cmd
*cmd
, struct iscsi_scsi_req
*hdr
,
1281 int cmdsn_ret
= 0, immed_ret
= IMMEDIATE_DATA_NORMAL_OPERATION
;
1285 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1288 u32 length
= min(cmd
->se_cmd
.data_length
- cmd
->write_data_done
,
1289 cmd
->first_burst_len
);
1291 pr_debug("Dumping min(%d - %d, %d) = %d bytes of immediate data\n",
1292 cmd
->se_cmd
.data_length
, cmd
->write_data_done
,
1293 cmd
->first_burst_len
, length
);
1294 rc
= iscsit_dump_data_payload(cmd
->conn
, length
, 1);
1295 pr_debug("Finished dumping immediate data\n");
1297 immed_ret
= IMMEDIATE_DATA_CANNOT_RECOVER
;
1299 immed_ret
= iscsit_handle_immediate_data(cmd
, hdr
,
1300 cmd
->first_burst_len
);
1303 if (immed_ret
== IMMEDIATE_DATA_NORMAL_OPERATION
) {
1305 * A PDU/CmdSN carrying Immediate Data passed
1306 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1307 * Immediate Bit is not set.
1309 cmdsn_ret
= iscsit_sequence_cmd(cmd
->conn
, cmd
,
1310 (unsigned char *)hdr
, hdr
->cmdsn
);
1311 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1314 if (cmd
->sense_reason
|| cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1315 target_put_sess_cmd(&cmd
->se_cmd
);
1318 } else if (cmd
->unsolicited_data
)
1319 iscsit_set_unsolicited_dataout(cmd
);
1321 } else if (immed_ret
== IMMEDIATE_DATA_ERL1_CRC_FAILURE
) {
1323 * Immediate Data failed DataCRC and ERL>=1,
1324 * silently drop this PDU and let the initiator
1325 * plug the CmdSN gap.
1327 * FIXME: Send Unsolicited NOPIN with reserved
1328 * TTT here to help the initiator figure out
1329 * the missing CmdSN, although they should be
1330 * intelligent enough to determine the missing
1331 * CmdSN and issue a retry to plug the sequence.
1333 cmd
->i_state
= ISTATE_REMOVE
;
1334 iscsit_add_cmd_to_immediate_queue(cmd
, cmd
->conn
, cmd
->i_state
);
1335 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1342 iscsit_handle_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1345 struct iscsi_scsi_req
*hdr
= (struct iscsi_scsi_req
*)buf
;
1347 bool dump_payload
= false;
1349 rc
= iscsit_setup_scsi_cmd(conn
, cmd
, buf
);
1353 * Allocation iovecs needed for struct socket operations for
1354 * traditional iSCSI block I/O.
1356 if (iscsit_allocate_iovecs(cmd
) < 0) {
1357 return iscsit_reject_cmd(cmd
,
1358 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1360 immed_data
= cmd
->immediate_data
;
1362 rc
= iscsit_process_scsi_cmd(conn
, cmd
, hdr
);
1366 dump_payload
= true;
1371 return iscsit_get_immediate_data(cmd
, hdr
, dump_payload
);
1374 static u32
iscsit_do_crypto_hash_sg(
1375 struct ahash_request
*hash
,
1376 struct iscsi_cmd
*cmd
,
1383 struct scatterlist
*sg
;
1384 unsigned int page_off
;
1386 crypto_ahash_init(hash
);
1388 sg
= cmd
->first_data_sg
;
1389 page_off
= cmd
->first_data_sg_off
;
1391 if (data_length
&& page_off
) {
1392 struct scatterlist first_sg
;
1393 u32 len
= min_t(u32
, data_length
, sg
->length
- page_off
);
1395 sg_init_table(&first_sg
, 1);
1396 sg_set_page(&first_sg
, sg_page(sg
), len
, sg
->offset
+ page_off
);
1398 ahash_request_set_crypt(hash
, &first_sg
, NULL
, len
);
1399 crypto_ahash_update(hash
);
1405 while (data_length
) {
1406 u32 cur_len
= min_t(u32
, data_length
, sg
->length
);
1408 ahash_request_set_crypt(hash
, sg
, NULL
, cur_len
);
1409 crypto_ahash_update(hash
);
1411 data_length
-= cur_len
;
1412 /* iscsit_map_iovec has already checked for invalid sg pointers */
1417 struct scatterlist pad_sg
;
1419 sg_init_one(&pad_sg
, pad_bytes
, padding
);
1420 ahash_request_set_crypt(hash
, &pad_sg
, (u8
*)&data_crc
,
1422 crypto_ahash_finup(hash
);
1424 ahash_request_set_crypt(hash
, NULL
, (u8
*)&data_crc
, 0);
1425 crypto_ahash_final(hash
);
1431 static void iscsit_do_crypto_hash_buf(struct ahash_request
*hash
,
1432 const void *buf
, u32 payload_length
, u32 padding
,
1433 const void *pad_bytes
, void *data_crc
)
1435 struct scatterlist sg
[2];
1437 sg_init_table(sg
, ARRAY_SIZE(sg
));
1438 sg_set_buf(sg
, buf
, payload_length
);
1440 sg_set_buf(sg
+ 1, pad_bytes
, padding
);
1442 ahash_request_set_crypt(hash
, sg
, data_crc
, payload_length
+ padding
);
1444 crypto_ahash_digest(hash
);
1448 __iscsit_check_dataout_hdr(struct iscsi_conn
*conn
, void *buf
,
1449 struct iscsi_cmd
*cmd
, u32 payload_length
,
1452 struct iscsi_data
*hdr
= buf
;
1453 struct se_cmd
*se_cmd
;
1457 atomic_long_add(payload_length
, &conn
->sess
->rx_data_octets
);
1459 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1460 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1461 hdr
->itt
, hdr
->ttt
, hdr
->datasn
, ntohl(hdr
->offset
),
1462 payload_length
, conn
->cid
);
1464 if (cmd
->cmd_flags
& ICF_GOT_LAST_DATAOUT
) {
1465 pr_err("Command ITT: 0x%08x received DataOUT after"
1466 " last DataOUT received, dumping payload\n",
1467 cmd
->init_task_tag
);
1468 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1471 if (cmd
->data_direction
!= DMA_TO_DEVICE
) {
1472 pr_err("Command ITT: 0x%08x received DataOUT for a"
1473 " NON-WRITE command.\n", cmd
->init_task_tag
);
1474 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1476 se_cmd
= &cmd
->se_cmd
;
1477 iscsit_mod_dataout_timer(cmd
);
1479 if ((be32_to_cpu(hdr
->offset
) + payload_length
) > cmd
->se_cmd
.data_length
) {
1480 pr_err("DataOut Offset: %u, Length %u greater than iSCSI Command EDTL %u, protocol error.\n",
1481 be32_to_cpu(hdr
->offset
), payload_length
,
1482 cmd
->se_cmd
.data_length
);
1483 return iscsit_reject_cmd(cmd
, ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1486 if (cmd
->unsolicited_data
) {
1487 int dump_unsolicited_data
= 0;
1489 if (conn
->sess
->sess_ops
->InitialR2T
) {
1490 pr_err("Received unexpected unsolicited data"
1491 " while InitialR2T=Yes, protocol error.\n");
1492 transport_send_check_condition_and_sense(&cmd
->se_cmd
,
1493 TCM_UNEXPECTED_UNSOLICITED_DATA
, 0);
1497 * Special case for dealing with Unsolicited DataOUT
1498 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1502 /* Something's amiss if we're not in WRITE_PENDING state... */
1503 WARN_ON(se_cmd
->t_state
!= TRANSPORT_WRITE_PENDING
);
1504 if (!(se_cmd
->se_cmd_flags
& SCF_SUPPORTED_SAM_OPCODE
))
1505 dump_unsolicited_data
= 1;
1507 if (dump_unsolicited_data
) {
1509 * Check if a delayed TASK_ABORTED status needs to
1510 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1511 * received with the unsolicited data out.
1513 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1514 iscsit_stop_dataout_timer(cmd
);
1516 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1520 * For the normal solicited data path:
1522 * Check for a delayed TASK_ABORTED status and dump any
1523 * incoming data out payload if one exists. Also, when the
1524 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1525 * data out sequence, we decrement outstanding_r2ts. Once
1526 * outstanding_r2ts reaches zero, go ahead and send the delayed
1527 * TASK_ABORTED status.
1529 if (se_cmd
->transport_state
& CMD_T_ABORTED
) {
1530 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
&&
1531 --cmd
->outstanding_r2ts
< 1)
1532 iscsit_stop_dataout_timer(cmd
);
1534 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1538 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1539 * within-command recovery checks before receiving the payload.
1541 rc
= iscsit_check_pre_dataout(cmd
, buf
);
1542 if (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
)
1544 else if (rc
== DATAOUT_CANNOT_RECOVER
)
1549 EXPORT_SYMBOL(__iscsit_check_dataout_hdr
);
1552 iscsit_check_dataout_hdr(struct iscsi_conn
*conn
, void *buf
,
1553 struct iscsi_cmd
**out_cmd
)
1555 struct iscsi_data
*hdr
= buf
;
1556 struct iscsi_cmd
*cmd
;
1557 u32 payload_length
= ntoh24(hdr
->dlength
);
1559 bool success
= false;
1561 if (!payload_length
) {
1562 pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
1566 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1567 pr_err_ratelimited("DataSegmentLength: %u is greater than"
1568 " MaxXmitDataSegmentLength: %u\n", payload_length
,
1569 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1570 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1573 cmd
= iscsit_find_cmd_from_itt_or_dump(conn
, hdr
->itt
, payload_length
);
1577 rc
= __iscsit_check_dataout_hdr(conn
, buf
, cmd
, payload_length
, &success
);
1584 EXPORT_SYMBOL(iscsit_check_dataout_hdr
);
1587 iscsit_get_dataout(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1588 struct iscsi_data
*hdr
)
1591 u32 checksum
, iov_count
= 0, padding
= 0, rx_got
= 0, rx_size
= 0;
1593 int iov_ret
, data_crc_failed
= 0;
1595 payload_length
= min_t(u32
, cmd
->se_cmd
.data_length
,
1596 ntoh24(hdr
->dlength
));
1597 rx_size
+= payload_length
;
1598 iov
= &cmd
->iov_data
[0];
1600 iov_ret
= iscsit_map_iovec(cmd
, iov
, cmd
->orig_iov_data_count
- 2,
1601 be32_to_cpu(hdr
->offset
), payload_length
);
1605 iov_count
+= iov_ret
;
1607 padding
= ((-payload_length
) & 3);
1609 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
1610 iov
[iov_count
++].iov_len
= padding
;
1612 pr_debug("Receiving %u padding bytes.\n", padding
);
1615 if (conn
->conn_ops
->DataDigest
) {
1616 iov
[iov_count
].iov_base
= &checksum
;
1617 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
1618 rx_size
+= ISCSI_CRC_LEN
;
1621 WARN_ON_ONCE(iov_count
> cmd
->orig_iov_data_count
);
1622 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
1624 iscsit_unmap_iovec(cmd
);
1626 if (rx_got
!= rx_size
)
1629 if (conn
->conn_ops
->DataDigest
) {
1632 data_crc
= iscsit_do_crypto_hash_sg(conn
->conn_rx_hash
, cmd
,
1633 be32_to_cpu(hdr
->offset
),
1634 payload_length
, padding
,
1637 if (checksum
!= data_crc
) {
1638 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1639 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1640 " does not match computed 0x%08x\n",
1641 hdr
->itt
, hdr
->offset
, payload_length
,
1642 hdr
->datasn
, checksum
, data_crc
);
1643 data_crc_failed
= 1;
1645 pr_debug("Got CRC32C DataDigest 0x%08x for"
1646 " %u bytes of Data Out\n", checksum
,
1651 return data_crc_failed
;
1655 iscsit_check_dataout_payload(struct iscsi_cmd
*cmd
, struct iscsi_data
*hdr
,
1656 bool data_crc_failed
)
1658 struct iscsi_conn
*conn
= cmd
->conn
;
1661 * Increment post receive data and CRC values or perform
1662 * within-command recovery.
1664 rc
= iscsit_check_post_dataout(cmd
, (unsigned char *)hdr
, data_crc_failed
);
1665 if ((rc
== DATAOUT_NORMAL
) || (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
))
1667 else if (rc
== DATAOUT_SEND_R2T
) {
1668 iscsit_set_dataout_sequence_values(cmd
);
1669 conn
->conn_transport
->iscsit_get_dataout(conn
, cmd
, false);
1670 } else if (rc
== DATAOUT_SEND_TO_TRANSPORT
) {
1672 * Handle extra special case for out of order
1673 * Unsolicited Data Out.
1675 spin_lock_bh(&cmd
->istate_lock
);
1676 ooo_cmdsn
= (cmd
->cmd_flags
& ICF_OOO_CMDSN
);
1677 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
1678 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
1679 spin_unlock_bh(&cmd
->istate_lock
);
1681 iscsit_stop_dataout_timer(cmd
);
1684 target_execute_cmd(&cmd
->se_cmd
);
1686 } else /* DATAOUT_CANNOT_RECOVER */
1691 EXPORT_SYMBOL(iscsit_check_dataout_payload
);
1693 static int iscsit_handle_data_out(struct iscsi_conn
*conn
, unsigned char *buf
)
1695 struct iscsi_cmd
*cmd
= NULL
;
1696 struct iscsi_data
*hdr
= (struct iscsi_data
*)buf
;
1698 bool data_crc_failed
= false;
1700 rc
= iscsit_check_dataout_hdr(conn
, buf
, &cmd
);
1706 rc
= iscsit_get_dataout(conn
, cmd
, hdr
);
1710 data_crc_failed
= true;
1712 return iscsit_check_dataout_payload(cmd
, hdr
, data_crc_failed
);
1715 int iscsit_setup_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1716 struct iscsi_nopout
*hdr
)
1718 u32 payload_length
= ntoh24(hdr
->dlength
);
1720 if (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
1721 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
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 if (hdr
->itt
== RESERVED_ITT
&& !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1731 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1732 " not set, protocol error.\n");
1734 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1735 (unsigned char *)hdr
);
1737 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1738 (unsigned char *)hdr
);
1741 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1742 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1743 " greater than MaxXmitDataSegmentLength: %u, protocol"
1744 " error.\n", payload_length
,
1745 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1747 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1748 (unsigned char *)hdr
);
1750 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1751 (unsigned char *)hdr
);
1754 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1755 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1756 hdr
->itt
== RESERVED_ITT
? "Response" : "Request",
1757 hdr
->itt
, hdr
->ttt
, hdr
->cmdsn
, hdr
->exp_statsn
,
1760 * This is not a response to a Unsolicited NopIN, which means
1761 * it can either be a NOPOUT ping request (with a valid ITT),
1762 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1763 * Either way, make sure we allocate an struct iscsi_cmd, as both
1764 * can contain ping data.
1766 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1767 cmd
->iscsi_opcode
= ISCSI_OP_NOOP_OUT
;
1768 cmd
->i_state
= ISTATE_SEND_NOPIN
;
1769 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ?
1771 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1772 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1773 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1774 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1775 cmd
->data_direction
= DMA_NONE
;
1780 EXPORT_SYMBOL(iscsit_setup_nop_out
);
1782 int iscsit_process_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1783 struct iscsi_nopout
*hdr
)
1785 struct iscsi_cmd
*cmd_p
= NULL
;
1788 * Initiator is expecting a NopIN ping reply..
1790 if (hdr
->itt
!= RESERVED_ITT
) {
1792 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1793 (unsigned char *)hdr
);
1795 spin_lock_bh(&conn
->cmd_lock
);
1796 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1797 spin_unlock_bh(&conn
->cmd_lock
);
1799 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1801 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
1802 iscsit_add_cmd_to_response_queue(cmd
, conn
,
1807 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1808 (unsigned char *)hdr
, hdr
->cmdsn
);
1809 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
1811 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1817 * This was a response to a unsolicited NOPIN ping.
1819 if (hdr
->ttt
!= cpu_to_be32(0xFFFFFFFF)) {
1820 cmd_p
= iscsit_find_cmd_from_ttt(conn
, be32_to_cpu(hdr
->ttt
));
1824 iscsit_stop_nopin_response_timer(conn
);
1826 cmd_p
->i_state
= ISTATE_REMOVE
;
1827 iscsit_add_cmd_to_immediate_queue(cmd_p
, conn
, cmd_p
->i_state
);
1829 iscsit_start_nopin_timer(conn
);
1833 * Otherwise, initiator is not expecting a NOPIN is response.
1834 * Just ignore for now.
1838 iscsit_free_cmd(cmd
, false);
1842 EXPORT_SYMBOL(iscsit_process_nop_out
);
1844 static int iscsit_handle_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1847 unsigned char *ping_data
= NULL
;
1848 struct iscsi_nopout
*hdr
= (struct iscsi_nopout
*)buf
;
1849 struct kvec
*iov
= NULL
;
1850 u32 payload_length
= ntoh24(hdr
->dlength
);
1853 ret
= iscsit_setup_nop_out(conn
, cmd
, hdr
);
1857 * Handle NOP-OUT payload for traditional iSCSI sockets
1859 if (payload_length
&& hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1860 u32 checksum
, data_crc
, padding
= 0;
1861 int niov
= 0, rx_got
, rx_size
= payload_length
;
1863 ping_data
= kzalloc(payload_length
+ 1, GFP_KERNEL
);
1869 iov
= &cmd
->iov_misc
[0];
1870 iov
[niov
].iov_base
= ping_data
;
1871 iov
[niov
++].iov_len
= payload_length
;
1873 padding
= ((-payload_length
) & 3);
1875 pr_debug("Receiving %u additional bytes"
1876 " for padding.\n", padding
);
1877 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
1878 iov
[niov
++].iov_len
= padding
;
1881 if (conn
->conn_ops
->DataDigest
) {
1882 iov
[niov
].iov_base
= &checksum
;
1883 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
1884 rx_size
+= ISCSI_CRC_LEN
;
1887 WARN_ON_ONCE(niov
> ARRAY_SIZE(cmd
->iov_misc
));
1888 rx_got
= rx_data(conn
, &cmd
->iov_misc
[0], niov
, rx_size
);
1889 if (rx_got
!= rx_size
) {
1894 if (conn
->conn_ops
->DataDigest
) {
1895 iscsit_do_crypto_hash_buf(conn
->conn_rx_hash
, ping_data
,
1896 payload_length
, padding
,
1897 cmd
->pad_bytes
, &data_crc
);
1899 if (checksum
!= data_crc
) {
1900 pr_err("Ping data CRC32C DataDigest"
1901 " 0x%08x does not match computed 0x%08x\n",
1902 checksum
, data_crc
);
1903 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
1904 pr_err("Unable to recover from"
1905 " NOPOUT Ping DataCRC failure while in"
1911 * Silently drop this PDU and let the
1912 * initiator plug the CmdSN gap.
1914 pr_debug("Dropping NOPOUT"
1915 " Command CmdSN: 0x%08x due to"
1916 " DataCRC error.\n", hdr
->cmdsn
);
1921 pr_debug("Got CRC32C DataDigest"
1922 " 0x%08x for %u bytes of ping data.\n",
1923 checksum
, payload_length
);
1927 ping_data
[payload_length
] = '\0';
1929 * Attach ping data to struct iscsi_cmd->buf_ptr.
1931 cmd
->buf_ptr
= ping_data
;
1932 cmd
->buf_ptr_size
= payload_length
;
1934 pr_debug("Got %u bytes of NOPOUT ping"
1935 " data.\n", payload_length
);
1936 pr_debug("Ping Data: \"%s\"\n", ping_data
);
1939 return iscsit_process_nop_out(conn
, cmd
, hdr
);
1942 iscsit_free_cmd(cmd
, false);
1948 static enum tcm_tmreq_table
iscsit_convert_tmf(u8 iscsi_tmf
)
1950 switch (iscsi_tmf
) {
1951 case ISCSI_TM_FUNC_ABORT_TASK
:
1952 return TMR_ABORT_TASK
;
1953 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
1954 return TMR_ABORT_TASK_SET
;
1955 case ISCSI_TM_FUNC_CLEAR_ACA
:
1956 return TMR_CLEAR_ACA
;
1957 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
1958 return TMR_CLEAR_TASK_SET
;
1959 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
1960 return TMR_LUN_RESET
;
1961 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
1962 return TMR_TARGET_WARM_RESET
;
1963 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
1964 return TMR_TARGET_COLD_RESET
;
1971 iscsit_handle_task_mgt_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1974 struct se_tmr_req
*se_tmr
;
1975 struct iscsi_tmr_req
*tmr_req
;
1976 struct iscsi_tm
*hdr
;
1977 int out_of_order_cmdsn
= 0, ret
;
1978 u8 function
, tcm_function
= TMR_UNKNOWN
;
1980 hdr
= (struct iscsi_tm
*) buf
;
1981 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
1982 function
= hdr
->flags
;
1984 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1985 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1986 " 0x%08x, CID: %hu\n", hdr
->itt
, hdr
->cmdsn
, function
,
1987 hdr
->rtt
, hdr
->refcmdsn
, conn
->cid
);
1989 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1990 ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1991 hdr
->rtt
!= RESERVED_ITT
)) {
1992 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1993 hdr
->rtt
= RESERVED_ITT
;
1996 if ((function
== ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1997 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1998 pr_err("Task Management Request TASK_REASSIGN not"
1999 " issued as immediate command, bad iSCSI Initiator"
2000 "implementation\n");
2001 return iscsit_add_reject_cmd(cmd
,
2002 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
2004 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
2005 be32_to_cpu(hdr
->refcmdsn
) != ISCSI_RESERVED_TAG
)
2006 hdr
->refcmdsn
= cpu_to_be32(ISCSI_RESERVED_TAG
);
2008 cmd
->data_direction
= DMA_NONE
;
2009 cmd
->tmr_req
= kzalloc(sizeof(*cmd
->tmr_req
), GFP_KERNEL
);
2010 if (!cmd
->tmr_req
) {
2011 return iscsit_add_reject_cmd(cmd
,
2012 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
2016 transport_init_se_cmd(&cmd
->se_cmd
, &iscsi_ops
,
2017 conn
->sess
->se_sess
, 0, DMA_NONE
,
2018 TCM_SIMPLE_TAG
, cmd
->sense_buffer
+ 2,
2019 scsilun_to_int(&hdr
->lun
));
2021 target_get_sess_cmd(&cmd
->se_cmd
, true);
2024 * TASK_REASSIGN for ERL=2 / connection stays inside of
2025 * LIO-Target $FABRIC_MOD
2027 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
2028 tcm_function
= iscsit_convert_tmf(function
);
2029 if (tcm_function
== TMR_UNKNOWN
) {
2030 pr_err("Unknown iSCSI TMR Function:"
2031 " 0x%02x\n", function
);
2032 return iscsit_add_reject_cmd(cmd
,
2033 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
2036 ret
= core_tmr_alloc_req(&cmd
->se_cmd
, cmd
->tmr_req
, tcm_function
,
2039 return iscsit_add_reject_cmd(cmd
,
2040 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
2042 cmd
->tmr_req
->se_tmr_req
= cmd
->se_cmd
.se_tmr_req
;
2044 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_TMFUNC
;
2045 cmd
->i_state
= ISTATE_SEND_TASKMGTRSP
;
2046 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2047 cmd
->init_task_tag
= hdr
->itt
;
2048 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2049 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
2050 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
2051 se_tmr
= cmd
->se_cmd
.se_tmr_req
;
2052 tmr_req
= cmd
->tmr_req
;
2054 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2056 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
2057 ret
= transport_lookup_tmr_lun(&cmd
->se_cmd
);
2059 se_tmr
->response
= ISCSI_TMF_RSP_NO_LUN
;
2065 case ISCSI_TM_FUNC_ABORT_TASK
:
2066 se_tmr
->response
= iscsit_tmr_abort_task(cmd
, buf
);
2067 if (se_tmr
->response
)
2070 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
2071 case ISCSI_TM_FUNC_CLEAR_ACA
:
2072 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
2073 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
2075 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
2076 if (iscsit_tmr_task_warm_reset(conn
, tmr_req
, buf
) < 0) {
2077 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
2081 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
2082 if (iscsit_tmr_task_cold_reset(conn
, tmr_req
, buf
) < 0) {
2083 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
2087 case ISCSI_TM_FUNC_TASK_REASSIGN
:
2088 se_tmr
->response
= iscsit_tmr_task_reassign(cmd
, buf
);
2090 * Perform sanity checks on the ExpDataSN only if the
2091 * TASK_REASSIGN was successful.
2093 if (se_tmr
->response
)
2096 if (iscsit_check_task_reassign_expdatasn(tmr_req
, conn
) < 0)
2097 return iscsit_add_reject_cmd(cmd
,
2098 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
2101 pr_err("Unknown TMR function: 0x%02x, protocol"
2102 " error.\n", function
);
2103 se_tmr
->response
= ISCSI_TMF_RSP_NOT_SUPPORTED
;
2107 if ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
2108 (se_tmr
->response
== ISCSI_TMF_RSP_COMPLETE
))
2109 se_tmr
->call_transport
= 1;
2111 spin_lock_bh(&conn
->cmd_lock
);
2112 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2113 spin_unlock_bh(&conn
->cmd_lock
);
2115 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
2116 int cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
2117 if (cmdsn_ret
== CMDSN_HIGHER_THAN_EXP
) {
2118 out_of_order_cmdsn
= 1;
2119 } else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
2120 target_put_sess_cmd(&cmd
->se_cmd
);
2122 } else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
) {
2126 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2128 if (out_of_order_cmdsn
|| !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
))
2131 * Found the referenced task, send to transport for processing.
2133 if (se_tmr
->call_transport
)
2134 return transport_generic_handle_tmr(&cmd
->se_cmd
);
2137 * Could not find the referenced LUN, task, or Task Management
2138 * command not authorized or supported. Change state and
2139 * let the tx_thread send the response.
2141 * For connection recovery, this is also the default action for
2142 * TMR TASK_REASSIGN.
2144 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2145 target_put_sess_cmd(&cmd
->se_cmd
);
2148 EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd
);
2150 /* #warning FIXME: Support Text Command parameters besides SendTargets */
2152 iscsit_setup_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2153 struct iscsi_text
*hdr
)
2155 u32 payload_length
= ntoh24(hdr
->dlength
);
2157 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
2158 pr_err("Unable to accept text parameter length: %u"
2159 "greater than MaxXmitDataSegmentLength %u.\n",
2160 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
2161 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
2162 (unsigned char *)hdr
);
2165 if (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) ||
2166 (hdr
->flags
& ISCSI_FLAG_TEXT_CONTINUE
)) {
2167 pr_err("Multi sequence text commands currently not supported\n");
2168 return iscsit_reject_cmd(cmd
, ISCSI_REASON_CMD_NOT_SUPPORTED
,
2169 (unsigned char *)hdr
);
2172 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2173 " ExpStatSN: 0x%08x, Length: %u\n", hdr
->itt
, hdr
->cmdsn
,
2174 hdr
->exp_statsn
, payload_length
);
2176 cmd
->iscsi_opcode
= ISCSI_OP_TEXT
;
2177 cmd
->i_state
= ISTATE_SEND_TEXTRSP
;
2178 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2179 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
2180 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2181 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
2182 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
2183 cmd
->data_direction
= DMA_NONE
;
2184 kfree(cmd
->text_in_ptr
);
2185 cmd
->text_in_ptr
= NULL
;
2189 EXPORT_SYMBOL(iscsit_setup_text_cmd
);
2192 iscsit_process_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2193 struct iscsi_text
*hdr
)
2195 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
;
2199 cmd
->targ_xfer_tag
= be32_to_cpu(hdr
->ttt
);
2200 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF) {
2201 pr_err("Unable to locate text_in buffer for sendtargets"
2205 goto empty_sendtargets
;
2207 if (strncmp("SendTargets=", text_in
, 12) != 0) {
2208 pr_err("Received Text Data that is not"
2209 " SendTargets, cannot continue.\n");
2212 /* '=' confirmed in strncmp */
2213 text_ptr
= strchr(text_in
, '=');
2215 if (!strncmp("=All", text_ptr
, 5)) {
2216 cmd
->cmd_flags
|= ICF_SENDTARGETS_ALL
;
2217 } else if (!strncmp("=iqn.", text_ptr
, 5) ||
2218 !strncmp("=eui.", text_ptr
, 5)) {
2219 cmd
->cmd_flags
|= ICF_SENDTARGETS_SINGLE
;
2221 pr_err("Unable to locate valid SendTargets%s value\n",
2226 spin_lock_bh(&conn
->cmd_lock
);
2227 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2228 spin_unlock_bh(&conn
->cmd_lock
);
2231 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2233 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
2234 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
2235 (unsigned char *)hdr
, hdr
->cmdsn
);
2236 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2242 return iscsit_execute_cmd(cmd
, 0);
2245 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
2246 (unsigned char *)hdr
);
2248 EXPORT_SYMBOL(iscsit_process_text_cmd
);
2251 iscsit_handle_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2254 struct iscsi_text
*hdr
= (struct iscsi_text
*)buf
;
2255 char *text_in
= NULL
;
2256 u32 payload_length
= ntoh24(hdr
->dlength
);
2259 rc
= iscsit_setup_text_cmd(conn
, cmd
, hdr
);
2263 rx_size
= payload_length
;
2264 if (payload_length
) {
2265 u32 checksum
= 0, data_crc
= 0;
2267 int niov
= 0, rx_got
;
2270 rx_size
= ALIGN(payload_length
, 4);
2271 text_in
= kzalloc(rx_size
, GFP_KERNEL
);
2275 cmd
->text_in_ptr
= text_in
;
2277 memset(iov
, 0, sizeof(iov
));
2278 iov
[niov
].iov_base
= text_in
;
2279 iov
[niov
++].iov_len
= rx_size
;
2281 padding
= rx_size
- payload_length
;
2283 pr_debug("Receiving %u additional bytes"
2284 " for padding.\n", padding
);
2285 if (conn
->conn_ops
->DataDigest
) {
2286 iov
[niov
].iov_base
= &checksum
;
2287 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
2288 rx_size
+= ISCSI_CRC_LEN
;
2291 WARN_ON_ONCE(niov
> ARRAY_SIZE(iov
));
2292 rx_got
= rx_data(conn
, &iov
[0], niov
, rx_size
);
2293 if (rx_got
!= rx_size
)
2296 if (conn
->conn_ops
->DataDigest
) {
2297 iscsit_do_crypto_hash_buf(conn
->conn_rx_hash
,
2298 text_in
, rx_size
, 0, NULL
,
2301 if (checksum
!= data_crc
) {
2302 pr_err("Text data CRC32C DataDigest"
2303 " 0x%08x does not match computed"
2304 " 0x%08x\n", checksum
, data_crc
);
2305 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2306 pr_err("Unable to recover from"
2307 " Text Data digest failure while in"
2312 * Silently drop this PDU and let the
2313 * initiator plug the CmdSN gap.
2315 pr_debug("Dropping Text"
2316 " Command CmdSN: 0x%08x due to"
2317 " DataCRC error.\n", hdr
->cmdsn
);
2322 pr_debug("Got CRC32C DataDigest"
2323 " 0x%08x for %u bytes of text data.\n",
2324 checksum
, payload_length
);
2327 text_in
[payload_length
- 1] = '\0';
2328 pr_debug("Successfully read %d bytes of text"
2329 " data.\n", payload_length
);
2332 return iscsit_process_text_cmd(conn
, cmd
, hdr
);
2335 kfree(cmd
->text_in_ptr
);
2336 cmd
->text_in_ptr
= NULL
;
2337 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
, buf
);
2340 int iscsit_logout_closesession(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2342 struct iscsi_conn
*conn_p
;
2343 struct iscsi_session
*sess
= conn
->sess
;
2345 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2346 " for SID: %u.\n", conn
->cid
, conn
->sess
->sid
);
2348 atomic_set(&sess
->session_logout
, 1);
2349 atomic_set(&conn
->conn_logout_remove
, 1);
2350 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_SESSION
;
2352 iscsit_inc_conn_usage_count(conn
);
2353 iscsit_inc_session_usage_count(sess
);
2355 spin_lock_bh(&sess
->conn_lock
);
2356 list_for_each_entry(conn_p
, &sess
->sess_conn_list
, conn_list
) {
2357 if (conn_p
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
)
2360 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2361 conn_p
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2363 spin_unlock_bh(&sess
->conn_lock
);
2365 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2370 int iscsit_logout_closeconnection(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2372 struct iscsi_conn
*l_conn
;
2373 struct iscsi_session
*sess
= conn
->sess
;
2375 pr_debug("Received logout request CLOSECONNECTION for CID:"
2376 " %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2379 * A Logout Request with a CLOSECONNECTION reason code for a CID
2380 * can arrive on a connection with a differing CID.
2382 if (conn
->cid
== cmd
->logout_cid
) {
2383 spin_lock_bh(&conn
->state_lock
);
2384 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2385 conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2387 atomic_set(&conn
->conn_logout_remove
, 1);
2388 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
;
2389 iscsit_inc_conn_usage_count(conn
);
2391 spin_unlock_bh(&conn
->state_lock
);
2394 * Handle all different cid CLOSECONNECTION requests in
2395 * iscsit_logout_post_handler_diffcid() as to give enough
2396 * time for any non immediate command's CmdSN to be
2397 * acknowledged on the connection in question.
2399 * Here we simply make sure the CID is still around.
2401 l_conn
= iscsit_get_conn_from_cid(sess
,
2404 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2405 iscsit_add_cmd_to_response_queue(cmd
, conn
,
2410 iscsit_dec_conn_usage_count(l_conn
);
2413 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2418 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2420 struct iscsi_session
*sess
= conn
->sess
;
2422 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2423 " CID: %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2425 if (sess
->sess_ops
->ErrorRecoveryLevel
!= 2) {
2426 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2427 " while ERL!=2.\n");
2428 cmd
->logout_response
= ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
;
2429 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2433 if (conn
->cid
== cmd
->logout_cid
) {
2434 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2435 " with CID: %hu on CID: %hu, implementation error.\n",
2436 cmd
->logout_cid
, conn
->cid
);
2437 cmd
->logout_response
= ISCSI_LOGOUT_CLEANUP_FAILED
;
2438 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2442 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2448 iscsit_handle_logout_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2451 int cmdsn_ret
, logout_remove
= 0;
2453 struct iscsi_logout
*hdr
;
2454 struct iscsi_tiqn
*tiqn
= iscsit_snmp_get_tiqn(conn
);
2456 hdr
= (struct iscsi_logout
*) buf
;
2457 reason_code
= (hdr
->flags
& 0x7f);
2460 spin_lock(&tiqn
->logout_stats
.lock
);
2461 if (reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
)
2462 tiqn
->logout_stats
.normal_logouts
++;
2464 tiqn
->logout_stats
.abnormal_logouts
++;
2465 spin_unlock(&tiqn
->logout_stats
.lock
);
2468 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2469 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2470 hdr
->itt
, hdr
->cmdsn
, hdr
->exp_statsn
, reason_code
,
2471 hdr
->cid
, conn
->cid
);
2473 if (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
) {
2474 pr_err("Received logout request on connection that"
2475 " is not in logged in state, ignoring request.\n");
2476 iscsit_free_cmd(cmd
, false);
2480 cmd
->iscsi_opcode
= ISCSI_OP_LOGOUT
;
2481 cmd
->i_state
= ISTATE_SEND_LOGOUTRSP
;
2482 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2483 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
2484 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2485 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
2486 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
2487 cmd
->logout_cid
= be16_to_cpu(hdr
->cid
);
2488 cmd
->logout_reason
= reason_code
;
2489 cmd
->data_direction
= DMA_NONE
;
2492 * We need to sleep in these cases (by returning 1) until the Logout
2493 * Response gets sent in the tx thread.
2495 if ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) ||
2496 ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
) &&
2497 be16_to_cpu(hdr
->cid
) == conn
->cid
))
2500 spin_lock_bh(&conn
->cmd_lock
);
2501 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2502 spin_unlock_bh(&conn
->cmd_lock
);
2504 if (reason_code
!= ISCSI_LOGOUT_REASON_RECOVERY
)
2505 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2508 * Immediate commands are executed, well, immediately.
2509 * Non-Immediate Logout Commands are executed in CmdSN order.
2511 if (cmd
->immediate_cmd
) {
2512 int ret
= iscsit_execute_cmd(cmd
, 0);
2517 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
2518 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
2520 else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2524 return logout_remove
;
2526 EXPORT_SYMBOL(iscsit_handle_logout_cmd
);
2528 int iscsit_handle_snack(
2529 struct iscsi_conn
*conn
,
2532 struct iscsi_snack
*hdr
;
2534 hdr
= (struct iscsi_snack
*) buf
;
2535 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
2537 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2538 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2539 " CID: %hu\n", hdr
->itt
, hdr
->exp_statsn
, hdr
->flags
,
2540 hdr
->begrun
, hdr
->runlength
, conn
->cid
);
2542 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2543 pr_err("Initiator sent SNACK request while in"
2544 " ErrorRecoveryLevel=0.\n");
2545 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2549 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2550 * call from inside iscsi_send_recovery_datain_or_r2t().
2552 switch (hdr
->flags
& ISCSI_FLAG_SNACK_TYPE_MASK
) {
2554 return iscsit_handle_recovery_datain_or_r2t(conn
, buf
,
2556 be32_to_cpu(hdr
->ttt
),
2557 be32_to_cpu(hdr
->begrun
),
2558 be32_to_cpu(hdr
->runlength
));
2559 case ISCSI_FLAG_SNACK_TYPE_STATUS
:
2560 return iscsit_handle_status_snack(conn
, hdr
->itt
,
2561 be32_to_cpu(hdr
->ttt
),
2562 be32_to_cpu(hdr
->begrun
), be32_to_cpu(hdr
->runlength
));
2563 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK
:
2564 return iscsit_handle_data_ack(conn
, be32_to_cpu(hdr
->ttt
),
2565 be32_to_cpu(hdr
->begrun
),
2566 be32_to_cpu(hdr
->runlength
));
2567 case ISCSI_FLAG_SNACK_TYPE_RDATA
:
2568 /* FIXME: Support R-Data SNACK */
2569 pr_err("R-Data SNACK Not Supported.\n");
2570 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2573 pr_err("Unknown SNACK type 0x%02x, protocol"
2574 " error.\n", hdr
->flags
& 0x0f);
2575 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2581 EXPORT_SYMBOL(iscsit_handle_snack
);
2583 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2585 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2586 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2587 wait_for_completion_interruptible_timeout(
2588 &conn
->rx_half_close_comp
,
2589 ISCSI_RX_THREAD_TCP_TIMEOUT
* HZ
);
2593 static int iscsit_handle_immediate_data(
2594 struct iscsi_cmd
*cmd
,
2595 struct iscsi_scsi_req
*hdr
,
2598 int iov_ret
, rx_got
= 0, rx_size
= 0;
2599 u32 checksum
, iov_count
= 0, padding
= 0;
2600 struct iscsi_conn
*conn
= cmd
->conn
;
2602 void *overflow_buf
= NULL
;
2604 BUG_ON(cmd
->write_data_done
> cmd
->se_cmd
.data_length
);
2605 rx_size
= min(cmd
->se_cmd
.data_length
- cmd
->write_data_done
, length
);
2606 iov_ret
= iscsit_map_iovec(cmd
, cmd
->iov_data
,
2607 cmd
->orig_iov_data_count
- 2,
2608 cmd
->write_data_done
, rx_size
);
2610 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2612 iov_count
= iov_ret
;
2613 iov
= &cmd
->iov_data
[0];
2614 if (rx_size
< length
) {
2616 * Special case: length of immediate data exceeds the data
2617 * buffer size derived from the CDB.
2619 overflow_buf
= kmalloc(length
- rx_size
, GFP_KERNEL
);
2620 if (!overflow_buf
) {
2621 iscsit_unmap_iovec(cmd
);
2622 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2624 cmd
->overflow_buf
= overflow_buf
;
2625 iov
[iov_count
].iov_base
= overflow_buf
;
2626 iov
[iov_count
].iov_len
= length
- rx_size
;
2631 padding
= ((-length
) & 3);
2633 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2634 iov
[iov_count
++].iov_len
= padding
;
2638 if (conn
->conn_ops
->DataDigest
) {
2639 iov
[iov_count
].iov_base
= &checksum
;
2640 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2641 rx_size
+= ISCSI_CRC_LEN
;
2644 WARN_ON_ONCE(iov_count
> cmd
->orig_iov_data_count
);
2645 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
2647 iscsit_unmap_iovec(cmd
);
2649 if (rx_got
!= rx_size
) {
2650 iscsit_rx_thread_wait_for_tcp(conn
);
2651 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2654 if (conn
->conn_ops
->DataDigest
) {
2657 data_crc
= iscsit_do_crypto_hash_sg(conn
->conn_rx_hash
, cmd
,
2658 cmd
->write_data_done
, length
, padding
,
2661 if (checksum
!= data_crc
) {
2662 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2663 " does not match computed 0x%08x\n", checksum
,
2666 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2667 pr_err("Unable to recover from"
2668 " Immediate Data digest failure while"
2670 iscsit_reject_cmd(cmd
,
2671 ISCSI_REASON_DATA_DIGEST_ERROR
,
2672 (unsigned char *)hdr
);
2673 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2675 iscsit_reject_cmd(cmd
,
2676 ISCSI_REASON_DATA_DIGEST_ERROR
,
2677 (unsigned char *)hdr
);
2678 return IMMEDIATE_DATA_ERL1_CRC_FAILURE
;
2681 pr_debug("Got CRC32C DataDigest 0x%08x for"
2682 " %u bytes of Immediate Data\n", checksum
,
2687 cmd
->write_data_done
+= length
;
2689 if (cmd
->write_data_done
== cmd
->se_cmd
.data_length
) {
2690 spin_lock_bh(&cmd
->istate_lock
);
2691 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
2692 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
2693 spin_unlock_bh(&cmd
->istate_lock
);
2696 return IMMEDIATE_DATA_NORMAL_OPERATION
;
2699 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2700 with active network interface */
2701 static void iscsit_build_conn_drop_async_message(struct iscsi_conn
*conn
)
2703 struct iscsi_cmd
*cmd
;
2704 struct iscsi_conn
*conn_p
;
2707 lockdep_assert_held(&conn
->sess
->conn_lock
);
2710 * Only send a Asynchronous Message on connections whos network
2711 * interface is still functional.
2713 list_for_each_entry(conn_p
, &conn
->sess
->sess_conn_list
, conn_list
) {
2714 if (conn_p
->conn_state
== TARG_CONN_STATE_LOGGED_IN
) {
2715 iscsit_inc_conn_usage_count(conn_p
);
2724 cmd
= iscsit_allocate_cmd(conn_p
, TASK_RUNNING
);
2726 iscsit_dec_conn_usage_count(conn_p
);
2730 cmd
->logout_cid
= conn
->cid
;
2731 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2732 cmd
->i_state
= ISTATE_SEND_ASYNCMSG
;
2734 spin_lock_bh(&conn_p
->cmd_lock
);
2735 list_add_tail(&cmd
->i_conn_node
, &conn_p
->conn_cmd_list
);
2736 spin_unlock_bh(&conn_p
->cmd_lock
);
2738 iscsit_add_cmd_to_response_queue(cmd
, conn_p
, cmd
->i_state
);
2739 iscsit_dec_conn_usage_count(conn_p
);
2742 static int iscsit_send_conn_drop_async_message(
2743 struct iscsi_cmd
*cmd
,
2744 struct iscsi_conn
*conn
)
2746 struct iscsi_async
*hdr
;
2748 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2750 hdr
= (struct iscsi_async
*) cmd
->pdu
;
2751 hdr
->opcode
= ISCSI_OP_ASYNC_EVENT
;
2752 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
2753 cmd
->init_task_tag
= RESERVED_ITT
;
2754 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2755 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->rsvd4
[0]);
2756 cmd
->stat_sn
= conn
->stat_sn
++;
2757 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2758 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2759 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2760 hdr
->async_event
= ISCSI_ASYNC_MSG_DROPPING_CONNECTION
;
2761 hdr
->param1
= cpu_to_be16(cmd
->logout_cid
);
2762 hdr
->param2
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Wait
);
2763 hdr
->param3
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Retain
);
2765 pr_debug("Sending Connection Dropped Async Message StatSN:"
2766 " 0x%08x, for CID: %hu on CID: %hu\n", cmd
->stat_sn
,
2767 cmd
->logout_cid
, conn
->cid
);
2769 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
2772 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2774 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2775 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2776 wait_for_completion_interruptible_timeout(
2777 &conn
->tx_half_close_comp
,
2778 ISCSI_TX_THREAD_TCP_TIMEOUT
* HZ
);
2783 iscsit_build_datain_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2784 struct iscsi_datain
*datain
, struct iscsi_data_rsp
*hdr
,
2787 hdr
->opcode
= ISCSI_OP_SCSI_DATA_IN
;
2788 hdr
->flags
= datain
->flags
;
2789 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
2790 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
2791 hdr
->flags
|= ISCSI_FLAG_DATA_OVERFLOW
;
2792 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2793 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
2794 hdr
->flags
|= ISCSI_FLAG_DATA_UNDERFLOW
;
2795 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2798 hton24(hdr
->dlength
, datain
->length
);
2799 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2800 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
2801 (struct scsi_lun
*)&hdr
->lun
);
2803 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2805 hdr
->itt
= cmd
->init_task_tag
;
2807 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2808 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2810 hdr
->ttt
= cpu_to_be32(0xFFFFFFFF);
2812 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2814 hdr
->statsn
= cpu_to_be32(0xFFFFFFFF);
2816 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2817 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2818 hdr
->datasn
= cpu_to_be32(datain
->data_sn
);
2819 hdr
->offset
= cpu_to_be32(datain
->offset
);
2821 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2822 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2823 cmd
->init_task_tag
, ntohl(hdr
->statsn
), ntohl(hdr
->datasn
),
2824 ntohl(hdr
->offset
), datain
->length
, conn
->cid
);
2826 EXPORT_SYMBOL(iscsit_build_datain_pdu
);
2828 static int iscsit_send_datain(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2830 struct iscsi_data_rsp
*hdr
= (struct iscsi_data_rsp
*)&cmd
->pdu
[0];
2831 struct iscsi_datain datain
;
2832 struct iscsi_datain_req
*dr
;
2834 bool set_statsn
= false;
2836 memset(&datain
, 0, sizeof(struct iscsi_datain
));
2837 dr
= iscsit_get_datain_values(cmd
, &datain
);
2839 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2840 cmd
->init_task_tag
);
2844 * Be paranoid and double check the logic for now.
2846 if ((datain
.offset
+ datain
.length
) > cmd
->se_cmd
.data_length
) {
2847 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2848 " datain.length: %u exceeds cmd->data_length: %u\n",
2849 cmd
->init_task_tag
, datain
.offset
, datain
.length
,
2850 cmd
->se_cmd
.data_length
);
2854 atomic_long_add(datain
.length
, &conn
->sess
->tx_data_octets
);
2856 * Special case for successfully execution w/ both DATAIN
2859 if ((datain
.flags
& ISCSI_FLAG_DATA_STATUS
) &&
2860 (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
))
2861 datain
.flags
&= ~ISCSI_FLAG_DATA_STATUS
;
2863 if ((dr
->dr_complete
== DATAIN_COMPLETE_NORMAL
) ||
2864 (dr
->dr_complete
== DATAIN_COMPLETE_CONNECTION_RECOVERY
)) {
2865 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2866 cmd
->stat_sn
= conn
->stat_sn
++;
2868 } else if (dr
->dr_complete
==
2869 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY
)
2873 iscsit_build_datain_pdu(cmd
, conn
, &datain
, hdr
, set_statsn
);
2875 ret
= conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, dr
, &datain
, 0);
2879 if (dr
->dr_complete
) {
2880 eodr
= (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ?
2882 iscsit_free_datain_req(cmd
, dr
);
2889 iscsit_build_logout_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2890 struct iscsi_logout_rsp
*hdr
)
2892 struct iscsi_conn
*logout_conn
= NULL
;
2893 struct iscsi_conn_recovery
*cr
= NULL
;
2894 struct iscsi_session
*sess
= conn
->sess
;
2896 * The actual shutting down of Sessions and/or Connections
2897 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2898 * is done in scsi_logout_post_handler().
2900 switch (cmd
->logout_reason
) {
2901 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
2902 pr_debug("iSCSI session logout successful, setting"
2903 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2904 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2906 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
2907 if (cmd
->logout_response
== ISCSI_LOGOUT_CID_NOT_FOUND
)
2910 * For CLOSECONNECTION logout requests carrying
2911 * a matching logout CID -> local CID, the reference
2912 * for the local CID will have been incremented in
2913 * iscsi_logout_closeconnection().
2915 * For CLOSECONNECTION logout requests carrying
2916 * a different CID than the connection it arrived
2917 * on, the connection responding to cmd->logout_cid
2918 * is stopped in iscsit_logout_post_handler_diffcid().
2921 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2922 " successful.\n", cmd
->logout_cid
, conn
->cid
);
2923 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2925 case ISCSI_LOGOUT_REASON_RECOVERY
:
2926 if ((cmd
->logout_response
== ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
) ||
2927 (cmd
->logout_response
== ISCSI_LOGOUT_CLEANUP_FAILED
))
2930 * If the connection is still active from our point of view
2931 * force connection recovery to occur.
2933 logout_conn
= iscsit_get_conn_from_cid_rcfr(sess
,
2936 iscsit_connection_reinstatement_rcfr(logout_conn
);
2937 iscsit_dec_conn_usage_count(logout_conn
);
2940 cr
= iscsit_get_inactive_connection_recovery_entry(
2941 conn
->sess
, cmd
->logout_cid
);
2943 pr_err("Unable to locate CID: %hu for"
2944 " REMOVECONNFORRECOVERY Logout Request.\n",
2946 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2950 iscsit_discard_cr_cmds_by_expstatsn(cr
, cmd
->exp_stat_sn
);
2952 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2953 " for recovery for CID: %hu on CID: %hu successful.\n",
2954 cmd
->logout_cid
, conn
->cid
);
2955 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2958 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2959 cmd
->logout_reason
);
2963 hdr
->opcode
= ISCSI_OP_LOGOUT_RSP
;
2964 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2965 hdr
->response
= cmd
->logout_response
;
2966 hdr
->itt
= cmd
->init_task_tag
;
2967 cmd
->stat_sn
= conn
->stat_sn
++;
2968 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2970 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2971 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2972 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2974 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2975 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2976 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
,
2977 cmd
->logout_cid
, conn
->cid
);
2981 EXPORT_SYMBOL(iscsit_build_logout_rsp
);
2984 iscsit_send_logout(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2988 rc
= iscsit_build_logout_rsp(cmd
, conn
,
2989 (struct iscsi_logout_rsp
*)&cmd
->pdu
[0]);
2993 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
2997 iscsit_build_nopin_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2998 struct iscsi_nopin
*hdr
, bool nopout_response
)
3000 hdr
->opcode
= ISCSI_OP_NOOP_IN
;
3001 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3002 hton24(hdr
->dlength
, cmd
->buf_ptr_size
);
3003 if (nopout_response
)
3004 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
3005 hdr
->itt
= cmd
->init_task_tag
;
3006 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
3007 cmd
->stat_sn
= (nopout_response
) ? conn
->stat_sn
++ :
3009 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3011 if (nopout_response
)
3012 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3014 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3015 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3017 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
3018 " StatSN: 0x%08x, Length %u\n", (nopout_response
) ?
3019 "Solicited" : "Unsolicited", cmd
->init_task_tag
,
3020 cmd
->targ_xfer_tag
, cmd
->stat_sn
, cmd
->buf_ptr_size
);
3022 EXPORT_SYMBOL(iscsit_build_nopin_rsp
);
3025 * Unsolicited NOPIN, either requesting a response or not.
3027 static int iscsit_send_unsolicited_nopin(
3028 struct iscsi_cmd
*cmd
,
3029 struct iscsi_conn
*conn
,
3032 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
3035 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, false);
3037 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
3038 " 0x%08x CID: %hu\n", hdr
->ttt
, cmd
->stat_sn
, conn
->cid
);
3040 ret
= conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
3044 spin_lock_bh(&cmd
->istate_lock
);
3045 cmd
->i_state
= want_response
?
3046 ISTATE_SENT_NOPIN_WANT_RESPONSE
: ISTATE_SENT_STATUS
;
3047 spin_unlock_bh(&cmd
->istate_lock
);
3053 iscsit_send_nopin(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3055 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
3057 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, true);
3060 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3061 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3063 pr_debug("Echoing back %u bytes of ping data.\n", cmd
->buf_ptr_size
);
3065 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
,
3070 static int iscsit_send_r2t(
3071 struct iscsi_cmd
*cmd
,
3072 struct iscsi_conn
*conn
)
3074 struct iscsi_r2t
*r2t
;
3075 struct iscsi_r2t_rsp
*hdr
;
3078 r2t
= iscsit_get_r2t_from_list(cmd
);
3082 hdr
= (struct iscsi_r2t_rsp
*) cmd
->pdu
;
3083 memset(hdr
, 0, ISCSI_HDR_LEN
);
3084 hdr
->opcode
= ISCSI_OP_R2T
;
3085 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3086 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
3087 (struct scsi_lun
*)&hdr
->lun
);
3088 hdr
->itt
= cmd
->init_task_tag
;
3089 if (conn
->conn_transport
->iscsit_get_r2t_ttt
)
3090 conn
->conn_transport
->iscsit_get_r2t_ttt(conn
, cmd
, r2t
);
3092 r2t
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
3093 hdr
->ttt
= cpu_to_be32(r2t
->targ_xfer_tag
);
3094 hdr
->statsn
= cpu_to_be32(conn
->stat_sn
);
3095 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3096 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3097 hdr
->r2tsn
= cpu_to_be32(r2t
->r2t_sn
);
3098 hdr
->data_offset
= cpu_to_be32(r2t
->offset
);
3099 hdr
->data_length
= cpu_to_be32(r2t
->xfer_len
);
3101 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3102 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3103 (!r2t
->recovery_r2t
) ? "" : "Recovery ", cmd
->init_task_tag
,
3104 r2t
->targ_xfer_tag
, ntohl(hdr
->statsn
), r2t
->r2t_sn
,
3105 r2t
->offset
, r2t
->xfer_len
, conn
->cid
);
3107 spin_lock_bh(&cmd
->r2t_lock
);
3109 spin_unlock_bh(&cmd
->r2t_lock
);
3111 ret
= conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
3116 spin_lock_bh(&cmd
->dataout_timeout_lock
);
3117 iscsit_start_dataout_timer(cmd
, conn
);
3118 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
3124 * @recovery: If called from iscsi_task_reassign_complete_write() for
3125 * connection recovery.
3127 int iscsit_build_r2ts_for_cmd(
3128 struct iscsi_conn
*conn
,
3129 struct iscsi_cmd
*cmd
,
3133 u32 offset
= 0, xfer_len
= 0;
3135 spin_lock_bh(&cmd
->r2t_lock
);
3136 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
) {
3137 spin_unlock_bh(&cmd
->r2t_lock
);
3141 if (conn
->sess
->sess_ops
->DataSequenceInOrder
&&
3143 cmd
->r2t_offset
= max(cmd
->r2t_offset
, cmd
->write_data_done
);
3145 while (cmd
->outstanding_r2ts
< conn
->sess
->sess_ops
->MaxOutstandingR2T
) {
3146 if (conn
->sess
->sess_ops
->DataSequenceInOrder
) {
3147 offset
= cmd
->r2t_offset
;
3149 if (first_r2t
&& recovery
) {
3150 int new_data_end
= offset
+
3151 conn
->sess
->sess_ops
->MaxBurstLength
-
3152 cmd
->next_burst_len
;
3154 if (new_data_end
> cmd
->se_cmd
.data_length
)
3155 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3158 conn
->sess
->sess_ops
->MaxBurstLength
-
3159 cmd
->next_burst_len
;
3161 int new_data_end
= offset
+
3162 conn
->sess
->sess_ops
->MaxBurstLength
;
3164 if (new_data_end
> cmd
->se_cmd
.data_length
)
3165 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3167 xfer_len
= conn
->sess
->sess_ops
->MaxBurstLength
;
3170 if ((s32
)xfer_len
< 0) {
3171 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3175 cmd
->r2t_offset
+= xfer_len
;
3177 if (cmd
->r2t_offset
== cmd
->se_cmd
.data_length
)
3178 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3180 struct iscsi_seq
*seq
;
3182 seq
= iscsit_get_seq_holder_for_r2t(cmd
);
3184 spin_unlock_bh(&cmd
->r2t_lock
);
3188 offset
= seq
->offset
;
3189 xfer_len
= seq
->xfer_len
;
3191 if (cmd
->seq_send_order
== cmd
->seq_count
)
3192 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3194 cmd
->outstanding_r2ts
++;
3197 if (iscsit_add_r2t_to_list(cmd
, offset
, xfer_len
, 0, 0) < 0) {
3198 spin_unlock_bh(&cmd
->r2t_lock
);
3202 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
)
3205 spin_unlock_bh(&cmd
->r2t_lock
);
3209 EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd
);
3211 void iscsit_build_rsp_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3212 bool inc_stat_sn
, struct iscsi_scsi_rsp
*hdr
)
3215 cmd
->stat_sn
= conn
->stat_sn
++;
3217 atomic_long_inc(&conn
->sess
->rsp_pdus
);
3219 memset(hdr
, 0, ISCSI_HDR_LEN
);
3220 hdr
->opcode
= ISCSI_OP_SCSI_CMD_RSP
;
3221 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3222 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
3223 hdr
->flags
|= ISCSI_FLAG_CMD_OVERFLOW
;
3224 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3225 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
3226 hdr
->flags
|= ISCSI_FLAG_CMD_UNDERFLOW
;
3227 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3229 hdr
->response
= cmd
->iscsi_response
;
3230 hdr
->cmd_status
= cmd
->se_cmd
.scsi_status
;
3231 hdr
->itt
= cmd
->init_task_tag
;
3232 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3234 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3235 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3236 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3238 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3239 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3240 cmd
->init_task_tag
, cmd
->stat_sn
, cmd
->se_cmd
.scsi_status
,
3241 cmd
->se_cmd
.scsi_status
, conn
->cid
);
3243 EXPORT_SYMBOL(iscsit_build_rsp_pdu
);
3245 static int iscsit_send_response(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3247 struct iscsi_scsi_rsp
*hdr
= (struct iscsi_scsi_rsp
*)&cmd
->pdu
[0];
3248 bool inc_stat_sn
= (cmd
->i_state
== ISTATE_SEND_STATUS
);
3249 void *data_buf
= NULL
;
3250 u32 padding
= 0, data_buf_len
= 0;
3252 iscsit_build_rsp_pdu(cmd
, conn
, inc_stat_sn
, hdr
);
3255 * Attach SENSE DATA payload to iSCSI Response PDU
3257 if (cmd
->se_cmd
.sense_buffer
&&
3258 ((cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
3259 (cmd
->se_cmd
.se_cmd_flags
& SCF_EMULATED_TASK_SENSE
))) {
3260 put_unaligned_be16(cmd
->se_cmd
.scsi_sense_length
, cmd
->sense_buffer
);
3261 cmd
->se_cmd
.scsi_sense_length
+= sizeof (__be16
);
3263 padding
= -(cmd
->se_cmd
.scsi_sense_length
) & 3;
3264 hton24(hdr
->dlength
, (u32
)cmd
->se_cmd
.scsi_sense_length
);
3265 data_buf
= cmd
->sense_buffer
;
3266 data_buf_len
= cmd
->se_cmd
.scsi_sense_length
+ padding
;
3269 memset(cmd
->sense_buffer
+
3270 cmd
->se_cmd
.scsi_sense_length
, 0, padding
);
3271 pr_debug("Adding %u bytes of padding to"
3272 " SENSE.\n", padding
);
3275 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3277 cmd
->se_cmd
.scsi_sense_length
);
3280 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, data_buf
,
3284 static u8
iscsit_convert_tcm_tmr_rsp(struct se_tmr_req
*se_tmr
)
3286 switch (se_tmr
->response
) {
3287 case TMR_FUNCTION_COMPLETE
:
3288 return ISCSI_TMF_RSP_COMPLETE
;
3289 case TMR_TASK_DOES_NOT_EXIST
:
3290 return ISCSI_TMF_RSP_NO_TASK
;
3291 case TMR_LUN_DOES_NOT_EXIST
:
3292 return ISCSI_TMF_RSP_NO_LUN
;
3293 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
:
3294 return ISCSI_TMF_RSP_NOT_SUPPORTED
;
3295 case TMR_FUNCTION_REJECTED
:
3297 return ISCSI_TMF_RSP_REJECTED
;
3302 iscsit_build_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3303 struct iscsi_tm_rsp
*hdr
)
3305 struct se_tmr_req
*se_tmr
= cmd
->se_cmd
.se_tmr_req
;
3307 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC_RSP
;
3308 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
3309 hdr
->response
= iscsit_convert_tcm_tmr_rsp(se_tmr
);
3310 hdr
->itt
= cmd
->init_task_tag
;
3311 cmd
->stat_sn
= conn
->stat_sn
++;
3312 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3314 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3315 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3316 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3318 pr_debug("Built Task Management Response ITT: 0x%08x,"
3319 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3320 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
, conn
->cid
);
3322 EXPORT_SYMBOL(iscsit_build_task_mgt_rsp
);
3325 iscsit_send_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3327 struct iscsi_tm_rsp
*hdr
= (struct iscsi_tm_rsp
*)&cmd
->pdu
[0];
3329 iscsit_build_task_mgt_rsp(cmd
, conn
, hdr
);
3331 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
3334 #define SENDTARGETS_BUF_LIMIT 32768U
3337 iscsit_build_sendtargets_response(struct iscsi_cmd
*cmd
,
3338 enum iscsit_transport_type network_transport
,
3339 int skip_bytes
, bool *completed
)
3341 char *payload
= NULL
;
3342 struct iscsi_conn
*conn
= cmd
->conn
;
3343 struct iscsi_portal_group
*tpg
;
3344 struct iscsi_tiqn
*tiqn
;
3345 struct iscsi_tpg_np
*tpg_np
;
3346 int buffer_len
, end_of_buf
= 0, len
= 0, payload_len
= 0;
3347 int target_name_printed
;
3348 unsigned char buf
[ISCSI_IQN_LEN
+12]; /* iqn + "TargetName=" + \0 */
3349 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
= NULL
;
3352 buffer_len
= min(conn
->conn_ops
->MaxRecvDataSegmentLength
,
3353 SENDTARGETS_BUF_LIMIT
);
3355 payload
= kzalloc(buffer_len
, GFP_KERNEL
);
3360 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
3363 if (cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
) {
3364 text_ptr
= strchr(text_in
, '=');
3366 pr_err("Unable to locate '=' string in text_in:"
3372 * Skip over '=' character..
3377 spin_lock(&tiqn_lock
);
3378 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
3379 if ((cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
) &&
3380 strcmp(tiqn
->tiqn
, text_ptr
)) {
3384 target_name_printed
= 0;
3386 spin_lock(&tiqn
->tiqn_tpg_lock
);
3387 list_for_each_entry(tpg
, &tiqn
->tiqn_tpg_list
, tpg_list
) {
3389 /* If demo_mode_discovery=0 and generate_node_acls=0
3390 * (demo mode dislabed) do not return
3391 * TargetName+TargetAddress unless a NodeACL exists.
3394 if ((tpg
->tpg_attrib
.generate_node_acls
== 0) &&
3395 (tpg
->tpg_attrib
.demo_mode_discovery
== 0) &&
3396 (!target_tpg_has_node_acl(&tpg
->tpg_se_tpg
,
3397 cmd
->conn
->sess
->sess_ops
->InitiatorName
))) {
3401 spin_lock(&tpg
->tpg_state_lock
);
3402 active
= (tpg
->tpg_state
== TPG_STATE_ACTIVE
);
3403 spin_unlock(&tpg
->tpg_state_lock
);
3405 if (!active
&& tpg
->tpg_attrib
.tpg_enabled_sendtargets
)
3408 spin_lock(&tpg
->tpg_np_lock
);
3409 list_for_each_entry(tpg_np
, &tpg
->tpg_gnp_list
,
3411 struct iscsi_np
*np
= tpg_np
->tpg_np
;
3412 struct sockaddr_storage
*sockaddr
;
3414 if (np
->np_network_transport
!= network_transport
)
3417 if (!target_name_printed
) {
3418 len
= sprintf(buf
, "TargetName=%s",
3422 if ((len
+ payload_len
) > buffer_len
) {
3423 spin_unlock(&tpg
->tpg_np_lock
);
3424 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3429 if (skip_bytes
&& len
<= skip_bytes
) {
3432 memcpy(payload
+ payload_len
, buf
, len
);
3434 target_name_printed
= 1;
3435 if (len
> skip_bytes
)
3440 if (inet_addr_is_any((struct sockaddr
*)&np
->np_sockaddr
))
3441 sockaddr
= &conn
->local_sockaddr
;
3443 sockaddr
= &np
->np_sockaddr
;
3445 len
= sprintf(buf
, "TargetAddress="
3451 if ((len
+ payload_len
) > buffer_len
) {
3452 spin_unlock(&tpg
->tpg_np_lock
);
3453 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3458 if (skip_bytes
&& len
<= skip_bytes
) {
3461 memcpy(payload
+ payload_len
, buf
, len
);
3463 if (len
> skip_bytes
)
3467 spin_unlock(&tpg
->tpg_np_lock
);
3469 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3476 if (cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
)
3479 spin_unlock(&tiqn_lock
);
3481 cmd
->buf_ptr
= payload
;
3487 iscsit_build_text_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3488 struct iscsi_text_rsp
*hdr
,
3489 enum iscsit_transport_type network_transport
)
3491 int text_length
, padding
;
3492 bool completed
= true;
3494 text_length
= iscsit_build_sendtargets_response(cmd
, network_transport
,
3495 cmd
->read_data_done
,
3497 if (text_length
< 0)
3501 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
3503 hdr
->flags
= ISCSI_FLAG_TEXT_CONTINUE
;
3504 cmd
->read_data_done
+= text_length
;
3505 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF)
3506 cmd
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
3508 hdr
->opcode
= ISCSI_OP_TEXT_RSP
;
3509 padding
= ((-text_length
) & 3);
3510 hton24(hdr
->dlength
, text_length
);
3511 hdr
->itt
= cmd
->init_task_tag
;
3512 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
3513 cmd
->stat_sn
= conn
->stat_sn
++;
3514 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3516 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3518 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3519 * correctly increment MaxCmdSN for each response answering a
3520 * non immediate text request with a valid CmdSN.
3522 cmd
->maxcmdsn_inc
= 0;
3523 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3524 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3526 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3527 " Length: %u, CID: %hu F: %d C: %d\n", cmd
->init_task_tag
,
3528 cmd
->targ_xfer_tag
, cmd
->stat_sn
, text_length
, conn
->cid
,
3529 !!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
),
3530 !!(hdr
->flags
& ISCSI_FLAG_TEXT_CONTINUE
));
3532 return text_length
+ padding
;
3534 EXPORT_SYMBOL(iscsit_build_text_rsp
);
3536 static int iscsit_send_text_rsp(
3537 struct iscsi_cmd
*cmd
,
3538 struct iscsi_conn
*conn
)
3540 struct iscsi_text_rsp
*hdr
= (struct iscsi_text_rsp
*)cmd
->pdu
;
3543 text_length
= iscsit_build_text_rsp(cmd
, conn
, hdr
,
3544 conn
->conn_transport
->transport_type
);
3545 if (text_length
< 0)
3548 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
,
3554 iscsit_build_reject(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3555 struct iscsi_reject
*hdr
)
3557 hdr
->opcode
= ISCSI_OP_REJECT
;
3558 hdr
->reason
= cmd
->reject_reason
;
3559 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3560 hton24(hdr
->dlength
, ISCSI_HDR_LEN
);
3561 hdr
->ffffffff
= cpu_to_be32(0xffffffff);
3562 cmd
->stat_sn
= conn
->stat_sn
++;
3563 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3564 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3565 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3568 EXPORT_SYMBOL(iscsit_build_reject
);
3570 static int iscsit_send_reject(
3571 struct iscsi_cmd
*cmd
,
3572 struct iscsi_conn
*conn
)
3574 struct iscsi_reject
*hdr
= (struct iscsi_reject
*)&cmd
->pdu
[0];
3576 iscsit_build_reject(cmd
, conn
, hdr
);
3578 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3579 " CID: %hu\n", ntohl(hdr
->statsn
), hdr
->reason
, conn
->cid
);
3581 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
,
3586 void iscsit_thread_get_cpumask(struct iscsi_conn
*conn
)
3590 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3591 * within iscsit_start_kthreads()
3593 * Here we use bitmap_id to determine which CPU that this
3594 * iSCSI connection's RX/TX threads will be scheduled to
3597 ord
= conn
->bitmap_id
% cpumask_weight(cpu_online_mask
);
3598 for_each_online_cpu(cpu
) {
3600 cpumask_set_cpu(cpu
, conn
->conn_cpumask
);
3605 * This should never be reached..
3608 cpumask_setall(conn
->conn_cpumask
);
3612 iscsit_immediate_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3617 case ISTATE_SEND_R2T
:
3618 ret
= iscsit_send_r2t(cmd
, conn
);
3623 spin_lock_bh(&conn
->cmd_lock
);
3624 list_del_init(&cmd
->i_conn_node
);
3625 spin_unlock_bh(&conn
->cmd_lock
);
3627 iscsit_free_cmd(cmd
, false);
3629 case ISTATE_SEND_NOPIN_WANT_RESPONSE
:
3630 iscsit_mod_nopin_response_timer(conn
);
3631 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 1);
3635 case ISTATE_SEND_NOPIN_NO_RESPONSE
:
3636 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 0);
3641 pr_err("Unknown Opcode: 0x%02x ITT:"
3642 " 0x%08x, i_state: %d on CID: %hu\n",
3643 cmd
->iscsi_opcode
, cmd
->init_task_tag
, state
,
3653 EXPORT_SYMBOL(iscsit_immediate_queue
);
3656 iscsit_handle_immediate_queue(struct iscsi_conn
*conn
)
3658 struct iscsit_transport
*t
= conn
->conn_transport
;
3659 struct iscsi_queue_req
*qr
;
3660 struct iscsi_cmd
*cmd
;
3664 while ((qr
= iscsit_get_cmd_from_immediate_queue(conn
))) {
3665 atomic_set(&conn
->check_immediate_queue
, 0);
3668 kmem_cache_free(lio_qr_cache
, qr
);
3670 ret
= t
->iscsit_immediate_queue(conn
, cmd
, state
);
3679 iscsit_response_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3685 case ISTATE_SEND_DATAIN
:
3686 ret
= iscsit_send_datain(cmd
, conn
);
3691 goto check_rsp_state
;
3692 else if (ret
== 1) {
3694 spin_lock_bh(&cmd
->istate_lock
);
3695 cmd
->i_state
= ISTATE_SENT_STATUS
;
3696 spin_unlock_bh(&cmd
->istate_lock
);
3698 if (atomic_read(&conn
->check_immediate_queue
))
3702 } else if (ret
== 2) {
3703 /* Still must send status,
3704 SCF_TRANSPORT_TASK_SENSE was set */
3705 spin_lock_bh(&cmd
->istate_lock
);
3706 cmd
->i_state
= ISTATE_SEND_STATUS
;
3707 spin_unlock_bh(&cmd
->istate_lock
);
3708 state
= ISTATE_SEND_STATUS
;
3709 goto check_rsp_state
;
3713 case ISTATE_SEND_STATUS
:
3714 case ISTATE_SEND_STATUS_RECOVERY
:
3715 ret
= iscsit_send_response(cmd
, conn
);
3717 case ISTATE_SEND_LOGOUTRSP
:
3718 ret
= iscsit_send_logout(cmd
, conn
);
3720 case ISTATE_SEND_ASYNCMSG
:
3721 ret
= iscsit_send_conn_drop_async_message(
3724 case ISTATE_SEND_NOPIN
:
3725 ret
= iscsit_send_nopin(cmd
, conn
);
3727 case ISTATE_SEND_REJECT
:
3728 ret
= iscsit_send_reject(cmd
, conn
);
3730 case ISTATE_SEND_TASKMGTRSP
:
3731 ret
= iscsit_send_task_mgt_rsp(cmd
, conn
);
3734 ret
= iscsit_tmr_post_handler(cmd
, conn
);
3736 iscsit_fall_back_to_erl0(conn
->sess
);
3738 case ISTATE_SEND_TEXTRSP
:
3739 ret
= iscsit_send_text_rsp(cmd
, conn
);
3742 pr_err("Unknown Opcode: 0x%02x ITT:"
3743 " 0x%08x, i_state: %d on CID: %hu\n",
3744 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3752 case ISTATE_SEND_LOGOUTRSP
:
3753 if (!iscsit_logout_post_handler(cmd
, conn
))
3756 case ISTATE_SEND_STATUS
:
3757 case ISTATE_SEND_ASYNCMSG
:
3758 case ISTATE_SEND_NOPIN
:
3759 case ISTATE_SEND_STATUS_RECOVERY
:
3760 case ISTATE_SEND_TEXTRSP
:
3761 case ISTATE_SEND_TASKMGTRSP
:
3762 case ISTATE_SEND_REJECT
:
3763 spin_lock_bh(&cmd
->istate_lock
);
3764 cmd
->i_state
= ISTATE_SENT_STATUS
;
3765 spin_unlock_bh(&cmd
->istate_lock
);
3768 pr_err("Unknown Opcode: 0x%02x ITT:"
3769 " 0x%08x, i_state: %d on CID: %hu\n",
3770 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3771 cmd
->i_state
, conn
->cid
);
3775 if (atomic_read(&conn
->check_immediate_queue
))
3783 EXPORT_SYMBOL(iscsit_response_queue
);
3785 static int iscsit_handle_response_queue(struct iscsi_conn
*conn
)
3787 struct iscsit_transport
*t
= conn
->conn_transport
;
3788 struct iscsi_queue_req
*qr
;
3789 struct iscsi_cmd
*cmd
;
3793 while ((qr
= iscsit_get_cmd_from_response_queue(conn
))) {
3796 kmem_cache_free(lio_qr_cache
, qr
);
3798 ret
= t
->iscsit_response_queue(conn
, cmd
, state
);
3799 if (ret
== 1 || ret
< 0)
3806 int iscsi_target_tx_thread(void *arg
)
3809 struct iscsi_conn
*conn
= arg
;
3810 bool conn_freed
= false;
3813 * Allow ourselves to be interrupted by SIGINT so that a
3814 * connection recovery / failure event can be triggered externally.
3816 allow_signal(SIGINT
);
3818 while (!kthread_should_stop()) {
3820 * Ensure that both TX and RX per connection kthreads
3821 * are scheduled to run on the same CPU.
3823 iscsit_thread_check_cpumask(conn
, current
, 1);
3825 wait_event_interruptible(conn
->queues_wq
,
3826 !iscsit_conn_all_queues_empty(conn
));
3828 if (signal_pending(current
))
3832 ret
= iscsit_handle_immediate_queue(conn
);
3836 ret
= iscsit_handle_response_queue(conn
);
3839 } else if (ret
== -ECONNRESET
) {
3842 } else if (ret
< 0) {
3849 * Avoid the normal connection failure code-path if this connection
3850 * is still within LOGIN mode, and iscsi_np process context is
3851 * responsible for cleaning up the early connection failure.
3853 if (conn
->conn_state
!= TARG_CONN_STATE_IN_LOGIN
)
3854 iscsit_take_action_for_connection_exit(conn
, &conn_freed
);
3857 while (!kthread_should_stop()) {
3864 static int iscsi_target_rx_opcode(struct iscsi_conn
*conn
, unsigned char *buf
)
3866 struct iscsi_hdr
*hdr
= (struct iscsi_hdr
*)buf
;
3867 struct iscsi_cmd
*cmd
;
3870 switch (hdr
->opcode
& ISCSI_OPCODE_MASK
) {
3871 case ISCSI_OP_SCSI_CMD
:
3872 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3876 ret
= iscsit_handle_scsi_cmd(conn
, cmd
, buf
);
3878 case ISCSI_OP_SCSI_DATA_OUT
:
3879 ret
= iscsit_handle_data_out(conn
, buf
);
3881 case ISCSI_OP_NOOP_OUT
:
3883 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
3884 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3888 ret
= iscsit_handle_nop_out(conn
, cmd
, buf
);
3890 case ISCSI_OP_SCSI_TMFUNC
:
3891 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3895 ret
= iscsit_handle_task_mgt_cmd(conn
, cmd
, buf
);
3898 if (hdr
->ttt
!= cpu_to_be32(0xFFFFFFFF)) {
3899 cmd
= iscsit_find_cmd_from_itt(conn
, hdr
->itt
);
3903 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3908 ret
= iscsit_handle_text_cmd(conn
, cmd
, buf
);
3910 case ISCSI_OP_LOGOUT
:
3911 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3915 ret
= iscsit_handle_logout_cmd(conn
, cmd
, buf
);
3917 wait_for_completion_timeout(&conn
->conn_logout_comp
,
3918 SECONDS_FOR_LOGOUT_COMP
* HZ
);
3920 case ISCSI_OP_SNACK
:
3921 ret
= iscsit_handle_snack(conn
, buf
);
3924 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr
->opcode
);
3925 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
3926 pr_err("Cannot recover from unknown"
3927 " opcode while ERL=0, closing iSCSI connection.\n");
3930 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3931 " closing iSCSI connection.\n");
3938 return iscsit_add_reject(conn
, ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
3941 static bool iscsi_target_check_conn_state(struct iscsi_conn
*conn
)
3945 spin_lock_bh(&conn
->state_lock
);
3946 ret
= (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
);
3947 spin_unlock_bh(&conn
->state_lock
);
3952 static void iscsit_get_rx_pdu(struct iscsi_conn
*conn
)
3956 u32 checksum
= 0, digest
= 0;
3959 buffer
= kcalloc(ISCSI_HDR_LEN
, sizeof(*buffer
), GFP_KERNEL
);
3963 while (!kthread_should_stop()) {
3965 * Ensure that both TX and RX per connection kthreads
3966 * are scheduled to run on the same CPU.
3968 iscsit_thread_check_cpumask(conn
, current
, 0);
3970 memset(&iov
, 0, sizeof(struct kvec
));
3972 iov
.iov_base
= buffer
;
3973 iov
.iov_len
= ISCSI_HDR_LEN
;
3975 ret
= rx_data(conn
, &iov
, 1, ISCSI_HDR_LEN
);
3976 if (ret
!= ISCSI_HDR_LEN
) {
3977 iscsit_rx_thread_wait_for_tcp(conn
);
3981 if (conn
->conn_ops
->HeaderDigest
) {
3982 iov
.iov_base
= &digest
;
3983 iov
.iov_len
= ISCSI_CRC_LEN
;
3985 ret
= rx_data(conn
, &iov
, 1, ISCSI_CRC_LEN
);
3986 if (ret
!= ISCSI_CRC_LEN
) {
3987 iscsit_rx_thread_wait_for_tcp(conn
);
3991 iscsit_do_crypto_hash_buf(conn
->conn_rx_hash
, buffer
,
3992 ISCSI_HDR_LEN
, 0, NULL
,
3995 if (digest
!= checksum
) {
3996 pr_err("HeaderDigest CRC32C failed,"
3997 " received 0x%08x, computed 0x%08x\n",
4000 * Set the PDU to 0xff so it will intentionally
4001 * hit default in the switch below.
4003 memset(buffer
, 0xff, ISCSI_HDR_LEN
);
4004 atomic_long_inc(&conn
->sess
->conn_digest_errors
);
4006 pr_debug("Got HeaderDigest CRC32C"
4007 " 0x%08x\n", checksum
);
4011 if (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
)
4014 opcode
= buffer
[0] & ISCSI_OPCODE_MASK
;
4016 if (conn
->sess
->sess_ops
->SessionType
&&
4017 ((!(opcode
& ISCSI_OP_TEXT
)) ||
4018 (!(opcode
& ISCSI_OP_LOGOUT
)))) {
4019 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4020 " while in Discovery Session, rejecting.\n", opcode
);
4021 iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
4026 ret
= iscsi_target_rx_opcode(conn
, buffer
);
4034 int iscsi_target_rx_thread(void *arg
)
4037 struct iscsi_conn
*conn
= arg
;
4038 bool conn_freed
= false;
4041 * Allow ourselves to be interrupted by SIGINT so that a
4042 * connection recovery / failure event can be triggered externally.
4044 allow_signal(SIGINT
);
4046 * Wait for iscsi_post_login_handler() to complete before allowing
4047 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4049 rc
= wait_for_completion_interruptible(&conn
->rx_login_comp
);
4050 if (rc
< 0 || iscsi_target_check_conn_state(conn
))
4053 if (!conn
->conn_transport
->iscsit_get_rx_pdu
)
4056 conn
->conn_transport
->iscsit_get_rx_pdu(conn
);
4058 if (!signal_pending(current
))
4059 atomic_set(&conn
->transport_failed
, 1);
4060 iscsit_take_action_for_connection_exit(conn
, &conn_freed
);
4064 while (!kthread_should_stop()) {
4072 static void iscsit_release_commands_from_conn(struct iscsi_conn
*conn
)
4074 LIST_HEAD(tmp_list
);
4075 struct iscsi_cmd
*cmd
= NULL
, *cmd_tmp
= NULL
;
4076 struct iscsi_session
*sess
= conn
->sess
;
4078 * We expect this function to only ever be called from either RX or TX
4079 * thread context via iscsit_close_connection() once the other context
4080 * has been reset -> returned sleeping pre-handler state.
4082 spin_lock_bh(&conn
->cmd_lock
);
4083 list_splice_init(&conn
->conn_cmd_list
, &tmp_list
);
4085 list_for_each_entry_safe(cmd
, cmd_tmp
, &tmp_list
, i_conn_node
) {
4086 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
4088 if (se_cmd
->se_tfo
!= NULL
) {
4089 spin_lock_irq(&se_cmd
->t_state_lock
);
4090 if (se_cmd
->transport_state
& CMD_T_ABORTED
) {
4092 * LIO's abort path owns the cleanup for this,
4093 * so put it back on the list and let
4094 * aborted_task handle it.
4096 list_move_tail(&cmd
->i_conn_node
,
4097 &conn
->conn_cmd_list
);
4099 se_cmd
->transport_state
|= CMD_T_FABRIC_STOP
;
4101 spin_unlock_irq(&se_cmd
->t_state_lock
);
4104 spin_unlock_bh(&conn
->cmd_lock
);
4106 list_for_each_entry_safe(cmd
, cmd_tmp
, &tmp_list
, i_conn_node
) {
4107 list_del_init(&cmd
->i_conn_node
);
4109 iscsit_increment_maxcmdsn(cmd
, sess
);
4110 iscsit_free_cmd(cmd
, true);
4115 static void iscsit_stop_timers_for_cmds(
4116 struct iscsi_conn
*conn
)
4118 struct iscsi_cmd
*cmd
;
4120 spin_lock_bh(&conn
->cmd_lock
);
4121 list_for_each_entry(cmd
, &conn
->conn_cmd_list
, i_conn_node
) {
4122 if (cmd
->data_direction
== DMA_TO_DEVICE
)
4123 iscsit_stop_dataout_timer(cmd
);
4125 spin_unlock_bh(&conn
->cmd_lock
);
4128 int iscsit_close_connection(
4129 struct iscsi_conn
*conn
)
4131 int conn_logout
= (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
);
4132 struct iscsi_session
*sess
= conn
->sess
;
4134 pr_debug("Closing iSCSI connection CID %hu on SID:"
4135 " %u\n", conn
->cid
, sess
->sid
);
4137 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4138 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4139 * sleeping and the logout response never got sent because the
4140 * connection failed.
4142 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4143 * to signal logout response TX interrupt completion. Go ahead and skip
4144 * this for iser since isert_rx_opcode() does not wait on logout failure,
4145 * and to avoid iscsi_conn pointer dereference in iser-target code.
4147 if (!conn
->conn_transport
->rdma_shutdown
)
4148 complete(&conn
->conn_logout_comp
);
4150 if (!strcmp(current
->comm
, ISCSI_RX_THREAD_NAME
)) {
4151 if (conn
->tx_thread
&&
4152 cmpxchg(&conn
->tx_thread_active
, true, false)) {
4153 send_sig(SIGINT
, conn
->tx_thread
, 1);
4154 kthread_stop(conn
->tx_thread
);
4156 } else if (!strcmp(current
->comm
, ISCSI_TX_THREAD_NAME
)) {
4157 if (conn
->rx_thread
&&
4158 cmpxchg(&conn
->rx_thread_active
, true, false)) {
4159 send_sig(SIGINT
, conn
->rx_thread
, 1);
4160 kthread_stop(conn
->rx_thread
);
4164 spin_lock(&iscsit_global
->ts_bitmap_lock
);
4165 bitmap_release_region(iscsit_global
->ts_bitmap
, conn
->bitmap_id
,
4167 spin_unlock(&iscsit_global
->ts_bitmap_lock
);
4169 iscsit_stop_timers_for_cmds(conn
);
4170 iscsit_stop_nopin_response_timer(conn
);
4171 iscsit_stop_nopin_timer(conn
);
4173 if (conn
->conn_transport
->iscsit_wait_conn
)
4174 conn
->conn_transport
->iscsit_wait_conn(conn
);
4177 * During Connection recovery drop unacknowledged out of order
4178 * commands for this connection, and prepare the other commands
4181 * During normal operation clear the out of order commands (but
4182 * do not free the struct iscsi_ooo_cmdsn's) and release all
4183 * struct iscsi_cmds.
4185 if (atomic_read(&conn
->connection_recovery
)) {
4186 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn
);
4187 iscsit_prepare_cmds_for_reallegiance(conn
);
4189 iscsit_clear_ooo_cmdsns_for_conn(conn
);
4190 iscsit_release_commands_from_conn(conn
);
4192 iscsit_free_queue_reqs_for_conn(conn
);
4195 * Handle decrementing session or connection usage count if
4196 * a logout response was not able to be sent because the
4197 * connection failed. Fall back to Session Recovery here.
4199 if (atomic_read(&conn
->conn_logout_remove
)) {
4200 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) {
4201 iscsit_dec_conn_usage_count(conn
);
4202 iscsit_dec_session_usage_count(sess
);
4204 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
)
4205 iscsit_dec_conn_usage_count(conn
);
4207 atomic_set(&conn
->conn_logout_remove
, 0);
4208 atomic_set(&sess
->session_reinstatement
, 0);
4209 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4212 spin_lock_bh(&sess
->conn_lock
);
4213 list_del(&conn
->conn_list
);
4216 * Attempt to let the Initiator know this connection failed by
4217 * sending an Connection Dropped Async Message on another
4218 * active connection.
4220 if (atomic_read(&conn
->connection_recovery
))
4221 iscsit_build_conn_drop_async_message(conn
);
4223 spin_unlock_bh(&sess
->conn_lock
);
4226 * If connection reinstatement is being performed on this connection,
4227 * up the connection reinstatement semaphore that is being blocked on
4228 * in iscsit_cause_connection_reinstatement().
4230 spin_lock_bh(&conn
->state_lock
);
4231 if (atomic_read(&conn
->sleep_on_conn_wait_comp
)) {
4232 spin_unlock_bh(&conn
->state_lock
);
4233 complete(&conn
->conn_wait_comp
);
4234 wait_for_completion(&conn
->conn_post_wait_comp
);
4235 spin_lock_bh(&conn
->state_lock
);
4239 * If connection reinstatement is being performed on this connection
4240 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4241 * connection wait rcfr semaphore that is being blocked on
4242 * an iscsit_connection_reinstatement_rcfr().
4244 if (atomic_read(&conn
->connection_wait_rcfr
)) {
4245 spin_unlock_bh(&conn
->state_lock
);
4246 complete(&conn
->conn_wait_rcfr_comp
);
4247 wait_for_completion(&conn
->conn_post_wait_comp
);
4248 spin_lock_bh(&conn
->state_lock
);
4250 atomic_set(&conn
->connection_reinstatement
, 1);
4251 spin_unlock_bh(&conn
->state_lock
);
4254 * If any other processes are accessing this connection pointer we
4255 * must wait until they have completed.
4257 iscsit_check_conn_usage_count(conn
);
4259 ahash_request_free(conn
->conn_tx_hash
);
4260 if (conn
->conn_rx_hash
) {
4261 struct crypto_ahash
*tfm
;
4263 tfm
= crypto_ahash_reqtfm(conn
->conn_rx_hash
);
4264 ahash_request_free(conn
->conn_rx_hash
);
4265 crypto_free_ahash(tfm
);
4269 sock_release(conn
->sock
);
4271 if (conn
->conn_transport
->iscsit_free_conn
)
4272 conn
->conn_transport
->iscsit_free_conn(conn
);
4274 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4275 conn
->conn_state
= TARG_CONN_STATE_FREE
;
4276 iscsit_free_conn(conn
);
4278 spin_lock_bh(&sess
->conn_lock
);
4279 atomic_dec(&sess
->nconn
);
4280 pr_debug("Decremented iSCSI connection count to %hu from node:"
4281 " %s\n", atomic_read(&sess
->nconn
),
4282 sess
->sess_ops
->InitiatorName
);
4284 * Make sure that if one connection fails in an non ERL=2 iSCSI
4285 * Session that they all fail.
4287 if ((sess
->sess_ops
->ErrorRecoveryLevel
!= 2) && !conn_logout
&&
4288 !atomic_read(&sess
->session_logout
))
4289 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4292 * If this was not the last connection in the session, and we are
4293 * performing session reinstatement or falling back to ERL=0, call
4294 * iscsit_stop_session() without sleeping to shutdown the other
4295 * active connections.
4297 if (atomic_read(&sess
->nconn
)) {
4298 if (!atomic_read(&sess
->session_reinstatement
) &&
4299 !atomic_read(&sess
->session_fall_back_to_erl0
)) {
4300 spin_unlock_bh(&sess
->conn_lock
);
4303 if (!atomic_read(&sess
->session_stop_active
)) {
4304 atomic_set(&sess
->session_stop_active
, 1);
4305 spin_unlock_bh(&sess
->conn_lock
);
4306 iscsit_stop_session(sess
, 0, 0);
4309 spin_unlock_bh(&sess
->conn_lock
);
4314 * If this was the last connection in the session and one of the
4315 * following is occurring:
4317 * Session Reinstatement is not being performed, and are falling back
4318 * to ERL=0 call iscsit_close_session().
4320 * Session Logout was requested. iscsit_close_session() will be called
4323 * Session Continuation is not being performed, start the Time2Retain
4324 * handler and check if sleep_on_sess_wait_sem is active.
4326 if (!atomic_read(&sess
->session_reinstatement
) &&
4327 atomic_read(&sess
->session_fall_back_to_erl0
)) {
4328 spin_unlock_bh(&sess
->conn_lock
);
4329 complete_all(&sess
->session_wait_comp
);
4330 iscsit_close_session(sess
);
4333 } else if (atomic_read(&sess
->session_logout
)) {
4334 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4335 sess
->session_state
= TARG_SESS_STATE_FREE
;
4337 if (atomic_read(&sess
->session_close
)) {
4338 spin_unlock_bh(&sess
->conn_lock
);
4339 complete_all(&sess
->session_wait_comp
);
4340 iscsit_close_session(sess
);
4342 spin_unlock_bh(&sess
->conn_lock
);
4347 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4348 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4350 if (!atomic_read(&sess
->session_continuation
))
4351 iscsit_start_time2retain_handler(sess
);
4353 if (atomic_read(&sess
->session_close
)) {
4354 spin_unlock_bh(&sess
->conn_lock
);
4355 complete_all(&sess
->session_wait_comp
);
4356 iscsit_close_session(sess
);
4358 spin_unlock_bh(&sess
->conn_lock
);
4366 * If the iSCSI Session for the iSCSI Initiator Node exists,
4367 * forcefully shutdown the iSCSI NEXUS.
4369 int iscsit_close_session(struct iscsi_session
*sess
)
4371 struct iscsi_portal_group
*tpg
= sess
->tpg
;
4372 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4374 if (atomic_read(&sess
->nconn
)) {
4375 pr_err("%d connection(s) still exist for iSCSI session"
4376 " to %s\n", atomic_read(&sess
->nconn
),
4377 sess
->sess_ops
->InitiatorName
);
4381 spin_lock_bh(&se_tpg
->session_lock
);
4382 atomic_set(&sess
->session_logout
, 1);
4383 atomic_set(&sess
->session_reinstatement
, 1);
4384 iscsit_stop_time2retain_timer(sess
);
4385 spin_unlock_bh(&se_tpg
->session_lock
);
4388 * transport_deregister_session_configfs() will clear the
4389 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4390 * can be setting it again with __transport_register_session() in
4391 * iscsi_post_login_handler() again after the iscsit_stop_session()
4392 * completes in iscsi_np context.
4394 transport_deregister_session_configfs(sess
->se_sess
);
4397 * If any other processes are accessing this session pointer we must
4398 * wait until they have completed. If we are in an interrupt (the
4399 * time2retain handler) and contain and active session usage count we
4400 * restart the timer and exit.
4402 if (!in_interrupt()) {
4403 iscsit_check_session_usage_count(sess
);
4405 if (iscsit_check_session_usage_count(sess
) == 2) {
4406 atomic_set(&sess
->session_logout
, 0);
4407 iscsit_start_time2retain_handler(sess
);
4412 transport_deregister_session(sess
->se_sess
);
4414 if (sess
->sess_ops
->ErrorRecoveryLevel
== 2)
4415 iscsit_free_connection_recovery_entries(sess
);
4417 iscsit_free_all_ooo_cmdsns(sess
);
4419 spin_lock_bh(&se_tpg
->session_lock
);
4420 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4421 sess
->session_state
= TARG_SESS_STATE_FREE
;
4422 pr_debug("Released iSCSI session from node: %s\n",
4423 sess
->sess_ops
->InitiatorName
);
4426 tpg
->tpg_tiqn
->tiqn_nsessions
--;
4428 pr_debug("Decremented number of active iSCSI Sessions on"
4429 " iSCSI TPG: %hu to %u\n", tpg
->tpgt
, tpg
->nsessions
);
4431 ida_free(&sess_ida
, sess
->session_index
);
4432 kfree(sess
->sess_ops
);
4433 sess
->sess_ops
= NULL
;
4434 spin_unlock_bh(&se_tpg
->session_lock
);
4440 static void iscsit_logout_post_handler_closesession(
4441 struct iscsi_conn
*conn
)
4443 struct iscsi_session
*sess
= conn
->sess
;
4446 * Traditional iscsi/tcp will invoke this logic from TX thread
4447 * context during session logout, so clear tx_thread_active and
4448 * sleep if iscsit_close_connection() has not already occured.
4450 * Since iser-target invokes this logic from it's own workqueue,
4451 * always sleep waiting for RX/TX thread shutdown to complete
4452 * within iscsit_close_connection().
4454 if (!conn
->conn_transport
->rdma_shutdown
) {
4455 sleep
= cmpxchg(&conn
->tx_thread_active
, true, false);
4460 atomic_set(&conn
->conn_logout_remove
, 0);
4461 complete(&conn
->conn_logout_comp
);
4463 iscsit_dec_conn_usage_count(conn
);
4464 atomic_set(&sess
->session_close
, 1);
4465 iscsit_stop_session(sess
, sleep
, sleep
);
4466 iscsit_dec_session_usage_count(sess
);
4469 static void iscsit_logout_post_handler_samecid(
4470 struct iscsi_conn
*conn
)
4474 if (!conn
->conn_transport
->rdma_shutdown
) {
4475 sleep
= cmpxchg(&conn
->tx_thread_active
, true, false);
4480 atomic_set(&conn
->conn_logout_remove
, 0);
4481 complete(&conn
->conn_logout_comp
);
4483 iscsit_cause_connection_reinstatement(conn
, sleep
);
4484 iscsit_dec_conn_usage_count(conn
);
4487 static void iscsit_logout_post_handler_diffcid(
4488 struct iscsi_conn
*conn
,
4491 struct iscsi_conn
*l_conn
;
4492 struct iscsi_session
*sess
= conn
->sess
;
4493 bool conn_found
= false;
4498 spin_lock_bh(&sess
->conn_lock
);
4499 list_for_each_entry(l_conn
, &sess
->sess_conn_list
, conn_list
) {
4500 if (l_conn
->cid
== cid
) {
4501 iscsit_inc_conn_usage_count(l_conn
);
4506 spin_unlock_bh(&sess
->conn_lock
);
4512 l_conn
->sock
->ops
->shutdown(l_conn
->sock
, RCV_SHUTDOWN
);
4514 spin_lock_bh(&l_conn
->state_lock
);
4515 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4516 l_conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
4517 spin_unlock_bh(&l_conn
->state_lock
);
4519 iscsit_cause_connection_reinstatement(l_conn
, 1);
4520 iscsit_dec_conn_usage_count(l_conn
);
4524 * Return of 0 causes the TX thread to restart.
4526 int iscsit_logout_post_handler(
4527 struct iscsi_cmd
*cmd
,
4528 struct iscsi_conn
*conn
)
4532 switch (cmd
->logout_reason
) {
4533 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
4534 switch (cmd
->logout_response
) {
4535 case ISCSI_LOGOUT_SUCCESS
:
4536 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4538 iscsit_logout_post_handler_closesession(conn
);
4542 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
4543 if (conn
->cid
== cmd
->logout_cid
) {
4544 switch (cmd
->logout_response
) {
4545 case ISCSI_LOGOUT_SUCCESS
:
4546 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4548 iscsit_logout_post_handler_samecid(conn
);
4552 switch (cmd
->logout_response
) {
4553 case ISCSI_LOGOUT_SUCCESS
:
4554 iscsit_logout_post_handler_diffcid(conn
,
4557 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4558 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4565 case ISCSI_LOGOUT_REASON_RECOVERY
:
4566 switch (cmd
->logout_response
) {
4567 case ISCSI_LOGOUT_SUCCESS
:
4568 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4569 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
:
4570 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4582 EXPORT_SYMBOL(iscsit_logout_post_handler
);
4584 void iscsit_fail_session(struct iscsi_session
*sess
)
4586 struct iscsi_conn
*conn
;
4588 spin_lock_bh(&sess
->conn_lock
);
4589 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
) {
4590 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4591 conn
->conn_state
= TARG_CONN_STATE_CLEANUP_WAIT
;
4593 spin_unlock_bh(&sess
->conn_lock
);
4595 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4596 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4599 void iscsit_stop_session(
4600 struct iscsi_session
*sess
,
4602 int connection_sleep
)
4604 u16 conn_count
= atomic_read(&sess
->nconn
);
4605 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4608 spin_lock_bh(&sess
->conn_lock
);
4610 if (connection_sleep
) {
4611 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4613 if (conn_count
== 0)
4616 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4619 iscsit_inc_conn_usage_count(conn_tmp
);
4622 iscsit_inc_conn_usage_count(conn
);
4624 spin_unlock_bh(&sess
->conn_lock
);
4625 iscsit_cause_connection_reinstatement(conn
, 1);
4626 spin_lock_bh(&sess
->conn_lock
);
4628 iscsit_dec_conn_usage_count(conn
);
4630 iscsit_dec_conn_usage_count(conn_tmp
);
4634 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
)
4635 iscsit_cause_connection_reinstatement(conn
, 0);
4638 if (session_sleep
&& atomic_read(&sess
->nconn
)) {
4639 spin_unlock_bh(&sess
->conn_lock
);
4640 wait_for_completion(&sess
->session_wait_comp
);
4642 spin_unlock_bh(&sess
->conn_lock
);
4645 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group
*tpg
, int force
)
4647 struct iscsi_session
*sess
;
4648 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4649 struct se_session
*se_sess
, *se_sess_tmp
;
4650 LIST_HEAD(free_list
);
4651 int session_count
= 0;
4653 spin_lock_bh(&se_tpg
->session_lock
);
4654 if (tpg
->nsessions
&& !force
) {
4655 spin_unlock_bh(&se_tpg
->session_lock
);
4659 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &se_tpg
->tpg_sess_list
,
4661 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4663 spin_lock(&sess
->conn_lock
);
4664 if (atomic_read(&sess
->session_fall_back_to_erl0
) ||
4665 atomic_read(&sess
->session_logout
) ||
4666 atomic_read(&sess
->session_close
) ||
4667 (sess
->time2retain_timer_flags
& ISCSI_TF_EXPIRED
)) {
4668 spin_unlock(&sess
->conn_lock
);
4671 iscsit_inc_session_usage_count(sess
);
4672 atomic_set(&sess
->session_reinstatement
, 1);
4673 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4674 atomic_set(&sess
->session_close
, 1);
4675 spin_unlock(&sess
->conn_lock
);
4677 list_move_tail(&se_sess
->sess_list
, &free_list
);
4679 spin_unlock_bh(&se_tpg
->session_lock
);
4681 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &free_list
, sess_list
) {
4682 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4684 list_del_init(&se_sess
->sess_list
);
4685 iscsit_stop_session(sess
, 1, 1);
4686 iscsit_dec_session_usage_count(sess
);
4690 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4691 " Group: %hu\n", session_count
, tpg
->tpgt
);
4695 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4696 MODULE_VERSION("4.1.x");
4697 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4698 MODULE_LICENSE("GPL");
4700 module_init(iscsi_target_init_module
);
4701 module_exit(iscsi_target_cleanup_module
);