gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target.c
blob6fad3e9fd389ac9370b905896c807b4ae80ecaa7
1 /*******************************************************************************
2 * This file contains main functions related to the iSCSI Target Core Driver.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
19 #include <linux/string.h>
20 #include <linux/kthread.h>
21 #include <linux/crypto.h>
22 #include <linux/completion.h>
23 #include <linux/module.h>
24 #include <linux/idr.h>
25 #include <asm/unaligned.h>
26 #include <scsi/scsi_device.h>
27 #include <scsi/iscsi_proto.h>
28 #include <scsi/scsi_tcq.h>
29 #include <target/target_core_base.h>
30 #include <target/target_core_fabric.h>
31 #include <target/target_core_configfs.h>
33 #include <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 __kernel_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;
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 } else {
292 sock_in = (struct sockaddr_in *)sockaddr;
293 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
295 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
296 ip_match = true;
298 port = ntohs(sock_in->sin_port);
301 if (ip_match && (np->np_port == port) &&
302 (np->np_network_transport == network_transport))
303 return true;
305 return false;
309 * Called with mutex np_lock held
311 static struct iscsi_np *iscsit_get_np(
312 struct __kernel_sockaddr_storage *sockaddr,
313 int network_transport)
315 struct iscsi_np *np;
316 bool match;
318 list_for_each_entry(np, &g_np_list, np_list) {
319 spin_lock_bh(&np->np_thread_lock);
320 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
321 spin_unlock_bh(&np->np_thread_lock);
322 continue;
325 match = iscsit_check_np_match(sockaddr, np, network_transport);
326 if (match) {
328 * Increment the np_exports reference count now to
329 * prevent iscsit_del_np() below from being called
330 * while iscsi_tpg_add_network_portal() is called.
332 np->np_exports++;
333 spin_unlock_bh(&np->np_thread_lock);
334 return np;
336 spin_unlock_bh(&np->np_thread_lock);
339 return NULL;
342 struct iscsi_np *iscsit_add_np(
343 struct __kernel_sockaddr_storage *sockaddr,
344 int network_transport)
346 struct sockaddr_in *sock_in;
347 struct sockaddr_in6 *sock_in6;
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 if (sockaddr->ss_family == AF_INET6) {
371 sock_in6 = (struct sockaddr_in6 *)sockaddr;
372 np->np_port = ntohs(sock_in6->sin6_port);
373 } else {
374 sock_in = (struct sockaddr_in *)sockaddr;
375 np->np_port = ntohs(sock_in->sin_port);
378 np->np_network_transport = network_transport;
379 spin_lock_init(&np->np_thread_lock);
380 init_completion(&np->np_restart_comp);
381 INIT_LIST_HEAD(&np->np_list);
383 ret = iscsi_target_setup_login_socket(np, sockaddr);
384 if (ret != 0) {
385 kfree(np);
386 mutex_unlock(&np_lock);
387 return ERR_PTR(ret);
390 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
391 if (IS_ERR(np->np_thread)) {
392 pr_err("Unable to create kthread: iscsi_np\n");
393 ret = PTR_ERR(np->np_thread);
394 kfree(np);
395 mutex_unlock(&np_lock);
396 return ERR_PTR(ret);
399 * Increment the np_exports reference count now to prevent
400 * iscsit_del_np() below from being run while a new call to
401 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
402 * active. We don't need to hold np->np_thread_lock at this
403 * point because iscsi_np has not been added to g_np_list yet.
405 np->np_exports = 1;
406 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
408 list_add_tail(&np->np_list, &g_np_list);
409 mutex_unlock(&np_lock);
411 pr_debug("CORE[0] - Added Network Portal: %pISc:%hu on %s\n",
412 &np->np_sockaddr, np->np_port, np->np_transport->name);
414 return np;
417 int iscsit_reset_np_thread(
418 struct iscsi_np *np,
419 struct iscsi_tpg_np *tpg_np,
420 struct iscsi_portal_group *tpg,
421 bool shutdown)
423 spin_lock_bh(&np->np_thread_lock);
424 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
425 spin_unlock_bh(&np->np_thread_lock);
426 return 0;
428 np->np_thread_state = ISCSI_NP_THREAD_RESET;
430 if (np->np_thread) {
431 spin_unlock_bh(&np->np_thread_lock);
432 send_sig(SIGINT, np->np_thread, 1);
433 wait_for_completion(&np->np_restart_comp);
434 spin_lock_bh(&np->np_thread_lock);
436 spin_unlock_bh(&np->np_thread_lock);
438 if (tpg_np && shutdown) {
439 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
441 wait_for_completion(&tpg_np->tpg_np_comp);
444 return 0;
447 static void iscsit_free_np(struct iscsi_np *np)
449 if (np->np_socket)
450 sock_release(np->np_socket);
453 int iscsit_del_np(struct iscsi_np *np)
455 spin_lock_bh(&np->np_thread_lock);
456 np->np_exports--;
457 if (np->np_exports) {
458 np->enabled = true;
459 spin_unlock_bh(&np->np_thread_lock);
460 return 0;
462 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
463 spin_unlock_bh(&np->np_thread_lock);
465 if (np->np_thread) {
467 * We need to send the signal to wakeup Linux/Net
468 * which may be sleeping in sock_accept()..
470 send_sig(SIGINT, np->np_thread, 1);
471 kthread_stop(np->np_thread);
472 np->np_thread = NULL;
475 np->np_transport->iscsit_free_np(np);
477 mutex_lock(&np_lock);
478 list_del(&np->np_list);
479 mutex_unlock(&np_lock);
481 pr_debug("CORE[0] - Removed Network Portal: %pISc:%hu on %s\n",
482 &np->np_sockaddr, np->np_port, np->np_transport->name);
484 iscsit_put_transport(np->np_transport);
485 kfree(np);
486 return 0;
489 static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
490 static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
492 static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
494 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
495 return 0;
498 static void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
500 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD);
502 spin_lock_bh(&conn->cmd_lock);
503 if (!list_empty(&cmd->i_conn_node) &&
504 !(cmd->se_cmd.transport_state & CMD_T_FABRIC_STOP))
505 list_del_init(&cmd->i_conn_node);
506 spin_unlock_bh(&conn->cmd_lock);
508 __iscsit_free_cmd(cmd, scsi_cmd, true);
511 static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
513 return TARGET_PROT_NORMAL;
516 static struct iscsit_transport iscsi_target_transport = {
517 .name = "iSCSI/TCP",
518 .transport_type = ISCSI_TCP,
519 .owner = NULL,
520 .iscsit_setup_np = iscsit_setup_np,
521 .iscsit_accept_np = iscsit_accept_np,
522 .iscsit_free_np = iscsit_free_np,
523 .iscsit_get_login_rx = iscsit_get_login_rx,
524 .iscsit_put_login_tx = iscsit_put_login_tx,
525 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
526 .iscsit_immediate_queue = iscsit_immediate_queue,
527 .iscsit_response_queue = iscsit_response_queue,
528 .iscsit_queue_data_in = iscsit_queue_rsp,
529 .iscsit_queue_status = iscsit_queue_rsp,
530 .iscsit_aborted_task = iscsit_aborted_task,
531 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
534 static int __init iscsi_target_init_module(void)
536 int ret = 0, size;
538 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
540 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
541 if (!iscsit_global) {
542 pr_err("Unable to allocate memory for iscsit_global\n");
543 return -1;
545 spin_lock_init(&iscsit_global->ts_bitmap_lock);
546 mutex_init(&auth_id_lock);
547 spin_lock_init(&sess_idr_lock);
548 idr_init(&tiqn_idr);
549 idr_init(&sess_idr);
551 ret = target_register_template(&iscsi_ops);
552 if (ret)
553 goto out;
555 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
556 iscsit_global->ts_bitmap = vzalloc(size);
557 if (!iscsit_global->ts_bitmap) {
558 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
559 goto configfs_out;
562 lio_qr_cache = kmem_cache_create("lio_qr_cache",
563 sizeof(struct iscsi_queue_req),
564 __alignof__(struct iscsi_queue_req), 0, NULL);
565 if (!lio_qr_cache) {
566 pr_err("nable to kmem_cache_create() for"
567 " lio_qr_cache\n");
568 goto bitmap_out;
571 lio_dr_cache = kmem_cache_create("lio_dr_cache",
572 sizeof(struct iscsi_datain_req),
573 __alignof__(struct iscsi_datain_req), 0, NULL);
574 if (!lio_dr_cache) {
575 pr_err("Unable to kmem_cache_create() for"
576 " lio_dr_cache\n");
577 goto qr_out;
580 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
581 sizeof(struct iscsi_ooo_cmdsn),
582 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
583 if (!lio_ooo_cache) {
584 pr_err("Unable to kmem_cache_create() for"
585 " lio_ooo_cache\n");
586 goto dr_out;
589 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
590 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
591 0, NULL);
592 if (!lio_r2t_cache) {
593 pr_err("Unable to kmem_cache_create() for"
594 " lio_r2t_cache\n");
595 goto ooo_out;
598 iscsit_register_transport(&iscsi_target_transport);
600 if (iscsit_load_discovery_tpg() < 0)
601 goto r2t_out;
603 return ret;
604 r2t_out:
605 iscsit_unregister_transport(&iscsi_target_transport);
606 kmem_cache_destroy(lio_r2t_cache);
607 ooo_out:
608 kmem_cache_destroy(lio_ooo_cache);
609 dr_out:
610 kmem_cache_destroy(lio_dr_cache);
611 qr_out:
612 kmem_cache_destroy(lio_qr_cache);
613 bitmap_out:
614 vfree(iscsit_global->ts_bitmap);
615 configfs_out:
616 /* XXX: this probably wants it to be it's own unwind step.. */
617 if (iscsit_global->discovery_tpg)
618 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
619 target_unregister_template(&iscsi_ops);
620 out:
621 kfree(iscsit_global);
622 return -ENOMEM;
625 static void __exit iscsi_target_cleanup_module(void)
627 iscsit_release_discovery_tpg();
628 iscsit_unregister_transport(&iscsi_target_transport);
629 kmem_cache_destroy(lio_qr_cache);
630 kmem_cache_destroy(lio_dr_cache);
631 kmem_cache_destroy(lio_ooo_cache);
632 kmem_cache_destroy(lio_r2t_cache);
635 * Shutdown discovery sessions and disable discovery TPG
637 if (iscsit_global->discovery_tpg)
638 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
640 target_unregister_template(&iscsi_ops);
642 vfree(iscsit_global->ts_bitmap);
643 kfree(iscsit_global);
646 static int iscsit_add_reject(
647 struct iscsi_conn *conn,
648 u8 reason,
649 unsigned char *buf)
651 struct iscsi_cmd *cmd;
653 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
654 if (!cmd)
655 return -1;
657 cmd->iscsi_opcode = ISCSI_OP_REJECT;
658 cmd->reject_reason = reason;
660 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
661 if (!cmd->buf_ptr) {
662 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
663 iscsit_free_cmd(cmd, false);
664 return -1;
667 spin_lock_bh(&conn->cmd_lock);
668 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
669 spin_unlock_bh(&conn->cmd_lock);
671 cmd->i_state = ISTATE_SEND_REJECT;
672 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
674 return -1;
677 static int iscsit_add_reject_from_cmd(
678 struct iscsi_cmd *cmd,
679 u8 reason,
680 bool add_to_conn,
681 unsigned char *buf)
683 struct iscsi_conn *conn;
685 if (!cmd->conn) {
686 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
687 cmd->init_task_tag);
688 return -1;
690 conn = cmd->conn;
692 cmd->iscsi_opcode = ISCSI_OP_REJECT;
693 cmd->reject_reason = reason;
695 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
696 if (!cmd->buf_ptr) {
697 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
698 iscsit_free_cmd(cmd, false);
699 return -1;
702 if (add_to_conn) {
703 spin_lock_bh(&conn->cmd_lock);
704 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
705 spin_unlock_bh(&conn->cmd_lock);
708 cmd->i_state = ISTATE_SEND_REJECT;
709 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
711 * Perform the kref_put now if se_cmd has already been setup by
712 * scsit_setup_scsi_cmd()
714 if (cmd->se_cmd.se_tfo != NULL) {
715 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
716 target_put_sess_cmd(&cmd->se_cmd);
718 return -1;
721 static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
722 unsigned char *buf)
724 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
727 int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
729 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
733 * Map some portion of the allocated scatterlist to an iovec, suitable for
734 * kernel sockets to copy data in/out.
736 static int iscsit_map_iovec(
737 struct iscsi_cmd *cmd,
738 struct kvec *iov,
739 u32 data_offset,
740 u32 data_length)
742 u32 i = 0;
743 struct scatterlist *sg;
744 unsigned int page_off;
747 * We know each entry in t_data_sg contains a page.
749 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
750 page_off = (data_offset % PAGE_SIZE);
752 cmd->first_data_sg = sg;
753 cmd->first_data_sg_off = page_off;
755 while (data_length) {
756 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
758 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
759 iov[i].iov_len = cur_len;
761 data_length -= cur_len;
762 page_off = 0;
763 sg = sg_next(sg);
764 i++;
767 cmd->kmapped_nents = i;
769 return i;
772 static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
774 u32 i;
775 struct scatterlist *sg;
777 sg = cmd->first_data_sg;
779 for (i = 0; i < cmd->kmapped_nents; i++)
780 kunmap(sg_page(&sg[i]));
783 static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
785 LIST_HEAD(ack_list);
786 struct iscsi_cmd *cmd, *cmd_p;
788 conn->exp_statsn = exp_statsn;
790 if (conn->sess->sess_ops->RDMAExtensions)
791 return;
793 spin_lock_bh(&conn->cmd_lock);
794 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
795 spin_lock(&cmd->istate_lock);
796 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
797 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
798 cmd->i_state = ISTATE_REMOVE;
799 spin_unlock(&cmd->istate_lock);
800 list_move_tail(&cmd->i_conn_node, &ack_list);
801 continue;
803 spin_unlock(&cmd->istate_lock);
805 spin_unlock_bh(&conn->cmd_lock);
807 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
808 list_del_init(&cmd->i_conn_node);
809 iscsit_free_cmd(cmd, false);
813 static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
815 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
817 iov_count += ISCSI_IOV_DATA_BUFFER;
819 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
820 if (!cmd->iov_data) {
821 pr_err("Unable to allocate cmd->iov_data\n");
822 return -ENOMEM;
825 cmd->orig_iov_data_count = iov_count;
826 return 0;
829 int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
830 unsigned char *buf)
832 int data_direction, payload_length;
833 struct iscsi_scsi_req *hdr;
834 int iscsi_task_attr;
835 int sam_task_attr;
837 atomic_long_inc(&conn->sess->cmd_pdus);
839 hdr = (struct iscsi_scsi_req *) buf;
840 payload_length = ntoh24(hdr->dlength);
842 /* FIXME; Add checks for AdditionalHeaderSegment */
844 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
845 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
846 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
847 " not set. Bad iSCSI Initiator.\n");
848 return iscsit_add_reject_cmd(cmd,
849 ISCSI_REASON_BOOKMARK_INVALID, buf);
852 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
853 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
855 * From RFC-3720 Section 10.3.1:
857 * "Either or both of R and W MAY be 1 when either the
858 * Expected Data Transfer Length and/or Bidirectional Read
859 * Expected Data Transfer Length are 0"
861 * For this case, go ahead and clear the unnecssary bits
862 * to avoid any confusion with ->data_direction.
864 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
865 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
867 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
868 " set when Expected Data Transfer Length is 0 for"
869 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
872 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
873 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
874 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
875 " MUST be set if Expected Data Transfer Length is not 0."
876 " Bad iSCSI Initiator\n");
877 return iscsit_add_reject_cmd(cmd,
878 ISCSI_REASON_BOOKMARK_INVALID, buf);
881 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
882 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
883 pr_err("Bidirectional operations not supported!\n");
884 return iscsit_add_reject_cmd(cmd,
885 ISCSI_REASON_BOOKMARK_INVALID, buf);
888 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
889 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
890 " Scsi Command PDU.\n");
891 return iscsit_add_reject_cmd(cmd,
892 ISCSI_REASON_BOOKMARK_INVALID, buf);
895 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
896 pr_err("ImmediateData=No but DataSegmentLength=%u,"
897 " protocol error.\n", payload_length);
898 return iscsit_add_reject_cmd(cmd,
899 ISCSI_REASON_PROTOCOL_ERROR, buf);
902 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
903 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
904 pr_err("Expected Data Transfer Length and Length of"
905 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
906 " bit is not set protocol error\n");
907 return iscsit_add_reject_cmd(cmd,
908 ISCSI_REASON_PROTOCOL_ERROR, buf);
911 if (payload_length > be32_to_cpu(hdr->data_length)) {
912 pr_err("DataSegmentLength: %u is greater than"
913 " EDTL: %u, protocol error.\n", payload_length,
914 hdr->data_length);
915 return iscsit_add_reject_cmd(cmd,
916 ISCSI_REASON_PROTOCOL_ERROR, buf);
919 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
920 pr_err("DataSegmentLength: %u is greater than"
921 " MaxXmitDataSegmentLength: %u, protocol error.\n",
922 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
923 return iscsit_add_reject_cmd(cmd,
924 ISCSI_REASON_PROTOCOL_ERROR, buf);
927 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
928 pr_err("DataSegmentLength: %u is greater than"
929 " FirstBurstLength: %u, protocol error.\n",
930 payload_length, conn->sess->sess_ops->FirstBurstLength);
931 return iscsit_add_reject_cmd(cmd,
932 ISCSI_REASON_BOOKMARK_INVALID, buf);
935 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
936 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
937 DMA_NONE;
939 cmd->data_direction = data_direction;
940 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
942 * Figure out the SAM Task Attribute for the incoming SCSI CDB
944 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
945 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
946 sam_task_attr = TCM_SIMPLE_TAG;
947 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
948 sam_task_attr = TCM_ORDERED_TAG;
949 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
950 sam_task_attr = TCM_HEAD_TAG;
951 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
952 sam_task_attr = TCM_ACA_TAG;
953 else {
954 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
955 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
956 sam_task_attr = TCM_SIMPLE_TAG;
959 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
960 cmd->i_state = ISTATE_NEW_CMD;
961 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
962 cmd->immediate_data = (payload_length) ? 1 : 0;
963 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
964 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
965 if (cmd->unsolicited_data)
966 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
968 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
969 if (hdr->flags & ISCSI_FLAG_CMD_READ)
970 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
971 else
972 cmd->targ_xfer_tag = 0xFFFFFFFF;
973 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
974 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
975 cmd->first_burst_len = payload_length;
977 if (!conn->sess->sess_ops->RDMAExtensions &&
978 cmd->data_direction == DMA_FROM_DEVICE) {
979 struct iscsi_datain_req *dr;
981 dr = iscsit_allocate_datain_req();
982 if (!dr)
983 return iscsit_add_reject_cmd(cmd,
984 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
986 iscsit_attach_datain_req(cmd, dr);
990 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
992 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
993 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
994 cmd->data_direction, sam_task_attr,
995 cmd->sense_buffer + 2);
997 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
998 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
999 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1000 conn->cid);
1002 target_get_sess_cmd(&cmd->se_cmd, true);
1004 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1005 scsilun_to_int(&hdr->lun));
1006 if (cmd->sense_reason)
1007 goto attach_cmd;
1009 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1010 if (cmd->sense_reason) {
1011 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
1012 return iscsit_add_reject_cmd(cmd,
1013 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1016 goto attach_cmd;
1019 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
1020 return iscsit_add_reject_cmd(cmd,
1021 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1024 attach_cmd:
1025 spin_lock_bh(&conn->cmd_lock);
1026 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1027 spin_unlock_bh(&conn->cmd_lock);
1029 * Check if we need to delay processing because of ALUA
1030 * Active/NonOptimized primary access state..
1032 core_alua_check_nonop_delay(&cmd->se_cmd);
1034 return 0;
1036 EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
1038 void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1040 iscsit_set_dataout_sequence_values(cmd);
1042 spin_lock_bh(&cmd->dataout_timeout_lock);
1043 iscsit_start_dataout_timer(cmd, cmd->conn);
1044 spin_unlock_bh(&cmd->dataout_timeout_lock);
1046 EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1048 int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1049 struct iscsi_scsi_req *hdr)
1051 int cmdsn_ret = 0;
1053 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1054 * the Immediate Bit is not set, and no Immediate
1055 * Data is attached.
1057 * A PDU/CmdSN carrying Immediate Data can only
1058 * be processed after the DataCRC has passed.
1059 * If the DataCRC fails, the CmdSN MUST NOT
1060 * be acknowledged. (See below)
1062 if (!cmd->immediate_data) {
1063 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1064 (unsigned char *)hdr, hdr->cmdsn);
1065 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1066 return -1;
1067 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1068 target_put_sess_cmd(&cmd->se_cmd);
1069 return 0;
1073 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1076 * If no Immediate Data is attached, it's OK to return now.
1078 if (!cmd->immediate_data) {
1079 if (!cmd->sense_reason && cmd->unsolicited_data)
1080 iscsit_set_unsoliticed_dataout(cmd);
1081 if (!cmd->sense_reason)
1082 return 0;
1084 target_put_sess_cmd(&cmd->se_cmd);
1085 return 0;
1089 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1090 * execution. These exceptions are processed in CmdSN order using
1091 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1093 if (cmd->sense_reason) {
1094 if (cmd->reject_reason)
1095 return 0;
1097 return 1;
1100 * Call directly into transport_generic_new_cmd() to perform
1101 * the backend memory allocation.
1103 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
1104 if (cmd->sense_reason)
1105 return 1;
1107 return 0;
1109 EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1111 static int
1112 iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1113 bool dump_payload)
1115 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1117 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1119 if (dump_payload)
1120 goto after_immediate_data;
1122 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1123 cmd->first_burst_len);
1124 after_immediate_data:
1125 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1127 * A PDU/CmdSN carrying Immediate Data passed
1128 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1129 * Immediate Bit is not set.
1131 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1132 (unsigned char *)hdr, hdr->cmdsn);
1133 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1134 return -1;
1136 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1137 int rc;
1139 rc = iscsit_dump_data_payload(cmd->conn,
1140 cmd->first_burst_len, 1);
1141 target_put_sess_cmd(&cmd->se_cmd);
1142 return rc;
1143 } else if (cmd->unsolicited_data)
1144 iscsit_set_unsoliticed_dataout(cmd);
1146 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1148 * Immediate Data failed DataCRC and ERL>=1,
1149 * silently drop this PDU and let the initiator
1150 * plug the CmdSN gap.
1152 * FIXME: Send Unsolicited NOPIN with reserved
1153 * TTT here to help the initiator figure out
1154 * the missing CmdSN, although they should be
1155 * intelligent enough to determine the missing
1156 * CmdSN and issue a retry to plug the sequence.
1158 cmd->i_state = ISTATE_REMOVE;
1159 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
1160 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1161 return -1;
1163 return 0;
1166 static int
1167 iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1168 unsigned char *buf)
1170 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1171 int rc, immed_data;
1172 bool dump_payload = false;
1174 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1175 if (rc < 0)
1176 return 0;
1178 * Allocation iovecs needed for struct socket operations for
1179 * traditional iSCSI block I/O.
1181 if (iscsit_allocate_iovecs(cmd) < 0) {
1182 return iscsit_reject_cmd(cmd,
1183 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1185 immed_data = cmd->immediate_data;
1187 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1188 if (rc < 0)
1189 return rc;
1190 else if (rc > 0)
1191 dump_payload = true;
1193 if (!immed_data)
1194 return 0;
1196 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1199 static u32 iscsit_do_crypto_hash_sg(
1200 struct hash_desc *hash,
1201 struct iscsi_cmd *cmd,
1202 u32 data_offset,
1203 u32 data_length,
1204 u32 padding,
1205 u8 *pad_bytes)
1207 u32 data_crc;
1208 u32 i;
1209 struct scatterlist *sg;
1210 unsigned int page_off;
1212 crypto_hash_init(hash);
1214 sg = cmd->first_data_sg;
1215 page_off = cmd->first_data_sg_off;
1217 i = 0;
1218 while (data_length) {
1219 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1221 crypto_hash_update(hash, &sg[i], cur_len);
1223 data_length -= cur_len;
1224 page_off = 0;
1225 i++;
1228 if (padding) {
1229 struct scatterlist pad_sg;
1231 sg_init_one(&pad_sg, pad_bytes, padding);
1232 crypto_hash_update(hash, &pad_sg, padding);
1234 crypto_hash_final(hash, (u8 *) &data_crc);
1236 return data_crc;
1239 static void iscsit_do_crypto_hash_buf(
1240 struct hash_desc *hash,
1241 const void *buf,
1242 u32 payload_length,
1243 u32 padding,
1244 u8 *pad_bytes,
1245 u8 *data_crc)
1247 struct scatterlist sg;
1249 crypto_hash_init(hash);
1251 sg_init_one(&sg, buf, payload_length);
1252 crypto_hash_update(hash, &sg, payload_length);
1254 if (padding) {
1255 sg_init_one(&sg, pad_bytes, padding);
1256 crypto_hash_update(hash, &sg, padding);
1258 crypto_hash_final(hash, data_crc);
1262 iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1263 struct iscsi_cmd **out_cmd)
1265 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1266 struct iscsi_cmd *cmd = NULL;
1267 struct se_cmd *se_cmd;
1268 u32 payload_length = ntoh24(hdr->dlength);
1269 int rc;
1271 if (!payload_length) {
1272 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1273 return 0;
1276 /* iSCSI write */
1277 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
1279 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1280 pr_err("DataSegmentLength: %u is greater than"
1281 " MaxXmitDataSegmentLength: %u\n", payload_length,
1282 conn->conn_ops->MaxXmitDataSegmentLength);
1283 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1284 buf);
1287 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1288 payload_length);
1289 if (!cmd)
1290 return 0;
1292 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1293 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1294 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
1295 payload_length, conn->cid);
1297 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1298 pr_err("Command ITT: 0x%08x received DataOUT after"
1299 " last DataOUT received, dumping payload\n",
1300 cmd->init_task_tag);
1301 return iscsit_dump_data_payload(conn, payload_length, 1);
1304 if (cmd->data_direction != DMA_TO_DEVICE) {
1305 pr_err("Command ITT: 0x%08x received DataOUT for a"
1306 " NON-WRITE command.\n", cmd->init_task_tag);
1307 return iscsit_dump_data_payload(conn, payload_length, 1);
1309 se_cmd = &cmd->se_cmd;
1310 iscsit_mod_dataout_timer(cmd);
1312 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
1313 pr_err("DataOut Offset: %u, Length %u greater than"
1314 " iSCSI Command EDTL %u, protocol error.\n",
1315 hdr->offset, payload_length, cmd->se_cmd.data_length);
1316 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
1319 if (cmd->unsolicited_data) {
1320 int dump_unsolicited_data = 0;
1322 if (conn->sess->sess_ops->InitialR2T) {
1323 pr_err("Received unexpected unsolicited data"
1324 " while InitialR2T=Yes, protocol error.\n");
1325 transport_send_check_condition_and_sense(&cmd->se_cmd,
1326 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1327 return -1;
1330 * Special case for dealing with Unsolicited DataOUT
1331 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1332 * failures;
1335 /* Something's amiss if we're not in WRITE_PENDING state... */
1336 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1337 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
1338 dump_unsolicited_data = 1;
1340 if (dump_unsolicited_data) {
1342 * Check if a delayed TASK_ABORTED status needs to
1343 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1344 * received with the unsolicitied data out.
1346 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1347 iscsit_stop_dataout_timer(cmd);
1349 transport_check_aborted_status(se_cmd,
1350 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1351 return iscsit_dump_data_payload(conn, payload_length, 1);
1353 } else {
1355 * For the normal solicited data path:
1357 * Check for a delayed TASK_ABORTED status and dump any
1358 * incoming data out payload if one exists. Also, when the
1359 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1360 * data out sequence, we decrement outstanding_r2ts. Once
1361 * outstanding_r2ts reaches zero, go ahead and send the delayed
1362 * TASK_ABORTED status.
1364 if (se_cmd->transport_state & CMD_T_ABORTED) {
1365 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1366 if (--cmd->outstanding_r2ts < 1) {
1367 iscsit_stop_dataout_timer(cmd);
1368 transport_check_aborted_status(
1369 se_cmd, 1);
1372 return iscsit_dump_data_payload(conn, payload_length, 1);
1376 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1377 * within-command recovery checks before receiving the payload.
1379 rc = iscsit_check_pre_dataout(cmd, buf);
1380 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
1381 return 0;
1382 else if (rc == DATAOUT_CANNOT_RECOVER)
1383 return -1;
1385 *out_cmd = cmd;
1386 return 0;
1388 EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1390 static int
1391 iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1392 struct iscsi_data *hdr)
1394 struct kvec *iov;
1395 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1396 u32 payload_length = ntoh24(hdr->dlength);
1397 int iov_ret, data_crc_failed = 0;
1399 rx_size += payload_length;
1400 iov = &cmd->iov_data[0];
1402 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1403 payload_length);
1404 if (iov_ret < 0)
1405 return -1;
1407 iov_count += iov_ret;
1409 padding = ((-payload_length) & 3);
1410 if (padding != 0) {
1411 iov[iov_count].iov_base = cmd->pad_bytes;
1412 iov[iov_count++].iov_len = padding;
1413 rx_size += padding;
1414 pr_debug("Receiving %u padding bytes.\n", padding);
1417 if (conn->conn_ops->DataDigest) {
1418 iov[iov_count].iov_base = &checksum;
1419 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1420 rx_size += ISCSI_CRC_LEN;
1423 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1425 iscsit_unmap_iovec(cmd);
1427 if (rx_got != rx_size)
1428 return -1;
1430 if (conn->conn_ops->DataDigest) {
1431 u32 data_crc;
1433 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
1434 be32_to_cpu(hdr->offset),
1435 payload_length, padding,
1436 cmd->pad_bytes);
1438 if (checksum != data_crc) {
1439 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1440 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1441 " does not match computed 0x%08x\n",
1442 hdr->itt, hdr->offset, payload_length,
1443 hdr->datasn, checksum, data_crc);
1444 data_crc_failed = 1;
1445 } else {
1446 pr_debug("Got CRC32C DataDigest 0x%08x for"
1447 " %u bytes of Data Out\n", checksum,
1448 payload_length);
1452 return data_crc_failed;
1456 iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1457 bool data_crc_failed)
1459 struct iscsi_conn *conn = cmd->conn;
1460 int rc, ooo_cmdsn;
1462 * Increment post receive data and CRC values or perform
1463 * within-command recovery.
1465 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1466 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
1467 return 0;
1468 else if (rc == DATAOUT_SEND_R2T) {
1469 iscsit_set_dataout_sequence_values(cmd);
1470 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1471 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
1473 * Handle extra special case for out of order
1474 * Unsolicited Data Out.
1476 spin_lock_bh(&cmd->istate_lock);
1477 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1478 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1479 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1480 spin_unlock_bh(&cmd->istate_lock);
1482 iscsit_stop_dataout_timer(cmd);
1483 if (ooo_cmdsn)
1484 return 0;
1485 target_execute_cmd(&cmd->se_cmd);
1486 return 0;
1487 } else /* DATAOUT_CANNOT_RECOVER */
1488 return -1;
1490 return 0;
1492 EXPORT_SYMBOL(iscsit_check_dataout_payload);
1494 static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1496 struct iscsi_cmd *cmd = NULL;
1497 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1498 int rc;
1499 bool data_crc_failed = false;
1501 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1502 if (rc < 0)
1503 return 0;
1504 else if (!cmd)
1505 return 0;
1507 rc = iscsit_get_dataout(conn, cmd, hdr);
1508 if (rc < 0)
1509 return rc;
1510 else if (rc > 0)
1511 data_crc_failed = true;
1513 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1516 int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1517 struct iscsi_nopout *hdr)
1519 u32 payload_length = ntoh24(hdr->dlength);
1521 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1522 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1523 if (!cmd)
1524 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1525 (unsigned char *)hdr);
1527 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1528 (unsigned char *)hdr);
1531 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1532 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1533 " not set, protocol error.\n");
1534 if (!cmd)
1535 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1536 (unsigned char *)hdr);
1538 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1539 (unsigned char *)hdr);
1542 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1543 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1544 " greater than MaxXmitDataSegmentLength: %u, protocol"
1545 " error.\n", payload_length,
1546 conn->conn_ops->MaxXmitDataSegmentLength);
1547 if (!cmd)
1548 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1549 (unsigned char *)hdr);
1551 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1552 (unsigned char *)hdr);
1555 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1556 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1557 hdr->itt == RESERVED_ITT ? "Response" : "Request",
1558 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1559 payload_length);
1561 * This is not a response to a Unsolicited NopIN, which means
1562 * it can either be a NOPOUT ping request (with a valid ITT),
1563 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1564 * Either way, make sure we allocate an struct iscsi_cmd, as both
1565 * can contain ping data.
1567 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1568 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1569 cmd->i_state = ISTATE_SEND_NOPIN;
1570 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1571 1 : 0);
1572 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1573 cmd->targ_xfer_tag = 0xFFFFFFFF;
1574 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1575 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1576 cmd->data_direction = DMA_NONE;
1579 return 0;
1581 EXPORT_SYMBOL(iscsit_setup_nop_out);
1583 int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1584 struct iscsi_nopout *hdr)
1586 struct iscsi_cmd *cmd_p = NULL;
1587 int cmdsn_ret = 0;
1589 * Initiator is expecting a NopIN ping reply..
1591 if (hdr->itt != RESERVED_ITT) {
1592 if (!cmd)
1593 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1594 (unsigned char *)hdr);
1596 spin_lock_bh(&conn->cmd_lock);
1597 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1598 spin_unlock_bh(&conn->cmd_lock);
1600 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1602 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1603 iscsit_add_cmd_to_response_queue(cmd, conn,
1604 cmd->i_state);
1605 return 0;
1608 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1609 (unsigned char *)hdr, hdr->cmdsn);
1610 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1611 return 0;
1612 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1613 return -1;
1615 return 0;
1618 * This was a response to a unsolicited NOPIN ping.
1620 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1621 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1622 if (!cmd_p)
1623 return -EINVAL;
1625 iscsit_stop_nopin_response_timer(conn);
1627 cmd_p->i_state = ISTATE_REMOVE;
1628 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1630 iscsit_start_nopin_timer(conn);
1631 return 0;
1634 * Otherwise, initiator is not expecting a NOPIN is response.
1635 * Just ignore for now.
1637 return 0;
1639 EXPORT_SYMBOL(iscsit_process_nop_out);
1641 static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1642 unsigned char *buf)
1644 unsigned char *ping_data = NULL;
1645 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1646 struct kvec *iov = NULL;
1647 u32 payload_length = ntoh24(hdr->dlength);
1648 int ret;
1650 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1651 if (ret < 0)
1652 return 0;
1654 * Handle NOP-OUT payload for traditional iSCSI sockets
1656 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1657 u32 checksum, data_crc, padding = 0;
1658 int niov = 0, rx_got, rx_size = payload_length;
1660 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1661 if (!ping_data) {
1662 pr_err("Unable to allocate memory for"
1663 " NOPOUT ping data.\n");
1664 ret = -1;
1665 goto out;
1668 iov = &cmd->iov_misc[0];
1669 iov[niov].iov_base = ping_data;
1670 iov[niov++].iov_len = payload_length;
1672 padding = ((-payload_length) & 3);
1673 if (padding != 0) {
1674 pr_debug("Receiving %u additional bytes"
1675 " for padding.\n", padding);
1676 iov[niov].iov_base = &cmd->pad_bytes;
1677 iov[niov++].iov_len = padding;
1678 rx_size += padding;
1680 if (conn->conn_ops->DataDigest) {
1681 iov[niov].iov_base = &checksum;
1682 iov[niov++].iov_len = ISCSI_CRC_LEN;
1683 rx_size += ISCSI_CRC_LEN;
1686 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1687 if (rx_got != rx_size) {
1688 ret = -1;
1689 goto out;
1692 if (conn->conn_ops->DataDigest) {
1693 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1694 ping_data, payload_length,
1695 padding, cmd->pad_bytes,
1696 (u8 *)&data_crc);
1698 if (checksum != data_crc) {
1699 pr_err("Ping data CRC32C DataDigest"
1700 " 0x%08x does not match computed 0x%08x\n",
1701 checksum, data_crc);
1702 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1703 pr_err("Unable to recover from"
1704 " NOPOUT Ping DataCRC failure while in"
1705 " ERL=0.\n");
1706 ret = -1;
1707 goto out;
1708 } else {
1710 * Silently drop this PDU and let the
1711 * initiator plug the CmdSN gap.
1713 pr_debug("Dropping NOPOUT"
1714 " Command CmdSN: 0x%08x due to"
1715 " DataCRC error.\n", hdr->cmdsn);
1716 ret = 0;
1717 goto out;
1719 } else {
1720 pr_debug("Got CRC32C DataDigest"
1721 " 0x%08x for %u bytes of ping data.\n",
1722 checksum, payload_length);
1726 ping_data[payload_length] = '\0';
1728 * Attach ping data to struct iscsi_cmd->buf_ptr.
1730 cmd->buf_ptr = ping_data;
1731 cmd->buf_ptr_size = payload_length;
1733 pr_debug("Got %u bytes of NOPOUT ping"
1734 " data.\n", payload_length);
1735 pr_debug("Ping Data: \"%s\"\n", ping_data);
1738 return iscsit_process_nop_out(conn, cmd, hdr);
1739 out:
1740 if (cmd)
1741 iscsit_free_cmd(cmd, false);
1743 kfree(ping_data);
1744 return ret;
1748 iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1749 unsigned char *buf)
1751 struct se_tmr_req *se_tmr;
1752 struct iscsi_tmr_req *tmr_req;
1753 struct iscsi_tm *hdr;
1754 int out_of_order_cmdsn = 0, ret;
1755 bool sess_ref = false;
1756 u8 function;
1758 hdr = (struct iscsi_tm *) buf;
1759 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1760 function = hdr->flags;
1762 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1763 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1764 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1765 hdr->rtt, hdr->refcmdsn, conn->cid);
1767 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1768 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1769 hdr->rtt != RESERVED_ITT)) {
1770 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1771 hdr->rtt = RESERVED_ITT;
1774 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1775 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1776 pr_err("Task Management Request TASK_REASSIGN not"
1777 " issued as immediate command, bad iSCSI Initiator"
1778 "implementation\n");
1779 return iscsit_add_reject_cmd(cmd,
1780 ISCSI_REASON_PROTOCOL_ERROR, buf);
1782 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1783 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1784 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
1786 cmd->data_direction = DMA_NONE;
1788 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1789 if (!cmd->tmr_req) {
1790 pr_err("Unable to allocate memory for"
1791 " Task Management command!\n");
1792 return iscsit_add_reject_cmd(cmd,
1793 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1794 buf);
1798 * TASK_REASSIGN for ERL=2 / connection stays inside of
1799 * LIO-Target $FABRIC_MOD
1801 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1803 u8 tcm_function;
1804 int ret;
1806 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
1807 conn->sess->se_sess, 0, DMA_NONE,
1808 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
1810 target_get_sess_cmd(&cmd->se_cmd, true);
1811 sess_ref = true;
1813 switch (function) {
1814 case ISCSI_TM_FUNC_ABORT_TASK:
1815 tcm_function = TMR_ABORT_TASK;
1816 break;
1817 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1818 tcm_function = TMR_ABORT_TASK_SET;
1819 break;
1820 case ISCSI_TM_FUNC_CLEAR_ACA:
1821 tcm_function = TMR_CLEAR_ACA;
1822 break;
1823 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1824 tcm_function = TMR_CLEAR_TASK_SET;
1825 break;
1826 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1827 tcm_function = TMR_LUN_RESET;
1828 break;
1829 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1830 tcm_function = TMR_TARGET_WARM_RESET;
1831 break;
1832 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1833 tcm_function = TMR_TARGET_COLD_RESET;
1834 break;
1835 default:
1836 pr_err("Unknown iSCSI TMR Function:"
1837 " 0x%02x\n", function);
1838 return iscsit_add_reject_cmd(cmd,
1839 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1842 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1843 tcm_function, GFP_KERNEL);
1844 if (ret < 0)
1845 return iscsit_add_reject_cmd(cmd,
1846 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1848 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1851 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1852 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1853 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1854 cmd->init_task_tag = hdr->itt;
1855 cmd->targ_xfer_tag = 0xFFFFFFFF;
1856 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1857 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1858 se_tmr = cmd->se_cmd.se_tmr_req;
1859 tmr_req = cmd->tmr_req;
1861 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1863 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1864 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1865 scsilun_to_int(&hdr->lun));
1866 if (ret < 0) {
1867 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1868 goto attach;
1872 switch (function) {
1873 case ISCSI_TM_FUNC_ABORT_TASK:
1874 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
1875 if (se_tmr->response)
1876 goto attach;
1877 break;
1878 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1879 case ISCSI_TM_FUNC_CLEAR_ACA:
1880 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1881 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1882 break;
1883 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1884 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
1885 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1886 goto attach;
1888 break;
1889 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1890 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
1891 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1892 goto attach;
1894 break;
1895 case ISCSI_TM_FUNC_TASK_REASSIGN:
1896 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1898 * Perform sanity checks on the ExpDataSN only if the
1899 * TASK_REASSIGN was successful.
1901 if (se_tmr->response)
1902 break;
1904 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
1905 return iscsit_add_reject_cmd(cmd,
1906 ISCSI_REASON_BOOKMARK_INVALID, buf);
1907 break;
1908 default:
1909 pr_err("Unknown TMR function: 0x%02x, protocol"
1910 " error.\n", function);
1911 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1912 goto attach;
1915 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1916 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1917 se_tmr->call_transport = 1;
1918 attach:
1919 spin_lock_bh(&conn->cmd_lock);
1920 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1921 spin_unlock_bh(&conn->cmd_lock);
1923 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1924 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1925 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1926 out_of_order_cmdsn = 1;
1927 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1928 return 0;
1929 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1930 return -1;
1932 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1934 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
1935 return 0;
1937 * Found the referenced task, send to transport for processing.
1939 if (se_tmr->call_transport)
1940 return transport_generic_handle_tmr(&cmd->se_cmd);
1943 * Could not find the referenced LUN, task, or Task Management
1944 * command not authorized or supported. Change state and
1945 * let the tx_thread send the response.
1947 * For connection recovery, this is also the default action for
1948 * TMR TASK_REASSIGN.
1950 if (sess_ref) {
1951 pr_debug("Handle TMR, using sess_ref=true check\n");
1952 target_put_sess_cmd(&cmd->se_cmd);
1955 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1956 return 0;
1958 EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
1960 /* #warning FIXME: Support Text Command parameters besides SendTargets */
1962 iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1963 struct iscsi_text *hdr)
1965 u32 payload_length = ntoh24(hdr->dlength);
1967 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1968 pr_err("Unable to accept text parameter length: %u"
1969 "greater than MaxXmitDataSegmentLength %u.\n",
1970 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
1971 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1972 (unsigned char *)hdr);
1975 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
1976 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
1977 pr_err("Multi sequence text commands currently not supported\n");
1978 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
1979 (unsigned char *)hdr);
1982 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1983 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1984 hdr->exp_statsn, payload_length);
1986 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1987 cmd->i_state = ISTATE_SEND_TEXTRSP;
1988 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1989 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1990 cmd->targ_xfer_tag = 0xFFFFFFFF;
1991 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1992 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1993 cmd->data_direction = DMA_NONE;
1994 cmd->text_in_ptr = NULL;
1996 return 0;
1998 EXPORT_SYMBOL(iscsit_setup_text_cmd);
2001 iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2002 struct iscsi_text *hdr)
2004 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
2005 int cmdsn_ret;
2007 if (!text_in) {
2008 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2009 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2010 pr_err("Unable to locate text_in buffer for sendtargets"
2011 " discovery\n");
2012 goto reject;
2014 goto empty_sendtargets;
2016 if (strncmp("SendTargets", text_in, 11) != 0) {
2017 pr_err("Received Text Data that is not"
2018 " SendTargets, cannot continue.\n");
2019 goto reject;
2021 text_ptr = strchr(text_in, '=');
2022 if (!text_ptr) {
2023 pr_err("No \"=\" separator found in Text Data,"
2024 " cannot continue.\n");
2025 goto reject;
2027 if (!strncmp("=All", text_ptr, 4)) {
2028 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
2029 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2030 !strncmp("=eui.", text_ptr, 5)) {
2031 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
2032 } else {
2033 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2034 goto reject;
2037 spin_lock_bh(&conn->cmd_lock);
2038 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2039 spin_unlock_bh(&conn->cmd_lock);
2041 empty_sendtargets:
2042 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2044 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2045 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2046 (unsigned char *)hdr, hdr->cmdsn);
2047 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2048 return -1;
2050 return 0;
2053 return iscsit_execute_cmd(cmd, 0);
2055 reject:
2056 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2057 (unsigned char *)hdr);
2059 EXPORT_SYMBOL(iscsit_process_text_cmd);
2061 static int
2062 iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2063 unsigned char *buf)
2065 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2066 char *text_in = NULL;
2067 u32 payload_length = ntoh24(hdr->dlength);
2068 int rx_size, rc;
2070 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2071 if (rc < 0)
2072 return 0;
2074 rx_size = payload_length;
2075 if (payload_length) {
2076 u32 checksum = 0, data_crc = 0;
2077 u32 padding = 0, pad_bytes = 0;
2078 int niov = 0, rx_got;
2079 struct kvec iov[3];
2081 text_in = kzalloc(payload_length, GFP_KERNEL);
2082 if (!text_in) {
2083 pr_err("Unable to allocate memory for"
2084 " incoming text parameters\n");
2085 goto reject;
2087 cmd->text_in_ptr = text_in;
2089 memset(iov, 0, 3 * sizeof(struct kvec));
2090 iov[niov].iov_base = text_in;
2091 iov[niov++].iov_len = payload_length;
2093 padding = ((-payload_length) & 3);
2094 if (padding != 0) {
2095 iov[niov].iov_base = &pad_bytes;
2096 iov[niov++].iov_len = padding;
2097 rx_size += padding;
2098 pr_debug("Receiving %u additional bytes"
2099 " for padding.\n", padding);
2101 if (conn->conn_ops->DataDigest) {
2102 iov[niov].iov_base = &checksum;
2103 iov[niov++].iov_len = ISCSI_CRC_LEN;
2104 rx_size += ISCSI_CRC_LEN;
2107 rx_got = rx_data(conn, &iov[0], niov, rx_size);
2108 if (rx_got != rx_size)
2109 goto reject;
2111 if (conn->conn_ops->DataDigest) {
2112 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
2113 text_in, payload_length,
2114 padding, (u8 *)&pad_bytes,
2115 (u8 *)&data_crc);
2117 if (checksum != data_crc) {
2118 pr_err("Text data CRC32C DataDigest"
2119 " 0x%08x does not match computed"
2120 " 0x%08x\n", checksum, data_crc);
2121 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2122 pr_err("Unable to recover from"
2123 " Text Data digest failure while in"
2124 " ERL=0.\n");
2125 goto reject;
2126 } else {
2128 * Silently drop this PDU and let the
2129 * initiator plug the CmdSN gap.
2131 pr_debug("Dropping Text"
2132 " Command CmdSN: 0x%08x due to"
2133 " DataCRC error.\n", hdr->cmdsn);
2134 kfree(text_in);
2135 return 0;
2137 } else {
2138 pr_debug("Got CRC32C DataDigest"
2139 " 0x%08x for %u bytes of text data.\n",
2140 checksum, payload_length);
2143 text_in[payload_length - 1] = '\0';
2144 pr_debug("Successfully read %d bytes of text"
2145 " data.\n", payload_length);
2148 return iscsit_process_text_cmd(conn, cmd, hdr);
2150 reject:
2151 kfree(cmd->text_in_ptr);
2152 cmd->text_in_ptr = NULL;
2153 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
2156 int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2158 struct iscsi_conn *conn_p;
2159 struct iscsi_session *sess = conn->sess;
2161 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2162 " for SID: %u.\n", conn->cid, conn->sess->sid);
2164 atomic_set(&sess->session_logout, 1);
2165 atomic_set(&conn->conn_logout_remove, 1);
2166 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2168 iscsit_inc_conn_usage_count(conn);
2169 iscsit_inc_session_usage_count(sess);
2171 spin_lock_bh(&sess->conn_lock);
2172 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2173 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2174 continue;
2176 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2177 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2179 spin_unlock_bh(&sess->conn_lock);
2181 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2183 return 0;
2186 int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2188 struct iscsi_conn *l_conn;
2189 struct iscsi_session *sess = conn->sess;
2191 pr_debug("Received logout request CLOSECONNECTION for CID:"
2192 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2195 * A Logout Request with a CLOSECONNECTION reason code for a CID
2196 * can arrive on a connection with a differing CID.
2198 if (conn->cid == cmd->logout_cid) {
2199 spin_lock_bh(&conn->state_lock);
2200 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2201 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2203 atomic_set(&conn->conn_logout_remove, 1);
2204 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2205 iscsit_inc_conn_usage_count(conn);
2207 spin_unlock_bh(&conn->state_lock);
2208 } else {
2210 * Handle all different cid CLOSECONNECTION requests in
2211 * iscsit_logout_post_handler_diffcid() as to give enough
2212 * time for any non immediate command's CmdSN to be
2213 * acknowledged on the connection in question.
2215 * Here we simply make sure the CID is still around.
2217 l_conn = iscsit_get_conn_from_cid(sess,
2218 cmd->logout_cid);
2219 if (!l_conn) {
2220 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2221 iscsit_add_cmd_to_response_queue(cmd, conn,
2222 cmd->i_state);
2223 return 0;
2226 iscsit_dec_conn_usage_count(l_conn);
2229 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2231 return 0;
2234 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2236 struct iscsi_session *sess = conn->sess;
2238 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2239 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2241 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2242 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2243 " while ERL!=2.\n");
2244 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2245 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2246 return 0;
2249 if (conn->cid == cmd->logout_cid) {
2250 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2251 " with CID: %hu on CID: %hu, implementation error.\n",
2252 cmd->logout_cid, conn->cid);
2253 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2254 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2255 return 0;
2258 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2260 return 0;
2264 iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2265 unsigned char *buf)
2267 int cmdsn_ret, logout_remove = 0;
2268 u8 reason_code = 0;
2269 struct iscsi_logout *hdr;
2270 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2272 hdr = (struct iscsi_logout *) buf;
2273 reason_code = (hdr->flags & 0x7f);
2275 if (tiqn) {
2276 spin_lock(&tiqn->logout_stats.lock);
2277 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2278 tiqn->logout_stats.normal_logouts++;
2279 else
2280 tiqn->logout_stats.abnormal_logouts++;
2281 spin_unlock(&tiqn->logout_stats.lock);
2284 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2285 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2286 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2287 hdr->cid, conn->cid);
2289 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2290 pr_err("Received logout request on connection that"
2291 " is not in logged in state, ignoring request.\n");
2292 iscsit_free_cmd(cmd, false);
2293 return 0;
2296 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2297 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2298 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2299 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2300 cmd->targ_xfer_tag = 0xFFFFFFFF;
2301 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2302 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2303 cmd->logout_cid = be16_to_cpu(hdr->cid);
2304 cmd->logout_reason = reason_code;
2305 cmd->data_direction = DMA_NONE;
2308 * We need to sleep in these cases (by returning 1) until the Logout
2309 * Response gets sent in the tx thread.
2311 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2312 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
2313 be16_to_cpu(hdr->cid) == conn->cid))
2314 logout_remove = 1;
2316 spin_lock_bh(&conn->cmd_lock);
2317 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2318 spin_unlock_bh(&conn->cmd_lock);
2320 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
2321 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2324 * Immediate commands are executed, well, immediately.
2325 * Non-Immediate Logout Commands are executed in CmdSN order.
2327 if (cmd->immediate_cmd) {
2328 int ret = iscsit_execute_cmd(cmd, 0);
2330 if (ret < 0)
2331 return ret;
2332 } else {
2333 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
2334 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
2335 logout_remove = 0;
2336 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2337 return -1;
2340 return logout_remove;
2342 EXPORT_SYMBOL(iscsit_handle_logout_cmd);
2344 static int iscsit_handle_snack(
2345 struct iscsi_conn *conn,
2346 unsigned char *buf)
2348 struct iscsi_snack *hdr;
2350 hdr = (struct iscsi_snack *) buf;
2351 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
2353 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2354 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2355 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2356 hdr->begrun, hdr->runlength, conn->cid);
2358 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2359 pr_err("Initiator sent SNACK request while in"
2360 " ErrorRecoveryLevel=0.\n");
2361 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2362 buf);
2365 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2366 * call from inside iscsi_send_recovery_datain_or_r2t().
2368 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2369 case 0:
2370 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
2371 hdr->itt,
2372 be32_to_cpu(hdr->ttt),
2373 be32_to_cpu(hdr->begrun),
2374 be32_to_cpu(hdr->runlength));
2375 case ISCSI_FLAG_SNACK_TYPE_STATUS:
2376 return iscsit_handle_status_snack(conn, hdr->itt,
2377 be32_to_cpu(hdr->ttt),
2378 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
2379 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
2380 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2381 be32_to_cpu(hdr->begrun),
2382 be32_to_cpu(hdr->runlength));
2383 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2384 /* FIXME: Support R-Data SNACK */
2385 pr_err("R-Data SNACK Not Supported.\n");
2386 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2387 buf);
2388 default:
2389 pr_err("Unknown SNACK type 0x%02x, protocol"
2390 " error.\n", hdr->flags & 0x0f);
2391 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2392 buf);
2395 return 0;
2398 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2400 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2401 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2402 wait_for_completion_interruptible_timeout(
2403 &conn->rx_half_close_comp,
2404 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2408 static int iscsit_handle_immediate_data(
2409 struct iscsi_cmd *cmd,
2410 struct iscsi_scsi_req *hdr,
2411 u32 length)
2413 int iov_ret, rx_got = 0, rx_size = 0;
2414 u32 checksum, iov_count = 0, padding = 0;
2415 struct iscsi_conn *conn = cmd->conn;
2416 struct kvec *iov;
2418 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2419 if (iov_ret < 0)
2420 return IMMEDIATE_DATA_CANNOT_RECOVER;
2422 rx_size = length;
2423 iov_count = iov_ret;
2424 iov = &cmd->iov_data[0];
2426 padding = ((-length) & 3);
2427 if (padding != 0) {
2428 iov[iov_count].iov_base = cmd->pad_bytes;
2429 iov[iov_count++].iov_len = padding;
2430 rx_size += padding;
2433 if (conn->conn_ops->DataDigest) {
2434 iov[iov_count].iov_base = &checksum;
2435 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2436 rx_size += ISCSI_CRC_LEN;
2439 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2441 iscsit_unmap_iovec(cmd);
2443 if (rx_got != rx_size) {
2444 iscsit_rx_thread_wait_for_tcp(conn);
2445 return IMMEDIATE_DATA_CANNOT_RECOVER;
2448 if (conn->conn_ops->DataDigest) {
2449 u32 data_crc;
2451 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2452 cmd->write_data_done, length, padding,
2453 cmd->pad_bytes);
2455 if (checksum != data_crc) {
2456 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2457 " does not match computed 0x%08x\n", checksum,
2458 data_crc);
2460 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2461 pr_err("Unable to recover from"
2462 " Immediate Data digest failure while"
2463 " in ERL=0.\n");
2464 iscsit_reject_cmd(cmd,
2465 ISCSI_REASON_DATA_DIGEST_ERROR,
2466 (unsigned char *)hdr);
2467 return IMMEDIATE_DATA_CANNOT_RECOVER;
2468 } else {
2469 iscsit_reject_cmd(cmd,
2470 ISCSI_REASON_DATA_DIGEST_ERROR,
2471 (unsigned char *)hdr);
2472 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2474 } else {
2475 pr_debug("Got CRC32C DataDigest 0x%08x for"
2476 " %u bytes of Immediate Data\n", checksum,
2477 length);
2481 cmd->write_data_done += length;
2483 if (cmd->write_data_done == cmd->se_cmd.data_length) {
2484 spin_lock_bh(&cmd->istate_lock);
2485 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2486 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2487 spin_unlock_bh(&cmd->istate_lock);
2490 return IMMEDIATE_DATA_NORMAL_OPERATION;
2494 * Called with sess->conn_lock held.
2496 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2497 with active network interface */
2498 static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2500 struct iscsi_cmd *cmd;
2501 struct iscsi_conn *conn_p;
2502 bool found = false;
2505 * Only send a Asynchronous Message on connections whos network
2506 * interface is still functional.
2508 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2509 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2510 iscsit_inc_conn_usage_count(conn_p);
2511 found = true;
2512 break;
2516 if (!found)
2517 return;
2519 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
2520 if (!cmd) {
2521 iscsit_dec_conn_usage_count(conn_p);
2522 return;
2525 cmd->logout_cid = conn->cid;
2526 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2527 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2529 spin_lock_bh(&conn_p->cmd_lock);
2530 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
2531 spin_unlock_bh(&conn_p->cmd_lock);
2533 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2534 iscsit_dec_conn_usage_count(conn_p);
2537 static int iscsit_send_conn_drop_async_message(
2538 struct iscsi_cmd *cmd,
2539 struct iscsi_conn *conn)
2541 struct iscsi_async *hdr;
2543 cmd->tx_size = ISCSI_HDR_LEN;
2544 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2546 hdr = (struct iscsi_async *) cmd->pdu;
2547 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2548 hdr->flags = ISCSI_FLAG_CMD_FINAL;
2549 cmd->init_task_tag = RESERVED_ITT;
2550 cmd->targ_xfer_tag = 0xFFFFFFFF;
2551 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2552 cmd->stat_sn = conn->stat_sn++;
2553 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2554 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2555 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2556 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2557 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2558 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2559 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2561 if (conn->conn_ops->HeaderDigest) {
2562 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2564 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2565 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
2567 cmd->tx_size += ISCSI_CRC_LEN;
2568 pr_debug("Attaching CRC32C HeaderDigest to"
2569 " Async Message 0x%08x\n", *header_digest);
2572 cmd->iov_misc[0].iov_base = cmd->pdu;
2573 cmd->iov_misc[0].iov_len = cmd->tx_size;
2574 cmd->iov_misc_count = 1;
2576 pr_debug("Sending Connection Dropped Async Message StatSN:"
2577 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2578 cmd->logout_cid, conn->cid);
2579 return 0;
2582 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2584 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2585 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2586 wait_for_completion_interruptible_timeout(
2587 &conn->tx_half_close_comp,
2588 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2592 static void
2593 iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2594 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2595 bool set_statsn)
2597 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2598 hdr->flags = datain->flags;
2599 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2600 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2601 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2602 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2603 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2604 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2605 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2608 hton24(hdr->dlength, datain->length);
2609 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2610 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2611 (struct scsi_lun *)&hdr->lun);
2612 else
2613 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2615 hdr->itt = cmd->init_task_tag;
2617 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2618 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2619 else
2620 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2621 if (set_statsn)
2622 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2623 else
2624 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2626 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2627 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2628 hdr->datasn = cpu_to_be32(datain->data_sn);
2629 hdr->offset = cpu_to_be32(datain->offset);
2631 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2632 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2633 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2634 ntohl(hdr->offset), datain->length, conn->cid);
2637 static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2639 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
2640 struct iscsi_datain datain;
2641 struct iscsi_datain_req *dr;
2642 struct kvec *iov;
2643 u32 iov_count = 0, tx_size = 0;
2644 int eodr = 0, ret, iov_ret;
2645 bool set_statsn = false;
2647 memset(&datain, 0, sizeof(struct iscsi_datain));
2648 dr = iscsit_get_datain_values(cmd, &datain);
2649 if (!dr) {
2650 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2651 cmd->init_task_tag);
2652 return -1;
2655 * Be paranoid and double check the logic for now.
2657 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
2658 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2659 " datain.length: %u exceeds cmd->data_length: %u\n",
2660 cmd->init_task_tag, datain.offset, datain.length,
2661 cmd->se_cmd.data_length);
2662 return -1;
2665 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
2667 * Special case for successfully execution w/ both DATAIN
2668 * and Sense Data.
2670 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2671 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2672 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2673 else {
2674 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2675 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2676 iscsit_increment_maxcmdsn(cmd, conn->sess);
2677 cmd->stat_sn = conn->stat_sn++;
2678 set_statsn = true;
2679 } else if (dr->dr_complete ==
2680 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2681 set_statsn = true;
2684 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
2686 iov = &cmd->iov_data[0];
2687 iov[iov_count].iov_base = cmd->pdu;
2688 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2689 tx_size += ISCSI_HDR_LEN;
2691 if (conn->conn_ops->HeaderDigest) {
2692 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2694 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2695 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
2697 iov[0].iov_len += ISCSI_CRC_LEN;
2698 tx_size += ISCSI_CRC_LEN;
2700 pr_debug("Attaching CRC32 HeaderDigest"
2701 " for DataIN PDU 0x%08x\n", *header_digest);
2704 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2705 datain.offset, datain.length);
2706 if (iov_ret < 0)
2707 return -1;
2709 iov_count += iov_ret;
2710 tx_size += datain.length;
2712 cmd->padding = ((-datain.length) & 3);
2713 if (cmd->padding) {
2714 iov[iov_count].iov_base = cmd->pad_bytes;
2715 iov[iov_count++].iov_len = cmd->padding;
2716 tx_size += cmd->padding;
2718 pr_debug("Attaching %u padding bytes\n",
2719 cmd->padding);
2721 if (conn->conn_ops->DataDigest) {
2722 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2723 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2725 iov[iov_count].iov_base = &cmd->data_crc;
2726 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2727 tx_size += ISCSI_CRC_LEN;
2729 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2730 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2733 cmd->iov_data_count = iov_count;
2734 cmd->tx_size = tx_size;
2736 /* sendpage is preferred but can't insert markers */
2737 if (!conn->conn_ops->IFMarker)
2738 ret = iscsit_fe_sendpage_sg(cmd, conn);
2739 else
2740 ret = iscsit_send_tx_data(cmd, conn, 0);
2742 iscsit_unmap_iovec(cmd);
2744 if (ret < 0) {
2745 iscsit_tx_thread_wait_for_tcp(conn);
2746 return ret;
2749 if (dr->dr_complete) {
2750 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
2751 2 : 1;
2752 iscsit_free_datain_req(cmd, dr);
2755 return eodr;
2759 iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2760 struct iscsi_logout_rsp *hdr)
2762 struct iscsi_conn *logout_conn = NULL;
2763 struct iscsi_conn_recovery *cr = NULL;
2764 struct iscsi_session *sess = conn->sess;
2766 * The actual shutting down of Sessions and/or Connections
2767 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2768 * is done in scsi_logout_post_handler().
2770 switch (cmd->logout_reason) {
2771 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2772 pr_debug("iSCSI session logout successful, setting"
2773 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2774 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2775 break;
2776 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2777 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2778 break;
2780 * For CLOSECONNECTION logout requests carrying
2781 * a matching logout CID -> local CID, the reference
2782 * for the local CID will have been incremented in
2783 * iscsi_logout_closeconnection().
2785 * For CLOSECONNECTION logout requests carrying
2786 * a different CID than the connection it arrived
2787 * on, the connection responding to cmd->logout_cid
2788 * is stopped in iscsit_logout_post_handler_diffcid().
2791 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2792 " successful.\n", cmd->logout_cid, conn->cid);
2793 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2794 break;
2795 case ISCSI_LOGOUT_REASON_RECOVERY:
2796 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2797 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2798 break;
2800 * If the connection is still active from our point of view
2801 * force connection recovery to occur.
2803 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2804 cmd->logout_cid);
2805 if (logout_conn) {
2806 iscsit_connection_reinstatement_rcfr(logout_conn);
2807 iscsit_dec_conn_usage_count(logout_conn);
2810 cr = iscsit_get_inactive_connection_recovery_entry(
2811 conn->sess, cmd->logout_cid);
2812 if (!cr) {
2813 pr_err("Unable to locate CID: %hu for"
2814 " REMOVECONNFORRECOVERY Logout Request.\n",
2815 cmd->logout_cid);
2816 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2817 break;
2820 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2822 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2823 " for recovery for CID: %hu on CID: %hu successful.\n",
2824 cmd->logout_cid, conn->cid);
2825 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2826 break;
2827 default:
2828 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2829 cmd->logout_reason);
2830 return -1;
2833 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2834 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2835 hdr->response = cmd->logout_response;
2836 hdr->itt = cmd->init_task_tag;
2837 cmd->stat_sn = conn->stat_sn++;
2838 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2840 iscsit_increment_maxcmdsn(cmd, conn->sess);
2841 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2842 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2844 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2845 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2846 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2847 cmd->logout_cid, conn->cid);
2849 return 0;
2851 EXPORT_SYMBOL(iscsit_build_logout_rsp);
2853 static int
2854 iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2856 struct kvec *iov;
2857 int niov = 0, tx_size, rc;
2859 rc = iscsit_build_logout_rsp(cmd, conn,
2860 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2861 if (rc < 0)
2862 return rc;
2864 tx_size = ISCSI_HDR_LEN;
2865 iov = &cmd->iov_misc[0];
2866 iov[niov].iov_base = cmd->pdu;
2867 iov[niov++].iov_len = ISCSI_HDR_LEN;
2869 if (conn->conn_ops->HeaderDigest) {
2870 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2872 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2873 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
2875 iov[0].iov_len += ISCSI_CRC_LEN;
2876 tx_size += ISCSI_CRC_LEN;
2877 pr_debug("Attaching CRC32C HeaderDigest to"
2878 " Logout Response 0x%08x\n", *header_digest);
2880 cmd->iov_misc_count = niov;
2881 cmd->tx_size = tx_size;
2883 return 0;
2886 void
2887 iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2888 struct iscsi_nopin *hdr, bool nopout_response)
2890 hdr->opcode = ISCSI_OP_NOOP_IN;
2891 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2892 hton24(hdr->dlength, cmd->buf_ptr_size);
2893 if (nopout_response)
2894 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2895 hdr->itt = cmd->init_task_tag;
2896 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2897 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2898 conn->stat_sn;
2899 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2901 if (nopout_response)
2902 iscsit_increment_maxcmdsn(cmd, conn->sess);
2904 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2905 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2907 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2908 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2909 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2910 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2912 EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2915 * Unsolicited NOPIN, either requesting a response or not.
2917 static int iscsit_send_unsolicited_nopin(
2918 struct iscsi_cmd *cmd,
2919 struct iscsi_conn *conn,
2920 int want_response)
2922 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2923 int tx_size = ISCSI_HDR_LEN, ret;
2925 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
2927 if (conn->conn_ops->HeaderDigest) {
2928 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2930 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2931 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
2933 tx_size += ISCSI_CRC_LEN;
2934 pr_debug("Attaching CRC32C HeaderDigest to"
2935 " NopIN 0x%08x\n", *header_digest);
2938 cmd->iov_misc[0].iov_base = cmd->pdu;
2939 cmd->iov_misc[0].iov_len = tx_size;
2940 cmd->iov_misc_count = 1;
2941 cmd->tx_size = tx_size;
2943 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2944 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2946 ret = iscsit_send_tx_data(cmd, conn, 1);
2947 if (ret < 0) {
2948 iscsit_tx_thread_wait_for_tcp(conn);
2949 return ret;
2952 spin_lock_bh(&cmd->istate_lock);
2953 cmd->i_state = want_response ?
2954 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2955 spin_unlock_bh(&cmd->istate_lock);
2957 return 0;
2960 static int
2961 iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2963 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2964 struct kvec *iov;
2965 u32 padding = 0;
2966 int niov = 0, tx_size;
2968 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
2970 tx_size = ISCSI_HDR_LEN;
2971 iov = &cmd->iov_misc[0];
2972 iov[niov].iov_base = cmd->pdu;
2973 iov[niov++].iov_len = ISCSI_HDR_LEN;
2975 if (conn->conn_ops->HeaderDigest) {
2976 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2978 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2979 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
2981 iov[0].iov_len += ISCSI_CRC_LEN;
2982 tx_size += ISCSI_CRC_LEN;
2983 pr_debug("Attaching CRC32C HeaderDigest"
2984 " to NopIn 0x%08x\n", *header_digest);
2988 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2989 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2991 if (cmd->buf_ptr_size) {
2992 iov[niov].iov_base = cmd->buf_ptr;
2993 iov[niov++].iov_len = cmd->buf_ptr_size;
2994 tx_size += cmd->buf_ptr_size;
2996 pr_debug("Echoing back %u bytes of ping"
2997 " data.\n", cmd->buf_ptr_size);
2999 padding = ((-cmd->buf_ptr_size) & 3);
3000 if (padding != 0) {
3001 iov[niov].iov_base = &cmd->pad_bytes;
3002 iov[niov++].iov_len = padding;
3003 tx_size += padding;
3004 pr_debug("Attaching %u additional"
3005 " padding bytes.\n", padding);
3007 if (conn->conn_ops->DataDigest) {
3008 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3009 cmd->buf_ptr, cmd->buf_ptr_size,
3010 padding, (u8 *)&cmd->pad_bytes,
3011 (u8 *)&cmd->data_crc);
3013 iov[niov].iov_base = &cmd->data_crc;
3014 iov[niov++].iov_len = ISCSI_CRC_LEN;
3015 tx_size += ISCSI_CRC_LEN;
3016 pr_debug("Attached DataDigest for %u"
3017 " bytes of ping data, CRC 0x%08x\n",
3018 cmd->buf_ptr_size, cmd->data_crc);
3022 cmd->iov_misc_count = niov;
3023 cmd->tx_size = tx_size;
3025 return 0;
3028 static int iscsit_send_r2t(
3029 struct iscsi_cmd *cmd,
3030 struct iscsi_conn *conn)
3032 int tx_size = 0;
3033 struct iscsi_r2t *r2t;
3034 struct iscsi_r2t_rsp *hdr;
3035 int ret;
3037 r2t = iscsit_get_r2t_from_list(cmd);
3038 if (!r2t)
3039 return -1;
3041 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3042 memset(hdr, 0, ISCSI_HDR_LEN);
3043 hdr->opcode = ISCSI_OP_R2T;
3044 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3045 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3046 (struct scsi_lun *)&hdr->lun);
3047 hdr->itt = cmd->init_task_tag;
3048 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
3049 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3050 hdr->statsn = cpu_to_be32(conn->stat_sn);
3051 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3052 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3053 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3054 hdr->data_offset = cpu_to_be32(r2t->offset);
3055 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3057 cmd->iov_misc[0].iov_base = cmd->pdu;
3058 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3059 tx_size += ISCSI_HDR_LEN;
3061 if (conn->conn_ops->HeaderDigest) {
3062 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3064 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3065 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
3067 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3068 tx_size += ISCSI_CRC_LEN;
3069 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3070 " PDU 0x%08x\n", *header_digest);
3073 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3074 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3075 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3076 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3077 r2t->offset, r2t->xfer_len, conn->cid);
3079 cmd->iov_misc_count = 1;
3080 cmd->tx_size = tx_size;
3082 spin_lock_bh(&cmd->r2t_lock);
3083 r2t->sent_r2t = 1;
3084 spin_unlock_bh(&cmd->r2t_lock);
3086 ret = iscsit_send_tx_data(cmd, conn, 1);
3087 if (ret < 0) {
3088 iscsit_tx_thread_wait_for_tcp(conn);
3089 return ret;
3092 spin_lock_bh(&cmd->dataout_timeout_lock);
3093 iscsit_start_dataout_timer(cmd, conn);
3094 spin_unlock_bh(&cmd->dataout_timeout_lock);
3096 return 0;
3100 * @recovery: If called from iscsi_task_reassign_complete_write() for
3101 * connection recovery.
3103 int iscsit_build_r2ts_for_cmd(
3104 struct iscsi_conn *conn,
3105 struct iscsi_cmd *cmd,
3106 bool recovery)
3108 int first_r2t = 1;
3109 u32 offset = 0, xfer_len = 0;
3111 spin_lock_bh(&cmd->r2t_lock);
3112 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3113 spin_unlock_bh(&cmd->r2t_lock);
3114 return 0;
3117 if (conn->sess->sess_ops->DataSequenceInOrder &&
3118 !recovery)
3119 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
3121 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3122 if (conn->sess->sess_ops->DataSequenceInOrder) {
3123 offset = cmd->r2t_offset;
3125 if (first_r2t && recovery) {
3126 int new_data_end = offset +
3127 conn->sess->sess_ops->MaxBurstLength -
3128 cmd->next_burst_len;
3130 if (new_data_end > cmd->se_cmd.data_length)
3131 xfer_len = cmd->se_cmd.data_length - offset;
3132 else
3133 xfer_len =
3134 conn->sess->sess_ops->MaxBurstLength -
3135 cmd->next_burst_len;
3136 } else {
3137 int new_data_end = offset +
3138 conn->sess->sess_ops->MaxBurstLength;
3140 if (new_data_end > cmd->se_cmd.data_length)
3141 xfer_len = cmd->se_cmd.data_length - offset;
3142 else
3143 xfer_len = conn->sess->sess_ops->MaxBurstLength;
3145 cmd->r2t_offset += xfer_len;
3147 if (cmd->r2t_offset == cmd->se_cmd.data_length)
3148 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3149 } else {
3150 struct iscsi_seq *seq;
3152 seq = iscsit_get_seq_holder_for_r2t(cmd);
3153 if (!seq) {
3154 spin_unlock_bh(&cmd->r2t_lock);
3155 return -1;
3158 offset = seq->offset;
3159 xfer_len = seq->xfer_len;
3161 if (cmd->seq_send_order == cmd->seq_count)
3162 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3164 cmd->outstanding_r2ts++;
3165 first_r2t = 0;
3167 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3168 spin_unlock_bh(&cmd->r2t_lock);
3169 return -1;
3172 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3173 break;
3175 spin_unlock_bh(&cmd->r2t_lock);
3177 return 0;
3180 void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3181 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
3183 if (inc_stat_sn)
3184 cmd->stat_sn = conn->stat_sn++;
3186 atomic_long_inc(&conn->sess->rsp_pdus);
3188 memset(hdr, 0, ISCSI_HDR_LEN);
3189 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3190 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3191 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3192 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
3193 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3194 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3195 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
3196 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3198 hdr->response = cmd->iscsi_response;
3199 hdr->cmd_status = cmd->se_cmd.scsi_status;
3200 hdr->itt = cmd->init_task_tag;
3201 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3203 iscsit_increment_maxcmdsn(cmd, conn->sess);
3204 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3205 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3207 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3208 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3209 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3210 cmd->se_cmd.scsi_status, conn->cid);
3212 EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3214 static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3216 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3217 struct kvec *iov;
3218 u32 padding = 0, tx_size = 0;
3219 int iov_count = 0;
3220 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3222 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3224 iov = &cmd->iov_misc[0];
3225 iov[iov_count].iov_base = cmd->pdu;
3226 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3227 tx_size += ISCSI_HDR_LEN;
3230 * Attach SENSE DATA payload to iSCSI Response PDU
3232 if (cmd->se_cmd.sense_buffer &&
3233 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3234 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
3235 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3236 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3238 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
3239 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
3240 iov[iov_count].iov_base = cmd->sense_buffer;
3241 iov[iov_count++].iov_len =
3242 (cmd->se_cmd.scsi_sense_length + padding);
3243 tx_size += cmd->se_cmd.scsi_sense_length;
3245 if (padding) {
3246 memset(cmd->sense_buffer +
3247 cmd->se_cmd.scsi_sense_length, 0, padding);
3248 tx_size += padding;
3249 pr_debug("Adding %u bytes of padding to"
3250 " SENSE.\n", padding);
3253 if (conn->conn_ops->DataDigest) {
3254 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3255 cmd->sense_buffer,
3256 (cmd->se_cmd.scsi_sense_length + padding),
3257 0, NULL, (u8 *)&cmd->data_crc);
3259 iov[iov_count].iov_base = &cmd->data_crc;
3260 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3261 tx_size += ISCSI_CRC_LEN;
3263 pr_debug("Attaching CRC32 DataDigest for"
3264 " SENSE, %u bytes CRC 0x%08x\n",
3265 (cmd->se_cmd.scsi_sense_length + padding),
3266 cmd->data_crc);
3269 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3270 " Response PDU\n",
3271 cmd->se_cmd.scsi_sense_length);
3274 if (conn->conn_ops->HeaderDigest) {
3275 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3277 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3278 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
3280 iov[0].iov_len += ISCSI_CRC_LEN;
3281 tx_size += ISCSI_CRC_LEN;
3282 pr_debug("Attaching CRC32 HeaderDigest for Response"
3283 " PDU 0x%08x\n", *header_digest);
3286 cmd->iov_misc_count = iov_count;
3287 cmd->tx_size = tx_size;
3289 return 0;
3292 static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3294 switch (se_tmr->response) {
3295 case TMR_FUNCTION_COMPLETE:
3296 return ISCSI_TMF_RSP_COMPLETE;
3297 case TMR_TASK_DOES_NOT_EXIST:
3298 return ISCSI_TMF_RSP_NO_TASK;
3299 case TMR_LUN_DOES_NOT_EXIST:
3300 return ISCSI_TMF_RSP_NO_LUN;
3301 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3302 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3303 case TMR_FUNCTION_REJECTED:
3304 default:
3305 return ISCSI_TMF_RSP_REJECTED;
3309 void
3310 iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3311 struct iscsi_tm_rsp *hdr)
3313 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3315 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
3316 hdr->flags = ISCSI_FLAG_CMD_FINAL;
3317 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
3318 hdr->itt = cmd->init_task_tag;
3319 cmd->stat_sn = conn->stat_sn++;
3320 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3322 iscsit_increment_maxcmdsn(cmd, conn->sess);
3323 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3324 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3326 pr_debug("Built Task Management Response ITT: 0x%08x,"
3327 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3328 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3330 EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3332 static int
3333 iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3335 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3336 u32 tx_size = 0;
3338 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3340 cmd->iov_misc[0].iov_base = cmd->pdu;
3341 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3342 tx_size += ISCSI_HDR_LEN;
3344 if (conn->conn_ops->HeaderDigest) {
3345 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3347 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3348 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
3350 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3351 tx_size += ISCSI_CRC_LEN;
3352 pr_debug("Attaching CRC32 HeaderDigest for Task"
3353 " Mgmt Response PDU 0x%08x\n", *header_digest);
3356 cmd->iov_misc_count = 1;
3357 cmd->tx_size = tx_size;
3359 return 0;
3362 static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3364 bool ret = false;
3366 if (np->np_sockaddr.ss_family == AF_INET6) {
3367 const struct sockaddr_in6 sin6 = {
3368 .sin6_addr = IN6ADDR_ANY_INIT };
3369 struct sockaddr_in6 *sock_in6 =
3370 (struct sockaddr_in6 *)&np->np_sockaddr;
3372 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3373 sin6.sin6_addr.s6_addr, 16))
3374 ret = true;
3375 } else {
3376 struct sockaddr_in * sock_in =
3377 (struct sockaddr_in *)&np->np_sockaddr;
3379 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
3380 ret = true;
3383 return ret;
3386 #define SENDTARGETS_BUF_LIMIT 32768U
3388 static int
3389 iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
3390 enum iscsit_transport_type network_transport,
3391 int skip_bytes, bool *completed)
3393 char *payload = NULL;
3394 struct iscsi_conn *conn = cmd->conn;
3395 struct iscsi_portal_group *tpg;
3396 struct iscsi_tiqn *tiqn;
3397 struct iscsi_tpg_np *tpg_np;
3398 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
3399 int target_name_printed;
3400 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
3401 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
3403 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
3404 SENDTARGETS_BUF_LIMIT);
3406 payload = kzalloc(buffer_len, GFP_KERNEL);
3407 if (!payload) {
3408 pr_err("Unable to allocate memory for sendtargets"
3409 " response.\n");
3410 return -ENOMEM;
3413 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
3414 * explicit case..
3416 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
3417 text_ptr = strchr(text_in, '=');
3418 if (!text_ptr) {
3419 pr_err("Unable to locate '=' string in text_in:"
3420 " %s\n", text_in);
3421 kfree(payload);
3422 return -EINVAL;
3425 * Skip over '=' character..
3427 text_ptr += 1;
3430 spin_lock(&tiqn_lock);
3431 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3432 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
3433 strcmp(tiqn->tiqn, text_ptr)) {
3434 continue;
3437 target_name_printed = 0;
3439 spin_lock(&tiqn->tiqn_tpg_lock);
3440 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3442 /* If demo_mode_discovery=0 and generate_node_acls=0
3443 * (demo mode dislabed) do not return
3444 * TargetName+TargetAddress unless a NodeACL exists.
3447 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3448 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3449 (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg,
3450 cmd->conn->sess->sess_ops->InitiatorName))) {
3451 continue;
3454 spin_lock(&tpg->tpg_state_lock);
3455 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3456 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3457 spin_unlock(&tpg->tpg_state_lock);
3458 continue;
3460 spin_unlock(&tpg->tpg_state_lock);
3462 spin_lock(&tpg->tpg_np_lock);
3463 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3464 tpg_np_list) {
3465 struct iscsi_np *np = tpg_np->tpg_np;
3466 bool inaddr_any = iscsit_check_inaddr_any(np);
3468 if (np->np_network_transport != network_transport)
3469 continue;
3471 if (!target_name_printed) {
3472 len = sprintf(buf, "TargetName=%s",
3473 tiqn->tiqn);
3474 len += 1;
3476 if ((len + payload_len) > buffer_len) {
3477 spin_unlock(&tpg->tpg_np_lock);
3478 spin_unlock(&tiqn->tiqn_tpg_lock);
3479 end_of_buf = 1;
3480 goto eob;
3483 if (skip_bytes && len <= skip_bytes) {
3484 skip_bytes -= len;
3485 } else {
3486 memcpy(payload + payload_len, buf, len);
3487 payload_len += len;
3488 target_name_printed = 1;
3489 if (len > skip_bytes)
3490 skip_bytes = 0;
3494 if (inaddr_any) {
3495 len = sprintf(buf, "TargetAddress="
3496 "%s:%hu,%hu",
3497 conn->local_ip,
3498 np->np_port,
3499 tpg->tpgt);
3500 } else {
3501 len = sprintf(buf, "TargetAddress="
3502 "%pISpc,%hu",
3503 &np->np_sockaddr,
3504 tpg->tpgt);
3506 len += 1;
3508 if ((len + payload_len) > buffer_len) {
3509 spin_unlock(&tpg->tpg_np_lock);
3510 spin_unlock(&tiqn->tiqn_tpg_lock);
3511 end_of_buf = 1;
3512 goto eob;
3515 if (skip_bytes && len <= skip_bytes) {
3516 skip_bytes -= len;
3517 } else {
3518 memcpy(payload + payload_len, buf, len);
3519 payload_len += len;
3520 if (len > skip_bytes)
3521 skip_bytes = 0;
3524 spin_unlock(&tpg->tpg_np_lock);
3526 spin_unlock(&tiqn->tiqn_tpg_lock);
3527 eob:
3528 if (end_of_buf) {
3529 *completed = false;
3530 break;
3533 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
3534 break;
3536 spin_unlock(&tiqn_lock);
3538 cmd->buf_ptr = payload;
3540 return payload_len;
3544 iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3545 struct iscsi_text_rsp *hdr,
3546 enum iscsit_transport_type network_transport)
3548 int text_length, padding;
3549 bool completed = true;
3551 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3552 cmd->read_data_done,
3553 &completed);
3554 if (text_length < 0)
3555 return text_length;
3557 if (completed) {
3558 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3559 } else {
3560 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3561 cmd->read_data_done += text_length;
3562 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3563 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3565 hdr->opcode = ISCSI_OP_TEXT_RSP;
3566 padding = ((-text_length) & 3);
3567 hton24(hdr->dlength, text_length);
3568 hdr->itt = cmd->init_task_tag;
3569 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3570 cmd->stat_sn = conn->stat_sn++;
3571 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3573 iscsit_increment_maxcmdsn(cmd, conn->sess);
3575 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3576 * correctly increment MaxCmdSN for each response answering a
3577 * non immediate text request with a valid CmdSN.
3579 cmd->maxcmdsn_inc = 0;
3580 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3581 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3583 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3584 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3585 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3586 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3587 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
3589 return text_length + padding;
3591 EXPORT_SYMBOL(iscsit_build_text_rsp);
3593 static int iscsit_send_text_rsp(
3594 struct iscsi_cmd *cmd,
3595 struct iscsi_conn *conn)
3597 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
3598 struct kvec *iov;
3599 u32 tx_size = 0;
3600 int text_length, iov_count = 0, rc;
3602 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_TCP);
3603 if (rc < 0)
3604 return rc;
3606 text_length = rc;
3607 iov = &cmd->iov_misc[0];
3608 iov[iov_count].iov_base = cmd->pdu;
3609 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3610 iov[iov_count].iov_base = cmd->buf_ptr;
3611 iov[iov_count++].iov_len = text_length;
3613 tx_size += (ISCSI_HDR_LEN + text_length);
3615 if (conn->conn_ops->HeaderDigest) {
3616 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3618 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3619 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
3621 iov[0].iov_len += ISCSI_CRC_LEN;
3622 tx_size += ISCSI_CRC_LEN;
3623 pr_debug("Attaching CRC32 HeaderDigest for"
3624 " Text Response PDU 0x%08x\n", *header_digest);
3627 if (conn->conn_ops->DataDigest) {
3628 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3629 cmd->buf_ptr, text_length,
3630 0, NULL, (u8 *)&cmd->data_crc);
3632 iov[iov_count].iov_base = &cmd->data_crc;
3633 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3634 tx_size += ISCSI_CRC_LEN;
3636 pr_debug("Attaching DataDigest for %u bytes of text"
3637 " data, CRC 0x%08x\n", text_length,
3638 cmd->data_crc);
3641 cmd->iov_misc_count = iov_count;
3642 cmd->tx_size = tx_size;
3644 return 0;
3647 void
3648 iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3649 struct iscsi_reject *hdr)
3651 hdr->opcode = ISCSI_OP_REJECT;
3652 hdr->reason = cmd->reject_reason;
3653 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3654 hton24(hdr->dlength, ISCSI_HDR_LEN);
3655 hdr->ffffffff = cpu_to_be32(0xffffffff);
3656 cmd->stat_sn = conn->stat_sn++;
3657 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3658 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3659 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3662 EXPORT_SYMBOL(iscsit_build_reject);
3664 static int iscsit_send_reject(
3665 struct iscsi_cmd *cmd,
3666 struct iscsi_conn *conn)
3668 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
3669 struct kvec *iov;
3670 u32 iov_count = 0, tx_size;
3672 iscsit_build_reject(cmd, conn, hdr);
3674 iov = &cmd->iov_misc[0];
3675 iov[iov_count].iov_base = cmd->pdu;
3676 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3677 iov[iov_count].iov_base = cmd->buf_ptr;
3678 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3680 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3682 if (conn->conn_ops->HeaderDigest) {
3683 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3685 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3686 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
3688 iov[0].iov_len += ISCSI_CRC_LEN;
3689 tx_size += ISCSI_CRC_LEN;
3690 pr_debug("Attaching CRC32 HeaderDigest for"
3691 " REJECT PDU 0x%08x\n", *header_digest);
3694 if (conn->conn_ops->DataDigest) {
3695 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3696 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
3698 iov[iov_count].iov_base = &cmd->data_crc;
3699 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3700 tx_size += ISCSI_CRC_LEN;
3701 pr_debug("Attaching CRC32 DataDigest for REJECT"
3702 " PDU 0x%08x\n", cmd->data_crc);
3705 cmd->iov_misc_count = iov_count;
3706 cmd->tx_size = tx_size;
3708 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3709 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3711 return 0;
3714 void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3716 int ord, cpu;
3718 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3719 * within iscsit_start_kthreads()
3721 * Here we use bitmap_id to determine which CPU that this
3722 * iSCSI connection's RX/TX threads will be scheduled to
3723 * execute upon.
3725 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
3726 for_each_online_cpu(cpu) {
3727 if (ord-- == 0) {
3728 cpumask_set_cpu(cpu, conn->conn_cpumask);
3729 return;
3733 * This should never be reached..
3735 dump_stack();
3736 cpumask_setall(conn->conn_cpumask);
3739 static inline void iscsit_thread_check_cpumask(
3740 struct iscsi_conn *conn,
3741 struct task_struct *p,
3742 int mode)
3745 * mode == 1 signals iscsi_target_tx_thread() usage.
3746 * mode == 0 signals iscsi_target_rx_thread() usage.
3748 if (mode == 1) {
3749 if (!conn->conn_tx_reset_cpumask)
3750 return;
3751 conn->conn_tx_reset_cpumask = 0;
3752 } else {
3753 if (!conn->conn_rx_reset_cpumask)
3754 return;
3755 conn->conn_rx_reset_cpumask = 0;
3758 * Update the CPU mask for this single kthread so that
3759 * both TX and RX kthreads are scheduled to run on the
3760 * same CPU.
3762 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3765 static int
3766 iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3768 int ret;
3770 switch (state) {
3771 case ISTATE_SEND_R2T:
3772 ret = iscsit_send_r2t(cmd, conn);
3773 if (ret < 0)
3774 goto err;
3775 break;
3776 case ISTATE_REMOVE:
3777 spin_lock_bh(&conn->cmd_lock);
3778 list_del_init(&cmd->i_conn_node);
3779 spin_unlock_bh(&conn->cmd_lock);
3781 iscsit_free_cmd(cmd, false);
3782 break;
3783 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3784 iscsit_mod_nopin_response_timer(conn);
3785 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3786 if (ret < 0)
3787 goto err;
3788 break;
3789 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3790 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3791 if (ret < 0)
3792 goto err;
3793 break;
3794 default:
3795 pr_err("Unknown Opcode: 0x%02x ITT:"
3796 " 0x%08x, i_state: %d on CID: %hu\n",
3797 cmd->iscsi_opcode, cmd->init_task_tag, state,
3798 conn->cid);
3799 goto err;
3802 return 0;
3804 err:
3805 return -1;
3808 static int
3809 iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3811 struct iscsit_transport *t = conn->conn_transport;
3812 struct iscsi_queue_req *qr;
3813 struct iscsi_cmd *cmd;
3814 u8 state;
3815 int ret;
3817 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3818 atomic_set(&conn->check_immediate_queue, 0);
3819 cmd = qr->cmd;
3820 state = qr->state;
3821 kmem_cache_free(lio_qr_cache, qr);
3823 ret = t->iscsit_immediate_queue(conn, cmd, state);
3824 if (ret < 0)
3825 return ret;
3828 return 0;
3831 static int
3832 iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3834 int ret;
3836 check_rsp_state:
3837 switch (state) {
3838 case ISTATE_SEND_DATAIN:
3839 ret = iscsit_send_datain(cmd, conn);
3840 if (ret < 0)
3841 goto err;
3842 else if (!ret)
3843 /* more drs */
3844 goto check_rsp_state;
3845 else if (ret == 1) {
3846 /* all done */
3847 spin_lock_bh(&cmd->istate_lock);
3848 cmd->i_state = ISTATE_SENT_STATUS;
3849 spin_unlock_bh(&cmd->istate_lock);
3851 if (atomic_read(&conn->check_immediate_queue))
3852 return 1;
3854 return 0;
3855 } else if (ret == 2) {
3856 /* Still must send status,
3857 SCF_TRANSPORT_TASK_SENSE was set */
3858 spin_lock_bh(&cmd->istate_lock);
3859 cmd->i_state = ISTATE_SEND_STATUS;
3860 spin_unlock_bh(&cmd->istate_lock);
3861 state = ISTATE_SEND_STATUS;
3862 goto check_rsp_state;
3865 break;
3866 case ISTATE_SEND_STATUS:
3867 case ISTATE_SEND_STATUS_RECOVERY:
3868 ret = iscsit_send_response(cmd, conn);
3869 break;
3870 case ISTATE_SEND_LOGOUTRSP:
3871 ret = iscsit_send_logout(cmd, conn);
3872 break;
3873 case ISTATE_SEND_ASYNCMSG:
3874 ret = iscsit_send_conn_drop_async_message(
3875 cmd, conn);
3876 break;
3877 case ISTATE_SEND_NOPIN:
3878 ret = iscsit_send_nopin(cmd, conn);
3879 break;
3880 case ISTATE_SEND_REJECT:
3881 ret = iscsit_send_reject(cmd, conn);
3882 break;
3883 case ISTATE_SEND_TASKMGTRSP:
3884 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3885 if (ret != 0)
3886 break;
3887 ret = iscsit_tmr_post_handler(cmd, conn);
3888 if (ret != 0)
3889 iscsit_fall_back_to_erl0(conn->sess);
3890 break;
3891 case ISTATE_SEND_TEXTRSP:
3892 ret = iscsit_send_text_rsp(cmd, conn);
3893 break;
3894 default:
3895 pr_err("Unknown Opcode: 0x%02x ITT:"
3896 " 0x%08x, i_state: %d on CID: %hu\n",
3897 cmd->iscsi_opcode, cmd->init_task_tag,
3898 state, conn->cid);
3899 goto err;
3901 if (ret < 0)
3902 goto err;
3904 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3905 iscsit_tx_thread_wait_for_tcp(conn);
3906 iscsit_unmap_iovec(cmd);
3907 goto err;
3909 iscsit_unmap_iovec(cmd);
3911 switch (state) {
3912 case ISTATE_SEND_LOGOUTRSP:
3913 if (!iscsit_logout_post_handler(cmd, conn))
3914 return -ECONNRESET;
3915 /* fall through */
3916 case ISTATE_SEND_STATUS:
3917 case ISTATE_SEND_ASYNCMSG:
3918 case ISTATE_SEND_NOPIN:
3919 case ISTATE_SEND_STATUS_RECOVERY:
3920 case ISTATE_SEND_TEXTRSP:
3921 case ISTATE_SEND_TASKMGTRSP:
3922 case ISTATE_SEND_REJECT:
3923 spin_lock_bh(&cmd->istate_lock);
3924 cmd->i_state = ISTATE_SENT_STATUS;
3925 spin_unlock_bh(&cmd->istate_lock);
3926 break;
3927 default:
3928 pr_err("Unknown Opcode: 0x%02x ITT:"
3929 " 0x%08x, i_state: %d on CID: %hu\n",
3930 cmd->iscsi_opcode, cmd->init_task_tag,
3931 cmd->i_state, conn->cid);
3932 goto err;
3935 if (atomic_read(&conn->check_immediate_queue))
3936 return 1;
3938 return 0;
3940 err:
3941 return -1;
3944 static int iscsit_handle_response_queue(struct iscsi_conn *conn)
3946 struct iscsit_transport *t = conn->conn_transport;
3947 struct iscsi_queue_req *qr;
3948 struct iscsi_cmd *cmd;
3949 u8 state;
3950 int ret;
3952 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3953 cmd = qr->cmd;
3954 state = qr->state;
3955 kmem_cache_free(lio_qr_cache, qr);
3957 ret = t->iscsit_response_queue(conn, cmd, state);
3958 if (ret == 1 || ret < 0)
3959 return ret;
3962 return 0;
3965 int iscsi_target_tx_thread(void *arg)
3967 int ret = 0;
3968 struct iscsi_conn *conn = arg;
3970 * Allow ourselves to be interrupted by SIGINT so that a
3971 * connection recovery / failure event can be triggered externally.
3973 allow_signal(SIGINT);
3975 while (!kthread_should_stop()) {
3977 * Ensure that both TX and RX per connection kthreads
3978 * are scheduled to run on the same CPU.
3980 iscsit_thread_check_cpumask(conn, current, 1);
3982 wait_event_interruptible(conn->queues_wq,
3983 !iscsit_conn_all_queues_empty(conn));
3985 if (signal_pending(current))
3986 goto transport_err;
3988 get_immediate:
3989 ret = iscsit_handle_immediate_queue(conn);
3990 if (ret < 0)
3991 goto transport_err;
3993 ret = iscsit_handle_response_queue(conn);
3994 if (ret == 1)
3995 goto get_immediate;
3996 else if (ret == -ECONNRESET)
3997 goto out;
3998 else if (ret < 0)
3999 goto transport_err;
4002 transport_err:
4004 * Avoid the normal connection failure code-path if this connection
4005 * is still within LOGIN mode, and iscsi_np process context is
4006 * responsible for cleaning up the early connection failure.
4008 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
4009 iscsit_take_action_for_connection_exit(conn);
4010 out:
4011 return 0;
4014 static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
4016 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
4017 struct iscsi_cmd *cmd;
4018 int ret = 0;
4020 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
4021 case ISCSI_OP_SCSI_CMD:
4022 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4023 if (!cmd)
4024 goto reject;
4026 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
4027 break;
4028 case ISCSI_OP_SCSI_DATA_OUT:
4029 ret = iscsit_handle_data_out(conn, buf);
4030 break;
4031 case ISCSI_OP_NOOP_OUT:
4032 cmd = NULL;
4033 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
4034 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4035 if (!cmd)
4036 goto reject;
4038 ret = iscsit_handle_nop_out(conn, cmd, buf);
4039 break;
4040 case ISCSI_OP_SCSI_TMFUNC:
4041 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4042 if (!cmd)
4043 goto reject;
4045 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
4046 break;
4047 case ISCSI_OP_TEXT:
4048 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
4049 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
4050 if (!cmd)
4051 goto reject;
4052 } else {
4053 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4054 if (!cmd)
4055 goto reject;
4058 ret = iscsit_handle_text_cmd(conn, cmd, buf);
4059 break;
4060 case ISCSI_OP_LOGOUT:
4061 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4062 if (!cmd)
4063 goto reject;
4065 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4066 if (ret > 0)
4067 wait_for_completion_timeout(&conn->conn_logout_comp,
4068 SECONDS_FOR_LOGOUT_COMP * HZ);
4069 break;
4070 case ISCSI_OP_SNACK:
4071 ret = iscsit_handle_snack(conn, buf);
4072 break;
4073 default:
4074 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4075 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4076 pr_err("Cannot recover from unknown"
4077 " opcode while ERL=0, closing iSCSI connection.\n");
4078 return -1;
4080 if (!conn->conn_ops->OFMarker) {
4081 pr_err("Unable to recover from unknown"
4082 " opcode while OFMarker=No, closing iSCSI"
4083 " connection.\n");
4084 return -1;
4086 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4087 pr_err("Unable to recover from unknown"
4088 " opcode, closing iSCSI connection.\n");
4089 return -1;
4091 break;
4094 return ret;
4095 reject:
4096 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
4099 static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
4101 bool ret;
4103 spin_lock_bh(&conn->state_lock);
4104 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
4105 spin_unlock_bh(&conn->state_lock);
4107 return ret;
4110 int iscsi_target_rx_thread(void *arg)
4112 int ret, rc;
4113 u8 buffer[ISCSI_HDR_LEN], opcode;
4114 u32 checksum = 0, digest = 0;
4115 struct iscsi_conn *conn = arg;
4116 struct kvec iov;
4118 * Allow ourselves to be interrupted by SIGINT so that a
4119 * connection recovery / failure event can be triggered externally.
4121 allow_signal(SIGINT);
4123 * Wait for iscsi_post_login_handler() to complete before allowing
4124 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4126 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4127 if (rc < 0 || iscsi_target_check_conn_state(conn))
4128 return 0;
4130 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4131 struct completion comp;
4133 init_completion(&comp);
4134 rc = wait_for_completion_interruptible(&comp);
4135 if (rc < 0)
4136 goto transport_err;
4138 goto transport_err;
4141 while (!kthread_should_stop()) {
4143 * Ensure that both TX and RX per connection kthreads
4144 * are scheduled to run on the same CPU.
4146 iscsit_thread_check_cpumask(conn, current, 0);
4148 memset(buffer, 0, ISCSI_HDR_LEN);
4149 memset(&iov, 0, sizeof(struct kvec));
4151 iov.iov_base = buffer;
4152 iov.iov_len = ISCSI_HDR_LEN;
4154 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4155 if (ret != ISCSI_HDR_LEN) {
4156 iscsit_rx_thread_wait_for_tcp(conn);
4157 goto transport_err;
4160 if (conn->conn_ops->HeaderDigest) {
4161 iov.iov_base = &digest;
4162 iov.iov_len = ISCSI_CRC_LEN;
4164 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4165 if (ret != ISCSI_CRC_LEN) {
4166 iscsit_rx_thread_wait_for_tcp(conn);
4167 goto transport_err;
4170 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4171 buffer, ISCSI_HDR_LEN,
4172 0, NULL, (u8 *)&checksum);
4174 if (digest != checksum) {
4175 pr_err("HeaderDigest CRC32C failed,"
4176 " received 0x%08x, computed 0x%08x\n",
4177 digest, checksum);
4179 * Set the PDU to 0xff so it will intentionally
4180 * hit default in the switch below.
4182 memset(buffer, 0xff, ISCSI_HDR_LEN);
4183 atomic_long_inc(&conn->sess->conn_digest_errors);
4184 } else {
4185 pr_debug("Got HeaderDigest CRC32C"
4186 " 0x%08x\n", checksum);
4190 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4191 goto transport_err;
4193 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4195 if (conn->sess->sess_ops->SessionType &&
4196 ((!(opcode & ISCSI_OP_TEXT)) ||
4197 (!(opcode & ISCSI_OP_LOGOUT)))) {
4198 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4199 " while in Discovery Session, rejecting.\n", opcode);
4200 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4201 buffer);
4202 goto transport_err;
4205 ret = iscsi_target_rx_opcode(conn, buffer);
4206 if (ret < 0)
4207 goto transport_err;
4210 transport_err:
4211 if (!signal_pending(current))
4212 atomic_set(&conn->transport_failed, 1);
4213 iscsit_take_action_for_connection_exit(conn);
4214 return 0;
4217 static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4219 LIST_HEAD(tmp_list);
4220 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4221 struct iscsi_session *sess = conn->sess;
4223 * We expect this function to only ever be called from either RX or TX
4224 * thread context via iscsit_close_connection() once the other context
4225 * has been reset -> returned sleeping pre-handler state.
4227 spin_lock_bh(&conn->cmd_lock);
4228 list_splice_init(&conn->conn_cmd_list, &tmp_list);
4230 list_for_each_entry(cmd, &tmp_list, i_conn_node) {
4231 struct se_cmd *se_cmd = &cmd->se_cmd;
4233 if (se_cmd->se_tfo != NULL) {
4234 spin_lock(&se_cmd->t_state_lock);
4235 se_cmd->transport_state |= CMD_T_FABRIC_STOP;
4236 spin_unlock(&se_cmd->t_state_lock);
4239 spin_unlock_bh(&conn->cmd_lock);
4241 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4242 list_del_init(&cmd->i_conn_node);
4244 iscsit_increment_maxcmdsn(cmd, sess);
4245 iscsit_free_cmd(cmd, true);
4250 static void iscsit_stop_timers_for_cmds(
4251 struct iscsi_conn *conn)
4253 struct iscsi_cmd *cmd;
4255 spin_lock_bh(&conn->cmd_lock);
4256 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
4257 if (cmd->data_direction == DMA_TO_DEVICE)
4258 iscsit_stop_dataout_timer(cmd);
4260 spin_unlock_bh(&conn->cmd_lock);
4263 int iscsit_close_connection(
4264 struct iscsi_conn *conn)
4266 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4267 struct iscsi_session *sess = conn->sess;
4269 pr_debug("Closing iSCSI connection CID %hu on SID:"
4270 " %u\n", conn->cid, sess->sid);
4272 * Always up conn_logout_comp for the traditional TCP case just in case
4273 * the RX Thread in iscsi_target_rx_opcode() is sleeping and the logout
4274 * response never got sent because the connection failed.
4276 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4277 * to signal logout response TX interrupt completion. Go ahead and skip
4278 * this for iser since isert_rx_opcode() does not wait on logout failure,
4279 * and to avoid iscsi_conn pointer dereference in iser-target code.
4281 if (conn->conn_transport->transport_type == ISCSI_TCP)
4282 complete(&conn->conn_logout_comp);
4284 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4285 if (conn->tx_thread &&
4286 cmpxchg(&conn->tx_thread_active, true, false)) {
4287 send_sig(SIGINT, conn->tx_thread, 1);
4288 kthread_stop(conn->tx_thread);
4290 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4291 if (conn->rx_thread &&
4292 cmpxchg(&conn->rx_thread_active, true, false)) {
4293 send_sig(SIGINT, conn->rx_thread, 1);
4294 kthread_stop(conn->rx_thread);
4298 spin_lock(&iscsit_global->ts_bitmap_lock);
4299 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4300 get_order(1));
4301 spin_unlock(&iscsit_global->ts_bitmap_lock);
4303 iscsit_stop_timers_for_cmds(conn);
4304 iscsit_stop_nopin_response_timer(conn);
4305 iscsit_stop_nopin_timer(conn);
4307 if (conn->conn_transport->iscsit_wait_conn)
4308 conn->conn_transport->iscsit_wait_conn(conn);
4311 * During Connection recovery drop unacknowledged out of order
4312 * commands for this connection, and prepare the other commands
4313 * for realligence.
4315 * During normal operation clear the out of order commands (but
4316 * do not free the struct iscsi_ooo_cmdsn's) and release all
4317 * struct iscsi_cmds.
4319 if (atomic_read(&conn->connection_recovery)) {
4320 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4321 iscsit_prepare_cmds_for_realligance(conn);
4322 } else {
4323 iscsit_clear_ooo_cmdsns_for_conn(conn);
4324 iscsit_release_commands_from_conn(conn);
4326 iscsit_free_queue_reqs_for_conn(conn);
4329 * Handle decrementing session or connection usage count if
4330 * a logout response was not able to be sent because the
4331 * connection failed. Fall back to Session Recovery here.
4333 if (atomic_read(&conn->conn_logout_remove)) {
4334 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4335 iscsit_dec_conn_usage_count(conn);
4336 iscsit_dec_session_usage_count(sess);
4338 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4339 iscsit_dec_conn_usage_count(conn);
4341 atomic_set(&conn->conn_logout_remove, 0);
4342 atomic_set(&sess->session_reinstatement, 0);
4343 atomic_set(&sess->session_fall_back_to_erl0, 1);
4346 spin_lock_bh(&sess->conn_lock);
4347 list_del(&conn->conn_list);
4350 * Attempt to let the Initiator know this connection failed by
4351 * sending an Connection Dropped Async Message on another
4352 * active connection.
4354 if (atomic_read(&conn->connection_recovery))
4355 iscsit_build_conn_drop_async_message(conn);
4357 spin_unlock_bh(&sess->conn_lock);
4360 * If connection reinstatement is being performed on this connection,
4361 * up the connection reinstatement semaphore that is being blocked on
4362 * in iscsit_cause_connection_reinstatement().
4364 spin_lock_bh(&conn->state_lock);
4365 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4366 spin_unlock_bh(&conn->state_lock);
4367 complete(&conn->conn_wait_comp);
4368 wait_for_completion(&conn->conn_post_wait_comp);
4369 spin_lock_bh(&conn->state_lock);
4373 * If connection reinstatement is being performed on this connection
4374 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4375 * connection wait rcfr semaphore that is being blocked on
4376 * an iscsit_connection_reinstatement_rcfr().
4378 if (atomic_read(&conn->connection_wait_rcfr)) {
4379 spin_unlock_bh(&conn->state_lock);
4380 complete(&conn->conn_wait_rcfr_comp);
4381 wait_for_completion(&conn->conn_post_wait_comp);
4382 spin_lock_bh(&conn->state_lock);
4384 atomic_set(&conn->connection_reinstatement, 1);
4385 spin_unlock_bh(&conn->state_lock);
4388 * If any other processes are accessing this connection pointer we
4389 * must wait until they have completed.
4391 iscsit_check_conn_usage_count(conn);
4393 if (conn->conn_rx_hash.tfm)
4394 crypto_free_hash(conn->conn_rx_hash.tfm);
4395 if (conn->conn_tx_hash.tfm)
4396 crypto_free_hash(conn->conn_tx_hash.tfm);
4398 free_cpumask_var(conn->conn_cpumask);
4400 kfree(conn->conn_ops);
4401 conn->conn_ops = NULL;
4403 if (conn->sock)
4404 sock_release(conn->sock);
4406 if (conn->conn_transport->iscsit_free_conn)
4407 conn->conn_transport->iscsit_free_conn(conn);
4409 iscsit_put_transport(conn->conn_transport);
4411 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4412 conn->conn_state = TARG_CONN_STATE_FREE;
4413 kfree(conn);
4415 spin_lock_bh(&sess->conn_lock);
4416 atomic_dec(&sess->nconn);
4417 pr_debug("Decremented iSCSI connection count to %hu from node:"
4418 " %s\n", atomic_read(&sess->nconn),
4419 sess->sess_ops->InitiatorName);
4421 * Make sure that if one connection fails in an non ERL=2 iSCSI
4422 * Session that they all fail.
4424 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4425 !atomic_read(&sess->session_logout))
4426 atomic_set(&sess->session_fall_back_to_erl0, 1);
4429 * If this was not the last connection in the session, and we are
4430 * performing session reinstatement or falling back to ERL=0, call
4431 * iscsit_stop_session() without sleeping to shutdown the other
4432 * active connections.
4434 if (atomic_read(&sess->nconn)) {
4435 if (!atomic_read(&sess->session_reinstatement) &&
4436 !atomic_read(&sess->session_fall_back_to_erl0)) {
4437 spin_unlock_bh(&sess->conn_lock);
4438 return 0;
4440 if (!atomic_read(&sess->session_stop_active)) {
4441 atomic_set(&sess->session_stop_active, 1);
4442 spin_unlock_bh(&sess->conn_lock);
4443 iscsit_stop_session(sess, 0, 0);
4444 return 0;
4446 spin_unlock_bh(&sess->conn_lock);
4447 return 0;
4451 * If this was the last connection in the session and one of the
4452 * following is occurring:
4454 * Session Reinstatement is not being performed, and are falling back
4455 * to ERL=0 call iscsit_close_session().
4457 * Session Logout was requested. iscsit_close_session() will be called
4458 * elsewhere.
4460 * Session Continuation is not being performed, start the Time2Retain
4461 * handler and check if sleep_on_sess_wait_sem is active.
4463 if (!atomic_read(&sess->session_reinstatement) &&
4464 atomic_read(&sess->session_fall_back_to_erl0)) {
4465 spin_unlock_bh(&sess->conn_lock);
4466 target_put_session(sess->se_sess);
4468 return 0;
4469 } else if (atomic_read(&sess->session_logout)) {
4470 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4471 sess->session_state = TARG_SESS_STATE_FREE;
4472 spin_unlock_bh(&sess->conn_lock);
4474 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4475 complete(&sess->session_wait_comp);
4477 return 0;
4478 } else {
4479 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4480 sess->session_state = TARG_SESS_STATE_FAILED;
4482 if (!atomic_read(&sess->session_continuation)) {
4483 spin_unlock_bh(&sess->conn_lock);
4484 iscsit_start_time2retain_handler(sess);
4485 } else
4486 spin_unlock_bh(&sess->conn_lock);
4488 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4489 complete(&sess->session_wait_comp);
4491 return 0;
4493 spin_unlock_bh(&sess->conn_lock);
4495 return 0;
4498 int iscsit_close_session(struct iscsi_session *sess)
4500 struct iscsi_portal_group *tpg = sess->tpg;
4501 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4503 if (atomic_read(&sess->nconn)) {
4504 pr_err("%d connection(s) still exist for iSCSI session"
4505 " to %s\n", atomic_read(&sess->nconn),
4506 sess->sess_ops->InitiatorName);
4507 BUG();
4510 spin_lock_bh(&se_tpg->session_lock);
4511 atomic_set(&sess->session_logout, 1);
4512 atomic_set(&sess->session_reinstatement, 1);
4513 iscsit_stop_time2retain_timer(sess);
4514 spin_unlock_bh(&se_tpg->session_lock);
4517 * transport_deregister_session_configfs() will clear the
4518 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4519 * can be setting it again with __transport_register_session() in
4520 * iscsi_post_login_handler() again after the iscsit_stop_session()
4521 * completes in iscsi_np context.
4523 transport_deregister_session_configfs(sess->se_sess);
4526 * If any other processes are accessing this session pointer we must
4527 * wait until they have completed. If we are in an interrupt (the
4528 * time2retain handler) and contain and active session usage count we
4529 * restart the timer and exit.
4531 if (!in_interrupt()) {
4532 if (iscsit_check_session_usage_count(sess) == 1)
4533 iscsit_stop_session(sess, 1, 1);
4534 } else {
4535 if (iscsit_check_session_usage_count(sess) == 2) {
4536 atomic_set(&sess->session_logout, 0);
4537 iscsit_start_time2retain_handler(sess);
4538 return 0;
4542 transport_deregister_session(sess->se_sess);
4544 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4545 iscsit_free_connection_recovery_entires(sess);
4547 iscsit_free_all_ooo_cmdsns(sess);
4549 spin_lock_bh(&se_tpg->session_lock);
4550 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4551 sess->session_state = TARG_SESS_STATE_FREE;
4552 pr_debug("Released iSCSI session from node: %s\n",
4553 sess->sess_ops->InitiatorName);
4554 tpg->nsessions--;
4555 if (tpg->tpg_tiqn)
4556 tpg->tpg_tiqn->tiqn_nsessions--;
4558 pr_debug("Decremented number of active iSCSI Sessions on"
4559 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4561 spin_lock(&sess_idr_lock);
4562 idr_remove(&sess_idr, sess->session_index);
4563 spin_unlock(&sess_idr_lock);
4565 kfree(sess->sess_ops);
4566 sess->sess_ops = NULL;
4567 spin_unlock_bh(&se_tpg->session_lock);
4569 kfree(sess);
4570 return 0;
4573 static void iscsit_logout_post_handler_closesession(
4574 struct iscsi_conn *conn)
4576 struct iscsi_session *sess = conn->sess;
4577 int sleep = 1;
4579 * Traditional iscsi/tcp will invoke this logic from TX thread
4580 * context during session logout, so clear tx_thread_active and
4581 * sleep if iscsit_close_connection() has not already occured.
4583 * Since iser-target invokes this logic from it's own workqueue,
4584 * always sleep waiting for RX/TX thread shutdown to complete
4585 * within iscsit_close_connection().
4587 if (conn->conn_transport->transport_type == ISCSI_TCP)
4588 sleep = cmpxchg(&conn->tx_thread_active, true, false);
4590 atomic_set(&conn->conn_logout_remove, 0);
4591 complete(&conn->conn_logout_comp);
4593 iscsit_dec_conn_usage_count(conn);
4594 iscsit_stop_session(sess, sleep, sleep);
4595 iscsit_dec_session_usage_count(sess);
4596 target_put_session(sess->se_sess);
4599 static void iscsit_logout_post_handler_samecid(
4600 struct iscsi_conn *conn)
4602 int sleep = 1;
4604 if (conn->conn_transport->transport_type == ISCSI_TCP)
4605 sleep = cmpxchg(&conn->tx_thread_active, true, false);
4607 atomic_set(&conn->conn_logout_remove, 0);
4608 complete(&conn->conn_logout_comp);
4610 iscsit_cause_connection_reinstatement(conn, sleep);
4611 iscsit_dec_conn_usage_count(conn);
4614 static void iscsit_logout_post_handler_diffcid(
4615 struct iscsi_conn *conn,
4616 u16 cid)
4618 struct iscsi_conn *l_conn;
4619 struct iscsi_session *sess = conn->sess;
4620 bool conn_found = false;
4622 if (!sess)
4623 return;
4625 spin_lock_bh(&sess->conn_lock);
4626 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4627 if (l_conn->cid == cid) {
4628 iscsit_inc_conn_usage_count(l_conn);
4629 conn_found = true;
4630 break;
4633 spin_unlock_bh(&sess->conn_lock);
4635 if (!conn_found)
4636 return;
4638 if (l_conn->sock)
4639 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4641 spin_lock_bh(&l_conn->state_lock);
4642 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4643 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4644 spin_unlock_bh(&l_conn->state_lock);
4646 iscsit_cause_connection_reinstatement(l_conn, 1);
4647 iscsit_dec_conn_usage_count(l_conn);
4651 * Return of 0 causes the TX thread to restart.
4653 int iscsit_logout_post_handler(
4654 struct iscsi_cmd *cmd,
4655 struct iscsi_conn *conn)
4657 int ret = 0;
4659 switch (cmd->logout_reason) {
4660 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4661 switch (cmd->logout_response) {
4662 case ISCSI_LOGOUT_SUCCESS:
4663 case ISCSI_LOGOUT_CLEANUP_FAILED:
4664 default:
4665 iscsit_logout_post_handler_closesession(conn);
4666 break;
4668 ret = 0;
4669 break;
4670 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4671 if (conn->cid == cmd->logout_cid) {
4672 switch (cmd->logout_response) {
4673 case ISCSI_LOGOUT_SUCCESS:
4674 case ISCSI_LOGOUT_CLEANUP_FAILED:
4675 default:
4676 iscsit_logout_post_handler_samecid(conn);
4677 break;
4679 ret = 0;
4680 } else {
4681 switch (cmd->logout_response) {
4682 case ISCSI_LOGOUT_SUCCESS:
4683 iscsit_logout_post_handler_diffcid(conn,
4684 cmd->logout_cid);
4685 break;
4686 case ISCSI_LOGOUT_CID_NOT_FOUND:
4687 case ISCSI_LOGOUT_CLEANUP_FAILED:
4688 default:
4689 break;
4691 ret = 1;
4693 break;
4694 case ISCSI_LOGOUT_REASON_RECOVERY:
4695 switch (cmd->logout_response) {
4696 case ISCSI_LOGOUT_SUCCESS:
4697 case ISCSI_LOGOUT_CID_NOT_FOUND:
4698 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4699 case ISCSI_LOGOUT_CLEANUP_FAILED:
4700 default:
4701 break;
4703 ret = 1;
4704 break;
4705 default:
4706 break;
4709 return ret;
4711 EXPORT_SYMBOL(iscsit_logout_post_handler);
4713 void iscsit_fail_session(struct iscsi_session *sess)
4715 struct iscsi_conn *conn;
4717 spin_lock_bh(&sess->conn_lock);
4718 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4719 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4720 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4722 spin_unlock_bh(&sess->conn_lock);
4724 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4725 sess->session_state = TARG_SESS_STATE_FAILED;
4728 int iscsit_free_session(struct iscsi_session *sess)
4730 u16 conn_count = atomic_read(&sess->nconn);
4731 struct iscsi_conn *conn, *conn_tmp = NULL;
4732 int is_last;
4734 spin_lock_bh(&sess->conn_lock);
4735 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4737 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4738 conn_list) {
4739 if (conn_count == 0)
4740 break;
4742 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4743 is_last = 1;
4744 } else {
4745 iscsit_inc_conn_usage_count(conn_tmp);
4746 is_last = 0;
4748 iscsit_inc_conn_usage_count(conn);
4750 spin_unlock_bh(&sess->conn_lock);
4751 iscsit_cause_connection_reinstatement(conn, 1);
4752 spin_lock_bh(&sess->conn_lock);
4754 iscsit_dec_conn_usage_count(conn);
4755 if (is_last == 0)
4756 iscsit_dec_conn_usage_count(conn_tmp);
4758 conn_count--;
4761 if (atomic_read(&sess->nconn)) {
4762 spin_unlock_bh(&sess->conn_lock);
4763 wait_for_completion(&sess->session_wait_comp);
4764 } else
4765 spin_unlock_bh(&sess->conn_lock);
4767 target_put_session(sess->se_sess);
4768 return 0;
4771 void iscsit_stop_session(
4772 struct iscsi_session *sess,
4773 int session_sleep,
4774 int connection_sleep)
4776 u16 conn_count = atomic_read(&sess->nconn);
4777 struct iscsi_conn *conn, *conn_tmp = NULL;
4778 int is_last;
4780 spin_lock_bh(&sess->conn_lock);
4781 if (session_sleep)
4782 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4784 if (connection_sleep) {
4785 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4786 conn_list) {
4787 if (conn_count == 0)
4788 break;
4790 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4791 is_last = 1;
4792 } else {
4793 iscsit_inc_conn_usage_count(conn_tmp);
4794 is_last = 0;
4796 iscsit_inc_conn_usage_count(conn);
4798 spin_unlock_bh(&sess->conn_lock);
4799 iscsit_cause_connection_reinstatement(conn, 1);
4800 spin_lock_bh(&sess->conn_lock);
4802 iscsit_dec_conn_usage_count(conn);
4803 if (is_last == 0)
4804 iscsit_dec_conn_usage_count(conn_tmp);
4805 conn_count--;
4807 } else {
4808 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4809 iscsit_cause_connection_reinstatement(conn, 0);
4812 if (session_sleep && atomic_read(&sess->nconn)) {
4813 spin_unlock_bh(&sess->conn_lock);
4814 wait_for_completion(&sess->session_wait_comp);
4815 } else
4816 spin_unlock_bh(&sess->conn_lock);
4819 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4821 struct iscsi_session *sess;
4822 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4823 struct se_session *se_sess, *se_sess_tmp;
4824 LIST_HEAD(free_list);
4825 int session_count = 0;
4827 spin_lock_bh(&se_tpg->session_lock);
4828 if (tpg->nsessions && !force) {
4829 spin_unlock_bh(&se_tpg->session_lock);
4830 return -1;
4833 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4834 sess_list) {
4835 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4837 spin_lock(&sess->conn_lock);
4838 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4839 atomic_read(&sess->session_logout) ||
4840 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4841 spin_unlock(&sess->conn_lock);
4842 continue;
4844 atomic_set(&sess->session_reinstatement, 1);
4845 spin_unlock(&sess->conn_lock);
4847 list_move_tail(&se_sess->sess_list, &free_list);
4849 spin_unlock_bh(&se_tpg->session_lock);
4851 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4852 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4854 iscsit_free_session(sess);
4855 session_count++;
4858 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4859 " Group: %hu\n", session_count, tpg->tpgt);
4860 return 0;
4863 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4864 MODULE_VERSION("4.1.x");
4865 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4866 MODULE_LICENSE("GPL");
4868 module_init(iscsi_target_init_module);
4869 module_exit(iscsi_target_cleanup_module);