1 /*******************************************************************************
2 * This file contains main functions related to the iSCSI Target Core Driver.
4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 ******************************************************************************/
21 #include <linux/string.h>
22 #include <linux/kthread.h>
23 #include <linux/crypto.h>
24 #include <linux/completion.h>
25 #include <linux/module.h>
26 #include <linux/idr.h>
27 #include <asm/unaligned.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/iscsi_proto.h>
30 #include <scsi/scsi_tcq.h>
31 #include <target/target_core_base.h>
32 #include <target/target_core_fabric.h>
33 #include <target/target_core_configfs.h>
35 #include "iscsi_target_core.h"
36 #include "iscsi_target_parameters.h"
37 #include "iscsi_target_seq_pdu_list.h"
38 #include "iscsi_target_tq.h"
39 #include "iscsi_target_configfs.h"
40 #include "iscsi_target_datain_values.h"
41 #include "iscsi_target_erl0.h"
42 #include "iscsi_target_erl1.h"
43 #include "iscsi_target_erl2.h"
44 #include "iscsi_target_login.h"
45 #include "iscsi_target_tmr.h"
46 #include "iscsi_target_tpg.h"
47 #include "iscsi_target_util.h"
48 #include "iscsi_target.h"
49 #include "iscsi_target_device.h"
50 #include "iscsi_target_stat.h"
52 #include <target/iscsi/iscsi_transport.h>
54 static LIST_HEAD(g_tiqn_list
);
55 static LIST_HEAD(g_np_list
);
56 static DEFINE_SPINLOCK(tiqn_lock
);
57 static DEFINE_SPINLOCK(np_lock
);
59 static struct idr tiqn_idr
;
61 struct mutex auth_id_lock
;
62 spinlock_t sess_idr_lock
;
64 struct iscsit_global
*iscsit_global
;
66 struct kmem_cache
*lio_cmd_cache
;
67 struct kmem_cache
*lio_qr_cache
;
68 struct kmem_cache
*lio_dr_cache
;
69 struct kmem_cache
*lio_ooo_cache
;
70 struct kmem_cache
*lio_r2t_cache
;
72 static int iscsit_handle_immediate_data(struct iscsi_cmd
*,
73 struct iscsi_scsi_req
*, u32
);
75 struct iscsi_tiqn
*iscsit_get_tiqn_for_login(unsigned char *buf
)
77 struct iscsi_tiqn
*tiqn
= NULL
;
79 spin_lock(&tiqn_lock
);
80 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
81 if (!strcmp(tiqn
->tiqn
, buf
)) {
83 spin_lock(&tiqn
->tiqn_state_lock
);
84 if (tiqn
->tiqn_state
== TIQN_STATE_ACTIVE
) {
85 tiqn
->tiqn_access_count
++;
86 spin_unlock(&tiqn
->tiqn_state_lock
);
87 spin_unlock(&tiqn_lock
);
90 spin_unlock(&tiqn
->tiqn_state_lock
);
93 spin_unlock(&tiqn_lock
);
98 static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn
*tiqn
)
100 spin_lock(&tiqn
->tiqn_state_lock
);
101 if (tiqn
->tiqn_state
== TIQN_STATE_ACTIVE
) {
102 tiqn
->tiqn_state
= TIQN_STATE_SHUTDOWN
;
103 spin_unlock(&tiqn
->tiqn_state_lock
);
106 spin_unlock(&tiqn
->tiqn_state_lock
);
111 void iscsit_put_tiqn_for_login(struct iscsi_tiqn
*tiqn
)
113 spin_lock(&tiqn
->tiqn_state_lock
);
114 tiqn
->tiqn_access_count
--;
115 spin_unlock(&tiqn
->tiqn_state_lock
);
119 * Note that IQN formatting is expected to be done in userspace, and
120 * no explict IQN format checks are done here.
122 struct iscsi_tiqn
*iscsit_add_tiqn(unsigned char *buf
)
124 struct iscsi_tiqn
*tiqn
= NULL
;
127 if (strlen(buf
) >= ISCSI_IQN_LEN
) {
128 pr_err("Target IQN exceeds %d bytes\n",
130 return ERR_PTR(-EINVAL
);
133 tiqn
= kzalloc(sizeof(struct iscsi_tiqn
), GFP_KERNEL
);
135 pr_err("Unable to allocate struct iscsi_tiqn\n");
136 return ERR_PTR(-ENOMEM
);
139 sprintf(tiqn
->tiqn
, "%s", buf
);
140 INIT_LIST_HEAD(&tiqn
->tiqn_list
);
141 INIT_LIST_HEAD(&tiqn
->tiqn_tpg_list
);
142 spin_lock_init(&tiqn
->tiqn_state_lock
);
143 spin_lock_init(&tiqn
->tiqn_tpg_lock
);
144 spin_lock_init(&tiqn
->sess_err_stats
.lock
);
145 spin_lock_init(&tiqn
->login_stats
.lock
);
146 spin_lock_init(&tiqn
->logout_stats
.lock
);
148 tiqn
->tiqn_state
= TIQN_STATE_ACTIVE
;
150 idr_preload(GFP_KERNEL
);
151 spin_lock(&tiqn_lock
);
153 ret
= idr_alloc(&tiqn_idr
, NULL
, 0, 0, GFP_NOWAIT
);
155 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
156 spin_unlock(&tiqn_lock
);
161 tiqn
->tiqn_index
= ret
;
162 list_add_tail(&tiqn
->tiqn_list
, &g_tiqn_list
);
164 spin_unlock(&tiqn_lock
);
167 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn
->tiqn
);
173 static void iscsit_wait_for_tiqn(struct iscsi_tiqn
*tiqn
)
176 * Wait for accesses to said struct iscsi_tiqn to end.
178 spin_lock(&tiqn
->tiqn_state_lock
);
179 while (tiqn
->tiqn_access_count
!= 0) {
180 spin_unlock(&tiqn
->tiqn_state_lock
);
182 spin_lock(&tiqn
->tiqn_state_lock
);
184 spin_unlock(&tiqn
->tiqn_state_lock
);
187 void iscsit_del_tiqn(struct iscsi_tiqn
*tiqn
)
190 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
191 * while holding tiqn->tiqn_state_lock. This means that all subsequent
192 * attempts to access this struct iscsi_tiqn will fail from both transport
193 * fabric and control code paths.
195 if (iscsit_set_tiqn_shutdown(tiqn
) < 0) {
196 pr_err("iscsit_set_tiqn_shutdown() failed\n");
200 iscsit_wait_for_tiqn(tiqn
);
202 spin_lock(&tiqn_lock
);
203 list_del(&tiqn
->tiqn_list
);
204 idr_remove(&tiqn_idr
, tiqn
->tiqn_index
);
205 spin_unlock(&tiqn_lock
);
207 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
212 int iscsit_access_np(struct iscsi_np
*np
, struct iscsi_portal_group
*tpg
)
216 * Determine if the network portal is accepting storage traffic.
218 spin_lock_bh(&np
->np_thread_lock
);
219 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
220 spin_unlock_bh(&np
->np_thread_lock
);
223 if (np
->np_login_tpg
) {
224 pr_err("np->np_login_tpg() is not NULL!\n");
225 spin_unlock_bh(&np
->np_thread_lock
);
228 spin_unlock_bh(&np
->np_thread_lock
);
230 * Determine if the portal group is accepting storage traffic.
232 spin_lock_bh(&tpg
->tpg_state_lock
);
233 if (tpg
->tpg_state
!= TPG_STATE_ACTIVE
) {
234 spin_unlock_bh(&tpg
->tpg_state_lock
);
237 spin_unlock_bh(&tpg
->tpg_state_lock
);
240 * Here we serialize access across the TIQN+TPG Tuple.
242 ret
= mutex_lock_interruptible(&tpg
->np_login_lock
);
243 if ((ret
!= 0) || signal_pending(current
))
246 spin_lock_bh(&np
->np_thread_lock
);
247 np
->np_login_tpg
= tpg
;
248 spin_unlock_bh(&np
->np_thread_lock
);
253 int iscsit_deaccess_np(struct iscsi_np
*np
, struct iscsi_portal_group
*tpg
)
255 struct iscsi_tiqn
*tiqn
= tpg
->tpg_tiqn
;
257 spin_lock_bh(&np
->np_thread_lock
);
258 np
->np_login_tpg
= NULL
;
259 spin_unlock_bh(&np
->np_thread_lock
);
261 mutex_unlock(&tpg
->np_login_lock
);
264 iscsit_put_tiqn_for_login(tiqn
);
269 bool iscsit_check_np_match(
270 struct __kernel_sockaddr_storage
*sockaddr
,
272 int network_transport
)
274 struct sockaddr_in
*sock_in
, *sock_in_e
;
275 struct sockaddr_in6
*sock_in6
, *sock_in6_e
;
276 bool ip_match
= false;
279 if (sockaddr
->ss_family
== AF_INET6
) {
280 sock_in6
= (struct sockaddr_in6
*)sockaddr
;
281 sock_in6_e
= (struct sockaddr_in6
*)&np
->np_sockaddr
;
283 if (!memcmp(&sock_in6
->sin6_addr
.in6_u
,
284 &sock_in6_e
->sin6_addr
.in6_u
,
285 sizeof(struct in6_addr
)))
288 port
= ntohs(sock_in6
->sin6_port
);
290 sock_in
= (struct sockaddr_in
*)sockaddr
;
291 sock_in_e
= (struct sockaddr_in
*)&np
->np_sockaddr
;
293 if (sock_in
->sin_addr
.s_addr
== sock_in_e
->sin_addr
.s_addr
)
296 port
= ntohs(sock_in
->sin_port
);
299 if ((ip_match
== true) && (np
->np_port
== port
) &&
300 (np
->np_network_transport
== network_transport
))
306 static struct iscsi_np
*iscsit_get_np(
307 struct __kernel_sockaddr_storage
*sockaddr
,
308 int network_transport
)
313 spin_lock_bh(&np_lock
);
314 list_for_each_entry(np
, &g_np_list
, np_list
) {
315 spin_lock(&np
->np_thread_lock
);
316 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
317 spin_unlock(&np
->np_thread_lock
);
321 match
= iscsit_check_np_match(sockaddr
, np
, network_transport
);
324 * Increment the np_exports reference count now to
325 * prevent iscsit_del_np() below from being called
326 * while iscsi_tpg_add_network_portal() is called.
329 spin_unlock(&np
->np_thread_lock
);
330 spin_unlock_bh(&np_lock
);
333 spin_unlock(&np
->np_thread_lock
);
335 spin_unlock_bh(&np_lock
);
340 struct iscsi_np
*iscsit_add_np(
341 struct __kernel_sockaddr_storage
*sockaddr
,
343 int network_transport
)
345 struct sockaddr_in
*sock_in
;
346 struct sockaddr_in6
*sock_in6
;
350 * Locate the existing struct iscsi_np if already active..
352 np
= iscsit_get_np(sockaddr
, network_transport
);
356 np
= kzalloc(sizeof(struct iscsi_np
), GFP_KERNEL
);
358 pr_err("Unable to allocate memory for struct iscsi_np\n");
359 return ERR_PTR(-ENOMEM
);
362 np
->np_flags
|= NPF_IP_NETWORK
;
363 if (sockaddr
->ss_family
== AF_INET6
) {
364 sock_in6
= (struct sockaddr_in6
*)sockaddr
;
365 snprintf(np
->np_ip
, IPV6_ADDRESS_SPACE
, "%s", ip_str
);
366 np
->np_port
= ntohs(sock_in6
->sin6_port
);
368 sock_in
= (struct sockaddr_in
*)sockaddr
;
369 sprintf(np
->np_ip
, "%s", ip_str
);
370 np
->np_port
= ntohs(sock_in
->sin_port
);
373 np
->np_network_transport
= network_transport
;
374 spin_lock_init(&np
->np_thread_lock
);
375 init_completion(&np
->np_restart_comp
);
376 INIT_LIST_HEAD(&np
->np_list
);
378 ret
= iscsi_target_setup_login_socket(np
, sockaddr
);
384 np
->np_thread
= kthread_run(iscsi_target_login_thread
, np
, "iscsi_np");
385 if (IS_ERR(np
->np_thread
)) {
386 pr_err("Unable to create kthread: iscsi_np\n");
387 ret
= PTR_ERR(np
->np_thread
);
392 * Increment the np_exports reference count now to prevent
393 * iscsit_del_np() below from being run while a new call to
394 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
395 * active. We don't need to hold np->np_thread_lock at this
396 * point because iscsi_np has not been added to g_np_list yet.
400 spin_lock_bh(&np_lock
);
401 list_add_tail(&np
->np_list
, &g_np_list
);
402 spin_unlock_bh(&np_lock
);
404 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
405 np
->np_ip
, np
->np_port
, np
->np_transport
->name
);
410 int iscsit_reset_np_thread(
412 struct iscsi_tpg_np
*tpg_np
,
413 struct iscsi_portal_group
*tpg
)
415 spin_lock_bh(&np
->np_thread_lock
);
418 * The reset operation need only be performed when the
419 * passed struct iscsi_portal_group has a login in progress
420 * to one of the network portals.
422 if (tpg_np
->tpg_np
->np_login_tpg
!= tpg
) {
423 spin_unlock_bh(&np
->np_thread_lock
);
427 if (np
->np_thread_state
== ISCSI_NP_THREAD_INACTIVE
) {
428 spin_unlock_bh(&np
->np_thread_lock
);
431 np
->np_thread_state
= ISCSI_NP_THREAD_RESET
;
434 spin_unlock_bh(&np
->np_thread_lock
);
435 send_sig(SIGINT
, np
->np_thread
, 1);
436 wait_for_completion(&np
->np_restart_comp
);
437 spin_lock_bh(&np
->np_thread_lock
);
439 spin_unlock_bh(&np
->np_thread_lock
);
444 static void iscsit_free_np(struct iscsi_np
*np
)
447 sock_release(np
->np_socket
);
450 int iscsit_del_np(struct iscsi_np
*np
)
452 spin_lock_bh(&np
->np_thread_lock
);
454 if (np
->np_exports
) {
455 spin_unlock_bh(&np
->np_thread_lock
);
458 np
->np_thread_state
= ISCSI_NP_THREAD_SHUTDOWN
;
459 spin_unlock_bh(&np
->np_thread_lock
);
463 * We need to send the signal to wakeup Linux/Net
464 * which may be sleeping in sock_accept()..
466 send_sig(SIGINT
, np
->np_thread
, 1);
467 kthread_stop(np
->np_thread
);
470 np
->np_transport
->iscsit_free_np(np
);
472 spin_lock_bh(&np_lock
);
473 list_del(&np
->np_list
);
474 spin_unlock_bh(&np_lock
);
476 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
477 np
->np_ip
, np
->np_port
, np
->np_transport
->name
);
479 iscsit_put_transport(np
->np_transport
);
484 static int iscsit_immediate_queue(struct iscsi_conn
*, struct iscsi_cmd
*, int);
485 static int iscsit_response_queue(struct iscsi_conn
*, struct iscsi_cmd
*, int);
487 static int iscsit_queue_rsp(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
)
489 iscsit_add_cmd_to_response_queue(cmd
, cmd
->conn
, cmd
->i_state
);
493 static struct iscsit_transport iscsi_target_transport
= {
495 .transport_type
= ISCSI_TCP
,
497 .iscsit_setup_np
= iscsit_setup_np
,
498 .iscsit_accept_np
= iscsit_accept_np
,
499 .iscsit_free_np
= iscsit_free_np
,
500 .iscsit_alloc_cmd
= iscsit_alloc_cmd
,
501 .iscsit_get_login_rx
= iscsit_get_login_rx
,
502 .iscsit_put_login_tx
= iscsit_put_login_tx
,
503 .iscsit_get_dataout
= iscsit_build_r2ts_for_cmd
,
504 .iscsit_immediate_queue
= iscsit_immediate_queue
,
505 .iscsit_response_queue
= iscsit_response_queue
,
506 .iscsit_queue_data_in
= iscsit_queue_rsp
,
507 .iscsit_queue_status
= iscsit_queue_rsp
,
510 static int __init
iscsi_target_init_module(void)
514 pr_debug("iSCSI-Target "ISCSIT_VERSION
"\n");
516 iscsit_global
= kzalloc(sizeof(struct iscsit_global
), GFP_KERNEL
);
517 if (!iscsit_global
) {
518 pr_err("Unable to allocate memory for iscsit_global\n");
521 mutex_init(&auth_id_lock
);
522 spin_lock_init(&sess_idr_lock
);
526 ret
= iscsi_target_register_configfs();
530 ret
= iscsi_thread_set_init();
534 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT
) !=
535 TARGET_THREAD_SET_COUNT
) {
536 pr_err("iscsi_allocate_thread_sets() returned"
537 " unexpected value!\n");
541 lio_cmd_cache
= kmem_cache_create("lio_cmd_cache",
542 sizeof(struct iscsi_cmd
), __alignof__(struct iscsi_cmd
),
544 if (!lio_cmd_cache
) {
545 pr_err("Unable to kmem_cache_create() for"
550 lio_qr_cache
= kmem_cache_create("lio_qr_cache",
551 sizeof(struct iscsi_queue_req
),
552 __alignof__(struct iscsi_queue_req
), 0, NULL
);
554 pr_err("nable to kmem_cache_create() for"
559 lio_dr_cache
= kmem_cache_create("lio_dr_cache",
560 sizeof(struct iscsi_datain_req
),
561 __alignof__(struct iscsi_datain_req
), 0, NULL
);
563 pr_err("Unable to kmem_cache_create() for"
568 lio_ooo_cache
= kmem_cache_create("lio_ooo_cache",
569 sizeof(struct iscsi_ooo_cmdsn
),
570 __alignof__(struct iscsi_ooo_cmdsn
), 0, NULL
);
571 if (!lio_ooo_cache
) {
572 pr_err("Unable to kmem_cache_create() for"
577 lio_r2t_cache
= kmem_cache_create("lio_r2t_cache",
578 sizeof(struct iscsi_r2t
), __alignof__(struct iscsi_r2t
),
580 if (!lio_r2t_cache
) {
581 pr_err("Unable to kmem_cache_create() for"
586 iscsit_register_transport(&iscsi_target_transport
);
588 if (iscsit_load_discovery_tpg() < 0)
593 kmem_cache_destroy(lio_r2t_cache
);
595 kmem_cache_destroy(lio_ooo_cache
);
597 kmem_cache_destroy(lio_dr_cache
);
599 kmem_cache_destroy(lio_qr_cache
);
601 kmem_cache_destroy(lio_cmd_cache
);
603 iscsi_deallocate_thread_sets();
605 iscsi_thread_set_free();
607 iscsi_target_deregister_configfs();
609 kfree(iscsit_global
);
613 static void __exit
iscsi_target_cleanup_module(void)
615 iscsi_deallocate_thread_sets();
616 iscsi_thread_set_free();
617 iscsit_release_discovery_tpg();
618 iscsit_unregister_transport(&iscsi_target_transport
);
619 kmem_cache_destroy(lio_cmd_cache
);
620 kmem_cache_destroy(lio_qr_cache
);
621 kmem_cache_destroy(lio_dr_cache
);
622 kmem_cache_destroy(lio_ooo_cache
);
623 kmem_cache_destroy(lio_r2t_cache
);
625 iscsi_target_deregister_configfs();
627 kfree(iscsit_global
);
630 static int iscsit_add_reject(
631 struct iscsi_conn
*conn
,
635 struct iscsi_cmd
*cmd
;
637 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
641 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
642 cmd
->reject_reason
= reason
;
644 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
646 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
647 iscsit_free_cmd(cmd
, false);
651 spin_lock_bh(&conn
->cmd_lock
);
652 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
653 spin_unlock_bh(&conn
->cmd_lock
);
655 cmd
->i_state
= ISTATE_SEND_REJECT
;
656 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
661 static int iscsit_add_reject_from_cmd(
662 struct iscsi_cmd
*cmd
,
667 struct iscsi_conn
*conn
;
670 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
676 cmd
->iscsi_opcode
= ISCSI_OP_REJECT
;
677 cmd
->reject_reason
= reason
;
679 cmd
->buf_ptr
= kmemdup(buf
, ISCSI_HDR_LEN
, GFP_KERNEL
);
681 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
682 iscsit_free_cmd(cmd
, false);
687 spin_lock_bh(&conn
->cmd_lock
);
688 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
689 spin_unlock_bh(&conn
->cmd_lock
);
692 cmd
->i_state
= ISTATE_SEND_REJECT
;
693 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
695 * Perform the kref_put now if se_cmd has already been setup by
696 * scsit_setup_scsi_cmd()
698 if (cmd
->se_cmd
.se_tfo
!= NULL
) {
699 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
700 target_put_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
);
705 static int iscsit_add_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
,
708 return iscsit_add_reject_from_cmd(cmd
, reason
, true, buf
);
711 int iscsit_reject_cmd(struct iscsi_cmd
*cmd
, u8 reason
, unsigned char *buf
)
713 return iscsit_add_reject_from_cmd(cmd
, reason
, false, buf
);
717 * Map some portion of the allocated scatterlist to an iovec, suitable for
718 * kernel sockets to copy data in/out.
720 static int iscsit_map_iovec(
721 struct iscsi_cmd
*cmd
,
727 struct scatterlist
*sg
;
728 unsigned int page_off
;
731 * We know each entry in t_data_sg contains a page.
733 sg
= &cmd
->se_cmd
.t_data_sg
[data_offset
/ PAGE_SIZE
];
734 page_off
= (data_offset
% PAGE_SIZE
);
736 cmd
->first_data_sg
= sg
;
737 cmd
->first_data_sg_off
= page_off
;
739 while (data_length
) {
740 u32 cur_len
= min_t(u32
, data_length
, sg
->length
- page_off
);
742 iov
[i
].iov_base
= kmap(sg_page(sg
)) + sg
->offset
+ page_off
;
743 iov
[i
].iov_len
= cur_len
;
745 data_length
-= cur_len
;
751 cmd
->kmapped_nents
= i
;
756 static void iscsit_unmap_iovec(struct iscsi_cmd
*cmd
)
759 struct scatterlist
*sg
;
761 sg
= cmd
->first_data_sg
;
763 for (i
= 0; i
< cmd
->kmapped_nents
; i
++)
764 kunmap(sg_page(&sg
[i
]));
767 static void iscsit_ack_from_expstatsn(struct iscsi_conn
*conn
, u32 exp_statsn
)
769 struct iscsi_cmd
*cmd
;
771 conn
->exp_statsn
= exp_statsn
;
773 if (conn
->sess
->sess_ops
->RDMAExtensions
)
776 spin_lock_bh(&conn
->cmd_lock
);
777 list_for_each_entry(cmd
, &conn
->conn_cmd_list
, i_conn_node
) {
778 spin_lock(&cmd
->istate_lock
);
779 if ((cmd
->i_state
== ISTATE_SENT_STATUS
) &&
780 iscsi_sna_lt(cmd
->stat_sn
, exp_statsn
)) {
781 cmd
->i_state
= ISTATE_REMOVE
;
782 spin_unlock(&cmd
->istate_lock
);
783 iscsit_add_cmd_to_immediate_queue(cmd
, conn
,
787 spin_unlock(&cmd
->istate_lock
);
789 spin_unlock_bh(&conn
->cmd_lock
);
792 static int iscsit_allocate_iovecs(struct iscsi_cmd
*cmd
)
794 u32 iov_count
= max(1UL, DIV_ROUND_UP(cmd
->se_cmd
.data_length
, PAGE_SIZE
));
796 iov_count
+= ISCSI_IOV_DATA_BUFFER
;
798 cmd
->iov_data
= kzalloc(iov_count
* sizeof(struct kvec
), GFP_KERNEL
);
799 if (!cmd
->iov_data
) {
800 pr_err("Unable to allocate cmd->iov_data\n");
804 cmd
->orig_iov_data_count
= iov_count
;
808 int iscsit_setup_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
811 int data_direction
, payload_length
;
812 struct iscsi_scsi_req
*hdr
;
816 spin_lock_bh(&conn
->sess
->session_stats_lock
);
817 conn
->sess
->cmd_pdus
++;
818 if (conn
->sess
->se_sess
->se_node_acl
) {
819 spin_lock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
820 conn
->sess
->se_sess
->se_node_acl
->num_cmds
++;
821 spin_unlock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
823 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
825 hdr
= (struct iscsi_scsi_req
*) buf
;
826 payload_length
= ntoh24(hdr
->dlength
);
828 /* FIXME; Add checks for AdditionalHeaderSegment */
830 if (!(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) &&
831 !(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)) {
832 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
833 " not set. Bad iSCSI Initiator.\n");
834 return iscsit_add_reject_cmd(cmd
,
835 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
838 if (((hdr
->flags
& ISCSI_FLAG_CMD_READ
) ||
839 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) && !hdr
->data_length
) {
841 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
842 * that adds support for RESERVE/RELEASE. There is a bug
843 * add with this new functionality that sets R/W bits when
844 * neither CDB carries any READ or WRITE datapayloads.
846 if ((hdr
->cdb
[0] == 0x16) || (hdr
->cdb
[0] == 0x17)) {
847 hdr
->flags
&= ~ISCSI_FLAG_CMD_READ
;
848 hdr
->flags
&= ~ISCSI_FLAG_CMD_WRITE
;
852 pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
853 " set when Expected Data Transfer Length is 0 for"
854 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr
->cdb
[0]);
855 return iscsit_add_reject_cmd(cmd
,
856 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
860 if (!(hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
861 !(hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) && (hdr
->data_length
!= 0)) {
862 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
863 " MUST be set if Expected Data Transfer Length is not 0."
864 " Bad iSCSI Initiator\n");
865 return iscsit_add_reject_cmd(cmd
,
866 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
869 if ((hdr
->flags
& ISCSI_FLAG_CMD_READ
) &&
870 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) {
871 pr_err("Bidirectional operations not supported!\n");
872 return iscsit_add_reject_cmd(cmd
,
873 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
876 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
877 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
878 " Scsi Command PDU.\n");
879 return iscsit_add_reject_cmd(cmd
,
880 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
883 if (payload_length
&& !conn
->sess
->sess_ops
->ImmediateData
) {
884 pr_err("ImmediateData=No but DataSegmentLength=%u,"
885 " protocol error.\n", payload_length
);
886 return iscsit_add_reject_cmd(cmd
,
887 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
890 if ((be32_to_cpu(hdr
->data_length
) == payload_length
) &&
891 (!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
))) {
892 pr_err("Expected Data Transfer Length and Length of"
893 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
894 " bit is not set protocol error\n");
895 return iscsit_add_reject_cmd(cmd
,
896 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
899 if (payload_length
> be32_to_cpu(hdr
->data_length
)) {
900 pr_err("DataSegmentLength: %u is greater than"
901 " EDTL: %u, protocol error.\n", payload_length
,
903 return iscsit_add_reject_cmd(cmd
,
904 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
907 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
908 pr_err("DataSegmentLength: %u is greater than"
909 " MaxXmitDataSegmentLength: %u, protocol error.\n",
910 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
911 return iscsit_add_reject_cmd(cmd
,
912 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
915 if (payload_length
> conn
->sess
->sess_ops
->FirstBurstLength
) {
916 pr_err("DataSegmentLength: %u is greater than"
917 " FirstBurstLength: %u, protocol error.\n",
918 payload_length
, conn
->sess
->sess_ops
->FirstBurstLength
);
919 return iscsit_add_reject_cmd(cmd
,
920 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
923 data_direction
= (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
) ? DMA_TO_DEVICE
:
924 (hdr
->flags
& ISCSI_FLAG_CMD_READ
) ? DMA_FROM_DEVICE
:
927 cmd
->data_direction
= data_direction
;
928 iscsi_task_attr
= hdr
->flags
& ISCSI_FLAG_CMD_ATTR_MASK
;
930 * Figure out the SAM Task Attribute for the incoming SCSI CDB
932 if ((iscsi_task_attr
== ISCSI_ATTR_UNTAGGED
) ||
933 (iscsi_task_attr
== ISCSI_ATTR_SIMPLE
))
934 sam_task_attr
= MSG_SIMPLE_TAG
;
935 else if (iscsi_task_attr
== ISCSI_ATTR_ORDERED
)
936 sam_task_attr
= MSG_ORDERED_TAG
;
937 else if (iscsi_task_attr
== ISCSI_ATTR_HEAD_OF_QUEUE
)
938 sam_task_attr
= MSG_HEAD_TAG
;
939 else if (iscsi_task_attr
== ISCSI_ATTR_ACA
)
940 sam_task_attr
= MSG_ACA_TAG
;
942 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
943 " MSG_SIMPLE_TAG\n", iscsi_task_attr
);
944 sam_task_attr
= MSG_SIMPLE_TAG
;
947 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_CMD
;
948 cmd
->i_state
= ISTATE_NEW_CMD
;
949 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
950 cmd
->immediate_data
= (payload_length
) ? 1 : 0;
951 cmd
->unsolicited_data
= ((!(hdr
->flags
& ISCSI_FLAG_CMD_FINAL
) &&
952 (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)) ? 1 : 0);
953 if (cmd
->unsolicited_data
)
954 cmd
->cmd_flags
|= ICF_NON_IMMEDIATE_UNSOLICITED_DATA
;
956 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
957 if (hdr
->flags
& ISCSI_FLAG_CMD_READ
) {
958 spin_lock_bh(&conn
->sess
->ttt_lock
);
959 cmd
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
960 if (cmd
->targ_xfer_tag
== 0xFFFFFFFF)
961 cmd
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
962 spin_unlock_bh(&conn
->sess
->ttt_lock
);
963 } else if (hdr
->flags
& ISCSI_FLAG_CMD_WRITE
)
964 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
965 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
966 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
967 cmd
->first_burst_len
= payload_length
;
969 if (!conn
->sess
->sess_ops
->RDMAExtensions
&&
970 cmd
->data_direction
== DMA_FROM_DEVICE
) {
971 struct iscsi_datain_req
*dr
;
973 dr
= iscsit_allocate_datain_req();
975 return iscsit_add_reject_cmd(cmd
,
976 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
978 iscsit_attach_datain_req(cmd
, dr
);
982 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
984 transport_init_se_cmd(&cmd
->se_cmd
, &lio_target_fabric_configfs
->tf_ops
,
985 conn
->sess
->se_sess
, be32_to_cpu(hdr
->data_length
),
986 cmd
->data_direction
, sam_task_attr
,
987 cmd
->sense_buffer
+ 2);
989 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
990 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr
->itt
,
991 hdr
->cmdsn
, be32_to_cpu(hdr
->data_length
), payload_length
,
994 target_get_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
, true);
996 cmd
->sense_reason
= transport_lookup_cmd_lun(&cmd
->se_cmd
,
997 scsilun_to_int(&hdr
->lun
));
998 if (cmd
->sense_reason
)
1001 cmd
->sense_reason
= target_setup_cmd_from_cdb(&cmd
->se_cmd
, hdr
->cdb
);
1002 if (cmd
->sense_reason
) {
1003 if (cmd
->sense_reason
== TCM_OUT_OF_RESOURCES
) {
1004 return iscsit_add_reject_cmd(cmd
,
1005 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1011 if (iscsit_build_pdu_and_seq_lists(cmd
, payload_length
) < 0) {
1012 return iscsit_add_reject_cmd(cmd
,
1013 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1017 spin_lock_bh(&conn
->cmd_lock
);
1018 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1019 spin_unlock_bh(&conn
->cmd_lock
);
1021 * Check if we need to delay processing because of ALUA
1022 * Active/NonOptimized primary access state..
1024 core_alua_check_nonop_delay(&cmd
->se_cmd
);
1028 EXPORT_SYMBOL(iscsit_setup_scsi_cmd
);
1030 void iscsit_set_unsoliticed_dataout(struct iscsi_cmd
*cmd
)
1032 iscsit_set_dataout_sequence_values(cmd
);
1034 spin_lock_bh(&cmd
->dataout_timeout_lock
);
1035 iscsit_start_dataout_timer(cmd
, cmd
->conn
);
1036 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
1038 EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout
);
1040 int iscsit_process_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1041 struct iscsi_scsi_req
*hdr
)
1045 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1046 * the Immediate Bit is not set, and no Immediate
1049 * A PDU/CmdSN carrying Immediate Data can only
1050 * be processed after the DataCRC has passed.
1051 * If the DataCRC fails, the CmdSN MUST NOT
1052 * be acknowledged. (See below)
1054 if (!cmd
->immediate_data
) {
1055 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1056 (unsigned char *)hdr
, hdr
->cmdsn
);
1057 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1059 else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1060 target_put_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
);
1065 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1068 * If no Immediate Data is attached, it's OK to return now.
1070 if (!cmd
->immediate_data
) {
1071 if (!cmd
->sense_reason
&& cmd
->unsolicited_data
)
1072 iscsit_set_unsoliticed_dataout(cmd
);
1073 if (!cmd
->sense_reason
)
1076 target_put_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
);
1081 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1082 * execution. These exceptions are processed in CmdSN order using
1083 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1085 if (cmd
->sense_reason
) {
1086 if (cmd
->reject_reason
)
1089 target_put_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
);
1093 * Call directly into transport_generic_new_cmd() to perform
1094 * the backend memory allocation.
1096 cmd
->sense_reason
= transport_generic_new_cmd(&cmd
->se_cmd
);
1097 if (cmd
->sense_reason
)
1102 EXPORT_SYMBOL(iscsit_process_scsi_cmd
);
1105 iscsit_get_immediate_data(struct iscsi_cmd
*cmd
, struct iscsi_scsi_req
*hdr
,
1108 struct iscsi_conn
*conn
= cmd
->conn
;
1109 int cmdsn_ret
= 0, immed_ret
= IMMEDIATE_DATA_NORMAL_OPERATION
;
1111 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1113 if (dump_payload
== true)
1114 goto after_immediate_data
;
1116 immed_ret
= iscsit_handle_immediate_data(cmd
, hdr
,
1117 cmd
->first_burst_len
);
1118 after_immediate_data
:
1119 if (immed_ret
== IMMEDIATE_DATA_NORMAL_OPERATION
) {
1121 * A PDU/CmdSN carrying Immediate Data passed
1122 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1123 * Immediate Bit is not set.
1125 cmdsn_ret
= iscsit_sequence_cmd(cmd
->conn
, cmd
,
1126 (unsigned char *)hdr
, hdr
->cmdsn
);
1127 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
) {
1129 } else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
) {
1130 target_put_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
);
1134 if (cmd
->sense_reason
) {
1137 rc
= iscsit_dump_data_payload(cmd
->conn
,
1138 cmd
->first_burst_len
, 1);
1139 target_put_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
);
1141 } else if (cmd
->unsolicited_data
)
1142 iscsit_set_unsoliticed_dataout(cmd
);
1144 } else if (immed_ret
== IMMEDIATE_DATA_ERL1_CRC_FAILURE
) {
1146 * Immediate Data failed DataCRC and ERL>=1,
1147 * silently drop this PDU and let the initiator
1148 * plug the CmdSN gap.
1150 * FIXME: Send Unsolicited NOPIN with reserved
1151 * TTT here to help the initiator figure out
1152 * the missing CmdSN, although they should be
1153 * intelligent enough to determine the missing
1154 * CmdSN and issue a retry to plug the sequence.
1156 cmd
->i_state
= ISTATE_REMOVE
;
1157 iscsit_add_cmd_to_immediate_queue(cmd
, cmd
->conn
, cmd
->i_state
);
1158 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1165 iscsit_handle_scsi_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1168 struct iscsi_scsi_req
*hdr
= (struct iscsi_scsi_req
*)buf
;
1170 bool dump_payload
= false;
1172 rc
= iscsit_setup_scsi_cmd(conn
, cmd
, buf
);
1176 * Allocation iovecs needed for struct socket operations for
1177 * traditional iSCSI block I/O.
1179 if (iscsit_allocate_iovecs(cmd
) < 0) {
1180 return iscsit_add_reject_cmd(cmd
,
1181 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1183 immed_data
= cmd
->immediate_data
;
1185 rc
= iscsit_process_scsi_cmd(conn
, cmd
, hdr
);
1189 dump_payload
= true;
1194 return iscsit_get_immediate_data(cmd
, hdr
, dump_payload
);
1197 static u32
iscsit_do_crypto_hash_sg(
1198 struct hash_desc
*hash
,
1199 struct iscsi_cmd
*cmd
,
1207 struct scatterlist
*sg
;
1208 unsigned int page_off
;
1210 crypto_hash_init(hash
);
1212 sg
= cmd
->first_data_sg
;
1213 page_off
= cmd
->first_data_sg_off
;
1216 while (data_length
) {
1217 u32 cur_len
= min_t(u32
, data_length
, (sg
[i
].length
- page_off
));
1219 crypto_hash_update(hash
, &sg
[i
], cur_len
);
1221 data_length
-= cur_len
;
1227 struct scatterlist pad_sg
;
1229 sg_init_one(&pad_sg
, pad_bytes
, padding
);
1230 crypto_hash_update(hash
, &pad_sg
, padding
);
1232 crypto_hash_final(hash
, (u8
*) &data_crc
);
1237 static void iscsit_do_crypto_hash_buf(
1238 struct hash_desc
*hash
,
1245 struct scatterlist sg
;
1247 crypto_hash_init(hash
);
1249 sg_init_one(&sg
, buf
, payload_length
);
1250 crypto_hash_update(hash
, &sg
, payload_length
);
1253 sg_init_one(&sg
, pad_bytes
, padding
);
1254 crypto_hash_update(hash
, &sg
, padding
);
1256 crypto_hash_final(hash
, data_crc
);
1260 iscsit_check_dataout_hdr(struct iscsi_conn
*conn
, unsigned char *buf
,
1261 struct iscsi_cmd
**out_cmd
)
1263 struct iscsi_data
*hdr
= (struct iscsi_data
*)buf
;
1264 struct iscsi_cmd
*cmd
= NULL
;
1265 struct se_cmd
*se_cmd
;
1266 u32 payload_length
= ntoh24(hdr
->dlength
);
1269 if (!payload_length
) {
1270 pr_err("DataOUT payload is ZERO, protocol error.\n");
1271 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1276 spin_lock_bh(&conn
->sess
->session_stats_lock
);
1277 conn
->sess
->rx_data_octets
+= payload_length
;
1278 if (conn
->sess
->se_sess
->se_node_acl
) {
1279 spin_lock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
1280 conn
->sess
->se_sess
->se_node_acl
->write_bytes
+= payload_length
;
1281 spin_unlock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
1283 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
1285 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1286 pr_err("DataSegmentLength: %u is greater than"
1287 " MaxXmitDataSegmentLength: %u\n", payload_length
,
1288 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1289 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
1293 cmd
= iscsit_find_cmd_from_itt_or_dump(conn
, hdr
->itt
,
1298 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1299 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1300 hdr
->itt
, hdr
->ttt
, hdr
->datasn
, ntohl(hdr
->offset
),
1301 payload_length
, conn
->cid
);
1303 if (cmd
->cmd_flags
& ICF_GOT_LAST_DATAOUT
) {
1304 pr_err("Command ITT: 0x%08x received DataOUT after"
1305 " last DataOUT received, dumping payload\n",
1306 cmd
->init_task_tag
);
1307 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1310 if (cmd
->data_direction
!= DMA_TO_DEVICE
) {
1311 pr_err("Command ITT: 0x%08x received DataOUT for a"
1312 " NON-WRITE command.\n", cmd
->init_task_tag
);
1313 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1315 se_cmd
= &cmd
->se_cmd
;
1316 iscsit_mod_dataout_timer(cmd
);
1318 if ((be32_to_cpu(hdr
->offset
) + payload_length
) > cmd
->se_cmd
.data_length
) {
1319 pr_err("DataOut Offset: %u, Length %u greater than"
1320 " iSCSI Command EDTL %u, protocol error.\n",
1321 hdr
->offset
, payload_length
, cmd
->se_cmd
.data_length
);
1322 return iscsit_reject_cmd(cmd
, ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1325 if (cmd
->unsolicited_data
) {
1326 int dump_unsolicited_data
= 0;
1328 if (conn
->sess
->sess_ops
->InitialR2T
) {
1329 pr_err("Received unexpected unsolicited data"
1330 " while InitialR2T=Yes, protocol error.\n");
1331 transport_send_check_condition_and_sense(&cmd
->se_cmd
,
1332 TCM_UNEXPECTED_UNSOLICITED_DATA
, 0);
1336 * Special case for dealing with Unsolicited DataOUT
1337 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1341 /* Something's amiss if we're not in WRITE_PENDING state... */
1342 WARN_ON(se_cmd
->t_state
!= TRANSPORT_WRITE_PENDING
);
1343 if (!(se_cmd
->se_cmd_flags
& SCF_SUPPORTED_SAM_OPCODE
))
1344 dump_unsolicited_data
= 1;
1346 if (dump_unsolicited_data
) {
1348 * Check if a delayed TASK_ABORTED status needs to
1349 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1350 * received with the unsolicitied data out.
1352 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1353 iscsit_stop_dataout_timer(cmd
);
1355 transport_check_aborted_status(se_cmd
,
1356 (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
));
1357 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1361 * For the normal solicited data path:
1363 * Check for a delayed TASK_ABORTED status and dump any
1364 * incoming data out payload if one exists. Also, when the
1365 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1366 * data out sequence, we decrement outstanding_r2ts. Once
1367 * outstanding_r2ts reaches zero, go ahead and send the delayed
1368 * TASK_ABORTED status.
1370 if (se_cmd
->transport_state
& CMD_T_ABORTED
) {
1371 if (hdr
->flags
& ISCSI_FLAG_CMD_FINAL
)
1372 if (--cmd
->outstanding_r2ts
< 1) {
1373 iscsit_stop_dataout_timer(cmd
);
1374 transport_check_aborted_status(
1378 return iscsit_dump_data_payload(conn
, payload_length
, 1);
1382 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1383 * within-command recovery checks before receiving the payload.
1385 rc
= iscsit_check_pre_dataout(cmd
, buf
);
1386 if (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
)
1388 else if (rc
== DATAOUT_CANNOT_RECOVER
)
1394 EXPORT_SYMBOL(iscsit_check_dataout_hdr
);
1397 iscsit_get_dataout(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1398 struct iscsi_data
*hdr
)
1401 u32 checksum
, iov_count
= 0, padding
= 0, rx_got
= 0, rx_size
= 0;
1402 u32 payload_length
= ntoh24(hdr
->dlength
);
1403 int iov_ret
, data_crc_failed
= 0;
1405 rx_size
+= payload_length
;
1406 iov
= &cmd
->iov_data
[0];
1408 iov_ret
= iscsit_map_iovec(cmd
, iov
, be32_to_cpu(hdr
->offset
),
1413 iov_count
+= iov_ret
;
1415 padding
= ((-payload_length
) & 3);
1417 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
1418 iov
[iov_count
++].iov_len
= padding
;
1420 pr_debug("Receiving %u padding bytes.\n", padding
);
1423 if (conn
->conn_ops
->DataDigest
) {
1424 iov
[iov_count
].iov_base
= &checksum
;
1425 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
1426 rx_size
+= ISCSI_CRC_LEN
;
1429 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
1431 iscsit_unmap_iovec(cmd
);
1433 if (rx_got
!= rx_size
)
1436 if (conn
->conn_ops
->DataDigest
) {
1439 data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_rx_hash
, cmd
,
1440 be32_to_cpu(hdr
->offset
),
1441 payload_length
, padding
,
1444 if (checksum
!= data_crc
) {
1445 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1446 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1447 " does not match computed 0x%08x\n",
1448 hdr
->itt
, hdr
->offset
, payload_length
,
1449 hdr
->datasn
, checksum
, data_crc
);
1450 data_crc_failed
= 1;
1452 pr_debug("Got CRC32C DataDigest 0x%08x for"
1453 " %u bytes of Data Out\n", checksum
,
1458 return data_crc_failed
;
1462 iscsit_check_dataout_payload(struct iscsi_cmd
*cmd
, struct iscsi_data
*hdr
,
1463 bool data_crc_failed
)
1465 struct iscsi_conn
*conn
= cmd
->conn
;
1468 * Increment post receive data and CRC values or perform
1469 * within-command recovery.
1471 rc
= iscsit_check_post_dataout(cmd
, (unsigned char *)hdr
, data_crc_failed
);
1472 if ((rc
== DATAOUT_NORMAL
) || (rc
== DATAOUT_WITHIN_COMMAND_RECOVERY
))
1474 else if (rc
== DATAOUT_SEND_R2T
) {
1475 iscsit_set_dataout_sequence_values(cmd
);
1476 conn
->conn_transport
->iscsit_get_dataout(conn
, cmd
, false);
1477 } else if (rc
== DATAOUT_SEND_TO_TRANSPORT
) {
1479 * Handle extra special case for out of order
1480 * Unsolicited Data Out.
1482 spin_lock_bh(&cmd
->istate_lock
);
1483 ooo_cmdsn
= (cmd
->cmd_flags
& ICF_OOO_CMDSN
);
1484 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
1485 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
1486 spin_unlock_bh(&cmd
->istate_lock
);
1488 iscsit_stop_dataout_timer(cmd
);
1491 target_execute_cmd(&cmd
->se_cmd
);
1493 } else /* DATAOUT_CANNOT_RECOVER */
1498 EXPORT_SYMBOL(iscsit_check_dataout_payload
);
1500 static int iscsit_handle_data_out(struct iscsi_conn
*conn
, unsigned char *buf
)
1502 struct iscsi_cmd
*cmd
;
1503 struct iscsi_data
*hdr
= (struct iscsi_data
*)buf
;
1505 bool data_crc_failed
= false;
1507 rc
= iscsit_check_dataout_hdr(conn
, buf
, &cmd
);
1513 rc
= iscsit_get_dataout(conn
, cmd
, hdr
);
1517 data_crc_failed
= true;
1519 return iscsit_check_dataout_payload(cmd
, hdr
, data_crc_failed
);
1522 int iscsit_setup_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1523 struct iscsi_nopout
*hdr
)
1525 u32 payload_length
= ntoh24(hdr
->dlength
);
1527 if (hdr
->itt
== RESERVED_ITT
&& !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1528 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1529 " not set, protocol error.\n");
1530 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1531 (unsigned char *)hdr
);
1534 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1535 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1536 " greater than MaxXmitDataSegmentLength: %u, protocol"
1537 " error.\n", payload_length
,
1538 conn
->conn_ops
->MaxXmitDataSegmentLength
);
1539 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1540 (unsigned char *)hdr
);
1543 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1544 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1545 hdr
->itt
== RESERVED_ITT
? "Response" : "Request",
1546 hdr
->itt
, hdr
->ttt
, hdr
->cmdsn
, hdr
->exp_statsn
,
1549 * This is not a response to a Unsolicited NopIN, which means
1550 * it can either be a NOPOUT ping request (with a valid ITT),
1551 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1552 * Either way, make sure we allocate an struct iscsi_cmd, as both
1553 * can contain ping data.
1555 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1556 cmd
->iscsi_opcode
= ISCSI_OP_NOOP_OUT
;
1557 cmd
->i_state
= ISTATE_SEND_NOPIN
;
1558 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ?
1560 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1561 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1562 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1563 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1564 cmd
->data_direction
= DMA_NONE
;
1569 EXPORT_SYMBOL(iscsit_setup_nop_out
);
1571 int iscsit_process_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1572 struct iscsi_nopout
*hdr
)
1574 struct iscsi_cmd
*cmd_p
= NULL
;
1577 * Initiator is expecting a NopIN ping reply..
1579 if (hdr
->itt
!= RESERVED_ITT
) {
1582 spin_lock_bh(&conn
->cmd_lock
);
1583 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1584 spin_unlock_bh(&conn
->cmd_lock
);
1586 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1588 if (hdr
->opcode
& ISCSI_OP_IMMEDIATE
) {
1589 iscsit_add_cmd_to_response_queue(cmd
, conn
,
1594 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
1595 (unsigned char *)hdr
, hdr
->cmdsn
);
1596 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
1598 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1604 * This was a response to a unsolicited NOPIN ping.
1606 if (hdr
->ttt
!= cpu_to_be32(0xFFFFFFFF)) {
1607 cmd_p
= iscsit_find_cmd_from_ttt(conn
, be32_to_cpu(hdr
->ttt
));
1611 iscsit_stop_nopin_response_timer(conn
);
1613 cmd_p
->i_state
= ISTATE_REMOVE
;
1614 iscsit_add_cmd_to_immediate_queue(cmd_p
, conn
, cmd_p
->i_state
);
1616 iscsit_start_nopin_timer(conn
);
1620 * Otherwise, initiator is not expecting a NOPIN is response.
1621 * Just ignore for now.
1625 EXPORT_SYMBOL(iscsit_process_nop_out
);
1627 static int iscsit_handle_nop_out(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1630 unsigned char *ping_data
= NULL
;
1631 struct iscsi_nopout
*hdr
= (struct iscsi_nopout
*)buf
;
1632 struct kvec
*iov
= NULL
;
1633 u32 payload_length
= ntoh24(hdr
->dlength
);
1636 ret
= iscsit_setup_nop_out(conn
, cmd
, hdr
);
1640 * Handle NOP-OUT payload for traditional iSCSI sockets
1642 if (payload_length
&& hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
1643 u32 checksum
, data_crc
, padding
= 0;
1644 int niov
= 0, rx_got
, rx_size
= payload_length
;
1646 ping_data
= kzalloc(payload_length
+ 1, GFP_KERNEL
);
1648 pr_err("Unable to allocate memory for"
1649 " NOPOUT ping data.\n");
1654 iov
= &cmd
->iov_misc
[0];
1655 iov
[niov
].iov_base
= ping_data
;
1656 iov
[niov
++].iov_len
= payload_length
;
1658 padding
= ((-payload_length
) & 3);
1660 pr_debug("Receiving %u additional bytes"
1661 " for padding.\n", padding
);
1662 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
1663 iov
[niov
++].iov_len
= padding
;
1666 if (conn
->conn_ops
->DataDigest
) {
1667 iov
[niov
].iov_base
= &checksum
;
1668 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
1669 rx_size
+= ISCSI_CRC_LEN
;
1672 rx_got
= rx_data(conn
, &cmd
->iov_misc
[0], niov
, rx_size
);
1673 if (rx_got
!= rx_size
) {
1678 if (conn
->conn_ops
->DataDigest
) {
1679 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
1680 ping_data
, payload_length
,
1681 padding
, cmd
->pad_bytes
,
1684 if (checksum
!= data_crc
) {
1685 pr_err("Ping data CRC32C DataDigest"
1686 " 0x%08x does not match computed 0x%08x\n",
1687 checksum
, data_crc
);
1688 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
1689 pr_err("Unable to recover from"
1690 " NOPOUT Ping DataCRC failure while in"
1696 * Silently drop this PDU and let the
1697 * initiator plug the CmdSN gap.
1699 pr_debug("Dropping NOPOUT"
1700 " Command CmdSN: 0x%08x due to"
1701 " DataCRC error.\n", hdr
->cmdsn
);
1706 pr_debug("Got CRC32C DataDigest"
1707 " 0x%08x for %u bytes of ping data.\n",
1708 checksum
, payload_length
);
1712 ping_data
[payload_length
] = '\0';
1714 * Attach ping data to struct iscsi_cmd->buf_ptr.
1716 cmd
->buf_ptr
= ping_data
;
1717 cmd
->buf_ptr_size
= payload_length
;
1719 pr_debug("Got %u bytes of NOPOUT ping"
1720 " data.\n", payload_length
);
1721 pr_debug("Ping Data: \"%s\"\n", ping_data
);
1724 return iscsit_process_nop_out(conn
, cmd
, hdr
);
1727 iscsit_free_cmd(cmd
, false);
1734 iscsit_handle_task_mgt_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1737 struct se_tmr_req
*se_tmr
;
1738 struct iscsi_tmr_req
*tmr_req
;
1739 struct iscsi_tm
*hdr
;
1740 int out_of_order_cmdsn
= 0, ret
;
1741 bool sess_ref
= false;
1744 hdr
= (struct iscsi_tm
*) buf
;
1745 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
1746 function
= hdr
->flags
;
1748 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1749 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1750 " 0x%08x, CID: %hu\n", hdr
->itt
, hdr
->cmdsn
, function
,
1751 hdr
->rtt
, hdr
->refcmdsn
, conn
->cid
);
1753 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1754 ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1755 hdr
->rtt
!= RESERVED_ITT
)) {
1756 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1757 hdr
->rtt
= RESERVED_ITT
;
1760 if ((function
== ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1761 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1762 pr_err("Task Management Request TASK_REASSIGN not"
1763 " issued as immediate command, bad iSCSI Initiator"
1764 "implementation\n");
1765 return iscsit_add_reject_cmd(cmd
,
1766 ISCSI_REASON_PROTOCOL_ERROR
, buf
);
1768 if ((function
!= ISCSI_TM_FUNC_ABORT_TASK
) &&
1769 be32_to_cpu(hdr
->refcmdsn
) != ISCSI_RESERVED_TAG
)
1770 hdr
->refcmdsn
= cpu_to_be32(ISCSI_RESERVED_TAG
);
1772 cmd
->data_direction
= DMA_NONE
;
1774 cmd
->tmr_req
= kzalloc(sizeof(struct iscsi_tmr_req
), GFP_KERNEL
);
1775 if (!cmd
->tmr_req
) {
1776 pr_err("Unable to allocate memory for"
1777 " Task Management command!\n");
1778 return iscsit_add_reject_cmd(cmd
,
1779 ISCSI_REASON_BOOKMARK_NO_RESOURCES
,
1784 * TASK_REASSIGN for ERL=2 / connection stays inside of
1785 * LIO-Target $FABRIC_MOD
1787 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
1792 transport_init_se_cmd(&cmd
->se_cmd
,
1793 &lio_target_fabric_configfs
->tf_ops
,
1794 conn
->sess
->se_sess
, 0, DMA_NONE
,
1795 MSG_SIMPLE_TAG
, cmd
->sense_buffer
+ 2);
1797 target_get_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
, true);
1801 case ISCSI_TM_FUNC_ABORT_TASK
:
1802 tcm_function
= TMR_ABORT_TASK
;
1804 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
1805 tcm_function
= TMR_ABORT_TASK_SET
;
1807 case ISCSI_TM_FUNC_CLEAR_ACA
:
1808 tcm_function
= TMR_CLEAR_ACA
;
1810 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
1811 tcm_function
= TMR_CLEAR_TASK_SET
;
1813 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
1814 tcm_function
= TMR_LUN_RESET
;
1816 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
1817 tcm_function
= TMR_TARGET_WARM_RESET
;
1819 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
1820 tcm_function
= TMR_TARGET_COLD_RESET
;
1823 pr_err("Unknown iSCSI TMR Function:"
1824 " 0x%02x\n", function
);
1825 return iscsit_add_reject_cmd(cmd
,
1826 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1829 ret
= core_tmr_alloc_req(&cmd
->se_cmd
, cmd
->tmr_req
,
1830 tcm_function
, GFP_KERNEL
);
1832 return iscsit_add_reject_cmd(cmd
,
1833 ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
1835 cmd
->tmr_req
->se_tmr_req
= cmd
->se_cmd
.se_tmr_req
;
1838 cmd
->iscsi_opcode
= ISCSI_OP_SCSI_TMFUNC
;
1839 cmd
->i_state
= ISTATE_SEND_TASKMGTRSP
;
1840 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1841 cmd
->init_task_tag
= hdr
->itt
;
1842 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1843 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1844 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1845 se_tmr
= cmd
->se_cmd
.se_tmr_req
;
1846 tmr_req
= cmd
->tmr_req
;
1848 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1850 if (function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) {
1851 ret
= transport_lookup_tmr_lun(&cmd
->se_cmd
,
1852 scsilun_to_int(&hdr
->lun
));
1854 se_tmr
->response
= ISCSI_TMF_RSP_NO_LUN
;
1860 case ISCSI_TM_FUNC_ABORT_TASK
:
1861 se_tmr
->response
= iscsit_tmr_abort_task(cmd
, buf
);
1862 if (se_tmr
->response
)
1865 case ISCSI_TM_FUNC_ABORT_TASK_SET
:
1866 case ISCSI_TM_FUNC_CLEAR_ACA
:
1867 case ISCSI_TM_FUNC_CLEAR_TASK_SET
:
1868 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
:
1870 case ISCSI_TM_FUNC_TARGET_WARM_RESET
:
1871 if (iscsit_tmr_task_warm_reset(conn
, tmr_req
, buf
) < 0) {
1872 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
1876 case ISCSI_TM_FUNC_TARGET_COLD_RESET
:
1877 if (iscsit_tmr_task_cold_reset(conn
, tmr_req
, buf
) < 0) {
1878 se_tmr
->response
= ISCSI_TMF_RSP_AUTH_FAILED
;
1882 case ISCSI_TM_FUNC_TASK_REASSIGN
:
1883 se_tmr
->response
= iscsit_tmr_task_reassign(cmd
, buf
);
1885 * Perform sanity checks on the ExpDataSN only if the
1886 * TASK_REASSIGN was successful.
1888 if (se_tmr
->response
)
1891 if (iscsit_check_task_reassign_expdatasn(tmr_req
, conn
) < 0)
1892 return iscsit_add_reject_cmd(cmd
,
1893 ISCSI_REASON_BOOKMARK_INVALID
, buf
);
1896 pr_err("Unknown TMR function: 0x%02x, protocol"
1897 " error.\n", function
);
1898 se_tmr
->response
= ISCSI_TMF_RSP_NOT_SUPPORTED
;
1902 if ((function
!= ISCSI_TM_FUNC_TASK_REASSIGN
) &&
1903 (se_tmr
->response
== ISCSI_TMF_RSP_COMPLETE
))
1904 se_tmr
->call_transport
= 1;
1906 spin_lock_bh(&conn
->cmd_lock
);
1907 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
1908 spin_unlock_bh(&conn
->cmd_lock
);
1910 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
1911 int cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
1912 if (cmdsn_ret
== CMDSN_HIGHER_THAN_EXP
)
1913 out_of_order_cmdsn
= 1;
1914 else if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
1916 else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
1919 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
1921 if (out_of_order_cmdsn
|| !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
))
1924 * Found the referenced task, send to transport for processing.
1926 if (se_tmr
->call_transport
)
1927 return transport_generic_handle_tmr(&cmd
->se_cmd
);
1930 * Could not find the referenced LUN, task, or Task Management
1931 * command not authorized or supported. Change state and
1932 * let the tx_thread send the response.
1934 * For connection recovery, this is also the default action for
1935 * TMR TASK_REASSIGN.
1938 pr_debug("Handle TMR, using sess_ref=true check\n");
1939 target_put_sess_cmd(conn
->sess
->se_sess
, &cmd
->se_cmd
);
1942 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
1945 EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd
);
1947 /* #warning FIXME: Support Text Command parameters besides SendTargets */
1949 iscsit_setup_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1950 struct iscsi_text
*hdr
)
1952 u32 payload_length
= ntoh24(hdr
->dlength
);
1954 if (payload_length
> conn
->conn_ops
->MaxXmitDataSegmentLength
) {
1955 pr_err("Unable to accept text parameter length: %u"
1956 "greater than MaxXmitDataSegmentLength %u.\n",
1957 payload_length
, conn
->conn_ops
->MaxXmitDataSegmentLength
);
1958 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
1959 (unsigned char *)hdr
);
1962 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1963 " ExpStatSN: 0x%08x, Length: %u\n", hdr
->itt
, hdr
->cmdsn
,
1964 hdr
->exp_statsn
, payload_length
);
1966 cmd
->iscsi_opcode
= ISCSI_OP_TEXT
;
1967 cmd
->i_state
= ISTATE_SEND_TEXTRSP
;
1968 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
1969 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
1970 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
1971 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
1972 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
1973 cmd
->data_direction
= DMA_NONE
;
1977 EXPORT_SYMBOL(iscsit_setup_text_cmd
);
1980 iscsit_process_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
1981 struct iscsi_text
*hdr
)
1983 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
;
1987 pr_err("Unable to locate text_in buffer for sendtargets"
1991 if (strncmp("SendTargets", text_in
, 11) != 0) {
1992 pr_err("Received Text Data that is not"
1993 " SendTargets, cannot continue.\n");
1996 text_ptr
= strchr(text_in
, '=');
1998 pr_err("No \"=\" separator found in Text Data,"
1999 " cannot continue.\n");
2002 if (!strncmp("=All", text_ptr
, 4)) {
2003 cmd
->cmd_flags
|= IFC_SENDTARGETS_ALL
;
2004 } else if (!strncmp("=iqn.", text_ptr
, 5) ||
2005 !strncmp("=eui.", text_ptr
, 5)) {
2006 cmd
->cmd_flags
|= IFC_SENDTARGETS_SINGLE
;
2008 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr
);
2012 spin_lock_bh(&conn
->cmd_lock
);
2013 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2014 spin_unlock_bh(&conn
->cmd_lock
);
2016 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2018 if (!(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
2019 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
,
2020 (unsigned char *)hdr
, hdr
->cmdsn
);
2021 if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2027 return iscsit_execute_cmd(cmd
, 0);
2030 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
,
2031 (unsigned char *)hdr
);
2033 EXPORT_SYMBOL(iscsit_process_text_cmd
);
2036 iscsit_handle_text_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2039 struct iscsi_text
*hdr
= (struct iscsi_text
*)buf
;
2040 char *text_in
= NULL
;
2041 u32 payload_length
= ntoh24(hdr
->dlength
);
2044 rc
= iscsit_setup_text_cmd(conn
, cmd
, hdr
);
2048 rx_size
= payload_length
;
2049 if (payload_length
) {
2050 u32 checksum
= 0, data_crc
= 0;
2051 u32 padding
= 0, pad_bytes
= 0;
2052 int niov
= 0, rx_got
;
2055 text_in
= kzalloc(payload_length
, GFP_KERNEL
);
2057 pr_err("Unable to allocate memory for"
2058 " incoming text parameters\n");
2061 cmd
->text_in_ptr
= text_in
;
2063 memset(iov
, 0, 3 * sizeof(struct kvec
));
2064 iov
[niov
].iov_base
= text_in
;
2065 iov
[niov
++].iov_len
= payload_length
;
2067 padding
= ((-payload_length
) & 3);
2069 iov
[niov
].iov_base
= &pad_bytes
;
2070 iov
[niov
++].iov_len
= padding
;
2072 pr_debug("Receiving %u additional bytes"
2073 " for padding.\n", padding
);
2075 if (conn
->conn_ops
->DataDigest
) {
2076 iov
[niov
].iov_base
= &checksum
;
2077 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
2078 rx_size
+= ISCSI_CRC_LEN
;
2081 rx_got
= rx_data(conn
, &iov
[0], niov
, rx_size
);
2082 if (rx_got
!= rx_size
)
2085 if (conn
->conn_ops
->DataDigest
) {
2086 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
2087 text_in
, payload_length
,
2088 padding
, (u8
*)&pad_bytes
,
2091 if (checksum
!= data_crc
) {
2092 pr_err("Text data CRC32C DataDigest"
2093 " 0x%08x does not match computed"
2094 " 0x%08x\n", checksum
, data_crc
);
2095 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2096 pr_err("Unable to recover from"
2097 " Text Data digest failure while in"
2102 * Silently drop this PDU and let the
2103 * initiator plug the CmdSN gap.
2105 pr_debug("Dropping Text"
2106 " Command CmdSN: 0x%08x due to"
2107 " DataCRC error.\n", hdr
->cmdsn
);
2112 pr_debug("Got CRC32C DataDigest"
2113 " 0x%08x for %u bytes of text data.\n",
2114 checksum
, payload_length
);
2117 text_in
[payload_length
- 1] = '\0';
2118 pr_debug("Successfully read %d bytes of text"
2119 " data.\n", payload_length
);
2122 return iscsit_process_text_cmd(conn
, cmd
, hdr
);
2125 kfree(cmd
->text_in_ptr
);
2126 cmd
->text_in_ptr
= NULL
;
2127 return iscsit_reject_cmd(cmd
, ISCSI_REASON_PROTOCOL_ERROR
, buf
);
2129 EXPORT_SYMBOL(iscsit_handle_text_cmd
);
2131 int iscsit_logout_closesession(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2133 struct iscsi_conn
*conn_p
;
2134 struct iscsi_session
*sess
= conn
->sess
;
2136 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2137 " for SID: %u.\n", conn
->cid
, conn
->sess
->sid
);
2139 atomic_set(&sess
->session_logout
, 1);
2140 atomic_set(&conn
->conn_logout_remove
, 1);
2141 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_SESSION
;
2143 iscsit_inc_conn_usage_count(conn
);
2144 iscsit_inc_session_usage_count(sess
);
2146 spin_lock_bh(&sess
->conn_lock
);
2147 list_for_each_entry(conn_p
, &sess
->sess_conn_list
, conn_list
) {
2148 if (conn_p
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
)
2151 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2152 conn_p
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2154 spin_unlock_bh(&sess
->conn_lock
);
2156 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2161 int iscsit_logout_closeconnection(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2163 struct iscsi_conn
*l_conn
;
2164 struct iscsi_session
*sess
= conn
->sess
;
2166 pr_debug("Received logout request CLOSECONNECTION for CID:"
2167 " %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2170 * A Logout Request with a CLOSECONNECTION reason code for a CID
2171 * can arrive on a connection with a differing CID.
2173 if (conn
->cid
== cmd
->logout_cid
) {
2174 spin_lock_bh(&conn
->state_lock
);
2175 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2176 conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
2178 atomic_set(&conn
->conn_logout_remove
, 1);
2179 conn
->conn_logout_reason
= ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
;
2180 iscsit_inc_conn_usage_count(conn
);
2182 spin_unlock_bh(&conn
->state_lock
);
2185 * Handle all different cid CLOSECONNECTION requests in
2186 * iscsit_logout_post_handler_diffcid() as to give enough
2187 * time for any non immediate command's CmdSN to be
2188 * acknowledged on the connection in question.
2190 * Here we simply make sure the CID is still around.
2192 l_conn
= iscsit_get_conn_from_cid(sess
,
2195 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2196 iscsit_add_cmd_to_response_queue(cmd
, conn
,
2201 iscsit_dec_conn_usage_count(l_conn
);
2204 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2209 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2211 struct iscsi_session
*sess
= conn
->sess
;
2213 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2214 " CID: %hu on CID: %hu.\n", cmd
->logout_cid
, conn
->cid
);
2216 if (sess
->sess_ops
->ErrorRecoveryLevel
!= 2) {
2217 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2218 " while ERL!=2.\n");
2219 cmd
->logout_response
= ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
;
2220 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2224 if (conn
->cid
== cmd
->logout_cid
) {
2225 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2226 " with CID: %hu on CID: %hu, implementation error.\n",
2227 cmd
->logout_cid
, conn
->cid
);
2228 cmd
->logout_response
= ISCSI_LOGOUT_CLEANUP_FAILED
;
2229 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2233 iscsit_add_cmd_to_response_queue(cmd
, conn
, cmd
->i_state
);
2239 iscsit_handle_logout_cmd(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
,
2242 int cmdsn_ret
, logout_remove
= 0;
2244 struct iscsi_logout
*hdr
;
2245 struct iscsi_tiqn
*tiqn
= iscsit_snmp_get_tiqn(conn
);
2247 hdr
= (struct iscsi_logout
*) buf
;
2248 reason_code
= (hdr
->flags
& 0x7f);
2251 spin_lock(&tiqn
->logout_stats
.lock
);
2252 if (reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
)
2253 tiqn
->logout_stats
.normal_logouts
++;
2255 tiqn
->logout_stats
.abnormal_logouts
++;
2256 spin_unlock(&tiqn
->logout_stats
.lock
);
2259 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2260 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2261 hdr
->itt
, hdr
->cmdsn
, hdr
->exp_statsn
, reason_code
,
2262 hdr
->cid
, conn
->cid
);
2264 if (conn
->conn_state
!= TARG_CONN_STATE_LOGGED_IN
) {
2265 pr_err("Received logout request on connection that"
2266 " is not in logged in state, ignoring request.\n");
2267 iscsit_free_cmd(cmd
, false);
2271 cmd
->iscsi_opcode
= ISCSI_OP_LOGOUT
;
2272 cmd
->i_state
= ISTATE_SEND_LOGOUTRSP
;
2273 cmd
->immediate_cmd
= ((hdr
->opcode
& ISCSI_OP_IMMEDIATE
) ? 1 : 0);
2274 conn
->sess
->init_task_tag
= cmd
->init_task_tag
= hdr
->itt
;
2275 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2276 cmd
->cmd_sn
= be32_to_cpu(hdr
->cmdsn
);
2277 cmd
->exp_stat_sn
= be32_to_cpu(hdr
->exp_statsn
);
2278 cmd
->logout_cid
= be16_to_cpu(hdr
->cid
);
2279 cmd
->logout_reason
= reason_code
;
2280 cmd
->data_direction
= DMA_NONE
;
2283 * We need to sleep in these cases (by returning 1) until the Logout
2284 * Response gets sent in the tx thread.
2286 if ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) ||
2287 ((reason_code
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
) &&
2288 be16_to_cpu(hdr
->cid
) == conn
->cid
))
2291 spin_lock_bh(&conn
->cmd_lock
);
2292 list_add_tail(&cmd
->i_conn_node
, &conn
->conn_cmd_list
);
2293 spin_unlock_bh(&conn
->cmd_lock
);
2295 if (reason_code
!= ISCSI_LOGOUT_REASON_RECOVERY
)
2296 iscsit_ack_from_expstatsn(conn
, be32_to_cpu(hdr
->exp_statsn
));
2299 * Immediate commands are executed, well, immediately.
2300 * Non-Immediate Logout Commands are executed in CmdSN order.
2302 if (cmd
->immediate_cmd
) {
2303 int ret
= iscsit_execute_cmd(cmd
, 0);
2308 cmdsn_ret
= iscsit_sequence_cmd(conn
, cmd
, buf
, hdr
->cmdsn
);
2309 if (cmdsn_ret
== CMDSN_LOWER_THAN_EXP
)
2311 else if (cmdsn_ret
== CMDSN_ERROR_CANNOT_RECOVER
)
2315 return logout_remove
;
2317 EXPORT_SYMBOL(iscsit_handle_logout_cmd
);
2319 static int iscsit_handle_snack(
2320 struct iscsi_conn
*conn
,
2323 struct iscsi_snack
*hdr
;
2325 hdr
= (struct iscsi_snack
*) buf
;
2326 hdr
->flags
&= ~ISCSI_FLAG_CMD_FINAL
;
2328 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2329 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2330 " CID: %hu\n", hdr
->itt
, hdr
->exp_statsn
, hdr
->flags
,
2331 hdr
->begrun
, hdr
->runlength
, conn
->cid
);
2333 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2334 pr_err("Initiator sent SNACK request while in"
2335 " ErrorRecoveryLevel=0.\n");
2336 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2340 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2341 * call from inside iscsi_send_recovery_datain_or_r2t().
2343 switch (hdr
->flags
& ISCSI_FLAG_SNACK_TYPE_MASK
) {
2345 return iscsit_handle_recovery_datain_or_r2t(conn
, buf
,
2347 be32_to_cpu(hdr
->ttt
),
2348 be32_to_cpu(hdr
->begrun
),
2349 be32_to_cpu(hdr
->runlength
));
2350 case ISCSI_FLAG_SNACK_TYPE_STATUS
:
2351 return iscsit_handle_status_snack(conn
, hdr
->itt
,
2352 be32_to_cpu(hdr
->ttt
),
2353 be32_to_cpu(hdr
->begrun
), be32_to_cpu(hdr
->runlength
));
2354 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK
:
2355 return iscsit_handle_data_ack(conn
, be32_to_cpu(hdr
->ttt
),
2356 be32_to_cpu(hdr
->begrun
),
2357 be32_to_cpu(hdr
->runlength
));
2358 case ISCSI_FLAG_SNACK_TYPE_RDATA
:
2359 /* FIXME: Support R-Data SNACK */
2360 pr_err("R-Data SNACK Not Supported.\n");
2361 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2364 pr_err("Unknown SNACK type 0x%02x, protocol"
2365 " error.\n", hdr
->flags
& 0x0f);
2366 return iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
2373 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2375 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2376 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2377 wait_for_completion_interruptible_timeout(
2378 &conn
->rx_half_close_comp
,
2379 ISCSI_RX_THREAD_TCP_TIMEOUT
* HZ
);
2383 static int iscsit_handle_immediate_data(
2384 struct iscsi_cmd
*cmd
,
2385 struct iscsi_scsi_req
*hdr
,
2388 int iov_ret
, rx_got
= 0, rx_size
= 0;
2389 u32 checksum
, iov_count
= 0, padding
= 0;
2390 struct iscsi_conn
*conn
= cmd
->conn
;
2393 iov_ret
= iscsit_map_iovec(cmd
, cmd
->iov_data
, cmd
->write_data_done
, length
);
2395 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2398 iov_count
= iov_ret
;
2399 iov
= &cmd
->iov_data
[0];
2401 padding
= ((-length
) & 3);
2403 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2404 iov
[iov_count
++].iov_len
= padding
;
2408 if (conn
->conn_ops
->DataDigest
) {
2409 iov
[iov_count
].iov_base
= &checksum
;
2410 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2411 rx_size
+= ISCSI_CRC_LEN
;
2414 rx_got
= rx_data(conn
, &cmd
->iov_data
[0], iov_count
, rx_size
);
2416 iscsit_unmap_iovec(cmd
);
2418 if (rx_got
!= rx_size
) {
2419 iscsit_rx_thread_wait_for_tcp(conn
);
2420 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2423 if (conn
->conn_ops
->DataDigest
) {
2426 data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_rx_hash
, cmd
,
2427 cmd
->write_data_done
, length
, padding
,
2430 if (checksum
!= data_crc
) {
2431 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2432 " does not match computed 0x%08x\n", checksum
,
2435 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
2436 pr_err("Unable to recover from"
2437 " Immediate Data digest failure while"
2439 iscsit_reject_cmd(cmd
,
2440 ISCSI_REASON_DATA_DIGEST_ERROR
,
2441 (unsigned char *)hdr
);
2442 return IMMEDIATE_DATA_CANNOT_RECOVER
;
2444 iscsit_reject_cmd(cmd
,
2445 ISCSI_REASON_DATA_DIGEST_ERROR
,
2446 (unsigned char *)hdr
);
2447 return IMMEDIATE_DATA_ERL1_CRC_FAILURE
;
2450 pr_debug("Got CRC32C DataDigest 0x%08x for"
2451 " %u bytes of Immediate Data\n", checksum
,
2456 cmd
->write_data_done
+= length
;
2458 if (cmd
->write_data_done
== cmd
->se_cmd
.data_length
) {
2459 spin_lock_bh(&cmd
->istate_lock
);
2460 cmd
->cmd_flags
|= ICF_GOT_LAST_DATAOUT
;
2461 cmd
->i_state
= ISTATE_RECEIVED_LAST_DATAOUT
;
2462 spin_unlock_bh(&cmd
->istate_lock
);
2465 return IMMEDIATE_DATA_NORMAL_OPERATION
;
2469 * Called with sess->conn_lock held.
2471 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2472 with active network interface */
2473 static void iscsit_build_conn_drop_async_message(struct iscsi_conn
*conn
)
2475 struct iscsi_cmd
*cmd
;
2476 struct iscsi_conn
*conn_p
;
2479 * Only send a Asynchronous Message on connections whos network
2480 * interface is still functional.
2482 list_for_each_entry(conn_p
, &conn
->sess
->sess_conn_list
, conn_list
) {
2483 if (conn_p
->conn_state
== TARG_CONN_STATE_LOGGED_IN
) {
2484 iscsit_inc_conn_usage_count(conn_p
);
2492 cmd
= iscsit_allocate_cmd(conn_p
, GFP_ATOMIC
);
2494 iscsit_dec_conn_usage_count(conn_p
);
2498 cmd
->logout_cid
= conn
->cid
;
2499 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2500 cmd
->i_state
= ISTATE_SEND_ASYNCMSG
;
2502 spin_lock_bh(&conn_p
->cmd_lock
);
2503 list_add_tail(&cmd
->i_conn_node
, &conn_p
->conn_cmd_list
);
2504 spin_unlock_bh(&conn_p
->cmd_lock
);
2506 iscsit_add_cmd_to_response_queue(cmd
, conn_p
, cmd
->i_state
);
2507 iscsit_dec_conn_usage_count(conn_p
);
2510 static int iscsit_send_conn_drop_async_message(
2511 struct iscsi_cmd
*cmd
,
2512 struct iscsi_conn
*conn
)
2514 struct iscsi_async
*hdr
;
2516 cmd
->tx_size
= ISCSI_HDR_LEN
;
2517 cmd
->iscsi_opcode
= ISCSI_OP_ASYNC_EVENT
;
2519 hdr
= (struct iscsi_async
*) cmd
->pdu
;
2520 hdr
->opcode
= ISCSI_OP_ASYNC_EVENT
;
2521 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
2522 cmd
->init_task_tag
= RESERVED_ITT
;
2523 cmd
->targ_xfer_tag
= 0xFFFFFFFF;
2524 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->rsvd4
[0]);
2525 cmd
->stat_sn
= conn
->stat_sn
++;
2526 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2527 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2528 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2529 hdr
->async_event
= ISCSI_ASYNC_MSG_DROPPING_CONNECTION
;
2530 hdr
->param1
= cpu_to_be16(cmd
->logout_cid
);
2531 hdr
->param2
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Wait
);
2532 hdr
->param3
= cpu_to_be16(conn
->sess
->sess_ops
->DefaultTime2Retain
);
2534 if (conn
->conn_ops
->HeaderDigest
) {
2535 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2537 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
2538 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2540 cmd
->tx_size
+= ISCSI_CRC_LEN
;
2541 pr_debug("Attaching CRC32C HeaderDigest to"
2542 " Async Message 0x%08x\n", *header_digest
);
2545 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2546 cmd
->iov_misc
[0].iov_len
= cmd
->tx_size
;
2547 cmd
->iov_misc_count
= 1;
2549 pr_debug("Sending Connection Dropped Async Message StatSN:"
2550 " 0x%08x, for CID: %hu on CID: %hu\n", cmd
->stat_sn
,
2551 cmd
->logout_cid
, conn
->cid
);
2555 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn
*conn
)
2557 if ((conn
->sock
->sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2558 (conn
->sock
->sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
2559 wait_for_completion_interruptible_timeout(
2560 &conn
->tx_half_close_comp
,
2561 ISCSI_TX_THREAD_TCP_TIMEOUT
* HZ
);
2566 iscsit_build_datain_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2567 struct iscsi_datain
*datain
, struct iscsi_data_rsp
*hdr
,
2570 hdr
->opcode
= ISCSI_OP_SCSI_DATA_IN
;
2571 hdr
->flags
= datain
->flags
;
2572 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
2573 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
2574 hdr
->flags
|= ISCSI_FLAG_DATA_OVERFLOW
;
2575 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2576 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
2577 hdr
->flags
|= ISCSI_FLAG_DATA_UNDERFLOW
;
2578 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
2581 hton24(hdr
->dlength
, datain
->length
);
2582 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2583 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
2584 (struct scsi_lun
*)&hdr
->lun
);
2586 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2588 hdr
->itt
= cmd
->init_task_tag
;
2590 if (hdr
->flags
& ISCSI_FLAG_DATA_ACK
)
2591 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2593 hdr
->ttt
= cpu_to_be32(0xFFFFFFFF);
2595 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2597 hdr
->statsn
= cpu_to_be32(0xFFFFFFFF);
2599 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2600 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2601 hdr
->datasn
= cpu_to_be32(datain
->data_sn
);
2602 hdr
->offset
= cpu_to_be32(datain
->offset
);
2604 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2605 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2606 cmd
->init_task_tag
, ntohl(hdr
->statsn
), ntohl(hdr
->datasn
),
2607 ntohl(hdr
->offset
), datain
->length
, conn
->cid
);
2610 static int iscsit_send_datain(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2612 struct iscsi_data_rsp
*hdr
= (struct iscsi_data_rsp
*)&cmd
->pdu
[0];
2613 struct iscsi_datain datain
;
2614 struct iscsi_datain_req
*dr
;
2616 u32 iov_count
= 0, tx_size
= 0;
2617 int eodr
= 0, ret
, iov_ret
;
2618 bool set_statsn
= false;
2620 memset(&datain
, 0, sizeof(struct iscsi_datain
));
2621 dr
= iscsit_get_datain_values(cmd
, &datain
);
2623 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2624 cmd
->init_task_tag
);
2628 * Be paranoid and double check the logic for now.
2630 if ((datain
.offset
+ datain
.length
) > cmd
->se_cmd
.data_length
) {
2631 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2632 " datain.length: %u exceeds cmd->data_length: %u\n",
2633 cmd
->init_task_tag
, datain
.offset
, datain
.length
,
2634 cmd
->se_cmd
.data_length
);
2638 spin_lock_bh(&conn
->sess
->session_stats_lock
);
2639 conn
->sess
->tx_data_octets
+= datain
.length
;
2640 if (conn
->sess
->se_sess
->se_node_acl
) {
2641 spin_lock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
2642 conn
->sess
->se_sess
->se_node_acl
->read_bytes
+= datain
.length
;
2643 spin_unlock(&conn
->sess
->se_sess
->se_node_acl
->stats_lock
);
2645 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
2647 * Special case for successfully execution w/ both DATAIN
2650 if ((datain
.flags
& ISCSI_FLAG_DATA_STATUS
) &&
2651 (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
))
2652 datain
.flags
&= ~ISCSI_FLAG_DATA_STATUS
;
2654 if ((dr
->dr_complete
== DATAIN_COMPLETE_NORMAL
) ||
2655 (dr
->dr_complete
== DATAIN_COMPLETE_CONNECTION_RECOVERY
)) {
2656 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2657 cmd
->stat_sn
= conn
->stat_sn
++;
2659 } else if (dr
->dr_complete
==
2660 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY
)
2664 iscsit_build_datain_pdu(cmd
, conn
, &datain
, hdr
, set_statsn
);
2666 iov
= &cmd
->iov_data
[0];
2667 iov
[iov_count
].iov_base
= cmd
->pdu
;
2668 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
2669 tx_size
+= ISCSI_HDR_LEN
;
2671 if (conn
->conn_ops
->HeaderDigest
) {
2672 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2674 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, cmd
->pdu
,
2675 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2677 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2678 tx_size
+= ISCSI_CRC_LEN
;
2680 pr_debug("Attaching CRC32 HeaderDigest"
2681 " for DataIN PDU 0x%08x\n", *header_digest
);
2684 iov_ret
= iscsit_map_iovec(cmd
, &cmd
->iov_data
[1],
2685 datain
.offset
, datain
.length
);
2689 iov_count
+= iov_ret
;
2690 tx_size
+= datain
.length
;
2692 cmd
->padding
= ((-datain
.length
) & 3);
2694 iov
[iov_count
].iov_base
= cmd
->pad_bytes
;
2695 iov
[iov_count
++].iov_len
= cmd
->padding
;
2696 tx_size
+= cmd
->padding
;
2698 pr_debug("Attaching %u padding bytes\n",
2701 if (conn
->conn_ops
->DataDigest
) {
2702 cmd
->data_crc
= iscsit_do_crypto_hash_sg(&conn
->conn_tx_hash
, cmd
,
2703 datain
.offset
, datain
.length
, cmd
->padding
, cmd
->pad_bytes
);
2705 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
2706 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
2707 tx_size
+= ISCSI_CRC_LEN
;
2709 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2710 " 0x%08x\n", datain
.length
+cmd
->padding
, cmd
->data_crc
);
2713 cmd
->iov_data_count
= iov_count
;
2714 cmd
->tx_size
= tx_size
;
2716 /* sendpage is preferred but can't insert markers */
2717 if (!conn
->conn_ops
->IFMarker
)
2718 ret
= iscsit_fe_sendpage_sg(cmd
, conn
);
2720 ret
= iscsit_send_tx_data(cmd
, conn
, 0);
2722 iscsit_unmap_iovec(cmd
);
2725 iscsit_tx_thread_wait_for_tcp(conn
);
2729 if (dr
->dr_complete
) {
2730 eodr
= (cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ?
2732 iscsit_free_datain_req(cmd
, dr
);
2739 iscsit_build_logout_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2740 struct iscsi_logout_rsp
*hdr
)
2742 struct iscsi_conn
*logout_conn
= NULL
;
2743 struct iscsi_conn_recovery
*cr
= NULL
;
2744 struct iscsi_session
*sess
= conn
->sess
;
2746 * The actual shutting down of Sessions and/or Connections
2747 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2748 * is done in scsi_logout_post_handler().
2750 switch (cmd
->logout_reason
) {
2751 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
2752 pr_debug("iSCSI session logout successful, setting"
2753 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2754 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2756 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
2757 if (cmd
->logout_response
== ISCSI_LOGOUT_CID_NOT_FOUND
)
2760 * For CLOSECONNECTION logout requests carrying
2761 * a matching logout CID -> local CID, the reference
2762 * for the local CID will have been incremented in
2763 * iscsi_logout_closeconnection().
2765 * For CLOSECONNECTION logout requests carrying
2766 * a different CID than the connection it arrived
2767 * on, the connection responding to cmd->logout_cid
2768 * is stopped in iscsit_logout_post_handler_diffcid().
2771 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2772 " successful.\n", cmd
->logout_cid
, conn
->cid
);
2773 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2775 case ISCSI_LOGOUT_REASON_RECOVERY
:
2776 if ((cmd
->logout_response
== ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
) ||
2777 (cmd
->logout_response
== ISCSI_LOGOUT_CLEANUP_FAILED
))
2780 * If the connection is still active from our point of view
2781 * force connection recovery to occur.
2783 logout_conn
= iscsit_get_conn_from_cid_rcfr(sess
,
2786 iscsit_connection_reinstatement_rcfr(logout_conn
);
2787 iscsit_dec_conn_usage_count(logout_conn
);
2790 cr
= iscsit_get_inactive_connection_recovery_entry(
2791 conn
->sess
, cmd
->logout_cid
);
2793 pr_err("Unable to locate CID: %hu for"
2794 " REMOVECONNFORRECOVERY Logout Request.\n",
2796 cmd
->logout_response
= ISCSI_LOGOUT_CID_NOT_FOUND
;
2800 iscsit_discard_cr_cmds_by_expstatsn(cr
, cmd
->exp_stat_sn
);
2802 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2803 " for recovery for CID: %hu on CID: %hu successful.\n",
2804 cmd
->logout_cid
, conn
->cid
);
2805 cmd
->logout_response
= ISCSI_LOGOUT_SUCCESS
;
2808 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2809 cmd
->logout_reason
);
2813 hdr
->opcode
= ISCSI_OP_LOGOUT_RSP
;
2814 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2815 hdr
->response
= cmd
->logout_response
;
2816 hdr
->itt
= cmd
->init_task_tag
;
2817 cmd
->stat_sn
= conn
->stat_sn
++;
2818 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2820 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2821 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2822 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2824 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2825 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2826 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
,
2827 cmd
->logout_cid
, conn
->cid
);
2831 EXPORT_SYMBOL(iscsit_build_logout_rsp
);
2834 iscsit_send_logout(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2837 int niov
= 0, tx_size
, rc
;
2839 rc
= iscsit_build_logout_rsp(cmd
, conn
,
2840 (struct iscsi_logout_rsp
*)&cmd
->pdu
[0]);
2844 tx_size
= ISCSI_HDR_LEN
;
2845 iov
= &cmd
->iov_misc
[0];
2846 iov
[niov
].iov_base
= cmd
->pdu
;
2847 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
2849 if (conn
->conn_ops
->HeaderDigest
) {
2850 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2852 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, &cmd
->pdu
[0],
2853 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2855 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2856 tx_size
+= ISCSI_CRC_LEN
;
2857 pr_debug("Attaching CRC32C HeaderDigest to"
2858 " Logout Response 0x%08x\n", *header_digest
);
2860 cmd
->iov_misc_count
= niov
;
2861 cmd
->tx_size
= tx_size
;
2867 iscsit_build_nopin_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
2868 struct iscsi_nopin
*hdr
, bool nopout_response
)
2870 hdr
->opcode
= ISCSI_OP_NOOP_IN
;
2871 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
2872 hton24(hdr
->dlength
, cmd
->buf_ptr_size
);
2873 if (nopout_response
)
2874 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL
, &hdr
->lun
);
2875 hdr
->itt
= cmd
->init_task_tag
;
2876 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
2877 cmd
->stat_sn
= (nopout_response
) ? conn
->stat_sn
++ :
2879 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
2881 if (nopout_response
)
2882 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
2884 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
2885 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
2887 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2888 " StatSN: 0x%08x, Length %u\n", (nopout_response
) ?
2889 "Solicitied" : "Unsolicitied", cmd
->init_task_tag
,
2890 cmd
->targ_xfer_tag
, cmd
->stat_sn
, cmd
->buf_ptr_size
);
2892 EXPORT_SYMBOL(iscsit_build_nopin_rsp
);
2895 * Unsolicited NOPIN, either requesting a response or not.
2897 static int iscsit_send_unsolicited_nopin(
2898 struct iscsi_cmd
*cmd
,
2899 struct iscsi_conn
*conn
,
2902 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
2903 int tx_size
= ISCSI_HDR_LEN
, ret
;
2905 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, false);
2907 if (conn
->conn_ops
->HeaderDigest
) {
2908 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2910 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
2911 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2913 tx_size
+= ISCSI_CRC_LEN
;
2914 pr_debug("Attaching CRC32C HeaderDigest to"
2915 " NopIN 0x%08x\n", *header_digest
);
2918 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
2919 cmd
->iov_misc
[0].iov_len
= tx_size
;
2920 cmd
->iov_misc_count
= 1;
2921 cmd
->tx_size
= tx_size
;
2923 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2924 " 0x%08x CID: %hu\n", hdr
->ttt
, cmd
->stat_sn
, conn
->cid
);
2926 ret
= iscsit_send_tx_data(cmd
, conn
, 1);
2928 iscsit_tx_thread_wait_for_tcp(conn
);
2932 spin_lock_bh(&cmd
->istate_lock
);
2933 cmd
->i_state
= want_response
?
2934 ISTATE_SENT_NOPIN_WANT_RESPONSE
: ISTATE_SENT_STATUS
;
2935 spin_unlock_bh(&cmd
->istate_lock
);
2941 iscsit_send_nopin(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
2943 struct iscsi_nopin
*hdr
= (struct iscsi_nopin
*)&cmd
->pdu
[0];
2946 int niov
= 0, tx_size
;
2948 iscsit_build_nopin_rsp(cmd
, conn
, hdr
, true);
2950 tx_size
= ISCSI_HDR_LEN
;
2951 iov
= &cmd
->iov_misc
[0];
2952 iov
[niov
].iov_base
= cmd
->pdu
;
2953 iov
[niov
++].iov_len
= ISCSI_HDR_LEN
;
2955 if (conn
->conn_ops
->HeaderDigest
) {
2956 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
2958 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
2959 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
2961 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
2962 tx_size
+= ISCSI_CRC_LEN
;
2963 pr_debug("Attaching CRC32C HeaderDigest"
2964 " to NopIn 0x%08x\n", *header_digest
);
2968 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2969 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2971 if (cmd
->buf_ptr_size
) {
2972 iov
[niov
].iov_base
= cmd
->buf_ptr
;
2973 iov
[niov
++].iov_len
= cmd
->buf_ptr_size
;
2974 tx_size
+= cmd
->buf_ptr_size
;
2976 pr_debug("Echoing back %u bytes of ping"
2977 " data.\n", cmd
->buf_ptr_size
);
2979 padding
= ((-cmd
->buf_ptr_size
) & 3);
2981 iov
[niov
].iov_base
= &cmd
->pad_bytes
;
2982 iov
[niov
++].iov_len
= padding
;
2984 pr_debug("Attaching %u additional"
2985 " padding bytes.\n", padding
);
2987 if (conn
->conn_ops
->DataDigest
) {
2988 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
2989 cmd
->buf_ptr
, cmd
->buf_ptr_size
,
2990 padding
, (u8
*)&cmd
->pad_bytes
,
2991 (u8
*)&cmd
->data_crc
);
2993 iov
[niov
].iov_base
= &cmd
->data_crc
;
2994 iov
[niov
++].iov_len
= ISCSI_CRC_LEN
;
2995 tx_size
+= ISCSI_CRC_LEN
;
2996 pr_debug("Attached DataDigest for %u"
2997 " bytes of ping data, CRC 0x%08x\n",
2998 cmd
->buf_ptr_size
, cmd
->data_crc
);
3002 cmd
->iov_misc_count
= niov
;
3003 cmd
->tx_size
= tx_size
;
3008 static int iscsit_send_r2t(
3009 struct iscsi_cmd
*cmd
,
3010 struct iscsi_conn
*conn
)
3013 struct iscsi_r2t
*r2t
;
3014 struct iscsi_r2t_rsp
*hdr
;
3017 r2t
= iscsit_get_r2t_from_list(cmd
);
3021 hdr
= (struct iscsi_r2t_rsp
*) cmd
->pdu
;
3022 memset(hdr
, 0, ISCSI_HDR_LEN
);
3023 hdr
->opcode
= ISCSI_OP_R2T
;
3024 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3025 int_to_scsilun(cmd
->se_cmd
.orig_fe_lun
,
3026 (struct scsi_lun
*)&hdr
->lun
);
3027 hdr
->itt
= cmd
->init_task_tag
;
3028 spin_lock_bh(&conn
->sess
->ttt_lock
);
3029 r2t
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
3030 if (r2t
->targ_xfer_tag
== 0xFFFFFFFF)
3031 r2t
->targ_xfer_tag
= conn
->sess
->targ_xfer_tag
++;
3032 spin_unlock_bh(&conn
->sess
->ttt_lock
);
3033 hdr
->ttt
= cpu_to_be32(r2t
->targ_xfer_tag
);
3034 hdr
->statsn
= cpu_to_be32(conn
->stat_sn
);
3035 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3036 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3037 hdr
->r2tsn
= cpu_to_be32(r2t
->r2t_sn
);
3038 hdr
->data_offset
= cpu_to_be32(r2t
->offset
);
3039 hdr
->data_length
= cpu_to_be32(r2t
->xfer_len
);
3041 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
3042 cmd
->iov_misc
[0].iov_len
= ISCSI_HDR_LEN
;
3043 tx_size
+= ISCSI_HDR_LEN
;
3045 if (conn
->conn_ops
->HeaderDigest
) {
3046 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3048 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3049 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3051 cmd
->iov_misc
[0].iov_len
+= ISCSI_CRC_LEN
;
3052 tx_size
+= ISCSI_CRC_LEN
;
3053 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3054 " PDU 0x%08x\n", *header_digest
);
3057 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3058 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3059 (!r2t
->recovery_r2t
) ? "" : "Recovery ", cmd
->init_task_tag
,
3060 r2t
->targ_xfer_tag
, ntohl(hdr
->statsn
), r2t
->r2t_sn
,
3061 r2t
->offset
, r2t
->xfer_len
, conn
->cid
);
3063 cmd
->iov_misc_count
= 1;
3064 cmd
->tx_size
= tx_size
;
3066 spin_lock_bh(&cmd
->r2t_lock
);
3068 spin_unlock_bh(&cmd
->r2t_lock
);
3070 ret
= iscsit_send_tx_data(cmd
, conn
, 1);
3072 iscsit_tx_thread_wait_for_tcp(conn
);
3076 spin_lock_bh(&cmd
->dataout_timeout_lock
);
3077 iscsit_start_dataout_timer(cmd
, conn
);
3078 spin_unlock_bh(&cmd
->dataout_timeout_lock
);
3084 * @recovery: If called from iscsi_task_reassign_complete_write() for
3085 * connection recovery.
3087 int iscsit_build_r2ts_for_cmd(
3088 struct iscsi_conn
*conn
,
3089 struct iscsi_cmd
*cmd
,
3093 u32 offset
= 0, xfer_len
= 0;
3095 spin_lock_bh(&cmd
->r2t_lock
);
3096 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
) {
3097 spin_unlock_bh(&cmd
->r2t_lock
);
3101 if (conn
->sess
->sess_ops
->DataSequenceInOrder
&&
3103 cmd
->r2t_offset
= max(cmd
->r2t_offset
, cmd
->write_data_done
);
3105 while (cmd
->outstanding_r2ts
< conn
->sess
->sess_ops
->MaxOutstandingR2T
) {
3106 if (conn
->sess
->sess_ops
->DataSequenceInOrder
) {
3107 offset
= cmd
->r2t_offset
;
3109 if (first_r2t
&& recovery
) {
3110 int new_data_end
= offset
+
3111 conn
->sess
->sess_ops
->MaxBurstLength
-
3112 cmd
->next_burst_len
;
3114 if (new_data_end
> cmd
->se_cmd
.data_length
)
3115 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3118 conn
->sess
->sess_ops
->MaxBurstLength
-
3119 cmd
->next_burst_len
;
3121 int new_data_end
= offset
+
3122 conn
->sess
->sess_ops
->MaxBurstLength
;
3124 if (new_data_end
> cmd
->se_cmd
.data_length
)
3125 xfer_len
= cmd
->se_cmd
.data_length
- offset
;
3127 xfer_len
= conn
->sess
->sess_ops
->MaxBurstLength
;
3129 cmd
->r2t_offset
+= xfer_len
;
3131 if (cmd
->r2t_offset
== cmd
->se_cmd
.data_length
)
3132 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3134 struct iscsi_seq
*seq
;
3136 seq
= iscsit_get_seq_holder_for_r2t(cmd
);
3138 spin_unlock_bh(&cmd
->r2t_lock
);
3142 offset
= seq
->offset
;
3143 xfer_len
= seq
->xfer_len
;
3145 if (cmd
->seq_send_order
== cmd
->seq_count
)
3146 cmd
->cmd_flags
|= ICF_SENT_LAST_R2T
;
3148 cmd
->outstanding_r2ts
++;
3151 if (iscsit_add_r2t_to_list(cmd
, offset
, xfer_len
, 0, 0) < 0) {
3152 spin_unlock_bh(&cmd
->r2t_lock
);
3156 if (cmd
->cmd_flags
& ICF_SENT_LAST_R2T
)
3159 spin_unlock_bh(&cmd
->r2t_lock
);
3164 void iscsit_build_rsp_pdu(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3165 bool inc_stat_sn
, struct iscsi_scsi_rsp
*hdr
)
3168 cmd
->stat_sn
= conn
->stat_sn
++;
3170 spin_lock_bh(&conn
->sess
->session_stats_lock
);
3171 conn
->sess
->rsp_pdus
++;
3172 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
3174 memset(hdr
, 0, ISCSI_HDR_LEN
);
3175 hdr
->opcode
= ISCSI_OP_SCSI_CMD_RSP
;
3176 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3177 if (cmd
->se_cmd
.se_cmd_flags
& SCF_OVERFLOW_BIT
) {
3178 hdr
->flags
|= ISCSI_FLAG_CMD_OVERFLOW
;
3179 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3180 } else if (cmd
->se_cmd
.se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
3181 hdr
->flags
|= ISCSI_FLAG_CMD_UNDERFLOW
;
3182 hdr
->residual_count
= cpu_to_be32(cmd
->se_cmd
.residual_count
);
3184 hdr
->response
= cmd
->iscsi_response
;
3185 hdr
->cmd_status
= cmd
->se_cmd
.scsi_status
;
3186 hdr
->itt
= cmd
->init_task_tag
;
3187 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3189 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3190 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3191 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3193 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3194 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3195 cmd
->init_task_tag
, cmd
->stat_sn
, cmd
->se_cmd
.scsi_status
,
3196 cmd
->se_cmd
.scsi_status
, conn
->cid
);
3198 EXPORT_SYMBOL(iscsit_build_rsp_pdu
);
3200 static int iscsit_send_response(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3202 struct iscsi_scsi_rsp
*hdr
= (struct iscsi_scsi_rsp
*)&cmd
->pdu
[0];
3204 u32 padding
= 0, tx_size
= 0;
3206 bool inc_stat_sn
= (cmd
->i_state
== ISTATE_SEND_STATUS
);
3208 iscsit_build_rsp_pdu(cmd
, conn
, inc_stat_sn
, hdr
);
3210 iov
= &cmd
->iov_misc
[0];
3211 iov
[iov_count
].iov_base
= cmd
->pdu
;
3212 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3213 tx_size
+= ISCSI_HDR_LEN
;
3216 * Attach SENSE DATA payload to iSCSI Response PDU
3218 if (cmd
->se_cmd
.sense_buffer
&&
3219 ((cmd
->se_cmd
.se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
3220 (cmd
->se_cmd
.se_cmd_flags
& SCF_EMULATED_TASK_SENSE
))) {
3221 put_unaligned_be16(cmd
->se_cmd
.scsi_sense_length
, cmd
->sense_buffer
);
3222 cmd
->se_cmd
.scsi_sense_length
+= sizeof (__be16
);
3224 padding
= -(cmd
->se_cmd
.scsi_sense_length
) & 3;
3225 hton24(hdr
->dlength
, (u32
)cmd
->se_cmd
.scsi_sense_length
);
3226 iov
[iov_count
].iov_base
= cmd
->sense_buffer
;
3227 iov
[iov_count
++].iov_len
=
3228 (cmd
->se_cmd
.scsi_sense_length
+ padding
);
3229 tx_size
+= cmd
->se_cmd
.scsi_sense_length
;
3232 memset(cmd
->sense_buffer
+
3233 cmd
->se_cmd
.scsi_sense_length
, 0, padding
);
3235 pr_debug("Adding %u bytes of padding to"
3236 " SENSE.\n", padding
);
3239 if (conn
->conn_ops
->DataDigest
) {
3240 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3242 (cmd
->se_cmd
.scsi_sense_length
+ padding
),
3243 0, NULL
, (u8
*)&cmd
->data_crc
);
3245 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3246 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3247 tx_size
+= ISCSI_CRC_LEN
;
3249 pr_debug("Attaching CRC32 DataDigest for"
3250 " SENSE, %u bytes CRC 0x%08x\n",
3251 (cmd
->se_cmd
.scsi_sense_length
+ padding
),
3255 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3257 cmd
->se_cmd
.scsi_sense_length
);
3260 if (conn
->conn_ops
->HeaderDigest
) {
3261 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3263 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, cmd
->pdu
,
3264 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3266 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3267 tx_size
+= ISCSI_CRC_LEN
;
3268 pr_debug("Attaching CRC32 HeaderDigest for Response"
3269 " PDU 0x%08x\n", *header_digest
);
3272 cmd
->iov_misc_count
= iov_count
;
3273 cmd
->tx_size
= tx_size
;
3278 static u8
iscsit_convert_tcm_tmr_rsp(struct se_tmr_req
*se_tmr
)
3280 switch (se_tmr
->response
) {
3281 case TMR_FUNCTION_COMPLETE
:
3282 return ISCSI_TMF_RSP_COMPLETE
;
3283 case TMR_TASK_DOES_NOT_EXIST
:
3284 return ISCSI_TMF_RSP_NO_TASK
;
3285 case TMR_LUN_DOES_NOT_EXIST
:
3286 return ISCSI_TMF_RSP_NO_LUN
;
3287 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
:
3288 return ISCSI_TMF_RSP_NOT_SUPPORTED
;
3289 case TMR_FUNCTION_REJECTED
:
3291 return ISCSI_TMF_RSP_REJECTED
;
3296 iscsit_build_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3297 struct iscsi_tm_rsp
*hdr
)
3299 struct se_tmr_req
*se_tmr
= cmd
->se_cmd
.se_tmr_req
;
3301 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC_RSP
;
3302 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
3303 hdr
->response
= iscsit_convert_tcm_tmr_rsp(se_tmr
);
3304 hdr
->itt
= cmd
->init_task_tag
;
3305 cmd
->stat_sn
= conn
->stat_sn
++;
3306 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3308 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3309 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3310 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3312 pr_debug("Built Task Management Response ITT: 0x%08x,"
3313 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3314 cmd
->init_task_tag
, cmd
->stat_sn
, hdr
->response
, conn
->cid
);
3316 EXPORT_SYMBOL(iscsit_build_task_mgt_rsp
);
3319 iscsit_send_task_mgt_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
)
3321 struct iscsi_tm_rsp
*hdr
= (struct iscsi_tm_rsp
*)&cmd
->pdu
[0];
3324 iscsit_build_task_mgt_rsp(cmd
, conn
, hdr
);
3326 cmd
->iov_misc
[0].iov_base
= cmd
->pdu
;
3327 cmd
->iov_misc
[0].iov_len
= ISCSI_HDR_LEN
;
3328 tx_size
+= ISCSI_HDR_LEN
;
3330 if (conn
->conn_ops
->HeaderDigest
) {
3331 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3333 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3334 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3336 cmd
->iov_misc
[0].iov_len
+= ISCSI_CRC_LEN
;
3337 tx_size
+= ISCSI_CRC_LEN
;
3338 pr_debug("Attaching CRC32 HeaderDigest for Task"
3339 " Mgmt Response PDU 0x%08x\n", *header_digest
);
3342 cmd
->iov_misc_count
= 1;
3343 cmd
->tx_size
= tx_size
;
3348 static bool iscsit_check_inaddr_any(struct iscsi_np
*np
)
3352 if (np
->np_sockaddr
.ss_family
== AF_INET6
) {
3353 const struct sockaddr_in6 sin6
= {
3354 .sin6_addr
= IN6ADDR_ANY_INIT
};
3355 struct sockaddr_in6
*sock_in6
=
3356 (struct sockaddr_in6
*)&np
->np_sockaddr
;
3358 if (!memcmp(sock_in6
->sin6_addr
.s6_addr
,
3359 sin6
.sin6_addr
.s6_addr
, 16))
3362 struct sockaddr_in
* sock_in
=
3363 (struct sockaddr_in
*)&np
->np_sockaddr
;
3365 if (sock_in
->sin_addr
.s_addr
== htonl(INADDR_ANY
))
3372 #define SENDTARGETS_BUF_LIMIT 32768U
3374 static int iscsit_build_sendtargets_response(struct iscsi_cmd
*cmd
)
3376 char *payload
= NULL
;
3377 struct iscsi_conn
*conn
= cmd
->conn
;
3378 struct iscsi_portal_group
*tpg
;
3379 struct iscsi_tiqn
*tiqn
;
3380 struct iscsi_tpg_np
*tpg_np
;
3381 int buffer_len
, end_of_buf
= 0, len
= 0, payload_len
= 0;
3382 unsigned char buf
[ISCSI_IQN_LEN
+12]; /* iqn + "TargetName=" + \0 */
3383 unsigned char *text_in
= cmd
->text_in_ptr
, *text_ptr
= NULL
;
3385 buffer_len
= max(conn
->conn_ops
->MaxRecvDataSegmentLength
,
3386 SENDTARGETS_BUF_LIMIT
);
3388 payload
= kzalloc(buffer_len
, GFP_KERNEL
);
3390 pr_err("Unable to allocate memory for sendtargets"
3395 * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE
3398 if (cmd
->cmd_flags
& IFC_SENDTARGETS_SINGLE
) {
3399 text_ptr
= strchr(text_in
, '=');
3401 pr_err("Unable to locate '=' string in text_in:"
3407 * Skip over '=' character..
3412 spin_lock(&tiqn_lock
);
3413 list_for_each_entry(tiqn
, &g_tiqn_list
, tiqn_list
) {
3414 if ((cmd
->cmd_flags
& IFC_SENDTARGETS_SINGLE
) &&
3415 strcmp(tiqn
->tiqn
, text_ptr
)) {
3419 len
= sprintf(buf
, "TargetName=%s", tiqn
->tiqn
);
3422 if ((len
+ payload_len
) > buffer_len
) {
3426 memcpy(payload
+ payload_len
, buf
, len
);
3429 spin_lock(&tiqn
->tiqn_tpg_lock
);
3430 list_for_each_entry(tpg
, &tiqn
->tiqn_tpg_list
, tpg_list
) {
3432 spin_lock(&tpg
->tpg_state_lock
);
3433 if ((tpg
->tpg_state
== TPG_STATE_FREE
) ||
3434 (tpg
->tpg_state
== TPG_STATE_INACTIVE
)) {
3435 spin_unlock(&tpg
->tpg_state_lock
);
3438 spin_unlock(&tpg
->tpg_state_lock
);
3440 spin_lock(&tpg
->tpg_np_lock
);
3441 list_for_each_entry(tpg_np
, &tpg
->tpg_gnp_list
,
3443 struct iscsi_np
*np
= tpg_np
->tpg_np
;
3444 bool inaddr_any
= iscsit_check_inaddr_any(np
);
3446 len
= sprintf(buf
, "TargetAddress="
3448 (np
->np_sockaddr
.ss_family
== AF_INET6
) ?
3449 "[" : "", (inaddr_any
== false) ?
3450 np
->np_ip
: conn
->local_ip
,
3451 (np
->np_sockaddr
.ss_family
== AF_INET6
) ?
3452 "]" : "", (inaddr_any
== false) ?
3453 np
->np_port
: conn
->local_port
,
3457 if ((len
+ payload_len
) > buffer_len
) {
3458 spin_unlock(&tpg
->tpg_np_lock
);
3459 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3463 memcpy(payload
+ payload_len
, buf
, len
);
3466 spin_unlock(&tpg
->tpg_np_lock
);
3468 spin_unlock(&tiqn
->tiqn_tpg_lock
);
3473 if (cmd
->cmd_flags
& IFC_SENDTARGETS_SINGLE
)
3476 spin_unlock(&tiqn_lock
);
3478 cmd
->buf_ptr
= payload
;
3484 iscsit_build_text_rsp(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3485 struct iscsi_text_rsp
*hdr
)
3487 int text_length
, padding
;
3489 text_length
= iscsit_build_sendtargets_response(cmd
);
3490 if (text_length
< 0)
3493 hdr
->opcode
= ISCSI_OP_TEXT_RSP
;
3494 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3495 padding
= ((-text_length
) & 3);
3496 hton24(hdr
->dlength
, text_length
);
3497 hdr
->itt
= cmd
->init_task_tag
;
3498 hdr
->ttt
= cpu_to_be32(cmd
->targ_xfer_tag
);
3499 cmd
->stat_sn
= conn
->stat_sn
++;
3500 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3502 iscsit_increment_maxcmdsn(cmd
, conn
->sess
);
3503 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3504 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3506 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3507 " Length: %u, CID: %hu\n", cmd
->init_task_tag
, cmd
->stat_sn
,
3508 text_length
, conn
->cid
);
3510 return text_length
+ padding
;
3512 EXPORT_SYMBOL(iscsit_build_text_rsp
);
3515 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3516 * MaxRecvDataSegmentLength.
3518 static int iscsit_send_text_rsp(
3519 struct iscsi_cmd
*cmd
,
3520 struct iscsi_conn
*conn
)
3522 struct iscsi_text_rsp
*hdr
= (struct iscsi_text_rsp
*)cmd
->pdu
;
3525 int text_length
, iov_count
= 0, rc
;
3527 rc
= iscsit_build_text_rsp(cmd
, conn
, hdr
);
3532 iov
= &cmd
->iov_misc
[0];
3533 iov
[iov_count
].iov_base
= cmd
->pdu
;
3534 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3535 iov
[iov_count
].iov_base
= cmd
->buf_ptr
;
3536 iov
[iov_count
++].iov_len
= text_length
;
3538 tx_size
+= (ISCSI_HDR_LEN
+ text_length
);
3540 if (conn
->conn_ops
->HeaderDigest
) {
3541 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3543 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3544 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3546 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3547 tx_size
+= ISCSI_CRC_LEN
;
3548 pr_debug("Attaching CRC32 HeaderDigest for"
3549 " Text Response PDU 0x%08x\n", *header_digest
);
3552 if (conn
->conn_ops
->DataDigest
) {
3553 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
,
3554 cmd
->buf_ptr
, text_length
,
3555 0, NULL
, (u8
*)&cmd
->data_crc
);
3557 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3558 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3559 tx_size
+= ISCSI_CRC_LEN
;
3561 pr_debug("Attaching DataDigest for %u bytes of text"
3562 " data, CRC 0x%08x\n", text_length
,
3566 cmd
->iov_misc_count
= iov_count
;
3567 cmd
->tx_size
= tx_size
;
3573 iscsit_build_reject(struct iscsi_cmd
*cmd
, struct iscsi_conn
*conn
,
3574 struct iscsi_reject
*hdr
)
3576 hdr
->opcode
= ISCSI_OP_REJECT
;
3577 hdr
->reason
= cmd
->reject_reason
;
3578 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
3579 hton24(hdr
->dlength
, ISCSI_HDR_LEN
);
3580 hdr
->ffffffff
= cpu_to_be32(0xffffffff);
3581 cmd
->stat_sn
= conn
->stat_sn
++;
3582 hdr
->statsn
= cpu_to_be32(cmd
->stat_sn
);
3583 hdr
->exp_cmdsn
= cpu_to_be32(conn
->sess
->exp_cmd_sn
);
3584 hdr
->max_cmdsn
= cpu_to_be32(conn
->sess
->max_cmd_sn
);
3587 EXPORT_SYMBOL(iscsit_build_reject
);
3589 static int iscsit_send_reject(
3590 struct iscsi_cmd
*cmd
,
3591 struct iscsi_conn
*conn
)
3593 struct iscsi_reject
*hdr
= (struct iscsi_reject
*)&cmd
->pdu
[0];
3595 u32 iov_count
= 0, tx_size
;
3597 iscsit_build_reject(cmd
, conn
, hdr
);
3599 iov
= &cmd
->iov_misc
[0];
3600 iov
[iov_count
].iov_base
= cmd
->pdu
;
3601 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3602 iov
[iov_count
].iov_base
= cmd
->buf_ptr
;
3603 iov
[iov_count
++].iov_len
= ISCSI_HDR_LEN
;
3605 tx_size
= (ISCSI_HDR_LEN
+ ISCSI_HDR_LEN
);
3607 if (conn
->conn_ops
->HeaderDigest
) {
3608 u32
*header_digest
= (u32
*)&cmd
->pdu
[ISCSI_HDR_LEN
];
3610 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, hdr
,
3611 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)header_digest
);
3613 iov
[0].iov_len
+= ISCSI_CRC_LEN
;
3614 tx_size
+= ISCSI_CRC_LEN
;
3615 pr_debug("Attaching CRC32 HeaderDigest for"
3616 " REJECT PDU 0x%08x\n", *header_digest
);
3619 if (conn
->conn_ops
->DataDigest
) {
3620 iscsit_do_crypto_hash_buf(&conn
->conn_tx_hash
, cmd
->buf_ptr
,
3621 ISCSI_HDR_LEN
, 0, NULL
, (u8
*)&cmd
->data_crc
);
3623 iov
[iov_count
].iov_base
= &cmd
->data_crc
;
3624 iov
[iov_count
++].iov_len
= ISCSI_CRC_LEN
;
3625 tx_size
+= ISCSI_CRC_LEN
;
3626 pr_debug("Attaching CRC32 DataDigest for REJECT"
3627 " PDU 0x%08x\n", cmd
->data_crc
);
3630 cmd
->iov_misc_count
= iov_count
;
3631 cmd
->tx_size
= tx_size
;
3633 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3634 " CID: %hu\n", ntohl(hdr
->statsn
), hdr
->reason
, conn
->cid
);
3639 void iscsit_thread_get_cpumask(struct iscsi_conn
*conn
)
3641 struct iscsi_thread_set
*ts
= conn
->thread_set
;
3644 * thread_id is assigned from iscsit_global->ts_bitmap from
3645 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3647 * Here we use thread_id to determine which CPU that this
3648 * iSCSI connection's iscsi_thread_set will be scheduled to
3651 ord
= ts
->thread_id
% cpumask_weight(cpu_online_mask
);
3652 for_each_online_cpu(cpu
) {
3654 cpumask_set_cpu(cpu
, conn
->conn_cpumask
);
3659 * This should never be reached..
3662 cpumask_setall(conn
->conn_cpumask
);
3665 static inline void iscsit_thread_check_cpumask(
3666 struct iscsi_conn
*conn
,
3667 struct task_struct
*p
,
3672 * mode == 1 signals iscsi_target_tx_thread() usage.
3673 * mode == 0 signals iscsi_target_rx_thread() usage.
3676 if (!conn
->conn_tx_reset_cpumask
)
3678 conn
->conn_tx_reset_cpumask
= 0;
3680 if (!conn
->conn_rx_reset_cpumask
)
3682 conn
->conn_rx_reset_cpumask
= 0;
3685 * Update the CPU mask for this single kthread so that
3686 * both TX and RX kthreads are scheduled to run on the
3689 memset(buf
, 0, 128);
3690 cpumask_scnprintf(buf
, 128, conn
->conn_cpumask
);
3691 set_cpus_allowed_ptr(p
, conn
->conn_cpumask
);
3695 iscsit_immediate_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3700 case ISTATE_SEND_R2T
:
3701 ret
= iscsit_send_r2t(cmd
, conn
);
3706 spin_lock_bh(&conn
->cmd_lock
);
3707 list_del(&cmd
->i_conn_node
);
3708 spin_unlock_bh(&conn
->cmd_lock
);
3710 iscsit_free_cmd(cmd
, false);
3712 case ISTATE_SEND_NOPIN_WANT_RESPONSE
:
3713 iscsit_mod_nopin_response_timer(conn
);
3714 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 1);
3718 case ISTATE_SEND_NOPIN_NO_RESPONSE
:
3719 ret
= iscsit_send_unsolicited_nopin(cmd
, conn
, 0);
3724 pr_err("Unknown Opcode: 0x%02x ITT:"
3725 " 0x%08x, i_state: %d on CID: %hu\n",
3726 cmd
->iscsi_opcode
, cmd
->init_task_tag
, state
,
3738 iscsit_handle_immediate_queue(struct iscsi_conn
*conn
)
3740 struct iscsit_transport
*t
= conn
->conn_transport
;
3741 struct iscsi_queue_req
*qr
;
3742 struct iscsi_cmd
*cmd
;
3746 while ((qr
= iscsit_get_cmd_from_immediate_queue(conn
))) {
3747 atomic_set(&conn
->check_immediate_queue
, 0);
3750 kmem_cache_free(lio_qr_cache
, qr
);
3752 ret
= t
->iscsit_immediate_queue(conn
, cmd
, state
);
3761 iscsit_response_queue(struct iscsi_conn
*conn
, struct iscsi_cmd
*cmd
, int state
)
3767 case ISTATE_SEND_DATAIN
:
3768 ret
= iscsit_send_datain(cmd
, conn
);
3773 goto check_rsp_state
;
3774 else if (ret
== 1) {
3776 spin_lock_bh(&cmd
->istate_lock
);
3777 cmd
->i_state
= ISTATE_SENT_STATUS
;
3778 spin_unlock_bh(&cmd
->istate_lock
);
3780 if (atomic_read(&conn
->check_immediate_queue
))
3784 } else if (ret
== 2) {
3785 /* Still must send status,
3786 SCF_TRANSPORT_TASK_SENSE was set */
3787 spin_lock_bh(&cmd
->istate_lock
);
3788 cmd
->i_state
= ISTATE_SEND_STATUS
;
3789 spin_unlock_bh(&cmd
->istate_lock
);
3790 state
= ISTATE_SEND_STATUS
;
3791 goto check_rsp_state
;
3795 case ISTATE_SEND_STATUS
:
3796 case ISTATE_SEND_STATUS_RECOVERY
:
3797 ret
= iscsit_send_response(cmd
, conn
);
3799 case ISTATE_SEND_LOGOUTRSP
:
3800 ret
= iscsit_send_logout(cmd
, conn
);
3802 case ISTATE_SEND_ASYNCMSG
:
3803 ret
= iscsit_send_conn_drop_async_message(
3806 case ISTATE_SEND_NOPIN
:
3807 ret
= iscsit_send_nopin(cmd
, conn
);
3809 case ISTATE_SEND_REJECT
:
3810 ret
= iscsit_send_reject(cmd
, conn
);
3812 case ISTATE_SEND_TASKMGTRSP
:
3813 ret
= iscsit_send_task_mgt_rsp(cmd
, conn
);
3816 ret
= iscsit_tmr_post_handler(cmd
, conn
);
3818 iscsit_fall_back_to_erl0(conn
->sess
);
3820 case ISTATE_SEND_TEXTRSP
:
3821 ret
= iscsit_send_text_rsp(cmd
, conn
);
3824 pr_err("Unknown Opcode: 0x%02x ITT:"
3825 " 0x%08x, i_state: %d on CID: %hu\n",
3826 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3833 if (iscsit_send_tx_data(cmd
, conn
, 1) < 0) {
3834 iscsit_tx_thread_wait_for_tcp(conn
);
3835 iscsit_unmap_iovec(cmd
);
3838 iscsit_unmap_iovec(cmd
);
3841 case ISTATE_SEND_LOGOUTRSP
:
3842 if (!iscsit_logout_post_handler(cmd
, conn
))
3845 case ISTATE_SEND_STATUS
:
3846 case ISTATE_SEND_ASYNCMSG
:
3847 case ISTATE_SEND_NOPIN
:
3848 case ISTATE_SEND_STATUS_RECOVERY
:
3849 case ISTATE_SEND_TEXTRSP
:
3850 case ISTATE_SEND_TASKMGTRSP
:
3851 case ISTATE_SEND_REJECT
:
3852 spin_lock_bh(&cmd
->istate_lock
);
3853 cmd
->i_state
= ISTATE_SENT_STATUS
;
3854 spin_unlock_bh(&cmd
->istate_lock
);
3857 pr_err("Unknown Opcode: 0x%02x ITT:"
3858 " 0x%08x, i_state: %d on CID: %hu\n",
3859 cmd
->iscsi_opcode
, cmd
->init_task_tag
,
3860 cmd
->i_state
, conn
->cid
);
3864 if (atomic_read(&conn
->check_immediate_queue
))
3875 static int iscsit_handle_response_queue(struct iscsi_conn
*conn
)
3877 struct iscsit_transport
*t
= conn
->conn_transport
;
3878 struct iscsi_queue_req
*qr
;
3879 struct iscsi_cmd
*cmd
;
3883 while ((qr
= iscsit_get_cmd_from_response_queue(conn
))) {
3886 kmem_cache_free(lio_qr_cache
, qr
);
3888 ret
= t
->iscsit_response_queue(conn
, cmd
, state
);
3889 if (ret
== 1 || ret
< 0)
3896 int iscsi_target_tx_thread(void *arg
)
3899 struct iscsi_conn
*conn
;
3900 struct iscsi_thread_set
*ts
= arg
;
3902 * Allow ourselves to be interrupted by SIGINT so that a
3903 * connection recovery / failure event can be triggered externally.
3905 allow_signal(SIGINT
);
3908 conn
= iscsi_tx_thread_pre_handler(ts
);
3914 while (!kthread_should_stop()) {
3916 * Ensure that both TX and RX per connection kthreads
3917 * are scheduled to run on the same CPU.
3919 iscsit_thread_check_cpumask(conn
, current
, 1);
3921 wait_event_interruptible(conn
->queues_wq
,
3922 !iscsit_conn_all_queues_empty(conn
) ||
3923 ts
->status
== ISCSI_THREAD_SET_RESET
);
3925 if ((ts
->status
== ISCSI_THREAD_SET_RESET
) ||
3926 signal_pending(current
))
3930 ret
= iscsit_handle_immediate_queue(conn
);
3934 ret
= iscsit_handle_response_queue(conn
);
3937 else if (ret
== -EAGAIN
)
3944 iscsit_take_action_for_connection_exit(conn
);
3950 static int iscsi_target_rx_opcode(struct iscsi_conn
*conn
, unsigned char *buf
)
3952 struct iscsi_hdr
*hdr
= (struct iscsi_hdr
*)buf
;
3953 struct iscsi_cmd
*cmd
;
3956 switch (hdr
->opcode
& ISCSI_OPCODE_MASK
) {
3957 case ISCSI_OP_SCSI_CMD
:
3958 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
3962 ret
= iscsit_handle_scsi_cmd(conn
, cmd
, buf
);
3964 case ISCSI_OP_SCSI_DATA_OUT
:
3965 ret
= iscsit_handle_data_out(conn
, buf
);
3967 case ISCSI_OP_NOOP_OUT
:
3969 if (hdr
->ttt
== cpu_to_be32(0xFFFFFFFF)) {
3970 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
3974 ret
= iscsit_handle_nop_out(conn
, cmd
, buf
);
3976 case ISCSI_OP_SCSI_TMFUNC
:
3977 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
3981 ret
= iscsit_handle_task_mgt_cmd(conn
, cmd
, buf
);
3984 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
3988 ret
= iscsit_handle_text_cmd(conn
, cmd
, buf
);
3990 case ISCSI_OP_LOGOUT
:
3991 cmd
= iscsit_allocate_cmd(conn
, GFP_KERNEL
);
3995 ret
= iscsit_handle_logout_cmd(conn
, cmd
, buf
);
3997 wait_for_completion_timeout(&conn
->conn_logout_comp
,
3998 SECONDS_FOR_LOGOUT_COMP
* HZ
);
4000 case ISCSI_OP_SNACK
:
4001 ret
= iscsit_handle_snack(conn
, buf
);
4004 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr
->opcode
);
4005 if (!conn
->sess
->sess_ops
->ErrorRecoveryLevel
) {
4006 pr_err("Cannot recover from unknown"
4007 " opcode while ERL=0, closing iSCSI connection.\n");
4010 if (!conn
->conn_ops
->OFMarker
) {
4011 pr_err("Unable to recover from unknown"
4012 " opcode while OFMarker=No, closing iSCSI"
4016 if (iscsit_recover_from_unknown_opcode(conn
) < 0) {
4017 pr_err("Unable to recover from unknown"
4018 " opcode, closing iSCSI connection.\n");
4026 return iscsit_add_reject(conn
, ISCSI_REASON_BOOKMARK_NO_RESOURCES
, buf
);
4029 int iscsi_target_rx_thread(void *arg
)
4032 u8 buffer
[ISCSI_HDR_LEN
], opcode
;
4033 u32 checksum
= 0, digest
= 0;
4034 struct iscsi_conn
*conn
= NULL
;
4035 struct iscsi_thread_set
*ts
= arg
;
4038 * Allow ourselves to be interrupted by SIGINT so that a
4039 * connection recovery / failure event can be triggered externally.
4041 allow_signal(SIGINT
);
4044 conn
= iscsi_rx_thread_pre_handler(ts
);
4048 if (conn
->conn_transport
->transport_type
== ISCSI_INFINIBAND
) {
4049 struct completion comp
;
4052 init_completion(&comp
);
4053 rc
= wait_for_completion_interruptible(&comp
);
4060 while (!kthread_should_stop()) {
4062 * Ensure that both TX and RX per connection kthreads
4063 * are scheduled to run on the same CPU.
4065 iscsit_thread_check_cpumask(conn
, current
, 0);
4067 memset(buffer
, 0, ISCSI_HDR_LEN
);
4068 memset(&iov
, 0, sizeof(struct kvec
));
4070 iov
.iov_base
= buffer
;
4071 iov
.iov_len
= ISCSI_HDR_LEN
;
4073 ret
= rx_data(conn
, &iov
, 1, ISCSI_HDR_LEN
);
4074 if (ret
!= ISCSI_HDR_LEN
) {
4075 iscsit_rx_thread_wait_for_tcp(conn
);
4079 if (conn
->conn_ops
->HeaderDigest
) {
4080 iov
.iov_base
= &digest
;
4081 iov
.iov_len
= ISCSI_CRC_LEN
;
4083 ret
= rx_data(conn
, &iov
, 1, ISCSI_CRC_LEN
);
4084 if (ret
!= ISCSI_CRC_LEN
) {
4085 iscsit_rx_thread_wait_for_tcp(conn
);
4089 iscsit_do_crypto_hash_buf(&conn
->conn_rx_hash
,
4090 buffer
, ISCSI_HDR_LEN
,
4091 0, NULL
, (u8
*)&checksum
);
4093 if (digest
!= checksum
) {
4094 pr_err("HeaderDigest CRC32C failed,"
4095 " received 0x%08x, computed 0x%08x\n",
4098 * Set the PDU to 0xff so it will intentionally
4099 * hit default in the switch below.
4101 memset(buffer
, 0xff, ISCSI_HDR_LEN
);
4102 spin_lock_bh(&conn
->sess
->session_stats_lock
);
4103 conn
->sess
->conn_digest_errors
++;
4104 spin_unlock_bh(&conn
->sess
->session_stats_lock
);
4106 pr_debug("Got HeaderDigest CRC32C"
4107 " 0x%08x\n", checksum
);
4111 if (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
)
4114 opcode
= buffer
[0] & ISCSI_OPCODE_MASK
;
4116 if (conn
->sess
->sess_ops
->SessionType
&&
4117 ((!(opcode
& ISCSI_OP_TEXT
)) ||
4118 (!(opcode
& ISCSI_OP_LOGOUT
)))) {
4119 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4120 " while in Discovery Session, rejecting.\n", opcode
);
4121 iscsit_add_reject(conn
, ISCSI_REASON_PROTOCOL_ERROR
,
4126 ret
= iscsi_target_rx_opcode(conn
, buffer
);
4132 if (!signal_pending(current
))
4133 atomic_set(&conn
->transport_failed
, 1);
4134 iscsit_take_action_for_connection_exit(conn
);
4140 static void iscsit_release_commands_from_conn(struct iscsi_conn
*conn
)
4142 struct iscsi_cmd
*cmd
= NULL
, *cmd_tmp
= NULL
;
4143 struct iscsi_session
*sess
= conn
->sess
;
4145 * We expect this function to only ever be called from either RX or TX
4146 * thread context via iscsit_close_connection() once the other context
4147 * has been reset -> returned sleeping pre-handler state.
4149 spin_lock_bh(&conn
->cmd_lock
);
4150 list_for_each_entry_safe(cmd
, cmd_tmp
, &conn
->conn_cmd_list
, i_conn_node
) {
4152 list_del(&cmd
->i_conn_node
);
4153 spin_unlock_bh(&conn
->cmd_lock
);
4155 iscsit_increment_maxcmdsn(cmd
, sess
);
4157 iscsit_free_cmd(cmd
, true);
4159 spin_lock_bh(&conn
->cmd_lock
);
4161 spin_unlock_bh(&conn
->cmd_lock
);
4164 static void iscsit_stop_timers_for_cmds(
4165 struct iscsi_conn
*conn
)
4167 struct iscsi_cmd
*cmd
;
4169 spin_lock_bh(&conn
->cmd_lock
);
4170 list_for_each_entry(cmd
, &conn
->conn_cmd_list
, i_conn_node
) {
4171 if (cmd
->data_direction
== DMA_TO_DEVICE
)
4172 iscsit_stop_dataout_timer(cmd
);
4174 spin_unlock_bh(&conn
->cmd_lock
);
4177 int iscsit_close_connection(
4178 struct iscsi_conn
*conn
)
4180 int conn_logout
= (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
);
4181 struct iscsi_session
*sess
= conn
->sess
;
4183 pr_debug("Closing iSCSI connection CID %hu on SID:"
4184 " %u\n", conn
->cid
, sess
->sid
);
4186 * Always up conn_logout_comp just in case the RX Thread is sleeping
4187 * and the logout response never got sent because the connection
4190 complete(&conn
->conn_logout_comp
);
4192 iscsi_release_thread_set(conn
);
4194 iscsit_stop_timers_for_cmds(conn
);
4195 iscsit_stop_nopin_response_timer(conn
);
4196 iscsit_stop_nopin_timer(conn
);
4197 iscsit_free_queue_reqs_for_conn(conn
);
4200 * During Connection recovery drop unacknowledged out of order
4201 * commands for this connection, and prepare the other commands
4204 * During normal operation clear the out of order commands (but
4205 * do not free the struct iscsi_ooo_cmdsn's) and release all
4206 * struct iscsi_cmds.
4208 if (atomic_read(&conn
->connection_recovery
)) {
4209 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn
);
4210 iscsit_prepare_cmds_for_realligance(conn
);
4212 iscsit_clear_ooo_cmdsns_for_conn(conn
);
4213 iscsit_release_commands_from_conn(conn
);
4217 * Handle decrementing session or connection usage count if
4218 * a logout response was not able to be sent because the
4219 * connection failed. Fall back to Session Recovery here.
4221 if (atomic_read(&conn
->conn_logout_remove
)) {
4222 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_SESSION
) {
4223 iscsit_dec_conn_usage_count(conn
);
4224 iscsit_dec_session_usage_count(sess
);
4226 if (conn
->conn_logout_reason
== ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
)
4227 iscsit_dec_conn_usage_count(conn
);
4229 atomic_set(&conn
->conn_logout_remove
, 0);
4230 atomic_set(&sess
->session_reinstatement
, 0);
4231 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4234 spin_lock_bh(&sess
->conn_lock
);
4235 list_del(&conn
->conn_list
);
4238 * Attempt to let the Initiator know this connection failed by
4239 * sending an Connection Dropped Async Message on another
4240 * active connection.
4242 if (atomic_read(&conn
->connection_recovery
))
4243 iscsit_build_conn_drop_async_message(conn
);
4245 spin_unlock_bh(&sess
->conn_lock
);
4248 * If connection reinstatement is being performed on this connection,
4249 * up the connection reinstatement semaphore that is being blocked on
4250 * in iscsit_cause_connection_reinstatement().
4252 spin_lock_bh(&conn
->state_lock
);
4253 if (atomic_read(&conn
->sleep_on_conn_wait_comp
)) {
4254 spin_unlock_bh(&conn
->state_lock
);
4255 complete(&conn
->conn_wait_comp
);
4256 wait_for_completion(&conn
->conn_post_wait_comp
);
4257 spin_lock_bh(&conn
->state_lock
);
4261 * If connection reinstatement is being performed on this connection
4262 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4263 * connection wait rcfr semaphore that is being blocked on
4264 * an iscsit_connection_reinstatement_rcfr().
4266 if (atomic_read(&conn
->connection_wait_rcfr
)) {
4267 spin_unlock_bh(&conn
->state_lock
);
4268 complete(&conn
->conn_wait_rcfr_comp
);
4269 wait_for_completion(&conn
->conn_post_wait_comp
);
4270 spin_lock_bh(&conn
->state_lock
);
4272 atomic_set(&conn
->connection_reinstatement
, 1);
4273 spin_unlock_bh(&conn
->state_lock
);
4276 * If any other processes are accessing this connection pointer we
4277 * must wait until they have completed.
4279 iscsit_check_conn_usage_count(conn
);
4281 if (conn
->conn_rx_hash
.tfm
)
4282 crypto_free_hash(conn
->conn_rx_hash
.tfm
);
4283 if (conn
->conn_tx_hash
.tfm
)
4284 crypto_free_hash(conn
->conn_tx_hash
.tfm
);
4286 if (conn
->conn_cpumask
)
4287 free_cpumask_var(conn
->conn_cpumask
);
4289 kfree(conn
->conn_ops
);
4290 conn
->conn_ops
= NULL
;
4293 sock_release(conn
->sock
);
4295 if (conn
->conn_transport
->iscsit_free_conn
)
4296 conn
->conn_transport
->iscsit_free_conn(conn
);
4298 iscsit_put_transport(conn
->conn_transport
);
4300 conn
->thread_set
= NULL
;
4302 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4303 conn
->conn_state
= TARG_CONN_STATE_FREE
;
4306 spin_lock_bh(&sess
->conn_lock
);
4307 atomic_dec(&sess
->nconn
);
4308 pr_debug("Decremented iSCSI connection count to %hu from node:"
4309 " %s\n", atomic_read(&sess
->nconn
),
4310 sess
->sess_ops
->InitiatorName
);
4312 * Make sure that if one connection fails in an non ERL=2 iSCSI
4313 * Session that they all fail.
4315 if ((sess
->sess_ops
->ErrorRecoveryLevel
!= 2) && !conn_logout
&&
4316 !atomic_read(&sess
->session_logout
))
4317 atomic_set(&sess
->session_fall_back_to_erl0
, 1);
4320 * If this was not the last connection in the session, and we are
4321 * performing session reinstatement or falling back to ERL=0, call
4322 * iscsit_stop_session() without sleeping to shutdown the other
4323 * active connections.
4325 if (atomic_read(&sess
->nconn
)) {
4326 if (!atomic_read(&sess
->session_reinstatement
) &&
4327 !atomic_read(&sess
->session_fall_back_to_erl0
)) {
4328 spin_unlock_bh(&sess
->conn_lock
);
4331 if (!atomic_read(&sess
->session_stop_active
)) {
4332 atomic_set(&sess
->session_stop_active
, 1);
4333 spin_unlock_bh(&sess
->conn_lock
);
4334 iscsit_stop_session(sess
, 0, 0);
4337 spin_unlock_bh(&sess
->conn_lock
);
4342 * If this was the last connection in the session and one of the
4343 * following is occurring:
4345 * Session Reinstatement is not being performed, and are falling back
4346 * to ERL=0 call iscsit_close_session().
4348 * Session Logout was requested. iscsit_close_session() will be called
4351 * Session Continuation is not being performed, start the Time2Retain
4352 * handler and check if sleep_on_sess_wait_sem is active.
4354 if (!atomic_read(&sess
->session_reinstatement
) &&
4355 atomic_read(&sess
->session_fall_back_to_erl0
)) {
4356 spin_unlock_bh(&sess
->conn_lock
);
4357 target_put_session(sess
->se_sess
);
4360 } else if (atomic_read(&sess
->session_logout
)) {
4361 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4362 sess
->session_state
= TARG_SESS_STATE_FREE
;
4363 spin_unlock_bh(&sess
->conn_lock
);
4365 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4366 complete(&sess
->session_wait_comp
);
4370 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4371 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4373 if (!atomic_read(&sess
->session_continuation
)) {
4374 spin_unlock_bh(&sess
->conn_lock
);
4375 iscsit_start_time2retain_handler(sess
);
4377 spin_unlock_bh(&sess
->conn_lock
);
4379 if (atomic_read(&sess
->sleep_on_sess_wait_comp
))
4380 complete(&sess
->session_wait_comp
);
4384 spin_unlock_bh(&sess
->conn_lock
);
4389 int iscsit_close_session(struct iscsi_session
*sess
)
4391 struct iscsi_portal_group
*tpg
= ISCSI_TPG_S(sess
);
4392 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4394 if (atomic_read(&sess
->nconn
)) {
4395 pr_err("%d connection(s) still exist for iSCSI session"
4396 " to %s\n", atomic_read(&sess
->nconn
),
4397 sess
->sess_ops
->InitiatorName
);
4401 spin_lock_bh(&se_tpg
->session_lock
);
4402 atomic_set(&sess
->session_logout
, 1);
4403 atomic_set(&sess
->session_reinstatement
, 1);
4404 iscsit_stop_time2retain_timer(sess
);
4405 spin_unlock_bh(&se_tpg
->session_lock
);
4408 * transport_deregister_session_configfs() will clear the
4409 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4410 * can be setting it again with __transport_register_session() in
4411 * iscsi_post_login_handler() again after the iscsit_stop_session()
4412 * completes in iscsi_np context.
4414 transport_deregister_session_configfs(sess
->se_sess
);
4417 * If any other processes are accessing this session pointer we must
4418 * wait until they have completed. If we are in an interrupt (the
4419 * time2retain handler) and contain and active session usage count we
4420 * restart the timer and exit.
4422 if (!in_interrupt()) {
4423 if (iscsit_check_session_usage_count(sess
) == 1)
4424 iscsit_stop_session(sess
, 1, 1);
4426 if (iscsit_check_session_usage_count(sess
) == 2) {
4427 atomic_set(&sess
->session_logout
, 0);
4428 iscsit_start_time2retain_handler(sess
);
4433 transport_deregister_session(sess
->se_sess
);
4435 if (sess
->sess_ops
->ErrorRecoveryLevel
== 2)
4436 iscsit_free_connection_recovery_entires(sess
);
4438 iscsit_free_all_ooo_cmdsns(sess
);
4440 spin_lock_bh(&se_tpg
->session_lock
);
4441 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4442 sess
->session_state
= TARG_SESS_STATE_FREE
;
4443 pr_debug("Released iSCSI session from node: %s\n",
4444 sess
->sess_ops
->InitiatorName
);
4447 tpg
->tpg_tiqn
->tiqn_nsessions
--;
4449 pr_debug("Decremented number of active iSCSI Sessions on"
4450 " iSCSI TPG: %hu to %u\n", tpg
->tpgt
, tpg
->nsessions
);
4452 spin_lock(&sess_idr_lock
);
4453 idr_remove(&sess_idr
, sess
->session_index
);
4454 spin_unlock(&sess_idr_lock
);
4456 kfree(sess
->sess_ops
);
4457 sess
->sess_ops
= NULL
;
4458 spin_unlock_bh(&se_tpg
->session_lock
);
4464 static void iscsit_logout_post_handler_closesession(
4465 struct iscsi_conn
*conn
)
4467 struct iscsi_session
*sess
= conn
->sess
;
4469 iscsi_set_thread_clear(conn
, ISCSI_CLEAR_TX_THREAD
);
4470 iscsi_set_thread_set_signal(conn
, ISCSI_SIGNAL_TX_THREAD
);
4472 atomic_set(&conn
->conn_logout_remove
, 0);
4473 complete(&conn
->conn_logout_comp
);
4475 iscsit_dec_conn_usage_count(conn
);
4476 iscsit_stop_session(sess
, 1, 1);
4477 iscsit_dec_session_usage_count(sess
);
4478 target_put_session(sess
->se_sess
);
4481 static void iscsit_logout_post_handler_samecid(
4482 struct iscsi_conn
*conn
)
4484 iscsi_set_thread_clear(conn
, ISCSI_CLEAR_TX_THREAD
);
4485 iscsi_set_thread_set_signal(conn
, ISCSI_SIGNAL_TX_THREAD
);
4487 atomic_set(&conn
->conn_logout_remove
, 0);
4488 complete(&conn
->conn_logout_comp
);
4490 iscsit_cause_connection_reinstatement(conn
, 1);
4491 iscsit_dec_conn_usage_count(conn
);
4494 static void iscsit_logout_post_handler_diffcid(
4495 struct iscsi_conn
*conn
,
4498 struct iscsi_conn
*l_conn
;
4499 struct iscsi_session
*sess
= conn
->sess
;
4504 spin_lock_bh(&sess
->conn_lock
);
4505 list_for_each_entry(l_conn
, &sess
->sess_conn_list
, conn_list
) {
4506 if (l_conn
->cid
== cid
) {
4507 iscsit_inc_conn_usage_count(l_conn
);
4511 spin_unlock_bh(&sess
->conn_lock
);
4517 l_conn
->sock
->ops
->shutdown(l_conn
->sock
, RCV_SHUTDOWN
);
4519 spin_lock_bh(&l_conn
->state_lock
);
4520 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4521 l_conn
->conn_state
= TARG_CONN_STATE_IN_LOGOUT
;
4522 spin_unlock_bh(&l_conn
->state_lock
);
4524 iscsit_cause_connection_reinstatement(l_conn
, 1);
4525 iscsit_dec_conn_usage_count(l_conn
);
4529 * Return of 0 causes the TX thread to restart.
4531 int iscsit_logout_post_handler(
4532 struct iscsi_cmd
*cmd
,
4533 struct iscsi_conn
*conn
)
4537 switch (cmd
->logout_reason
) {
4538 case ISCSI_LOGOUT_REASON_CLOSE_SESSION
:
4539 switch (cmd
->logout_response
) {
4540 case ISCSI_LOGOUT_SUCCESS
:
4541 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4543 iscsit_logout_post_handler_closesession(conn
);
4548 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION
:
4549 if (conn
->cid
== cmd
->logout_cid
) {
4550 switch (cmd
->logout_response
) {
4551 case ISCSI_LOGOUT_SUCCESS
:
4552 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4554 iscsit_logout_post_handler_samecid(conn
);
4559 switch (cmd
->logout_response
) {
4560 case ISCSI_LOGOUT_SUCCESS
:
4561 iscsit_logout_post_handler_diffcid(conn
,
4564 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4565 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4572 case ISCSI_LOGOUT_REASON_RECOVERY
:
4573 switch (cmd
->logout_response
) {
4574 case ISCSI_LOGOUT_SUCCESS
:
4575 case ISCSI_LOGOUT_CID_NOT_FOUND
:
4576 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED
:
4577 case ISCSI_LOGOUT_CLEANUP_FAILED
:
4589 EXPORT_SYMBOL(iscsit_logout_post_handler
);
4591 void iscsit_fail_session(struct iscsi_session
*sess
)
4593 struct iscsi_conn
*conn
;
4595 spin_lock_bh(&sess
->conn_lock
);
4596 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
) {
4597 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4598 conn
->conn_state
= TARG_CONN_STATE_CLEANUP_WAIT
;
4600 spin_unlock_bh(&sess
->conn_lock
);
4602 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4603 sess
->session_state
= TARG_SESS_STATE_FAILED
;
4606 int iscsit_free_session(struct iscsi_session
*sess
)
4608 u16 conn_count
= atomic_read(&sess
->nconn
);
4609 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4612 spin_lock_bh(&sess
->conn_lock
);
4613 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4615 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4617 if (conn_count
== 0)
4620 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4623 iscsit_inc_conn_usage_count(conn_tmp
);
4626 iscsit_inc_conn_usage_count(conn
);
4628 spin_unlock_bh(&sess
->conn_lock
);
4629 iscsit_cause_connection_reinstatement(conn
, 1);
4630 spin_lock_bh(&sess
->conn_lock
);
4632 iscsit_dec_conn_usage_count(conn
);
4634 iscsit_dec_conn_usage_count(conn_tmp
);
4639 if (atomic_read(&sess
->nconn
)) {
4640 spin_unlock_bh(&sess
->conn_lock
);
4641 wait_for_completion(&sess
->session_wait_comp
);
4643 spin_unlock_bh(&sess
->conn_lock
);
4645 target_put_session(sess
->se_sess
);
4649 void iscsit_stop_session(
4650 struct iscsi_session
*sess
,
4652 int connection_sleep
)
4654 u16 conn_count
= atomic_read(&sess
->nconn
);
4655 struct iscsi_conn
*conn
, *conn_tmp
= NULL
;
4658 spin_lock_bh(&sess
->conn_lock
);
4660 atomic_set(&sess
->sleep_on_sess_wait_comp
, 1);
4662 if (connection_sleep
) {
4663 list_for_each_entry_safe(conn
, conn_tmp
, &sess
->sess_conn_list
,
4665 if (conn_count
== 0)
4668 if (list_is_last(&conn
->conn_list
, &sess
->sess_conn_list
)) {
4671 iscsit_inc_conn_usage_count(conn_tmp
);
4674 iscsit_inc_conn_usage_count(conn
);
4676 spin_unlock_bh(&sess
->conn_lock
);
4677 iscsit_cause_connection_reinstatement(conn
, 1);
4678 spin_lock_bh(&sess
->conn_lock
);
4680 iscsit_dec_conn_usage_count(conn
);
4682 iscsit_dec_conn_usage_count(conn_tmp
);
4686 list_for_each_entry(conn
, &sess
->sess_conn_list
, conn_list
)
4687 iscsit_cause_connection_reinstatement(conn
, 0);
4690 if (session_sleep
&& atomic_read(&sess
->nconn
)) {
4691 spin_unlock_bh(&sess
->conn_lock
);
4692 wait_for_completion(&sess
->session_wait_comp
);
4694 spin_unlock_bh(&sess
->conn_lock
);
4697 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group
*tpg
, int force
)
4699 struct iscsi_session
*sess
;
4700 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
4701 struct se_session
*se_sess
, *se_sess_tmp
;
4702 int session_count
= 0;
4704 spin_lock_bh(&se_tpg
->session_lock
);
4705 if (tpg
->nsessions
&& !force
) {
4706 spin_unlock_bh(&se_tpg
->session_lock
);
4710 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &se_tpg
->tpg_sess_list
,
4712 sess
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
4714 spin_lock(&sess
->conn_lock
);
4715 if (atomic_read(&sess
->session_fall_back_to_erl0
) ||
4716 atomic_read(&sess
->session_logout
) ||
4717 (sess
->time2retain_timer_flags
& ISCSI_TF_EXPIRED
)) {
4718 spin_unlock(&sess
->conn_lock
);
4721 atomic_set(&sess
->session_reinstatement
, 1);
4722 spin_unlock(&sess
->conn_lock
);
4723 spin_unlock_bh(&se_tpg
->session_lock
);
4725 iscsit_free_session(sess
);
4726 spin_lock_bh(&se_tpg
->session_lock
);
4730 spin_unlock_bh(&se_tpg
->session_lock
);
4732 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4733 " Group: %hu\n", session_count
, tpg
->tpgt
);
4737 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4738 MODULE_VERSION("4.1.x");
4739 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4740 MODULE_LICENSE("GPL");
4742 module_init(iscsi_target_init_module
);
4743 module_exit(iscsi_target_cleanup_module
);