1 /*******************************************************************************
2 * This file contains main functions related to the iSCSI Target Core Driver.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
19 #include <linux/string.h>
20 #include <linux/kthread.h>
21 #include <linux/crypto.h>
22 #include <linux/completion.h>
23 #include <linux/module.h>
24 #include <linux/idr.h>
25 #include <asm/unaligned.h>
26 #include <scsi/scsi_device.h>
27 #include <scsi/iscsi_proto.h>
28 #include <scsi/scsi_tcq.h>
29 #include <target/target_core_base.h>
30 #include <target/target_core_fabric.h>
31 #include <target/target_core_configfs.h>
33 #include <target/iscsi/iscsi_target_core.h>
34 #include "iscsi_target_parameters.h"
35 #include "iscsi_target_seq_pdu_list.h"
36 #include "iscsi_target_datain_values.h"
37 #include "iscsi_target_erl0.h"
38 #include "iscsi_target_erl1.h"
39 #include "iscsi_target_erl2.h"
40 #include "iscsi_target_login.h"
41 #include "iscsi_target_tmr.h"
42 #include "iscsi_target_tpg.h"
43 #include "iscsi_target_util.h"
44 #include "iscsi_target.h"
45 #include "iscsi_target_device.h"
46 #include <target/iscsi/iscsi_target_stat.h>
48 #include <target/iscsi/iscsi_transport.h>
50 static LIST_HEAD(g_tiqn_list
);
51 static LIST_HEAD(g_np_list
);
52 static DEFINE_SPINLOCK(tiqn_lock
);
53 static DEFINE_MUTEX(np_lock
);
55 static struct idr tiqn_idr
;
57 struct mutex auth_id_lock
;
58 spinlock_t sess_idr_lock
;
60 struct iscsit_global
*iscsit_global
;
62 struct kmem_cache
*lio_qr_cache
;
63 struct kmem_cache
*lio_dr_cache
;
64 struct kmem_cache
*lio_ooo_cache
;
65 struct kmem_cache
*lio_r2t_cache
;
67 static int iscsit_handle_immediate_data(struct iscsi_cmd
*,
68 struct iscsi_scsi_req
*, u32
);
70 struct iscsi_tiqn
*iscsit_get_tiqn_for_login(unsigned char *buf
)
72 struct iscsi_tiqn
*tiqn
= NULL
;
74 spin_lock(&tiqn_lock
);
75 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
76 if (!strcmp(tiqn
->tiqn
, buf
)) {
78 spin_lock(&tiqn
->tiqn_state_lock
);
79 if (tiqn
->tiqn_state
== TIQN_STATE_ACTIVE
) {
80 tiqn
->tiqn_access_count
++;
81 spin_unlock(&tiqn
->tiqn_state_lock
);
82 spin_unlock(&tiqn_lock
);
85 spin_unlock(&tiqn
->tiqn_state_lock
);
88 spin_unlock(&tiqn_lock
);
93 static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn
*tiqn
)
95 spin_lock(&tiqn
->tiqn_state_lock
);
96 if (tiqn
->tiqn_state
== TIQN_STATE_ACTIVE
) {
97 tiqn
->tiqn_state
= TIQN_STATE_SHUTDOWN
;
98 spin_unlock(&tiqn
->tiqn_state_lock
);
101 spin_unlock(&tiqn
->tiqn_state_lock
);
106 void iscsit_put_tiqn_for_login(struct iscsi_tiqn
*tiqn
)
108 spin_lock(&tiqn
->tiqn_state_lock
);
109 tiqn
->tiqn_access_count
--;
110 spin_unlock(&tiqn
->tiqn_state_lock
);
114 * Note that IQN formatting is expected to be done in userspace, and
115 * no explict IQN format checks are done here.
117 struct iscsi_tiqn
*iscsit_add_tiqn(unsigned char *buf
)
119 struct iscsi_tiqn
*tiqn
= NULL
;
122 if (strlen(buf
) >= ISCSI_IQN_LEN
) {
123 pr_err("Target IQN exceeds %d bytes\n",
125 return ERR_PTR(-EINVAL
);
128 tiqn
= kzalloc(sizeof(struct iscsi_tiqn
), GFP_KERNEL
);
130 pr_err("Unable to allocate struct iscsi_tiqn\n");
131 return ERR_PTR(-ENOMEM
);
134 sprintf(tiqn
->tiqn
, "%s", buf
);
135 INIT_LIST_HEAD(&tiqn
->tiqn_list
);
136 INIT_LIST_HEAD(&tiqn
->tiqn_tpg_list
);
137 spin_lock_init(&tiqn
->tiqn_state_lock
);
138 spin_lock_init(&tiqn
->tiqn_tpg_lock
);
139 spin_lock_init(&tiqn
->sess_err_stats
.lock
);
140 spin_lock_init(&tiqn
->login_stats
.lock
);
141 spin_lock_init(&tiqn
->logout_stats
.lock
);
143 tiqn
->tiqn_state
= TIQN_STATE_ACTIVE
;
145 idr_preload(GFP_KERNEL
);
146 spin_lock(&tiqn_lock
);
148 ret
= idr_alloc(&tiqn_idr
, NULL
, 0, 0, GFP_NOWAIT
);
150 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
151 spin_unlock(&tiqn_lock
);
156 tiqn
->tiqn_index
= ret
;
157 list_add_tail(&tiqn
->tiqn_list
, &g_tiqn_list
);
159 spin_unlock(&tiqn_lock
);
162 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn
->tiqn
);
168 static void iscsit_wait_for_tiqn(struct iscsi_tiqn
*tiqn
)
171 * Wait for accesses to said struct iscsi_tiqn to end.
173 spin_lock(&tiqn
->tiqn_state_lock
);
174 while (tiqn
->tiqn_access_count
!= 0) {
175 spin_unlock(&tiqn
->tiqn_state_lock
);
177 spin_lock(&tiqn
->tiqn_state_lock
);
179 spin_unlock(&tiqn
->tiqn_state_lock
);
182 void iscsit_del_tiqn(struct iscsi_tiqn
*tiqn
)
185 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
186 * while holding tiqn->tiqn_state_lock. This means that all subsequent
187 * attempts to access this struct iscsi_tiqn will fail from both transport
188 * fabric and control code paths.
190 if (iscsit_set_tiqn_shutdown(tiqn
) < 0) {
191 pr_err("iscsit_set_tiqn_shutdown() failed\n");
195 iscsit_wait_for_tiqn(tiqn
);
197 spin_lock(&tiqn_lock
);
198 list_del(&tiqn
->tiqn_list
);
199 idr_remove(&tiqn_idr
, tiqn
->tiqn_index
);
200 spin_unlock(&tiqn_lock
);
202 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
207 int iscsit_access_np(struct iscsi_np
*np
, struct iscsi_portal_group
*tpg
)
211 * Determine if the network portal is accepting storage traffic.
213 spin_lock_bh(&np
->np_thread_lock
);
214 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
215 spin_unlock_bh(&np
->np_thread_lock
);
218 spin_unlock_bh(&np
->np_thread_lock
);
220 * Determine if the portal group is accepting storage traffic.
222 spin_lock_bh(&tpg
->tpg_state_lock
);
223 if (tpg
->tpg_state
!= TPG_STATE_ACTIVE
) {
224 spin_unlock_bh(&tpg
->tpg_state_lock
);
227 spin_unlock_bh(&tpg
->tpg_state_lock
);
230 * Here we serialize access across the TIQN+TPG Tuple.
232 ret
= down_interruptible(&tpg
->np_login_sem
);
236 spin_lock_bh(&tpg
->tpg_state_lock
);
237 if (tpg
->tpg_state
!= TPG_STATE_ACTIVE
) {
238 spin_unlock_bh(&tpg
->tpg_state_lock
);
239 up(&tpg
->np_login_sem
);
242 spin_unlock_bh(&tpg
->tpg_state_lock
);
247 void iscsit_login_kref_put(struct kref
*kref
)
249 struct iscsi_tpg_np
*tpg_np
= container_of(kref
,
250 struct iscsi_tpg_np
, tpg_np_kref
);
252 complete(&tpg_np
->tpg_np_comp
);
255 int iscsit_deaccess_np(struct iscsi_np
*np
, struct iscsi_portal_group
*tpg
,
256 struct iscsi_tpg_np
*tpg_np
)
258 struct iscsi_tiqn
*tiqn
= tpg
->tpg_tiqn
;
260 up(&tpg
->np_login_sem
);
263 kref_put(&tpg_np
->tpg_np_kref
, iscsit_login_kref_put
);
266 iscsit_put_tiqn_for_login(tiqn
);
271 bool iscsit_check_np_match(
272 struct __kernel_sockaddr_storage
*sockaddr
,
274 int network_transport
)
276 struct sockaddr_in
*sock_in
, *sock_in_e
;
277 struct sockaddr_in6
*sock_in6
, *sock_in6_e
;
278 bool ip_match
= false;
281 if (sockaddr
->ss_family
== AF_INET6
) {
282 sock_in6
= (struct sockaddr_in6
*)sockaddr
;
283 sock_in6_e
= (struct sockaddr_in6
*)&np
->np_sockaddr
;
285 if (!memcmp(&sock_in6
->sin6_addr
.in6_u
,
286 &sock_in6_e
->sin6_addr
.in6_u
,
287 sizeof(struct in6_addr
)))
290 port
= ntohs(sock_in6
->sin6_port
);
292 sock_in
= (struct sockaddr_in
*)sockaddr
;
293 sock_in_e
= (struct sockaddr_in
*)&np
->np_sockaddr
;
295 if (sock_in
->sin_addr
.s_addr
== sock_in_e
->sin_addr
.s_addr
)
298 port
= ntohs(sock_in
->sin_port
);
301 if (ip_match
&& (np
->np_port
== port
) &&
302 (np
->np_network_transport
== network_transport
))
309 * Called with mutex np_lock held
311 static struct iscsi_np
*iscsit_get_np(
312 struct __kernel_sockaddr_storage
*sockaddr
,
313 int network_transport
)
318 list_for_each_entry(np
, &g_np_list
, np_list
) {
319 spin_lock_bh(&np
->np_thread_lock
);
320 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
321 spin_unlock_bh(&np
->np_thread_lock
);
325 match
= iscsit_check_np_match(sockaddr
, np
, network_transport
);
328 * Increment the np_exports reference count now to
329 * prevent iscsit_del_np() below from being called
330 * while iscsi_tpg_add_network_portal() is called.
333 spin_unlock_bh(&np
->np_thread_lock
);
336 spin_unlock_bh(&np
->np_thread_lock
);
342 struct iscsi_np
*iscsit_add_np(
343 struct __kernel_sockaddr_storage
*sockaddr
,
344 int network_transport
)
346 struct sockaddr_in
*sock_in
;
347 struct sockaddr_in6
*sock_in6
;
351 mutex_lock(&np_lock
);
354 * Locate the existing struct iscsi_np if already active..
356 np
= iscsit_get_np(sockaddr
, network_transport
);
358 mutex_unlock(&np_lock
);
362 np
= kzalloc(sizeof(struct iscsi_np
), GFP_KERNEL
);
364 pr_err("Unable to allocate memory for struct iscsi_np\n");
365 mutex_unlock(&np_lock
);
366 return ERR_PTR(-ENOMEM
);
369 np
->np_flags
|= NPF_IP_NETWORK
;
370 if (sockaddr
->ss_family
== AF_INET6
) {
371 sock_in6
= (struct sockaddr_in6
*)sockaddr
;
372 np
->np_port
= ntohs(sock_in6
->sin6_port
);
374 sock_in
= (struct sockaddr_in
*)sockaddr
;
375 np
->np_port
= ntohs(sock_in
->sin_port
);
378 np
->np_network_transport
= network_transport
;
379 spin_lock_init(&np
->np_thread_lock
);
380 init_completion(&np
->np_restart_comp
);
381 INIT_LIST_HEAD(&np
->np_list
);
383 ret
= iscsi_target_setup_login_socket(np
, sockaddr
);
386 mutex_unlock(&np_lock
);
390 np
->np_thread
= kthread_run(iscsi_target_login_thread
, np
, "iscsi_np");
391 if (IS_ERR(np
->np_thread
)) {
392 pr_err("Unable to create kthread: iscsi_np\n");
393 ret
= PTR_ERR(np
->np_thread
);
395 mutex_unlock(&np_lock
);
399 * Increment the np_exports reference count now to prevent
400 * iscsit_del_np() below from being run while a new call to
401 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
402 * active. We don't need to hold np->np_thread_lock at this
403 * point because iscsi_np has not been added to g_np_list yet.
406 np
->np_thread_state
= ISCSI_NP_THREAD_ACTIVE
;
408 list_add_tail(&np
->np_list
, &g_np_list
);
409 mutex_unlock(&np_lock
);
411 pr_debug("CORE[0] - Added Network Portal: %pISc:%hu on %s\n",
412 &np
->np_sockaddr
, np
->np_port
, np
->np_transport
->name
);
417 int iscsit_reset_np_thread(
419 struct iscsi_tpg_np
*tpg_np
,
420 struct iscsi_portal_group
*tpg
,
423 spin_lock_bh(&np
->np_thread_lock
);
424 if (np
->np_thread_state
== ISCSI_NP_THREAD_INACTIVE
) {
425 spin_unlock_bh(&np
->np_thread_lock
);
428 np
->np_thread_state
= ISCSI_NP_THREAD_RESET
;
431 spin_unlock_bh(&np
->np_thread_lock
);
432 send_sig(SIGINT
, np
->np_thread
, 1);
433 wait_for_completion(&np
->np_restart_comp
);
434 spin_lock_bh(&np
->np_thread_lock
);
436 spin_unlock_bh(&np
->np_thread_lock
);
438 if (tpg_np
&& shutdown
) {
439 kref_put(&tpg_np
->tpg_np_kref
, iscsit_login_kref_put
);
441 wait_for_completion(&tpg_np
->tpg_np_comp
);
447 static void iscsit_free_np(struct iscsi_np
*np
)
450 sock_release(np
->np_socket
);
453 int iscsit_del_np(struct iscsi_np
*np
)
455 spin_lock_bh(&np
->np_thread_lock
);
457 if (np
->np_exports
) {
459 spin_unlock_bh(&np
->np_thread_lock
);
462 np
->np_thread_state
= ISCSI_NP_THREAD_SHUTDOWN
;
463 spin_unlock_bh(&np
->np_thread_lock
);
467 * We need to send the signal to wakeup Linux/Net
468 * which may be sleeping in sock_accept()..
470 send_sig(SIGINT
, np
->np_thread
, 1);
471 kthread_stop(np
->np_thread
);
472 np
->np_thread
= NULL
;
475 np
->np_transport
->iscsit_free_np(np
);
477 mutex_lock(&np_lock
);
478 list_del(&np
->np_list
);
479 mutex_unlock(&np_lock
);
481 pr_debug("CORE[0] - Removed Network Portal: %pISc:%hu on %s\n",
482 &np
->np_sockaddr
, np
->np_port
, np
->np_transport
->name
);
484 iscsit_put_transport(np
->np_transport
);
489 static int iscsit_immediate_queue(struct iscsi_conn
*, struct iscsi_cmd
*, int);
490 static int iscsit_response_queue(struct iscsi_conn
*, struct iscsi_cmd
*, int);
492 static int iscsit_queue_rsp(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
)
494 iscsit_add_cmd_to_response_queue(cmd
, cmd
->conn
, cmd
->i_state
);
498 static void iscsit_aborted_task(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
)
500 bool scsi_cmd
= (cmd
->iscsi_opcode
== ISCSI_OP_SCSI_CMD
);
502 spin_lock_bh(&conn
->cmd_lock
);
503 if (!list_empty(&cmd
->i_conn_node
) &&
504 !(cmd
->se_cmd
.transport_state
& CMD_T_FABRIC_STOP
))
505 list_del_init(&cmd
->i_conn_node
);
506 spin_unlock_bh(&conn
->cmd_lock
);
508 __iscsit_free_cmd(cmd
, scsi_cmd
, true);
511 static enum target_prot_op
iscsit_get_sup_prot_ops(struct iscsi_conn
*conn
)
513 return TARGET_PROT_NORMAL
;
516 static struct iscsit_transport iscsi_target_transport
= {
518 .transport_type
= ISCSI_TCP
,
520 .iscsit_setup_np
= iscsit_setup_np
,
521 .iscsit_accept_np
= iscsit_accept_np
,
522 .iscsit_free_np
= iscsit_free_np
,
523 .iscsit_get_login_rx
= iscsit_get_login_rx
,
524 .iscsit_put_login_tx
= iscsit_put_login_tx
,
525 .iscsit_get_dataout
= iscsit_build_r2ts_for_cmd
,
526 .iscsit_immediate_queue
= iscsit_immediate_queue
,
527 .iscsit_response_queue
= iscsit_response_queue
,
528 .iscsit_queue_data_in
= iscsit_queue_rsp
,
529 .iscsit_queue_status
= iscsit_queue_rsp
,
530 .iscsit_aborted_task
= iscsit_aborted_task
,
531 .iscsit_get_sup_prot_ops
= iscsit_get_sup_prot_ops
,
534 static int __init
iscsi_target_init_module(void)
538 pr_debug("iSCSI-Target "ISCSIT_VERSION
"\n");
540 iscsit_global
= kzalloc(sizeof(struct iscsit_global
), GFP_KERNEL
);
541 if (!iscsit_global
) {
542 pr_err("Unable to allocate memory for iscsit_global\n");
545 spin_lock_init(&iscsit_global
->ts_bitmap_lock
);
546 mutex_init(&auth_id_lock
);
547 spin_lock_init(&sess_idr_lock
);
551 ret
= target_register_template(&iscsi_ops
);
555 size
= BITS_TO_LONGS(ISCSIT_BITMAP_BITS
) * sizeof(long);
556 iscsit_global
->ts_bitmap
= vzalloc(size
);
557 if (!iscsit_global
->ts_bitmap
) {
558 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
562 lio_qr_cache
= kmem_cache_create("lio_qr_cache",
563 sizeof(struct iscsi_queue_req
),
564 __alignof__(struct iscsi_queue_req
), 0, NULL
);
566 pr_err("nable to kmem_cache_create() for"
571 lio_dr_cache
= kmem_cache_create("lio_dr_cache",
572 sizeof(struct iscsi_datain_req
),
573 __alignof__(struct iscsi_datain_req
), 0, NULL
);
575 pr_err("Unable to kmem_cache_create() for"
580 lio_ooo_cache
= kmem_cache_create("lio_ooo_cache",
581 sizeof(struct iscsi_ooo_cmdsn
),
582 __alignof__(struct iscsi_ooo_cmdsn
), 0, NULL
);
583 if (!lio_ooo_cache
) {
584 pr_err("Unable to kmem_cache_create() for"
589 lio_r2t_cache
= kmem_cache_create("lio_r2t_cache",
590 sizeof(struct iscsi_r2t
), __alignof__(struct iscsi_r2t
),
592 if (!lio_r2t_cache
) {
593 pr_err("Unable to kmem_cache_create() for"
598 iscsit_register_transport(&iscsi_target_transport
);
600 if (iscsit_load_discovery_tpg() < 0)
605 iscsit_unregister_transport(&iscsi_target_transport
);
606 kmem_cache_destroy(lio_r2t_cache
);
608 kmem_cache_destroy(lio_ooo_cache
);
610 kmem_cache_destroy(lio_dr_cache
);
612 kmem_cache_destroy(lio_qr_cache
);
614 vfree(iscsit_global
->ts_bitmap
);
616 /* XXX: this probably wants it to be it's own unwind step.. */
617 if (iscsit_global
->discovery_tpg
)
618 iscsit_tpg_disable_portal_group(iscsit_global
->discovery_tpg
, 1);
619 target_unregister_template(&iscsi_ops
);
621 kfree(iscsit_global
);
625 static void __exit
iscsi_target_cleanup_module(void)
627 iscsit_release_discovery_tpg();
628 iscsit_unregister_transport(&iscsi_target_transport
);
629 kmem_cache_destroy(lio_qr_cache
);
630 kmem_cache_destroy(lio_dr_cache
);
631 kmem_cache_destroy(lio_ooo_cache
);
632 kmem_cache_destroy(lio_r2t_cache
);
635 * Shutdown discovery sessions and disable discovery TPG
637 if (iscsit_global
->discovery_tpg
)
638 iscsit_tpg_disable_portal_group(iscsit_global
->discovery_tpg
, 1);
640 target_unregister_template(&iscsi_ops
);
642 vfree(iscsit_global
->ts_bitmap
);
643 kfree(iscsit_global
);
646 static int iscsit_add_reject(
647 struct iscsi_conn
*conn
,
651 struct iscsi_cmd
*cmd
;
653 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
657 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
658 cmd
->reject_reason
= reason
;
660 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
662 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
663 iscsit_free_cmd(cmd
, false);
667 spin_lock_bh(&conn
->cmd_lock
);
668 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
669 spin_unlock_bh(&conn
->cmd_lock
);
671 cmd
->i_state
= ISTATE_SEND_REJECT
;
672 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
677 static int iscsit_add_reject_from_cmd(
678 struct iscsi_cmd
*cmd
,
683 struct iscsi_conn
*conn
;
686 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
692 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
693 cmd
->reject_reason
= reason
;
695 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
697 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
698 iscsit_free_cmd(cmd
, false);
703 spin_lock_bh(&conn
->cmd_lock
);
704 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
705 spin_unlock_bh(&conn
->cmd_lock
);
708 cmd
->i_state
= ISTATE_SEND_REJECT
;
709 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
711 * Perform the kref_put now if se_cmd has already been setup by
712 * scsit_setup_scsi_cmd()
714 if (cmd
->se_cmd
.se_tfo
!= NULL
) {
715 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
716 target_put_sess_cmd(&cmd
->se_cmd
);
721 static int iscsit_add_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
,
724 return iscsit_add_reject_from_cmd(cmd
, reason
, true, buf
);
727 int iscsit_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
, unsigned char *buf
)
729 return iscsit_add_reject_from_cmd(cmd
, reason
, false, buf
);
733 * Map some portion of the allocated scatterlist to an iovec, suitable for
734 * kernel sockets to copy data in/out.
736 static int iscsit_map_iovec(
737 struct iscsi_cmd
*cmd
,
743 struct scatterlist
*sg
;
744 unsigned int page_off
;
747 * We know each entry in t_data_sg contains a page.
749 sg
= &cmd
->se_cmd
.t_data_sg
[data_offset
/ PAGE_SIZE
];
750 page_off
= (data_offset
% PAGE_SIZE
);
752 cmd
->first_data_sg
= sg
;
753 cmd
->first_data_sg_off
= page_off
;
755 while (data_length
) {
756 u32 cur_len
= min_t(u32
, data_length
, sg
->length
- page_off
);
758 iov
[i
].iov_base
= kmap(sg_page(sg
)) + sg
->offset
+ page_off
;
759 iov
[i
].iov_len
= cur_len
;
761 data_length
-= cur_len
;
767 cmd
->kmapped_nents
= i
;
772 static void iscsit_unmap_iovec(struct iscsi_cmd
*cmd
)
775 struct scatterlist
*sg
;
777 sg
= cmd
->first_data_sg
;
779 for (i
= 0; i
< cmd
->kmapped_nents
; i
++)
780 kunmap(sg_page(&sg
[i
]));
783 static void iscsit_ack_from_expstatsn(struct iscsi_conn
*conn
, u32 exp_statsn
)
786 struct iscsi_cmd
*cmd
, *cmd_p
;
788 conn
->exp_statsn
= exp_statsn
;
790 if (conn
->sess
->sess_ops
->RDMAExtensions
)
793 spin_lock_bh(&conn
->cmd_lock
);
794 list_for_each_entry_safe(cmd
, cmd_p
, &conn
->conn_cmd_list
, i_conn_node
) {
795 spin_lock(&cmd
->istate_lock
);
796 if ((cmd
->i_state
== ISTATE_SENT_STATUS
) &&
797 iscsi_sna_lt(cmd
->stat_sn
, exp_statsn
)) {
798 cmd
->i_state
= ISTATE_REMOVE
;
799 spin_unlock(&cmd
->istate_lock
);
800 list_move_tail(&cmd
->i_conn_node
, &ack_list
);
803 spin_unlock(&cmd
->istate_lock
);
805 spin_unlock_bh(&conn
->cmd_lock
);
807 list_for_each_entry_safe(cmd
, cmd_p
, &ack_list
, i_conn_node
) {
808 list_del_init(&cmd
->i_conn_node
);
809 iscsit_free_cmd(cmd
, false);
813 static int iscsit_allocate_iovecs(struct iscsi_cmd
*cmd
)
815 u32 iov_count
= max(1UL, DIV_ROUND_UP(cmd
->se_cmd
.data_length
, PAGE_SIZE
));
817 iov_count
+= ISCSI_IOV_DATA_BUFFER
;
819 cmd
->iov_data
= kzalloc(iov_count
* sizeof(struct kvec
), GFP_KERNEL
);
820 if (!cmd
->iov_data
) {
821 pr_err("Unable to allocate cmd->iov_data\n");
825 cmd
->orig_iov_data_count
= iov_count
;
829 int iscsit_setup_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
832 int data_direction
, payload_length
;
833 struct iscsi_scsi_req
*hdr
;
837 atomic_long_inc(&conn
->sess
->cmd_pdus
);
839 hdr
= (struct iscsi_scsi_req
*) buf
;
840 payload_length
= ntoh24(hdr
->dlength
);
842 /* FIXME; Add checks for AdditionalHeaderSegment */
844 if (!(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) &&
845 !(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
846 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
847 " not set. Bad iSCSI Initiator.\n");
848 return iscsit_add_reject_cmd(cmd
,
849 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
852 if (((hdr
->flags
& ISCSI_FLAG_CMD_READ
) ||
853 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) && !hdr
->data_length
) {
855 * From RFC-3720 Section 10.3.1:
857 * "Either or both of R and W MAY be 1 when either the
858 * Expected Data Transfer Length and/or Bidirectional Read
859 * Expected Data Transfer Length are 0"
861 * For this case, go ahead and clear the unnecssary bits
862 * to avoid any confusion with ->data_direction.
864 hdr
->flags
&= ~ISCSI_FLAG_CMD_READ
;
865 hdr
->flags
&= ~ISCSI_FLAG_CMD_WRITE
;
867 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
868 " set when Expected Data Transfer Length is 0 for"
869 " CDB: 0x%02x, Fixing up flags\n", hdr
->cdb
[0]);
872 if (!(hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
873 !(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) && (hdr
->data_length
!= 0)) {
874 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
875 " MUST be set if Expected Data Transfer Length is not 0."
876 " Bad iSCSI Initiator\n");
877 return iscsit_add_reject_cmd(cmd
,
878 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
881 if ((hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
882 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) {
883 pr_err("Bidirectional operations not supported!\n");
884 return iscsit_add_reject_cmd(cmd
,
885 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
888 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
889 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
890 " Scsi Command PDU.\n");
891 return iscsit_add_reject_cmd(cmd
,
892 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
895 if (payload_length
&& !conn
->sess
->sess_ops
->ImmediateData
) {
896 pr_err("ImmediateData=No but DataSegmentLength=%u,"
897 " protocol error.\n", payload_length
);
898 return iscsit_add_reject_cmd(cmd
,
899 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
902 if ((be32_to_cpu(hdr
->data_length
) == payload_length
) &&
903 (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
))) {
904 pr_err("Expected Data Transfer Length and Length of"
905 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
906 " bit is not set protocol error\n");
907 return iscsit_add_reject_cmd(cmd
,
908 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
911 if (payload_length
> be32_to_cpu(hdr
->data_length
)) {
912 pr_err("DataSegmentLength: %u is greater than"
913 " EDTL: %u, protocol error.\n", payload_length
,
915 return iscsit_add_reject_cmd(cmd
,
916 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
919 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
920 pr_err("DataSegmentLength: %u is greater than"
921 " MaxXmitDataSegmentLength: %u, protocol error.\n",
922 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
923 return iscsit_add_reject_cmd(cmd
,
924 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
927 if (payload_length
> conn
->sess
->sess_ops
->FirstBurstLength
) {
928 pr_err("DataSegmentLength: %u is greater than"
929 " FirstBurstLength: %u, protocol error.\n",
930 payload_length
, conn
->sess
->sess_ops
->FirstBurstLength
);
931 return iscsit_add_reject_cmd(cmd
,
932 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
935 data_direction
= (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) ? DMA_TO_DEVICE
:
936 (hdr
->flags
& ISCSI_FLAG_CMD_READ
) ? DMA_FROM_DEVICE
:
939 cmd
->data_direction
= data_direction
;
940 iscsi_task_attr
= hdr
->flags
& ISCSI_FLAG_CMD_ATTR_MASK
;
942 * Figure out the SAM Task Attribute for the incoming SCSI CDB
944 if ((iscsi_task_attr
== ISCSI_ATTR_UNTAGGED
) ||
945 (iscsi_task_attr
== ISCSI_ATTR_SIMPLE
))
946 sam_task_attr
= TCM_SIMPLE_TAG
;
947 else if (iscsi_task_attr
== ISCSI_ATTR_ORDERED
)
948 sam_task_attr
= TCM_ORDERED_TAG
;
949 else if (iscsi_task_attr
== ISCSI_ATTR_HEAD_OF_QUEUE
)
950 sam_task_attr
= TCM_HEAD_TAG
;
951 else if (iscsi_task_attr
== ISCSI_ATTR_ACA
)
952 sam_task_attr
= TCM_ACA_TAG
;
954 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
955 " TCM_SIMPLE_TAG\n", iscsi_task_attr
);
956 sam_task_attr
= TCM_SIMPLE_TAG
;
959 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_CMD
;
960 cmd
->i_state
= ISTATE_NEW_CMD
;
961 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
962 cmd
->immediate_data
= (payload_length
) ? 1 : 0;
963 cmd
->unsolicited_data
= ((!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) &&
964 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) ? 1 : 0);
965 if (cmd
->unsolicited_data
)
966 cmd
->cmd_flags
|= ICF_NON_IMMEDIATE_UNSOLICITED_DATA
;
968 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
969 if (hdr
->flags
& ISCSI_FLAG_CMD_READ
)
970 cmd
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
972 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
973 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
974 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
975 cmd
->first_burst_len
= payload_length
;
977 if (!conn
->sess
->sess_ops
->RDMAExtensions
&&
978 cmd
->data_direction
== DMA_FROM_DEVICE
) {
979 struct iscsi_datain_req
*dr
;
981 dr
= iscsit_allocate_datain_req();
983 return iscsit_add_reject_cmd(cmd
,
984 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
986 iscsit_attach_datain_req(cmd
, dr
);
990 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
992 transport_init_se_cmd(&cmd
->se_cmd
, &iscsi_ops
,
993 conn
->sess
->se_sess
, be32_to_cpu(hdr
->data_length
),
994 cmd
->data_direction
, sam_task_attr
,
995 cmd
->sense_buffer
+ 2);
997 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
998 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr
->itt
,
999 hdr
->cmdsn
, be32_to_cpu(hdr
->data_length
), payload_length
,
1002 target_get_sess_cmd(&cmd
->se_cmd
, true);
1004 cmd
->sense_reason
= transport_lookup_cmd_lun(&cmd
->se_cmd
,
1005 scsilun_to_int(&hdr
->lun
));
1006 if (cmd
->sense_reason
)
1009 cmd
->sense_reason
= target_setup_cmd_from_cdb(&cmd
->se_cmd
, hdr
->cdb
);
1010 if (cmd
->sense_reason
) {
1011 if (cmd
->sense_reason
== TCM_OUT_OF_RESOURCES
) {
1012 return iscsit_add_reject_cmd(cmd
,
1013 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1019 if (iscsit_build_pdu_and_seq_lists(cmd
, payload_length
) < 0) {
1020 return iscsit_add_reject_cmd(cmd
,
1021 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1025 spin_lock_bh(&conn
->cmd_lock
);
1026 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1027 spin_unlock_bh(&conn
->cmd_lock
);
1029 * Check if we need to delay processing because of ALUA
1030 * Active/NonOptimized primary access state..
1032 core_alua_check_nonop_delay(&cmd
->se_cmd
);
1036 EXPORT_SYMBOL(iscsit_setup_scsi_cmd
);
1038 void iscsit_set_unsoliticed_dataout(struct iscsi_cmd
*cmd
)
1040 iscsit_set_dataout_sequence_values(cmd
);
1042 spin_lock_bh(&cmd
->dataout_timeout_lock
);
1043 iscsit_start_dataout_timer(cmd
, cmd
->conn
);
1044 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
1046 EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout
);
1048 int iscsit_process_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1049 struct iscsi_scsi_req
*hdr
)
1053 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1054 * the Immediate Bit is not set, and no Immediate
1057 * A PDU/CmdSN carrying Immediate Data can only
1058 * be processed after the DataCRC has passed.
1059 * If the DataCRC fails, the CmdSN MUST NOT
1060 * be acknowledged. (See below)
1062 if (!cmd
->immediate_data
) {
1063 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1064 (unsigned char *)hdr
, hdr
->cmdsn
);
1065 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1067 else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1068 target_put_sess_cmd(&cmd
->se_cmd
);
1073 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1076 * If no Immediate Data is attached, it's OK to return now.
1078 if (!cmd
->immediate_data
) {
1079 if (!cmd
->sense_reason
&& cmd
->unsolicited_data
)
1080 iscsit_set_unsoliticed_dataout(cmd
);
1081 if (!cmd
->sense_reason
)
1084 target_put_sess_cmd(&cmd
->se_cmd
);
1089 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1090 * execution. These exceptions are processed in CmdSN order using
1091 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1093 if (cmd
->sense_reason
) {
1094 if (cmd
->reject_reason
)
1100 * Call directly into transport_generic_new_cmd() to perform
1101 * the backend memory allocation.
1103 cmd
->sense_reason
= transport_generic_new_cmd(&cmd
->se_cmd
);
1104 if (cmd
->sense_reason
)
1109 EXPORT_SYMBOL(iscsit_process_scsi_cmd
);
1112 iscsit_get_immediate_data(struct iscsi_cmd
*cmd
, struct iscsi_scsi_req
*hdr
,
1115 int cmdsn_ret
= 0, immed_ret
= IMMEDIATE_DATA_NORMAL_OPERATION
;
1117 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1120 goto after_immediate_data
;
1122 immed_ret
= iscsit_handle_immediate_data(cmd
, hdr
,
1123 cmd
->first_burst_len
);
1124 after_immediate_data
:
1125 if (immed_ret
== IMMEDIATE_DATA_NORMAL_OPERATION
) {
1127 * A PDU/CmdSN carrying Immediate Data passed
1128 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1129 * Immediate Bit is not set.
1131 cmdsn_ret
= iscsit_sequence_cmd(cmd
->conn
, cmd
,
1132 (unsigned char *)hdr
, hdr
->cmdsn
);
1133 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1136 if (cmd
->sense_reason
|| cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1139 rc
= iscsit_dump_data_payload(cmd
->conn
,
1140 cmd
->first_burst_len
, 1);
1141 target_put_sess_cmd(&cmd
->se_cmd
);
1143 } else if (cmd
->unsolicited_data
)
1144 iscsit_set_unsoliticed_dataout(cmd
);
1146 } else if (immed_ret
== IMMEDIATE_DATA_ERL1_CRC_FAILURE
) {
1148 * Immediate Data failed DataCRC and ERL>=1,
1149 * silently drop this PDU and let the initiator
1150 * plug the CmdSN gap.
1152 * FIXME: Send Unsolicited NOPIN with reserved
1153 * TTT here to help the initiator figure out
1154 * the missing CmdSN, although they should be
1155 * intelligent enough to determine the missing
1156 * CmdSN and issue a retry to plug the sequence.
1158 cmd
->i_state
= ISTATE_REMOVE
;
1159 iscsit_add_cmd_to_immediate_queue(cmd
, cmd
->conn
, cmd
->i_state
);
1160 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1167 iscsit_handle_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1170 struct iscsi_scsi_req
*hdr
= (struct iscsi_scsi_req
*)buf
;
1172 bool dump_payload
= false;
1174 rc
= iscsit_setup_scsi_cmd(conn
, cmd
, buf
);
1178 * Allocation iovecs needed for struct socket operations for
1179 * traditional iSCSI block I/O.
1181 if (iscsit_allocate_iovecs(cmd
) < 0) {
1182 return iscsit_reject_cmd(cmd
,
1183 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1185 immed_data
= cmd
->immediate_data
;
1187 rc
= iscsit_process_scsi_cmd(conn
, cmd
, hdr
);
1191 dump_payload
= true;
1196 return iscsit_get_immediate_data(cmd
, hdr
, dump_payload
);
1199 static u32
iscsit_do_crypto_hash_sg(
1200 struct hash_desc
*hash
,
1201 struct iscsi_cmd
*cmd
,
1209 struct scatterlist
*sg
;
1210 unsigned int page_off
;
1212 crypto_hash_init(hash
);
1214 sg
= cmd
->first_data_sg
;
1215 page_off
= cmd
->first_data_sg_off
;
1218 while (data_length
) {
1219 u32 cur_len
= min_t(u32
, data_length
, (sg
[i
].length
- page_off
));
1221 crypto_hash_update(hash
, &sg
[i
], cur_len
);
1223 data_length
-= cur_len
;
1229 struct scatterlist pad_sg
;
1231 sg_init_one(&pad_sg
, pad_bytes
, padding
);
1232 crypto_hash_update(hash
, &pad_sg
, padding
);
1234 crypto_hash_final(hash
, (u8
*) &data_crc
);
1239 static void iscsit_do_crypto_hash_buf(
1240 struct hash_desc
*hash
,
1247 struct scatterlist sg
;
1249 crypto_hash_init(hash
);
1251 sg_init_one(&sg
, buf
, payload_length
);
1252 crypto_hash_update(hash
, &sg
, payload_length
);
1255 sg_init_one(&sg
, pad_bytes
, padding
);
1256 crypto_hash_update(hash
, &sg
, padding
);
1258 crypto_hash_final(hash
, data_crc
);
1262 iscsit_check_dataout_hdr(struct iscsi_conn
*conn
, unsigned char *buf
,
1263 struct iscsi_cmd
**out_cmd
)
1265 struct iscsi_data
*hdr
= (struct iscsi_data
*)buf
;
1266 struct iscsi_cmd
*cmd
= NULL
;
1267 struct se_cmd
*se_cmd
;
1268 u32 payload_length
= ntoh24(hdr
->dlength
);
1271 if (!payload_length
) {
1272 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1277 atomic_long_add(payload_length
, &conn
->sess
->rx_data_octets
);
1279 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1280 pr_err("DataSegmentLength: %u is greater than"
1281 " MaxXmitDataSegmentLength: %u\n", payload_length
,
1282 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1283 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1287 cmd
= iscsit_find_cmd_from_itt_or_dump(conn
, hdr
->itt
,
1292 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1293 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1294 hdr
->itt
, hdr
->ttt
, hdr
->datasn
, ntohl(hdr
->offset
),
1295 payload_length
, conn
->cid
);
1297 if (cmd
->cmd_flags
& ICF_GOT_LAST_DATAOUT
) {
1298 pr_err("Command ITT: 0x%08x received DataOUT after"
1299 " last DataOUT received, dumping payload\n",
1300 cmd
->init_task_tag
);
1301 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1304 if (cmd
->data_direction
!= DMA_TO_DEVICE
) {
1305 pr_err("Command ITT: 0x%08x received DataOUT for a"
1306 " NON-WRITE command.\n", cmd
->init_task_tag
);
1307 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1309 se_cmd
= &cmd
->se_cmd
;
1310 iscsit_mod_dataout_timer(cmd
);
1312 if ((be32_to_cpu(hdr
->offset
) + payload_length
) > cmd
->se_cmd
.data_length
) {
1313 pr_err("DataOut Offset: %u, Length %u greater than"
1314 " iSCSI Command EDTL %u, protocol error.\n",
1315 hdr
->offset
, payload_length
, cmd
->se_cmd
.data_length
);
1316 return iscsit_reject_cmd(cmd
, ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1319 if (cmd
->unsolicited_data
) {
1320 int dump_unsolicited_data
= 0;
1322 if (conn
->sess
->sess_ops
->InitialR2T
) {
1323 pr_err("Received unexpected unsolicited data"
1324 " while InitialR2T=Yes, protocol error.\n");
1325 transport_send_check_condition_and_sense(&cmd
->se_cmd
,
1326 TCM_UNEXPECTED_UNSOLICITED_DATA
, 0);
1330 * Special case for dealing with Unsolicited DataOUT
1331 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1335 /* Something's amiss if we're not in WRITE_PENDING state... */
1336 WARN_ON(se_cmd
->t_state
!= TRANSPORT_WRITE_PENDING
);
1337 if (!(se_cmd
->se_cmd_flags
& SCF_SUPPORTED_SAM_OPCODE
))
1338 dump_unsolicited_data
= 1;
1340 if (dump_unsolicited_data
) {
1342 * Check if a delayed TASK_ABORTED status needs to
1343 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1344 * received with the unsolicitied data out.
1346 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1347 iscsit_stop_dataout_timer(cmd
);
1349 transport_check_aborted_status(se_cmd
,
1350 (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
));
1351 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1355 * For the normal solicited data path:
1357 * Check for a delayed TASK_ABORTED status and dump any
1358 * incoming data out payload if one exists. Also, when the
1359 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1360 * data out sequence, we decrement outstanding_r2ts. Once
1361 * outstanding_r2ts reaches zero, go ahead and send the delayed
1362 * TASK_ABORTED status.
1364 if (se_cmd
->transport_state
& CMD_T_ABORTED
) {
1365 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1366 if (--cmd
->outstanding_r2ts
< 1) {
1367 iscsit_stop_dataout_timer(cmd
);
1368 transport_check_aborted_status(
1372 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1376 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1377 * within-command recovery checks before receiving the payload.
1379 rc
= iscsit_check_pre_dataout(cmd
, buf
);
1380 if (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
)
1382 else if (rc
== DATAOUT_CANNOT_RECOVER
)
1388 EXPORT_SYMBOL(iscsit_check_dataout_hdr
);
1391 iscsit_get_dataout(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1392 struct iscsi_data
*hdr
)
1395 u32 checksum
, iov_count
= 0, padding
= 0, rx_got
= 0, rx_size
= 0;
1396 u32 payload_length
= ntoh24(hdr
->dlength
);
1397 int iov_ret
, data_crc_failed
= 0;
1399 rx_size
+= payload_length
;
1400 iov
= &cmd
->iov_data
[0];
1402 iov_ret
= iscsit_map_iovec(cmd
, iov
, be32_to_cpu(hdr
->offset
),
1407 iov_count
+= iov_ret
;
1409 padding
= ((-payload_length
) & 3);
1411 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
1412 iov
[iov_count
++].iov_len
= padding
;
1414 pr_debug("Receiving %u padding bytes.\n", padding
);
1417 if (conn
->conn_ops
->DataDigest
) {
1418 iov
[iov_count
].iov_base
= &checksum
;
1419 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
1420 rx_size
+= ISCSI_CRC_LEN
;
1423 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
1425 iscsit_unmap_iovec(cmd
);
1427 if (rx_got
!= rx_size
)
1430 if (conn
->conn_ops
->DataDigest
) {
1433 data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_rx_hash
, cmd
,
1434 be32_to_cpu(hdr
->offset
),
1435 payload_length
, padding
,
1438 if (checksum
!= data_crc
) {
1439 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1440 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1441 " does not match computed 0x%08x\n",
1442 hdr
->itt
, hdr
->offset
, payload_length
,
1443 hdr
->datasn
, checksum
, data_crc
);
1444 data_crc_failed
= 1;
1446 pr_debug("Got CRC32C DataDigest 0x%08x for"
1447 " %u bytes of Data Out\n", checksum
,
1452 return data_crc_failed
;
1456 iscsit_check_dataout_payload(struct iscsi_cmd
*cmd
, struct iscsi_data
*hdr
,
1457 bool data_crc_failed
)
1459 struct iscsi_conn
*conn
= cmd
->conn
;
1462 * Increment post receive data and CRC values or perform
1463 * within-command recovery.
1465 rc
= iscsit_check_post_dataout(cmd
, (unsigned char *)hdr
, data_crc_failed
);
1466 if ((rc
== DATAOUT_NORMAL
) || (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
))
1468 else if (rc
== DATAOUT_SEND_R2T
) {
1469 iscsit_set_dataout_sequence_values(cmd
);
1470 conn
->conn_transport
->iscsit_get_dataout(conn
, cmd
, false);
1471 } else if (rc
== DATAOUT_SEND_TO_TRANSPORT
) {
1473 * Handle extra special case for out of order
1474 * Unsolicited Data Out.
1476 spin_lock_bh(&cmd
->istate_lock
);
1477 ooo_cmdsn
= (cmd
->cmd_flags
& ICF_OOO_CMDSN
);
1478 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
1479 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
1480 spin_unlock_bh(&cmd
->istate_lock
);
1482 iscsit_stop_dataout_timer(cmd
);
1485 target_execute_cmd(&cmd
->se_cmd
);
1487 } else /* DATAOUT_CANNOT_RECOVER */
1492 EXPORT_SYMBOL(iscsit_check_dataout_payload
);
1494 static int iscsit_handle_data_out(struct iscsi_conn
*conn
, unsigned char *buf
)
1496 struct iscsi_cmd
*cmd
= NULL
;
1497 struct iscsi_data
*hdr
= (struct iscsi_data
*)buf
;
1499 bool data_crc_failed
= false;
1501 rc
= iscsit_check_dataout_hdr(conn
, buf
, &cmd
);
1507 rc
= iscsit_get_dataout(conn
, cmd
, hdr
);
1511 data_crc_failed
= true;
1513 return iscsit_check_dataout_payload(cmd
, hdr
, data_crc_failed
);
1516 int iscsit_setup_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1517 struct iscsi_nopout
*hdr
)
1519 u32 payload_length
= ntoh24(hdr
->dlength
);
1521 if (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
1522 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1524 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1525 (unsigned char *)hdr
);
1527 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1528 (unsigned char *)hdr
);
1531 if (hdr
->itt
== RESERVED_ITT
&& !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1532 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1533 " not set, protocol error.\n");
1535 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1536 (unsigned char *)hdr
);
1538 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1539 (unsigned char *)hdr
);
1542 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1543 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1544 " greater than MaxXmitDataSegmentLength: %u, protocol"
1545 " error.\n", payload_length
,
1546 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1548 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1549 (unsigned char *)hdr
);
1551 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1552 (unsigned char *)hdr
);
1555 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1556 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1557 hdr
->itt
== RESERVED_ITT
? "Response" : "Request",
1558 hdr
->itt
, hdr
->ttt
, hdr
->cmdsn
, hdr
->exp_statsn
,
1561 * This is not a response to a Unsolicited NopIN, which means
1562 * it can either be a NOPOUT ping request (with a valid ITT),
1563 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1564 * Either way, make sure we allocate an struct iscsi_cmd, as both
1565 * can contain ping data.
1567 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1568 cmd
->iscsi_opcode
= ISCSI_OP_NOOP_OUT
;
1569 cmd
->i_state
= ISTATE_SEND_NOPIN
;
1570 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ?
1572 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1573 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1574 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1575 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1576 cmd
->data_direction
= DMA_NONE
;
1581 EXPORT_SYMBOL(iscsit_setup_nop_out
);
1583 int iscsit_process_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1584 struct iscsi_nopout
*hdr
)
1586 struct iscsi_cmd
*cmd_p
= NULL
;
1589 * Initiator is expecting a NopIN ping reply..
1591 if (hdr
->itt
!= RESERVED_ITT
) {
1593 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1594 (unsigned char *)hdr
);
1596 spin_lock_bh(&conn
->cmd_lock
);
1597 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1598 spin_unlock_bh(&conn
->cmd_lock
);
1600 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1602 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
1603 iscsit_add_cmd_to_response_queue(cmd
, conn
,
1608 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1609 (unsigned char *)hdr
, hdr
->cmdsn
);
1610 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
1612 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1618 * This was a response to a unsolicited NOPIN ping.
1620 if (hdr
->ttt
!= cpu_to_be32(0xFFFFFFFF)) {
1621 cmd_p
= iscsit_find_cmd_from_ttt(conn
, be32_to_cpu(hdr
->ttt
));
1625 iscsit_stop_nopin_response_timer(conn
);
1627 cmd_p
->i_state
= ISTATE_REMOVE
;
1628 iscsit_add_cmd_to_immediate_queue(cmd_p
, conn
, cmd_p
->i_state
);
1630 iscsit_start_nopin_timer(conn
);
1634 * Otherwise, initiator is not expecting a NOPIN is response.
1635 * Just ignore for now.
1639 EXPORT_SYMBOL(iscsit_process_nop_out
);
1641 static int iscsit_handle_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1644 unsigned char *ping_data
= NULL
;
1645 struct iscsi_nopout
*hdr
= (struct iscsi_nopout
*)buf
;
1646 struct kvec
*iov
= NULL
;
1647 u32 payload_length
= ntoh24(hdr
->dlength
);
1650 ret
= iscsit_setup_nop_out(conn
, cmd
, hdr
);
1654 * Handle NOP-OUT payload for traditional iSCSI sockets
1656 if (payload_length
&& hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1657 u32 checksum
, data_crc
, padding
= 0;
1658 int niov
= 0, rx_got
, rx_size
= payload_length
;
1660 ping_data
= kzalloc(payload_length
+ 1, GFP_KERNEL
);
1662 pr_err("Unable to allocate memory for"
1663 " NOPOUT ping data.\n");
1668 iov
= &cmd
->iov_misc
[0];
1669 iov
[niov
].iov_base
= ping_data
;
1670 iov
[niov
++].iov_len
= payload_length
;
1672 padding
= ((-payload_length
) & 3);
1674 pr_debug("Receiving %u additional bytes"
1675 " for padding.\n", padding
);
1676 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
1677 iov
[niov
++].iov_len
= padding
;
1680 if (conn
->conn_ops
->DataDigest
) {
1681 iov
[niov
].iov_base
= &checksum
;
1682 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
1683 rx_size
+= ISCSI_CRC_LEN
;
1686 rx_got
= rx_data(conn
, &cmd
->iov_misc
[0], niov
, rx_size
);
1687 if (rx_got
!= rx_size
) {
1692 if (conn
->conn_ops
->DataDigest
) {
1693 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
1694 ping_data
, payload_length
,
1695 padding
, cmd
->pad_bytes
,
1698 if (checksum
!= data_crc
) {
1699 pr_err("Ping data CRC32C DataDigest"
1700 " 0x%08x does not match computed 0x%08x\n",
1701 checksum
, data_crc
);
1702 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
1703 pr_err("Unable to recover from"
1704 " NOPOUT Ping DataCRC failure while in"
1710 * Silently drop this PDU and let the
1711 * initiator plug the CmdSN gap.
1713 pr_debug("Dropping NOPOUT"
1714 " Command CmdSN: 0x%08x due to"
1715 " DataCRC error.\n", hdr
->cmdsn
);
1720 pr_debug("Got CRC32C DataDigest"
1721 " 0x%08x for %u bytes of ping data.\n",
1722 checksum
, payload_length
);
1726 ping_data
[payload_length
] = '\0';
1728 * Attach ping data to struct iscsi_cmd->buf_ptr.
1730 cmd
->buf_ptr
= ping_data
;
1731 cmd
->buf_ptr_size
= payload_length
;
1733 pr_debug("Got %u bytes of NOPOUT ping"
1734 " data.\n", payload_length
);
1735 pr_debug("Ping Data: \"%s\"\n", ping_data
);
1738 return iscsit_process_nop_out(conn
, cmd
, hdr
);
1741 iscsit_free_cmd(cmd
, false);
1748 iscsit_handle_task_mgt_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1751 struct se_tmr_req
*se_tmr
;
1752 struct iscsi_tmr_req
*tmr_req
;
1753 struct iscsi_tm
*hdr
;
1754 int out_of_order_cmdsn
= 0, ret
;
1755 bool sess_ref
= false;
1758 hdr
= (struct iscsi_tm
*) buf
;
1759 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
1760 function
= hdr
->flags
;
1762 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1763 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1764 " 0x%08x, CID: %hu\n", hdr
->itt
, hdr
->cmdsn
, function
,
1765 hdr
->rtt
, hdr
->refcmdsn
, conn
->cid
);
1767 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1768 ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1769 hdr
->rtt
!= RESERVED_ITT
)) {
1770 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1771 hdr
->rtt
= RESERVED_ITT
;
1774 if ((function
== ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1775 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1776 pr_err("Task Management Request TASK_REASSIGN not"
1777 " issued as immediate command, bad iSCSI Initiator"
1778 "implementation\n");
1779 return iscsit_add_reject_cmd(cmd
,
1780 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1782 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1783 be32_to_cpu(hdr
->refcmdsn
) != ISCSI_RESERVED_TAG
)
1784 hdr
->refcmdsn
= cpu_to_be32(ISCSI_RESERVED_TAG
);
1786 cmd
->data_direction
= DMA_NONE
;
1788 cmd
->tmr_req
= kzalloc(sizeof(struct iscsi_tmr_req
), GFP_KERNEL
);
1789 if (!cmd
->tmr_req
) {
1790 pr_err("Unable to allocate memory for"
1791 " Task Management command!\n");
1792 return iscsit_add_reject_cmd(cmd
,
1793 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1798 * TASK_REASSIGN for ERL=2 / connection stays inside of
1799 * LIO-Target $FABRIC_MOD
1801 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
1806 transport_init_se_cmd(&cmd
->se_cmd
, &iscsi_ops
,
1807 conn
->sess
->se_sess
, 0, DMA_NONE
,
1808 TCM_SIMPLE_TAG
, cmd
->sense_buffer
+ 2);
1810 target_get_sess_cmd(&cmd
->se_cmd
, true);
1814 case ISCSI_TM_FUNC_ABORT_TASK
:
1815 tcm_function
= TMR_ABORT_TASK
;
1817 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
1818 tcm_function
= TMR_ABORT_TASK_SET
;
1820 case ISCSI_TM_FUNC_CLEAR_ACA
:
1821 tcm_function
= TMR_CLEAR_ACA
;
1823 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
1824 tcm_function
= TMR_CLEAR_TASK_SET
;
1826 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
1827 tcm_function
= TMR_LUN_RESET
;
1829 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
1830 tcm_function
= TMR_TARGET_WARM_RESET
;
1832 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
1833 tcm_function
= TMR_TARGET_COLD_RESET
;
1836 pr_err("Unknown iSCSI TMR Function:"
1837 " 0x%02x\n", function
);
1838 return iscsit_add_reject_cmd(cmd
,
1839 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1842 ret
= core_tmr_alloc_req(&cmd
->se_cmd
, cmd
->tmr_req
,
1843 tcm_function
, GFP_KERNEL
);
1845 return iscsit_add_reject_cmd(cmd
,
1846 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1848 cmd
->tmr_req
->se_tmr_req
= cmd
->se_cmd
.se_tmr_req
;
1851 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_TMFUNC
;
1852 cmd
->i_state
= ISTATE_SEND_TASKMGTRSP
;
1853 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1854 cmd
->init_task_tag
= hdr
->itt
;
1855 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1856 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1857 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1858 se_tmr
= cmd
->se_cmd
.se_tmr_req
;
1859 tmr_req
= cmd
->tmr_req
;
1861 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1863 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
1864 ret
= transport_lookup_tmr_lun(&cmd
->se_cmd
,
1865 scsilun_to_int(&hdr
->lun
));
1867 se_tmr
->response
= ISCSI_TMF_RSP_NO_LUN
;
1873 case ISCSI_TM_FUNC_ABORT_TASK
:
1874 se_tmr
->response
= iscsit_tmr_abort_task(cmd
, buf
);
1875 if (se_tmr
->response
)
1878 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
1879 case ISCSI_TM_FUNC_CLEAR_ACA
:
1880 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
1881 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
1883 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
1884 if (iscsit_tmr_task_warm_reset(conn
, tmr_req
, buf
) < 0) {
1885 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
1889 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
1890 if (iscsit_tmr_task_cold_reset(conn
, tmr_req
, buf
) < 0) {
1891 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
1895 case ISCSI_TM_FUNC_TASK_REASSIGN
:
1896 se_tmr
->response
= iscsit_tmr_task_reassign(cmd
, buf
);
1898 * Perform sanity checks on the ExpDataSN only if the
1899 * TASK_REASSIGN was successful.
1901 if (se_tmr
->response
)
1904 if (iscsit_check_task_reassign_expdatasn(tmr_req
, conn
) < 0)
1905 return iscsit_add_reject_cmd(cmd
,
1906 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1909 pr_err("Unknown TMR function: 0x%02x, protocol"
1910 " error.\n", function
);
1911 se_tmr
->response
= ISCSI_TMF_RSP_NOT_SUPPORTED
;
1915 if ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1916 (se_tmr
->response
== ISCSI_TMF_RSP_COMPLETE
))
1917 se_tmr
->call_transport
= 1;
1919 spin_lock_bh(&conn
->cmd_lock
);
1920 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1921 spin_unlock_bh(&conn
->cmd_lock
);
1923 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1924 int cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
1925 if (cmdsn_ret
== CMDSN_HIGHER_THAN_EXP
)
1926 out_of_order_cmdsn
= 1;
1927 else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
1929 else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1932 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1934 if (out_of_order_cmdsn
|| !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
))
1937 * Found the referenced task, send to transport for processing.
1939 if (se_tmr
->call_transport
)
1940 return transport_generic_handle_tmr(&cmd
->se_cmd
);
1943 * Could not find the referenced LUN, task, or Task Management
1944 * command not authorized or supported. Change state and
1945 * let the tx_thread send the response.
1947 * For connection recovery, this is also the default action for
1948 * TMR TASK_REASSIGN.
1951 pr_debug("Handle TMR, using sess_ref=true check\n");
1952 target_put_sess_cmd(&cmd
->se_cmd
);
1955 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
1958 EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd
);
1960 /* #warning FIXME: Support Text Command parameters besides SendTargets */
1962 iscsit_setup_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1963 struct iscsi_text
*hdr
)
1965 u32 payload_length
= ntoh24(hdr
->dlength
);
1967 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1968 pr_err("Unable to accept text parameter length: %u"
1969 "greater than MaxXmitDataSegmentLength %u.\n",
1970 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
1971 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1972 (unsigned char *)hdr
);
1975 if (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) ||
1976 (hdr
->flags
& ISCSI_FLAG_TEXT_CONTINUE
)) {
1977 pr_err("Multi sequence text commands currently not supported\n");
1978 return iscsit_reject_cmd(cmd
, ISCSI_REASON_CMD_NOT_SUPPORTED
,
1979 (unsigned char *)hdr
);
1982 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1983 " ExpStatSN: 0x%08x, Length: %u\n", hdr
->itt
, hdr
->cmdsn
,
1984 hdr
->exp_statsn
, payload_length
);
1986 cmd
->iscsi_opcode
= ISCSI_OP_TEXT
;
1987 cmd
->i_state
= ISTATE_SEND_TEXTRSP
;
1988 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1989 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1990 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1991 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1992 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1993 cmd
->data_direction
= DMA_NONE
;
1994 cmd
->text_in_ptr
= NULL
;
1998 EXPORT_SYMBOL(iscsit_setup_text_cmd
);
2001 iscsit_process_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2002 struct iscsi_text
*hdr
)
2004 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
;
2008 cmd
->targ_xfer_tag
= be32_to_cpu(hdr
->ttt
);
2009 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF) {
2010 pr_err("Unable to locate text_in buffer for sendtargets"
2014 goto empty_sendtargets
;
2016 if (strncmp("SendTargets", text_in
, 11) != 0) {
2017 pr_err("Received Text Data that is not"
2018 " SendTargets, cannot continue.\n");
2021 text_ptr
= strchr(text_in
, '=');
2023 pr_err("No \"=\" separator found in Text Data,"
2024 " cannot continue.\n");
2027 if (!strncmp("=All", text_ptr
, 4)) {
2028 cmd
->cmd_flags
|= ICF_SENDTARGETS_ALL
;
2029 } else if (!strncmp("=iqn.", text_ptr
, 5) ||
2030 !strncmp("=eui.", text_ptr
, 5)) {
2031 cmd
->cmd_flags
|= ICF_SENDTARGETS_SINGLE
;
2033 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr
);
2037 spin_lock_bh(&conn
->cmd_lock
);
2038 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2039 spin_unlock_bh(&conn
->cmd_lock
);
2042 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2044 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
2045 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
2046 (unsigned char *)hdr
, hdr
->cmdsn
);
2047 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2053 return iscsit_execute_cmd(cmd
, 0);
2056 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
2057 (unsigned char *)hdr
);
2059 EXPORT_SYMBOL(iscsit_process_text_cmd
);
2062 iscsit_handle_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2065 struct iscsi_text
*hdr
= (struct iscsi_text
*)buf
;
2066 char *text_in
= NULL
;
2067 u32 payload_length
= ntoh24(hdr
->dlength
);
2070 rc
= iscsit_setup_text_cmd(conn
, cmd
, hdr
);
2074 rx_size
= payload_length
;
2075 if (payload_length
) {
2076 u32 checksum
= 0, data_crc
= 0;
2077 u32 padding
= 0, pad_bytes
= 0;
2078 int niov
= 0, rx_got
;
2081 text_in
= kzalloc(payload_length
, GFP_KERNEL
);
2083 pr_err("Unable to allocate memory for"
2084 " incoming text parameters\n");
2087 cmd
->text_in_ptr
= text_in
;
2089 memset(iov
, 0, 3 * sizeof(struct kvec
));
2090 iov
[niov
].iov_base
= text_in
;
2091 iov
[niov
++].iov_len
= payload_length
;
2093 padding
= ((-payload_length
) & 3);
2095 iov
[niov
].iov_base
= &pad_bytes
;
2096 iov
[niov
++].iov_len
= padding
;
2098 pr_debug("Receiving %u additional bytes"
2099 " for padding.\n", padding
);
2101 if (conn
->conn_ops
->DataDigest
) {
2102 iov
[niov
].iov_base
= &checksum
;
2103 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
2104 rx_size
+= ISCSI_CRC_LEN
;
2107 rx_got
= rx_data(conn
, &iov
[0], niov
, rx_size
);
2108 if (rx_got
!= rx_size
)
2111 if (conn
->conn_ops
->DataDigest
) {
2112 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
2113 text_in
, payload_length
,
2114 padding
, (u8
*)&pad_bytes
,
2117 if (checksum
!= data_crc
) {
2118 pr_err("Text data CRC32C DataDigest"
2119 " 0x%08x does not match computed"
2120 " 0x%08x\n", checksum
, data_crc
);
2121 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2122 pr_err("Unable to recover from"
2123 " Text Data digest failure while in"
2128 * Silently drop this PDU and let the
2129 * initiator plug the CmdSN gap.
2131 pr_debug("Dropping Text"
2132 " Command CmdSN: 0x%08x due to"
2133 " DataCRC error.\n", hdr
->cmdsn
);
2138 pr_debug("Got CRC32C DataDigest"
2139 " 0x%08x for %u bytes of text data.\n",
2140 checksum
, payload_length
);
2143 text_in
[payload_length
- 1] = '\0';
2144 pr_debug("Successfully read %d bytes of text"
2145 " data.\n", payload_length
);
2148 return iscsit_process_text_cmd(conn
, cmd
, hdr
);
2151 kfree(cmd
->text_in_ptr
);
2152 cmd
->text_in_ptr
= NULL
;
2153 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
, buf
);
2156 int iscsit_logout_closesession(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2158 struct iscsi_conn
*conn_p
;
2159 struct iscsi_session
*sess
= conn
->sess
;
2161 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2162 " for SID: %u.\n", conn
->cid
, conn
->sess
->sid
);
2164 atomic_set(&sess
->session_logout
, 1);
2165 atomic_set(&conn
->conn_logout_remove
, 1);
2166 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_SESSION
;
2168 iscsit_inc_conn_usage_count(conn
);
2169 iscsit_inc_session_usage_count(sess
);
2171 spin_lock_bh(&sess
->conn_lock
);
2172 list_for_each_entry(conn_p
, &sess
->sess_conn_list
, conn_list
) {
2173 if (conn_p
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
)
2176 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2177 conn_p
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2179 spin_unlock_bh(&sess
->conn_lock
);
2181 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2186 int iscsit_logout_closeconnection(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2188 struct iscsi_conn
*l_conn
;
2189 struct iscsi_session
*sess
= conn
->sess
;
2191 pr_debug("Received logout request CLOSECONNECTION for CID:"
2192 " %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2195 * A Logout Request with a CLOSECONNECTION reason code for a CID
2196 * can arrive on a connection with a differing CID.
2198 if (conn
->cid
== cmd
->logout_cid
) {
2199 spin_lock_bh(&conn
->state_lock
);
2200 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2201 conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2203 atomic_set(&conn
->conn_logout_remove
, 1);
2204 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
;
2205 iscsit_inc_conn_usage_count(conn
);
2207 spin_unlock_bh(&conn
->state_lock
);
2210 * Handle all different cid CLOSECONNECTION requests in
2211 * iscsit_logout_post_handler_diffcid() as to give enough
2212 * time for any non immediate command's CmdSN to be
2213 * acknowledged on the connection in question.
2215 * Here we simply make sure the CID is still around.
2217 l_conn
= iscsit_get_conn_from_cid(sess
,
2220 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2221 iscsit_add_cmd_to_response_queue(cmd
, conn
,
2226 iscsit_dec_conn_usage_count(l_conn
);
2229 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2234 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2236 struct iscsi_session
*sess
= conn
->sess
;
2238 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2239 " CID: %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2241 if (sess
->sess_ops
->ErrorRecoveryLevel
!= 2) {
2242 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2243 " while ERL!=2.\n");
2244 cmd
->logout_response
= ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
;
2245 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2249 if (conn
->cid
== cmd
->logout_cid
) {
2250 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2251 " with CID: %hu on CID: %hu, implementation error.\n",
2252 cmd
->logout_cid
, conn
->cid
);
2253 cmd
->logout_response
= ISCSI_LOGOUT_CLEANUP_FAILED
;
2254 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2258 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2264 iscsit_handle_logout_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2267 int cmdsn_ret
, logout_remove
= 0;
2269 struct iscsi_logout
*hdr
;
2270 struct iscsi_tiqn
*tiqn
= iscsit_snmp_get_tiqn(conn
);
2272 hdr
= (struct iscsi_logout
*) buf
;
2273 reason_code
= (hdr
->flags
& 0x7f);
2276 spin_lock(&tiqn
->logout_stats
.lock
);
2277 if (reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
)
2278 tiqn
->logout_stats
.normal_logouts
++;
2280 tiqn
->logout_stats
.abnormal_logouts
++;
2281 spin_unlock(&tiqn
->logout_stats
.lock
);
2284 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2285 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2286 hdr
->itt
, hdr
->cmdsn
, hdr
->exp_statsn
, reason_code
,
2287 hdr
->cid
, conn
->cid
);
2289 if (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
) {
2290 pr_err("Received logout request on connection that"
2291 " is not in logged in state, ignoring request.\n");
2292 iscsit_free_cmd(cmd
, false);
2296 cmd
->iscsi_opcode
= ISCSI_OP_LOGOUT
;
2297 cmd
->i_state
= ISTATE_SEND_LOGOUTRSP
;
2298 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2299 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
2300 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2301 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
2302 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
2303 cmd
->logout_cid
= be16_to_cpu(hdr
->cid
);
2304 cmd
->logout_reason
= reason_code
;
2305 cmd
->data_direction
= DMA_NONE
;
2308 * We need to sleep in these cases (by returning 1) until the Logout
2309 * Response gets sent in the tx thread.
2311 if ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) ||
2312 ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
) &&
2313 be16_to_cpu(hdr
->cid
) == conn
->cid
))
2316 spin_lock_bh(&conn
->cmd_lock
);
2317 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2318 spin_unlock_bh(&conn
->cmd_lock
);
2320 if (reason_code
!= ISCSI_LOGOUT_REASON_RECOVERY
)
2321 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2324 * Immediate commands are executed, well, immediately.
2325 * Non-Immediate Logout Commands are executed in CmdSN order.
2327 if (cmd
->immediate_cmd
) {
2328 int ret
= iscsit_execute_cmd(cmd
, 0);
2333 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
2334 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
2336 else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2340 return logout_remove
;
2342 EXPORT_SYMBOL(iscsit_handle_logout_cmd
);
2344 static int iscsit_handle_snack(
2345 struct iscsi_conn
*conn
,
2348 struct iscsi_snack
*hdr
;
2350 hdr
= (struct iscsi_snack
*) buf
;
2351 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
2353 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2354 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2355 " CID: %hu\n", hdr
->itt
, hdr
->exp_statsn
, hdr
->flags
,
2356 hdr
->begrun
, hdr
->runlength
, conn
->cid
);
2358 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2359 pr_err("Initiator sent SNACK request while in"
2360 " ErrorRecoveryLevel=0.\n");
2361 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2365 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2366 * call from inside iscsi_send_recovery_datain_or_r2t().
2368 switch (hdr
->flags
& ISCSI_FLAG_SNACK_TYPE_MASK
) {
2370 return iscsit_handle_recovery_datain_or_r2t(conn
, buf
,
2372 be32_to_cpu(hdr
->ttt
),
2373 be32_to_cpu(hdr
->begrun
),
2374 be32_to_cpu(hdr
->runlength
));
2375 case ISCSI_FLAG_SNACK_TYPE_STATUS
:
2376 return iscsit_handle_status_snack(conn
, hdr
->itt
,
2377 be32_to_cpu(hdr
->ttt
),
2378 be32_to_cpu(hdr
->begrun
), be32_to_cpu(hdr
->runlength
));
2379 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK
:
2380 return iscsit_handle_data_ack(conn
, be32_to_cpu(hdr
->ttt
),
2381 be32_to_cpu(hdr
->begrun
),
2382 be32_to_cpu(hdr
->runlength
));
2383 case ISCSI_FLAG_SNACK_TYPE_RDATA
:
2384 /* FIXME: Support R-Data SNACK */
2385 pr_err("R-Data SNACK Not Supported.\n");
2386 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2389 pr_err("Unknown SNACK type 0x%02x, protocol"
2390 " error.\n", hdr
->flags
& 0x0f);
2391 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2398 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2400 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2401 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2402 wait_for_completion_interruptible_timeout(
2403 &conn
->rx_half_close_comp
,
2404 ISCSI_RX_THREAD_TCP_TIMEOUT
* HZ
);
2408 static int iscsit_handle_immediate_data(
2409 struct iscsi_cmd
*cmd
,
2410 struct iscsi_scsi_req
*hdr
,
2413 int iov_ret
, rx_got
= 0, rx_size
= 0;
2414 u32 checksum
, iov_count
= 0, padding
= 0;
2415 struct iscsi_conn
*conn
= cmd
->conn
;
2418 iov_ret
= iscsit_map_iovec(cmd
, cmd
->iov_data
, cmd
->write_data_done
, length
);
2420 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2423 iov_count
= iov_ret
;
2424 iov
= &cmd
->iov_data
[0];
2426 padding
= ((-length
) & 3);
2428 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2429 iov
[iov_count
++].iov_len
= padding
;
2433 if (conn
->conn_ops
->DataDigest
) {
2434 iov
[iov_count
].iov_base
= &checksum
;
2435 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2436 rx_size
+= ISCSI_CRC_LEN
;
2439 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
2441 iscsit_unmap_iovec(cmd
);
2443 if (rx_got
!= rx_size
) {
2444 iscsit_rx_thread_wait_for_tcp(conn
);
2445 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2448 if (conn
->conn_ops
->DataDigest
) {
2451 data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_rx_hash
, cmd
,
2452 cmd
->write_data_done
, length
, padding
,
2455 if (checksum
!= data_crc
) {
2456 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2457 " does not match computed 0x%08x\n", checksum
,
2460 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2461 pr_err("Unable to recover from"
2462 " Immediate Data digest failure while"
2464 iscsit_reject_cmd(cmd
,
2465 ISCSI_REASON_DATA_DIGEST_ERROR
,
2466 (unsigned char *)hdr
);
2467 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2469 iscsit_reject_cmd(cmd
,
2470 ISCSI_REASON_DATA_DIGEST_ERROR
,
2471 (unsigned char *)hdr
);
2472 return IMMEDIATE_DATA_ERL1_CRC_FAILURE
;
2475 pr_debug("Got CRC32C DataDigest 0x%08x for"
2476 " %u bytes of Immediate Data\n", checksum
,
2481 cmd
->write_data_done
+= length
;
2483 if (cmd
->write_data_done
== cmd
->se_cmd
.data_length
) {
2484 spin_lock_bh(&cmd
->istate_lock
);
2485 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
2486 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
2487 spin_unlock_bh(&cmd
->istate_lock
);
2490 return IMMEDIATE_DATA_NORMAL_OPERATION
;
2494 * Called with sess->conn_lock held.
2496 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2497 with active network interface */
2498 static void iscsit_build_conn_drop_async_message(struct iscsi_conn
*conn
)
2500 struct iscsi_cmd
*cmd
;
2501 struct iscsi_conn
*conn_p
;
2505 * Only send a Asynchronous Message on connections whos network
2506 * interface is still functional.
2508 list_for_each_entry(conn_p
, &conn
->sess
->sess_conn_list
, conn_list
) {
2509 if (conn_p
->conn_state
== TARG_CONN_STATE_LOGGED_IN
) {
2510 iscsit_inc_conn_usage_count(conn_p
);
2519 cmd
= iscsit_allocate_cmd(conn_p
, TASK_RUNNING
);
2521 iscsit_dec_conn_usage_count(conn_p
);
2525 cmd
->logout_cid
= conn
->cid
;
2526 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2527 cmd
->i_state
= ISTATE_SEND_ASYNCMSG
;
2529 spin_lock_bh(&conn_p
->cmd_lock
);
2530 list_add_tail(&cmd
->i_conn_node
, &conn_p
->conn_cmd_list
);
2531 spin_unlock_bh(&conn_p
->cmd_lock
);
2533 iscsit_add_cmd_to_response_queue(cmd
, conn_p
, cmd
->i_state
);
2534 iscsit_dec_conn_usage_count(conn_p
);
2537 static int iscsit_send_conn_drop_async_message(
2538 struct iscsi_cmd
*cmd
,
2539 struct iscsi_conn
*conn
)
2541 struct iscsi_async
*hdr
;
2543 cmd
->tx_size
= ISCSI_HDR_LEN
;
2544 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2546 hdr
= (struct iscsi_async
*) cmd
->pdu
;
2547 hdr
->opcode
= ISCSI_OP_ASYNC_EVENT
;
2548 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
2549 cmd
->init_task_tag
= RESERVED_ITT
;
2550 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2551 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->rsvd4
[0]);
2552 cmd
->stat_sn
= conn
->stat_sn
++;
2553 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2554 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2555 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2556 hdr
->async_event
= ISCSI_ASYNC_MSG_DROPPING_CONNECTION
;
2557 hdr
->param1
= cpu_to_be16(cmd
->logout_cid
);
2558 hdr
->param2
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Wait
);
2559 hdr
->param3
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Retain
);
2561 if (conn
->conn_ops
->HeaderDigest
) {
2562 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2564 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
2565 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2567 cmd
->tx_size
+= ISCSI_CRC_LEN
;
2568 pr_debug("Attaching CRC32C HeaderDigest to"
2569 " Async Message 0x%08x\n", *header_digest
);
2572 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2573 cmd
->iov_misc
[0].iov_len
= cmd
->tx_size
;
2574 cmd
->iov_misc_count
= 1;
2576 pr_debug("Sending Connection Dropped Async Message StatSN:"
2577 " 0x%08x, for CID: %hu on CID: %hu\n", cmd
->stat_sn
,
2578 cmd
->logout_cid
, conn
->cid
);
2582 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2584 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2585 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2586 wait_for_completion_interruptible_timeout(
2587 &conn
->tx_half_close_comp
,
2588 ISCSI_TX_THREAD_TCP_TIMEOUT
* HZ
);
2593 iscsit_build_datain_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2594 struct iscsi_datain
*datain
, struct iscsi_data_rsp
*hdr
,
2597 hdr
->opcode
= ISCSI_OP_SCSI_DATA_IN
;
2598 hdr
->flags
= datain
->flags
;
2599 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
2600 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
2601 hdr
->flags
|= ISCSI_FLAG_DATA_OVERFLOW
;
2602 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2603 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
2604 hdr
->flags
|= ISCSI_FLAG_DATA_UNDERFLOW
;
2605 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2608 hton24(hdr
->dlength
, datain
->length
);
2609 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2610 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
2611 (struct scsi_lun
*)&hdr
->lun
);
2613 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2615 hdr
->itt
= cmd
->init_task_tag
;
2617 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2618 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2620 hdr
->ttt
= cpu_to_be32(0xFFFFFFFF);
2622 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2624 hdr
->statsn
= cpu_to_be32(0xFFFFFFFF);
2626 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2627 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2628 hdr
->datasn
= cpu_to_be32(datain
->data_sn
);
2629 hdr
->offset
= cpu_to_be32(datain
->offset
);
2631 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2632 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2633 cmd
->init_task_tag
, ntohl(hdr
->statsn
), ntohl(hdr
->datasn
),
2634 ntohl(hdr
->offset
), datain
->length
, conn
->cid
);
2637 static int iscsit_send_datain(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2639 struct iscsi_data_rsp
*hdr
= (struct iscsi_data_rsp
*)&cmd
->pdu
[0];
2640 struct iscsi_datain datain
;
2641 struct iscsi_datain_req
*dr
;
2643 u32 iov_count
= 0, tx_size
= 0;
2644 int eodr
= 0, ret
, iov_ret
;
2645 bool set_statsn
= false;
2647 memset(&datain
, 0, sizeof(struct iscsi_datain
));
2648 dr
= iscsit_get_datain_values(cmd
, &datain
);
2650 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2651 cmd
->init_task_tag
);
2655 * Be paranoid and double check the logic for now.
2657 if ((datain
.offset
+ datain
.length
) > cmd
->se_cmd
.data_length
) {
2658 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2659 " datain.length: %u exceeds cmd->data_length: %u\n",
2660 cmd
->init_task_tag
, datain
.offset
, datain
.length
,
2661 cmd
->se_cmd
.data_length
);
2665 atomic_long_add(datain
.length
, &conn
->sess
->tx_data_octets
);
2667 * Special case for successfully execution w/ both DATAIN
2670 if ((datain
.flags
& ISCSI_FLAG_DATA_STATUS
) &&
2671 (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
))
2672 datain
.flags
&= ~ISCSI_FLAG_DATA_STATUS
;
2674 if ((dr
->dr_complete
== DATAIN_COMPLETE_NORMAL
) ||
2675 (dr
->dr_complete
== DATAIN_COMPLETE_CONNECTION_RECOVERY
)) {
2676 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2677 cmd
->stat_sn
= conn
->stat_sn
++;
2679 } else if (dr
->dr_complete
==
2680 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY
)
2684 iscsit_build_datain_pdu(cmd
, conn
, &datain
, hdr
, set_statsn
);
2686 iov
= &cmd
->iov_data
[0];
2687 iov
[iov_count
].iov_base
= cmd
->pdu
;
2688 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
2689 tx_size
+= ISCSI_HDR_LEN
;
2691 if (conn
->conn_ops
->HeaderDigest
) {
2692 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2694 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, cmd
->pdu
,
2695 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2697 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2698 tx_size
+= ISCSI_CRC_LEN
;
2700 pr_debug("Attaching CRC32 HeaderDigest"
2701 " for DataIN PDU 0x%08x\n", *header_digest
);
2704 iov_ret
= iscsit_map_iovec(cmd
, &cmd
->iov_data
[1],
2705 datain
.offset
, datain
.length
);
2709 iov_count
+= iov_ret
;
2710 tx_size
+= datain
.length
;
2712 cmd
->padding
= ((-datain
.length
) & 3);
2714 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2715 iov
[iov_count
++].iov_len
= cmd
->padding
;
2716 tx_size
+= cmd
->padding
;
2718 pr_debug("Attaching %u padding bytes\n",
2721 if (conn
->conn_ops
->DataDigest
) {
2722 cmd
->data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_tx_hash
, cmd
,
2723 datain
.offset
, datain
.length
, cmd
->padding
, cmd
->pad_bytes
);
2725 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
2726 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2727 tx_size
+= ISCSI_CRC_LEN
;
2729 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2730 " 0x%08x\n", datain
.length
+cmd
->padding
, cmd
->data_crc
);
2733 cmd
->iov_data_count
= iov_count
;
2734 cmd
->tx_size
= tx_size
;
2736 /* sendpage is preferred but can't insert markers */
2737 if (!conn
->conn_ops
->IFMarker
)
2738 ret
= iscsit_fe_sendpage_sg(cmd
, conn
);
2740 ret
= iscsit_send_tx_data(cmd
, conn
, 0);
2742 iscsit_unmap_iovec(cmd
);
2745 iscsit_tx_thread_wait_for_tcp(conn
);
2749 if (dr
->dr_complete
) {
2750 eodr
= (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ?
2752 iscsit_free_datain_req(cmd
, dr
);
2759 iscsit_build_logout_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2760 struct iscsi_logout_rsp
*hdr
)
2762 struct iscsi_conn
*logout_conn
= NULL
;
2763 struct iscsi_conn_recovery
*cr
= NULL
;
2764 struct iscsi_session
*sess
= conn
->sess
;
2766 * The actual shutting down of Sessions and/or Connections
2767 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2768 * is done in scsi_logout_post_handler().
2770 switch (cmd
->logout_reason
) {
2771 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
2772 pr_debug("iSCSI session logout successful, setting"
2773 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2774 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2776 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
2777 if (cmd
->logout_response
== ISCSI_LOGOUT_CID_NOT_FOUND
)
2780 * For CLOSECONNECTION logout requests carrying
2781 * a matching logout CID -> local CID, the reference
2782 * for the local CID will have been incremented in
2783 * iscsi_logout_closeconnection().
2785 * For CLOSECONNECTION logout requests carrying
2786 * a different CID than the connection it arrived
2787 * on, the connection responding to cmd->logout_cid
2788 * is stopped in iscsit_logout_post_handler_diffcid().
2791 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2792 " successful.\n", cmd
->logout_cid
, conn
->cid
);
2793 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2795 case ISCSI_LOGOUT_REASON_RECOVERY
:
2796 if ((cmd
->logout_response
== ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
) ||
2797 (cmd
->logout_response
== ISCSI_LOGOUT_CLEANUP_FAILED
))
2800 * If the connection is still active from our point of view
2801 * force connection recovery to occur.
2803 logout_conn
= iscsit_get_conn_from_cid_rcfr(sess
,
2806 iscsit_connection_reinstatement_rcfr(logout_conn
);
2807 iscsit_dec_conn_usage_count(logout_conn
);
2810 cr
= iscsit_get_inactive_connection_recovery_entry(
2811 conn
->sess
, cmd
->logout_cid
);
2813 pr_err("Unable to locate CID: %hu for"
2814 " REMOVECONNFORRECOVERY Logout Request.\n",
2816 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2820 iscsit_discard_cr_cmds_by_expstatsn(cr
, cmd
->exp_stat_sn
);
2822 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2823 " for recovery for CID: %hu on CID: %hu successful.\n",
2824 cmd
->logout_cid
, conn
->cid
);
2825 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2828 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2829 cmd
->logout_reason
);
2833 hdr
->opcode
= ISCSI_OP_LOGOUT_RSP
;
2834 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2835 hdr
->response
= cmd
->logout_response
;
2836 hdr
->itt
= cmd
->init_task_tag
;
2837 cmd
->stat_sn
= conn
->stat_sn
++;
2838 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2840 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2841 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2842 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2844 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2845 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2846 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
,
2847 cmd
->logout_cid
, conn
->cid
);
2851 EXPORT_SYMBOL(iscsit_build_logout_rsp
);
2854 iscsit_send_logout(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2857 int niov
= 0, tx_size
, rc
;
2859 rc
= iscsit_build_logout_rsp(cmd
, conn
,
2860 (struct iscsi_logout_rsp
*)&cmd
->pdu
[0]);
2864 tx_size
= ISCSI_HDR_LEN
;
2865 iov
= &cmd
->iov_misc
[0];
2866 iov
[niov
].iov_base
= cmd
->pdu
;
2867 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
2869 if (conn
->conn_ops
->HeaderDigest
) {
2870 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2872 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, &cmd
->pdu
[0],
2873 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2875 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2876 tx_size
+= ISCSI_CRC_LEN
;
2877 pr_debug("Attaching CRC32C HeaderDigest to"
2878 " Logout Response 0x%08x\n", *header_digest
);
2880 cmd
->iov_misc_count
= niov
;
2881 cmd
->tx_size
= tx_size
;
2887 iscsit_build_nopin_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2888 struct iscsi_nopin
*hdr
, bool nopout_response
)
2890 hdr
->opcode
= ISCSI_OP_NOOP_IN
;
2891 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2892 hton24(hdr
->dlength
, cmd
->buf_ptr_size
);
2893 if (nopout_response
)
2894 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2895 hdr
->itt
= cmd
->init_task_tag
;
2896 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2897 cmd
->stat_sn
= (nopout_response
) ? conn
->stat_sn
++ :
2899 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2901 if (nopout_response
)
2902 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2904 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2905 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2907 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2908 " StatSN: 0x%08x, Length %u\n", (nopout_response
) ?
2909 "Solicitied" : "Unsolicitied", cmd
->init_task_tag
,
2910 cmd
->targ_xfer_tag
, cmd
->stat_sn
, cmd
->buf_ptr_size
);
2912 EXPORT_SYMBOL(iscsit_build_nopin_rsp
);
2915 * Unsolicited NOPIN, either requesting a response or not.
2917 static int iscsit_send_unsolicited_nopin(
2918 struct iscsi_cmd
*cmd
,
2919 struct iscsi_conn
*conn
,
2922 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
2923 int tx_size
= ISCSI_HDR_LEN
, ret
;
2925 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, false);
2927 if (conn
->conn_ops
->HeaderDigest
) {
2928 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2930 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
2931 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2933 tx_size
+= ISCSI_CRC_LEN
;
2934 pr_debug("Attaching CRC32C HeaderDigest to"
2935 " NopIN 0x%08x\n", *header_digest
);
2938 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2939 cmd
->iov_misc
[0].iov_len
= tx_size
;
2940 cmd
->iov_misc_count
= 1;
2941 cmd
->tx_size
= tx_size
;
2943 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2944 " 0x%08x CID: %hu\n", hdr
->ttt
, cmd
->stat_sn
, conn
->cid
);
2946 ret
= iscsit_send_tx_data(cmd
, conn
, 1);
2948 iscsit_tx_thread_wait_for_tcp(conn
);
2952 spin_lock_bh(&cmd
->istate_lock
);
2953 cmd
->i_state
= want_response
?
2954 ISTATE_SENT_NOPIN_WANT_RESPONSE
: ISTATE_SENT_STATUS
;
2955 spin_unlock_bh(&cmd
->istate_lock
);
2961 iscsit_send_nopin(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2963 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
2966 int niov
= 0, tx_size
;
2968 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, true);
2970 tx_size
= ISCSI_HDR_LEN
;
2971 iov
= &cmd
->iov_misc
[0];
2972 iov
[niov
].iov_base
= cmd
->pdu
;
2973 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
2975 if (conn
->conn_ops
->HeaderDigest
) {
2976 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2978 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
2979 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2981 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2982 tx_size
+= ISCSI_CRC_LEN
;
2983 pr_debug("Attaching CRC32C HeaderDigest"
2984 " to NopIn 0x%08x\n", *header_digest
);
2988 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2989 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2991 if (cmd
->buf_ptr_size
) {
2992 iov
[niov
].iov_base
= cmd
->buf_ptr
;
2993 iov
[niov
++].iov_len
= cmd
->buf_ptr_size
;
2994 tx_size
+= cmd
->buf_ptr_size
;
2996 pr_debug("Echoing back %u bytes of ping"
2997 " data.\n", cmd
->buf_ptr_size
);
2999 padding
= ((-cmd
->buf_ptr_size
) & 3);
3001 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
3002 iov
[niov
++].iov_len
= padding
;
3004 pr_debug("Attaching %u additional"
3005 " padding bytes.\n", padding
);
3007 if (conn
->conn_ops
->DataDigest
) {
3008 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3009 cmd
->buf_ptr
, cmd
->buf_ptr_size
,
3010 padding
, (u8
*)&cmd
->pad_bytes
,
3011 (u8
*)&cmd
->data_crc
);
3013 iov
[niov
].iov_base
= &cmd
->data_crc
;
3014 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
3015 tx_size
+= ISCSI_CRC_LEN
;
3016 pr_debug("Attached DataDigest for %u"
3017 " bytes of ping data, CRC 0x%08x\n",
3018 cmd
->buf_ptr_size
, cmd
->data_crc
);
3022 cmd
->iov_misc_count
= niov
;
3023 cmd
->tx_size
= tx_size
;
3028 static int iscsit_send_r2t(
3029 struct iscsi_cmd
*cmd
,
3030 struct iscsi_conn
*conn
)
3033 struct iscsi_r2t
*r2t
;
3034 struct iscsi_r2t_rsp
*hdr
;
3037 r2t
= iscsit_get_r2t_from_list(cmd
);
3041 hdr
= (struct iscsi_r2t_rsp
*) cmd
->pdu
;
3042 memset(hdr
, 0, ISCSI_HDR_LEN
);
3043 hdr
->opcode
= ISCSI_OP_R2T
;
3044 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3045 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
3046 (struct scsi_lun
*)&hdr
->lun
);
3047 hdr
->itt
= cmd
->init_task_tag
;
3048 r2t
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
3049 hdr
->ttt
= cpu_to_be32(r2t
->targ_xfer_tag
);
3050 hdr
->statsn
= cpu_to_be32(conn
->stat_sn
);
3051 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3052 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3053 hdr
->r2tsn
= cpu_to_be32(r2t
->r2t_sn
);
3054 hdr
->data_offset
= cpu_to_be32(r2t
->offset
);
3055 hdr
->data_length
= cpu_to_be32(r2t
->xfer_len
);
3057 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
3058 cmd
->iov_misc
[0].iov_len
= ISCSI_HDR_LEN
;
3059 tx_size
+= ISCSI_HDR_LEN
;
3061 if (conn
->conn_ops
->HeaderDigest
) {
3062 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3064 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3065 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3067 cmd
->iov_misc
[0].iov_len
+= ISCSI_CRC_LEN
;
3068 tx_size
+= ISCSI_CRC_LEN
;
3069 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3070 " PDU 0x%08x\n", *header_digest
);
3073 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3074 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3075 (!r2t
->recovery_r2t
) ? "" : "Recovery ", cmd
->init_task_tag
,
3076 r2t
->targ_xfer_tag
, ntohl(hdr
->statsn
), r2t
->r2t_sn
,
3077 r2t
->offset
, r2t
->xfer_len
, conn
->cid
);
3079 cmd
->iov_misc_count
= 1;
3080 cmd
->tx_size
= tx_size
;
3082 spin_lock_bh(&cmd
->r2t_lock
);
3084 spin_unlock_bh(&cmd
->r2t_lock
);
3086 ret
= iscsit_send_tx_data(cmd
, conn
, 1);
3088 iscsit_tx_thread_wait_for_tcp(conn
);
3092 spin_lock_bh(&cmd
->dataout_timeout_lock
);
3093 iscsit_start_dataout_timer(cmd
, conn
);
3094 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
3100 * @recovery: If called from iscsi_task_reassign_complete_write() for
3101 * connection recovery.
3103 int iscsit_build_r2ts_for_cmd(
3104 struct iscsi_conn
*conn
,
3105 struct iscsi_cmd
*cmd
,
3109 u32 offset
= 0, xfer_len
= 0;
3111 spin_lock_bh(&cmd
->r2t_lock
);
3112 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
) {
3113 spin_unlock_bh(&cmd
->r2t_lock
);
3117 if (conn
->sess
->sess_ops
->DataSequenceInOrder
&&
3119 cmd
->r2t_offset
= max(cmd
->r2t_offset
, cmd
->write_data_done
);
3121 while (cmd
->outstanding_r2ts
< conn
->sess
->sess_ops
->MaxOutstandingR2T
) {
3122 if (conn
->sess
->sess_ops
->DataSequenceInOrder
) {
3123 offset
= cmd
->r2t_offset
;
3125 if (first_r2t
&& recovery
) {
3126 int new_data_end
= offset
+
3127 conn
->sess
->sess_ops
->MaxBurstLength
-
3128 cmd
->next_burst_len
;
3130 if (new_data_end
> cmd
->se_cmd
.data_length
)
3131 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3134 conn
->sess
->sess_ops
->MaxBurstLength
-
3135 cmd
->next_burst_len
;
3137 int new_data_end
= offset
+
3138 conn
->sess
->sess_ops
->MaxBurstLength
;
3140 if (new_data_end
> cmd
->se_cmd
.data_length
)
3141 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3143 xfer_len
= conn
->sess
->sess_ops
->MaxBurstLength
;
3145 cmd
->r2t_offset
+= xfer_len
;
3147 if (cmd
->r2t_offset
== cmd
->se_cmd
.data_length
)
3148 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3150 struct iscsi_seq
*seq
;
3152 seq
= iscsit_get_seq_holder_for_r2t(cmd
);
3154 spin_unlock_bh(&cmd
->r2t_lock
);
3158 offset
= seq
->offset
;
3159 xfer_len
= seq
->xfer_len
;
3161 if (cmd
->seq_send_order
== cmd
->seq_count
)
3162 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3164 cmd
->outstanding_r2ts
++;
3167 if (iscsit_add_r2t_to_list(cmd
, offset
, xfer_len
, 0, 0) < 0) {
3168 spin_unlock_bh(&cmd
->r2t_lock
);
3172 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
)
3175 spin_unlock_bh(&cmd
->r2t_lock
);
3180 void iscsit_build_rsp_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3181 bool inc_stat_sn
, struct iscsi_scsi_rsp
*hdr
)
3184 cmd
->stat_sn
= conn
->stat_sn
++;
3186 atomic_long_inc(&conn
->sess
->rsp_pdus
);
3188 memset(hdr
, 0, ISCSI_HDR_LEN
);
3189 hdr
->opcode
= ISCSI_OP_SCSI_CMD_RSP
;
3190 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3191 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
3192 hdr
->flags
|= ISCSI_FLAG_CMD_OVERFLOW
;
3193 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3194 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
3195 hdr
->flags
|= ISCSI_FLAG_CMD_UNDERFLOW
;
3196 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3198 hdr
->response
= cmd
->iscsi_response
;
3199 hdr
->cmd_status
= cmd
->se_cmd
.scsi_status
;
3200 hdr
->itt
= cmd
->init_task_tag
;
3201 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3203 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3204 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3205 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3207 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3208 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3209 cmd
->init_task_tag
, cmd
->stat_sn
, cmd
->se_cmd
.scsi_status
,
3210 cmd
->se_cmd
.scsi_status
, conn
->cid
);
3212 EXPORT_SYMBOL(iscsit_build_rsp_pdu
);
3214 static int iscsit_send_response(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3216 struct iscsi_scsi_rsp
*hdr
= (struct iscsi_scsi_rsp
*)&cmd
->pdu
[0];
3218 u32 padding
= 0, tx_size
= 0;
3220 bool inc_stat_sn
= (cmd
->i_state
== ISTATE_SEND_STATUS
);
3222 iscsit_build_rsp_pdu(cmd
, conn
, inc_stat_sn
, hdr
);
3224 iov
= &cmd
->iov_misc
[0];
3225 iov
[iov_count
].iov_base
= cmd
->pdu
;
3226 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3227 tx_size
+= ISCSI_HDR_LEN
;
3230 * Attach SENSE DATA payload to iSCSI Response PDU
3232 if (cmd
->se_cmd
.sense_buffer
&&
3233 ((cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
3234 (cmd
->se_cmd
.se_cmd_flags
& SCF_EMULATED_TASK_SENSE
))) {
3235 put_unaligned_be16(cmd
->se_cmd
.scsi_sense_length
, cmd
->sense_buffer
);
3236 cmd
->se_cmd
.scsi_sense_length
+= sizeof (__be16
);
3238 padding
= -(cmd
->se_cmd
.scsi_sense_length
) & 3;
3239 hton24(hdr
->dlength
, (u32
)cmd
->se_cmd
.scsi_sense_length
);
3240 iov
[iov_count
].iov_base
= cmd
->sense_buffer
;
3241 iov
[iov_count
++].iov_len
=
3242 (cmd
->se_cmd
.scsi_sense_length
+ padding
);
3243 tx_size
+= cmd
->se_cmd
.scsi_sense_length
;
3246 memset(cmd
->sense_buffer
+
3247 cmd
->se_cmd
.scsi_sense_length
, 0, padding
);
3249 pr_debug("Adding %u bytes of padding to"
3250 " SENSE.\n", padding
);
3253 if (conn
->conn_ops
->DataDigest
) {
3254 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3256 (cmd
->se_cmd
.scsi_sense_length
+ padding
),
3257 0, NULL
, (u8
*)&cmd
->data_crc
);
3259 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3260 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3261 tx_size
+= ISCSI_CRC_LEN
;
3263 pr_debug("Attaching CRC32 DataDigest for"
3264 " SENSE, %u bytes CRC 0x%08x\n",
3265 (cmd
->se_cmd
.scsi_sense_length
+ padding
),
3269 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3271 cmd
->se_cmd
.scsi_sense_length
);
3274 if (conn
->conn_ops
->HeaderDigest
) {
3275 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3277 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, cmd
->pdu
,
3278 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3280 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3281 tx_size
+= ISCSI_CRC_LEN
;
3282 pr_debug("Attaching CRC32 HeaderDigest for Response"
3283 " PDU 0x%08x\n", *header_digest
);
3286 cmd
->iov_misc_count
= iov_count
;
3287 cmd
->tx_size
= tx_size
;
3292 static u8
iscsit_convert_tcm_tmr_rsp(struct se_tmr_req
*se_tmr
)
3294 switch (se_tmr
->response
) {
3295 case TMR_FUNCTION_COMPLETE
:
3296 return ISCSI_TMF_RSP_COMPLETE
;
3297 case TMR_TASK_DOES_NOT_EXIST
:
3298 return ISCSI_TMF_RSP_NO_TASK
;
3299 case TMR_LUN_DOES_NOT_EXIST
:
3300 return ISCSI_TMF_RSP_NO_LUN
;
3301 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
:
3302 return ISCSI_TMF_RSP_NOT_SUPPORTED
;
3303 case TMR_FUNCTION_REJECTED
:
3305 return ISCSI_TMF_RSP_REJECTED
;
3310 iscsit_build_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3311 struct iscsi_tm_rsp
*hdr
)
3313 struct se_tmr_req
*se_tmr
= cmd
->se_cmd
.se_tmr_req
;
3315 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC_RSP
;
3316 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
3317 hdr
->response
= iscsit_convert_tcm_tmr_rsp(se_tmr
);
3318 hdr
->itt
= cmd
->init_task_tag
;
3319 cmd
->stat_sn
= conn
->stat_sn
++;
3320 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3322 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3323 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3324 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3326 pr_debug("Built Task Management Response ITT: 0x%08x,"
3327 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3328 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
, conn
->cid
);
3330 EXPORT_SYMBOL(iscsit_build_task_mgt_rsp
);
3333 iscsit_send_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3335 struct iscsi_tm_rsp
*hdr
= (struct iscsi_tm_rsp
*)&cmd
->pdu
[0];
3338 iscsit_build_task_mgt_rsp(cmd
, conn
, hdr
);
3340 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
3341 cmd
->iov_misc
[0].iov_len
= ISCSI_HDR_LEN
;
3342 tx_size
+= ISCSI_HDR_LEN
;
3344 if (conn
->conn_ops
->HeaderDigest
) {
3345 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3347 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3348 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3350 cmd
->iov_misc
[0].iov_len
+= ISCSI_CRC_LEN
;
3351 tx_size
+= ISCSI_CRC_LEN
;
3352 pr_debug("Attaching CRC32 HeaderDigest for Task"
3353 " Mgmt Response PDU 0x%08x\n", *header_digest
);
3356 cmd
->iov_misc_count
= 1;
3357 cmd
->tx_size
= tx_size
;
3362 static bool iscsit_check_inaddr_any(struct iscsi_np
*np
)
3366 if (np
->np_sockaddr
.ss_family
== AF_INET6
) {
3367 const struct sockaddr_in6 sin6
= {
3368 .sin6_addr
= IN6ADDR_ANY_INIT
};
3369 struct sockaddr_in6
*sock_in6
=
3370 (struct sockaddr_in6
*)&np
->np_sockaddr
;
3372 if (!memcmp(sock_in6
->sin6_addr
.s6_addr
,
3373 sin6
.sin6_addr
.s6_addr
, 16))
3376 struct sockaddr_in
* sock_in
=
3377 (struct sockaddr_in
*)&np
->np_sockaddr
;
3379 if (sock_in
->sin_addr
.s_addr
== htonl(INADDR_ANY
))
3386 #define SENDTARGETS_BUF_LIMIT 32768U
3389 iscsit_build_sendtargets_response(struct iscsi_cmd
*cmd
,
3390 enum iscsit_transport_type network_transport
,
3391 int skip_bytes
, bool *completed
)
3393 char *payload
= NULL
;
3394 struct iscsi_conn
*conn
= cmd
->conn
;
3395 struct iscsi_portal_group
*tpg
;
3396 struct iscsi_tiqn
*tiqn
;
3397 struct iscsi_tpg_np
*tpg_np
;
3398 int buffer_len
, end_of_buf
= 0, len
= 0, payload_len
= 0;
3399 int target_name_printed
;
3400 unsigned char buf
[ISCSI_IQN_LEN
+12]; /* iqn + "TargetName=" + \0 */
3401 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
= NULL
;
3403 buffer_len
= min(conn
->conn_ops
->MaxRecvDataSegmentLength
,
3404 SENDTARGETS_BUF_LIMIT
);
3406 payload
= kzalloc(buffer_len
, GFP_KERNEL
);
3408 pr_err("Unable to allocate memory for sendtargets"
3413 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
3416 if (cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
) {
3417 text_ptr
= strchr(text_in
, '=');
3419 pr_err("Unable to locate '=' string in text_in:"
3425 * Skip over '=' character..
3430 spin_lock(&tiqn_lock
);
3431 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
3432 if ((cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
) &&
3433 strcmp(tiqn
->tiqn
, text_ptr
)) {
3437 target_name_printed
= 0;
3439 spin_lock(&tiqn
->tiqn_tpg_lock
);
3440 list_for_each_entry(tpg
, &tiqn
->tiqn_tpg_list
, tpg_list
) {
3442 /* If demo_mode_discovery=0 and generate_node_acls=0
3443 * (demo mode dislabed) do not return
3444 * TargetName+TargetAddress unless a NodeACL exists.
3447 if ((tpg
->tpg_attrib
.generate_node_acls
== 0) &&
3448 (tpg
->tpg_attrib
.demo_mode_discovery
== 0) &&
3449 (!core_tpg_get_initiator_node_acl(&tpg
->tpg_se_tpg
,
3450 cmd
->conn
->sess
->sess_ops
->InitiatorName
))) {
3454 spin_lock(&tpg
->tpg_state_lock
);
3455 if ((tpg
->tpg_state
== TPG_STATE_FREE
) ||
3456 (tpg
->tpg_state
== TPG_STATE_INACTIVE
)) {
3457 spin_unlock(&tpg
->tpg_state_lock
);
3460 spin_unlock(&tpg
->tpg_state_lock
);
3462 spin_lock(&tpg
->tpg_np_lock
);
3463 list_for_each_entry(tpg_np
, &tpg
->tpg_gnp_list
,
3465 struct iscsi_np
*np
= tpg_np
->tpg_np
;
3466 bool inaddr_any
= iscsit_check_inaddr_any(np
);
3468 if (np
->np_network_transport
!= network_transport
)
3471 if (!target_name_printed
) {
3472 len
= sprintf(buf
, "TargetName=%s",
3476 if ((len
+ payload_len
) > buffer_len
) {
3477 spin_unlock(&tpg
->tpg_np_lock
);
3478 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3483 if (skip_bytes
&& len
<= skip_bytes
) {
3486 memcpy(payload
+ payload_len
, buf
, len
);
3488 target_name_printed
= 1;
3489 if (len
> skip_bytes
)
3495 len
= sprintf(buf
, "TargetAddress="
3501 len
= sprintf(buf
, "TargetAddress="
3508 if ((len
+ payload_len
) > buffer_len
) {
3509 spin_unlock(&tpg
->tpg_np_lock
);
3510 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3515 if (skip_bytes
&& len
<= skip_bytes
) {
3518 memcpy(payload
+ payload_len
, buf
, len
);
3520 if (len
> skip_bytes
)
3524 spin_unlock(&tpg
->tpg_np_lock
);
3526 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3533 if (cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
)
3536 spin_unlock(&tiqn_lock
);
3538 cmd
->buf_ptr
= payload
;
3544 iscsit_build_text_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3545 struct iscsi_text_rsp
*hdr
,
3546 enum iscsit_transport_type network_transport
)
3548 int text_length
, padding
;
3549 bool completed
= true;
3551 text_length
= iscsit_build_sendtargets_response(cmd
, network_transport
,
3552 cmd
->read_data_done
,
3554 if (text_length
< 0)
3558 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3560 hdr
->flags
|= ISCSI_FLAG_TEXT_CONTINUE
;
3561 cmd
->read_data_done
+= text_length
;
3562 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF)
3563 cmd
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
3565 hdr
->opcode
= ISCSI_OP_TEXT_RSP
;
3566 padding
= ((-text_length
) & 3);
3567 hton24(hdr
->dlength
, text_length
);
3568 hdr
->itt
= cmd
->init_task_tag
;
3569 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
3570 cmd
->stat_sn
= conn
->stat_sn
++;
3571 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3573 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3575 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3576 * correctly increment MaxCmdSN for each response answering a
3577 * non immediate text request with a valid CmdSN.
3579 cmd
->maxcmdsn_inc
= 0;
3580 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3581 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3583 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3584 " Length: %u, CID: %hu F: %d C: %d\n", cmd
->init_task_tag
,
3585 cmd
->targ_xfer_tag
, cmd
->stat_sn
, text_length
, conn
->cid
,
3586 !!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
),
3587 !!(hdr
->flags
& ISCSI_FLAG_TEXT_CONTINUE
));
3589 return text_length
+ padding
;
3591 EXPORT_SYMBOL(iscsit_build_text_rsp
);
3593 static int iscsit_send_text_rsp(
3594 struct iscsi_cmd
*cmd
,
3595 struct iscsi_conn
*conn
)
3597 struct iscsi_text_rsp
*hdr
= (struct iscsi_text_rsp
*)cmd
->pdu
;
3600 int text_length
, iov_count
= 0, rc
;
3602 rc
= iscsit_build_text_rsp(cmd
, conn
, hdr
, ISCSI_TCP
);
3607 iov
= &cmd
->iov_misc
[0];
3608 iov
[iov_count
].iov_base
= cmd
->pdu
;
3609 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3610 iov
[iov_count
].iov_base
= cmd
->buf_ptr
;
3611 iov
[iov_count
++].iov_len
= text_length
;
3613 tx_size
+= (ISCSI_HDR_LEN
+ text_length
);
3615 if (conn
->conn_ops
->HeaderDigest
) {
3616 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3618 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3619 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3621 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3622 tx_size
+= ISCSI_CRC_LEN
;
3623 pr_debug("Attaching CRC32 HeaderDigest for"
3624 " Text Response PDU 0x%08x\n", *header_digest
);
3627 if (conn
->conn_ops
->DataDigest
) {
3628 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3629 cmd
->buf_ptr
, text_length
,
3630 0, NULL
, (u8
*)&cmd
->data_crc
);
3632 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3633 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3634 tx_size
+= ISCSI_CRC_LEN
;
3636 pr_debug("Attaching DataDigest for %u bytes of text"
3637 " data, CRC 0x%08x\n", text_length
,
3641 cmd
->iov_misc_count
= iov_count
;
3642 cmd
->tx_size
= tx_size
;
3648 iscsit_build_reject(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3649 struct iscsi_reject
*hdr
)
3651 hdr
->opcode
= ISCSI_OP_REJECT
;
3652 hdr
->reason
= cmd
->reject_reason
;
3653 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3654 hton24(hdr
->dlength
, ISCSI_HDR_LEN
);
3655 hdr
->ffffffff
= cpu_to_be32(0xffffffff);
3656 cmd
->stat_sn
= conn
->stat_sn
++;
3657 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3658 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3659 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3662 EXPORT_SYMBOL(iscsit_build_reject
);
3664 static int iscsit_send_reject(
3665 struct iscsi_cmd
*cmd
,
3666 struct iscsi_conn
*conn
)
3668 struct iscsi_reject
*hdr
= (struct iscsi_reject
*)&cmd
->pdu
[0];
3670 u32 iov_count
= 0, tx_size
;
3672 iscsit_build_reject(cmd
, conn
, hdr
);
3674 iov
= &cmd
->iov_misc
[0];
3675 iov
[iov_count
].iov_base
= cmd
->pdu
;
3676 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3677 iov
[iov_count
].iov_base
= cmd
->buf_ptr
;
3678 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3680 tx_size
= (ISCSI_HDR_LEN
+ ISCSI_HDR_LEN
);
3682 if (conn
->conn_ops
->HeaderDigest
) {
3683 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3685 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3686 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3688 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3689 tx_size
+= ISCSI_CRC_LEN
;
3690 pr_debug("Attaching CRC32 HeaderDigest for"
3691 " REJECT PDU 0x%08x\n", *header_digest
);
3694 if (conn
->conn_ops
->DataDigest
) {
3695 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, cmd
->buf_ptr
,
3696 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)&cmd
->data_crc
);
3698 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3699 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3700 tx_size
+= ISCSI_CRC_LEN
;
3701 pr_debug("Attaching CRC32 DataDigest for REJECT"
3702 " PDU 0x%08x\n", cmd
->data_crc
);
3705 cmd
->iov_misc_count
= iov_count
;
3706 cmd
->tx_size
= tx_size
;
3708 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3709 " CID: %hu\n", ntohl(hdr
->statsn
), hdr
->reason
, conn
->cid
);
3714 void iscsit_thread_get_cpumask(struct iscsi_conn
*conn
)
3718 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3719 * within iscsit_start_kthreads()
3721 * Here we use bitmap_id to determine which CPU that this
3722 * iSCSI connection's RX/TX threads will be scheduled to
3725 ord
= conn
->bitmap_id
% cpumask_weight(cpu_online_mask
);
3726 for_each_online_cpu(cpu
) {
3728 cpumask_set_cpu(cpu
, conn
->conn_cpumask
);
3733 * This should never be reached..
3736 cpumask_setall(conn
->conn_cpumask
);
3739 static inline void iscsit_thread_check_cpumask(
3740 struct iscsi_conn
*conn
,
3741 struct task_struct
*p
,
3745 * mode == 1 signals iscsi_target_tx_thread() usage.
3746 * mode == 0 signals iscsi_target_rx_thread() usage.
3749 if (!conn
->conn_tx_reset_cpumask
)
3751 conn
->conn_tx_reset_cpumask
= 0;
3753 if (!conn
->conn_rx_reset_cpumask
)
3755 conn
->conn_rx_reset_cpumask
= 0;
3758 * Update the CPU mask for this single kthread so that
3759 * both TX and RX kthreads are scheduled to run on the
3762 set_cpus_allowed_ptr(p
, conn
->conn_cpumask
);
3766 iscsit_immediate_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3771 case ISTATE_SEND_R2T
:
3772 ret
= iscsit_send_r2t(cmd
, conn
);
3777 spin_lock_bh(&conn
->cmd_lock
);
3778 list_del_init(&cmd
->i_conn_node
);
3779 spin_unlock_bh(&conn
->cmd_lock
);
3781 iscsit_free_cmd(cmd
, false);
3783 case ISTATE_SEND_NOPIN_WANT_RESPONSE
:
3784 iscsit_mod_nopin_response_timer(conn
);
3785 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 1);
3789 case ISTATE_SEND_NOPIN_NO_RESPONSE
:
3790 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 0);
3795 pr_err("Unknown Opcode: 0x%02x ITT:"
3796 " 0x%08x, i_state: %d on CID: %hu\n",
3797 cmd
->iscsi_opcode
, cmd
->init_task_tag
, state
,
3809 iscsit_handle_immediate_queue(struct iscsi_conn
*conn
)
3811 struct iscsit_transport
*t
= conn
->conn_transport
;
3812 struct iscsi_queue_req
*qr
;
3813 struct iscsi_cmd
*cmd
;
3817 while ((qr
= iscsit_get_cmd_from_immediate_queue(conn
))) {
3818 atomic_set(&conn
->check_immediate_queue
, 0);
3821 kmem_cache_free(lio_qr_cache
, qr
);
3823 ret
= t
->iscsit_immediate_queue(conn
, cmd
, state
);
3832 iscsit_response_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3838 case ISTATE_SEND_DATAIN
:
3839 ret
= iscsit_send_datain(cmd
, conn
);
3844 goto check_rsp_state
;
3845 else if (ret
== 1) {
3847 spin_lock_bh(&cmd
->istate_lock
);
3848 cmd
->i_state
= ISTATE_SENT_STATUS
;
3849 spin_unlock_bh(&cmd
->istate_lock
);
3851 if (atomic_read(&conn
->check_immediate_queue
))
3855 } else if (ret
== 2) {
3856 /* Still must send status,
3857 SCF_TRANSPORT_TASK_SENSE was set */
3858 spin_lock_bh(&cmd
->istate_lock
);
3859 cmd
->i_state
= ISTATE_SEND_STATUS
;
3860 spin_unlock_bh(&cmd
->istate_lock
);
3861 state
= ISTATE_SEND_STATUS
;
3862 goto check_rsp_state
;
3866 case ISTATE_SEND_STATUS
:
3867 case ISTATE_SEND_STATUS_RECOVERY
:
3868 ret
= iscsit_send_response(cmd
, conn
);
3870 case ISTATE_SEND_LOGOUTRSP
:
3871 ret
= iscsit_send_logout(cmd
, conn
);
3873 case ISTATE_SEND_ASYNCMSG
:
3874 ret
= iscsit_send_conn_drop_async_message(
3877 case ISTATE_SEND_NOPIN
:
3878 ret
= iscsit_send_nopin(cmd
, conn
);
3880 case ISTATE_SEND_REJECT
:
3881 ret
= iscsit_send_reject(cmd
, conn
);
3883 case ISTATE_SEND_TASKMGTRSP
:
3884 ret
= iscsit_send_task_mgt_rsp(cmd
, conn
);
3887 ret
= iscsit_tmr_post_handler(cmd
, conn
);
3889 iscsit_fall_back_to_erl0(conn
->sess
);
3891 case ISTATE_SEND_TEXTRSP
:
3892 ret
= iscsit_send_text_rsp(cmd
, conn
);
3895 pr_err("Unknown Opcode: 0x%02x ITT:"
3896 " 0x%08x, i_state: %d on CID: %hu\n",
3897 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3904 if (iscsit_send_tx_data(cmd
, conn
, 1) < 0) {
3905 iscsit_tx_thread_wait_for_tcp(conn
);
3906 iscsit_unmap_iovec(cmd
);
3909 iscsit_unmap_iovec(cmd
);
3912 case ISTATE_SEND_LOGOUTRSP
:
3913 if (!iscsit_logout_post_handler(cmd
, conn
))
3916 case ISTATE_SEND_STATUS
:
3917 case ISTATE_SEND_ASYNCMSG
:
3918 case ISTATE_SEND_NOPIN
:
3919 case ISTATE_SEND_STATUS_RECOVERY
:
3920 case ISTATE_SEND_TEXTRSP
:
3921 case ISTATE_SEND_TASKMGTRSP
:
3922 case ISTATE_SEND_REJECT
:
3923 spin_lock_bh(&cmd
->istate_lock
);
3924 cmd
->i_state
= ISTATE_SENT_STATUS
;
3925 spin_unlock_bh(&cmd
->istate_lock
);
3928 pr_err("Unknown Opcode: 0x%02x ITT:"
3929 " 0x%08x, i_state: %d on CID: %hu\n",
3930 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3931 cmd
->i_state
, conn
->cid
);
3935 if (atomic_read(&conn
->check_immediate_queue
))
3944 static int iscsit_handle_response_queue(struct iscsi_conn
*conn
)
3946 struct iscsit_transport
*t
= conn
->conn_transport
;
3947 struct iscsi_queue_req
*qr
;
3948 struct iscsi_cmd
*cmd
;
3952 while ((qr
= iscsit_get_cmd_from_response_queue(conn
))) {
3955 kmem_cache_free(lio_qr_cache
, qr
);
3957 ret
= t
->iscsit_response_queue(conn
, cmd
, state
);
3958 if (ret
== 1 || ret
< 0)
3965 int iscsi_target_tx_thread(void *arg
)
3968 struct iscsi_conn
*conn
= arg
;
3970 * Allow ourselves to be interrupted by SIGINT so that a
3971 * connection recovery / failure event can be triggered externally.
3973 allow_signal(SIGINT
);
3975 while (!kthread_should_stop()) {
3977 * Ensure that both TX and RX per connection kthreads
3978 * are scheduled to run on the same CPU.
3980 iscsit_thread_check_cpumask(conn
, current
, 1);
3982 wait_event_interruptible(conn
->queues_wq
,
3983 !iscsit_conn_all_queues_empty(conn
));
3985 if (signal_pending(current
))
3989 ret
= iscsit_handle_immediate_queue(conn
);
3993 ret
= iscsit_handle_response_queue(conn
);
3996 else if (ret
== -ECONNRESET
)
4004 * Avoid the normal connection failure code-path if this connection
4005 * is still within LOGIN mode, and iscsi_np process context is
4006 * responsible for cleaning up the early connection failure.
4008 if (conn
->conn_state
!= TARG_CONN_STATE_IN_LOGIN
)
4009 iscsit_take_action_for_connection_exit(conn
);
4014 static int iscsi_target_rx_opcode(struct iscsi_conn
*conn
, unsigned char *buf
)
4016 struct iscsi_hdr
*hdr
= (struct iscsi_hdr
*)buf
;
4017 struct iscsi_cmd
*cmd
;
4020 switch (hdr
->opcode
& ISCSI_OPCODE_MASK
) {
4021 case ISCSI_OP_SCSI_CMD
:
4022 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
4026 ret
= iscsit_handle_scsi_cmd(conn
, cmd
, buf
);
4028 case ISCSI_OP_SCSI_DATA_OUT
:
4029 ret
= iscsit_handle_data_out(conn
, buf
);
4031 case ISCSI_OP_NOOP_OUT
:
4033 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
4034 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
4038 ret
= iscsit_handle_nop_out(conn
, cmd
, buf
);
4040 case ISCSI_OP_SCSI_TMFUNC
:
4041 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
4045 ret
= iscsit_handle_task_mgt_cmd(conn
, cmd
, buf
);
4048 if (hdr
->ttt
!= cpu_to_be32(0xFFFFFFFF)) {
4049 cmd
= iscsit_find_cmd_from_itt(conn
, hdr
->itt
);
4053 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
4058 ret
= iscsit_handle_text_cmd(conn
, cmd
, buf
);
4060 case ISCSI_OP_LOGOUT
:
4061 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
4065 ret
= iscsit_handle_logout_cmd(conn
, cmd
, buf
);
4067 wait_for_completion_timeout(&conn
->conn_logout_comp
,
4068 SECONDS_FOR_LOGOUT_COMP
* HZ
);
4070 case ISCSI_OP_SNACK
:
4071 ret
= iscsit_handle_snack(conn
, buf
);
4074 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr
->opcode
);
4075 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
4076 pr_err("Cannot recover from unknown"
4077 " opcode while ERL=0, closing iSCSI connection.\n");
4080 if (!conn
->conn_ops
->OFMarker
) {
4081 pr_err("Unable to recover from unknown"
4082 " opcode while OFMarker=No, closing iSCSI"
4086 if (iscsit_recover_from_unknown_opcode(conn
) < 0) {
4087 pr_err("Unable to recover from unknown"
4088 " opcode, closing iSCSI connection.\n");
4096 return iscsit_add_reject(conn
, ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
4099 static bool iscsi_target_check_conn_state(struct iscsi_conn
*conn
)
4103 spin_lock_bh(&conn
->state_lock
);
4104 ret
= (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
);
4105 spin_unlock_bh(&conn
->state_lock
);
4110 int iscsi_target_rx_thread(void *arg
)
4113 u8 buffer
[ISCSI_HDR_LEN
], opcode
;
4114 u32 checksum
= 0, digest
= 0;
4115 struct iscsi_conn
*conn
= arg
;
4118 * Allow ourselves to be interrupted by SIGINT so that a
4119 * connection recovery / failure event can be triggered externally.
4121 allow_signal(SIGINT
);
4123 * Wait for iscsi_post_login_handler() to complete before allowing
4124 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4126 rc
= wait_for_completion_interruptible(&conn
->rx_login_comp
);
4127 if (rc
< 0 || iscsi_target_check_conn_state(conn
))
4130 if (conn
->conn_transport
->transport_type
== ISCSI_INFINIBAND
) {
4131 struct completion comp
;
4133 init_completion(&comp
);
4134 rc
= wait_for_completion_interruptible(&comp
);
4141 while (!kthread_should_stop()) {
4143 * Ensure that both TX and RX per connection kthreads
4144 * are scheduled to run on the same CPU.
4146 iscsit_thread_check_cpumask(conn
, current
, 0);
4148 memset(buffer
, 0, ISCSI_HDR_LEN
);
4149 memset(&iov
, 0, sizeof(struct kvec
));
4151 iov
.iov_base
= buffer
;
4152 iov
.iov_len
= ISCSI_HDR_LEN
;
4154 ret
= rx_data(conn
, &iov
, 1, ISCSI_HDR_LEN
);
4155 if (ret
!= ISCSI_HDR_LEN
) {
4156 iscsit_rx_thread_wait_for_tcp(conn
);
4160 if (conn
->conn_ops
->HeaderDigest
) {
4161 iov
.iov_base
= &digest
;
4162 iov
.iov_len
= ISCSI_CRC_LEN
;
4164 ret
= rx_data(conn
, &iov
, 1, ISCSI_CRC_LEN
);
4165 if (ret
!= ISCSI_CRC_LEN
) {
4166 iscsit_rx_thread_wait_for_tcp(conn
);
4170 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
4171 buffer
, ISCSI_HDR_LEN
,
4172 0, NULL
, (u8
*)&checksum
);
4174 if (digest
!= checksum
) {
4175 pr_err("HeaderDigest CRC32C failed,"
4176 " received 0x%08x, computed 0x%08x\n",
4179 * Set the PDU to 0xff so it will intentionally
4180 * hit default in the switch below.
4182 memset(buffer
, 0xff, ISCSI_HDR_LEN
);
4183 atomic_long_inc(&conn
->sess
->conn_digest_errors
);
4185 pr_debug("Got HeaderDigest CRC32C"
4186 " 0x%08x\n", checksum
);
4190 if (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
)
4193 opcode
= buffer
[0] & ISCSI_OPCODE_MASK
;
4195 if (conn
->sess
->sess_ops
->SessionType
&&
4196 ((!(opcode
& ISCSI_OP_TEXT
)) ||
4197 (!(opcode
& ISCSI_OP_LOGOUT
)))) {
4198 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4199 " while in Discovery Session, rejecting.\n", opcode
);
4200 iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
4205 ret
= iscsi_target_rx_opcode(conn
, buffer
);
4211 if (!signal_pending(current
))
4212 atomic_set(&conn
->transport_failed
, 1);
4213 iscsit_take_action_for_connection_exit(conn
);
4217 static void iscsit_release_commands_from_conn(struct iscsi_conn
*conn
)
4219 LIST_HEAD(tmp_list
);
4220 struct iscsi_cmd
*cmd
= NULL
, *cmd_tmp
= NULL
;
4221 struct iscsi_session
*sess
= conn
->sess
;
4223 * We expect this function to only ever be called from either RX or TX
4224 * thread context via iscsit_close_connection() once the other context
4225 * has been reset -> returned sleeping pre-handler state.
4227 spin_lock_bh(&conn
->cmd_lock
);
4228 list_splice_init(&conn
->conn_cmd_list
, &tmp_list
);
4230 list_for_each_entry(cmd
, &tmp_list
, i_conn_node
) {
4231 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
4233 if (se_cmd
->se_tfo
!= NULL
) {
4234 spin_lock(&se_cmd
->t_state_lock
);
4235 se_cmd
->transport_state
|= CMD_T_FABRIC_STOP
;
4236 spin_unlock(&se_cmd
->t_state_lock
);
4239 spin_unlock_bh(&conn
->cmd_lock
);
4241 list_for_each_entry_safe(cmd
, cmd_tmp
, &tmp_list
, i_conn_node
) {
4242 list_del_init(&cmd
->i_conn_node
);
4244 iscsit_increment_maxcmdsn(cmd
, sess
);
4245 iscsit_free_cmd(cmd
, true);
4250 static void iscsit_stop_timers_for_cmds(
4251 struct iscsi_conn
*conn
)
4253 struct iscsi_cmd
*cmd
;
4255 spin_lock_bh(&conn
->cmd_lock
);
4256 list_for_each_entry(cmd
, &conn
->conn_cmd_list
, i_conn_node
) {
4257 if (cmd
->data_direction
== DMA_TO_DEVICE
)
4258 iscsit_stop_dataout_timer(cmd
);
4260 spin_unlock_bh(&conn
->cmd_lock
);
4263 int iscsit_close_connection(
4264 struct iscsi_conn
*conn
)
4266 int conn_logout
= (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
);
4267 struct iscsi_session
*sess
= conn
->sess
;
4269 pr_debug("Closing iSCSI connection CID %hu on SID:"
4270 " %u\n", conn
->cid
, sess
->sid
);
4272 * Always up conn_logout_comp for the traditional TCP case just in case
4273 * the RX Thread in iscsi_target_rx_opcode() is sleeping and the logout
4274 * response never got sent because the connection failed.
4276 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4277 * to signal logout response TX interrupt completion. Go ahead and skip
4278 * this for iser since isert_rx_opcode() does not wait on logout failure,
4279 * and to avoid iscsi_conn pointer dereference in iser-target code.
4281 if (conn
->conn_transport
->transport_type
== ISCSI_TCP
)
4282 complete(&conn
->conn_logout_comp
);
4284 if (!strcmp(current
->comm
, ISCSI_RX_THREAD_NAME
)) {
4285 if (conn
->tx_thread
&&
4286 cmpxchg(&conn
->tx_thread_active
, true, false)) {
4287 send_sig(SIGINT
, conn
->tx_thread
, 1);
4288 kthread_stop(conn
->tx_thread
);
4290 } else if (!strcmp(current
->comm
, ISCSI_TX_THREAD_NAME
)) {
4291 if (conn
->rx_thread
&&
4292 cmpxchg(&conn
->rx_thread_active
, true, false)) {
4293 send_sig(SIGINT
, conn
->rx_thread
, 1);
4294 kthread_stop(conn
->rx_thread
);
4298 spin_lock(&iscsit_global
->ts_bitmap_lock
);
4299 bitmap_release_region(iscsit_global
->ts_bitmap
, conn
->bitmap_id
,
4301 spin_unlock(&iscsit_global
->ts_bitmap_lock
);
4303 iscsit_stop_timers_for_cmds(conn
);
4304 iscsit_stop_nopin_response_timer(conn
);
4305 iscsit_stop_nopin_timer(conn
);
4307 if (conn
->conn_transport
->iscsit_wait_conn
)
4308 conn
->conn_transport
->iscsit_wait_conn(conn
);
4311 * During Connection recovery drop unacknowledged out of order
4312 * commands for this connection, and prepare the other commands
4315 * During normal operation clear the out of order commands (but
4316 * do not free the struct iscsi_ooo_cmdsn's) and release all
4317 * struct iscsi_cmds.
4319 if (atomic_read(&conn
->connection_recovery
)) {
4320 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn
);
4321 iscsit_prepare_cmds_for_realligance(conn
);
4323 iscsit_clear_ooo_cmdsns_for_conn(conn
);
4324 iscsit_release_commands_from_conn(conn
);
4326 iscsit_free_queue_reqs_for_conn(conn
);
4329 * Handle decrementing session or connection usage count if
4330 * a logout response was not able to be sent because the
4331 * connection failed. Fall back to Session Recovery here.
4333 if (atomic_read(&conn
->conn_logout_remove
)) {
4334 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) {
4335 iscsit_dec_conn_usage_count(conn
);
4336 iscsit_dec_session_usage_count(sess
);
4338 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
)
4339 iscsit_dec_conn_usage_count(conn
);
4341 atomic_set(&conn
->conn_logout_remove
, 0);
4342 atomic_set(&sess
->session_reinstatement
, 0);
4343 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4346 spin_lock_bh(&sess
->conn_lock
);
4347 list_del(&conn
->conn_list
);
4350 * Attempt to let the Initiator know this connection failed by
4351 * sending an Connection Dropped Async Message on another
4352 * active connection.
4354 if (atomic_read(&conn
->connection_recovery
))
4355 iscsit_build_conn_drop_async_message(conn
);
4357 spin_unlock_bh(&sess
->conn_lock
);
4360 * If connection reinstatement is being performed on this connection,
4361 * up the connection reinstatement semaphore that is being blocked on
4362 * in iscsit_cause_connection_reinstatement().
4364 spin_lock_bh(&conn
->state_lock
);
4365 if (atomic_read(&conn
->sleep_on_conn_wait_comp
)) {
4366 spin_unlock_bh(&conn
->state_lock
);
4367 complete(&conn
->conn_wait_comp
);
4368 wait_for_completion(&conn
->conn_post_wait_comp
);
4369 spin_lock_bh(&conn
->state_lock
);
4373 * If connection reinstatement is being performed on this connection
4374 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4375 * connection wait rcfr semaphore that is being blocked on
4376 * an iscsit_connection_reinstatement_rcfr().
4378 if (atomic_read(&conn
->connection_wait_rcfr
)) {
4379 spin_unlock_bh(&conn
->state_lock
);
4380 complete(&conn
->conn_wait_rcfr_comp
);
4381 wait_for_completion(&conn
->conn_post_wait_comp
);
4382 spin_lock_bh(&conn
->state_lock
);
4384 atomic_set(&conn
->connection_reinstatement
, 1);
4385 spin_unlock_bh(&conn
->state_lock
);
4388 * If any other processes are accessing this connection pointer we
4389 * must wait until they have completed.
4391 iscsit_check_conn_usage_count(conn
);
4393 if (conn
->conn_rx_hash
.tfm
)
4394 crypto_free_hash(conn
->conn_rx_hash
.tfm
);
4395 if (conn
->conn_tx_hash
.tfm
)
4396 crypto_free_hash(conn
->conn_tx_hash
.tfm
);
4398 free_cpumask_var(conn
->conn_cpumask
);
4400 kfree(conn
->conn_ops
);
4401 conn
->conn_ops
= NULL
;
4404 sock_release(conn
->sock
);
4406 if (conn
->conn_transport
->iscsit_free_conn
)
4407 conn
->conn_transport
->iscsit_free_conn(conn
);
4409 iscsit_put_transport(conn
->conn_transport
);
4411 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4412 conn
->conn_state
= TARG_CONN_STATE_FREE
;
4415 spin_lock_bh(&sess
->conn_lock
);
4416 atomic_dec(&sess
->nconn
);
4417 pr_debug("Decremented iSCSI connection count to %hu from node:"
4418 " %s\n", atomic_read(&sess
->nconn
),
4419 sess
->sess_ops
->InitiatorName
);
4421 * Make sure that if one connection fails in an non ERL=2 iSCSI
4422 * Session that they all fail.
4424 if ((sess
->sess_ops
->ErrorRecoveryLevel
!= 2) && !conn_logout
&&
4425 !atomic_read(&sess
->session_logout
))
4426 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4429 * If this was not the last connection in the session, and we are
4430 * performing session reinstatement or falling back to ERL=0, call
4431 * iscsit_stop_session() without sleeping to shutdown the other
4432 * active connections.
4434 if (atomic_read(&sess
->nconn
)) {
4435 if (!atomic_read(&sess
->session_reinstatement
) &&
4436 !atomic_read(&sess
->session_fall_back_to_erl0
)) {
4437 spin_unlock_bh(&sess
->conn_lock
);
4440 if (!atomic_read(&sess
->session_stop_active
)) {
4441 atomic_set(&sess
->session_stop_active
, 1);
4442 spin_unlock_bh(&sess
->conn_lock
);
4443 iscsit_stop_session(sess
, 0, 0);
4446 spin_unlock_bh(&sess
->conn_lock
);
4451 * If this was the last connection in the session and one of the
4452 * following is occurring:
4454 * Session Reinstatement is not being performed, and are falling back
4455 * to ERL=0 call iscsit_close_session().
4457 * Session Logout was requested. iscsit_close_session() will be called
4460 * Session Continuation is not being performed, start the Time2Retain
4461 * handler and check if sleep_on_sess_wait_sem is active.
4463 if (!atomic_read(&sess
->session_reinstatement
) &&
4464 atomic_read(&sess
->session_fall_back_to_erl0
)) {
4465 spin_unlock_bh(&sess
->conn_lock
);
4466 target_put_session(sess
->se_sess
);
4469 } else if (atomic_read(&sess
->session_logout
)) {
4470 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4471 sess
->session_state
= TARG_SESS_STATE_FREE
;
4472 spin_unlock_bh(&sess
->conn_lock
);
4474 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4475 complete(&sess
->session_wait_comp
);
4479 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4480 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4482 if (!atomic_read(&sess
->session_continuation
)) {
4483 spin_unlock_bh(&sess
->conn_lock
);
4484 iscsit_start_time2retain_handler(sess
);
4486 spin_unlock_bh(&sess
->conn_lock
);
4488 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4489 complete(&sess
->session_wait_comp
);
4493 spin_unlock_bh(&sess
->conn_lock
);
4498 int iscsit_close_session(struct iscsi_session
*sess
)
4500 struct iscsi_portal_group
*tpg
= sess
->tpg
;
4501 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4503 if (atomic_read(&sess
->nconn
)) {
4504 pr_err("%d connection(s) still exist for iSCSI session"
4505 " to %s\n", atomic_read(&sess
->nconn
),
4506 sess
->sess_ops
->InitiatorName
);
4510 spin_lock_bh(&se_tpg
->session_lock
);
4511 atomic_set(&sess
->session_logout
, 1);
4512 atomic_set(&sess
->session_reinstatement
, 1);
4513 iscsit_stop_time2retain_timer(sess
);
4514 spin_unlock_bh(&se_tpg
->session_lock
);
4517 * transport_deregister_session_configfs() will clear the
4518 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4519 * can be setting it again with __transport_register_session() in
4520 * iscsi_post_login_handler() again after the iscsit_stop_session()
4521 * completes in iscsi_np context.
4523 transport_deregister_session_configfs(sess
->se_sess
);
4526 * If any other processes are accessing this session pointer we must
4527 * wait until they have completed. If we are in an interrupt (the
4528 * time2retain handler) and contain and active session usage count we
4529 * restart the timer and exit.
4531 if (!in_interrupt()) {
4532 if (iscsit_check_session_usage_count(sess
) == 1)
4533 iscsit_stop_session(sess
, 1, 1);
4535 if (iscsit_check_session_usage_count(sess
) == 2) {
4536 atomic_set(&sess
->session_logout
, 0);
4537 iscsit_start_time2retain_handler(sess
);
4542 transport_deregister_session(sess
->se_sess
);
4544 if (sess
->sess_ops
->ErrorRecoveryLevel
== 2)
4545 iscsit_free_connection_recovery_entires(sess
);
4547 iscsit_free_all_ooo_cmdsns(sess
);
4549 spin_lock_bh(&se_tpg
->session_lock
);
4550 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4551 sess
->session_state
= TARG_SESS_STATE_FREE
;
4552 pr_debug("Released iSCSI session from node: %s\n",
4553 sess
->sess_ops
->InitiatorName
);
4556 tpg
->tpg_tiqn
->tiqn_nsessions
--;
4558 pr_debug("Decremented number of active iSCSI Sessions on"
4559 " iSCSI TPG: %hu to %u\n", tpg
->tpgt
, tpg
->nsessions
);
4561 spin_lock(&sess_idr_lock
);
4562 idr_remove(&sess_idr
, sess
->session_index
);
4563 spin_unlock(&sess_idr_lock
);
4565 kfree(sess
->sess_ops
);
4566 sess
->sess_ops
= NULL
;
4567 spin_unlock_bh(&se_tpg
->session_lock
);
4573 static void iscsit_logout_post_handler_closesession(
4574 struct iscsi_conn
*conn
)
4576 struct iscsi_session
*sess
= conn
->sess
;
4579 * Traditional iscsi/tcp will invoke this logic from TX thread
4580 * context during session logout, so clear tx_thread_active and
4581 * sleep if iscsit_close_connection() has not already occured.
4583 * Since iser-target invokes this logic from it's own workqueue,
4584 * always sleep waiting for RX/TX thread shutdown to complete
4585 * within iscsit_close_connection().
4587 if (conn
->conn_transport
->transport_type
== ISCSI_TCP
)
4588 sleep
= cmpxchg(&conn
->tx_thread_active
, true, false);
4590 atomic_set(&conn
->conn_logout_remove
, 0);
4591 complete(&conn
->conn_logout_comp
);
4593 iscsit_dec_conn_usage_count(conn
);
4594 iscsit_stop_session(sess
, sleep
, sleep
);
4595 iscsit_dec_session_usage_count(sess
);
4596 target_put_session(sess
->se_sess
);
4599 static void iscsit_logout_post_handler_samecid(
4600 struct iscsi_conn
*conn
)
4604 if (conn
->conn_transport
->transport_type
== ISCSI_TCP
)
4605 sleep
= cmpxchg(&conn
->tx_thread_active
, true, false);
4607 atomic_set(&conn
->conn_logout_remove
, 0);
4608 complete(&conn
->conn_logout_comp
);
4610 iscsit_cause_connection_reinstatement(conn
, sleep
);
4611 iscsit_dec_conn_usage_count(conn
);
4614 static void iscsit_logout_post_handler_diffcid(
4615 struct iscsi_conn
*conn
,
4618 struct iscsi_conn
*l_conn
;
4619 struct iscsi_session
*sess
= conn
->sess
;
4620 bool conn_found
= false;
4625 spin_lock_bh(&sess
->conn_lock
);
4626 list_for_each_entry(l_conn
, &sess
->sess_conn_list
, conn_list
) {
4627 if (l_conn
->cid
== cid
) {
4628 iscsit_inc_conn_usage_count(l_conn
);
4633 spin_unlock_bh(&sess
->conn_lock
);
4639 l_conn
->sock
->ops
->shutdown(l_conn
->sock
, RCV_SHUTDOWN
);
4641 spin_lock_bh(&l_conn
->state_lock
);
4642 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4643 l_conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
4644 spin_unlock_bh(&l_conn
->state_lock
);
4646 iscsit_cause_connection_reinstatement(l_conn
, 1);
4647 iscsit_dec_conn_usage_count(l_conn
);
4651 * Return of 0 causes the TX thread to restart.
4653 int iscsit_logout_post_handler(
4654 struct iscsi_cmd
*cmd
,
4655 struct iscsi_conn
*conn
)
4659 switch (cmd
->logout_reason
) {
4660 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
4661 switch (cmd
->logout_response
) {
4662 case ISCSI_LOGOUT_SUCCESS
:
4663 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4665 iscsit_logout_post_handler_closesession(conn
);
4670 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
4671 if (conn
->cid
== cmd
->logout_cid
) {
4672 switch (cmd
->logout_response
) {
4673 case ISCSI_LOGOUT_SUCCESS
:
4674 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4676 iscsit_logout_post_handler_samecid(conn
);
4681 switch (cmd
->logout_response
) {
4682 case ISCSI_LOGOUT_SUCCESS
:
4683 iscsit_logout_post_handler_diffcid(conn
,
4686 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4687 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4694 case ISCSI_LOGOUT_REASON_RECOVERY
:
4695 switch (cmd
->logout_response
) {
4696 case ISCSI_LOGOUT_SUCCESS
:
4697 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4698 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
:
4699 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4711 EXPORT_SYMBOL(iscsit_logout_post_handler
);
4713 void iscsit_fail_session(struct iscsi_session
*sess
)
4715 struct iscsi_conn
*conn
;
4717 spin_lock_bh(&sess
->conn_lock
);
4718 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
) {
4719 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4720 conn
->conn_state
= TARG_CONN_STATE_CLEANUP_WAIT
;
4722 spin_unlock_bh(&sess
->conn_lock
);
4724 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4725 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4728 int iscsit_free_session(struct iscsi_session
*sess
)
4730 u16 conn_count
= atomic_read(&sess
->nconn
);
4731 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4734 spin_lock_bh(&sess
->conn_lock
);
4735 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4737 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4739 if (conn_count
== 0)
4742 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4745 iscsit_inc_conn_usage_count(conn_tmp
);
4748 iscsit_inc_conn_usage_count(conn
);
4750 spin_unlock_bh(&sess
->conn_lock
);
4751 iscsit_cause_connection_reinstatement(conn
, 1);
4752 spin_lock_bh(&sess
->conn_lock
);
4754 iscsit_dec_conn_usage_count(conn
);
4756 iscsit_dec_conn_usage_count(conn_tmp
);
4761 if (atomic_read(&sess
->nconn
)) {
4762 spin_unlock_bh(&sess
->conn_lock
);
4763 wait_for_completion(&sess
->session_wait_comp
);
4765 spin_unlock_bh(&sess
->conn_lock
);
4767 target_put_session(sess
->se_sess
);
4771 void iscsit_stop_session(
4772 struct iscsi_session
*sess
,
4774 int connection_sleep
)
4776 u16 conn_count
= atomic_read(&sess
->nconn
);
4777 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4780 spin_lock_bh(&sess
->conn_lock
);
4782 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4784 if (connection_sleep
) {
4785 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4787 if (conn_count
== 0)
4790 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4793 iscsit_inc_conn_usage_count(conn_tmp
);
4796 iscsit_inc_conn_usage_count(conn
);
4798 spin_unlock_bh(&sess
->conn_lock
);
4799 iscsit_cause_connection_reinstatement(conn
, 1);
4800 spin_lock_bh(&sess
->conn_lock
);
4802 iscsit_dec_conn_usage_count(conn
);
4804 iscsit_dec_conn_usage_count(conn_tmp
);
4808 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
)
4809 iscsit_cause_connection_reinstatement(conn
, 0);
4812 if (session_sleep
&& atomic_read(&sess
->nconn
)) {
4813 spin_unlock_bh(&sess
->conn_lock
);
4814 wait_for_completion(&sess
->session_wait_comp
);
4816 spin_unlock_bh(&sess
->conn_lock
);
4819 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group
*tpg
, int force
)
4821 struct iscsi_session
*sess
;
4822 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4823 struct se_session
*se_sess
, *se_sess_tmp
;
4824 LIST_HEAD(free_list
);
4825 int session_count
= 0;
4827 spin_lock_bh(&se_tpg
->session_lock
);
4828 if (tpg
->nsessions
&& !force
) {
4829 spin_unlock_bh(&se_tpg
->session_lock
);
4833 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &se_tpg
->tpg_sess_list
,
4835 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4837 spin_lock(&sess
->conn_lock
);
4838 if (atomic_read(&sess
->session_fall_back_to_erl0
) ||
4839 atomic_read(&sess
->session_logout
) ||
4840 (sess
->time2retain_timer_flags
& ISCSI_TF_EXPIRED
)) {
4841 spin_unlock(&sess
->conn_lock
);
4844 atomic_set(&sess
->session_reinstatement
, 1);
4845 spin_unlock(&sess
->conn_lock
);
4847 list_move_tail(&se_sess
->sess_list
, &free_list
);
4849 spin_unlock_bh(&se_tpg
->session_lock
);
4851 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &free_list
, sess_list
) {
4852 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4854 iscsit_free_session(sess
);
4858 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4859 " Group: %hu\n", session_count
, tpg
->tpgt
);
4863 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4864 MODULE_VERSION("4.1.x");
4865 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4866 MODULE_LICENSE("GPL");
4868 module_init(iscsi_target_init_module
);
4869 module_exit(iscsi_target_cleanup_module
);