1 /*******************************************************************************
2 * This file contains main functions related to the iSCSI Target Core Driver.
4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 ******************************************************************************/
21 #include <linux/string.h>
22 #include <linux/kthread.h>
23 #include <linux/crypto.h>
24 #include <linux/completion.h>
25 #include <asm/unaligned.h>
26 #include <scsi/scsi_device.h>
27 #include <scsi/iscsi_proto.h>
28 #include <target/target_core_base.h>
29 #include <target/target_core_tmr.h>
30 #include <target/target_core_transport.h>
32 #include "iscsi_target_core.h"
33 #include "iscsi_target_parameters.h"
34 #include "iscsi_target_seq_pdu_list.h"
35 #include "iscsi_target_tq.h"
36 #include "iscsi_target_configfs.h"
37 #include "iscsi_target_datain_values.h"
38 #include "iscsi_target_erl0.h"
39 #include "iscsi_target_erl1.h"
40 #include "iscsi_target_erl2.h"
41 #include "iscsi_target_login.h"
42 #include "iscsi_target_tmr.h"
43 #include "iscsi_target_tpg.h"
44 #include "iscsi_target_util.h"
45 #include "iscsi_target.h"
46 #include "iscsi_target_device.h"
47 #include "iscsi_target_stat.h"
49 static LIST_HEAD(g_tiqn_list
);
50 static LIST_HEAD(g_np_list
);
51 static DEFINE_SPINLOCK(tiqn_lock
);
52 static DEFINE_SPINLOCK(np_lock
);
54 static struct idr tiqn_idr
;
56 struct mutex auth_id_lock
;
57 spinlock_t sess_idr_lock
;
59 struct iscsit_global
*iscsit_global
;
61 struct kmem_cache
*lio_cmd_cache
;
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 unsigned char *buf
, u32
);
69 static int iscsit_logout_post_handler(struct iscsi_cmd
*, struct iscsi_conn
*);
71 struct iscsi_tiqn
*iscsit_get_tiqn_for_login(unsigned char *buf
)
73 struct iscsi_tiqn
*tiqn
= NULL
;
75 spin_lock(&tiqn_lock
);
76 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
77 if (!strcmp(tiqn
->tiqn
, buf
)) {
79 spin_lock(&tiqn
->tiqn_state_lock
);
80 if (tiqn
->tiqn_state
== TIQN_STATE_ACTIVE
) {
81 tiqn
->tiqn_access_count
++;
82 spin_unlock(&tiqn
->tiqn_state_lock
);
83 spin_unlock(&tiqn_lock
);
86 spin_unlock(&tiqn
->tiqn_state_lock
);
89 spin_unlock(&tiqn_lock
);
94 static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn
*tiqn
)
96 spin_lock(&tiqn
->tiqn_state_lock
);
97 if (tiqn
->tiqn_state
== TIQN_STATE_ACTIVE
) {
98 tiqn
->tiqn_state
= TIQN_STATE_SHUTDOWN
;
99 spin_unlock(&tiqn
->tiqn_state_lock
);
102 spin_unlock(&tiqn
->tiqn_state_lock
);
107 void iscsit_put_tiqn_for_login(struct iscsi_tiqn
*tiqn
)
109 spin_lock(&tiqn
->tiqn_state_lock
);
110 tiqn
->tiqn_access_count
--;
111 spin_unlock(&tiqn
->tiqn_state_lock
);
115 * Note that IQN formatting is expected to be done in userspace, and
116 * no explict IQN format checks are done here.
118 struct iscsi_tiqn
*iscsit_add_tiqn(unsigned char *buf
)
120 struct iscsi_tiqn
*tiqn
= NULL
;
123 if (strlen(buf
) >= ISCSI_IQN_LEN
) {
124 pr_err("Target IQN exceeds %d bytes\n",
126 return ERR_PTR(-EINVAL
);
129 tiqn
= kzalloc(sizeof(struct iscsi_tiqn
), GFP_KERNEL
);
131 pr_err("Unable to allocate struct iscsi_tiqn\n");
132 return ERR_PTR(-ENOMEM
);
135 sprintf(tiqn
->tiqn
, "%s", buf
);
136 INIT_LIST_HEAD(&tiqn
->tiqn_list
);
137 INIT_LIST_HEAD(&tiqn
->tiqn_tpg_list
);
138 spin_lock_init(&tiqn
->tiqn_state_lock
);
139 spin_lock_init(&tiqn
->tiqn_tpg_lock
);
140 spin_lock_init(&tiqn
->sess_err_stats
.lock
);
141 spin_lock_init(&tiqn
->login_stats
.lock
);
142 spin_lock_init(&tiqn
->logout_stats
.lock
);
144 if (!idr_pre_get(&tiqn_idr
, GFP_KERNEL
)) {
145 pr_err("idr_pre_get() for tiqn_idr failed\n");
147 return ERR_PTR(-ENOMEM
);
149 tiqn
->tiqn_state
= TIQN_STATE_ACTIVE
;
151 spin_lock(&tiqn_lock
);
152 ret
= idr_get_new(&tiqn_idr
, NULL
, &tiqn
->tiqn_index
);
154 pr_err("idr_get_new() failed for tiqn->tiqn_index\n");
155 spin_unlock(&tiqn_lock
);
159 list_add_tail(&tiqn
->tiqn_list
, &g_tiqn_list
);
160 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 if (np
->np_login_tpg
) {
219 pr_err("np->np_login_tpg() is not NULL!\n");
220 spin_unlock_bh(&np
->np_thread_lock
);
223 spin_unlock_bh(&np
->np_thread_lock
);
225 * Determine if the portal group is accepting storage traffic.
227 spin_lock_bh(&tpg
->tpg_state_lock
);
228 if (tpg
->tpg_state
!= TPG_STATE_ACTIVE
) {
229 spin_unlock_bh(&tpg
->tpg_state_lock
);
232 spin_unlock_bh(&tpg
->tpg_state_lock
);
235 * Here we serialize access across the TIQN+TPG Tuple.
237 ret
= mutex_lock_interruptible(&tpg
->np_login_lock
);
238 if ((ret
!= 0) || signal_pending(current
))
241 spin_lock_bh(&np
->np_thread_lock
);
242 np
->np_login_tpg
= tpg
;
243 spin_unlock_bh(&np
->np_thread_lock
);
248 int iscsit_deaccess_np(struct iscsi_np
*np
, struct iscsi_portal_group
*tpg
)
250 struct iscsi_tiqn
*tiqn
= tpg
->tpg_tiqn
;
252 spin_lock_bh(&np
->np_thread_lock
);
253 np
->np_login_tpg
= NULL
;
254 spin_unlock_bh(&np
->np_thread_lock
);
256 mutex_unlock(&tpg
->np_login_lock
);
259 iscsit_put_tiqn_for_login(tiqn
);
264 static struct iscsi_np
*iscsit_get_np(
265 struct __kernel_sockaddr_storage
*sockaddr
,
266 int network_transport
)
268 struct sockaddr_in
*sock_in
, *sock_in_e
;
269 struct sockaddr_in6
*sock_in6
, *sock_in6_e
;
274 spin_lock_bh(&np_lock
);
275 list_for_each_entry(np
, &g_np_list
, np_list
) {
276 spin_lock(&np
->np_thread_lock
);
277 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
278 spin_unlock(&np
->np_thread_lock
);
282 if (sockaddr
->ss_family
== AF_INET6
) {
283 sock_in6
= (struct sockaddr_in6
*)sockaddr
;
284 sock_in6_e
= (struct sockaddr_in6
*)&np
->np_sockaddr
;
286 if (!memcmp((void *)&sock_in6
->sin6_addr
.in6_u
,
287 (void *)&sock_in6_e
->sin6_addr
.in6_u
,
288 sizeof(struct in6_addr
)))
291 port
= ntohs(sock_in6
->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
==
297 sock_in_e
->sin_addr
.s_addr
)
300 port
= ntohs(sock_in
->sin_port
);
303 if ((ip_match
== 1) && (np
->np_port
== port
) &&
304 (np
->np_network_transport
== network_transport
)) {
306 * Increment the np_exports reference count now to
307 * prevent iscsit_del_np() below from being called
308 * while iscsi_tpg_add_network_portal() is called.
311 spin_unlock(&np
->np_thread_lock
);
312 spin_unlock_bh(&np_lock
);
315 spin_unlock(&np
->np_thread_lock
);
317 spin_unlock_bh(&np_lock
);
322 struct iscsi_np
*iscsit_add_np(
323 struct __kernel_sockaddr_storage
*sockaddr
,
325 int network_transport
)
327 struct sockaddr_in
*sock_in
;
328 struct sockaddr_in6
*sock_in6
;
332 * Locate the existing struct iscsi_np if already active..
334 np
= iscsit_get_np(sockaddr
, network_transport
);
338 np
= kzalloc(sizeof(struct iscsi_np
), GFP_KERNEL
);
340 pr_err("Unable to allocate memory for struct iscsi_np\n");
341 return ERR_PTR(-ENOMEM
);
344 np
->np_flags
|= NPF_IP_NETWORK
;
345 if (sockaddr
->ss_family
== AF_INET6
) {
346 sock_in6
= (struct sockaddr_in6
*)sockaddr
;
347 snprintf(np
->np_ip
, IPV6_ADDRESS_SPACE
, "%s", ip_str
);
348 np
->np_port
= ntohs(sock_in6
->sin6_port
);
350 sock_in
= (struct sockaddr_in
*)sockaddr
;
351 sprintf(np
->np_ip
, "%s", ip_str
);
352 np
->np_port
= ntohs(sock_in
->sin_port
);
355 np
->np_network_transport
= network_transport
;
356 spin_lock_init(&np
->np_thread_lock
);
357 init_completion(&np
->np_restart_comp
);
358 INIT_LIST_HEAD(&np
->np_list
);
360 ret
= iscsi_target_setup_login_socket(np
, sockaddr
);
366 np
->np_thread
= kthread_run(iscsi_target_login_thread
, np
, "iscsi_np");
367 if (IS_ERR(np
->np_thread
)) {
368 pr_err("Unable to create kthread: iscsi_np\n");
369 ret
= PTR_ERR(np
->np_thread
);
374 * Increment the np_exports reference count now to prevent
375 * iscsit_del_np() below from being run while a new call to
376 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
377 * active. We don't need to hold np->np_thread_lock at this
378 * point because iscsi_np has not been added to g_np_list yet.
382 spin_lock_bh(&np_lock
);
383 list_add_tail(&np
->np_list
, &g_np_list
);
384 spin_unlock_bh(&np_lock
);
386 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
387 np
->np_ip
, np
->np_port
, (np
->np_network_transport
== ISCSI_TCP
) ?
393 int iscsit_reset_np_thread(
395 struct iscsi_tpg_np
*tpg_np
,
396 struct iscsi_portal_group
*tpg
)
398 spin_lock_bh(&np
->np_thread_lock
);
401 * The reset operation need only be performed when the
402 * passed struct iscsi_portal_group has a login in progress
403 * to one of the network portals.
405 if (tpg_np
->tpg_np
->np_login_tpg
!= tpg
) {
406 spin_unlock_bh(&np
->np_thread_lock
);
410 if (np
->np_thread_state
== ISCSI_NP_THREAD_INACTIVE
) {
411 spin_unlock_bh(&np
->np_thread_lock
);
414 np
->np_thread_state
= ISCSI_NP_THREAD_RESET
;
417 spin_unlock_bh(&np
->np_thread_lock
);
418 send_sig(SIGINT
, np
->np_thread
, 1);
419 wait_for_completion(&np
->np_restart_comp
);
420 spin_lock_bh(&np
->np_thread_lock
);
422 spin_unlock_bh(&np
->np_thread_lock
);
427 int iscsit_del_np_comm(struct iscsi_np
*np
)
433 * Some network transports allocate their own struct sock->file,
434 * see if we need to free any additional allocated resources.
436 if (np
->np_flags
& NPF_SCTP_STRUCT_FILE
) {
437 kfree(np
->np_socket
->file
);
438 np
->np_socket
->file
= NULL
;
441 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
) {
450 spin_unlock_bh(&np
->np_thread_lock
);
453 np
->np_thread_state
= ISCSI_NP_THREAD_SHUTDOWN
;
454 spin_unlock_bh(&np
->np_thread_lock
);
458 * We need to send the signal to wakeup Linux/Net
459 * which may be sleeping in sock_accept()..
461 send_sig(SIGINT
, np
->np_thread
, 1);
462 kthread_stop(np
->np_thread
);
464 iscsit_del_np_comm(np
);
466 spin_lock_bh(&np_lock
);
467 list_del(&np
->np_list
);
468 spin_unlock_bh(&np_lock
);
470 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
471 np
->np_ip
, np
->np_port
, (np
->np_network_transport
== ISCSI_TCP
) ?
478 static int __init
iscsi_target_init_module(void)
482 pr_debug("iSCSI-Target "ISCSIT_VERSION
"\n");
484 iscsit_global
= kzalloc(sizeof(struct iscsit_global
), GFP_KERNEL
);
485 if (!iscsit_global
) {
486 pr_err("Unable to allocate memory for iscsit_global\n");
489 mutex_init(&auth_id_lock
);
490 spin_lock_init(&sess_idr_lock
);
494 ret
= iscsi_target_register_configfs();
498 ret
= iscsi_thread_set_init();
502 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT
) !=
503 TARGET_THREAD_SET_COUNT
) {
504 pr_err("iscsi_allocate_thread_sets() returned"
505 " unexpected value!\n");
509 lio_cmd_cache
= kmem_cache_create("lio_cmd_cache",
510 sizeof(struct iscsi_cmd
), __alignof__(struct iscsi_cmd
),
512 if (!lio_cmd_cache
) {
513 pr_err("Unable to kmem_cache_create() for"
518 lio_qr_cache
= kmem_cache_create("lio_qr_cache",
519 sizeof(struct iscsi_queue_req
),
520 __alignof__(struct iscsi_queue_req
), 0, NULL
);
522 pr_err("nable to kmem_cache_create() for"
527 lio_dr_cache
= kmem_cache_create("lio_dr_cache",
528 sizeof(struct iscsi_datain_req
),
529 __alignof__(struct iscsi_datain_req
), 0, NULL
);
531 pr_err("Unable to kmem_cache_create() for"
536 lio_ooo_cache
= kmem_cache_create("lio_ooo_cache",
537 sizeof(struct iscsi_ooo_cmdsn
),
538 __alignof__(struct iscsi_ooo_cmdsn
), 0, NULL
);
539 if (!lio_ooo_cache
) {
540 pr_err("Unable to kmem_cache_create() for"
545 lio_r2t_cache
= kmem_cache_create("lio_r2t_cache",
546 sizeof(struct iscsi_r2t
), __alignof__(struct iscsi_r2t
),
548 if (!lio_r2t_cache
) {
549 pr_err("Unable to kmem_cache_create() for"
554 if (iscsit_load_discovery_tpg() < 0)
559 kmem_cache_destroy(lio_r2t_cache
);
561 kmem_cache_destroy(lio_ooo_cache
);
563 kmem_cache_destroy(lio_dr_cache
);
565 kmem_cache_destroy(lio_qr_cache
);
567 kmem_cache_destroy(lio_cmd_cache
);
569 iscsi_deallocate_thread_sets();
571 iscsi_thread_set_free();
573 iscsi_target_deregister_configfs();
575 kfree(iscsit_global
);
579 static void __exit
iscsi_target_cleanup_module(void)
581 iscsi_deallocate_thread_sets();
582 iscsi_thread_set_free();
583 iscsit_release_discovery_tpg();
584 kmem_cache_destroy(lio_cmd_cache
);
585 kmem_cache_destroy(lio_qr_cache
);
586 kmem_cache_destroy(lio_dr_cache
);
587 kmem_cache_destroy(lio_ooo_cache
);
588 kmem_cache_destroy(lio_r2t_cache
);
590 iscsi_target_deregister_configfs();
592 kfree(iscsit_global
);
595 int iscsit_add_reject(
599 struct iscsi_conn
*conn
)
601 struct iscsi_cmd
*cmd
;
602 struct iscsi_reject
*hdr
;
605 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
609 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
611 cmd
->cmd_flags
|= ICF_REJECT_FAIL_CONN
;
613 hdr
= (struct iscsi_reject
*) cmd
->pdu
;
614 hdr
->reason
= reason
;
616 cmd
->buf_ptr
= kzalloc(ISCSI_HDR_LEN
, GFP_KERNEL
);
618 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
619 iscsit_release_cmd(cmd
);
622 memcpy(cmd
->buf_ptr
, buf
, ISCSI_HDR_LEN
);
624 spin_lock_bh(&conn
->cmd_lock
);
625 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
626 spin_unlock_bh(&conn
->cmd_lock
);
628 cmd
->i_state
= ISTATE_SEND_REJECT
;
629 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
631 ret
= wait_for_completion_interruptible(&cmd
->reject_comp
);
635 return (!fail_conn
) ? 0 : -1;
638 int iscsit_add_reject_from_cmd(
643 struct iscsi_cmd
*cmd
)
645 struct iscsi_conn
*conn
;
646 struct iscsi_reject
*hdr
;
650 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
656 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
658 cmd
->cmd_flags
|= ICF_REJECT_FAIL_CONN
;
660 hdr
= (struct iscsi_reject
*) cmd
->pdu
;
661 hdr
->reason
= reason
;
663 cmd
->buf_ptr
= kzalloc(ISCSI_HDR_LEN
, GFP_KERNEL
);
665 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
666 iscsit_release_cmd(cmd
);
669 memcpy(cmd
->buf_ptr
, buf
, ISCSI_HDR_LEN
);
672 spin_lock_bh(&conn
->cmd_lock
);
673 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
674 spin_unlock_bh(&conn
->cmd_lock
);
677 cmd
->i_state
= ISTATE_SEND_REJECT
;
678 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
680 ret
= wait_for_completion_interruptible(&cmd
->reject_comp
);
684 return (!fail_conn
) ? 0 : -1;
688 * Map some portion of the allocated scatterlist to an iovec, suitable for
689 * kernel sockets to copy data in/out. This handles both pages and slab-allocated
690 * buffers, since we have been tricky and mapped t_mem_sg to the buffer in
691 * either case (see iscsit_alloc_buffs)
693 static int iscsit_map_iovec(
694 struct iscsi_cmd
*cmd
,
700 struct scatterlist
*sg
;
701 unsigned int page_off
;
704 * We have a private mapping of the allocated pages in t_mem_sg.
705 * At this point, we also know each contains a page.
707 sg
= &cmd
->t_mem_sg
[data_offset
/ PAGE_SIZE
];
708 page_off
= (data_offset
% PAGE_SIZE
);
710 cmd
->first_data_sg
= sg
;
711 cmd
->first_data_sg_off
= page_off
;
713 while (data_length
) {
714 u32 cur_len
= min_t(u32
, data_length
, sg
->length
- page_off
);
716 iov
[i
].iov_base
= kmap(sg_page(sg
)) + sg
->offset
+ page_off
;
717 iov
[i
].iov_len
= cur_len
;
719 data_length
-= cur_len
;
725 cmd
->kmapped_nents
= i
;
730 static void iscsit_unmap_iovec(struct iscsi_cmd
*cmd
)
733 struct scatterlist
*sg
;
735 sg
= cmd
->first_data_sg
;
737 for (i
= 0; i
< cmd
->kmapped_nents
; i
++)
738 kunmap(sg_page(&sg
[i
]));
741 static void iscsit_ack_from_expstatsn(struct iscsi_conn
*conn
, u32 exp_statsn
)
743 struct iscsi_cmd
*cmd
;
745 conn
->exp_statsn
= exp_statsn
;
747 spin_lock_bh(&conn
->cmd_lock
);
748 list_for_each_entry(cmd
, &conn
->conn_cmd_list
, i_list
) {
749 spin_lock(&cmd
->istate_lock
);
750 if ((cmd
->i_state
== ISTATE_SENT_STATUS
) &&
751 (cmd
->stat_sn
< exp_statsn
)) {
752 cmd
->i_state
= ISTATE_REMOVE
;
753 spin_unlock(&cmd
->istate_lock
);
754 iscsit_add_cmd_to_immediate_queue(cmd
, conn
,
758 spin_unlock(&cmd
->istate_lock
);
760 spin_unlock_bh(&conn
->cmd_lock
);
763 static int iscsit_allocate_iovecs(struct iscsi_cmd
*cmd
)
765 u32 iov_count
= (cmd
->se_cmd
.t_data_nents
== 0) ? 1 :
766 cmd
->se_cmd
.t_data_nents
;
768 iov_count
+= TRANSPORT_IOV_DATA_BUFFER
;
770 cmd
->iov_data
= kzalloc(iov_count
* sizeof(struct kvec
), GFP_KERNEL
);
771 if (!cmd
->iov_data
) {
772 pr_err("Unable to allocate cmd->iov_data\n");
776 cmd
->orig_iov_data_count
= iov_count
;
780 static int iscsit_alloc_buffs(struct iscsi_cmd
*cmd
)
782 struct scatterlist
*sgl
;
783 u32 length
= cmd
->se_cmd
.data_length
;
784 int nents
= DIV_ROUND_UP(length
, PAGE_SIZE
);
787 * If no SCSI payload is present, allocate the default iovecs used for
791 return iscsit_allocate_iovecs(cmd
);
793 sgl
= kzalloc(sizeof(*sgl
) * nents
, GFP_KERNEL
);
797 sg_init_table(sgl
, nents
);
800 int buf_size
= min_t(int, length
, PAGE_SIZE
);
803 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
805 goto page_alloc_failed
;
807 sg_set_page(&sgl
[i
], page
, buf_size
, 0);
814 cmd
->t_mem_sg_nents
= nents
;
816 /* BIDI ops not supported */
818 /* Tell the core about our preallocated memory */
819 transport_generic_map_mem_to_cmd(&cmd
->se_cmd
, sgl
, nents
, NULL
, 0);
821 * Allocate iovecs for SCSI payload after transport_generic_map_mem_to_cmd
822 * so that cmd->se_cmd.t_tasks_se_num has been set.
824 ret
= iscsit_allocate_iovecs(cmd
);
826 goto page_alloc_failed
;
832 __free_page(sg_page(&sgl
[i
]));
835 kfree(cmd
->t_mem_sg
);
836 cmd
->t_mem_sg
= NULL
;
840 static int iscsit_handle_scsi_cmd(
841 struct iscsi_conn
*conn
,
844 int data_direction
, cmdsn_ret
= 0, immed_ret
, ret
, transport_ret
;
845 int dump_immediate_data
= 0, send_check_condition
= 0, payload_length
;
846 struct iscsi_cmd
*cmd
= NULL
;
847 struct iscsi_scsi_req
*hdr
;
849 spin_lock_bh(&conn
->sess
->session_stats_lock
);
850 conn
->sess
->cmd_pdus
++;
851 if (conn
->sess
->se_sess
->se_node_acl
) {
852 spin_lock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
853 conn
->sess
->se_sess
->se_node_acl
->num_cmds
++;
854 spin_unlock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
856 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
858 hdr
= (struct iscsi_scsi_req
*) buf
;
859 payload_length
= ntoh24(hdr
->dlength
);
860 hdr
->itt
= be32_to_cpu(hdr
->itt
);
861 hdr
->data_length
= be32_to_cpu(hdr
->data_length
);
862 hdr
->cmdsn
= be32_to_cpu(hdr
->cmdsn
);
863 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
865 /* FIXME; Add checks for AdditionalHeaderSegment */
867 if (!(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) &&
868 !(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
869 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
870 " not set. Bad iSCSI Initiator.\n");
871 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID
, 1,
875 if (((hdr
->flags
& ISCSI_FLAG_CMD_READ
) ||
876 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) && !hdr
->data_length
) {
878 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
879 * that adds support for RESERVE/RELEASE. There is a bug
880 * add with this new functionality that sets R/W bits when
881 * neither CDB carries any READ or WRITE datapayloads.
883 if ((hdr
->cdb
[0] == 0x16) || (hdr
->cdb
[0] == 0x17)) {
884 hdr
->flags
&= ~ISCSI_FLAG_CMD_READ
;
885 hdr
->flags
&= ~ISCSI_FLAG_CMD_WRITE
;
889 pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
890 " set when Expected Data Transfer Length is 0 for"
891 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr
->cdb
[0]);
892 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID
, 1,
897 if (!(hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
898 !(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) && (hdr
->data_length
!= 0)) {
899 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
900 " MUST be set if Expected Data Transfer Length is not 0."
901 " Bad iSCSI Initiator\n");
902 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID
, 1,
906 if ((hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
907 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) {
908 pr_err("Bidirectional operations not supported!\n");
909 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID
, 1,
913 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
914 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
915 " Scsi Command PDU.\n");
916 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID
, 1,
920 if (payload_length
&& !conn
->sess
->sess_ops
->ImmediateData
) {
921 pr_err("ImmediateData=No but DataSegmentLength=%u,"
922 " protocol error.\n", payload_length
);
923 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
927 if ((hdr
->data_length
== payload_length
) &&
928 (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
))) {
929 pr_err("Expected Data Transfer Length and Length of"
930 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
931 " bit is not set protocol error\n");
932 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
936 if (payload_length
> hdr
->data_length
) {
937 pr_err("DataSegmentLength: %u is greater than"
938 " EDTL: %u, protocol error.\n", payload_length
,
940 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
944 if (payload_length
> conn
->conn_ops
->MaxRecvDataSegmentLength
) {
945 pr_err("DataSegmentLength: %u is greater than"
946 " MaxRecvDataSegmentLength: %u, protocol error.\n",
947 payload_length
, conn
->conn_ops
->MaxRecvDataSegmentLength
);
948 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
952 if (payload_length
> conn
->sess
->sess_ops
->FirstBurstLength
) {
953 pr_err("DataSegmentLength: %u is greater than"
954 " FirstBurstLength: %u, protocol error.\n",
955 payload_length
, conn
->sess
->sess_ops
->FirstBurstLength
);
956 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID
, 1,
960 data_direction
= (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) ? DMA_TO_DEVICE
:
961 (hdr
->flags
& ISCSI_FLAG_CMD_READ
) ? DMA_FROM_DEVICE
:
964 cmd
= iscsit_allocate_se_cmd(conn
, hdr
->data_length
, data_direction
,
965 (hdr
->flags
& ISCSI_FLAG_CMD_ATTR_MASK
));
967 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES
, 1,
970 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
971 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr
->itt
,
972 hdr
->cmdsn
, hdr
->data_length
, payload_length
, conn
->cid
);
974 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_CMD
;
975 cmd
->i_state
= ISTATE_NEW_CMD
;
976 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
977 cmd
->immediate_data
= (payload_length
) ? 1 : 0;
978 cmd
->unsolicited_data
= ((!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) &&
979 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) ? 1 : 0);
980 if (cmd
->unsolicited_data
)
981 cmd
->cmd_flags
|= ICF_NON_IMMEDIATE_UNSOLICITED_DATA
;
983 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
984 if (hdr
->flags
& ISCSI_FLAG_CMD_READ
) {
985 spin_lock_bh(&conn
->sess
->ttt_lock
);
986 cmd
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
987 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF)
988 cmd
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
989 spin_unlock_bh(&conn
->sess
->ttt_lock
);
990 } else if (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)
991 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
992 cmd
->cmd_sn
= hdr
->cmdsn
;
993 cmd
->exp_stat_sn
= hdr
->exp_statsn
;
994 cmd
->first_burst_len
= payload_length
;
996 if (cmd
->data_direction
== DMA_FROM_DEVICE
) {
997 struct iscsi_datain_req
*dr
;
999 dr
= iscsit_allocate_datain_req();
1001 return iscsit_add_reject_from_cmd(
1002 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1005 iscsit_attach_datain_req(cmd
, dr
);
1009 * The CDB is going to an se_device_t.
1011 ret
= iscsit_get_lun_for_cmd(cmd
, hdr
->cdb
,
1012 get_unaligned_le64(&hdr
->lun
));
1014 if (cmd
->se_cmd
.scsi_sense_reason
== TCM_NON_EXISTENT_LUN
) {
1015 pr_debug("Responding to non-acl'ed,"
1016 " non-existent or non-exported iSCSI LUN:"
1017 " 0x%016Lx\n", get_unaligned_le64(&hdr
->lun
));
1019 if (ret
== PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES
)
1020 return iscsit_add_reject_from_cmd(
1021 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1024 send_check_condition
= 1;
1028 * The Initiator Node has access to the LUN (the addressing method
1029 * is handled inside of iscsit_get_lun_for_cmd()). Now it's time to
1030 * allocate 1->N transport tasks (depending on sector count and
1031 * maximum request size the physical HBA(s) can handle.
1033 transport_ret
= transport_generic_allocate_tasks(&cmd
->se_cmd
, hdr
->cdb
);
1034 if (transport_ret
== -ENOMEM
) {
1035 return iscsit_add_reject_from_cmd(
1036 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1038 } else if (transport_ret
== -EINVAL
) {
1040 * Unsupported SAM Opcode. CHECK_CONDITION will be sent
1041 * in iscsit_execute_cmd() during the CmdSN OOO Execution
1044 send_check_condition
= 1;
1046 if (iscsit_decide_list_to_build(cmd
, payload_length
) < 0)
1047 return iscsit_add_reject_from_cmd(
1048 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1053 spin_lock_bh(&conn
->cmd_lock
);
1054 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
1055 spin_unlock_bh(&conn
->cmd_lock
);
1057 * Check if we need to delay processing because of ALUA
1058 * Active/NonOptimized primary access state..
1060 core_alua_check_nonop_delay(&cmd
->se_cmd
);
1062 * Allocate and setup SGL used with transport_generic_map_mem_to_cmd().
1063 * also call iscsit_allocate_iovecs()
1065 ret
= iscsit_alloc_buffs(cmd
);
1067 return iscsit_add_reject_from_cmd(
1068 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1071 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1072 * the Immediate Bit is not set, and no Immediate
1075 * A PDU/CmdSN carrying Immediate Data can only
1076 * be processed after the DataCRC has passed.
1077 * If the DataCRC fails, the CmdSN MUST NOT
1078 * be acknowledged. (See below)
1080 if (!cmd
->immediate_data
) {
1081 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, hdr
->cmdsn
);
1082 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1083 return iscsit_add_reject_from_cmd(
1084 ISCSI_REASON_PROTOCOL_ERROR
,
1088 iscsit_ack_from_expstatsn(conn
, hdr
->exp_statsn
);
1091 * If no Immediate Data is attached, it's OK to return now.
1093 if (!cmd
->immediate_data
) {
1094 if (send_check_condition
)
1097 if (cmd
->unsolicited_data
) {
1098 iscsit_set_dataout_sequence_values(cmd
);
1100 spin_lock_bh(&cmd
->dataout_timeout_lock
);
1101 iscsit_start_dataout_timer(cmd
, cmd
->conn
);
1102 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
1109 * Early CHECK_CONDITIONs never make it to the transport processing
1110 * thread. They are processed in CmdSN order by
1111 * iscsit_check_received_cmdsn() below.
1113 if (send_check_condition
) {
1114 immed_ret
= IMMEDIATE_DATA_NORMAL_OPERATION
;
1115 dump_immediate_data
= 1;
1116 goto after_immediate_data
;
1119 * Call directly into transport_generic_new_cmd() to perform
1120 * the backend memory allocation.
1122 ret
= transport_generic_new_cmd(&cmd
->se_cmd
);
1123 if ((ret
< 0) || (cmd
->se_cmd
.se_cmd_flags
& SCF_SE_CMD_FAILED
)) {
1124 immed_ret
= IMMEDIATE_DATA_NORMAL_OPERATION
;
1125 dump_immediate_data
= 1;
1126 goto after_immediate_data
;
1129 immed_ret
= iscsit_handle_immediate_data(cmd
, buf
, payload_length
);
1130 after_immediate_data
:
1131 if (immed_ret
== IMMEDIATE_DATA_NORMAL_OPERATION
) {
1133 * A PDU/CmdSN carrying Immediate Data passed
1134 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1135 * Immediate Bit is not set.
1137 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, hdr
->cmdsn
);
1139 * Special case for Unsupported SAM WRITE Opcodes
1140 * and ImmediateData=Yes.
1142 if (dump_immediate_data
) {
1143 if (iscsit_dump_data_payload(conn
, payload_length
, 1) < 0)
1145 } else if (cmd
->unsolicited_data
) {
1146 iscsit_set_dataout_sequence_values(cmd
);
1148 spin_lock_bh(&cmd
->dataout_timeout_lock
);
1149 iscsit_start_dataout_timer(cmd
, cmd
->conn
);
1150 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
1153 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1154 return iscsit_add_reject_from_cmd(
1155 ISCSI_REASON_PROTOCOL_ERROR
,
1158 } else if (immed_ret
== IMMEDIATE_DATA_ERL1_CRC_FAILURE
) {
1160 * Immediate Data failed DataCRC and ERL>=1,
1161 * silently drop this PDU and let the initiator
1162 * plug the CmdSN gap.
1164 * FIXME: Send Unsolicited NOPIN with reserved
1165 * TTT here to help the initiator figure out
1166 * the missing CmdSN, although they should be
1167 * intelligent enough to determine the missing
1168 * CmdSN and issue a retry to plug the sequence.
1170 cmd
->i_state
= ISTATE_REMOVE
;
1171 iscsit_add_cmd_to_immediate_queue(cmd
, conn
, cmd
->i_state
);
1172 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1178 static u32
iscsit_do_crypto_hash_sg(
1179 struct hash_desc
*hash
,
1180 struct iscsi_cmd
*cmd
,
1188 struct scatterlist
*sg
;
1189 unsigned int page_off
;
1191 crypto_hash_init(hash
);
1193 sg
= cmd
->first_data_sg
;
1194 page_off
= cmd
->first_data_sg_off
;
1197 while (data_length
) {
1198 u32 cur_len
= min_t(u32
, data_length
, (sg
[i
].length
- page_off
));
1200 crypto_hash_update(hash
, &sg
[i
], cur_len
);
1202 data_length
-= cur_len
;
1208 struct scatterlist pad_sg
;
1210 sg_init_one(&pad_sg
, pad_bytes
, padding
);
1211 crypto_hash_update(hash
, &pad_sg
, padding
);
1213 crypto_hash_final(hash
, (u8
*) &data_crc
);
1218 static void iscsit_do_crypto_hash_buf(
1219 struct hash_desc
*hash
,
1226 struct scatterlist sg
;
1228 crypto_hash_init(hash
);
1230 sg_init_one(&sg
, (u8
*)buf
, payload_length
);
1231 crypto_hash_update(hash
, &sg
, payload_length
);
1234 sg_init_one(&sg
, pad_bytes
, padding
);
1235 crypto_hash_update(hash
, &sg
, padding
);
1237 crypto_hash_final(hash
, data_crc
);
1240 static int iscsit_handle_data_out(struct iscsi_conn
*conn
, unsigned char *buf
)
1242 int iov_ret
, ooo_cmdsn
= 0, ret
;
1243 u8 data_crc_failed
= 0;
1244 u32 checksum
, iov_count
= 0, padding
= 0, rx_got
= 0;
1245 u32 rx_size
= 0, payload_length
;
1246 struct iscsi_cmd
*cmd
= NULL
;
1247 struct se_cmd
*se_cmd
;
1248 struct iscsi_data
*hdr
;
1250 unsigned long flags
;
1252 hdr
= (struct iscsi_data
*) buf
;
1253 payload_length
= ntoh24(hdr
->dlength
);
1254 hdr
->itt
= be32_to_cpu(hdr
->itt
);
1255 hdr
->ttt
= be32_to_cpu(hdr
->ttt
);
1256 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
1257 hdr
->datasn
= be32_to_cpu(hdr
->datasn
);
1258 hdr
->offset
= be32_to_cpu(hdr
->offset
);
1260 if (!payload_length
) {
1261 pr_err("DataOUT payload is ZERO, protocol error.\n");
1262 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
1267 spin_lock_bh(&conn
->sess
->session_stats_lock
);
1268 conn
->sess
->rx_data_octets
+= payload_length
;
1269 if (conn
->sess
->se_sess
->se_node_acl
) {
1270 spin_lock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
1271 conn
->sess
->se_sess
->se_node_acl
->write_bytes
+= payload_length
;
1272 spin_unlock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
1274 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
1276 if (payload_length
> conn
->conn_ops
->MaxRecvDataSegmentLength
) {
1277 pr_err("DataSegmentLength: %u is greater than"
1278 " MaxRecvDataSegmentLength: %u\n", payload_length
,
1279 conn
->conn_ops
->MaxRecvDataSegmentLength
);
1280 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
1284 cmd
= iscsit_find_cmd_from_itt_or_dump(conn
, hdr
->itt
,
1289 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1290 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1291 hdr
->itt
, hdr
->ttt
, hdr
->datasn
, hdr
->offset
,
1292 payload_length
, conn
->cid
);
1294 if (cmd
->cmd_flags
& ICF_GOT_LAST_DATAOUT
) {
1295 pr_err("Command ITT: 0x%08x received DataOUT after"
1296 " last DataOUT received, dumping payload\n",
1297 cmd
->init_task_tag
);
1298 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1301 if (cmd
->data_direction
!= DMA_TO_DEVICE
) {
1302 pr_err("Command ITT: 0x%08x received DataOUT for a"
1303 " NON-WRITE command.\n", cmd
->init_task_tag
);
1304 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR
,
1307 se_cmd
= &cmd
->se_cmd
;
1308 iscsit_mod_dataout_timer(cmd
);
1310 if ((hdr
->offset
+ payload_length
) > cmd
->data_length
) {
1311 pr_err("DataOut Offset: %u, Length %u greater than"
1312 " iSCSI Command EDTL %u, protocol error.\n",
1313 hdr
->offset
, payload_length
, cmd
->data_length
);
1314 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID
,
1318 if (cmd
->unsolicited_data
) {
1319 int dump_unsolicited_data
= 0;
1321 if (conn
->sess
->sess_ops
->InitialR2T
) {
1322 pr_err("Received unexpected unsolicited data"
1323 " while InitialR2T=Yes, protocol error.\n");
1324 transport_send_check_condition_and_sense(&cmd
->se_cmd
,
1325 TCM_UNEXPECTED_UNSOLICITED_DATA
, 0);
1329 * Special case for dealing with Unsolicited DataOUT
1330 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1334 /* Something's amiss if we're not in WRITE_PENDING state... */
1335 spin_lock_irqsave(&se_cmd
->t_state_lock
, flags
);
1336 WARN_ON(se_cmd
->t_state
!= TRANSPORT_WRITE_PENDING
);
1337 spin_unlock_irqrestore(&se_cmd
->t_state_lock
, flags
);
1339 spin_lock_irqsave(&se_cmd
->t_state_lock
, flags
);
1340 if (!(se_cmd
->se_cmd_flags
& SCF_SUPPORTED_SAM_OPCODE
) ||
1341 (se_cmd
->se_cmd_flags
& SCF_SE_CMD_FAILED
))
1342 dump_unsolicited_data
= 1;
1343 spin_unlock_irqrestore(&se_cmd
->t_state_lock
, flags
);
1345 if (dump_unsolicited_data
) {
1347 * Check if a delayed TASK_ABORTED status needs to
1348 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1349 * received with the unsolicitied data out.
1351 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1352 iscsit_stop_dataout_timer(cmd
);
1354 transport_check_aborted_status(se_cmd
,
1355 (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
));
1356 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1360 * For the normal solicited data path:
1362 * Check for a delayed TASK_ABORTED status and dump any
1363 * incoming data out payload if one exists. Also, when the
1364 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1365 * data out sequence, we decrement outstanding_r2ts. Once
1366 * outstanding_r2ts reaches zero, go ahead and send the delayed
1367 * TASK_ABORTED status.
1369 if (atomic_read(&se_cmd
->t_transport_aborted
) != 0) {
1370 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1371 if (--cmd
->outstanding_r2ts
< 1) {
1372 iscsit_stop_dataout_timer(cmd
);
1373 transport_check_aborted_status(
1377 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1381 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1382 * within-command recovery checks before receiving the payload.
1384 ret
= iscsit_check_pre_dataout(cmd
, buf
);
1385 if (ret
== DATAOUT_WITHIN_COMMAND_RECOVERY
)
1387 else if (ret
== DATAOUT_CANNOT_RECOVER
)
1390 rx_size
+= payload_length
;
1391 iov
= &cmd
->iov_data
[0];
1393 iov_ret
= iscsit_map_iovec(cmd
, iov
, hdr
->offset
, payload_length
);
1397 iov_count
+= iov_ret
;
1399 padding
= ((-payload_length
) & 3);
1401 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
1402 iov
[iov_count
++].iov_len
= padding
;
1404 pr_debug("Receiving %u padding bytes.\n", padding
);
1407 if (conn
->conn_ops
->DataDigest
) {
1408 iov
[iov_count
].iov_base
= &checksum
;
1409 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
1410 rx_size
+= ISCSI_CRC_LEN
;
1413 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
1415 iscsit_unmap_iovec(cmd
);
1417 if (rx_got
!= rx_size
)
1420 if (conn
->conn_ops
->DataDigest
) {
1423 data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_rx_hash
, cmd
,
1424 hdr
->offset
, payload_length
, padding
,
1427 if (checksum
!= data_crc
) {
1428 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1429 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1430 " does not match computed 0x%08x\n",
1431 hdr
->itt
, hdr
->offset
, payload_length
,
1432 hdr
->datasn
, checksum
, data_crc
);
1433 data_crc_failed
= 1;
1435 pr_debug("Got CRC32C DataDigest 0x%08x for"
1436 " %u bytes of Data Out\n", checksum
,
1441 * Increment post receive data and CRC values or perform
1442 * within-command recovery.
1444 ret
= iscsit_check_post_dataout(cmd
, buf
, data_crc_failed
);
1445 if ((ret
== DATAOUT_NORMAL
) || (ret
== DATAOUT_WITHIN_COMMAND_RECOVERY
))
1447 else if (ret
== DATAOUT_SEND_R2T
) {
1448 iscsit_set_dataout_sequence_values(cmd
);
1449 iscsit_build_r2ts_for_cmd(cmd
, conn
, 0);
1450 } else if (ret
== DATAOUT_SEND_TO_TRANSPORT
) {
1452 * Handle extra special case for out of order
1453 * Unsolicited Data Out.
1455 spin_lock_bh(&cmd
->istate_lock
);
1456 ooo_cmdsn
= (cmd
->cmd_flags
& ICF_OOO_CMDSN
);
1457 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
1458 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
1459 spin_unlock_bh(&cmd
->istate_lock
);
1461 iscsit_stop_dataout_timer(cmd
);
1462 return (!ooo_cmdsn
) ? transport_generic_handle_data(
1464 } else /* DATAOUT_CANNOT_RECOVER */
1470 static int iscsit_handle_nop_out(
1471 struct iscsi_conn
*conn
,
1474 unsigned char *ping_data
= NULL
;
1475 int cmdsn_ret
, niov
= 0, ret
= 0, rx_got
, rx_size
;
1476 u32 checksum
, data_crc
, padding
= 0, payload_length
;
1478 struct iscsi_cmd
*cmd
= NULL
;
1479 struct kvec
*iov
= NULL
;
1480 struct iscsi_nopout
*hdr
;
1482 hdr
= (struct iscsi_nopout
*) buf
;
1483 payload_length
= ntoh24(hdr
->dlength
);
1484 lun
= get_unaligned_le64(&hdr
->lun
);
1485 hdr
->itt
= be32_to_cpu(hdr
->itt
);
1486 hdr
->ttt
= be32_to_cpu(hdr
->ttt
);
1487 hdr
->cmdsn
= be32_to_cpu(hdr
->cmdsn
);
1488 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
1490 if ((hdr
->itt
== 0xFFFFFFFF) && !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1491 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1492 " not set, protocol error.\n");
1493 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
1497 if (payload_length
> conn
->conn_ops
->MaxRecvDataSegmentLength
) {
1498 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1499 " greater than MaxRecvDataSegmentLength: %u, protocol"
1500 " error.\n", payload_length
,
1501 conn
->conn_ops
->MaxRecvDataSegmentLength
);
1502 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
1506 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%09x,"
1507 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1508 (hdr
->itt
== 0xFFFFFFFF) ? "Response" : "Request",
1509 hdr
->itt
, hdr
->ttt
, hdr
->cmdsn
, hdr
->exp_statsn
,
1512 * This is not a response to a Unsolicited NopIN, which means
1513 * it can either be a NOPOUT ping request (with a valid ITT),
1514 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1515 * Either way, make sure we allocate an struct iscsi_cmd, as both
1516 * can contain ping data.
1518 if (hdr
->ttt
== 0xFFFFFFFF) {
1519 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
1521 return iscsit_add_reject(
1522 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1525 cmd
->iscsi_opcode
= ISCSI_OP_NOOP_OUT
;
1526 cmd
->i_state
= ISTATE_SEND_NOPIN
;
1527 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ?
1529 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1530 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1531 cmd
->cmd_sn
= hdr
->cmdsn
;
1532 cmd
->exp_stat_sn
= hdr
->exp_statsn
;
1533 cmd
->data_direction
= DMA_NONE
;
1536 if (payload_length
&& (hdr
->ttt
== 0xFFFFFFFF)) {
1537 rx_size
= payload_length
;
1538 ping_data
= kzalloc(payload_length
+ 1, GFP_KERNEL
);
1540 pr_err("Unable to allocate memory for"
1541 " NOPOUT ping data.\n");
1546 iov
= &cmd
->iov_misc
[0];
1547 iov
[niov
].iov_base
= ping_data
;
1548 iov
[niov
++].iov_len
= payload_length
;
1550 padding
= ((-payload_length
) & 3);
1552 pr_debug("Receiving %u additional bytes"
1553 " for padding.\n", padding
);
1554 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
1555 iov
[niov
++].iov_len
= padding
;
1558 if (conn
->conn_ops
->DataDigest
) {
1559 iov
[niov
].iov_base
= &checksum
;
1560 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
1561 rx_size
+= ISCSI_CRC_LEN
;
1564 rx_got
= rx_data(conn
, &cmd
->iov_misc
[0], niov
, rx_size
);
1565 if (rx_got
!= rx_size
) {
1570 if (conn
->conn_ops
->DataDigest
) {
1571 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
1572 ping_data
, payload_length
,
1573 padding
, cmd
->pad_bytes
,
1576 if (checksum
!= data_crc
) {
1577 pr_err("Ping data CRC32C DataDigest"
1578 " 0x%08x does not match computed 0x%08x\n",
1579 checksum
, data_crc
);
1580 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
1581 pr_err("Unable to recover from"
1582 " NOPOUT Ping DataCRC failure while in"
1588 * Silently drop this PDU and let the
1589 * initiator plug the CmdSN gap.
1591 pr_debug("Dropping NOPOUT"
1592 " Command CmdSN: 0x%08x due to"
1593 " DataCRC error.\n", hdr
->cmdsn
);
1598 pr_debug("Got CRC32C DataDigest"
1599 " 0x%08x for %u bytes of ping data.\n",
1600 checksum
, payload_length
);
1604 ping_data
[payload_length
] = '\0';
1606 * Attach ping data to struct iscsi_cmd->buf_ptr.
1608 cmd
->buf_ptr
= (void *)ping_data
;
1609 cmd
->buf_ptr_size
= payload_length
;
1611 pr_debug("Got %u bytes of NOPOUT ping"
1612 " data.\n", payload_length
);
1613 pr_debug("Ping Data: \"%s\"\n", ping_data
);
1616 if (hdr
->itt
!= 0xFFFFFFFF) {
1618 pr_err("Checking CmdSN for NOPOUT,"
1619 " but cmd is NULL!\n");
1623 * Initiator is expecting a NopIN ping reply,
1625 spin_lock_bh(&conn
->cmd_lock
);
1626 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
1627 spin_unlock_bh(&conn
->cmd_lock
);
1629 iscsit_ack_from_expstatsn(conn
, hdr
->exp_statsn
);
1631 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
1632 iscsit_add_cmd_to_response_queue(cmd
, conn
,
1637 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, hdr
->cmdsn
);
1638 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1642 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1643 return iscsit_add_reject_from_cmd(
1644 ISCSI_REASON_PROTOCOL_ERROR
,
1650 if (hdr
->ttt
!= 0xFFFFFFFF) {
1652 * This was a response to a unsolicited NOPIN ping.
1654 cmd
= iscsit_find_cmd_from_ttt(conn
, hdr
->ttt
);
1658 iscsit_stop_nopin_response_timer(conn
);
1660 cmd
->i_state
= ISTATE_REMOVE
;
1661 iscsit_add_cmd_to_immediate_queue(cmd
, conn
, cmd
->i_state
);
1662 iscsit_start_nopin_timer(conn
);
1665 * Initiator is not expecting a NOPIN is response.
1666 * Just ignore for now.
1668 * iSCSI v19-91 10.18
1669 * "A NOP-OUT may also be used to confirm a changed
1670 * ExpStatSN if another PDU will not be available
1680 iscsit_release_cmd(cmd
);
1686 static int iscsit_handle_task_mgt_cmd(
1687 struct iscsi_conn
*conn
,
1690 struct iscsi_cmd
*cmd
;
1691 struct se_tmr_req
*se_tmr
;
1692 struct iscsi_tmr_req
*tmr_req
;
1693 struct iscsi_tm
*hdr
;
1695 int out_of_order_cmdsn
= 0;
1699 hdr
= (struct iscsi_tm
*) buf
;
1700 payload_length
= ntoh24(hdr
->dlength
);
1701 hdr
->itt
= be32_to_cpu(hdr
->itt
);
1702 hdr
->rtt
= be32_to_cpu(hdr
->rtt
);
1703 hdr
->cmdsn
= be32_to_cpu(hdr
->cmdsn
);
1704 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
1705 hdr
->refcmdsn
= be32_to_cpu(hdr
->refcmdsn
);
1706 hdr
->exp_datasn
= be32_to_cpu(hdr
->exp_datasn
);
1707 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
1708 function
= hdr
->flags
;
1710 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1711 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1712 " 0x%08x, CID: %hu\n", hdr
->itt
, hdr
->cmdsn
, function
,
1713 hdr
->rtt
, hdr
->refcmdsn
, conn
->cid
);
1715 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1716 ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1717 (hdr
->rtt
!= ISCSI_RESERVED_TAG
))) {
1718 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1719 hdr
->rtt
= ISCSI_RESERVED_TAG
;
1722 if ((function
== ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1723 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1724 pr_err("Task Management Request TASK_REASSIGN not"
1725 " issued as immediate command, bad iSCSI Initiator"
1726 "implementation\n");
1727 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
1730 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1731 (hdr
->refcmdsn
!= ISCSI_RESERVED_TAG
))
1732 hdr
->refcmdsn
= ISCSI_RESERVED_TAG
;
1734 cmd
= iscsit_allocate_se_cmd_for_tmr(conn
, function
);
1736 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1739 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_TMFUNC
;
1740 cmd
->i_state
= ISTATE_SEND_TASKMGTRSP
;
1741 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1742 cmd
->init_task_tag
= hdr
->itt
;
1743 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1744 cmd
->cmd_sn
= hdr
->cmdsn
;
1745 cmd
->exp_stat_sn
= hdr
->exp_statsn
;
1746 se_tmr
= cmd
->se_cmd
.se_tmr_req
;
1747 tmr_req
= cmd
->tmr_req
;
1749 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1751 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
1752 ret
= iscsit_get_lun_for_tmr(cmd
,
1753 get_unaligned_le64(&hdr
->lun
));
1755 cmd
->se_cmd
.se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
1756 se_tmr
->response
= ISCSI_TMF_RSP_NO_LUN
;
1762 case ISCSI_TM_FUNC_ABORT_TASK
:
1763 se_tmr
->response
= iscsit_tmr_abort_task(cmd
, buf
);
1764 if (se_tmr
->response
!= ISCSI_TMF_RSP_COMPLETE
) {
1765 cmd
->se_cmd
.se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
1769 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
1770 case ISCSI_TM_FUNC_CLEAR_ACA
:
1771 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
1772 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
1774 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
1775 if (iscsit_tmr_task_warm_reset(conn
, tmr_req
, buf
) < 0) {
1776 cmd
->se_cmd
.se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
1777 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
1781 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
1782 if (iscsit_tmr_task_cold_reset(conn
, tmr_req
, buf
) < 0) {
1783 cmd
->se_cmd
.se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
1784 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
1788 case ISCSI_TM_FUNC_TASK_REASSIGN
:
1789 se_tmr
->response
= iscsit_tmr_task_reassign(cmd
, buf
);
1791 * Perform sanity checks on the ExpDataSN only if the
1792 * TASK_REASSIGN was successful.
1794 if (se_tmr
->response
!= ISCSI_TMF_RSP_COMPLETE
)
1797 if (iscsit_check_task_reassign_expdatasn(tmr_req
, conn
) < 0)
1798 return iscsit_add_reject_from_cmd(
1799 ISCSI_REASON_BOOKMARK_INVALID
, 1, 1,
1803 pr_err("Unknown TMR function: 0x%02x, protocol"
1804 " error.\n", function
);
1805 cmd
->se_cmd
.se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
1806 se_tmr
->response
= ISCSI_TMF_RSP_NOT_SUPPORTED
;
1810 if ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1811 (se_tmr
->response
== ISCSI_TMF_RSP_COMPLETE
))
1812 se_tmr
->call_transport
= 1;
1814 spin_lock_bh(&conn
->cmd_lock
);
1815 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
1816 spin_unlock_bh(&conn
->cmd_lock
);
1818 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1819 int cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, hdr
->cmdsn
);
1820 if (cmdsn_ret
== CMDSN_HIGHER_THAN_EXP
)
1821 out_of_order_cmdsn
= 1;
1822 else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1824 } else { /* (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) */
1825 return iscsit_add_reject_from_cmd(
1826 ISCSI_REASON_PROTOCOL_ERROR
,
1830 iscsit_ack_from_expstatsn(conn
, hdr
->exp_statsn
);
1832 if (out_of_order_cmdsn
)
1835 * Found the referenced task, send to transport for processing.
1837 if (se_tmr
->call_transport
)
1838 return transport_generic_handle_tmr(&cmd
->se_cmd
);
1841 * Could not find the referenced LUN, task, or Task Management
1842 * command not authorized or supported. Change state and
1843 * let the tx_thread send the response.
1845 * For connection recovery, this is also the default action for
1846 * TMR TASK_REASSIGN.
1848 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
1852 /* #warning FIXME: Support Text Command parameters besides SendTargets */
1853 static int iscsit_handle_text_cmd(
1854 struct iscsi_conn
*conn
,
1857 char *text_ptr
, *text_in
;
1858 int cmdsn_ret
, niov
= 0, rx_got
, rx_size
;
1859 u32 checksum
= 0, data_crc
= 0, payload_length
;
1860 u32 padding
= 0, pad_bytes
= 0, text_length
= 0;
1861 struct iscsi_cmd
*cmd
;
1863 struct iscsi_text
*hdr
;
1865 hdr
= (struct iscsi_text
*) buf
;
1866 payload_length
= ntoh24(hdr
->dlength
);
1867 hdr
->itt
= be32_to_cpu(hdr
->itt
);
1868 hdr
->ttt
= be32_to_cpu(hdr
->ttt
);
1869 hdr
->cmdsn
= be32_to_cpu(hdr
->cmdsn
);
1870 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
1872 if (payload_length
> conn
->conn_ops
->MaxRecvDataSegmentLength
) {
1873 pr_err("Unable to accept text parameter length: %u"
1874 "greater than MaxRecvDataSegmentLength %u.\n",
1875 payload_length
, conn
->conn_ops
->MaxRecvDataSegmentLength
);
1876 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
1880 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1881 " ExpStatSN: 0x%08x, Length: %u\n", hdr
->itt
, hdr
->cmdsn
,
1882 hdr
->exp_statsn
, payload_length
);
1884 rx_size
= text_length
= payload_length
;
1886 text_in
= kzalloc(text_length
, GFP_KERNEL
);
1888 pr_err("Unable to allocate memory for"
1889 " incoming text parameters\n");
1893 memset(iov
, 0, 3 * sizeof(struct kvec
));
1894 iov
[niov
].iov_base
= text_in
;
1895 iov
[niov
++].iov_len
= text_length
;
1897 padding
= ((-payload_length
) & 3);
1899 iov
[niov
].iov_base
= &pad_bytes
;
1900 iov
[niov
++].iov_len
= padding
;
1902 pr_debug("Receiving %u additional bytes"
1903 " for padding.\n", padding
);
1905 if (conn
->conn_ops
->DataDigest
) {
1906 iov
[niov
].iov_base
= &checksum
;
1907 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
1908 rx_size
+= ISCSI_CRC_LEN
;
1911 rx_got
= rx_data(conn
, &iov
[0], niov
, rx_size
);
1912 if (rx_got
!= rx_size
) {
1917 if (conn
->conn_ops
->DataDigest
) {
1918 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
1919 text_in
, text_length
,
1920 padding
, (u8
*)&pad_bytes
,
1923 if (checksum
!= data_crc
) {
1924 pr_err("Text data CRC32C DataDigest"
1925 " 0x%08x does not match computed"
1926 " 0x%08x\n", checksum
, data_crc
);
1927 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
1928 pr_err("Unable to recover from"
1929 " Text Data digest failure while in"
1935 * Silently drop this PDU and let the
1936 * initiator plug the CmdSN gap.
1938 pr_debug("Dropping Text"
1939 " Command CmdSN: 0x%08x due to"
1940 " DataCRC error.\n", hdr
->cmdsn
);
1945 pr_debug("Got CRC32C DataDigest"
1946 " 0x%08x for %u bytes of text data.\n",
1947 checksum
, text_length
);
1950 text_in
[text_length
- 1] = '\0';
1951 pr_debug("Successfully read %d bytes of text"
1952 " data.\n", text_length
);
1954 if (strncmp("SendTargets", text_in
, 11) != 0) {
1955 pr_err("Received Text Data that is not"
1956 " SendTargets, cannot continue.\n");
1960 text_ptr
= strchr(text_in
, '=');
1962 pr_err("No \"=\" separator found in Text Data,"
1963 " cannot continue.\n");
1967 if (strncmp("=All", text_ptr
, 4) != 0) {
1968 pr_err("Unable to locate All value for"
1969 " SendTargets key, cannot continue.\n");
1973 /*#warning Support SendTargets=(iSCSI Target Name/Nothing) values. */
1977 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
1979 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1982 cmd
->iscsi_opcode
= ISCSI_OP_TEXT
;
1983 cmd
->i_state
= ISTATE_SEND_TEXTRSP
;
1984 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1985 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1986 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1987 cmd
->cmd_sn
= hdr
->cmdsn
;
1988 cmd
->exp_stat_sn
= hdr
->exp_statsn
;
1989 cmd
->data_direction
= DMA_NONE
;
1991 spin_lock_bh(&conn
->cmd_lock
);
1992 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
1993 spin_unlock_bh(&conn
->cmd_lock
);
1995 iscsit_ack_from_expstatsn(conn
, hdr
->exp_statsn
);
1997 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1998 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, hdr
->cmdsn
);
1999 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2000 return iscsit_add_reject_from_cmd(
2001 ISCSI_REASON_PROTOCOL_ERROR
,
2007 return iscsit_execute_cmd(cmd
, 0);
2010 int iscsit_logout_closesession(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2012 struct iscsi_conn
*conn_p
;
2013 struct iscsi_session
*sess
= conn
->sess
;
2015 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2016 " for SID: %u.\n", conn
->cid
, conn
->sess
->sid
);
2018 atomic_set(&sess
->session_logout
, 1);
2019 atomic_set(&conn
->conn_logout_remove
, 1);
2020 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_SESSION
;
2022 iscsit_inc_conn_usage_count(conn
);
2023 iscsit_inc_session_usage_count(sess
);
2025 spin_lock_bh(&sess
->conn_lock
);
2026 list_for_each_entry(conn_p
, &sess
->sess_conn_list
, conn_list
) {
2027 if (conn_p
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
)
2030 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2031 conn_p
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2033 spin_unlock_bh(&sess
->conn_lock
);
2035 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2040 int iscsit_logout_closeconnection(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2042 struct iscsi_conn
*l_conn
;
2043 struct iscsi_session
*sess
= conn
->sess
;
2045 pr_debug("Received logout request CLOSECONNECTION for CID:"
2046 " %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2049 * A Logout Request with a CLOSECONNECTION reason code for a CID
2050 * can arrive on a connection with a differing CID.
2052 if (conn
->cid
== cmd
->logout_cid
) {
2053 spin_lock_bh(&conn
->state_lock
);
2054 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2055 conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2057 atomic_set(&conn
->conn_logout_remove
, 1);
2058 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
;
2059 iscsit_inc_conn_usage_count(conn
);
2061 spin_unlock_bh(&conn
->state_lock
);
2064 * Handle all different cid CLOSECONNECTION requests in
2065 * iscsit_logout_post_handler_diffcid() as to give enough
2066 * time for any non immediate command's CmdSN to be
2067 * acknowledged on the connection in question.
2069 * Here we simply make sure the CID is still around.
2071 l_conn
= iscsit_get_conn_from_cid(sess
,
2074 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2075 iscsit_add_cmd_to_response_queue(cmd
, conn
,
2080 iscsit_dec_conn_usage_count(l_conn
);
2083 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2088 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2090 struct iscsi_session
*sess
= conn
->sess
;
2092 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2093 " CID: %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2095 if (sess
->sess_ops
->ErrorRecoveryLevel
!= 2) {
2096 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2097 " while ERL!=2.\n");
2098 cmd
->logout_response
= ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
;
2099 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2103 if (conn
->cid
== cmd
->logout_cid
) {
2104 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2105 " with CID: %hu on CID: %hu, implementation error.\n",
2106 cmd
->logout_cid
, conn
->cid
);
2107 cmd
->logout_response
= ISCSI_LOGOUT_CLEANUP_FAILED
;
2108 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2112 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2117 static int iscsit_handle_logout_cmd(
2118 struct iscsi_conn
*conn
,
2121 int cmdsn_ret
, logout_remove
= 0;
2123 struct iscsi_cmd
*cmd
;
2124 struct iscsi_logout
*hdr
;
2125 struct iscsi_tiqn
*tiqn
= iscsit_snmp_get_tiqn(conn
);
2127 hdr
= (struct iscsi_logout
*) buf
;
2128 reason_code
= (hdr
->flags
& 0x7f);
2129 hdr
->itt
= be32_to_cpu(hdr
->itt
);
2130 hdr
->cid
= be16_to_cpu(hdr
->cid
);
2131 hdr
->cmdsn
= be32_to_cpu(hdr
->cmdsn
);
2132 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
2135 spin_lock(&tiqn
->logout_stats
.lock
);
2136 if (reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
)
2137 tiqn
->logout_stats
.normal_logouts
++;
2139 tiqn
->logout_stats
.abnormal_logouts
++;
2140 spin_unlock(&tiqn
->logout_stats
.lock
);
2143 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2144 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2145 hdr
->itt
, hdr
->cmdsn
, hdr
->exp_statsn
, reason_code
,
2146 hdr
->cid
, conn
->cid
);
2148 if (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
) {
2149 pr_err("Received logout request on connection that"
2150 " is not in logged in state, ignoring request.\n");
2154 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
2156 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES
, 1,
2159 cmd
->iscsi_opcode
= ISCSI_OP_LOGOUT
;
2160 cmd
->i_state
= ISTATE_SEND_LOGOUTRSP
;
2161 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2162 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
2163 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2164 cmd
->cmd_sn
= hdr
->cmdsn
;
2165 cmd
->exp_stat_sn
= hdr
->exp_statsn
;
2166 cmd
->logout_cid
= hdr
->cid
;
2167 cmd
->logout_reason
= reason_code
;
2168 cmd
->data_direction
= DMA_NONE
;
2171 * We need to sleep in these cases (by returning 1) until the Logout
2172 * Response gets sent in the tx thread.
2174 if ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) ||
2175 ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
) &&
2176 (hdr
->cid
== conn
->cid
)))
2179 spin_lock_bh(&conn
->cmd_lock
);
2180 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
2181 spin_unlock_bh(&conn
->cmd_lock
);
2183 if (reason_code
!= ISCSI_LOGOUT_REASON_RECOVERY
)
2184 iscsit_ack_from_expstatsn(conn
, hdr
->exp_statsn
);
2187 * Immediate commands are executed, well, immediately.
2188 * Non-Immediate Logout Commands are executed in CmdSN order.
2190 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
2191 int ret
= iscsit_execute_cmd(cmd
, 0);
2196 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, hdr
->cmdsn
);
2197 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
2199 } else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
) {
2200 return iscsit_add_reject_from_cmd(
2201 ISCSI_REASON_PROTOCOL_ERROR
,
2206 return logout_remove
;
2209 static int iscsit_handle_snack(
2210 struct iscsi_conn
*conn
,
2215 struct iscsi_snack
*hdr
;
2217 hdr
= (struct iscsi_snack
*) buf
;
2218 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
2219 lun
= get_unaligned_le64(&hdr
->lun
);
2220 unpacked_lun
= scsilun_to_int((struct scsi_lun
*)&lun
);
2221 hdr
->itt
= be32_to_cpu(hdr
->itt
);
2222 hdr
->ttt
= be32_to_cpu(hdr
->ttt
);
2223 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
2224 hdr
->begrun
= be32_to_cpu(hdr
->begrun
);
2225 hdr
->runlength
= be32_to_cpu(hdr
->runlength
);
2227 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2228 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2229 " CID: %hu\n", hdr
->itt
, hdr
->exp_statsn
, hdr
->flags
,
2230 hdr
->begrun
, hdr
->runlength
, conn
->cid
);
2232 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2233 pr_err("Initiator sent SNACK request while in"
2234 " ErrorRecoveryLevel=0.\n");
2235 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
2239 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2240 * call from inside iscsi_send_recovery_datain_or_r2t().
2242 switch (hdr
->flags
& ISCSI_FLAG_SNACK_TYPE_MASK
) {
2244 return iscsit_handle_recovery_datain_or_r2t(conn
, buf
,
2245 hdr
->itt
, hdr
->ttt
, hdr
->begrun
, hdr
->runlength
);
2247 case ISCSI_FLAG_SNACK_TYPE_STATUS
:
2248 return iscsit_handle_status_snack(conn
, hdr
->itt
, hdr
->ttt
,
2249 hdr
->begrun
, hdr
->runlength
);
2250 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK
:
2251 return iscsit_handle_data_ack(conn
, hdr
->ttt
, hdr
->begrun
,
2253 case ISCSI_FLAG_SNACK_TYPE_RDATA
:
2254 /* FIXME: Support R-Data SNACK */
2255 pr_err("R-Data SNACK Not Supported.\n");
2256 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
2259 pr_err("Unknown SNACK type 0x%02x, protocol"
2260 " error.\n", hdr
->flags
& 0x0f);
2261 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
2268 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2270 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2271 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2272 wait_for_completion_interruptible_timeout(
2273 &conn
->rx_half_close_comp
,
2274 ISCSI_RX_THREAD_TCP_TIMEOUT
* HZ
);
2278 static int iscsit_handle_immediate_data(
2279 struct iscsi_cmd
*cmd
,
2283 int iov_ret
, rx_got
= 0, rx_size
= 0;
2284 u32 checksum
, iov_count
= 0, padding
= 0;
2285 struct iscsi_conn
*conn
= cmd
->conn
;
2288 iov_ret
= iscsit_map_iovec(cmd
, cmd
->iov_data
, cmd
->write_data_done
, length
);
2290 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2293 iov_count
= iov_ret
;
2294 iov
= &cmd
->iov_data
[0];
2296 padding
= ((-length
) & 3);
2298 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2299 iov
[iov_count
++].iov_len
= padding
;
2303 if (conn
->conn_ops
->DataDigest
) {
2304 iov
[iov_count
].iov_base
= &checksum
;
2305 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2306 rx_size
+= ISCSI_CRC_LEN
;
2309 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
2311 iscsit_unmap_iovec(cmd
);
2313 if (rx_got
!= rx_size
) {
2314 iscsit_rx_thread_wait_for_tcp(conn
);
2315 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2318 if (conn
->conn_ops
->DataDigest
) {
2321 data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_rx_hash
, cmd
,
2322 cmd
->write_data_done
, length
, padding
,
2325 if (checksum
!= data_crc
) {
2326 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2327 " does not match computed 0x%08x\n", checksum
,
2330 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2331 pr_err("Unable to recover from"
2332 " Immediate Data digest failure while"
2334 iscsit_add_reject_from_cmd(
2335 ISCSI_REASON_DATA_DIGEST_ERROR
,
2337 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2339 iscsit_add_reject_from_cmd(
2340 ISCSI_REASON_DATA_DIGEST_ERROR
,
2342 return IMMEDIATE_DATA_ERL1_CRC_FAILURE
;
2345 pr_debug("Got CRC32C DataDigest 0x%08x for"
2346 " %u bytes of Immediate Data\n", checksum
,
2351 cmd
->write_data_done
+= length
;
2353 if (cmd
->write_data_done
== cmd
->data_length
) {
2354 spin_lock_bh(&cmd
->istate_lock
);
2355 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
2356 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
2357 spin_unlock_bh(&cmd
->istate_lock
);
2360 return IMMEDIATE_DATA_NORMAL_OPERATION
;
2364 * Called with sess->conn_lock held.
2366 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2367 with active network interface */
2368 static void iscsit_build_conn_drop_async_message(struct iscsi_conn
*conn
)
2370 struct iscsi_cmd
*cmd
;
2371 struct iscsi_conn
*conn_p
;
2374 * Only send a Asynchronous Message on connections whos network
2375 * interface is still functional.
2377 list_for_each_entry(conn_p
, &conn
->sess
->sess_conn_list
, conn_list
) {
2378 if (conn_p
->conn_state
== TARG_CONN_STATE_LOGGED_IN
) {
2379 iscsit_inc_conn_usage_count(conn_p
);
2387 cmd
= iscsit_allocate_cmd(conn_p
, GFP_KERNEL
);
2389 iscsit_dec_conn_usage_count(conn_p
);
2393 cmd
->logout_cid
= conn
->cid
;
2394 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2395 cmd
->i_state
= ISTATE_SEND_ASYNCMSG
;
2397 spin_lock_bh(&conn_p
->cmd_lock
);
2398 list_add_tail(&cmd
->i_list
, &conn_p
->conn_cmd_list
);
2399 spin_unlock_bh(&conn_p
->cmd_lock
);
2401 iscsit_add_cmd_to_response_queue(cmd
, conn_p
, cmd
->i_state
);
2402 iscsit_dec_conn_usage_count(conn_p
);
2405 static int iscsit_send_conn_drop_async_message(
2406 struct iscsi_cmd
*cmd
,
2407 struct iscsi_conn
*conn
)
2409 struct iscsi_async
*hdr
;
2411 cmd
->tx_size
= ISCSI_HDR_LEN
;
2412 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2414 hdr
= (struct iscsi_async
*) cmd
->pdu
;
2415 hdr
->opcode
= ISCSI_OP_ASYNC_EVENT
;
2416 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
2417 cmd
->init_task_tag
= 0xFFFFFFFF;
2418 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2419 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->rsvd4
[0]);
2420 cmd
->stat_sn
= conn
->stat_sn
++;
2421 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2422 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2423 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2424 hdr
->async_event
= ISCSI_ASYNC_MSG_DROPPING_CONNECTION
;
2425 hdr
->param1
= cpu_to_be16(cmd
->logout_cid
);
2426 hdr
->param2
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Wait
);
2427 hdr
->param3
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Retain
);
2429 if (conn
->conn_ops
->HeaderDigest
) {
2430 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2432 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2433 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
2434 0, NULL
, (u8
*)header_digest
);
2436 cmd
->tx_size
+= ISCSI_CRC_LEN
;
2437 pr_debug("Attaching CRC32C HeaderDigest to"
2438 " Async Message 0x%08x\n", *header_digest
);
2441 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2442 cmd
->iov_misc
[0].iov_len
= cmd
->tx_size
;
2443 cmd
->iov_misc_count
= 1;
2445 pr_debug("Sending Connection Dropped Async Message StatSN:"
2446 " 0x%08x, for CID: %hu on CID: %hu\n", cmd
->stat_sn
,
2447 cmd
->logout_cid
, conn
->cid
);
2451 static int iscsit_send_data_in(
2452 struct iscsi_cmd
*cmd
,
2453 struct iscsi_conn
*conn
,
2456 int iov_ret
= 0, set_statsn
= 0;
2457 u32 iov_count
= 0, tx_size
= 0;
2458 struct iscsi_datain datain
;
2459 struct iscsi_datain_req
*dr
;
2460 struct iscsi_data_rsp
*hdr
;
2463 memset(&datain
, 0, sizeof(struct iscsi_datain
));
2464 dr
= iscsit_get_datain_values(cmd
, &datain
);
2466 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2467 cmd
->init_task_tag
);
2472 * Be paranoid and double check the logic for now.
2474 if ((datain
.offset
+ datain
.length
) > cmd
->data_length
) {
2475 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2476 " datain.length: %u exceeds cmd->data_length: %u\n",
2477 cmd
->init_task_tag
, datain
.offset
, datain
.length
,
2482 spin_lock_bh(&conn
->sess
->session_stats_lock
);
2483 conn
->sess
->tx_data_octets
+= datain
.length
;
2484 if (conn
->sess
->se_sess
->se_node_acl
) {
2485 spin_lock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
2486 conn
->sess
->se_sess
->se_node_acl
->read_bytes
+= datain
.length
;
2487 spin_unlock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
2489 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
2491 * Special case for successfully execution w/ both DATAIN
2494 if ((datain
.flags
& ISCSI_FLAG_DATA_STATUS
) &&
2495 (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
))
2496 datain
.flags
&= ~ISCSI_FLAG_DATA_STATUS
;
2498 if ((dr
->dr_complete
== DATAIN_COMPLETE_NORMAL
) ||
2499 (dr
->dr_complete
== DATAIN_COMPLETE_CONNECTION_RECOVERY
)) {
2500 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2501 cmd
->stat_sn
= conn
->stat_sn
++;
2503 } else if (dr
->dr_complete
==
2504 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY
)
2508 hdr
= (struct iscsi_data_rsp
*) cmd
->pdu
;
2509 memset(hdr
, 0, ISCSI_HDR_LEN
);
2510 hdr
->opcode
= ISCSI_OP_SCSI_DATA_IN
;
2511 hdr
->flags
= datain
.flags
;
2512 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
2513 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
2514 hdr
->flags
|= ISCSI_FLAG_DATA_OVERFLOW
;
2515 hdr
->residual_count
= cpu_to_be32(cmd
->residual_count
);
2516 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
2517 hdr
->flags
|= ISCSI_FLAG_DATA_UNDERFLOW
;
2518 hdr
->residual_count
= cpu_to_be32(cmd
->residual_count
);
2521 hton24(hdr
->dlength
, datain
.length
);
2522 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2523 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
2524 (struct scsi_lun
*)&hdr
->lun
);
2526 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2528 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
2529 hdr
->ttt
= (hdr
->flags
& ISCSI_FLAG_DATA_ACK
) ?
2530 cpu_to_be32(cmd
->targ_xfer_tag
) :
2532 hdr
->statsn
= (set_statsn
) ? cpu_to_be32(cmd
->stat_sn
) :
2534 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2535 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2536 hdr
->datasn
= cpu_to_be32(datain
.data_sn
);
2537 hdr
->offset
= cpu_to_be32(datain
.offset
);
2539 iov
= &cmd
->iov_data
[0];
2540 iov
[iov_count
].iov_base
= cmd
->pdu
;
2541 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
2542 tx_size
+= ISCSI_HDR_LEN
;
2544 if (conn
->conn_ops
->HeaderDigest
) {
2545 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2547 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2548 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
2549 0, NULL
, (u8
*)header_digest
);
2551 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2552 tx_size
+= ISCSI_CRC_LEN
;
2554 pr_debug("Attaching CRC32 HeaderDigest"
2555 " for DataIN PDU 0x%08x\n", *header_digest
);
2558 iov_ret
= iscsit_map_iovec(cmd
, &cmd
->iov_data
[1], datain
.offset
, datain
.length
);
2562 iov_count
+= iov_ret
;
2563 tx_size
+= datain
.length
;
2565 cmd
->padding
= ((-datain
.length
) & 3);
2567 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2568 iov
[iov_count
++].iov_len
= cmd
->padding
;
2569 tx_size
+= cmd
->padding
;
2571 pr_debug("Attaching %u padding bytes\n",
2574 if (conn
->conn_ops
->DataDigest
) {
2575 cmd
->data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_tx_hash
, cmd
,
2576 datain
.offset
, datain
.length
, cmd
->padding
, cmd
->pad_bytes
);
2578 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
2579 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2580 tx_size
+= ISCSI_CRC_LEN
;
2582 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2583 " 0x%08x\n", datain
.length
+cmd
->padding
, cmd
->data_crc
);
2586 cmd
->iov_data_count
= iov_count
;
2587 cmd
->tx_size
= tx_size
;
2589 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2590 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2591 cmd
->init_task_tag
, ntohl(hdr
->statsn
), ntohl(hdr
->datasn
),
2592 ntohl(hdr
->offset
), datain
.length
, conn
->cid
);
2594 if (dr
->dr_complete
) {
2595 *eodr
= (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ?
2597 iscsit_free_datain_req(cmd
, dr
);
2603 static int iscsit_send_logout_response(
2604 struct iscsi_cmd
*cmd
,
2605 struct iscsi_conn
*conn
)
2607 int niov
= 0, tx_size
;
2608 struct iscsi_conn
*logout_conn
= NULL
;
2609 struct iscsi_conn_recovery
*cr
= NULL
;
2610 struct iscsi_session
*sess
= conn
->sess
;
2612 struct iscsi_logout_rsp
*hdr
;
2614 * The actual shutting down of Sessions and/or Connections
2615 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2616 * is done in scsi_logout_post_handler().
2618 switch (cmd
->logout_reason
) {
2619 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
2620 pr_debug("iSCSI session logout successful, setting"
2621 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2622 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2624 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
2625 if (cmd
->logout_response
== ISCSI_LOGOUT_CID_NOT_FOUND
)
2628 * For CLOSECONNECTION logout requests carrying
2629 * a matching logout CID -> local CID, the reference
2630 * for the local CID will have been incremented in
2631 * iscsi_logout_closeconnection().
2633 * For CLOSECONNECTION logout requests carrying
2634 * a different CID than the connection it arrived
2635 * on, the connection responding to cmd->logout_cid
2636 * is stopped in iscsit_logout_post_handler_diffcid().
2639 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2640 " successful.\n", cmd
->logout_cid
, conn
->cid
);
2641 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2643 case ISCSI_LOGOUT_REASON_RECOVERY
:
2644 if ((cmd
->logout_response
== ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
) ||
2645 (cmd
->logout_response
== ISCSI_LOGOUT_CLEANUP_FAILED
))
2648 * If the connection is still active from our point of view
2649 * force connection recovery to occur.
2651 logout_conn
= iscsit_get_conn_from_cid_rcfr(sess
,
2653 if ((logout_conn
)) {
2654 iscsit_connection_reinstatement_rcfr(logout_conn
);
2655 iscsit_dec_conn_usage_count(logout_conn
);
2658 cr
= iscsit_get_inactive_connection_recovery_entry(
2659 conn
->sess
, cmd
->logout_cid
);
2661 pr_err("Unable to locate CID: %hu for"
2662 " REMOVECONNFORRECOVERY Logout Request.\n",
2664 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2668 iscsit_discard_cr_cmds_by_expstatsn(cr
, cmd
->exp_stat_sn
);
2670 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2671 " for recovery for CID: %hu on CID: %hu successful.\n",
2672 cmd
->logout_cid
, conn
->cid
);
2673 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2676 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2677 cmd
->logout_reason
);
2681 tx_size
= ISCSI_HDR_LEN
;
2682 hdr
= (struct iscsi_logout_rsp
*)cmd
->pdu
;
2683 memset(hdr
, 0, ISCSI_HDR_LEN
);
2684 hdr
->opcode
= ISCSI_OP_LOGOUT_RSP
;
2685 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2686 hdr
->response
= cmd
->logout_response
;
2687 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
2688 cmd
->stat_sn
= conn
->stat_sn
++;
2689 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2691 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2692 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2693 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2695 iov
= &cmd
->iov_misc
[0];
2696 iov
[niov
].iov_base
= cmd
->pdu
;
2697 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
2699 if (conn
->conn_ops
->HeaderDigest
) {
2700 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2702 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2703 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
2704 0, NULL
, (u8
*)header_digest
);
2706 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2707 tx_size
+= ISCSI_CRC_LEN
;
2708 pr_debug("Attaching CRC32C HeaderDigest to"
2709 " Logout Response 0x%08x\n", *header_digest
);
2711 cmd
->iov_misc_count
= niov
;
2712 cmd
->tx_size
= tx_size
;
2714 pr_debug("Sending Logout Response ITT: 0x%08x StatSN:"
2715 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2716 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
,
2717 cmd
->logout_cid
, conn
->cid
);
2723 * Unsolicited NOPIN, either requesting a response or not.
2725 static int iscsit_send_unsolicited_nopin(
2726 struct iscsi_cmd
*cmd
,
2727 struct iscsi_conn
*conn
,
2730 int tx_size
= ISCSI_HDR_LEN
;
2731 struct iscsi_nopin
*hdr
;
2733 hdr
= (struct iscsi_nopin
*) cmd
->pdu
;
2734 memset(hdr
, 0, ISCSI_HDR_LEN
);
2735 hdr
->opcode
= ISCSI_OP_NOOP_IN
;
2736 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2737 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
2738 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2739 cmd
->stat_sn
= conn
->stat_sn
;
2740 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2741 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2742 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2744 if (conn
->conn_ops
->HeaderDigest
) {
2745 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2747 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2748 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
2749 0, NULL
, (u8
*)header_digest
);
2751 tx_size
+= ISCSI_CRC_LEN
;
2752 pr_debug("Attaching CRC32C HeaderDigest to"
2753 " NopIN 0x%08x\n", *header_digest
);
2756 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2757 cmd
->iov_misc
[0].iov_len
= tx_size
;
2758 cmd
->iov_misc_count
= 1;
2759 cmd
->tx_size
= tx_size
;
2761 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2762 " 0x%08x CID: %hu\n", hdr
->ttt
, cmd
->stat_sn
, conn
->cid
);
2767 static int iscsit_send_nopin_response(
2768 struct iscsi_cmd
*cmd
,
2769 struct iscsi_conn
*conn
)
2771 int niov
= 0, tx_size
;
2774 struct iscsi_nopin
*hdr
;
2776 tx_size
= ISCSI_HDR_LEN
;
2777 hdr
= (struct iscsi_nopin
*) cmd
->pdu
;
2778 memset(hdr
, 0, ISCSI_HDR_LEN
);
2779 hdr
->opcode
= ISCSI_OP_NOOP_IN
;
2780 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2781 hton24(hdr
->dlength
, cmd
->buf_ptr_size
);
2782 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2783 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
2784 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2785 cmd
->stat_sn
= conn
->stat_sn
++;
2786 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2788 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2789 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2790 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2792 iov
= &cmd
->iov_misc
[0];
2793 iov
[niov
].iov_base
= cmd
->pdu
;
2794 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
2796 if (conn
->conn_ops
->HeaderDigest
) {
2797 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2799 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2800 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
2801 0, NULL
, (u8
*)header_digest
);
2803 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2804 tx_size
+= ISCSI_CRC_LEN
;
2805 pr_debug("Attaching CRC32C HeaderDigest"
2806 " to NopIn 0x%08x\n", *header_digest
);
2810 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2811 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2813 if (cmd
->buf_ptr_size
) {
2814 iov
[niov
].iov_base
= cmd
->buf_ptr
;
2815 iov
[niov
++].iov_len
= cmd
->buf_ptr_size
;
2816 tx_size
+= cmd
->buf_ptr_size
;
2818 pr_debug("Echoing back %u bytes of ping"
2819 " data.\n", cmd
->buf_ptr_size
);
2821 padding
= ((-cmd
->buf_ptr_size
) & 3);
2823 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
2824 iov
[niov
++].iov_len
= padding
;
2826 pr_debug("Attaching %u additional"
2827 " padding bytes.\n", padding
);
2829 if (conn
->conn_ops
->DataDigest
) {
2830 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2831 cmd
->buf_ptr
, cmd
->buf_ptr_size
,
2832 padding
, (u8
*)&cmd
->pad_bytes
,
2833 (u8
*)&cmd
->data_crc
);
2835 iov
[niov
].iov_base
= &cmd
->data_crc
;
2836 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
2837 tx_size
+= ISCSI_CRC_LEN
;
2838 pr_debug("Attached DataDigest for %u"
2839 " bytes of ping data, CRC 0x%08x\n",
2840 cmd
->buf_ptr_size
, cmd
->data_crc
);
2844 cmd
->iov_misc_count
= niov
;
2845 cmd
->tx_size
= tx_size
;
2847 pr_debug("Sending NOPIN Response ITT: 0x%08x, TTT:"
2848 " 0x%08x, StatSN: 0x%08x, Length %u\n", cmd
->init_task_tag
,
2849 cmd
->targ_xfer_tag
, cmd
->stat_sn
, cmd
->buf_ptr_size
);
2854 int iscsit_send_r2t(
2855 struct iscsi_cmd
*cmd
,
2856 struct iscsi_conn
*conn
)
2859 struct iscsi_r2t
*r2t
;
2860 struct iscsi_r2t_rsp
*hdr
;
2862 r2t
= iscsit_get_r2t_from_list(cmd
);
2866 hdr
= (struct iscsi_r2t_rsp
*) cmd
->pdu
;
2867 memset(hdr
, 0, ISCSI_HDR_LEN
);
2868 hdr
->opcode
= ISCSI_OP_R2T
;
2869 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2870 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
2871 (struct scsi_lun
*)&hdr
->lun
);
2872 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
2873 spin_lock_bh(&conn
->sess
->ttt_lock
);
2874 r2t
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
2875 if (r2t
->targ_xfer_tag
== 0xFFFFFFFF)
2876 r2t
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
2877 spin_unlock_bh(&conn
->sess
->ttt_lock
);
2878 hdr
->ttt
= cpu_to_be32(r2t
->targ_xfer_tag
);
2879 hdr
->statsn
= cpu_to_be32(conn
->stat_sn
);
2880 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2881 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2882 hdr
->r2tsn
= cpu_to_be32(r2t
->r2t_sn
);
2883 hdr
->data_offset
= cpu_to_be32(r2t
->offset
);
2884 hdr
->data_length
= cpu_to_be32(r2t
->xfer_len
);
2886 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2887 cmd
->iov_misc
[0].iov_len
= ISCSI_HDR_LEN
;
2888 tx_size
+= ISCSI_HDR_LEN
;
2890 if (conn
->conn_ops
->HeaderDigest
) {
2891 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2893 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2894 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
2895 0, NULL
, (u8
*)header_digest
);
2897 cmd
->iov_misc
[0].iov_len
+= ISCSI_CRC_LEN
;
2898 tx_size
+= ISCSI_CRC_LEN
;
2899 pr_debug("Attaching CRC32 HeaderDigest for R2T"
2900 " PDU 0x%08x\n", *header_digest
);
2903 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
2904 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
2905 (!r2t
->recovery_r2t
) ? "" : "Recovery ", cmd
->init_task_tag
,
2906 r2t
->targ_xfer_tag
, ntohl(hdr
->statsn
), r2t
->r2t_sn
,
2907 r2t
->offset
, r2t
->xfer_len
, conn
->cid
);
2909 cmd
->iov_misc_count
= 1;
2910 cmd
->tx_size
= tx_size
;
2912 spin_lock_bh(&cmd
->r2t_lock
);
2914 spin_unlock_bh(&cmd
->r2t_lock
);
2920 * type 0: Normal Operation.
2921 * type 1: Called from Storage Transport.
2922 * type 2: Called from iscsi_task_reassign_complete_write() for
2923 * connection recovery.
2925 int iscsit_build_r2ts_for_cmd(
2926 struct iscsi_cmd
*cmd
,
2927 struct iscsi_conn
*conn
,
2931 u32 offset
= 0, xfer_len
= 0;
2933 spin_lock_bh(&cmd
->r2t_lock
);
2934 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
) {
2935 spin_unlock_bh(&cmd
->r2t_lock
);
2939 if (conn
->sess
->sess_ops
->DataSequenceInOrder
&& (type
!= 2))
2940 if (cmd
->r2t_offset
< cmd
->write_data_done
)
2941 cmd
->r2t_offset
= cmd
->write_data_done
;
2943 while (cmd
->outstanding_r2ts
< conn
->sess
->sess_ops
->MaxOutstandingR2T
) {
2944 if (conn
->sess
->sess_ops
->DataSequenceInOrder
) {
2945 offset
= cmd
->r2t_offset
;
2947 if (first_r2t
&& (type
== 2)) {
2948 xfer_len
= ((offset
+
2949 (conn
->sess
->sess_ops
->MaxBurstLength
-
2950 cmd
->next_burst_len
) >
2952 (cmd
->data_length
- offset
) :
2953 (conn
->sess
->sess_ops
->MaxBurstLength
-
2954 cmd
->next_burst_len
));
2956 xfer_len
= ((offset
+
2957 conn
->sess
->sess_ops
->MaxBurstLength
) >
2959 (cmd
->data_length
- offset
) :
2960 conn
->sess
->sess_ops
->MaxBurstLength
;
2962 cmd
->r2t_offset
+= xfer_len
;
2964 if (cmd
->r2t_offset
== cmd
->data_length
)
2965 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
2967 struct iscsi_seq
*seq
;
2969 seq
= iscsit_get_seq_holder_for_r2t(cmd
);
2971 spin_unlock_bh(&cmd
->r2t_lock
);
2975 offset
= seq
->offset
;
2976 xfer_len
= seq
->xfer_len
;
2978 if (cmd
->seq_send_order
== cmd
->seq_count
)
2979 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
2981 cmd
->outstanding_r2ts
++;
2984 if (iscsit_add_r2t_to_list(cmd
, offset
, xfer_len
, 0, 0) < 0) {
2985 spin_unlock_bh(&cmd
->r2t_lock
);
2989 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
)
2992 spin_unlock_bh(&cmd
->r2t_lock
);
2997 static int iscsit_send_status(
2998 struct iscsi_cmd
*cmd
,
2999 struct iscsi_conn
*conn
)
3001 u8 iov_count
= 0, recovery
;
3002 u32 padding
= 0, tx_size
= 0;
3003 struct iscsi_scsi_rsp
*hdr
;
3006 recovery
= (cmd
->i_state
!= ISTATE_SEND_STATUS
);
3008 cmd
->stat_sn
= conn
->stat_sn
++;
3010 spin_lock_bh(&conn
->sess
->session_stats_lock
);
3011 conn
->sess
->rsp_pdus
++;
3012 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
3014 hdr
= (struct iscsi_scsi_rsp
*) cmd
->pdu
;
3015 memset(hdr
, 0, ISCSI_HDR_LEN
);
3016 hdr
->opcode
= ISCSI_OP_SCSI_CMD_RSP
;
3017 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3018 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
3019 hdr
->flags
|= ISCSI_FLAG_CMD_OVERFLOW
;
3020 hdr
->residual_count
= cpu_to_be32(cmd
->residual_count
);
3021 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
3022 hdr
->flags
|= ISCSI_FLAG_CMD_UNDERFLOW
;
3023 hdr
->residual_count
= cpu_to_be32(cmd
->residual_count
);
3025 hdr
->response
= cmd
->iscsi_response
;
3026 hdr
->cmd_status
= cmd
->se_cmd
.scsi_status
;
3027 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
3028 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3030 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3031 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3032 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3034 iov
= &cmd
->iov_misc
[0];
3035 iov
[iov_count
].iov_base
= cmd
->pdu
;
3036 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3037 tx_size
+= ISCSI_HDR_LEN
;
3040 * Attach SENSE DATA payload to iSCSI Response PDU
3042 if (cmd
->se_cmd
.sense_buffer
&&
3043 ((cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
3044 (cmd
->se_cmd
.se_cmd_flags
& SCF_EMULATED_TASK_SENSE
))) {
3045 padding
= -(cmd
->se_cmd
.scsi_sense_length
) & 3;
3046 hton24(hdr
->dlength
, cmd
->se_cmd
.scsi_sense_length
);
3047 iov
[iov_count
].iov_base
= cmd
->se_cmd
.sense_buffer
;
3048 iov
[iov_count
++].iov_len
=
3049 (cmd
->se_cmd
.scsi_sense_length
+ padding
);
3050 tx_size
+= cmd
->se_cmd
.scsi_sense_length
;
3053 memset(cmd
->se_cmd
.sense_buffer
+
3054 cmd
->se_cmd
.scsi_sense_length
, 0, padding
);
3056 pr_debug("Adding %u bytes of padding to"
3057 " SENSE.\n", padding
);
3060 if (conn
->conn_ops
->DataDigest
) {
3061 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3062 cmd
->se_cmd
.sense_buffer
,
3063 (cmd
->se_cmd
.scsi_sense_length
+ padding
),
3064 0, NULL
, (u8
*)&cmd
->data_crc
);
3066 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3067 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3068 tx_size
+= ISCSI_CRC_LEN
;
3070 pr_debug("Attaching CRC32 DataDigest for"
3071 " SENSE, %u bytes CRC 0x%08x\n",
3072 (cmd
->se_cmd
.scsi_sense_length
+ padding
),
3076 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3078 cmd
->se_cmd
.scsi_sense_length
);
3081 if (conn
->conn_ops
->HeaderDigest
) {
3082 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3084 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3085 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
3086 0, NULL
, (u8
*)header_digest
);
3088 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3089 tx_size
+= ISCSI_CRC_LEN
;
3090 pr_debug("Attaching CRC32 HeaderDigest for Response"
3091 " PDU 0x%08x\n", *header_digest
);
3094 cmd
->iov_misc_count
= iov_count
;
3095 cmd
->tx_size
= tx_size
;
3097 pr_debug("Built %sSCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3098 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3099 (!recovery
) ? "" : "Recovery ", cmd
->init_task_tag
,
3100 cmd
->stat_sn
, 0x00, cmd
->se_cmd
.scsi_status
, conn
->cid
);
3105 static u8
iscsit_convert_tcm_tmr_rsp(struct se_tmr_req
*se_tmr
)
3107 switch (se_tmr
->response
) {
3108 case TMR_FUNCTION_COMPLETE
:
3109 return ISCSI_TMF_RSP_COMPLETE
;
3110 case TMR_TASK_DOES_NOT_EXIST
:
3111 return ISCSI_TMF_RSP_NO_TASK
;
3112 case TMR_LUN_DOES_NOT_EXIST
:
3113 return ISCSI_TMF_RSP_NO_LUN
;
3114 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
:
3115 return ISCSI_TMF_RSP_NOT_SUPPORTED
;
3116 case TMR_FUNCTION_AUTHORIZATION_FAILED
:
3117 return ISCSI_TMF_RSP_AUTH_FAILED
;
3118 case TMR_FUNCTION_REJECTED
:
3120 return ISCSI_TMF_RSP_REJECTED
;
3124 static int iscsit_send_task_mgt_rsp(
3125 struct iscsi_cmd
*cmd
,
3126 struct iscsi_conn
*conn
)
3128 struct se_tmr_req
*se_tmr
= cmd
->se_cmd
.se_tmr_req
;
3129 struct iscsi_tm_rsp
*hdr
;
3132 hdr
= (struct iscsi_tm_rsp
*) cmd
->pdu
;
3133 memset(hdr
, 0, ISCSI_HDR_LEN
);
3134 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC_RSP
;
3135 hdr
->response
= iscsit_convert_tcm_tmr_rsp(se_tmr
);
3136 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
3137 cmd
->stat_sn
= conn
->stat_sn
++;
3138 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3140 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3141 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3142 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3144 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
3145 cmd
->iov_misc
[0].iov_len
= ISCSI_HDR_LEN
;
3146 tx_size
+= ISCSI_HDR_LEN
;
3148 if (conn
->conn_ops
->HeaderDigest
) {
3149 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3151 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3152 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
3153 0, NULL
, (u8
*)header_digest
);
3155 cmd
->iov_misc
[0].iov_len
+= ISCSI_CRC_LEN
;
3156 tx_size
+= ISCSI_CRC_LEN
;
3157 pr_debug("Attaching CRC32 HeaderDigest for Task"
3158 " Mgmt Response PDU 0x%08x\n", *header_digest
);
3161 cmd
->iov_misc_count
= 1;
3162 cmd
->tx_size
= tx_size
;
3164 pr_debug("Built Task Management Response ITT: 0x%08x,"
3165 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3166 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
, conn
->cid
);
3171 static int iscsit_build_sendtargets_response(struct iscsi_cmd
*cmd
)
3173 char *payload
= NULL
;
3174 struct iscsi_conn
*conn
= cmd
->conn
;
3175 struct iscsi_portal_group
*tpg
;
3176 struct iscsi_tiqn
*tiqn
;
3177 struct iscsi_tpg_np
*tpg_np
;
3178 int buffer_len
, end_of_buf
= 0, len
= 0, payload_len
= 0;
3179 unsigned char buf
[256];
3181 buffer_len
= (conn
->conn_ops
->MaxRecvDataSegmentLength
> 32768) ?
3182 32768 : conn
->conn_ops
->MaxRecvDataSegmentLength
;
3184 memset(buf
, 0, 256);
3186 payload
= kzalloc(buffer_len
, GFP_KERNEL
);
3188 pr_err("Unable to allocate memory for sendtargets"
3193 spin_lock(&tiqn_lock
);
3194 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
3195 len
= sprintf(buf
, "TargetName=%s", tiqn
->tiqn
);
3198 if ((len
+ payload_len
) > buffer_len
) {
3199 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3203 memcpy((void *)payload
+ payload_len
, buf
, len
);
3206 spin_lock(&tiqn
->tiqn_tpg_lock
);
3207 list_for_each_entry(tpg
, &tiqn
->tiqn_tpg_list
, tpg_list
) {
3209 spin_lock(&tpg
->tpg_state_lock
);
3210 if ((tpg
->tpg_state
== TPG_STATE_FREE
) ||
3211 (tpg
->tpg_state
== TPG_STATE_INACTIVE
)) {
3212 spin_unlock(&tpg
->tpg_state_lock
);
3215 spin_unlock(&tpg
->tpg_state_lock
);
3217 spin_lock(&tpg
->tpg_np_lock
);
3218 list_for_each_entry(tpg_np
, &tpg
->tpg_gnp_list
,
3220 len
= sprintf(buf
, "TargetAddress="
3222 (tpg_np
->tpg_np
->np_sockaddr
.ss_family
== AF_INET6
) ?
3223 "[" : "", tpg_np
->tpg_np
->np_ip
,
3224 (tpg_np
->tpg_np
->np_sockaddr
.ss_family
== AF_INET6
) ?
3225 "]" : "", tpg_np
->tpg_np
->np_port
,
3229 if ((len
+ payload_len
) > buffer_len
) {
3230 spin_unlock(&tpg
->tpg_np_lock
);
3231 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3235 memcpy((void *)payload
+ payload_len
, buf
, len
);
3238 spin_unlock(&tpg
->tpg_np_lock
);
3240 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3245 spin_unlock(&tiqn_lock
);
3247 cmd
->buf_ptr
= payload
;
3253 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3254 * MaxRecvDataSegmentLength.
3256 static int iscsit_send_text_rsp(
3257 struct iscsi_cmd
*cmd
,
3258 struct iscsi_conn
*conn
)
3260 struct iscsi_text_rsp
*hdr
;
3262 u32 padding
= 0, tx_size
= 0;
3263 int text_length
, iov_count
= 0;
3265 text_length
= iscsit_build_sendtargets_response(cmd
);
3266 if (text_length
< 0)
3269 padding
= ((-text_length
) & 3);
3271 memset(cmd
->buf_ptr
+ text_length
, 0, padding
);
3272 pr_debug("Attaching %u additional bytes for"
3273 " padding.\n", padding
);
3276 hdr
= (struct iscsi_text_rsp
*) cmd
->pdu
;
3277 memset(hdr
, 0, ISCSI_HDR_LEN
);
3278 hdr
->opcode
= ISCSI_OP_TEXT_RSP
;
3279 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3280 hton24(hdr
->dlength
, text_length
);
3281 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
3282 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
3283 cmd
->stat_sn
= conn
->stat_sn
++;
3284 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3286 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3287 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3288 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3290 iov
= &cmd
->iov_misc
[0];
3292 iov
[iov_count
].iov_base
= cmd
->pdu
;
3293 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3294 iov
[iov_count
].iov_base
= cmd
->buf_ptr
;
3295 iov
[iov_count
++].iov_len
= text_length
+ padding
;
3297 tx_size
+= (ISCSI_HDR_LEN
+ text_length
+ padding
);
3299 if (conn
->conn_ops
->HeaderDigest
) {
3300 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3302 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3303 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
3304 0, NULL
, (u8
*)header_digest
);
3306 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3307 tx_size
+= ISCSI_CRC_LEN
;
3308 pr_debug("Attaching CRC32 HeaderDigest for"
3309 " Text Response PDU 0x%08x\n", *header_digest
);
3312 if (conn
->conn_ops
->DataDigest
) {
3313 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3314 cmd
->buf_ptr
, (text_length
+ padding
),
3315 0, NULL
, (u8
*)&cmd
->data_crc
);
3317 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3318 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3319 tx_size
+= ISCSI_CRC_LEN
;
3321 pr_debug("Attaching DataDigest for %u bytes of text"
3322 " data, CRC 0x%08x\n", (text_length
+ padding
),
3326 cmd
->iov_misc_count
= iov_count
;
3327 cmd
->tx_size
= tx_size
;
3329 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3330 " Length: %u, CID: %hu\n", cmd
->init_task_tag
, cmd
->stat_sn
,
3331 text_length
, conn
->cid
);
3335 static int iscsit_send_reject(
3336 struct iscsi_cmd
*cmd
,
3337 struct iscsi_conn
*conn
)
3339 u32 iov_count
= 0, tx_size
= 0;
3340 struct iscsi_reject
*hdr
;
3343 hdr
= (struct iscsi_reject
*) cmd
->pdu
;
3344 hdr
->opcode
= ISCSI_OP_REJECT
;
3345 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3346 hton24(hdr
->dlength
, ISCSI_HDR_LEN
);
3347 cmd
->stat_sn
= conn
->stat_sn
++;
3348 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3349 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3350 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3352 iov
= &cmd
->iov_misc
[0];
3354 iov
[iov_count
].iov_base
= cmd
->pdu
;
3355 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3356 iov
[iov_count
].iov_base
= cmd
->buf_ptr
;
3357 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3359 tx_size
= (ISCSI_HDR_LEN
+ ISCSI_HDR_LEN
);
3361 if (conn
->conn_ops
->HeaderDigest
) {
3362 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3364 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3365 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
3366 0, NULL
, (u8
*)header_digest
);
3368 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3369 tx_size
+= ISCSI_CRC_LEN
;
3370 pr_debug("Attaching CRC32 HeaderDigest for"
3371 " REJECT PDU 0x%08x\n", *header_digest
);
3374 if (conn
->conn_ops
->DataDigest
) {
3375 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3376 (unsigned char *)cmd
->buf_ptr
, ISCSI_HDR_LEN
,
3377 0, NULL
, (u8
*)&cmd
->data_crc
);
3379 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3380 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3381 tx_size
+= ISCSI_CRC_LEN
;
3382 pr_debug("Attaching CRC32 DataDigest for REJECT"
3383 " PDU 0x%08x\n", cmd
->data_crc
);
3386 cmd
->iov_misc_count
= iov_count
;
3387 cmd
->tx_size
= tx_size
;
3389 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3390 " CID: %hu\n", ntohl(hdr
->statsn
), hdr
->reason
, conn
->cid
);
3395 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
3397 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
3398 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
3399 wait_for_completion_interruptible_timeout(
3400 &conn
->tx_half_close_comp
,
3401 ISCSI_TX_THREAD_TCP_TIMEOUT
* HZ
);
3407 void iscsit_thread_get_cpumask(struct iscsi_conn
*conn
)
3409 struct iscsi_thread_set
*ts
= conn
->thread_set
;
3412 * thread_id is assigned from iscsit_global->ts_bitmap from
3413 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3415 * Here we use thread_id to determine which CPU that this
3416 * iSCSI connection's iscsi_thread_set will be scheduled to
3419 ord
= ts
->thread_id
% cpumask_weight(cpu_online_mask
);
3421 pr_debug(">>>>>>>>>>>>>>>>>>>> Generated ord: %d from"
3422 " thread_id: %d\n", ord
, ts
->thread_id
);
3424 for_each_online_cpu(cpu
) {
3426 cpumask_set_cpu(cpu
, conn
->conn_cpumask
);
3431 * This should never be reached..
3434 cpumask_setall(conn
->conn_cpumask
);
3437 static inline void iscsit_thread_check_cpumask(
3438 struct iscsi_conn
*conn
,
3439 struct task_struct
*p
,
3444 * mode == 1 signals iscsi_target_tx_thread() usage.
3445 * mode == 0 signals iscsi_target_rx_thread() usage.
3448 if (!conn
->conn_tx_reset_cpumask
)
3450 conn
->conn_tx_reset_cpumask
= 0;
3452 if (!conn
->conn_rx_reset_cpumask
)
3454 conn
->conn_rx_reset_cpumask
= 0;
3457 * Update the CPU mask for this single kthread so that
3458 * both TX and RX kthreads are scheduled to run on the
3461 memset(buf
, 0, 128);
3462 cpumask_scnprintf(buf
, 128, conn
->conn_cpumask
);
3464 pr_debug(">>>>>>>>>>>>>> Calling set_cpus_allowed_ptr():"
3465 " %s for %s\n", buf
, p
->comm
);
3467 set_cpus_allowed_ptr(p
, conn
->conn_cpumask
);
3472 void iscsit_thread_get_cpumask(struct iscsi_conn
*conn
)
3477 #define iscsit_thread_check_cpumask(X, Y, Z) ({})
3478 #endif /* CONFIG_SMP */
3480 int iscsi_target_tx_thread(void *arg
)
3485 int sent_status
= 0;
3488 struct iscsi_cmd
*cmd
= NULL
;
3489 struct iscsi_conn
*conn
;
3490 struct iscsi_queue_req
*qr
= NULL
;
3491 struct se_cmd
*se_cmd
;
3492 struct iscsi_thread_set
*ts
= (struct iscsi_thread_set
*)arg
;
3494 * Allow ourselves to be interrupted by SIGINT so that a
3495 * connection recovery / failure event can be triggered externally.
3497 allow_signal(SIGINT
);
3500 conn
= iscsi_tx_thread_pre_handler(ts
);
3504 eodr
= map_sg
= ret
= sent_status
= use_misc
= 0;
3506 while (!kthread_should_stop()) {
3508 * Ensure that both TX and RX per connection kthreads
3509 * are scheduled to run on the same CPU.
3511 iscsit_thread_check_cpumask(conn
, current
, 1);
3513 schedule_timeout_interruptible(MAX_SCHEDULE_TIMEOUT
);
3515 if ((ts
->status
== ISCSI_THREAD_SET_RESET
) ||
3516 signal_pending(current
))
3520 qr
= iscsit_get_cmd_from_immediate_queue(conn
);
3522 atomic_set(&conn
->check_immediate_queue
, 0);
3525 kmem_cache_free(lio_qr_cache
, qr
);
3527 spin_lock_bh(&cmd
->istate_lock
);
3529 case ISTATE_SEND_R2T
:
3530 spin_unlock_bh(&cmd
->istate_lock
);
3531 ret
= iscsit_send_r2t(cmd
, conn
);
3534 spin_unlock_bh(&cmd
->istate_lock
);
3536 if (cmd
->data_direction
== DMA_TO_DEVICE
)
3537 iscsit_stop_dataout_timer(cmd
);
3539 spin_lock_bh(&conn
->cmd_lock
);
3540 list_del(&cmd
->i_list
);
3541 spin_unlock_bh(&conn
->cmd_lock
);
3543 * Determine if a struct se_cmd is assoicated with
3544 * this struct iscsi_cmd.
3546 if (!(cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
) &&
3548 iscsit_release_cmd(cmd
);
3550 transport_generic_free_cmd(&cmd
->se_cmd
,
3553 case ISTATE_SEND_NOPIN_WANT_RESPONSE
:
3554 spin_unlock_bh(&cmd
->istate_lock
);
3555 iscsit_mod_nopin_response_timer(conn
);
3556 ret
= iscsit_send_unsolicited_nopin(cmd
,
3559 case ISTATE_SEND_NOPIN_NO_RESPONSE
:
3560 spin_unlock_bh(&cmd
->istate_lock
);
3561 ret
= iscsit_send_unsolicited_nopin(cmd
,
3565 pr_err("Unknown Opcode: 0x%02x ITT:"
3566 " 0x%08x, i_state: %d on CID: %hu\n",
3567 cmd
->iscsi_opcode
, cmd
->init_task_tag
, state
,
3569 spin_unlock_bh(&cmd
->istate_lock
);
3573 conn
->tx_immediate_queue
= 0;
3577 if (iscsit_send_tx_data(cmd
, conn
, 1) < 0) {
3578 conn
->tx_immediate_queue
= 0;
3579 iscsit_tx_thread_wait_for_tcp(conn
);
3583 spin_lock_bh(&cmd
->istate_lock
);
3585 case ISTATE_SEND_R2T
:
3586 spin_unlock_bh(&cmd
->istate_lock
);
3587 spin_lock_bh(&cmd
->dataout_timeout_lock
);
3588 iscsit_start_dataout_timer(cmd
, conn
);
3589 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
3591 case ISTATE_SEND_NOPIN_WANT_RESPONSE
:
3592 cmd
->i_state
= ISTATE_SENT_NOPIN_WANT_RESPONSE
;
3593 spin_unlock_bh(&cmd
->istate_lock
);
3595 case ISTATE_SEND_NOPIN_NO_RESPONSE
:
3596 cmd
->i_state
= ISTATE_SENT_STATUS
;
3597 spin_unlock_bh(&cmd
->istate_lock
);
3600 pr_err("Unknown Opcode: 0x%02x ITT:"
3601 " 0x%08x, i_state: %d on CID: %hu\n",
3602 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3604 spin_unlock_bh(&cmd
->istate_lock
);
3609 conn
->tx_immediate_queue
= 0;
3612 qr
= iscsit_get_cmd_from_response_queue(conn
);
3616 kmem_cache_free(lio_qr_cache
, qr
);
3618 spin_lock_bh(&cmd
->istate_lock
);
3621 case ISTATE_SEND_DATAIN
:
3622 spin_unlock_bh(&cmd
->istate_lock
);
3623 ret
= iscsit_send_data_in(cmd
, conn
,
3627 case ISTATE_SEND_STATUS
:
3628 case ISTATE_SEND_STATUS_RECOVERY
:
3629 spin_unlock_bh(&cmd
->istate_lock
);
3631 ret
= iscsit_send_status(cmd
, conn
);
3633 case ISTATE_SEND_LOGOUTRSP
:
3634 spin_unlock_bh(&cmd
->istate_lock
);
3636 ret
= iscsit_send_logout_response(cmd
, conn
);
3638 case ISTATE_SEND_ASYNCMSG
:
3639 spin_unlock_bh(&cmd
->istate_lock
);
3641 ret
= iscsit_send_conn_drop_async_message(
3644 case ISTATE_SEND_NOPIN
:
3645 spin_unlock_bh(&cmd
->istate_lock
);
3647 ret
= iscsit_send_nopin_response(cmd
, conn
);
3649 case ISTATE_SEND_REJECT
:
3650 spin_unlock_bh(&cmd
->istate_lock
);
3652 ret
= iscsit_send_reject(cmd
, conn
);
3654 case ISTATE_SEND_TASKMGTRSP
:
3655 spin_unlock_bh(&cmd
->istate_lock
);
3657 ret
= iscsit_send_task_mgt_rsp(cmd
, conn
);
3660 ret
= iscsit_tmr_post_handler(cmd
, conn
);
3662 iscsit_fall_back_to_erl0(conn
->sess
);
3664 case ISTATE_SEND_TEXTRSP
:
3665 spin_unlock_bh(&cmd
->istate_lock
);
3667 ret
= iscsit_send_text_rsp(cmd
, conn
);
3670 pr_err("Unknown Opcode: 0x%02x ITT:"
3671 " 0x%08x, i_state: %d on CID: %hu\n",
3672 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3674 spin_unlock_bh(&cmd
->istate_lock
);
3678 conn
->tx_response_queue
= 0;
3682 se_cmd
= &cmd
->se_cmd
;
3684 if (map_sg
&& !conn
->conn_ops
->IFMarker
) {
3685 if (iscsit_fe_sendpage_sg(cmd
, conn
) < 0) {
3686 conn
->tx_response_queue
= 0;
3687 iscsit_tx_thread_wait_for_tcp(conn
);
3688 iscsit_unmap_iovec(cmd
);
3692 if (iscsit_send_tx_data(cmd
, conn
, use_misc
) < 0) {
3693 conn
->tx_response_queue
= 0;
3694 iscsit_tx_thread_wait_for_tcp(conn
);
3695 iscsit_unmap_iovec(cmd
);
3700 iscsit_unmap_iovec(cmd
);
3702 spin_lock_bh(&cmd
->istate_lock
);
3704 case ISTATE_SEND_DATAIN
:
3706 goto check_rsp_state
;
3709 cmd
->i_state
= ISTATE_SENT_LAST_DATAIN
;
3711 eodr
= use_misc
= 0;
3712 } else if (eodr
== 2) {
3713 cmd
->i_state
= state
=
3716 eodr
= use_misc
= 0;
3717 goto check_rsp_state
;
3720 case ISTATE_SEND_STATUS
:
3724 case ISTATE_SEND_ASYNCMSG
:
3725 case ISTATE_SEND_NOPIN
:
3726 case ISTATE_SEND_STATUS_RECOVERY
:
3727 case ISTATE_SEND_TEXTRSP
:
3731 case ISTATE_SEND_REJECT
:
3733 if (cmd
->cmd_flags
& ICF_REJECT_FAIL_CONN
) {
3734 cmd
->cmd_flags
&= ~ICF_REJECT_FAIL_CONN
;
3735 spin_unlock_bh(&cmd
->istate_lock
);
3736 complete(&cmd
->reject_comp
);
3739 complete(&cmd
->reject_comp
);
3741 case ISTATE_SEND_TASKMGTRSP
:
3745 case ISTATE_SEND_LOGOUTRSP
:
3746 spin_unlock_bh(&cmd
->istate_lock
);
3747 if (!iscsit_logout_post_handler(cmd
, conn
))
3749 spin_lock_bh(&cmd
->istate_lock
);
3754 pr_err("Unknown Opcode: 0x%02x ITT:"
3755 " 0x%08x, i_state: %d on CID: %hu\n",
3756 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3757 cmd
->i_state
, conn
->cid
);
3758 spin_unlock_bh(&cmd
->istate_lock
);
3763 cmd
->i_state
= ISTATE_SENT_STATUS
;
3766 spin_unlock_bh(&cmd
->istate_lock
);
3768 if (atomic_read(&conn
->check_immediate_queue
))
3773 conn
->tx_response_queue
= 0;
3777 iscsit_take_action_for_connection_exit(conn
);
3783 int iscsi_target_rx_thread(void *arg
)
3786 u8 buffer
[ISCSI_HDR_LEN
], opcode
;
3787 u32 checksum
= 0, digest
= 0;
3788 struct iscsi_conn
*conn
= NULL
;
3789 struct iscsi_thread_set
*ts
= (struct iscsi_thread_set
*)arg
;
3792 * Allow ourselves to be interrupted by SIGINT so that a
3793 * connection recovery / failure event can be triggered externally.
3795 allow_signal(SIGINT
);
3798 conn
= iscsi_rx_thread_pre_handler(ts
);
3802 while (!kthread_should_stop()) {
3804 * Ensure that both TX and RX per connection kthreads
3805 * are scheduled to run on the same CPU.
3807 iscsit_thread_check_cpumask(conn
, current
, 0);
3809 memset(buffer
, 0, ISCSI_HDR_LEN
);
3810 memset(&iov
, 0, sizeof(struct kvec
));
3812 iov
.iov_base
= buffer
;
3813 iov
.iov_len
= ISCSI_HDR_LEN
;
3815 ret
= rx_data(conn
, &iov
, 1, ISCSI_HDR_LEN
);
3816 if (ret
!= ISCSI_HDR_LEN
) {
3817 iscsit_rx_thread_wait_for_tcp(conn
);
3822 * Set conn->bad_hdr for use with REJECT PDUs.
3824 memcpy(&conn
->bad_hdr
, &buffer
, ISCSI_HDR_LEN
);
3826 if (conn
->conn_ops
->HeaderDigest
) {
3827 iov
.iov_base
= &digest
;
3828 iov
.iov_len
= ISCSI_CRC_LEN
;
3830 ret
= rx_data(conn
, &iov
, 1, ISCSI_CRC_LEN
);
3831 if (ret
!= ISCSI_CRC_LEN
) {
3832 iscsit_rx_thread_wait_for_tcp(conn
);
3836 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
3837 buffer
, ISCSI_HDR_LEN
,
3838 0, NULL
, (u8
*)&checksum
);
3840 if (digest
!= checksum
) {
3841 pr_err("HeaderDigest CRC32C failed,"
3842 " received 0x%08x, computed 0x%08x\n",
3845 * Set the PDU to 0xff so it will intentionally
3846 * hit default in the switch below.
3848 memset(buffer
, 0xff, ISCSI_HDR_LEN
);
3849 spin_lock_bh(&conn
->sess
->session_stats_lock
);
3850 conn
->sess
->conn_digest_errors
++;
3851 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
3853 pr_debug("Got HeaderDigest CRC32C"
3854 " 0x%08x\n", checksum
);
3858 if (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
)
3861 opcode
= buffer
[0] & ISCSI_OPCODE_MASK
;
3863 if (conn
->sess
->sess_ops
->SessionType
&&
3864 ((!(opcode
& ISCSI_OP_TEXT
)) ||
3865 (!(opcode
& ISCSI_OP_LOGOUT
)))) {
3866 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3867 " while in Discovery Session, rejecting.\n", opcode
);
3868 iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
3874 case ISCSI_OP_SCSI_CMD
:
3875 if (iscsit_handle_scsi_cmd(conn
, buffer
) < 0)
3878 case ISCSI_OP_SCSI_DATA_OUT
:
3879 if (iscsit_handle_data_out(conn
, buffer
) < 0)
3882 case ISCSI_OP_NOOP_OUT
:
3883 if (iscsit_handle_nop_out(conn
, buffer
) < 0)
3886 case ISCSI_OP_SCSI_TMFUNC
:
3887 if (iscsit_handle_task_mgt_cmd(conn
, buffer
) < 0)
3891 if (iscsit_handle_text_cmd(conn
, buffer
) < 0)
3894 case ISCSI_OP_LOGOUT
:
3895 ret
= iscsit_handle_logout_cmd(conn
, buffer
);
3897 wait_for_completion_timeout(&conn
->conn_logout_comp
,
3898 SECONDS_FOR_LOGOUT_COMP
* HZ
);
3903 case ISCSI_OP_SNACK
:
3904 if (iscsit_handle_snack(conn
, buffer
) < 0)
3908 pr_err("Got unknown iSCSI OpCode: 0x%02x\n",
3910 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
3911 pr_err("Cannot recover from unknown"
3912 " opcode while ERL=0, closing iSCSI connection"
3916 if (!conn
->conn_ops
->OFMarker
) {
3917 pr_err("Unable to recover from unknown"
3918 " opcode while OFMarker=No, closing iSCSI"
3922 if (iscsit_recover_from_unknown_opcode(conn
) < 0) {
3923 pr_err("Unable to recover from unknown"
3924 " opcode, closing iSCSI connection.\n");
3932 if (!signal_pending(current
))
3933 atomic_set(&conn
->transport_failed
, 1);
3934 iscsit_take_action_for_connection_exit(conn
);
3940 static void iscsit_release_commands_from_conn(struct iscsi_conn
*conn
)
3942 struct iscsi_cmd
*cmd
= NULL
, *cmd_tmp
= NULL
;
3943 struct iscsi_session
*sess
= conn
->sess
;
3944 struct se_cmd
*se_cmd
;
3946 * We expect this function to only ever be called from either RX or TX
3947 * thread context via iscsit_close_connection() once the other context
3948 * has been reset -> returned sleeping pre-handler state.
3950 spin_lock_bh(&conn
->cmd_lock
);
3951 list_for_each_entry_safe(cmd
, cmd_tmp
, &conn
->conn_cmd_list
, i_list
) {
3952 if (!(cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
)) {
3954 list_del(&cmd
->i_list
);
3955 spin_unlock_bh(&conn
->cmd_lock
);
3956 iscsit_increment_maxcmdsn(cmd
, sess
);
3957 se_cmd
= &cmd
->se_cmd
;
3959 * Special cases for active iSCSI TMR, and
3960 * transport_lookup_cmd_lun() failing from
3961 * iscsit_get_lun_for_cmd() in iscsit_handle_scsi_cmd().
3963 if (cmd
->tmr_req
&& se_cmd
->transport_wait_for_tasks
)
3964 se_cmd
->transport_wait_for_tasks(se_cmd
, 1, 1);
3965 else if (cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
)
3966 transport_release_cmd(se_cmd
);
3968 iscsit_release_cmd(cmd
);
3970 spin_lock_bh(&conn
->cmd_lock
);
3973 list_del(&cmd
->i_list
);
3974 spin_unlock_bh(&conn
->cmd_lock
);
3976 iscsit_increment_maxcmdsn(cmd
, sess
);
3977 se_cmd
= &cmd
->se_cmd
;
3979 if (se_cmd
->transport_wait_for_tasks
)
3980 se_cmd
->transport_wait_for_tasks(se_cmd
, 1, 1);
3982 spin_lock_bh(&conn
->cmd_lock
);
3984 spin_unlock_bh(&conn
->cmd_lock
);
3987 static void iscsit_stop_timers_for_cmds(
3988 struct iscsi_conn
*conn
)
3990 struct iscsi_cmd
*cmd
;
3992 spin_lock_bh(&conn
->cmd_lock
);
3993 list_for_each_entry(cmd
, &conn
->conn_cmd_list
, i_list
) {
3994 if (cmd
->data_direction
== DMA_TO_DEVICE
)
3995 iscsit_stop_dataout_timer(cmd
);
3997 spin_unlock_bh(&conn
->cmd_lock
);
4000 int iscsit_close_connection(
4001 struct iscsi_conn
*conn
)
4003 int conn_logout
= (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
);
4004 struct iscsi_session
*sess
= conn
->sess
;
4006 pr_debug("Closing iSCSI connection CID %hu on SID:"
4007 " %u\n", conn
->cid
, sess
->sid
);
4009 * Always up conn_logout_comp just in case the RX Thread is sleeping
4010 * and the logout response never got sent because the connection
4013 complete(&conn
->conn_logout_comp
);
4015 iscsi_release_thread_set(conn
);
4017 iscsit_stop_timers_for_cmds(conn
);
4018 iscsit_stop_nopin_response_timer(conn
);
4019 iscsit_stop_nopin_timer(conn
);
4020 iscsit_free_queue_reqs_for_conn(conn
);
4023 * During Connection recovery drop unacknowledged out of order
4024 * commands for this connection, and prepare the other commands
4027 * During normal operation clear the out of order commands (but
4028 * do not free the struct iscsi_ooo_cmdsn's) and release all
4029 * struct iscsi_cmds.
4031 if (atomic_read(&conn
->connection_recovery
)) {
4032 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn
);
4033 iscsit_prepare_cmds_for_realligance(conn
);
4035 iscsit_clear_ooo_cmdsns_for_conn(conn
);
4036 iscsit_release_commands_from_conn(conn
);
4040 * Handle decrementing session or connection usage count if
4041 * a logout response was not able to be sent because the
4042 * connection failed. Fall back to Session Recovery here.
4044 if (atomic_read(&conn
->conn_logout_remove
)) {
4045 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) {
4046 iscsit_dec_conn_usage_count(conn
);
4047 iscsit_dec_session_usage_count(sess
);
4049 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
)
4050 iscsit_dec_conn_usage_count(conn
);
4052 atomic_set(&conn
->conn_logout_remove
, 0);
4053 atomic_set(&sess
->session_reinstatement
, 0);
4054 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4057 spin_lock_bh(&sess
->conn_lock
);
4058 list_del(&conn
->conn_list
);
4061 * Attempt to let the Initiator know this connection failed by
4062 * sending an Connection Dropped Async Message on another
4063 * active connection.
4065 if (atomic_read(&conn
->connection_recovery
))
4066 iscsit_build_conn_drop_async_message(conn
);
4068 spin_unlock_bh(&sess
->conn_lock
);
4071 * If connection reinstatement is being performed on this connection,
4072 * up the connection reinstatement semaphore that is being blocked on
4073 * in iscsit_cause_connection_reinstatement().
4075 spin_lock_bh(&conn
->state_lock
);
4076 if (atomic_read(&conn
->sleep_on_conn_wait_comp
)) {
4077 spin_unlock_bh(&conn
->state_lock
);
4078 complete(&conn
->conn_wait_comp
);
4079 wait_for_completion(&conn
->conn_post_wait_comp
);
4080 spin_lock_bh(&conn
->state_lock
);
4084 * If connection reinstatement is being performed on this connection
4085 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4086 * connection wait rcfr semaphore that is being blocked on
4087 * an iscsit_connection_reinstatement_rcfr().
4089 if (atomic_read(&conn
->connection_wait_rcfr
)) {
4090 spin_unlock_bh(&conn
->state_lock
);
4091 complete(&conn
->conn_wait_rcfr_comp
);
4092 wait_for_completion(&conn
->conn_post_wait_comp
);
4093 spin_lock_bh(&conn
->state_lock
);
4095 atomic_set(&conn
->connection_reinstatement
, 1);
4096 spin_unlock_bh(&conn
->state_lock
);
4099 * If any other processes are accessing this connection pointer we
4100 * must wait until they have completed.
4102 iscsit_check_conn_usage_count(conn
);
4104 if (conn
->conn_rx_hash
.tfm
)
4105 crypto_free_hash(conn
->conn_rx_hash
.tfm
);
4106 if (conn
->conn_tx_hash
.tfm
)
4107 crypto_free_hash(conn
->conn_tx_hash
.tfm
);
4109 if (conn
->conn_cpumask
)
4110 free_cpumask_var(conn
->conn_cpumask
);
4112 kfree(conn
->conn_ops
);
4113 conn
->conn_ops
= NULL
;
4116 if (conn
->conn_flags
& CONNFLAG_SCTP_STRUCT_FILE
) {
4117 kfree(conn
->sock
->file
);
4118 conn
->sock
->file
= NULL
;
4120 sock_release(conn
->sock
);
4122 conn
->thread_set
= NULL
;
4124 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4125 conn
->conn_state
= TARG_CONN_STATE_FREE
;
4128 spin_lock_bh(&sess
->conn_lock
);
4129 atomic_dec(&sess
->nconn
);
4130 pr_debug("Decremented iSCSI connection count to %hu from node:"
4131 " %s\n", atomic_read(&sess
->nconn
),
4132 sess
->sess_ops
->InitiatorName
);
4134 * Make sure that if one connection fails in an non ERL=2 iSCSI
4135 * Session that they all fail.
4137 if ((sess
->sess_ops
->ErrorRecoveryLevel
!= 2) && !conn_logout
&&
4138 !atomic_read(&sess
->session_logout
))
4139 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4142 * If this was not the last connection in the session, and we are
4143 * performing session reinstatement or falling back to ERL=0, call
4144 * iscsit_stop_session() without sleeping to shutdown the other
4145 * active connections.
4147 if (atomic_read(&sess
->nconn
)) {
4148 if (!atomic_read(&sess
->session_reinstatement
) &&
4149 !atomic_read(&sess
->session_fall_back_to_erl0
)) {
4150 spin_unlock_bh(&sess
->conn_lock
);
4153 if (!atomic_read(&sess
->session_stop_active
)) {
4154 atomic_set(&sess
->session_stop_active
, 1);
4155 spin_unlock_bh(&sess
->conn_lock
);
4156 iscsit_stop_session(sess
, 0, 0);
4159 spin_unlock_bh(&sess
->conn_lock
);
4164 * If this was the last connection in the session and one of the
4165 * following is occurring:
4167 * Session Reinstatement is not being performed, and are falling back
4168 * to ERL=0 call iscsit_close_session().
4170 * Session Logout was requested. iscsit_close_session() will be called
4173 * Session Continuation is not being performed, start the Time2Retain
4174 * handler and check if sleep_on_sess_wait_sem is active.
4176 if (!atomic_read(&sess
->session_reinstatement
) &&
4177 atomic_read(&sess
->session_fall_back_to_erl0
)) {
4178 spin_unlock_bh(&sess
->conn_lock
);
4179 iscsit_close_session(sess
);
4182 } else if (atomic_read(&sess
->session_logout
)) {
4183 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4184 sess
->session_state
= TARG_SESS_STATE_FREE
;
4185 spin_unlock_bh(&sess
->conn_lock
);
4187 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4188 complete(&sess
->session_wait_comp
);
4192 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4193 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4195 if (!atomic_read(&sess
->session_continuation
)) {
4196 spin_unlock_bh(&sess
->conn_lock
);
4197 iscsit_start_time2retain_handler(sess
);
4199 spin_unlock_bh(&sess
->conn_lock
);
4201 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4202 complete(&sess
->session_wait_comp
);
4206 spin_unlock_bh(&sess
->conn_lock
);
4211 int iscsit_close_session(struct iscsi_session
*sess
)
4213 struct iscsi_portal_group
*tpg
= ISCSI_TPG_S(sess
);
4214 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4216 if (atomic_read(&sess
->nconn
)) {
4217 pr_err("%d connection(s) still exist for iSCSI session"
4218 " to %s\n", atomic_read(&sess
->nconn
),
4219 sess
->sess_ops
->InitiatorName
);
4223 spin_lock_bh(&se_tpg
->session_lock
);
4224 atomic_set(&sess
->session_logout
, 1);
4225 atomic_set(&sess
->session_reinstatement
, 1);
4226 iscsit_stop_time2retain_timer(sess
);
4227 spin_unlock_bh(&se_tpg
->session_lock
);
4230 * transport_deregister_session_configfs() will clear the
4231 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4232 * can be setting it again with __transport_register_session() in
4233 * iscsi_post_login_handler() again after the iscsit_stop_session()
4234 * completes in iscsi_np context.
4236 transport_deregister_session_configfs(sess
->se_sess
);
4239 * If any other processes are accessing this session pointer we must
4240 * wait until they have completed. If we are in an interrupt (the
4241 * time2retain handler) and contain and active session usage count we
4242 * restart the timer and exit.
4244 if (!in_interrupt()) {
4245 if (iscsit_check_session_usage_count(sess
) == 1)
4246 iscsit_stop_session(sess
, 1, 1);
4248 if (iscsit_check_session_usage_count(sess
) == 2) {
4249 atomic_set(&sess
->session_logout
, 0);
4250 iscsit_start_time2retain_handler(sess
);
4255 transport_deregister_session(sess
->se_sess
);
4257 if (sess
->sess_ops
->ErrorRecoveryLevel
== 2)
4258 iscsit_free_connection_recovery_entires(sess
);
4260 iscsit_free_all_ooo_cmdsns(sess
);
4262 spin_lock_bh(&se_tpg
->session_lock
);
4263 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4264 sess
->session_state
= TARG_SESS_STATE_FREE
;
4265 pr_debug("Released iSCSI session from node: %s\n",
4266 sess
->sess_ops
->InitiatorName
);
4269 tpg
->tpg_tiqn
->tiqn_nsessions
--;
4271 pr_debug("Decremented number of active iSCSI Sessions on"
4272 " iSCSI TPG: %hu to %u\n", tpg
->tpgt
, tpg
->nsessions
);
4274 spin_lock(&sess_idr_lock
);
4275 idr_remove(&sess_idr
, sess
->session_index
);
4276 spin_unlock(&sess_idr_lock
);
4278 kfree(sess
->sess_ops
);
4279 sess
->sess_ops
= NULL
;
4280 spin_unlock_bh(&se_tpg
->session_lock
);
4286 static void iscsit_logout_post_handler_closesession(
4287 struct iscsi_conn
*conn
)
4289 struct iscsi_session
*sess
= conn
->sess
;
4291 iscsi_set_thread_clear(conn
, ISCSI_CLEAR_TX_THREAD
);
4292 iscsi_set_thread_set_signal(conn
, ISCSI_SIGNAL_TX_THREAD
);
4294 atomic_set(&conn
->conn_logout_remove
, 0);
4295 complete(&conn
->conn_logout_comp
);
4297 iscsit_dec_conn_usage_count(conn
);
4298 iscsit_stop_session(sess
, 1, 1);
4299 iscsit_dec_session_usage_count(sess
);
4300 iscsit_close_session(sess
);
4303 static void iscsit_logout_post_handler_samecid(
4304 struct iscsi_conn
*conn
)
4306 iscsi_set_thread_clear(conn
, ISCSI_CLEAR_TX_THREAD
);
4307 iscsi_set_thread_set_signal(conn
, ISCSI_SIGNAL_TX_THREAD
);
4309 atomic_set(&conn
->conn_logout_remove
, 0);
4310 complete(&conn
->conn_logout_comp
);
4312 iscsit_cause_connection_reinstatement(conn
, 1);
4313 iscsit_dec_conn_usage_count(conn
);
4316 static void iscsit_logout_post_handler_diffcid(
4317 struct iscsi_conn
*conn
,
4320 struct iscsi_conn
*l_conn
;
4321 struct iscsi_session
*sess
= conn
->sess
;
4326 spin_lock_bh(&sess
->conn_lock
);
4327 list_for_each_entry(l_conn
, &sess
->sess_conn_list
, conn_list
) {
4328 if (l_conn
->cid
== cid
) {
4329 iscsit_inc_conn_usage_count(l_conn
);
4333 spin_unlock_bh(&sess
->conn_lock
);
4339 l_conn
->sock
->ops
->shutdown(l_conn
->sock
, RCV_SHUTDOWN
);
4341 spin_lock_bh(&l_conn
->state_lock
);
4342 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4343 l_conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
4344 spin_unlock_bh(&l_conn
->state_lock
);
4346 iscsit_cause_connection_reinstatement(l_conn
, 1);
4347 iscsit_dec_conn_usage_count(l_conn
);
4351 * Return of 0 causes the TX thread to restart.
4353 static int iscsit_logout_post_handler(
4354 struct iscsi_cmd
*cmd
,
4355 struct iscsi_conn
*conn
)
4359 switch (cmd
->logout_reason
) {
4360 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
4361 switch (cmd
->logout_response
) {
4362 case ISCSI_LOGOUT_SUCCESS
:
4363 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4365 iscsit_logout_post_handler_closesession(conn
);
4370 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
4371 if (conn
->cid
== cmd
->logout_cid
) {
4372 switch (cmd
->logout_response
) {
4373 case ISCSI_LOGOUT_SUCCESS
:
4374 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4376 iscsit_logout_post_handler_samecid(conn
);
4381 switch (cmd
->logout_response
) {
4382 case ISCSI_LOGOUT_SUCCESS
:
4383 iscsit_logout_post_handler_diffcid(conn
,
4386 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4387 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4394 case ISCSI_LOGOUT_REASON_RECOVERY
:
4395 switch (cmd
->logout_response
) {
4396 case ISCSI_LOGOUT_SUCCESS
:
4397 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4398 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
:
4399 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4412 void iscsit_fail_session(struct iscsi_session
*sess
)
4414 struct iscsi_conn
*conn
;
4416 spin_lock_bh(&sess
->conn_lock
);
4417 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
) {
4418 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4419 conn
->conn_state
= TARG_CONN_STATE_CLEANUP_WAIT
;
4421 spin_unlock_bh(&sess
->conn_lock
);
4423 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4424 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4427 int iscsit_free_session(struct iscsi_session
*sess
)
4429 u16 conn_count
= atomic_read(&sess
->nconn
);
4430 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4433 spin_lock_bh(&sess
->conn_lock
);
4434 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4436 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4438 if (conn_count
== 0)
4441 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4444 iscsit_inc_conn_usage_count(conn_tmp
);
4447 iscsit_inc_conn_usage_count(conn
);
4449 spin_unlock_bh(&sess
->conn_lock
);
4450 iscsit_cause_connection_reinstatement(conn
, 1);
4451 spin_lock_bh(&sess
->conn_lock
);
4453 iscsit_dec_conn_usage_count(conn
);
4455 iscsit_dec_conn_usage_count(conn_tmp
);
4460 if (atomic_read(&sess
->nconn
)) {
4461 spin_unlock_bh(&sess
->conn_lock
);
4462 wait_for_completion(&sess
->session_wait_comp
);
4464 spin_unlock_bh(&sess
->conn_lock
);
4466 iscsit_close_session(sess
);
4470 void iscsit_stop_session(
4471 struct iscsi_session
*sess
,
4473 int connection_sleep
)
4475 u16 conn_count
= atomic_read(&sess
->nconn
);
4476 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4479 spin_lock_bh(&sess
->conn_lock
);
4481 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4483 if (connection_sleep
) {
4484 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4486 if (conn_count
== 0)
4489 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4492 iscsit_inc_conn_usage_count(conn_tmp
);
4495 iscsit_inc_conn_usage_count(conn
);
4497 spin_unlock_bh(&sess
->conn_lock
);
4498 iscsit_cause_connection_reinstatement(conn
, 1);
4499 spin_lock_bh(&sess
->conn_lock
);
4501 iscsit_dec_conn_usage_count(conn
);
4503 iscsit_dec_conn_usage_count(conn_tmp
);
4507 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
)
4508 iscsit_cause_connection_reinstatement(conn
, 0);
4511 if (session_sleep
&& atomic_read(&sess
->nconn
)) {
4512 spin_unlock_bh(&sess
->conn_lock
);
4513 wait_for_completion(&sess
->session_wait_comp
);
4515 spin_unlock_bh(&sess
->conn_lock
);
4518 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group
*tpg
, int force
)
4520 struct iscsi_session
*sess
;
4521 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4522 struct se_session
*se_sess
, *se_sess_tmp
;
4523 int session_count
= 0;
4525 spin_lock_bh(&se_tpg
->session_lock
);
4526 if (tpg
->nsessions
&& !force
) {
4527 spin_unlock_bh(&se_tpg
->session_lock
);
4531 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &se_tpg
->tpg_sess_list
,
4533 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4535 spin_lock(&sess
->conn_lock
);
4536 if (atomic_read(&sess
->session_fall_back_to_erl0
) ||
4537 atomic_read(&sess
->session_logout
) ||
4538 (sess
->time2retain_timer_flags
& ISCSI_TF_EXPIRED
)) {
4539 spin_unlock(&sess
->conn_lock
);
4542 atomic_set(&sess
->session_reinstatement
, 1);
4543 spin_unlock(&sess
->conn_lock
);
4544 spin_unlock_bh(&se_tpg
->session_lock
);
4546 iscsit_free_session(sess
);
4547 spin_lock_bh(&se_tpg
->session_lock
);
4551 spin_unlock_bh(&se_tpg
->session_lock
);
4553 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4554 " Group: %hu\n", session_count
, tpg
->tpgt
);
4558 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4559 MODULE_VERSION("4.1.x");
4560 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4561 MODULE_LICENSE("GPL");
4563 module_init(iscsi_target_init_module
);
4564 module_exit(iscsi_target_cleanup_module
);