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 <linux/module.h>
26 #include <linux/idr.h>
27 #include <asm/unaligned.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/iscsi_proto.h>
30 #include <target/target_core_base.h>
31 #include <target/target_core_fabric.h>
33 #include "iscsi_target_core.h"
34 #include "iscsi_target_parameters.h"
35 #include "iscsi_target_seq_pdu_list.h"
36 #include "iscsi_target_tq.h"
37 #include "iscsi_target_configfs.h"
38 #include "iscsi_target_datain_values.h"
39 #include "iscsi_target_erl0.h"
40 #include "iscsi_target_erl1.h"
41 #include "iscsi_target_erl2.h"
42 #include "iscsi_target_login.h"
43 #include "iscsi_target_tmr.h"
44 #include "iscsi_target_tpg.h"
45 #include "iscsi_target_util.h"
46 #include "iscsi_target.h"
47 #include "iscsi_target_device.h"
48 #include "iscsi_target_stat.h"
50 static LIST_HEAD(g_tiqn_list
);
51 static LIST_HEAD(g_np_list
);
52 static DEFINE_SPINLOCK(tiqn_lock
);
53 static DEFINE_MUTEX(np_lock
);
55 static struct idr tiqn_idr
;
57 struct mutex auth_id_lock
;
58 spinlock_t sess_idr_lock
;
60 struct iscsit_global
*iscsit_global
;
62 struct kmem_cache
*lio_cmd_cache
;
63 struct kmem_cache
*lio_qr_cache
;
64 struct kmem_cache
*lio_dr_cache
;
65 struct kmem_cache
*lio_ooo_cache
;
66 struct kmem_cache
*lio_r2t_cache
;
68 static int iscsit_handle_immediate_data(struct iscsi_cmd
*,
69 unsigned char *buf
, u32
);
70 static int iscsit_logout_post_handler(struct iscsi_cmd
*, struct iscsi_conn
*);
72 struct iscsi_tiqn
*iscsit_get_tiqn_for_login(unsigned char *buf
)
74 struct iscsi_tiqn
*tiqn
= NULL
;
76 spin_lock(&tiqn_lock
);
77 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
78 if (!strcmp(tiqn
->tiqn
, buf
)) {
80 spin_lock(&tiqn
->tiqn_state_lock
);
81 if (tiqn
->tiqn_state
== TIQN_STATE_ACTIVE
) {
82 tiqn
->tiqn_access_count
++;
83 spin_unlock(&tiqn
->tiqn_state_lock
);
84 spin_unlock(&tiqn_lock
);
87 spin_unlock(&tiqn
->tiqn_state_lock
);
90 spin_unlock(&tiqn_lock
);
95 static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn
*tiqn
)
97 spin_lock(&tiqn
->tiqn_state_lock
);
98 if (tiqn
->tiqn_state
== TIQN_STATE_ACTIVE
) {
99 tiqn
->tiqn_state
= TIQN_STATE_SHUTDOWN
;
100 spin_unlock(&tiqn
->tiqn_state_lock
);
103 spin_unlock(&tiqn
->tiqn_state_lock
);
108 void iscsit_put_tiqn_for_login(struct iscsi_tiqn
*tiqn
)
110 spin_lock(&tiqn
->tiqn_state_lock
);
111 tiqn
->tiqn_access_count
--;
112 spin_unlock(&tiqn
->tiqn_state_lock
);
116 * Note that IQN formatting is expected to be done in userspace, and
117 * no explict IQN format checks are done here.
119 struct iscsi_tiqn
*iscsit_add_tiqn(unsigned char *buf
)
121 struct iscsi_tiqn
*tiqn
= NULL
;
124 if (strlen(buf
) >= ISCSI_IQN_LEN
) {
125 pr_err("Target IQN exceeds %d bytes\n",
127 return ERR_PTR(-EINVAL
);
130 tiqn
= kzalloc(sizeof(struct iscsi_tiqn
), GFP_KERNEL
);
132 pr_err("Unable to allocate struct iscsi_tiqn\n");
133 return ERR_PTR(-ENOMEM
);
136 sprintf(tiqn
->tiqn
, "%s", buf
);
137 INIT_LIST_HEAD(&tiqn
->tiqn_list
);
138 INIT_LIST_HEAD(&tiqn
->tiqn_tpg_list
);
139 spin_lock_init(&tiqn
->tiqn_state_lock
);
140 spin_lock_init(&tiqn
->tiqn_tpg_lock
);
141 spin_lock_init(&tiqn
->sess_err_stats
.lock
);
142 spin_lock_init(&tiqn
->login_stats
.lock
);
143 spin_lock_init(&tiqn
->logout_stats
.lock
);
145 if (!idr_pre_get(&tiqn_idr
, GFP_KERNEL
)) {
146 pr_err("idr_pre_get() for tiqn_idr failed\n");
148 return ERR_PTR(-ENOMEM
);
150 tiqn
->tiqn_state
= TIQN_STATE_ACTIVE
;
152 spin_lock(&tiqn_lock
);
153 ret
= idr_get_new(&tiqn_idr
, NULL
, &tiqn
->tiqn_index
);
155 pr_err("idr_get_new() failed for tiqn->tiqn_index\n");
156 spin_unlock(&tiqn_lock
);
160 list_add_tail(&tiqn
->tiqn_list
, &g_tiqn_list
);
161 spin_unlock(&tiqn_lock
);
163 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn
->tiqn
);
169 static void iscsit_wait_for_tiqn(struct iscsi_tiqn
*tiqn
)
172 * Wait for accesses to said struct iscsi_tiqn to end.
174 spin_lock(&tiqn
->tiqn_state_lock
);
175 while (tiqn
->tiqn_access_count
!= 0) {
176 spin_unlock(&tiqn
->tiqn_state_lock
);
178 spin_lock(&tiqn
->tiqn_state_lock
);
180 spin_unlock(&tiqn
->tiqn_state_lock
);
183 void iscsit_del_tiqn(struct iscsi_tiqn
*tiqn
)
186 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
187 * while holding tiqn->tiqn_state_lock. This means that all subsequent
188 * attempts to access this struct iscsi_tiqn will fail from both transport
189 * fabric and control code paths.
191 if (iscsit_set_tiqn_shutdown(tiqn
) < 0) {
192 pr_err("iscsit_set_tiqn_shutdown() failed\n");
196 iscsit_wait_for_tiqn(tiqn
);
198 spin_lock(&tiqn_lock
);
199 list_del(&tiqn
->tiqn_list
);
200 idr_remove(&tiqn_idr
, tiqn
->tiqn_index
);
201 spin_unlock(&tiqn_lock
);
203 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
208 int iscsit_access_np(struct iscsi_np
*np
, struct iscsi_portal_group
*tpg
)
212 * Determine if the network portal is accepting storage traffic.
214 spin_lock_bh(&np
->np_thread_lock
);
215 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
216 spin_unlock_bh(&np
->np_thread_lock
);
219 if (np
->np_login_tpg
) {
220 pr_err("np->np_login_tpg() is not NULL!\n");
221 spin_unlock_bh(&np
->np_thread_lock
);
224 spin_unlock_bh(&np
->np_thread_lock
);
226 * Determine if the portal group is accepting storage traffic.
228 spin_lock_bh(&tpg
->tpg_state_lock
);
229 if (tpg
->tpg_state
!= TPG_STATE_ACTIVE
) {
230 spin_unlock_bh(&tpg
->tpg_state_lock
);
233 spin_unlock_bh(&tpg
->tpg_state_lock
);
236 * Here we serialize access across the TIQN+TPG Tuple.
238 ret
= mutex_lock_interruptible(&tpg
->np_login_lock
);
239 if ((ret
!= 0) || signal_pending(current
))
242 spin_lock_bh(&np
->np_thread_lock
);
243 np
->np_login_tpg
= tpg
;
244 spin_unlock_bh(&np
->np_thread_lock
);
249 int iscsit_deaccess_np(struct iscsi_np
*np
, struct iscsi_portal_group
*tpg
)
251 struct iscsi_tiqn
*tiqn
= tpg
->tpg_tiqn
;
253 spin_lock_bh(&np
->np_thread_lock
);
254 np
->np_login_tpg
= NULL
;
255 spin_unlock_bh(&np
->np_thread_lock
);
257 mutex_unlock(&tpg
->np_login_lock
);
260 iscsit_put_tiqn_for_login(tiqn
);
266 * Called with mutex np_lock held
268 static struct iscsi_np
*iscsit_get_np(
269 struct __kernel_sockaddr_storage
*sockaddr
,
270 int network_transport
)
272 struct sockaddr_in
*sock_in
, *sock_in_e
;
273 struct sockaddr_in6
*sock_in6
, *sock_in6_e
;
278 list_for_each_entry(np
, &g_np_list
, np_list
) {
279 spin_lock_bh(&np
->np_thread_lock
);
280 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
281 spin_unlock_bh(&np
->np_thread_lock
);
285 if (sockaddr
->ss_family
== AF_INET6
) {
286 sock_in6
= (struct sockaddr_in6
*)sockaddr
;
287 sock_in6_e
= (struct sockaddr_in6
*)&np
->np_sockaddr
;
289 if (!memcmp(&sock_in6
->sin6_addr
.in6_u
,
290 &sock_in6_e
->sin6_addr
.in6_u
,
291 sizeof(struct in6_addr
)))
294 port
= ntohs(sock_in6
->sin6_port
);
296 sock_in
= (struct sockaddr_in
*)sockaddr
;
297 sock_in_e
= (struct sockaddr_in
*)&np
->np_sockaddr
;
299 if (sock_in
->sin_addr
.s_addr
==
300 sock_in_e
->sin_addr
.s_addr
)
303 port
= ntohs(sock_in
->sin_port
);
306 if ((ip_match
== 1) && (np
->np_port
== port
) &&
307 (np
->np_network_transport
== network_transport
)) {
309 * Increment the np_exports reference count now to
310 * prevent iscsit_del_np() below from being called
311 * while iscsi_tpg_add_network_portal() is called.
314 spin_unlock_bh(&np
->np_thread_lock
);
317 spin_unlock_bh(&np
->np_thread_lock
);
323 struct iscsi_np
*iscsit_add_np(
324 struct __kernel_sockaddr_storage
*sockaddr
,
326 int network_transport
)
328 struct sockaddr_in
*sock_in
;
329 struct sockaddr_in6
*sock_in6
;
333 mutex_lock(&np_lock
);
336 * Locate the existing struct iscsi_np if already active..
338 np
= iscsit_get_np(sockaddr
, network_transport
);
340 mutex_unlock(&np_lock
);
344 np
= kzalloc(sizeof(struct iscsi_np
), GFP_KERNEL
);
346 pr_err("Unable to allocate memory for struct iscsi_np\n");
347 mutex_unlock(&np_lock
);
348 return ERR_PTR(-ENOMEM
);
351 np
->np_flags
|= NPF_IP_NETWORK
;
352 if (sockaddr
->ss_family
== AF_INET6
) {
353 sock_in6
= (struct sockaddr_in6
*)sockaddr
;
354 snprintf(np
->np_ip
, IPV6_ADDRESS_SPACE
, "%s", ip_str
);
355 np
->np_port
= ntohs(sock_in6
->sin6_port
);
357 sock_in
= (struct sockaddr_in
*)sockaddr
;
358 sprintf(np
->np_ip
, "%s", ip_str
);
359 np
->np_port
= ntohs(sock_in
->sin_port
);
362 np
->np_network_transport
= network_transport
;
363 spin_lock_init(&np
->np_thread_lock
);
364 init_completion(&np
->np_restart_comp
);
365 INIT_LIST_HEAD(&np
->np_list
);
367 ret
= iscsi_target_setup_login_socket(np
, sockaddr
);
370 mutex_unlock(&np_lock
);
374 np
->np_thread
= kthread_run(iscsi_target_login_thread
, np
, "iscsi_np");
375 if (IS_ERR(np
->np_thread
)) {
376 pr_err("Unable to create kthread: iscsi_np\n");
377 ret
= PTR_ERR(np
->np_thread
);
379 mutex_unlock(&np_lock
);
383 * Increment the np_exports reference count now to prevent
384 * iscsit_del_np() below from being run while a new call to
385 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
386 * active. We don't need to hold np->np_thread_lock at this
387 * point because iscsi_np has not been added to g_np_list yet.
390 np
->np_thread_state
= ISCSI_NP_THREAD_ACTIVE
;
392 list_add_tail(&np
->np_list
, &g_np_list
);
393 mutex_unlock(&np_lock
);
395 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
396 np
->np_ip
, np
->np_port
, (np
->np_network_transport
== ISCSI_TCP
) ?
402 int iscsit_reset_np_thread(
404 struct iscsi_tpg_np
*tpg_np
,
405 struct iscsi_portal_group
*tpg
)
407 spin_lock_bh(&np
->np_thread_lock
);
410 * The reset operation need only be performed when the
411 * passed struct iscsi_portal_group has a login in progress
412 * to one of the network portals.
414 if (tpg_np
->tpg_np
->np_login_tpg
!= tpg
) {
415 spin_unlock_bh(&np
->np_thread_lock
);
419 if (np
->np_thread_state
== ISCSI_NP_THREAD_INACTIVE
) {
420 spin_unlock_bh(&np
->np_thread_lock
);
423 np
->np_thread_state
= ISCSI_NP_THREAD_RESET
;
426 spin_unlock_bh(&np
->np_thread_lock
);
427 send_sig(SIGINT
, np
->np_thread
, 1);
428 wait_for_completion(&np
->np_restart_comp
);
429 spin_lock_bh(&np
->np_thread_lock
);
431 spin_unlock_bh(&np
->np_thread_lock
);
436 int iscsit_del_np_comm(struct iscsi_np
*np
)
439 sock_release(np
->np_socket
);
443 int iscsit_del_np(struct iscsi_np
*np
)
445 spin_lock_bh(&np
->np_thread_lock
);
447 if (np
->np_exports
) {
448 spin_unlock_bh(&np
->np_thread_lock
);
451 np
->np_thread_state
= ISCSI_NP_THREAD_SHUTDOWN
;
452 spin_unlock_bh(&np
->np_thread_lock
);
456 * We need to send the signal to wakeup Linux/Net
457 * which may be sleeping in sock_accept()..
459 send_sig(SIGINT
, np
->np_thread
, 1);
460 kthread_stop(np
->np_thread
);
462 iscsit_del_np_comm(np
);
464 mutex_lock(&np_lock
);
465 list_del(&np
->np_list
);
466 mutex_unlock(&np_lock
);
468 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
469 np
->np_ip
, np
->np_port
, (np
->np_network_transport
== ISCSI_TCP
) ?
476 static int __init
iscsi_target_init_module(void)
480 pr_debug("iSCSI-Target "ISCSIT_VERSION
"\n");
482 iscsit_global
= kzalloc(sizeof(struct iscsit_global
), GFP_KERNEL
);
483 if (!iscsit_global
) {
484 pr_err("Unable to allocate memory for iscsit_global\n");
487 mutex_init(&auth_id_lock
);
488 spin_lock_init(&sess_idr_lock
);
492 ret
= iscsi_target_register_configfs();
496 ret
= iscsi_thread_set_init();
500 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT
) !=
501 TARGET_THREAD_SET_COUNT
) {
502 pr_err("iscsi_allocate_thread_sets() returned"
503 " unexpected value!\n");
507 lio_cmd_cache
= kmem_cache_create("lio_cmd_cache",
508 sizeof(struct iscsi_cmd
), __alignof__(struct iscsi_cmd
),
510 if (!lio_cmd_cache
) {
511 pr_err("Unable to kmem_cache_create() for"
516 lio_qr_cache
= kmem_cache_create("lio_qr_cache",
517 sizeof(struct iscsi_queue_req
),
518 __alignof__(struct iscsi_queue_req
), 0, NULL
);
520 pr_err("nable to kmem_cache_create() for"
525 lio_dr_cache
= kmem_cache_create("lio_dr_cache",
526 sizeof(struct iscsi_datain_req
),
527 __alignof__(struct iscsi_datain_req
), 0, NULL
);
529 pr_err("Unable to kmem_cache_create() for"
534 lio_ooo_cache
= kmem_cache_create("lio_ooo_cache",
535 sizeof(struct iscsi_ooo_cmdsn
),
536 __alignof__(struct iscsi_ooo_cmdsn
), 0, NULL
);
537 if (!lio_ooo_cache
) {
538 pr_err("Unable to kmem_cache_create() for"
543 lio_r2t_cache
= kmem_cache_create("lio_r2t_cache",
544 sizeof(struct iscsi_r2t
), __alignof__(struct iscsi_r2t
),
546 if (!lio_r2t_cache
) {
547 pr_err("Unable to kmem_cache_create() for"
552 if (iscsit_load_discovery_tpg() < 0)
557 kmem_cache_destroy(lio_r2t_cache
);
559 kmem_cache_destroy(lio_ooo_cache
);
561 kmem_cache_destroy(lio_dr_cache
);
563 kmem_cache_destroy(lio_qr_cache
);
565 kmem_cache_destroy(lio_cmd_cache
);
567 iscsi_deallocate_thread_sets();
569 iscsi_thread_set_free();
571 iscsi_target_deregister_configfs();
573 kfree(iscsit_global
);
577 static void __exit
iscsi_target_cleanup_module(void)
579 iscsi_deallocate_thread_sets();
580 iscsi_thread_set_free();
581 iscsit_release_discovery_tpg();
582 kmem_cache_destroy(lio_cmd_cache
);
583 kmem_cache_destroy(lio_qr_cache
);
584 kmem_cache_destroy(lio_dr_cache
);
585 kmem_cache_destroy(lio_ooo_cache
);
586 kmem_cache_destroy(lio_r2t_cache
);
588 iscsi_target_deregister_configfs();
590 kfree(iscsit_global
);
593 int iscsit_add_reject(
597 struct iscsi_conn
*conn
)
599 struct iscsi_cmd
*cmd
;
600 struct iscsi_reject
*hdr
;
603 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
607 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
609 cmd
->cmd_flags
|= ICF_REJECT_FAIL_CONN
;
611 hdr
= (struct iscsi_reject
*) cmd
->pdu
;
612 hdr
->reason
= reason
;
614 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
616 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
617 iscsit_release_cmd(cmd
);
621 spin_lock_bh(&conn
->cmd_lock
);
622 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
623 spin_unlock_bh(&conn
->cmd_lock
);
625 cmd
->i_state
= ISTATE_SEND_REJECT
;
626 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
628 ret
= wait_for_completion_interruptible(&cmd
->reject_comp
);
632 return (!fail_conn
) ? 0 : -1;
635 int iscsit_add_reject_from_cmd(
640 struct iscsi_cmd
*cmd
)
642 struct iscsi_conn
*conn
;
643 struct iscsi_reject
*hdr
;
647 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
653 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
655 cmd
->cmd_flags
|= ICF_REJECT_FAIL_CONN
;
657 hdr
= (struct iscsi_reject
*) cmd
->pdu
;
658 hdr
->reason
= reason
;
660 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
662 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
663 iscsit_release_cmd(cmd
);
668 spin_lock_bh(&conn
->cmd_lock
);
669 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
670 spin_unlock_bh(&conn
->cmd_lock
);
673 cmd
->i_state
= ISTATE_SEND_REJECT
;
674 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
676 ret
= wait_for_completion_interruptible(&cmd
->reject_comp
);
680 return (!fail_conn
) ? 0 : -1;
684 * Map some portion of the allocated scatterlist to an iovec, suitable for
685 * kernel sockets to copy data in/out. This handles both pages and slab-allocated
686 * buffers, since we have been tricky and mapped t_mem_sg to the buffer in
687 * either case (see iscsit_alloc_buffs)
689 static int iscsit_map_iovec(
690 struct iscsi_cmd
*cmd
,
696 struct scatterlist
*sg
;
697 unsigned int page_off
;
700 * We have a private mapping of the allocated pages in t_mem_sg.
701 * At this point, we also know each contains a page.
703 sg
= &cmd
->t_mem_sg
[data_offset
/ PAGE_SIZE
];
704 page_off
= (data_offset
% PAGE_SIZE
);
706 cmd
->first_data_sg
= sg
;
707 cmd
->first_data_sg_off
= page_off
;
709 while (data_length
) {
710 u32 cur_len
= min_t(u32
, data_length
, sg
->length
- page_off
);
712 iov
[i
].iov_base
= kmap(sg_page(sg
)) + sg
->offset
+ page_off
;
713 iov
[i
].iov_len
= cur_len
;
715 data_length
-= cur_len
;
721 cmd
->kmapped_nents
= i
;
726 static void iscsit_unmap_iovec(struct iscsi_cmd
*cmd
)
729 struct scatterlist
*sg
;
731 sg
= cmd
->first_data_sg
;
733 for (i
= 0; i
< cmd
->kmapped_nents
; i
++)
734 kunmap(sg_page(&sg
[i
]));
737 static void iscsit_ack_from_expstatsn(struct iscsi_conn
*conn
, u32 exp_statsn
)
739 struct iscsi_cmd
*cmd
;
741 conn
->exp_statsn
= exp_statsn
;
743 spin_lock_bh(&conn
->cmd_lock
);
744 list_for_each_entry(cmd
, &conn
->conn_cmd_list
, i_list
) {
745 spin_lock(&cmd
->istate_lock
);
746 if ((cmd
->i_state
== ISTATE_SENT_STATUS
) &&
747 (cmd
->stat_sn
< exp_statsn
)) {
748 cmd
->i_state
= ISTATE_REMOVE
;
749 spin_unlock(&cmd
->istate_lock
);
750 iscsit_add_cmd_to_immediate_queue(cmd
, conn
,
754 spin_unlock(&cmd
->istate_lock
);
756 spin_unlock_bh(&conn
->cmd_lock
);
759 static int iscsit_allocate_iovecs(struct iscsi_cmd
*cmd
)
761 u32 iov_count
= (cmd
->se_cmd
.t_data_nents
== 0) ? 1 :
762 cmd
->se_cmd
.t_data_nents
;
764 iov_count
+= ISCSI_IOV_DATA_BUFFER
;
766 cmd
->iov_data
= kzalloc(iov_count
* sizeof(struct kvec
), GFP_KERNEL
);
767 if (!cmd
->iov_data
) {
768 pr_err("Unable to allocate cmd->iov_data\n");
772 cmd
->orig_iov_data_count
= iov_count
;
776 static int iscsit_alloc_buffs(struct iscsi_cmd
*cmd
)
778 struct scatterlist
*sgl
;
779 u32 length
= cmd
->se_cmd
.data_length
;
780 int nents
= DIV_ROUND_UP(length
, PAGE_SIZE
);
781 int i
= 0, j
= 0, ret
;
783 * If no SCSI payload is present, allocate the default iovecs used for
787 return iscsit_allocate_iovecs(cmd
);
789 sgl
= kzalloc(sizeof(*sgl
) * nents
, GFP_KERNEL
);
793 sg_init_table(sgl
, nents
);
796 int buf_size
= min_t(int, length
, PAGE_SIZE
);
799 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
801 goto page_alloc_failed
;
803 sg_set_page(&sgl
[i
], page
, buf_size
, 0);
810 cmd
->t_mem_sg_nents
= nents
;
812 /* BIDI ops not supported */
814 /* Tell the core about our preallocated memory */
815 transport_generic_map_mem_to_cmd(&cmd
->se_cmd
, sgl
, nents
, NULL
, 0);
817 * Allocate iovecs for SCSI payload after transport_generic_map_mem_to_cmd
818 * so that cmd->se_cmd.t_tasks_se_num has been set.
820 ret
= iscsit_allocate_iovecs(cmd
);
828 __free_page(sg_page(&sgl
[j
++]));
834 static int iscsit_handle_scsi_cmd(
835 struct iscsi_conn
*conn
,
838 int data_direction
, cmdsn_ret
= 0, immed_ret
, ret
, transport_ret
;
839 int dump_immediate_data
= 0, send_check_condition
= 0, payload_length
;
840 struct iscsi_cmd
*cmd
= NULL
;
841 struct iscsi_scsi_req
*hdr
;
843 spin_lock_bh(&conn
->sess
->session_stats_lock
);
844 conn
->sess
->cmd_pdus
++;
845 if (conn
->sess
->se_sess
->se_node_acl
) {
846 spin_lock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
847 conn
->sess
->se_sess
->se_node_acl
->num_cmds
++;
848 spin_unlock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
850 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
852 hdr
= (struct iscsi_scsi_req
*) buf
;
853 payload_length
= ntoh24(hdr
->dlength
);
854 hdr
->itt
= be32_to_cpu(hdr
->itt
);
855 hdr
->data_length
= be32_to_cpu(hdr
->data_length
);
856 hdr
->cmdsn
= be32_to_cpu(hdr
->cmdsn
);
857 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
859 /* FIXME; Add checks for AdditionalHeaderSegment */
861 if (!(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) &&
862 !(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
863 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
864 " not set. Bad iSCSI Initiator.\n");
865 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID
, 1,
869 if (((hdr
->flags
& ISCSI_FLAG_CMD_READ
) ||
870 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) && !hdr
->data_length
) {
872 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
873 * that adds support for RESERVE/RELEASE. There is a bug
874 * add with this new functionality that sets R/W bits when
875 * neither CDB carries any READ or WRITE datapayloads.
877 if ((hdr
->cdb
[0] == 0x16) || (hdr
->cdb
[0] == 0x17)) {
878 hdr
->flags
&= ~ISCSI_FLAG_CMD_READ
;
879 hdr
->flags
&= ~ISCSI_FLAG_CMD_WRITE
;
883 pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
884 " set when Expected Data Transfer Length is 0 for"
885 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr
->cdb
[0]);
886 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID
, 1,
891 if (!(hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
892 !(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) && (hdr
->data_length
!= 0)) {
893 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
894 " MUST be set if Expected Data Transfer Length is not 0."
895 " Bad iSCSI Initiator\n");
896 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID
, 1,
900 if ((hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
901 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) {
902 pr_err("Bidirectional operations not supported!\n");
903 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID
, 1,
907 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
908 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
909 " Scsi Command PDU.\n");
910 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID
, 1,
914 if (payload_length
&& !conn
->sess
->sess_ops
->ImmediateData
) {
915 pr_err("ImmediateData=No but DataSegmentLength=%u,"
916 " protocol error.\n", payload_length
);
917 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
921 if ((hdr
->data_length
== payload_length
) &&
922 (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
))) {
923 pr_err("Expected Data Transfer Length and Length of"
924 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
925 " bit is not set protocol error\n");
926 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
930 if (payload_length
> hdr
->data_length
) {
931 pr_err("DataSegmentLength: %u is greater than"
932 " EDTL: %u, protocol error.\n", payload_length
,
934 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
938 if (payload_length
> conn
->conn_ops
->MaxRecvDataSegmentLength
) {
939 pr_err("DataSegmentLength: %u is greater than"
940 " MaxRecvDataSegmentLength: %u, protocol error.\n",
941 payload_length
, conn
->conn_ops
->MaxRecvDataSegmentLength
);
942 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
946 if (payload_length
> conn
->sess
->sess_ops
->FirstBurstLength
) {
947 pr_err("DataSegmentLength: %u is greater than"
948 " FirstBurstLength: %u, protocol error.\n",
949 payload_length
, conn
->sess
->sess_ops
->FirstBurstLength
);
950 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID
, 1,
954 data_direction
= (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) ? DMA_TO_DEVICE
:
955 (hdr
->flags
& ISCSI_FLAG_CMD_READ
) ? DMA_FROM_DEVICE
:
958 cmd
= iscsit_allocate_se_cmd(conn
, hdr
->data_length
, data_direction
,
959 (hdr
->flags
& ISCSI_FLAG_CMD_ATTR_MASK
));
961 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES
, 1,
964 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
965 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr
->itt
,
966 hdr
->cmdsn
, hdr
->data_length
, payload_length
, conn
->cid
);
968 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_CMD
;
969 cmd
->i_state
= ISTATE_NEW_CMD
;
970 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
971 cmd
->immediate_data
= (payload_length
) ? 1 : 0;
972 cmd
->unsolicited_data
= ((!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) &&
973 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) ? 1 : 0);
974 if (cmd
->unsolicited_data
)
975 cmd
->cmd_flags
|= ICF_NON_IMMEDIATE_UNSOLICITED_DATA
;
977 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
978 if (hdr
->flags
& ISCSI_FLAG_CMD_READ
) {
979 spin_lock_bh(&conn
->sess
->ttt_lock
);
980 cmd
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
981 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF)
982 cmd
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
983 spin_unlock_bh(&conn
->sess
->ttt_lock
);
984 } else if (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)
985 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
986 cmd
->cmd_sn
= hdr
->cmdsn
;
987 cmd
->exp_stat_sn
= hdr
->exp_statsn
;
988 cmd
->first_burst_len
= payload_length
;
990 if (cmd
->data_direction
== DMA_FROM_DEVICE
) {
991 struct iscsi_datain_req
*dr
;
993 dr
= iscsit_allocate_datain_req();
995 return iscsit_add_reject_from_cmd(
996 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
999 iscsit_attach_datain_req(cmd
, dr
);
1003 * The CDB is going to an se_device_t.
1005 ret
= transport_lookup_cmd_lun(&cmd
->se_cmd
,
1006 scsilun_to_int(&hdr
->lun
));
1008 if (cmd
->se_cmd
.scsi_sense_reason
== TCM_NON_EXISTENT_LUN
) {
1009 pr_debug("Responding to non-acl'ed,"
1010 " non-existent or non-exported iSCSI LUN:"
1011 " 0x%016Lx\n", get_unaligned_le64(&hdr
->lun
));
1013 send_check_condition
= 1;
1017 * The Initiator Node has access to the LUN (the addressing method
1018 * is handled inside of iscsit_get_lun_for_cmd()). Now it's time to
1019 * allocate 1->N transport tasks (depending on sector count and
1020 * maximum request size the physical HBA(s) can handle.
1022 transport_ret
= transport_generic_allocate_tasks(&cmd
->se_cmd
, hdr
->cdb
);
1023 if (transport_ret
== -ENOMEM
) {
1024 return iscsit_add_reject_from_cmd(
1025 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1027 } else if (transport_ret
< 0) {
1029 * Unsupported SAM Opcode. CHECK_CONDITION will be sent
1030 * in iscsit_execute_cmd() during the CmdSN OOO Execution
1033 send_check_condition
= 1;
1035 cmd
->data_length
= cmd
->se_cmd
.data_length
;
1037 if (iscsit_decide_list_to_build(cmd
, payload_length
) < 0)
1038 return iscsit_add_reject_from_cmd(
1039 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1044 spin_lock_bh(&conn
->cmd_lock
);
1045 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
1046 spin_unlock_bh(&conn
->cmd_lock
);
1048 * Check if we need to delay processing because of ALUA
1049 * Active/NonOptimized primary access state..
1051 core_alua_check_nonop_delay(&cmd
->se_cmd
);
1053 * Allocate and setup SGL used with transport_generic_map_mem_to_cmd().
1054 * also call iscsit_allocate_iovecs()
1056 ret
= iscsit_alloc_buffs(cmd
);
1058 return iscsit_add_reject_from_cmd(
1059 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1062 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1063 * the Immediate Bit is not set, and no Immediate
1066 * A PDU/CmdSN carrying Immediate Data can only
1067 * be processed after the DataCRC has passed.
1068 * If the DataCRC fails, the CmdSN MUST NOT
1069 * be acknowledged. (See below)
1071 if (!cmd
->immediate_data
) {
1072 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, hdr
->cmdsn
);
1073 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
1075 else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1076 return iscsit_add_reject_from_cmd(
1077 ISCSI_REASON_PROTOCOL_ERROR
,
1081 iscsit_ack_from_expstatsn(conn
, hdr
->exp_statsn
);
1084 * If no Immediate Data is attached, it's OK to return now.
1086 if (!cmd
->immediate_data
) {
1087 if (send_check_condition
)
1090 if (cmd
->unsolicited_data
) {
1091 iscsit_set_dataout_sequence_values(cmd
);
1093 spin_lock_bh(&cmd
->dataout_timeout_lock
);
1094 iscsit_start_dataout_timer(cmd
, cmd
->conn
);
1095 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
1102 * Early CHECK_CONDITIONs never make it to the transport processing
1103 * thread. They are processed in CmdSN order by
1104 * iscsit_check_received_cmdsn() below.
1106 if (send_check_condition
) {
1107 immed_ret
= IMMEDIATE_DATA_NORMAL_OPERATION
;
1108 dump_immediate_data
= 1;
1109 goto after_immediate_data
;
1112 * Call directly into transport_generic_new_cmd() to perform
1113 * the backend memory allocation.
1115 ret
= transport_generic_new_cmd(&cmd
->se_cmd
);
1117 immed_ret
= IMMEDIATE_DATA_NORMAL_OPERATION
;
1118 dump_immediate_data
= 1;
1119 goto after_immediate_data
;
1122 immed_ret
= iscsit_handle_immediate_data(cmd
, buf
, payload_length
);
1123 after_immediate_data
:
1124 if (immed_ret
== IMMEDIATE_DATA_NORMAL_OPERATION
) {
1126 * A PDU/CmdSN carrying Immediate Data passed
1127 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1128 * Immediate Bit is not set.
1130 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, hdr
->cmdsn
);
1132 * Special case for Unsupported SAM WRITE Opcodes
1133 * and ImmediateData=Yes.
1135 if (dump_immediate_data
) {
1136 if (iscsit_dump_data_payload(conn
, payload_length
, 1) < 0)
1138 } else if (cmd
->unsolicited_data
) {
1139 iscsit_set_dataout_sequence_values(cmd
);
1141 spin_lock_bh(&cmd
->dataout_timeout_lock
);
1142 iscsit_start_dataout_timer(cmd
, cmd
->conn
);
1143 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
1146 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1147 return iscsit_add_reject_from_cmd(
1148 ISCSI_REASON_PROTOCOL_ERROR
,
1151 } else if (immed_ret
== IMMEDIATE_DATA_ERL1_CRC_FAILURE
) {
1153 * Immediate Data failed DataCRC and ERL>=1,
1154 * silently drop this PDU and let the initiator
1155 * plug the CmdSN gap.
1157 * FIXME: Send Unsolicited NOPIN with reserved
1158 * TTT here to help the initiator figure out
1159 * the missing CmdSN, although they should be
1160 * intelligent enough to determine the missing
1161 * CmdSN and issue a retry to plug the sequence.
1163 cmd
->i_state
= ISTATE_REMOVE
;
1164 iscsit_add_cmd_to_immediate_queue(cmd
, conn
, cmd
->i_state
);
1165 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1171 static u32
iscsit_do_crypto_hash_sg(
1172 struct hash_desc
*hash
,
1173 struct iscsi_cmd
*cmd
,
1181 struct scatterlist
*sg
;
1182 unsigned int page_off
;
1184 crypto_hash_init(hash
);
1186 sg
= cmd
->first_data_sg
;
1187 page_off
= cmd
->first_data_sg_off
;
1190 while (data_length
) {
1191 u32 cur_len
= min_t(u32
, data_length
, (sg
[i
].length
- page_off
));
1193 crypto_hash_update(hash
, &sg
[i
], cur_len
);
1195 data_length
-= cur_len
;
1201 struct scatterlist pad_sg
;
1203 sg_init_one(&pad_sg
, pad_bytes
, padding
);
1204 crypto_hash_update(hash
, &pad_sg
, padding
);
1206 crypto_hash_final(hash
, (u8
*) &data_crc
);
1211 static void iscsit_do_crypto_hash_buf(
1212 struct hash_desc
*hash
,
1219 struct scatterlist sg
;
1221 crypto_hash_init(hash
);
1223 sg_init_one(&sg
, buf
, payload_length
);
1224 crypto_hash_update(hash
, &sg
, payload_length
);
1227 sg_init_one(&sg
, pad_bytes
, padding
);
1228 crypto_hash_update(hash
, &sg
, padding
);
1230 crypto_hash_final(hash
, data_crc
);
1233 static int iscsit_handle_data_out(struct iscsi_conn
*conn
, unsigned char *buf
)
1235 int iov_ret
, ooo_cmdsn
= 0, ret
;
1236 u8 data_crc_failed
= 0;
1237 u32 checksum
, iov_count
= 0, padding
= 0, rx_got
= 0;
1238 u32 rx_size
= 0, payload_length
;
1239 struct iscsi_cmd
*cmd
= NULL
;
1240 struct se_cmd
*se_cmd
;
1241 struct iscsi_data
*hdr
;
1243 unsigned long flags
;
1245 hdr
= (struct iscsi_data
*) buf
;
1246 payload_length
= ntoh24(hdr
->dlength
);
1247 hdr
->itt
= be32_to_cpu(hdr
->itt
);
1248 hdr
->ttt
= be32_to_cpu(hdr
->ttt
);
1249 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
1250 hdr
->datasn
= be32_to_cpu(hdr
->datasn
);
1251 hdr
->offset
= be32_to_cpu(hdr
->offset
);
1253 if (!payload_length
) {
1254 pr_err("DataOUT payload is ZERO, protocol error.\n");
1255 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
1260 spin_lock_bh(&conn
->sess
->session_stats_lock
);
1261 conn
->sess
->rx_data_octets
+= payload_length
;
1262 if (conn
->sess
->se_sess
->se_node_acl
) {
1263 spin_lock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
1264 conn
->sess
->se_sess
->se_node_acl
->write_bytes
+= payload_length
;
1265 spin_unlock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
1267 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
1269 if (payload_length
> conn
->conn_ops
->MaxRecvDataSegmentLength
) {
1270 pr_err("DataSegmentLength: %u is greater than"
1271 " MaxRecvDataSegmentLength: %u\n", payload_length
,
1272 conn
->conn_ops
->MaxRecvDataSegmentLength
);
1273 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
1277 cmd
= iscsit_find_cmd_from_itt_or_dump(conn
, hdr
->itt
,
1282 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1283 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1284 hdr
->itt
, hdr
->ttt
, hdr
->datasn
, hdr
->offset
,
1285 payload_length
, conn
->cid
);
1287 if (cmd
->cmd_flags
& ICF_GOT_LAST_DATAOUT
) {
1288 pr_err("Command ITT: 0x%08x received DataOUT after"
1289 " last DataOUT received, dumping payload\n",
1290 cmd
->init_task_tag
);
1291 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1294 if (cmd
->data_direction
!= DMA_TO_DEVICE
) {
1295 pr_err("Command ITT: 0x%08x received DataOUT for a"
1296 " NON-WRITE command.\n", cmd
->init_task_tag
);
1297 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR
,
1300 se_cmd
= &cmd
->se_cmd
;
1301 iscsit_mod_dataout_timer(cmd
);
1303 if ((hdr
->offset
+ payload_length
) > cmd
->data_length
) {
1304 pr_err("DataOut Offset: %u, Length %u greater than"
1305 " iSCSI Command EDTL %u, protocol error.\n",
1306 hdr
->offset
, payload_length
, cmd
->data_length
);
1307 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID
,
1311 if (cmd
->unsolicited_data
) {
1312 int dump_unsolicited_data
= 0;
1314 if (conn
->sess
->sess_ops
->InitialR2T
) {
1315 pr_err("Received unexpected unsolicited data"
1316 " while InitialR2T=Yes, protocol error.\n");
1317 transport_send_check_condition_and_sense(&cmd
->se_cmd
,
1318 TCM_UNEXPECTED_UNSOLICITED_DATA
, 0);
1322 * Special case for dealing with Unsolicited DataOUT
1323 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1327 /* Something's amiss if we're not in WRITE_PENDING state... */
1328 spin_lock_irqsave(&se_cmd
->t_state_lock
, flags
);
1329 WARN_ON(se_cmd
->t_state
!= TRANSPORT_WRITE_PENDING
);
1330 spin_unlock_irqrestore(&se_cmd
->t_state_lock
, flags
);
1332 spin_lock_irqsave(&se_cmd
->t_state_lock
, flags
);
1333 if (!(se_cmd
->se_cmd_flags
& SCF_SUPPORTED_SAM_OPCODE
) ||
1334 (se_cmd
->se_cmd_flags
& SCF_SCSI_CDB_EXCEPTION
))
1335 dump_unsolicited_data
= 1;
1336 spin_unlock_irqrestore(&se_cmd
->t_state_lock
, flags
);
1338 if (dump_unsolicited_data
) {
1340 * Check if a delayed TASK_ABORTED status needs to
1341 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1342 * received with the unsolicitied data out.
1344 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1345 iscsit_stop_dataout_timer(cmd
);
1347 transport_check_aborted_status(se_cmd
,
1348 (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
));
1349 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1353 * For the normal solicited data path:
1355 * Check for a delayed TASK_ABORTED status and dump any
1356 * incoming data out payload if one exists. Also, when the
1357 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1358 * data out sequence, we decrement outstanding_r2ts. Once
1359 * outstanding_r2ts reaches zero, go ahead and send the delayed
1360 * TASK_ABORTED status.
1362 if (se_cmd
->transport_state
& CMD_T_ABORTED
) {
1363 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1364 if (--cmd
->outstanding_r2ts
< 1) {
1365 iscsit_stop_dataout_timer(cmd
);
1366 transport_check_aborted_status(
1370 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1374 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1375 * within-command recovery checks before receiving the payload.
1377 ret
= iscsit_check_pre_dataout(cmd
, buf
);
1378 if (ret
== DATAOUT_WITHIN_COMMAND_RECOVERY
)
1380 else if (ret
== DATAOUT_CANNOT_RECOVER
)
1383 rx_size
+= payload_length
;
1384 iov
= &cmd
->iov_data
[0];
1386 iov_ret
= iscsit_map_iovec(cmd
, iov
, hdr
->offset
, payload_length
);
1390 iov_count
+= iov_ret
;
1392 padding
= ((-payload_length
) & 3);
1394 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
1395 iov
[iov_count
++].iov_len
= padding
;
1397 pr_debug("Receiving %u padding bytes.\n", padding
);
1400 if (conn
->conn_ops
->DataDigest
) {
1401 iov
[iov_count
].iov_base
= &checksum
;
1402 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
1403 rx_size
+= ISCSI_CRC_LEN
;
1406 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
1408 iscsit_unmap_iovec(cmd
);
1410 if (rx_got
!= rx_size
)
1413 if (conn
->conn_ops
->DataDigest
) {
1416 data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_rx_hash
, cmd
,
1417 hdr
->offset
, payload_length
, padding
,
1420 if (checksum
!= data_crc
) {
1421 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1422 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1423 " does not match computed 0x%08x\n",
1424 hdr
->itt
, hdr
->offset
, payload_length
,
1425 hdr
->datasn
, checksum
, data_crc
);
1426 data_crc_failed
= 1;
1428 pr_debug("Got CRC32C DataDigest 0x%08x for"
1429 " %u bytes of Data Out\n", checksum
,
1434 * Increment post receive data and CRC values or perform
1435 * within-command recovery.
1437 ret
= iscsit_check_post_dataout(cmd
, buf
, data_crc_failed
);
1438 if ((ret
== DATAOUT_NORMAL
) || (ret
== DATAOUT_WITHIN_COMMAND_RECOVERY
))
1440 else if (ret
== DATAOUT_SEND_R2T
) {
1441 iscsit_set_dataout_sequence_values(cmd
);
1442 iscsit_build_r2ts_for_cmd(cmd
, conn
, 0);
1443 } else if (ret
== DATAOUT_SEND_TO_TRANSPORT
) {
1445 * Handle extra special case for out of order
1446 * Unsolicited Data Out.
1448 spin_lock_bh(&cmd
->istate_lock
);
1449 ooo_cmdsn
= (cmd
->cmd_flags
& ICF_OOO_CMDSN
);
1450 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
1451 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
1452 spin_unlock_bh(&cmd
->istate_lock
);
1454 iscsit_stop_dataout_timer(cmd
);
1455 return (!ooo_cmdsn
) ? transport_generic_handle_data(
1457 } else /* DATAOUT_CANNOT_RECOVER */
1463 static int iscsit_handle_nop_out(
1464 struct iscsi_conn
*conn
,
1467 unsigned char *ping_data
= NULL
;
1468 int cmdsn_ret
, niov
= 0, ret
= 0, rx_got
, rx_size
;
1469 u32 checksum
, data_crc
, padding
= 0, payload_length
;
1470 struct iscsi_cmd
*cmd
= NULL
;
1471 struct kvec
*iov
= NULL
;
1472 struct iscsi_nopout
*hdr
;
1474 hdr
= (struct iscsi_nopout
*) buf
;
1475 payload_length
= ntoh24(hdr
->dlength
);
1476 hdr
->itt
= be32_to_cpu(hdr
->itt
);
1477 hdr
->ttt
= be32_to_cpu(hdr
->ttt
);
1478 hdr
->cmdsn
= be32_to_cpu(hdr
->cmdsn
);
1479 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
1481 if ((hdr
->itt
== 0xFFFFFFFF) && !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1482 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1483 " not set, protocol error.\n");
1484 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
1488 if (payload_length
> conn
->conn_ops
->MaxRecvDataSegmentLength
) {
1489 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1490 " greater than MaxRecvDataSegmentLength: %u, protocol"
1491 " error.\n", payload_length
,
1492 conn
->conn_ops
->MaxRecvDataSegmentLength
);
1493 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
1497 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%09x,"
1498 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1499 (hdr
->itt
== 0xFFFFFFFF) ? "Response" : "Request",
1500 hdr
->itt
, hdr
->ttt
, hdr
->cmdsn
, hdr
->exp_statsn
,
1503 * This is not a response to a Unsolicited NopIN, which means
1504 * it can either be a NOPOUT ping request (with a valid ITT),
1505 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1506 * Either way, make sure we allocate an struct iscsi_cmd, as both
1507 * can contain ping data.
1509 if (hdr
->ttt
== 0xFFFFFFFF) {
1510 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
1512 return iscsit_add_reject(
1513 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1516 cmd
->iscsi_opcode
= ISCSI_OP_NOOP_OUT
;
1517 cmd
->i_state
= ISTATE_SEND_NOPIN
;
1518 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ?
1520 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1521 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1522 cmd
->cmd_sn
= hdr
->cmdsn
;
1523 cmd
->exp_stat_sn
= hdr
->exp_statsn
;
1524 cmd
->data_direction
= DMA_NONE
;
1527 if (payload_length
&& (hdr
->ttt
== 0xFFFFFFFF)) {
1528 rx_size
= payload_length
;
1529 ping_data
= kzalloc(payload_length
+ 1, GFP_KERNEL
);
1531 pr_err("Unable to allocate memory for"
1532 " NOPOUT ping data.\n");
1537 iov
= &cmd
->iov_misc
[0];
1538 iov
[niov
].iov_base
= ping_data
;
1539 iov
[niov
++].iov_len
= payload_length
;
1541 padding
= ((-payload_length
) & 3);
1543 pr_debug("Receiving %u additional bytes"
1544 " for padding.\n", padding
);
1545 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
1546 iov
[niov
++].iov_len
= padding
;
1549 if (conn
->conn_ops
->DataDigest
) {
1550 iov
[niov
].iov_base
= &checksum
;
1551 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
1552 rx_size
+= ISCSI_CRC_LEN
;
1555 rx_got
= rx_data(conn
, &cmd
->iov_misc
[0], niov
, rx_size
);
1556 if (rx_got
!= rx_size
) {
1561 if (conn
->conn_ops
->DataDigest
) {
1562 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
1563 ping_data
, payload_length
,
1564 padding
, cmd
->pad_bytes
,
1567 if (checksum
!= data_crc
) {
1568 pr_err("Ping data CRC32C DataDigest"
1569 " 0x%08x does not match computed 0x%08x\n",
1570 checksum
, data_crc
);
1571 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
1572 pr_err("Unable to recover from"
1573 " NOPOUT Ping DataCRC failure while in"
1579 * Silently drop this PDU and let the
1580 * initiator plug the CmdSN gap.
1582 pr_debug("Dropping NOPOUT"
1583 " Command CmdSN: 0x%08x due to"
1584 " DataCRC error.\n", hdr
->cmdsn
);
1589 pr_debug("Got CRC32C DataDigest"
1590 " 0x%08x for %u bytes of ping data.\n",
1591 checksum
, payload_length
);
1595 ping_data
[payload_length
] = '\0';
1597 * Attach ping data to struct iscsi_cmd->buf_ptr.
1599 cmd
->buf_ptr
= ping_data
;
1600 cmd
->buf_ptr_size
= payload_length
;
1602 pr_debug("Got %u bytes of NOPOUT ping"
1603 " data.\n", payload_length
);
1604 pr_debug("Ping Data: \"%s\"\n", ping_data
);
1607 if (hdr
->itt
!= 0xFFFFFFFF) {
1609 pr_err("Checking CmdSN for NOPOUT,"
1610 " but cmd is NULL!\n");
1614 * Initiator is expecting a NopIN ping reply,
1616 spin_lock_bh(&conn
->cmd_lock
);
1617 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
1618 spin_unlock_bh(&conn
->cmd_lock
);
1620 iscsit_ack_from_expstatsn(conn
, hdr
->exp_statsn
);
1622 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
1623 iscsit_add_cmd_to_response_queue(cmd
, conn
,
1628 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, hdr
->cmdsn
);
1629 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1633 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1634 return iscsit_add_reject_from_cmd(
1635 ISCSI_REASON_PROTOCOL_ERROR
,
1641 if (hdr
->ttt
!= 0xFFFFFFFF) {
1643 * This was a response to a unsolicited NOPIN ping.
1645 cmd
= iscsit_find_cmd_from_ttt(conn
, hdr
->ttt
);
1649 iscsit_stop_nopin_response_timer(conn
);
1651 cmd
->i_state
= ISTATE_REMOVE
;
1652 iscsit_add_cmd_to_immediate_queue(cmd
, conn
, cmd
->i_state
);
1653 iscsit_start_nopin_timer(conn
);
1656 * Initiator is not expecting a NOPIN is response.
1657 * Just ignore for now.
1659 * iSCSI v19-91 10.18
1660 * "A NOP-OUT may also be used to confirm a changed
1661 * ExpStatSN if another PDU will not be available
1671 iscsit_release_cmd(cmd
);
1677 static int iscsit_handle_task_mgt_cmd(
1678 struct iscsi_conn
*conn
,
1681 struct iscsi_cmd
*cmd
;
1682 struct se_tmr_req
*se_tmr
;
1683 struct iscsi_tmr_req
*tmr_req
;
1684 struct iscsi_tm
*hdr
;
1685 int out_of_order_cmdsn
= 0;
1689 hdr
= (struct iscsi_tm
*) buf
;
1690 hdr
->itt
= be32_to_cpu(hdr
->itt
);
1691 hdr
->rtt
= be32_to_cpu(hdr
->rtt
);
1692 hdr
->cmdsn
= be32_to_cpu(hdr
->cmdsn
);
1693 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
1694 hdr
->refcmdsn
= be32_to_cpu(hdr
->refcmdsn
);
1695 hdr
->exp_datasn
= be32_to_cpu(hdr
->exp_datasn
);
1696 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
1697 function
= hdr
->flags
;
1699 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1700 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1701 " 0x%08x, CID: %hu\n", hdr
->itt
, hdr
->cmdsn
, function
,
1702 hdr
->rtt
, hdr
->refcmdsn
, conn
->cid
);
1704 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1705 ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1706 (hdr
->rtt
!= ISCSI_RESERVED_TAG
))) {
1707 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1708 hdr
->rtt
= ISCSI_RESERVED_TAG
;
1711 if ((function
== ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1712 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1713 pr_err("Task Management Request TASK_REASSIGN not"
1714 " issued as immediate command, bad iSCSI Initiator"
1715 "implementation\n");
1716 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
1719 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1720 (hdr
->refcmdsn
!= ISCSI_RESERVED_TAG
))
1721 hdr
->refcmdsn
= ISCSI_RESERVED_TAG
;
1723 cmd
= iscsit_allocate_se_cmd_for_tmr(conn
, function
);
1725 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1728 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_TMFUNC
;
1729 cmd
->i_state
= ISTATE_SEND_TASKMGTRSP
;
1730 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1731 cmd
->init_task_tag
= hdr
->itt
;
1732 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1733 cmd
->cmd_sn
= hdr
->cmdsn
;
1734 cmd
->exp_stat_sn
= hdr
->exp_statsn
;
1735 se_tmr
= cmd
->se_cmd
.se_tmr_req
;
1736 tmr_req
= cmd
->tmr_req
;
1738 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1740 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
1741 ret
= transport_lookup_tmr_lun(&cmd
->se_cmd
,
1742 scsilun_to_int(&hdr
->lun
));
1744 cmd
->se_cmd
.se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
1745 se_tmr
->response
= ISCSI_TMF_RSP_NO_LUN
;
1751 case ISCSI_TM_FUNC_ABORT_TASK
:
1752 se_tmr
->response
= iscsit_tmr_abort_task(cmd
, buf
);
1753 if (se_tmr
->response
!= ISCSI_TMF_RSP_COMPLETE
) {
1754 cmd
->se_cmd
.se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
1758 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
1759 case ISCSI_TM_FUNC_CLEAR_ACA
:
1760 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
1761 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
1763 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
1764 if (iscsit_tmr_task_warm_reset(conn
, tmr_req
, buf
) < 0) {
1765 cmd
->se_cmd
.se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
1766 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
1770 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
1771 if (iscsit_tmr_task_cold_reset(conn
, tmr_req
, buf
) < 0) {
1772 cmd
->se_cmd
.se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
1773 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
1777 case ISCSI_TM_FUNC_TASK_REASSIGN
:
1778 se_tmr
->response
= iscsit_tmr_task_reassign(cmd
, buf
);
1780 * Perform sanity checks on the ExpDataSN only if the
1781 * TASK_REASSIGN was successful.
1783 if (se_tmr
->response
!= ISCSI_TMF_RSP_COMPLETE
)
1786 if (iscsit_check_task_reassign_expdatasn(tmr_req
, conn
) < 0)
1787 return iscsit_add_reject_from_cmd(
1788 ISCSI_REASON_BOOKMARK_INVALID
, 1, 1,
1792 pr_err("Unknown TMR function: 0x%02x, protocol"
1793 " error.\n", function
);
1794 cmd
->se_cmd
.se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
1795 se_tmr
->response
= ISCSI_TMF_RSP_NOT_SUPPORTED
;
1799 if ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1800 (se_tmr
->response
== ISCSI_TMF_RSP_COMPLETE
))
1801 se_tmr
->call_transport
= 1;
1803 spin_lock_bh(&conn
->cmd_lock
);
1804 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
1805 spin_unlock_bh(&conn
->cmd_lock
);
1807 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1808 int cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, hdr
->cmdsn
);
1809 if (cmdsn_ret
== CMDSN_HIGHER_THAN_EXP
)
1810 out_of_order_cmdsn
= 1;
1811 else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
1813 else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1814 return iscsit_add_reject_from_cmd(
1815 ISCSI_REASON_PROTOCOL_ERROR
,
1818 iscsit_ack_from_expstatsn(conn
, hdr
->exp_statsn
);
1820 if (out_of_order_cmdsn
|| !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
))
1823 * Found the referenced task, send to transport for processing.
1825 if (se_tmr
->call_transport
)
1826 return transport_generic_handle_tmr(&cmd
->se_cmd
);
1829 * Could not find the referenced LUN, task, or Task Management
1830 * command not authorized or supported. Change state and
1831 * let the tx_thread send the response.
1833 * For connection recovery, this is also the default action for
1834 * TMR TASK_REASSIGN.
1836 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
1840 /* #warning FIXME: Support Text Command parameters besides SendTargets */
1841 static int iscsit_handle_text_cmd(
1842 struct iscsi_conn
*conn
,
1845 char *text_ptr
, *text_in
;
1846 int cmdsn_ret
, niov
= 0, rx_got
, rx_size
;
1847 u32 checksum
= 0, data_crc
= 0, payload_length
;
1848 u32 padding
= 0, pad_bytes
= 0, text_length
= 0;
1849 struct iscsi_cmd
*cmd
;
1851 struct iscsi_text
*hdr
;
1853 hdr
= (struct iscsi_text
*) buf
;
1854 payload_length
= ntoh24(hdr
->dlength
);
1855 hdr
->itt
= be32_to_cpu(hdr
->itt
);
1856 hdr
->ttt
= be32_to_cpu(hdr
->ttt
);
1857 hdr
->cmdsn
= be32_to_cpu(hdr
->cmdsn
);
1858 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
1860 if (payload_length
> conn
->conn_ops
->MaxRecvDataSegmentLength
) {
1861 pr_err("Unable to accept text parameter length: %u"
1862 "greater than MaxRecvDataSegmentLength %u.\n",
1863 payload_length
, conn
->conn_ops
->MaxRecvDataSegmentLength
);
1864 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
1868 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1869 " ExpStatSN: 0x%08x, Length: %u\n", hdr
->itt
, hdr
->cmdsn
,
1870 hdr
->exp_statsn
, payload_length
);
1872 rx_size
= text_length
= payload_length
;
1874 text_in
= kzalloc(text_length
, GFP_KERNEL
);
1876 pr_err("Unable to allocate memory for"
1877 " incoming text parameters\n");
1881 memset(iov
, 0, 3 * sizeof(struct kvec
));
1882 iov
[niov
].iov_base
= text_in
;
1883 iov
[niov
++].iov_len
= text_length
;
1885 padding
= ((-payload_length
) & 3);
1887 iov
[niov
].iov_base
= &pad_bytes
;
1888 iov
[niov
++].iov_len
= padding
;
1890 pr_debug("Receiving %u additional bytes"
1891 " for padding.\n", padding
);
1893 if (conn
->conn_ops
->DataDigest
) {
1894 iov
[niov
].iov_base
= &checksum
;
1895 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
1896 rx_size
+= ISCSI_CRC_LEN
;
1899 rx_got
= rx_data(conn
, &iov
[0], niov
, rx_size
);
1900 if (rx_got
!= rx_size
) {
1905 if (conn
->conn_ops
->DataDigest
) {
1906 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
1907 text_in
, text_length
,
1908 padding
, (u8
*)&pad_bytes
,
1911 if (checksum
!= data_crc
) {
1912 pr_err("Text data CRC32C DataDigest"
1913 " 0x%08x does not match computed"
1914 " 0x%08x\n", checksum
, data_crc
);
1915 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
1916 pr_err("Unable to recover from"
1917 " Text Data digest failure while in"
1923 * Silently drop this PDU and let the
1924 * initiator plug the CmdSN gap.
1926 pr_debug("Dropping Text"
1927 " Command CmdSN: 0x%08x due to"
1928 " DataCRC error.\n", hdr
->cmdsn
);
1933 pr_debug("Got CRC32C DataDigest"
1934 " 0x%08x for %u bytes of text data.\n",
1935 checksum
, text_length
);
1938 text_in
[text_length
- 1] = '\0';
1939 pr_debug("Successfully read %d bytes of text"
1940 " data.\n", text_length
);
1942 if (strncmp("SendTargets", text_in
, 11) != 0) {
1943 pr_err("Received Text Data that is not"
1944 " SendTargets, cannot continue.\n");
1948 text_ptr
= strchr(text_in
, '=');
1950 pr_err("No \"=\" separator found in Text Data,"
1951 " cannot continue.\n");
1955 if (strncmp("=All", text_ptr
, 4) != 0) {
1956 pr_err("Unable to locate All value for"
1957 " SendTargets key, cannot continue.\n");
1961 /*#warning Support SendTargets=(iSCSI Target Name/Nothing) values. */
1965 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
1967 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1970 cmd
->iscsi_opcode
= ISCSI_OP_TEXT
;
1971 cmd
->i_state
= ISTATE_SEND_TEXTRSP
;
1972 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1973 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1974 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1975 cmd
->cmd_sn
= hdr
->cmdsn
;
1976 cmd
->exp_stat_sn
= hdr
->exp_statsn
;
1977 cmd
->data_direction
= DMA_NONE
;
1979 spin_lock_bh(&conn
->cmd_lock
);
1980 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
1981 spin_unlock_bh(&conn
->cmd_lock
);
1983 iscsit_ack_from_expstatsn(conn
, hdr
->exp_statsn
);
1985 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1986 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, hdr
->cmdsn
);
1987 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1988 return iscsit_add_reject_from_cmd(
1989 ISCSI_REASON_PROTOCOL_ERROR
,
1995 return iscsit_execute_cmd(cmd
, 0);
1998 int iscsit_logout_closesession(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2000 struct iscsi_conn
*conn_p
;
2001 struct iscsi_session
*sess
= conn
->sess
;
2003 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2004 " for SID: %u.\n", conn
->cid
, conn
->sess
->sid
);
2006 atomic_set(&sess
->session_logout
, 1);
2007 atomic_set(&conn
->conn_logout_remove
, 1);
2008 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_SESSION
;
2010 iscsit_inc_conn_usage_count(conn
);
2011 iscsit_inc_session_usage_count(sess
);
2013 spin_lock_bh(&sess
->conn_lock
);
2014 list_for_each_entry(conn_p
, &sess
->sess_conn_list
, conn_list
) {
2015 if (conn_p
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
)
2018 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2019 conn_p
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2021 spin_unlock_bh(&sess
->conn_lock
);
2023 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2028 int iscsit_logout_closeconnection(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2030 struct iscsi_conn
*l_conn
;
2031 struct iscsi_session
*sess
= conn
->sess
;
2033 pr_debug("Received logout request CLOSECONNECTION for CID:"
2034 " %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2037 * A Logout Request with a CLOSECONNECTION reason code for a CID
2038 * can arrive on a connection with a differing CID.
2040 if (conn
->cid
== cmd
->logout_cid
) {
2041 spin_lock_bh(&conn
->state_lock
);
2042 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2043 conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2045 atomic_set(&conn
->conn_logout_remove
, 1);
2046 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
;
2047 iscsit_inc_conn_usage_count(conn
);
2049 spin_unlock_bh(&conn
->state_lock
);
2052 * Handle all different cid CLOSECONNECTION requests in
2053 * iscsit_logout_post_handler_diffcid() as to give enough
2054 * time for any non immediate command's CmdSN to be
2055 * acknowledged on the connection in question.
2057 * Here we simply make sure the CID is still around.
2059 l_conn
= iscsit_get_conn_from_cid(sess
,
2062 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2063 iscsit_add_cmd_to_response_queue(cmd
, conn
,
2068 iscsit_dec_conn_usage_count(l_conn
);
2071 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2076 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2078 struct iscsi_session
*sess
= conn
->sess
;
2080 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2081 " CID: %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2083 if (sess
->sess_ops
->ErrorRecoveryLevel
!= 2) {
2084 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2085 " while ERL!=2.\n");
2086 cmd
->logout_response
= ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
;
2087 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2091 if (conn
->cid
== cmd
->logout_cid
) {
2092 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2093 " with CID: %hu on CID: %hu, implementation error.\n",
2094 cmd
->logout_cid
, conn
->cid
);
2095 cmd
->logout_response
= ISCSI_LOGOUT_CLEANUP_FAILED
;
2096 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2100 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2105 static int iscsit_handle_logout_cmd(
2106 struct iscsi_conn
*conn
,
2109 int cmdsn_ret
, logout_remove
= 0;
2111 struct iscsi_cmd
*cmd
;
2112 struct iscsi_logout
*hdr
;
2113 struct iscsi_tiqn
*tiqn
= iscsit_snmp_get_tiqn(conn
);
2115 hdr
= (struct iscsi_logout
*) buf
;
2116 reason_code
= (hdr
->flags
& 0x7f);
2117 hdr
->itt
= be32_to_cpu(hdr
->itt
);
2118 hdr
->cid
= be16_to_cpu(hdr
->cid
);
2119 hdr
->cmdsn
= be32_to_cpu(hdr
->cmdsn
);
2120 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
2123 spin_lock(&tiqn
->logout_stats
.lock
);
2124 if (reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
)
2125 tiqn
->logout_stats
.normal_logouts
++;
2127 tiqn
->logout_stats
.abnormal_logouts
++;
2128 spin_unlock(&tiqn
->logout_stats
.lock
);
2131 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2132 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2133 hdr
->itt
, hdr
->cmdsn
, hdr
->exp_statsn
, reason_code
,
2134 hdr
->cid
, conn
->cid
);
2136 if (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
) {
2137 pr_err("Received logout request on connection that"
2138 " is not in logged in state, ignoring request.\n");
2142 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
2144 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES
, 1,
2147 cmd
->iscsi_opcode
= ISCSI_OP_LOGOUT
;
2148 cmd
->i_state
= ISTATE_SEND_LOGOUTRSP
;
2149 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2150 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
2151 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2152 cmd
->cmd_sn
= hdr
->cmdsn
;
2153 cmd
->exp_stat_sn
= hdr
->exp_statsn
;
2154 cmd
->logout_cid
= hdr
->cid
;
2155 cmd
->logout_reason
= reason_code
;
2156 cmd
->data_direction
= DMA_NONE
;
2159 * We need to sleep in these cases (by returning 1) until the Logout
2160 * Response gets sent in the tx thread.
2162 if ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) ||
2163 ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
) &&
2164 (hdr
->cid
== conn
->cid
)))
2167 spin_lock_bh(&conn
->cmd_lock
);
2168 list_add_tail(&cmd
->i_list
, &conn
->conn_cmd_list
);
2169 spin_unlock_bh(&conn
->cmd_lock
);
2171 if (reason_code
!= ISCSI_LOGOUT_REASON_RECOVERY
)
2172 iscsit_ack_from_expstatsn(conn
, hdr
->exp_statsn
);
2175 * Immediate commands are executed, well, immediately.
2176 * Non-Immediate Logout Commands are executed in CmdSN order.
2178 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
2179 int ret
= iscsit_execute_cmd(cmd
, 0);
2184 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, hdr
->cmdsn
);
2185 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
2187 } else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
) {
2188 return iscsit_add_reject_from_cmd(
2189 ISCSI_REASON_PROTOCOL_ERROR
,
2194 return logout_remove
;
2197 static int iscsit_handle_snack(
2198 struct iscsi_conn
*conn
,
2201 struct iscsi_snack
*hdr
;
2203 hdr
= (struct iscsi_snack
*) buf
;
2204 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
2205 hdr
->itt
= be32_to_cpu(hdr
->itt
);
2206 hdr
->ttt
= be32_to_cpu(hdr
->ttt
);
2207 hdr
->exp_statsn
= be32_to_cpu(hdr
->exp_statsn
);
2208 hdr
->begrun
= be32_to_cpu(hdr
->begrun
);
2209 hdr
->runlength
= be32_to_cpu(hdr
->runlength
);
2211 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2212 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2213 " CID: %hu\n", hdr
->itt
, hdr
->exp_statsn
, hdr
->flags
,
2214 hdr
->begrun
, hdr
->runlength
, conn
->cid
);
2216 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2217 pr_err("Initiator sent SNACK request while in"
2218 " ErrorRecoveryLevel=0.\n");
2219 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
2223 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2224 * call from inside iscsi_send_recovery_datain_or_r2t().
2226 switch (hdr
->flags
& ISCSI_FLAG_SNACK_TYPE_MASK
) {
2228 return iscsit_handle_recovery_datain_or_r2t(conn
, buf
,
2229 hdr
->itt
, hdr
->ttt
, hdr
->begrun
, hdr
->runlength
);
2230 case ISCSI_FLAG_SNACK_TYPE_STATUS
:
2231 return iscsit_handle_status_snack(conn
, hdr
->itt
, hdr
->ttt
,
2232 hdr
->begrun
, hdr
->runlength
);
2233 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK
:
2234 return iscsit_handle_data_ack(conn
, hdr
->ttt
, hdr
->begrun
,
2236 case ISCSI_FLAG_SNACK_TYPE_RDATA
:
2237 /* FIXME: Support R-Data SNACK */
2238 pr_err("R-Data SNACK Not Supported.\n");
2239 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
2242 pr_err("Unknown SNACK type 0x%02x, protocol"
2243 " error.\n", hdr
->flags
& 0x0f);
2244 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
2251 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2253 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2254 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2255 wait_for_completion_interruptible_timeout(
2256 &conn
->rx_half_close_comp
,
2257 ISCSI_RX_THREAD_TCP_TIMEOUT
* HZ
);
2261 static int iscsit_handle_immediate_data(
2262 struct iscsi_cmd
*cmd
,
2266 int iov_ret
, rx_got
= 0, rx_size
= 0;
2267 u32 checksum
, iov_count
= 0, padding
= 0;
2268 struct iscsi_conn
*conn
= cmd
->conn
;
2271 iov_ret
= iscsit_map_iovec(cmd
, cmd
->iov_data
, cmd
->write_data_done
, length
);
2273 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2276 iov_count
= iov_ret
;
2277 iov
= &cmd
->iov_data
[0];
2279 padding
= ((-length
) & 3);
2281 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2282 iov
[iov_count
++].iov_len
= padding
;
2286 if (conn
->conn_ops
->DataDigest
) {
2287 iov
[iov_count
].iov_base
= &checksum
;
2288 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2289 rx_size
+= ISCSI_CRC_LEN
;
2292 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
2294 iscsit_unmap_iovec(cmd
);
2296 if (rx_got
!= rx_size
) {
2297 iscsit_rx_thread_wait_for_tcp(conn
);
2298 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2301 if (conn
->conn_ops
->DataDigest
) {
2304 data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_rx_hash
, cmd
,
2305 cmd
->write_data_done
, length
, padding
,
2308 if (checksum
!= data_crc
) {
2309 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2310 " does not match computed 0x%08x\n", checksum
,
2313 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2314 pr_err("Unable to recover from"
2315 " Immediate Data digest failure while"
2317 iscsit_add_reject_from_cmd(
2318 ISCSI_REASON_DATA_DIGEST_ERROR
,
2320 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2322 iscsit_add_reject_from_cmd(
2323 ISCSI_REASON_DATA_DIGEST_ERROR
,
2325 return IMMEDIATE_DATA_ERL1_CRC_FAILURE
;
2328 pr_debug("Got CRC32C DataDigest 0x%08x for"
2329 " %u bytes of Immediate Data\n", checksum
,
2334 cmd
->write_data_done
+= length
;
2336 if (cmd
->write_data_done
== cmd
->data_length
) {
2337 spin_lock_bh(&cmd
->istate_lock
);
2338 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
2339 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
2340 spin_unlock_bh(&cmd
->istate_lock
);
2343 return IMMEDIATE_DATA_NORMAL_OPERATION
;
2347 * Called with sess->conn_lock held.
2349 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2350 with active network interface */
2351 static void iscsit_build_conn_drop_async_message(struct iscsi_conn
*conn
)
2353 struct iscsi_cmd
*cmd
;
2354 struct iscsi_conn
*conn_p
;
2358 * Only send a Asynchronous Message on connections whos network
2359 * interface is still functional.
2361 list_for_each_entry(conn_p
, &conn
->sess
->sess_conn_list
, conn_list
) {
2362 if (conn_p
->conn_state
== TARG_CONN_STATE_LOGGED_IN
) {
2363 iscsit_inc_conn_usage_count(conn_p
);
2372 cmd
= iscsit_allocate_cmd(conn_p
, GFP_ATOMIC
);
2374 iscsit_dec_conn_usage_count(conn_p
);
2378 cmd
->logout_cid
= conn
->cid
;
2379 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2380 cmd
->i_state
= ISTATE_SEND_ASYNCMSG
;
2382 spin_lock_bh(&conn_p
->cmd_lock
);
2383 list_add_tail(&cmd
->i_list
, &conn_p
->conn_cmd_list
);
2384 spin_unlock_bh(&conn_p
->cmd_lock
);
2386 iscsit_add_cmd_to_response_queue(cmd
, conn_p
, cmd
->i_state
);
2387 iscsit_dec_conn_usage_count(conn_p
);
2390 static int iscsit_send_conn_drop_async_message(
2391 struct iscsi_cmd
*cmd
,
2392 struct iscsi_conn
*conn
)
2394 struct iscsi_async
*hdr
;
2396 cmd
->tx_size
= ISCSI_HDR_LEN
;
2397 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2399 hdr
= (struct iscsi_async
*) cmd
->pdu
;
2400 hdr
->opcode
= ISCSI_OP_ASYNC_EVENT
;
2401 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
2402 cmd
->init_task_tag
= 0xFFFFFFFF;
2403 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2404 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->rsvd4
[0]);
2405 cmd
->stat_sn
= conn
->stat_sn
++;
2406 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2407 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2408 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2409 hdr
->async_event
= ISCSI_ASYNC_MSG_DROPPING_CONNECTION
;
2410 hdr
->param1
= cpu_to_be16(cmd
->logout_cid
);
2411 hdr
->param2
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Wait
);
2412 hdr
->param3
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Retain
);
2414 if (conn
->conn_ops
->HeaderDigest
) {
2415 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2417 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2418 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
2419 0, NULL
, (u8
*)header_digest
);
2421 cmd
->tx_size
+= ISCSI_CRC_LEN
;
2422 pr_debug("Attaching CRC32C HeaderDigest to"
2423 " Async Message 0x%08x\n", *header_digest
);
2426 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2427 cmd
->iov_misc
[0].iov_len
= cmd
->tx_size
;
2428 cmd
->iov_misc_count
= 1;
2430 pr_debug("Sending Connection Dropped Async Message StatSN:"
2431 " 0x%08x, for CID: %hu on CID: %hu\n", cmd
->stat_sn
,
2432 cmd
->logout_cid
, conn
->cid
);
2436 static int iscsit_send_data_in(
2437 struct iscsi_cmd
*cmd
,
2438 struct iscsi_conn
*conn
,
2441 int iov_ret
= 0, set_statsn
= 0;
2442 u32 iov_count
= 0, tx_size
= 0;
2443 struct iscsi_datain datain
;
2444 struct iscsi_datain_req
*dr
;
2445 struct iscsi_data_rsp
*hdr
;
2448 memset(&datain
, 0, sizeof(struct iscsi_datain
));
2449 dr
= iscsit_get_datain_values(cmd
, &datain
);
2451 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2452 cmd
->init_task_tag
);
2457 * Be paranoid and double check the logic for now.
2459 if ((datain
.offset
+ datain
.length
) > cmd
->data_length
) {
2460 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2461 " datain.length: %u exceeds cmd->data_length: %u\n",
2462 cmd
->init_task_tag
, datain
.offset
, datain
.length
,
2467 spin_lock_bh(&conn
->sess
->session_stats_lock
);
2468 conn
->sess
->tx_data_octets
+= datain
.length
;
2469 if (conn
->sess
->se_sess
->se_node_acl
) {
2470 spin_lock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
2471 conn
->sess
->se_sess
->se_node_acl
->read_bytes
+= datain
.length
;
2472 spin_unlock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
2474 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
2476 * Special case for successfully execution w/ both DATAIN
2479 if ((datain
.flags
& ISCSI_FLAG_DATA_STATUS
) &&
2480 (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
))
2481 datain
.flags
&= ~ISCSI_FLAG_DATA_STATUS
;
2483 if ((dr
->dr_complete
== DATAIN_COMPLETE_NORMAL
) ||
2484 (dr
->dr_complete
== DATAIN_COMPLETE_CONNECTION_RECOVERY
)) {
2485 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2486 cmd
->stat_sn
= conn
->stat_sn
++;
2488 } else if (dr
->dr_complete
==
2489 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY
)
2493 hdr
= (struct iscsi_data_rsp
*) cmd
->pdu
;
2494 memset(hdr
, 0, ISCSI_HDR_LEN
);
2495 hdr
->opcode
= ISCSI_OP_SCSI_DATA_IN
;
2496 hdr
->flags
= datain
.flags
;
2497 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
2498 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
2499 hdr
->flags
|= ISCSI_FLAG_DATA_OVERFLOW
;
2500 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2501 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
2502 hdr
->flags
|= ISCSI_FLAG_DATA_UNDERFLOW
;
2503 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2506 hton24(hdr
->dlength
, datain
.length
);
2507 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2508 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
2509 (struct scsi_lun
*)&hdr
->lun
);
2511 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2513 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
2514 hdr
->ttt
= (hdr
->flags
& ISCSI_FLAG_DATA_ACK
) ?
2515 cpu_to_be32(cmd
->targ_xfer_tag
) :
2517 hdr
->statsn
= (set_statsn
) ? cpu_to_be32(cmd
->stat_sn
) :
2519 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2520 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2521 hdr
->datasn
= cpu_to_be32(datain
.data_sn
);
2522 hdr
->offset
= cpu_to_be32(datain
.offset
);
2524 iov
= &cmd
->iov_data
[0];
2525 iov
[iov_count
].iov_base
= cmd
->pdu
;
2526 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
2527 tx_size
+= ISCSI_HDR_LEN
;
2529 if (conn
->conn_ops
->HeaderDigest
) {
2530 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2532 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2533 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
2534 0, NULL
, (u8
*)header_digest
);
2536 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2537 tx_size
+= ISCSI_CRC_LEN
;
2539 pr_debug("Attaching CRC32 HeaderDigest"
2540 " for DataIN PDU 0x%08x\n", *header_digest
);
2543 iov_ret
= iscsit_map_iovec(cmd
, &cmd
->iov_data
[1], datain
.offset
, datain
.length
);
2547 iov_count
+= iov_ret
;
2548 tx_size
+= datain
.length
;
2550 cmd
->padding
= ((-datain
.length
) & 3);
2552 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2553 iov
[iov_count
++].iov_len
= cmd
->padding
;
2554 tx_size
+= cmd
->padding
;
2556 pr_debug("Attaching %u padding bytes\n",
2559 if (conn
->conn_ops
->DataDigest
) {
2560 cmd
->data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_tx_hash
, cmd
,
2561 datain
.offset
, datain
.length
, cmd
->padding
, cmd
->pad_bytes
);
2563 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
2564 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2565 tx_size
+= ISCSI_CRC_LEN
;
2567 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2568 " 0x%08x\n", datain
.length
+cmd
->padding
, cmd
->data_crc
);
2571 cmd
->iov_data_count
= iov_count
;
2572 cmd
->tx_size
= tx_size
;
2574 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2575 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2576 cmd
->init_task_tag
, ntohl(hdr
->statsn
), ntohl(hdr
->datasn
),
2577 ntohl(hdr
->offset
), datain
.length
, conn
->cid
);
2579 if (dr
->dr_complete
) {
2580 *eodr
= (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ?
2582 iscsit_free_datain_req(cmd
, dr
);
2588 static int iscsit_send_logout_response(
2589 struct iscsi_cmd
*cmd
,
2590 struct iscsi_conn
*conn
)
2592 int niov
= 0, tx_size
;
2593 struct iscsi_conn
*logout_conn
= NULL
;
2594 struct iscsi_conn_recovery
*cr
= NULL
;
2595 struct iscsi_session
*sess
= conn
->sess
;
2597 struct iscsi_logout_rsp
*hdr
;
2599 * The actual shutting down of Sessions and/or Connections
2600 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2601 * is done in scsi_logout_post_handler().
2603 switch (cmd
->logout_reason
) {
2604 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
2605 pr_debug("iSCSI session logout successful, setting"
2606 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2607 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2609 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
2610 if (cmd
->logout_response
== ISCSI_LOGOUT_CID_NOT_FOUND
)
2613 * For CLOSECONNECTION logout requests carrying
2614 * a matching logout CID -> local CID, the reference
2615 * for the local CID will have been incremented in
2616 * iscsi_logout_closeconnection().
2618 * For CLOSECONNECTION logout requests carrying
2619 * a different CID than the connection it arrived
2620 * on, the connection responding to cmd->logout_cid
2621 * is stopped in iscsit_logout_post_handler_diffcid().
2624 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2625 " successful.\n", cmd
->logout_cid
, conn
->cid
);
2626 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2628 case ISCSI_LOGOUT_REASON_RECOVERY
:
2629 if ((cmd
->logout_response
== ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
) ||
2630 (cmd
->logout_response
== ISCSI_LOGOUT_CLEANUP_FAILED
))
2633 * If the connection is still active from our point of view
2634 * force connection recovery to occur.
2636 logout_conn
= iscsit_get_conn_from_cid_rcfr(sess
,
2638 if ((logout_conn
)) {
2639 iscsit_connection_reinstatement_rcfr(logout_conn
);
2640 iscsit_dec_conn_usage_count(logout_conn
);
2643 cr
= iscsit_get_inactive_connection_recovery_entry(
2644 conn
->sess
, cmd
->logout_cid
);
2646 pr_err("Unable to locate CID: %hu for"
2647 " REMOVECONNFORRECOVERY Logout Request.\n",
2649 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2653 iscsit_discard_cr_cmds_by_expstatsn(cr
, cmd
->exp_stat_sn
);
2655 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2656 " for recovery for CID: %hu on CID: %hu successful.\n",
2657 cmd
->logout_cid
, conn
->cid
);
2658 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2661 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2662 cmd
->logout_reason
);
2666 tx_size
= ISCSI_HDR_LEN
;
2667 hdr
= (struct iscsi_logout_rsp
*)cmd
->pdu
;
2668 memset(hdr
, 0, ISCSI_HDR_LEN
);
2669 hdr
->opcode
= ISCSI_OP_LOGOUT_RSP
;
2670 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2671 hdr
->response
= cmd
->logout_response
;
2672 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
2673 cmd
->stat_sn
= conn
->stat_sn
++;
2674 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2676 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2677 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2678 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2680 iov
= &cmd
->iov_misc
[0];
2681 iov
[niov
].iov_base
= cmd
->pdu
;
2682 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
2684 if (conn
->conn_ops
->HeaderDigest
) {
2685 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2687 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2688 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
2689 0, NULL
, (u8
*)header_digest
);
2691 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2692 tx_size
+= ISCSI_CRC_LEN
;
2693 pr_debug("Attaching CRC32C HeaderDigest to"
2694 " Logout Response 0x%08x\n", *header_digest
);
2696 cmd
->iov_misc_count
= niov
;
2697 cmd
->tx_size
= tx_size
;
2699 pr_debug("Sending Logout Response ITT: 0x%08x StatSN:"
2700 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2701 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
,
2702 cmd
->logout_cid
, conn
->cid
);
2708 * Unsolicited NOPIN, either requesting a response or not.
2710 static int iscsit_send_unsolicited_nopin(
2711 struct iscsi_cmd
*cmd
,
2712 struct iscsi_conn
*conn
,
2715 int tx_size
= ISCSI_HDR_LEN
;
2716 struct iscsi_nopin
*hdr
;
2718 hdr
= (struct iscsi_nopin
*) cmd
->pdu
;
2719 memset(hdr
, 0, ISCSI_HDR_LEN
);
2720 hdr
->opcode
= ISCSI_OP_NOOP_IN
;
2721 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2722 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
2723 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2724 cmd
->stat_sn
= conn
->stat_sn
;
2725 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2726 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2727 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2729 if (conn
->conn_ops
->HeaderDigest
) {
2730 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2732 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2733 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
2734 0, NULL
, (u8
*)header_digest
);
2736 tx_size
+= ISCSI_CRC_LEN
;
2737 pr_debug("Attaching CRC32C HeaderDigest to"
2738 " NopIN 0x%08x\n", *header_digest
);
2741 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2742 cmd
->iov_misc
[0].iov_len
= tx_size
;
2743 cmd
->iov_misc_count
= 1;
2744 cmd
->tx_size
= tx_size
;
2746 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2747 " 0x%08x CID: %hu\n", hdr
->ttt
, cmd
->stat_sn
, conn
->cid
);
2752 static int iscsit_send_nopin_response(
2753 struct iscsi_cmd
*cmd
,
2754 struct iscsi_conn
*conn
)
2756 int niov
= 0, tx_size
;
2759 struct iscsi_nopin
*hdr
;
2761 tx_size
= ISCSI_HDR_LEN
;
2762 hdr
= (struct iscsi_nopin
*) cmd
->pdu
;
2763 memset(hdr
, 0, ISCSI_HDR_LEN
);
2764 hdr
->opcode
= ISCSI_OP_NOOP_IN
;
2765 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2766 hton24(hdr
->dlength
, cmd
->buf_ptr_size
);
2767 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2768 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
2769 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2770 cmd
->stat_sn
= conn
->stat_sn
++;
2771 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2773 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2774 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2775 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2777 iov
= &cmd
->iov_misc
[0];
2778 iov
[niov
].iov_base
= cmd
->pdu
;
2779 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
2781 if (conn
->conn_ops
->HeaderDigest
) {
2782 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2784 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2785 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
2786 0, NULL
, (u8
*)header_digest
);
2788 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2789 tx_size
+= ISCSI_CRC_LEN
;
2790 pr_debug("Attaching CRC32C HeaderDigest"
2791 " to NopIn 0x%08x\n", *header_digest
);
2795 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2796 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2798 if (cmd
->buf_ptr_size
) {
2799 iov
[niov
].iov_base
= cmd
->buf_ptr
;
2800 iov
[niov
++].iov_len
= cmd
->buf_ptr_size
;
2801 tx_size
+= cmd
->buf_ptr_size
;
2803 pr_debug("Echoing back %u bytes of ping"
2804 " data.\n", cmd
->buf_ptr_size
);
2806 padding
= ((-cmd
->buf_ptr_size
) & 3);
2808 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
2809 iov
[niov
++].iov_len
= padding
;
2811 pr_debug("Attaching %u additional"
2812 " padding bytes.\n", padding
);
2814 if (conn
->conn_ops
->DataDigest
) {
2815 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2816 cmd
->buf_ptr
, cmd
->buf_ptr_size
,
2817 padding
, (u8
*)&cmd
->pad_bytes
,
2818 (u8
*)&cmd
->data_crc
);
2820 iov
[niov
].iov_base
= &cmd
->data_crc
;
2821 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
2822 tx_size
+= ISCSI_CRC_LEN
;
2823 pr_debug("Attached DataDigest for %u"
2824 " bytes of ping data, CRC 0x%08x\n",
2825 cmd
->buf_ptr_size
, cmd
->data_crc
);
2829 cmd
->iov_misc_count
= niov
;
2830 cmd
->tx_size
= tx_size
;
2832 pr_debug("Sending NOPIN Response ITT: 0x%08x, TTT:"
2833 " 0x%08x, StatSN: 0x%08x, Length %u\n", cmd
->init_task_tag
,
2834 cmd
->targ_xfer_tag
, cmd
->stat_sn
, cmd
->buf_ptr_size
);
2839 int iscsit_send_r2t(
2840 struct iscsi_cmd
*cmd
,
2841 struct iscsi_conn
*conn
)
2844 struct iscsi_r2t
*r2t
;
2845 struct iscsi_r2t_rsp
*hdr
;
2847 r2t
= iscsit_get_r2t_from_list(cmd
);
2851 hdr
= (struct iscsi_r2t_rsp
*) cmd
->pdu
;
2852 memset(hdr
, 0, ISCSI_HDR_LEN
);
2853 hdr
->opcode
= ISCSI_OP_R2T
;
2854 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2855 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
2856 (struct scsi_lun
*)&hdr
->lun
);
2857 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
2858 spin_lock_bh(&conn
->sess
->ttt_lock
);
2859 r2t
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
2860 if (r2t
->targ_xfer_tag
== 0xFFFFFFFF)
2861 r2t
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
2862 spin_unlock_bh(&conn
->sess
->ttt_lock
);
2863 hdr
->ttt
= cpu_to_be32(r2t
->targ_xfer_tag
);
2864 hdr
->statsn
= cpu_to_be32(conn
->stat_sn
);
2865 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2866 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2867 hdr
->r2tsn
= cpu_to_be32(r2t
->r2t_sn
);
2868 hdr
->data_offset
= cpu_to_be32(r2t
->offset
);
2869 hdr
->data_length
= cpu_to_be32(r2t
->xfer_len
);
2871 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2872 cmd
->iov_misc
[0].iov_len
= ISCSI_HDR_LEN
;
2873 tx_size
+= ISCSI_HDR_LEN
;
2875 if (conn
->conn_ops
->HeaderDigest
) {
2876 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2878 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2879 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
2880 0, NULL
, (u8
*)header_digest
);
2882 cmd
->iov_misc
[0].iov_len
+= ISCSI_CRC_LEN
;
2883 tx_size
+= ISCSI_CRC_LEN
;
2884 pr_debug("Attaching CRC32 HeaderDigest for R2T"
2885 " PDU 0x%08x\n", *header_digest
);
2888 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
2889 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
2890 (!r2t
->recovery_r2t
) ? "" : "Recovery ", cmd
->init_task_tag
,
2891 r2t
->targ_xfer_tag
, ntohl(hdr
->statsn
), r2t
->r2t_sn
,
2892 r2t
->offset
, r2t
->xfer_len
, conn
->cid
);
2894 cmd
->iov_misc_count
= 1;
2895 cmd
->tx_size
= tx_size
;
2897 spin_lock_bh(&cmd
->r2t_lock
);
2899 spin_unlock_bh(&cmd
->r2t_lock
);
2905 * type 0: Normal Operation.
2906 * type 1: Called from Storage Transport.
2907 * type 2: Called from iscsi_task_reassign_complete_write() for
2908 * connection recovery.
2910 int iscsit_build_r2ts_for_cmd(
2911 struct iscsi_cmd
*cmd
,
2912 struct iscsi_conn
*conn
,
2916 u32 offset
= 0, xfer_len
= 0;
2918 spin_lock_bh(&cmd
->r2t_lock
);
2919 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
) {
2920 spin_unlock_bh(&cmd
->r2t_lock
);
2924 if (conn
->sess
->sess_ops
->DataSequenceInOrder
&& (type
!= 2))
2925 if (cmd
->r2t_offset
< cmd
->write_data_done
)
2926 cmd
->r2t_offset
= cmd
->write_data_done
;
2928 while (cmd
->outstanding_r2ts
< conn
->sess
->sess_ops
->MaxOutstandingR2T
) {
2929 if (conn
->sess
->sess_ops
->DataSequenceInOrder
) {
2930 offset
= cmd
->r2t_offset
;
2932 if (first_r2t
&& (type
== 2)) {
2933 xfer_len
= ((offset
+
2934 (conn
->sess
->sess_ops
->MaxBurstLength
-
2935 cmd
->next_burst_len
) >
2937 (cmd
->data_length
- offset
) :
2938 (conn
->sess
->sess_ops
->MaxBurstLength
-
2939 cmd
->next_burst_len
));
2941 xfer_len
= ((offset
+
2942 conn
->sess
->sess_ops
->MaxBurstLength
) >
2944 (cmd
->data_length
- offset
) :
2945 conn
->sess
->sess_ops
->MaxBurstLength
;
2947 cmd
->r2t_offset
+= xfer_len
;
2949 if (cmd
->r2t_offset
== cmd
->data_length
)
2950 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
2952 struct iscsi_seq
*seq
;
2954 seq
= iscsit_get_seq_holder_for_r2t(cmd
);
2956 spin_unlock_bh(&cmd
->r2t_lock
);
2960 offset
= seq
->offset
;
2961 xfer_len
= seq
->xfer_len
;
2963 if (cmd
->seq_send_order
== cmd
->seq_count
)
2964 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
2966 cmd
->outstanding_r2ts
++;
2969 if (iscsit_add_r2t_to_list(cmd
, offset
, xfer_len
, 0, 0) < 0) {
2970 spin_unlock_bh(&cmd
->r2t_lock
);
2974 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
)
2977 spin_unlock_bh(&cmd
->r2t_lock
);
2982 static int iscsit_send_status(
2983 struct iscsi_cmd
*cmd
,
2984 struct iscsi_conn
*conn
)
2986 u8 iov_count
= 0, recovery
;
2987 u32 padding
= 0, tx_size
= 0;
2988 struct iscsi_scsi_rsp
*hdr
;
2991 recovery
= (cmd
->i_state
!= ISTATE_SEND_STATUS
);
2993 cmd
->stat_sn
= conn
->stat_sn
++;
2995 spin_lock_bh(&conn
->sess
->session_stats_lock
);
2996 conn
->sess
->rsp_pdus
++;
2997 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
2999 hdr
= (struct iscsi_scsi_rsp
*) cmd
->pdu
;
3000 memset(hdr
, 0, ISCSI_HDR_LEN
);
3001 hdr
->opcode
= ISCSI_OP_SCSI_CMD_RSP
;
3002 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3003 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
3004 hdr
->flags
|= ISCSI_FLAG_CMD_OVERFLOW
;
3005 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3006 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
3007 hdr
->flags
|= ISCSI_FLAG_CMD_UNDERFLOW
;
3008 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3010 hdr
->response
= cmd
->iscsi_response
;
3011 hdr
->cmd_status
= cmd
->se_cmd
.scsi_status
;
3012 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
3013 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3015 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3016 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3017 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3019 iov
= &cmd
->iov_misc
[0];
3020 iov
[iov_count
].iov_base
= cmd
->pdu
;
3021 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3022 tx_size
+= ISCSI_HDR_LEN
;
3025 * Attach SENSE DATA payload to iSCSI Response PDU
3027 if (cmd
->se_cmd
.sense_buffer
&&
3028 ((cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
3029 (cmd
->se_cmd
.se_cmd_flags
& SCF_EMULATED_TASK_SENSE
))) {
3030 padding
= -(cmd
->se_cmd
.scsi_sense_length
) & 3;
3031 hton24(hdr
->dlength
, cmd
->se_cmd
.scsi_sense_length
);
3032 iov
[iov_count
].iov_base
= cmd
->se_cmd
.sense_buffer
;
3033 iov
[iov_count
++].iov_len
=
3034 (cmd
->se_cmd
.scsi_sense_length
+ padding
);
3035 tx_size
+= cmd
->se_cmd
.scsi_sense_length
;
3038 memset(cmd
->se_cmd
.sense_buffer
+
3039 cmd
->se_cmd
.scsi_sense_length
, 0, padding
);
3041 pr_debug("Adding %u bytes of padding to"
3042 " SENSE.\n", padding
);
3045 if (conn
->conn_ops
->DataDigest
) {
3046 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3047 cmd
->se_cmd
.sense_buffer
,
3048 (cmd
->se_cmd
.scsi_sense_length
+ padding
),
3049 0, NULL
, (u8
*)&cmd
->data_crc
);
3051 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3052 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3053 tx_size
+= ISCSI_CRC_LEN
;
3055 pr_debug("Attaching CRC32 DataDigest for"
3056 " SENSE, %u bytes CRC 0x%08x\n",
3057 (cmd
->se_cmd
.scsi_sense_length
+ padding
),
3061 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3063 cmd
->se_cmd
.scsi_sense_length
);
3066 if (conn
->conn_ops
->HeaderDigest
) {
3067 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3069 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3070 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
3071 0, NULL
, (u8
*)header_digest
);
3073 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3074 tx_size
+= ISCSI_CRC_LEN
;
3075 pr_debug("Attaching CRC32 HeaderDigest for Response"
3076 " PDU 0x%08x\n", *header_digest
);
3079 cmd
->iov_misc_count
= iov_count
;
3080 cmd
->tx_size
= tx_size
;
3082 pr_debug("Built %sSCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3083 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3084 (!recovery
) ? "" : "Recovery ", cmd
->init_task_tag
,
3085 cmd
->stat_sn
, 0x00, cmd
->se_cmd
.scsi_status
, conn
->cid
);
3090 static u8
iscsit_convert_tcm_tmr_rsp(struct se_tmr_req
*se_tmr
)
3092 switch (se_tmr
->response
) {
3093 case TMR_FUNCTION_COMPLETE
:
3094 return ISCSI_TMF_RSP_COMPLETE
;
3095 case TMR_TASK_DOES_NOT_EXIST
:
3096 return ISCSI_TMF_RSP_NO_TASK
;
3097 case TMR_LUN_DOES_NOT_EXIST
:
3098 return ISCSI_TMF_RSP_NO_LUN
;
3099 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
:
3100 return ISCSI_TMF_RSP_NOT_SUPPORTED
;
3101 case TMR_FUNCTION_AUTHORIZATION_FAILED
:
3102 return ISCSI_TMF_RSP_AUTH_FAILED
;
3103 case TMR_FUNCTION_REJECTED
:
3105 return ISCSI_TMF_RSP_REJECTED
;
3109 static int iscsit_send_task_mgt_rsp(
3110 struct iscsi_cmd
*cmd
,
3111 struct iscsi_conn
*conn
)
3113 struct se_tmr_req
*se_tmr
= cmd
->se_cmd
.se_tmr_req
;
3114 struct iscsi_tm_rsp
*hdr
;
3117 hdr
= (struct iscsi_tm_rsp
*) cmd
->pdu
;
3118 memset(hdr
, 0, ISCSI_HDR_LEN
);
3119 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC_RSP
;
3120 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
3121 hdr
->response
= iscsit_convert_tcm_tmr_rsp(se_tmr
);
3122 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
3123 cmd
->stat_sn
= conn
->stat_sn
++;
3124 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3126 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3127 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3128 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3130 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
3131 cmd
->iov_misc
[0].iov_len
= ISCSI_HDR_LEN
;
3132 tx_size
+= ISCSI_HDR_LEN
;
3134 if (conn
->conn_ops
->HeaderDigest
) {
3135 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3137 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3138 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
3139 0, NULL
, (u8
*)header_digest
);
3141 cmd
->iov_misc
[0].iov_len
+= ISCSI_CRC_LEN
;
3142 tx_size
+= ISCSI_CRC_LEN
;
3143 pr_debug("Attaching CRC32 HeaderDigest for Task"
3144 " Mgmt Response PDU 0x%08x\n", *header_digest
);
3147 cmd
->iov_misc_count
= 1;
3148 cmd
->tx_size
= tx_size
;
3150 pr_debug("Built Task Management Response ITT: 0x%08x,"
3151 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3152 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
, conn
->cid
);
3157 static bool iscsit_check_inaddr_any(struct iscsi_np
*np
)
3161 if (np
->np_sockaddr
.ss_family
== AF_INET6
) {
3162 const struct sockaddr_in6 sin6
= {
3163 .sin6_addr
= IN6ADDR_ANY_INIT
};
3164 struct sockaddr_in6
*sock_in6
=
3165 (struct sockaddr_in6
*)&np
->np_sockaddr
;
3167 if (!memcmp(sock_in6
->sin6_addr
.s6_addr
,
3168 sin6
.sin6_addr
.s6_addr
, 16))
3171 struct sockaddr_in
* sock_in
=
3172 (struct sockaddr_in
*)&np
->np_sockaddr
;
3174 if (sock_in
->sin_addr
.s_addr
== INADDR_ANY
)
3181 static int iscsit_build_sendtargets_response(struct iscsi_cmd
*cmd
)
3183 char *payload
= NULL
;
3184 struct iscsi_conn
*conn
= cmd
->conn
;
3185 struct iscsi_portal_group
*tpg
;
3186 struct iscsi_tiqn
*tiqn
;
3187 struct iscsi_tpg_np
*tpg_np
;
3188 int buffer_len
, end_of_buf
= 0, len
= 0, payload_len
= 0;
3189 unsigned char buf
[256];
3191 buffer_len
= (conn
->conn_ops
->MaxRecvDataSegmentLength
> 32768) ?
3192 32768 : conn
->conn_ops
->MaxRecvDataSegmentLength
;
3194 memset(buf
, 0, 256);
3196 payload
= kzalloc(buffer_len
, GFP_KERNEL
);
3198 pr_err("Unable to allocate memory for sendtargets"
3203 spin_lock(&tiqn_lock
);
3204 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
3205 len
= sprintf(buf
, "TargetName=%s", tiqn
->tiqn
);
3208 if ((len
+ payload_len
) > buffer_len
) {
3212 memcpy(payload
+ payload_len
, buf
, len
);
3215 spin_lock(&tiqn
->tiqn_tpg_lock
);
3216 list_for_each_entry(tpg
, &tiqn
->tiqn_tpg_list
, tpg_list
) {
3218 spin_lock(&tpg
->tpg_state_lock
);
3219 if ((tpg
->tpg_state
== TPG_STATE_FREE
) ||
3220 (tpg
->tpg_state
== TPG_STATE_INACTIVE
)) {
3221 spin_unlock(&tpg
->tpg_state_lock
);
3224 spin_unlock(&tpg
->tpg_state_lock
);
3226 spin_lock(&tpg
->tpg_np_lock
);
3227 list_for_each_entry(tpg_np
, &tpg
->tpg_gnp_list
,
3229 struct iscsi_np
*np
= tpg_np
->tpg_np
;
3230 bool inaddr_any
= iscsit_check_inaddr_any(np
);
3232 len
= sprintf(buf
, "TargetAddress="
3234 (np
->np_sockaddr
.ss_family
== AF_INET6
) ?
3235 "[" : "", (inaddr_any
== false) ?
3236 np
->np_ip
: conn
->local_ip
,
3237 (np
->np_sockaddr
.ss_family
== AF_INET6
) ?
3238 "]" : "", (inaddr_any
== false) ?
3239 np
->np_port
: conn
->local_port
,
3243 if ((len
+ payload_len
) > buffer_len
) {
3244 spin_unlock(&tpg
->tpg_np_lock
);
3245 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3249 memcpy(payload
+ payload_len
, buf
, len
);
3252 spin_unlock(&tpg
->tpg_np_lock
);
3254 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3259 spin_unlock(&tiqn_lock
);
3261 cmd
->buf_ptr
= payload
;
3267 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3268 * MaxRecvDataSegmentLength.
3270 static int iscsit_send_text_rsp(
3271 struct iscsi_cmd
*cmd
,
3272 struct iscsi_conn
*conn
)
3274 struct iscsi_text_rsp
*hdr
;
3276 u32 padding
= 0, tx_size
= 0;
3277 int text_length
, iov_count
= 0;
3279 text_length
= iscsit_build_sendtargets_response(cmd
);
3280 if (text_length
< 0)
3283 padding
= ((-text_length
) & 3);
3285 memset(cmd
->buf_ptr
+ text_length
, 0, padding
);
3286 pr_debug("Attaching %u additional bytes for"
3287 " padding.\n", padding
);
3290 hdr
= (struct iscsi_text_rsp
*) cmd
->pdu
;
3291 memset(hdr
, 0, ISCSI_HDR_LEN
);
3292 hdr
->opcode
= ISCSI_OP_TEXT_RSP
;
3293 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3294 hton24(hdr
->dlength
, text_length
);
3295 hdr
->itt
= cpu_to_be32(cmd
->init_task_tag
);
3296 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
3297 cmd
->stat_sn
= conn
->stat_sn
++;
3298 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3300 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3301 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3302 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3304 iov
= &cmd
->iov_misc
[0];
3306 iov
[iov_count
].iov_base
= cmd
->pdu
;
3307 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3308 iov
[iov_count
].iov_base
= cmd
->buf_ptr
;
3309 iov
[iov_count
++].iov_len
= text_length
+ padding
;
3311 tx_size
+= (ISCSI_HDR_LEN
+ text_length
+ padding
);
3313 if (conn
->conn_ops
->HeaderDigest
) {
3314 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3316 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3317 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
3318 0, NULL
, (u8
*)header_digest
);
3320 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3321 tx_size
+= ISCSI_CRC_LEN
;
3322 pr_debug("Attaching CRC32 HeaderDigest for"
3323 " Text Response PDU 0x%08x\n", *header_digest
);
3326 if (conn
->conn_ops
->DataDigest
) {
3327 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3328 cmd
->buf_ptr
, (text_length
+ padding
),
3329 0, NULL
, (u8
*)&cmd
->data_crc
);
3331 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3332 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3333 tx_size
+= ISCSI_CRC_LEN
;
3335 pr_debug("Attaching DataDigest for %u bytes of text"
3336 " data, CRC 0x%08x\n", (text_length
+ padding
),
3340 cmd
->iov_misc_count
= iov_count
;
3341 cmd
->tx_size
= tx_size
;
3343 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3344 " Length: %u, CID: %hu\n", cmd
->init_task_tag
, cmd
->stat_sn
,
3345 text_length
, conn
->cid
);
3349 static int iscsit_send_reject(
3350 struct iscsi_cmd
*cmd
,
3351 struct iscsi_conn
*conn
)
3353 u32 iov_count
= 0, tx_size
= 0;
3354 struct iscsi_reject
*hdr
;
3357 hdr
= (struct iscsi_reject
*) cmd
->pdu
;
3358 hdr
->opcode
= ISCSI_OP_REJECT
;
3359 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3360 hton24(hdr
->dlength
, ISCSI_HDR_LEN
);
3361 hdr
->ffffffff
= 0xffffffff;
3362 cmd
->stat_sn
= conn
->stat_sn
++;
3363 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3364 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3365 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3367 iov
= &cmd
->iov_misc
[0];
3369 iov
[iov_count
].iov_base
= cmd
->pdu
;
3370 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3371 iov
[iov_count
].iov_base
= cmd
->buf_ptr
;
3372 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3374 tx_size
= (ISCSI_HDR_LEN
+ ISCSI_HDR_LEN
);
3376 if (conn
->conn_ops
->HeaderDigest
) {
3377 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3379 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3380 (unsigned char *)hdr
, ISCSI_HDR_LEN
,
3381 0, NULL
, (u8
*)header_digest
);
3383 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3384 tx_size
+= ISCSI_CRC_LEN
;
3385 pr_debug("Attaching CRC32 HeaderDigest for"
3386 " REJECT PDU 0x%08x\n", *header_digest
);
3389 if (conn
->conn_ops
->DataDigest
) {
3390 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3391 (unsigned char *)cmd
->buf_ptr
, ISCSI_HDR_LEN
,
3392 0, NULL
, (u8
*)&cmd
->data_crc
);
3394 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3395 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3396 tx_size
+= ISCSI_CRC_LEN
;
3397 pr_debug("Attaching CRC32 DataDigest for REJECT"
3398 " PDU 0x%08x\n", cmd
->data_crc
);
3401 cmd
->iov_misc_count
= iov_count
;
3402 cmd
->tx_size
= tx_size
;
3404 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3405 " CID: %hu\n", ntohl(hdr
->statsn
), hdr
->reason
, conn
->cid
);
3410 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
3412 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
3413 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
3414 wait_for_completion_interruptible_timeout(
3415 &conn
->tx_half_close_comp
,
3416 ISCSI_TX_THREAD_TCP_TIMEOUT
* HZ
);
3422 void iscsit_thread_get_cpumask(struct iscsi_conn
*conn
)
3424 struct iscsi_thread_set
*ts
= conn
->thread_set
;
3427 * thread_id is assigned from iscsit_global->ts_bitmap from
3428 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3430 * Here we use thread_id to determine which CPU that this
3431 * iSCSI connection's iscsi_thread_set will be scheduled to
3434 ord
= ts
->thread_id
% cpumask_weight(cpu_online_mask
);
3436 pr_debug(">>>>>>>>>>>>>>>>>>>> Generated ord: %d from"
3437 " thread_id: %d\n", ord
, ts
->thread_id
);
3439 for_each_online_cpu(cpu
) {
3441 cpumask_set_cpu(cpu
, conn
->conn_cpumask
);
3446 * This should never be reached..
3449 cpumask_setall(conn
->conn_cpumask
);
3452 static inline void iscsit_thread_check_cpumask(
3453 struct iscsi_conn
*conn
,
3454 struct task_struct
*p
,
3459 * mode == 1 signals iscsi_target_tx_thread() usage.
3460 * mode == 0 signals iscsi_target_rx_thread() usage.
3463 if (!conn
->conn_tx_reset_cpumask
)
3465 conn
->conn_tx_reset_cpumask
= 0;
3467 if (!conn
->conn_rx_reset_cpumask
)
3469 conn
->conn_rx_reset_cpumask
= 0;
3472 * Update the CPU mask for this single kthread so that
3473 * both TX and RX kthreads are scheduled to run on the
3476 memset(buf
, 0, 128);
3477 cpumask_scnprintf(buf
, 128, conn
->conn_cpumask
);
3479 pr_debug(">>>>>>>>>>>>>> Calling set_cpus_allowed_ptr():"
3480 " %s for %s\n", buf
, p
->comm
);
3482 set_cpus_allowed_ptr(p
, conn
->conn_cpumask
);
3487 void iscsit_thread_get_cpumask(struct iscsi_conn
*conn
)
3492 #define iscsit_thread_check_cpumask(X, Y, Z) ({})
3493 #endif /* CONFIG_SMP */
3495 int iscsi_target_tx_thread(void *arg
)
3500 int sent_status
= 0;
3503 struct iscsi_cmd
*cmd
= NULL
;
3504 struct iscsi_conn
*conn
;
3505 struct iscsi_queue_req
*qr
= NULL
;
3506 struct iscsi_thread_set
*ts
= arg
;
3508 * Allow ourselves to be interrupted by SIGINT so that a
3509 * connection recovery / failure event can be triggered externally.
3511 allow_signal(SIGINT
);
3514 conn
= iscsi_tx_thread_pre_handler(ts
);
3518 eodr
= map_sg
= ret
= sent_status
= use_misc
= 0;
3520 while (!kthread_should_stop()) {
3522 * Ensure that both TX and RX per connection kthreads
3523 * are scheduled to run on the same CPU.
3525 iscsit_thread_check_cpumask(conn
, current
, 1);
3527 wait_event_interruptible(conn
->queues_wq
,
3528 !iscsit_conn_all_queues_empty(conn
) ||
3529 ts
->status
== ISCSI_THREAD_SET_RESET
);
3531 if ((ts
->status
== ISCSI_THREAD_SET_RESET
) ||
3532 signal_pending(current
))
3536 qr
= iscsit_get_cmd_from_immediate_queue(conn
);
3538 atomic_set(&conn
->check_immediate_queue
, 0);
3541 kmem_cache_free(lio_qr_cache
, qr
);
3543 spin_lock_bh(&cmd
->istate_lock
);
3545 case ISTATE_SEND_R2T
:
3546 spin_unlock_bh(&cmd
->istate_lock
);
3547 ret
= iscsit_send_r2t(cmd
, conn
);
3550 spin_unlock_bh(&cmd
->istate_lock
);
3552 if (cmd
->data_direction
== DMA_TO_DEVICE
)
3553 iscsit_stop_dataout_timer(cmd
);
3555 spin_lock_bh(&conn
->cmd_lock
);
3556 list_del(&cmd
->i_list
);
3557 spin_unlock_bh(&conn
->cmd_lock
);
3559 iscsit_free_cmd(cmd
);
3561 case ISTATE_SEND_NOPIN_WANT_RESPONSE
:
3562 spin_unlock_bh(&cmd
->istate_lock
);
3563 iscsit_mod_nopin_response_timer(conn
);
3564 ret
= iscsit_send_unsolicited_nopin(cmd
,
3567 case ISTATE_SEND_NOPIN_NO_RESPONSE
:
3568 spin_unlock_bh(&cmd
->istate_lock
);
3569 ret
= iscsit_send_unsolicited_nopin(cmd
,
3573 pr_err("Unknown Opcode: 0x%02x ITT:"
3574 " 0x%08x, i_state: %d on CID: %hu\n",
3575 cmd
->iscsi_opcode
, cmd
->init_task_tag
, state
,
3577 spin_unlock_bh(&cmd
->istate_lock
);
3581 conn
->tx_immediate_queue
= 0;
3585 if (iscsit_send_tx_data(cmd
, conn
, 1) < 0) {
3586 conn
->tx_immediate_queue
= 0;
3587 iscsit_tx_thread_wait_for_tcp(conn
);
3591 spin_lock_bh(&cmd
->istate_lock
);
3593 case ISTATE_SEND_R2T
:
3594 spin_unlock_bh(&cmd
->istate_lock
);
3595 spin_lock_bh(&cmd
->dataout_timeout_lock
);
3596 iscsit_start_dataout_timer(cmd
, conn
);
3597 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
3599 case ISTATE_SEND_NOPIN_WANT_RESPONSE
:
3600 cmd
->i_state
= ISTATE_SENT_NOPIN_WANT_RESPONSE
;
3601 spin_unlock_bh(&cmd
->istate_lock
);
3603 case ISTATE_SEND_NOPIN_NO_RESPONSE
:
3604 cmd
->i_state
= ISTATE_SENT_STATUS
;
3605 spin_unlock_bh(&cmd
->istate_lock
);
3608 pr_err("Unknown Opcode: 0x%02x ITT:"
3609 " 0x%08x, i_state: %d on CID: %hu\n",
3610 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3612 spin_unlock_bh(&cmd
->istate_lock
);
3617 conn
->tx_immediate_queue
= 0;
3620 qr
= iscsit_get_cmd_from_response_queue(conn
);
3624 kmem_cache_free(lio_qr_cache
, qr
);
3626 spin_lock_bh(&cmd
->istate_lock
);
3629 case ISTATE_SEND_DATAIN
:
3630 spin_unlock_bh(&cmd
->istate_lock
);
3631 ret
= iscsit_send_data_in(cmd
, conn
,
3635 case ISTATE_SEND_STATUS
:
3636 case ISTATE_SEND_STATUS_RECOVERY
:
3637 spin_unlock_bh(&cmd
->istate_lock
);
3639 ret
= iscsit_send_status(cmd
, conn
);
3641 case ISTATE_SEND_LOGOUTRSP
:
3642 spin_unlock_bh(&cmd
->istate_lock
);
3644 ret
= iscsit_send_logout_response(cmd
, conn
);
3646 case ISTATE_SEND_ASYNCMSG
:
3647 spin_unlock_bh(&cmd
->istate_lock
);
3649 ret
= iscsit_send_conn_drop_async_message(
3652 case ISTATE_SEND_NOPIN
:
3653 spin_unlock_bh(&cmd
->istate_lock
);
3655 ret
= iscsit_send_nopin_response(cmd
, conn
);
3657 case ISTATE_SEND_REJECT
:
3658 spin_unlock_bh(&cmd
->istate_lock
);
3660 ret
= iscsit_send_reject(cmd
, conn
);
3662 case ISTATE_SEND_TASKMGTRSP
:
3663 spin_unlock_bh(&cmd
->istate_lock
);
3665 ret
= iscsit_send_task_mgt_rsp(cmd
, conn
);
3668 ret
= iscsit_tmr_post_handler(cmd
, conn
);
3670 iscsit_fall_back_to_erl0(conn
->sess
);
3672 case ISTATE_SEND_TEXTRSP
:
3673 spin_unlock_bh(&cmd
->istate_lock
);
3675 ret
= iscsit_send_text_rsp(cmd
, conn
);
3678 pr_err("Unknown Opcode: 0x%02x ITT:"
3679 " 0x%08x, i_state: %d on CID: %hu\n",
3680 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3682 spin_unlock_bh(&cmd
->istate_lock
);
3686 conn
->tx_response_queue
= 0;
3690 if (map_sg
&& !conn
->conn_ops
->IFMarker
) {
3691 if (iscsit_fe_sendpage_sg(cmd
, conn
) < 0) {
3692 conn
->tx_response_queue
= 0;
3693 iscsit_tx_thread_wait_for_tcp(conn
);
3694 iscsit_unmap_iovec(cmd
);
3698 if (iscsit_send_tx_data(cmd
, conn
, use_misc
) < 0) {
3699 conn
->tx_response_queue
= 0;
3700 iscsit_tx_thread_wait_for_tcp(conn
);
3701 iscsit_unmap_iovec(cmd
);
3706 iscsit_unmap_iovec(cmd
);
3708 spin_lock_bh(&cmd
->istate_lock
);
3710 case ISTATE_SEND_DATAIN
:
3712 goto check_rsp_state
;
3715 cmd
->i_state
= ISTATE_SENT_LAST_DATAIN
;
3717 eodr
= use_misc
= 0;
3718 } else if (eodr
== 2) {
3719 cmd
->i_state
= state
=
3722 eodr
= use_misc
= 0;
3723 goto check_rsp_state
;
3726 case ISTATE_SEND_STATUS
:
3730 case ISTATE_SEND_ASYNCMSG
:
3731 case ISTATE_SEND_NOPIN
:
3732 case ISTATE_SEND_STATUS_RECOVERY
:
3733 case ISTATE_SEND_TEXTRSP
:
3737 case ISTATE_SEND_REJECT
:
3739 if (cmd
->cmd_flags
& ICF_REJECT_FAIL_CONN
) {
3740 cmd
->cmd_flags
&= ~ICF_REJECT_FAIL_CONN
;
3741 spin_unlock_bh(&cmd
->istate_lock
);
3742 complete(&cmd
->reject_comp
);
3745 complete(&cmd
->reject_comp
);
3747 case ISTATE_SEND_TASKMGTRSP
:
3751 case ISTATE_SEND_LOGOUTRSP
:
3752 spin_unlock_bh(&cmd
->istate_lock
);
3753 if (!iscsit_logout_post_handler(cmd
, conn
))
3755 spin_lock_bh(&cmd
->istate_lock
);
3760 pr_err("Unknown Opcode: 0x%02x ITT:"
3761 " 0x%08x, i_state: %d on CID: %hu\n",
3762 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3763 cmd
->i_state
, conn
->cid
);
3764 spin_unlock_bh(&cmd
->istate_lock
);
3769 cmd
->i_state
= ISTATE_SENT_STATUS
;
3772 spin_unlock_bh(&cmd
->istate_lock
);
3774 if (atomic_read(&conn
->check_immediate_queue
))
3779 conn
->tx_response_queue
= 0;
3783 iscsit_take_action_for_connection_exit(conn
);
3789 int iscsi_target_rx_thread(void *arg
)
3792 u8 buffer
[ISCSI_HDR_LEN
], opcode
;
3793 u32 checksum
= 0, digest
= 0;
3794 struct iscsi_conn
*conn
= NULL
;
3795 struct iscsi_thread_set
*ts
= arg
;
3798 * Allow ourselves to be interrupted by SIGINT so that a
3799 * connection recovery / failure event can be triggered externally.
3801 allow_signal(SIGINT
);
3804 conn
= iscsi_rx_thread_pre_handler(ts
);
3808 while (!kthread_should_stop()) {
3810 * Ensure that both TX and RX per connection kthreads
3811 * are scheduled to run on the same CPU.
3813 iscsit_thread_check_cpumask(conn
, current
, 0);
3815 memset(buffer
, 0, ISCSI_HDR_LEN
);
3816 memset(&iov
, 0, sizeof(struct kvec
));
3818 iov
.iov_base
= buffer
;
3819 iov
.iov_len
= ISCSI_HDR_LEN
;
3821 ret
= rx_data(conn
, &iov
, 1, ISCSI_HDR_LEN
);
3822 if (ret
!= ISCSI_HDR_LEN
) {
3823 iscsit_rx_thread_wait_for_tcp(conn
);
3828 * Set conn->bad_hdr for use with REJECT PDUs.
3830 memcpy(&conn
->bad_hdr
, &buffer
, ISCSI_HDR_LEN
);
3832 if (conn
->conn_ops
->HeaderDigest
) {
3833 iov
.iov_base
= &digest
;
3834 iov
.iov_len
= ISCSI_CRC_LEN
;
3836 ret
= rx_data(conn
, &iov
, 1, ISCSI_CRC_LEN
);
3837 if (ret
!= ISCSI_CRC_LEN
) {
3838 iscsit_rx_thread_wait_for_tcp(conn
);
3842 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
3843 buffer
, ISCSI_HDR_LEN
,
3844 0, NULL
, (u8
*)&checksum
);
3846 if (digest
!= checksum
) {
3847 pr_err("HeaderDigest CRC32C failed,"
3848 " received 0x%08x, computed 0x%08x\n",
3851 * Set the PDU to 0xff so it will intentionally
3852 * hit default in the switch below.
3854 memset(buffer
, 0xff, ISCSI_HDR_LEN
);
3855 spin_lock_bh(&conn
->sess
->session_stats_lock
);
3856 conn
->sess
->conn_digest_errors
++;
3857 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
3859 pr_debug("Got HeaderDigest CRC32C"
3860 " 0x%08x\n", checksum
);
3864 if (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
)
3867 opcode
= buffer
[0] & ISCSI_OPCODE_MASK
;
3869 if (conn
->sess
->sess_ops
->SessionType
&&
3870 ((!(opcode
& ISCSI_OP_TEXT
)) ||
3871 (!(opcode
& ISCSI_OP_LOGOUT
)))) {
3872 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3873 " while in Discovery Session, rejecting.\n", opcode
);
3874 iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR
, 1,
3880 case ISCSI_OP_SCSI_CMD
:
3881 if (iscsit_handle_scsi_cmd(conn
, buffer
) < 0)
3884 case ISCSI_OP_SCSI_DATA_OUT
:
3885 if (iscsit_handle_data_out(conn
, buffer
) < 0)
3888 case ISCSI_OP_NOOP_OUT
:
3889 if (iscsit_handle_nop_out(conn
, buffer
) < 0)
3892 case ISCSI_OP_SCSI_TMFUNC
:
3893 if (iscsit_handle_task_mgt_cmd(conn
, buffer
) < 0)
3897 if (iscsit_handle_text_cmd(conn
, buffer
) < 0)
3900 case ISCSI_OP_LOGOUT
:
3901 ret
= iscsit_handle_logout_cmd(conn
, buffer
);
3903 wait_for_completion_timeout(&conn
->conn_logout_comp
,
3904 SECONDS_FOR_LOGOUT_COMP
* HZ
);
3909 case ISCSI_OP_SNACK
:
3910 if (iscsit_handle_snack(conn
, buffer
) < 0)
3914 pr_err("Got unknown iSCSI OpCode: 0x%02x\n",
3916 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
3917 pr_err("Cannot recover from unknown"
3918 " opcode while ERL=0, closing iSCSI connection"
3922 if (!conn
->conn_ops
->OFMarker
) {
3923 pr_err("Unable to recover from unknown"
3924 " opcode while OFMarker=No, closing iSCSI"
3928 if (iscsit_recover_from_unknown_opcode(conn
) < 0) {
3929 pr_err("Unable to recover from unknown"
3930 " opcode, closing iSCSI connection.\n");
3938 if (!signal_pending(current
))
3939 atomic_set(&conn
->transport_failed
, 1);
3940 iscsit_take_action_for_connection_exit(conn
);
3946 static void iscsit_release_commands_from_conn(struct iscsi_conn
*conn
)
3948 struct iscsi_cmd
*cmd
= NULL
, *cmd_tmp
= NULL
;
3949 struct iscsi_session
*sess
= conn
->sess
;
3951 * We expect this function to only ever be called from either RX or TX
3952 * thread context via iscsit_close_connection() once the other context
3953 * has been reset -> returned sleeping pre-handler state.
3955 spin_lock_bh(&conn
->cmd_lock
);
3956 list_for_each_entry_safe(cmd
, cmd_tmp
, &conn
->conn_cmd_list
, i_list
) {
3958 list_del(&cmd
->i_list
);
3959 spin_unlock_bh(&conn
->cmd_lock
);
3961 iscsit_increment_maxcmdsn(cmd
, sess
);
3963 iscsit_free_cmd(cmd
);
3965 spin_lock_bh(&conn
->cmd_lock
);
3967 spin_unlock_bh(&conn
->cmd_lock
);
3970 static void iscsit_stop_timers_for_cmds(
3971 struct iscsi_conn
*conn
)
3973 struct iscsi_cmd
*cmd
;
3975 spin_lock_bh(&conn
->cmd_lock
);
3976 list_for_each_entry(cmd
, &conn
->conn_cmd_list
, i_list
) {
3977 if (cmd
->data_direction
== DMA_TO_DEVICE
)
3978 iscsit_stop_dataout_timer(cmd
);
3980 spin_unlock_bh(&conn
->cmd_lock
);
3983 int iscsit_close_connection(
3984 struct iscsi_conn
*conn
)
3986 int conn_logout
= (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
);
3987 struct iscsi_session
*sess
= conn
->sess
;
3989 pr_debug("Closing iSCSI connection CID %hu on SID:"
3990 " %u\n", conn
->cid
, sess
->sid
);
3992 * Always up conn_logout_comp just in case the RX Thread is sleeping
3993 * and the logout response never got sent because the connection
3996 complete(&conn
->conn_logout_comp
);
3998 iscsi_release_thread_set(conn
);
4000 iscsit_stop_timers_for_cmds(conn
);
4001 iscsit_stop_nopin_response_timer(conn
);
4002 iscsit_stop_nopin_timer(conn
);
4003 iscsit_free_queue_reqs_for_conn(conn
);
4006 * During Connection recovery drop unacknowledged out of order
4007 * commands for this connection, and prepare the other commands
4010 * During normal operation clear the out of order commands (but
4011 * do not free the struct iscsi_ooo_cmdsn's) and release all
4012 * struct iscsi_cmds.
4014 if (atomic_read(&conn
->connection_recovery
)) {
4015 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn
);
4016 iscsit_prepare_cmds_for_realligance(conn
);
4018 iscsit_clear_ooo_cmdsns_for_conn(conn
);
4019 iscsit_release_commands_from_conn(conn
);
4023 * Handle decrementing session or connection usage count if
4024 * a logout response was not able to be sent because the
4025 * connection failed. Fall back to Session Recovery here.
4027 if (atomic_read(&conn
->conn_logout_remove
)) {
4028 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) {
4029 iscsit_dec_conn_usage_count(conn
);
4030 iscsit_dec_session_usage_count(sess
);
4032 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
)
4033 iscsit_dec_conn_usage_count(conn
);
4035 atomic_set(&conn
->conn_logout_remove
, 0);
4036 atomic_set(&sess
->session_reinstatement
, 0);
4037 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4040 spin_lock_bh(&sess
->conn_lock
);
4041 list_del(&conn
->conn_list
);
4044 * Attempt to let the Initiator know this connection failed by
4045 * sending an Connection Dropped Async Message on another
4046 * active connection.
4048 if (atomic_read(&conn
->connection_recovery
))
4049 iscsit_build_conn_drop_async_message(conn
);
4051 spin_unlock_bh(&sess
->conn_lock
);
4054 * If connection reinstatement is being performed on this connection,
4055 * up the connection reinstatement semaphore that is being blocked on
4056 * in iscsit_cause_connection_reinstatement().
4058 spin_lock_bh(&conn
->state_lock
);
4059 if (atomic_read(&conn
->sleep_on_conn_wait_comp
)) {
4060 spin_unlock_bh(&conn
->state_lock
);
4061 complete(&conn
->conn_wait_comp
);
4062 wait_for_completion(&conn
->conn_post_wait_comp
);
4063 spin_lock_bh(&conn
->state_lock
);
4067 * If connection reinstatement is being performed on this connection
4068 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4069 * connection wait rcfr semaphore that is being blocked on
4070 * an iscsit_connection_reinstatement_rcfr().
4072 if (atomic_read(&conn
->connection_wait_rcfr
)) {
4073 spin_unlock_bh(&conn
->state_lock
);
4074 complete(&conn
->conn_wait_rcfr_comp
);
4075 wait_for_completion(&conn
->conn_post_wait_comp
);
4076 spin_lock_bh(&conn
->state_lock
);
4078 atomic_set(&conn
->connection_reinstatement
, 1);
4079 spin_unlock_bh(&conn
->state_lock
);
4082 * If any other processes are accessing this connection pointer we
4083 * must wait until they have completed.
4085 iscsit_check_conn_usage_count(conn
);
4087 if (conn
->conn_rx_hash
.tfm
)
4088 crypto_free_hash(conn
->conn_rx_hash
.tfm
);
4089 if (conn
->conn_tx_hash
.tfm
)
4090 crypto_free_hash(conn
->conn_tx_hash
.tfm
);
4092 if (conn
->conn_cpumask
)
4093 free_cpumask_var(conn
->conn_cpumask
);
4095 kfree(conn
->conn_ops
);
4096 conn
->conn_ops
= NULL
;
4099 sock_release(conn
->sock
);
4100 conn
->thread_set
= NULL
;
4102 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4103 conn
->conn_state
= TARG_CONN_STATE_FREE
;
4106 spin_lock_bh(&sess
->conn_lock
);
4107 atomic_dec(&sess
->nconn
);
4108 pr_debug("Decremented iSCSI connection count to %hu from node:"
4109 " %s\n", atomic_read(&sess
->nconn
),
4110 sess
->sess_ops
->InitiatorName
);
4112 * Make sure that if one connection fails in an non ERL=2 iSCSI
4113 * Session that they all fail.
4115 if ((sess
->sess_ops
->ErrorRecoveryLevel
!= 2) && !conn_logout
&&
4116 !atomic_read(&sess
->session_logout
))
4117 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4120 * If this was not the last connection in the session, and we are
4121 * performing session reinstatement or falling back to ERL=0, call
4122 * iscsit_stop_session() without sleeping to shutdown the other
4123 * active connections.
4125 if (atomic_read(&sess
->nconn
)) {
4126 if (!atomic_read(&sess
->session_reinstatement
) &&
4127 !atomic_read(&sess
->session_fall_back_to_erl0
)) {
4128 spin_unlock_bh(&sess
->conn_lock
);
4131 if (!atomic_read(&sess
->session_stop_active
)) {
4132 atomic_set(&sess
->session_stop_active
, 1);
4133 spin_unlock_bh(&sess
->conn_lock
);
4134 iscsit_stop_session(sess
, 0, 0);
4137 spin_unlock_bh(&sess
->conn_lock
);
4142 * If this was the last connection in the session and one of the
4143 * following is occurring:
4145 * Session Reinstatement is not being performed, and are falling back
4146 * to ERL=0 call iscsit_close_session().
4148 * Session Logout was requested. iscsit_close_session() will be called
4151 * Session Continuation is not being performed, start the Time2Retain
4152 * handler and check if sleep_on_sess_wait_sem is active.
4154 if (!atomic_read(&sess
->session_reinstatement
) &&
4155 atomic_read(&sess
->session_fall_back_to_erl0
)) {
4156 spin_unlock_bh(&sess
->conn_lock
);
4157 target_put_session(sess
->se_sess
);
4160 } else if (atomic_read(&sess
->session_logout
)) {
4161 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4162 sess
->session_state
= TARG_SESS_STATE_FREE
;
4163 spin_unlock_bh(&sess
->conn_lock
);
4165 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4166 complete(&sess
->session_wait_comp
);
4170 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4171 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4173 if (!atomic_read(&sess
->session_continuation
)) {
4174 spin_unlock_bh(&sess
->conn_lock
);
4175 iscsit_start_time2retain_handler(sess
);
4177 spin_unlock_bh(&sess
->conn_lock
);
4179 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4180 complete(&sess
->session_wait_comp
);
4184 spin_unlock_bh(&sess
->conn_lock
);
4189 int iscsit_close_session(struct iscsi_session
*sess
)
4191 struct iscsi_portal_group
*tpg
= ISCSI_TPG_S(sess
);
4192 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4194 if (atomic_read(&sess
->nconn
)) {
4195 pr_err("%d connection(s) still exist for iSCSI session"
4196 " to %s\n", atomic_read(&sess
->nconn
),
4197 sess
->sess_ops
->InitiatorName
);
4201 spin_lock_bh(&se_tpg
->session_lock
);
4202 atomic_set(&sess
->session_logout
, 1);
4203 atomic_set(&sess
->session_reinstatement
, 1);
4204 iscsit_stop_time2retain_timer(sess
);
4205 spin_unlock_bh(&se_tpg
->session_lock
);
4208 * transport_deregister_session_configfs() will clear the
4209 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4210 * can be setting it again with __transport_register_session() in
4211 * iscsi_post_login_handler() again after the iscsit_stop_session()
4212 * completes in iscsi_np context.
4214 transport_deregister_session_configfs(sess
->se_sess
);
4217 * If any other processes are accessing this session pointer we must
4218 * wait until they have completed. If we are in an interrupt (the
4219 * time2retain handler) and contain and active session usage count we
4220 * restart the timer and exit.
4222 if (!in_interrupt()) {
4223 if (iscsit_check_session_usage_count(sess
) == 1)
4224 iscsit_stop_session(sess
, 1, 1);
4226 if (iscsit_check_session_usage_count(sess
) == 2) {
4227 atomic_set(&sess
->session_logout
, 0);
4228 iscsit_start_time2retain_handler(sess
);
4233 transport_deregister_session(sess
->se_sess
);
4235 if (sess
->sess_ops
->ErrorRecoveryLevel
== 2)
4236 iscsit_free_connection_recovery_entires(sess
);
4238 iscsit_free_all_ooo_cmdsns(sess
);
4240 spin_lock_bh(&se_tpg
->session_lock
);
4241 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4242 sess
->session_state
= TARG_SESS_STATE_FREE
;
4243 pr_debug("Released iSCSI session from node: %s\n",
4244 sess
->sess_ops
->InitiatorName
);
4247 tpg
->tpg_tiqn
->tiqn_nsessions
--;
4249 pr_debug("Decremented number of active iSCSI Sessions on"
4250 " iSCSI TPG: %hu to %u\n", tpg
->tpgt
, tpg
->nsessions
);
4252 spin_lock(&sess_idr_lock
);
4253 idr_remove(&sess_idr
, sess
->session_index
);
4254 spin_unlock(&sess_idr_lock
);
4256 kfree(sess
->sess_ops
);
4257 sess
->sess_ops
= NULL
;
4258 spin_unlock_bh(&se_tpg
->session_lock
);
4264 static void iscsit_logout_post_handler_closesession(
4265 struct iscsi_conn
*conn
)
4267 struct iscsi_session
*sess
= conn
->sess
;
4269 iscsi_set_thread_clear(conn
, ISCSI_CLEAR_TX_THREAD
);
4270 iscsi_set_thread_set_signal(conn
, ISCSI_SIGNAL_TX_THREAD
);
4272 atomic_set(&conn
->conn_logout_remove
, 0);
4273 complete(&conn
->conn_logout_comp
);
4275 iscsit_dec_conn_usage_count(conn
);
4276 iscsit_stop_session(sess
, 1, 1);
4277 iscsit_dec_session_usage_count(sess
);
4278 target_put_session(sess
->se_sess
);
4281 static void iscsit_logout_post_handler_samecid(
4282 struct iscsi_conn
*conn
)
4284 iscsi_set_thread_clear(conn
, ISCSI_CLEAR_TX_THREAD
);
4285 iscsi_set_thread_set_signal(conn
, ISCSI_SIGNAL_TX_THREAD
);
4287 atomic_set(&conn
->conn_logout_remove
, 0);
4288 complete(&conn
->conn_logout_comp
);
4290 iscsit_cause_connection_reinstatement(conn
, 1);
4291 iscsit_dec_conn_usage_count(conn
);
4294 static void iscsit_logout_post_handler_diffcid(
4295 struct iscsi_conn
*conn
,
4298 struct iscsi_conn
*l_conn
;
4299 struct iscsi_session
*sess
= conn
->sess
;
4304 spin_lock_bh(&sess
->conn_lock
);
4305 list_for_each_entry(l_conn
, &sess
->sess_conn_list
, conn_list
) {
4306 if (l_conn
->cid
== cid
) {
4307 iscsit_inc_conn_usage_count(l_conn
);
4311 spin_unlock_bh(&sess
->conn_lock
);
4317 l_conn
->sock
->ops
->shutdown(l_conn
->sock
, RCV_SHUTDOWN
);
4319 spin_lock_bh(&l_conn
->state_lock
);
4320 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4321 l_conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
4322 spin_unlock_bh(&l_conn
->state_lock
);
4324 iscsit_cause_connection_reinstatement(l_conn
, 1);
4325 iscsit_dec_conn_usage_count(l_conn
);
4329 * Return of 0 causes the TX thread to restart.
4331 static int iscsit_logout_post_handler(
4332 struct iscsi_cmd
*cmd
,
4333 struct iscsi_conn
*conn
)
4337 switch (cmd
->logout_reason
) {
4338 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
4339 switch (cmd
->logout_response
) {
4340 case ISCSI_LOGOUT_SUCCESS
:
4341 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4343 iscsit_logout_post_handler_closesession(conn
);
4348 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
4349 if (conn
->cid
== cmd
->logout_cid
) {
4350 switch (cmd
->logout_response
) {
4351 case ISCSI_LOGOUT_SUCCESS
:
4352 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4354 iscsit_logout_post_handler_samecid(conn
);
4359 switch (cmd
->logout_response
) {
4360 case ISCSI_LOGOUT_SUCCESS
:
4361 iscsit_logout_post_handler_diffcid(conn
,
4364 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4365 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4372 case ISCSI_LOGOUT_REASON_RECOVERY
:
4373 switch (cmd
->logout_response
) {
4374 case ISCSI_LOGOUT_SUCCESS
:
4375 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4376 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
:
4377 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4390 void iscsit_fail_session(struct iscsi_session
*sess
)
4392 struct iscsi_conn
*conn
;
4394 spin_lock_bh(&sess
->conn_lock
);
4395 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
) {
4396 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4397 conn
->conn_state
= TARG_CONN_STATE_CLEANUP_WAIT
;
4399 spin_unlock_bh(&sess
->conn_lock
);
4401 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4402 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4405 int iscsit_free_session(struct iscsi_session
*sess
)
4407 u16 conn_count
= atomic_read(&sess
->nconn
);
4408 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4411 spin_lock_bh(&sess
->conn_lock
);
4412 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4414 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4416 if (conn_count
== 0)
4419 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4422 iscsit_inc_conn_usage_count(conn_tmp
);
4425 iscsit_inc_conn_usage_count(conn
);
4427 spin_unlock_bh(&sess
->conn_lock
);
4428 iscsit_cause_connection_reinstatement(conn
, 1);
4429 spin_lock_bh(&sess
->conn_lock
);
4431 iscsit_dec_conn_usage_count(conn
);
4433 iscsit_dec_conn_usage_count(conn_tmp
);
4438 if (atomic_read(&sess
->nconn
)) {
4439 spin_unlock_bh(&sess
->conn_lock
);
4440 wait_for_completion(&sess
->session_wait_comp
);
4442 spin_unlock_bh(&sess
->conn_lock
);
4444 target_put_session(sess
->se_sess
);
4448 void iscsit_stop_session(
4449 struct iscsi_session
*sess
,
4451 int connection_sleep
)
4453 u16 conn_count
= atomic_read(&sess
->nconn
);
4454 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4457 spin_lock_bh(&sess
->conn_lock
);
4459 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4461 if (connection_sleep
) {
4462 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4464 if (conn_count
== 0)
4467 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4470 iscsit_inc_conn_usage_count(conn_tmp
);
4473 iscsit_inc_conn_usage_count(conn
);
4475 spin_unlock_bh(&sess
->conn_lock
);
4476 iscsit_cause_connection_reinstatement(conn
, 1);
4477 spin_lock_bh(&sess
->conn_lock
);
4479 iscsit_dec_conn_usage_count(conn
);
4481 iscsit_dec_conn_usage_count(conn_tmp
);
4485 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
)
4486 iscsit_cause_connection_reinstatement(conn
, 0);
4489 if (session_sleep
&& atomic_read(&sess
->nconn
)) {
4490 spin_unlock_bh(&sess
->conn_lock
);
4491 wait_for_completion(&sess
->session_wait_comp
);
4493 spin_unlock_bh(&sess
->conn_lock
);
4496 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group
*tpg
, int force
)
4498 struct iscsi_session
*sess
;
4499 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4500 struct se_session
*se_sess
, *se_sess_tmp
;
4501 int session_count
= 0;
4503 spin_lock_bh(&se_tpg
->session_lock
);
4504 if (tpg
->nsessions
&& !force
) {
4505 spin_unlock_bh(&se_tpg
->session_lock
);
4509 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &se_tpg
->tpg_sess_list
,
4511 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4513 spin_lock(&sess
->conn_lock
);
4514 if (atomic_read(&sess
->session_fall_back_to_erl0
) ||
4515 atomic_read(&sess
->session_logout
) ||
4516 (sess
->time2retain_timer_flags
& ISCSI_TF_EXPIRED
)) {
4517 spin_unlock(&sess
->conn_lock
);
4520 atomic_set(&sess
->session_reinstatement
, 1);
4521 spin_unlock(&sess
->conn_lock
);
4522 spin_unlock_bh(&se_tpg
->session_lock
);
4524 iscsit_free_session(sess
);
4525 spin_lock_bh(&se_tpg
->session_lock
);
4529 spin_unlock_bh(&se_tpg
->session_lock
);
4531 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4532 " Group: %hu\n", session_count
, tpg
->tpgt
);
4536 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4537 MODULE_VERSION("4.1.x");
4538 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4539 MODULE_LICENSE("GPL");
4541 module_init(iscsi_target_init_module
);
4542 module_exit(iscsi_target_cleanup_module
);