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 !(cmd
->se_cmd
.transport_state
& CMD_T_FABRIC_STOP
))
488 list_del_init(&cmd
->i_conn_node
);
489 spin_unlock_bh(&conn
->cmd_lock
);
491 __iscsit_free_cmd(cmd
, true);
493 EXPORT_SYMBOL(iscsit_aborted_task
);
495 static void iscsit_do_crypto_hash_buf(struct ahash_request
*, const void *,
496 u32
, u32
, const void *, void *);
497 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn
*);
500 iscsit_xmit_nondatain_pdu(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
501 const void *data_buf
, u32 data_buf_len
)
503 struct iscsi_hdr
*hdr
= (struct iscsi_hdr
*)cmd
->pdu
;
505 u32 niov
= 0, tx_size
= ISCSI_HDR_LEN
;
508 iov
= &cmd
->iov_misc
[0];
509 iov
[niov
].iov_base
= cmd
->pdu
;
510 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
512 if (conn
->conn_ops
->HeaderDigest
) {
513 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
515 iscsit_do_crypto_hash_buf(conn
->conn_tx_hash
, hdr
,
516 ISCSI_HDR_LEN
, 0, NULL
,
519 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
520 tx_size
+= ISCSI_CRC_LEN
;
521 pr_debug("Attaching CRC32C HeaderDigest"
522 " to opcode 0x%x 0x%08x\n",
523 hdr
->opcode
, *header_digest
);
527 u32 padding
= ((-data_buf_len
) & 3);
529 iov
[niov
].iov_base
= (void *)data_buf
;
530 iov
[niov
++].iov_len
= data_buf_len
;
531 tx_size
+= data_buf_len
;
534 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
535 iov
[niov
++].iov_len
= padding
;
537 pr_debug("Attaching %u additional"
538 " padding bytes.\n", padding
);
541 if (conn
->conn_ops
->DataDigest
) {
542 iscsit_do_crypto_hash_buf(conn
->conn_tx_hash
,
543 data_buf
, data_buf_len
,
544 padding
, &cmd
->pad_bytes
,
547 iov
[niov
].iov_base
= &cmd
->data_crc
;
548 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
549 tx_size
+= ISCSI_CRC_LEN
;
550 pr_debug("Attached DataDigest for %u"
551 " bytes opcode 0x%x, CRC 0x%08x\n",
552 data_buf_len
, hdr
->opcode
, cmd
->data_crc
);
556 cmd
->iov_misc_count
= niov
;
557 cmd
->tx_size
= tx_size
;
559 ret
= iscsit_send_tx_data(cmd
, conn
, 1);
561 iscsit_tx_thread_wait_for_tcp(conn
);
568 static int iscsit_map_iovec(struct iscsi_cmd
*cmd
, struct kvec
*iov
, int nvec
,
569 u32 data_offset
, u32 data_length
);
570 static void iscsit_unmap_iovec(struct iscsi_cmd
*);
571 static u32
iscsit_do_crypto_hash_sg(struct ahash_request
*, struct iscsi_cmd
*,
572 u32
, u32
, u32
, u8
*);
574 iscsit_xmit_datain_pdu(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
575 const struct iscsi_datain
*datain
)
578 u32 iov_count
= 0, tx_size
= 0;
581 iov
= &cmd
->iov_data
[0];
582 iov
[iov_count
].iov_base
= cmd
->pdu
;
583 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
584 tx_size
+= ISCSI_HDR_LEN
;
586 if (conn
->conn_ops
->HeaderDigest
) {
587 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
589 iscsit_do_crypto_hash_buf(conn
->conn_tx_hash
, cmd
->pdu
,
590 ISCSI_HDR_LEN
, 0, NULL
,
593 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
594 tx_size
+= ISCSI_CRC_LEN
;
596 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
600 iov_ret
= iscsit_map_iovec(cmd
, &cmd
->iov_data
[iov_count
],
601 cmd
->orig_iov_data_count
- (iov_count
+ 2),
602 datain
->offset
, datain
->length
);
606 iov_count
+= iov_ret
;
607 tx_size
+= datain
->length
;
609 cmd
->padding
= ((-datain
->length
) & 3);
611 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
612 iov
[iov_count
++].iov_len
= cmd
->padding
;
613 tx_size
+= cmd
->padding
;
615 pr_debug("Attaching %u padding bytes\n", cmd
->padding
);
618 if (conn
->conn_ops
->DataDigest
) {
619 cmd
->data_crc
= iscsit_do_crypto_hash_sg(conn
->conn_tx_hash
,
625 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
626 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
627 tx_size
+= ISCSI_CRC_LEN
;
629 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
630 datain
->length
+ cmd
->padding
, cmd
->data_crc
);
633 cmd
->iov_data_count
= iov_count
;
634 cmd
->tx_size
= tx_size
;
636 ret
= iscsit_fe_sendpage_sg(cmd
, conn
);
638 iscsit_unmap_iovec(cmd
);
641 iscsit_tx_thread_wait_for_tcp(conn
);
648 static int iscsit_xmit_pdu(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
649 struct iscsi_datain_req
*dr
, const void *buf
,
653 return iscsit_xmit_datain_pdu(conn
, cmd
, buf
);
655 return iscsit_xmit_nondatain_pdu(conn
, cmd
, buf
, buf_len
);
658 static enum target_prot_op
iscsit_get_sup_prot_ops(struct iscsi_conn
*conn
)
660 return TARGET_PROT_NORMAL
;
663 static struct iscsit_transport iscsi_target_transport
= {
665 .transport_type
= ISCSI_TCP
,
666 .rdma_shutdown
= false,
668 .iscsit_setup_np
= iscsit_setup_np
,
669 .iscsit_accept_np
= iscsit_accept_np
,
670 .iscsit_free_np
= iscsit_free_np
,
671 .iscsit_get_login_rx
= iscsit_get_login_rx
,
672 .iscsit_put_login_tx
= iscsit_put_login_tx
,
673 .iscsit_get_dataout
= iscsit_build_r2ts_for_cmd
,
674 .iscsit_immediate_queue
= iscsit_immediate_queue
,
675 .iscsit_response_queue
= iscsit_response_queue
,
676 .iscsit_queue_data_in
= iscsit_queue_rsp
,
677 .iscsit_queue_status
= iscsit_queue_rsp
,
678 .iscsit_aborted_task
= iscsit_aborted_task
,
679 .iscsit_xmit_pdu
= iscsit_xmit_pdu
,
680 .iscsit_get_rx_pdu
= iscsit_get_rx_pdu
,
681 .iscsit_get_sup_prot_ops
= iscsit_get_sup_prot_ops
,
684 static int __init
iscsi_target_init_module(void)
688 pr_debug("iSCSI-Target "ISCSIT_VERSION
"\n");
689 iscsit_global
= kzalloc(sizeof(*iscsit_global
), GFP_KERNEL
);
693 spin_lock_init(&iscsit_global
->ts_bitmap_lock
);
694 mutex_init(&auth_id_lock
);
697 ret
= target_register_template(&iscsi_ops
);
701 size
= BITS_TO_LONGS(ISCSIT_BITMAP_BITS
) * sizeof(long);
702 iscsit_global
->ts_bitmap
= vzalloc(size
);
703 if (!iscsit_global
->ts_bitmap
)
706 lio_qr_cache
= kmem_cache_create("lio_qr_cache",
707 sizeof(struct iscsi_queue_req
),
708 __alignof__(struct iscsi_queue_req
), 0, NULL
);
710 pr_err("Unable to kmem_cache_create() for"
715 lio_dr_cache
= kmem_cache_create("lio_dr_cache",
716 sizeof(struct iscsi_datain_req
),
717 __alignof__(struct iscsi_datain_req
), 0, NULL
);
719 pr_err("Unable to kmem_cache_create() for"
724 lio_ooo_cache
= kmem_cache_create("lio_ooo_cache",
725 sizeof(struct iscsi_ooo_cmdsn
),
726 __alignof__(struct iscsi_ooo_cmdsn
), 0, NULL
);
727 if (!lio_ooo_cache
) {
728 pr_err("Unable to kmem_cache_create() for"
733 lio_r2t_cache
= kmem_cache_create("lio_r2t_cache",
734 sizeof(struct iscsi_r2t
), __alignof__(struct iscsi_r2t
),
736 if (!lio_r2t_cache
) {
737 pr_err("Unable to kmem_cache_create() for"
742 iscsit_register_transport(&iscsi_target_transport
);
744 if (iscsit_load_discovery_tpg() < 0)
749 iscsit_unregister_transport(&iscsi_target_transport
);
750 kmem_cache_destroy(lio_r2t_cache
);
752 kmem_cache_destroy(lio_ooo_cache
);
754 kmem_cache_destroy(lio_dr_cache
);
756 kmem_cache_destroy(lio_qr_cache
);
758 vfree(iscsit_global
->ts_bitmap
);
760 /* XXX: this probably wants it to be it's own unwind step.. */
761 if (iscsit_global
->discovery_tpg
)
762 iscsit_tpg_disable_portal_group(iscsit_global
->discovery_tpg
, 1);
763 target_unregister_template(&iscsi_ops
);
765 kfree(iscsit_global
);
769 static void __exit
iscsi_target_cleanup_module(void)
771 iscsit_release_discovery_tpg();
772 iscsit_unregister_transport(&iscsi_target_transport
);
773 kmem_cache_destroy(lio_qr_cache
);
774 kmem_cache_destroy(lio_dr_cache
);
775 kmem_cache_destroy(lio_ooo_cache
);
776 kmem_cache_destroy(lio_r2t_cache
);
779 * Shutdown discovery sessions and disable discovery TPG
781 if (iscsit_global
->discovery_tpg
)
782 iscsit_tpg_disable_portal_group(iscsit_global
->discovery_tpg
, 1);
784 target_unregister_template(&iscsi_ops
);
786 vfree(iscsit_global
->ts_bitmap
);
787 kfree(iscsit_global
);
790 int iscsit_add_reject(
791 struct iscsi_conn
*conn
,
795 struct iscsi_cmd
*cmd
;
797 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
801 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
802 cmd
->reject_reason
= reason
;
804 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
806 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
807 iscsit_free_cmd(cmd
, false);
811 spin_lock_bh(&conn
->cmd_lock
);
812 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
813 spin_unlock_bh(&conn
->cmd_lock
);
815 cmd
->i_state
= ISTATE_SEND_REJECT
;
816 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
820 EXPORT_SYMBOL(iscsit_add_reject
);
822 static int iscsit_add_reject_from_cmd(
823 struct iscsi_cmd
*cmd
,
828 struct iscsi_conn
*conn
;
829 const bool do_put
= cmd
->se_cmd
.se_tfo
!= NULL
;
832 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
838 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
839 cmd
->reject_reason
= reason
;
841 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
843 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
844 iscsit_free_cmd(cmd
, false);
849 spin_lock_bh(&conn
->cmd_lock
);
850 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
851 spin_unlock_bh(&conn
->cmd_lock
);
854 cmd
->i_state
= ISTATE_SEND_REJECT
;
855 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
857 * Perform the kref_put now if se_cmd has already been setup by
858 * scsit_setup_scsi_cmd()
861 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
862 target_put_sess_cmd(&cmd
->se_cmd
);
867 static int iscsit_add_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
,
870 return iscsit_add_reject_from_cmd(cmd
, reason
, true, buf
);
873 int iscsit_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
, unsigned char *buf
)
875 return iscsit_add_reject_from_cmd(cmd
, reason
, false, buf
);
877 EXPORT_SYMBOL(iscsit_reject_cmd
);
880 * Map some portion of the allocated scatterlist to an iovec, suitable for
881 * kernel sockets to copy data in/out.
883 static int iscsit_map_iovec(struct iscsi_cmd
*cmd
, struct kvec
*iov
, int nvec
,
884 u32 data_offset
, u32 data_length
)
886 u32 i
= 0, orig_data_length
= data_length
;
887 struct scatterlist
*sg
;
888 unsigned int page_off
;
891 * We know each entry in t_data_sg contains a page.
893 u32 ent
= data_offset
/ PAGE_SIZE
;
898 if (ent
>= cmd
->se_cmd
.t_data_nents
) {
899 pr_err("Initial page entry out-of-bounds\n");
903 sg
= &cmd
->se_cmd
.t_data_sg
[ent
];
904 page_off
= (data_offset
% PAGE_SIZE
);
906 cmd
->first_data_sg
= sg
;
907 cmd
->first_data_sg_off
= page_off
;
909 while (data_length
) {
912 if (WARN_ON_ONCE(!sg
|| i
>= nvec
))
915 cur_len
= min_t(u32
, data_length
, sg
->length
- page_off
);
917 iov
[i
].iov_base
= kmap(sg_page(sg
)) + sg
->offset
+ page_off
;
918 iov
[i
].iov_len
= cur_len
;
920 data_length
-= cur_len
;
926 cmd
->kmapped_nents
= i
;
931 pr_err("offset %d + length %d overflow; %d/%d; sg-list:\n",
932 data_offset
, orig_data_length
, i
, nvec
);
933 for_each_sg(cmd
->se_cmd
.t_data_sg
, sg
,
934 cmd
->se_cmd
.t_data_nents
, i
) {
935 pr_err("[%d] off %d len %d\n",
936 i
, sg
->offset
, sg
->length
);
941 static void iscsit_unmap_iovec(struct iscsi_cmd
*cmd
)
944 struct scatterlist
*sg
;
946 sg
= cmd
->first_data_sg
;
948 for (i
= 0; i
< cmd
->kmapped_nents
; i
++)
949 kunmap(sg_page(&sg
[i
]));
952 static void iscsit_ack_from_expstatsn(struct iscsi_conn
*conn
, u32 exp_statsn
)
955 struct iscsi_cmd
*cmd
, *cmd_p
;
957 conn
->exp_statsn
= exp_statsn
;
959 if (conn
->sess
->sess_ops
->RDMAExtensions
)
962 spin_lock_bh(&conn
->cmd_lock
);
963 list_for_each_entry_safe(cmd
, cmd_p
, &conn
->conn_cmd_list
, i_conn_node
) {
964 spin_lock(&cmd
->istate_lock
);
965 if ((cmd
->i_state
== ISTATE_SENT_STATUS
) &&
966 iscsi_sna_lt(cmd
->stat_sn
, exp_statsn
)) {
967 cmd
->i_state
= ISTATE_REMOVE
;
968 spin_unlock(&cmd
->istate_lock
);
969 list_move_tail(&cmd
->i_conn_node
, &ack_list
);
972 spin_unlock(&cmd
->istate_lock
);
974 spin_unlock_bh(&conn
->cmd_lock
);
976 list_for_each_entry_safe(cmd
, cmd_p
, &ack_list
, i_conn_node
) {
977 list_del_init(&cmd
->i_conn_node
);
978 iscsit_free_cmd(cmd
, false);
982 static int iscsit_allocate_iovecs(struct iscsi_cmd
*cmd
)
984 u32 iov_count
= max(1UL, DIV_ROUND_UP(cmd
->se_cmd
.data_length
, PAGE_SIZE
));
986 iov_count
+= ISCSI_IOV_DATA_BUFFER
;
987 cmd
->iov_data
= kcalloc(iov_count
, sizeof(*cmd
->iov_data
), GFP_KERNEL
);
991 cmd
->orig_iov_data_count
= iov_count
;
995 int iscsit_setup_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
998 int data_direction
, payload_length
;
999 struct iscsi_scsi_req
*hdr
;
1000 int iscsi_task_attr
;
1003 atomic_long_inc(&conn
->sess
->cmd_pdus
);
1005 hdr
= (struct iscsi_scsi_req
*) buf
;
1006 payload_length
= ntoh24(hdr
->dlength
);
1008 /* FIXME; Add checks for AdditionalHeaderSegment */
1010 if (!(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) &&
1011 !(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
1012 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1013 " not set. Bad iSCSI Initiator.\n");
1014 return iscsit_add_reject_cmd(cmd
,
1015 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1018 if (((hdr
->flags
& ISCSI_FLAG_CMD_READ
) ||
1019 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) && !hdr
->data_length
) {
1021 * From RFC-3720 Section 10.3.1:
1023 * "Either or both of R and W MAY be 1 when either the
1024 * Expected Data Transfer Length and/or Bidirectional Read
1025 * Expected Data Transfer Length are 0"
1027 * For this case, go ahead and clear the unnecssary bits
1028 * to avoid any confusion with ->data_direction.
1030 hdr
->flags
&= ~ISCSI_FLAG_CMD_READ
;
1031 hdr
->flags
&= ~ISCSI_FLAG_CMD_WRITE
;
1033 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
1034 " set when Expected Data Transfer Length is 0 for"
1035 " CDB: 0x%02x, Fixing up flags\n", hdr
->cdb
[0]);
1038 if (!(hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
1039 !(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) && (hdr
->data_length
!= 0)) {
1040 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1041 " MUST be set if Expected Data Transfer Length is not 0."
1042 " Bad iSCSI Initiator\n");
1043 return iscsit_add_reject_cmd(cmd
,
1044 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1047 if ((hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
1048 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) {
1049 pr_err("Bidirectional operations not supported!\n");
1050 return iscsit_add_reject_cmd(cmd
,
1051 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1054 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
1055 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1056 " Scsi Command PDU.\n");
1057 return iscsit_add_reject_cmd(cmd
,
1058 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1061 if (payload_length
&& !conn
->sess
->sess_ops
->ImmediateData
) {
1062 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1063 " protocol error.\n", payload_length
);
1064 return iscsit_add_reject_cmd(cmd
,
1065 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1068 if ((be32_to_cpu(hdr
->data_length
) == payload_length
) &&
1069 (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
))) {
1070 pr_err("Expected Data Transfer Length and Length of"
1071 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1072 " bit is not set protocol error\n");
1073 return iscsit_add_reject_cmd(cmd
,
1074 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1077 if (payload_length
> be32_to_cpu(hdr
->data_length
)) {
1078 pr_err("DataSegmentLength: %u is greater than"
1079 " EDTL: %u, protocol error.\n", payload_length
,
1081 return iscsit_add_reject_cmd(cmd
,
1082 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1085 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1086 pr_err("DataSegmentLength: %u is greater than"
1087 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1088 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
1089 return iscsit_add_reject_cmd(cmd
,
1090 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1093 if (payload_length
> conn
->sess
->sess_ops
->FirstBurstLength
) {
1094 pr_err("DataSegmentLength: %u is greater than"
1095 " FirstBurstLength: %u, protocol error.\n",
1096 payload_length
, conn
->sess
->sess_ops
->FirstBurstLength
);
1097 return iscsit_add_reject_cmd(cmd
,
1098 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1101 data_direction
= (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) ? DMA_TO_DEVICE
:
1102 (hdr
->flags
& ISCSI_FLAG_CMD_READ
) ? DMA_FROM_DEVICE
:
1105 cmd
->data_direction
= data_direction
;
1106 iscsi_task_attr
= hdr
->flags
& ISCSI_FLAG_CMD_ATTR_MASK
;
1108 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1110 if ((iscsi_task_attr
== ISCSI_ATTR_UNTAGGED
) ||
1111 (iscsi_task_attr
== ISCSI_ATTR_SIMPLE
))
1112 sam_task_attr
= TCM_SIMPLE_TAG
;
1113 else if (iscsi_task_attr
== ISCSI_ATTR_ORDERED
)
1114 sam_task_attr
= TCM_ORDERED_TAG
;
1115 else if (iscsi_task_attr
== ISCSI_ATTR_HEAD_OF_QUEUE
)
1116 sam_task_attr
= TCM_HEAD_TAG
;
1117 else if (iscsi_task_attr
== ISCSI_ATTR_ACA
)
1118 sam_task_attr
= TCM_ACA_TAG
;
1120 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
1121 " TCM_SIMPLE_TAG\n", iscsi_task_attr
);
1122 sam_task_attr
= TCM_SIMPLE_TAG
;
1125 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_CMD
;
1126 cmd
->i_state
= ISTATE_NEW_CMD
;
1127 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1128 cmd
->immediate_data
= (payload_length
) ? 1 : 0;
1129 cmd
->unsolicited_data
= ((!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) &&
1130 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) ? 1 : 0);
1131 if (cmd
->unsolicited_data
)
1132 cmd
->cmd_flags
|= ICF_NON_IMMEDIATE_UNSOLICITED_DATA
;
1134 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1135 if (hdr
->flags
& ISCSI_FLAG_CMD_READ
)
1136 cmd
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
1138 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1139 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1140 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1141 cmd
->first_burst_len
= payload_length
;
1143 if (!conn
->sess
->sess_ops
->RDMAExtensions
&&
1144 cmd
->data_direction
== DMA_FROM_DEVICE
) {
1145 struct iscsi_datain_req
*dr
;
1147 dr
= iscsit_allocate_datain_req();
1149 return iscsit_add_reject_cmd(cmd
,
1150 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1152 iscsit_attach_datain_req(cmd
, dr
);
1156 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1158 transport_init_se_cmd(&cmd
->se_cmd
, &iscsi_ops
,
1159 conn
->sess
->se_sess
, be32_to_cpu(hdr
->data_length
),
1160 cmd
->data_direction
, sam_task_attr
,
1161 cmd
->sense_buffer
+ 2);
1163 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1164 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr
->itt
,
1165 hdr
->cmdsn
, be32_to_cpu(hdr
->data_length
), payload_length
,
1168 target_get_sess_cmd(&cmd
->se_cmd
, true);
1170 cmd
->sense_reason
= transport_lookup_cmd_lun(&cmd
->se_cmd
,
1171 scsilun_to_int(&hdr
->lun
));
1172 if (cmd
->sense_reason
)
1175 /* only used for printks or comparing with ->ref_task_tag */
1176 cmd
->se_cmd
.tag
= (__force u32
)cmd
->init_task_tag
;
1177 cmd
->sense_reason
= target_setup_cmd_from_cdb(&cmd
->se_cmd
, hdr
->cdb
);
1178 if (cmd
->sense_reason
) {
1179 if (cmd
->sense_reason
== TCM_OUT_OF_RESOURCES
) {
1180 return iscsit_add_reject_cmd(cmd
,
1181 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1187 if (iscsit_build_pdu_and_seq_lists(cmd
, payload_length
) < 0) {
1188 return iscsit_add_reject_cmd(cmd
,
1189 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1193 spin_lock_bh(&conn
->cmd_lock
);
1194 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1195 spin_unlock_bh(&conn
->cmd_lock
);
1197 * Check if we need to delay processing because of ALUA
1198 * Active/NonOptimized primary access state..
1200 core_alua_check_nonop_delay(&cmd
->se_cmd
);
1204 EXPORT_SYMBOL(iscsit_setup_scsi_cmd
);
1206 void iscsit_set_unsolicited_dataout(struct iscsi_cmd
*cmd
)
1208 iscsit_set_dataout_sequence_values(cmd
);
1210 spin_lock_bh(&cmd
->dataout_timeout_lock
);
1211 iscsit_start_dataout_timer(cmd
, cmd
->conn
);
1212 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
1214 EXPORT_SYMBOL(iscsit_set_unsolicited_dataout
);
1216 int iscsit_process_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1217 struct iscsi_scsi_req
*hdr
)
1221 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1222 * the Immediate Bit is not set, and no Immediate
1225 * A PDU/CmdSN carrying Immediate Data can only
1226 * be processed after the DataCRC has passed.
1227 * If the DataCRC fails, the CmdSN MUST NOT
1228 * be acknowledged. (See below)
1230 if (!cmd
->immediate_data
) {
1231 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1232 (unsigned char *)hdr
, hdr
->cmdsn
);
1233 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1235 else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1236 target_put_sess_cmd(&cmd
->se_cmd
);
1241 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1244 * If no Immediate Data is attached, it's OK to return now.
1246 if (!cmd
->immediate_data
) {
1247 if (!cmd
->sense_reason
&& cmd
->unsolicited_data
)
1248 iscsit_set_unsolicited_dataout(cmd
);
1249 if (!cmd
->sense_reason
)
1252 target_put_sess_cmd(&cmd
->se_cmd
);
1257 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1258 * execution. These exceptions are processed in CmdSN order using
1259 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1261 if (cmd
->sense_reason
)
1264 * Call directly into transport_generic_new_cmd() to perform
1265 * the backend memory allocation.
1267 cmd
->sense_reason
= transport_generic_new_cmd(&cmd
->se_cmd
);
1268 if (cmd
->sense_reason
)
1273 EXPORT_SYMBOL(iscsit_process_scsi_cmd
);
1276 iscsit_get_immediate_data(struct iscsi_cmd
*cmd
, struct iscsi_scsi_req
*hdr
,
1279 int cmdsn_ret
= 0, immed_ret
= IMMEDIATE_DATA_NORMAL_OPERATION
;
1283 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1286 u32 length
= min(cmd
->se_cmd
.data_length
- cmd
->write_data_done
,
1287 cmd
->first_burst_len
);
1289 pr_debug("Dumping min(%d - %d, %d) = %d bytes of immediate data\n",
1290 cmd
->se_cmd
.data_length
, cmd
->write_data_done
,
1291 cmd
->first_burst_len
, length
);
1292 rc
= iscsit_dump_data_payload(cmd
->conn
, length
, 1);
1293 pr_debug("Finished dumping immediate data\n");
1295 immed_ret
= IMMEDIATE_DATA_CANNOT_RECOVER
;
1297 immed_ret
= iscsit_handle_immediate_data(cmd
, hdr
,
1298 cmd
->first_burst_len
);
1301 if (immed_ret
== IMMEDIATE_DATA_NORMAL_OPERATION
) {
1303 * A PDU/CmdSN carrying Immediate Data passed
1304 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1305 * Immediate Bit is not set.
1307 cmdsn_ret
= iscsit_sequence_cmd(cmd
->conn
, cmd
,
1308 (unsigned char *)hdr
, hdr
->cmdsn
);
1309 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1312 if (cmd
->sense_reason
|| cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1313 target_put_sess_cmd(&cmd
->se_cmd
);
1316 } else if (cmd
->unsolicited_data
)
1317 iscsit_set_unsolicited_dataout(cmd
);
1319 } else if (immed_ret
== IMMEDIATE_DATA_ERL1_CRC_FAILURE
) {
1321 * Immediate Data failed DataCRC and ERL>=1,
1322 * silently drop this PDU and let the initiator
1323 * plug the CmdSN gap.
1325 * FIXME: Send Unsolicited NOPIN with reserved
1326 * TTT here to help the initiator figure out
1327 * the missing CmdSN, although they should be
1328 * intelligent enough to determine the missing
1329 * CmdSN and issue a retry to plug the sequence.
1331 cmd
->i_state
= ISTATE_REMOVE
;
1332 iscsit_add_cmd_to_immediate_queue(cmd
, cmd
->conn
, cmd
->i_state
);
1333 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1340 iscsit_handle_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1343 struct iscsi_scsi_req
*hdr
= (struct iscsi_scsi_req
*)buf
;
1345 bool dump_payload
= false;
1347 rc
= iscsit_setup_scsi_cmd(conn
, cmd
, buf
);
1351 * Allocation iovecs needed for struct socket operations for
1352 * traditional iSCSI block I/O.
1354 if (iscsit_allocate_iovecs(cmd
) < 0) {
1355 return iscsit_reject_cmd(cmd
,
1356 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1358 immed_data
= cmd
->immediate_data
;
1360 rc
= iscsit_process_scsi_cmd(conn
, cmd
, hdr
);
1364 dump_payload
= true;
1369 return iscsit_get_immediate_data(cmd
, hdr
, dump_payload
);
1372 static u32
iscsit_do_crypto_hash_sg(
1373 struct ahash_request
*hash
,
1374 struct iscsi_cmd
*cmd
,
1381 struct scatterlist
*sg
;
1382 unsigned int page_off
;
1384 crypto_ahash_init(hash
);
1386 sg
= cmd
->first_data_sg
;
1387 page_off
= cmd
->first_data_sg_off
;
1389 while (data_length
) {
1390 u32 cur_len
= min_t(u32
, data_length
, (sg
->length
- page_off
));
1392 ahash_request_set_crypt(hash
, sg
, NULL
, cur_len
);
1393 crypto_ahash_update(hash
);
1395 data_length
-= cur_len
;
1397 /* iscsit_map_iovec has already checked for invalid sg pointers */
1402 struct scatterlist pad_sg
;
1404 sg_init_one(&pad_sg
, pad_bytes
, padding
);
1405 ahash_request_set_crypt(hash
, &pad_sg
, (u8
*)&data_crc
,
1407 crypto_ahash_finup(hash
);
1409 ahash_request_set_crypt(hash
, NULL
, (u8
*)&data_crc
, 0);
1410 crypto_ahash_final(hash
);
1416 static void iscsit_do_crypto_hash_buf(struct ahash_request
*hash
,
1417 const void *buf
, u32 payload_length
, u32 padding
,
1418 const void *pad_bytes
, void *data_crc
)
1420 struct scatterlist sg
[2];
1422 sg_init_table(sg
, ARRAY_SIZE(sg
));
1423 sg_set_buf(sg
, buf
, payload_length
);
1425 sg_set_buf(sg
+ 1, pad_bytes
, padding
);
1427 ahash_request_set_crypt(hash
, sg
, data_crc
, payload_length
+ padding
);
1429 crypto_ahash_digest(hash
);
1433 __iscsit_check_dataout_hdr(struct iscsi_conn
*conn
, void *buf
,
1434 struct iscsi_cmd
*cmd
, u32 payload_length
,
1437 struct iscsi_data
*hdr
= buf
;
1438 struct se_cmd
*se_cmd
;
1442 atomic_long_add(payload_length
, &conn
->sess
->rx_data_octets
);
1444 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1445 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1446 hdr
->itt
, hdr
->ttt
, hdr
->datasn
, ntohl(hdr
->offset
),
1447 payload_length
, conn
->cid
);
1449 if (cmd
->cmd_flags
& ICF_GOT_LAST_DATAOUT
) {
1450 pr_err("Command ITT: 0x%08x received DataOUT after"
1451 " last DataOUT received, dumping payload\n",
1452 cmd
->init_task_tag
);
1453 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1456 if (cmd
->data_direction
!= DMA_TO_DEVICE
) {
1457 pr_err("Command ITT: 0x%08x received DataOUT for a"
1458 " NON-WRITE command.\n", cmd
->init_task_tag
);
1459 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1461 se_cmd
= &cmd
->se_cmd
;
1462 iscsit_mod_dataout_timer(cmd
);
1464 if ((be32_to_cpu(hdr
->offset
) + payload_length
) > cmd
->se_cmd
.data_length
) {
1465 pr_err("DataOut Offset: %u, Length %u greater than iSCSI Command EDTL %u, protocol error.\n",
1466 be32_to_cpu(hdr
->offset
), payload_length
,
1467 cmd
->se_cmd
.data_length
);
1468 return iscsit_reject_cmd(cmd
, ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1471 if (cmd
->unsolicited_data
) {
1472 int dump_unsolicited_data
= 0;
1474 if (conn
->sess
->sess_ops
->InitialR2T
) {
1475 pr_err("Received unexpected unsolicited data"
1476 " while InitialR2T=Yes, protocol error.\n");
1477 transport_send_check_condition_and_sense(&cmd
->se_cmd
,
1478 TCM_UNEXPECTED_UNSOLICITED_DATA
, 0);
1482 * Special case for dealing with Unsolicited DataOUT
1483 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1487 /* Something's amiss if we're not in WRITE_PENDING state... */
1488 WARN_ON(se_cmd
->t_state
!= TRANSPORT_WRITE_PENDING
);
1489 if (!(se_cmd
->se_cmd_flags
& SCF_SUPPORTED_SAM_OPCODE
))
1490 dump_unsolicited_data
= 1;
1492 if (dump_unsolicited_data
) {
1494 * Check if a delayed TASK_ABORTED status needs to
1495 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1496 * received with the unsolicited data out.
1498 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1499 iscsit_stop_dataout_timer(cmd
);
1501 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1505 * For the normal solicited data path:
1507 * Check for a delayed TASK_ABORTED status and dump any
1508 * incoming data out payload if one exists. Also, when the
1509 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1510 * data out sequence, we decrement outstanding_r2ts. Once
1511 * outstanding_r2ts reaches zero, go ahead and send the delayed
1512 * TASK_ABORTED status.
1514 if (se_cmd
->transport_state
& CMD_T_ABORTED
) {
1515 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
&&
1516 --cmd
->outstanding_r2ts
< 1)
1517 iscsit_stop_dataout_timer(cmd
);
1519 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1523 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1524 * within-command recovery checks before receiving the payload.
1526 rc
= iscsit_check_pre_dataout(cmd
, buf
);
1527 if (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
)
1529 else if (rc
== DATAOUT_CANNOT_RECOVER
)
1534 EXPORT_SYMBOL(__iscsit_check_dataout_hdr
);
1537 iscsit_check_dataout_hdr(struct iscsi_conn
*conn
, void *buf
,
1538 struct iscsi_cmd
**out_cmd
)
1540 struct iscsi_data
*hdr
= buf
;
1541 struct iscsi_cmd
*cmd
;
1542 u32 payload_length
= ntoh24(hdr
->dlength
);
1544 bool success
= false;
1546 if (!payload_length
) {
1547 pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
1551 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1552 pr_err_ratelimited("DataSegmentLength: %u is greater than"
1553 " MaxXmitDataSegmentLength: %u\n", payload_length
,
1554 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1555 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1558 cmd
= iscsit_find_cmd_from_itt_or_dump(conn
, hdr
->itt
, payload_length
);
1562 rc
= __iscsit_check_dataout_hdr(conn
, buf
, cmd
, payload_length
, &success
);
1569 EXPORT_SYMBOL(iscsit_check_dataout_hdr
);
1572 iscsit_get_dataout(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1573 struct iscsi_data
*hdr
)
1576 u32 checksum
, iov_count
= 0, padding
= 0, rx_got
= 0, rx_size
= 0;
1578 int iov_ret
, data_crc_failed
= 0;
1580 payload_length
= min_t(u32
, cmd
->se_cmd
.data_length
,
1581 ntoh24(hdr
->dlength
));
1582 rx_size
+= payload_length
;
1583 iov
= &cmd
->iov_data
[0];
1585 iov_ret
= iscsit_map_iovec(cmd
, iov
, cmd
->orig_iov_data_count
- 2,
1586 be32_to_cpu(hdr
->offset
), payload_length
);
1590 iov_count
+= iov_ret
;
1592 padding
= ((-payload_length
) & 3);
1594 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
1595 iov
[iov_count
++].iov_len
= padding
;
1597 pr_debug("Receiving %u padding bytes.\n", padding
);
1600 if (conn
->conn_ops
->DataDigest
) {
1601 iov
[iov_count
].iov_base
= &checksum
;
1602 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
1603 rx_size
+= ISCSI_CRC_LEN
;
1606 WARN_ON_ONCE(iov_count
> cmd
->orig_iov_data_count
);
1607 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
1609 iscsit_unmap_iovec(cmd
);
1611 if (rx_got
!= rx_size
)
1614 if (conn
->conn_ops
->DataDigest
) {
1617 data_crc
= iscsit_do_crypto_hash_sg(conn
->conn_rx_hash
, cmd
,
1618 be32_to_cpu(hdr
->offset
),
1619 payload_length
, padding
,
1622 if (checksum
!= data_crc
) {
1623 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1624 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1625 " does not match computed 0x%08x\n",
1626 hdr
->itt
, hdr
->offset
, payload_length
,
1627 hdr
->datasn
, checksum
, data_crc
);
1628 data_crc_failed
= 1;
1630 pr_debug("Got CRC32C DataDigest 0x%08x for"
1631 " %u bytes of Data Out\n", checksum
,
1636 return data_crc_failed
;
1640 iscsit_check_dataout_payload(struct iscsi_cmd
*cmd
, struct iscsi_data
*hdr
,
1641 bool data_crc_failed
)
1643 struct iscsi_conn
*conn
= cmd
->conn
;
1646 * Increment post receive data and CRC values or perform
1647 * within-command recovery.
1649 rc
= iscsit_check_post_dataout(cmd
, (unsigned char *)hdr
, data_crc_failed
);
1650 if ((rc
== DATAOUT_NORMAL
) || (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
))
1652 else if (rc
== DATAOUT_SEND_R2T
) {
1653 iscsit_set_dataout_sequence_values(cmd
);
1654 conn
->conn_transport
->iscsit_get_dataout(conn
, cmd
, false);
1655 } else if (rc
== DATAOUT_SEND_TO_TRANSPORT
) {
1657 * Handle extra special case for out of order
1658 * Unsolicited Data Out.
1660 spin_lock_bh(&cmd
->istate_lock
);
1661 ooo_cmdsn
= (cmd
->cmd_flags
& ICF_OOO_CMDSN
);
1662 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
1663 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
1664 spin_unlock_bh(&cmd
->istate_lock
);
1666 iscsit_stop_dataout_timer(cmd
);
1669 target_execute_cmd(&cmd
->se_cmd
);
1671 } else /* DATAOUT_CANNOT_RECOVER */
1676 EXPORT_SYMBOL(iscsit_check_dataout_payload
);
1678 static int iscsit_handle_data_out(struct iscsi_conn
*conn
, unsigned char *buf
)
1680 struct iscsi_cmd
*cmd
= NULL
;
1681 struct iscsi_data
*hdr
= (struct iscsi_data
*)buf
;
1683 bool data_crc_failed
= false;
1685 rc
= iscsit_check_dataout_hdr(conn
, buf
, &cmd
);
1691 rc
= iscsit_get_dataout(conn
, cmd
, hdr
);
1695 data_crc_failed
= true;
1697 return iscsit_check_dataout_payload(cmd
, hdr
, data_crc_failed
);
1700 int iscsit_setup_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1701 struct iscsi_nopout
*hdr
)
1703 u32 payload_length
= ntoh24(hdr
->dlength
);
1705 if (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
1706 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1708 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1709 (unsigned char *)hdr
);
1711 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1712 (unsigned char *)hdr
);
1715 if (hdr
->itt
== RESERVED_ITT
&& !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1716 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1717 " not set, protocol error.\n");
1719 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1720 (unsigned char *)hdr
);
1722 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1723 (unsigned char *)hdr
);
1726 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1727 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1728 " greater than MaxXmitDataSegmentLength: %u, protocol"
1729 " error.\n", payload_length
,
1730 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1732 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1733 (unsigned char *)hdr
);
1735 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1736 (unsigned char *)hdr
);
1739 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1740 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1741 hdr
->itt
== RESERVED_ITT
? "Response" : "Request",
1742 hdr
->itt
, hdr
->ttt
, hdr
->cmdsn
, hdr
->exp_statsn
,
1745 * This is not a response to a Unsolicited NopIN, which means
1746 * it can either be a NOPOUT ping request (with a valid ITT),
1747 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1748 * Either way, make sure we allocate an struct iscsi_cmd, as both
1749 * can contain ping data.
1751 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1752 cmd
->iscsi_opcode
= ISCSI_OP_NOOP_OUT
;
1753 cmd
->i_state
= ISTATE_SEND_NOPIN
;
1754 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ?
1756 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1757 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1758 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1759 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1760 cmd
->data_direction
= DMA_NONE
;
1765 EXPORT_SYMBOL(iscsit_setup_nop_out
);
1767 int iscsit_process_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1768 struct iscsi_nopout
*hdr
)
1770 struct iscsi_cmd
*cmd_p
= NULL
;
1773 * Initiator is expecting a NopIN ping reply..
1775 if (hdr
->itt
!= RESERVED_ITT
) {
1777 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1778 (unsigned char *)hdr
);
1780 spin_lock_bh(&conn
->cmd_lock
);
1781 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1782 spin_unlock_bh(&conn
->cmd_lock
);
1784 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1786 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
1787 iscsit_add_cmd_to_response_queue(cmd
, conn
,
1792 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1793 (unsigned char *)hdr
, hdr
->cmdsn
);
1794 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
1796 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1802 * This was a response to a unsolicited NOPIN ping.
1804 if (hdr
->ttt
!= cpu_to_be32(0xFFFFFFFF)) {
1805 cmd_p
= iscsit_find_cmd_from_ttt(conn
, be32_to_cpu(hdr
->ttt
));
1809 iscsit_stop_nopin_response_timer(conn
);
1811 cmd_p
->i_state
= ISTATE_REMOVE
;
1812 iscsit_add_cmd_to_immediate_queue(cmd_p
, conn
, cmd_p
->i_state
);
1814 iscsit_start_nopin_timer(conn
);
1818 * Otherwise, initiator is not expecting a NOPIN is response.
1819 * Just ignore for now.
1823 iscsit_free_cmd(cmd
, false);
1827 EXPORT_SYMBOL(iscsit_process_nop_out
);
1829 static int iscsit_handle_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1832 unsigned char *ping_data
= NULL
;
1833 struct iscsi_nopout
*hdr
= (struct iscsi_nopout
*)buf
;
1834 struct kvec
*iov
= NULL
;
1835 u32 payload_length
= ntoh24(hdr
->dlength
);
1838 ret
= iscsit_setup_nop_out(conn
, cmd
, hdr
);
1842 * Handle NOP-OUT payload for traditional iSCSI sockets
1844 if (payload_length
&& hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1845 u32 checksum
, data_crc
, padding
= 0;
1846 int niov
= 0, rx_got
, rx_size
= payload_length
;
1848 ping_data
= kzalloc(payload_length
+ 1, GFP_KERNEL
);
1854 iov
= &cmd
->iov_misc
[0];
1855 iov
[niov
].iov_base
= ping_data
;
1856 iov
[niov
++].iov_len
= payload_length
;
1858 padding
= ((-payload_length
) & 3);
1860 pr_debug("Receiving %u additional bytes"
1861 " for padding.\n", padding
);
1862 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
1863 iov
[niov
++].iov_len
= padding
;
1866 if (conn
->conn_ops
->DataDigest
) {
1867 iov
[niov
].iov_base
= &checksum
;
1868 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
1869 rx_size
+= ISCSI_CRC_LEN
;
1872 WARN_ON_ONCE(niov
> ARRAY_SIZE(cmd
->iov_misc
));
1873 rx_got
= rx_data(conn
, &cmd
->iov_misc
[0], niov
, rx_size
);
1874 if (rx_got
!= rx_size
) {
1879 if (conn
->conn_ops
->DataDigest
) {
1880 iscsit_do_crypto_hash_buf(conn
->conn_rx_hash
, ping_data
,
1881 payload_length
, padding
,
1882 cmd
->pad_bytes
, &data_crc
);
1884 if (checksum
!= data_crc
) {
1885 pr_err("Ping data CRC32C DataDigest"
1886 " 0x%08x does not match computed 0x%08x\n",
1887 checksum
, data_crc
);
1888 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
1889 pr_err("Unable to recover from"
1890 " NOPOUT Ping DataCRC failure while in"
1896 * Silently drop this PDU and let the
1897 * initiator plug the CmdSN gap.
1899 pr_debug("Dropping NOPOUT"
1900 " Command CmdSN: 0x%08x due to"
1901 " DataCRC error.\n", hdr
->cmdsn
);
1906 pr_debug("Got CRC32C DataDigest"
1907 " 0x%08x for %u bytes of ping data.\n",
1908 checksum
, payload_length
);
1912 ping_data
[payload_length
] = '\0';
1914 * Attach ping data to struct iscsi_cmd->buf_ptr.
1916 cmd
->buf_ptr
= ping_data
;
1917 cmd
->buf_ptr_size
= payload_length
;
1919 pr_debug("Got %u bytes of NOPOUT ping"
1920 " data.\n", payload_length
);
1921 pr_debug("Ping Data: \"%s\"\n", ping_data
);
1924 return iscsit_process_nop_out(conn
, cmd
, hdr
);
1927 iscsit_free_cmd(cmd
, false);
1933 static enum tcm_tmreq_table
iscsit_convert_tmf(u8 iscsi_tmf
)
1935 switch (iscsi_tmf
) {
1936 case ISCSI_TM_FUNC_ABORT_TASK
:
1937 return TMR_ABORT_TASK
;
1938 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
1939 return TMR_ABORT_TASK_SET
;
1940 case ISCSI_TM_FUNC_CLEAR_ACA
:
1941 return TMR_CLEAR_ACA
;
1942 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
1943 return TMR_CLEAR_TASK_SET
;
1944 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
1945 return TMR_LUN_RESET
;
1946 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
1947 return TMR_TARGET_WARM_RESET
;
1948 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
1949 return TMR_TARGET_COLD_RESET
;
1956 iscsit_handle_task_mgt_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1959 struct se_tmr_req
*se_tmr
;
1960 struct iscsi_tmr_req
*tmr_req
;
1961 struct iscsi_tm
*hdr
;
1962 int out_of_order_cmdsn
= 0, ret
;
1963 u8 function
, tcm_function
= TMR_UNKNOWN
;
1965 hdr
= (struct iscsi_tm
*) buf
;
1966 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
1967 function
= hdr
->flags
;
1969 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1970 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1971 " 0x%08x, CID: %hu\n", hdr
->itt
, hdr
->cmdsn
, function
,
1972 hdr
->rtt
, hdr
->refcmdsn
, conn
->cid
);
1974 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1975 ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1976 hdr
->rtt
!= RESERVED_ITT
)) {
1977 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1978 hdr
->rtt
= RESERVED_ITT
;
1981 if ((function
== ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1982 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1983 pr_err("Task Management Request TASK_REASSIGN not"
1984 " issued as immediate command, bad iSCSI Initiator"
1985 "implementation\n");
1986 return iscsit_add_reject_cmd(cmd
,
1987 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1989 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1990 be32_to_cpu(hdr
->refcmdsn
) != ISCSI_RESERVED_TAG
)
1991 hdr
->refcmdsn
= cpu_to_be32(ISCSI_RESERVED_TAG
);
1993 cmd
->data_direction
= DMA_NONE
;
1994 cmd
->tmr_req
= kzalloc(sizeof(*cmd
->tmr_req
), GFP_KERNEL
);
1995 if (!cmd
->tmr_req
) {
1996 return iscsit_add_reject_cmd(cmd
,
1997 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
2001 transport_init_se_cmd(&cmd
->se_cmd
, &iscsi_ops
,
2002 conn
->sess
->se_sess
, 0, DMA_NONE
,
2003 TCM_SIMPLE_TAG
, cmd
->sense_buffer
+ 2);
2005 target_get_sess_cmd(&cmd
->se_cmd
, true);
2008 * TASK_REASSIGN for ERL=2 / connection stays inside of
2009 * LIO-Target $FABRIC_MOD
2011 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
2012 tcm_function
= iscsit_convert_tmf(function
);
2013 if (tcm_function
== TMR_UNKNOWN
) {
2014 pr_err("Unknown iSCSI TMR Function:"
2015 " 0x%02x\n", function
);
2016 return iscsit_add_reject_cmd(cmd
,
2017 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
2020 ret
= core_tmr_alloc_req(&cmd
->se_cmd
, cmd
->tmr_req
, tcm_function
,
2023 return iscsit_add_reject_cmd(cmd
,
2024 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
2026 cmd
->tmr_req
->se_tmr_req
= cmd
->se_cmd
.se_tmr_req
;
2028 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_TMFUNC
;
2029 cmd
->i_state
= ISTATE_SEND_TASKMGTRSP
;
2030 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2031 cmd
->init_task_tag
= hdr
->itt
;
2032 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2033 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
2034 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
2035 se_tmr
= cmd
->se_cmd
.se_tmr_req
;
2036 tmr_req
= cmd
->tmr_req
;
2038 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2040 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
2041 ret
= transport_lookup_tmr_lun(&cmd
->se_cmd
,
2042 scsilun_to_int(&hdr
->lun
));
2044 se_tmr
->response
= ISCSI_TMF_RSP_NO_LUN
;
2050 case ISCSI_TM_FUNC_ABORT_TASK
:
2051 se_tmr
->response
= iscsit_tmr_abort_task(cmd
, buf
);
2052 if (se_tmr
->response
)
2055 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
2056 case ISCSI_TM_FUNC_CLEAR_ACA
:
2057 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
2058 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
2060 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
2061 if (iscsit_tmr_task_warm_reset(conn
, tmr_req
, buf
) < 0) {
2062 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
2066 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
2067 if (iscsit_tmr_task_cold_reset(conn
, tmr_req
, buf
) < 0) {
2068 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
2072 case ISCSI_TM_FUNC_TASK_REASSIGN
:
2073 se_tmr
->response
= iscsit_tmr_task_reassign(cmd
, buf
);
2075 * Perform sanity checks on the ExpDataSN only if the
2076 * TASK_REASSIGN was successful.
2078 if (se_tmr
->response
)
2081 if (iscsit_check_task_reassign_expdatasn(tmr_req
, conn
) < 0)
2082 return iscsit_add_reject_cmd(cmd
,
2083 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
2086 pr_err("Unknown TMR function: 0x%02x, protocol"
2087 " error.\n", function
);
2088 se_tmr
->response
= ISCSI_TMF_RSP_NOT_SUPPORTED
;
2092 if ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
2093 (se_tmr
->response
== ISCSI_TMF_RSP_COMPLETE
))
2094 se_tmr
->call_transport
= 1;
2096 spin_lock_bh(&conn
->cmd_lock
);
2097 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2098 spin_unlock_bh(&conn
->cmd_lock
);
2100 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
2101 int cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
2102 if (cmdsn_ret
== CMDSN_HIGHER_THAN_EXP
) {
2103 out_of_order_cmdsn
= 1;
2104 } else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
2105 target_put_sess_cmd(&cmd
->se_cmd
);
2107 } else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
) {
2111 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2113 if (out_of_order_cmdsn
|| !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
))
2116 * Found the referenced task, send to transport for processing.
2118 if (se_tmr
->call_transport
)
2119 return transport_generic_handle_tmr(&cmd
->se_cmd
);
2122 * Could not find the referenced LUN, task, or Task Management
2123 * command not authorized or supported. Change state and
2124 * let the tx_thread send the response.
2126 * For connection recovery, this is also the default action for
2127 * TMR TASK_REASSIGN.
2129 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2130 target_put_sess_cmd(&cmd
->se_cmd
);
2133 EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd
);
2135 /* #warning FIXME: Support Text Command parameters besides SendTargets */
2137 iscsit_setup_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2138 struct iscsi_text
*hdr
)
2140 u32 payload_length
= ntoh24(hdr
->dlength
);
2142 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
2143 pr_err("Unable to accept text parameter length: %u"
2144 "greater than MaxXmitDataSegmentLength %u.\n",
2145 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
2146 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
2147 (unsigned char *)hdr
);
2150 if (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) ||
2151 (hdr
->flags
& ISCSI_FLAG_TEXT_CONTINUE
)) {
2152 pr_err("Multi sequence text commands currently not supported\n");
2153 return iscsit_reject_cmd(cmd
, ISCSI_REASON_CMD_NOT_SUPPORTED
,
2154 (unsigned char *)hdr
);
2157 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2158 " ExpStatSN: 0x%08x, Length: %u\n", hdr
->itt
, hdr
->cmdsn
,
2159 hdr
->exp_statsn
, payload_length
);
2161 cmd
->iscsi_opcode
= ISCSI_OP_TEXT
;
2162 cmd
->i_state
= ISTATE_SEND_TEXTRSP
;
2163 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2164 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
2165 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2166 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
2167 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
2168 cmd
->data_direction
= DMA_NONE
;
2169 kfree(cmd
->text_in_ptr
);
2170 cmd
->text_in_ptr
= NULL
;
2174 EXPORT_SYMBOL(iscsit_setup_text_cmd
);
2177 iscsit_process_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2178 struct iscsi_text
*hdr
)
2180 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
;
2184 cmd
->targ_xfer_tag
= be32_to_cpu(hdr
->ttt
);
2185 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF) {
2186 pr_err("Unable to locate text_in buffer for sendtargets"
2190 goto empty_sendtargets
;
2192 if (strncmp("SendTargets=", text_in
, 12) != 0) {
2193 pr_err("Received Text Data that is not"
2194 " SendTargets, cannot continue.\n");
2197 /* '=' confirmed in strncmp */
2198 text_ptr
= strchr(text_in
, '=');
2200 if (!strncmp("=All", text_ptr
, 5)) {
2201 cmd
->cmd_flags
|= ICF_SENDTARGETS_ALL
;
2202 } else if (!strncmp("=iqn.", text_ptr
, 5) ||
2203 !strncmp("=eui.", text_ptr
, 5)) {
2204 cmd
->cmd_flags
|= ICF_SENDTARGETS_SINGLE
;
2206 pr_err("Unable to locate valid SendTargets%s value\n",
2211 spin_lock_bh(&conn
->cmd_lock
);
2212 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2213 spin_unlock_bh(&conn
->cmd_lock
);
2216 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2218 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
2219 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
2220 (unsigned char *)hdr
, hdr
->cmdsn
);
2221 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2227 return iscsit_execute_cmd(cmd
, 0);
2230 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
2231 (unsigned char *)hdr
);
2233 EXPORT_SYMBOL(iscsit_process_text_cmd
);
2236 iscsit_handle_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2239 struct iscsi_text
*hdr
= (struct iscsi_text
*)buf
;
2240 char *text_in
= NULL
;
2241 u32 payload_length
= ntoh24(hdr
->dlength
);
2244 rc
= iscsit_setup_text_cmd(conn
, cmd
, hdr
);
2248 rx_size
= payload_length
;
2249 if (payload_length
) {
2250 u32 checksum
= 0, data_crc
= 0;
2252 int niov
= 0, rx_got
;
2255 rx_size
= ALIGN(payload_length
, 4);
2256 text_in
= kzalloc(rx_size
, GFP_KERNEL
);
2260 cmd
->text_in_ptr
= text_in
;
2262 memset(iov
, 0, sizeof(iov
));
2263 iov
[niov
].iov_base
= text_in
;
2264 iov
[niov
++].iov_len
= rx_size
;
2266 padding
= rx_size
- payload_length
;
2268 pr_debug("Receiving %u additional bytes"
2269 " for padding.\n", padding
);
2270 if (conn
->conn_ops
->DataDigest
) {
2271 iov
[niov
].iov_base
= &checksum
;
2272 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
2273 rx_size
+= ISCSI_CRC_LEN
;
2276 WARN_ON_ONCE(niov
> ARRAY_SIZE(iov
));
2277 rx_got
= rx_data(conn
, &iov
[0], niov
, rx_size
);
2278 if (rx_got
!= rx_size
)
2281 if (conn
->conn_ops
->DataDigest
) {
2282 iscsit_do_crypto_hash_buf(conn
->conn_rx_hash
,
2283 text_in
, rx_size
, 0, NULL
,
2286 if (checksum
!= data_crc
) {
2287 pr_err("Text data CRC32C DataDigest"
2288 " 0x%08x does not match computed"
2289 " 0x%08x\n", checksum
, data_crc
);
2290 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2291 pr_err("Unable to recover from"
2292 " Text Data digest failure while in"
2297 * Silently drop this PDU and let the
2298 * initiator plug the CmdSN gap.
2300 pr_debug("Dropping Text"
2301 " Command CmdSN: 0x%08x due to"
2302 " DataCRC error.\n", hdr
->cmdsn
);
2307 pr_debug("Got CRC32C DataDigest"
2308 " 0x%08x for %u bytes of text data.\n",
2309 checksum
, payload_length
);
2312 text_in
[payload_length
- 1] = '\0';
2313 pr_debug("Successfully read %d bytes of text"
2314 " data.\n", payload_length
);
2317 return iscsit_process_text_cmd(conn
, cmd
, hdr
);
2320 kfree(cmd
->text_in_ptr
);
2321 cmd
->text_in_ptr
= NULL
;
2322 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
, buf
);
2325 int iscsit_logout_closesession(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2327 struct iscsi_conn
*conn_p
;
2328 struct iscsi_session
*sess
= conn
->sess
;
2330 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2331 " for SID: %u.\n", conn
->cid
, conn
->sess
->sid
);
2333 atomic_set(&sess
->session_logout
, 1);
2334 atomic_set(&conn
->conn_logout_remove
, 1);
2335 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_SESSION
;
2337 iscsit_inc_conn_usage_count(conn
);
2338 iscsit_inc_session_usage_count(sess
);
2340 spin_lock_bh(&sess
->conn_lock
);
2341 list_for_each_entry(conn_p
, &sess
->sess_conn_list
, conn_list
) {
2342 if (conn_p
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
)
2345 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2346 conn_p
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2348 spin_unlock_bh(&sess
->conn_lock
);
2350 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2355 int iscsit_logout_closeconnection(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2357 struct iscsi_conn
*l_conn
;
2358 struct iscsi_session
*sess
= conn
->sess
;
2360 pr_debug("Received logout request CLOSECONNECTION for CID:"
2361 " %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2364 * A Logout Request with a CLOSECONNECTION reason code for a CID
2365 * can arrive on a connection with a differing CID.
2367 if (conn
->cid
== cmd
->logout_cid
) {
2368 spin_lock_bh(&conn
->state_lock
);
2369 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2370 conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2372 atomic_set(&conn
->conn_logout_remove
, 1);
2373 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
;
2374 iscsit_inc_conn_usage_count(conn
);
2376 spin_unlock_bh(&conn
->state_lock
);
2379 * Handle all different cid CLOSECONNECTION requests in
2380 * iscsit_logout_post_handler_diffcid() as to give enough
2381 * time for any non immediate command's CmdSN to be
2382 * acknowledged on the connection in question.
2384 * Here we simply make sure the CID is still around.
2386 l_conn
= iscsit_get_conn_from_cid(sess
,
2389 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2390 iscsit_add_cmd_to_response_queue(cmd
, conn
,
2395 iscsit_dec_conn_usage_count(l_conn
);
2398 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2403 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2405 struct iscsi_session
*sess
= conn
->sess
;
2407 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2408 " CID: %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2410 if (sess
->sess_ops
->ErrorRecoveryLevel
!= 2) {
2411 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2412 " while ERL!=2.\n");
2413 cmd
->logout_response
= ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
;
2414 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2418 if (conn
->cid
== cmd
->logout_cid
) {
2419 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2420 " with CID: %hu on CID: %hu, implementation error.\n",
2421 cmd
->logout_cid
, conn
->cid
);
2422 cmd
->logout_response
= ISCSI_LOGOUT_CLEANUP_FAILED
;
2423 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2427 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2433 iscsit_handle_logout_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2436 int cmdsn_ret
, logout_remove
= 0;
2438 struct iscsi_logout
*hdr
;
2439 struct iscsi_tiqn
*tiqn
= iscsit_snmp_get_tiqn(conn
);
2441 hdr
= (struct iscsi_logout
*) buf
;
2442 reason_code
= (hdr
->flags
& 0x7f);
2445 spin_lock(&tiqn
->logout_stats
.lock
);
2446 if (reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
)
2447 tiqn
->logout_stats
.normal_logouts
++;
2449 tiqn
->logout_stats
.abnormal_logouts
++;
2450 spin_unlock(&tiqn
->logout_stats
.lock
);
2453 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2454 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2455 hdr
->itt
, hdr
->cmdsn
, hdr
->exp_statsn
, reason_code
,
2456 hdr
->cid
, conn
->cid
);
2458 if (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
) {
2459 pr_err("Received logout request on connection that"
2460 " is not in logged in state, ignoring request.\n");
2461 iscsit_free_cmd(cmd
, false);
2465 cmd
->iscsi_opcode
= ISCSI_OP_LOGOUT
;
2466 cmd
->i_state
= ISTATE_SEND_LOGOUTRSP
;
2467 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2468 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
2469 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2470 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
2471 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
2472 cmd
->logout_cid
= be16_to_cpu(hdr
->cid
);
2473 cmd
->logout_reason
= reason_code
;
2474 cmd
->data_direction
= DMA_NONE
;
2477 * We need to sleep in these cases (by returning 1) until the Logout
2478 * Response gets sent in the tx thread.
2480 if ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) ||
2481 ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
) &&
2482 be16_to_cpu(hdr
->cid
) == conn
->cid
))
2485 spin_lock_bh(&conn
->cmd_lock
);
2486 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2487 spin_unlock_bh(&conn
->cmd_lock
);
2489 if (reason_code
!= ISCSI_LOGOUT_REASON_RECOVERY
)
2490 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2493 * Immediate commands are executed, well, immediately.
2494 * Non-Immediate Logout Commands are executed in CmdSN order.
2496 if (cmd
->immediate_cmd
) {
2497 int ret
= iscsit_execute_cmd(cmd
, 0);
2502 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
2503 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
2505 else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2509 return logout_remove
;
2511 EXPORT_SYMBOL(iscsit_handle_logout_cmd
);
2513 int iscsit_handle_snack(
2514 struct iscsi_conn
*conn
,
2517 struct iscsi_snack
*hdr
;
2519 hdr
= (struct iscsi_snack
*) buf
;
2520 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
2522 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2523 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2524 " CID: %hu\n", hdr
->itt
, hdr
->exp_statsn
, hdr
->flags
,
2525 hdr
->begrun
, hdr
->runlength
, conn
->cid
);
2527 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2528 pr_err("Initiator sent SNACK request while in"
2529 " ErrorRecoveryLevel=0.\n");
2530 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2534 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2535 * call from inside iscsi_send_recovery_datain_or_r2t().
2537 switch (hdr
->flags
& ISCSI_FLAG_SNACK_TYPE_MASK
) {
2539 return iscsit_handle_recovery_datain_or_r2t(conn
, buf
,
2541 be32_to_cpu(hdr
->ttt
),
2542 be32_to_cpu(hdr
->begrun
),
2543 be32_to_cpu(hdr
->runlength
));
2544 case ISCSI_FLAG_SNACK_TYPE_STATUS
:
2545 return iscsit_handle_status_snack(conn
, hdr
->itt
,
2546 be32_to_cpu(hdr
->ttt
),
2547 be32_to_cpu(hdr
->begrun
), be32_to_cpu(hdr
->runlength
));
2548 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK
:
2549 return iscsit_handle_data_ack(conn
, be32_to_cpu(hdr
->ttt
),
2550 be32_to_cpu(hdr
->begrun
),
2551 be32_to_cpu(hdr
->runlength
));
2552 case ISCSI_FLAG_SNACK_TYPE_RDATA
:
2553 /* FIXME: Support R-Data SNACK */
2554 pr_err("R-Data SNACK Not Supported.\n");
2555 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2558 pr_err("Unknown SNACK type 0x%02x, protocol"
2559 " error.\n", hdr
->flags
& 0x0f);
2560 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2566 EXPORT_SYMBOL(iscsit_handle_snack
);
2568 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2570 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2571 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2572 wait_for_completion_interruptible_timeout(
2573 &conn
->rx_half_close_comp
,
2574 ISCSI_RX_THREAD_TCP_TIMEOUT
* HZ
);
2578 static int iscsit_handle_immediate_data(
2579 struct iscsi_cmd
*cmd
,
2580 struct iscsi_scsi_req
*hdr
,
2583 int iov_ret
, rx_got
= 0, rx_size
= 0;
2584 u32 checksum
, iov_count
= 0, padding
= 0;
2585 struct iscsi_conn
*conn
= cmd
->conn
;
2587 void *overflow_buf
= NULL
;
2589 BUG_ON(cmd
->write_data_done
> cmd
->se_cmd
.data_length
);
2590 rx_size
= min(cmd
->se_cmd
.data_length
- cmd
->write_data_done
, length
);
2591 iov_ret
= iscsit_map_iovec(cmd
, cmd
->iov_data
,
2592 cmd
->orig_iov_data_count
- 2,
2593 cmd
->write_data_done
, rx_size
);
2595 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2597 iov_count
= iov_ret
;
2598 iov
= &cmd
->iov_data
[0];
2599 if (rx_size
< length
) {
2601 * Special case: length of immediate data exceeds the data
2602 * buffer size derived from the CDB.
2604 overflow_buf
= kmalloc(length
- rx_size
, GFP_KERNEL
);
2605 if (!overflow_buf
) {
2606 iscsit_unmap_iovec(cmd
);
2607 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2609 cmd
->overflow_buf
= overflow_buf
;
2610 iov
[iov_count
].iov_base
= overflow_buf
;
2611 iov
[iov_count
].iov_len
= length
- rx_size
;
2616 padding
= ((-length
) & 3);
2618 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2619 iov
[iov_count
++].iov_len
= padding
;
2623 if (conn
->conn_ops
->DataDigest
) {
2624 iov
[iov_count
].iov_base
= &checksum
;
2625 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2626 rx_size
+= ISCSI_CRC_LEN
;
2629 WARN_ON_ONCE(iov_count
> cmd
->orig_iov_data_count
);
2630 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
2632 iscsit_unmap_iovec(cmd
);
2634 if (rx_got
!= rx_size
) {
2635 iscsit_rx_thread_wait_for_tcp(conn
);
2636 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2639 if (conn
->conn_ops
->DataDigest
) {
2642 data_crc
= iscsit_do_crypto_hash_sg(conn
->conn_rx_hash
, cmd
,
2643 cmd
->write_data_done
, length
, padding
,
2646 if (checksum
!= data_crc
) {
2647 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2648 " does not match computed 0x%08x\n", checksum
,
2651 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2652 pr_err("Unable to recover from"
2653 " Immediate Data digest failure while"
2655 iscsit_reject_cmd(cmd
,
2656 ISCSI_REASON_DATA_DIGEST_ERROR
,
2657 (unsigned char *)hdr
);
2658 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2660 iscsit_reject_cmd(cmd
,
2661 ISCSI_REASON_DATA_DIGEST_ERROR
,
2662 (unsigned char *)hdr
);
2663 return IMMEDIATE_DATA_ERL1_CRC_FAILURE
;
2666 pr_debug("Got CRC32C DataDigest 0x%08x for"
2667 " %u bytes of Immediate Data\n", checksum
,
2672 cmd
->write_data_done
+= length
;
2674 if (cmd
->write_data_done
== cmd
->se_cmd
.data_length
) {
2675 spin_lock_bh(&cmd
->istate_lock
);
2676 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
2677 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
2678 spin_unlock_bh(&cmd
->istate_lock
);
2681 return IMMEDIATE_DATA_NORMAL_OPERATION
;
2684 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2685 with active network interface */
2686 static void iscsit_build_conn_drop_async_message(struct iscsi_conn
*conn
)
2688 struct iscsi_cmd
*cmd
;
2689 struct iscsi_conn
*conn_p
;
2692 lockdep_assert_held(&conn
->sess
->conn_lock
);
2695 * Only send a Asynchronous Message on connections whos network
2696 * interface is still functional.
2698 list_for_each_entry(conn_p
, &conn
->sess
->sess_conn_list
, conn_list
) {
2699 if (conn_p
->conn_state
== TARG_CONN_STATE_LOGGED_IN
) {
2700 iscsit_inc_conn_usage_count(conn_p
);
2709 cmd
= iscsit_allocate_cmd(conn_p
, TASK_RUNNING
);
2711 iscsit_dec_conn_usage_count(conn_p
);
2715 cmd
->logout_cid
= conn
->cid
;
2716 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2717 cmd
->i_state
= ISTATE_SEND_ASYNCMSG
;
2719 spin_lock_bh(&conn_p
->cmd_lock
);
2720 list_add_tail(&cmd
->i_conn_node
, &conn_p
->conn_cmd_list
);
2721 spin_unlock_bh(&conn_p
->cmd_lock
);
2723 iscsit_add_cmd_to_response_queue(cmd
, conn_p
, cmd
->i_state
);
2724 iscsit_dec_conn_usage_count(conn_p
);
2727 static int iscsit_send_conn_drop_async_message(
2728 struct iscsi_cmd
*cmd
,
2729 struct iscsi_conn
*conn
)
2731 struct iscsi_async
*hdr
;
2733 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2735 hdr
= (struct iscsi_async
*) cmd
->pdu
;
2736 hdr
->opcode
= ISCSI_OP_ASYNC_EVENT
;
2737 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
2738 cmd
->init_task_tag
= RESERVED_ITT
;
2739 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2740 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->rsvd4
[0]);
2741 cmd
->stat_sn
= conn
->stat_sn
++;
2742 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2743 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2744 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2745 hdr
->async_event
= ISCSI_ASYNC_MSG_DROPPING_CONNECTION
;
2746 hdr
->param1
= cpu_to_be16(cmd
->logout_cid
);
2747 hdr
->param2
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Wait
);
2748 hdr
->param3
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Retain
);
2750 pr_debug("Sending Connection Dropped Async Message StatSN:"
2751 " 0x%08x, for CID: %hu on CID: %hu\n", cmd
->stat_sn
,
2752 cmd
->logout_cid
, conn
->cid
);
2754 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
2757 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2759 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2760 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2761 wait_for_completion_interruptible_timeout(
2762 &conn
->tx_half_close_comp
,
2763 ISCSI_TX_THREAD_TCP_TIMEOUT
* HZ
);
2768 iscsit_build_datain_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2769 struct iscsi_datain
*datain
, struct iscsi_data_rsp
*hdr
,
2772 hdr
->opcode
= ISCSI_OP_SCSI_DATA_IN
;
2773 hdr
->flags
= datain
->flags
;
2774 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
2775 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
2776 hdr
->flags
|= ISCSI_FLAG_DATA_OVERFLOW
;
2777 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2778 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
2779 hdr
->flags
|= ISCSI_FLAG_DATA_UNDERFLOW
;
2780 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2783 hton24(hdr
->dlength
, datain
->length
);
2784 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2785 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
2786 (struct scsi_lun
*)&hdr
->lun
);
2788 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2790 hdr
->itt
= cmd
->init_task_tag
;
2792 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2793 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2795 hdr
->ttt
= cpu_to_be32(0xFFFFFFFF);
2797 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2799 hdr
->statsn
= cpu_to_be32(0xFFFFFFFF);
2801 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2802 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2803 hdr
->datasn
= cpu_to_be32(datain
->data_sn
);
2804 hdr
->offset
= cpu_to_be32(datain
->offset
);
2806 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2807 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2808 cmd
->init_task_tag
, ntohl(hdr
->statsn
), ntohl(hdr
->datasn
),
2809 ntohl(hdr
->offset
), datain
->length
, conn
->cid
);
2811 EXPORT_SYMBOL(iscsit_build_datain_pdu
);
2813 static int iscsit_send_datain(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2815 struct iscsi_data_rsp
*hdr
= (struct iscsi_data_rsp
*)&cmd
->pdu
[0];
2816 struct iscsi_datain datain
;
2817 struct iscsi_datain_req
*dr
;
2819 bool set_statsn
= false;
2821 memset(&datain
, 0, sizeof(struct iscsi_datain
));
2822 dr
= iscsit_get_datain_values(cmd
, &datain
);
2824 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2825 cmd
->init_task_tag
);
2829 * Be paranoid and double check the logic for now.
2831 if ((datain
.offset
+ datain
.length
) > cmd
->se_cmd
.data_length
) {
2832 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2833 " datain.length: %u exceeds cmd->data_length: %u\n",
2834 cmd
->init_task_tag
, datain
.offset
, datain
.length
,
2835 cmd
->se_cmd
.data_length
);
2839 atomic_long_add(datain
.length
, &conn
->sess
->tx_data_octets
);
2841 * Special case for successfully execution w/ both DATAIN
2844 if ((datain
.flags
& ISCSI_FLAG_DATA_STATUS
) &&
2845 (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
))
2846 datain
.flags
&= ~ISCSI_FLAG_DATA_STATUS
;
2848 if ((dr
->dr_complete
== DATAIN_COMPLETE_NORMAL
) ||
2849 (dr
->dr_complete
== DATAIN_COMPLETE_CONNECTION_RECOVERY
)) {
2850 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2851 cmd
->stat_sn
= conn
->stat_sn
++;
2853 } else if (dr
->dr_complete
==
2854 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY
)
2858 iscsit_build_datain_pdu(cmd
, conn
, &datain
, hdr
, set_statsn
);
2860 ret
= conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, dr
, &datain
, 0);
2864 if (dr
->dr_complete
) {
2865 eodr
= (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ?
2867 iscsit_free_datain_req(cmd
, dr
);
2874 iscsit_build_logout_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2875 struct iscsi_logout_rsp
*hdr
)
2877 struct iscsi_conn
*logout_conn
= NULL
;
2878 struct iscsi_conn_recovery
*cr
= NULL
;
2879 struct iscsi_session
*sess
= conn
->sess
;
2881 * The actual shutting down of Sessions and/or Connections
2882 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2883 * is done in scsi_logout_post_handler().
2885 switch (cmd
->logout_reason
) {
2886 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
2887 pr_debug("iSCSI session logout successful, setting"
2888 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2889 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2891 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
2892 if (cmd
->logout_response
== ISCSI_LOGOUT_CID_NOT_FOUND
)
2895 * For CLOSECONNECTION logout requests carrying
2896 * a matching logout CID -> local CID, the reference
2897 * for the local CID will have been incremented in
2898 * iscsi_logout_closeconnection().
2900 * For CLOSECONNECTION logout requests carrying
2901 * a different CID than the connection it arrived
2902 * on, the connection responding to cmd->logout_cid
2903 * is stopped in iscsit_logout_post_handler_diffcid().
2906 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2907 " successful.\n", cmd
->logout_cid
, conn
->cid
);
2908 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2910 case ISCSI_LOGOUT_REASON_RECOVERY
:
2911 if ((cmd
->logout_response
== ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
) ||
2912 (cmd
->logout_response
== ISCSI_LOGOUT_CLEANUP_FAILED
))
2915 * If the connection is still active from our point of view
2916 * force connection recovery to occur.
2918 logout_conn
= iscsit_get_conn_from_cid_rcfr(sess
,
2921 iscsit_connection_reinstatement_rcfr(logout_conn
);
2922 iscsit_dec_conn_usage_count(logout_conn
);
2925 cr
= iscsit_get_inactive_connection_recovery_entry(
2926 conn
->sess
, cmd
->logout_cid
);
2928 pr_err("Unable to locate CID: %hu for"
2929 " REMOVECONNFORRECOVERY Logout Request.\n",
2931 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2935 iscsit_discard_cr_cmds_by_expstatsn(cr
, cmd
->exp_stat_sn
);
2937 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2938 " for recovery for CID: %hu on CID: %hu successful.\n",
2939 cmd
->logout_cid
, conn
->cid
);
2940 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2943 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2944 cmd
->logout_reason
);
2948 hdr
->opcode
= ISCSI_OP_LOGOUT_RSP
;
2949 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2950 hdr
->response
= cmd
->logout_response
;
2951 hdr
->itt
= cmd
->init_task_tag
;
2952 cmd
->stat_sn
= conn
->stat_sn
++;
2953 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2955 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2956 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2957 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2959 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2960 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2961 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
,
2962 cmd
->logout_cid
, conn
->cid
);
2966 EXPORT_SYMBOL(iscsit_build_logout_rsp
);
2969 iscsit_send_logout(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2973 rc
= iscsit_build_logout_rsp(cmd
, conn
,
2974 (struct iscsi_logout_rsp
*)&cmd
->pdu
[0]);
2978 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
2982 iscsit_build_nopin_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2983 struct iscsi_nopin
*hdr
, bool nopout_response
)
2985 hdr
->opcode
= ISCSI_OP_NOOP_IN
;
2986 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2987 hton24(hdr
->dlength
, cmd
->buf_ptr_size
);
2988 if (nopout_response
)
2989 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2990 hdr
->itt
= cmd
->init_task_tag
;
2991 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2992 cmd
->stat_sn
= (nopout_response
) ? conn
->stat_sn
++ :
2994 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2996 if (nopout_response
)
2997 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2999 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3000 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3002 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
3003 " StatSN: 0x%08x, Length %u\n", (nopout_response
) ?
3004 "Solicited" : "Unsolicited", cmd
->init_task_tag
,
3005 cmd
->targ_xfer_tag
, cmd
->stat_sn
, cmd
->buf_ptr_size
);
3007 EXPORT_SYMBOL(iscsit_build_nopin_rsp
);
3010 * Unsolicited NOPIN, either requesting a response or not.
3012 static int iscsit_send_unsolicited_nopin(
3013 struct iscsi_cmd
*cmd
,
3014 struct iscsi_conn
*conn
,
3017 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
3020 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, false);
3022 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
3023 " 0x%08x CID: %hu\n", hdr
->ttt
, cmd
->stat_sn
, conn
->cid
);
3025 ret
= conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
3029 spin_lock_bh(&cmd
->istate_lock
);
3030 cmd
->i_state
= want_response
?
3031 ISTATE_SENT_NOPIN_WANT_RESPONSE
: ISTATE_SENT_STATUS
;
3032 spin_unlock_bh(&cmd
->istate_lock
);
3038 iscsit_send_nopin(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3040 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
3042 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, true);
3045 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3046 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3048 pr_debug("Echoing back %u bytes of ping data.\n", cmd
->buf_ptr_size
);
3050 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
,
3055 static int iscsit_send_r2t(
3056 struct iscsi_cmd
*cmd
,
3057 struct iscsi_conn
*conn
)
3059 struct iscsi_r2t
*r2t
;
3060 struct iscsi_r2t_rsp
*hdr
;
3063 r2t
= iscsit_get_r2t_from_list(cmd
);
3067 hdr
= (struct iscsi_r2t_rsp
*) cmd
->pdu
;
3068 memset(hdr
, 0, ISCSI_HDR_LEN
);
3069 hdr
->opcode
= ISCSI_OP_R2T
;
3070 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3071 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
3072 (struct scsi_lun
*)&hdr
->lun
);
3073 hdr
->itt
= cmd
->init_task_tag
;
3074 if (conn
->conn_transport
->iscsit_get_r2t_ttt
)
3075 conn
->conn_transport
->iscsit_get_r2t_ttt(conn
, cmd
, r2t
);
3077 r2t
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
3078 hdr
->ttt
= cpu_to_be32(r2t
->targ_xfer_tag
);
3079 hdr
->statsn
= cpu_to_be32(conn
->stat_sn
);
3080 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3081 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3082 hdr
->r2tsn
= cpu_to_be32(r2t
->r2t_sn
);
3083 hdr
->data_offset
= cpu_to_be32(r2t
->offset
);
3084 hdr
->data_length
= cpu_to_be32(r2t
->xfer_len
);
3086 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3087 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3088 (!r2t
->recovery_r2t
) ? "" : "Recovery ", cmd
->init_task_tag
,
3089 r2t
->targ_xfer_tag
, ntohl(hdr
->statsn
), r2t
->r2t_sn
,
3090 r2t
->offset
, r2t
->xfer_len
, conn
->cid
);
3092 spin_lock_bh(&cmd
->r2t_lock
);
3094 spin_unlock_bh(&cmd
->r2t_lock
);
3096 ret
= conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
3101 spin_lock_bh(&cmd
->dataout_timeout_lock
);
3102 iscsit_start_dataout_timer(cmd
, conn
);
3103 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
3109 * @recovery: If called from iscsi_task_reassign_complete_write() for
3110 * connection recovery.
3112 int iscsit_build_r2ts_for_cmd(
3113 struct iscsi_conn
*conn
,
3114 struct iscsi_cmd
*cmd
,
3118 u32 offset
= 0, xfer_len
= 0;
3120 spin_lock_bh(&cmd
->r2t_lock
);
3121 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
) {
3122 spin_unlock_bh(&cmd
->r2t_lock
);
3126 if (conn
->sess
->sess_ops
->DataSequenceInOrder
&&
3128 cmd
->r2t_offset
= max(cmd
->r2t_offset
, cmd
->write_data_done
);
3130 while (cmd
->outstanding_r2ts
< conn
->sess
->sess_ops
->MaxOutstandingR2T
) {
3131 if (conn
->sess
->sess_ops
->DataSequenceInOrder
) {
3132 offset
= cmd
->r2t_offset
;
3134 if (first_r2t
&& recovery
) {
3135 int new_data_end
= offset
+
3136 conn
->sess
->sess_ops
->MaxBurstLength
-
3137 cmd
->next_burst_len
;
3139 if (new_data_end
> cmd
->se_cmd
.data_length
)
3140 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3143 conn
->sess
->sess_ops
->MaxBurstLength
-
3144 cmd
->next_burst_len
;
3146 int new_data_end
= offset
+
3147 conn
->sess
->sess_ops
->MaxBurstLength
;
3149 if (new_data_end
> cmd
->se_cmd
.data_length
)
3150 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3152 xfer_len
= conn
->sess
->sess_ops
->MaxBurstLength
;
3155 if ((s32
)xfer_len
< 0) {
3156 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3160 cmd
->r2t_offset
+= xfer_len
;
3162 if (cmd
->r2t_offset
== cmd
->se_cmd
.data_length
)
3163 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3165 struct iscsi_seq
*seq
;
3167 seq
= iscsit_get_seq_holder_for_r2t(cmd
);
3169 spin_unlock_bh(&cmd
->r2t_lock
);
3173 offset
= seq
->offset
;
3174 xfer_len
= seq
->xfer_len
;
3176 if (cmd
->seq_send_order
== cmd
->seq_count
)
3177 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3179 cmd
->outstanding_r2ts
++;
3182 if (iscsit_add_r2t_to_list(cmd
, offset
, xfer_len
, 0, 0) < 0) {
3183 spin_unlock_bh(&cmd
->r2t_lock
);
3187 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
)
3190 spin_unlock_bh(&cmd
->r2t_lock
);
3194 EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd
);
3196 void iscsit_build_rsp_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3197 bool inc_stat_sn
, struct iscsi_scsi_rsp
*hdr
)
3200 cmd
->stat_sn
= conn
->stat_sn
++;
3202 atomic_long_inc(&conn
->sess
->rsp_pdus
);
3204 memset(hdr
, 0, ISCSI_HDR_LEN
);
3205 hdr
->opcode
= ISCSI_OP_SCSI_CMD_RSP
;
3206 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3207 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
3208 hdr
->flags
|= ISCSI_FLAG_CMD_OVERFLOW
;
3209 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3210 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
3211 hdr
->flags
|= ISCSI_FLAG_CMD_UNDERFLOW
;
3212 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3214 hdr
->response
= cmd
->iscsi_response
;
3215 hdr
->cmd_status
= cmd
->se_cmd
.scsi_status
;
3216 hdr
->itt
= cmd
->init_task_tag
;
3217 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3219 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3220 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3221 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3223 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3224 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3225 cmd
->init_task_tag
, cmd
->stat_sn
, cmd
->se_cmd
.scsi_status
,
3226 cmd
->se_cmd
.scsi_status
, conn
->cid
);
3228 EXPORT_SYMBOL(iscsit_build_rsp_pdu
);
3230 static int iscsit_send_response(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3232 struct iscsi_scsi_rsp
*hdr
= (struct iscsi_scsi_rsp
*)&cmd
->pdu
[0];
3233 bool inc_stat_sn
= (cmd
->i_state
== ISTATE_SEND_STATUS
);
3234 void *data_buf
= NULL
;
3235 u32 padding
= 0, data_buf_len
= 0;
3237 iscsit_build_rsp_pdu(cmd
, conn
, inc_stat_sn
, hdr
);
3240 * Attach SENSE DATA payload to iSCSI Response PDU
3242 if (cmd
->se_cmd
.sense_buffer
&&
3243 ((cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
3244 (cmd
->se_cmd
.se_cmd_flags
& SCF_EMULATED_TASK_SENSE
))) {
3245 put_unaligned_be16(cmd
->se_cmd
.scsi_sense_length
, cmd
->sense_buffer
);
3246 cmd
->se_cmd
.scsi_sense_length
+= sizeof (__be16
);
3248 padding
= -(cmd
->se_cmd
.scsi_sense_length
) & 3;
3249 hton24(hdr
->dlength
, (u32
)cmd
->se_cmd
.scsi_sense_length
);
3250 data_buf
= cmd
->sense_buffer
;
3251 data_buf_len
= cmd
->se_cmd
.scsi_sense_length
+ padding
;
3254 memset(cmd
->sense_buffer
+
3255 cmd
->se_cmd
.scsi_sense_length
, 0, padding
);
3256 pr_debug("Adding %u bytes of padding to"
3257 " SENSE.\n", padding
);
3260 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3262 cmd
->se_cmd
.scsi_sense_length
);
3265 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, data_buf
,
3269 static u8
iscsit_convert_tcm_tmr_rsp(struct se_tmr_req
*se_tmr
)
3271 switch (se_tmr
->response
) {
3272 case TMR_FUNCTION_COMPLETE
:
3273 return ISCSI_TMF_RSP_COMPLETE
;
3274 case TMR_TASK_DOES_NOT_EXIST
:
3275 return ISCSI_TMF_RSP_NO_TASK
;
3276 case TMR_LUN_DOES_NOT_EXIST
:
3277 return ISCSI_TMF_RSP_NO_LUN
;
3278 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
:
3279 return ISCSI_TMF_RSP_NOT_SUPPORTED
;
3280 case TMR_FUNCTION_REJECTED
:
3282 return ISCSI_TMF_RSP_REJECTED
;
3287 iscsit_build_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3288 struct iscsi_tm_rsp
*hdr
)
3290 struct se_tmr_req
*se_tmr
= cmd
->se_cmd
.se_tmr_req
;
3292 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC_RSP
;
3293 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
3294 hdr
->response
= iscsit_convert_tcm_tmr_rsp(se_tmr
);
3295 hdr
->itt
= cmd
->init_task_tag
;
3296 cmd
->stat_sn
= conn
->stat_sn
++;
3297 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3299 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3300 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3301 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3303 pr_debug("Built Task Management Response ITT: 0x%08x,"
3304 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3305 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
, conn
->cid
);
3307 EXPORT_SYMBOL(iscsit_build_task_mgt_rsp
);
3310 iscsit_send_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3312 struct iscsi_tm_rsp
*hdr
= (struct iscsi_tm_rsp
*)&cmd
->pdu
[0];
3314 iscsit_build_task_mgt_rsp(cmd
, conn
, hdr
);
3316 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
, NULL
, 0);
3319 #define SENDTARGETS_BUF_LIMIT 32768U
3322 iscsit_build_sendtargets_response(struct iscsi_cmd
*cmd
,
3323 enum iscsit_transport_type network_transport
,
3324 int skip_bytes
, bool *completed
)
3326 char *payload
= NULL
;
3327 struct iscsi_conn
*conn
= cmd
->conn
;
3328 struct iscsi_portal_group
*tpg
;
3329 struct iscsi_tiqn
*tiqn
;
3330 struct iscsi_tpg_np
*tpg_np
;
3331 int buffer_len
, end_of_buf
= 0, len
= 0, payload_len
= 0;
3332 int target_name_printed
;
3333 unsigned char buf
[ISCSI_IQN_LEN
+12]; /* iqn + "TargetName=" + \0 */
3334 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
= NULL
;
3337 buffer_len
= min(conn
->conn_ops
->MaxRecvDataSegmentLength
,
3338 SENDTARGETS_BUF_LIMIT
);
3340 payload
= kzalloc(buffer_len
, GFP_KERNEL
);
3345 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
3348 if (cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
) {
3349 text_ptr
= strchr(text_in
, '=');
3351 pr_err("Unable to locate '=' string in text_in:"
3357 * Skip over '=' character..
3362 spin_lock(&tiqn_lock
);
3363 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
3364 if ((cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
) &&
3365 strcmp(tiqn
->tiqn
, text_ptr
)) {
3369 target_name_printed
= 0;
3371 spin_lock(&tiqn
->tiqn_tpg_lock
);
3372 list_for_each_entry(tpg
, &tiqn
->tiqn_tpg_list
, tpg_list
) {
3374 /* If demo_mode_discovery=0 and generate_node_acls=0
3375 * (demo mode dislabed) do not return
3376 * TargetName+TargetAddress unless a NodeACL exists.
3379 if ((tpg
->tpg_attrib
.generate_node_acls
== 0) &&
3380 (tpg
->tpg_attrib
.demo_mode_discovery
== 0) &&
3381 (!target_tpg_has_node_acl(&tpg
->tpg_se_tpg
,
3382 cmd
->conn
->sess
->sess_ops
->InitiatorName
))) {
3386 spin_lock(&tpg
->tpg_state_lock
);
3387 active
= (tpg
->tpg_state
== TPG_STATE_ACTIVE
);
3388 spin_unlock(&tpg
->tpg_state_lock
);
3390 if (!active
&& tpg
->tpg_attrib
.tpg_enabled_sendtargets
)
3393 spin_lock(&tpg
->tpg_np_lock
);
3394 list_for_each_entry(tpg_np
, &tpg
->tpg_gnp_list
,
3396 struct iscsi_np
*np
= tpg_np
->tpg_np
;
3397 struct sockaddr_storage
*sockaddr
;
3399 if (np
->np_network_transport
!= network_transport
)
3402 if (!target_name_printed
) {
3403 len
= sprintf(buf
, "TargetName=%s",
3407 if ((len
+ payload_len
) > buffer_len
) {
3408 spin_unlock(&tpg
->tpg_np_lock
);
3409 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3414 if (skip_bytes
&& len
<= skip_bytes
) {
3417 memcpy(payload
+ payload_len
, buf
, len
);
3419 target_name_printed
= 1;
3420 if (len
> skip_bytes
)
3425 if (inet_addr_is_any((struct sockaddr
*)&np
->np_sockaddr
))
3426 sockaddr
= &conn
->local_sockaddr
;
3428 sockaddr
= &np
->np_sockaddr
;
3430 len
= sprintf(buf
, "TargetAddress="
3436 if ((len
+ payload_len
) > buffer_len
) {
3437 spin_unlock(&tpg
->tpg_np_lock
);
3438 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3443 if (skip_bytes
&& len
<= skip_bytes
) {
3446 memcpy(payload
+ payload_len
, buf
, len
);
3448 if (len
> skip_bytes
)
3452 spin_unlock(&tpg
->tpg_np_lock
);
3454 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3461 if (cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
)
3464 spin_unlock(&tiqn_lock
);
3466 cmd
->buf_ptr
= payload
;
3472 iscsit_build_text_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3473 struct iscsi_text_rsp
*hdr
,
3474 enum iscsit_transport_type network_transport
)
3476 int text_length
, padding
;
3477 bool completed
= true;
3479 text_length
= iscsit_build_sendtargets_response(cmd
, network_transport
,
3480 cmd
->read_data_done
,
3482 if (text_length
< 0)
3486 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
3488 hdr
->flags
= ISCSI_FLAG_TEXT_CONTINUE
;
3489 cmd
->read_data_done
+= text_length
;
3490 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF)
3491 cmd
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
3493 hdr
->opcode
= ISCSI_OP_TEXT_RSP
;
3494 padding
= ((-text_length
) & 3);
3495 hton24(hdr
->dlength
, text_length
);
3496 hdr
->itt
= cmd
->init_task_tag
;
3497 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
3498 cmd
->stat_sn
= conn
->stat_sn
++;
3499 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3501 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3503 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3504 * correctly increment MaxCmdSN for each response answering a
3505 * non immediate text request with a valid CmdSN.
3507 cmd
->maxcmdsn_inc
= 0;
3508 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3509 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3511 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3512 " Length: %u, CID: %hu F: %d C: %d\n", cmd
->init_task_tag
,
3513 cmd
->targ_xfer_tag
, cmd
->stat_sn
, text_length
, conn
->cid
,
3514 !!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
),
3515 !!(hdr
->flags
& ISCSI_FLAG_TEXT_CONTINUE
));
3517 return text_length
+ padding
;
3519 EXPORT_SYMBOL(iscsit_build_text_rsp
);
3521 static int iscsit_send_text_rsp(
3522 struct iscsi_cmd
*cmd
,
3523 struct iscsi_conn
*conn
)
3525 struct iscsi_text_rsp
*hdr
= (struct iscsi_text_rsp
*)cmd
->pdu
;
3528 text_length
= iscsit_build_text_rsp(cmd
, conn
, hdr
,
3529 conn
->conn_transport
->transport_type
);
3530 if (text_length
< 0)
3533 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
,
3539 iscsit_build_reject(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3540 struct iscsi_reject
*hdr
)
3542 hdr
->opcode
= ISCSI_OP_REJECT
;
3543 hdr
->reason
= cmd
->reject_reason
;
3544 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3545 hton24(hdr
->dlength
, ISCSI_HDR_LEN
);
3546 hdr
->ffffffff
= cpu_to_be32(0xffffffff);
3547 cmd
->stat_sn
= conn
->stat_sn
++;
3548 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3549 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3550 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3553 EXPORT_SYMBOL(iscsit_build_reject
);
3555 static int iscsit_send_reject(
3556 struct iscsi_cmd
*cmd
,
3557 struct iscsi_conn
*conn
)
3559 struct iscsi_reject
*hdr
= (struct iscsi_reject
*)&cmd
->pdu
[0];
3561 iscsit_build_reject(cmd
, conn
, hdr
);
3563 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3564 " CID: %hu\n", ntohl(hdr
->statsn
), hdr
->reason
, conn
->cid
);
3566 return conn
->conn_transport
->iscsit_xmit_pdu(conn
, cmd
, NULL
,
3571 void iscsit_thread_get_cpumask(struct iscsi_conn
*conn
)
3575 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3576 * within iscsit_start_kthreads()
3578 * Here we use bitmap_id to determine which CPU that this
3579 * iSCSI connection's RX/TX threads will be scheduled to
3582 ord
= conn
->bitmap_id
% cpumask_weight(cpu_online_mask
);
3583 for_each_online_cpu(cpu
) {
3585 cpumask_set_cpu(cpu
, conn
->conn_cpumask
);
3590 * This should never be reached..
3593 cpumask_setall(conn
->conn_cpumask
);
3597 iscsit_immediate_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3602 case ISTATE_SEND_R2T
:
3603 ret
= iscsit_send_r2t(cmd
, conn
);
3608 spin_lock_bh(&conn
->cmd_lock
);
3609 list_del_init(&cmd
->i_conn_node
);
3610 spin_unlock_bh(&conn
->cmd_lock
);
3612 iscsit_free_cmd(cmd
, false);
3614 case ISTATE_SEND_NOPIN_WANT_RESPONSE
:
3615 iscsit_mod_nopin_response_timer(conn
);
3616 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 1);
3620 case ISTATE_SEND_NOPIN_NO_RESPONSE
:
3621 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 0);
3626 pr_err("Unknown Opcode: 0x%02x ITT:"
3627 " 0x%08x, i_state: %d on CID: %hu\n",
3628 cmd
->iscsi_opcode
, cmd
->init_task_tag
, state
,
3638 EXPORT_SYMBOL(iscsit_immediate_queue
);
3641 iscsit_handle_immediate_queue(struct iscsi_conn
*conn
)
3643 struct iscsit_transport
*t
= conn
->conn_transport
;
3644 struct iscsi_queue_req
*qr
;
3645 struct iscsi_cmd
*cmd
;
3649 while ((qr
= iscsit_get_cmd_from_immediate_queue(conn
))) {
3650 atomic_set(&conn
->check_immediate_queue
, 0);
3653 kmem_cache_free(lio_qr_cache
, qr
);
3655 ret
= t
->iscsit_immediate_queue(conn
, cmd
, state
);
3664 iscsit_response_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3670 case ISTATE_SEND_DATAIN
:
3671 ret
= iscsit_send_datain(cmd
, conn
);
3676 goto check_rsp_state
;
3677 else if (ret
== 1) {
3679 spin_lock_bh(&cmd
->istate_lock
);
3680 cmd
->i_state
= ISTATE_SENT_STATUS
;
3681 spin_unlock_bh(&cmd
->istate_lock
);
3683 if (atomic_read(&conn
->check_immediate_queue
))
3687 } else if (ret
== 2) {
3688 /* Still must send status,
3689 SCF_TRANSPORT_TASK_SENSE was set */
3690 spin_lock_bh(&cmd
->istate_lock
);
3691 cmd
->i_state
= ISTATE_SEND_STATUS
;
3692 spin_unlock_bh(&cmd
->istate_lock
);
3693 state
= ISTATE_SEND_STATUS
;
3694 goto check_rsp_state
;
3698 case ISTATE_SEND_STATUS
:
3699 case ISTATE_SEND_STATUS_RECOVERY
:
3700 ret
= iscsit_send_response(cmd
, conn
);
3702 case ISTATE_SEND_LOGOUTRSP
:
3703 ret
= iscsit_send_logout(cmd
, conn
);
3705 case ISTATE_SEND_ASYNCMSG
:
3706 ret
= iscsit_send_conn_drop_async_message(
3709 case ISTATE_SEND_NOPIN
:
3710 ret
= iscsit_send_nopin(cmd
, conn
);
3712 case ISTATE_SEND_REJECT
:
3713 ret
= iscsit_send_reject(cmd
, conn
);
3715 case ISTATE_SEND_TASKMGTRSP
:
3716 ret
= iscsit_send_task_mgt_rsp(cmd
, conn
);
3719 ret
= iscsit_tmr_post_handler(cmd
, conn
);
3721 iscsit_fall_back_to_erl0(conn
->sess
);
3723 case ISTATE_SEND_TEXTRSP
:
3724 ret
= iscsit_send_text_rsp(cmd
, conn
);
3727 pr_err("Unknown Opcode: 0x%02x ITT:"
3728 " 0x%08x, i_state: %d on CID: %hu\n",
3729 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3737 case ISTATE_SEND_LOGOUTRSP
:
3738 if (!iscsit_logout_post_handler(cmd
, conn
))
3741 case ISTATE_SEND_STATUS
:
3742 case ISTATE_SEND_ASYNCMSG
:
3743 case ISTATE_SEND_NOPIN
:
3744 case ISTATE_SEND_STATUS_RECOVERY
:
3745 case ISTATE_SEND_TEXTRSP
:
3746 case ISTATE_SEND_TASKMGTRSP
:
3747 case ISTATE_SEND_REJECT
:
3748 spin_lock_bh(&cmd
->istate_lock
);
3749 cmd
->i_state
= ISTATE_SENT_STATUS
;
3750 spin_unlock_bh(&cmd
->istate_lock
);
3753 pr_err("Unknown Opcode: 0x%02x ITT:"
3754 " 0x%08x, i_state: %d on CID: %hu\n",
3755 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3756 cmd
->i_state
, conn
->cid
);
3760 if (atomic_read(&conn
->check_immediate_queue
))
3768 EXPORT_SYMBOL(iscsit_response_queue
);
3770 static int iscsit_handle_response_queue(struct iscsi_conn
*conn
)
3772 struct iscsit_transport
*t
= conn
->conn_transport
;
3773 struct iscsi_queue_req
*qr
;
3774 struct iscsi_cmd
*cmd
;
3778 while ((qr
= iscsit_get_cmd_from_response_queue(conn
))) {
3781 kmem_cache_free(lio_qr_cache
, qr
);
3783 ret
= t
->iscsit_response_queue(conn
, cmd
, state
);
3784 if (ret
== 1 || ret
< 0)
3791 int iscsi_target_tx_thread(void *arg
)
3794 struct iscsi_conn
*conn
= arg
;
3795 bool conn_freed
= false;
3798 * Allow ourselves to be interrupted by SIGINT so that a
3799 * connection recovery / failure event can be triggered externally.
3801 allow_signal(SIGINT
);
3803 while (!kthread_should_stop()) {
3805 * Ensure that both TX and RX per connection kthreads
3806 * are scheduled to run on the same CPU.
3808 iscsit_thread_check_cpumask(conn
, current
, 1);
3810 wait_event_interruptible(conn
->queues_wq
,
3811 !iscsit_conn_all_queues_empty(conn
));
3813 if (signal_pending(current
))
3817 ret
= iscsit_handle_immediate_queue(conn
);
3821 ret
= iscsit_handle_response_queue(conn
);
3824 } else if (ret
== -ECONNRESET
) {
3827 } else if (ret
< 0) {
3834 * Avoid the normal connection failure code-path if this connection
3835 * is still within LOGIN mode, and iscsi_np process context is
3836 * responsible for cleaning up the early connection failure.
3838 if (conn
->conn_state
!= TARG_CONN_STATE_IN_LOGIN
)
3839 iscsit_take_action_for_connection_exit(conn
, &conn_freed
);
3842 while (!kthread_should_stop()) {
3849 static int iscsi_target_rx_opcode(struct iscsi_conn
*conn
, unsigned char *buf
)
3851 struct iscsi_hdr
*hdr
= (struct iscsi_hdr
*)buf
;
3852 struct iscsi_cmd
*cmd
;
3855 switch (hdr
->opcode
& ISCSI_OPCODE_MASK
) {
3856 case ISCSI_OP_SCSI_CMD
:
3857 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3861 ret
= iscsit_handle_scsi_cmd(conn
, cmd
, buf
);
3863 case ISCSI_OP_SCSI_DATA_OUT
:
3864 ret
= iscsit_handle_data_out(conn
, buf
);
3866 case ISCSI_OP_NOOP_OUT
:
3868 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
3869 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3873 ret
= iscsit_handle_nop_out(conn
, cmd
, buf
);
3875 case ISCSI_OP_SCSI_TMFUNC
:
3876 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3880 ret
= iscsit_handle_task_mgt_cmd(conn
, cmd
, buf
);
3883 if (hdr
->ttt
!= cpu_to_be32(0xFFFFFFFF)) {
3884 cmd
= iscsit_find_cmd_from_itt(conn
, hdr
->itt
);
3888 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3893 ret
= iscsit_handle_text_cmd(conn
, cmd
, buf
);
3895 case ISCSI_OP_LOGOUT
:
3896 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3900 ret
= iscsit_handle_logout_cmd(conn
, cmd
, buf
);
3902 wait_for_completion_timeout(&conn
->conn_logout_comp
,
3903 SECONDS_FOR_LOGOUT_COMP
* HZ
);
3905 case ISCSI_OP_SNACK
:
3906 ret
= iscsit_handle_snack(conn
, buf
);
3909 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr
->opcode
);
3910 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
3911 pr_err("Cannot recover from unknown"
3912 " opcode while ERL=0, closing iSCSI connection.\n");
3915 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3916 " closing iSCSI connection.\n");
3923 return iscsit_add_reject(conn
, ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
3926 static bool iscsi_target_check_conn_state(struct iscsi_conn
*conn
)
3930 spin_lock_bh(&conn
->state_lock
);
3931 ret
= (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
);
3932 spin_unlock_bh(&conn
->state_lock
);
3937 static void iscsit_get_rx_pdu(struct iscsi_conn
*conn
)
3941 u32 checksum
= 0, digest
= 0;
3944 buffer
= kcalloc(ISCSI_HDR_LEN
, sizeof(*buffer
), GFP_KERNEL
);
3948 while (!kthread_should_stop()) {
3950 * Ensure that both TX and RX per connection kthreads
3951 * are scheduled to run on the same CPU.
3953 iscsit_thread_check_cpumask(conn
, current
, 0);
3955 memset(&iov
, 0, sizeof(struct kvec
));
3957 iov
.iov_base
= buffer
;
3958 iov
.iov_len
= ISCSI_HDR_LEN
;
3960 ret
= rx_data(conn
, &iov
, 1, ISCSI_HDR_LEN
);
3961 if (ret
!= ISCSI_HDR_LEN
) {
3962 iscsit_rx_thread_wait_for_tcp(conn
);
3966 if (conn
->conn_ops
->HeaderDigest
) {
3967 iov
.iov_base
= &digest
;
3968 iov
.iov_len
= ISCSI_CRC_LEN
;
3970 ret
= rx_data(conn
, &iov
, 1, ISCSI_CRC_LEN
);
3971 if (ret
!= ISCSI_CRC_LEN
) {
3972 iscsit_rx_thread_wait_for_tcp(conn
);
3976 iscsit_do_crypto_hash_buf(conn
->conn_rx_hash
, buffer
,
3977 ISCSI_HDR_LEN
, 0, NULL
,
3980 if (digest
!= checksum
) {
3981 pr_err("HeaderDigest CRC32C failed,"
3982 " received 0x%08x, computed 0x%08x\n",
3985 * Set the PDU to 0xff so it will intentionally
3986 * hit default in the switch below.
3988 memset(buffer
, 0xff, ISCSI_HDR_LEN
);
3989 atomic_long_inc(&conn
->sess
->conn_digest_errors
);
3991 pr_debug("Got HeaderDigest CRC32C"
3992 " 0x%08x\n", checksum
);
3996 if (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
)
3999 opcode
= buffer
[0] & ISCSI_OPCODE_MASK
;
4001 if (conn
->sess
->sess_ops
->SessionType
&&
4002 ((!(opcode
& ISCSI_OP_TEXT
)) ||
4003 (!(opcode
& ISCSI_OP_LOGOUT
)))) {
4004 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4005 " while in Discovery Session, rejecting.\n", opcode
);
4006 iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
4011 ret
= iscsi_target_rx_opcode(conn
, buffer
);
4019 int iscsi_target_rx_thread(void *arg
)
4022 struct iscsi_conn
*conn
= arg
;
4023 bool conn_freed
= false;
4026 * Allow ourselves to be interrupted by SIGINT so that a
4027 * connection recovery / failure event can be triggered externally.
4029 allow_signal(SIGINT
);
4031 * Wait for iscsi_post_login_handler() to complete before allowing
4032 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4034 rc
= wait_for_completion_interruptible(&conn
->rx_login_comp
);
4035 if (rc
< 0 || iscsi_target_check_conn_state(conn
))
4038 if (!conn
->conn_transport
->iscsit_get_rx_pdu
)
4041 conn
->conn_transport
->iscsit_get_rx_pdu(conn
);
4043 if (!signal_pending(current
))
4044 atomic_set(&conn
->transport_failed
, 1);
4045 iscsit_take_action_for_connection_exit(conn
, &conn_freed
);
4049 while (!kthread_should_stop()) {
4057 static void iscsit_release_commands_from_conn(struct iscsi_conn
*conn
)
4059 LIST_HEAD(tmp_list
);
4060 struct iscsi_cmd
*cmd
= NULL
, *cmd_tmp
= NULL
;
4061 struct iscsi_session
*sess
= conn
->sess
;
4063 * We expect this function to only ever be called from either RX or TX
4064 * thread context via iscsit_close_connection() once the other context
4065 * has been reset -> returned sleeping pre-handler state.
4067 spin_lock_bh(&conn
->cmd_lock
);
4068 list_splice_init(&conn
->conn_cmd_list
, &tmp_list
);
4070 list_for_each_entry(cmd
, &tmp_list
, i_conn_node
) {
4071 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
4073 if (se_cmd
->se_tfo
!= NULL
) {
4074 spin_lock_irq(&se_cmd
->t_state_lock
);
4075 se_cmd
->transport_state
|= CMD_T_FABRIC_STOP
;
4076 spin_unlock_irq(&se_cmd
->t_state_lock
);
4079 spin_unlock_bh(&conn
->cmd_lock
);
4081 list_for_each_entry_safe(cmd
, cmd_tmp
, &tmp_list
, i_conn_node
) {
4082 list_del_init(&cmd
->i_conn_node
);
4084 iscsit_increment_maxcmdsn(cmd
, sess
);
4085 iscsit_free_cmd(cmd
, true);
4090 static void iscsit_stop_timers_for_cmds(
4091 struct iscsi_conn
*conn
)
4093 struct iscsi_cmd
*cmd
;
4095 spin_lock_bh(&conn
->cmd_lock
);
4096 list_for_each_entry(cmd
, &conn
->conn_cmd_list
, i_conn_node
) {
4097 if (cmd
->data_direction
== DMA_TO_DEVICE
)
4098 iscsit_stop_dataout_timer(cmd
);
4100 spin_unlock_bh(&conn
->cmd_lock
);
4103 int iscsit_close_connection(
4104 struct iscsi_conn
*conn
)
4106 int conn_logout
= (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
);
4107 struct iscsi_session
*sess
= conn
->sess
;
4109 pr_debug("Closing iSCSI connection CID %hu on SID:"
4110 " %u\n", conn
->cid
, sess
->sid
);
4112 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4113 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4114 * sleeping and the logout response never got sent because the
4115 * connection failed.
4117 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4118 * to signal logout response TX interrupt completion. Go ahead and skip
4119 * this for iser since isert_rx_opcode() does not wait on logout failure,
4120 * and to avoid iscsi_conn pointer dereference in iser-target code.
4122 if (!conn
->conn_transport
->rdma_shutdown
)
4123 complete(&conn
->conn_logout_comp
);
4125 if (!strcmp(current
->comm
, ISCSI_RX_THREAD_NAME
)) {
4126 if (conn
->tx_thread
&&
4127 cmpxchg(&conn
->tx_thread_active
, true, false)) {
4128 send_sig(SIGINT
, conn
->tx_thread
, 1);
4129 kthread_stop(conn
->tx_thread
);
4131 } else if (!strcmp(current
->comm
, ISCSI_TX_THREAD_NAME
)) {
4132 if (conn
->rx_thread
&&
4133 cmpxchg(&conn
->rx_thread_active
, true, false)) {
4134 send_sig(SIGINT
, conn
->rx_thread
, 1);
4135 kthread_stop(conn
->rx_thread
);
4139 spin_lock(&iscsit_global
->ts_bitmap_lock
);
4140 bitmap_release_region(iscsit_global
->ts_bitmap
, conn
->bitmap_id
,
4142 spin_unlock(&iscsit_global
->ts_bitmap_lock
);
4144 iscsit_stop_timers_for_cmds(conn
);
4145 iscsit_stop_nopin_response_timer(conn
);
4146 iscsit_stop_nopin_timer(conn
);
4148 if (conn
->conn_transport
->iscsit_wait_conn
)
4149 conn
->conn_transport
->iscsit_wait_conn(conn
);
4152 * During Connection recovery drop unacknowledged out of order
4153 * commands for this connection, and prepare the other commands
4156 * During normal operation clear the out of order commands (but
4157 * do not free the struct iscsi_ooo_cmdsn's) and release all
4158 * struct iscsi_cmds.
4160 if (atomic_read(&conn
->connection_recovery
)) {
4161 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn
);
4162 iscsit_prepare_cmds_for_reallegiance(conn
);
4164 iscsit_clear_ooo_cmdsns_for_conn(conn
);
4165 iscsit_release_commands_from_conn(conn
);
4167 iscsit_free_queue_reqs_for_conn(conn
);
4170 * Handle decrementing session or connection usage count if
4171 * a logout response was not able to be sent because the
4172 * connection failed. Fall back to Session Recovery here.
4174 if (atomic_read(&conn
->conn_logout_remove
)) {
4175 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) {
4176 iscsit_dec_conn_usage_count(conn
);
4177 iscsit_dec_session_usage_count(sess
);
4179 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
)
4180 iscsit_dec_conn_usage_count(conn
);
4182 atomic_set(&conn
->conn_logout_remove
, 0);
4183 atomic_set(&sess
->session_reinstatement
, 0);
4184 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4187 spin_lock_bh(&sess
->conn_lock
);
4188 list_del(&conn
->conn_list
);
4191 * Attempt to let the Initiator know this connection failed by
4192 * sending an Connection Dropped Async Message on another
4193 * active connection.
4195 if (atomic_read(&conn
->connection_recovery
))
4196 iscsit_build_conn_drop_async_message(conn
);
4198 spin_unlock_bh(&sess
->conn_lock
);
4201 * If connection reinstatement is being performed on this connection,
4202 * up the connection reinstatement semaphore that is being blocked on
4203 * in iscsit_cause_connection_reinstatement().
4205 spin_lock_bh(&conn
->state_lock
);
4206 if (atomic_read(&conn
->sleep_on_conn_wait_comp
)) {
4207 spin_unlock_bh(&conn
->state_lock
);
4208 complete(&conn
->conn_wait_comp
);
4209 wait_for_completion(&conn
->conn_post_wait_comp
);
4210 spin_lock_bh(&conn
->state_lock
);
4214 * If connection reinstatement is being performed on this connection
4215 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4216 * connection wait rcfr semaphore that is being blocked on
4217 * an iscsit_connection_reinstatement_rcfr().
4219 if (atomic_read(&conn
->connection_wait_rcfr
)) {
4220 spin_unlock_bh(&conn
->state_lock
);
4221 complete(&conn
->conn_wait_rcfr_comp
);
4222 wait_for_completion(&conn
->conn_post_wait_comp
);
4223 spin_lock_bh(&conn
->state_lock
);
4225 atomic_set(&conn
->connection_reinstatement
, 1);
4226 spin_unlock_bh(&conn
->state_lock
);
4229 * If any other processes are accessing this connection pointer we
4230 * must wait until they have completed.
4232 iscsit_check_conn_usage_count(conn
);
4234 ahash_request_free(conn
->conn_tx_hash
);
4235 if (conn
->conn_rx_hash
) {
4236 struct crypto_ahash
*tfm
;
4238 tfm
= crypto_ahash_reqtfm(conn
->conn_rx_hash
);
4239 ahash_request_free(conn
->conn_rx_hash
);
4240 crypto_free_ahash(tfm
);
4244 sock_release(conn
->sock
);
4246 if (conn
->conn_transport
->iscsit_free_conn
)
4247 conn
->conn_transport
->iscsit_free_conn(conn
);
4249 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4250 conn
->conn_state
= TARG_CONN_STATE_FREE
;
4251 iscsit_free_conn(conn
);
4253 spin_lock_bh(&sess
->conn_lock
);
4254 atomic_dec(&sess
->nconn
);
4255 pr_debug("Decremented iSCSI connection count to %hu from node:"
4256 " %s\n", atomic_read(&sess
->nconn
),
4257 sess
->sess_ops
->InitiatorName
);
4259 * Make sure that if one connection fails in an non ERL=2 iSCSI
4260 * Session that they all fail.
4262 if ((sess
->sess_ops
->ErrorRecoveryLevel
!= 2) && !conn_logout
&&
4263 !atomic_read(&sess
->session_logout
))
4264 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4267 * If this was not the last connection in the session, and we are
4268 * performing session reinstatement or falling back to ERL=0, call
4269 * iscsit_stop_session() without sleeping to shutdown the other
4270 * active connections.
4272 if (atomic_read(&sess
->nconn
)) {
4273 if (!atomic_read(&sess
->session_reinstatement
) &&
4274 !atomic_read(&sess
->session_fall_back_to_erl0
)) {
4275 spin_unlock_bh(&sess
->conn_lock
);
4278 if (!atomic_read(&sess
->session_stop_active
)) {
4279 atomic_set(&sess
->session_stop_active
, 1);
4280 spin_unlock_bh(&sess
->conn_lock
);
4281 iscsit_stop_session(sess
, 0, 0);
4284 spin_unlock_bh(&sess
->conn_lock
);
4289 * If this was the last connection in the session and one of the
4290 * following is occurring:
4292 * Session Reinstatement is not being performed, and are falling back
4293 * to ERL=0 call iscsit_close_session().
4295 * Session Logout was requested. iscsit_close_session() will be called
4298 * Session Continuation is not being performed, start the Time2Retain
4299 * handler and check if sleep_on_sess_wait_sem is active.
4301 if (!atomic_read(&sess
->session_reinstatement
) &&
4302 atomic_read(&sess
->session_fall_back_to_erl0
)) {
4303 spin_unlock_bh(&sess
->conn_lock
);
4304 complete_all(&sess
->session_wait_comp
);
4305 iscsit_close_session(sess
);
4308 } else if (atomic_read(&sess
->session_logout
)) {
4309 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4310 sess
->session_state
= TARG_SESS_STATE_FREE
;
4312 if (atomic_read(&sess
->session_close
)) {
4313 spin_unlock_bh(&sess
->conn_lock
);
4314 complete_all(&sess
->session_wait_comp
);
4315 iscsit_close_session(sess
);
4317 spin_unlock_bh(&sess
->conn_lock
);
4322 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4323 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4325 if (!atomic_read(&sess
->session_continuation
))
4326 iscsit_start_time2retain_handler(sess
);
4328 if (atomic_read(&sess
->session_close
)) {
4329 spin_unlock_bh(&sess
->conn_lock
);
4330 complete_all(&sess
->session_wait_comp
);
4331 iscsit_close_session(sess
);
4333 spin_unlock_bh(&sess
->conn_lock
);
4341 * If the iSCSI Session for the iSCSI Initiator Node exists,
4342 * forcefully shutdown the iSCSI NEXUS.
4344 int iscsit_close_session(struct iscsi_session
*sess
)
4346 struct iscsi_portal_group
*tpg
= sess
->tpg
;
4347 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4349 if (atomic_read(&sess
->nconn
)) {
4350 pr_err("%d connection(s) still exist for iSCSI session"
4351 " to %s\n", atomic_read(&sess
->nconn
),
4352 sess
->sess_ops
->InitiatorName
);
4356 spin_lock_bh(&se_tpg
->session_lock
);
4357 atomic_set(&sess
->session_logout
, 1);
4358 atomic_set(&sess
->session_reinstatement
, 1);
4359 iscsit_stop_time2retain_timer(sess
);
4360 spin_unlock_bh(&se_tpg
->session_lock
);
4363 * transport_deregister_session_configfs() will clear the
4364 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4365 * can be setting it again with __transport_register_session() in
4366 * iscsi_post_login_handler() again after the iscsit_stop_session()
4367 * completes in iscsi_np context.
4369 transport_deregister_session_configfs(sess
->se_sess
);
4372 * If any other processes are accessing this session pointer we must
4373 * wait until they have completed. If we are in an interrupt (the
4374 * time2retain handler) and contain and active session usage count we
4375 * restart the timer and exit.
4377 if (!in_interrupt()) {
4378 iscsit_check_session_usage_count(sess
);
4380 if (iscsit_check_session_usage_count(sess
) == 2) {
4381 atomic_set(&sess
->session_logout
, 0);
4382 iscsit_start_time2retain_handler(sess
);
4387 transport_deregister_session(sess
->se_sess
);
4389 if (sess
->sess_ops
->ErrorRecoveryLevel
== 2)
4390 iscsit_free_connection_recovery_entries(sess
);
4392 iscsit_free_all_ooo_cmdsns(sess
);
4394 spin_lock_bh(&se_tpg
->session_lock
);
4395 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4396 sess
->session_state
= TARG_SESS_STATE_FREE
;
4397 pr_debug("Released iSCSI session from node: %s\n",
4398 sess
->sess_ops
->InitiatorName
);
4401 tpg
->tpg_tiqn
->tiqn_nsessions
--;
4403 pr_debug("Decremented number of active iSCSI Sessions on"
4404 " iSCSI TPG: %hu to %u\n", tpg
->tpgt
, tpg
->nsessions
);
4406 ida_free(&sess_ida
, sess
->session_index
);
4407 kfree(sess
->sess_ops
);
4408 sess
->sess_ops
= NULL
;
4409 spin_unlock_bh(&se_tpg
->session_lock
);
4415 static void iscsit_logout_post_handler_closesession(
4416 struct iscsi_conn
*conn
)
4418 struct iscsi_session
*sess
= conn
->sess
;
4421 * Traditional iscsi/tcp will invoke this logic from TX thread
4422 * context during session logout, so clear tx_thread_active and
4423 * sleep if iscsit_close_connection() has not already occured.
4425 * Since iser-target invokes this logic from it's own workqueue,
4426 * always sleep waiting for RX/TX thread shutdown to complete
4427 * within iscsit_close_connection().
4429 if (!conn
->conn_transport
->rdma_shutdown
) {
4430 sleep
= cmpxchg(&conn
->tx_thread_active
, true, false);
4435 atomic_set(&conn
->conn_logout_remove
, 0);
4436 complete(&conn
->conn_logout_comp
);
4438 iscsit_dec_conn_usage_count(conn
);
4439 atomic_set(&sess
->session_close
, 1);
4440 iscsit_stop_session(sess
, sleep
, sleep
);
4441 iscsit_dec_session_usage_count(sess
);
4444 static void iscsit_logout_post_handler_samecid(
4445 struct iscsi_conn
*conn
)
4449 if (!conn
->conn_transport
->rdma_shutdown
) {
4450 sleep
= cmpxchg(&conn
->tx_thread_active
, true, false);
4455 atomic_set(&conn
->conn_logout_remove
, 0);
4456 complete(&conn
->conn_logout_comp
);
4458 iscsit_cause_connection_reinstatement(conn
, sleep
);
4459 iscsit_dec_conn_usage_count(conn
);
4462 static void iscsit_logout_post_handler_diffcid(
4463 struct iscsi_conn
*conn
,
4466 struct iscsi_conn
*l_conn
;
4467 struct iscsi_session
*sess
= conn
->sess
;
4468 bool conn_found
= false;
4473 spin_lock_bh(&sess
->conn_lock
);
4474 list_for_each_entry(l_conn
, &sess
->sess_conn_list
, conn_list
) {
4475 if (l_conn
->cid
== cid
) {
4476 iscsit_inc_conn_usage_count(l_conn
);
4481 spin_unlock_bh(&sess
->conn_lock
);
4487 l_conn
->sock
->ops
->shutdown(l_conn
->sock
, RCV_SHUTDOWN
);
4489 spin_lock_bh(&l_conn
->state_lock
);
4490 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4491 l_conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
4492 spin_unlock_bh(&l_conn
->state_lock
);
4494 iscsit_cause_connection_reinstatement(l_conn
, 1);
4495 iscsit_dec_conn_usage_count(l_conn
);
4499 * Return of 0 causes the TX thread to restart.
4501 int iscsit_logout_post_handler(
4502 struct iscsi_cmd
*cmd
,
4503 struct iscsi_conn
*conn
)
4507 switch (cmd
->logout_reason
) {
4508 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
4509 switch (cmd
->logout_response
) {
4510 case ISCSI_LOGOUT_SUCCESS
:
4511 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4513 iscsit_logout_post_handler_closesession(conn
);
4518 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
4519 if (conn
->cid
== cmd
->logout_cid
) {
4520 switch (cmd
->logout_response
) {
4521 case ISCSI_LOGOUT_SUCCESS
:
4522 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4524 iscsit_logout_post_handler_samecid(conn
);
4529 switch (cmd
->logout_response
) {
4530 case ISCSI_LOGOUT_SUCCESS
:
4531 iscsit_logout_post_handler_diffcid(conn
,
4534 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4535 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4542 case ISCSI_LOGOUT_REASON_RECOVERY
:
4543 switch (cmd
->logout_response
) {
4544 case ISCSI_LOGOUT_SUCCESS
:
4545 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4546 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
:
4547 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4559 EXPORT_SYMBOL(iscsit_logout_post_handler
);
4561 void iscsit_fail_session(struct iscsi_session
*sess
)
4563 struct iscsi_conn
*conn
;
4565 spin_lock_bh(&sess
->conn_lock
);
4566 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
) {
4567 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4568 conn
->conn_state
= TARG_CONN_STATE_CLEANUP_WAIT
;
4570 spin_unlock_bh(&sess
->conn_lock
);
4572 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4573 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4576 void iscsit_stop_session(
4577 struct iscsi_session
*sess
,
4579 int connection_sleep
)
4581 u16 conn_count
= atomic_read(&sess
->nconn
);
4582 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4585 spin_lock_bh(&sess
->conn_lock
);
4587 if (connection_sleep
) {
4588 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4590 if (conn_count
== 0)
4593 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4596 iscsit_inc_conn_usage_count(conn_tmp
);
4599 iscsit_inc_conn_usage_count(conn
);
4601 spin_unlock_bh(&sess
->conn_lock
);
4602 iscsit_cause_connection_reinstatement(conn
, 1);
4603 spin_lock_bh(&sess
->conn_lock
);
4605 iscsit_dec_conn_usage_count(conn
);
4607 iscsit_dec_conn_usage_count(conn_tmp
);
4611 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
)
4612 iscsit_cause_connection_reinstatement(conn
, 0);
4615 if (session_sleep
&& atomic_read(&sess
->nconn
)) {
4616 spin_unlock_bh(&sess
->conn_lock
);
4617 wait_for_completion(&sess
->session_wait_comp
);
4619 spin_unlock_bh(&sess
->conn_lock
);
4622 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group
*tpg
, int force
)
4624 struct iscsi_session
*sess
;
4625 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4626 struct se_session
*se_sess
, *se_sess_tmp
;
4627 LIST_HEAD(free_list
);
4628 int session_count
= 0;
4630 spin_lock_bh(&se_tpg
->session_lock
);
4631 if (tpg
->nsessions
&& !force
) {
4632 spin_unlock_bh(&se_tpg
->session_lock
);
4636 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &se_tpg
->tpg_sess_list
,
4638 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4640 spin_lock(&sess
->conn_lock
);
4641 if (atomic_read(&sess
->session_fall_back_to_erl0
) ||
4642 atomic_read(&sess
->session_logout
) ||
4643 atomic_read(&sess
->session_close
) ||
4644 (sess
->time2retain_timer_flags
& ISCSI_TF_EXPIRED
)) {
4645 spin_unlock(&sess
->conn_lock
);
4648 iscsit_inc_session_usage_count(sess
);
4649 atomic_set(&sess
->session_reinstatement
, 1);
4650 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4651 atomic_set(&sess
->session_close
, 1);
4652 spin_unlock(&sess
->conn_lock
);
4654 list_move_tail(&se_sess
->sess_list
, &free_list
);
4656 spin_unlock_bh(&se_tpg
->session_lock
);
4658 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &free_list
, sess_list
) {
4659 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4661 list_del_init(&se_sess
->sess_list
);
4662 iscsit_stop_session(sess
, 1, 1);
4663 iscsit_dec_session_usage_count(sess
);
4667 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4668 " Group: %hu\n", session_count
, tpg
->tpgt
);
4672 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4673 MODULE_VERSION("4.1.x");
4674 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4675 MODULE_LICENSE("GPL");
4677 module_init(iscsi_target_init_module
);
4678 module_exit(iscsi_target_cleanup_module
);