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/vmalloc.h>
25 #include <linux/idr.h>
26 #include <asm/unaligned.h>
27 #include <scsi/scsi_proto.h>
28 #include <scsi/iscsi_proto.h>
29 #include <scsi/scsi_tcq.h>
30 #include <target/target_core_base.h>
31 #include <target/target_core_fabric.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 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
);
291 port_e
= ntohs(sock_in6_e
->sin6_port
);
293 sock_in
= (struct sockaddr_in
*)sockaddr
;
294 sock_in_e
= (struct sockaddr_in
*)&np
->np_sockaddr
;
296 if (sock_in
->sin_addr
.s_addr
== sock_in_e
->sin_addr
.s_addr
)
299 port
= ntohs(sock_in
->sin_port
);
300 port_e
= ntohs(sock_in_e
->sin_port
);
303 if (ip_match
&& (port_e
== port
) &&
304 (np
->np_network_transport
== network_transport
))
311 * Called with mutex np_lock held
313 static struct iscsi_np
*iscsit_get_np(
314 struct sockaddr_storage
*sockaddr
,
315 int network_transport
)
320 list_for_each_entry(np
, &g_np_list
, np_list
) {
321 spin_lock_bh(&np
->np_thread_lock
);
322 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
323 spin_unlock_bh(&np
->np_thread_lock
);
327 match
= iscsit_check_np_match(sockaddr
, np
, network_transport
);
330 * Increment the np_exports reference count now to
331 * prevent iscsit_del_np() below from being called
332 * while iscsi_tpg_add_network_portal() is called.
335 spin_unlock_bh(&np
->np_thread_lock
);
338 spin_unlock_bh(&np
->np_thread_lock
);
344 struct iscsi_np
*iscsit_add_np(
345 struct sockaddr_storage
*sockaddr
,
346 int network_transport
)
351 mutex_lock(&np_lock
);
354 * Locate the existing struct iscsi_np if already active..
356 np
= iscsit_get_np(sockaddr
, network_transport
);
358 mutex_unlock(&np_lock
);
362 np
= kzalloc(sizeof(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 np
->np_network_transport
= network_transport
;
371 spin_lock_init(&np
->np_thread_lock
);
372 init_completion(&np
->np_restart_comp
);
373 INIT_LIST_HEAD(&np
->np_list
);
375 ret
= iscsi_target_setup_login_socket(np
, sockaddr
);
378 mutex_unlock(&np_lock
);
382 np
->np_thread
= kthread_run(iscsi_target_login_thread
, np
, "iscsi_np");
383 if (IS_ERR(np
->np_thread
)) {
384 pr_err("Unable to create kthread: iscsi_np\n");
385 ret
= PTR_ERR(np
->np_thread
);
387 mutex_unlock(&np_lock
);
391 * Increment the np_exports reference count now to prevent
392 * iscsit_del_np() below from being run while a new call to
393 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
394 * active. We don't need to hold np->np_thread_lock at this
395 * point because iscsi_np has not been added to g_np_list yet.
398 np
->np_thread_state
= ISCSI_NP_THREAD_ACTIVE
;
400 list_add_tail(&np
->np_list
, &g_np_list
);
401 mutex_unlock(&np_lock
);
403 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
404 &np
->np_sockaddr
, np
->np_transport
->name
);
409 int iscsit_reset_np_thread(
411 struct iscsi_tpg_np
*tpg_np
,
412 struct iscsi_portal_group
*tpg
,
415 spin_lock_bh(&np
->np_thread_lock
);
416 if (np
->np_thread_state
== ISCSI_NP_THREAD_INACTIVE
) {
417 spin_unlock_bh(&np
->np_thread_lock
);
420 np
->np_thread_state
= ISCSI_NP_THREAD_RESET
;
423 spin_unlock_bh(&np
->np_thread_lock
);
424 send_sig(SIGINT
, np
->np_thread
, 1);
425 wait_for_completion(&np
->np_restart_comp
);
426 spin_lock_bh(&np
->np_thread_lock
);
428 spin_unlock_bh(&np
->np_thread_lock
);
430 if (tpg_np
&& shutdown
) {
431 kref_put(&tpg_np
->tpg_np_kref
, iscsit_login_kref_put
);
433 wait_for_completion(&tpg_np
->tpg_np_comp
);
439 static void iscsit_free_np(struct iscsi_np
*np
)
442 sock_release(np
->np_socket
);
445 int iscsit_del_np(struct iscsi_np
*np
)
447 spin_lock_bh(&np
->np_thread_lock
);
449 if (np
->np_exports
) {
451 spin_unlock_bh(&np
->np_thread_lock
);
454 np
->np_thread_state
= ISCSI_NP_THREAD_SHUTDOWN
;
455 spin_unlock_bh(&np
->np_thread_lock
);
459 * We need to send the signal to wakeup Linux/Net
460 * which may be sleeping in sock_accept()..
462 send_sig(SIGINT
, np
->np_thread
, 1);
463 kthread_stop(np
->np_thread
);
464 np
->np_thread
= NULL
;
467 np
->np_transport
->iscsit_free_np(np
);
469 mutex_lock(&np_lock
);
470 list_del(&np
->np_list
);
471 mutex_unlock(&np_lock
);
473 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
474 &np
->np_sockaddr
, np
->np_transport
->name
);
476 iscsit_put_transport(np
->np_transport
);
481 static int iscsit_immediate_queue(struct iscsi_conn
*, struct iscsi_cmd
*, int);
482 static int iscsit_response_queue(struct iscsi_conn
*, struct iscsi_cmd
*, int);
484 static int iscsit_queue_rsp(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
)
486 iscsit_add_cmd_to_response_queue(cmd
, cmd
->conn
, cmd
->i_state
);
490 static void iscsit_aborted_task(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
)
492 bool scsi_cmd
= (cmd
->iscsi_opcode
== ISCSI_OP_SCSI_CMD
);
494 spin_lock_bh(&conn
->cmd_lock
);
495 if (!list_empty(&cmd
->i_conn_node
))
496 list_del_init(&cmd
->i_conn_node
);
497 spin_unlock_bh(&conn
->cmd_lock
);
499 __iscsit_free_cmd(cmd
, scsi_cmd
, true);
502 static enum target_prot_op
iscsit_get_sup_prot_ops(struct iscsi_conn
*conn
)
504 return TARGET_PROT_NORMAL
;
507 static struct iscsit_transport iscsi_target_transport
= {
509 .transport_type
= ISCSI_TCP
,
511 .iscsit_setup_np
= iscsit_setup_np
,
512 .iscsit_accept_np
= iscsit_accept_np
,
513 .iscsit_free_np
= iscsit_free_np
,
514 .iscsit_get_login_rx
= iscsit_get_login_rx
,
515 .iscsit_put_login_tx
= iscsit_put_login_tx
,
516 .iscsit_get_dataout
= iscsit_build_r2ts_for_cmd
,
517 .iscsit_immediate_queue
= iscsit_immediate_queue
,
518 .iscsit_response_queue
= iscsit_response_queue
,
519 .iscsit_queue_data_in
= iscsit_queue_rsp
,
520 .iscsit_queue_status
= iscsit_queue_rsp
,
521 .iscsit_aborted_task
= iscsit_aborted_task
,
522 .iscsit_get_sup_prot_ops
= iscsit_get_sup_prot_ops
,
525 static int __init
iscsi_target_init_module(void)
529 pr_debug("iSCSI-Target "ISCSIT_VERSION
"\n");
531 iscsit_global
= kzalloc(sizeof(struct iscsit_global
), GFP_KERNEL
);
532 if (!iscsit_global
) {
533 pr_err("Unable to allocate memory for iscsit_global\n");
536 spin_lock_init(&iscsit_global
->ts_bitmap_lock
);
537 mutex_init(&auth_id_lock
);
538 spin_lock_init(&sess_idr_lock
);
542 ret
= target_register_template(&iscsi_ops
);
546 size
= BITS_TO_LONGS(ISCSIT_BITMAP_BITS
) * sizeof(long);
547 iscsit_global
->ts_bitmap
= vzalloc(size
);
548 if (!iscsit_global
->ts_bitmap
) {
549 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
553 lio_qr_cache
= kmem_cache_create("lio_qr_cache",
554 sizeof(struct iscsi_queue_req
),
555 __alignof__(struct iscsi_queue_req
), 0, NULL
);
557 pr_err("nable to kmem_cache_create() for"
562 lio_dr_cache
= kmem_cache_create("lio_dr_cache",
563 sizeof(struct iscsi_datain_req
),
564 __alignof__(struct iscsi_datain_req
), 0, NULL
);
566 pr_err("Unable to kmem_cache_create() for"
571 lio_ooo_cache
= kmem_cache_create("lio_ooo_cache",
572 sizeof(struct iscsi_ooo_cmdsn
),
573 __alignof__(struct iscsi_ooo_cmdsn
), 0, NULL
);
574 if (!lio_ooo_cache
) {
575 pr_err("Unable to kmem_cache_create() for"
580 lio_r2t_cache
= kmem_cache_create("lio_r2t_cache",
581 sizeof(struct iscsi_r2t
), __alignof__(struct iscsi_r2t
),
583 if (!lio_r2t_cache
) {
584 pr_err("Unable to kmem_cache_create() for"
589 iscsit_register_transport(&iscsi_target_transport
);
591 if (iscsit_load_discovery_tpg() < 0)
596 iscsit_unregister_transport(&iscsi_target_transport
);
597 kmem_cache_destroy(lio_r2t_cache
);
599 kmem_cache_destroy(lio_ooo_cache
);
601 kmem_cache_destroy(lio_dr_cache
);
603 kmem_cache_destroy(lio_qr_cache
);
605 vfree(iscsit_global
->ts_bitmap
);
607 /* XXX: this probably wants it to be it's own unwind step.. */
608 if (iscsit_global
->discovery_tpg
)
609 iscsit_tpg_disable_portal_group(iscsit_global
->discovery_tpg
, 1);
610 target_unregister_template(&iscsi_ops
);
612 kfree(iscsit_global
);
616 static void __exit
iscsi_target_cleanup_module(void)
618 iscsit_release_discovery_tpg();
619 iscsit_unregister_transport(&iscsi_target_transport
);
620 kmem_cache_destroy(lio_qr_cache
);
621 kmem_cache_destroy(lio_dr_cache
);
622 kmem_cache_destroy(lio_ooo_cache
);
623 kmem_cache_destroy(lio_r2t_cache
);
626 * Shutdown discovery sessions and disable discovery TPG
628 if (iscsit_global
->discovery_tpg
)
629 iscsit_tpg_disable_portal_group(iscsit_global
->discovery_tpg
, 1);
631 target_unregister_template(&iscsi_ops
);
633 vfree(iscsit_global
->ts_bitmap
);
634 kfree(iscsit_global
);
637 static int iscsit_add_reject(
638 struct iscsi_conn
*conn
,
642 struct iscsi_cmd
*cmd
;
644 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
648 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
649 cmd
->reject_reason
= reason
;
651 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
653 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
654 iscsit_free_cmd(cmd
, false);
658 spin_lock_bh(&conn
->cmd_lock
);
659 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
660 spin_unlock_bh(&conn
->cmd_lock
);
662 cmd
->i_state
= ISTATE_SEND_REJECT
;
663 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
668 static int iscsit_add_reject_from_cmd(
669 struct iscsi_cmd
*cmd
,
674 struct iscsi_conn
*conn
;
677 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
683 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
684 cmd
->reject_reason
= reason
;
686 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
688 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
689 iscsit_free_cmd(cmd
, false);
694 spin_lock_bh(&conn
->cmd_lock
);
695 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
696 spin_unlock_bh(&conn
->cmd_lock
);
699 cmd
->i_state
= ISTATE_SEND_REJECT
;
700 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
702 * Perform the kref_put now if se_cmd has already been setup by
703 * scsit_setup_scsi_cmd()
705 if (cmd
->se_cmd
.se_tfo
!= NULL
) {
706 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
707 target_put_sess_cmd(&cmd
->se_cmd
);
712 static int iscsit_add_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
,
715 return iscsit_add_reject_from_cmd(cmd
, reason
, true, buf
);
718 int iscsit_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
, unsigned char *buf
)
720 return iscsit_add_reject_from_cmd(cmd
, reason
, false, buf
);
724 * Map some portion of the allocated scatterlist to an iovec, suitable for
725 * kernel sockets to copy data in/out.
727 static int iscsit_map_iovec(
728 struct iscsi_cmd
*cmd
,
734 struct scatterlist
*sg
;
735 unsigned int page_off
;
738 * We know each entry in t_data_sg contains a page.
740 sg
= &cmd
->se_cmd
.t_data_sg
[data_offset
/ PAGE_SIZE
];
741 page_off
= (data_offset
% PAGE_SIZE
);
743 cmd
->first_data_sg
= sg
;
744 cmd
->first_data_sg_off
= page_off
;
746 while (data_length
) {
747 u32 cur_len
= min_t(u32
, data_length
, sg
->length
- page_off
);
749 iov
[i
].iov_base
= kmap(sg_page(sg
)) + sg
->offset
+ page_off
;
750 iov
[i
].iov_len
= cur_len
;
752 data_length
-= cur_len
;
758 cmd
->kmapped_nents
= i
;
763 static void iscsit_unmap_iovec(struct iscsi_cmd
*cmd
)
766 struct scatterlist
*sg
;
768 sg
= cmd
->first_data_sg
;
770 for (i
= 0; i
< cmd
->kmapped_nents
; i
++)
771 kunmap(sg_page(&sg
[i
]));
774 static void iscsit_ack_from_expstatsn(struct iscsi_conn
*conn
, u32 exp_statsn
)
777 struct iscsi_cmd
*cmd
, *cmd_p
;
779 conn
->exp_statsn
= exp_statsn
;
781 if (conn
->sess
->sess_ops
->RDMAExtensions
)
784 spin_lock_bh(&conn
->cmd_lock
);
785 list_for_each_entry_safe(cmd
, cmd_p
, &conn
->conn_cmd_list
, i_conn_node
) {
786 spin_lock(&cmd
->istate_lock
);
787 if ((cmd
->i_state
== ISTATE_SENT_STATUS
) &&
788 iscsi_sna_lt(cmd
->stat_sn
, exp_statsn
)) {
789 cmd
->i_state
= ISTATE_REMOVE
;
790 spin_unlock(&cmd
->istate_lock
);
791 list_move_tail(&cmd
->i_conn_node
, &ack_list
);
794 spin_unlock(&cmd
->istate_lock
);
796 spin_unlock_bh(&conn
->cmd_lock
);
798 list_for_each_entry_safe(cmd
, cmd_p
, &ack_list
, i_conn_node
) {
799 list_del_init(&cmd
->i_conn_node
);
800 iscsit_free_cmd(cmd
, false);
804 static int iscsit_allocate_iovecs(struct iscsi_cmd
*cmd
)
806 u32 iov_count
= max(1UL, DIV_ROUND_UP(cmd
->se_cmd
.data_length
, PAGE_SIZE
));
808 iov_count
+= ISCSI_IOV_DATA_BUFFER
;
810 cmd
->iov_data
= kzalloc(iov_count
* sizeof(struct kvec
), GFP_KERNEL
);
811 if (!cmd
->iov_data
) {
812 pr_err("Unable to allocate cmd->iov_data\n");
816 cmd
->orig_iov_data_count
= iov_count
;
820 int iscsit_setup_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
823 int data_direction
, payload_length
;
824 struct iscsi_scsi_req
*hdr
;
828 atomic_long_inc(&conn
->sess
->cmd_pdus
);
830 hdr
= (struct iscsi_scsi_req
*) buf
;
831 payload_length
= ntoh24(hdr
->dlength
);
833 /* FIXME; Add checks for AdditionalHeaderSegment */
835 if (!(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) &&
836 !(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
837 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
838 " not set. Bad iSCSI Initiator.\n");
839 return iscsit_add_reject_cmd(cmd
,
840 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
843 if (((hdr
->flags
& ISCSI_FLAG_CMD_READ
) ||
844 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) && !hdr
->data_length
) {
846 * From RFC-3720 Section 10.3.1:
848 * "Either or both of R and W MAY be 1 when either the
849 * Expected Data Transfer Length and/or Bidirectional Read
850 * Expected Data Transfer Length are 0"
852 * For this case, go ahead and clear the unnecssary bits
853 * to avoid any confusion with ->data_direction.
855 hdr
->flags
&= ~ISCSI_FLAG_CMD_READ
;
856 hdr
->flags
&= ~ISCSI_FLAG_CMD_WRITE
;
858 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
859 " set when Expected Data Transfer Length is 0 for"
860 " CDB: 0x%02x, Fixing up flags\n", hdr
->cdb
[0]);
863 if (!(hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
864 !(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) && (hdr
->data_length
!= 0)) {
865 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
866 " MUST be set if Expected Data Transfer Length is not 0."
867 " Bad iSCSI Initiator\n");
868 return iscsit_add_reject_cmd(cmd
,
869 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
872 if ((hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
873 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) {
874 pr_err("Bidirectional operations not supported!\n");
875 return iscsit_add_reject_cmd(cmd
,
876 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
879 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
880 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
881 " Scsi Command PDU.\n");
882 return iscsit_add_reject_cmd(cmd
,
883 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
886 if (payload_length
&& !conn
->sess
->sess_ops
->ImmediateData
) {
887 pr_err("ImmediateData=No but DataSegmentLength=%u,"
888 " protocol error.\n", payload_length
);
889 return iscsit_add_reject_cmd(cmd
,
890 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
893 if ((be32_to_cpu(hdr
->data_length
) == payload_length
) &&
894 (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
))) {
895 pr_err("Expected Data Transfer Length and Length of"
896 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
897 " bit is not set protocol error\n");
898 return iscsit_add_reject_cmd(cmd
,
899 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
902 if (payload_length
> be32_to_cpu(hdr
->data_length
)) {
903 pr_err("DataSegmentLength: %u is greater than"
904 " EDTL: %u, protocol error.\n", payload_length
,
906 return iscsit_add_reject_cmd(cmd
,
907 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
910 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
911 pr_err("DataSegmentLength: %u is greater than"
912 " MaxXmitDataSegmentLength: %u, protocol error.\n",
913 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
914 return iscsit_add_reject_cmd(cmd
,
915 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
918 if (payload_length
> conn
->sess
->sess_ops
->FirstBurstLength
) {
919 pr_err("DataSegmentLength: %u is greater than"
920 " FirstBurstLength: %u, protocol error.\n",
921 payload_length
, conn
->sess
->sess_ops
->FirstBurstLength
);
922 return iscsit_add_reject_cmd(cmd
,
923 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
926 data_direction
= (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) ? DMA_TO_DEVICE
:
927 (hdr
->flags
& ISCSI_FLAG_CMD_READ
) ? DMA_FROM_DEVICE
:
930 cmd
->data_direction
= data_direction
;
931 iscsi_task_attr
= hdr
->flags
& ISCSI_FLAG_CMD_ATTR_MASK
;
933 * Figure out the SAM Task Attribute for the incoming SCSI CDB
935 if ((iscsi_task_attr
== ISCSI_ATTR_UNTAGGED
) ||
936 (iscsi_task_attr
== ISCSI_ATTR_SIMPLE
))
937 sam_task_attr
= TCM_SIMPLE_TAG
;
938 else if (iscsi_task_attr
== ISCSI_ATTR_ORDERED
)
939 sam_task_attr
= TCM_ORDERED_TAG
;
940 else if (iscsi_task_attr
== ISCSI_ATTR_HEAD_OF_QUEUE
)
941 sam_task_attr
= TCM_HEAD_TAG
;
942 else if (iscsi_task_attr
== ISCSI_ATTR_ACA
)
943 sam_task_attr
= TCM_ACA_TAG
;
945 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
946 " TCM_SIMPLE_TAG\n", iscsi_task_attr
);
947 sam_task_attr
= TCM_SIMPLE_TAG
;
950 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_CMD
;
951 cmd
->i_state
= ISTATE_NEW_CMD
;
952 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
953 cmd
->immediate_data
= (payload_length
) ? 1 : 0;
954 cmd
->unsolicited_data
= ((!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) &&
955 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) ? 1 : 0);
956 if (cmd
->unsolicited_data
)
957 cmd
->cmd_flags
|= ICF_NON_IMMEDIATE_UNSOLICITED_DATA
;
959 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
960 if (hdr
->flags
& ISCSI_FLAG_CMD_READ
)
961 cmd
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
963 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
964 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
965 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
966 cmd
->first_burst_len
= payload_length
;
968 if (!conn
->sess
->sess_ops
->RDMAExtensions
&&
969 cmd
->data_direction
== DMA_FROM_DEVICE
) {
970 struct iscsi_datain_req
*dr
;
972 dr
= iscsit_allocate_datain_req();
974 return iscsit_add_reject_cmd(cmd
,
975 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
977 iscsit_attach_datain_req(cmd
, dr
);
981 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
983 transport_init_se_cmd(&cmd
->se_cmd
, &iscsi_ops
,
984 conn
->sess
->se_sess
, be32_to_cpu(hdr
->data_length
),
985 cmd
->data_direction
, sam_task_attr
,
986 cmd
->sense_buffer
+ 2);
988 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
989 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr
->itt
,
990 hdr
->cmdsn
, be32_to_cpu(hdr
->data_length
), payload_length
,
993 target_get_sess_cmd(&cmd
->se_cmd
, true);
995 cmd
->sense_reason
= transport_lookup_cmd_lun(&cmd
->se_cmd
,
996 scsilun_to_int(&hdr
->lun
));
997 if (cmd
->sense_reason
)
1000 /* only used for printks or comparing with ->ref_task_tag */
1001 cmd
->se_cmd
.tag
= (__force u32
)cmd
->init_task_tag
;
1002 cmd
->sense_reason
= target_setup_cmd_from_cdb(&cmd
->se_cmd
, hdr
->cdb
);
1003 if (cmd
->sense_reason
) {
1004 if (cmd
->sense_reason
== TCM_OUT_OF_RESOURCES
) {
1005 return iscsit_add_reject_cmd(cmd
,
1006 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1012 if (iscsit_build_pdu_and_seq_lists(cmd
, payload_length
) < 0) {
1013 return iscsit_add_reject_cmd(cmd
,
1014 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1018 spin_lock_bh(&conn
->cmd_lock
);
1019 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1020 spin_unlock_bh(&conn
->cmd_lock
);
1022 * Check if we need to delay processing because of ALUA
1023 * Active/NonOptimized primary access state..
1025 core_alua_check_nonop_delay(&cmd
->se_cmd
);
1029 EXPORT_SYMBOL(iscsit_setup_scsi_cmd
);
1031 void iscsit_set_unsoliticed_dataout(struct iscsi_cmd
*cmd
)
1033 iscsit_set_dataout_sequence_values(cmd
);
1035 spin_lock_bh(&cmd
->dataout_timeout_lock
);
1036 iscsit_start_dataout_timer(cmd
, cmd
->conn
);
1037 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
1039 EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout
);
1041 int iscsit_process_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1042 struct iscsi_scsi_req
*hdr
)
1046 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1047 * the Immediate Bit is not set, and no Immediate
1050 * A PDU/CmdSN carrying Immediate Data can only
1051 * be processed after the DataCRC has passed.
1052 * If the DataCRC fails, the CmdSN MUST NOT
1053 * be acknowledged. (See below)
1055 if (!cmd
->immediate_data
) {
1056 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1057 (unsigned char *)hdr
, hdr
->cmdsn
);
1058 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1060 else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1061 target_put_sess_cmd(&cmd
->se_cmd
);
1066 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1069 * If no Immediate Data is attached, it's OK to return now.
1071 if (!cmd
->immediate_data
) {
1072 if (!cmd
->sense_reason
&& cmd
->unsolicited_data
)
1073 iscsit_set_unsoliticed_dataout(cmd
);
1074 if (!cmd
->sense_reason
)
1077 target_put_sess_cmd(&cmd
->se_cmd
);
1082 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1083 * execution. These exceptions are processed in CmdSN order using
1084 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1086 if (cmd
->sense_reason
) {
1087 if (cmd
->reject_reason
)
1093 * Call directly into transport_generic_new_cmd() to perform
1094 * the backend memory allocation.
1096 cmd
->sense_reason
= transport_generic_new_cmd(&cmd
->se_cmd
);
1097 if (cmd
->sense_reason
)
1102 EXPORT_SYMBOL(iscsit_process_scsi_cmd
);
1105 iscsit_get_immediate_data(struct iscsi_cmd
*cmd
, struct iscsi_scsi_req
*hdr
,
1108 int cmdsn_ret
= 0, immed_ret
= IMMEDIATE_DATA_NORMAL_OPERATION
;
1110 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1113 goto after_immediate_data
;
1115 immed_ret
= iscsit_handle_immediate_data(cmd
, hdr
,
1116 cmd
->first_burst_len
);
1117 after_immediate_data
:
1118 if (immed_ret
== IMMEDIATE_DATA_NORMAL_OPERATION
) {
1120 * A PDU/CmdSN carrying Immediate Data passed
1121 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1122 * Immediate Bit is not set.
1124 cmdsn_ret
= iscsit_sequence_cmd(cmd
->conn
, cmd
,
1125 (unsigned char *)hdr
, hdr
->cmdsn
);
1126 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1129 if (cmd
->sense_reason
|| cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1132 rc
= iscsit_dump_data_payload(cmd
->conn
,
1133 cmd
->first_burst_len
, 1);
1134 target_put_sess_cmd(&cmd
->se_cmd
);
1136 } else if (cmd
->unsolicited_data
)
1137 iscsit_set_unsoliticed_dataout(cmd
);
1139 } else if (immed_ret
== IMMEDIATE_DATA_ERL1_CRC_FAILURE
) {
1141 * Immediate Data failed DataCRC and ERL>=1,
1142 * silently drop this PDU and let the initiator
1143 * plug the CmdSN gap.
1145 * FIXME: Send Unsolicited NOPIN with reserved
1146 * TTT here to help the initiator figure out
1147 * the missing CmdSN, although they should be
1148 * intelligent enough to determine the missing
1149 * CmdSN and issue a retry to plug the sequence.
1151 cmd
->i_state
= ISTATE_REMOVE
;
1152 iscsit_add_cmd_to_immediate_queue(cmd
, cmd
->conn
, cmd
->i_state
);
1153 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1160 iscsit_handle_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1163 struct iscsi_scsi_req
*hdr
= (struct iscsi_scsi_req
*)buf
;
1165 bool dump_payload
= false;
1167 rc
= iscsit_setup_scsi_cmd(conn
, cmd
, buf
);
1171 * Allocation iovecs needed for struct socket operations for
1172 * traditional iSCSI block I/O.
1174 if (iscsit_allocate_iovecs(cmd
) < 0) {
1175 return iscsit_reject_cmd(cmd
,
1176 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1178 immed_data
= cmd
->immediate_data
;
1180 rc
= iscsit_process_scsi_cmd(conn
, cmd
, hdr
);
1184 dump_payload
= true;
1189 return iscsit_get_immediate_data(cmd
, hdr
, dump_payload
);
1192 static u32
iscsit_do_crypto_hash_sg(
1193 struct hash_desc
*hash
,
1194 struct iscsi_cmd
*cmd
,
1201 struct scatterlist
*sg
;
1202 unsigned int page_off
;
1204 crypto_hash_init(hash
);
1206 sg
= cmd
->first_data_sg
;
1207 page_off
= cmd
->first_data_sg_off
;
1209 while (data_length
) {
1210 u32 cur_len
= min_t(u32
, data_length
, (sg
->length
- page_off
));
1212 crypto_hash_update(hash
, sg
, cur_len
);
1214 data_length
-= cur_len
;
1216 /* iscsit_map_iovec has already checked for invalid sg pointers */
1221 struct scatterlist pad_sg
;
1223 sg_init_one(&pad_sg
, pad_bytes
, padding
);
1224 crypto_hash_update(hash
, &pad_sg
, padding
);
1226 crypto_hash_final(hash
, (u8
*) &data_crc
);
1231 static void iscsit_do_crypto_hash_buf(
1232 struct hash_desc
*hash
,
1239 struct scatterlist sg
;
1241 crypto_hash_init(hash
);
1243 sg_init_one(&sg
, buf
, payload_length
);
1244 crypto_hash_update(hash
, &sg
, payload_length
);
1247 sg_init_one(&sg
, pad_bytes
, padding
);
1248 crypto_hash_update(hash
, &sg
, padding
);
1250 crypto_hash_final(hash
, data_crc
);
1254 iscsit_check_dataout_hdr(struct iscsi_conn
*conn
, unsigned char *buf
,
1255 struct iscsi_cmd
**out_cmd
)
1257 struct iscsi_data
*hdr
= (struct iscsi_data
*)buf
;
1258 struct iscsi_cmd
*cmd
= NULL
;
1259 struct se_cmd
*se_cmd
;
1260 u32 payload_length
= ntoh24(hdr
->dlength
);
1263 if (!payload_length
) {
1264 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1269 atomic_long_add(payload_length
, &conn
->sess
->rx_data_octets
);
1271 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1272 pr_err("DataSegmentLength: %u is greater than"
1273 " MaxXmitDataSegmentLength: %u\n", payload_length
,
1274 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1275 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1279 cmd
= iscsit_find_cmd_from_itt_or_dump(conn
, hdr
->itt
,
1284 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1285 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1286 hdr
->itt
, hdr
->ttt
, hdr
->datasn
, ntohl(hdr
->offset
),
1287 payload_length
, conn
->cid
);
1289 if (cmd
->cmd_flags
& ICF_GOT_LAST_DATAOUT
) {
1290 pr_err("Command ITT: 0x%08x received DataOUT after"
1291 " last DataOUT received, dumping payload\n",
1292 cmd
->init_task_tag
);
1293 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1296 if (cmd
->data_direction
!= DMA_TO_DEVICE
) {
1297 pr_err("Command ITT: 0x%08x received DataOUT for a"
1298 " NON-WRITE command.\n", cmd
->init_task_tag
);
1299 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1301 se_cmd
= &cmd
->se_cmd
;
1302 iscsit_mod_dataout_timer(cmd
);
1304 if ((be32_to_cpu(hdr
->offset
) + payload_length
) > cmd
->se_cmd
.data_length
) {
1305 pr_err("DataOut Offset: %u, Length %u greater than"
1306 " iSCSI Command EDTL %u, protocol error.\n",
1307 hdr
->offset
, payload_length
, cmd
->se_cmd
.data_length
);
1308 return iscsit_reject_cmd(cmd
, ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1311 if (cmd
->unsolicited_data
) {
1312 int dump_unsolicited_data
= 0;
1314 if (conn
->sess
->sess_ops
->InitialR2T
) {
1315 pr_err("Received unexpected unsolicited data"
1316 " while InitialR2T=Yes, protocol error.\n");
1317 transport_send_check_condition_and_sense(&cmd
->se_cmd
,
1318 TCM_UNEXPECTED_UNSOLICITED_DATA
, 0);
1322 * Special case for dealing with Unsolicited DataOUT
1323 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1327 /* Something's amiss if we're not in WRITE_PENDING state... */
1328 WARN_ON(se_cmd
->t_state
!= TRANSPORT_WRITE_PENDING
);
1329 if (!(se_cmd
->se_cmd_flags
& SCF_SUPPORTED_SAM_OPCODE
))
1330 dump_unsolicited_data
= 1;
1332 if (dump_unsolicited_data
) {
1334 * Check if a delayed TASK_ABORTED status needs to
1335 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1336 * received with the unsolicitied data out.
1338 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1339 iscsit_stop_dataout_timer(cmd
);
1341 transport_check_aborted_status(se_cmd
,
1342 (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
));
1343 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1347 * For the normal solicited data path:
1349 * Check for a delayed TASK_ABORTED status and dump any
1350 * incoming data out payload if one exists. Also, when the
1351 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1352 * data out sequence, we decrement outstanding_r2ts. Once
1353 * outstanding_r2ts reaches zero, go ahead and send the delayed
1354 * TASK_ABORTED status.
1356 if (se_cmd
->transport_state
& CMD_T_ABORTED
) {
1357 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1358 if (--cmd
->outstanding_r2ts
< 1) {
1359 iscsit_stop_dataout_timer(cmd
);
1360 transport_check_aborted_status(
1364 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1368 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1369 * within-command recovery checks before receiving the payload.
1371 rc
= iscsit_check_pre_dataout(cmd
, buf
);
1372 if (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
)
1374 else if (rc
== DATAOUT_CANNOT_RECOVER
)
1380 EXPORT_SYMBOL(iscsit_check_dataout_hdr
);
1383 iscsit_get_dataout(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1384 struct iscsi_data
*hdr
)
1387 u32 checksum
, iov_count
= 0, padding
= 0, rx_got
= 0, rx_size
= 0;
1388 u32 payload_length
= ntoh24(hdr
->dlength
);
1389 int iov_ret
, data_crc_failed
= 0;
1391 rx_size
+= payload_length
;
1392 iov
= &cmd
->iov_data
[0];
1394 iov_ret
= iscsit_map_iovec(cmd
, iov
, be32_to_cpu(hdr
->offset
),
1399 iov_count
+= iov_ret
;
1401 padding
= ((-payload_length
) & 3);
1403 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
1404 iov
[iov_count
++].iov_len
= padding
;
1406 pr_debug("Receiving %u padding bytes.\n", padding
);
1409 if (conn
->conn_ops
->DataDigest
) {
1410 iov
[iov_count
].iov_base
= &checksum
;
1411 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
1412 rx_size
+= ISCSI_CRC_LEN
;
1415 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
1417 iscsit_unmap_iovec(cmd
);
1419 if (rx_got
!= rx_size
)
1422 if (conn
->conn_ops
->DataDigest
) {
1425 data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_rx_hash
, cmd
,
1426 be32_to_cpu(hdr
->offset
),
1427 payload_length
, padding
,
1430 if (checksum
!= data_crc
) {
1431 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1432 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1433 " does not match computed 0x%08x\n",
1434 hdr
->itt
, hdr
->offset
, payload_length
,
1435 hdr
->datasn
, checksum
, data_crc
);
1436 data_crc_failed
= 1;
1438 pr_debug("Got CRC32C DataDigest 0x%08x for"
1439 " %u bytes of Data Out\n", checksum
,
1444 return data_crc_failed
;
1448 iscsit_check_dataout_payload(struct iscsi_cmd
*cmd
, struct iscsi_data
*hdr
,
1449 bool data_crc_failed
)
1451 struct iscsi_conn
*conn
= cmd
->conn
;
1454 * Increment post receive data and CRC values or perform
1455 * within-command recovery.
1457 rc
= iscsit_check_post_dataout(cmd
, (unsigned char *)hdr
, data_crc_failed
);
1458 if ((rc
== DATAOUT_NORMAL
) || (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
))
1460 else if (rc
== DATAOUT_SEND_R2T
) {
1461 iscsit_set_dataout_sequence_values(cmd
);
1462 conn
->conn_transport
->iscsit_get_dataout(conn
, cmd
, false);
1463 } else if (rc
== DATAOUT_SEND_TO_TRANSPORT
) {
1465 * Handle extra special case for out of order
1466 * Unsolicited Data Out.
1468 spin_lock_bh(&cmd
->istate_lock
);
1469 ooo_cmdsn
= (cmd
->cmd_flags
& ICF_OOO_CMDSN
);
1470 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
1471 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
1472 spin_unlock_bh(&cmd
->istate_lock
);
1474 iscsit_stop_dataout_timer(cmd
);
1477 target_execute_cmd(&cmd
->se_cmd
);
1479 } else /* DATAOUT_CANNOT_RECOVER */
1484 EXPORT_SYMBOL(iscsit_check_dataout_payload
);
1486 static int iscsit_handle_data_out(struct iscsi_conn
*conn
, unsigned char *buf
)
1488 struct iscsi_cmd
*cmd
= NULL
;
1489 struct iscsi_data
*hdr
= (struct iscsi_data
*)buf
;
1491 bool data_crc_failed
= false;
1493 rc
= iscsit_check_dataout_hdr(conn
, buf
, &cmd
);
1499 rc
= iscsit_get_dataout(conn
, cmd
, hdr
);
1503 data_crc_failed
= true;
1505 return iscsit_check_dataout_payload(cmd
, hdr
, data_crc_failed
);
1508 int iscsit_setup_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1509 struct iscsi_nopout
*hdr
)
1511 u32 payload_length
= ntoh24(hdr
->dlength
);
1513 if (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
1514 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1516 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1517 (unsigned char *)hdr
);
1519 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1520 (unsigned char *)hdr
);
1523 if (hdr
->itt
== RESERVED_ITT
&& !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1524 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1525 " not set, protocol error.\n");
1527 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1528 (unsigned char *)hdr
);
1530 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1531 (unsigned char *)hdr
);
1534 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1535 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1536 " greater than MaxXmitDataSegmentLength: %u, protocol"
1537 " error.\n", payload_length
,
1538 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1540 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1541 (unsigned char *)hdr
);
1543 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1544 (unsigned char *)hdr
);
1547 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1548 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1549 hdr
->itt
== RESERVED_ITT
? "Response" : "Request",
1550 hdr
->itt
, hdr
->ttt
, hdr
->cmdsn
, hdr
->exp_statsn
,
1553 * This is not a response to a Unsolicited NopIN, which means
1554 * it can either be a NOPOUT ping request (with a valid ITT),
1555 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1556 * Either way, make sure we allocate an struct iscsi_cmd, as both
1557 * can contain ping data.
1559 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1560 cmd
->iscsi_opcode
= ISCSI_OP_NOOP_OUT
;
1561 cmd
->i_state
= ISTATE_SEND_NOPIN
;
1562 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ?
1564 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1565 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1566 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1567 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1568 cmd
->data_direction
= DMA_NONE
;
1573 EXPORT_SYMBOL(iscsit_setup_nop_out
);
1575 int iscsit_process_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1576 struct iscsi_nopout
*hdr
)
1578 struct iscsi_cmd
*cmd_p
= NULL
;
1581 * Initiator is expecting a NopIN ping reply..
1583 if (hdr
->itt
!= RESERVED_ITT
) {
1585 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1586 (unsigned char *)hdr
);
1588 spin_lock_bh(&conn
->cmd_lock
);
1589 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1590 spin_unlock_bh(&conn
->cmd_lock
);
1592 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1594 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
1595 iscsit_add_cmd_to_response_queue(cmd
, conn
,
1600 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1601 (unsigned char *)hdr
, hdr
->cmdsn
);
1602 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
1604 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1610 * This was a response to a unsolicited NOPIN ping.
1612 if (hdr
->ttt
!= cpu_to_be32(0xFFFFFFFF)) {
1613 cmd_p
= iscsit_find_cmd_from_ttt(conn
, be32_to_cpu(hdr
->ttt
));
1617 iscsit_stop_nopin_response_timer(conn
);
1619 cmd_p
->i_state
= ISTATE_REMOVE
;
1620 iscsit_add_cmd_to_immediate_queue(cmd_p
, conn
, cmd_p
->i_state
);
1622 iscsit_start_nopin_timer(conn
);
1626 * Otherwise, initiator is not expecting a NOPIN is response.
1627 * Just ignore for now.
1631 EXPORT_SYMBOL(iscsit_process_nop_out
);
1633 static int iscsit_handle_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1636 unsigned char *ping_data
= NULL
;
1637 struct iscsi_nopout
*hdr
= (struct iscsi_nopout
*)buf
;
1638 struct kvec
*iov
= NULL
;
1639 u32 payload_length
= ntoh24(hdr
->dlength
);
1642 ret
= iscsit_setup_nop_out(conn
, cmd
, hdr
);
1646 * Handle NOP-OUT payload for traditional iSCSI sockets
1648 if (payload_length
&& hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1649 u32 checksum
, data_crc
, padding
= 0;
1650 int niov
= 0, rx_got
, rx_size
= payload_length
;
1652 ping_data
= kzalloc(payload_length
+ 1, GFP_KERNEL
);
1654 pr_err("Unable to allocate memory for"
1655 " NOPOUT ping data.\n");
1660 iov
= &cmd
->iov_misc
[0];
1661 iov
[niov
].iov_base
= ping_data
;
1662 iov
[niov
++].iov_len
= payload_length
;
1664 padding
= ((-payload_length
) & 3);
1666 pr_debug("Receiving %u additional bytes"
1667 " for padding.\n", padding
);
1668 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
1669 iov
[niov
++].iov_len
= padding
;
1672 if (conn
->conn_ops
->DataDigest
) {
1673 iov
[niov
].iov_base
= &checksum
;
1674 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
1675 rx_size
+= ISCSI_CRC_LEN
;
1678 rx_got
= rx_data(conn
, &cmd
->iov_misc
[0], niov
, rx_size
);
1679 if (rx_got
!= rx_size
) {
1684 if (conn
->conn_ops
->DataDigest
) {
1685 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
1686 ping_data
, payload_length
,
1687 padding
, cmd
->pad_bytes
,
1690 if (checksum
!= data_crc
) {
1691 pr_err("Ping data CRC32C DataDigest"
1692 " 0x%08x does not match computed 0x%08x\n",
1693 checksum
, data_crc
);
1694 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
1695 pr_err("Unable to recover from"
1696 " NOPOUT Ping DataCRC failure while in"
1702 * Silently drop this PDU and let the
1703 * initiator plug the CmdSN gap.
1705 pr_debug("Dropping NOPOUT"
1706 " Command CmdSN: 0x%08x due to"
1707 " DataCRC error.\n", hdr
->cmdsn
);
1712 pr_debug("Got CRC32C DataDigest"
1713 " 0x%08x for %u bytes of ping data.\n",
1714 checksum
, payload_length
);
1718 ping_data
[payload_length
] = '\0';
1720 * Attach ping data to struct iscsi_cmd->buf_ptr.
1722 cmd
->buf_ptr
= ping_data
;
1723 cmd
->buf_ptr_size
= payload_length
;
1725 pr_debug("Got %u bytes of NOPOUT ping"
1726 " data.\n", payload_length
);
1727 pr_debug("Ping Data: \"%s\"\n", ping_data
);
1730 return iscsit_process_nop_out(conn
, cmd
, hdr
);
1733 iscsit_free_cmd(cmd
, false);
1740 iscsit_handle_task_mgt_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1743 struct se_tmr_req
*se_tmr
;
1744 struct iscsi_tmr_req
*tmr_req
;
1745 struct iscsi_tm
*hdr
;
1746 int out_of_order_cmdsn
= 0, ret
;
1747 bool sess_ref
= false;
1750 hdr
= (struct iscsi_tm
*) buf
;
1751 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
1752 function
= hdr
->flags
;
1754 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1755 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1756 " 0x%08x, CID: %hu\n", hdr
->itt
, hdr
->cmdsn
, function
,
1757 hdr
->rtt
, hdr
->refcmdsn
, conn
->cid
);
1759 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1760 ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1761 hdr
->rtt
!= RESERVED_ITT
)) {
1762 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1763 hdr
->rtt
= RESERVED_ITT
;
1766 if ((function
== ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1767 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1768 pr_err("Task Management Request TASK_REASSIGN not"
1769 " issued as immediate command, bad iSCSI Initiator"
1770 "implementation\n");
1771 return iscsit_add_reject_cmd(cmd
,
1772 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1774 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1775 be32_to_cpu(hdr
->refcmdsn
) != ISCSI_RESERVED_TAG
)
1776 hdr
->refcmdsn
= cpu_to_be32(ISCSI_RESERVED_TAG
);
1778 cmd
->data_direction
= DMA_NONE
;
1780 cmd
->tmr_req
= kzalloc(sizeof(struct iscsi_tmr_req
), GFP_KERNEL
);
1781 if (!cmd
->tmr_req
) {
1782 pr_err("Unable to allocate memory for"
1783 " Task Management command!\n");
1784 return iscsit_add_reject_cmd(cmd
,
1785 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1790 * TASK_REASSIGN for ERL=2 / connection stays inside of
1791 * LIO-Target $FABRIC_MOD
1793 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
1798 transport_init_se_cmd(&cmd
->se_cmd
, &iscsi_ops
,
1799 conn
->sess
->se_sess
, 0, DMA_NONE
,
1800 TCM_SIMPLE_TAG
, cmd
->sense_buffer
+ 2);
1802 target_get_sess_cmd(&cmd
->se_cmd
, true);
1806 case ISCSI_TM_FUNC_ABORT_TASK
:
1807 tcm_function
= TMR_ABORT_TASK
;
1809 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
1810 tcm_function
= TMR_ABORT_TASK_SET
;
1812 case ISCSI_TM_FUNC_CLEAR_ACA
:
1813 tcm_function
= TMR_CLEAR_ACA
;
1815 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
1816 tcm_function
= TMR_CLEAR_TASK_SET
;
1818 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
1819 tcm_function
= TMR_LUN_RESET
;
1821 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
1822 tcm_function
= TMR_TARGET_WARM_RESET
;
1824 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
1825 tcm_function
= TMR_TARGET_COLD_RESET
;
1828 pr_err("Unknown iSCSI TMR Function:"
1829 " 0x%02x\n", function
);
1830 return iscsit_add_reject_cmd(cmd
,
1831 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1834 ret
= core_tmr_alloc_req(&cmd
->se_cmd
, cmd
->tmr_req
,
1835 tcm_function
, GFP_KERNEL
);
1837 return iscsit_add_reject_cmd(cmd
,
1838 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1840 cmd
->tmr_req
->se_tmr_req
= cmd
->se_cmd
.se_tmr_req
;
1843 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_TMFUNC
;
1844 cmd
->i_state
= ISTATE_SEND_TASKMGTRSP
;
1845 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1846 cmd
->init_task_tag
= hdr
->itt
;
1847 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1848 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1849 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1850 se_tmr
= cmd
->se_cmd
.se_tmr_req
;
1851 tmr_req
= cmd
->tmr_req
;
1853 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1855 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
1856 ret
= transport_lookup_tmr_lun(&cmd
->se_cmd
,
1857 scsilun_to_int(&hdr
->lun
));
1859 se_tmr
->response
= ISCSI_TMF_RSP_NO_LUN
;
1865 case ISCSI_TM_FUNC_ABORT_TASK
:
1866 se_tmr
->response
= iscsit_tmr_abort_task(cmd
, buf
);
1867 if (se_tmr
->response
)
1870 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
1871 case ISCSI_TM_FUNC_CLEAR_ACA
:
1872 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
1873 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
1875 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
1876 if (iscsit_tmr_task_warm_reset(conn
, tmr_req
, buf
) < 0) {
1877 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
1881 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
1882 if (iscsit_tmr_task_cold_reset(conn
, tmr_req
, buf
) < 0) {
1883 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
1887 case ISCSI_TM_FUNC_TASK_REASSIGN
:
1888 se_tmr
->response
= iscsit_tmr_task_reassign(cmd
, buf
);
1890 * Perform sanity checks on the ExpDataSN only if the
1891 * TASK_REASSIGN was successful.
1893 if (se_tmr
->response
)
1896 if (iscsit_check_task_reassign_expdatasn(tmr_req
, conn
) < 0)
1897 return iscsit_add_reject_cmd(cmd
,
1898 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1901 pr_err("Unknown TMR function: 0x%02x, protocol"
1902 " error.\n", function
);
1903 se_tmr
->response
= ISCSI_TMF_RSP_NOT_SUPPORTED
;
1907 if ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1908 (se_tmr
->response
== ISCSI_TMF_RSP_COMPLETE
))
1909 se_tmr
->call_transport
= 1;
1911 spin_lock_bh(&conn
->cmd_lock
);
1912 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1913 spin_unlock_bh(&conn
->cmd_lock
);
1915 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1916 int cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
1917 if (cmdsn_ret
== CMDSN_HIGHER_THAN_EXP
)
1918 out_of_order_cmdsn
= 1;
1919 else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
1921 else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1924 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1926 if (out_of_order_cmdsn
|| !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
))
1929 * Found the referenced task, send to transport for processing.
1931 if (se_tmr
->call_transport
)
1932 return transport_generic_handle_tmr(&cmd
->se_cmd
);
1935 * Could not find the referenced LUN, task, or Task Management
1936 * command not authorized or supported. Change state and
1937 * let the tx_thread send the response.
1939 * For connection recovery, this is also the default action for
1940 * TMR TASK_REASSIGN.
1943 pr_debug("Handle TMR, using sess_ref=true check\n");
1944 target_put_sess_cmd(&cmd
->se_cmd
);
1947 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
1950 EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd
);
1952 /* #warning FIXME: Support Text Command parameters besides SendTargets */
1954 iscsit_setup_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1955 struct iscsi_text
*hdr
)
1957 u32 payload_length
= ntoh24(hdr
->dlength
);
1959 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1960 pr_err("Unable to accept text parameter length: %u"
1961 "greater than MaxXmitDataSegmentLength %u.\n",
1962 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
1963 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1964 (unsigned char *)hdr
);
1967 if (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) ||
1968 (hdr
->flags
& ISCSI_FLAG_TEXT_CONTINUE
)) {
1969 pr_err("Multi sequence text commands currently not supported\n");
1970 return iscsit_reject_cmd(cmd
, ISCSI_REASON_CMD_NOT_SUPPORTED
,
1971 (unsigned char *)hdr
);
1974 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1975 " ExpStatSN: 0x%08x, Length: %u\n", hdr
->itt
, hdr
->cmdsn
,
1976 hdr
->exp_statsn
, payload_length
);
1978 cmd
->iscsi_opcode
= ISCSI_OP_TEXT
;
1979 cmd
->i_state
= ISTATE_SEND_TEXTRSP
;
1980 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1981 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1982 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1983 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1984 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1985 cmd
->data_direction
= DMA_NONE
;
1986 cmd
->text_in_ptr
= NULL
;
1990 EXPORT_SYMBOL(iscsit_setup_text_cmd
);
1993 iscsit_process_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1994 struct iscsi_text
*hdr
)
1996 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
;
2000 cmd
->targ_xfer_tag
= be32_to_cpu(hdr
->ttt
);
2001 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF) {
2002 pr_err("Unable to locate text_in buffer for sendtargets"
2006 goto empty_sendtargets
;
2008 if (strncmp("SendTargets", text_in
, 11) != 0) {
2009 pr_err("Received Text Data that is not"
2010 " SendTargets, cannot continue.\n");
2013 text_ptr
= strchr(text_in
, '=');
2015 pr_err("No \"=\" separator found in Text Data,"
2016 " cannot continue.\n");
2019 if (!strncmp("=All", text_ptr
, 4)) {
2020 cmd
->cmd_flags
|= ICF_SENDTARGETS_ALL
;
2021 } else if (!strncmp("=iqn.", text_ptr
, 5) ||
2022 !strncmp("=eui.", text_ptr
, 5)) {
2023 cmd
->cmd_flags
|= ICF_SENDTARGETS_SINGLE
;
2025 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr
);
2029 spin_lock_bh(&conn
->cmd_lock
);
2030 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2031 spin_unlock_bh(&conn
->cmd_lock
);
2034 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2036 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
2037 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
2038 (unsigned char *)hdr
, hdr
->cmdsn
);
2039 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2045 return iscsit_execute_cmd(cmd
, 0);
2048 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
2049 (unsigned char *)hdr
);
2051 EXPORT_SYMBOL(iscsit_process_text_cmd
);
2054 iscsit_handle_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2057 struct iscsi_text
*hdr
= (struct iscsi_text
*)buf
;
2058 char *text_in
= NULL
;
2059 u32 payload_length
= ntoh24(hdr
->dlength
);
2062 rc
= iscsit_setup_text_cmd(conn
, cmd
, hdr
);
2066 rx_size
= payload_length
;
2067 if (payload_length
) {
2068 u32 checksum
= 0, data_crc
= 0;
2069 u32 padding
= 0, pad_bytes
= 0;
2070 int niov
= 0, rx_got
;
2073 text_in
= kzalloc(payload_length
, GFP_KERNEL
);
2075 pr_err("Unable to allocate memory for"
2076 " incoming text parameters\n");
2079 cmd
->text_in_ptr
= text_in
;
2081 memset(iov
, 0, 3 * sizeof(struct kvec
));
2082 iov
[niov
].iov_base
= text_in
;
2083 iov
[niov
++].iov_len
= payload_length
;
2085 padding
= ((-payload_length
) & 3);
2087 iov
[niov
].iov_base
= &pad_bytes
;
2088 iov
[niov
++].iov_len
= padding
;
2090 pr_debug("Receiving %u additional bytes"
2091 " for padding.\n", padding
);
2093 if (conn
->conn_ops
->DataDigest
) {
2094 iov
[niov
].iov_base
= &checksum
;
2095 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
2096 rx_size
+= ISCSI_CRC_LEN
;
2099 rx_got
= rx_data(conn
, &iov
[0], niov
, rx_size
);
2100 if (rx_got
!= rx_size
)
2103 if (conn
->conn_ops
->DataDigest
) {
2104 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
2105 text_in
, payload_length
,
2106 padding
, (u8
*)&pad_bytes
,
2109 if (checksum
!= data_crc
) {
2110 pr_err("Text data CRC32C DataDigest"
2111 " 0x%08x does not match computed"
2112 " 0x%08x\n", checksum
, data_crc
);
2113 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2114 pr_err("Unable to recover from"
2115 " Text Data digest failure while in"
2120 * Silently drop this PDU and let the
2121 * initiator plug the CmdSN gap.
2123 pr_debug("Dropping Text"
2124 " Command CmdSN: 0x%08x due to"
2125 " DataCRC error.\n", hdr
->cmdsn
);
2130 pr_debug("Got CRC32C DataDigest"
2131 " 0x%08x for %u bytes of text data.\n",
2132 checksum
, payload_length
);
2135 text_in
[payload_length
- 1] = '\0';
2136 pr_debug("Successfully read %d bytes of text"
2137 " data.\n", payload_length
);
2140 return iscsit_process_text_cmd(conn
, cmd
, hdr
);
2143 kfree(cmd
->text_in_ptr
);
2144 cmd
->text_in_ptr
= NULL
;
2145 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
, buf
);
2148 int iscsit_logout_closesession(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2150 struct iscsi_conn
*conn_p
;
2151 struct iscsi_session
*sess
= conn
->sess
;
2153 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2154 " for SID: %u.\n", conn
->cid
, conn
->sess
->sid
);
2156 atomic_set(&sess
->session_logout
, 1);
2157 atomic_set(&conn
->conn_logout_remove
, 1);
2158 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_SESSION
;
2160 iscsit_inc_conn_usage_count(conn
);
2161 iscsit_inc_session_usage_count(sess
);
2163 spin_lock_bh(&sess
->conn_lock
);
2164 list_for_each_entry(conn_p
, &sess
->sess_conn_list
, conn_list
) {
2165 if (conn_p
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
)
2168 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2169 conn_p
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2171 spin_unlock_bh(&sess
->conn_lock
);
2173 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2178 int iscsit_logout_closeconnection(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2180 struct iscsi_conn
*l_conn
;
2181 struct iscsi_session
*sess
= conn
->sess
;
2183 pr_debug("Received logout request CLOSECONNECTION for CID:"
2184 " %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2187 * A Logout Request with a CLOSECONNECTION reason code for a CID
2188 * can arrive on a connection with a differing CID.
2190 if (conn
->cid
== cmd
->logout_cid
) {
2191 spin_lock_bh(&conn
->state_lock
);
2192 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2193 conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2195 atomic_set(&conn
->conn_logout_remove
, 1);
2196 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
;
2197 iscsit_inc_conn_usage_count(conn
);
2199 spin_unlock_bh(&conn
->state_lock
);
2202 * Handle all different cid CLOSECONNECTION requests in
2203 * iscsit_logout_post_handler_diffcid() as to give enough
2204 * time for any non immediate command's CmdSN to be
2205 * acknowledged on the connection in question.
2207 * Here we simply make sure the CID is still around.
2209 l_conn
= iscsit_get_conn_from_cid(sess
,
2212 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2213 iscsit_add_cmd_to_response_queue(cmd
, conn
,
2218 iscsit_dec_conn_usage_count(l_conn
);
2221 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2226 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2228 struct iscsi_session
*sess
= conn
->sess
;
2230 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2231 " CID: %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2233 if (sess
->sess_ops
->ErrorRecoveryLevel
!= 2) {
2234 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2235 " while ERL!=2.\n");
2236 cmd
->logout_response
= ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
;
2237 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2241 if (conn
->cid
== cmd
->logout_cid
) {
2242 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2243 " with CID: %hu on CID: %hu, implementation error.\n",
2244 cmd
->logout_cid
, conn
->cid
);
2245 cmd
->logout_response
= ISCSI_LOGOUT_CLEANUP_FAILED
;
2246 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2250 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2256 iscsit_handle_logout_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2259 int cmdsn_ret
, logout_remove
= 0;
2261 struct iscsi_logout
*hdr
;
2262 struct iscsi_tiqn
*tiqn
= iscsit_snmp_get_tiqn(conn
);
2264 hdr
= (struct iscsi_logout
*) buf
;
2265 reason_code
= (hdr
->flags
& 0x7f);
2268 spin_lock(&tiqn
->logout_stats
.lock
);
2269 if (reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
)
2270 tiqn
->logout_stats
.normal_logouts
++;
2272 tiqn
->logout_stats
.abnormal_logouts
++;
2273 spin_unlock(&tiqn
->logout_stats
.lock
);
2276 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2277 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2278 hdr
->itt
, hdr
->cmdsn
, hdr
->exp_statsn
, reason_code
,
2279 hdr
->cid
, conn
->cid
);
2281 if (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
) {
2282 pr_err("Received logout request on connection that"
2283 " is not in logged in state, ignoring request.\n");
2284 iscsit_free_cmd(cmd
, false);
2288 cmd
->iscsi_opcode
= ISCSI_OP_LOGOUT
;
2289 cmd
->i_state
= ISTATE_SEND_LOGOUTRSP
;
2290 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2291 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
2292 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2293 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
2294 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
2295 cmd
->logout_cid
= be16_to_cpu(hdr
->cid
);
2296 cmd
->logout_reason
= reason_code
;
2297 cmd
->data_direction
= DMA_NONE
;
2300 * We need to sleep in these cases (by returning 1) until the Logout
2301 * Response gets sent in the tx thread.
2303 if ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) ||
2304 ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
) &&
2305 be16_to_cpu(hdr
->cid
) == conn
->cid
))
2308 spin_lock_bh(&conn
->cmd_lock
);
2309 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2310 spin_unlock_bh(&conn
->cmd_lock
);
2312 if (reason_code
!= ISCSI_LOGOUT_REASON_RECOVERY
)
2313 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2316 * Immediate commands are executed, well, immediately.
2317 * Non-Immediate Logout Commands are executed in CmdSN order.
2319 if (cmd
->immediate_cmd
) {
2320 int ret
= iscsit_execute_cmd(cmd
, 0);
2325 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
2326 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
2328 else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2332 return logout_remove
;
2334 EXPORT_SYMBOL(iscsit_handle_logout_cmd
);
2336 static int iscsit_handle_snack(
2337 struct iscsi_conn
*conn
,
2340 struct iscsi_snack
*hdr
;
2342 hdr
= (struct iscsi_snack
*) buf
;
2343 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
2345 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2346 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2347 " CID: %hu\n", hdr
->itt
, hdr
->exp_statsn
, hdr
->flags
,
2348 hdr
->begrun
, hdr
->runlength
, conn
->cid
);
2350 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2351 pr_err("Initiator sent SNACK request while in"
2352 " ErrorRecoveryLevel=0.\n");
2353 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2357 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2358 * call from inside iscsi_send_recovery_datain_or_r2t().
2360 switch (hdr
->flags
& ISCSI_FLAG_SNACK_TYPE_MASK
) {
2362 return iscsit_handle_recovery_datain_or_r2t(conn
, buf
,
2364 be32_to_cpu(hdr
->ttt
),
2365 be32_to_cpu(hdr
->begrun
),
2366 be32_to_cpu(hdr
->runlength
));
2367 case ISCSI_FLAG_SNACK_TYPE_STATUS
:
2368 return iscsit_handle_status_snack(conn
, hdr
->itt
,
2369 be32_to_cpu(hdr
->ttt
),
2370 be32_to_cpu(hdr
->begrun
), be32_to_cpu(hdr
->runlength
));
2371 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK
:
2372 return iscsit_handle_data_ack(conn
, be32_to_cpu(hdr
->ttt
),
2373 be32_to_cpu(hdr
->begrun
),
2374 be32_to_cpu(hdr
->runlength
));
2375 case ISCSI_FLAG_SNACK_TYPE_RDATA
:
2376 /* FIXME: Support R-Data SNACK */
2377 pr_err("R-Data SNACK Not Supported.\n");
2378 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2381 pr_err("Unknown SNACK type 0x%02x, protocol"
2382 " error.\n", hdr
->flags
& 0x0f);
2383 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2390 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2392 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2393 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2394 wait_for_completion_interruptible_timeout(
2395 &conn
->rx_half_close_comp
,
2396 ISCSI_RX_THREAD_TCP_TIMEOUT
* HZ
);
2400 static int iscsit_handle_immediate_data(
2401 struct iscsi_cmd
*cmd
,
2402 struct iscsi_scsi_req
*hdr
,
2405 int iov_ret
, rx_got
= 0, rx_size
= 0;
2406 u32 checksum
, iov_count
= 0, padding
= 0;
2407 struct iscsi_conn
*conn
= cmd
->conn
;
2410 iov_ret
= iscsit_map_iovec(cmd
, cmd
->iov_data
, cmd
->write_data_done
, length
);
2412 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2415 iov_count
= iov_ret
;
2416 iov
= &cmd
->iov_data
[0];
2418 padding
= ((-length
) & 3);
2420 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2421 iov
[iov_count
++].iov_len
= padding
;
2425 if (conn
->conn_ops
->DataDigest
) {
2426 iov
[iov_count
].iov_base
= &checksum
;
2427 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2428 rx_size
+= ISCSI_CRC_LEN
;
2431 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
2433 iscsit_unmap_iovec(cmd
);
2435 if (rx_got
!= rx_size
) {
2436 iscsit_rx_thread_wait_for_tcp(conn
);
2437 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2440 if (conn
->conn_ops
->DataDigest
) {
2443 data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_rx_hash
, cmd
,
2444 cmd
->write_data_done
, length
, padding
,
2447 if (checksum
!= data_crc
) {
2448 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2449 " does not match computed 0x%08x\n", checksum
,
2452 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2453 pr_err("Unable to recover from"
2454 " Immediate Data digest failure while"
2456 iscsit_reject_cmd(cmd
,
2457 ISCSI_REASON_DATA_DIGEST_ERROR
,
2458 (unsigned char *)hdr
);
2459 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2461 iscsit_reject_cmd(cmd
,
2462 ISCSI_REASON_DATA_DIGEST_ERROR
,
2463 (unsigned char *)hdr
);
2464 return IMMEDIATE_DATA_ERL1_CRC_FAILURE
;
2467 pr_debug("Got CRC32C DataDigest 0x%08x for"
2468 " %u bytes of Immediate Data\n", checksum
,
2473 cmd
->write_data_done
+= length
;
2475 if (cmd
->write_data_done
== cmd
->se_cmd
.data_length
) {
2476 spin_lock_bh(&cmd
->istate_lock
);
2477 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
2478 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
2479 spin_unlock_bh(&cmd
->istate_lock
);
2482 return IMMEDIATE_DATA_NORMAL_OPERATION
;
2486 * Called with sess->conn_lock held.
2488 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2489 with active network interface */
2490 static void iscsit_build_conn_drop_async_message(struct iscsi_conn
*conn
)
2492 struct iscsi_cmd
*cmd
;
2493 struct iscsi_conn
*conn_p
;
2497 * Only send a Asynchronous Message on connections whos network
2498 * interface is still functional.
2500 list_for_each_entry(conn_p
, &conn
->sess
->sess_conn_list
, conn_list
) {
2501 if (conn_p
->conn_state
== TARG_CONN_STATE_LOGGED_IN
) {
2502 iscsit_inc_conn_usage_count(conn_p
);
2511 cmd
= iscsit_allocate_cmd(conn_p
, TASK_RUNNING
);
2513 iscsit_dec_conn_usage_count(conn_p
);
2517 cmd
->logout_cid
= conn
->cid
;
2518 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2519 cmd
->i_state
= ISTATE_SEND_ASYNCMSG
;
2521 spin_lock_bh(&conn_p
->cmd_lock
);
2522 list_add_tail(&cmd
->i_conn_node
, &conn_p
->conn_cmd_list
);
2523 spin_unlock_bh(&conn_p
->cmd_lock
);
2525 iscsit_add_cmd_to_response_queue(cmd
, conn_p
, cmd
->i_state
);
2526 iscsit_dec_conn_usage_count(conn_p
);
2529 static int iscsit_send_conn_drop_async_message(
2530 struct iscsi_cmd
*cmd
,
2531 struct iscsi_conn
*conn
)
2533 struct iscsi_async
*hdr
;
2535 cmd
->tx_size
= ISCSI_HDR_LEN
;
2536 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2538 hdr
= (struct iscsi_async
*) cmd
->pdu
;
2539 hdr
->opcode
= ISCSI_OP_ASYNC_EVENT
;
2540 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
2541 cmd
->init_task_tag
= RESERVED_ITT
;
2542 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2543 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->rsvd4
[0]);
2544 cmd
->stat_sn
= conn
->stat_sn
++;
2545 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2546 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2547 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2548 hdr
->async_event
= ISCSI_ASYNC_MSG_DROPPING_CONNECTION
;
2549 hdr
->param1
= cpu_to_be16(cmd
->logout_cid
);
2550 hdr
->param2
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Wait
);
2551 hdr
->param3
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Retain
);
2553 if (conn
->conn_ops
->HeaderDigest
) {
2554 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2556 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
2557 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2559 cmd
->tx_size
+= ISCSI_CRC_LEN
;
2560 pr_debug("Attaching CRC32C HeaderDigest to"
2561 " Async Message 0x%08x\n", *header_digest
);
2564 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2565 cmd
->iov_misc
[0].iov_len
= cmd
->tx_size
;
2566 cmd
->iov_misc_count
= 1;
2568 pr_debug("Sending Connection Dropped Async Message StatSN:"
2569 " 0x%08x, for CID: %hu on CID: %hu\n", cmd
->stat_sn
,
2570 cmd
->logout_cid
, conn
->cid
);
2574 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2576 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2577 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2578 wait_for_completion_interruptible_timeout(
2579 &conn
->tx_half_close_comp
,
2580 ISCSI_TX_THREAD_TCP_TIMEOUT
* HZ
);
2585 iscsit_build_datain_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2586 struct iscsi_datain
*datain
, struct iscsi_data_rsp
*hdr
,
2589 hdr
->opcode
= ISCSI_OP_SCSI_DATA_IN
;
2590 hdr
->flags
= datain
->flags
;
2591 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
2592 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
2593 hdr
->flags
|= ISCSI_FLAG_DATA_OVERFLOW
;
2594 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2595 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
2596 hdr
->flags
|= ISCSI_FLAG_DATA_UNDERFLOW
;
2597 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2600 hton24(hdr
->dlength
, datain
->length
);
2601 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2602 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
2603 (struct scsi_lun
*)&hdr
->lun
);
2605 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2607 hdr
->itt
= cmd
->init_task_tag
;
2609 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2610 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2612 hdr
->ttt
= cpu_to_be32(0xFFFFFFFF);
2614 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2616 hdr
->statsn
= cpu_to_be32(0xFFFFFFFF);
2618 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2619 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2620 hdr
->datasn
= cpu_to_be32(datain
->data_sn
);
2621 hdr
->offset
= cpu_to_be32(datain
->offset
);
2623 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2624 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2625 cmd
->init_task_tag
, ntohl(hdr
->statsn
), ntohl(hdr
->datasn
),
2626 ntohl(hdr
->offset
), datain
->length
, conn
->cid
);
2629 static int iscsit_send_datain(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2631 struct iscsi_data_rsp
*hdr
= (struct iscsi_data_rsp
*)&cmd
->pdu
[0];
2632 struct iscsi_datain datain
;
2633 struct iscsi_datain_req
*dr
;
2635 u32 iov_count
= 0, tx_size
= 0;
2636 int eodr
= 0, ret
, iov_ret
;
2637 bool set_statsn
= false;
2639 memset(&datain
, 0, sizeof(struct iscsi_datain
));
2640 dr
= iscsit_get_datain_values(cmd
, &datain
);
2642 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2643 cmd
->init_task_tag
);
2647 * Be paranoid and double check the logic for now.
2649 if ((datain
.offset
+ datain
.length
) > cmd
->se_cmd
.data_length
) {
2650 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2651 " datain.length: %u exceeds cmd->data_length: %u\n",
2652 cmd
->init_task_tag
, datain
.offset
, datain
.length
,
2653 cmd
->se_cmd
.data_length
);
2657 atomic_long_add(datain
.length
, &conn
->sess
->tx_data_octets
);
2659 * Special case for successfully execution w/ both DATAIN
2662 if ((datain
.flags
& ISCSI_FLAG_DATA_STATUS
) &&
2663 (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
))
2664 datain
.flags
&= ~ISCSI_FLAG_DATA_STATUS
;
2666 if ((dr
->dr_complete
== DATAIN_COMPLETE_NORMAL
) ||
2667 (dr
->dr_complete
== DATAIN_COMPLETE_CONNECTION_RECOVERY
)) {
2668 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2669 cmd
->stat_sn
= conn
->stat_sn
++;
2671 } else if (dr
->dr_complete
==
2672 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY
)
2676 iscsit_build_datain_pdu(cmd
, conn
, &datain
, hdr
, set_statsn
);
2678 iov
= &cmd
->iov_data
[0];
2679 iov
[iov_count
].iov_base
= cmd
->pdu
;
2680 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
2681 tx_size
+= ISCSI_HDR_LEN
;
2683 if (conn
->conn_ops
->HeaderDigest
) {
2684 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2686 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, cmd
->pdu
,
2687 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2689 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2690 tx_size
+= ISCSI_CRC_LEN
;
2692 pr_debug("Attaching CRC32 HeaderDigest"
2693 " for DataIN PDU 0x%08x\n", *header_digest
);
2696 iov_ret
= iscsit_map_iovec(cmd
, &cmd
->iov_data
[1],
2697 datain
.offset
, datain
.length
);
2701 iov_count
+= iov_ret
;
2702 tx_size
+= datain
.length
;
2704 cmd
->padding
= ((-datain
.length
) & 3);
2706 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2707 iov
[iov_count
++].iov_len
= cmd
->padding
;
2708 tx_size
+= cmd
->padding
;
2710 pr_debug("Attaching %u padding bytes\n",
2713 if (conn
->conn_ops
->DataDigest
) {
2714 cmd
->data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_tx_hash
, cmd
,
2715 datain
.offset
, datain
.length
, cmd
->padding
, cmd
->pad_bytes
);
2717 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
2718 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2719 tx_size
+= ISCSI_CRC_LEN
;
2721 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2722 " 0x%08x\n", datain
.length
+cmd
->padding
, cmd
->data_crc
);
2725 cmd
->iov_data_count
= iov_count
;
2726 cmd
->tx_size
= tx_size
;
2728 ret
= iscsit_fe_sendpage_sg(cmd
, conn
);
2730 iscsit_unmap_iovec(cmd
);
2733 iscsit_tx_thread_wait_for_tcp(conn
);
2737 if (dr
->dr_complete
) {
2738 eodr
= (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ?
2740 iscsit_free_datain_req(cmd
, dr
);
2747 iscsit_build_logout_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2748 struct iscsi_logout_rsp
*hdr
)
2750 struct iscsi_conn
*logout_conn
= NULL
;
2751 struct iscsi_conn_recovery
*cr
= NULL
;
2752 struct iscsi_session
*sess
= conn
->sess
;
2754 * The actual shutting down of Sessions and/or Connections
2755 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2756 * is done in scsi_logout_post_handler().
2758 switch (cmd
->logout_reason
) {
2759 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
2760 pr_debug("iSCSI session logout successful, setting"
2761 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2762 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2764 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
2765 if (cmd
->logout_response
== ISCSI_LOGOUT_CID_NOT_FOUND
)
2768 * For CLOSECONNECTION logout requests carrying
2769 * a matching logout CID -> local CID, the reference
2770 * for the local CID will have been incremented in
2771 * iscsi_logout_closeconnection().
2773 * For CLOSECONNECTION logout requests carrying
2774 * a different CID than the connection it arrived
2775 * on, the connection responding to cmd->logout_cid
2776 * is stopped in iscsit_logout_post_handler_diffcid().
2779 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2780 " successful.\n", cmd
->logout_cid
, conn
->cid
);
2781 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2783 case ISCSI_LOGOUT_REASON_RECOVERY
:
2784 if ((cmd
->logout_response
== ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
) ||
2785 (cmd
->logout_response
== ISCSI_LOGOUT_CLEANUP_FAILED
))
2788 * If the connection is still active from our point of view
2789 * force connection recovery to occur.
2791 logout_conn
= iscsit_get_conn_from_cid_rcfr(sess
,
2794 iscsit_connection_reinstatement_rcfr(logout_conn
);
2795 iscsit_dec_conn_usage_count(logout_conn
);
2798 cr
= iscsit_get_inactive_connection_recovery_entry(
2799 conn
->sess
, cmd
->logout_cid
);
2801 pr_err("Unable to locate CID: %hu for"
2802 " REMOVECONNFORRECOVERY Logout Request.\n",
2804 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2808 iscsit_discard_cr_cmds_by_expstatsn(cr
, cmd
->exp_stat_sn
);
2810 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2811 " for recovery for CID: %hu on CID: %hu successful.\n",
2812 cmd
->logout_cid
, conn
->cid
);
2813 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2816 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2817 cmd
->logout_reason
);
2821 hdr
->opcode
= ISCSI_OP_LOGOUT_RSP
;
2822 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2823 hdr
->response
= cmd
->logout_response
;
2824 hdr
->itt
= cmd
->init_task_tag
;
2825 cmd
->stat_sn
= conn
->stat_sn
++;
2826 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2828 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2829 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2830 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2832 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2833 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2834 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
,
2835 cmd
->logout_cid
, conn
->cid
);
2839 EXPORT_SYMBOL(iscsit_build_logout_rsp
);
2842 iscsit_send_logout(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2845 int niov
= 0, tx_size
, rc
;
2847 rc
= iscsit_build_logout_rsp(cmd
, conn
,
2848 (struct iscsi_logout_rsp
*)&cmd
->pdu
[0]);
2852 tx_size
= ISCSI_HDR_LEN
;
2853 iov
= &cmd
->iov_misc
[0];
2854 iov
[niov
].iov_base
= cmd
->pdu
;
2855 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
2857 if (conn
->conn_ops
->HeaderDigest
) {
2858 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2860 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, &cmd
->pdu
[0],
2861 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2863 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2864 tx_size
+= ISCSI_CRC_LEN
;
2865 pr_debug("Attaching CRC32C HeaderDigest to"
2866 " Logout Response 0x%08x\n", *header_digest
);
2868 cmd
->iov_misc_count
= niov
;
2869 cmd
->tx_size
= tx_size
;
2875 iscsit_build_nopin_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2876 struct iscsi_nopin
*hdr
, bool nopout_response
)
2878 hdr
->opcode
= ISCSI_OP_NOOP_IN
;
2879 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2880 hton24(hdr
->dlength
, cmd
->buf_ptr_size
);
2881 if (nopout_response
)
2882 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2883 hdr
->itt
= cmd
->init_task_tag
;
2884 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2885 cmd
->stat_sn
= (nopout_response
) ? conn
->stat_sn
++ :
2887 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2889 if (nopout_response
)
2890 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2892 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2893 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
2895 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2896 " StatSN: 0x%08x, Length %u\n", (nopout_response
) ?
2897 "Solicitied" : "Unsolicitied", cmd
->init_task_tag
,
2898 cmd
->targ_xfer_tag
, cmd
->stat_sn
, cmd
->buf_ptr_size
);
2900 EXPORT_SYMBOL(iscsit_build_nopin_rsp
);
2903 * Unsolicited NOPIN, either requesting a response or not.
2905 static int iscsit_send_unsolicited_nopin(
2906 struct iscsi_cmd
*cmd
,
2907 struct iscsi_conn
*conn
,
2910 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
2911 int tx_size
= ISCSI_HDR_LEN
, ret
;
2913 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, false);
2915 if (conn
->conn_ops
->HeaderDigest
) {
2916 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2918 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
2919 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2921 tx_size
+= ISCSI_CRC_LEN
;
2922 pr_debug("Attaching CRC32C HeaderDigest to"
2923 " NopIN 0x%08x\n", *header_digest
);
2926 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2927 cmd
->iov_misc
[0].iov_len
= tx_size
;
2928 cmd
->iov_misc_count
= 1;
2929 cmd
->tx_size
= tx_size
;
2931 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2932 " 0x%08x CID: %hu\n", hdr
->ttt
, cmd
->stat_sn
, conn
->cid
);
2934 ret
= iscsit_send_tx_data(cmd
, conn
, 1);
2936 iscsit_tx_thread_wait_for_tcp(conn
);
2940 spin_lock_bh(&cmd
->istate_lock
);
2941 cmd
->i_state
= want_response
?
2942 ISTATE_SENT_NOPIN_WANT_RESPONSE
: ISTATE_SENT_STATUS
;
2943 spin_unlock_bh(&cmd
->istate_lock
);
2949 iscsit_send_nopin(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2951 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
2954 int niov
= 0, tx_size
;
2956 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, true);
2958 tx_size
= ISCSI_HDR_LEN
;
2959 iov
= &cmd
->iov_misc
[0];
2960 iov
[niov
].iov_base
= cmd
->pdu
;
2961 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
2963 if (conn
->conn_ops
->HeaderDigest
) {
2964 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2966 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
2967 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2969 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2970 tx_size
+= ISCSI_CRC_LEN
;
2971 pr_debug("Attaching CRC32C HeaderDigest"
2972 " to NopIn 0x%08x\n", *header_digest
);
2976 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2977 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2979 if (cmd
->buf_ptr_size
) {
2980 iov
[niov
].iov_base
= cmd
->buf_ptr
;
2981 iov
[niov
++].iov_len
= cmd
->buf_ptr_size
;
2982 tx_size
+= cmd
->buf_ptr_size
;
2984 pr_debug("Echoing back %u bytes of ping"
2985 " data.\n", cmd
->buf_ptr_size
);
2987 padding
= ((-cmd
->buf_ptr_size
) & 3);
2989 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
2990 iov
[niov
++].iov_len
= padding
;
2992 pr_debug("Attaching %u additional"
2993 " padding bytes.\n", padding
);
2995 if (conn
->conn_ops
->DataDigest
) {
2996 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2997 cmd
->buf_ptr
, cmd
->buf_ptr_size
,
2998 padding
, (u8
*)&cmd
->pad_bytes
,
2999 (u8
*)&cmd
->data_crc
);
3001 iov
[niov
].iov_base
= &cmd
->data_crc
;
3002 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
3003 tx_size
+= ISCSI_CRC_LEN
;
3004 pr_debug("Attached DataDigest for %u"
3005 " bytes of ping data, CRC 0x%08x\n",
3006 cmd
->buf_ptr_size
, cmd
->data_crc
);
3010 cmd
->iov_misc_count
= niov
;
3011 cmd
->tx_size
= tx_size
;
3016 static int iscsit_send_r2t(
3017 struct iscsi_cmd
*cmd
,
3018 struct iscsi_conn
*conn
)
3021 struct iscsi_r2t
*r2t
;
3022 struct iscsi_r2t_rsp
*hdr
;
3025 r2t
= iscsit_get_r2t_from_list(cmd
);
3029 hdr
= (struct iscsi_r2t_rsp
*) cmd
->pdu
;
3030 memset(hdr
, 0, ISCSI_HDR_LEN
);
3031 hdr
->opcode
= ISCSI_OP_R2T
;
3032 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3033 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
3034 (struct scsi_lun
*)&hdr
->lun
);
3035 hdr
->itt
= cmd
->init_task_tag
;
3036 r2t
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
3037 hdr
->ttt
= cpu_to_be32(r2t
->targ_xfer_tag
);
3038 hdr
->statsn
= cpu_to_be32(conn
->stat_sn
);
3039 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3040 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3041 hdr
->r2tsn
= cpu_to_be32(r2t
->r2t_sn
);
3042 hdr
->data_offset
= cpu_to_be32(r2t
->offset
);
3043 hdr
->data_length
= cpu_to_be32(r2t
->xfer_len
);
3045 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
3046 cmd
->iov_misc
[0].iov_len
= ISCSI_HDR_LEN
;
3047 tx_size
+= ISCSI_HDR_LEN
;
3049 if (conn
->conn_ops
->HeaderDigest
) {
3050 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3052 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3053 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3055 cmd
->iov_misc
[0].iov_len
+= ISCSI_CRC_LEN
;
3056 tx_size
+= ISCSI_CRC_LEN
;
3057 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3058 " PDU 0x%08x\n", *header_digest
);
3061 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3062 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3063 (!r2t
->recovery_r2t
) ? "" : "Recovery ", cmd
->init_task_tag
,
3064 r2t
->targ_xfer_tag
, ntohl(hdr
->statsn
), r2t
->r2t_sn
,
3065 r2t
->offset
, r2t
->xfer_len
, conn
->cid
);
3067 cmd
->iov_misc_count
= 1;
3068 cmd
->tx_size
= tx_size
;
3070 spin_lock_bh(&cmd
->r2t_lock
);
3072 spin_unlock_bh(&cmd
->r2t_lock
);
3074 ret
= iscsit_send_tx_data(cmd
, conn
, 1);
3076 iscsit_tx_thread_wait_for_tcp(conn
);
3080 spin_lock_bh(&cmd
->dataout_timeout_lock
);
3081 iscsit_start_dataout_timer(cmd
, conn
);
3082 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
3088 * @recovery: If called from iscsi_task_reassign_complete_write() for
3089 * connection recovery.
3091 int iscsit_build_r2ts_for_cmd(
3092 struct iscsi_conn
*conn
,
3093 struct iscsi_cmd
*cmd
,
3097 u32 offset
= 0, xfer_len
= 0;
3099 spin_lock_bh(&cmd
->r2t_lock
);
3100 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
) {
3101 spin_unlock_bh(&cmd
->r2t_lock
);
3105 if (conn
->sess
->sess_ops
->DataSequenceInOrder
&&
3107 cmd
->r2t_offset
= max(cmd
->r2t_offset
, cmd
->write_data_done
);
3109 while (cmd
->outstanding_r2ts
< conn
->sess
->sess_ops
->MaxOutstandingR2T
) {
3110 if (conn
->sess
->sess_ops
->DataSequenceInOrder
) {
3111 offset
= cmd
->r2t_offset
;
3113 if (first_r2t
&& recovery
) {
3114 int new_data_end
= offset
+
3115 conn
->sess
->sess_ops
->MaxBurstLength
-
3116 cmd
->next_burst_len
;
3118 if (new_data_end
> cmd
->se_cmd
.data_length
)
3119 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3122 conn
->sess
->sess_ops
->MaxBurstLength
-
3123 cmd
->next_burst_len
;
3125 int new_data_end
= offset
+
3126 conn
->sess
->sess_ops
->MaxBurstLength
;
3128 if (new_data_end
> cmd
->se_cmd
.data_length
)
3129 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3131 xfer_len
= conn
->sess
->sess_ops
->MaxBurstLength
;
3133 cmd
->r2t_offset
+= xfer_len
;
3135 if (cmd
->r2t_offset
== cmd
->se_cmd
.data_length
)
3136 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3138 struct iscsi_seq
*seq
;
3140 seq
= iscsit_get_seq_holder_for_r2t(cmd
);
3142 spin_unlock_bh(&cmd
->r2t_lock
);
3146 offset
= seq
->offset
;
3147 xfer_len
= seq
->xfer_len
;
3149 if (cmd
->seq_send_order
== cmd
->seq_count
)
3150 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3152 cmd
->outstanding_r2ts
++;
3155 if (iscsit_add_r2t_to_list(cmd
, offset
, xfer_len
, 0, 0) < 0) {
3156 spin_unlock_bh(&cmd
->r2t_lock
);
3160 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
)
3163 spin_unlock_bh(&cmd
->r2t_lock
);
3168 void iscsit_build_rsp_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3169 bool inc_stat_sn
, struct iscsi_scsi_rsp
*hdr
)
3172 cmd
->stat_sn
= conn
->stat_sn
++;
3174 atomic_long_inc(&conn
->sess
->rsp_pdus
);
3176 memset(hdr
, 0, ISCSI_HDR_LEN
);
3177 hdr
->opcode
= ISCSI_OP_SCSI_CMD_RSP
;
3178 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3179 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
3180 hdr
->flags
|= ISCSI_FLAG_CMD_OVERFLOW
;
3181 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3182 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
3183 hdr
->flags
|= ISCSI_FLAG_CMD_UNDERFLOW
;
3184 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3186 hdr
->response
= cmd
->iscsi_response
;
3187 hdr
->cmd_status
= cmd
->se_cmd
.scsi_status
;
3188 hdr
->itt
= cmd
->init_task_tag
;
3189 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3191 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3192 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3193 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3195 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3196 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3197 cmd
->init_task_tag
, cmd
->stat_sn
, cmd
->se_cmd
.scsi_status
,
3198 cmd
->se_cmd
.scsi_status
, conn
->cid
);
3200 EXPORT_SYMBOL(iscsit_build_rsp_pdu
);
3202 static int iscsit_send_response(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3204 struct iscsi_scsi_rsp
*hdr
= (struct iscsi_scsi_rsp
*)&cmd
->pdu
[0];
3206 u32 padding
= 0, tx_size
= 0;
3208 bool inc_stat_sn
= (cmd
->i_state
== ISTATE_SEND_STATUS
);
3210 iscsit_build_rsp_pdu(cmd
, conn
, inc_stat_sn
, hdr
);
3212 iov
= &cmd
->iov_misc
[0];
3213 iov
[iov_count
].iov_base
= cmd
->pdu
;
3214 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3215 tx_size
+= ISCSI_HDR_LEN
;
3218 * Attach SENSE DATA payload to iSCSI Response PDU
3220 if (cmd
->se_cmd
.sense_buffer
&&
3221 ((cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
3222 (cmd
->se_cmd
.se_cmd_flags
& SCF_EMULATED_TASK_SENSE
))) {
3223 put_unaligned_be16(cmd
->se_cmd
.scsi_sense_length
, cmd
->sense_buffer
);
3224 cmd
->se_cmd
.scsi_sense_length
+= sizeof (__be16
);
3226 padding
= -(cmd
->se_cmd
.scsi_sense_length
) & 3;
3227 hton24(hdr
->dlength
, (u32
)cmd
->se_cmd
.scsi_sense_length
);
3228 iov
[iov_count
].iov_base
= cmd
->sense_buffer
;
3229 iov
[iov_count
++].iov_len
=
3230 (cmd
->se_cmd
.scsi_sense_length
+ padding
);
3231 tx_size
+= cmd
->se_cmd
.scsi_sense_length
;
3234 memset(cmd
->sense_buffer
+
3235 cmd
->se_cmd
.scsi_sense_length
, 0, padding
);
3237 pr_debug("Adding %u bytes of padding to"
3238 " SENSE.\n", padding
);
3241 if (conn
->conn_ops
->DataDigest
) {
3242 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3244 (cmd
->se_cmd
.scsi_sense_length
+ padding
),
3245 0, NULL
, (u8
*)&cmd
->data_crc
);
3247 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3248 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3249 tx_size
+= ISCSI_CRC_LEN
;
3251 pr_debug("Attaching CRC32 DataDigest for"
3252 " SENSE, %u bytes CRC 0x%08x\n",
3253 (cmd
->se_cmd
.scsi_sense_length
+ padding
),
3257 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3259 cmd
->se_cmd
.scsi_sense_length
);
3262 if (conn
->conn_ops
->HeaderDigest
) {
3263 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3265 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, cmd
->pdu
,
3266 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3268 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3269 tx_size
+= ISCSI_CRC_LEN
;
3270 pr_debug("Attaching CRC32 HeaderDigest for Response"
3271 " PDU 0x%08x\n", *header_digest
);
3274 cmd
->iov_misc_count
= iov_count
;
3275 cmd
->tx_size
= tx_size
;
3280 static u8
iscsit_convert_tcm_tmr_rsp(struct se_tmr_req
*se_tmr
)
3282 switch (se_tmr
->response
) {
3283 case TMR_FUNCTION_COMPLETE
:
3284 return ISCSI_TMF_RSP_COMPLETE
;
3285 case TMR_TASK_DOES_NOT_EXIST
:
3286 return ISCSI_TMF_RSP_NO_TASK
;
3287 case TMR_LUN_DOES_NOT_EXIST
:
3288 return ISCSI_TMF_RSP_NO_LUN
;
3289 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
:
3290 return ISCSI_TMF_RSP_NOT_SUPPORTED
;
3291 case TMR_FUNCTION_REJECTED
:
3293 return ISCSI_TMF_RSP_REJECTED
;
3298 iscsit_build_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3299 struct iscsi_tm_rsp
*hdr
)
3301 struct se_tmr_req
*se_tmr
= cmd
->se_cmd
.se_tmr_req
;
3303 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC_RSP
;
3304 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
3305 hdr
->response
= iscsit_convert_tcm_tmr_rsp(se_tmr
);
3306 hdr
->itt
= cmd
->init_task_tag
;
3307 cmd
->stat_sn
= conn
->stat_sn
++;
3308 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3310 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3311 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3312 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3314 pr_debug("Built Task Management Response ITT: 0x%08x,"
3315 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3316 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
, conn
->cid
);
3318 EXPORT_SYMBOL(iscsit_build_task_mgt_rsp
);
3321 iscsit_send_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3323 struct iscsi_tm_rsp
*hdr
= (struct iscsi_tm_rsp
*)&cmd
->pdu
[0];
3326 iscsit_build_task_mgt_rsp(cmd
, conn
, hdr
);
3328 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
3329 cmd
->iov_misc
[0].iov_len
= ISCSI_HDR_LEN
;
3330 tx_size
+= ISCSI_HDR_LEN
;
3332 if (conn
->conn_ops
->HeaderDigest
) {
3333 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3335 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3336 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3338 cmd
->iov_misc
[0].iov_len
+= ISCSI_CRC_LEN
;
3339 tx_size
+= ISCSI_CRC_LEN
;
3340 pr_debug("Attaching CRC32 HeaderDigest for Task"
3341 " Mgmt Response PDU 0x%08x\n", *header_digest
);
3344 cmd
->iov_misc_count
= 1;
3345 cmd
->tx_size
= tx_size
;
3350 static bool iscsit_check_inaddr_any(struct iscsi_np
*np
)
3354 if (np
->np_sockaddr
.ss_family
== AF_INET6
) {
3355 const struct sockaddr_in6 sin6
= {
3356 .sin6_addr
= IN6ADDR_ANY_INIT
};
3357 struct sockaddr_in6
*sock_in6
=
3358 (struct sockaddr_in6
*)&np
->np_sockaddr
;
3360 if (!memcmp(sock_in6
->sin6_addr
.s6_addr
,
3361 sin6
.sin6_addr
.s6_addr
, 16))
3364 struct sockaddr_in
* sock_in
=
3365 (struct sockaddr_in
*)&np
->np_sockaddr
;
3367 if (sock_in
->sin_addr
.s_addr
== htonl(INADDR_ANY
))
3374 #define SENDTARGETS_BUF_LIMIT 32768U
3377 iscsit_build_sendtargets_response(struct iscsi_cmd
*cmd
,
3378 enum iscsit_transport_type network_transport
,
3379 int skip_bytes
, bool *completed
)
3381 char *payload
= NULL
;
3382 struct iscsi_conn
*conn
= cmd
->conn
;
3383 struct iscsi_portal_group
*tpg
;
3384 struct iscsi_tiqn
*tiqn
;
3385 struct iscsi_tpg_np
*tpg_np
;
3386 int buffer_len
, end_of_buf
= 0, len
= 0, payload_len
= 0;
3387 int target_name_printed
;
3388 unsigned char buf
[ISCSI_IQN_LEN
+12]; /* iqn + "TargetName=" + \0 */
3389 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
= NULL
;
3392 buffer_len
= min(conn
->conn_ops
->MaxRecvDataSegmentLength
,
3393 SENDTARGETS_BUF_LIMIT
);
3395 payload
= kzalloc(buffer_len
, GFP_KERNEL
);
3397 pr_err("Unable to allocate memory for sendtargets"
3402 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
3405 if (cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
) {
3406 text_ptr
= strchr(text_in
, '=');
3408 pr_err("Unable to locate '=' string in text_in:"
3414 * Skip over '=' character..
3419 spin_lock(&tiqn_lock
);
3420 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
3421 if ((cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
) &&
3422 strcmp(tiqn
->tiqn
, text_ptr
)) {
3426 target_name_printed
= 0;
3428 spin_lock(&tiqn
->tiqn_tpg_lock
);
3429 list_for_each_entry(tpg
, &tiqn
->tiqn_tpg_list
, tpg_list
) {
3431 /* If demo_mode_discovery=0 and generate_node_acls=0
3432 * (demo mode dislabed) do not return
3433 * TargetName+TargetAddress unless a NodeACL exists.
3436 if ((tpg
->tpg_attrib
.generate_node_acls
== 0) &&
3437 (tpg
->tpg_attrib
.demo_mode_discovery
== 0) &&
3438 (!core_tpg_get_initiator_node_acl(&tpg
->tpg_se_tpg
,
3439 cmd
->conn
->sess
->sess_ops
->InitiatorName
))) {
3443 spin_lock(&tpg
->tpg_state_lock
);
3444 active
= (tpg
->tpg_state
== TPG_STATE_ACTIVE
);
3445 spin_unlock(&tpg
->tpg_state_lock
);
3447 if (!active
&& tpg
->tpg_attrib
.tpg_enabled_sendtargets
)
3450 spin_lock(&tpg
->tpg_np_lock
);
3451 list_for_each_entry(tpg_np
, &tpg
->tpg_gnp_list
,
3453 struct iscsi_np
*np
= tpg_np
->tpg_np
;
3454 bool inaddr_any
= iscsit_check_inaddr_any(np
);
3455 struct sockaddr_storage
*sockaddr
;
3457 if (np
->np_network_transport
!= network_transport
)
3460 if (!target_name_printed
) {
3461 len
= sprintf(buf
, "TargetName=%s",
3465 if ((len
+ payload_len
) > buffer_len
) {
3466 spin_unlock(&tpg
->tpg_np_lock
);
3467 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3472 if (skip_bytes
&& len
<= skip_bytes
) {
3475 memcpy(payload
+ payload_len
, buf
, len
);
3477 target_name_printed
= 1;
3478 if (len
> skip_bytes
)
3484 sockaddr
= &conn
->local_sockaddr
;
3486 sockaddr
= &np
->np_sockaddr
;
3488 len
= sprintf(buf
, "TargetAddress="
3494 if ((len
+ payload_len
) > buffer_len
) {
3495 spin_unlock(&tpg
->tpg_np_lock
);
3496 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3501 if (skip_bytes
&& len
<= skip_bytes
) {
3504 memcpy(payload
+ payload_len
, buf
, len
);
3506 if (len
> skip_bytes
)
3510 spin_unlock(&tpg
->tpg_np_lock
);
3512 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3519 if (cmd
->cmd_flags
& ICF_SENDTARGETS_SINGLE
)
3522 spin_unlock(&tiqn_lock
);
3524 cmd
->buf_ptr
= payload
;
3530 iscsit_build_text_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3531 struct iscsi_text_rsp
*hdr
,
3532 enum iscsit_transport_type network_transport
)
3534 int text_length
, padding
;
3535 bool completed
= true;
3537 text_length
= iscsit_build_sendtargets_response(cmd
, network_transport
,
3538 cmd
->read_data_done
,
3540 if (text_length
< 0)
3544 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3546 hdr
->flags
|= ISCSI_FLAG_TEXT_CONTINUE
;
3547 cmd
->read_data_done
+= text_length
;
3548 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF)
3549 cmd
->targ_xfer_tag
= session_get_next_ttt(conn
->sess
);
3551 hdr
->opcode
= ISCSI_OP_TEXT_RSP
;
3552 padding
= ((-text_length
) & 3);
3553 hton24(hdr
->dlength
, text_length
);
3554 hdr
->itt
= cmd
->init_task_tag
;
3555 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
3556 cmd
->stat_sn
= conn
->stat_sn
++;
3557 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3559 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3561 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3562 * correctly increment MaxCmdSN for each response answering a
3563 * non immediate text request with a valid CmdSN.
3565 cmd
->maxcmdsn_inc
= 0;
3566 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3567 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3569 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3570 " Length: %u, CID: %hu F: %d C: %d\n", cmd
->init_task_tag
,
3571 cmd
->targ_xfer_tag
, cmd
->stat_sn
, text_length
, conn
->cid
,
3572 !!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
),
3573 !!(hdr
->flags
& ISCSI_FLAG_TEXT_CONTINUE
));
3575 return text_length
+ padding
;
3577 EXPORT_SYMBOL(iscsit_build_text_rsp
);
3579 static int iscsit_send_text_rsp(
3580 struct iscsi_cmd
*cmd
,
3581 struct iscsi_conn
*conn
)
3583 struct iscsi_text_rsp
*hdr
= (struct iscsi_text_rsp
*)cmd
->pdu
;
3586 int text_length
, iov_count
= 0, rc
;
3588 rc
= iscsit_build_text_rsp(cmd
, conn
, hdr
, ISCSI_TCP
);
3593 iov
= &cmd
->iov_misc
[0];
3594 iov
[iov_count
].iov_base
= cmd
->pdu
;
3595 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3596 iov
[iov_count
].iov_base
= cmd
->buf_ptr
;
3597 iov
[iov_count
++].iov_len
= text_length
;
3599 tx_size
+= (ISCSI_HDR_LEN
+ text_length
);
3601 if (conn
->conn_ops
->HeaderDigest
) {
3602 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3604 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3605 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3607 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3608 tx_size
+= ISCSI_CRC_LEN
;
3609 pr_debug("Attaching CRC32 HeaderDigest for"
3610 " Text Response PDU 0x%08x\n", *header_digest
);
3613 if (conn
->conn_ops
->DataDigest
) {
3614 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3615 cmd
->buf_ptr
, text_length
,
3616 0, NULL
, (u8
*)&cmd
->data_crc
);
3618 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3619 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3620 tx_size
+= ISCSI_CRC_LEN
;
3622 pr_debug("Attaching DataDigest for %u bytes of text"
3623 " data, CRC 0x%08x\n", text_length
,
3627 cmd
->iov_misc_count
= iov_count
;
3628 cmd
->tx_size
= tx_size
;
3634 iscsit_build_reject(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3635 struct iscsi_reject
*hdr
)
3637 hdr
->opcode
= ISCSI_OP_REJECT
;
3638 hdr
->reason
= cmd
->reject_reason
;
3639 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3640 hton24(hdr
->dlength
, ISCSI_HDR_LEN
);
3641 hdr
->ffffffff
= cpu_to_be32(0xffffffff);
3642 cmd
->stat_sn
= conn
->stat_sn
++;
3643 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3644 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3645 hdr
->max_cmdsn
= cpu_to_be32((u32
) atomic_read(&conn
->sess
->max_cmd_sn
));
3648 EXPORT_SYMBOL(iscsit_build_reject
);
3650 static int iscsit_send_reject(
3651 struct iscsi_cmd
*cmd
,
3652 struct iscsi_conn
*conn
)
3654 struct iscsi_reject
*hdr
= (struct iscsi_reject
*)&cmd
->pdu
[0];
3656 u32 iov_count
= 0, tx_size
;
3658 iscsit_build_reject(cmd
, conn
, hdr
);
3660 iov
= &cmd
->iov_misc
[0];
3661 iov
[iov_count
].iov_base
= cmd
->pdu
;
3662 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3663 iov
[iov_count
].iov_base
= cmd
->buf_ptr
;
3664 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3666 tx_size
= (ISCSI_HDR_LEN
+ ISCSI_HDR_LEN
);
3668 if (conn
->conn_ops
->HeaderDigest
) {
3669 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3671 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3672 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3674 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3675 tx_size
+= ISCSI_CRC_LEN
;
3676 pr_debug("Attaching CRC32 HeaderDigest for"
3677 " REJECT PDU 0x%08x\n", *header_digest
);
3680 if (conn
->conn_ops
->DataDigest
) {
3681 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, cmd
->buf_ptr
,
3682 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)&cmd
->data_crc
);
3684 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3685 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3686 tx_size
+= ISCSI_CRC_LEN
;
3687 pr_debug("Attaching CRC32 DataDigest for REJECT"
3688 " PDU 0x%08x\n", cmd
->data_crc
);
3691 cmd
->iov_misc_count
= iov_count
;
3692 cmd
->tx_size
= tx_size
;
3694 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3695 " CID: %hu\n", ntohl(hdr
->statsn
), hdr
->reason
, conn
->cid
);
3700 void iscsit_thread_get_cpumask(struct iscsi_conn
*conn
)
3704 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3705 * within iscsit_start_kthreads()
3707 * Here we use bitmap_id to determine which CPU that this
3708 * iSCSI connection's RX/TX threads will be scheduled to
3711 ord
= conn
->bitmap_id
% cpumask_weight(cpu_online_mask
);
3712 for_each_online_cpu(cpu
) {
3714 cpumask_set_cpu(cpu
, conn
->conn_cpumask
);
3719 * This should never be reached..
3722 cpumask_setall(conn
->conn_cpumask
);
3725 static inline void iscsit_thread_check_cpumask(
3726 struct iscsi_conn
*conn
,
3727 struct task_struct
*p
,
3731 * mode == 1 signals iscsi_target_tx_thread() usage.
3732 * mode == 0 signals iscsi_target_rx_thread() usage.
3735 if (!conn
->conn_tx_reset_cpumask
)
3737 conn
->conn_tx_reset_cpumask
= 0;
3739 if (!conn
->conn_rx_reset_cpumask
)
3741 conn
->conn_rx_reset_cpumask
= 0;
3744 * Update the CPU mask for this single kthread so that
3745 * both TX and RX kthreads are scheduled to run on the
3748 set_cpus_allowed_ptr(p
, conn
->conn_cpumask
);
3752 iscsit_immediate_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3757 case ISTATE_SEND_R2T
:
3758 ret
= iscsit_send_r2t(cmd
, conn
);
3763 spin_lock_bh(&conn
->cmd_lock
);
3764 list_del_init(&cmd
->i_conn_node
);
3765 spin_unlock_bh(&conn
->cmd_lock
);
3767 iscsit_free_cmd(cmd
, false);
3769 case ISTATE_SEND_NOPIN_WANT_RESPONSE
:
3770 iscsit_mod_nopin_response_timer(conn
);
3771 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 1);
3775 case ISTATE_SEND_NOPIN_NO_RESPONSE
:
3776 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 0);
3781 pr_err("Unknown Opcode: 0x%02x ITT:"
3782 " 0x%08x, i_state: %d on CID: %hu\n",
3783 cmd
->iscsi_opcode
, cmd
->init_task_tag
, state
,
3795 iscsit_handle_immediate_queue(struct iscsi_conn
*conn
)
3797 struct iscsit_transport
*t
= conn
->conn_transport
;
3798 struct iscsi_queue_req
*qr
;
3799 struct iscsi_cmd
*cmd
;
3803 while ((qr
= iscsit_get_cmd_from_immediate_queue(conn
))) {
3804 atomic_set(&conn
->check_immediate_queue
, 0);
3807 kmem_cache_free(lio_qr_cache
, qr
);
3809 ret
= t
->iscsit_immediate_queue(conn
, cmd
, state
);
3818 iscsit_response_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3824 case ISTATE_SEND_DATAIN
:
3825 ret
= iscsit_send_datain(cmd
, conn
);
3830 goto check_rsp_state
;
3831 else if (ret
== 1) {
3833 spin_lock_bh(&cmd
->istate_lock
);
3834 cmd
->i_state
= ISTATE_SENT_STATUS
;
3835 spin_unlock_bh(&cmd
->istate_lock
);
3837 if (atomic_read(&conn
->check_immediate_queue
))
3841 } else if (ret
== 2) {
3842 /* Still must send status,
3843 SCF_TRANSPORT_TASK_SENSE was set */
3844 spin_lock_bh(&cmd
->istate_lock
);
3845 cmd
->i_state
= ISTATE_SEND_STATUS
;
3846 spin_unlock_bh(&cmd
->istate_lock
);
3847 state
= ISTATE_SEND_STATUS
;
3848 goto check_rsp_state
;
3852 case ISTATE_SEND_STATUS
:
3853 case ISTATE_SEND_STATUS_RECOVERY
:
3854 ret
= iscsit_send_response(cmd
, conn
);
3856 case ISTATE_SEND_LOGOUTRSP
:
3857 ret
= iscsit_send_logout(cmd
, conn
);
3859 case ISTATE_SEND_ASYNCMSG
:
3860 ret
= iscsit_send_conn_drop_async_message(
3863 case ISTATE_SEND_NOPIN
:
3864 ret
= iscsit_send_nopin(cmd
, conn
);
3866 case ISTATE_SEND_REJECT
:
3867 ret
= iscsit_send_reject(cmd
, conn
);
3869 case ISTATE_SEND_TASKMGTRSP
:
3870 ret
= iscsit_send_task_mgt_rsp(cmd
, conn
);
3873 ret
= iscsit_tmr_post_handler(cmd
, conn
);
3875 iscsit_fall_back_to_erl0(conn
->sess
);
3877 case ISTATE_SEND_TEXTRSP
:
3878 ret
= iscsit_send_text_rsp(cmd
, conn
);
3881 pr_err("Unknown Opcode: 0x%02x ITT:"
3882 " 0x%08x, i_state: %d on CID: %hu\n",
3883 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3890 if (iscsit_send_tx_data(cmd
, conn
, 1) < 0) {
3891 iscsit_tx_thread_wait_for_tcp(conn
);
3892 iscsit_unmap_iovec(cmd
);
3895 iscsit_unmap_iovec(cmd
);
3898 case ISTATE_SEND_LOGOUTRSP
:
3899 if (!iscsit_logout_post_handler(cmd
, conn
))
3902 case ISTATE_SEND_STATUS
:
3903 case ISTATE_SEND_ASYNCMSG
:
3904 case ISTATE_SEND_NOPIN
:
3905 case ISTATE_SEND_STATUS_RECOVERY
:
3906 case ISTATE_SEND_TEXTRSP
:
3907 case ISTATE_SEND_TASKMGTRSP
:
3908 case ISTATE_SEND_REJECT
:
3909 spin_lock_bh(&cmd
->istate_lock
);
3910 cmd
->i_state
= ISTATE_SENT_STATUS
;
3911 spin_unlock_bh(&cmd
->istate_lock
);
3914 pr_err("Unknown Opcode: 0x%02x ITT:"
3915 " 0x%08x, i_state: %d on CID: %hu\n",
3916 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3917 cmd
->i_state
, conn
->cid
);
3921 if (atomic_read(&conn
->check_immediate_queue
))
3930 static int iscsit_handle_response_queue(struct iscsi_conn
*conn
)
3932 struct iscsit_transport
*t
= conn
->conn_transport
;
3933 struct iscsi_queue_req
*qr
;
3934 struct iscsi_cmd
*cmd
;
3938 while ((qr
= iscsit_get_cmd_from_response_queue(conn
))) {
3941 kmem_cache_free(lio_qr_cache
, qr
);
3943 ret
= t
->iscsit_response_queue(conn
, cmd
, state
);
3944 if (ret
== 1 || ret
< 0)
3951 int iscsi_target_tx_thread(void *arg
)
3954 struct iscsi_conn
*conn
= arg
;
3956 * Allow ourselves to be interrupted by SIGINT so that a
3957 * connection recovery / failure event can be triggered externally.
3959 allow_signal(SIGINT
);
3961 while (!kthread_should_stop()) {
3963 * Ensure that both TX and RX per connection kthreads
3964 * are scheduled to run on the same CPU.
3966 iscsit_thread_check_cpumask(conn
, current
, 1);
3968 wait_event_interruptible(conn
->queues_wq
,
3969 !iscsit_conn_all_queues_empty(conn
));
3971 if (signal_pending(current
))
3975 ret
= iscsit_handle_immediate_queue(conn
);
3979 ret
= iscsit_handle_response_queue(conn
);
3982 else if (ret
== -ECONNRESET
)
3990 * Avoid the normal connection failure code-path if this connection
3991 * is still within LOGIN mode, and iscsi_np process context is
3992 * responsible for cleaning up the early connection failure.
3994 if (conn
->conn_state
!= TARG_CONN_STATE_IN_LOGIN
)
3995 iscsit_take_action_for_connection_exit(conn
);
4000 static int iscsi_target_rx_opcode(struct iscsi_conn
*conn
, unsigned char *buf
)
4002 struct iscsi_hdr
*hdr
= (struct iscsi_hdr
*)buf
;
4003 struct iscsi_cmd
*cmd
;
4006 switch (hdr
->opcode
& ISCSI_OPCODE_MASK
) {
4007 case ISCSI_OP_SCSI_CMD
:
4008 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
4012 ret
= iscsit_handle_scsi_cmd(conn
, cmd
, buf
);
4014 case ISCSI_OP_SCSI_DATA_OUT
:
4015 ret
= iscsit_handle_data_out(conn
, buf
);
4017 case ISCSI_OP_NOOP_OUT
:
4019 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
4020 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
4024 ret
= iscsit_handle_nop_out(conn
, cmd
, buf
);
4026 case ISCSI_OP_SCSI_TMFUNC
:
4027 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
4031 ret
= iscsit_handle_task_mgt_cmd(conn
, cmd
, buf
);
4034 if (hdr
->ttt
!= cpu_to_be32(0xFFFFFFFF)) {
4035 cmd
= iscsit_find_cmd_from_itt(conn
, hdr
->itt
);
4039 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
4044 ret
= iscsit_handle_text_cmd(conn
, cmd
, buf
);
4046 case ISCSI_OP_LOGOUT
:
4047 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
4051 ret
= iscsit_handle_logout_cmd(conn
, cmd
, buf
);
4053 wait_for_completion_timeout(&conn
->conn_logout_comp
,
4054 SECONDS_FOR_LOGOUT_COMP
* HZ
);
4056 case ISCSI_OP_SNACK
:
4057 ret
= iscsit_handle_snack(conn
, buf
);
4060 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr
->opcode
);
4061 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
4062 pr_err("Cannot recover from unknown"
4063 " opcode while ERL=0, closing iSCSI connection.\n");
4066 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
4067 " closing iSCSI connection.\n");
4074 return iscsit_add_reject(conn
, ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
4077 static bool iscsi_target_check_conn_state(struct iscsi_conn
*conn
)
4081 spin_lock_bh(&conn
->state_lock
);
4082 ret
= (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
);
4083 spin_unlock_bh(&conn
->state_lock
);
4088 int iscsi_target_rx_thread(void *arg
)
4091 u8 buffer
[ISCSI_HDR_LEN
], opcode
;
4092 u32 checksum
= 0, digest
= 0;
4093 struct iscsi_conn
*conn
= arg
;
4096 * Allow ourselves to be interrupted by SIGINT so that a
4097 * connection recovery / failure event can be triggered externally.
4099 allow_signal(SIGINT
);
4101 * Wait for iscsi_post_login_handler() to complete before allowing
4102 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4104 rc
= wait_for_completion_interruptible(&conn
->rx_login_comp
);
4105 if (rc
< 0 || iscsi_target_check_conn_state(conn
))
4108 if (conn
->conn_transport
->transport_type
== ISCSI_INFINIBAND
) {
4109 struct completion comp
;
4111 init_completion(&comp
);
4112 rc
= wait_for_completion_interruptible(&comp
);
4119 while (!kthread_should_stop()) {
4121 * Ensure that both TX and RX per connection kthreads
4122 * are scheduled to run on the same CPU.
4124 iscsit_thread_check_cpumask(conn
, current
, 0);
4126 memset(buffer
, 0, ISCSI_HDR_LEN
);
4127 memset(&iov
, 0, sizeof(struct kvec
));
4129 iov
.iov_base
= buffer
;
4130 iov
.iov_len
= ISCSI_HDR_LEN
;
4132 ret
= rx_data(conn
, &iov
, 1, ISCSI_HDR_LEN
);
4133 if (ret
!= ISCSI_HDR_LEN
) {
4134 iscsit_rx_thread_wait_for_tcp(conn
);
4138 if (conn
->conn_ops
->HeaderDigest
) {
4139 iov
.iov_base
= &digest
;
4140 iov
.iov_len
= ISCSI_CRC_LEN
;
4142 ret
= rx_data(conn
, &iov
, 1, ISCSI_CRC_LEN
);
4143 if (ret
!= ISCSI_CRC_LEN
) {
4144 iscsit_rx_thread_wait_for_tcp(conn
);
4148 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
4149 buffer
, ISCSI_HDR_LEN
,
4150 0, NULL
, (u8
*)&checksum
);
4152 if (digest
!= checksum
) {
4153 pr_err("HeaderDigest CRC32C failed,"
4154 " received 0x%08x, computed 0x%08x\n",
4157 * Set the PDU to 0xff so it will intentionally
4158 * hit default in the switch below.
4160 memset(buffer
, 0xff, ISCSI_HDR_LEN
);
4161 atomic_long_inc(&conn
->sess
->conn_digest_errors
);
4163 pr_debug("Got HeaderDigest CRC32C"
4164 " 0x%08x\n", checksum
);
4168 if (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
)
4171 opcode
= buffer
[0] & ISCSI_OPCODE_MASK
;
4173 if (conn
->sess
->sess_ops
->SessionType
&&
4174 ((!(opcode
& ISCSI_OP_TEXT
)) ||
4175 (!(opcode
& ISCSI_OP_LOGOUT
)))) {
4176 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4177 " while in Discovery Session, rejecting.\n", opcode
);
4178 iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
4183 ret
= iscsi_target_rx_opcode(conn
, buffer
);
4189 if (!signal_pending(current
))
4190 atomic_set(&conn
->transport_failed
, 1);
4191 iscsit_take_action_for_connection_exit(conn
);
4195 static void iscsit_release_commands_from_conn(struct iscsi_conn
*conn
)
4197 struct iscsi_cmd
*cmd
= NULL
, *cmd_tmp
= NULL
;
4198 struct iscsi_session
*sess
= conn
->sess
;
4200 * We expect this function to only ever be called from either RX or TX
4201 * thread context via iscsit_close_connection() once the other context
4202 * has been reset -> returned sleeping pre-handler state.
4204 spin_lock_bh(&conn
->cmd_lock
);
4205 list_for_each_entry_safe(cmd
, cmd_tmp
, &conn
->conn_cmd_list
, i_conn_node
) {
4207 list_del_init(&cmd
->i_conn_node
);
4208 spin_unlock_bh(&conn
->cmd_lock
);
4210 iscsit_increment_maxcmdsn(cmd
, sess
);
4212 iscsit_free_cmd(cmd
, true);
4214 spin_lock_bh(&conn
->cmd_lock
);
4216 spin_unlock_bh(&conn
->cmd_lock
);
4219 static void iscsit_stop_timers_for_cmds(
4220 struct iscsi_conn
*conn
)
4222 struct iscsi_cmd
*cmd
;
4224 spin_lock_bh(&conn
->cmd_lock
);
4225 list_for_each_entry(cmd
, &conn
->conn_cmd_list
, i_conn_node
) {
4226 if (cmd
->data_direction
== DMA_TO_DEVICE
)
4227 iscsit_stop_dataout_timer(cmd
);
4229 spin_unlock_bh(&conn
->cmd_lock
);
4232 int iscsit_close_connection(
4233 struct iscsi_conn
*conn
)
4235 int conn_logout
= (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
);
4236 struct iscsi_session
*sess
= conn
->sess
;
4238 pr_debug("Closing iSCSI connection CID %hu on SID:"
4239 " %u\n", conn
->cid
, sess
->sid
);
4241 * Always up conn_logout_comp for the traditional TCP case just in case
4242 * the RX Thread in iscsi_target_rx_opcode() is sleeping and the logout
4243 * response never got sent because the connection failed.
4245 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4246 * to signal logout response TX interrupt completion. Go ahead and skip
4247 * this for iser since isert_rx_opcode() does not wait on logout failure,
4248 * and to avoid iscsi_conn pointer dereference in iser-target code.
4250 if (conn
->conn_transport
->transport_type
== ISCSI_TCP
)
4251 complete(&conn
->conn_logout_comp
);
4253 if (!strcmp(current
->comm
, ISCSI_RX_THREAD_NAME
)) {
4254 if (conn
->tx_thread
&&
4255 cmpxchg(&conn
->tx_thread_active
, true, false)) {
4256 send_sig(SIGINT
, conn
->tx_thread
, 1);
4257 kthread_stop(conn
->tx_thread
);
4259 } else if (!strcmp(current
->comm
, ISCSI_TX_THREAD_NAME
)) {
4260 if (conn
->rx_thread
&&
4261 cmpxchg(&conn
->rx_thread_active
, true, false)) {
4262 send_sig(SIGINT
, conn
->rx_thread
, 1);
4263 kthread_stop(conn
->rx_thread
);
4267 spin_lock(&iscsit_global
->ts_bitmap_lock
);
4268 bitmap_release_region(iscsit_global
->ts_bitmap
, conn
->bitmap_id
,
4270 spin_unlock(&iscsit_global
->ts_bitmap_lock
);
4272 iscsit_stop_timers_for_cmds(conn
);
4273 iscsit_stop_nopin_response_timer(conn
);
4274 iscsit_stop_nopin_timer(conn
);
4276 if (conn
->conn_transport
->iscsit_wait_conn
)
4277 conn
->conn_transport
->iscsit_wait_conn(conn
);
4280 * During Connection recovery drop unacknowledged out of order
4281 * commands for this connection, and prepare the other commands
4284 * During normal operation clear the out of order commands (but
4285 * do not free the struct iscsi_ooo_cmdsn's) and release all
4286 * struct iscsi_cmds.
4288 if (atomic_read(&conn
->connection_recovery
)) {
4289 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn
);
4290 iscsit_prepare_cmds_for_realligance(conn
);
4292 iscsit_clear_ooo_cmdsns_for_conn(conn
);
4293 iscsit_release_commands_from_conn(conn
);
4295 iscsit_free_queue_reqs_for_conn(conn
);
4298 * Handle decrementing session or connection usage count if
4299 * a logout response was not able to be sent because the
4300 * connection failed. Fall back to Session Recovery here.
4302 if (atomic_read(&conn
->conn_logout_remove
)) {
4303 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) {
4304 iscsit_dec_conn_usage_count(conn
);
4305 iscsit_dec_session_usage_count(sess
);
4307 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
)
4308 iscsit_dec_conn_usage_count(conn
);
4310 atomic_set(&conn
->conn_logout_remove
, 0);
4311 atomic_set(&sess
->session_reinstatement
, 0);
4312 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4315 spin_lock_bh(&sess
->conn_lock
);
4316 list_del(&conn
->conn_list
);
4319 * Attempt to let the Initiator know this connection failed by
4320 * sending an Connection Dropped Async Message on another
4321 * active connection.
4323 if (atomic_read(&conn
->connection_recovery
))
4324 iscsit_build_conn_drop_async_message(conn
);
4326 spin_unlock_bh(&sess
->conn_lock
);
4329 * If connection reinstatement is being performed on this connection,
4330 * up the connection reinstatement semaphore that is being blocked on
4331 * in iscsit_cause_connection_reinstatement().
4333 spin_lock_bh(&conn
->state_lock
);
4334 if (atomic_read(&conn
->sleep_on_conn_wait_comp
)) {
4335 spin_unlock_bh(&conn
->state_lock
);
4336 complete(&conn
->conn_wait_comp
);
4337 wait_for_completion(&conn
->conn_post_wait_comp
);
4338 spin_lock_bh(&conn
->state_lock
);
4342 * If connection reinstatement is being performed on this connection
4343 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4344 * connection wait rcfr semaphore that is being blocked on
4345 * an iscsit_connection_reinstatement_rcfr().
4347 if (atomic_read(&conn
->connection_wait_rcfr
)) {
4348 spin_unlock_bh(&conn
->state_lock
);
4349 complete(&conn
->conn_wait_rcfr_comp
);
4350 wait_for_completion(&conn
->conn_post_wait_comp
);
4351 spin_lock_bh(&conn
->state_lock
);
4353 atomic_set(&conn
->connection_reinstatement
, 1);
4354 spin_unlock_bh(&conn
->state_lock
);
4357 * If any other processes are accessing this connection pointer we
4358 * must wait until they have completed.
4360 iscsit_check_conn_usage_count(conn
);
4362 if (conn
->conn_rx_hash
.tfm
)
4363 crypto_free_hash(conn
->conn_rx_hash
.tfm
);
4364 if (conn
->conn_tx_hash
.tfm
)
4365 crypto_free_hash(conn
->conn_tx_hash
.tfm
);
4367 free_cpumask_var(conn
->conn_cpumask
);
4369 kfree(conn
->conn_ops
);
4370 conn
->conn_ops
= NULL
;
4373 sock_release(conn
->sock
);
4375 if (conn
->conn_transport
->iscsit_free_conn
)
4376 conn
->conn_transport
->iscsit_free_conn(conn
);
4378 iscsit_put_transport(conn
->conn_transport
);
4380 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4381 conn
->conn_state
= TARG_CONN_STATE_FREE
;
4384 spin_lock_bh(&sess
->conn_lock
);
4385 atomic_dec(&sess
->nconn
);
4386 pr_debug("Decremented iSCSI connection count to %hu from node:"
4387 " %s\n", atomic_read(&sess
->nconn
),
4388 sess
->sess_ops
->InitiatorName
);
4390 * Make sure that if one connection fails in an non ERL=2 iSCSI
4391 * Session that they all fail.
4393 if ((sess
->sess_ops
->ErrorRecoveryLevel
!= 2) && !conn_logout
&&
4394 !atomic_read(&sess
->session_logout
))
4395 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4398 * If this was not the last connection in the session, and we are
4399 * performing session reinstatement or falling back to ERL=0, call
4400 * iscsit_stop_session() without sleeping to shutdown the other
4401 * active connections.
4403 if (atomic_read(&sess
->nconn
)) {
4404 if (!atomic_read(&sess
->session_reinstatement
) &&
4405 !atomic_read(&sess
->session_fall_back_to_erl0
)) {
4406 spin_unlock_bh(&sess
->conn_lock
);
4409 if (!atomic_read(&sess
->session_stop_active
)) {
4410 atomic_set(&sess
->session_stop_active
, 1);
4411 spin_unlock_bh(&sess
->conn_lock
);
4412 iscsit_stop_session(sess
, 0, 0);
4415 spin_unlock_bh(&sess
->conn_lock
);
4420 * If this was the last connection in the session and one of the
4421 * following is occurring:
4423 * Session Reinstatement is not being performed, and are falling back
4424 * to ERL=0 call iscsit_close_session().
4426 * Session Logout was requested. iscsit_close_session() will be called
4429 * Session Continuation is not being performed, start the Time2Retain
4430 * handler and check if sleep_on_sess_wait_sem is active.
4432 if (!atomic_read(&sess
->session_reinstatement
) &&
4433 atomic_read(&sess
->session_fall_back_to_erl0
)) {
4434 spin_unlock_bh(&sess
->conn_lock
);
4435 target_put_session(sess
->se_sess
);
4438 } else if (atomic_read(&sess
->session_logout
)) {
4439 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4440 sess
->session_state
= TARG_SESS_STATE_FREE
;
4441 spin_unlock_bh(&sess
->conn_lock
);
4443 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4444 complete(&sess
->session_wait_comp
);
4448 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4449 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4451 if (!atomic_read(&sess
->session_continuation
)) {
4452 spin_unlock_bh(&sess
->conn_lock
);
4453 iscsit_start_time2retain_handler(sess
);
4455 spin_unlock_bh(&sess
->conn_lock
);
4457 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4458 complete(&sess
->session_wait_comp
);
4462 spin_unlock_bh(&sess
->conn_lock
);
4467 int iscsit_close_session(struct iscsi_session
*sess
)
4469 struct iscsi_portal_group
*tpg
= sess
->tpg
;
4470 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4472 if (atomic_read(&sess
->nconn
)) {
4473 pr_err("%d connection(s) still exist for iSCSI session"
4474 " to %s\n", atomic_read(&sess
->nconn
),
4475 sess
->sess_ops
->InitiatorName
);
4479 spin_lock_bh(&se_tpg
->session_lock
);
4480 atomic_set(&sess
->session_logout
, 1);
4481 atomic_set(&sess
->session_reinstatement
, 1);
4482 iscsit_stop_time2retain_timer(sess
);
4483 spin_unlock_bh(&se_tpg
->session_lock
);
4486 * transport_deregister_session_configfs() will clear the
4487 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4488 * can be setting it again with __transport_register_session() in
4489 * iscsi_post_login_handler() again after the iscsit_stop_session()
4490 * completes in iscsi_np context.
4492 transport_deregister_session_configfs(sess
->se_sess
);
4495 * If any other processes are accessing this session pointer we must
4496 * wait until they have completed. If we are in an interrupt (the
4497 * time2retain handler) and contain and active session usage count we
4498 * restart the timer and exit.
4500 if (!in_interrupt()) {
4501 if (iscsit_check_session_usage_count(sess
) == 1)
4502 iscsit_stop_session(sess
, 1, 1);
4504 if (iscsit_check_session_usage_count(sess
) == 2) {
4505 atomic_set(&sess
->session_logout
, 0);
4506 iscsit_start_time2retain_handler(sess
);
4511 transport_deregister_session(sess
->se_sess
);
4513 if (sess
->sess_ops
->ErrorRecoveryLevel
== 2)
4514 iscsit_free_connection_recovery_entires(sess
);
4516 iscsit_free_all_ooo_cmdsns(sess
);
4518 spin_lock_bh(&se_tpg
->session_lock
);
4519 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4520 sess
->session_state
= TARG_SESS_STATE_FREE
;
4521 pr_debug("Released iSCSI session from node: %s\n",
4522 sess
->sess_ops
->InitiatorName
);
4525 tpg
->tpg_tiqn
->tiqn_nsessions
--;
4527 pr_debug("Decremented number of active iSCSI Sessions on"
4528 " iSCSI TPG: %hu to %u\n", tpg
->tpgt
, tpg
->nsessions
);
4530 spin_lock(&sess_idr_lock
);
4531 idr_remove(&sess_idr
, sess
->session_index
);
4532 spin_unlock(&sess_idr_lock
);
4534 kfree(sess
->sess_ops
);
4535 sess
->sess_ops
= NULL
;
4536 spin_unlock_bh(&se_tpg
->session_lock
);
4542 static void iscsit_logout_post_handler_closesession(
4543 struct iscsi_conn
*conn
)
4545 struct iscsi_session
*sess
= conn
->sess
;
4548 * Traditional iscsi/tcp will invoke this logic from TX thread
4549 * context during session logout, so clear tx_thread_active and
4550 * sleep if iscsit_close_connection() has not already occured.
4552 * Since iser-target invokes this logic from it's own workqueue,
4553 * always sleep waiting for RX/TX thread shutdown to complete
4554 * within iscsit_close_connection().
4556 if (conn
->conn_transport
->transport_type
== ISCSI_TCP
)
4557 sleep
= cmpxchg(&conn
->tx_thread_active
, true, false);
4559 atomic_set(&conn
->conn_logout_remove
, 0);
4560 complete(&conn
->conn_logout_comp
);
4562 iscsit_dec_conn_usage_count(conn
);
4563 iscsit_stop_session(sess
, sleep
, sleep
);
4564 iscsit_dec_session_usage_count(sess
);
4565 target_put_session(sess
->se_sess
);
4568 static void iscsit_logout_post_handler_samecid(
4569 struct iscsi_conn
*conn
)
4573 if (conn
->conn_transport
->transport_type
== ISCSI_TCP
)
4574 sleep
= cmpxchg(&conn
->tx_thread_active
, true, false);
4576 atomic_set(&conn
->conn_logout_remove
, 0);
4577 complete(&conn
->conn_logout_comp
);
4579 iscsit_cause_connection_reinstatement(conn
, sleep
);
4580 iscsit_dec_conn_usage_count(conn
);
4583 static void iscsit_logout_post_handler_diffcid(
4584 struct iscsi_conn
*conn
,
4587 struct iscsi_conn
*l_conn
;
4588 struct iscsi_session
*sess
= conn
->sess
;
4589 bool conn_found
= false;
4594 spin_lock_bh(&sess
->conn_lock
);
4595 list_for_each_entry(l_conn
, &sess
->sess_conn_list
, conn_list
) {
4596 if (l_conn
->cid
== cid
) {
4597 iscsit_inc_conn_usage_count(l_conn
);
4602 spin_unlock_bh(&sess
->conn_lock
);
4608 l_conn
->sock
->ops
->shutdown(l_conn
->sock
, RCV_SHUTDOWN
);
4610 spin_lock_bh(&l_conn
->state_lock
);
4611 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4612 l_conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
4613 spin_unlock_bh(&l_conn
->state_lock
);
4615 iscsit_cause_connection_reinstatement(l_conn
, 1);
4616 iscsit_dec_conn_usage_count(l_conn
);
4620 * Return of 0 causes the TX thread to restart.
4622 int iscsit_logout_post_handler(
4623 struct iscsi_cmd
*cmd
,
4624 struct iscsi_conn
*conn
)
4628 switch (cmd
->logout_reason
) {
4629 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
4630 switch (cmd
->logout_response
) {
4631 case ISCSI_LOGOUT_SUCCESS
:
4632 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4634 iscsit_logout_post_handler_closesession(conn
);
4639 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
4640 if (conn
->cid
== cmd
->logout_cid
) {
4641 switch (cmd
->logout_response
) {
4642 case ISCSI_LOGOUT_SUCCESS
:
4643 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4645 iscsit_logout_post_handler_samecid(conn
);
4650 switch (cmd
->logout_response
) {
4651 case ISCSI_LOGOUT_SUCCESS
:
4652 iscsit_logout_post_handler_diffcid(conn
,
4655 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4656 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4663 case ISCSI_LOGOUT_REASON_RECOVERY
:
4664 switch (cmd
->logout_response
) {
4665 case ISCSI_LOGOUT_SUCCESS
:
4666 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4667 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
:
4668 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4680 EXPORT_SYMBOL(iscsit_logout_post_handler
);
4682 void iscsit_fail_session(struct iscsi_session
*sess
)
4684 struct iscsi_conn
*conn
;
4686 spin_lock_bh(&sess
->conn_lock
);
4687 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
) {
4688 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4689 conn
->conn_state
= TARG_CONN_STATE_CLEANUP_WAIT
;
4691 spin_unlock_bh(&sess
->conn_lock
);
4693 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4694 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4697 int iscsit_free_session(struct iscsi_session
*sess
)
4699 u16 conn_count
= atomic_read(&sess
->nconn
);
4700 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4703 spin_lock_bh(&sess
->conn_lock
);
4704 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4706 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4708 if (conn_count
== 0)
4711 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4714 iscsit_inc_conn_usage_count(conn_tmp
);
4717 iscsit_inc_conn_usage_count(conn
);
4719 spin_unlock_bh(&sess
->conn_lock
);
4720 iscsit_cause_connection_reinstatement(conn
, 1);
4721 spin_lock_bh(&sess
->conn_lock
);
4723 iscsit_dec_conn_usage_count(conn
);
4725 iscsit_dec_conn_usage_count(conn_tmp
);
4730 if (atomic_read(&sess
->nconn
)) {
4731 spin_unlock_bh(&sess
->conn_lock
);
4732 wait_for_completion(&sess
->session_wait_comp
);
4734 spin_unlock_bh(&sess
->conn_lock
);
4736 target_put_session(sess
->se_sess
);
4740 void iscsit_stop_session(
4741 struct iscsi_session
*sess
,
4743 int connection_sleep
)
4745 u16 conn_count
= atomic_read(&sess
->nconn
);
4746 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4749 spin_lock_bh(&sess
->conn_lock
);
4751 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4753 if (connection_sleep
) {
4754 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4756 if (conn_count
== 0)
4759 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4762 iscsit_inc_conn_usage_count(conn_tmp
);
4765 iscsit_inc_conn_usage_count(conn
);
4767 spin_unlock_bh(&sess
->conn_lock
);
4768 iscsit_cause_connection_reinstatement(conn
, 1);
4769 spin_lock_bh(&sess
->conn_lock
);
4771 iscsit_dec_conn_usage_count(conn
);
4773 iscsit_dec_conn_usage_count(conn_tmp
);
4777 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
)
4778 iscsit_cause_connection_reinstatement(conn
, 0);
4781 if (session_sleep
&& atomic_read(&sess
->nconn
)) {
4782 spin_unlock_bh(&sess
->conn_lock
);
4783 wait_for_completion(&sess
->session_wait_comp
);
4785 spin_unlock_bh(&sess
->conn_lock
);
4788 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group
*tpg
, int force
)
4790 struct iscsi_session
*sess
;
4791 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4792 struct se_session
*se_sess
, *se_sess_tmp
;
4793 LIST_HEAD(free_list
);
4794 int session_count
= 0;
4796 spin_lock_bh(&se_tpg
->session_lock
);
4797 if (tpg
->nsessions
&& !force
) {
4798 spin_unlock_bh(&se_tpg
->session_lock
);
4802 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &se_tpg
->tpg_sess_list
,
4804 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4806 spin_lock(&sess
->conn_lock
);
4807 if (atomic_read(&sess
->session_fall_back_to_erl0
) ||
4808 atomic_read(&sess
->session_logout
) ||
4809 (sess
->time2retain_timer_flags
& ISCSI_TF_EXPIRED
)) {
4810 spin_unlock(&sess
->conn_lock
);
4813 atomic_set(&sess
->session_reinstatement
, 1);
4814 spin_unlock(&sess
->conn_lock
);
4816 list_move_tail(&se_sess
->sess_list
, &free_list
);
4818 spin_unlock_bh(&se_tpg
->session_lock
);
4820 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &free_list
, sess_list
) {
4821 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4823 iscsit_free_session(sess
);
4827 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4828 " Group: %hu\n", session_count
, tpg
->tpgt
);
4832 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4833 MODULE_VERSION("4.1.x");
4834 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4835 MODULE_LICENSE("GPL");
4837 module_init(iscsi_target_init_module
);
4838 module_exit(iscsi_target_cleanup_module
);