1 /*******************************************************************************
2 * This file contains main functions related to the iSCSI Target Core Driver.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
19 #include <linux/string.h>
20 #include <linux/kthread.h>
21 #include <linux/crypto.h>
22 #include <linux/completion.h>
23 #include <linux/module.h>
24 #include <linux/idr.h>
25 #include <asm/unaligned.h>
26 #include <scsi/scsi_device.h>
27 #include <scsi/iscsi_proto.h>
28 #include <scsi/scsi_tcq.h>
29 #include <target/target_core_base.h>
30 #include <target/target_core_fabric.h>
31 #include <target/target_core_configfs.h>
33 #include "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 #include <target/iscsi/iscsi_transport.h>
52 static LIST_HEAD(g_tiqn_list
);
53 static LIST_HEAD(g_np_list
);
54 static DEFINE_SPINLOCK(tiqn_lock
);
55 static DEFINE_MUTEX(np_lock
);
57 static struct idr tiqn_idr
;
59 struct mutex auth_id_lock
;
60 spinlock_t sess_idr_lock
;
62 struct iscsit_global
*iscsit_global
;
64 struct kmem_cache
*lio_qr_cache
;
65 struct kmem_cache
*lio_dr_cache
;
66 struct kmem_cache
*lio_ooo_cache
;
67 struct kmem_cache
*lio_r2t_cache
;
69 static int iscsit_handle_immediate_data(struct iscsi_cmd
*,
70 struct iscsi_scsi_req
*, u32
);
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 tiqn
->tiqn_state
= TIQN_STATE_ACTIVE
;
147 idr_preload(GFP_KERNEL
);
148 spin_lock(&tiqn_lock
);
150 ret
= idr_alloc(&tiqn_idr
, NULL
, 0, 0, GFP_NOWAIT
);
152 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
153 spin_unlock(&tiqn_lock
);
158 tiqn
->tiqn_index
= ret
;
159 list_add_tail(&tiqn
->tiqn_list
, &g_tiqn_list
);
161 spin_unlock(&tiqn_lock
);
164 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn
->tiqn
);
170 static void iscsit_wait_for_tiqn(struct iscsi_tiqn
*tiqn
)
173 * Wait for accesses to said struct iscsi_tiqn to end.
175 spin_lock(&tiqn
->tiqn_state_lock
);
176 while (tiqn
->tiqn_access_count
!= 0) {
177 spin_unlock(&tiqn
->tiqn_state_lock
);
179 spin_lock(&tiqn
->tiqn_state_lock
);
181 spin_unlock(&tiqn
->tiqn_state_lock
);
184 void iscsit_del_tiqn(struct iscsi_tiqn
*tiqn
)
187 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
188 * while holding tiqn->tiqn_state_lock. This means that all subsequent
189 * attempts to access this struct iscsi_tiqn will fail from both transport
190 * fabric and control code paths.
192 if (iscsit_set_tiqn_shutdown(tiqn
) < 0) {
193 pr_err("iscsit_set_tiqn_shutdown() failed\n");
197 iscsit_wait_for_tiqn(tiqn
);
199 spin_lock(&tiqn_lock
);
200 list_del(&tiqn
->tiqn_list
);
201 idr_remove(&tiqn_idr
, tiqn
->tiqn_index
);
202 spin_unlock(&tiqn_lock
);
204 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
209 int iscsit_access_np(struct iscsi_np
*np
, struct iscsi_portal_group
*tpg
)
213 * Determine if the network portal is accepting storage traffic.
215 spin_lock_bh(&np
->np_thread_lock
);
216 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
217 spin_unlock_bh(&np
->np_thread_lock
);
220 spin_unlock_bh(&np
->np_thread_lock
);
222 * Determine if the portal group is accepting storage traffic.
224 spin_lock_bh(&tpg
->tpg_state_lock
);
225 if (tpg
->tpg_state
!= TPG_STATE_ACTIVE
) {
226 spin_unlock_bh(&tpg
->tpg_state_lock
);
229 spin_unlock_bh(&tpg
->tpg_state_lock
);
232 * Here we serialize access across the TIQN+TPG Tuple.
234 ret
= down_interruptible(&tpg
->np_login_sem
);
235 if ((ret
!= 0) || signal_pending(current
))
238 spin_lock_bh(&tpg
->tpg_state_lock
);
239 if (tpg
->tpg_state
!= TPG_STATE_ACTIVE
) {
240 spin_unlock_bh(&tpg
->tpg_state_lock
);
241 up(&tpg
->np_login_sem
);
244 spin_unlock_bh(&tpg
->tpg_state_lock
);
249 void iscsit_login_kref_put(struct kref
*kref
)
251 struct iscsi_tpg_np
*tpg_np
= container_of(kref
,
252 struct iscsi_tpg_np
, tpg_np_kref
);
254 complete(&tpg_np
->tpg_np_comp
);
257 int iscsit_deaccess_np(struct iscsi_np
*np
, struct iscsi_portal_group
*tpg
,
258 struct iscsi_tpg_np
*tpg_np
)
260 struct iscsi_tiqn
*tiqn
= tpg
->tpg_tiqn
;
262 up(&tpg
->np_login_sem
);
265 kref_put(&tpg_np
->tpg_np_kref
, iscsit_login_kref_put
);
268 iscsit_put_tiqn_for_login(tiqn
);
273 bool iscsit_check_np_match(
274 struct __kernel_sockaddr_storage
*sockaddr
,
276 int network_transport
)
278 struct sockaddr_in
*sock_in
, *sock_in_e
;
279 struct sockaddr_in6
*sock_in6
, *sock_in6_e
;
280 bool ip_match
= false;
283 if (sockaddr
->ss_family
== AF_INET6
) {
284 sock_in6
= (struct sockaddr_in6
*)sockaddr
;
285 sock_in6_e
= (struct sockaddr_in6
*)&np
->np_sockaddr
;
287 if (!memcmp(&sock_in6
->sin6_addr
.in6_u
,
288 &sock_in6_e
->sin6_addr
.in6_u
,
289 sizeof(struct in6_addr
)))
292 port
= ntohs(sock_in6
->sin6_port
);
294 sock_in
= (struct sockaddr_in
*)sockaddr
;
295 sock_in_e
= (struct sockaddr_in
*)&np
->np_sockaddr
;
297 if (sock_in
->sin_addr
.s_addr
== sock_in_e
->sin_addr
.s_addr
)
300 port
= ntohs(sock_in
->sin_port
);
303 if ((ip_match
== true) && (np
->np_port
== port
) &&
304 (np
->np_network_transport
== network_transport
))
311 * Called with mutex np_lock held
313 static struct iscsi_np
*iscsit_get_np(
314 struct __kernel_sockaddr_storage
*sockaddr
,
315 int network_transport
)
320 list_for_each_entry(np
, &g_np_list
, np_list
) {
321 spin_lock_bh(&np
->np_thread_lock
);
322 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
323 spin_unlock_bh(&np
->np_thread_lock
);
327 match
= iscsit_check_np_match(sockaddr
, np
, network_transport
);
330 * Increment the np_exports reference count now to
331 * prevent iscsit_del_np() below from being called
332 * while iscsi_tpg_add_network_portal() is called.
335 spin_unlock_bh(&np
->np_thread_lock
);
338 spin_unlock_bh(&np
->np_thread_lock
);
344 struct iscsi_np
*iscsit_add_np(
345 struct __kernel_sockaddr_storage
*sockaddr
,
347 int network_transport
)
349 struct sockaddr_in
*sock_in
;
350 struct sockaddr_in6
*sock_in6
;
354 mutex_lock(&np_lock
);
357 * Locate the existing struct iscsi_np if already active..
359 np
= iscsit_get_np(sockaddr
, network_transport
);
361 mutex_unlock(&np_lock
);
365 np
= kzalloc(sizeof(struct iscsi_np
), GFP_KERNEL
);
367 pr_err("Unable to allocate memory for struct iscsi_np\n");
368 mutex_unlock(&np_lock
);
369 return ERR_PTR(-ENOMEM
);
372 np
->np_flags
|= NPF_IP_NETWORK
;
373 if (sockaddr
->ss_family
== AF_INET6
) {
374 sock_in6
= (struct sockaddr_in6
*)sockaddr
;
375 snprintf(np
->np_ip
, IPV6_ADDRESS_SPACE
, "%s", ip_str
);
376 np
->np_port
= ntohs(sock_in6
->sin6_port
);
378 sock_in
= (struct sockaddr_in
*)sockaddr
;
379 sprintf(np
->np_ip
, "%s", ip_str
);
380 np
->np_port
= ntohs(sock_in
->sin_port
);
383 np
->np_network_transport
= network_transport
;
384 spin_lock_init(&np
->np_thread_lock
);
385 init_completion(&np
->np_restart_comp
);
386 INIT_LIST_HEAD(&np
->np_list
);
388 ret
= iscsi_target_setup_login_socket(np
, sockaddr
);
391 mutex_unlock(&np_lock
);
395 np
->np_thread
= kthread_run(iscsi_target_login_thread
, np
, "iscsi_np");
396 if (IS_ERR(np
->np_thread
)) {
397 pr_err("Unable to create kthread: iscsi_np\n");
398 ret
= PTR_ERR(np
->np_thread
);
400 mutex_unlock(&np_lock
);
404 * Increment the np_exports reference count now to prevent
405 * iscsit_del_np() below from being run while a new call to
406 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
407 * active. We don't need to hold np->np_thread_lock at this
408 * point because iscsi_np has not been added to g_np_list yet.
411 np
->np_thread_state
= ISCSI_NP_THREAD_ACTIVE
;
413 list_add_tail(&np
->np_list
, &g_np_list
);
414 mutex_unlock(&np_lock
);
416 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
417 np
->np_ip
, np
->np_port
, np
->np_transport
->name
);
422 int iscsit_reset_np_thread(
424 struct iscsi_tpg_np
*tpg_np
,
425 struct iscsi_portal_group
*tpg
,
428 spin_lock_bh(&np
->np_thread_lock
);
429 if (np
->np_thread_state
== ISCSI_NP_THREAD_INACTIVE
) {
430 spin_unlock_bh(&np
->np_thread_lock
);
433 np
->np_thread_state
= ISCSI_NP_THREAD_RESET
;
436 spin_unlock_bh(&np
->np_thread_lock
);
437 send_sig(SIGINT
, np
->np_thread
, 1);
438 wait_for_completion(&np
->np_restart_comp
);
439 spin_lock_bh(&np
->np_thread_lock
);
441 spin_unlock_bh(&np
->np_thread_lock
);
443 if (tpg_np
&& shutdown
) {
444 kref_put(&tpg_np
->tpg_np_kref
, iscsit_login_kref_put
);
446 wait_for_completion(&tpg_np
->tpg_np_comp
);
452 static void iscsit_free_np(struct iscsi_np
*np
)
455 sock_release(np
->np_socket
);
458 int iscsit_del_np(struct iscsi_np
*np
)
460 spin_lock_bh(&np
->np_thread_lock
);
462 if (np
->np_exports
) {
463 spin_unlock_bh(&np
->np_thread_lock
);
466 np
->np_thread_state
= ISCSI_NP_THREAD_SHUTDOWN
;
467 spin_unlock_bh(&np
->np_thread_lock
);
471 * We need to send the signal to wakeup Linux/Net
472 * which may be sleeping in sock_accept()..
474 send_sig(SIGINT
, np
->np_thread
, 1);
475 kthread_stop(np
->np_thread
);
476 np
->np_thread
= NULL
;
479 np
->np_transport
->iscsit_free_np(np
);
481 mutex_lock(&np_lock
);
482 list_del(&np
->np_list
);
483 mutex_unlock(&np_lock
);
485 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
486 np
->np_ip
, np
->np_port
, np
->np_transport
->name
);
488 iscsit_put_transport(np
->np_transport
);
493 static int iscsit_immediate_queue(struct iscsi_conn
*, struct iscsi_cmd
*, int);
494 static int iscsit_response_queue(struct iscsi_conn
*, struct iscsi_cmd
*, int);
496 static int iscsit_queue_rsp(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
)
498 iscsit_add_cmd_to_response_queue(cmd
, cmd
->conn
, cmd
->i_state
);
502 static struct iscsit_transport iscsi_target_transport
= {
504 .transport_type
= ISCSI_TCP
,
506 .iscsit_setup_np
= iscsit_setup_np
,
507 .iscsit_accept_np
= iscsit_accept_np
,
508 .iscsit_free_np
= iscsit_free_np
,
509 .iscsit_get_login_rx
= iscsit_get_login_rx
,
510 .iscsit_put_login_tx
= iscsit_put_login_tx
,
511 .iscsit_get_dataout
= iscsit_build_r2ts_for_cmd
,
512 .iscsit_immediate_queue
= iscsit_immediate_queue
,
513 .iscsit_response_queue
= iscsit_response_queue
,
514 .iscsit_queue_data_in
= iscsit_queue_rsp
,
515 .iscsit_queue_status
= iscsit_queue_rsp
,
518 static int __init
iscsi_target_init_module(void)
522 pr_debug("iSCSI-Target "ISCSIT_VERSION
"\n");
524 iscsit_global
= kzalloc(sizeof(struct iscsit_global
), GFP_KERNEL
);
525 if (!iscsit_global
) {
526 pr_err("Unable to allocate memory for iscsit_global\n");
529 mutex_init(&auth_id_lock
);
530 spin_lock_init(&sess_idr_lock
);
534 ret
= iscsi_target_register_configfs();
538 ret
= iscsi_thread_set_init();
542 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT
) !=
543 TARGET_THREAD_SET_COUNT
) {
544 pr_err("iscsi_allocate_thread_sets() returned"
545 " unexpected value!\n");
549 lio_qr_cache
= kmem_cache_create("lio_qr_cache",
550 sizeof(struct iscsi_queue_req
),
551 __alignof__(struct iscsi_queue_req
), 0, NULL
);
553 pr_err("nable to kmem_cache_create() for"
558 lio_dr_cache
= kmem_cache_create("lio_dr_cache",
559 sizeof(struct iscsi_datain_req
),
560 __alignof__(struct iscsi_datain_req
), 0, NULL
);
562 pr_err("Unable to kmem_cache_create() for"
567 lio_ooo_cache
= kmem_cache_create("lio_ooo_cache",
568 sizeof(struct iscsi_ooo_cmdsn
),
569 __alignof__(struct iscsi_ooo_cmdsn
), 0, NULL
);
570 if (!lio_ooo_cache
) {
571 pr_err("Unable to kmem_cache_create() for"
576 lio_r2t_cache
= kmem_cache_create("lio_r2t_cache",
577 sizeof(struct iscsi_r2t
), __alignof__(struct iscsi_r2t
),
579 if (!lio_r2t_cache
) {
580 pr_err("Unable to kmem_cache_create() for"
585 iscsit_register_transport(&iscsi_target_transport
);
587 if (iscsit_load_discovery_tpg() < 0)
592 kmem_cache_destroy(lio_r2t_cache
);
594 kmem_cache_destroy(lio_ooo_cache
);
596 kmem_cache_destroy(lio_dr_cache
);
598 kmem_cache_destroy(lio_qr_cache
);
600 iscsi_deallocate_thread_sets();
602 iscsi_thread_set_free();
604 iscsi_target_deregister_configfs();
606 kfree(iscsit_global
);
610 static void __exit
iscsi_target_cleanup_module(void)
612 iscsi_deallocate_thread_sets();
613 iscsi_thread_set_free();
614 iscsit_release_discovery_tpg();
615 iscsit_unregister_transport(&iscsi_target_transport
);
616 kmem_cache_destroy(lio_qr_cache
);
617 kmem_cache_destroy(lio_dr_cache
);
618 kmem_cache_destroy(lio_ooo_cache
);
619 kmem_cache_destroy(lio_r2t_cache
);
621 iscsi_target_deregister_configfs();
623 kfree(iscsit_global
);
626 static int iscsit_add_reject(
627 struct iscsi_conn
*conn
,
631 struct iscsi_cmd
*cmd
;
633 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
637 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
638 cmd
->reject_reason
= reason
;
640 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
642 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
643 iscsit_free_cmd(cmd
, false);
647 spin_lock_bh(&conn
->cmd_lock
);
648 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
649 spin_unlock_bh(&conn
->cmd_lock
);
651 cmd
->i_state
= ISTATE_SEND_REJECT
;
652 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
657 static int iscsit_add_reject_from_cmd(
658 struct iscsi_cmd
*cmd
,
663 struct iscsi_conn
*conn
;
666 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
672 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
673 cmd
->reject_reason
= reason
;
675 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
677 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
678 iscsit_free_cmd(cmd
, false);
683 spin_lock_bh(&conn
->cmd_lock
);
684 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
685 spin_unlock_bh(&conn
->cmd_lock
);
688 cmd
->i_state
= ISTATE_SEND_REJECT
;
689 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
691 * Perform the kref_put now if se_cmd has already been setup by
692 * scsit_setup_scsi_cmd()
694 if (cmd
->se_cmd
.se_tfo
!= NULL
) {
695 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
696 target_put_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
);
701 static int iscsit_add_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
,
704 return iscsit_add_reject_from_cmd(cmd
, reason
, true, buf
);
707 int iscsit_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
, unsigned char *buf
)
709 return iscsit_add_reject_from_cmd(cmd
, reason
, false, buf
);
713 * Map some portion of the allocated scatterlist to an iovec, suitable for
714 * kernel sockets to copy data in/out.
716 static int iscsit_map_iovec(
717 struct iscsi_cmd
*cmd
,
723 struct scatterlist
*sg
;
724 unsigned int page_off
;
727 * We know each entry in t_data_sg contains a page.
729 sg
= &cmd
->se_cmd
.t_data_sg
[data_offset
/ PAGE_SIZE
];
730 page_off
= (data_offset
% PAGE_SIZE
);
732 cmd
->first_data_sg
= sg
;
733 cmd
->first_data_sg_off
= page_off
;
735 while (data_length
) {
736 u32 cur_len
= min_t(u32
, data_length
, sg
->length
- page_off
);
738 iov
[i
].iov_base
= kmap(sg_page(sg
)) + sg
->offset
+ page_off
;
739 iov
[i
].iov_len
= cur_len
;
741 data_length
-= cur_len
;
747 cmd
->kmapped_nents
= i
;
752 static void iscsit_unmap_iovec(struct iscsi_cmd
*cmd
)
755 struct scatterlist
*sg
;
757 sg
= cmd
->first_data_sg
;
759 for (i
= 0; i
< cmd
->kmapped_nents
; i
++)
760 kunmap(sg_page(&sg
[i
]));
763 static void iscsit_ack_from_expstatsn(struct iscsi_conn
*conn
, u32 exp_statsn
)
766 struct iscsi_cmd
*cmd
, *cmd_p
;
768 conn
->exp_statsn
= exp_statsn
;
770 if (conn
->sess
->sess_ops
->RDMAExtensions
)
773 spin_lock_bh(&conn
->cmd_lock
);
774 list_for_each_entry_safe(cmd
, cmd_p
, &conn
->conn_cmd_list
, i_conn_node
) {
775 spin_lock(&cmd
->istate_lock
);
776 if ((cmd
->i_state
== ISTATE_SENT_STATUS
) &&
777 iscsi_sna_lt(cmd
->stat_sn
, exp_statsn
)) {
778 cmd
->i_state
= ISTATE_REMOVE
;
779 spin_unlock(&cmd
->istate_lock
);
780 list_move_tail(&cmd
->i_conn_node
, &ack_list
);
783 spin_unlock(&cmd
->istate_lock
);
785 spin_unlock_bh(&conn
->cmd_lock
);
787 list_for_each_entry_safe(cmd
, cmd_p
, &ack_list
, i_conn_node
) {
788 list_del(&cmd
->i_conn_node
);
789 iscsit_free_cmd(cmd
, false);
793 static int iscsit_allocate_iovecs(struct iscsi_cmd
*cmd
)
795 u32 iov_count
= max(1UL, DIV_ROUND_UP(cmd
->se_cmd
.data_length
, PAGE_SIZE
));
797 iov_count
+= ISCSI_IOV_DATA_BUFFER
;
799 cmd
->iov_data
= kzalloc(iov_count
* sizeof(struct kvec
), GFP_KERNEL
);
800 if (!cmd
->iov_data
) {
801 pr_err("Unable to allocate cmd->iov_data\n");
805 cmd
->orig_iov_data_count
= iov_count
;
809 int iscsit_setup_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
812 int data_direction
, payload_length
;
813 struct iscsi_scsi_req
*hdr
;
817 atomic_long_inc(&conn
->sess
->cmd_pdus
);
819 hdr
= (struct iscsi_scsi_req
*) buf
;
820 payload_length
= ntoh24(hdr
->dlength
);
822 /* FIXME; Add checks for AdditionalHeaderSegment */
824 if (!(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) &&
825 !(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
826 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
827 " not set. Bad iSCSI Initiator.\n");
828 return iscsit_add_reject_cmd(cmd
,
829 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
832 if (((hdr
->flags
& ISCSI_FLAG_CMD_READ
) ||
833 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) && !hdr
->data_length
) {
835 * From RFC-3720 Section 10.3.1:
837 * "Either or both of R and W MAY be 1 when either the
838 * Expected Data Transfer Length and/or Bidirectional Read
839 * Expected Data Transfer Length are 0"
841 * For this case, go ahead and clear the unnecssary bits
842 * to avoid any confusion with ->data_direction.
844 hdr
->flags
&= ~ISCSI_FLAG_CMD_READ
;
845 hdr
->flags
&= ~ISCSI_FLAG_CMD_WRITE
;
847 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
848 " set when Expected Data Transfer Length is 0 for"
849 " CDB: 0x%02x, Fixing up flags\n", hdr
->cdb
[0]);
852 if (!(hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
853 !(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) && (hdr
->data_length
!= 0)) {
854 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
855 " MUST be set if Expected Data Transfer Length is not 0."
856 " Bad iSCSI Initiator\n");
857 return iscsit_add_reject_cmd(cmd
,
858 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
861 if ((hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
862 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) {
863 pr_err("Bidirectional operations not supported!\n");
864 return iscsit_add_reject_cmd(cmd
,
865 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
868 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
869 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
870 " Scsi Command PDU.\n");
871 return iscsit_add_reject_cmd(cmd
,
872 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
875 if (payload_length
&& !conn
->sess
->sess_ops
->ImmediateData
) {
876 pr_err("ImmediateData=No but DataSegmentLength=%u,"
877 " protocol error.\n", payload_length
);
878 return iscsit_add_reject_cmd(cmd
,
879 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
882 if ((be32_to_cpu(hdr
->data_length
) == payload_length
) &&
883 (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
))) {
884 pr_err("Expected Data Transfer Length and Length of"
885 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
886 " bit is not set protocol error\n");
887 return iscsit_add_reject_cmd(cmd
,
888 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
891 if (payload_length
> be32_to_cpu(hdr
->data_length
)) {
892 pr_err("DataSegmentLength: %u is greater than"
893 " EDTL: %u, protocol error.\n", payload_length
,
895 return iscsit_add_reject_cmd(cmd
,
896 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
899 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
900 pr_err("DataSegmentLength: %u is greater than"
901 " MaxXmitDataSegmentLength: %u, protocol error.\n",
902 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
903 return iscsit_add_reject_cmd(cmd
,
904 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
907 if (payload_length
> conn
->sess
->sess_ops
->FirstBurstLength
) {
908 pr_err("DataSegmentLength: %u is greater than"
909 " FirstBurstLength: %u, protocol error.\n",
910 payload_length
, conn
->sess
->sess_ops
->FirstBurstLength
);
911 return iscsit_add_reject_cmd(cmd
,
912 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
915 data_direction
= (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) ? DMA_TO_DEVICE
:
916 (hdr
->flags
& ISCSI_FLAG_CMD_READ
) ? DMA_FROM_DEVICE
:
919 cmd
->data_direction
= data_direction
;
920 iscsi_task_attr
= hdr
->flags
& ISCSI_FLAG_CMD_ATTR_MASK
;
922 * Figure out the SAM Task Attribute for the incoming SCSI CDB
924 if ((iscsi_task_attr
== ISCSI_ATTR_UNTAGGED
) ||
925 (iscsi_task_attr
== ISCSI_ATTR_SIMPLE
))
926 sam_task_attr
= MSG_SIMPLE_TAG
;
927 else if (iscsi_task_attr
== ISCSI_ATTR_ORDERED
)
928 sam_task_attr
= MSG_ORDERED_TAG
;
929 else if (iscsi_task_attr
== ISCSI_ATTR_HEAD_OF_QUEUE
)
930 sam_task_attr
= MSG_HEAD_TAG
;
931 else if (iscsi_task_attr
== ISCSI_ATTR_ACA
)
932 sam_task_attr
= MSG_ACA_TAG
;
934 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
935 " MSG_SIMPLE_TAG\n", iscsi_task_attr
);
936 sam_task_attr
= MSG_SIMPLE_TAG
;
939 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_CMD
;
940 cmd
->i_state
= ISTATE_NEW_CMD
;
941 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
942 cmd
->immediate_data
= (payload_length
) ? 1 : 0;
943 cmd
->unsolicited_data
= ((!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) &&
944 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) ? 1 : 0);
945 if (cmd
->unsolicited_data
)
946 cmd
->cmd_flags
|= ICF_NON_IMMEDIATE_UNSOLICITED_DATA
;
948 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
949 if (hdr
->flags
& ISCSI_FLAG_CMD_READ
) {
950 spin_lock_bh(&conn
->sess
->ttt_lock
);
951 cmd
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
952 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF)
953 cmd
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
954 spin_unlock_bh(&conn
->sess
->ttt_lock
);
955 } else if (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)
956 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
957 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
958 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
959 cmd
->first_burst_len
= payload_length
;
961 if (!conn
->sess
->sess_ops
->RDMAExtensions
&&
962 cmd
->data_direction
== DMA_FROM_DEVICE
) {
963 struct iscsi_datain_req
*dr
;
965 dr
= iscsit_allocate_datain_req();
967 return iscsit_add_reject_cmd(cmd
,
968 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
970 iscsit_attach_datain_req(cmd
, dr
);
974 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
976 transport_init_se_cmd(&cmd
->se_cmd
, &lio_target_fabric_configfs
->tf_ops
,
977 conn
->sess
->se_sess
, be32_to_cpu(hdr
->data_length
),
978 cmd
->data_direction
, sam_task_attr
,
979 cmd
->sense_buffer
+ 2);
981 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
982 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr
->itt
,
983 hdr
->cmdsn
, be32_to_cpu(hdr
->data_length
), payload_length
,
986 target_get_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
, true);
988 cmd
->sense_reason
= transport_lookup_cmd_lun(&cmd
->se_cmd
,
989 scsilun_to_int(&hdr
->lun
));
990 if (cmd
->sense_reason
)
993 cmd
->sense_reason
= target_setup_cmd_from_cdb(&cmd
->se_cmd
, hdr
->cdb
);
994 if (cmd
->sense_reason
) {
995 if (cmd
->sense_reason
== TCM_OUT_OF_RESOURCES
) {
996 return iscsit_add_reject_cmd(cmd
,
997 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1003 if (iscsit_build_pdu_and_seq_lists(cmd
, payload_length
) < 0) {
1004 return iscsit_add_reject_cmd(cmd
,
1005 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1009 spin_lock_bh(&conn
->cmd_lock
);
1010 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1011 spin_unlock_bh(&conn
->cmd_lock
);
1013 * Check if we need to delay processing because of ALUA
1014 * Active/NonOptimized primary access state..
1016 core_alua_check_nonop_delay(&cmd
->se_cmd
);
1020 EXPORT_SYMBOL(iscsit_setup_scsi_cmd
);
1022 void iscsit_set_unsoliticed_dataout(struct iscsi_cmd
*cmd
)
1024 iscsit_set_dataout_sequence_values(cmd
);
1026 spin_lock_bh(&cmd
->dataout_timeout_lock
);
1027 iscsit_start_dataout_timer(cmd
, cmd
->conn
);
1028 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
1030 EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout
);
1032 int iscsit_process_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1033 struct iscsi_scsi_req
*hdr
)
1037 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1038 * the Immediate Bit is not set, and no Immediate
1041 * A PDU/CmdSN carrying Immediate Data can only
1042 * be processed after the DataCRC has passed.
1043 * If the DataCRC fails, the CmdSN MUST NOT
1044 * be acknowledged. (See below)
1046 if (!cmd
->immediate_data
) {
1047 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1048 (unsigned char *)hdr
, hdr
->cmdsn
);
1049 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1051 else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1052 target_put_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
);
1057 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1060 * If no Immediate Data is attached, it's OK to return now.
1062 if (!cmd
->immediate_data
) {
1063 if (!cmd
->sense_reason
&& cmd
->unsolicited_data
)
1064 iscsit_set_unsoliticed_dataout(cmd
);
1065 if (!cmd
->sense_reason
)
1068 target_put_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
);
1073 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1074 * execution. These exceptions are processed in CmdSN order using
1075 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1077 if (cmd
->sense_reason
) {
1078 if (cmd
->reject_reason
)
1084 * Call directly into transport_generic_new_cmd() to perform
1085 * the backend memory allocation.
1087 cmd
->sense_reason
= transport_generic_new_cmd(&cmd
->se_cmd
);
1088 if (cmd
->sense_reason
)
1093 EXPORT_SYMBOL(iscsit_process_scsi_cmd
);
1096 iscsit_get_immediate_data(struct iscsi_cmd
*cmd
, struct iscsi_scsi_req
*hdr
,
1099 struct iscsi_conn
*conn
= cmd
->conn
;
1100 int cmdsn_ret
= 0, immed_ret
= IMMEDIATE_DATA_NORMAL_OPERATION
;
1102 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1104 if (dump_payload
== true)
1105 goto after_immediate_data
;
1107 immed_ret
= iscsit_handle_immediate_data(cmd
, hdr
,
1108 cmd
->first_burst_len
);
1109 after_immediate_data
:
1110 if (immed_ret
== IMMEDIATE_DATA_NORMAL_OPERATION
) {
1112 * A PDU/CmdSN carrying Immediate Data passed
1113 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1114 * Immediate Bit is not set.
1116 cmdsn_ret
= iscsit_sequence_cmd(cmd
->conn
, cmd
,
1117 (unsigned char *)hdr
, hdr
->cmdsn
);
1118 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1121 if (cmd
->sense_reason
|| cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1124 rc
= iscsit_dump_data_payload(cmd
->conn
,
1125 cmd
->first_burst_len
, 1);
1126 target_put_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
);
1128 } else if (cmd
->unsolicited_data
)
1129 iscsit_set_unsoliticed_dataout(cmd
);
1131 } else if (immed_ret
== IMMEDIATE_DATA_ERL1_CRC_FAILURE
) {
1133 * Immediate Data failed DataCRC and ERL>=1,
1134 * silently drop this PDU and let the initiator
1135 * plug the CmdSN gap.
1137 * FIXME: Send Unsolicited NOPIN with reserved
1138 * TTT here to help the initiator figure out
1139 * the missing CmdSN, although they should be
1140 * intelligent enough to determine the missing
1141 * CmdSN and issue a retry to plug the sequence.
1143 cmd
->i_state
= ISTATE_REMOVE
;
1144 iscsit_add_cmd_to_immediate_queue(cmd
, cmd
->conn
, cmd
->i_state
);
1145 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1152 iscsit_handle_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1155 struct iscsi_scsi_req
*hdr
= (struct iscsi_scsi_req
*)buf
;
1157 bool dump_payload
= false;
1159 rc
= iscsit_setup_scsi_cmd(conn
, cmd
, buf
);
1163 * Allocation iovecs needed for struct socket operations for
1164 * traditional iSCSI block I/O.
1166 if (iscsit_allocate_iovecs(cmd
) < 0) {
1167 return iscsit_add_reject_cmd(cmd
,
1168 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1170 immed_data
= cmd
->immediate_data
;
1172 rc
= iscsit_process_scsi_cmd(conn
, cmd
, hdr
);
1176 dump_payload
= true;
1181 return iscsit_get_immediate_data(cmd
, hdr
, dump_payload
);
1184 static u32
iscsit_do_crypto_hash_sg(
1185 struct hash_desc
*hash
,
1186 struct iscsi_cmd
*cmd
,
1194 struct scatterlist
*sg
;
1195 unsigned int page_off
;
1197 crypto_hash_init(hash
);
1199 sg
= cmd
->first_data_sg
;
1200 page_off
= cmd
->first_data_sg_off
;
1203 while (data_length
) {
1204 u32 cur_len
= min_t(u32
, data_length
, (sg
[i
].length
- page_off
));
1206 crypto_hash_update(hash
, &sg
[i
], cur_len
);
1208 data_length
-= cur_len
;
1214 struct scatterlist pad_sg
;
1216 sg_init_one(&pad_sg
, pad_bytes
, padding
);
1217 crypto_hash_update(hash
, &pad_sg
, padding
);
1219 crypto_hash_final(hash
, (u8
*) &data_crc
);
1224 static void iscsit_do_crypto_hash_buf(
1225 struct hash_desc
*hash
,
1232 struct scatterlist sg
;
1234 crypto_hash_init(hash
);
1236 sg_init_one(&sg
, buf
, payload_length
);
1237 crypto_hash_update(hash
, &sg
, payload_length
);
1240 sg_init_one(&sg
, pad_bytes
, padding
);
1241 crypto_hash_update(hash
, &sg
, padding
);
1243 crypto_hash_final(hash
, data_crc
);
1247 iscsit_check_dataout_hdr(struct iscsi_conn
*conn
, unsigned char *buf
,
1248 struct iscsi_cmd
**out_cmd
)
1250 struct iscsi_data
*hdr
= (struct iscsi_data
*)buf
;
1251 struct iscsi_cmd
*cmd
= NULL
;
1252 struct se_cmd
*se_cmd
;
1253 u32 payload_length
= ntoh24(hdr
->dlength
);
1256 if (!payload_length
) {
1257 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1262 atomic_long_add(payload_length
, &conn
->sess
->rx_data_octets
);
1264 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1265 pr_err("DataSegmentLength: %u is greater than"
1266 " MaxXmitDataSegmentLength: %u\n", payload_length
,
1267 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1268 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1272 cmd
= iscsit_find_cmd_from_itt_or_dump(conn
, hdr
->itt
,
1277 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1278 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1279 hdr
->itt
, hdr
->ttt
, hdr
->datasn
, ntohl(hdr
->offset
),
1280 payload_length
, conn
->cid
);
1282 if (cmd
->cmd_flags
& ICF_GOT_LAST_DATAOUT
) {
1283 pr_err("Command ITT: 0x%08x received DataOUT after"
1284 " last DataOUT received, dumping payload\n",
1285 cmd
->init_task_tag
);
1286 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1289 if (cmd
->data_direction
!= DMA_TO_DEVICE
) {
1290 pr_err("Command ITT: 0x%08x received DataOUT for a"
1291 " NON-WRITE command.\n", cmd
->init_task_tag
);
1292 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1294 se_cmd
= &cmd
->se_cmd
;
1295 iscsit_mod_dataout_timer(cmd
);
1297 if ((be32_to_cpu(hdr
->offset
) + payload_length
) > cmd
->se_cmd
.data_length
) {
1298 pr_err("DataOut Offset: %u, Length %u greater than"
1299 " iSCSI Command EDTL %u, protocol error.\n",
1300 hdr
->offset
, payload_length
, cmd
->se_cmd
.data_length
);
1301 return iscsit_reject_cmd(cmd
, ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1304 if (cmd
->unsolicited_data
) {
1305 int dump_unsolicited_data
= 0;
1307 if (conn
->sess
->sess_ops
->InitialR2T
) {
1308 pr_err("Received unexpected unsolicited data"
1309 " while InitialR2T=Yes, protocol error.\n");
1310 transport_send_check_condition_and_sense(&cmd
->se_cmd
,
1311 TCM_UNEXPECTED_UNSOLICITED_DATA
, 0);
1315 * Special case for dealing with Unsolicited DataOUT
1316 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1320 /* Something's amiss if we're not in WRITE_PENDING state... */
1321 WARN_ON(se_cmd
->t_state
!= TRANSPORT_WRITE_PENDING
);
1322 if (!(se_cmd
->se_cmd_flags
& SCF_SUPPORTED_SAM_OPCODE
))
1323 dump_unsolicited_data
= 1;
1325 if (dump_unsolicited_data
) {
1327 * Check if a delayed TASK_ABORTED status needs to
1328 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1329 * received with the unsolicitied data out.
1331 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1332 iscsit_stop_dataout_timer(cmd
);
1334 transport_check_aborted_status(se_cmd
,
1335 (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
));
1336 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1340 * For the normal solicited data path:
1342 * Check for a delayed TASK_ABORTED status and dump any
1343 * incoming data out payload if one exists. Also, when the
1344 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1345 * data out sequence, we decrement outstanding_r2ts. Once
1346 * outstanding_r2ts reaches zero, go ahead and send the delayed
1347 * TASK_ABORTED status.
1349 if (se_cmd
->transport_state
& CMD_T_ABORTED
) {
1350 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1351 if (--cmd
->outstanding_r2ts
< 1) {
1352 iscsit_stop_dataout_timer(cmd
);
1353 transport_check_aborted_status(
1357 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1361 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1362 * within-command recovery checks before receiving the payload.
1364 rc
= iscsit_check_pre_dataout(cmd
, buf
);
1365 if (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
)
1367 else if (rc
== DATAOUT_CANNOT_RECOVER
)
1373 EXPORT_SYMBOL(iscsit_check_dataout_hdr
);
1376 iscsit_get_dataout(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1377 struct iscsi_data
*hdr
)
1380 u32 checksum
, iov_count
= 0, padding
= 0, rx_got
= 0, rx_size
= 0;
1381 u32 payload_length
= ntoh24(hdr
->dlength
);
1382 int iov_ret
, data_crc_failed
= 0;
1384 rx_size
+= payload_length
;
1385 iov
= &cmd
->iov_data
[0];
1387 iov_ret
= iscsit_map_iovec(cmd
, iov
, be32_to_cpu(hdr
->offset
),
1392 iov_count
+= iov_ret
;
1394 padding
= ((-payload_length
) & 3);
1396 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
1397 iov
[iov_count
++].iov_len
= padding
;
1399 pr_debug("Receiving %u padding bytes.\n", padding
);
1402 if (conn
->conn_ops
->DataDigest
) {
1403 iov
[iov_count
].iov_base
= &checksum
;
1404 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
1405 rx_size
+= ISCSI_CRC_LEN
;
1408 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
1410 iscsit_unmap_iovec(cmd
);
1412 if (rx_got
!= rx_size
)
1415 if (conn
->conn_ops
->DataDigest
) {
1418 data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_rx_hash
, cmd
,
1419 be32_to_cpu(hdr
->offset
),
1420 payload_length
, padding
,
1423 if (checksum
!= data_crc
) {
1424 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1425 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1426 " does not match computed 0x%08x\n",
1427 hdr
->itt
, hdr
->offset
, payload_length
,
1428 hdr
->datasn
, checksum
, data_crc
);
1429 data_crc_failed
= 1;
1431 pr_debug("Got CRC32C DataDigest 0x%08x for"
1432 " %u bytes of Data Out\n", checksum
,
1437 return data_crc_failed
;
1441 iscsit_check_dataout_payload(struct iscsi_cmd
*cmd
, struct iscsi_data
*hdr
,
1442 bool data_crc_failed
)
1444 struct iscsi_conn
*conn
= cmd
->conn
;
1447 * Increment post receive data and CRC values or perform
1448 * within-command recovery.
1450 rc
= iscsit_check_post_dataout(cmd
, (unsigned char *)hdr
, data_crc_failed
);
1451 if ((rc
== DATAOUT_NORMAL
) || (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
))
1453 else if (rc
== DATAOUT_SEND_R2T
) {
1454 iscsit_set_dataout_sequence_values(cmd
);
1455 conn
->conn_transport
->iscsit_get_dataout(conn
, cmd
, false);
1456 } else if (rc
== DATAOUT_SEND_TO_TRANSPORT
) {
1458 * Handle extra special case for out of order
1459 * Unsolicited Data Out.
1461 spin_lock_bh(&cmd
->istate_lock
);
1462 ooo_cmdsn
= (cmd
->cmd_flags
& ICF_OOO_CMDSN
);
1463 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
1464 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
1465 spin_unlock_bh(&cmd
->istate_lock
);
1467 iscsit_stop_dataout_timer(cmd
);
1470 target_execute_cmd(&cmd
->se_cmd
);
1472 } else /* DATAOUT_CANNOT_RECOVER */
1477 EXPORT_SYMBOL(iscsit_check_dataout_payload
);
1479 static int iscsit_handle_data_out(struct iscsi_conn
*conn
, unsigned char *buf
)
1481 struct iscsi_cmd
*cmd
= NULL
;
1482 struct iscsi_data
*hdr
= (struct iscsi_data
*)buf
;
1484 bool data_crc_failed
= false;
1486 rc
= iscsit_check_dataout_hdr(conn
, buf
, &cmd
);
1492 rc
= iscsit_get_dataout(conn
, cmd
, hdr
);
1496 data_crc_failed
= true;
1498 return iscsit_check_dataout_payload(cmd
, hdr
, data_crc_failed
);
1501 int iscsit_setup_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1502 struct iscsi_nopout
*hdr
)
1504 u32 payload_length
= ntoh24(hdr
->dlength
);
1506 if (hdr
->itt
== RESERVED_ITT
&& !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1507 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1508 " not set, protocol error.\n");
1510 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1511 (unsigned char *)hdr
);
1513 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1514 (unsigned char *)hdr
);
1517 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1518 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1519 " greater than MaxXmitDataSegmentLength: %u, protocol"
1520 " error.\n", payload_length
,
1521 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1523 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1524 (unsigned char *)hdr
);
1526 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1527 (unsigned char *)hdr
);
1530 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1531 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1532 hdr
->itt
== RESERVED_ITT
? "Response" : "Request",
1533 hdr
->itt
, hdr
->ttt
, hdr
->cmdsn
, hdr
->exp_statsn
,
1536 * This is not a response to a Unsolicited NopIN, which means
1537 * it can either be a NOPOUT ping request (with a valid ITT),
1538 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1539 * Either way, make sure we allocate an struct iscsi_cmd, as both
1540 * can contain ping data.
1542 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1543 cmd
->iscsi_opcode
= ISCSI_OP_NOOP_OUT
;
1544 cmd
->i_state
= ISTATE_SEND_NOPIN
;
1545 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ?
1547 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1548 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1549 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1550 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1551 cmd
->data_direction
= DMA_NONE
;
1556 EXPORT_SYMBOL(iscsit_setup_nop_out
);
1558 int iscsit_process_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1559 struct iscsi_nopout
*hdr
)
1561 struct iscsi_cmd
*cmd_p
= NULL
;
1564 * Initiator is expecting a NopIN ping reply..
1566 if (hdr
->itt
!= RESERVED_ITT
) {
1569 spin_lock_bh(&conn
->cmd_lock
);
1570 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1571 spin_unlock_bh(&conn
->cmd_lock
);
1573 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1575 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
1576 iscsit_add_cmd_to_response_queue(cmd
, conn
,
1581 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1582 (unsigned char *)hdr
, hdr
->cmdsn
);
1583 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
1585 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1591 * This was a response to a unsolicited NOPIN ping.
1593 if (hdr
->ttt
!= cpu_to_be32(0xFFFFFFFF)) {
1594 cmd_p
= iscsit_find_cmd_from_ttt(conn
, be32_to_cpu(hdr
->ttt
));
1598 iscsit_stop_nopin_response_timer(conn
);
1600 cmd_p
->i_state
= ISTATE_REMOVE
;
1601 iscsit_add_cmd_to_immediate_queue(cmd_p
, conn
, cmd_p
->i_state
);
1603 iscsit_start_nopin_timer(conn
);
1607 * Otherwise, initiator is not expecting a NOPIN is response.
1608 * Just ignore for now.
1612 EXPORT_SYMBOL(iscsit_process_nop_out
);
1614 static int iscsit_handle_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1617 unsigned char *ping_data
= NULL
;
1618 struct iscsi_nopout
*hdr
= (struct iscsi_nopout
*)buf
;
1619 struct kvec
*iov
= NULL
;
1620 u32 payload_length
= ntoh24(hdr
->dlength
);
1623 ret
= iscsit_setup_nop_out(conn
, cmd
, hdr
);
1627 * Handle NOP-OUT payload for traditional iSCSI sockets
1629 if (payload_length
&& hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1630 u32 checksum
, data_crc
, padding
= 0;
1631 int niov
= 0, rx_got
, rx_size
= payload_length
;
1633 ping_data
= kzalloc(payload_length
+ 1, GFP_KERNEL
);
1635 pr_err("Unable to allocate memory for"
1636 " NOPOUT ping data.\n");
1641 iov
= &cmd
->iov_misc
[0];
1642 iov
[niov
].iov_base
= ping_data
;
1643 iov
[niov
++].iov_len
= payload_length
;
1645 padding
= ((-payload_length
) & 3);
1647 pr_debug("Receiving %u additional bytes"
1648 " for padding.\n", padding
);
1649 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
1650 iov
[niov
++].iov_len
= padding
;
1653 if (conn
->conn_ops
->DataDigest
) {
1654 iov
[niov
].iov_base
= &checksum
;
1655 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
1656 rx_size
+= ISCSI_CRC_LEN
;
1659 rx_got
= rx_data(conn
, &cmd
->iov_misc
[0], niov
, rx_size
);
1660 if (rx_got
!= rx_size
) {
1665 if (conn
->conn_ops
->DataDigest
) {
1666 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
1667 ping_data
, payload_length
,
1668 padding
, cmd
->pad_bytes
,
1671 if (checksum
!= data_crc
) {
1672 pr_err("Ping data CRC32C DataDigest"
1673 " 0x%08x does not match computed 0x%08x\n",
1674 checksum
, data_crc
);
1675 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
1676 pr_err("Unable to recover from"
1677 " NOPOUT Ping DataCRC failure while in"
1683 * Silently drop this PDU and let the
1684 * initiator plug the CmdSN gap.
1686 pr_debug("Dropping NOPOUT"
1687 " Command CmdSN: 0x%08x due to"
1688 " DataCRC error.\n", hdr
->cmdsn
);
1693 pr_debug("Got CRC32C DataDigest"
1694 " 0x%08x for %u bytes of ping data.\n",
1695 checksum
, payload_length
);
1699 ping_data
[payload_length
] = '\0';
1701 * Attach ping data to struct iscsi_cmd->buf_ptr.
1703 cmd
->buf_ptr
= ping_data
;
1704 cmd
->buf_ptr_size
= payload_length
;
1706 pr_debug("Got %u bytes of NOPOUT ping"
1707 " data.\n", payload_length
);
1708 pr_debug("Ping Data: \"%s\"\n", ping_data
);
1711 return iscsit_process_nop_out(conn
, cmd
, hdr
);
1714 iscsit_free_cmd(cmd
, false);
1721 iscsit_handle_task_mgt_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1724 struct se_tmr_req
*se_tmr
;
1725 struct iscsi_tmr_req
*tmr_req
;
1726 struct iscsi_tm
*hdr
;
1727 int out_of_order_cmdsn
= 0, ret
;
1728 bool sess_ref
= false;
1731 hdr
= (struct iscsi_tm
*) buf
;
1732 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
1733 function
= hdr
->flags
;
1735 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1736 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1737 " 0x%08x, CID: %hu\n", hdr
->itt
, hdr
->cmdsn
, function
,
1738 hdr
->rtt
, hdr
->refcmdsn
, conn
->cid
);
1740 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1741 ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1742 hdr
->rtt
!= RESERVED_ITT
)) {
1743 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1744 hdr
->rtt
= RESERVED_ITT
;
1747 if ((function
== ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1748 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1749 pr_err("Task Management Request TASK_REASSIGN not"
1750 " issued as immediate command, bad iSCSI Initiator"
1751 "implementation\n");
1752 return iscsit_add_reject_cmd(cmd
,
1753 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1755 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1756 be32_to_cpu(hdr
->refcmdsn
) != ISCSI_RESERVED_TAG
)
1757 hdr
->refcmdsn
= cpu_to_be32(ISCSI_RESERVED_TAG
);
1759 cmd
->data_direction
= DMA_NONE
;
1761 cmd
->tmr_req
= kzalloc(sizeof(struct iscsi_tmr_req
), GFP_KERNEL
);
1762 if (!cmd
->tmr_req
) {
1763 pr_err("Unable to allocate memory for"
1764 " Task Management command!\n");
1765 return iscsit_add_reject_cmd(cmd
,
1766 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1771 * TASK_REASSIGN for ERL=2 / connection stays inside of
1772 * LIO-Target $FABRIC_MOD
1774 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
1779 transport_init_se_cmd(&cmd
->se_cmd
,
1780 &lio_target_fabric_configfs
->tf_ops
,
1781 conn
->sess
->se_sess
, 0, DMA_NONE
,
1782 MSG_SIMPLE_TAG
, cmd
->sense_buffer
+ 2);
1784 target_get_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
, true);
1788 case ISCSI_TM_FUNC_ABORT_TASK
:
1789 tcm_function
= TMR_ABORT_TASK
;
1791 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
1792 tcm_function
= TMR_ABORT_TASK_SET
;
1794 case ISCSI_TM_FUNC_CLEAR_ACA
:
1795 tcm_function
= TMR_CLEAR_ACA
;
1797 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
1798 tcm_function
= TMR_CLEAR_TASK_SET
;
1800 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
1801 tcm_function
= TMR_LUN_RESET
;
1803 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
1804 tcm_function
= TMR_TARGET_WARM_RESET
;
1806 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
1807 tcm_function
= TMR_TARGET_COLD_RESET
;
1810 pr_err("Unknown iSCSI TMR Function:"
1811 " 0x%02x\n", function
);
1812 return iscsit_add_reject_cmd(cmd
,
1813 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1816 ret
= core_tmr_alloc_req(&cmd
->se_cmd
, cmd
->tmr_req
,
1817 tcm_function
, GFP_KERNEL
);
1819 return iscsit_add_reject_cmd(cmd
,
1820 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1822 cmd
->tmr_req
->se_tmr_req
= cmd
->se_cmd
.se_tmr_req
;
1825 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_TMFUNC
;
1826 cmd
->i_state
= ISTATE_SEND_TASKMGTRSP
;
1827 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1828 cmd
->init_task_tag
= hdr
->itt
;
1829 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1830 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1831 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1832 se_tmr
= cmd
->se_cmd
.se_tmr_req
;
1833 tmr_req
= cmd
->tmr_req
;
1835 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1837 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
1838 ret
= transport_lookup_tmr_lun(&cmd
->se_cmd
,
1839 scsilun_to_int(&hdr
->lun
));
1841 se_tmr
->response
= ISCSI_TMF_RSP_NO_LUN
;
1847 case ISCSI_TM_FUNC_ABORT_TASK
:
1848 se_tmr
->response
= iscsit_tmr_abort_task(cmd
, buf
);
1849 if (se_tmr
->response
)
1852 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
1853 case ISCSI_TM_FUNC_CLEAR_ACA
:
1854 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
1855 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
1857 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
1858 if (iscsit_tmr_task_warm_reset(conn
, tmr_req
, buf
) < 0) {
1859 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
1863 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
1864 if (iscsit_tmr_task_cold_reset(conn
, tmr_req
, buf
) < 0) {
1865 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
1869 case ISCSI_TM_FUNC_TASK_REASSIGN
:
1870 se_tmr
->response
= iscsit_tmr_task_reassign(cmd
, buf
);
1872 * Perform sanity checks on the ExpDataSN only if the
1873 * TASK_REASSIGN was successful.
1875 if (se_tmr
->response
)
1878 if (iscsit_check_task_reassign_expdatasn(tmr_req
, conn
) < 0)
1879 return iscsit_add_reject_cmd(cmd
,
1880 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1883 pr_err("Unknown TMR function: 0x%02x, protocol"
1884 " error.\n", function
);
1885 se_tmr
->response
= ISCSI_TMF_RSP_NOT_SUPPORTED
;
1889 if ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1890 (se_tmr
->response
== ISCSI_TMF_RSP_COMPLETE
))
1891 se_tmr
->call_transport
= 1;
1893 spin_lock_bh(&conn
->cmd_lock
);
1894 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1895 spin_unlock_bh(&conn
->cmd_lock
);
1897 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1898 int cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
1899 if (cmdsn_ret
== CMDSN_HIGHER_THAN_EXP
)
1900 out_of_order_cmdsn
= 1;
1901 else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
1903 else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1906 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1908 if (out_of_order_cmdsn
|| !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
))
1911 * Found the referenced task, send to transport for processing.
1913 if (se_tmr
->call_transport
)
1914 return transport_generic_handle_tmr(&cmd
->se_cmd
);
1917 * Could not find the referenced LUN, task, or Task Management
1918 * command not authorized or supported. Change state and
1919 * let the tx_thread send the response.
1921 * For connection recovery, this is also the default action for
1922 * TMR TASK_REASSIGN.
1925 pr_debug("Handle TMR, using sess_ref=true check\n");
1926 target_put_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
);
1929 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
1932 EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd
);
1934 /* #warning FIXME: Support Text Command parameters besides SendTargets */
1936 iscsit_setup_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1937 struct iscsi_text
*hdr
)
1939 u32 payload_length
= ntoh24(hdr
->dlength
);
1941 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1942 pr_err("Unable to accept text parameter length: %u"
1943 "greater than MaxXmitDataSegmentLength %u.\n",
1944 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
1945 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1946 (unsigned char *)hdr
);
1949 if (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) ||
1950 (hdr
->flags
& ISCSI_FLAG_TEXT_CONTINUE
)) {
1951 pr_err("Multi sequence text commands currently not supported\n");
1952 return iscsit_reject_cmd(cmd
, ISCSI_REASON_CMD_NOT_SUPPORTED
,
1953 (unsigned char *)hdr
);
1956 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1957 " ExpStatSN: 0x%08x, Length: %u\n", hdr
->itt
, hdr
->cmdsn
,
1958 hdr
->exp_statsn
, payload_length
);
1960 cmd
->iscsi_opcode
= ISCSI_OP_TEXT
;
1961 cmd
->i_state
= ISTATE_SEND_TEXTRSP
;
1962 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1963 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1964 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1965 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1966 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1967 cmd
->data_direction
= DMA_NONE
;
1971 EXPORT_SYMBOL(iscsit_setup_text_cmd
);
1974 iscsit_process_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1975 struct iscsi_text
*hdr
)
1977 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
;
1981 pr_err("Unable to locate text_in buffer for sendtargets"
1985 if (strncmp("SendTargets", text_in
, 11) != 0) {
1986 pr_err("Received Text Data that is not"
1987 " SendTargets, cannot continue.\n");
1990 text_ptr
= strchr(text_in
, '=');
1992 pr_err("No \"=\" separator found in Text Data,"
1993 " cannot continue.\n");
1996 if (!strncmp("=All", text_ptr
, 4)) {
1997 cmd
->cmd_flags
|= IFC_SENDTARGETS_ALL
;
1998 } else if (!strncmp("=iqn.", text_ptr
, 5) ||
1999 !strncmp("=eui.", text_ptr
, 5)) {
2000 cmd
->cmd_flags
|= IFC_SENDTARGETS_SINGLE
;
2002 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr
);
2006 spin_lock_bh(&conn
->cmd_lock
);
2007 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2008 spin_unlock_bh(&conn
->cmd_lock
);
2010 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2012 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
2013 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
2014 (unsigned char *)hdr
, hdr
->cmdsn
);
2015 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2021 return iscsit_execute_cmd(cmd
, 0);
2024 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
2025 (unsigned char *)hdr
);
2027 EXPORT_SYMBOL(iscsit_process_text_cmd
);
2030 iscsit_handle_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2033 struct iscsi_text
*hdr
= (struct iscsi_text
*)buf
;
2034 char *text_in
= NULL
;
2035 u32 payload_length
= ntoh24(hdr
->dlength
);
2038 rc
= iscsit_setup_text_cmd(conn
, cmd
, hdr
);
2042 rx_size
= payload_length
;
2043 if (payload_length
) {
2044 u32 checksum
= 0, data_crc
= 0;
2045 u32 padding
= 0, pad_bytes
= 0;
2046 int niov
= 0, rx_got
;
2049 text_in
= kzalloc(payload_length
, GFP_KERNEL
);
2051 pr_err("Unable to allocate memory for"
2052 " incoming text parameters\n");
2055 cmd
->text_in_ptr
= text_in
;
2057 memset(iov
, 0, 3 * sizeof(struct kvec
));
2058 iov
[niov
].iov_base
= text_in
;
2059 iov
[niov
++].iov_len
= payload_length
;
2061 padding
= ((-payload_length
) & 3);
2063 iov
[niov
].iov_base
= &pad_bytes
;
2064 iov
[niov
++].iov_len
= padding
;
2066 pr_debug("Receiving %u additional bytes"
2067 " for padding.\n", padding
);
2069 if (conn
->conn_ops
->DataDigest
) {
2070 iov
[niov
].iov_base
= &checksum
;
2071 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
2072 rx_size
+= ISCSI_CRC_LEN
;
2075 rx_got
= rx_data(conn
, &iov
[0], niov
, rx_size
);
2076 if (rx_got
!= rx_size
)
2079 if (conn
->conn_ops
->DataDigest
) {
2080 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
2081 text_in
, payload_length
,
2082 padding
, (u8
*)&pad_bytes
,
2085 if (checksum
!= data_crc
) {
2086 pr_err("Text data CRC32C DataDigest"
2087 " 0x%08x does not match computed"
2088 " 0x%08x\n", checksum
, data_crc
);
2089 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2090 pr_err("Unable to recover from"
2091 " Text Data digest failure while in"
2096 * Silently drop this PDU and let the
2097 * initiator plug the CmdSN gap.
2099 pr_debug("Dropping Text"
2100 " Command CmdSN: 0x%08x due to"
2101 " DataCRC error.\n", hdr
->cmdsn
);
2106 pr_debug("Got CRC32C DataDigest"
2107 " 0x%08x for %u bytes of text data.\n",
2108 checksum
, payload_length
);
2111 text_in
[payload_length
- 1] = '\0';
2112 pr_debug("Successfully read %d bytes of text"
2113 " data.\n", payload_length
);
2116 return iscsit_process_text_cmd(conn
, cmd
, hdr
);
2119 kfree(cmd
->text_in_ptr
);
2120 cmd
->text_in_ptr
= NULL
;
2121 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
, buf
);
2123 EXPORT_SYMBOL(iscsit_handle_text_cmd
);
2125 int iscsit_logout_closesession(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2127 struct iscsi_conn
*conn_p
;
2128 struct iscsi_session
*sess
= conn
->sess
;
2130 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2131 " for SID: %u.\n", conn
->cid
, conn
->sess
->sid
);
2133 atomic_set(&sess
->session_logout
, 1);
2134 atomic_set(&conn
->conn_logout_remove
, 1);
2135 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_SESSION
;
2137 iscsit_inc_conn_usage_count(conn
);
2138 iscsit_inc_session_usage_count(sess
);
2140 spin_lock_bh(&sess
->conn_lock
);
2141 list_for_each_entry(conn_p
, &sess
->sess_conn_list
, conn_list
) {
2142 if (conn_p
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
)
2145 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2146 conn_p
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2148 spin_unlock_bh(&sess
->conn_lock
);
2150 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2155 int iscsit_logout_closeconnection(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2157 struct iscsi_conn
*l_conn
;
2158 struct iscsi_session
*sess
= conn
->sess
;
2160 pr_debug("Received logout request CLOSECONNECTION for CID:"
2161 " %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2164 * A Logout Request with a CLOSECONNECTION reason code for a CID
2165 * can arrive on a connection with a differing CID.
2167 if (conn
->cid
== cmd
->logout_cid
) {
2168 spin_lock_bh(&conn
->state_lock
);
2169 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2170 conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2172 atomic_set(&conn
->conn_logout_remove
, 1);
2173 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
;
2174 iscsit_inc_conn_usage_count(conn
);
2176 spin_unlock_bh(&conn
->state_lock
);
2179 * Handle all different cid CLOSECONNECTION requests in
2180 * iscsit_logout_post_handler_diffcid() as to give enough
2181 * time for any non immediate command's CmdSN to be
2182 * acknowledged on the connection in question.
2184 * Here we simply make sure the CID is still around.
2186 l_conn
= iscsit_get_conn_from_cid(sess
,
2189 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2190 iscsit_add_cmd_to_response_queue(cmd
, conn
,
2195 iscsit_dec_conn_usage_count(l_conn
);
2198 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2203 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2205 struct iscsi_session
*sess
= conn
->sess
;
2207 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2208 " CID: %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2210 if (sess
->sess_ops
->ErrorRecoveryLevel
!= 2) {
2211 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2212 " while ERL!=2.\n");
2213 cmd
->logout_response
= ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
;
2214 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2218 if (conn
->cid
== cmd
->logout_cid
) {
2219 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2220 " with CID: %hu on CID: %hu, implementation error.\n",
2221 cmd
->logout_cid
, conn
->cid
);
2222 cmd
->logout_response
= ISCSI_LOGOUT_CLEANUP_FAILED
;
2223 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2227 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2233 iscsit_handle_logout_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2236 int cmdsn_ret
, logout_remove
= 0;
2238 struct iscsi_logout
*hdr
;
2239 struct iscsi_tiqn
*tiqn
= iscsit_snmp_get_tiqn(conn
);
2241 hdr
= (struct iscsi_logout
*) buf
;
2242 reason_code
= (hdr
->flags
& 0x7f);
2245 spin_lock(&tiqn
->logout_stats
.lock
);
2246 if (reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
)
2247 tiqn
->logout_stats
.normal_logouts
++;
2249 tiqn
->logout_stats
.abnormal_logouts
++;
2250 spin_unlock(&tiqn
->logout_stats
.lock
);
2253 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2254 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2255 hdr
->itt
, hdr
->cmdsn
, hdr
->exp_statsn
, reason_code
,
2256 hdr
->cid
, conn
->cid
);
2258 if (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
) {
2259 pr_err("Received logout request on connection that"
2260 " is not in logged in state, ignoring request.\n");
2261 iscsit_free_cmd(cmd
, false);
2265 cmd
->iscsi_opcode
= ISCSI_OP_LOGOUT
;
2266 cmd
->i_state
= ISTATE_SEND_LOGOUTRSP
;
2267 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2268 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
2269 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2270 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
2271 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
2272 cmd
->logout_cid
= be16_to_cpu(hdr
->cid
);
2273 cmd
->logout_reason
= reason_code
;
2274 cmd
->data_direction
= DMA_NONE
;
2277 * We need to sleep in these cases (by returning 1) until the Logout
2278 * Response gets sent in the tx thread.
2280 if ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) ||
2281 ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
) &&
2282 be16_to_cpu(hdr
->cid
) == conn
->cid
))
2285 spin_lock_bh(&conn
->cmd_lock
);
2286 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2287 spin_unlock_bh(&conn
->cmd_lock
);
2289 if (reason_code
!= ISCSI_LOGOUT_REASON_RECOVERY
)
2290 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2293 * Immediate commands are executed, well, immediately.
2294 * Non-Immediate Logout Commands are executed in CmdSN order.
2296 if (cmd
->immediate_cmd
) {
2297 int ret
= iscsit_execute_cmd(cmd
, 0);
2302 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
2303 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
2305 else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2309 return logout_remove
;
2311 EXPORT_SYMBOL(iscsit_handle_logout_cmd
);
2313 static int iscsit_handle_snack(
2314 struct iscsi_conn
*conn
,
2317 struct iscsi_snack
*hdr
;
2319 hdr
= (struct iscsi_snack
*) buf
;
2320 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
2322 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2323 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2324 " CID: %hu\n", hdr
->itt
, hdr
->exp_statsn
, hdr
->flags
,
2325 hdr
->begrun
, hdr
->runlength
, conn
->cid
);
2327 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2328 pr_err("Initiator sent SNACK request while in"
2329 " ErrorRecoveryLevel=0.\n");
2330 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2334 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2335 * call from inside iscsi_send_recovery_datain_or_r2t().
2337 switch (hdr
->flags
& ISCSI_FLAG_SNACK_TYPE_MASK
) {
2339 return iscsit_handle_recovery_datain_or_r2t(conn
, buf
,
2341 be32_to_cpu(hdr
->ttt
),
2342 be32_to_cpu(hdr
->begrun
),
2343 be32_to_cpu(hdr
->runlength
));
2344 case ISCSI_FLAG_SNACK_TYPE_STATUS
:
2345 return iscsit_handle_status_snack(conn
, hdr
->itt
,
2346 be32_to_cpu(hdr
->ttt
),
2347 be32_to_cpu(hdr
->begrun
), be32_to_cpu(hdr
->runlength
));
2348 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK
:
2349 return iscsit_handle_data_ack(conn
, be32_to_cpu(hdr
->ttt
),
2350 be32_to_cpu(hdr
->begrun
),
2351 be32_to_cpu(hdr
->runlength
));
2352 case ISCSI_FLAG_SNACK_TYPE_RDATA
:
2353 /* FIXME: Support R-Data SNACK */
2354 pr_err("R-Data SNACK Not Supported.\n");
2355 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2358 pr_err("Unknown SNACK type 0x%02x, protocol"
2359 " error.\n", hdr
->flags
& 0x0f);
2360 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2367 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2369 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2370 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2371 wait_for_completion_interruptible_timeout(
2372 &conn
->rx_half_close_comp
,
2373 ISCSI_RX_THREAD_TCP_TIMEOUT
* HZ
);
2377 static int iscsit_handle_immediate_data(
2378 struct iscsi_cmd
*cmd
,
2379 struct iscsi_scsi_req
*hdr
,
2382 int iov_ret
, rx_got
= 0, rx_size
= 0;
2383 u32 checksum
, iov_count
= 0, padding
= 0;
2384 struct iscsi_conn
*conn
= cmd
->conn
;
2387 iov_ret
= iscsit_map_iovec(cmd
, cmd
->iov_data
, cmd
->write_data_done
, length
);
2389 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2392 iov_count
= iov_ret
;
2393 iov
= &cmd
->iov_data
[0];
2395 padding
= ((-length
) & 3);
2397 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2398 iov
[iov_count
++].iov_len
= padding
;
2402 if (conn
->conn_ops
->DataDigest
) {
2403 iov
[iov_count
].iov_base
= &checksum
;
2404 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2405 rx_size
+= ISCSI_CRC_LEN
;
2408 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
2410 iscsit_unmap_iovec(cmd
);
2412 if (rx_got
!= rx_size
) {
2413 iscsit_rx_thread_wait_for_tcp(conn
);
2414 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2417 if (conn
->conn_ops
->DataDigest
) {
2420 data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_rx_hash
, cmd
,
2421 cmd
->write_data_done
, length
, padding
,
2424 if (checksum
!= data_crc
) {
2425 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2426 " does not match computed 0x%08x\n", checksum
,
2429 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2430 pr_err("Unable to recover from"
2431 " Immediate Data digest failure while"
2433 iscsit_reject_cmd(cmd
,
2434 ISCSI_REASON_DATA_DIGEST_ERROR
,
2435 (unsigned char *)hdr
);
2436 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2438 iscsit_reject_cmd(cmd
,
2439 ISCSI_REASON_DATA_DIGEST_ERROR
,
2440 (unsigned char *)hdr
);
2441 return IMMEDIATE_DATA_ERL1_CRC_FAILURE
;
2444 pr_debug("Got CRC32C DataDigest 0x%08x for"
2445 " %u bytes of Immediate Data\n", checksum
,
2450 cmd
->write_data_done
+= length
;
2452 if (cmd
->write_data_done
== cmd
->se_cmd
.data_length
) {
2453 spin_lock_bh(&cmd
->istate_lock
);
2454 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
2455 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
2456 spin_unlock_bh(&cmd
->istate_lock
);
2459 return IMMEDIATE_DATA_NORMAL_OPERATION
;
2463 * Called with sess->conn_lock held.
2465 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2466 with active network interface */
2467 static void iscsit_build_conn_drop_async_message(struct iscsi_conn
*conn
)
2469 struct iscsi_cmd
*cmd
;
2470 struct iscsi_conn
*conn_p
;
2473 * Only send a Asynchronous Message on connections whos network
2474 * interface is still functional.
2476 list_for_each_entry(conn_p
, &conn
->sess
->sess_conn_list
, conn_list
) {
2477 if (conn_p
->conn_state
== TARG_CONN_STATE_LOGGED_IN
) {
2478 iscsit_inc_conn_usage_count(conn_p
);
2486 cmd
= iscsit_allocate_cmd(conn_p
, TASK_RUNNING
);
2488 iscsit_dec_conn_usage_count(conn_p
);
2492 cmd
->logout_cid
= conn
->cid
;
2493 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2494 cmd
->i_state
= ISTATE_SEND_ASYNCMSG
;
2496 spin_lock_bh(&conn_p
->cmd_lock
);
2497 list_add_tail(&cmd
->i_conn_node
, &conn_p
->conn_cmd_list
);
2498 spin_unlock_bh(&conn_p
->cmd_lock
);
2500 iscsit_add_cmd_to_response_queue(cmd
, conn_p
, cmd
->i_state
);
2501 iscsit_dec_conn_usage_count(conn_p
);
2504 static int iscsit_send_conn_drop_async_message(
2505 struct iscsi_cmd
*cmd
,
2506 struct iscsi_conn
*conn
)
2508 struct iscsi_async
*hdr
;
2510 cmd
->tx_size
= ISCSI_HDR_LEN
;
2511 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2513 hdr
= (struct iscsi_async
*) cmd
->pdu
;
2514 hdr
->opcode
= ISCSI_OP_ASYNC_EVENT
;
2515 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
2516 cmd
->init_task_tag
= RESERVED_ITT
;
2517 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2518 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->rsvd4
[0]);
2519 cmd
->stat_sn
= conn
->stat_sn
++;
2520 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2521 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2522 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2523 hdr
->async_event
= ISCSI_ASYNC_MSG_DROPPING_CONNECTION
;
2524 hdr
->param1
= cpu_to_be16(cmd
->logout_cid
);
2525 hdr
->param2
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Wait
);
2526 hdr
->param3
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Retain
);
2528 if (conn
->conn_ops
->HeaderDigest
) {
2529 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2531 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
2532 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2534 cmd
->tx_size
+= ISCSI_CRC_LEN
;
2535 pr_debug("Attaching CRC32C HeaderDigest to"
2536 " Async Message 0x%08x\n", *header_digest
);
2539 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2540 cmd
->iov_misc
[0].iov_len
= cmd
->tx_size
;
2541 cmd
->iov_misc_count
= 1;
2543 pr_debug("Sending Connection Dropped Async Message StatSN:"
2544 " 0x%08x, for CID: %hu on CID: %hu\n", cmd
->stat_sn
,
2545 cmd
->logout_cid
, conn
->cid
);
2549 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2551 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2552 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2553 wait_for_completion_interruptible_timeout(
2554 &conn
->tx_half_close_comp
,
2555 ISCSI_TX_THREAD_TCP_TIMEOUT
* HZ
);
2560 iscsit_build_datain_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2561 struct iscsi_datain
*datain
, struct iscsi_data_rsp
*hdr
,
2564 hdr
->opcode
= ISCSI_OP_SCSI_DATA_IN
;
2565 hdr
->flags
= datain
->flags
;
2566 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
2567 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
2568 hdr
->flags
|= ISCSI_FLAG_DATA_OVERFLOW
;
2569 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2570 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
2571 hdr
->flags
|= ISCSI_FLAG_DATA_UNDERFLOW
;
2572 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2575 hton24(hdr
->dlength
, datain
->length
);
2576 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2577 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
2578 (struct scsi_lun
*)&hdr
->lun
);
2580 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2582 hdr
->itt
= cmd
->init_task_tag
;
2584 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2585 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2587 hdr
->ttt
= cpu_to_be32(0xFFFFFFFF);
2589 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2591 hdr
->statsn
= cpu_to_be32(0xFFFFFFFF);
2593 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2594 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2595 hdr
->datasn
= cpu_to_be32(datain
->data_sn
);
2596 hdr
->offset
= cpu_to_be32(datain
->offset
);
2598 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2599 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2600 cmd
->init_task_tag
, ntohl(hdr
->statsn
), ntohl(hdr
->datasn
),
2601 ntohl(hdr
->offset
), datain
->length
, conn
->cid
);
2604 static int iscsit_send_datain(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2606 struct iscsi_data_rsp
*hdr
= (struct iscsi_data_rsp
*)&cmd
->pdu
[0];
2607 struct iscsi_datain datain
;
2608 struct iscsi_datain_req
*dr
;
2610 u32 iov_count
= 0, tx_size
= 0;
2611 int eodr
= 0, ret
, iov_ret
;
2612 bool set_statsn
= false;
2614 memset(&datain
, 0, sizeof(struct iscsi_datain
));
2615 dr
= iscsit_get_datain_values(cmd
, &datain
);
2617 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2618 cmd
->init_task_tag
);
2622 * Be paranoid and double check the logic for now.
2624 if ((datain
.offset
+ datain
.length
) > cmd
->se_cmd
.data_length
) {
2625 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2626 " datain.length: %u exceeds cmd->data_length: %u\n",
2627 cmd
->init_task_tag
, datain
.offset
, datain
.length
,
2628 cmd
->se_cmd
.data_length
);
2632 atomic_long_add(datain
.length
, &conn
->sess
->tx_data_octets
);
2634 * Special case for successfully execution w/ both DATAIN
2637 if ((datain
.flags
& ISCSI_FLAG_DATA_STATUS
) &&
2638 (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
))
2639 datain
.flags
&= ~ISCSI_FLAG_DATA_STATUS
;
2641 if ((dr
->dr_complete
== DATAIN_COMPLETE_NORMAL
) ||
2642 (dr
->dr_complete
== DATAIN_COMPLETE_CONNECTION_RECOVERY
)) {
2643 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2644 cmd
->stat_sn
= conn
->stat_sn
++;
2646 } else if (dr
->dr_complete
==
2647 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY
)
2651 iscsit_build_datain_pdu(cmd
, conn
, &datain
, hdr
, set_statsn
);
2653 iov
= &cmd
->iov_data
[0];
2654 iov
[iov_count
].iov_base
= cmd
->pdu
;
2655 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
2656 tx_size
+= ISCSI_HDR_LEN
;
2658 if (conn
->conn_ops
->HeaderDigest
) {
2659 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2661 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, cmd
->pdu
,
2662 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2664 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2665 tx_size
+= ISCSI_CRC_LEN
;
2667 pr_debug("Attaching CRC32 HeaderDigest"
2668 " for DataIN PDU 0x%08x\n", *header_digest
);
2671 iov_ret
= iscsit_map_iovec(cmd
, &cmd
->iov_data
[1],
2672 datain
.offset
, datain
.length
);
2676 iov_count
+= iov_ret
;
2677 tx_size
+= datain
.length
;
2679 cmd
->padding
= ((-datain
.length
) & 3);
2681 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2682 iov
[iov_count
++].iov_len
= cmd
->padding
;
2683 tx_size
+= cmd
->padding
;
2685 pr_debug("Attaching %u padding bytes\n",
2688 if (conn
->conn_ops
->DataDigest
) {
2689 cmd
->data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_tx_hash
, cmd
,
2690 datain
.offset
, datain
.length
, cmd
->padding
, cmd
->pad_bytes
);
2692 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
2693 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2694 tx_size
+= ISCSI_CRC_LEN
;
2696 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2697 " 0x%08x\n", datain
.length
+cmd
->padding
, cmd
->data_crc
);
2700 cmd
->iov_data_count
= iov_count
;
2701 cmd
->tx_size
= tx_size
;
2703 /* sendpage is preferred but can't insert markers */
2704 if (!conn
->conn_ops
->IFMarker
)
2705 ret
= iscsit_fe_sendpage_sg(cmd
, conn
);
2707 ret
= iscsit_send_tx_data(cmd
, conn
, 0);
2709 iscsit_unmap_iovec(cmd
);
2712 iscsit_tx_thread_wait_for_tcp(conn
);
2716 if (dr
->dr_complete
) {
2717 eodr
= (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ?
2719 iscsit_free_datain_req(cmd
, dr
);
2726 iscsit_build_logout_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2727 struct iscsi_logout_rsp
*hdr
)
2729 struct iscsi_conn
*logout_conn
= NULL
;
2730 struct iscsi_conn_recovery
*cr
= NULL
;
2731 struct iscsi_session
*sess
= conn
->sess
;
2733 * The actual shutting down of Sessions and/or Connections
2734 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2735 * is done in scsi_logout_post_handler().
2737 switch (cmd
->logout_reason
) {
2738 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
2739 pr_debug("iSCSI session logout successful, setting"
2740 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2741 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2743 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
2744 if (cmd
->logout_response
== ISCSI_LOGOUT_CID_NOT_FOUND
)
2747 * For CLOSECONNECTION logout requests carrying
2748 * a matching logout CID -> local CID, the reference
2749 * for the local CID will have been incremented in
2750 * iscsi_logout_closeconnection().
2752 * For CLOSECONNECTION logout requests carrying
2753 * a different CID than the connection it arrived
2754 * on, the connection responding to cmd->logout_cid
2755 * is stopped in iscsit_logout_post_handler_diffcid().
2758 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2759 " successful.\n", cmd
->logout_cid
, conn
->cid
);
2760 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2762 case ISCSI_LOGOUT_REASON_RECOVERY
:
2763 if ((cmd
->logout_response
== ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
) ||
2764 (cmd
->logout_response
== ISCSI_LOGOUT_CLEANUP_FAILED
))
2767 * If the connection is still active from our point of view
2768 * force connection recovery to occur.
2770 logout_conn
= iscsit_get_conn_from_cid_rcfr(sess
,
2773 iscsit_connection_reinstatement_rcfr(logout_conn
);
2774 iscsit_dec_conn_usage_count(logout_conn
);
2777 cr
= iscsit_get_inactive_connection_recovery_entry(
2778 conn
->sess
, cmd
->logout_cid
);
2780 pr_err("Unable to locate CID: %hu for"
2781 " REMOVECONNFORRECOVERY Logout Request.\n",
2783 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2787 iscsit_discard_cr_cmds_by_expstatsn(cr
, cmd
->exp_stat_sn
);
2789 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2790 " for recovery for CID: %hu on CID: %hu successful.\n",
2791 cmd
->logout_cid
, conn
->cid
);
2792 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2795 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2796 cmd
->logout_reason
);
2800 hdr
->opcode
= ISCSI_OP_LOGOUT_RSP
;
2801 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2802 hdr
->response
= cmd
->logout_response
;
2803 hdr
->itt
= cmd
->init_task_tag
;
2804 cmd
->stat_sn
= conn
->stat_sn
++;
2805 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2807 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2808 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2809 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2811 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2812 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2813 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
,
2814 cmd
->logout_cid
, conn
->cid
);
2818 EXPORT_SYMBOL(iscsit_build_logout_rsp
);
2821 iscsit_send_logout(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2824 int niov
= 0, tx_size
, rc
;
2826 rc
= iscsit_build_logout_rsp(cmd
, conn
,
2827 (struct iscsi_logout_rsp
*)&cmd
->pdu
[0]);
2831 tx_size
= ISCSI_HDR_LEN
;
2832 iov
= &cmd
->iov_misc
[0];
2833 iov
[niov
].iov_base
= cmd
->pdu
;
2834 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
2836 if (conn
->conn_ops
->HeaderDigest
) {
2837 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2839 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, &cmd
->pdu
[0],
2840 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2842 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2843 tx_size
+= ISCSI_CRC_LEN
;
2844 pr_debug("Attaching CRC32C HeaderDigest to"
2845 " Logout Response 0x%08x\n", *header_digest
);
2847 cmd
->iov_misc_count
= niov
;
2848 cmd
->tx_size
= tx_size
;
2854 iscsit_build_nopin_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2855 struct iscsi_nopin
*hdr
, bool nopout_response
)
2857 hdr
->opcode
= ISCSI_OP_NOOP_IN
;
2858 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2859 hton24(hdr
->dlength
, cmd
->buf_ptr_size
);
2860 if (nopout_response
)
2861 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2862 hdr
->itt
= cmd
->init_task_tag
;
2863 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2864 cmd
->stat_sn
= (nopout_response
) ? conn
->stat_sn
++ :
2866 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2868 if (nopout_response
)
2869 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2871 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2872 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2874 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2875 " StatSN: 0x%08x, Length %u\n", (nopout_response
) ?
2876 "Solicitied" : "Unsolicitied", cmd
->init_task_tag
,
2877 cmd
->targ_xfer_tag
, cmd
->stat_sn
, cmd
->buf_ptr_size
);
2879 EXPORT_SYMBOL(iscsit_build_nopin_rsp
);
2882 * Unsolicited NOPIN, either requesting a response or not.
2884 static int iscsit_send_unsolicited_nopin(
2885 struct iscsi_cmd
*cmd
,
2886 struct iscsi_conn
*conn
,
2889 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
2890 int tx_size
= ISCSI_HDR_LEN
, ret
;
2892 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, false);
2894 if (conn
->conn_ops
->HeaderDigest
) {
2895 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2897 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
2898 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2900 tx_size
+= ISCSI_CRC_LEN
;
2901 pr_debug("Attaching CRC32C HeaderDigest to"
2902 " NopIN 0x%08x\n", *header_digest
);
2905 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2906 cmd
->iov_misc
[0].iov_len
= tx_size
;
2907 cmd
->iov_misc_count
= 1;
2908 cmd
->tx_size
= tx_size
;
2910 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2911 " 0x%08x CID: %hu\n", hdr
->ttt
, cmd
->stat_sn
, conn
->cid
);
2913 ret
= iscsit_send_tx_data(cmd
, conn
, 1);
2915 iscsit_tx_thread_wait_for_tcp(conn
);
2919 spin_lock_bh(&cmd
->istate_lock
);
2920 cmd
->i_state
= want_response
?
2921 ISTATE_SENT_NOPIN_WANT_RESPONSE
: ISTATE_SENT_STATUS
;
2922 spin_unlock_bh(&cmd
->istate_lock
);
2928 iscsit_send_nopin(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2930 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
2933 int niov
= 0, tx_size
;
2935 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, true);
2937 tx_size
= ISCSI_HDR_LEN
;
2938 iov
= &cmd
->iov_misc
[0];
2939 iov
[niov
].iov_base
= cmd
->pdu
;
2940 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
2942 if (conn
->conn_ops
->HeaderDigest
) {
2943 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2945 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
2946 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2948 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2949 tx_size
+= ISCSI_CRC_LEN
;
2950 pr_debug("Attaching CRC32C HeaderDigest"
2951 " to NopIn 0x%08x\n", *header_digest
);
2955 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2956 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2958 if (cmd
->buf_ptr_size
) {
2959 iov
[niov
].iov_base
= cmd
->buf_ptr
;
2960 iov
[niov
++].iov_len
= cmd
->buf_ptr_size
;
2961 tx_size
+= cmd
->buf_ptr_size
;
2963 pr_debug("Echoing back %u bytes of ping"
2964 " data.\n", cmd
->buf_ptr_size
);
2966 padding
= ((-cmd
->buf_ptr_size
) & 3);
2968 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
2969 iov
[niov
++].iov_len
= padding
;
2971 pr_debug("Attaching %u additional"
2972 " padding bytes.\n", padding
);
2974 if (conn
->conn_ops
->DataDigest
) {
2975 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2976 cmd
->buf_ptr
, cmd
->buf_ptr_size
,
2977 padding
, (u8
*)&cmd
->pad_bytes
,
2978 (u8
*)&cmd
->data_crc
);
2980 iov
[niov
].iov_base
= &cmd
->data_crc
;
2981 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
2982 tx_size
+= ISCSI_CRC_LEN
;
2983 pr_debug("Attached DataDigest for %u"
2984 " bytes of ping data, CRC 0x%08x\n",
2985 cmd
->buf_ptr_size
, cmd
->data_crc
);
2989 cmd
->iov_misc_count
= niov
;
2990 cmd
->tx_size
= tx_size
;
2995 static int iscsit_send_r2t(
2996 struct iscsi_cmd
*cmd
,
2997 struct iscsi_conn
*conn
)
3000 struct iscsi_r2t
*r2t
;
3001 struct iscsi_r2t_rsp
*hdr
;
3004 r2t
= iscsit_get_r2t_from_list(cmd
);
3008 hdr
= (struct iscsi_r2t_rsp
*) cmd
->pdu
;
3009 memset(hdr
, 0, ISCSI_HDR_LEN
);
3010 hdr
->opcode
= ISCSI_OP_R2T
;
3011 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3012 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
3013 (struct scsi_lun
*)&hdr
->lun
);
3014 hdr
->itt
= cmd
->init_task_tag
;
3015 spin_lock_bh(&conn
->sess
->ttt_lock
);
3016 r2t
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
3017 if (r2t
->targ_xfer_tag
== 0xFFFFFFFF)
3018 r2t
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
3019 spin_unlock_bh(&conn
->sess
->ttt_lock
);
3020 hdr
->ttt
= cpu_to_be32(r2t
->targ_xfer_tag
);
3021 hdr
->statsn
= cpu_to_be32(conn
->stat_sn
);
3022 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3023 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3024 hdr
->r2tsn
= cpu_to_be32(r2t
->r2t_sn
);
3025 hdr
->data_offset
= cpu_to_be32(r2t
->offset
);
3026 hdr
->data_length
= cpu_to_be32(r2t
->xfer_len
);
3028 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
3029 cmd
->iov_misc
[0].iov_len
= ISCSI_HDR_LEN
;
3030 tx_size
+= ISCSI_HDR_LEN
;
3032 if (conn
->conn_ops
->HeaderDigest
) {
3033 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3035 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3036 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3038 cmd
->iov_misc
[0].iov_len
+= ISCSI_CRC_LEN
;
3039 tx_size
+= ISCSI_CRC_LEN
;
3040 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3041 " PDU 0x%08x\n", *header_digest
);
3044 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3045 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3046 (!r2t
->recovery_r2t
) ? "" : "Recovery ", cmd
->init_task_tag
,
3047 r2t
->targ_xfer_tag
, ntohl(hdr
->statsn
), r2t
->r2t_sn
,
3048 r2t
->offset
, r2t
->xfer_len
, conn
->cid
);
3050 cmd
->iov_misc_count
= 1;
3051 cmd
->tx_size
= tx_size
;
3053 spin_lock_bh(&cmd
->r2t_lock
);
3055 spin_unlock_bh(&cmd
->r2t_lock
);
3057 ret
= iscsit_send_tx_data(cmd
, conn
, 1);
3059 iscsit_tx_thread_wait_for_tcp(conn
);
3063 spin_lock_bh(&cmd
->dataout_timeout_lock
);
3064 iscsit_start_dataout_timer(cmd
, conn
);
3065 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
3071 * @recovery: If called from iscsi_task_reassign_complete_write() for
3072 * connection recovery.
3074 int iscsit_build_r2ts_for_cmd(
3075 struct iscsi_conn
*conn
,
3076 struct iscsi_cmd
*cmd
,
3080 u32 offset
= 0, xfer_len
= 0;
3082 spin_lock_bh(&cmd
->r2t_lock
);
3083 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
) {
3084 spin_unlock_bh(&cmd
->r2t_lock
);
3088 if (conn
->sess
->sess_ops
->DataSequenceInOrder
&&
3090 cmd
->r2t_offset
= max(cmd
->r2t_offset
, cmd
->write_data_done
);
3092 while (cmd
->outstanding_r2ts
< conn
->sess
->sess_ops
->MaxOutstandingR2T
) {
3093 if (conn
->sess
->sess_ops
->DataSequenceInOrder
) {
3094 offset
= cmd
->r2t_offset
;
3096 if (first_r2t
&& recovery
) {
3097 int new_data_end
= offset
+
3098 conn
->sess
->sess_ops
->MaxBurstLength
-
3099 cmd
->next_burst_len
;
3101 if (new_data_end
> cmd
->se_cmd
.data_length
)
3102 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3105 conn
->sess
->sess_ops
->MaxBurstLength
-
3106 cmd
->next_burst_len
;
3108 int new_data_end
= offset
+
3109 conn
->sess
->sess_ops
->MaxBurstLength
;
3111 if (new_data_end
> cmd
->se_cmd
.data_length
)
3112 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3114 xfer_len
= conn
->sess
->sess_ops
->MaxBurstLength
;
3116 cmd
->r2t_offset
+= xfer_len
;
3118 if (cmd
->r2t_offset
== cmd
->se_cmd
.data_length
)
3119 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3121 struct iscsi_seq
*seq
;
3123 seq
= iscsit_get_seq_holder_for_r2t(cmd
);
3125 spin_unlock_bh(&cmd
->r2t_lock
);
3129 offset
= seq
->offset
;
3130 xfer_len
= seq
->xfer_len
;
3132 if (cmd
->seq_send_order
== cmd
->seq_count
)
3133 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3135 cmd
->outstanding_r2ts
++;
3138 if (iscsit_add_r2t_to_list(cmd
, offset
, xfer_len
, 0, 0) < 0) {
3139 spin_unlock_bh(&cmd
->r2t_lock
);
3143 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
)
3146 spin_unlock_bh(&cmd
->r2t_lock
);
3151 void iscsit_build_rsp_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3152 bool inc_stat_sn
, struct iscsi_scsi_rsp
*hdr
)
3155 cmd
->stat_sn
= conn
->stat_sn
++;
3157 atomic_long_inc(&conn
->sess
->rsp_pdus
);
3159 memset(hdr
, 0, ISCSI_HDR_LEN
);
3160 hdr
->opcode
= ISCSI_OP_SCSI_CMD_RSP
;
3161 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3162 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
3163 hdr
->flags
|= ISCSI_FLAG_CMD_OVERFLOW
;
3164 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3165 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
3166 hdr
->flags
|= ISCSI_FLAG_CMD_UNDERFLOW
;
3167 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3169 hdr
->response
= cmd
->iscsi_response
;
3170 hdr
->cmd_status
= cmd
->se_cmd
.scsi_status
;
3171 hdr
->itt
= cmd
->init_task_tag
;
3172 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3174 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3175 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3176 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3178 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3179 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3180 cmd
->init_task_tag
, cmd
->stat_sn
, cmd
->se_cmd
.scsi_status
,
3181 cmd
->se_cmd
.scsi_status
, conn
->cid
);
3183 EXPORT_SYMBOL(iscsit_build_rsp_pdu
);
3185 static int iscsit_send_response(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3187 struct iscsi_scsi_rsp
*hdr
= (struct iscsi_scsi_rsp
*)&cmd
->pdu
[0];
3189 u32 padding
= 0, tx_size
= 0;
3191 bool inc_stat_sn
= (cmd
->i_state
== ISTATE_SEND_STATUS
);
3193 iscsit_build_rsp_pdu(cmd
, conn
, inc_stat_sn
, hdr
);
3195 iov
= &cmd
->iov_misc
[0];
3196 iov
[iov_count
].iov_base
= cmd
->pdu
;
3197 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3198 tx_size
+= ISCSI_HDR_LEN
;
3201 * Attach SENSE DATA payload to iSCSI Response PDU
3203 if (cmd
->se_cmd
.sense_buffer
&&
3204 ((cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
3205 (cmd
->se_cmd
.se_cmd_flags
& SCF_EMULATED_TASK_SENSE
))) {
3206 put_unaligned_be16(cmd
->se_cmd
.scsi_sense_length
, cmd
->sense_buffer
);
3207 cmd
->se_cmd
.scsi_sense_length
+= sizeof (__be16
);
3209 padding
= -(cmd
->se_cmd
.scsi_sense_length
) & 3;
3210 hton24(hdr
->dlength
, (u32
)cmd
->se_cmd
.scsi_sense_length
);
3211 iov
[iov_count
].iov_base
= cmd
->sense_buffer
;
3212 iov
[iov_count
++].iov_len
=
3213 (cmd
->se_cmd
.scsi_sense_length
+ padding
);
3214 tx_size
+= cmd
->se_cmd
.scsi_sense_length
;
3217 memset(cmd
->sense_buffer
+
3218 cmd
->se_cmd
.scsi_sense_length
, 0, padding
);
3220 pr_debug("Adding %u bytes of padding to"
3221 " SENSE.\n", padding
);
3224 if (conn
->conn_ops
->DataDigest
) {
3225 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3227 (cmd
->se_cmd
.scsi_sense_length
+ padding
),
3228 0, NULL
, (u8
*)&cmd
->data_crc
);
3230 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3231 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3232 tx_size
+= ISCSI_CRC_LEN
;
3234 pr_debug("Attaching CRC32 DataDigest for"
3235 " SENSE, %u bytes CRC 0x%08x\n",
3236 (cmd
->se_cmd
.scsi_sense_length
+ padding
),
3240 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3242 cmd
->se_cmd
.scsi_sense_length
);
3245 if (conn
->conn_ops
->HeaderDigest
) {
3246 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3248 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, cmd
->pdu
,
3249 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3251 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3252 tx_size
+= ISCSI_CRC_LEN
;
3253 pr_debug("Attaching CRC32 HeaderDigest for Response"
3254 " PDU 0x%08x\n", *header_digest
);
3257 cmd
->iov_misc_count
= iov_count
;
3258 cmd
->tx_size
= tx_size
;
3263 static u8
iscsit_convert_tcm_tmr_rsp(struct se_tmr_req
*se_tmr
)
3265 switch (se_tmr
->response
) {
3266 case TMR_FUNCTION_COMPLETE
:
3267 return ISCSI_TMF_RSP_COMPLETE
;
3268 case TMR_TASK_DOES_NOT_EXIST
:
3269 return ISCSI_TMF_RSP_NO_TASK
;
3270 case TMR_LUN_DOES_NOT_EXIST
:
3271 return ISCSI_TMF_RSP_NO_LUN
;
3272 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
:
3273 return ISCSI_TMF_RSP_NOT_SUPPORTED
;
3274 case TMR_FUNCTION_REJECTED
:
3276 return ISCSI_TMF_RSP_REJECTED
;
3281 iscsit_build_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3282 struct iscsi_tm_rsp
*hdr
)
3284 struct se_tmr_req
*se_tmr
= cmd
->se_cmd
.se_tmr_req
;
3286 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC_RSP
;
3287 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
3288 hdr
->response
= iscsit_convert_tcm_tmr_rsp(se_tmr
);
3289 hdr
->itt
= cmd
->init_task_tag
;
3290 cmd
->stat_sn
= conn
->stat_sn
++;
3291 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3293 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3294 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3295 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3297 pr_debug("Built Task Management Response ITT: 0x%08x,"
3298 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3299 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
, conn
->cid
);
3301 EXPORT_SYMBOL(iscsit_build_task_mgt_rsp
);
3304 iscsit_send_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3306 struct iscsi_tm_rsp
*hdr
= (struct iscsi_tm_rsp
*)&cmd
->pdu
[0];
3309 iscsit_build_task_mgt_rsp(cmd
, conn
, hdr
);
3311 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
3312 cmd
->iov_misc
[0].iov_len
= ISCSI_HDR_LEN
;
3313 tx_size
+= ISCSI_HDR_LEN
;
3315 if (conn
->conn_ops
->HeaderDigest
) {
3316 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3318 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3319 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3321 cmd
->iov_misc
[0].iov_len
+= ISCSI_CRC_LEN
;
3322 tx_size
+= ISCSI_CRC_LEN
;
3323 pr_debug("Attaching CRC32 HeaderDigest for Task"
3324 " Mgmt Response PDU 0x%08x\n", *header_digest
);
3327 cmd
->iov_misc_count
= 1;
3328 cmd
->tx_size
= tx_size
;
3333 static bool iscsit_check_inaddr_any(struct iscsi_np
*np
)
3337 if (np
->np_sockaddr
.ss_family
== AF_INET6
) {
3338 const struct sockaddr_in6 sin6
= {
3339 .sin6_addr
= IN6ADDR_ANY_INIT
};
3340 struct sockaddr_in6
*sock_in6
=
3341 (struct sockaddr_in6
*)&np
->np_sockaddr
;
3343 if (!memcmp(sock_in6
->sin6_addr
.s6_addr
,
3344 sin6
.sin6_addr
.s6_addr
, 16))
3347 struct sockaddr_in
* sock_in
=
3348 (struct sockaddr_in
*)&np
->np_sockaddr
;
3350 if (sock_in
->sin_addr
.s_addr
== htonl(INADDR_ANY
))
3357 #define SENDTARGETS_BUF_LIMIT 32768U
3359 static int iscsit_build_sendtargets_response(struct iscsi_cmd
*cmd
)
3361 char *payload
= NULL
;
3362 struct iscsi_conn
*conn
= cmd
->conn
;
3363 struct iscsi_portal_group
*tpg
;
3364 struct iscsi_tiqn
*tiqn
;
3365 struct iscsi_tpg_np
*tpg_np
;
3366 int buffer_len
, end_of_buf
= 0, len
= 0, payload_len
= 0;
3367 int target_name_printed
;
3368 unsigned char buf
[ISCSI_IQN_LEN
+12]; /* iqn + "TargetName=" + \0 */
3369 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
= NULL
;
3371 buffer_len
= max(conn
->conn_ops
->MaxRecvDataSegmentLength
,
3372 SENDTARGETS_BUF_LIMIT
);
3374 payload
= kzalloc(buffer_len
, GFP_KERNEL
);
3376 pr_err("Unable to allocate memory for sendtargets"
3381 * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE
3384 if (cmd
->cmd_flags
& IFC_SENDTARGETS_SINGLE
) {
3385 text_ptr
= strchr(text_in
, '=');
3387 pr_err("Unable to locate '=' string in text_in:"
3393 * Skip over '=' character..
3398 spin_lock(&tiqn_lock
);
3399 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
3400 if ((cmd
->cmd_flags
& IFC_SENDTARGETS_SINGLE
) &&
3401 strcmp(tiqn
->tiqn
, text_ptr
)) {
3405 target_name_printed
= 0;
3407 spin_lock(&tiqn
->tiqn_tpg_lock
);
3408 list_for_each_entry(tpg
, &tiqn
->tiqn_tpg_list
, tpg_list
) {
3410 /* If demo_mode_discovery=0 and generate_node_acls=0
3411 * (demo mode dislabed) do not return
3412 * TargetName+TargetAddress unless a NodeACL exists.
3415 if ((tpg
->tpg_attrib
.generate_node_acls
== 0) &&
3416 (tpg
->tpg_attrib
.demo_mode_discovery
== 0) &&
3417 (!core_tpg_get_initiator_node_acl(&tpg
->tpg_se_tpg
,
3418 cmd
->conn
->sess
->sess_ops
->InitiatorName
))) {
3422 spin_lock(&tpg
->tpg_state_lock
);
3423 if ((tpg
->tpg_state
== TPG_STATE_FREE
) ||
3424 (tpg
->tpg_state
== TPG_STATE_INACTIVE
)) {
3425 spin_unlock(&tpg
->tpg_state_lock
);
3428 spin_unlock(&tpg
->tpg_state_lock
);
3430 spin_lock(&tpg
->tpg_np_lock
);
3431 list_for_each_entry(tpg_np
, &tpg
->tpg_gnp_list
,
3433 struct iscsi_np
*np
= tpg_np
->tpg_np
;
3434 bool inaddr_any
= iscsit_check_inaddr_any(np
);
3436 if (!target_name_printed
) {
3437 len
= sprintf(buf
, "TargetName=%s",
3441 if ((len
+ payload_len
) > buffer_len
) {
3442 spin_unlock(&tpg
->tpg_np_lock
);
3443 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3447 memcpy(payload
+ payload_len
, buf
, len
);
3449 target_name_printed
= 1;
3452 len
= sprintf(buf
, "TargetAddress="
3454 (inaddr_any
== false) ?
3455 np
->np_ip
: conn
->local_ip
,
3456 (inaddr_any
== false) ?
3457 np
->np_port
: conn
->local_port
,
3461 if ((len
+ payload_len
) > buffer_len
) {
3462 spin_unlock(&tpg
->tpg_np_lock
);
3463 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3467 memcpy(payload
+ payload_len
, buf
, len
);
3470 spin_unlock(&tpg
->tpg_np_lock
);
3472 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3477 if (cmd
->cmd_flags
& IFC_SENDTARGETS_SINGLE
)
3480 spin_unlock(&tiqn_lock
);
3482 cmd
->buf_ptr
= payload
;
3488 iscsit_build_text_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3489 struct iscsi_text_rsp
*hdr
)
3491 int text_length
, padding
;
3493 text_length
= iscsit_build_sendtargets_response(cmd
);
3494 if (text_length
< 0)
3497 hdr
->opcode
= ISCSI_OP_TEXT_RSP
;
3498 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3499 padding
= ((-text_length
) & 3);
3500 hton24(hdr
->dlength
, text_length
);
3501 hdr
->itt
= cmd
->init_task_tag
;
3502 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
3503 cmd
->stat_sn
= conn
->stat_sn
++;
3504 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3506 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3507 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3508 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3510 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3511 " Length: %u, CID: %hu\n", cmd
->init_task_tag
, cmd
->stat_sn
,
3512 text_length
, conn
->cid
);
3514 return text_length
+ padding
;
3516 EXPORT_SYMBOL(iscsit_build_text_rsp
);
3519 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3520 * MaxRecvDataSegmentLength.
3522 static int iscsit_send_text_rsp(
3523 struct iscsi_cmd
*cmd
,
3524 struct iscsi_conn
*conn
)
3526 struct iscsi_text_rsp
*hdr
= (struct iscsi_text_rsp
*)cmd
->pdu
;
3529 int text_length
, iov_count
= 0, rc
;
3531 rc
= iscsit_build_text_rsp(cmd
, conn
, hdr
);
3536 iov
= &cmd
->iov_misc
[0];
3537 iov
[iov_count
].iov_base
= cmd
->pdu
;
3538 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3539 iov
[iov_count
].iov_base
= cmd
->buf_ptr
;
3540 iov
[iov_count
++].iov_len
= text_length
;
3542 tx_size
+= (ISCSI_HDR_LEN
+ text_length
);
3544 if (conn
->conn_ops
->HeaderDigest
) {
3545 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3547 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3548 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3550 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3551 tx_size
+= ISCSI_CRC_LEN
;
3552 pr_debug("Attaching CRC32 HeaderDigest for"
3553 " Text Response PDU 0x%08x\n", *header_digest
);
3556 if (conn
->conn_ops
->DataDigest
) {
3557 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3558 cmd
->buf_ptr
, text_length
,
3559 0, NULL
, (u8
*)&cmd
->data_crc
);
3561 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3562 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3563 tx_size
+= ISCSI_CRC_LEN
;
3565 pr_debug("Attaching DataDigest for %u bytes of text"
3566 " data, CRC 0x%08x\n", text_length
,
3570 cmd
->iov_misc_count
= iov_count
;
3571 cmd
->tx_size
= tx_size
;
3577 iscsit_build_reject(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3578 struct iscsi_reject
*hdr
)
3580 hdr
->opcode
= ISCSI_OP_REJECT
;
3581 hdr
->reason
= cmd
->reject_reason
;
3582 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3583 hton24(hdr
->dlength
, ISCSI_HDR_LEN
);
3584 hdr
->ffffffff
= cpu_to_be32(0xffffffff);
3585 cmd
->stat_sn
= conn
->stat_sn
++;
3586 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3587 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3588 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3591 EXPORT_SYMBOL(iscsit_build_reject
);
3593 static int iscsit_send_reject(
3594 struct iscsi_cmd
*cmd
,
3595 struct iscsi_conn
*conn
)
3597 struct iscsi_reject
*hdr
= (struct iscsi_reject
*)&cmd
->pdu
[0];
3599 u32 iov_count
= 0, tx_size
;
3601 iscsit_build_reject(cmd
, conn
, hdr
);
3603 iov
= &cmd
->iov_misc
[0];
3604 iov
[iov_count
].iov_base
= cmd
->pdu
;
3605 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3606 iov
[iov_count
].iov_base
= cmd
->buf_ptr
;
3607 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3609 tx_size
= (ISCSI_HDR_LEN
+ ISCSI_HDR_LEN
);
3611 if (conn
->conn_ops
->HeaderDigest
) {
3612 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3614 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3615 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3617 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3618 tx_size
+= ISCSI_CRC_LEN
;
3619 pr_debug("Attaching CRC32 HeaderDigest for"
3620 " REJECT PDU 0x%08x\n", *header_digest
);
3623 if (conn
->conn_ops
->DataDigest
) {
3624 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, cmd
->buf_ptr
,
3625 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)&cmd
->data_crc
);
3627 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3628 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3629 tx_size
+= ISCSI_CRC_LEN
;
3630 pr_debug("Attaching CRC32 DataDigest for REJECT"
3631 " PDU 0x%08x\n", cmd
->data_crc
);
3634 cmd
->iov_misc_count
= iov_count
;
3635 cmd
->tx_size
= tx_size
;
3637 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3638 " CID: %hu\n", ntohl(hdr
->statsn
), hdr
->reason
, conn
->cid
);
3643 void iscsit_thread_get_cpumask(struct iscsi_conn
*conn
)
3645 struct iscsi_thread_set
*ts
= conn
->thread_set
;
3648 * thread_id is assigned from iscsit_global->ts_bitmap from
3649 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3651 * Here we use thread_id to determine which CPU that this
3652 * iSCSI connection's iscsi_thread_set will be scheduled to
3655 ord
= ts
->thread_id
% cpumask_weight(cpu_online_mask
);
3656 for_each_online_cpu(cpu
) {
3658 cpumask_set_cpu(cpu
, conn
->conn_cpumask
);
3663 * This should never be reached..
3666 cpumask_setall(conn
->conn_cpumask
);
3669 static inline void iscsit_thread_check_cpumask(
3670 struct iscsi_conn
*conn
,
3671 struct task_struct
*p
,
3676 * mode == 1 signals iscsi_target_tx_thread() usage.
3677 * mode == 0 signals iscsi_target_rx_thread() usage.
3680 if (!conn
->conn_tx_reset_cpumask
)
3682 conn
->conn_tx_reset_cpumask
= 0;
3684 if (!conn
->conn_rx_reset_cpumask
)
3686 conn
->conn_rx_reset_cpumask
= 0;
3689 * Update the CPU mask for this single kthread so that
3690 * both TX and RX kthreads are scheduled to run on the
3693 memset(buf
, 0, 128);
3694 cpumask_scnprintf(buf
, 128, conn
->conn_cpumask
);
3695 set_cpus_allowed_ptr(p
, conn
->conn_cpumask
);
3699 iscsit_immediate_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3704 case ISTATE_SEND_R2T
:
3705 ret
= iscsit_send_r2t(cmd
, conn
);
3710 spin_lock_bh(&conn
->cmd_lock
);
3711 list_del(&cmd
->i_conn_node
);
3712 spin_unlock_bh(&conn
->cmd_lock
);
3714 iscsit_free_cmd(cmd
, false);
3716 case ISTATE_SEND_NOPIN_WANT_RESPONSE
:
3717 iscsit_mod_nopin_response_timer(conn
);
3718 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 1);
3722 case ISTATE_SEND_NOPIN_NO_RESPONSE
:
3723 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 0);
3728 pr_err("Unknown Opcode: 0x%02x ITT:"
3729 " 0x%08x, i_state: %d on CID: %hu\n",
3730 cmd
->iscsi_opcode
, cmd
->init_task_tag
, state
,
3742 iscsit_handle_immediate_queue(struct iscsi_conn
*conn
)
3744 struct iscsit_transport
*t
= conn
->conn_transport
;
3745 struct iscsi_queue_req
*qr
;
3746 struct iscsi_cmd
*cmd
;
3750 while ((qr
= iscsit_get_cmd_from_immediate_queue(conn
))) {
3751 atomic_set(&conn
->check_immediate_queue
, 0);
3754 kmem_cache_free(lio_qr_cache
, qr
);
3756 ret
= t
->iscsit_immediate_queue(conn
, cmd
, state
);
3765 iscsit_response_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3771 case ISTATE_SEND_DATAIN
:
3772 ret
= iscsit_send_datain(cmd
, conn
);
3777 goto check_rsp_state
;
3778 else if (ret
== 1) {
3780 spin_lock_bh(&cmd
->istate_lock
);
3781 cmd
->i_state
= ISTATE_SENT_STATUS
;
3782 spin_unlock_bh(&cmd
->istate_lock
);
3784 if (atomic_read(&conn
->check_immediate_queue
))
3788 } else if (ret
== 2) {
3789 /* Still must send status,
3790 SCF_TRANSPORT_TASK_SENSE was set */
3791 spin_lock_bh(&cmd
->istate_lock
);
3792 cmd
->i_state
= ISTATE_SEND_STATUS
;
3793 spin_unlock_bh(&cmd
->istate_lock
);
3794 state
= ISTATE_SEND_STATUS
;
3795 goto check_rsp_state
;
3799 case ISTATE_SEND_STATUS
:
3800 case ISTATE_SEND_STATUS_RECOVERY
:
3801 ret
= iscsit_send_response(cmd
, conn
);
3803 case ISTATE_SEND_LOGOUTRSP
:
3804 ret
= iscsit_send_logout(cmd
, conn
);
3806 case ISTATE_SEND_ASYNCMSG
:
3807 ret
= iscsit_send_conn_drop_async_message(
3810 case ISTATE_SEND_NOPIN
:
3811 ret
= iscsit_send_nopin(cmd
, conn
);
3813 case ISTATE_SEND_REJECT
:
3814 ret
= iscsit_send_reject(cmd
, conn
);
3816 case ISTATE_SEND_TASKMGTRSP
:
3817 ret
= iscsit_send_task_mgt_rsp(cmd
, conn
);
3820 ret
= iscsit_tmr_post_handler(cmd
, conn
);
3822 iscsit_fall_back_to_erl0(conn
->sess
);
3824 case ISTATE_SEND_TEXTRSP
:
3825 ret
= iscsit_send_text_rsp(cmd
, conn
);
3828 pr_err("Unknown Opcode: 0x%02x ITT:"
3829 " 0x%08x, i_state: %d on CID: %hu\n",
3830 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3837 if (iscsit_send_tx_data(cmd
, conn
, 1) < 0) {
3838 iscsit_tx_thread_wait_for_tcp(conn
);
3839 iscsit_unmap_iovec(cmd
);
3842 iscsit_unmap_iovec(cmd
);
3845 case ISTATE_SEND_LOGOUTRSP
:
3846 if (!iscsit_logout_post_handler(cmd
, conn
))
3849 case ISTATE_SEND_STATUS
:
3850 case ISTATE_SEND_ASYNCMSG
:
3851 case ISTATE_SEND_NOPIN
:
3852 case ISTATE_SEND_STATUS_RECOVERY
:
3853 case ISTATE_SEND_TEXTRSP
:
3854 case ISTATE_SEND_TASKMGTRSP
:
3855 case ISTATE_SEND_REJECT
:
3856 spin_lock_bh(&cmd
->istate_lock
);
3857 cmd
->i_state
= ISTATE_SENT_STATUS
;
3858 spin_unlock_bh(&cmd
->istate_lock
);
3861 pr_err("Unknown Opcode: 0x%02x ITT:"
3862 " 0x%08x, i_state: %d on CID: %hu\n",
3863 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3864 cmd
->i_state
, conn
->cid
);
3868 if (atomic_read(&conn
->check_immediate_queue
))
3879 static int iscsit_handle_response_queue(struct iscsi_conn
*conn
)
3881 struct iscsit_transport
*t
= conn
->conn_transport
;
3882 struct iscsi_queue_req
*qr
;
3883 struct iscsi_cmd
*cmd
;
3887 while ((qr
= iscsit_get_cmd_from_response_queue(conn
))) {
3890 kmem_cache_free(lio_qr_cache
, qr
);
3892 ret
= t
->iscsit_response_queue(conn
, cmd
, state
);
3893 if (ret
== 1 || ret
< 0)
3900 int iscsi_target_tx_thread(void *arg
)
3903 struct iscsi_conn
*conn
;
3904 struct iscsi_thread_set
*ts
= arg
;
3906 * Allow ourselves to be interrupted by SIGINT so that a
3907 * connection recovery / failure event can be triggered externally.
3909 allow_signal(SIGINT
);
3912 conn
= iscsi_tx_thread_pre_handler(ts
);
3918 while (!kthread_should_stop()) {
3920 * Ensure that both TX and RX per connection kthreads
3921 * are scheduled to run on the same CPU.
3923 iscsit_thread_check_cpumask(conn
, current
, 1);
3925 wait_event_interruptible(conn
->queues_wq
,
3926 !iscsit_conn_all_queues_empty(conn
) ||
3927 ts
->status
== ISCSI_THREAD_SET_RESET
);
3929 if ((ts
->status
== ISCSI_THREAD_SET_RESET
) ||
3930 signal_pending(current
))
3934 ret
= iscsit_handle_immediate_queue(conn
);
3938 ret
= iscsit_handle_response_queue(conn
);
3941 else if (ret
== -EAGAIN
)
3948 iscsit_take_action_for_connection_exit(conn
);
3954 static int iscsi_target_rx_opcode(struct iscsi_conn
*conn
, unsigned char *buf
)
3956 struct iscsi_hdr
*hdr
= (struct iscsi_hdr
*)buf
;
3957 struct iscsi_cmd
*cmd
;
3960 switch (hdr
->opcode
& ISCSI_OPCODE_MASK
) {
3961 case ISCSI_OP_SCSI_CMD
:
3962 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3966 ret
= iscsit_handle_scsi_cmd(conn
, cmd
, buf
);
3968 case ISCSI_OP_SCSI_DATA_OUT
:
3969 ret
= iscsit_handle_data_out(conn
, buf
);
3971 case ISCSI_OP_NOOP_OUT
:
3973 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
3974 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3978 ret
= iscsit_handle_nop_out(conn
, cmd
, buf
);
3980 case ISCSI_OP_SCSI_TMFUNC
:
3981 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3985 ret
= iscsit_handle_task_mgt_cmd(conn
, cmd
, buf
);
3988 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3992 ret
= iscsit_handle_text_cmd(conn
, cmd
, buf
);
3994 case ISCSI_OP_LOGOUT
:
3995 cmd
= iscsit_allocate_cmd(conn
, TASK_INTERRUPTIBLE
);
3999 ret
= iscsit_handle_logout_cmd(conn
, cmd
, buf
);
4001 wait_for_completion_timeout(&conn
->conn_logout_comp
,
4002 SECONDS_FOR_LOGOUT_COMP
* HZ
);
4004 case ISCSI_OP_SNACK
:
4005 ret
= iscsit_handle_snack(conn
, buf
);
4008 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr
->opcode
);
4009 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
4010 pr_err("Cannot recover from unknown"
4011 " opcode while ERL=0, closing iSCSI connection.\n");
4014 if (!conn
->conn_ops
->OFMarker
) {
4015 pr_err("Unable to recover from unknown"
4016 " opcode while OFMarker=No, closing iSCSI"
4020 if (iscsit_recover_from_unknown_opcode(conn
) < 0) {
4021 pr_err("Unable to recover from unknown"
4022 " opcode, closing iSCSI connection.\n");
4030 return iscsit_add_reject(conn
, ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
4033 int iscsi_target_rx_thread(void *arg
)
4036 u8 buffer
[ISCSI_HDR_LEN
], opcode
;
4037 u32 checksum
= 0, digest
= 0;
4038 struct iscsi_conn
*conn
= NULL
;
4039 struct iscsi_thread_set
*ts
= arg
;
4042 * Allow ourselves to be interrupted by SIGINT so that a
4043 * connection recovery / failure event can be triggered externally.
4045 allow_signal(SIGINT
);
4048 conn
= iscsi_rx_thread_pre_handler(ts
);
4052 if (conn
->conn_transport
->transport_type
== ISCSI_INFINIBAND
) {
4053 struct completion comp
;
4056 init_completion(&comp
);
4057 rc
= wait_for_completion_interruptible(&comp
);
4064 while (!kthread_should_stop()) {
4066 * Ensure that both TX and RX per connection kthreads
4067 * are scheduled to run on the same CPU.
4069 iscsit_thread_check_cpumask(conn
, current
, 0);
4071 memset(buffer
, 0, ISCSI_HDR_LEN
);
4072 memset(&iov
, 0, sizeof(struct kvec
));
4074 iov
.iov_base
= buffer
;
4075 iov
.iov_len
= ISCSI_HDR_LEN
;
4077 ret
= rx_data(conn
, &iov
, 1, ISCSI_HDR_LEN
);
4078 if (ret
!= ISCSI_HDR_LEN
) {
4079 iscsit_rx_thread_wait_for_tcp(conn
);
4083 if (conn
->conn_ops
->HeaderDigest
) {
4084 iov
.iov_base
= &digest
;
4085 iov
.iov_len
= ISCSI_CRC_LEN
;
4087 ret
= rx_data(conn
, &iov
, 1, ISCSI_CRC_LEN
);
4088 if (ret
!= ISCSI_CRC_LEN
) {
4089 iscsit_rx_thread_wait_for_tcp(conn
);
4093 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
4094 buffer
, ISCSI_HDR_LEN
,
4095 0, NULL
, (u8
*)&checksum
);
4097 if (digest
!= checksum
) {
4098 pr_err("HeaderDigest CRC32C failed,"
4099 " received 0x%08x, computed 0x%08x\n",
4102 * Set the PDU to 0xff so it will intentionally
4103 * hit default in the switch below.
4105 memset(buffer
, 0xff, ISCSI_HDR_LEN
);
4106 atomic_long_inc(&conn
->sess
->conn_digest_errors
);
4108 pr_debug("Got HeaderDigest CRC32C"
4109 " 0x%08x\n", checksum
);
4113 if (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
)
4116 opcode
= buffer
[0] & ISCSI_OPCODE_MASK
;
4118 if (conn
->sess
->sess_ops
->SessionType
&&
4119 ((!(opcode
& ISCSI_OP_TEXT
)) ||
4120 (!(opcode
& ISCSI_OP_LOGOUT
)))) {
4121 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4122 " while in Discovery Session, rejecting.\n", opcode
);
4123 iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
4128 ret
= iscsi_target_rx_opcode(conn
, buffer
);
4134 if (!signal_pending(current
))
4135 atomic_set(&conn
->transport_failed
, 1);
4136 iscsit_take_action_for_connection_exit(conn
);
4142 static void iscsit_release_commands_from_conn(struct iscsi_conn
*conn
)
4144 struct iscsi_cmd
*cmd
= NULL
, *cmd_tmp
= NULL
;
4145 struct iscsi_session
*sess
= conn
->sess
;
4147 * We expect this function to only ever be called from either RX or TX
4148 * thread context via iscsit_close_connection() once the other context
4149 * has been reset -> returned sleeping pre-handler state.
4151 spin_lock_bh(&conn
->cmd_lock
);
4152 list_for_each_entry_safe(cmd
, cmd_tmp
, &conn
->conn_cmd_list
, i_conn_node
) {
4154 list_del(&cmd
->i_conn_node
);
4155 spin_unlock_bh(&conn
->cmd_lock
);
4157 iscsit_increment_maxcmdsn(cmd
, sess
);
4159 iscsit_free_cmd(cmd
, true);
4161 spin_lock_bh(&conn
->cmd_lock
);
4163 spin_unlock_bh(&conn
->cmd_lock
);
4166 static void iscsit_stop_timers_for_cmds(
4167 struct iscsi_conn
*conn
)
4169 struct iscsi_cmd
*cmd
;
4171 spin_lock_bh(&conn
->cmd_lock
);
4172 list_for_each_entry(cmd
, &conn
->conn_cmd_list
, i_conn_node
) {
4173 if (cmd
->data_direction
== DMA_TO_DEVICE
)
4174 iscsit_stop_dataout_timer(cmd
);
4176 spin_unlock_bh(&conn
->cmd_lock
);
4179 int iscsit_close_connection(
4180 struct iscsi_conn
*conn
)
4182 int conn_logout
= (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
);
4183 struct iscsi_session
*sess
= conn
->sess
;
4185 pr_debug("Closing iSCSI connection CID %hu on SID:"
4186 " %u\n", conn
->cid
, sess
->sid
);
4188 * Always up conn_logout_comp just in case the RX Thread is sleeping
4189 * and the logout response never got sent because the connection
4192 complete(&conn
->conn_logout_comp
);
4194 iscsi_release_thread_set(conn
);
4196 iscsit_stop_timers_for_cmds(conn
);
4197 iscsit_stop_nopin_response_timer(conn
);
4198 iscsit_stop_nopin_timer(conn
);
4199 iscsit_free_queue_reqs_for_conn(conn
);
4202 * During Connection recovery drop unacknowledged out of order
4203 * commands for this connection, and prepare the other commands
4206 * During normal operation clear the out of order commands (but
4207 * do not free the struct iscsi_ooo_cmdsn's) and release all
4208 * struct iscsi_cmds.
4210 if (atomic_read(&conn
->connection_recovery
)) {
4211 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn
);
4212 iscsit_prepare_cmds_for_realligance(conn
);
4214 iscsit_clear_ooo_cmdsns_for_conn(conn
);
4215 iscsit_release_commands_from_conn(conn
);
4219 * Handle decrementing session or connection usage count if
4220 * a logout response was not able to be sent because the
4221 * connection failed. Fall back to Session Recovery here.
4223 if (atomic_read(&conn
->conn_logout_remove
)) {
4224 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) {
4225 iscsit_dec_conn_usage_count(conn
);
4226 iscsit_dec_session_usage_count(sess
);
4228 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
)
4229 iscsit_dec_conn_usage_count(conn
);
4231 atomic_set(&conn
->conn_logout_remove
, 0);
4232 atomic_set(&sess
->session_reinstatement
, 0);
4233 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4236 spin_lock_bh(&sess
->conn_lock
);
4237 list_del(&conn
->conn_list
);
4240 * Attempt to let the Initiator know this connection failed by
4241 * sending an Connection Dropped Async Message on another
4242 * active connection.
4244 if (atomic_read(&conn
->connection_recovery
))
4245 iscsit_build_conn_drop_async_message(conn
);
4247 spin_unlock_bh(&sess
->conn_lock
);
4250 * If connection reinstatement is being performed on this connection,
4251 * up the connection reinstatement semaphore that is being blocked on
4252 * in iscsit_cause_connection_reinstatement().
4254 spin_lock_bh(&conn
->state_lock
);
4255 if (atomic_read(&conn
->sleep_on_conn_wait_comp
)) {
4256 spin_unlock_bh(&conn
->state_lock
);
4257 complete(&conn
->conn_wait_comp
);
4258 wait_for_completion(&conn
->conn_post_wait_comp
);
4259 spin_lock_bh(&conn
->state_lock
);
4263 * If connection reinstatement is being performed on this connection
4264 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4265 * connection wait rcfr semaphore that is being blocked on
4266 * an iscsit_connection_reinstatement_rcfr().
4268 if (atomic_read(&conn
->connection_wait_rcfr
)) {
4269 spin_unlock_bh(&conn
->state_lock
);
4270 complete(&conn
->conn_wait_rcfr_comp
);
4271 wait_for_completion(&conn
->conn_post_wait_comp
);
4272 spin_lock_bh(&conn
->state_lock
);
4274 atomic_set(&conn
->connection_reinstatement
, 1);
4275 spin_unlock_bh(&conn
->state_lock
);
4278 * If any other processes are accessing this connection pointer we
4279 * must wait until they have completed.
4281 iscsit_check_conn_usage_count(conn
);
4283 if (conn
->conn_rx_hash
.tfm
)
4284 crypto_free_hash(conn
->conn_rx_hash
.tfm
);
4285 if (conn
->conn_tx_hash
.tfm
)
4286 crypto_free_hash(conn
->conn_tx_hash
.tfm
);
4288 if (conn
->conn_cpumask
)
4289 free_cpumask_var(conn
->conn_cpumask
);
4291 kfree(conn
->conn_ops
);
4292 conn
->conn_ops
= NULL
;
4295 sock_release(conn
->sock
);
4297 if (conn
->conn_transport
->iscsit_free_conn
)
4298 conn
->conn_transport
->iscsit_free_conn(conn
);
4300 iscsit_put_transport(conn
->conn_transport
);
4302 conn
->thread_set
= NULL
;
4304 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4305 conn
->conn_state
= TARG_CONN_STATE_FREE
;
4308 spin_lock_bh(&sess
->conn_lock
);
4309 atomic_dec(&sess
->nconn
);
4310 pr_debug("Decremented iSCSI connection count to %hu from node:"
4311 " %s\n", atomic_read(&sess
->nconn
),
4312 sess
->sess_ops
->InitiatorName
);
4314 * Make sure that if one connection fails in an non ERL=2 iSCSI
4315 * Session that they all fail.
4317 if ((sess
->sess_ops
->ErrorRecoveryLevel
!= 2) && !conn_logout
&&
4318 !atomic_read(&sess
->session_logout
))
4319 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4322 * If this was not the last connection in the session, and we are
4323 * performing session reinstatement or falling back to ERL=0, call
4324 * iscsit_stop_session() without sleeping to shutdown the other
4325 * active connections.
4327 if (atomic_read(&sess
->nconn
)) {
4328 if (!atomic_read(&sess
->session_reinstatement
) &&
4329 !atomic_read(&sess
->session_fall_back_to_erl0
)) {
4330 spin_unlock_bh(&sess
->conn_lock
);
4333 if (!atomic_read(&sess
->session_stop_active
)) {
4334 atomic_set(&sess
->session_stop_active
, 1);
4335 spin_unlock_bh(&sess
->conn_lock
);
4336 iscsit_stop_session(sess
, 0, 0);
4339 spin_unlock_bh(&sess
->conn_lock
);
4344 * If this was the last connection in the session and one of the
4345 * following is occurring:
4347 * Session Reinstatement is not being performed, and are falling back
4348 * to ERL=0 call iscsit_close_session().
4350 * Session Logout was requested. iscsit_close_session() will be called
4353 * Session Continuation is not being performed, start the Time2Retain
4354 * handler and check if sleep_on_sess_wait_sem is active.
4356 if (!atomic_read(&sess
->session_reinstatement
) &&
4357 atomic_read(&sess
->session_fall_back_to_erl0
)) {
4358 spin_unlock_bh(&sess
->conn_lock
);
4359 target_put_session(sess
->se_sess
);
4362 } else if (atomic_read(&sess
->session_logout
)) {
4363 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4364 sess
->session_state
= TARG_SESS_STATE_FREE
;
4365 spin_unlock_bh(&sess
->conn_lock
);
4367 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4368 complete(&sess
->session_wait_comp
);
4372 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4373 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4375 if (!atomic_read(&sess
->session_continuation
)) {
4376 spin_unlock_bh(&sess
->conn_lock
);
4377 iscsit_start_time2retain_handler(sess
);
4379 spin_unlock_bh(&sess
->conn_lock
);
4381 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4382 complete(&sess
->session_wait_comp
);
4386 spin_unlock_bh(&sess
->conn_lock
);
4391 int iscsit_close_session(struct iscsi_session
*sess
)
4393 struct iscsi_portal_group
*tpg
= sess
->tpg
;
4394 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4396 if (atomic_read(&sess
->nconn
)) {
4397 pr_err("%d connection(s) still exist for iSCSI session"
4398 " to %s\n", atomic_read(&sess
->nconn
),
4399 sess
->sess_ops
->InitiatorName
);
4403 spin_lock_bh(&se_tpg
->session_lock
);
4404 atomic_set(&sess
->session_logout
, 1);
4405 atomic_set(&sess
->session_reinstatement
, 1);
4406 iscsit_stop_time2retain_timer(sess
);
4407 spin_unlock_bh(&se_tpg
->session_lock
);
4410 * transport_deregister_session_configfs() will clear the
4411 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4412 * can be setting it again with __transport_register_session() in
4413 * iscsi_post_login_handler() again after the iscsit_stop_session()
4414 * completes in iscsi_np context.
4416 transport_deregister_session_configfs(sess
->se_sess
);
4419 * If any other processes are accessing this session pointer we must
4420 * wait until they have completed. If we are in an interrupt (the
4421 * time2retain handler) and contain and active session usage count we
4422 * restart the timer and exit.
4424 if (!in_interrupt()) {
4425 if (iscsit_check_session_usage_count(sess
) == 1)
4426 iscsit_stop_session(sess
, 1, 1);
4428 if (iscsit_check_session_usage_count(sess
) == 2) {
4429 atomic_set(&sess
->session_logout
, 0);
4430 iscsit_start_time2retain_handler(sess
);
4435 transport_deregister_session(sess
->se_sess
);
4437 if (sess
->sess_ops
->ErrorRecoveryLevel
== 2)
4438 iscsit_free_connection_recovery_entires(sess
);
4440 iscsit_free_all_ooo_cmdsns(sess
);
4442 spin_lock_bh(&se_tpg
->session_lock
);
4443 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4444 sess
->session_state
= TARG_SESS_STATE_FREE
;
4445 pr_debug("Released iSCSI session from node: %s\n",
4446 sess
->sess_ops
->InitiatorName
);
4449 tpg
->tpg_tiqn
->tiqn_nsessions
--;
4451 pr_debug("Decremented number of active iSCSI Sessions on"
4452 " iSCSI TPG: %hu to %u\n", tpg
->tpgt
, tpg
->nsessions
);
4454 spin_lock(&sess_idr_lock
);
4455 idr_remove(&sess_idr
, sess
->session_index
);
4456 spin_unlock(&sess_idr_lock
);
4458 kfree(sess
->sess_ops
);
4459 sess
->sess_ops
= NULL
;
4460 spin_unlock_bh(&se_tpg
->session_lock
);
4466 static void iscsit_logout_post_handler_closesession(
4467 struct iscsi_conn
*conn
)
4469 struct iscsi_session
*sess
= conn
->sess
;
4471 iscsi_set_thread_clear(conn
, ISCSI_CLEAR_TX_THREAD
);
4472 iscsi_set_thread_set_signal(conn
, ISCSI_SIGNAL_TX_THREAD
);
4474 atomic_set(&conn
->conn_logout_remove
, 0);
4475 complete(&conn
->conn_logout_comp
);
4477 iscsit_dec_conn_usage_count(conn
);
4478 iscsit_stop_session(sess
, 1, 1);
4479 iscsit_dec_session_usage_count(sess
);
4480 target_put_session(sess
->se_sess
);
4483 static void iscsit_logout_post_handler_samecid(
4484 struct iscsi_conn
*conn
)
4486 iscsi_set_thread_clear(conn
, ISCSI_CLEAR_TX_THREAD
);
4487 iscsi_set_thread_set_signal(conn
, ISCSI_SIGNAL_TX_THREAD
);
4489 atomic_set(&conn
->conn_logout_remove
, 0);
4490 complete(&conn
->conn_logout_comp
);
4492 iscsit_cause_connection_reinstatement(conn
, 1);
4493 iscsit_dec_conn_usage_count(conn
);
4496 static void iscsit_logout_post_handler_diffcid(
4497 struct iscsi_conn
*conn
,
4500 struct iscsi_conn
*l_conn
;
4501 struct iscsi_session
*sess
= conn
->sess
;
4506 spin_lock_bh(&sess
->conn_lock
);
4507 list_for_each_entry(l_conn
, &sess
->sess_conn_list
, conn_list
) {
4508 if (l_conn
->cid
== cid
) {
4509 iscsit_inc_conn_usage_count(l_conn
);
4513 spin_unlock_bh(&sess
->conn_lock
);
4519 l_conn
->sock
->ops
->shutdown(l_conn
->sock
, RCV_SHUTDOWN
);
4521 spin_lock_bh(&l_conn
->state_lock
);
4522 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4523 l_conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
4524 spin_unlock_bh(&l_conn
->state_lock
);
4526 iscsit_cause_connection_reinstatement(l_conn
, 1);
4527 iscsit_dec_conn_usage_count(l_conn
);
4531 * Return of 0 causes the TX thread to restart.
4533 int iscsit_logout_post_handler(
4534 struct iscsi_cmd
*cmd
,
4535 struct iscsi_conn
*conn
)
4539 switch (cmd
->logout_reason
) {
4540 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
4541 switch (cmd
->logout_response
) {
4542 case ISCSI_LOGOUT_SUCCESS
:
4543 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4545 iscsit_logout_post_handler_closesession(conn
);
4550 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
4551 if (conn
->cid
== cmd
->logout_cid
) {
4552 switch (cmd
->logout_response
) {
4553 case ISCSI_LOGOUT_SUCCESS
:
4554 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4556 iscsit_logout_post_handler_samecid(conn
);
4561 switch (cmd
->logout_response
) {
4562 case ISCSI_LOGOUT_SUCCESS
:
4563 iscsit_logout_post_handler_diffcid(conn
,
4566 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4567 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4574 case ISCSI_LOGOUT_REASON_RECOVERY
:
4575 switch (cmd
->logout_response
) {
4576 case ISCSI_LOGOUT_SUCCESS
:
4577 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4578 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
:
4579 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4591 EXPORT_SYMBOL(iscsit_logout_post_handler
);
4593 void iscsit_fail_session(struct iscsi_session
*sess
)
4595 struct iscsi_conn
*conn
;
4597 spin_lock_bh(&sess
->conn_lock
);
4598 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
) {
4599 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4600 conn
->conn_state
= TARG_CONN_STATE_CLEANUP_WAIT
;
4602 spin_unlock_bh(&sess
->conn_lock
);
4604 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4605 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4608 int iscsit_free_session(struct iscsi_session
*sess
)
4610 u16 conn_count
= atomic_read(&sess
->nconn
);
4611 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4614 spin_lock_bh(&sess
->conn_lock
);
4615 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4617 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4619 if (conn_count
== 0)
4622 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4625 iscsit_inc_conn_usage_count(conn_tmp
);
4628 iscsit_inc_conn_usage_count(conn
);
4630 spin_unlock_bh(&sess
->conn_lock
);
4631 iscsit_cause_connection_reinstatement(conn
, 1);
4632 spin_lock_bh(&sess
->conn_lock
);
4634 iscsit_dec_conn_usage_count(conn
);
4636 iscsit_dec_conn_usage_count(conn_tmp
);
4641 if (atomic_read(&sess
->nconn
)) {
4642 spin_unlock_bh(&sess
->conn_lock
);
4643 wait_for_completion(&sess
->session_wait_comp
);
4645 spin_unlock_bh(&sess
->conn_lock
);
4647 target_put_session(sess
->se_sess
);
4651 void iscsit_stop_session(
4652 struct iscsi_session
*sess
,
4654 int connection_sleep
)
4656 u16 conn_count
= atomic_read(&sess
->nconn
);
4657 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4660 spin_lock_bh(&sess
->conn_lock
);
4662 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4664 if (connection_sleep
) {
4665 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4667 if (conn_count
== 0)
4670 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4673 iscsit_inc_conn_usage_count(conn_tmp
);
4676 iscsit_inc_conn_usage_count(conn
);
4678 spin_unlock_bh(&sess
->conn_lock
);
4679 iscsit_cause_connection_reinstatement(conn
, 1);
4680 spin_lock_bh(&sess
->conn_lock
);
4682 iscsit_dec_conn_usage_count(conn
);
4684 iscsit_dec_conn_usage_count(conn_tmp
);
4688 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
)
4689 iscsit_cause_connection_reinstatement(conn
, 0);
4692 if (session_sleep
&& atomic_read(&sess
->nconn
)) {
4693 spin_unlock_bh(&sess
->conn_lock
);
4694 wait_for_completion(&sess
->session_wait_comp
);
4696 spin_unlock_bh(&sess
->conn_lock
);
4699 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group
*tpg
, int force
)
4701 struct iscsi_session
*sess
;
4702 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4703 struct se_session
*se_sess
, *se_sess_tmp
;
4704 int session_count
= 0;
4706 spin_lock_bh(&se_tpg
->session_lock
);
4707 if (tpg
->nsessions
&& !force
) {
4708 spin_unlock_bh(&se_tpg
->session_lock
);
4712 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &se_tpg
->tpg_sess_list
,
4714 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4716 spin_lock(&sess
->conn_lock
);
4717 if (atomic_read(&sess
->session_fall_back_to_erl0
) ||
4718 atomic_read(&sess
->session_logout
) ||
4719 (sess
->time2retain_timer_flags
& ISCSI_TF_EXPIRED
)) {
4720 spin_unlock(&sess
->conn_lock
);
4723 atomic_set(&sess
->session_reinstatement
, 1);
4724 spin_unlock(&sess
->conn_lock
);
4725 spin_unlock_bh(&se_tpg
->session_lock
);
4727 iscsit_free_session(sess
);
4728 spin_lock_bh(&se_tpg
->session_lock
);
4732 spin_unlock_bh(&se_tpg
->session_lock
);
4734 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4735 " Group: %hu\n", session_count
, tpg
->tpgt
);
4739 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4740 MODULE_VERSION("4.1.x");
4741 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4742 MODULE_LICENSE("GPL");
4744 module_init(iscsi_target_init_module
);
4745 module_exit(iscsi_target_cleanup_module
);