Linux 4.4.50
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target.c
blobbd810c1092777418cf3374db4fa58cc3a0baece0
1 /*******************************************************************************
2 * This file contains main functions related to the iSCSI Target Core Driver.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
19 #include <linux/string.h>
20 #include <linux/kthread.h>
21 #include <linux/crypto.h>
22 #include <linux/completion.h>
23 #include <linux/module.h>
24 #include <linux/vmalloc.h>
25 #include <linux/idr.h>
26 #include <asm/unaligned.h>
27 #include <scsi/scsi_proto.h>
28 #include <scsi/iscsi_proto.h>
29 #include <scsi/scsi_tcq.h>
30 #include <target/target_core_base.h>
31 #include <target/target_core_fabric.h>
33 #include <target/iscsi/iscsi_target_core.h>
34 #include "iscsi_target_parameters.h"
35 #include "iscsi_target_seq_pdu_list.h"
36 #include "iscsi_target_datain_values.h"
37 #include "iscsi_target_erl0.h"
38 #include "iscsi_target_erl1.h"
39 #include "iscsi_target_erl2.h"
40 #include "iscsi_target_login.h"
41 #include "iscsi_target_tmr.h"
42 #include "iscsi_target_tpg.h"
43 #include "iscsi_target_util.h"
44 #include "iscsi_target.h"
45 #include "iscsi_target_device.h"
46 #include <target/iscsi/iscsi_target_stat.h>
48 #include <target/iscsi/iscsi_transport.h>
50 static LIST_HEAD(g_tiqn_list);
51 static LIST_HEAD(g_np_list);
52 static DEFINE_SPINLOCK(tiqn_lock);
53 static DEFINE_MUTEX(np_lock);
55 static struct idr tiqn_idr;
56 struct idr sess_idr;
57 struct mutex auth_id_lock;
58 spinlock_t sess_idr_lock;
60 struct iscsit_global *iscsit_global;
62 struct kmem_cache *lio_qr_cache;
63 struct kmem_cache *lio_dr_cache;
64 struct kmem_cache *lio_ooo_cache;
65 struct kmem_cache *lio_r2t_cache;
67 static int iscsit_handle_immediate_data(struct iscsi_cmd *,
68 struct iscsi_scsi_req *, u32);
70 struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
72 struct iscsi_tiqn *tiqn = NULL;
74 spin_lock(&tiqn_lock);
75 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
76 if (!strcmp(tiqn->tiqn, buf)) {
78 spin_lock(&tiqn->tiqn_state_lock);
79 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
80 tiqn->tiqn_access_count++;
81 spin_unlock(&tiqn->tiqn_state_lock);
82 spin_unlock(&tiqn_lock);
83 return tiqn;
85 spin_unlock(&tiqn->tiqn_state_lock);
88 spin_unlock(&tiqn_lock);
90 return NULL;
93 static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
95 spin_lock(&tiqn->tiqn_state_lock);
96 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
97 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
98 spin_unlock(&tiqn->tiqn_state_lock);
99 return 0;
101 spin_unlock(&tiqn->tiqn_state_lock);
103 return -1;
106 void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
108 spin_lock(&tiqn->tiqn_state_lock);
109 tiqn->tiqn_access_count--;
110 spin_unlock(&tiqn->tiqn_state_lock);
114 * Note that IQN formatting is expected to be done in userspace, and
115 * no explict IQN format checks are done here.
117 struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
119 struct iscsi_tiqn *tiqn = NULL;
120 int ret;
122 if (strlen(buf) >= ISCSI_IQN_LEN) {
123 pr_err("Target IQN exceeds %d bytes\n",
124 ISCSI_IQN_LEN);
125 return ERR_PTR(-EINVAL);
128 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
129 if (!tiqn) {
130 pr_err("Unable to allocate struct iscsi_tiqn\n");
131 return ERR_PTR(-ENOMEM);
134 sprintf(tiqn->tiqn, "%s", buf);
135 INIT_LIST_HEAD(&tiqn->tiqn_list);
136 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
137 spin_lock_init(&tiqn->tiqn_state_lock);
138 spin_lock_init(&tiqn->tiqn_tpg_lock);
139 spin_lock_init(&tiqn->sess_err_stats.lock);
140 spin_lock_init(&tiqn->login_stats.lock);
141 spin_lock_init(&tiqn->logout_stats.lock);
143 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
145 idr_preload(GFP_KERNEL);
146 spin_lock(&tiqn_lock);
148 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
149 if (ret < 0) {
150 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
151 spin_unlock(&tiqn_lock);
152 idr_preload_end();
153 kfree(tiqn);
154 return ERR_PTR(ret);
156 tiqn->tiqn_index = ret;
157 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
159 spin_unlock(&tiqn_lock);
160 idr_preload_end();
162 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
164 return tiqn;
168 static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
171 * Wait for accesses to said struct iscsi_tiqn to end.
173 spin_lock(&tiqn->tiqn_state_lock);
174 while (tiqn->tiqn_access_count != 0) {
175 spin_unlock(&tiqn->tiqn_state_lock);
176 msleep(10);
177 spin_lock(&tiqn->tiqn_state_lock);
179 spin_unlock(&tiqn->tiqn_state_lock);
182 void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
185 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
186 * while holding tiqn->tiqn_state_lock. This means that all subsequent
187 * attempts to access this struct iscsi_tiqn will fail from both transport
188 * fabric and control code paths.
190 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
191 pr_err("iscsit_set_tiqn_shutdown() failed\n");
192 return;
195 iscsit_wait_for_tiqn(tiqn);
197 spin_lock(&tiqn_lock);
198 list_del(&tiqn->tiqn_list);
199 idr_remove(&tiqn_idr, tiqn->tiqn_index);
200 spin_unlock(&tiqn_lock);
202 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
203 tiqn->tiqn);
204 kfree(tiqn);
207 int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
209 int ret;
211 * Determine if the network portal is accepting storage traffic.
213 spin_lock_bh(&np->np_thread_lock);
214 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
215 spin_unlock_bh(&np->np_thread_lock);
216 return -1;
218 spin_unlock_bh(&np->np_thread_lock);
220 * Determine if the portal group is accepting storage traffic.
222 spin_lock_bh(&tpg->tpg_state_lock);
223 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
224 spin_unlock_bh(&tpg->tpg_state_lock);
225 return -1;
227 spin_unlock_bh(&tpg->tpg_state_lock);
230 * Here we serialize access across the TIQN+TPG Tuple.
232 ret = down_interruptible(&tpg->np_login_sem);
233 if (ret != 0)
234 return -1;
236 spin_lock_bh(&tpg->tpg_state_lock);
237 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
238 spin_unlock_bh(&tpg->tpg_state_lock);
239 up(&tpg->np_login_sem);
240 return -1;
242 spin_unlock_bh(&tpg->tpg_state_lock);
244 return 0;
247 void iscsit_login_kref_put(struct kref *kref)
249 struct iscsi_tpg_np *tpg_np = container_of(kref,
250 struct iscsi_tpg_np, tpg_np_kref);
252 complete(&tpg_np->tpg_np_comp);
255 int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
256 struct iscsi_tpg_np *tpg_np)
258 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
260 up(&tpg->np_login_sem);
262 if (tpg_np)
263 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
265 if (tiqn)
266 iscsit_put_tiqn_for_login(tiqn);
268 return 0;
271 bool iscsit_check_np_match(
272 struct sockaddr_storage *sockaddr,
273 struct iscsi_np *np,
274 int network_transport)
276 struct sockaddr_in *sock_in, *sock_in_e;
277 struct sockaddr_in6 *sock_in6, *sock_in6_e;
278 bool ip_match = false;
279 u16 port, port_e;
281 if (sockaddr->ss_family == AF_INET6) {
282 sock_in6 = (struct sockaddr_in6 *)sockaddr;
283 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
285 if (!memcmp(&sock_in6->sin6_addr.in6_u,
286 &sock_in6_e->sin6_addr.in6_u,
287 sizeof(struct in6_addr)))
288 ip_match = true;
290 port = ntohs(sock_in6->sin6_port);
291 port_e = ntohs(sock_in6_e->sin6_port);
292 } else {
293 sock_in = (struct sockaddr_in *)sockaddr;
294 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
296 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
297 ip_match = true;
299 port = ntohs(sock_in->sin_port);
300 port_e = ntohs(sock_in_e->sin_port);
303 if (ip_match && (port_e == port) &&
304 (np->np_network_transport == network_transport))
305 return true;
307 return false;
311 * Called with mutex np_lock held
313 static struct iscsi_np *iscsit_get_np(
314 struct sockaddr_storage *sockaddr,
315 int network_transport)
317 struct iscsi_np *np;
318 bool match;
320 list_for_each_entry(np, &g_np_list, np_list) {
321 spin_lock_bh(&np->np_thread_lock);
322 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
323 spin_unlock_bh(&np->np_thread_lock);
324 continue;
327 match = iscsit_check_np_match(sockaddr, np, network_transport);
328 if (match) {
330 * Increment the np_exports reference count now to
331 * prevent iscsit_del_np() below from being called
332 * while iscsi_tpg_add_network_portal() is called.
334 np->np_exports++;
335 spin_unlock_bh(&np->np_thread_lock);
336 return np;
338 spin_unlock_bh(&np->np_thread_lock);
341 return NULL;
344 struct iscsi_np *iscsit_add_np(
345 struct sockaddr_storage *sockaddr,
346 int network_transport)
348 struct iscsi_np *np;
349 int ret;
351 mutex_lock(&np_lock);
354 * Locate the existing struct iscsi_np if already active..
356 np = iscsit_get_np(sockaddr, network_transport);
357 if (np) {
358 mutex_unlock(&np_lock);
359 return np;
362 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
363 if (!np) {
364 pr_err("Unable to allocate memory for struct iscsi_np\n");
365 mutex_unlock(&np_lock);
366 return ERR_PTR(-ENOMEM);
369 np->np_flags |= NPF_IP_NETWORK;
370 np->np_network_transport = network_transport;
371 spin_lock_init(&np->np_thread_lock);
372 init_completion(&np->np_restart_comp);
373 INIT_LIST_HEAD(&np->np_list);
375 ret = iscsi_target_setup_login_socket(np, sockaddr);
376 if (ret != 0) {
377 kfree(np);
378 mutex_unlock(&np_lock);
379 return ERR_PTR(ret);
382 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
383 if (IS_ERR(np->np_thread)) {
384 pr_err("Unable to create kthread: iscsi_np\n");
385 ret = PTR_ERR(np->np_thread);
386 kfree(np);
387 mutex_unlock(&np_lock);
388 return ERR_PTR(ret);
391 * Increment the np_exports reference count now to prevent
392 * iscsit_del_np() below from being run while a new call to
393 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
394 * active. We don't need to hold np->np_thread_lock at this
395 * point because iscsi_np has not been added to g_np_list yet.
397 np->np_exports = 1;
398 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
400 list_add_tail(&np->np_list, &g_np_list);
401 mutex_unlock(&np_lock);
403 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
404 &np->np_sockaddr, np->np_transport->name);
406 return np;
409 int iscsit_reset_np_thread(
410 struct iscsi_np *np,
411 struct iscsi_tpg_np *tpg_np,
412 struct iscsi_portal_group *tpg,
413 bool shutdown)
415 spin_lock_bh(&np->np_thread_lock);
416 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
417 spin_unlock_bh(&np->np_thread_lock);
418 return 0;
420 np->np_thread_state = ISCSI_NP_THREAD_RESET;
422 if (np->np_thread) {
423 spin_unlock_bh(&np->np_thread_lock);
424 send_sig(SIGINT, np->np_thread, 1);
425 wait_for_completion(&np->np_restart_comp);
426 spin_lock_bh(&np->np_thread_lock);
428 spin_unlock_bh(&np->np_thread_lock);
430 if (tpg_np && shutdown) {
431 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
433 wait_for_completion(&tpg_np->tpg_np_comp);
436 return 0;
439 static void iscsit_free_np(struct iscsi_np *np)
441 if (np->np_socket)
442 sock_release(np->np_socket);
445 int iscsit_del_np(struct iscsi_np *np)
447 spin_lock_bh(&np->np_thread_lock);
448 np->np_exports--;
449 if (np->np_exports) {
450 np->enabled = true;
451 spin_unlock_bh(&np->np_thread_lock);
452 return 0;
454 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
455 spin_unlock_bh(&np->np_thread_lock);
457 if (np->np_thread) {
459 * We need to send the signal to wakeup Linux/Net
460 * which may be sleeping in sock_accept()..
462 send_sig(SIGINT, np->np_thread, 1);
463 kthread_stop(np->np_thread);
464 np->np_thread = NULL;
467 np->np_transport->iscsit_free_np(np);
469 mutex_lock(&np_lock);
470 list_del(&np->np_list);
471 mutex_unlock(&np_lock);
473 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
474 &np->np_sockaddr, np->np_transport->name);
476 iscsit_put_transport(np->np_transport);
477 kfree(np);
478 return 0;
481 static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
482 static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
484 static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
486 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
487 return 0;
490 static void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
492 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD);
494 spin_lock_bh(&conn->cmd_lock);
495 if (!list_empty(&cmd->i_conn_node) &&
496 !(cmd->se_cmd.transport_state & CMD_T_FABRIC_STOP))
497 list_del_init(&cmd->i_conn_node);
498 spin_unlock_bh(&conn->cmd_lock);
500 __iscsit_free_cmd(cmd, scsi_cmd, true);
503 static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
505 return TARGET_PROT_NORMAL;
508 static struct iscsit_transport iscsi_target_transport = {
509 .name = "iSCSI/TCP",
510 .transport_type = ISCSI_TCP,
511 .owner = NULL,
512 .iscsit_setup_np = iscsit_setup_np,
513 .iscsit_accept_np = iscsit_accept_np,
514 .iscsit_free_np = iscsit_free_np,
515 .iscsit_get_login_rx = iscsit_get_login_rx,
516 .iscsit_put_login_tx = iscsit_put_login_tx,
517 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
518 .iscsit_immediate_queue = iscsit_immediate_queue,
519 .iscsit_response_queue = iscsit_response_queue,
520 .iscsit_queue_data_in = iscsit_queue_rsp,
521 .iscsit_queue_status = iscsit_queue_rsp,
522 .iscsit_aborted_task = iscsit_aborted_task,
523 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
526 static int __init iscsi_target_init_module(void)
528 int ret = 0, size;
530 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
532 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
533 if (!iscsit_global) {
534 pr_err("Unable to allocate memory for iscsit_global\n");
535 return -1;
537 spin_lock_init(&iscsit_global->ts_bitmap_lock);
538 mutex_init(&auth_id_lock);
539 spin_lock_init(&sess_idr_lock);
540 idr_init(&tiqn_idr);
541 idr_init(&sess_idr);
543 ret = target_register_template(&iscsi_ops);
544 if (ret)
545 goto out;
547 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
548 iscsit_global->ts_bitmap = vzalloc(size);
549 if (!iscsit_global->ts_bitmap) {
550 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
551 goto configfs_out;
554 lio_qr_cache = kmem_cache_create("lio_qr_cache",
555 sizeof(struct iscsi_queue_req),
556 __alignof__(struct iscsi_queue_req), 0, NULL);
557 if (!lio_qr_cache) {
558 pr_err("nable to kmem_cache_create() for"
559 " lio_qr_cache\n");
560 goto bitmap_out;
563 lio_dr_cache = kmem_cache_create("lio_dr_cache",
564 sizeof(struct iscsi_datain_req),
565 __alignof__(struct iscsi_datain_req), 0, NULL);
566 if (!lio_dr_cache) {
567 pr_err("Unable to kmem_cache_create() for"
568 " lio_dr_cache\n");
569 goto qr_out;
572 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
573 sizeof(struct iscsi_ooo_cmdsn),
574 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
575 if (!lio_ooo_cache) {
576 pr_err("Unable to kmem_cache_create() for"
577 " lio_ooo_cache\n");
578 goto dr_out;
581 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
582 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
583 0, NULL);
584 if (!lio_r2t_cache) {
585 pr_err("Unable to kmem_cache_create() for"
586 " lio_r2t_cache\n");
587 goto ooo_out;
590 iscsit_register_transport(&iscsi_target_transport);
592 if (iscsit_load_discovery_tpg() < 0)
593 goto r2t_out;
595 return ret;
596 r2t_out:
597 iscsit_unregister_transport(&iscsi_target_transport);
598 kmem_cache_destroy(lio_r2t_cache);
599 ooo_out:
600 kmem_cache_destroy(lio_ooo_cache);
601 dr_out:
602 kmem_cache_destroy(lio_dr_cache);
603 qr_out:
604 kmem_cache_destroy(lio_qr_cache);
605 bitmap_out:
606 vfree(iscsit_global->ts_bitmap);
607 configfs_out:
608 /* XXX: this probably wants it to be it's own unwind step.. */
609 if (iscsit_global->discovery_tpg)
610 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
611 target_unregister_template(&iscsi_ops);
612 out:
613 kfree(iscsit_global);
614 return -ENOMEM;
617 static void __exit iscsi_target_cleanup_module(void)
619 iscsit_release_discovery_tpg();
620 iscsit_unregister_transport(&iscsi_target_transport);
621 kmem_cache_destroy(lio_qr_cache);
622 kmem_cache_destroy(lio_dr_cache);
623 kmem_cache_destroy(lio_ooo_cache);
624 kmem_cache_destroy(lio_r2t_cache);
627 * Shutdown discovery sessions and disable discovery TPG
629 if (iscsit_global->discovery_tpg)
630 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
632 target_unregister_template(&iscsi_ops);
634 vfree(iscsit_global->ts_bitmap);
635 kfree(iscsit_global);
638 static int iscsit_add_reject(
639 struct iscsi_conn *conn,
640 u8 reason,
641 unsigned char *buf)
643 struct iscsi_cmd *cmd;
645 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
646 if (!cmd)
647 return -1;
649 cmd->iscsi_opcode = ISCSI_OP_REJECT;
650 cmd->reject_reason = reason;
652 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
653 if (!cmd->buf_ptr) {
654 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
655 iscsit_free_cmd(cmd, false);
656 return -1;
659 spin_lock_bh(&conn->cmd_lock);
660 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
661 spin_unlock_bh(&conn->cmd_lock);
663 cmd->i_state = ISTATE_SEND_REJECT;
664 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
666 return -1;
669 static int iscsit_add_reject_from_cmd(
670 struct iscsi_cmd *cmd,
671 u8 reason,
672 bool add_to_conn,
673 unsigned char *buf)
675 struct iscsi_conn *conn;
677 if (!cmd->conn) {
678 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
679 cmd->init_task_tag);
680 return -1;
682 conn = cmd->conn;
684 cmd->iscsi_opcode = ISCSI_OP_REJECT;
685 cmd->reject_reason = reason;
687 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
688 if (!cmd->buf_ptr) {
689 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
690 iscsit_free_cmd(cmd, false);
691 return -1;
694 if (add_to_conn) {
695 spin_lock_bh(&conn->cmd_lock);
696 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
697 spin_unlock_bh(&conn->cmd_lock);
700 cmd->i_state = ISTATE_SEND_REJECT;
701 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
703 * Perform the kref_put now if se_cmd has already been setup by
704 * scsit_setup_scsi_cmd()
706 if (cmd->se_cmd.se_tfo != NULL) {
707 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
708 target_put_sess_cmd(&cmd->se_cmd);
710 return -1;
713 static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
714 unsigned char *buf)
716 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
719 int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
721 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
725 * Map some portion of the allocated scatterlist to an iovec, suitable for
726 * kernel sockets to copy data in/out.
728 static int iscsit_map_iovec(
729 struct iscsi_cmd *cmd,
730 struct kvec *iov,
731 u32 data_offset,
732 u32 data_length)
734 u32 i = 0;
735 struct scatterlist *sg;
736 unsigned int page_off;
739 * We know each entry in t_data_sg contains a page.
741 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
742 page_off = (data_offset % PAGE_SIZE);
744 cmd->first_data_sg = sg;
745 cmd->first_data_sg_off = page_off;
747 while (data_length) {
748 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
750 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
751 iov[i].iov_len = cur_len;
753 data_length -= cur_len;
754 page_off = 0;
755 sg = sg_next(sg);
756 i++;
759 cmd->kmapped_nents = i;
761 return i;
764 static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
766 u32 i;
767 struct scatterlist *sg;
769 sg = cmd->first_data_sg;
771 for (i = 0; i < cmd->kmapped_nents; i++)
772 kunmap(sg_page(&sg[i]));
775 static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
777 LIST_HEAD(ack_list);
778 struct iscsi_cmd *cmd, *cmd_p;
780 conn->exp_statsn = exp_statsn;
782 if (conn->sess->sess_ops->RDMAExtensions)
783 return;
785 spin_lock_bh(&conn->cmd_lock);
786 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
787 spin_lock(&cmd->istate_lock);
788 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
789 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
790 cmd->i_state = ISTATE_REMOVE;
791 spin_unlock(&cmd->istate_lock);
792 list_move_tail(&cmd->i_conn_node, &ack_list);
793 continue;
795 spin_unlock(&cmd->istate_lock);
797 spin_unlock_bh(&conn->cmd_lock);
799 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
800 list_del_init(&cmd->i_conn_node);
801 iscsit_free_cmd(cmd, false);
805 static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
807 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
809 iov_count += ISCSI_IOV_DATA_BUFFER;
811 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
812 if (!cmd->iov_data) {
813 pr_err("Unable to allocate cmd->iov_data\n");
814 return -ENOMEM;
817 cmd->orig_iov_data_count = iov_count;
818 return 0;
821 int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
822 unsigned char *buf)
824 int data_direction, payload_length;
825 struct iscsi_scsi_req *hdr;
826 int iscsi_task_attr;
827 int sam_task_attr;
829 atomic_long_inc(&conn->sess->cmd_pdus);
831 hdr = (struct iscsi_scsi_req *) buf;
832 payload_length = ntoh24(hdr->dlength);
834 /* FIXME; Add checks for AdditionalHeaderSegment */
836 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
837 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
838 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
839 " not set. Bad iSCSI Initiator.\n");
840 return iscsit_add_reject_cmd(cmd,
841 ISCSI_REASON_BOOKMARK_INVALID, buf);
844 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
845 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
847 * From RFC-3720 Section 10.3.1:
849 * "Either or both of R and W MAY be 1 when either the
850 * Expected Data Transfer Length and/or Bidirectional Read
851 * Expected Data Transfer Length are 0"
853 * For this case, go ahead and clear the unnecssary bits
854 * to avoid any confusion with ->data_direction.
856 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
857 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
859 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
860 " set when Expected Data Transfer Length is 0 for"
861 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
864 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
865 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
866 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
867 " MUST be set if Expected Data Transfer Length is not 0."
868 " Bad iSCSI Initiator\n");
869 return iscsit_add_reject_cmd(cmd,
870 ISCSI_REASON_BOOKMARK_INVALID, buf);
873 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
874 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
875 pr_err("Bidirectional operations not supported!\n");
876 return iscsit_add_reject_cmd(cmd,
877 ISCSI_REASON_BOOKMARK_INVALID, buf);
880 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
881 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
882 " Scsi Command PDU.\n");
883 return iscsit_add_reject_cmd(cmd,
884 ISCSI_REASON_BOOKMARK_INVALID, buf);
887 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
888 pr_err("ImmediateData=No but DataSegmentLength=%u,"
889 " protocol error.\n", payload_length);
890 return iscsit_add_reject_cmd(cmd,
891 ISCSI_REASON_PROTOCOL_ERROR, buf);
894 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
895 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
896 pr_err("Expected Data Transfer Length and Length of"
897 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
898 " bit is not set protocol error\n");
899 return iscsit_add_reject_cmd(cmd,
900 ISCSI_REASON_PROTOCOL_ERROR, buf);
903 if (payload_length > be32_to_cpu(hdr->data_length)) {
904 pr_err("DataSegmentLength: %u is greater than"
905 " EDTL: %u, protocol error.\n", payload_length,
906 hdr->data_length);
907 return iscsit_add_reject_cmd(cmd,
908 ISCSI_REASON_PROTOCOL_ERROR, buf);
911 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
912 pr_err("DataSegmentLength: %u is greater than"
913 " MaxXmitDataSegmentLength: %u, protocol error.\n",
914 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
915 return iscsit_add_reject_cmd(cmd,
916 ISCSI_REASON_PROTOCOL_ERROR, buf);
919 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
920 pr_err("DataSegmentLength: %u is greater than"
921 " FirstBurstLength: %u, protocol error.\n",
922 payload_length, conn->sess->sess_ops->FirstBurstLength);
923 return iscsit_add_reject_cmd(cmd,
924 ISCSI_REASON_BOOKMARK_INVALID, buf);
927 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
928 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
929 DMA_NONE;
931 cmd->data_direction = data_direction;
932 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
934 * Figure out the SAM Task Attribute for the incoming SCSI CDB
936 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
937 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
938 sam_task_attr = TCM_SIMPLE_TAG;
939 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
940 sam_task_attr = TCM_ORDERED_TAG;
941 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
942 sam_task_attr = TCM_HEAD_TAG;
943 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
944 sam_task_attr = TCM_ACA_TAG;
945 else {
946 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
947 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
948 sam_task_attr = TCM_SIMPLE_TAG;
951 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
952 cmd->i_state = ISTATE_NEW_CMD;
953 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
954 cmd->immediate_data = (payload_length) ? 1 : 0;
955 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
956 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
957 if (cmd->unsolicited_data)
958 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
960 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
961 if (hdr->flags & ISCSI_FLAG_CMD_READ)
962 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
963 else
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();
974 if (!dr)
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, &iscsi_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,
992 conn->cid);
994 target_get_sess_cmd(&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)
999 goto attach_cmd;
1001 /* only used for printks or comparing with ->ref_task_tag */
1002 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
1003 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1004 if (cmd->sense_reason) {
1005 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
1006 return iscsit_add_reject_cmd(cmd,
1007 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1010 goto attach_cmd;
1013 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
1014 return iscsit_add_reject_cmd(cmd,
1015 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1018 attach_cmd:
1019 spin_lock_bh(&conn->cmd_lock);
1020 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1021 spin_unlock_bh(&conn->cmd_lock);
1023 * Check if we need to delay processing because of ALUA
1024 * Active/NonOptimized primary access state..
1026 core_alua_check_nonop_delay(&cmd->se_cmd);
1028 return 0;
1030 EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
1032 void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1034 iscsit_set_dataout_sequence_values(cmd);
1036 spin_lock_bh(&cmd->dataout_timeout_lock);
1037 iscsit_start_dataout_timer(cmd, cmd->conn);
1038 spin_unlock_bh(&cmd->dataout_timeout_lock);
1040 EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1042 int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1043 struct iscsi_scsi_req *hdr)
1045 int cmdsn_ret = 0;
1047 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1048 * the Immediate Bit is not set, and no Immediate
1049 * Data is attached.
1051 * A PDU/CmdSN carrying Immediate Data can only
1052 * be processed after the DataCRC has passed.
1053 * If the DataCRC fails, the CmdSN MUST NOT
1054 * be acknowledged. (See below)
1056 if (!cmd->immediate_data) {
1057 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1058 (unsigned char *)hdr, hdr->cmdsn);
1059 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1060 return -1;
1061 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1062 target_put_sess_cmd(&cmd->se_cmd);
1063 return 0;
1067 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1070 * If no Immediate Data is attached, it's OK to return now.
1072 if (!cmd->immediate_data) {
1073 if (!cmd->sense_reason && cmd->unsolicited_data)
1074 iscsit_set_unsoliticed_dataout(cmd);
1075 if (!cmd->sense_reason)
1076 return 0;
1078 target_put_sess_cmd(&cmd->se_cmd);
1079 return 0;
1083 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1084 * execution. These exceptions are processed in CmdSN order using
1085 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1087 if (cmd->sense_reason) {
1088 if (cmd->reject_reason)
1089 return 0;
1091 return 1;
1094 * Call directly into transport_generic_new_cmd() to perform
1095 * the backend memory allocation.
1097 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
1098 if (cmd->sense_reason)
1099 return 1;
1101 return 0;
1103 EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1105 static int
1106 iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1107 bool dump_payload)
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)
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)
1128 return -1;
1130 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1131 int rc;
1133 rc = iscsit_dump_data_payload(cmd->conn,
1134 cmd->first_burst_len, 1);
1135 target_put_sess_cmd(&cmd->se_cmd);
1136 return rc;
1137 } else if (cmd->unsolicited_data)
1138 iscsit_set_unsoliticed_dataout(cmd);
1140 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1142 * Immediate Data failed DataCRC and ERL>=1,
1143 * silently drop this PDU and let the initiator
1144 * plug the CmdSN gap.
1146 * FIXME: Send Unsolicited NOPIN with reserved
1147 * TTT here to help the initiator figure out
1148 * the missing CmdSN, although they should be
1149 * intelligent enough to determine the missing
1150 * CmdSN and issue a retry to plug the sequence.
1152 cmd->i_state = ISTATE_REMOVE;
1153 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
1154 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1155 return -1;
1157 return 0;
1160 static int
1161 iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1162 unsigned char *buf)
1164 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1165 int rc, immed_data;
1166 bool dump_payload = false;
1168 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1169 if (rc < 0)
1170 return 0;
1172 * Allocation iovecs needed for struct socket operations for
1173 * traditional iSCSI block I/O.
1175 if (iscsit_allocate_iovecs(cmd) < 0) {
1176 return iscsit_reject_cmd(cmd,
1177 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1179 immed_data = cmd->immediate_data;
1181 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1182 if (rc < 0)
1183 return rc;
1184 else if (rc > 0)
1185 dump_payload = true;
1187 if (!immed_data)
1188 return 0;
1190 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1193 static u32 iscsit_do_crypto_hash_sg(
1194 struct hash_desc *hash,
1195 struct iscsi_cmd *cmd,
1196 u32 data_offset,
1197 u32 data_length,
1198 u32 padding,
1199 u8 *pad_bytes)
1201 u32 data_crc;
1202 struct scatterlist *sg;
1203 unsigned int page_off;
1205 crypto_hash_init(hash);
1207 sg = cmd->first_data_sg;
1208 page_off = cmd->first_data_sg_off;
1210 while (data_length) {
1211 u32 cur_len = min_t(u32, data_length, (sg->length - page_off));
1213 crypto_hash_update(hash, sg, cur_len);
1215 data_length -= cur_len;
1216 page_off = 0;
1217 /* iscsit_map_iovec has already checked for invalid sg pointers */
1218 sg = sg_next(sg);
1221 if (padding) {
1222 struct scatterlist pad_sg;
1224 sg_init_one(&pad_sg, pad_bytes, padding);
1225 crypto_hash_update(hash, &pad_sg, padding);
1227 crypto_hash_final(hash, (u8 *) &data_crc);
1229 return data_crc;
1232 static void iscsit_do_crypto_hash_buf(
1233 struct hash_desc *hash,
1234 const void *buf,
1235 u32 payload_length,
1236 u32 padding,
1237 u8 *pad_bytes,
1238 u8 *data_crc)
1240 struct scatterlist sg;
1242 crypto_hash_init(hash);
1244 sg_init_one(&sg, buf, payload_length);
1245 crypto_hash_update(hash, &sg, payload_length);
1247 if (padding) {
1248 sg_init_one(&sg, pad_bytes, padding);
1249 crypto_hash_update(hash, &sg, padding);
1251 crypto_hash_final(hash, data_crc);
1255 iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1256 struct iscsi_cmd **out_cmd)
1258 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1259 struct iscsi_cmd *cmd = NULL;
1260 struct se_cmd *se_cmd;
1261 u32 payload_length = ntoh24(hdr->dlength);
1262 int rc;
1264 if (!payload_length) {
1265 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1266 return 0;
1269 /* iSCSI write */
1270 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
1272 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1273 pr_err("DataSegmentLength: %u is greater than"
1274 " MaxXmitDataSegmentLength: %u\n", payload_length,
1275 conn->conn_ops->MaxXmitDataSegmentLength);
1276 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1277 buf);
1280 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1281 payload_length);
1282 if (!cmd)
1283 return 0;
1285 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1286 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1287 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
1288 payload_length, conn->cid);
1290 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1291 pr_err("Command ITT: 0x%08x received DataOUT after"
1292 " last DataOUT received, dumping payload\n",
1293 cmd->init_task_tag);
1294 return iscsit_dump_data_payload(conn, payload_length, 1);
1297 if (cmd->data_direction != DMA_TO_DEVICE) {
1298 pr_err("Command ITT: 0x%08x received DataOUT for a"
1299 " NON-WRITE command.\n", cmd->init_task_tag);
1300 return iscsit_dump_data_payload(conn, payload_length, 1);
1302 se_cmd = &cmd->se_cmd;
1303 iscsit_mod_dataout_timer(cmd);
1305 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
1306 pr_err("DataOut Offset: %u, Length %u greater than"
1307 " iSCSI Command EDTL %u, protocol error.\n",
1308 hdr->offset, payload_length, cmd->se_cmd.data_length);
1309 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
1312 if (cmd->unsolicited_data) {
1313 int dump_unsolicited_data = 0;
1315 if (conn->sess->sess_ops->InitialR2T) {
1316 pr_err("Received unexpected unsolicited data"
1317 " while InitialR2T=Yes, protocol error.\n");
1318 transport_send_check_condition_and_sense(&cmd->se_cmd,
1319 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1320 return -1;
1323 * Special case for dealing with Unsolicited DataOUT
1324 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1325 * failures;
1328 /* Something's amiss if we're not in WRITE_PENDING state... */
1329 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1330 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
1331 dump_unsolicited_data = 1;
1333 if (dump_unsolicited_data) {
1335 * Check if a delayed TASK_ABORTED status needs to
1336 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1337 * received with the unsolicitied data out.
1339 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1340 iscsit_stop_dataout_timer(cmd);
1342 transport_check_aborted_status(se_cmd,
1343 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1344 return iscsit_dump_data_payload(conn, payload_length, 1);
1346 } else {
1348 * For the normal solicited data path:
1350 * Check for a delayed TASK_ABORTED status and dump any
1351 * incoming data out payload if one exists. Also, when the
1352 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1353 * data out sequence, we decrement outstanding_r2ts. Once
1354 * outstanding_r2ts reaches zero, go ahead and send the delayed
1355 * TASK_ABORTED status.
1357 if (se_cmd->transport_state & CMD_T_ABORTED) {
1358 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1359 if (--cmd->outstanding_r2ts < 1) {
1360 iscsit_stop_dataout_timer(cmd);
1361 transport_check_aborted_status(
1362 se_cmd, 1);
1365 return iscsit_dump_data_payload(conn, payload_length, 1);
1369 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1370 * within-command recovery checks before receiving the payload.
1372 rc = iscsit_check_pre_dataout(cmd, buf);
1373 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
1374 return 0;
1375 else if (rc == DATAOUT_CANNOT_RECOVER)
1376 return -1;
1378 *out_cmd = cmd;
1379 return 0;
1381 EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1383 static int
1384 iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1385 struct iscsi_data *hdr)
1387 struct kvec *iov;
1388 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1389 u32 payload_length = ntoh24(hdr->dlength);
1390 int iov_ret, data_crc_failed = 0;
1392 rx_size += payload_length;
1393 iov = &cmd->iov_data[0];
1395 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1396 payload_length);
1397 if (iov_ret < 0)
1398 return -1;
1400 iov_count += iov_ret;
1402 padding = ((-payload_length) & 3);
1403 if (padding != 0) {
1404 iov[iov_count].iov_base = cmd->pad_bytes;
1405 iov[iov_count++].iov_len = padding;
1406 rx_size += padding;
1407 pr_debug("Receiving %u padding bytes.\n", padding);
1410 if (conn->conn_ops->DataDigest) {
1411 iov[iov_count].iov_base = &checksum;
1412 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1413 rx_size += ISCSI_CRC_LEN;
1416 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1418 iscsit_unmap_iovec(cmd);
1420 if (rx_got != rx_size)
1421 return -1;
1423 if (conn->conn_ops->DataDigest) {
1424 u32 data_crc;
1426 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
1427 be32_to_cpu(hdr->offset),
1428 payload_length, padding,
1429 cmd->pad_bytes);
1431 if (checksum != data_crc) {
1432 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1433 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1434 " does not match computed 0x%08x\n",
1435 hdr->itt, hdr->offset, payload_length,
1436 hdr->datasn, checksum, data_crc);
1437 data_crc_failed = 1;
1438 } else {
1439 pr_debug("Got CRC32C DataDigest 0x%08x for"
1440 " %u bytes of Data Out\n", checksum,
1441 payload_length);
1445 return data_crc_failed;
1449 iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1450 bool data_crc_failed)
1452 struct iscsi_conn *conn = cmd->conn;
1453 int rc, ooo_cmdsn;
1455 * Increment post receive data and CRC values or perform
1456 * within-command recovery.
1458 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1459 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
1460 return 0;
1461 else if (rc == DATAOUT_SEND_R2T) {
1462 iscsit_set_dataout_sequence_values(cmd);
1463 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1464 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
1466 * Handle extra special case for out of order
1467 * Unsolicited Data Out.
1469 spin_lock_bh(&cmd->istate_lock);
1470 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1471 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1472 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1473 spin_unlock_bh(&cmd->istate_lock);
1475 iscsit_stop_dataout_timer(cmd);
1476 if (ooo_cmdsn)
1477 return 0;
1478 target_execute_cmd(&cmd->se_cmd);
1479 return 0;
1480 } else /* DATAOUT_CANNOT_RECOVER */
1481 return -1;
1483 return 0;
1485 EXPORT_SYMBOL(iscsit_check_dataout_payload);
1487 static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1489 struct iscsi_cmd *cmd = NULL;
1490 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1491 int rc;
1492 bool data_crc_failed = false;
1494 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1495 if (rc < 0)
1496 return 0;
1497 else if (!cmd)
1498 return 0;
1500 rc = iscsit_get_dataout(conn, cmd, hdr);
1501 if (rc < 0)
1502 return rc;
1503 else if (rc > 0)
1504 data_crc_failed = true;
1506 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1509 int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1510 struct iscsi_nopout *hdr)
1512 u32 payload_length = ntoh24(hdr->dlength);
1514 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1515 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1516 if (!cmd)
1517 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1518 (unsigned char *)hdr);
1520 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1521 (unsigned char *)hdr);
1524 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1525 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1526 " not set, protocol error.\n");
1527 if (!cmd)
1528 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1529 (unsigned char *)hdr);
1531 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1532 (unsigned char *)hdr);
1535 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1536 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1537 " greater than MaxXmitDataSegmentLength: %u, protocol"
1538 " error.\n", payload_length,
1539 conn->conn_ops->MaxXmitDataSegmentLength);
1540 if (!cmd)
1541 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1542 (unsigned char *)hdr);
1544 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1545 (unsigned char *)hdr);
1548 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1549 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1550 hdr->itt == RESERVED_ITT ? "Response" : "Request",
1551 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1552 payload_length);
1554 * This is not a response to a Unsolicited NopIN, which means
1555 * it can either be a NOPOUT ping request (with a valid ITT),
1556 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1557 * Either way, make sure we allocate an struct iscsi_cmd, as both
1558 * can contain ping data.
1560 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1561 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1562 cmd->i_state = ISTATE_SEND_NOPIN;
1563 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1564 1 : 0);
1565 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1566 cmd->targ_xfer_tag = 0xFFFFFFFF;
1567 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1568 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1569 cmd->data_direction = DMA_NONE;
1572 return 0;
1574 EXPORT_SYMBOL(iscsit_setup_nop_out);
1576 int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1577 struct iscsi_nopout *hdr)
1579 struct iscsi_cmd *cmd_p = NULL;
1580 int cmdsn_ret = 0;
1582 * Initiator is expecting a NopIN ping reply..
1584 if (hdr->itt != RESERVED_ITT) {
1585 if (!cmd)
1586 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1587 (unsigned char *)hdr);
1589 spin_lock_bh(&conn->cmd_lock);
1590 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1591 spin_unlock_bh(&conn->cmd_lock);
1593 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1595 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1596 iscsit_add_cmd_to_response_queue(cmd, conn,
1597 cmd->i_state);
1598 return 0;
1601 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1602 (unsigned char *)hdr, hdr->cmdsn);
1603 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1604 return 0;
1605 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1606 return -1;
1608 return 0;
1611 * This was a response to a unsolicited NOPIN ping.
1613 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1614 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1615 if (!cmd_p)
1616 return -EINVAL;
1618 iscsit_stop_nopin_response_timer(conn);
1620 cmd_p->i_state = ISTATE_REMOVE;
1621 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1623 iscsit_start_nopin_timer(conn);
1624 return 0;
1627 * Otherwise, initiator is not expecting a NOPIN is response.
1628 * Just ignore for now.
1630 return 0;
1632 EXPORT_SYMBOL(iscsit_process_nop_out);
1634 static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1635 unsigned char *buf)
1637 unsigned char *ping_data = NULL;
1638 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1639 struct kvec *iov = NULL;
1640 u32 payload_length = ntoh24(hdr->dlength);
1641 int ret;
1643 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1644 if (ret < 0)
1645 return 0;
1647 * Handle NOP-OUT payload for traditional iSCSI sockets
1649 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1650 u32 checksum, data_crc, padding = 0;
1651 int niov = 0, rx_got, rx_size = payload_length;
1653 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1654 if (!ping_data) {
1655 pr_err("Unable to allocate memory for"
1656 " NOPOUT ping data.\n");
1657 ret = -1;
1658 goto out;
1661 iov = &cmd->iov_misc[0];
1662 iov[niov].iov_base = ping_data;
1663 iov[niov++].iov_len = payload_length;
1665 padding = ((-payload_length) & 3);
1666 if (padding != 0) {
1667 pr_debug("Receiving %u additional bytes"
1668 " for padding.\n", padding);
1669 iov[niov].iov_base = &cmd->pad_bytes;
1670 iov[niov++].iov_len = padding;
1671 rx_size += padding;
1673 if (conn->conn_ops->DataDigest) {
1674 iov[niov].iov_base = &checksum;
1675 iov[niov++].iov_len = ISCSI_CRC_LEN;
1676 rx_size += ISCSI_CRC_LEN;
1679 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1680 if (rx_got != rx_size) {
1681 ret = -1;
1682 goto out;
1685 if (conn->conn_ops->DataDigest) {
1686 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1687 ping_data, payload_length,
1688 padding, cmd->pad_bytes,
1689 (u8 *)&data_crc);
1691 if (checksum != data_crc) {
1692 pr_err("Ping data CRC32C DataDigest"
1693 " 0x%08x does not match computed 0x%08x\n",
1694 checksum, data_crc);
1695 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1696 pr_err("Unable to recover from"
1697 " NOPOUT Ping DataCRC failure while in"
1698 " ERL=0.\n");
1699 ret = -1;
1700 goto out;
1701 } else {
1703 * Silently drop this PDU and let the
1704 * initiator plug the CmdSN gap.
1706 pr_debug("Dropping NOPOUT"
1707 " Command CmdSN: 0x%08x due to"
1708 " DataCRC error.\n", hdr->cmdsn);
1709 ret = 0;
1710 goto out;
1712 } else {
1713 pr_debug("Got CRC32C DataDigest"
1714 " 0x%08x for %u bytes of ping data.\n",
1715 checksum, payload_length);
1719 ping_data[payload_length] = '\0';
1721 * Attach ping data to struct iscsi_cmd->buf_ptr.
1723 cmd->buf_ptr = ping_data;
1724 cmd->buf_ptr_size = payload_length;
1726 pr_debug("Got %u bytes of NOPOUT ping"
1727 " data.\n", payload_length);
1728 pr_debug("Ping Data: \"%s\"\n", ping_data);
1731 return iscsit_process_nop_out(conn, cmd, hdr);
1732 out:
1733 if (cmd)
1734 iscsit_free_cmd(cmd, false);
1736 kfree(ping_data);
1737 return ret;
1741 iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1742 unsigned char *buf)
1744 struct se_tmr_req *se_tmr;
1745 struct iscsi_tmr_req *tmr_req;
1746 struct iscsi_tm *hdr;
1747 int out_of_order_cmdsn = 0, ret;
1748 bool sess_ref = false;
1749 u8 function;
1751 hdr = (struct iscsi_tm *) buf;
1752 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1753 function = hdr->flags;
1755 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1756 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1757 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1758 hdr->rtt, hdr->refcmdsn, conn->cid);
1760 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1761 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1762 hdr->rtt != RESERVED_ITT)) {
1763 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1764 hdr->rtt = RESERVED_ITT;
1767 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1768 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1769 pr_err("Task Management Request TASK_REASSIGN not"
1770 " issued as immediate command, bad iSCSI Initiator"
1771 "implementation\n");
1772 return iscsit_add_reject_cmd(cmd,
1773 ISCSI_REASON_PROTOCOL_ERROR, buf);
1775 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1776 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1777 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
1779 cmd->data_direction = DMA_NONE;
1781 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1782 if (!cmd->tmr_req) {
1783 pr_err("Unable to allocate memory for"
1784 " Task Management command!\n");
1785 return iscsit_add_reject_cmd(cmd,
1786 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1787 buf);
1791 * TASK_REASSIGN for ERL=2 / connection stays inside of
1792 * LIO-Target $FABRIC_MOD
1794 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1796 u8 tcm_function;
1797 int ret;
1799 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
1800 conn->sess->se_sess, 0, DMA_NONE,
1801 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
1803 target_get_sess_cmd(&cmd->se_cmd, true);
1804 sess_ref = true;
1806 switch (function) {
1807 case ISCSI_TM_FUNC_ABORT_TASK:
1808 tcm_function = TMR_ABORT_TASK;
1809 break;
1810 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1811 tcm_function = TMR_ABORT_TASK_SET;
1812 break;
1813 case ISCSI_TM_FUNC_CLEAR_ACA:
1814 tcm_function = TMR_CLEAR_ACA;
1815 break;
1816 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1817 tcm_function = TMR_CLEAR_TASK_SET;
1818 break;
1819 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1820 tcm_function = TMR_LUN_RESET;
1821 break;
1822 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1823 tcm_function = TMR_TARGET_WARM_RESET;
1824 break;
1825 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1826 tcm_function = TMR_TARGET_COLD_RESET;
1827 break;
1828 default:
1829 pr_err("Unknown iSCSI TMR Function:"
1830 " 0x%02x\n", function);
1831 return iscsit_add_reject_cmd(cmd,
1832 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1835 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1836 tcm_function, GFP_KERNEL);
1837 if (ret < 0)
1838 return iscsit_add_reject_cmd(cmd,
1839 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1841 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1844 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1845 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1846 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1847 cmd->init_task_tag = hdr->itt;
1848 cmd->targ_xfer_tag = 0xFFFFFFFF;
1849 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1850 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1851 se_tmr = cmd->se_cmd.se_tmr_req;
1852 tmr_req = cmd->tmr_req;
1854 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1856 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1857 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1858 scsilun_to_int(&hdr->lun));
1859 if (ret < 0) {
1860 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1861 goto attach;
1865 switch (function) {
1866 case ISCSI_TM_FUNC_ABORT_TASK:
1867 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
1868 if (se_tmr->response)
1869 goto attach;
1870 break;
1871 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1872 case ISCSI_TM_FUNC_CLEAR_ACA:
1873 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1874 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1875 break;
1876 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1877 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
1878 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1879 goto attach;
1881 break;
1882 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1883 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
1884 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1885 goto attach;
1887 break;
1888 case ISCSI_TM_FUNC_TASK_REASSIGN:
1889 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1891 * Perform sanity checks on the ExpDataSN only if the
1892 * TASK_REASSIGN was successful.
1894 if (se_tmr->response)
1895 break;
1897 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
1898 return iscsit_add_reject_cmd(cmd,
1899 ISCSI_REASON_BOOKMARK_INVALID, buf);
1900 break;
1901 default:
1902 pr_err("Unknown TMR function: 0x%02x, protocol"
1903 " error.\n", function);
1904 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1905 goto attach;
1908 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1909 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1910 se_tmr->call_transport = 1;
1911 attach:
1912 spin_lock_bh(&conn->cmd_lock);
1913 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1914 spin_unlock_bh(&conn->cmd_lock);
1916 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1917 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1918 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1919 out_of_order_cmdsn = 1;
1920 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1921 return 0;
1922 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1923 return -1;
1925 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1927 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
1928 return 0;
1930 * Found the referenced task, send to transport for processing.
1932 if (se_tmr->call_transport)
1933 return transport_generic_handle_tmr(&cmd->se_cmd);
1936 * Could not find the referenced LUN, task, or Task Management
1937 * command not authorized or supported. Change state and
1938 * let the tx_thread send the response.
1940 * For connection recovery, this is also the default action for
1941 * TMR TASK_REASSIGN.
1943 if (sess_ref) {
1944 pr_debug("Handle TMR, using sess_ref=true check\n");
1945 target_put_sess_cmd(&cmd->se_cmd);
1948 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1949 return 0;
1951 EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
1953 /* #warning FIXME: Support Text Command parameters besides SendTargets */
1955 iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1956 struct iscsi_text *hdr)
1958 u32 payload_length = ntoh24(hdr->dlength);
1960 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1961 pr_err("Unable to accept text parameter length: %u"
1962 "greater than MaxXmitDataSegmentLength %u.\n",
1963 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
1964 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1965 (unsigned char *)hdr);
1968 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
1969 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
1970 pr_err("Multi sequence text commands currently not supported\n");
1971 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
1972 (unsigned char *)hdr);
1975 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1976 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1977 hdr->exp_statsn, payload_length);
1979 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1980 cmd->i_state = ISTATE_SEND_TEXTRSP;
1981 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1982 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1983 cmd->targ_xfer_tag = 0xFFFFFFFF;
1984 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1985 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1986 cmd->data_direction = DMA_NONE;
1987 cmd->text_in_ptr = NULL;
1989 return 0;
1991 EXPORT_SYMBOL(iscsit_setup_text_cmd);
1994 iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1995 struct iscsi_text *hdr)
1997 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
1998 int cmdsn_ret;
2000 if (!text_in) {
2001 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2002 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2003 pr_err("Unable to locate text_in buffer for sendtargets"
2004 " discovery\n");
2005 goto reject;
2007 goto empty_sendtargets;
2009 if (strncmp("SendTargets", text_in, 11) != 0) {
2010 pr_err("Received Text Data that is not"
2011 " SendTargets, cannot continue.\n");
2012 goto reject;
2014 text_ptr = strchr(text_in, '=');
2015 if (!text_ptr) {
2016 pr_err("No \"=\" separator found in Text Data,"
2017 " cannot continue.\n");
2018 goto reject;
2020 if (!strncmp("=All", text_ptr, 4)) {
2021 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
2022 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2023 !strncmp("=eui.", text_ptr, 5)) {
2024 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
2025 } else {
2026 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2027 goto reject;
2030 spin_lock_bh(&conn->cmd_lock);
2031 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2032 spin_unlock_bh(&conn->cmd_lock);
2034 empty_sendtargets:
2035 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2037 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2038 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2039 (unsigned char *)hdr, hdr->cmdsn);
2040 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2041 return -1;
2043 return 0;
2046 return iscsit_execute_cmd(cmd, 0);
2048 reject:
2049 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2050 (unsigned char *)hdr);
2052 EXPORT_SYMBOL(iscsit_process_text_cmd);
2054 static int
2055 iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2056 unsigned char *buf)
2058 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2059 char *text_in = NULL;
2060 u32 payload_length = ntoh24(hdr->dlength);
2061 int rx_size, rc;
2063 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2064 if (rc < 0)
2065 return 0;
2067 rx_size = payload_length;
2068 if (payload_length) {
2069 u32 checksum = 0, data_crc = 0;
2070 u32 padding = 0, pad_bytes = 0;
2071 int niov = 0, rx_got;
2072 struct kvec iov[3];
2074 text_in = kzalloc(payload_length, GFP_KERNEL);
2075 if (!text_in) {
2076 pr_err("Unable to allocate memory for"
2077 " incoming text parameters\n");
2078 goto reject;
2080 cmd->text_in_ptr = text_in;
2082 memset(iov, 0, 3 * sizeof(struct kvec));
2083 iov[niov].iov_base = text_in;
2084 iov[niov++].iov_len = payload_length;
2086 padding = ((-payload_length) & 3);
2087 if (padding != 0) {
2088 iov[niov].iov_base = &pad_bytes;
2089 iov[niov++].iov_len = padding;
2090 rx_size += padding;
2091 pr_debug("Receiving %u additional bytes"
2092 " for padding.\n", padding);
2094 if (conn->conn_ops->DataDigest) {
2095 iov[niov].iov_base = &checksum;
2096 iov[niov++].iov_len = ISCSI_CRC_LEN;
2097 rx_size += ISCSI_CRC_LEN;
2100 rx_got = rx_data(conn, &iov[0], niov, rx_size);
2101 if (rx_got != rx_size)
2102 goto reject;
2104 if (conn->conn_ops->DataDigest) {
2105 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
2106 text_in, payload_length,
2107 padding, (u8 *)&pad_bytes,
2108 (u8 *)&data_crc);
2110 if (checksum != data_crc) {
2111 pr_err("Text data CRC32C DataDigest"
2112 " 0x%08x does not match computed"
2113 " 0x%08x\n", checksum, data_crc);
2114 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2115 pr_err("Unable to recover from"
2116 " Text Data digest failure while in"
2117 " ERL=0.\n");
2118 goto reject;
2119 } else {
2121 * Silently drop this PDU and let the
2122 * initiator plug the CmdSN gap.
2124 pr_debug("Dropping Text"
2125 " Command CmdSN: 0x%08x due to"
2126 " DataCRC error.\n", hdr->cmdsn);
2127 kfree(text_in);
2128 return 0;
2130 } else {
2131 pr_debug("Got CRC32C DataDigest"
2132 " 0x%08x for %u bytes of text data.\n",
2133 checksum, payload_length);
2136 text_in[payload_length - 1] = '\0';
2137 pr_debug("Successfully read %d bytes of text"
2138 " data.\n", payload_length);
2141 return iscsit_process_text_cmd(conn, cmd, hdr);
2143 reject:
2144 kfree(cmd->text_in_ptr);
2145 cmd->text_in_ptr = NULL;
2146 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
2149 int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2151 struct iscsi_conn *conn_p;
2152 struct iscsi_session *sess = conn->sess;
2154 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2155 " for SID: %u.\n", conn->cid, conn->sess->sid);
2157 atomic_set(&sess->session_logout, 1);
2158 atomic_set(&conn->conn_logout_remove, 1);
2159 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2161 iscsit_inc_conn_usage_count(conn);
2162 iscsit_inc_session_usage_count(sess);
2164 spin_lock_bh(&sess->conn_lock);
2165 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2166 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2167 continue;
2169 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2170 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2172 spin_unlock_bh(&sess->conn_lock);
2174 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2176 return 0;
2179 int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2181 struct iscsi_conn *l_conn;
2182 struct iscsi_session *sess = conn->sess;
2184 pr_debug("Received logout request CLOSECONNECTION for CID:"
2185 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2188 * A Logout Request with a CLOSECONNECTION reason code for a CID
2189 * can arrive on a connection with a differing CID.
2191 if (conn->cid == cmd->logout_cid) {
2192 spin_lock_bh(&conn->state_lock);
2193 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2194 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2196 atomic_set(&conn->conn_logout_remove, 1);
2197 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2198 iscsit_inc_conn_usage_count(conn);
2200 spin_unlock_bh(&conn->state_lock);
2201 } else {
2203 * Handle all different cid CLOSECONNECTION requests in
2204 * iscsit_logout_post_handler_diffcid() as to give enough
2205 * time for any non immediate command's CmdSN to be
2206 * acknowledged on the connection in question.
2208 * Here we simply make sure the CID is still around.
2210 l_conn = iscsit_get_conn_from_cid(sess,
2211 cmd->logout_cid);
2212 if (!l_conn) {
2213 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2214 iscsit_add_cmd_to_response_queue(cmd, conn,
2215 cmd->i_state);
2216 return 0;
2219 iscsit_dec_conn_usage_count(l_conn);
2222 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2224 return 0;
2227 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2229 struct iscsi_session *sess = conn->sess;
2231 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2232 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2234 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2235 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2236 " while ERL!=2.\n");
2237 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2238 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2239 return 0;
2242 if (conn->cid == cmd->logout_cid) {
2243 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2244 " with CID: %hu on CID: %hu, implementation error.\n",
2245 cmd->logout_cid, conn->cid);
2246 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2247 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2248 return 0;
2251 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2253 return 0;
2257 iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2258 unsigned char *buf)
2260 int cmdsn_ret, logout_remove = 0;
2261 u8 reason_code = 0;
2262 struct iscsi_logout *hdr;
2263 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2265 hdr = (struct iscsi_logout *) buf;
2266 reason_code = (hdr->flags & 0x7f);
2268 if (tiqn) {
2269 spin_lock(&tiqn->logout_stats.lock);
2270 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2271 tiqn->logout_stats.normal_logouts++;
2272 else
2273 tiqn->logout_stats.abnormal_logouts++;
2274 spin_unlock(&tiqn->logout_stats.lock);
2277 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2278 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2279 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2280 hdr->cid, conn->cid);
2282 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2283 pr_err("Received logout request on connection that"
2284 " is not in logged in state, ignoring request.\n");
2285 iscsit_free_cmd(cmd, false);
2286 return 0;
2289 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2290 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2291 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2292 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2293 cmd->targ_xfer_tag = 0xFFFFFFFF;
2294 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2295 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2296 cmd->logout_cid = be16_to_cpu(hdr->cid);
2297 cmd->logout_reason = reason_code;
2298 cmd->data_direction = DMA_NONE;
2301 * We need to sleep in these cases (by returning 1) until the Logout
2302 * Response gets sent in the tx thread.
2304 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2305 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
2306 be16_to_cpu(hdr->cid) == conn->cid))
2307 logout_remove = 1;
2309 spin_lock_bh(&conn->cmd_lock);
2310 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2311 spin_unlock_bh(&conn->cmd_lock);
2313 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
2314 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2317 * Immediate commands are executed, well, immediately.
2318 * Non-Immediate Logout Commands are executed in CmdSN order.
2320 if (cmd->immediate_cmd) {
2321 int ret = iscsit_execute_cmd(cmd, 0);
2323 if (ret < 0)
2324 return ret;
2325 } else {
2326 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
2327 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
2328 logout_remove = 0;
2329 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2330 return -1;
2333 return logout_remove;
2335 EXPORT_SYMBOL(iscsit_handle_logout_cmd);
2337 static int iscsit_handle_snack(
2338 struct iscsi_conn *conn,
2339 unsigned char *buf)
2341 struct iscsi_snack *hdr;
2343 hdr = (struct iscsi_snack *) buf;
2344 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
2346 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2347 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2348 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2349 hdr->begrun, hdr->runlength, conn->cid);
2351 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2352 pr_err("Initiator sent SNACK request while in"
2353 " ErrorRecoveryLevel=0.\n");
2354 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2355 buf);
2358 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2359 * call from inside iscsi_send_recovery_datain_or_r2t().
2361 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2362 case 0:
2363 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
2364 hdr->itt,
2365 be32_to_cpu(hdr->ttt),
2366 be32_to_cpu(hdr->begrun),
2367 be32_to_cpu(hdr->runlength));
2368 case ISCSI_FLAG_SNACK_TYPE_STATUS:
2369 return iscsit_handle_status_snack(conn, hdr->itt,
2370 be32_to_cpu(hdr->ttt),
2371 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
2372 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
2373 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2374 be32_to_cpu(hdr->begrun),
2375 be32_to_cpu(hdr->runlength));
2376 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2377 /* FIXME: Support R-Data SNACK */
2378 pr_err("R-Data SNACK Not Supported.\n");
2379 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2380 buf);
2381 default:
2382 pr_err("Unknown SNACK type 0x%02x, protocol"
2383 " error.\n", hdr->flags & 0x0f);
2384 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2385 buf);
2388 return 0;
2391 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2393 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2394 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2395 wait_for_completion_interruptible_timeout(
2396 &conn->rx_half_close_comp,
2397 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2401 static int iscsit_handle_immediate_data(
2402 struct iscsi_cmd *cmd,
2403 struct iscsi_scsi_req *hdr,
2404 u32 length)
2406 int iov_ret, rx_got = 0, rx_size = 0;
2407 u32 checksum, iov_count = 0, padding = 0;
2408 struct iscsi_conn *conn = cmd->conn;
2409 struct kvec *iov;
2411 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2412 if (iov_ret < 0)
2413 return IMMEDIATE_DATA_CANNOT_RECOVER;
2415 rx_size = length;
2416 iov_count = iov_ret;
2417 iov = &cmd->iov_data[0];
2419 padding = ((-length) & 3);
2420 if (padding != 0) {
2421 iov[iov_count].iov_base = cmd->pad_bytes;
2422 iov[iov_count++].iov_len = padding;
2423 rx_size += padding;
2426 if (conn->conn_ops->DataDigest) {
2427 iov[iov_count].iov_base = &checksum;
2428 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2429 rx_size += ISCSI_CRC_LEN;
2432 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2434 iscsit_unmap_iovec(cmd);
2436 if (rx_got != rx_size) {
2437 iscsit_rx_thread_wait_for_tcp(conn);
2438 return IMMEDIATE_DATA_CANNOT_RECOVER;
2441 if (conn->conn_ops->DataDigest) {
2442 u32 data_crc;
2444 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2445 cmd->write_data_done, length, padding,
2446 cmd->pad_bytes);
2448 if (checksum != data_crc) {
2449 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2450 " does not match computed 0x%08x\n", checksum,
2451 data_crc);
2453 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2454 pr_err("Unable to recover from"
2455 " Immediate Data digest failure while"
2456 " in ERL=0.\n");
2457 iscsit_reject_cmd(cmd,
2458 ISCSI_REASON_DATA_DIGEST_ERROR,
2459 (unsigned char *)hdr);
2460 return IMMEDIATE_DATA_CANNOT_RECOVER;
2461 } else {
2462 iscsit_reject_cmd(cmd,
2463 ISCSI_REASON_DATA_DIGEST_ERROR,
2464 (unsigned char *)hdr);
2465 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2467 } else {
2468 pr_debug("Got CRC32C DataDigest 0x%08x for"
2469 " %u bytes of Immediate Data\n", checksum,
2470 length);
2474 cmd->write_data_done += length;
2476 if (cmd->write_data_done == cmd->se_cmd.data_length) {
2477 spin_lock_bh(&cmd->istate_lock);
2478 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2479 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2480 spin_unlock_bh(&cmd->istate_lock);
2483 return IMMEDIATE_DATA_NORMAL_OPERATION;
2487 * Called with sess->conn_lock held.
2489 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2490 with active network interface */
2491 static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2493 struct iscsi_cmd *cmd;
2494 struct iscsi_conn *conn_p;
2495 bool found = false;
2498 * Only send a Asynchronous Message on connections whos network
2499 * interface is still functional.
2501 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2502 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2503 iscsit_inc_conn_usage_count(conn_p);
2504 found = true;
2505 break;
2509 if (!found)
2510 return;
2512 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
2513 if (!cmd) {
2514 iscsit_dec_conn_usage_count(conn_p);
2515 return;
2518 cmd->logout_cid = conn->cid;
2519 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2520 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2522 spin_lock_bh(&conn_p->cmd_lock);
2523 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
2524 spin_unlock_bh(&conn_p->cmd_lock);
2526 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2527 iscsit_dec_conn_usage_count(conn_p);
2530 static int iscsit_send_conn_drop_async_message(
2531 struct iscsi_cmd *cmd,
2532 struct iscsi_conn *conn)
2534 struct iscsi_async *hdr;
2536 cmd->tx_size = ISCSI_HDR_LEN;
2537 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2539 hdr = (struct iscsi_async *) cmd->pdu;
2540 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2541 hdr->flags = ISCSI_FLAG_CMD_FINAL;
2542 cmd->init_task_tag = RESERVED_ITT;
2543 cmd->targ_xfer_tag = 0xFFFFFFFF;
2544 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2545 cmd->stat_sn = conn->stat_sn++;
2546 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2547 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2548 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2549 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2550 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2551 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2552 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2554 if (conn->conn_ops->HeaderDigest) {
2555 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2557 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2558 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
2560 cmd->tx_size += ISCSI_CRC_LEN;
2561 pr_debug("Attaching CRC32C HeaderDigest to"
2562 " Async Message 0x%08x\n", *header_digest);
2565 cmd->iov_misc[0].iov_base = cmd->pdu;
2566 cmd->iov_misc[0].iov_len = cmd->tx_size;
2567 cmd->iov_misc_count = 1;
2569 pr_debug("Sending Connection Dropped Async Message StatSN:"
2570 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2571 cmd->logout_cid, conn->cid);
2572 return 0;
2575 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2577 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2578 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2579 wait_for_completion_interruptible_timeout(
2580 &conn->tx_half_close_comp,
2581 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2585 static void
2586 iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2587 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2588 bool set_statsn)
2590 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2591 hdr->flags = datain->flags;
2592 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2593 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2594 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2595 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2596 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2597 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2598 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2601 hton24(hdr->dlength, datain->length);
2602 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2603 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2604 (struct scsi_lun *)&hdr->lun);
2605 else
2606 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2608 hdr->itt = cmd->init_task_tag;
2610 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2611 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2612 else
2613 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2614 if (set_statsn)
2615 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2616 else
2617 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2619 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2620 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2621 hdr->datasn = cpu_to_be32(datain->data_sn);
2622 hdr->offset = cpu_to_be32(datain->offset);
2624 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2625 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2626 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2627 ntohl(hdr->offset), datain->length, conn->cid);
2630 static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2632 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
2633 struct iscsi_datain datain;
2634 struct iscsi_datain_req *dr;
2635 struct kvec *iov;
2636 u32 iov_count = 0, tx_size = 0;
2637 int eodr = 0, ret, iov_ret;
2638 bool set_statsn = false;
2640 memset(&datain, 0, sizeof(struct iscsi_datain));
2641 dr = iscsit_get_datain_values(cmd, &datain);
2642 if (!dr) {
2643 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2644 cmd->init_task_tag);
2645 return -1;
2648 * Be paranoid and double check the logic for now.
2650 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
2651 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2652 " datain.length: %u exceeds cmd->data_length: %u\n",
2653 cmd->init_task_tag, datain.offset, datain.length,
2654 cmd->se_cmd.data_length);
2655 return -1;
2658 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
2660 * Special case for successfully execution w/ both DATAIN
2661 * and Sense Data.
2663 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2664 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2665 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2666 else {
2667 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2668 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2669 iscsit_increment_maxcmdsn(cmd, conn->sess);
2670 cmd->stat_sn = conn->stat_sn++;
2671 set_statsn = true;
2672 } else if (dr->dr_complete ==
2673 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2674 set_statsn = true;
2677 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
2679 iov = &cmd->iov_data[0];
2680 iov[iov_count].iov_base = cmd->pdu;
2681 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2682 tx_size += ISCSI_HDR_LEN;
2684 if (conn->conn_ops->HeaderDigest) {
2685 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2687 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2688 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
2690 iov[0].iov_len += ISCSI_CRC_LEN;
2691 tx_size += ISCSI_CRC_LEN;
2693 pr_debug("Attaching CRC32 HeaderDigest"
2694 " for DataIN PDU 0x%08x\n", *header_digest);
2697 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2698 datain.offset, datain.length);
2699 if (iov_ret < 0)
2700 return -1;
2702 iov_count += iov_ret;
2703 tx_size += datain.length;
2705 cmd->padding = ((-datain.length) & 3);
2706 if (cmd->padding) {
2707 iov[iov_count].iov_base = cmd->pad_bytes;
2708 iov[iov_count++].iov_len = cmd->padding;
2709 tx_size += cmd->padding;
2711 pr_debug("Attaching %u padding bytes\n",
2712 cmd->padding);
2714 if (conn->conn_ops->DataDigest) {
2715 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2716 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2718 iov[iov_count].iov_base = &cmd->data_crc;
2719 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2720 tx_size += ISCSI_CRC_LEN;
2722 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2723 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2726 cmd->iov_data_count = iov_count;
2727 cmd->tx_size = tx_size;
2729 ret = iscsit_fe_sendpage_sg(cmd, conn);
2731 iscsit_unmap_iovec(cmd);
2733 if (ret < 0) {
2734 iscsit_tx_thread_wait_for_tcp(conn);
2735 return ret;
2738 if (dr->dr_complete) {
2739 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
2740 2 : 1;
2741 iscsit_free_datain_req(cmd, dr);
2744 return eodr;
2748 iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2749 struct iscsi_logout_rsp *hdr)
2751 struct iscsi_conn *logout_conn = NULL;
2752 struct iscsi_conn_recovery *cr = NULL;
2753 struct iscsi_session *sess = conn->sess;
2755 * The actual shutting down of Sessions and/or Connections
2756 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2757 * is done in scsi_logout_post_handler().
2759 switch (cmd->logout_reason) {
2760 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2761 pr_debug("iSCSI session logout successful, setting"
2762 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2763 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2764 break;
2765 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2766 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2767 break;
2769 * For CLOSECONNECTION logout requests carrying
2770 * a matching logout CID -> local CID, the reference
2771 * for the local CID will have been incremented in
2772 * iscsi_logout_closeconnection().
2774 * For CLOSECONNECTION logout requests carrying
2775 * a different CID than the connection it arrived
2776 * on, the connection responding to cmd->logout_cid
2777 * is stopped in iscsit_logout_post_handler_diffcid().
2780 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2781 " successful.\n", cmd->logout_cid, conn->cid);
2782 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2783 break;
2784 case ISCSI_LOGOUT_REASON_RECOVERY:
2785 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2786 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2787 break;
2789 * If the connection is still active from our point of view
2790 * force connection recovery to occur.
2792 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2793 cmd->logout_cid);
2794 if (logout_conn) {
2795 iscsit_connection_reinstatement_rcfr(logout_conn);
2796 iscsit_dec_conn_usage_count(logout_conn);
2799 cr = iscsit_get_inactive_connection_recovery_entry(
2800 conn->sess, cmd->logout_cid);
2801 if (!cr) {
2802 pr_err("Unable to locate CID: %hu for"
2803 " REMOVECONNFORRECOVERY Logout Request.\n",
2804 cmd->logout_cid);
2805 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2806 break;
2809 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2811 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2812 " for recovery for CID: %hu on CID: %hu successful.\n",
2813 cmd->logout_cid, conn->cid);
2814 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2815 break;
2816 default:
2817 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2818 cmd->logout_reason);
2819 return -1;
2822 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2823 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2824 hdr->response = cmd->logout_response;
2825 hdr->itt = cmd->init_task_tag;
2826 cmd->stat_sn = conn->stat_sn++;
2827 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2829 iscsit_increment_maxcmdsn(cmd, conn->sess);
2830 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2831 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2833 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2834 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2835 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2836 cmd->logout_cid, conn->cid);
2838 return 0;
2840 EXPORT_SYMBOL(iscsit_build_logout_rsp);
2842 static int
2843 iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2845 struct kvec *iov;
2846 int niov = 0, tx_size, rc;
2848 rc = iscsit_build_logout_rsp(cmd, conn,
2849 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2850 if (rc < 0)
2851 return rc;
2853 tx_size = ISCSI_HDR_LEN;
2854 iov = &cmd->iov_misc[0];
2855 iov[niov].iov_base = cmd->pdu;
2856 iov[niov++].iov_len = ISCSI_HDR_LEN;
2858 if (conn->conn_ops->HeaderDigest) {
2859 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2861 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2862 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
2864 iov[0].iov_len += ISCSI_CRC_LEN;
2865 tx_size += ISCSI_CRC_LEN;
2866 pr_debug("Attaching CRC32C HeaderDigest to"
2867 " Logout Response 0x%08x\n", *header_digest);
2869 cmd->iov_misc_count = niov;
2870 cmd->tx_size = tx_size;
2872 return 0;
2875 void
2876 iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2877 struct iscsi_nopin *hdr, bool nopout_response)
2879 hdr->opcode = ISCSI_OP_NOOP_IN;
2880 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2881 hton24(hdr->dlength, cmd->buf_ptr_size);
2882 if (nopout_response)
2883 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2884 hdr->itt = cmd->init_task_tag;
2885 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2886 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2887 conn->stat_sn;
2888 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2890 if (nopout_response)
2891 iscsit_increment_maxcmdsn(cmd, conn->sess);
2893 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2894 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2896 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2897 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2898 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2899 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2901 EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2904 * Unsolicited NOPIN, either requesting a response or not.
2906 static int iscsit_send_unsolicited_nopin(
2907 struct iscsi_cmd *cmd,
2908 struct iscsi_conn *conn,
2909 int want_response)
2911 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2912 int tx_size = ISCSI_HDR_LEN, ret;
2914 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
2916 if (conn->conn_ops->HeaderDigest) {
2917 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2919 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2920 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
2922 tx_size += ISCSI_CRC_LEN;
2923 pr_debug("Attaching CRC32C HeaderDigest to"
2924 " NopIN 0x%08x\n", *header_digest);
2927 cmd->iov_misc[0].iov_base = cmd->pdu;
2928 cmd->iov_misc[0].iov_len = tx_size;
2929 cmd->iov_misc_count = 1;
2930 cmd->tx_size = tx_size;
2932 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2933 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2935 ret = iscsit_send_tx_data(cmd, conn, 1);
2936 if (ret < 0) {
2937 iscsit_tx_thread_wait_for_tcp(conn);
2938 return ret;
2941 spin_lock_bh(&cmd->istate_lock);
2942 cmd->i_state = want_response ?
2943 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2944 spin_unlock_bh(&cmd->istate_lock);
2946 return 0;
2949 static int
2950 iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2952 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2953 struct kvec *iov;
2954 u32 padding = 0;
2955 int niov = 0, tx_size;
2957 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
2959 tx_size = ISCSI_HDR_LEN;
2960 iov = &cmd->iov_misc[0];
2961 iov[niov].iov_base = cmd->pdu;
2962 iov[niov++].iov_len = ISCSI_HDR_LEN;
2964 if (conn->conn_ops->HeaderDigest) {
2965 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2967 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2968 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
2970 iov[0].iov_len += ISCSI_CRC_LEN;
2971 tx_size += ISCSI_CRC_LEN;
2972 pr_debug("Attaching CRC32C HeaderDigest"
2973 " to NopIn 0x%08x\n", *header_digest);
2977 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2978 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2980 if (cmd->buf_ptr_size) {
2981 iov[niov].iov_base = cmd->buf_ptr;
2982 iov[niov++].iov_len = cmd->buf_ptr_size;
2983 tx_size += cmd->buf_ptr_size;
2985 pr_debug("Echoing back %u bytes of ping"
2986 " data.\n", cmd->buf_ptr_size);
2988 padding = ((-cmd->buf_ptr_size) & 3);
2989 if (padding != 0) {
2990 iov[niov].iov_base = &cmd->pad_bytes;
2991 iov[niov++].iov_len = padding;
2992 tx_size += padding;
2993 pr_debug("Attaching %u additional"
2994 " padding bytes.\n", padding);
2996 if (conn->conn_ops->DataDigest) {
2997 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2998 cmd->buf_ptr, cmd->buf_ptr_size,
2999 padding, (u8 *)&cmd->pad_bytes,
3000 (u8 *)&cmd->data_crc);
3002 iov[niov].iov_base = &cmd->data_crc;
3003 iov[niov++].iov_len = ISCSI_CRC_LEN;
3004 tx_size += ISCSI_CRC_LEN;
3005 pr_debug("Attached DataDigest for %u"
3006 " bytes of ping data, CRC 0x%08x\n",
3007 cmd->buf_ptr_size, cmd->data_crc);
3011 cmd->iov_misc_count = niov;
3012 cmd->tx_size = tx_size;
3014 return 0;
3017 static int iscsit_send_r2t(
3018 struct iscsi_cmd *cmd,
3019 struct iscsi_conn *conn)
3021 int tx_size = 0;
3022 struct iscsi_r2t *r2t;
3023 struct iscsi_r2t_rsp *hdr;
3024 int ret;
3026 r2t = iscsit_get_r2t_from_list(cmd);
3027 if (!r2t)
3028 return -1;
3030 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3031 memset(hdr, 0, ISCSI_HDR_LEN);
3032 hdr->opcode = ISCSI_OP_R2T;
3033 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3034 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3035 (struct scsi_lun *)&hdr->lun);
3036 hdr->itt = cmd->init_task_tag;
3037 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
3038 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3039 hdr->statsn = cpu_to_be32(conn->stat_sn);
3040 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3041 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3042 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3043 hdr->data_offset = cpu_to_be32(r2t->offset);
3044 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3046 cmd->iov_misc[0].iov_base = cmd->pdu;
3047 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3048 tx_size += ISCSI_HDR_LEN;
3050 if (conn->conn_ops->HeaderDigest) {
3051 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3053 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3054 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
3056 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3057 tx_size += ISCSI_CRC_LEN;
3058 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3059 " PDU 0x%08x\n", *header_digest);
3062 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3063 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3064 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3065 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3066 r2t->offset, r2t->xfer_len, conn->cid);
3068 cmd->iov_misc_count = 1;
3069 cmd->tx_size = tx_size;
3071 spin_lock_bh(&cmd->r2t_lock);
3072 r2t->sent_r2t = 1;
3073 spin_unlock_bh(&cmd->r2t_lock);
3075 ret = iscsit_send_tx_data(cmd, conn, 1);
3076 if (ret < 0) {
3077 iscsit_tx_thread_wait_for_tcp(conn);
3078 return ret;
3081 spin_lock_bh(&cmd->dataout_timeout_lock);
3082 iscsit_start_dataout_timer(cmd, conn);
3083 spin_unlock_bh(&cmd->dataout_timeout_lock);
3085 return 0;
3089 * @recovery: If called from iscsi_task_reassign_complete_write() for
3090 * connection recovery.
3092 int iscsit_build_r2ts_for_cmd(
3093 struct iscsi_conn *conn,
3094 struct iscsi_cmd *cmd,
3095 bool recovery)
3097 int first_r2t = 1;
3098 u32 offset = 0, xfer_len = 0;
3100 spin_lock_bh(&cmd->r2t_lock);
3101 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3102 spin_unlock_bh(&cmd->r2t_lock);
3103 return 0;
3106 if (conn->sess->sess_ops->DataSequenceInOrder &&
3107 !recovery)
3108 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
3110 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3111 if (conn->sess->sess_ops->DataSequenceInOrder) {
3112 offset = cmd->r2t_offset;
3114 if (first_r2t && recovery) {
3115 int new_data_end = offset +
3116 conn->sess->sess_ops->MaxBurstLength -
3117 cmd->next_burst_len;
3119 if (new_data_end > cmd->se_cmd.data_length)
3120 xfer_len = cmd->se_cmd.data_length - offset;
3121 else
3122 xfer_len =
3123 conn->sess->sess_ops->MaxBurstLength -
3124 cmd->next_burst_len;
3125 } else {
3126 int new_data_end = offset +
3127 conn->sess->sess_ops->MaxBurstLength;
3129 if (new_data_end > cmd->se_cmd.data_length)
3130 xfer_len = cmd->se_cmd.data_length - offset;
3131 else
3132 xfer_len = conn->sess->sess_ops->MaxBurstLength;
3134 cmd->r2t_offset += xfer_len;
3136 if (cmd->r2t_offset == cmd->se_cmd.data_length)
3137 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3138 } else {
3139 struct iscsi_seq *seq;
3141 seq = iscsit_get_seq_holder_for_r2t(cmd);
3142 if (!seq) {
3143 spin_unlock_bh(&cmd->r2t_lock);
3144 return -1;
3147 offset = seq->offset;
3148 xfer_len = seq->xfer_len;
3150 if (cmd->seq_send_order == cmd->seq_count)
3151 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3153 cmd->outstanding_r2ts++;
3154 first_r2t = 0;
3156 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3157 spin_unlock_bh(&cmd->r2t_lock);
3158 return -1;
3161 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3162 break;
3164 spin_unlock_bh(&cmd->r2t_lock);
3166 return 0;
3169 void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3170 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
3172 if (inc_stat_sn)
3173 cmd->stat_sn = conn->stat_sn++;
3175 atomic_long_inc(&conn->sess->rsp_pdus);
3177 memset(hdr, 0, ISCSI_HDR_LEN);
3178 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3179 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3180 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3181 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
3182 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3183 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3184 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
3185 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3187 hdr->response = cmd->iscsi_response;
3188 hdr->cmd_status = cmd->se_cmd.scsi_status;
3189 hdr->itt = cmd->init_task_tag;
3190 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3192 iscsit_increment_maxcmdsn(cmd, conn->sess);
3193 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3194 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3196 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3197 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3198 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3199 cmd->se_cmd.scsi_status, conn->cid);
3201 EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3203 static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3205 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3206 struct kvec *iov;
3207 u32 padding = 0, tx_size = 0;
3208 int iov_count = 0;
3209 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3211 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3213 iov = &cmd->iov_misc[0];
3214 iov[iov_count].iov_base = cmd->pdu;
3215 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3216 tx_size += ISCSI_HDR_LEN;
3219 * Attach SENSE DATA payload to iSCSI Response PDU
3221 if (cmd->se_cmd.sense_buffer &&
3222 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3223 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
3224 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3225 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3227 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
3228 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
3229 iov[iov_count].iov_base = cmd->sense_buffer;
3230 iov[iov_count++].iov_len =
3231 (cmd->se_cmd.scsi_sense_length + padding);
3232 tx_size += cmd->se_cmd.scsi_sense_length;
3234 if (padding) {
3235 memset(cmd->sense_buffer +
3236 cmd->se_cmd.scsi_sense_length, 0, padding);
3237 tx_size += padding;
3238 pr_debug("Adding %u bytes of padding to"
3239 " SENSE.\n", padding);
3242 if (conn->conn_ops->DataDigest) {
3243 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3244 cmd->sense_buffer,
3245 (cmd->se_cmd.scsi_sense_length + padding),
3246 0, NULL, (u8 *)&cmd->data_crc);
3248 iov[iov_count].iov_base = &cmd->data_crc;
3249 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3250 tx_size += ISCSI_CRC_LEN;
3252 pr_debug("Attaching CRC32 DataDigest for"
3253 " SENSE, %u bytes CRC 0x%08x\n",
3254 (cmd->se_cmd.scsi_sense_length + padding),
3255 cmd->data_crc);
3258 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3259 " Response PDU\n",
3260 cmd->se_cmd.scsi_sense_length);
3263 if (conn->conn_ops->HeaderDigest) {
3264 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3266 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3267 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
3269 iov[0].iov_len += ISCSI_CRC_LEN;
3270 tx_size += ISCSI_CRC_LEN;
3271 pr_debug("Attaching CRC32 HeaderDigest for Response"
3272 " PDU 0x%08x\n", *header_digest);
3275 cmd->iov_misc_count = iov_count;
3276 cmd->tx_size = tx_size;
3278 return 0;
3281 static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3283 switch (se_tmr->response) {
3284 case TMR_FUNCTION_COMPLETE:
3285 return ISCSI_TMF_RSP_COMPLETE;
3286 case TMR_TASK_DOES_NOT_EXIST:
3287 return ISCSI_TMF_RSP_NO_TASK;
3288 case TMR_LUN_DOES_NOT_EXIST:
3289 return ISCSI_TMF_RSP_NO_LUN;
3290 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3291 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3292 case TMR_FUNCTION_REJECTED:
3293 default:
3294 return ISCSI_TMF_RSP_REJECTED;
3298 void
3299 iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3300 struct iscsi_tm_rsp *hdr)
3302 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3304 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
3305 hdr->flags = ISCSI_FLAG_CMD_FINAL;
3306 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
3307 hdr->itt = cmd->init_task_tag;
3308 cmd->stat_sn = conn->stat_sn++;
3309 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3311 iscsit_increment_maxcmdsn(cmd, conn->sess);
3312 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3313 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3315 pr_debug("Built Task Management Response ITT: 0x%08x,"
3316 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3317 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3319 EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3321 static int
3322 iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3324 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3325 u32 tx_size = 0;
3327 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3329 cmd->iov_misc[0].iov_base = cmd->pdu;
3330 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3331 tx_size += ISCSI_HDR_LEN;
3333 if (conn->conn_ops->HeaderDigest) {
3334 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3336 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3337 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
3339 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3340 tx_size += ISCSI_CRC_LEN;
3341 pr_debug("Attaching CRC32 HeaderDigest for Task"
3342 " Mgmt Response PDU 0x%08x\n", *header_digest);
3345 cmd->iov_misc_count = 1;
3346 cmd->tx_size = tx_size;
3348 return 0;
3351 static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3353 bool ret = false;
3355 if (np->np_sockaddr.ss_family == AF_INET6) {
3356 const struct sockaddr_in6 sin6 = {
3357 .sin6_addr = IN6ADDR_ANY_INIT };
3358 struct sockaddr_in6 *sock_in6 =
3359 (struct sockaddr_in6 *)&np->np_sockaddr;
3361 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3362 sin6.sin6_addr.s6_addr, 16))
3363 ret = true;
3364 } else {
3365 struct sockaddr_in * sock_in =
3366 (struct sockaddr_in *)&np->np_sockaddr;
3368 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
3369 ret = true;
3372 return ret;
3375 #define SENDTARGETS_BUF_LIMIT 32768U
3377 static int
3378 iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
3379 enum iscsit_transport_type network_transport,
3380 int skip_bytes, bool *completed)
3382 char *payload = NULL;
3383 struct iscsi_conn *conn = cmd->conn;
3384 struct iscsi_portal_group *tpg;
3385 struct iscsi_tiqn *tiqn;
3386 struct iscsi_tpg_np *tpg_np;
3387 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
3388 int target_name_printed;
3389 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
3390 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
3391 bool active;
3393 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
3394 SENDTARGETS_BUF_LIMIT);
3396 payload = kzalloc(buffer_len, GFP_KERNEL);
3397 if (!payload) {
3398 pr_err("Unable to allocate memory for sendtargets"
3399 " response.\n");
3400 return -ENOMEM;
3403 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
3404 * explicit case..
3406 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
3407 text_ptr = strchr(text_in, '=');
3408 if (!text_ptr) {
3409 pr_err("Unable to locate '=' string in text_in:"
3410 " %s\n", text_in);
3411 kfree(payload);
3412 return -EINVAL;
3415 * Skip over '=' character..
3417 text_ptr += 1;
3420 spin_lock(&tiqn_lock);
3421 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3422 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
3423 strcmp(tiqn->tiqn, text_ptr)) {
3424 continue;
3427 target_name_printed = 0;
3429 spin_lock(&tiqn->tiqn_tpg_lock);
3430 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3432 /* If demo_mode_discovery=0 and generate_node_acls=0
3433 * (demo mode dislabed) do not return
3434 * TargetName+TargetAddress unless a NodeACL exists.
3437 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3438 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3439 (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg,
3440 cmd->conn->sess->sess_ops->InitiatorName))) {
3441 continue;
3444 spin_lock(&tpg->tpg_state_lock);
3445 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
3446 spin_unlock(&tpg->tpg_state_lock);
3448 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3449 continue;
3451 spin_lock(&tpg->tpg_np_lock);
3452 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3453 tpg_np_list) {
3454 struct iscsi_np *np = tpg_np->tpg_np;
3455 bool inaddr_any = iscsit_check_inaddr_any(np);
3456 struct sockaddr_storage *sockaddr;
3458 if (np->np_network_transport != network_transport)
3459 continue;
3461 if (!target_name_printed) {
3462 len = sprintf(buf, "TargetName=%s",
3463 tiqn->tiqn);
3464 len += 1;
3466 if ((len + payload_len) > buffer_len) {
3467 spin_unlock(&tpg->tpg_np_lock);
3468 spin_unlock(&tiqn->tiqn_tpg_lock);
3469 end_of_buf = 1;
3470 goto eob;
3473 if (skip_bytes && len <= skip_bytes) {
3474 skip_bytes -= len;
3475 } else {
3476 memcpy(payload + payload_len, buf, len);
3477 payload_len += len;
3478 target_name_printed = 1;
3479 if (len > skip_bytes)
3480 skip_bytes = 0;
3484 if (inaddr_any)
3485 sockaddr = &conn->local_sockaddr;
3486 else
3487 sockaddr = &np->np_sockaddr;
3489 len = sprintf(buf, "TargetAddress="
3490 "%pISpc,%hu",
3491 sockaddr,
3492 tpg->tpgt);
3493 len += 1;
3495 if ((len + payload_len) > buffer_len) {
3496 spin_unlock(&tpg->tpg_np_lock);
3497 spin_unlock(&tiqn->tiqn_tpg_lock);
3498 end_of_buf = 1;
3499 goto eob;
3502 if (skip_bytes && len <= skip_bytes) {
3503 skip_bytes -= len;
3504 } else {
3505 memcpy(payload + payload_len, buf, len);
3506 payload_len += len;
3507 if (len > skip_bytes)
3508 skip_bytes = 0;
3511 spin_unlock(&tpg->tpg_np_lock);
3513 spin_unlock(&tiqn->tiqn_tpg_lock);
3514 eob:
3515 if (end_of_buf) {
3516 *completed = false;
3517 break;
3520 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
3521 break;
3523 spin_unlock(&tiqn_lock);
3525 cmd->buf_ptr = payload;
3527 return payload_len;
3531 iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3532 struct iscsi_text_rsp *hdr,
3533 enum iscsit_transport_type network_transport)
3535 int text_length, padding;
3536 bool completed = true;
3538 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3539 cmd->read_data_done,
3540 &completed);
3541 if (text_length < 0)
3542 return text_length;
3544 if (completed) {
3545 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3546 } else {
3547 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3548 cmd->read_data_done += text_length;
3549 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3550 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3552 hdr->opcode = ISCSI_OP_TEXT_RSP;
3553 padding = ((-text_length) & 3);
3554 hton24(hdr->dlength, text_length);
3555 hdr->itt = cmd->init_task_tag;
3556 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3557 cmd->stat_sn = conn->stat_sn++;
3558 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3560 iscsit_increment_maxcmdsn(cmd, conn->sess);
3562 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3563 * correctly increment MaxCmdSN for each response answering a
3564 * non immediate text request with a valid CmdSN.
3566 cmd->maxcmdsn_inc = 0;
3567 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3568 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3570 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3571 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3572 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3573 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3574 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
3576 return text_length + padding;
3578 EXPORT_SYMBOL(iscsit_build_text_rsp);
3580 static int iscsit_send_text_rsp(
3581 struct iscsi_cmd *cmd,
3582 struct iscsi_conn *conn)
3584 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
3585 struct kvec *iov;
3586 u32 tx_size = 0;
3587 int text_length, iov_count = 0, rc;
3589 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_TCP);
3590 if (rc < 0)
3591 return rc;
3593 text_length = rc;
3594 iov = &cmd->iov_misc[0];
3595 iov[iov_count].iov_base = cmd->pdu;
3596 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3597 iov[iov_count].iov_base = cmd->buf_ptr;
3598 iov[iov_count++].iov_len = text_length;
3600 tx_size += (ISCSI_HDR_LEN + text_length);
3602 if (conn->conn_ops->HeaderDigest) {
3603 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3605 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3606 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
3608 iov[0].iov_len += ISCSI_CRC_LEN;
3609 tx_size += ISCSI_CRC_LEN;
3610 pr_debug("Attaching CRC32 HeaderDigest for"
3611 " Text Response PDU 0x%08x\n", *header_digest);
3614 if (conn->conn_ops->DataDigest) {
3615 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3616 cmd->buf_ptr, text_length,
3617 0, NULL, (u8 *)&cmd->data_crc);
3619 iov[iov_count].iov_base = &cmd->data_crc;
3620 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3621 tx_size += ISCSI_CRC_LEN;
3623 pr_debug("Attaching DataDigest for %u bytes of text"
3624 " data, CRC 0x%08x\n", text_length,
3625 cmd->data_crc);
3628 cmd->iov_misc_count = iov_count;
3629 cmd->tx_size = tx_size;
3631 return 0;
3634 void
3635 iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3636 struct iscsi_reject *hdr)
3638 hdr->opcode = ISCSI_OP_REJECT;
3639 hdr->reason = cmd->reject_reason;
3640 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3641 hton24(hdr->dlength, ISCSI_HDR_LEN);
3642 hdr->ffffffff = cpu_to_be32(0xffffffff);
3643 cmd->stat_sn = conn->stat_sn++;
3644 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3645 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3646 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3649 EXPORT_SYMBOL(iscsit_build_reject);
3651 static int iscsit_send_reject(
3652 struct iscsi_cmd *cmd,
3653 struct iscsi_conn *conn)
3655 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
3656 struct kvec *iov;
3657 u32 iov_count = 0, tx_size;
3659 iscsit_build_reject(cmd, conn, hdr);
3661 iov = &cmd->iov_misc[0];
3662 iov[iov_count].iov_base = cmd->pdu;
3663 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3664 iov[iov_count].iov_base = cmd->buf_ptr;
3665 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3667 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3669 if (conn->conn_ops->HeaderDigest) {
3670 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3672 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3673 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
3675 iov[0].iov_len += ISCSI_CRC_LEN;
3676 tx_size += ISCSI_CRC_LEN;
3677 pr_debug("Attaching CRC32 HeaderDigest for"
3678 " REJECT PDU 0x%08x\n", *header_digest);
3681 if (conn->conn_ops->DataDigest) {
3682 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3683 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
3685 iov[iov_count].iov_base = &cmd->data_crc;
3686 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3687 tx_size += ISCSI_CRC_LEN;
3688 pr_debug("Attaching CRC32 DataDigest for REJECT"
3689 " PDU 0x%08x\n", cmd->data_crc);
3692 cmd->iov_misc_count = iov_count;
3693 cmd->tx_size = tx_size;
3695 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3696 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3698 return 0;
3701 void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3703 int ord, cpu;
3705 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3706 * within iscsit_start_kthreads()
3708 * Here we use bitmap_id to determine which CPU that this
3709 * iSCSI connection's RX/TX threads will be scheduled to
3710 * execute upon.
3712 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
3713 for_each_online_cpu(cpu) {
3714 if (ord-- == 0) {
3715 cpumask_set_cpu(cpu, conn->conn_cpumask);
3716 return;
3720 * This should never be reached..
3722 dump_stack();
3723 cpumask_setall(conn->conn_cpumask);
3726 static inline void iscsit_thread_check_cpumask(
3727 struct iscsi_conn *conn,
3728 struct task_struct *p,
3729 int mode)
3732 * mode == 1 signals iscsi_target_tx_thread() usage.
3733 * mode == 0 signals iscsi_target_rx_thread() usage.
3735 if (mode == 1) {
3736 if (!conn->conn_tx_reset_cpumask)
3737 return;
3738 conn->conn_tx_reset_cpumask = 0;
3739 } else {
3740 if (!conn->conn_rx_reset_cpumask)
3741 return;
3742 conn->conn_rx_reset_cpumask = 0;
3745 * Update the CPU mask for this single kthread so that
3746 * both TX and RX kthreads are scheduled to run on the
3747 * same CPU.
3749 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3752 static int
3753 iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3755 int ret;
3757 switch (state) {
3758 case ISTATE_SEND_R2T:
3759 ret = iscsit_send_r2t(cmd, conn);
3760 if (ret < 0)
3761 goto err;
3762 break;
3763 case ISTATE_REMOVE:
3764 spin_lock_bh(&conn->cmd_lock);
3765 list_del_init(&cmd->i_conn_node);
3766 spin_unlock_bh(&conn->cmd_lock);
3768 iscsit_free_cmd(cmd, false);
3769 break;
3770 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3771 iscsit_mod_nopin_response_timer(conn);
3772 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3773 if (ret < 0)
3774 goto err;
3775 break;
3776 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3777 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3778 if (ret < 0)
3779 goto err;
3780 break;
3781 default:
3782 pr_err("Unknown Opcode: 0x%02x ITT:"
3783 " 0x%08x, i_state: %d on CID: %hu\n",
3784 cmd->iscsi_opcode, cmd->init_task_tag, state,
3785 conn->cid);
3786 goto err;
3789 return 0;
3791 err:
3792 return -1;
3795 static int
3796 iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3798 struct iscsit_transport *t = conn->conn_transport;
3799 struct iscsi_queue_req *qr;
3800 struct iscsi_cmd *cmd;
3801 u8 state;
3802 int ret;
3804 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3805 atomic_set(&conn->check_immediate_queue, 0);
3806 cmd = qr->cmd;
3807 state = qr->state;
3808 kmem_cache_free(lio_qr_cache, qr);
3810 ret = t->iscsit_immediate_queue(conn, cmd, state);
3811 if (ret < 0)
3812 return ret;
3815 return 0;
3818 static int
3819 iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3821 int ret;
3823 check_rsp_state:
3824 switch (state) {
3825 case ISTATE_SEND_DATAIN:
3826 ret = iscsit_send_datain(cmd, conn);
3827 if (ret < 0)
3828 goto err;
3829 else if (!ret)
3830 /* more drs */
3831 goto check_rsp_state;
3832 else if (ret == 1) {
3833 /* all done */
3834 spin_lock_bh(&cmd->istate_lock);
3835 cmd->i_state = ISTATE_SENT_STATUS;
3836 spin_unlock_bh(&cmd->istate_lock);
3838 if (atomic_read(&conn->check_immediate_queue))
3839 return 1;
3841 return 0;
3842 } else if (ret == 2) {
3843 /* Still must send status,
3844 SCF_TRANSPORT_TASK_SENSE was set */
3845 spin_lock_bh(&cmd->istate_lock);
3846 cmd->i_state = ISTATE_SEND_STATUS;
3847 spin_unlock_bh(&cmd->istate_lock);
3848 state = ISTATE_SEND_STATUS;
3849 goto check_rsp_state;
3852 break;
3853 case ISTATE_SEND_STATUS:
3854 case ISTATE_SEND_STATUS_RECOVERY:
3855 ret = iscsit_send_response(cmd, conn);
3856 break;
3857 case ISTATE_SEND_LOGOUTRSP:
3858 ret = iscsit_send_logout(cmd, conn);
3859 break;
3860 case ISTATE_SEND_ASYNCMSG:
3861 ret = iscsit_send_conn_drop_async_message(
3862 cmd, conn);
3863 break;
3864 case ISTATE_SEND_NOPIN:
3865 ret = iscsit_send_nopin(cmd, conn);
3866 break;
3867 case ISTATE_SEND_REJECT:
3868 ret = iscsit_send_reject(cmd, conn);
3869 break;
3870 case ISTATE_SEND_TASKMGTRSP:
3871 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3872 if (ret != 0)
3873 break;
3874 ret = iscsit_tmr_post_handler(cmd, conn);
3875 if (ret != 0)
3876 iscsit_fall_back_to_erl0(conn->sess);
3877 break;
3878 case ISTATE_SEND_TEXTRSP:
3879 ret = iscsit_send_text_rsp(cmd, conn);
3880 break;
3881 default:
3882 pr_err("Unknown Opcode: 0x%02x ITT:"
3883 " 0x%08x, i_state: %d on CID: %hu\n",
3884 cmd->iscsi_opcode, cmd->init_task_tag,
3885 state, conn->cid);
3886 goto err;
3888 if (ret < 0)
3889 goto err;
3891 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3892 iscsit_tx_thread_wait_for_tcp(conn);
3893 iscsit_unmap_iovec(cmd);
3894 goto err;
3896 iscsit_unmap_iovec(cmd);
3898 switch (state) {
3899 case ISTATE_SEND_LOGOUTRSP:
3900 if (!iscsit_logout_post_handler(cmd, conn))
3901 return -ECONNRESET;
3902 /* fall through */
3903 case ISTATE_SEND_STATUS:
3904 case ISTATE_SEND_ASYNCMSG:
3905 case ISTATE_SEND_NOPIN:
3906 case ISTATE_SEND_STATUS_RECOVERY:
3907 case ISTATE_SEND_TEXTRSP:
3908 case ISTATE_SEND_TASKMGTRSP:
3909 case ISTATE_SEND_REJECT:
3910 spin_lock_bh(&cmd->istate_lock);
3911 cmd->i_state = ISTATE_SENT_STATUS;
3912 spin_unlock_bh(&cmd->istate_lock);
3913 break;
3914 default:
3915 pr_err("Unknown Opcode: 0x%02x ITT:"
3916 " 0x%08x, i_state: %d on CID: %hu\n",
3917 cmd->iscsi_opcode, cmd->init_task_tag,
3918 cmd->i_state, conn->cid);
3919 goto err;
3922 if (atomic_read(&conn->check_immediate_queue))
3923 return 1;
3925 return 0;
3927 err:
3928 return -1;
3931 static int iscsit_handle_response_queue(struct iscsi_conn *conn)
3933 struct iscsit_transport *t = conn->conn_transport;
3934 struct iscsi_queue_req *qr;
3935 struct iscsi_cmd *cmd;
3936 u8 state;
3937 int ret;
3939 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3940 cmd = qr->cmd;
3941 state = qr->state;
3942 kmem_cache_free(lio_qr_cache, qr);
3944 ret = t->iscsit_response_queue(conn, cmd, state);
3945 if (ret == 1 || ret < 0)
3946 return ret;
3949 return 0;
3952 int iscsi_target_tx_thread(void *arg)
3954 int ret = 0;
3955 struct iscsi_conn *conn = arg;
3957 * Allow ourselves to be interrupted by SIGINT so that a
3958 * connection recovery / failure event can be triggered externally.
3960 allow_signal(SIGINT);
3962 while (!kthread_should_stop()) {
3964 * Ensure that both TX and RX per connection kthreads
3965 * are scheduled to run on the same CPU.
3967 iscsit_thread_check_cpumask(conn, current, 1);
3969 wait_event_interruptible(conn->queues_wq,
3970 !iscsit_conn_all_queues_empty(conn));
3972 if (signal_pending(current))
3973 goto transport_err;
3975 get_immediate:
3976 ret = iscsit_handle_immediate_queue(conn);
3977 if (ret < 0)
3978 goto transport_err;
3980 ret = iscsit_handle_response_queue(conn);
3981 if (ret == 1)
3982 goto get_immediate;
3983 else if (ret == -ECONNRESET)
3984 goto out;
3985 else if (ret < 0)
3986 goto transport_err;
3989 transport_err:
3991 * Avoid the normal connection failure code-path if this connection
3992 * is still within LOGIN mode, and iscsi_np process context is
3993 * responsible for cleaning up the early connection failure.
3995 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
3996 iscsit_take_action_for_connection_exit(conn);
3997 out:
3998 return 0;
4001 static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
4003 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
4004 struct iscsi_cmd *cmd;
4005 int ret = 0;
4007 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
4008 case ISCSI_OP_SCSI_CMD:
4009 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4010 if (!cmd)
4011 goto reject;
4013 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
4014 break;
4015 case ISCSI_OP_SCSI_DATA_OUT:
4016 ret = iscsit_handle_data_out(conn, buf);
4017 break;
4018 case ISCSI_OP_NOOP_OUT:
4019 cmd = NULL;
4020 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
4021 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4022 if (!cmd)
4023 goto reject;
4025 ret = iscsit_handle_nop_out(conn, cmd, buf);
4026 break;
4027 case ISCSI_OP_SCSI_TMFUNC:
4028 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4029 if (!cmd)
4030 goto reject;
4032 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
4033 break;
4034 case ISCSI_OP_TEXT:
4035 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
4036 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
4037 if (!cmd)
4038 goto reject;
4039 } else {
4040 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4041 if (!cmd)
4042 goto reject;
4045 ret = iscsit_handle_text_cmd(conn, cmd, buf);
4046 break;
4047 case ISCSI_OP_LOGOUT:
4048 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4049 if (!cmd)
4050 goto reject;
4052 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4053 if (ret > 0)
4054 wait_for_completion_timeout(&conn->conn_logout_comp,
4055 SECONDS_FOR_LOGOUT_COMP * HZ);
4056 break;
4057 case ISCSI_OP_SNACK:
4058 ret = iscsit_handle_snack(conn, buf);
4059 break;
4060 default:
4061 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4062 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4063 pr_err("Cannot recover from unknown"
4064 " opcode while ERL=0, closing iSCSI connection.\n");
4065 return -1;
4067 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
4068 " closing iSCSI connection.\n");
4069 ret = -1;
4070 break;
4073 return ret;
4074 reject:
4075 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
4078 static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
4080 bool ret;
4082 spin_lock_bh(&conn->state_lock);
4083 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
4084 spin_unlock_bh(&conn->state_lock);
4086 return ret;
4089 int iscsi_target_rx_thread(void *arg)
4091 int ret, rc;
4092 u8 buffer[ISCSI_HDR_LEN], opcode;
4093 u32 checksum = 0, digest = 0;
4094 struct iscsi_conn *conn = arg;
4095 struct kvec iov;
4097 * Allow ourselves to be interrupted by SIGINT so that a
4098 * connection recovery / failure event can be triggered externally.
4100 allow_signal(SIGINT);
4102 * Wait for iscsi_post_login_handler() to complete before allowing
4103 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4105 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4106 if (rc < 0 || iscsi_target_check_conn_state(conn))
4107 return 0;
4109 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4110 struct completion comp;
4112 init_completion(&comp);
4113 rc = wait_for_completion_interruptible(&comp);
4114 if (rc < 0)
4115 goto transport_err;
4117 goto transport_err;
4120 while (!kthread_should_stop()) {
4122 * Ensure that both TX and RX per connection kthreads
4123 * are scheduled to run on the same CPU.
4125 iscsit_thread_check_cpumask(conn, current, 0);
4127 memset(buffer, 0, ISCSI_HDR_LEN);
4128 memset(&iov, 0, sizeof(struct kvec));
4130 iov.iov_base = buffer;
4131 iov.iov_len = ISCSI_HDR_LEN;
4133 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4134 if (ret != ISCSI_HDR_LEN) {
4135 iscsit_rx_thread_wait_for_tcp(conn);
4136 goto transport_err;
4139 if (conn->conn_ops->HeaderDigest) {
4140 iov.iov_base = &digest;
4141 iov.iov_len = ISCSI_CRC_LEN;
4143 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4144 if (ret != ISCSI_CRC_LEN) {
4145 iscsit_rx_thread_wait_for_tcp(conn);
4146 goto transport_err;
4149 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4150 buffer, ISCSI_HDR_LEN,
4151 0, NULL, (u8 *)&checksum);
4153 if (digest != checksum) {
4154 pr_err("HeaderDigest CRC32C failed,"
4155 " received 0x%08x, computed 0x%08x\n",
4156 digest, checksum);
4158 * Set the PDU to 0xff so it will intentionally
4159 * hit default in the switch below.
4161 memset(buffer, 0xff, ISCSI_HDR_LEN);
4162 atomic_long_inc(&conn->sess->conn_digest_errors);
4163 } else {
4164 pr_debug("Got HeaderDigest CRC32C"
4165 " 0x%08x\n", checksum);
4169 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4170 goto transport_err;
4172 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4174 if (conn->sess->sess_ops->SessionType &&
4175 ((!(opcode & ISCSI_OP_TEXT)) ||
4176 (!(opcode & ISCSI_OP_LOGOUT)))) {
4177 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4178 " while in Discovery Session, rejecting.\n", opcode);
4179 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4180 buffer);
4181 goto transport_err;
4184 ret = iscsi_target_rx_opcode(conn, buffer);
4185 if (ret < 0)
4186 goto transport_err;
4189 transport_err:
4190 if (!signal_pending(current))
4191 atomic_set(&conn->transport_failed, 1);
4192 iscsit_take_action_for_connection_exit(conn);
4193 return 0;
4196 static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4198 LIST_HEAD(tmp_list);
4199 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4200 struct iscsi_session *sess = conn->sess;
4202 * We expect this function to only ever be called from either RX or TX
4203 * thread context via iscsit_close_connection() once the other context
4204 * has been reset -> returned sleeping pre-handler state.
4206 spin_lock_bh(&conn->cmd_lock);
4207 list_splice_init(&conn->conn_cmd_list, &tmp_list);
4209 list_for_each_entry(cmd, &tmp_list, i_conn_node) {
4210 struct se_cmd *se_cmd = &cmd->se_cmd;
4212 if (se_cmd->se_tfo != NULL) {
4213 spin_lock(&se_cmd->t_state_lock);
4214 se_cmd->transport_state |= CMD_T_FABRIC_STOP;
4215 spin_unlock(&se_cmd->t_state_lock);
4218 spin_unlock_bh(&conn->cmd_lock);
4220 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4221 list_del_init(&cmd->i_conn_node);
4223 iscsit_increment_maxcmdsn(cmd, sess);
4224 iscsit_free_cmd(cmd, true);
4229 static void iscsit_stop_timers_for_cmds(
4230 struct iscsi_conn *conn)
4232 struct iscsi_cmd *cmd;
4234 spin_lock_bh(&conn->cmd_lock);
4235 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
4236 if (cmd->data_direction == DMA_TO_DEVICE)
4237 iscsit_stop_dataout_timer(cmd);
4239 spin_unlock_bh(&conn->cmd_lock);
4242 int iscsit_close_connection(
4243 struct iscsi_conn *conn)
4245 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4246 struct iscsi_session *sess = conn->sess;
4248 pr_debug("Closing iSCSI connection CID %hu on SID:"
4249 " %u\n", conn->cid, sess->sid);
4251 * Always up conn_logout_comp for the traditional TCP case just in case
4252 * the RX Thread in iscsi_target_rx_opcode() is sleeping and the logout
4253 * response never got sent because the connection failed.
4255 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4256 * to signal logout response TX interrupt completion. Go ahead and skip
4257 * this for iser since isert_rx_opcode() does not wait on logout failure,
4258 * and to avoid iscsi_conn pointer dereference in iser-target code.
4260 if (conn->conn_transport->transport_type == ISCSI_TCP)
4261 complete(&conn->conn_logout_comp);
4263 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4264 if (conn->tx_thread &&
4265 cmpxchg(&conn->tx_thread_active, true, false)) {
4266 send_sig(SIGINT, conn->tx_thread, 1);
4267 kthread_stop(conn->tx_thread);
4269 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4270 if (conn->rx_thread &&
4271 cmpxchg(&conn->rx_thread_active, true, false)) {
4272 send_sig(SIGINT, conn->rx_thread, 1);
4273 kthread_stop(conn->rx_thread);
4277 spin_lock(&iscsit_global->ts_bitmap_lock);
4278 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4279 get_order(1));
4280 spin_unlock(&iscsit_global->ts_bitmap_lock);
4282 iscsit_stop_timers_for_cmds(conn);
4283 iscsit_stop_nopin_response_timer(conn);
4284 iscsit_stop_nopin_timer(conn);
4286 if (conn->conn_transport->iscsit_wait_conn)
4287 conn->conn_transport->iscsit_wait_conn(conn);
4290 * During Connection recovery drop unacknowledged out of order
4291 * commands for this connection, and prepare the other commands
4292 * for realligence.
4294 * During normal operation clear the out of order commands (but
4295 * do not free the struct iscsi_ooo_cmdsn's) and release all
4296 * struct iscsi_cmds.
4298 if (atomic_read(&conn->connection_recovery)) {
4299 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4300 iscsit_prepare_cmds_for_realligance(conn);
4301 } else {
4302 iscsit_clear_ooo_cmdsns_for_conn(conn);
4303 iscsit_release_commands_from_conn(conn);
4305 iscsit_free_queue_reqs_for_conn(conn);
4308 * Handle decrementing session or connection usage count if
4309 * a logout response was not able to be sent because the
4310 * connection failed. Fall back to Session Recovery here.
4312 if (atomic_read(&conn->conn_logout_remove)) {
4313 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4314 iscsit_dec_conn_usage_count(conn);
4315 iscsit_dec_session_usage_count(sess);
4317 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4318 iscsit_dec_conn_usage_count(conn);
4320 atomic_set(&conn->conn_logout_remove, 0);
4321 atomic_set(&sess->session_reinstatement, 0);
4322 atomic_set(&sess->session_fall_back_to_erl0, 1);
4325 spin_lock_bh(&sess->conn_lock);
4326 list_del(&conn->conn_list);
4329 * Attempt to let the Initiator know this connection failed by
4330 * sending an Connection Dropped Async Message on another
4331 * active connection.
4333 if (atomic_read(&conn->connection_recovery))
4334 iscsit_build_conn_drop_async_message(conn);
4336 spin_unlock_bh(&sess->conn_lock);
4339 * If connection reinstatement is being performed on this connection,
4340 * up the connection reinstatement semaphore that is being blocked on
4341 * in iscsit_cause_connection_reinstatement().
4343 spin_lock_bh(&conn->state_lock);
4344 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4345 spin_unlock_bh(&conn->state_lock);
4346 complete(&conn->conn_wait_comp);
4347 wait_for_completion(&conn->conn_post_wait_comp);
4348 spin_lock_bh(&conn->state_lock);
4352 * If connection reinstatement is being performed on this connection
4353 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4354 * connection wait rcfr semaphore that is being blocked on
4355 * an iscsit_connection_reinstatement_rcfr().
4357 if (atomic_read(&conn->connection_wait_rcfr)) {
4358 spin_unlock_bh(&conn->state_lock);
4359 complete(&conn->conn_wait_rcfr_comp);
4360 wait_for_completion(&conn->conn_post_wait_comp);
4361 spin_lock_bh(&conn->state_lock);
4363 atomic_set(&conn->connection_reinstatement, 1);
4364 spin_unlock_bh(&conn->state_lock);
4367 * If any other processes are accessing this connection pointer we
4368 * must wait until they have completed.
4370 iscsit_check_conn_usage_count(conn);
4372 if (conn->conn_rx_hash.tfm)
4373 crypto_free_hash(conn->conn_rx_hash.tfm);
4374 if (conn->conn_tx_hash.tfm)
4375 crypto_free_hash(conn->conn_tx_hash.tfm);
4377 free_cpumask_var(conn->conn_cpumask);
4379 kfree(conn->conn_ops);
4380 conn->conn_ops = NULL;
4382 if (conn->sock)
4383 sock_release(conn->sock);
4385 if (conn->conn_transport->iscsit_free_conn)
4386 conn->conn_transport->iscsit_free_conn(conn);
4388 iscsit_put_transport(conn->conn_transport);
4390 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4391 conn->conn_state = TARG_CONN_STATE_FREE;
4392 kfree(conn);
4394 spin_lock_bh(&sess->conn_lock);
4395 atomic_dec(&sess->nconn);
4396 pr_debug("Decremented iSCSI connection count to %hu from node:"
4397 " %s\n", atomic_read(&sess->nconn),
4398 sess->sess_ops->InitiatorName);
4400 * Make sure that if one connection fails in an non ERL=2 iSCSI
4401 * Session that they all fail.
4403 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4404 !atomic_read(&sess->session_logout))
4405 atomic_set(&sess->session_fall_back_to_erl0, 1);
4408 * If this was not the last connection in the session, and we are
4409 * performing session reinstatement or falling back to ERL=0, call
4410 * iscsit_stop_session() without sleeping to shutdown the other
4411 * active connections.
4413 if (atomic_read(&sess->nconn)) {
4414 if (!atomic_read(&sess->session_reinstatement) &&
4415 !atomic_read(&sess->session_fall_back_to_erl0)) {
4416 spin_unlock_bh(&sess->conn_lock);
4417 return 0;
4419 if (!atomic_read(&sess->session_stop_active)) {
4420 atomic_set(&sess->session_stop_active, 1);
4421 spin_unlock_bh(&sess->conn_lock);
4422 iscsit_stop_session(sess, 0, 0);
4423 return 0;
4425 spin_unlock_bh(&sess->conn_lock);
4426 return 0;
4430 * If this was the last connection in the session and one of the
4431 * following is occurring:
4433 * Session Reinstatement is not being performed, and are falling back
4434 * to ERL=0 call iscsit_close_session().
4436 * Session Logout was requested. iscsit_close_session() will be called
4437 * elsewhere.
4439 * Session Continuation is not being performed, start the Time2Retain
4440 * handler and check if sleep_on_sess_wait_sem is active.
4442 if (!atomic_read(&sess->session_reinstatement) &&
4443 atomic_read(&sess->session_fall_back_to_erl0)) {
4444 spin_unlock_bh(&sess->conn_lock);
4445 target_put_session(sess->se_sess);
4447 return 0;
4448 } else if (atomic_read(&sess->session_logout)) {
4449 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4450 sess->session_state = TARG_SESS_STATE_FREE;
4451 spin_unlock_bh(&sess->conn_lock);
4453 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4454 complete(&sess->session_wait_comp);
4456 return 0;
4457 } else {
4458 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4459 sess->session_state = TARG_SESS_STATE_FAILED;
4461 if (!atomic_read(&sess->session_continuation)) {
4462 spin_unlock_bh(&sess->conn_lock);
4463 iscsit_start_time2retain_handler(sess);
4464 } else
4465 spin_unlock_bh(&sess->conn_lock);
4467 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4468 complete(&sess->session_wait_comp);
4470 return 0;
4472 spin_unlock_bh(&sess->conn_lock);
4474 return 0;
4477 int iscsit_close_session(struct iscsi_session *sess)
4479 struct iscsi_portal_group *tpg = sess->tpg;
4480 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4482 if (atomic_read(&sess->nconn)) {
4483 pr_err("%d connection(s) still exist for iSCSI session"
4484 " to %s\n", atomic_read(&sess->nconn),
4485 sess->sess_ops->InitiatorName);
4486 BUG();
4489 spin_lock_bh(&se_tpg->session_lock);
4490 atomic_set(&sess->session_logout, 1);
4491 atomic_set(&sess->session_reinstatement, 1);
4492 iscsit_stop_time2retain_timer(sess);
4493 spin_unlock_bh(&se_tpg->session_lock);
4496 * transport_deregister_session_configfs() will clear the
4497 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4498 * can be setting it again with __transport_register_session() in
4499 * iscsi_post_login_handler() again after the iscsit_stop_session()
4500 * completes in iscsi_np context.
4502 transport_deregister_session_configfs(sess->se_sess);
4505 * If any other processes are accessing this session pointer we must
4506 * wait until they have completed. If we are in an interrupt (the
4507 * time2retain handler) and contain and active session usage count we
4508 * restart the timer and exit.
4510 if (!in_interrupt()) {
4511 if (iscsit_check_session_usage_count(sess) == 1)
4512 iscsit_stop_session(sess, 1, 1);
4513 } else {
4514 if (iscsit_check_session_usage_count(sess) == 2) {
4515 atomic_set(&sess->session_logout, 0);
4516 iscsit_start_time2retain_handler(sess);
4517 return 0;
4521 transport_deregister_session(sess->se_sess);
4523 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4524 iscsit_free_connection_recovery_entires(sess);
4526 iscsit_free_all_ooo_cmdsns(sess);
4528 spin_lock_bh(&se_tpg->session_lock);
4529 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4530 sess->session_state = TARG_SESS_STATE_FREE;
4531 pr_debug("Released iSCSI session from node: %s\n",
4532 sess->sess_ops->InitiatorName);
4533 tpg->nsessions--;
4534 if (tpg->tpg_tiqn)
4535 tpg->tpg_tiqn->tiqn_nsessions--;
4537 pr_debug("Decremented number of active iSCSI Sessions on"
4538 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4540 spin_lock(&sess_idr_lock);
4541 idr_remove(&sess_idr, sess->session_index);
4542 spin_unlock(&sess_idr_lock);
4544 kfree(sess->sess_ops);
4545 sess->sess_ops = NULL;
4546 spin_unlock_bh(&se_tpg->session_lock);
4548 kfree(sess);
4549 return 0;
4552 static void iscsit_logout_post_handler_closesession(
4553 struct iscsi_conn *conn)
4555 struct iscsi_session *sess = conn->sess;
4556 int sleep = 1;
4558 * Traditional iscsi/tcp will invoke this logic from TX thread
4559 * context during session logout, so clear tx_thread_active and
4560 * sleep if iscsit_close_connection() has not already occured.
4562 * Since iser-target invokes this logic from it's own workqueue,
4563 * always sleep waiting for RX/TX thread shutdown to complete
4564 * within iscsit_close_connection().
4566 if (conn->conn_transport->transport_type == ISCSI_TCP)
4567 sleep = cmpxchg(&conn->tx_thread_active, true, false);
4569 atomic_set(&conn->conn_logout_remove, 0);
4570 complete(&conn->conn_logout_comp);
4572 iscsit_dec_conn_usage_count(conn);
4573 iscsit_stop_session(sess, sleep, sleep);
4574 iscsit_dec_session_usage_count(sess);
4575 target_put_session(sess->se_sess);
4578 static void iscsit_logout_post_handler_samecid(
4579 struct iscsi_conn *conn)
4581 int sleep = 1;
4583 if (conn->conn_transport->transport_type == ISCSI_TCP)
4584 sleep = cmpxchg(&conn->tx_thread_active, true, false);
4586 atomic_set(&conn->conn_logout_remove, 0);
4587 complete(&conn->conn_logout_comp);
4589 iscsit_cause_connection_reinstatement(conn, sleep);
4590 iscsit_dec_conn_usage_count(conn);
4593 static void iscsit_logout_post_handler_diffcid(
4594 struct iscsi_conn *conn,
4595 u16 cid)
4597 struct iscsi_conn *l_conn;
4598 struct iscsi_session *sess = conn->sess;
4599 bool conn_found = false;
4601 if (!sess)
4602 return;
4604 spin_lock_bh(&sess->conn_lock);
4605 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4606 if (l_conn->cid == cid) {
4607 iscsit_inc_conn_usage_count(l_conn);
4608 conn_found = true;
4609 break;
4612 spin_unlock_bh(&sess->conn_lock);
4614 if (!conn_found)
4615 return;
4617 if (l_conn->sock)
4618 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4620 spin_lock_bh(&l_conn->state_lock);
4621 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4622 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4623 spin_unlock_bh(&l_conn->state_lock);
4625 iscsit_cause_connection_reinstatement(l_conn, 1);
4626 iscsit_dec_conn_usage_count(l_conn);
4630 * Return of 0 causes the TX thread to restart.
4632 int iscsit_logout_post_handler(
4633 struct iscsi_cmd *cmd,
4634 struct iscsi_conn *conn)
4636 int ret = 0;
4638 switch (cmd->logout_reason) {
4639 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4640 switch (cmd->logout_response) {
4641 case ISCSI_LOGOUT_SUCCESS:
4642 case ISCSI_LOGOUT_CLEANUP_FAILED:
4643 default:
4644 iscsit_logout_post_handler_closesession(conn);
4645 break;
4647 ret = 0;
4648 break;
4649 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4650 if (conn->cid == cmd->logout_cid) {
4651 switch (cmd->logout_response) {
4652 case ISCSI_LOGOUT_SUCCESS:
4653 case ISCSI_LOGOUT_CLEANUP_FAILED:
4654 default:
4655 iscsit_logout_post_handler_samecid(conn);
4656 break;
4658 ret = 0;
4659 } else {
4660 switch (cmd->logout_response) {
4661 case ISCSI_LOGOUT_SUCCESS:
4662 iscsit_logout_post_handler_diffcid(conn,
4663 cmd->logout_cid);
4664 break;
4665 case ISCSI_LOGOUT_CID_NOT_FOUND:
4666 case ISCSI_LOGOUT_CLEANUP_FAILED:
4667 default:
4668 break;
4670 ret = 1;
4672 break;
4673 case ISCSI_LOGOUT_REASON_RECOVERY:
4674 switch (cmd->logout_response) {
4675 case ISCSI_LOGOUT_SUCCESS:
4676 case ISCSI_LOGOUT_CID_NOT_FOUND:
4677 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4678 case ISCSI_LOGOUT_CLEANUP_FAILED:
4679 default:
4680 break;
4682 ret = 1;
4683 break;
4684 default:
4685 break;
4688 return ret;
4690 EXPORT_SYMBOL(iscsit_logout_post_handler);
4692 void iscsit_fail_session(struct iscsi_session *sess)
4694 struct iscsi_conn *conn;
4696 spin_lock_bh(&sess->conn_lock);
4697 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4698 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4699 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4701 spin_unlock_bh(&sess->conn_lock);
4703 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4704 sess->session_state = TARG_SESS_STATE_FAILED;
4707 int iscsit_free_session(struct iscsi_session *sess)
4709 u16 conn_count = atomic_read(&sess->nconn);
4710 struct iscsi_conn *conn, *conn_tmp = NULL;
4711 int is_last;
4713 spin_lock_bh(&sess->conn_lock);
4714 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4716 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4717 conn_list) {
4718 if (conn_count == 0)
4719 break;
4721 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4722 is_last = 1;
4723 } else {
4724 iscsit_inc_conn_usage_count(conn_tmp);
4725 is_last = 0;
4727 iscsit_inc_conn_usage_count(conn);
4729 spin_unlock_bh(&sess->conn_lock);
4730 iscsit_cause_connection_reinstatement(conn, 1);
4731 spin_lock_bh(&sess->conn_lock);
4733 iscsit_dec_conn_usage_count(conn);
4734 if (is_last == 0)
4735 iscsit_dec_conn_usage_count(conn_tmp);
4737 conn_count--;
4740 if (atomic_read(&sess->nconn)) {
4741 spin_unlock_bh(&sess->conn_lock);
4742 wait_for_completion(&sess->session_wait_comp);
4743 } else
4744 spin_unlock_bh(&sess->conn_lock);
4746 target_put_session(sess->se_sess);
4747 return 0;
4750 void iscsit_stop_session(
4751 struct iscsi_session *sess,
4752 int session_sleep,
4753 int connection_sleep)
4755 u16 conn_count = atomic_read(&sess->nconn);
4756 struct iscsi_conn *conn, *conn_tmp = NULL;
4757 int is_last;
4759 spin_lock_bh(&sess->conn_lock);
4760 if (session_sleep)
4761 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4763 if (connection_sleep) {
4764 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4765 conn_list) {
4766 if (conn_count == 0)
4767 break;
4769 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4770 is_last = 1;
4771 } else {
4772 iscsit_inc_conn_usage_count(conn_tmp);
4773 is_last = 0;
4775 iscsit_inc_conn_usage_count(conn);
4777 spin_unlock_bh(&sess->conn_lock);
4778 iscsit_cause_connection_reinstatement(conn, 1);
4779 spin_lock_bh(&sess->conn_lock);
4781 iscsit_dec_conn_usage_count(conn);
4782 if (is_last == 0)
4783 iscsit_dec_conn_usage_count(conn_tmp);
4784 conn_count--;
4786 } else {
4787 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4788 iscsit_cause_connection_reinstatement(conn, 0);
4791 if (session_sleep && atomic_read(&sess->nconn)) {
4792 spin_unlock_bh(&sess->conn_lock);
4793 wait_for_completion(&sess->session_wait_comp);
4794 } else
4795 spin_unlock_bh(&sess->conn_lock);
4798 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4800 struct iscsi_session *sess;
4801 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4802 struct se_session *se_sess, *se_sess_tmp;
4803 LIST_HEAD(free_list);
4804 int session_count = 0;
4806 spin_lock_bh(&se_tpg->session_lock);
4807 if (tpg->nsessions && !force) {
4808 spin_unlock_bh(&se_tpg->session_lock);
4809 return -1;
4812 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4813 sess_list) {
4814 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4816 spin_lock(&sess->conn_lock);
4817 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4818 atomic_read(&sess->session_logout) ||
4819 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4820 spin_unlock(&sess->conn_lock);
4821 continue;
4823 atomic_set(&sess->session_reinstatement, 1);
4824 spin_unlock(&sess->conn_lock);
4826 list_move_tail(&se_sess->sess_list, &free_list);
4828 spin_unlock_bh(&se_tpg->session_lock);
4830 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4831 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4833 iscsit_free_session(sess);
4834 session_count++;
4837 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4838 " Group: %hu\n", session_count, tpg->tpgt);
4839 return 0;
4842 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4843 MODULE_VERSION("4.1.x");
4844 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4845 MODULE_LICENSE("GPL");
4847 module_init(iscsi_target_init_module);
4848 module_exit(iscsi_target_cleanup_module);