include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / target / iscsi / iscsi_target.c
blobc24fb10de60be4912962f9649feb2e21a96d231b
1 /*******************************************************************************
2 * This file contains main functions related to the iSCSI Target Core Driver.
4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 ******************************************************************************/
21 #include <linux/string.h>
22 #include <linux/kthread.h>
23 #include <linux/crypto.h>
24 #include <linux/completion.h>
25 #include <asm/unaligned.h>
26 #include <scsi/scsi_device.h>
27 #include <scsi/iscsi_proto.h>
28 #include <target/target_core_base.h>
29 #include <target/target_core_tmr.h>
30 #include <target/target_core_transport.h>
32 #include "iscsi_target_core.h"
33 #include "iscsi_target_parameters.h"
34 #include "iscsi_target_seq_pdu_list.h"
35 #include "iscsi_target_tq.h"
36 #include "iscsi_target_configfs.h"
37 #include "iscsi_target_datain_values.h"
38 #include "iscsi_target_erl0.h"
39 #include "iscsi_target_erl1.h"
40 #include "iscsi_target_erl2.h"
41 #include "iscsi_target_login.h"
42 #include "iscsi_target_tmr.h"
43 #include "iscsi_target_tpg.h"
44 #include "iscsi_target_util.h"
45 #include "iscsi_target.h"
46 #include "iscsi_target_device.h"
47 #include "iscsi_target_stat.h"
49 static LIST_HEAD(g_tiqn_list);
50 static LIST_HEAD(g_np_list);
51 static DEFINE_SPINLOCK(tiqn_lock);
52 static DEFINE_SPINLOCK(np_lock);
54 static struct idr tiqn_idr;
55 struct idr sess_idr;
56 struct mutex auth_id_lock;
57 spinlock_t sess_idr_lock;
59 struct iscsit_global *iscsit_global;
61 struct kmem_cache *lio_cmd_cache;
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 unsigned char *buf, u32);
69 static int iscsit_logout_post_handler(struct iscsi_cmd *, struct iscsi_conn *);
71 struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
73 struct iscsi_tiqn *tiqn = NULL;
75 spin_lock(&tiqn_lock);
76 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
77 if (!strcmp(tiqn->tiqn, buf)) {
79 spin_lock(&tiqn->tiqn_state_lock);
80 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
81 tiqn->tiqn_access_count++;
82 spin_unlock(&tiqn->tiqn_state_lock);
83 spin_unlock(&tiqn_lock);
84 return tiqn;
86 spin_unlock(&tiqn->tiqn_state_lock);
89 spin_unlock(&tiqn_lock);
91 return NULL;
94 static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
96 spin_lock(&tiqn->tiqn_state_lock);
97 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
98 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
99 spin_unlock(&tiqn->tiqn_state_lock);
100 return 0;
102 spin_unlock(&tiqn->tiqn_state_lock);
104 return -1;
107 void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
109 spin_lock(&tiqn->tiqn_state_lock);
110 tiqn->tiqn_access_count--;
111 spin_unlock(&tiqn->tiqn_state_lock);
115 * Note that IQN formatting is expected to be done in userspace, and
116 * no explict IQN format checks are done here.
118 struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
120 struct iscsi_tiqn *tiqn = NULL;
121 int ret;
123 if (strlen(buf) >= ISCSI_IQN_LEN) {
124 pr_err("Target IQN exceeds %d bytes\n",
125 ISCSI_IQN_LEN);
126 return ERR_PTR(-EINVAL);
129 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
130 if (!tiqn) {
131 pr_err("Unable to allocate struct iscsi_tiqn\n");
132 return ERR_PTR(-ENOMEM);
135 sprintf(tiqn->tiqn, "%s", buf);
136 INIT_LIST_HEAD(&tiqn->tiqn_list);
137 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
138 spin_lock_init(&tiqn->tiqn_state_lock);
139 spin_lock_init(&tiqn->tiqn_tpg_lock);
140 spin_lock_init(&tiqn->sess_err_stats.lock);
141 spin_lock_init(&tiqn->login_stats.lock);
142 spin_lock_init(&tiqn->logout_stats.lock);
144 if (!idr_pre_get(&tiqn_idr, GFP_KERNEL)) {
145 pr_err("idr_pre_get() for tiqn_idr failed\n");
146 kfree(tiqn);
147 return ERR_PTR(-ENOMEM);
149 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
151 spin_lock(&tiqn_lock);
152 ret = idr_get_new(&tiqn_idr, NULL, &tiqn->tiqn_index);
153 if (ret < 0) {
154 pr_err("idr_get_new() failed for tiqn->tiqn_index\n");
155 spin_unlock(&tiqn_lock);
156 kfree(tiqn);
157 return ERR_PTR(ret);
159 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
160 spin_unlock(&tiqn_lock);
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 if (np->np_login_tpg) {
219 pr_err("np->np_login_tpg() is not NULL!\n");
220 spin_unlock_bh(&np->np_thread_lock);
221 return -1;
223 spin_unlock_bh(&np->np_thread_lock);
225 * Determine if the portal group is accepting storage traffic.
227 spin_lock_bh(&tpg->tpg_state_lock);
228 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
229 spin_unlock_bh(&tpg->tpg_state_lock);
230 return -1;
232 spin_unlock_bh(&tpg->tpg_state_lock);
235 * Here we serialize access across the TIQN+TPG Tuple.
237 ret = mutex_lock_interruptible(&tpg->np_login_lock);
238 if ((ret != 0) || signal_pending(current))
239 return -1;
241 spin_lock_bh(&np->np_thread_lock);
242 np->np_login_tpg = tpg;
243 spin_unlock_bh(&np->np_thread_lock);
245 return 0;
248 int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
250 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
252 spin_lock_bh(&np->np_thread_lock);
253 np->np_login_tpg = NULL;
254 spin_unlock_bh(&np->np_thread_lock);
256 mutex_unlock(&tpg->np_login_lock);
258 if (tiqn)
259 iscsit_put_tiqn_for_login(tiqn);
261 return 0;
264 static struct iscsi_np *iscsit_get_np(
265 struct __kernel_sockaddr_storage *sockaddr,
266 int network_transport)
268 struct sockaddr_in *sock_in, *sock_in_e;
269 struct sockaddr_in6 *sock_in6, *sock_in6_e;
270 struct iscsi_np *np;
271 int ip_match = 0;
272 u16 port;
274 spin_lock_bh(&np_lock);
275 list_for_each_entry(np, &g_np_list, np_list) {
276 spin_lock(&np->np_thread_lock);
277 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
278 spin_unlock(&np->np_thread_lock);
279 continue;
282 if (sockaddr->ss_family == AF_INET6) {
283 sock_in6 = (struct sockaddr_in6 *)sockaddr;
284 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
286 if (!memcmp((void *)&sock_in6->sin6_addr.in6_u,
287 (void *)&sock_in6_e->sin6_addr.in6_u,
288 sizeof(struct in6_addr)))
289 ip_match = 1;
291 port = ntohs(sock_in6->sin6_port);
292 } else {
293 sock_in = (struct sockaddr_in *)sockaddr;
294 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
296 if (sock_in->sin_addr.s_addr ==
297 sock_in_e->sin_addr.s_addr)
298 ip_match = 1;
300 port = ntohs(sock_in->sin_port);
303 if ((ip_match == 1) && (np->np_port == port) &&
304 (np->np_network_transport == network_transport)) {
306 * Increment the np_exports reference count now to
307 * prevent iscsit_del_np() below from being called
308 * while iscsi_tpg_add_network_portal() is called.
310 np->np_exports++;
311 spin_unlock(&np->np_thread_lock);
312 spin_unlock_bh(&np_lock);
313 return np;
315 spin_unlock(&np->np_thread_lock);
317 spin_unlock_bh(&np_lock);
319 return NULL;
322 struct iscsi_np *iscsit_add_np(
323 struct __kernel_sockaddr_storage *sockaddr,
324 char *ip_str,
325 int network_transport)
327 struct sockaddr_in *sock_in;
328 struct sockaddr_in6 *sock_in6;
329 struct iscsi_np *np;
330 int ret;
332 * Locate the existing struct iscsi_np if already active..
334 np = iscsit_get_np(sockaddr, network_transport);
335 if (np)
336 return np;
338 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
339 if (!np) {
340 pr_err("Unable to allocate memory for struct iscsi_np\n");
341 return ERR_PTR(-ENOMEM);
344 np->np_flags |= NPF_IP_NETWORK;
345 if (sockaddr->ss_family == AF_INET6) {
346 sock_in6 = (struct sockaddr_in6 *)sockaddr;
347 snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str);
348 np->np_port = ntohs(sock_in6->sin6_port);
349 } else {
350 sock_in = (struct sockaddr_in *)sockaddr;
351 sprintf(np->np_ip, "%s", ip_str);
352 np->np_port = ntohs(sock_in->sin_port);
355 np->np_network_transport = network_transport;
356 spin_lock_init(&np->np_thread_lock);
357 init_completion(&np->np_restart_comp);
358 INIT_LIST_HEAD(&np->np_list);
360 ret = iscsi_target_setup_login_socket(np, sockaddr);
361 if (ret != 0) {
362 kfree(np);
363 return ERR_PTR(ret);
366 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
367 if (IS_ERR(np->np_thread)) {
368 pr_err("Unable to create kthread: iscsi_np\n");
369 ret = PTR_ERR(np->np_thread);
370 kfree(np);
371 return ERR_PTR(ret);
374 * Increment the np_exports reference count now to prevent
375 * iscsit_del_np() below from being run while a new call to
376 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
377 * active. We don't need to hold np->np_thread_lock at this
378 * point because iscsi_np has not been added to g_np_list yet.
380 np->np_exports = 1;
382 spin_lock_bh(&np_lock);
383 list_add_tail(&np->np_list, &g_np_list);
384 spin_unlock_bh(&np_lock);
386 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
387 np->np_ip, np->np_port, (np->np_network_transport == ISCSI_TCP) ?
388 "TCP" : "SCTP");
390 return np;
393 int iscsit_reset_np_thread(
394 struct iscsi_np *np,
395 struct iscsi_tpg_np *tpg_np,
396 struct iscsi_portal_group *tpg)
398 spin_lock_bh(&np->np_thread_lock);
399 if (tpg && tpg_np) {
401 * The reset operation need only be performed when the
402 * passed struct iscsi_portal_group has a login in progress
403 * to one of the network portals.
405 if (tpg_np->tpg_np->np_login_tpg != tpg) {
406 spin_unlock_bh(&np->np_thread_lock);
407 return 0;
410 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
411 spin_unlock_bh(&np->np_thread_lock);
412 return 0;
414 np->np_thread_state = ISCSI_NP_THREAD_RESET;
416 if (np->np_thread) {
417 spin_unlock_bh(&np->np_thread_lock);
418 send_sig(SIGINT, np->np_thread, 1);
419 wait_for_completion(&np->np_restart_comp);
420 spin_lock_bh(&np->np_thread_lock);
422 spin_unlock_bh(&np->np_thread_lock);
424 return 0;
427 int iscsit_del_np_comm(struct iscsi_np *np)
429 if (!np->np_socket)
430 return 0;
433 * Some network transports allocate their own struct sock->file,
434 * see if we need to free any additional allocated resources.
436 if (np->np_flags & NPF_SCTP_STRUCT_FILE) {
437 kfree(np->np_socket->file);
438 np->np_socket->file = NULL;
441 sock_release(np->np_socket);
442 return 0;
445 int iscsit_del_np(struct iscsi_np *np)
447 spin_lock_bh(&np->np_thread_lock);
448 np->np_exports--;
449 if (np->np_exports) {
450 spin_unlock_bh(&np->np_thread_lock);
451 return 0;
453 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
454 spin_unlock_bh(&np->np_thread_lock);
456 if (np->np_thread) {
458 * We need to send the signal to wakeup Linux/Net
459 * which may be sleeping in sock_accept()..
461 send_sig(SIGINT, np->np_thread, 1);
462 kthread_stop(np->np_thread);
464 iscsit_del_np_comm(np);
466 spin_lock_bh(&np_lock);
467 list_del(&np->np_list);
468 spin_unlock_bh(&np_lock);
470 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
471 np->np_ip, np->np_port, (np->np_network_transport == ISCSI_TCP) ?
472 "TCP" : "SCTP");
474 kfree(np);
475 return 0;
478 static int __init iscsi_target_init_module(void)
480 int ret = 0;
482 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
484 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
485 if (!iscsit_global) {
486 pr_err("Unable to allocate memory for iscsit_global\n");
487 return -1;
489 mutex_init(&auth_id_lock);
490 spin_lock_init(&sess_idr_lock);
491 idr_init(&tiqn_idr);
492 idr_init(&sess_idr);
494 ret = iscsi_target_register_configfs();
495 if (ret < 0)
496 goto out;
498 ret = iscsi_thread_set_init();
499 if (ret < 0)
500 goto configfs_out;
502 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
503 TARGET_THREAD_SET_COUNT) {
504 pr_err("iscsi_allocate_thread_sets() returned"
505 " unexpected value!\n");
506 goto ts_out1;
509 lio_cmd_cache = kmem_cache_create("lio_cmd_cache",
510 sizeof(struct iscsi_cmd), __alignof__(struct iscsi_cmd),
511 0, NULL);
512 if (!lio_cmd_cache) {
513 pr_err("Unable to kmem_cache_create() for"
514 " lio_cmd_cache\n");
515 goto ts_out2;
518 lio_qr_cache = kmem_cache_create("lio_qr_cache",
519 sizeof(struct iscsi_queue_req),
520 __alignof__(struct iscsi_queue_req), 0, NULL);
521 if (!lio_qr_cache) {
522 pr_err("nable to kmem_cache_create() for"
523 " lio_qr_cache\n");
524 goto cmd_out;
527 lio_dr_cache = kmem_cache_create("lio_dr_cache",
528 sizeof(struct iscsi_datain_req),
529 __alignof__(struct iscsi_datain_req), 0, NULL);
530 if (!lio_dr_cache) {
531 pr_err("Unable to kmem_cache_create() for"
532 " lio_dr_cache\n");
533 goto qr_out;
536 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
537 sizeof(struct iscsi_ooo_cmdsn),
538 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
539 if (!lio_ooo_cache) {
540 pr_err("Unable to kmem_cache_create() for"
541 " lio_ooo_cache\n");
542 goto dr_out;
545 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
546 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
547 0, NULL);
548 if (!lio_r2t_cache) {
549 pr_err("Unable to kmem_cache_create() for"
550 " lio_r2t_cache\n");
551 goto ooo_out;
554 if (iscsit_load_discovery_tpg() < 0)
555 goto r2t_out;
557 return ret;
558 r2t_out:
559 kmem_cache_destroy(lio_r2t_cache);
560 ooo_out:
561 kmem_cache_destroy(lio_ooo_cache);
562 dr_out:
563 kmem_cache_destroy(lio_dr_cache);
564 qr_out:
565 kmem_cache_destroy(lio_qr_cache);
566 cmd_out:
567 kmem_cache_destroy(lio_cmd_cache);
568 ts_out2:
569 iscsi_deallocate_thread_sets();
570 ts_out1:
571 iscsi_thread_set_free();
572 configfs_out:
573 iscsi_target_deregister_configfs();
574 out:
575 kfree(iscsit_global);
576 return -ENOMEM;
579 static void __exit iscsi_target_cleanup_module(void)
581 iscsi_deallocate_thread_sets();
582 iscsi_thread_set_free();
583 iscsit_release_discovery_tpg();
584 kmem_cache_destroy(lio_cmd_cache);
585 kmem_cache_destroy(lio_qr_cache);
586 kmem_cache_destroy(lio_dr_cache);
587 kmem_cache_destroy(lio_ooo_cache);
588 kmem_cache_destroy(lio_r2t_cache);
590 iscsi_target_deregister_configfs();
592 kfree(iscsit_global);
595 int iscsit_add_reject(
596 u8 reason,
597 int fail_conn,
598 unsigned char *buf,
599 struct iscsi_conn *conn)
601 struct iscsi_cmd *cmd;
602 struct iscsi_reject *hdr;
603 int ret;
605 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
606 if (!cmd)
607 return -1;
609 cmd->iscsi_opcode = ISCSI_OP_REJECT;
610 if (fail_conn)
611 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
613 hdr = (struct iscsi_reject *) cmd->pdu;
614 hdr->reason = reason;
616 cmd->buf_ptr = kzalloc(ISCSI_HDR_LEN, GFP_KERNEL);
617 if (!cmd->buf_ptr) {
618 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
619 iscsit_release_cmd(cmd);
620 return -1;
622 memcpy(cmd->buf_ptr, buf, ISCSI_HDR_LEN);
624 spin_lock_bh(&conn->cmd_lock);
625 list_add_tail(&cmd->i_list, &conn->conn_cmd_list);
626 spin_unlock_bh(&conn->cmd_lock);
628 cmd->i_state = ISTATE_SEND_REJECT;
629 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
631 ret = wait_for_completion_interruptible(&cmd->reject_comp);
632 if (ret != 0)
633 return -1;
635 return (!fail_conn) ? 0 : -1;
638 int iscsit_add_reject_from_cmd(
639 u8 reason,
640 int fail_conn,
641 int add_to_conn,
642 unsigned char *buf,
643 struct iscsi_cmd *cmd)
645 struct iscsi_conn *conn;
646 struct iscsi_reject *hdr;
647 int ret;
649 if (!cmd->conn) {
650 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
651 cmd->init_task_tag);
652 return -1;
654 conn = cmd->conn;
656 cmd->iscsi_opcode = ISCSI_OP_REJECT;
657 if (fail_conn)
658 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
660 hdr = (struct iscsi_reject *) cmd->pdu;
661 hdr->reason = reason;
663 cmd->buf_ptr = kzalloc(ISCSI_HDR_LEN, GFP_KERNEL);
664 if (!cmd->buf_ptr) {
665 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
666 iscsit_release_cmd(cmd);
667 return -1;
669 memcpy(cmd->buf_ptr, buf, ISCSI_HDR_LEN);
671 if (add_to_conn) {
672 spin_lock_bh(&conn->cmd_lock);
673 list_add_tail(&cmd->i_list, &conn->conn_cmd_list);
674 spin_unlock_bh(&conn->cmd_lock);
677 cmd->i_state = ISTATE_SEND_REJECT;
678 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
680 ret = wait_for_completion_interruptible(&cmd->reject_comp);
681 if (ret != 0)
682 return -1;
684 return (!fail_conn) ? 0 : -1;
688 * Map some portion of the allocated scatterlist to an iovec, suitable for
689 * kernel sockets to copy data in/out. This handles both pages and slab-allocated
690 * buffers, since we have been tricky and mapped t_mem_sg to the buffer in
691 * either case (see iscsit_alloc_buffs)
693 static int iscsit_map_iovec(
694 struct iscsi_cmd *cmd,
695 struct kvec *iov,
696 u32 data_offset,
697 u32 data_length)
699 u32 i = 0;
700 struct scatterlist *sg;
701 unsigned int page_off;
704 * We have a private mapping of the allocated pages in t_mem_sg.
705 * At this point, we also know each contains a page.
707 sg = &cmd->t_mem_sg[data_offset / PAGE_SIZE];
708 page_off = (data_offset % PAGE_SIZE);
710 cmd->first_data_sg = sg;
711 cmd->first_data_sg_off = page_off;
713 while (data_length) {
714 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
716 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
717 iov[i].iov_len = cur_len;
719 data_length -= cur_len;
720 page_off = 0;
721 sg = sg_next(sg);
722 i++;
725 cmd->kmapped_nents = i;
727 return i;
730 static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
732 u32 i;
733 struct scatterlist *sg;
735 sg = cmd->first_data_sg;
737 for (i = 0; i < cmd->kmapped_nents; i++)
738 kunmap(sg_page(&sg[i]));
741 static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
743 struct iscsi_cmd *cmd;
745 conn->exp_statsn = exp_statsn;
747 spin_lock_bh(&conn->cmd_lock);
748 list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
749 spin_lock(&cmd->istate_lock);
750 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
751 (cmd->stat_sn < exp_statsn)) {
752 cmd->i_state = ISTATE_REMOVE;
753 spin_unlock(&cmd->istate_lock);
754 iscsit_add_cmd_to_immediate_queue(cmd, conn,
755 cmd->i_state);
756 continue;
758 spin_unlock(&cmd->istate_lock);
760 spin_unlock_bh(&conn->cmd_lock);
763 static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
765 u32 iov_count = (cmd->se_cmd.t_data_nents == 0) ? 1 :
766 cmd->se_cmd.t_data_nents;
768 iov_count += TRANSPORT_IOV_DATA_BUFFER;
770 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
771 if (!cmd->iov_data) {
772 pr_err("Unable to allocate cmd->iov_data\n");
773 return -ENOMEM;
776 cmd->orig_iov_data_count = iov_count;
777 return 0;
780 static int iscsit_alloc_buffs(struct iscsi_cmd *cmd)
782 struct scatterlist *sgl;
783 u32 length = cmd->se_cmd.data_length;
784 int nents = DIV_ROUND_UP(length, PAGE_SIZE);
785 int i = 0, ret;
787 * If no SCSI payload is present, allocate the default iovecs used for
788 * iSCSI PDU Header
790 if (!length)
791 return iscsit_allocate_iovecs(cmd);
793 sgl = kzalloc(sizeof(*sgl) * nents, GFP_KERNEL);
794 if (!sgl)
795 return -ENOMEM;
797 sg_init_table(sgl, nents);
799 while (length) {
800 int buf_size = min_t(int, length, PAGE_SIZE);
801 struct page *page;
803 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
804 if (!page)
805 goto page_alloc_failed;
807 sg_set_page(&sgl[i], page, buf_size, 0);
809 length -= buf_size;
810 i++;
813 cmd->t_mem_sg = sgl;
814 cmd->t_mem_sg_nents = nents;
816 /* BIDI ops not supported */
818 /* Tell the core about our preallocated memory */
819 transport_generic_map_mem_to_cmd(&cmd->se_cmd, sgl, nents, NULL, 0);
821 * Allocate iovecs for SCSI payload after transport_generic_map_mem_to_cmd
822 * so that cmd->se_cmd.t_tasks_se_num has been set.
824 ret = iscsit_allocate_iovecs(cmd);
825 if (ret < 0)
826 goto page_alloc_failed;
828 return 0;
830 page_alloc_failed:
831 while (i >= 0) {
832 __free_page(sg_page(&sgl[i]));
833 i--;
835 kfree(cmd->t_mem_sg);
836 cmd->t_mem_sg = NULL;
837 return -ENOMEM;
840 static int iscsit_handle_scsi_cmd(
841 struct iscsi_conn *conn,
842 unsigned char *buf)
844 int data_direction, cmdsn_ret = 0, immed_ret, ret, transport_ret;
845 int dump_immediate_data = 0, send_check_condition = 0, payload_length;
846 struct iscsi_cmd *cmd = NULL;
847 struct iscsi_scsi_req *hdr;
849 spin_lock_bh(&conn->sess->session_stats_lock);
850 conn->sess->cmd_pdus++;
851 if (conn->sess->se_sess->se_node_acl) {
852 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
853 conn->sess->se_sess->se_node_acl->num_cmds++;
854 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
856 spin_unlock_bh(&conn->sess->session_stats_lock);
858 hdr = (struct iscsi_scsi_req *) buf;
859 payload_length = ntoh24(hdr->dlength);
860 hdr->itt = be32_to_cpu(hdr->itt);
861 hdr->data_length = be32_to_cpu(hdr->data_length);
862 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
863 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
865 /* FIXME; Add checks for AdditionalHeaderSegment */
867 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
868 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
869 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
870 " not set. Bad iSCSI Initiator.\n");
871 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
872 buf, conn);
875 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
876 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
878 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
879 * that adds support for RESERVE/RELEASE. There is a bug
880 * add with this new functionality that sets R/W bits when
881 * neither CDB carries any READ or WRITE datapayloads.
883 if ((hdr->cdb[0] == 0x16) || (hdr->cdb[0] == 0x17)) {
884 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
885 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
886 goto done;
889 pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
890 " set when Expected Data Transfer Length is 0 for"
891 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr->cdb[0]);
892 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
893 buf, conn);
895 done:
897 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
898 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
899 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
900 " MUST be set if Expected Data Transfer Length is not 0."
901 " Bad iSCSI Initiator\n");
902 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
903 buf, conn);
906 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
907 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
908 pr_err("Bidirectional operations not supported!\n");
909 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
910 buf, conn);
913 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
914 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
915 " Scsi Command PDU.\n");
916 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
917 buf, conn);
920 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
921 pr_err("ImmediateData=No but DataSegmentLength=%u,"
922 " protocol error.\n", payload_length);
923 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
924 buf, conn);
927 if ((hdr->data_length == payload_length) &&
928 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
929 pr_err("Expected Data Transfer Length and Length of"
930 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
931 " bit is not set protocol error\n");
932 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
933 buf, conn);
936 if (payload_length > hdr->data_length) {
937 pr_err("DataSegmentLength: %u is greater than"
938 " EDTL: %u, protocol error.\n", payload_length,
939 hdr->data_length);
940 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
941 buf, conn);
944 if (payload_length > conn->conn_ops->MaxRecvDataSegmentLength) {
945 pr_err("DataSegmentLength: %u is greater than"
946 " MaxRecvDataSegmentLength: %u, protocol error.\n",
947 payload_length, conn->conn_ops->MaxRecvDataSegmentLength);
948 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
949 buf, conn);
952 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
953 pr_err("DataSegmentLength: %u is greater than"
954 " FirstBurstLength: %u, protocol error.\n",
955 payload_length, conn->sess->sess_ops->FirstBurstLength);
956 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
957 buf, conn);
960 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
961 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
962 DMA_NONE;
964 cmd = iscsit_allocate_se_cmd(conn, hdr->data_length, data_direction,
965 (hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK));
966 if (!cmd)
967 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
968 buf, conn);
970 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
971 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
972 hdr->cmdsn, hdr->data_length, payload_length, conn->cid);
974 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
975 cmd->i_state = ISTATE_NEW_CMD;
976 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
977 cmd->immediate_data = (payload_length) ? 1 : 0;
978 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
979 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
980 if (cmd->unsolicited_data)
981 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
983 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
984 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
985 spin_lock_bh(&conn->sess->ttt_lock);
986 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
987 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
988 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
989 spin_unlock_bh(&conn->sess->ttt_lock);
990 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
991 cmd->targ_xfer_tag = 0xFFFFFFFF;
992 cmd->cmd_sn = hdr->cmdsn;
993 cmd->exp_stat_sn = hdr->exp_statsn;
994 cmd->first_burst_len = payload_length;
996 if (cmd->data_direction == DMA_FROM_DEVICE) {
997 struct iscsi_datain_req *dr;
999 dr = iscsit_allocate_datain_req();
1000 if (!dr)
1001 return iscsit_add_reject_from_cmd(
1002 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1003 1, 1, buf, cmd);
1005 iscsit_attach_datain_req(cmd, dr);
1009 * The CDB is going to an se_device_t.
1011 ret = iscsit_get_lun_for_cmd(cmd, hdr->cdb,
1012 get_unaligned_le64(&hdr->lun));
1013 if (ret < 0) {
1014 if (cmd->se_cmd.scsi_sense_reason == TCM_NON_EXISTENT_LUN) {
1015 pr_debug("Responding to non-acl'ed,"
1016 " non-existent or non-exported iSCSI LUN:"
1017 " 0x%016Lx\n", get_unaligned_le64(&hdr->lun));
1019 if (ret == PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES)
1020 return iscsit_add_reject_from_cmd(
1021 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1022 1, 1, buf, cmd);
1024 send_check_condition = 1;
1025 goto attach_cmd;
1028 * The Initiator Node has access to the LUN (the addressing method
1029 * is handled inside of iscsit_get_lun_for_cmd()). Now it's time to
1030 * allocate 1->N transport tasks (depending on sector count and
1031 * maximum request size the physical HBA(s) can handle.
1033 transport_ret = transport_generic_allocate_tasks(&cmd->se_cmd, hdr->cdb);
1034 if (transport_ret == -ENOMEM) {
1035 return iscsit_add_reject_from_cmd(
1036 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1037 1, 1, buf, cmd);
1038 } else if (transport_ret == -EINVAL) {
1040 * Unsupported SAM Opcode. CHECK_CONDITION will be sent
1041 * in iscsit_execute_cmd() during the CmdSN OOO Execution
1042 * Mechinism.
1044 send_check_condition = 1;
1045 } else {
1046 if (iscsit_decide_list_to_build(cmd, payload_length) < 0)
1047 return iscsit_add_reject_from_cmd(
1048 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1049 1, 1, buf, cmd);
1052 attach_cmd:
1053 spin_lock_bh(&conn->cmd_lock);
1054 list_add_tail(&cmd->i_list, &conn->conn_cmd_list);
1055 spin_unlock_bh(&conn->cmd_lock);
1057 * Check if we need to delay processing because of ALUA
1058 * Active/NonOptimized primary access state..
1060 core_alua_check_nonop_delay(&cmd->se_cmd);
1062 * Allocate and setup SGL used with transport_generic_map_mem_to_cmd().
1063 * also call iscsit_allocate_iovecs()
1065 ret = iscsit_alloc_buffs(cmd);
1066 if (ret < 0)
1067 return iscsit_add_reject_from_cmd(
1068 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1069 1, 1, buf, cmd);
1071 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1072 * the Immediate Bit is not set, and no Immediate
1073 * Data is attached.
1075 * A PDU/CmdSN carrying Immediate Data can only
1076 * be processed after the DataCRC has passed.
1077 * If the DataCRC fails, the CmdSN MUST NOT
1078 * be acknowledged. (See below)
1080 if (!cmd->immediate_data) {
1081 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1082 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1083 return iscsit_add_reject_from_cmd(
1084 ISCSI_REASON_PROTOCOL_ERROR,
1085 1, 0, buf, cmd);
1088 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
1091 * If no Immediate Data is attached, it's OK to return now.
1093 if (!cmd->immediate_data) {
1094 if (send_check_condition)
1095 return 0;
1097 if (cmd->unsolicited_data) {
1098 iscsit_set_dataout_sequence_values(cmd);
1100 spin_lock_bh(&cmd->dataout_timeout_lock);
1101 iscsit_start_dataout_timer(cmd, cmd->conn);
1102 spin_unlock_bh(&cmd->dataout_timeout_lock);
1105 return 0;
1109 * Early CHECK_CONDITIONs never make it to the transport processing
1110 * thread. They are processed in CmdSN order by
1111 * iscsit_check_received_cmdsn() below.
1113 if (send_check_condition) {
1114 immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1115 dump_immediate_data = 1;
1116 goto after_immediate_data;
1119 * Call directly into transport_generic_new_cmd() to perform
1120 * the backend memory allocation.
1122 ret = transport_generic_new_cmd(&cmd->se_cmd);
1123 if ((ret < 0) || (cmd->se_cmd.se_cmd_flags & SCF_SE_CMD_FAILED)) {
1124 immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1125 dump_immediate_data = 1;
1126 goto after_immediate_data;
1129 immed_ret = iscsit_handle_immediate_data(cmd, buf, payload_length);
1130 after_immediate_data:
1131 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1133 * A PDU/CmdSN carrying Immediate Data passed
1134 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1135 * Immediate Bit is not set.
1137 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1139 * Special case for Unsupported SAM WRITE Opcodes
1140 * and ImmediateData=Yes.
1142 if (dump_immediate_data) {
1143 if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
1144 return -1;
1145 } else if (cmd->unsolicited_data) {
1146 iscsit_set_dataout_sequence_values(cmd);
1148 spin_lock_bh(&cmd->dataout_timeout_lock);
1149 iscsit_start_dataout_timer(cmd, cmd->conn);
1150 spin_unlock_bh(&cmd->dataout_timeout_lock);
1153 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1154 return iscsit_add_reject_from_cmd(
1155 ISCSI_REASON_PROTOCOL_ERROR,
1156 1, 0, buf, cmd);
1158 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1160 * Immediate Data failed DataCRC and ERL>=1,
1161 * silently drop this PDU and let the initiator
1162 * plug the CmdSN gap.
1164 * FIXME: Send Unsolicited NOPIN with reserved
1165 * TTT here to help the initiator figure out
1166 * the missing CmdSN, although they should be
1167 * intelligent enough to determine the missing
1168 * CmdSN and issue a retry to plug the sequence.
1170 cmd->i_state = ISTATE_REMOVE;
1171 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1172 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1173 return -1;
1175 return 0;
1178 static u32 iscsit_do_crypto_hash_sg(
1179 struct hash_desc *hash,
1180 struct iscsi_cmd *cmd,
1181 u32 data_offset,
1182 u32 data_length,
1183 u32 padding,
1184 u8 *pad_bytes)
1186 u32 data_crc;
1187 u32 i;
1188 struct scatterlist *sg;
1189 unsigned int page_off;
1191 crypto_hash_init(hash);
1193 sg = cmd->first_data_sg;
1194 page_off = cmd->first_data_sg_off;
1196 i = 0;
1197 while (data_length) {
1198 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1200 crypto_hash_update(hash, &sg[i], cur_len);
1202 data_length -= cur_len;
1203 page_off = 0;
1204 i++;
1207 if (padding) {
1208 struct scatterlist pad_sg;
1210 sg_init_one(&pad_sg, pad_bytes, padding);
1211 crypto_hash_update(hash, &pad_sg, padding);
1213 crypto_hash_final(hash, (u8 *) &data_crc);
1215 return data_crc;
1218 static void iscsit_do_crypto_hash_buf(
1219 struct hash_desc *hash,
1220 unsigned char *buf,
1221 u32 payload_length,
1222 u32 padding,
1223 u8 *pad_bytes,
1224 u8 *data_crc)
1226 struct scatterlist sg;
1228 crypto_hash_init(hash);
1230 sg_init_one(&sg, (u8 *)buf, payload_length);
1231 crypto_hash_update(hash, &sg, payload_length);
1233 if (padding) {
1234 sg_init_one(&sg, pad_bytes, padding);
1235 crypto_hash_update(hash, &sg, padding);
1237 crypto_hash_final(hash, data_crc);
1240 static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1242 int iov_ret, ooo_cmdsn = 0, ret;
1243 u8 data_crc_failed = 0;
1244 u32 checksum, iov_count = 0, padding = 0, rx_got = 0;
1245 u32 rx_size = 0, payload_length;
1246 struct iscsi_cmd *cmd = NULL;
1247 struct se_cmd *se_cmd;
1248 struct iscsi_data *hdr;
1249 struct kvec *iov;
1250 unsigned long flags;
1252 hdr = (struct iscsi_data *) buf;
1253 payload_length = ntoh24(hdr->dlength);
1254 hdr->itt = be32_to_cpu(hdr->itt);
1255 hdr->ttt = be32_to_cpu(hdr->ttt);
1256 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
1257 hdr->datasn = be32_to_cpu(hdr->datasn);
1258 hdr->offset = be32_to_cpu(hdr->offset);
1260 if (!payload_length) {
1261 pr_err("DataOUT payload is ZERO, protocol error.\n");
1262 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1263 buf, conn);
1266 /* iSCSI write */
1267 spin_lock_bh(&conn->sess->session_stats_lock);
1268 conn->sess->rx_data_octets += payload_length;
1269 if (conn->sess->se_sess->se_node_acl) {
1270 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
1271 conn->sess->se_sess->se_node_acl->write_bytes += payload_length;
1272 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
1274 spin_unlock_bh(&conn->sess->session_stats_lock);
1276 if (payload_length > conn->conn_ops->MaxRecvDataSegmentLength) {
1277 pr_err("DataSegmentLength: %u is greater than"
1278 " MaxRecvDataSegmentLength: %u\n", payload_length,
1279 conn->conn_ops->MaxRecvDataSegmentLength);
1280 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1281 buf, conn);
1284 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1285 payload_length);
1286 if (!cmd)
1287 return 0;
1289 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1290 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1291 hdr->itt, hdr->ttt, hdr->datasn, hdr->offset,
1292 payload_length, conn->cid);
1294 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1295 pr_err("Command ITT: 0x%08x received DataOUT after"
1296 " last DataOUT received, dumping payload\n",
1297 cmd->init_task_tag);
1298 return iscsit_dump_data_payload(conn, payload_length, 1);
1301 if (cmd->data_direction != DMA_TO_DEVICE) {
1302 pr_err("Command ITT: 0x%08x received DataOUT for a"
1303 " NON-WRITE command.\n", cmd->init_task_tag);
1304 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
1305 1, 0, buf, cmd);
1307 se_cmd = &cmd->se_cmd;
1308 iscsit_mod_dataout_timer(cmd);
1310 if ((hdr->offset + payload_length) > cmd->data_length) {
1311 pr_err("DataOut Offset: %u, Length %u greater than"
1312 " iSCSI Command EDTL %u, protocol error.\n",
1313 hdr->offset, payload_length, cmd->data_length);
1314 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
1315 1, 0, buf, cmd);
1318 if (cmd->unsolicited_data) {
1319 int dump_unsolicited_data = 0;
1321 if (conn->sess->sess_ops->InitialR2T) {
1322 pr_err("Received unexpected unsolicited data"
1323 " while InitialR2T=Yes, protocol error.\n");
1324 transport_send_check_condition_and_sense(&cmd->se_cmd,
1325 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1326 return -1;
1329 * Special case for dealing with Unsolicited DataOUT
1330 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1331 * failures;
1334 /* Something's amiss if we're not in WRITE_PENDING state... */
1335 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1336 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1337 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1339 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1340 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) ||
1341 (se_cmd->se_cmd_flags & SCF_SE_CMD_FAILED))
1342 dump_unsolicited_data = 1;
1343 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1345 if (dump_unsolicited_data) {
1347 * Check if a delayed TASK_ABORTED status needs to
1348 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1349 * received with the unsolicitied data out.
1351 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1352 iscsit_stop_dataout_timer(cmd);
1354 transport_check_aborted_status(se_cmd,
1355 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1356 return iscsit_dump_data_payload(conn, payload_length, 1);
1358 } else {
1360 * For the normal solicited data path:
1362 * Check for a delayed TASK_ABORTED status and dump any
1363 * incoming data out payload if one exists. Also, when the
1364 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1365 * data out sequence, we decrement outstanding_r2ts. Once
1366 * outstanding_r2ts reaches zero, go ahead and send the delayed
1367 * TASK_ABORTED status.
1369 if (atomic_read(&se_cmd->t_transport_aborted) != 0) {
1370 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1371 if (--cmd->outstanding_r2ts < 1) {
1372 iscsit_stop_dataout_timer(cmd);
1373 transport_check_aborted_status(
1374 se_cmd, 1);
1377 return iscsit_dump_data_payload(conn, payload_length, 1);
1381 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1382 * within-command recovery checks before receiving the payload.
1384 ret = iscsit_check_pre_dataout(cmd, buf);
1385 if (ret == DATAOUT_WITHIN_COMMAND_RECOVERY)
1386 return 0;
1387 else if (ret == DATAOUT_CANNOT_RECOVER)
1388 return -1;
1390 rx_size += payload_length;
1391 iov = &cmd->iov_data[0];
1393 iov_ret = iscsit_map_iovec(cmd, iov, hdr->offset, payload_length);
1394 if (iov_ret < 0)
1395 return -1;
1397 iov_count += iov_ret;
1399 padding = ((-payload_length) & 3);
1400 if (padding != 0) {
1401 iov[iov_count].iov_base = cmd->pad_bytes;
1402 iov[iov_count++].iov_len = padding;
1403 rx_size += padding;
1404 pr_debug("Receiving %u padding bytes.\n", padding);
1407 if (conn->conn_ops->DataDigest) {
1408 iov[iov_count].iov_base = &checksum;
1409 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1410 rx_size += ISCSI_CRC_LEN;
1413 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1415 iscsit_unmap_iovec(cmd);
1417 if (rx_got != rx_size)
1418 return -1;
1420 if (conn->conn_ops->DataDigest) {
1421 u32 data_crc;
1423 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
1424 hdr->offset, payload_length, padding,
1425 cmd->pad_bytes);
1427 if (checksum != data_crc) {
1428 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1429 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1430 " does not match computed 0x%08x\n",
1431 hdr->itt, hdr->offset, payload_length,
1432 hdr->datasn, checksum, data_crc);
1433 data_crc_failed = 1;
1434 } else {
1435 pr_debug("Got CRC32C DataDigest 0x%08x for"
1436 " %u bytes of Data Out\n", checksum,
1437 payload_length);
1441 * Increment post receive data and CRC values or perform
1442 * within-command recovery.
1444 ret = iscsit_check_post_dataout(cmd, buf, data_crc_failed);
1445 if ((ret == DATAOUT_NORMAL) || (ret == DATAOUT_WITHIN_COMMAND_RECOVERY))
1446 return 0;
1447 else if (ret == DATAOUT_SEND_R2T) {
1448 iscsit_set_dataout_sequence_values(cmd);
1449 iscsit_build_r2ts_for_cmd(cmd, conn, 0);
1450 } else if (ret == DATAOUT_SEND_TO_TRANSPORT) {
1452 * Handle extra special case for out of order
1453 * Unsolicited Data Out.
1455 spin_lock_bh(&cmd->istate_lock);
1456 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1457 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1458 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1459 spin_unlock_bh(&cmd->istate_lock);
1461 iscsit_stop_dataout_timer(cmd);
1462 return (!ooo_cmdsn) ? transport_generic_handle_data(
1463 &cmd->se_cmd) : 0;
1464 } else /* DATAOUT_CANNOT_RECOVER */
1465 return -1;
1467 return 0;
1470 static int iscsit_handle_nop_out(
1471 struct iscsi_conn *conn,
1472 unsigned char *buf)
1474 unsigned char *ping_data = NULL;
1475 int cmdsn_ret, niov = 0, ret = 0, rx_got, rx_size;
1476 u32 checksum, data_crc, padding = 0, payload_length;
1477 u64 lun;
1478 struct iscsi_cmd *cmd = NULL;
1479 struct kvec *iov = NULL;
1480 struct iscsi_nopout *hdr;
1482 hdr = (struct iscsi_nopout *) buf;
1483 payload_length = ntoh24(hdr->dlength);
1484 lun = get_unaligned_le64(&hdr->lun);
1485 hdr->itt = be32_to_cpu(hdr->itt);
1486 hdr->ttt = be32_to_cpu(hdr->ttt);
1487 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
1488 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
1490 if ((hdr->itt == 0xFFFFFFFF) && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1491 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1492 " not set, protocol error.\n");
1493 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1494 buf, conn);
1497 if (payload_length > conn->conn_ops->MaxRecvDataSegmentLength) {
1498 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1499 " greater than MaxRecvDataSegmentLength: %u, protocol"
1500 " error.\n", payload_length,
1501 conn->conn_ops->MaxRecvDataSegmentLength);
1502 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1503 buf, conn);
1506 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%09x,"
1507 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1508 (hdr->itt == 0xFFFFFFFF) ? "Response" : "Request",
1509 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1510 payload_length);
1512 * This is not a response to a Unsolicited NopIN, which means
1513 * it can either be a NOPOUT ping request (with a valid ITT),
1514 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1515 * Either way, make sure we allocate an struct iscsi_cmd, as both
1516 * can contain ping data.
1518 if (hdr->ttt == 0xFFFFFFFF) {
1519 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1520 if (!cmd)
1521 return iscsit_add_reject(
1522 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1523 1, buf, conn);
1525 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1526 cmd->i_state = ISTATE_SEND_NOPIN;
1527 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1528 1 : 0);
1529 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1530 cmd->targ_xfer_tag = 0xFFFFFFFF;
1531 cmd->cmd_sn = hdr->cmdsn;
1532 cmd->exp_stat_sn = hdr->exp_statsn;
1533 cmd->data_direction = DMA_NONE;
1536 if (payload_length && (hdr->ttt == 0xFFFFFFFF)) {
1537 rx_size = payload_length;
1538 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1539 if (!ping_data) {
1540 pr_err("Unable to allocate memory for"
1541 " NOPOUT ping data.\n");
1542 ret = -1;
1543 goto out;
1546 iov = &cmd->iov_misc[0];
1547 iov[niov].iov_base = ping_data;
1548 iov[niov++].iov_len = payload_length;
1550 padding = ((-payload_length) & 3);
1551 if (padding != 0) {
1552 pr_debug("Receiving %u additional bytes"
1553 " for padding.\n", padding);
1554 iov[niov].iov_base = &cmd->pad_bytes;
1555 iov[niov++].iov_len = padding;
1556 rx_size += padding;
1558 if (conn->conn_ops->DataDigest) {
1559 iov[niov].iov_base = &checksum;
1560 iov[niov++].iov_len = ISCSI_CRC_LEN;
1561 rx_size += ISCSI_CRC_LEN;
1564 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1565 if (rx_got != rx_size) {
1566 ret = -1;
1567 goto out;
1570 if (conn->conn_ops->DataDigest) {
1571 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1572 ping_data, payload_length,
1573 padding, cmd->pad_bytes,
1574 (u8 *)&data_crc);
1576 if (checksum != data_crc) {
1577 pr_err("Ping data CRC32C DataDigest"
1578 " 0x%08x does not match computed 0x%08x\n",
1579 checksum, data_crc);
1580 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1581 pr_err("Unable to recover from"
1582 " NOPOUT Ping DataCRC failure while in"
1583 " ERL=0.\n");
1584 ret = -1;
1585 goto out;
1586 } else {
1588 * Silently drop this PDU and let the
1589 * initiator plug the CmdSN gap.
1591 pr_debug("Dropping NOPOUT"
1592 " Command CmdSN: 0x%08x due to"
1593 " DataCRC error.\n", hdr->cmdsn);
1594 ret = 0;
1595 goto out;
1597 } else {
1598 pr_debug("Got CRC32C DataDigest"
1599 " 0x%08x for %u bytes of ping data.\n",
1600 checksum, payload_length);
1604 ping_data[payload_length] = '\0';
1606 * Attach ping data to struct iscsi_cmd->buf_ptr.
1608 cmd->buf_ptr = (void *)ping_data;
1609 cmd->buf_ptr_size = payload_length;
1611 pr_debug("Got %u bytes of NOPOUT ping"
1612 " data.\n", payload_length);
1613 pr_debug("Ping Data: \"%s\"\n", ping_data);
1616 if (hdr->itt != 0xFFFFFFFF) {
1617 if (!cmd) {
1618 pr_err("Checking CmdSN for NOPOUT,"
1619 " but cmd is NULL!\n");
1620 return -1;
1623 * Initiator is expecting a NopIN ping reply,
1625 spin_lock_bh(&conn->cmd_lock);
1626 list_add_tail(&cmd->i_list, &conn->conn_cmd_list);
1627 spin_unlock_bh(&conn->cmd_lock);
1629 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
1631 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1632 iscsit_add_cmd_to_response_queue(cmd, conn,
1633 cmd->i_state);
1634 return 0;
1637 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1638 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1639 ret = 0;
1640 goto ping_out;
1642 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1643 return iscsit_add_reject_from_cmd(
1644 ISCSI_REASON_PROTOCOL_ERROR,
1645 1, 0, buf, cmd);
1647 return 0;
1650 if (hdr->ttt != 0xFFFFFFFF) {
1652 * This was a response to a unsolicited NOPIN ping.
1654 cmd = iscsit_find_cmd_from_ttt(conn, hdr->ttt);
1655 if (!cmd)
1656 return -1;
1658 iscsit_stop_nopin_response_timer(conn);
1660 cmd->i_state = ISTATE_REMOVE;
1661 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1662 iscsit_start_nopin_timer(conn);
1663 } else {
1665 * Initiator is not expecting a NOPIN is response.
1666 * Just ignore for now.
1668 * iSCSI v19-91 10.18
1669 * "A NOP-OUT may also be used to confirm a changed
1670 * ExpStatSN if another PDU will not be available
1671 * for a long time."
1673 ret = 0;
1674 goto out;
1677 return 0;
1678 out:
1679 if (cmd)
1680 iscsit_release_cmd(cmd);
1681 ping_out:
1682 kfree(ping_data);
1683 return ret;
1686 static int iscsit_handle_task_mgt_cmd(
1687 struct iscsi_conn *conn,
1688 unsigned char *buf)
1690 struct iscsi_cmd *cmd;
1691 struct se_tmr_req *se_tmr;
1692 struct iscsi_tmr_req *tmr_req;
1693 struct iscsi_tm *hdr;
1694 u32 payload_length;
1695 int out_of_order_cmdsn = 0;
1696 int ret;
1697 u8 function;
1699 hdr = (struct iscsi_tm *) buf;
1700 payload_length = ntoh24(hdr->dlength);
1701 hdr->itt = be32_to_cpu(hdr->itt);
1702 hdr->rtt = be32_to_cpu(hdr->rtt);
1703 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
1704 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
1705 hdr->refcmdsn = be32_to_cpu(hdr->refcmdsn);
1706 hdr->exp_datasn = be32_to_cpu(hdr->exp_datasn);
1707 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1708 function = hdr->flags;
1710 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1711 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1712 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1713 hdr->rtt, hdr->refcmdsn, conn->cid);
1715 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1716 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1717 (hdr->rtt != ISCSI_RESERVED_TAG))) {
1718 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1719 hdr->rtt = ISCSI_RESERVED_TAG;
1722 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1723 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1724 pr_err("Task Management Request TASK_REASSIGN not"
1725 " issued as immediate command, bad iSCSI Initiator"
1726 "implementation\n");
1727 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1728 buf, conn);
1730 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1731 (hdr->refcmdsn != ISCSI_RESERVED_TAG))
1732 hdr->refcmdsn = ISCSI_RESERVED_TAG;
1734 cmd = iscsit_allocate_se_cmd_for_tmr(conn, function);
1735 if (!cmd)
1736 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1737 1, buf, conn);
1739 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1740 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1741 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1742 cmd->init_task_tag = hdr->itt;
1743 cmd->targ_xfer_tag = 0xFFFFFFFF;
1744 cmd->cmd_sn = hdr->cmdsn;
1745 cmd->exp_stat_sn = hdr->exp_statsn;
1746 se_tmr = cmd->se_cmd.se_tmr_req;
1747 tmr_req = cmd->tmr_req;
1749 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1751 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1752 ret = iscsit_get_lun_for_tmr(cmd,
1753 get_unaligned_le64(&hdr->lun));
1754 if (ret < 0) {
1755 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1756 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1757 goto attach;
1761 switch (function) {
1762 case ISCSI_TM_FUNC_ABORT_TASK:
1763 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
1764 if (se_tmr->response != ISCSI_TMF_RSP_COMPLETE) {
1765 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1766 goto attach;
1768 break;
1769 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1770 case ISCSI_TM_FUNC_CLEAR_ACA:
1771 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1772 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1773 break;
1774 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1775 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
1776 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1777 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1778 goto attach;
1780 break;
1781 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1782 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
1783 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1784 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1785 goto attach;
1787 break;
1788 case ISCSI_TM_FUNC_TASK_REASSIGN:
1789 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1791 * Perform sanity checks on the ExpDataSN only if the
1792 * TASK_REASSIGN was successful.
1794 if (se_tmr->response != ISCSI_TMF_RSP_COMPLETE)
1795 break;
1797 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
1798 return iscsit_add_reject_from_cmd(
1799 ISCSI_REASON_BOOKMARK_INVALID, 1, 1,
1800 buf, cmd);
1801 break;
1802 default:
1803 pr_err("Unknown TMR function: 0x%02x, protocol"
1804 " error.\n", function);
1805 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1806 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1807 goto attach;
1810 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1811 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1812 se_tmr->call_transport = 1;
1813 attach:
1814 spin_lock_bh(&conn->cmd_lock);
1815 list_add_tail(&cmd->i_list, &conn->conn_cmd_list);
1816 spin_unlock_bh(&conn->cmd_lock);
1818 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1819 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1820 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1821 out_of_order_cmdsn = 1;
1822 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1823 return 0;
1824 } else { /* (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) */
1825 return iscsit_add_reject_from_cmd(
1826 ISCSI_REASON_PROTOCOL_ERROR,
1827 1, 0, buf, cmd);
1830 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
1832 if (out_of_order_cmdsn)
1833 return 0;
1835 * Found the referenced task, send to transport for processing.
1837 if (se_tmr->call_transport)
1838 return transport_generic_handle_tmr(&cmd->se_cmd);
1841 * Could not find the referenced LUN, task, or Task Management
1842 * command not authorized or supported. Change state and
1843 * let the tx_thread send the response.
1845 * For connection recovery, this is also the default action for
1846 * TMR TASK_REASSIGN.
1848 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1849 return 0;
1852 /* #warning FIXME: Support Text Command parameters besides SendTargets */
1853 static int iscsit_handle_text_cmd(
1854 struct iscsi_conn *conn,
1855 unsigned char *buf)
1857 char *text_ptr, *text_in;
1858 int cmdsn_ret, niov = 0, rx_got, rx_size;
1859 u32 checksum = 0, data_crc = 0, payload_length;
1860 u32 padding = 0, pad_bytes = 0, text_length = 0;
1861 struct iscsi_cmd *cmd;
1862 struct kvec iov[3];
1863 struct iscsi_text *hdr;
1865 hdr = (struct iscsi_text *) buf;
1866 payload_length = ntoh24(hdr->dlength);
1867 hdr->itt = be32_to_cpu(hdr->itt);
1868 hdr->ttt = be32_to_cpu(hdr->ttt);
1869 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
1870 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
1872 if (payload_length > conn->conn_ops->MaxRecvDataSegmentLength) {
1873 pr_err("Unable to accept text parameter length: %u"
1874 "greater than MaxRecvDataSegmentLength %u.\n",
1875 payload_length, conn->conn_ops->MaxRecvDataSegmentLength);
1876 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1877 buf, conn);
1880 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1881 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1882 hdr->exp_statsn, payload_length);
1884 rx_size = text_length = payload_length;
1885 if (text_length) {
1886 text_in = kzalloc(text_length, GFP_KERNEL);
1887 if (!text_in) {
1888 pr_err("Unable to allocate memory for"
1889 " incoming text parameters\n");
1890 return -1;
1893 memset(iov, 0, 3 * sizeof(struct kvec));
1894 iov[niov].iov_base = text_in;
1895 iov[niov++].iov_len = text_length;
1897 padding = ((-payload_length) & 3);
1898 if (padding != 0) {
1899 iov[niov].iov_base = &pad_bytes;
1900 iov[niov++].iov_len = padding;
1901 rx_size += padding;
1902 pr_debug("Receiving %u additional bytes"
1903 " for padding.\n", padding);
1905 if (conn->conn_ops->DataDigest) {
1906 iov[niov].iov_base = &checksum;
1907 iov[niov++].iov_len = ISCSI_CRC_LEN;
1908 rx_size += ISCSI_CRC_LEN;
1911 rx_got = rx_data(conn, &iov[0], niov, rx_size);
1912 if (rx_got != rx_size) {
1913 kfree(text_in);
1914 return -1;
1917 if (conn->conn_ops->DataDigest) {
1918 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1919 text_in, text_length,
1920 padding, (u8 *)&pad_bytes,
1921 (u8 *)&data_crc);
1923 if (checksum != data_crc) {
1924 pr_err("Text data CRC32C DataDigest"
1925 " 0x%08x does not match computed"
1926 " 0x%08x\n", checksum, data_crc);
1927 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1928 pr_err("Unable to recover from"
1929 " Text Data digest failure while in"
1930 " ERL=0.\n");
1931 kfree(text_in);
1932 return -1;
1933 } else {
1935 * Silently drop this PDU and let the
1936 * initiator plug the CmdSN gap.
1938 pr_debug("Dropping Text"
1939 " Command CmdSN: 0x%08x due to"
1940 " DataCRC error.\n", hdr->cmdsn);
1941 kfree(text_in);
1942 return 0;
1944 } else {
1945 pr_debug("Got CRC32C DataDigest"
1946 " 0x%08x for %u bytes of text data.\n",
1947 checksum, text_length);
1950 text_in[text_length - 1] = '\0';
1951 pr_debug("Successfully read %d bytes of text"
1952 " data.\n", text_length);
1954 if (strncmp("SendTargets", text_in, 11) != 0) {
1955 pr_err("Received Text Data that is not"
1956 " SendTargets, cannot continue.\n");
1957 kfree(text_in);
1958 return -1;
1960 text_ptr = strchr(text_in, '=');
1961 if (!text_ptr) {
1962 pr_err("No \"=\" separator found in Text Data,"
1963 " cannot continue.\n");
1964 kfree(text_in);
1965 return -1;
1967 if (strncmp("=All", text_ptr, 4) != 0) {
1968 pr_err("Unable to locate All value for"
1969 " SendTargets key, cannot continue.\n");
1970 kfree(text_in);
1971 return -1;
1973 /*#warning Support SendTargets=(iSCSI Target Name/Nothing) values. */
1974 kfree(text_in);
1977 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1978 if (!cmd)
1979 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1980 1, buf, conn);
1982 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1983 cmd->i_state = ISTATE_SEND_TEXTRSP;
1984 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1985 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1986 cmd->targ_xfer_tag = 0xFFFFFFFF;
1987 cmd->cmd_sn = hdr->cmdsn;
1988 cmd->exp_stat_sn = hdr->exp_statsn;
1989 cmd->data_direction = DMA_NONE;
1991 spin_lock_bh(&conn->cmd_lock);
1992 list_add_tail(&cmd->i_list, &conn->conn_cmd_list);
1993 spin_unlock_bh(&conn->cmd_lock);
1995 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
1997 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1998 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1999 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2000 return iscsit_add_reject_from_cmd(
2001 ISCSI_REASON_PROTOCOL_ERROR,
2002 1, 0, buf, cmd);
2004 return 0;
2007 return iscsit_execute_cmd(cmd, 0);
2010 int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2012 struct iscsi_conn *conn_p;
2013 struct iscsi_session *sess = conn->sess;
2015 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2016 " for SID: %u.\n", conn->cid, conn->sess->sid);
2018 atomic_set(&sess->session_logout, 1);
2019 atomic_set(&conn->conn_logout_remove, 1);
2020 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2022 iscsit_inc_conn_usage_count(conn);
2023 iscsit_inc_session_usage_count(sess);
2025 spin_lock_bh(&sess->conn_lock);
2026 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2027 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2028 continue;
2030 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2031 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2033 spin_unlock_bh(&sess->conn_lock);
2035 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2037 return 0;
2040 int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2042 struct iscsi_conn *l_conn;
2043 struct iscsi_session *sess = conn->sess;
2045 pr_debug("Received logout request CLOSECONNECTION for CID:"
2046 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2049 * A Logout Request with a CLOSECONNECTION reason code for a CID
2050 * can arrive on a connection with a differing CID.
2052 if (conn->cid == cmd->logout_cid) {
2053 spin_lock_bh(&conn->state_lock);
2054 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2055 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2057 atomic_set(&conn->conn_logout_remove, 1);
2058 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2059 iscsit_inc_conn_usage_count(conn);
2061 spin_unlock_bh(&conn->state_lock);
2062 } else {
2064 * Handle all different cid CLOSECONNECTION requests in
2065 * iscsit_logout_post_handler_diffcid() as to give enough
2066 * time for any non immediate command's CmdSN to be
2067 * acknowledged on the connection in question.
2069 * Here we simply make sure the CID is still around.
2071 l_conn = iscsit_get_conn_from_cid(sess,
2072 cmd->logout_cid);
2073 if (!l_conn) {
2074 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2075 iscsit_add_cmd_to_response_queue(cmd, conn,
2076 cmd->i_state);
2077 return 0;
2080 iscsit_dec_conn_usage_count(l_conn);
2083 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2085 return 0;
2088 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2090 struct iscsi_session *sess = conn->sess;
2092 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2093 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2095 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2096 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2097 " while ERL!=2.\n");
2098 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2099 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2100 return 0;
2103 if (conn->cid == cmd->logout_cid) {
2104 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2105 " with CID: %hu on CID: %hu, implementation error.\n",
2106 cmd->logout_cid, conn->cid);
2107 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2108 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2109 return 0;
2112 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2114 return 0;
2117 static int iscsit_handle_logout_cmd(
2118 struct iscsi_conn *conn,
2119 unsigned char *buf)
2121 int cmdsn_ret, logout_remove = 0;
2122 u8 reason_code = 0;
2123 struct iscsi_cmd *cmd;
2124 struct iscsi_logout *hdr;
2125 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2127 hdr = (struct iscsi_logout *) buf;
2128 reason_code = (hdr->flags & 0x7f);
2129 hdr->itt = be32_to_cpu(hdr->itt);
2130 hdr->cid = be16_to_cpu(hdr->cid);
2131 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
2132 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
2134 if (tiqn) {
2135 spin_lock(&tiqn->logout_stats.lock);
2136 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2137 tiqn->logout_stats.normal_logouts++;
2138 else
2139 tiqn->logout_stats.abnormal_logouts++;
2140 spin_unlock(&tiqn->logout_stats.lock);
2143 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2144 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2145 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2146 hdr->cid, conn->cid);
2148 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2149 pr_err("Received logout request on connection that"
2150 " is not in logged in state, ignoring request.\n");
2151 return 0;
2154 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
2155 if (!cmd)
2156 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
2157 buf, conn);
2159 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2160 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2161 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2162 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2163 cmd->targ_xfer_tag = 0xFFFFFFFF;
2164 cmd->cmd_sn = hdr->cmdsn;
2165 cmd->exp_stat_sn = hdr->exp_statsn;
2166 cmd->logout_cid = hdr->cid;
2167 cmd->logout_reason = reason_code;
2168 cmd->data_direction = DMA_NONE;
2171 * We need to sleep in these cases (by returning 1) until the Logout
2172 * Response gets sent in the tx thread.
2174 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2175 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
2176 (hdr->cid == conn->cid)))
2177 logout_remove = 1;
2179 spin_lock_bh(&conn->cmd_lock);
2180 list_add_tail(&cmd->i_list, &conn->conn_cmd_list);
2181 spin_unlock_bh(&conn->cmd_lock);
2183 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
2184 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
2187 * Immediate commands are executed, well, immediately.
2188 * Non-Immediate Logout Commands are executed in CmdSN order.
2190 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
2191 int ret = iscsit_execute_cmd(cmd, 0);
2193 if (ret < 0)
2194 return ret;
2195 } else {
2196 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
2197 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2198 logout_remove = 0;
2199 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
2200 return iscsit_add_reject_from_cmd(
2201 ISCSI_REASON_PROTOCOL_ERROR,
2202 1, 0, buf, cmd);
2206 return logout_remove;
2209 static int iscsit_handle_snack(
2210 struct iscsi_conn *conn,
2211 unsigned char *buf)
2213 u32 unpacked_lun;
2214 u64 lun;
2215 struct iscsi_snack *hdr;
2217 hdr = (struct iscsi_snack *) buf;
2218 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
2219 lun = get_unaligned_le64(&hdr->lun);
2220 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
2221 hdr->itt = be32_to_cpu(hdr->itt);
2222 hdr->ttt = be32_to_cpu(hdr->ttt);
2223 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
2224 hdr->begrun = be32_to_cpu(hdr->begrun);
2225 hdr->runlength = be32_to_cpu(hdr->runlength);
2227 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2228 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2229 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2230 hdr->begrun, hdr->runlength, conn->cid);
2232 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2233 pr_err("Initiator sent SNACK request while in"
2234 " ErrorRecoveryLevel=0.\n");
2235 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2236 buf, conn);
2239 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2240 * call from inside iscsi_send_recovery_datain_or_r2t().
2242 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2243 case 0:
2244 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
2245 hdr->itt, hdr->ttt, hdr->begrun, hdr->runlength);
2246 return 0;
2247 case ISCSI_FLAG_SNACK_TYPE_STATUS:
2248 return iscsit_handle_status_snack(conn, hdr->itt, hdr->ttt,
2249 hdr->begrun, hdr->runlength);
2250 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
2251 return iscsit_handle_data_ack(conn, hdr->ttt, hdr->begrun,
2252 hdr->runlength);
2253 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2254 /* FIXME: Support R-Data SNACK */
2255 pr_err("R-Data SNACK Not Supported.\n");
2256 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2257 buf, conn);
2258 default:
2259 pr_err("Unknown SNACK type 0x%02x, protocol"
2260 " error.\n", hdr->flags & 0x0f);
2261 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2262 buf, conn);
2265 return 0;
2268 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2270 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2271 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2272 wait_for_completion_interruptible_timeout(
2273 &conn->rx_half_close_comp,
2274 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2278 static int iscsit_handle_immediate_data(
2279 struct iscsi_cmd *cmd,
2280 unsigned char *buf,
2281 u32 length)
2283 int iov_ret, rx_got = 0, rx_size = 0;
2284 u32 checksum, iov_count = 0, padding = 0;
2285 struct iscsi_conn *conn = cmd->conn;
2286 struct kvec *iov;
2288 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2289 if (iov_ret < 0)
2290 return IMMEDIATE_DATA_CANNOT_RECOVER;
2292 rx_size = length;
2293 iov_count = iov_ret;
2294 iov = &cmd->iov_data[0];
2296 padding = ((-length) & 3);
2297 if (padding != 0) {
2298 iov[iov_count].iov_base = cmd->pad_bytes;
2299 iov[iov_count++].iov_len = padding;
2300 rx_size += padding;
2303 if (conn->conn_ops->DataDigest) {
2304 iov[iov_count].iov_base = &checksum;
2305 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2306 rx_size += ISCSI_CRC_LEN;
2309 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2311 iscsit_unmap_iovec(cmd);
2313 if (rx_got != rx_size) {
2314 iscsit_rx_thread_wait_for_tcp(conn);
2315 return IMMEDIATE_DATA_CANNOT_RECOVER;
2318 if (conn->conn_ops->DataDigest) {
2319 u32 data_crc;
2321 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2322 cmd->write_data_done, length, padding,
2323 cmd->pad_bytes);
2325 if (checksum != data_crc) {
2326 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2327 " does not match computed 0x%08x\n", checksum,
2328 data_crc);
2330 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2331 pr_err("Unable to recover from"
2332 " Immediate Data digest failure while"
2333 " in ERL=0.\n");
2334 iscsit_add_reject_from_cmd(
2335 ISCSI_REASON_DATA_DIGEST_ERROR,
2336 1, 0, buf, cmd);
2337 return IMMEDIATE_DATA_CANNOT_RECOVER;
2338 } else {
2339 iscsit_add_reject_from_cmd(
2340 ISCSI_REASON_DATA_DIGEST_ERROR,
2341 0, 0, buf, cmd);
2342 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2344 } else {
2345 pr_debug("Got CRC32C DataDigest 0x%08x for"
2346 " %u bytes of Immediate Data\n", checksum,
2347 length);
2351 cmd->write_data_done += length;
2353 if (cmd->write_data_done == cmd->data_length) {
2354 spin_lock_bh(&cmd->istate_lock);
2355 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2356 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2357 spin_unlock_bh(&cmd->istate_lock);
2360 return IMMEDIATE_DATA_NORMAL_OPERATION;
2364 * Called with sess->conn_lock held.
2366 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2367 with active network interface */
2368 static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2370 struct iscsi_cmd *cmd;
2371 struct iscsi_conn *conn_p;
2374 * Only send a Asynchronous Message on connections whos network
2375 * interface is still functional.
2377 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2378 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2379 iscsit_inc_conn_usage_count(conn_p);
2380 break;
2384 if (!conn_p)
2385 return;
2387 cmd = iscsit_allocate_cmd(conn_p, GFP_KERNEL);
2388 if (!cmd) {
2389 iscsit_dec_conn_usage_count(conn_p);
2390 return;
2393 cmd->logout_cid = conn->cid;
2394 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2395 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2397 spin_lock_bh(&conn_p->cmd_lock);
2398 list_add_tail(&cmd->i_list, &conn_p->conn_cmd_list);
2399 spin_unlock_bh(&conn_p->cmd_lock);
2401 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2402 iscsit_dec_conn_usage_count(conn_p);
2405 static int iscsit_send_conn_drop_async_message(
2406 struct iscsi_cmd *cmd,
2407 struct iscsi_conn *conn)
2409 struct iscsi_async *hdr;
2411 cmd->tx_size = ISCSI_HDR_LEN;
2412 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2414 hdr = (struct iscsi_async *) cmd->pdu;
2415 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2416 hdr->flags = ISCSI_FLAG_CMD_FINAL;
2417 cmd->init_task_tag = 0xFFFFFFFF;
2418 cmd->targ_xfer_tag = 0xFFFFFFFF;
2419 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2420 cmd->stat_sn = conn->stat_sn++;
2421 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2422 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2423 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2424 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2425 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2426 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2427 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2429 if (conn->conn_ops->HeaderDigest) {
2430 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2432 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2433 (unsigned char *)hdr, ISCSI_HDR_LEN,
2434 0, NULL, (u8 *)header_digest);
2436 cmd->tx_size += ISCSI_CRC_LEN;
2437 pr_debug("Attaching CRC32C HeaderDigest to"
2438 " Async Message 0x%08x\n", *header_digest);
2441 cmd->iov_misc[0].iov_base = cmd->pdu;
2442 cmd->iov_misc[0].iov_len = cmd->tx_size;
2443 cmd->iov_misc_count = 1;
2445 pr_debug("Sending Connection Dropped Async Message StatSN:"
2446 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2447 cmd->logout_cid, conn->cid);
2448 return 0;
2451 static int iscsit_send_data_in(
2452 struct iscsi_cmd *cmd,
2453 struct iscsi_conn *conn,
2454 int *eodr)
2456 int iov_ret = 0, set_statsn = 0;
2457 u32 iov_count = 0, tx_size = 0;
2458 struct iscsi_datain datain;
2459 struct iscsi_datain_req *dr;
2460 struct iscsi_data_rsp *hdr;
2461 struct kvec *iov;
2463 memset(&datain, 0, sizeof(struct iscsi_datain));
2464 dr = iscsit_get_datain_values(cmd, &datain);
2465 if (!dr) {
2466 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2467 cmd->init_task_tag);
2468 return -1;
2472 * Be paranoid and double check the logic for now.
2474 if ((datain.offset + datain.length) > cmd->data_length) {
2475 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2476 " datain.length: %u exceeds cmd->data_length: %u\n",
2477 cmd->init_task_tag, datain.offset, datain.length,
2478 cmd->data_length);
2479 return -1;
2482 spin_lock_bh(&conn->sess->session_stats_lock);
2483 conn->sess->tx_data_octets += datain.length;
2484 if (conn->sess->se_sess->se_node_acl) {
2485 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2486 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2487 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2489 spin_unlock_bh(&conn->sess->session_stats_lock);
2491 * Special case for successfully execution w/ both DATAIN
2492 * and Sense Data.
2494 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2495 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2496 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2497 else {
2498 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2499 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2500 iscsit_increment_maxcmdsn(cmd, conn->sess);
2501 cmd->stat_sn = conn->stat_sn++;
2502 set_statsn = 1;
2503 } else if (dr->dr_complete ==
2504 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2505 set_statsn = 1;
2508 hdr = (struct iscsi_data_rsp *) cmd->pdu;
2509 memset(hdr, 0, ISCSI_HDR_LEN);
2510 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2511 hdr->flags = datain.flags;
2512 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2513 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2514 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2515 hdr->residual_count = cpu_to_be32(cmd->residual_count);
2516 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2517 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2518 hdr->residual_count = cpu_to_be32(cmd->residual_count);
2521 hton24(hdr->dlength, datain.length);
2522 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2523 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2524 (struct scsi_lun *)&hdr->lun);
2525 else
2526 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2528 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2529 hdr->ttt = (hdr->flags & ISCSI_FLAG_DATA_ACK) ?
2530 cpu_to_be32(cmd->targ_xfer_tag) :
2531 0xFFFFFFFF;
2532 hdr->statsn = (set_statsn) ? cpu_to_be32(cmd->stat_sn) :
2533 0xFFFFFFFF;
2534 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2535 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2536 hdr->datasn = cpu_to_be32(datain.data_sn);
2537 hdr->offset = cpu_to_be32(datain.offset);
2539 iov = &cmd->iov_data[0];
2540 iov[iov_count].iov_base = cmd->pdu;
2541 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2542 tx_size += ISCSI_HDR_LEN;
2544 if (conn->conn_ops->HeaderDigest) {
2545 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2547 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2548 (unsigned char *)hdr, ISCSI_HDR_LEN,
2549 0, NULL, (u8 *)header_digest);
2551 iov[0].iov_len += ISCSI_CRC_LEN;
2552 tx_size += ISCSI_CRC_LEN;
2554 pr_debug("Attaching CRC32 HeaderDigest"
2555 " for DataIN PDU 0x%08x\n", *header_digest);
2558 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1], datain.offset, datain.length);
2559 if (iov_ret < 0)
2560 return -1;
2562 iov_count += iov_ret;
2563 tx_size += datain.length;
2565 cmd->padding = ((-datain.length) & 3);
2566 if (cmd->padding) {
2567 iov[iov_count].iov_base = cmd->pad_bytes;
2568 iov[iov_count++].iov_len = cmd->padding;
2569 tx_size += cmd->padding;
2571 pr_debug("Attaching %u padding bytes\n",
2572 cmd->padding);
2574 if (conn->conn_ops->DataDigest) {
2575 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2576 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2578 iov[iov_count].iov_base = &cmd->data_crc;
2579 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2580 tx_size += ISCSI_CRC_LEN;
2582 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2583 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2586 cmd->iov_data_count = iov_count;
2587 cmd->tx_size = tx_size;
2589 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2590 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2591 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2592 ntohl(hdr->offset), datain.length, conn->cid);
2594 if (dr->dr_complete) {
2595 *eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
2596 2 : 1;
2597 iscsit_free_datain_req(cmd, dr);
2600 return 0;
2603 static int iscsit_send_logout_response(
2604 struct iscsi_cmd *cmd,
2605 struct iscsi_conn *conn)
2607 int niov = 0, tx_size;
2608 struct iscsi_conn *logout_conn = NULL;
2609 struct iscsi_conn_recovery *cr = NULL;
2610 struct iscsi_session *sess = conn->sess;
2611 struct kvec *iov;
2612 struct iscsi_logout_rsp *hdr;
2614 * The actual shutting down of Sessions and/or Connections
2615 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2616 * is done in scsi_logout_post_handler().
2618 switch (cmd->logout_reason) {
2619 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2620 pr_debug("iSCSI session logout successful, setting"
2621 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2622 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2623 break;
2624 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2625 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2626 break;
2628 * For CLOSECONNECTION logout requests carrying
2629 * a matching logout CID -> local CID, the reference
2630 * for the local CID will have been incremented in
2631 * iscsi_logout_closeconnection().
2633 * For CLOSECONNECTION logout requests carrying
2634 * a different CID than the connection it arrived
2635 * on, the connection responding to cmd->logout_cid
2636 * is stopped in iscsit_logout_post_handler_diffcid().
2639 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2640 " successful.\n", cmd->logout_cid, conn->cid);
2641 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2642 break;
2643 case ISCSI_LOGOUT_REASON_RECOVERY:
2644 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2645 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2646 break;
2648 * If the connection is still active from our point of view
2649 * force connection recovery to occur.
2651 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2652 cmd->logout_cid);
2653 if ((logout_conn)) {
2654 iscsit_connection_reinstatement_rcfr(logout_conn);
2655 iscsit_dec_conn_usage_count(logout_conn);
2658 cr = iscsit_get_inactive_connection_recovery_entry(
2659 conn->sess, cmd->logout_cid);
2660 if (!cr) {
2661 pr_err("Unable to locate CID: %hu for"
2662 " REMOVECONNFORRECOVERY Logout Request.\n",
2663 cmd->logout_cid);
2664 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2665 break;
2668 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2670 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2671 " for recovery for CID: %hu on CID: %hu successful.\n",
2672 cmd->logout_cid, conn->cid);
2673 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2674 break;
2675 default:
2676 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2677 cmd->logout_reason);
2678 return -1;
2681 tx_size = ISCSI_HDR_LEN;
2682 hdr = (struct iscsi_logout_rsp *)cmd->pdu;
2683 memset(hdr, 0, ISCSI_HDR_LEN);
2684 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2685 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2686 hdr->response = cmd->logout_response;
2687 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2688 cmd->stat_sn = conn->stat_sn++;
2689 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2691 iscsit_increment_maxcmdsn(cmd, conn->sess);
2692 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2693 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2695 iov = &cmd->iov_misc[0];
2696 iov[niov].iov_base = cmd->pdu;
2697 iov[niov++].iov_len = ISCSI_HDR_LEN;
2699 if (conn->conn_ops->HeaderDigest) {
2700 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2702 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2703 (unsigned char *)hdr, ISCSI_HDR_LEN,
2704 0, NULL, (u8 *)header_digest);
2706 iov[0].iov_len += ISCSI_CRC_LEN;
2707 tx_size += ISCSI_CRC_LEN;
2708 pr_debug("Attaching CRC32C HeaderDigest to"
2709 " Logout Response 0x%08x\n", *header_digest);
2711 cmd->iov_misc_count = niov;
2712 cmd->tx_size = tx_size;
2714 pr_debug("Sending Logout Response ITT: 0x%08x StatSN:"
2715 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2716 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2717 cmd->logout_cid, conn->cid);
2719 return 0;
2723 * Unsolicited NOPIN, either requesting a response or not.
2725 static int iscsit_send_unsolicited_nopin(
2726 struct iscsi_cmd *cmd,
2727 struct iscsi_conn *conn,
2728 int want_response)
2730 int tx_size = ISCSI_HDR_LEN;
2731 struct iscsi_nopin *hdr;
2733 hdr = (struct iscsi_nopin *) cmd->pdu;
2734 memset(hdr, 0, ISCSI_HDR_LEN);
2735 hdr->opcode = ISCSI_OP_NOOP_IN;
2736 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2737 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2738 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2739 cmd->stat_sn = conn->stat_sn;
2740 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2741 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2742 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2744 if (conn->conn_ops->HeaderDigest) {
2745 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2747 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2748 (unsigned char *)hdr, ISCSI_HDR_LEN,
2749 0, NULL, (u8 *)header_digest);
2751 tx_size += ISCSI_CRC_LEN;
2752 pr_debug("Attaching CRC32C HeaderDigest to"
2753 " NopIN 0x%08x\n", *header_digest);
2756 cmd->iov_misc[0].iov_base = cmd->pdu;
2757 cmd->iov_misc[0].iov_len = tx_size;
2758 cmd->iov_misc_count = 1;
2759 cmd->tx_size = tx_size;
2761 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2762 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2764 return 0;
2767 static int iscsit_send_nopin_response(
2768 struct iscsi_cmd *cmd,
2769 struct iscsi_conn *conn)
2771 int niov = 0, tx_size;
2772 u32 padding = 0;
2773 struct kvec *iov;
2774 struct iscsi_nopin *hdr;
2776 tx_size = ISCSI_HDR_LEN;
2777 hdr = (struct iscsi_nopin *) cmd->pdu;
2778 memset(hdr, 0, ISCSI_HDR_LEN);
2779 hdr->opcode = ISCSI_OP_NOOP_IN;
2780 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2781 hton24(hdr->dlength, cmd->buf_ptr_size);
2782 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2783 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2784 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2785 cmd->stat_sn = conn->stat_sn++;
2786 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2788 iscsit_increment_maxcmdsn(cmd, conn->sess);
2789 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2790 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2792 iov = &cmd->iov_misc[0];
2793 iov[niov].iov_base = cmd->pdu;
2794 iov[niov++].iov_len = ISCSI_HDR_LEN;
2796 if (conn->conn_ops->HeaderDigest) {
2797 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2799 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2800 (unsigned char *)hdr, ISCSI_HDR_LEN,
2801 0, NULL, (u8 *)header_digest);
2803 iov[0].iov_len += ISCSI_CRC_LEN;
2804 tx_size += ISCSI_CRC_LEN;
2805 pr_debug("Attaching CRC32C HeaderDigest"
2806 " to NopIn 0x%08x\n", *header_digest);
2810 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2811 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2813 if (cmd->buf_ptr_size) {
2814 iov[niov].iov_base = cmd->buf_ptr;
2815 iov[niov++].iov_len = cmd->buf_ptr_size;
2816 tx_size += cmd->buf_ptr_size;
2818 pr_debug("Echoing back %u bytes of ping"
2819 " data.\n", cmd->buf_ptr_size);
2821 padding = ((-cmd->buf_ptr_size) & 3);
2822 if (padding != 0) {
2823 iov[niov].iov_base = &cmd->pad_bytes;
2824 iov[niov++].iov_len = padding;
2825 tx_size += padding;
2826 pr_debug("Attaching %u additional"
2827 " padding bytes.\n", padding);
2829 if (conn->conn_ops->DataDigest) {
2830 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2831 cmd->buf_ptr, cmd->buf_ptr_size,
2832 padding, (u8 *)&cmd->pad_bytes,
2833 (u8 *)&cmd->data_crc);
2835 iov[niov].iov_base = &cmd->data_crc;
2836 iov[niov++].iov_len = ISCSI_CRC_LEN;
2837 tx_size += ISCSI_CRC_LEN;
2838 pr_debug("Attached DataDigest for %u"
2839 " bytes of ping data, CRC 0x%08x\n",
2840 cmd->buf_ptr_size, cmd->data_crc);
2844 cmd->iov_misc_count = niov;
2845 cmd->tx_size = tx_size;
2847 pr_debug("Sending NOPIN Response ITT: 0x%08x, TTT:"
2848 " 0x%08x, StatSN: 0x%08x, Length %u\n", cmd->init_task_tag,
2849 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2851 return 0;
2854 int iscsit_send_r2t(
2855 struct iscsi_cmd *cmd,
2856 struct iscsi_conn *conn)
2858 int tx_size = 0;
2859 struct iscsi_r2t *r2t;
2860 struct iscsi_r2t_rsp *hdr;
2862 r2t = iscsit_get_r2t_from_list(cmd);
2863 if (!r2t)
2864 return -1;
2866 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
2867 memset(hdr, 0, ISCSI_HDR_LEN);
2868 hdr->opcode = ISCSI_OP_R2T;
2869 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2870 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2871 (struct scsi_lun *)&hdr->lun);
2872 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2873 spin_lock_bh(&conn->sess->ttt_lock);
2874 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2875 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
2876 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2877 spin_unlock_bh(&conn->sess->ttt_lock);
2878 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
2879 hdr->statsn = cpu_to_be32(conn->stat_sn);
2880 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2881 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2882 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
2883 hdr->data_offset = cpu_to_be32(r2t->offset);
2884 hdr->data_length = cpu_to_be32(r2t->xfer_len);
2886 cmd->iov_misc[0].iov_base = cmd->pdu;
2887 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
2888 tx_size += ISCSI_HDR_LEN;
2890 if (conn->conn_ops->HeaderDigest) {
2891 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2893 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2894 (unsigned char *)hdr, ISCSI_HDR_LEN,
2895 0, NULL, (u8 *)header_digest);
2897 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
2898 tx_size += ISCSI_CRC_LEN;
2899 pr_debug("Attaching CRC32 HeaderDigest for R2T"
2900 " PDU 0x%08x\n", *header_digest);
2903 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
2904 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
2905 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
2906 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
2907 r2t->offset, r2t->xfer_len, conn->cid);
2909 cmd->iov_misc_count = 1;
2910 cmd->tx_size = tx_size;
2912 spin_lock_bh(&cmd->r2t_lock);
2913 r2t->sent_r2t = 1;
2914 spin_unlock_bh(&cmd->r2t_lock);
2916 return 0;
2920 * type 0: Normal Operation.
2921 * type 1: Called from Storage Transport.
2922 * type 2: Called from iscsi_task_reassign_complete_write() for
2923 * connection recovery.
2925 int iscsit_build_r2ts_for_cmd(
2926 struct iscsi_cmd *cmd,
2927 struct iscsi_conn *conn,
2928 int type)
2930 int first_r2t = 1;
2931 u32 offset = 0, xfer_len = 0;
2933 spin_lock_bh(&cmd->r2t_lock);
2934 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
2935 spin_unlock_bh(&cmd->r2t_lock);
2936 return 0;
2939 if (conn->sess->sess_ops->DataSequenceInOrder && (type != 2))
2940 if (cmd->r2t_offset < cmd->write_data_done)
2941 cmd->r2t_offset = cmd->write_data_done;
2943 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
2944 if (conn->sess->sess_ops->DataSequenceInOrder) {
2945 offset = cmd->r2t_offset;
2947 if (first_r2t && (type == 2)) {
2948 xfer_len = ((offset +
2949 (conn->sess->sess_ops->MaxBurstLength -
2950 cmd->next_burst_len) >
2951 cmd->data_length) ?
2952 (cmd->data_length - offset) :
2953 (conn->sess->sess_ops->MaxBurstLength -
2954 cmd->next_burst_len));
2955 } else {
2956 xfer_len = ((offset +
2957 conn->sess->sess_ops->MaxBurstLength) >
2958 cmd->data_length) ?
2959 (cmd->data_length - offset) :
2960 conn->sess->sess_ops->MaxBurstLength;
2962 cmd->r2t_offset += xfer_len;
2964 if (cmd->r2t_offset == cmd->data_length)
2965 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
2966 } else {
2967 struct iscsi_seq *seq;
2969 seq = iscsit_get_seq_holder_for_r2t(cmd);
2970 if (!seq) {
2971 spin_unlock_bh(&cmd->r2t_lock);
2972 return -1;
2975 offset = seq->offset;
2976 xfer_len = seq->xfer_len;
2978 if (cmd->seq_send_order == cmd->seq_count)
2979 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
2981 cmd->outstanding_r2ts++;
2982 first_r2t = 0;
2984 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
2985 spin_unlock_bh(&cmd->r2t_lock);
2986 return -1;
2989 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
2990 break;
2992 spin_unlock_bh(&cmd->r2t_lock);
2994 return 0;
2997 static int iscsit_send_status(
2998 struct iscsi_cmd *cmd,
2999 struct iscsi_conn *conn)
3001 u8 iov_count = 0, recovery;
3002 u32 padding = 0, tx_size = 0;
3003 struct iscsi_scsi_rsp *hdr;
3004 struct kvec *iov;
3006 recovery = (cmd->i_state != ISTATE_SEND_STATUS);
3007 if (!recovery)
3008 cmd->stat_sn = conn->stat_sn++;
3010 spin_lock_bh(&conn->sess->session_stats_lock);
3011 conn->sess->rsp_pdus++;
3012 spin_unlock_bh(&conn->sess->session_stats_lock);
3014 hdr = (struct iscsi_scsi_rsp *) cmd->pdu;
3015 memset(hdr, 0, ISCSI_HDR_LEN);
3016 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3017 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3018 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3019 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
3020 hdr->residual_count = cpu_to_be32(cmd->residual_count);
3021 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3022 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
3023 hdr->residual_count = cpu_to_be32(cmd->residual_count);
3025 hdr->response = cmd->iscsi_response;
3026 hdr->cmd_status = cmd->se_cmd.scsi_status;
3027 hdr->itt = cpu_to_be32(cmd->init_task_tag);
3028 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3030 iscsit_increment_maxcmdsn(cmd, conn->sess);
3031 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3032 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3034 iov = &cmd->iov_misc[0];
3035 iov[iov_count].iov_base = cmd->pdu;
3036 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3037 tx_size += ISCSI_HDR_LEN;
3040 * Attach SENSE DATA payload to iSCSI Response PDU
3042 if (cmd->se_cmd.sense_buffer &&
3043 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3044 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
3045 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
3046 hton24(hdr->dlength, cmd->se_cmd.scsi_sense_length);
3047 iov[iov_count].iov_base = cmd->se_cmd.sense_buffer;
3048 iov[iov_count++].iov_len =
3049 (cmd->se_cmd.scsi_sense_length + padding);
3050 tx_size += cmd->se_cmd.scsi_sense_length;
3052 if (padding) {
3053 memset(cmd->se_cmd.sense_buffer +
3054 cmd->se_cmd.scsi_sense_length, 0, padding);
3055 tx_size += padding;
3056 pr_debug("Adding %u bytes of padding to"
3057 " SENSE.\n", padding);
3060 if (conn->conn_ops->DataDigest) {
3061 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3062 cmd->se_cmd.sense_buffer,
3063 (cmd->se_cmd.scsi_sense_length + padding),
3064 0, NULL, (u8 *)&cmd->data_crc);
3066 iov[iov_count].iov_base = &cmd->data_crc;
3067 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3068 tx_size += ISCSI_CRC_LEN;
3070 pr_debug("Attaching CRC32 DataDigest for"
3071 " SENSE, %u bytes CRC 0x%08x\n",
3072 (cmd->se_cmd.scsi_sense_length + padding),
3073 cmd->data_crc);
3076 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3077 " Response PDU\n",
3078 cmd->se_cmd.scsi_sense_length);
3081 if (conn->conn_ops->HeaderDigest) {
3082 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3084 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3085 (unsigned char *)hdr, ISCSI_HDR_LEN,
3086 0, NULL, (u8 *)header_digest);
3088 iov[0].iov_len += ISCSI_CRC_LEN;
3089 tx_size += ISCSI_CRC_LEN;
3090 pr_debug("Attaching CRC32 HeaderDigest for Response"
3091 " PDU 0x%08x\n", *header_digest);
3094 cmd->iov_misc_count = iov_count;
3095 cmd->tx_size = tx_size;
3097 pr_debug("Built %sSCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3098 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3099 (!recovery) ? "" : "Recovery ", cmd->init_task_tag,
3100 cmd->stat_sn, 0x00, cmd->se_cmd.scsi_status, conn->cid);
3102 return 0;
3105 static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3107 switch (se_tmr->response) {
3108 case TMR_FUNCTION_COMPLETE:
3109 return ISCSI_TMF_RSP_COMPLETE;
3110 case TMR_TASK_DOES_NOT_EXIST:
3111 return ISCSI_TMF_RSP_NO_TASK;
3112 case TMR_LUN_DOES_NOT_EXIST:
3113 return ISCSI_TMF_RSP_NO_LUN;
3114 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3115 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3116 case TMR_FUNCTION_AUTHORIZATION_FAILED:
3117 return ISCSI_TMF_RSP_AUTH_FAILED;
3118 case TMR_FUNCTION_REJECTED:
3119 default:
3120 return ISCSI_TMF_RSP_REJECTED;
3124 static int iscsit_send_task_mgt_rsp(
3125 struct iscsi_cmd *cmd,
3126 struct iscsi_conn *conn)
3128 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3129 struct iscsi_tm_rsp *hdr;
3130 u32 tx_size = 0;
3132 hdr = (struct iscsi_tm_rsp *) cmd->pdu;
3133 memset(hdr, 0, ISCSI_HDR_LEN);
3134 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
3135 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
3136 hdr->itt = cpu_to_be32(cmd->init_task_tag);
3137 cmd->stat_sn = conn->stat_sn++;
3138 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3140 iscsit_increment_maxcmdsn(cmd, conn->sess);
3141 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3142 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3144 cmd->iov_misc[0].iov_base = cmd->pdu;
3145 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3146 tx_size += ISCSI_HDR_LEN;
3148 if (conn->conn_ops->HeaderDigest) {
3149 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3151 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3152 (unsigned char *)hdr, ISCSI_HDR_LEN,
3153 0, NULL, (u8 *)header_digest);
3155 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3156 tx_size += ISCSI_CRC_LEN;
3157 pr_debug("Attaching CRC32 HeaderDigest for Task"
3158 " Mgmt Response PDU 0x%08x\n", *header_digest);
3161 cmd->iov_misc_count = 1;
3162 cmd->tx_size = tx_size;
3164 pr_debug("Built Task Management Response ITT: 0x%08x,"
3165 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3166 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3168 return 0;
3171 static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3173 char *payload = NULL;
3174 struct iscsi_conn *conn = cmd->conn;
3175 struct iscsi_portal_group *tpg;
3176 struct iscsi_tiqn *tiqn;
3177 struct iscsi_tpg_np *tpg_np;
3178 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
3179 unsigned char buf[256];
3181 buffer_len = (conn->conn_ops->MaxRecvDataSegmentLength > 32768) ?
3182 32768 : conn->conn_ops->MaxRecvDataSegmentLength;
3184 memset(buf, 0, 256);
3186 payload = kzalloc(buffer_len, GFP_KERNEL);
3187 if (!payload) {
3188 pr_err("Unable to allocate memory for sendtargets"
3189 " response.\n");
3190 return -ENOMEM;
3193 spin_lock(&tiqn_lock);
3194 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3195 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3196 len += 1;
3198 if ((len + payload_len) > buffer_len) {
3199 spin_unlock(&tiqn->tiqn_tpg_lock);
3200 end_of_buf = 1;
3201 goto eob;
3203 memcpy((void *)payload + payload_len, buf, len);
3204 payload_len += len;
3206 spin_lock(&tiqn->tiqn_tpg_lock);
3207 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3209 spin_lock(&tpg->tpg_state_lock);
3210 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3211 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3212 spin_unlock(&tpg->tpg_state_lock);
3213 continue;
3215 spin_unlock(&tpg->tpg_state_lock);
3217 spin_lock(&tpg->tpg_np_lock);
3218 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3219 tpg_np_list) {
3220 len = sprintf(buf, "TargetAddress="
3221 "%s%s%s:%hu,%hu",
3222 (tpg_np->tpg_np->np_sockaddr.ss_family == AF_INET6) ?
3223 "[" : "", tpg_np->tpg_np->np_ip,
3224 (tpg_np->tpg_np->np_sockaddr.ss_family == AF_INET6) ?
3225 "]" : "", tpg_np->tpg_np->np_port,
3226 tpg->tpgt);
3227 len += 1;
3229 if ((len + payload_len) > buffer_len) {
3230 spin_unlock(&tpg->tpg_np_lock);
3231 spin_unlock(&tiqn->tiqn_tpg_lock);
3232 end_of_buf = 1;
3233 goto eob;
3235 memcpy((void *)payload + payload_len, buf, len);
3236 payload_len += len;
3238 spin_unlock(&tpg->tpg_np_lock);
3240 spin_unlock(&tiqn->tiqn_tpg_lock);
3241 eob:
3242 if (end_of_buf)
3243 break;
3245 spin_unlock(&tiqn_lock);
3247 cmd->buf_ptr = payload;
3249 return payload_len;
3253 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3254 * MaxRecvDataSegmentLength.
3256 static int iscsit_send_text_rsp(
3257 struct iscsi_cmd *cmd,
3258 struct iscsi_conn *conn)
3260 struct iscsi_text_rsp *hdr;
3261 struct kvec *iov;
3262 u32 padding = 0, tx_size = 0;
3263 int text_length, iov_count = 0;
3265 text_length = iscsit_build_sendtargets_response(cmd);
3266 if (text_length < 0)
3267 return text_length;
3269 padding = ((-text_length) & 3);
3270 if (padding != 0) {
3271 memset(cmd->buf_ptr + text_length, 0, padding);
3272 pr_debug("Attaching %u additional bytes for"
3273 " padding.\n", padding);
3276 hdr = (struct iscsi_text_rsp *) cmd->pdu;
3277 memset(hdr, 0, ISCSI_HDR_LEN);
3278 hdr->opcode = ISCSI_OP_TEXT_RSP;
3279 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3280 hton24(hdr->dlength, text_length);
3281 hdr->itt = cpu_to_be32(cmd->init_task_tag);
3282 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3283 cmd->stat_sn = conn->stat_sn++;
3284 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3286 iscsit_increment_maxcmdsn(cmd, conn->sess);
3287 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3288 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3290 iov = &cmd->iov_misc[0];
3292 iov[iov_count].iov_base = cmd->pdu;
3293 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3294 iov[iov_count].iov_base = cmd->buf_ptr;
3295 iov[iov_count++].iov_len = text_length + padding;
3297 tx_size += (ISCSI_HDR_LEN + text_length + padding);
3299 if (conn->conn_ops->HeaderDigest) {
3300 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3302 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3303 (unsigned char *)hdr, ISCSI_HDR_LEN,
3304 0, NULL, (u8 *)header_digest);
3306 iov[0].iov_len += ISCSI_CRC_LEN;
3307 tx_size += ISCSI_CRC_LEN;
3308 pr_debug("Attaching CRC32 HeaderDigest for"
3309 " Text Response PDU 0x%08x\n", *header_digest);
3312 if (conn->conn_ops->DataDigest) {
3313 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3314 cmd->buf_ptr, (text_length + padding),
3315 0, NULL, (u8 *)&cmd->data_crc);
3317 iov[iov_count].iov_base = &cmd->data_crc;
3318 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3319 tx_size += ISCSI_CRC_LEN;
3321 pr_debug("Attaching DataDigest for %u bytes of text"
3322 " data, CRC 0x%08x\n", (text_length + padding),
3323 cmd->data_crc);
3326 cmd->iov_misc_count = iov_count;
3327 cmd->tx_size = tx_size;
3329 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3330 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3331 text_length, conn->cid);
3332 return 0;
3335 static int iscsit_send_reject(
3336 struct iscsi_cmd *cmd,
3337 struct iscsi_conn *conn)
3339 u32 iov_count = 0, tx_size = 0;
3340 struct iscsi_reject *hdr;
3341 struct kvec *iov;
3343 hdr = (struct iscsi_reject *) cmd->pdu;
3344 hdr->opcode = ISCSI_OP_REJECT;
3345 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3346 hton24(hdr->dlength, ISCSI_HDR_LEN);
3347 cmd->stat_sn = conn->stat_sn++;
3348 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3349 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3350 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3352 iov = &cmd->iov_misc[0];
3354 iov[iov_count].iov_base = cmd->pdu;
3355 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3356 iov[iov_count].iov_base = cmd->buf_ptr;
3357 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3359 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3361 if (conn->conn_ops->HeaderDigest) {
3362 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3364 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3365 (unsigned char *)hdr, ISCSI_HDR_LEN,
3366 0, NULL, (u8 *)header_digest);
3368 iov[0].iov_len += ISCSI_CRC_LEN;
3369 tx_size += ISCSI_CRC_LEN;
3370 pr_debug("Attaching CRC32 HeaderDigest for"
3371 " REJECT PDU 0x%08x\n", *header_digest);
3374 if (conn->conn_ops->DataDigest) {
3375 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3376 (unsigned char *)cmd->buf_ptr, ISCSI_HDR_LEN,
3377 0, NULL, (u8 *)&cmd->data_crc);
3379 iov[iov_count].iov_base = &cmd->data_crc;
3380 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3381 tx_size += ISCSI_CRC_LEN;
3382 pr_debug("Attaching CRC32 DataDigest for REJECT"
3383 " PDU 0x%08x\n", cmd->data_crc);
3386 cmd->iov_misc_count = iov_count;
3387 cmd->tx_size = tx_size;
3389 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3390 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3392 return 0;
3395 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
3397 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
3398 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
3399 wait_for_completion_interruptible_timeout(
3400 &conn->tx_half_close_comp,
3401 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
3405 #ifdef CONFIG_SMP
3407 void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3409 struct iscsi_thread_set *ts = conn->thread_set;
3410 int ord, cpu;
3412 * thread_id is assigned from iscsit_global->ts_bitmap from
3413 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3415 * Here we use thread_id to determine which CPU that this
3416 * iSCSI connection's iscsi_thread_set will be scheduled to
3417 * execute upon.
3419 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
3420 #if 0
3421 pr_debug(">>>>>>>>>>>>>>>>>>>> Generated ord: %d from"
3422 " thread_id: %d\n", ord, ts->thread_id);
3423 #endif
3424 for_each_online_cpu(cpu) {
3425 if (ord-- == 0) {
3426 cpumask_set_cpu(cpu, conn->conn_cpumask);
3427 return;
3431 * This should never be reached..
3433 dump_stack();
3434 cpumask_setall(conn->conn_cpumask);
3437 static inline void iscsit_thread_check_cpumask(
3438 struct iscsi_conn *conn,
3439 struct task_struct *p,
3440 int mode)
3442 char buf[128];
3444 * mode == 1 signals iscsi_target_tx_thread() usage.
3445 * mode == 0 signals iscsi_target_rx_thread() usage.
3447 if (mode == 1) {
3448 if (!conn->conn_tx_reset_cpumask)
3449 return;
3450 conn->conn_tx_reset_cpumask = 0;
3451 } else {
3452 if (!conn->conn_rx_reset_cpumask)
3453 return;
3454 conn->conn_rx_reset_cpumask = 0;
3457 * Update the CPU mask for this single kthread so that
3458 * both TX and RX kthreads are scheduled to run on the
3459 * same CPU.
3461 memset(buf, 0, 128);
3462 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
3463 #if 0
3464 pr_debug(">>>>>>>>>>>>>> Calling set_cpus_allowed_ptr():"
3465 " %s for %s\n", buf, p->comm);
3466 #endif
3467 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3470 #else
3472 void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3474 return;
3477 #define iscsit_thread_check_cpumask(X, Y, Z) ({})
3478 #endif /* CONFIG_SMP */
3480 int iscsi_target_tx_thread(void *arg)
3482 u8 state;
3483 int eodr = 0;
3484 int ret = 0;
3485 int sent_status = 0;
3486 int use_misc = 0;
3487 int map_sg = 0;
3488 struct iscsi_cmd *cmd = NULL;
3489 struct iscsi_conn *conn;
3490 struct iscsi_queue_req *qr = NULL;
3491 struct se_cmd *se_cmd;
3492 struct iscsi_thread_set *ts = (struct iscsi_thread_set *)arg;
3494 * Allow ourselves to be interrupted by SIGINT so that a
3495 * connection recovery / failure event can be triggered externally.
3497 allow_signal(SIGINT);
3499 restart:
3500 conn = iscsi_tx_thread_pre_handler(ts);
3501 if (!conn)
3502 goto out;
3504 eodr = map_sg = ret = sent_status = use_misc = 0;
3506 while (!kthread_should_stop()) {
3508 * Ensure that both TX and RX per connection kthreads
3509 * are scheduled to run on the same CPU.
3511 iscsit_thread_check_cpumask(conn, current, 1);
3513 schedule_timeout_interruptible(MAX_SCHEDULE_TIMEOUT);
3515 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3516 signal_pending(current))
3517 goto transport_err;
3519 get_immediate:
3520 qr = iscsit_get_cmd_from_immediate_queue(conn);
3521 if (qr) {
3522 atomic_set(&conn->check_immediate_queue, 0);
3523 cmd = qr->cmd;
3524 state = qr->state;
3525 kmem_cache_free(lio_qr_cache, qr);
3527 spin_lock_bh(&cmd->istate_lock);
3528 switch (state) {
3529 case ISTATE_SEND_R2T:
3530 spin_unlock_bh(&cmd->istate_lock);
3531 ret = iscsit_send_r2t(cmd, conn);
3532 break;
3533 case ISTATE_REMOVE:
3534 spin_unlock_bh(&cmd->istate_lock);
3536 if (cmd->data_direction == DMA_TO_DEVICE)
3537 iscsit_stop_dataout_timer(cmd);
3539 spin_lock_bh(&conn->cmd_lock);
3540 list_del(&cmd->i_list);
3541 spin_unlock_bh(&conn->cmd_lock);
3543 * Determine if a struct se_cmd is assoicated with
3544 * this struct iscsi_cmd.
3546 if (!(cmd->se_cmd.se_cmd_flags & SCF_SE_LUN_CMD) &&
3547 !(cmd->tmr_req))
3548 iscsit_release_cmd(cmd);
3549 else
3550 transport_generic_free_cmd(&cmd->se_cmd,
3551 1, 0);
3552 goto get_immediate;
3553 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3554 spin_unlock_bh(&cmd->istate_lock);
3555 iscsit_mod_nopin_response_timer(conn);
3556 ret = iscsit_send_unsolicited_nopin(cmd,
3557 conn, 1);
3558 break;
3559 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3560 spin_unlock_bh(&cmd->istate_lock);
3561 ret = iscsit_send_unsolicited_nopin(cmd,
3562 conn, 0);
3563 break;
3564 default:
3565 pr_err("Unknown Opcode: 0x%02x ITT:"
3566 " 0x%08x, i_state: %d on CID: %hu\n",
3567 cmd->iscsi_opcode, cmd->init_task_tag, state,
3568 conn->cid);
3569 spin_unlock_bh(&cmd->istate_lock);
3570 goto transport_err;
3572 if (ret < 0) {
3573 conn->tx_immediate_queue = 0;
3574 goto transport_err;
3577 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3578 conn->tx_immediate_queue = 0;
3579 iscsit_tx_thread_wait_for_tcp(conn);
3580 goto transport_err;
3583 spin_lock_bh(&cmd->istate_lock);
3584 switch (state) {
3585 case ISTATE_SEND_R2T:
3586 spin_unlock_bh(&cmd->istate_lock);
3587 spin_lock_bh(&cmd->dataout_timeout_lock);
3588 iscsit_start_dataout_timer(cmd, conn);
3589 spin_unlock_bh(&cmd->dataout_timeout_lock);
3590 break;
3591 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3592 cmd->i_state = ISTATE_SENT_NOPIN_WANT_RESPONSE;
3593 spin_unlock_bh(&cmd->istate_lock);
3594 break;
3595 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3596 cmd->i_state = ISTATE_SENT_STATUS;
3597 spin_unlock_bh(&cmd->istate_lock);
3598 break;
3599 default:
3600 pr_err("Unknown Opcode: 0x%02x ITT:"
3601 " 0x%08x, i_state: %d on CID: %hu\n",
3602 cmd->iscsi_opcode, cmd->init_task_tag,
3603 state, conn->cid);
3604 spin_unlock_bh(&cmd->istate_lock);
3605 goto transport_err;
3607 goto get_immediate;
3608 } else
3609 conn->tx_immediate_queue = 0;
3611 get_response:
3612 qr = iscsit_get_cmd_from_response_queue(conn);
3613 if (qr) {
3614 cmd = qr->cmd;
3615 state = qr->state;
3616 kmem_cache_free(lio_qr_cache, qr);
3618 spin_lock_bh(&cmd->istate_lock);
3619 check_rsp_state:
3620 switch (state) {
3621 case ISTATE_SEND_DATAIN:
3622 spin_unlock_bh(&cmd->istate_lock);
3623 ret = iscsit_send_data_in(cmd, conn,
3624 &eodr);
3625 map_sg = 1;
3626 break;
3627 case ISTATE_SEND_STATUS:
3628 case ISTATE_SEND_STATUS_RECOVERY:
3629 spin_unlock_bh(&cmd->istate_lock);
3630 use_misc = 1;
3631 ret = iscsit_send_status(cmd, conn);
3632 break;
3633 case ISTATE_SEND_LOGOUTRSP:
3634 spin_unlock_bh(&cmd->istate_lock);
3635 use_misc = 1;
3636 ret = iscsit_send_logout_response(cmd, conn);
3637 break;
3638 case ISTATE_SEND_ASYNCMSG:
3639 spin_unlock_bh(&cmd->istate_lock);
3640 use_misc = 1;
3641 ret = iscsit_send_conn_drop_async_message(
3642 cmd, conn);
3643 break;
3644 case ISTATE_SEND_NOPIN:
3645 spin_unlock_bh(&cmd->istate_lock);
3646 use_misc = 1;
3647 ret = iscsit_send_nopin_response(cmd, conn);
3648 break;
3649 case ISTATE_SEND_REJECT:
3650 spin_unlock_bh(&cmd->istate_lock);
3651 use_misc = 1;
3652 ret = iscsit_send_reject(cmd, conn);
3653 break;
3654 case ISTATE_SEND_TASKMGTRSP:
3655 spin_unlock_bh(&cmd->istate_lock);
3656 use_misc = 1;
3657 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3658 if (ret != 0)
3659 break;
3660 ret = iscsit_tmr_post_handler(cmd, conn);
3661 if (ret != 0)
3662 iscsit_fall_back_to_erl0(conn->sess);
3663 break;
3664 case ISTATE_SEND_TEXTRSP:
3665 spin_unlock_bh(&cmd->istate_lock);
3666 use_misc = 1;
3667 ret = iscsit_send_text_rsp(cmd, conn);
3668 break;
3669 default:
3670 pr_err("Unknown Opcode: 0x%02x ITT:"
3671 " 0x%08x, i_state: %d on CID: %hu\n",
3672 cmd->iscsi_opcode, cmd->init_task_tag,
3673 state, conn->cid);
3674 spin_unlock_bh(&cmd->istate_lock);
3675 goto transport_err;
3677 if (ret < 0) {
3678 conn->tx_response_queue = 0;
3679 goto transport_err;
3682 se_cmd = &cmd->se_cmd;
3684 if (map_sg && !conn->conn_ops->IFMarker) {
3685 if (iscsit_fe_sendpage_sg(cmd, conn) < 0) {
3686 conn->tx_response_queue = 0;
3687 iscsit_tx_thread_wait_for_tcp(conn);
3688 iscsit_unmap_iovec(cmd);
3689 goto transport_err;
3691 } else {
3692 if (iscsit_send_tx_data(cmd, conn, use_misc) < 0) {
3693 conn->tx_response_queue = 0;
3694 iscsit_tx_thread_wait_for_tcp(conn);
3695 iscsit_unmap_iovec(cmd);
3696 goto transport_err;
3699 map_sg = 0;
3700 iscsit_unmap_iovec(cmd);
3702 spin_lock_bh(&cmd->istate_lock);
3703 switch (state) {
3704 case ISTATE_SEND_DATAIN:
3705 if (!eodr)
3706 goto check_rsp_state;
3708 if (eodr == 1) {
3709 cmd->i_state = ISTATE_SENT_LAST_DATAIN;
3710 sent_status = 1;
3711 eodr = use_misc = 0;
3712 } else if (eodr == 2) {
3713 cmd->i_state = state =
3714 ISTATE_SEND_STATUS;
3715 sent_status = 0;
3716 eodr = use_misc = 0;
3717 goto check_rsp_state;
3719 break;
3720 case ISTATE_SEND_STATUS:
3721 use_misc = 0;
3722 sent_status = 1;
3723 break;
3724 case ISTATE_SEND_ASYNCMSG:
3725 case ISTATE_SEND_NOPIN:
3726 case ISTATE_SEND_STATUS_RECOVERY:
3727 case ISTATE_SEND_TEXTRSP:
3728 use_misc = 0;
3729 sent_status = 1;
3730 break;
3731 case ISTATE_SEND_REJECT:
3732 use_misc = 0;
3733 if (cmd->cmd_flags & ICF_REJECT_FAIL_CONN) {
3734 cmd->cmd_flags &= ~ICF_REJECT_FAIL_CONN;
3735 spin_unlock_bh(&cmd->istate_lock);
3736 complete(&cmd->reject_comp);
3737 goto transport_err;
3739 complete(&cmd->reject_comp);
3740 break;
3741 case ISTATE_SEND_TASKMGTRSP:
3742 use_misc = 0;
3743 sent_status = 1;
3744 break;
3745 case ISTATE_SEND_LOGOUTRSP:
3746 spin_unlock_bh(&cmd->istate_lock);
3747 if (!iscsit_logout_post_handler(cmd, conn))
3748 goto restart;
3749 spin_lock_bh(&cmd->istate_lock);
3750 use_misc = 0;
3751 sent_status = 1;
3752 break;
3753 default:
3754 pr_err("Unknown Opcode: 0x%02x ITT:"
3755 " 0x%08x, i_state: %d on CID: %hu\n",
3756 cmd->iscsi_opcode, cmd->init_task_tag,
3757 cmd->i_state, conn->cid);
3758 spin_unlock_bh(&cmd->istate_lock);
3759 goto transport_err;
3762 if (sent_status) {
3763 cmd->i_state = ISTATE_SENT_STATUS;
3764 sent_status = 0;
3766 spin_unlock_bh(&cmd->istate_lock);
3768 if (atomic_read(&conn->check_immediate_queue))
3769 goto get_immediate;
3771 goto get_response;
3772 } else
3773 conn->tx_response_queue = 0;
3776 transport_err:
3777 iscsit_take_action_for_connection_exit(conn);
3778 goto restart;
3779 out:
3780 return 0;
3783 int iscsi_target_rx_thread(void *arg)
3785 int ret;
3786 u8 buffer[ISCSI_HDR_LEN], opcode;
3787 u32 checksum = 0, digest = 0;
3788 struct iscsi_conn *conn = NULL;
3789 struct iscsi_thread_set *ts = (struct iscsi_thread_set *)arg;
3790 struct kvec iov;
3792 * Allow ourselves to be interrupted by SIGINT so that a
3793 * connection recovery / failure event can be triggered externally.
3795 allow_signal(SIGINT);
3797 restart:
3798 conn = iscsi_rx_thread_pre_handler(ts);
3799 if (!conn)
3800 goto out;
3802 while (!kthread_should_stop()) {
3804 * Ensure that both TX and RX per connection kthreads
3805 * are scheduled to run on the same CPU.
3807 iscsit_thread_check_cpumask(conn, current, 0);
3809 memset(buffer, 0, ISCSI_HDR_LEN);
3810 memset(&iov, 0, sizeof(struct kvec));
3812 iov.iov_base = buffer;
3813 iov.iov_len = ISCSI_HDR_LEN;
3815 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3816 if (ret != ISCSI_HDR_LEN) {
3817 iscsit_rx_thread_wait_for_tcp(conn);
3818 goto transport_err;
3822 * Set conn->bad_hdr for use with REJECT PDUs.
3824 memcpy(&conn->bad_hdr, &buffer, ISCSI_HDR_LEN);
3826 if (conn->conn_ops->HeaderDigest) {
3827 iov.iov_base = &digest;
3828 iov.iov_len = ISCSI_CRC_LEN;
3830 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3831 if (ret != ISCSI_CRC_LEN) {
3832 iscsit_rx_thread_wait_for_tcp(conn);
3833 goto transport_err;
3836 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
3837 buffer, ISCSI_HDR_LEN,
3838 0, NULL, (u8 *)&checksum);
3840 if (digest != checksum) {
3841 pr_err("HeaderDigest CRC32C failed,"
3842 " received 0x%08x, computed 0x%08x\n",
3843 digest, checksum);
3845 * Set the PDU to 0xff so it will intentionally
3846 * hit default in the switch below.
3848 memset(buffer, 0xff, ISCSI_HDR_LEN);
3849 spin_lock_bh(&conn->sess->session_stats_lock);
3850 conn->sess->conn_digest_errors++;
3851 spin_unlock_bh(&conn->sess->session_stats_lock);
3852 } else {
3853 pr_debug("Got HeaderDigest CRC32C"
3854 " 0x%08x\n", checksum);
3858 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
3859 goto transport_err;
3861 opcode = buffer[0] & ISCSI_OPCODE_MASK;
3863 if (conn->sess->sess_ops->SessionType &&
3864 ((!(opcode & ISCSI_OP_TEXT)) ||
3865 (!(opcode & ISCSI_OP_LOGOUT)))) {
3866 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3867 " while in Discovery Session, rejecting.\n", opcode);
3868 iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
3869 buffer, conn);
3870 goto transport_err;
3873 switch (opcode) {
3874 case ISCSI_OP_SCSI_CMD:
3875 if (iscsit_handle_scsi_cmd(conn, buffer) < 0)
3876 goto transport_err;
3877 break;
3878 case ISCSI_OP_SCSI_DATA_OUT:
3879 if (iscsit_handle_data_out(conn, buffer) < 0)
3880 goto transport_err;
3881 break;
3882 case ISCSI_OP_NOOP_OUT:
3883 if (iscsit_handle_nop_out(conn, buffer) < 0)
3884 goto transport_err;
3885 break;
3886 case ISCSI_OP_SCSI_TMFUNC:
3887 if (iscsit_handle_task_mgt_cmd(conn, buffer) < 0)
3888 goto transport_err;
3889 break;
3890 case ISCSI_OP_TEXT:
3891 if (iscsit_handle_text_cmd(conn, buffer) < 0)
3892 goto transport_err;
3893 break;
3894 case ISCSI_OP_LOGOUT:
3895 ret = iscsit_handle_logout_cmd(conn, buffer);
3896 if (ret > 0) {
3897 wait_for_completion_timeout(&conn->conn_logout_comp,
3898 SECONDS_FOR_LOGOUT_COMP * HZ);
3899 goto transport_err;
3900 } else if (ret < 0)
3901 goto transport_err;
3902 break;
3903 case ISCSI_OP_SNACK:
3904 if (iscsit_handle_snack(conn, buffer) < 0)
3905 goto transport_err;
3906 break;
3907 default:
3908 pr_err("Got unknown iSCSI OpCode: 0x%02x\n",
3909 opcode);
3910 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3911 pr_err("Cannot recover from unknown"
3912 " opcode while ERL=0, closing iSCSI connection"
3913 ".\n");
3914 goto transport_err;
3916 if (!conn->conn_ops->OFMarker) {
3917 pr_err("Unable to recover from unknown"
3918 " opcode while OFMarker=No, closing iSCSI"
3919 " connection.\n");
3920 goto transport_err;
3922 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
3923 pr_err("Unable to recover from unknown"
3924 " opcode, closing iSCSI connection.\n");
3925 goto transport_err;
3927 break;
3931 transport_err:
3932 if (!signal_pending(current))
3933 atomic_set(&conn->transport_failed, 1);
3934 iscsit_take_action_for_connection_exit(conn);
3935 goto restart;
3936 out:
3937 return 0;
3940 static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
3942 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
3943 struct iscsi_session *sess = conn->sess;
3944 struct se_cmd *se_cmd;
3946 * We expect this function to only ever be called from either RX or TX
3947 * thread context via iscsit_close_connection() once the other context
3948 * has been reset -> returned sleeping pre-handler state.
3950 spin_lock_bh(&conn->cmd_lock);
3951 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_list) {
3952 if (!(cmd->se_cmd.se_cmd_flags & SCF_SE_LUN_CMD)) {
3954 list_del(&cmd->i_list);
3955 spin_unlock_bh(&conn->cmd_lock);
3956 iscsit_increment_maxcmdsn(cmd, sess);
3957 se_cmd = &cmd->se_cmd;
3959 * Special cases for active iSCSI TMR, and
3960 * transport_lookup_cmd_lun() failing from
3961 * iscsit_get_lun_for_cmd() in iscsit_handle_scsi_cmd().
3963 if (cmd->tmr_req && se_cmd->transport_wait_for_tasks)
3964 se_cmd->transport_wait_for_tasks(se_cmd, 1, 1);
3965 else if (cmd->se_cmd.se_cmd_flags & SCF_SE_LUN_CMD)
3966 transport_release_cmd(se_cmd);
3967 else
3968 iscsit_release_cmd(cmd);
3970 spin_lock_bh(&conn->cmd_lock);
3971 continue;
3973 list_del(&cmd->i_list);
3974 spin_unlock_bh(&conn->cmd_lock);
3976 iscsit_increment_maxcmdsn(cmd, sess);
3977 se_cmd = &cmd->se_cmd;
3979 if (se_cmd->transport_wait_for_tasks)
3980 se_cmd->transport_wait_for_tasks(se_cmd, 1, 1);
3982 spin_lock_bh(&conn->cmd_lock);
3984 spin_unlock_bh(&conn->cmd_lock);
3987 static void iscsit_stop_timers_for_cmds(
3988 struct iscsi_conn *conn)
3990 struct iscsi_cmd *cmd;
3992 spin_lock_bh(&conn->cmd_lock);
3993 list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
3994 if (cmd->data_direction == DMA_TO_DEVICE)
3995 iscsit_stop_dataout_timer(cmd);
3997 spin_unlock_bh(&conn->cmd_lock);
4000 int iscsit_close_connection(
4001 struct iscsi_conn *conn)
4003 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4004 struct iscsi_session *sess = conn->sess;
4006 pr_debug("Closing iSCSI connection CID %hu on SID:"
4007 " %u\n", conn->cid, sess->sid);
4009 * Always up conn_logout_comp just in case the RX Thread is sleeping
4010 * and the logout response never got sent because the connection
4011 * failed.
4013 complete(&conn->conn_logout_comp);
4015 iscsi_release_thread_set(conn);
4017 iscsit_stop_timers_for_cmds(conn);
4018 iscsit_stop_nopin_response_timer(conn);
4019 iscsit_stop_nopin_timer(conn);
4020 iscsit_free_queue_reqs_for_conn(conn);
4023 * During Connection recovery drop unacknowledged out of order
4024 * commands for this connection, and prepare the other commands
4025 * for realligence.
4027 * During normal operation clear the out of order commands (but
4028 * do not free the struct iscsi_ooo_cmdsn's) and release all
4029 * struct iscsi_cmds.
4031 if (atomic_read(&conn->connection_recovery)) {
4032 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4033 iscsit_prepare_cmds_for_realligance(conn);
4034 } else {
4035 iscsit_clear_ooo_cmdsns_for_conn(conn);
4036 iscsit_release_commands_from_conn(conn);
4040 * Handle decrementing session or connection usage count if
4041 * a logout response was not able to be sent because the
4042 * connection failed. Fall back to Session Recovery here.
4044 if (atomic_read(&conn->conn_logout_remove)) {
4045 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4046 iscsit_dec_conn_usage_count(conn);
4047 iscsit_dec_session_usage_count(sess);
4049 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4050 iscsit_dec_conn_usage_count(conn);
4052 atomic_set(&conn->conn_logout_remove, 0);
4053 atomic_set(&sess->session_reinstatement, 0);
4054 atomic_set(&sess->session_fall_back_to_erl0, 1);
4057 spin_lock_bh(&sess->conn_lock);
4058 list_del(&conn->conn_list);
4061 * Attempt to let the Initiator know this connection failed by
4062 * sending an Connection Dropped Async Message on another
4063 * active connection.
4065 if (atomic_read(&conn->connection_recovery))
4066 iscsit_build_conn_drop_async_message(conn);
4068 spin_unlock_bh(&sess->conn_lock);
4071 * If connection reinstatement is being performed on this connection,
4072 * up the connection reinstatement semaphore that is being blocked on
4073 * in iscsit_cause_connection_reinstatement().
4075 spin_lock_bh(&conn->state_lock);
4076 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4077 spin_unlock_bh(&conn->state_lock);
4078 complete(&conn->conn_wait_comp);
4079 wait_for_completion(&conn->conn_post_wait_comp);
4080 spin_lock_bh(&conn->state_lock);
4084 * If connection reinstatement is being performed on this connection
4085 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4086 * connection wait rcfr semaphore that is being blocked on
4087 * an iscsit_connection_reinstatement_rcfr().
4089 if (atomic_read(&conn->connection_wait_rcfr)) {
4090 spin_unlock_bh(&conn->state_lock);
4091 complete(&conn->conn_wait_rcfr_comp);
4092 wait_for_completion(&conn->conn_post_wait_comp);
4093 spin_lock_bh(&conn->state_lock);
4095 atomic_set(&conn->connection_reinstatement, 1);
4096 spin_unlock_bh(&conn->state_lock);
4099 * If any other processes are accessing this connection pointer we
4100 * must wait until they have completed.
4102 iscsit_check_conn_usage_count(conn);
4104 if (conn->conn_rx_hash.tfm)
4105 crypto_free_hash(conn->conn_rx_hash.tfm);
4106 if (conn->conn_tx_hash.tfm)
4107 crypto_free_hash(conn->conn_tx_hash.tfm);
4109 if (conn->conn_cpumask)
4110 free_cpumask_var(conn->conn_cpumask);
4112 kfree(conn->conn_ops);
4113 conn->conn_ops = NULL;
4115 if (conn->sock) {
4116 if (conn->conn_flags & CONNFLAG_SCTP_STRUCT_FILE) {
4117 kfree(conn->sock->file);
4118 conn->sock->file = NULL;
4120 sock_release(conn->sock);
4122 conn->thread_set = NULL;
4124 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4125 conn->conn_state = TARG_CONN_STATE_FREE;
4126 kfree(conn);
4128 spin_lock_bh(&sess->conn_lock);
4129 atomic_dec(&sess->nconn);
4130 pr_debug("Decremented iSCSI connection count to %hu from node:"
4131 " %s\n", atomic_read(&sess->nconn),
4132 sess->sess_ops->InitiatorName);
4134 * Make sure that if one connection fails in an non ERL=2 iSCSI
4135 * Session that they all fail.
4137 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4138 !atomic_read(&sess->session_logout))
4139 atomic_set(&sess->session_fall_back_to_erl0, 1);
4142 * If this was not the last connection in the session, and we are
4143 * performing session reinstatement or falling back to ERL=0, call
4144 * iscsit_stop_session() without sleeping to shutdown the other
4145 * active connections.
4147 if (atomic_read(&sess->nconn)) {
4148 if (!atomic_read(&sess->session_reinstatement) &&
4149 !atomic_read(&sess->session_fall_back_to_erl0)) {
4150 spin_unlock_bh(&sess->conn_lock);
4151 return 0;
4153 if (!atomic_read(&sess->session_stop_active)) {
4154 atomic_set(&sess->session_stop_active, 1);
4155 spin_unlock_bh(&sess->conn_lock);
4156 iscsit_stop_session(sess, 0, 0);
4157 return 0;
4159 spin_unlock_bh(&sess->conn_lock);
4160 return 0;
4164 * If this was the last connection in the session and one of the
4165 * following is occurring:
4167 * Session Reinstatement is not being performed, and are falling back
4168 * to ERL=0 call iscsit_close_session().
4170 * Session Logout was requested. iscsit_close_session() will be called
4171 * elsewhere.
4173 * Session Continuation is not being performed, start the Time2Retain
4174 * handler and check if sleep_on_sess_wait_sem is active.
4176 if (!atomic_read(&sess->session_reinstatement) &&
4177 atomic_read(&sess->session_fall_back_to_erl0)) {
4178 spin_unlock_bh(&sess->conn_lock);
4179 iscsit_close_session(sess);
4181 return 0;
4182 } else if (atomic_read(&sess->session_logout)) {
4183 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4184 sess->session_state = TARG_SESS_STATE_FREE;
4185 spin_unlock_bh(&sess->conn_lock);
4187 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4188 complete(&sess->session_wait_comp);
4190 return 0;
4191 } else {
4192 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4193 sess->session_state = TARG_SESS_STATE_FAILED;
4195 if (!atomic_read(&sess->session_continuation)) {
4196 spin_unlock_bh(&sess->conn_lock);
4197 iscsit_start_time2retain_handler(sess);
4198 } else
4199 spin_unlock_bh(&sess->conn_lock);
4201 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4202 complete(&sess->session_wait_comp);
4204 return 0;
4206 spin_unlock_bh(&sess->conn_lock);
4208 return 0;
4211 int iscsit_close_session(struct iscsi_session *sess)
4213 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4214 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4216 if (atomic_read(&sess->nconn)) {
4217 pr_err("%d connection(s) still exist for iSCSI session"
4218 " to %s\n", atomic_read(&sess->nconn),
4219 sess->sess_ops->InitiatorName);
4220 BUG();
4223 spin_lock_bh(&se_tpg->session_lock);
4224 atomic_set(&sess->session_logout, 1);
4225 atomic_set(&sess->session_reinstatement, 1);
4226 iscsit_stop_time2retain_timer(sess);
4227 spin_unlock_bh(&se_tpg->session_lock);
4230 * transport_deregister_session_configfs() will clear the
4231 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4232 * can be setting it again with __transport_register_session() in
4233 * iscsi_post_login_handler() again after the iscsit_stop_session()
4234 * completes in iscsi_np context.
4236 transport_deregister_session_configfs(sess->se_sess);
4239 * If any other processes are accessing this session pointer we must
4240 * wait until they have completed. If we are in an interrupt (the
4241 * time2retain handler) and contain and active session usage count we
4242 * restart the timer and exit.
4244 if (!in_interrupt()) {
4245 if (iscsit_check_session_usage_count(sess) == 1)
4246 iscsit_stop_session(sess, 1, 1);
4247 } else {
4248 if (iscsit_check_session_usage_count(sess) == 2) {
4249 atomic_set(&sess->session_logout, 0);
4250 iscsit_start_time2retain_handler(sess);
4251 return 0;
4255 transport_deregister_session(sess->se_sess);
4257 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4258 iscsit_free_connection_recovery_entires(sess);
4260 iscsit_free_all_ooo_cmdsns(sess);
4262 spin_lock_bh(&se_tpg->session_lock);
4263 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4264 sess->session_state = TARG_SESS_STATE_FREE;
4265 pr_debug("Released iSCSI session from node: %s\n",
4266 sess->sess_ops->InitiatorName);
4267 tpg->nsessions--;
4268 if (tpg->tpg_tiqn)
4269 tpg->tpg_tiqn->tiqn_nsessions--;
4271 pr_debug("Decremented number of active iSCSI Sessions on"
4272 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4274 spin_lock(&sess_idr_lock);
4275 idr_remove(&sess_idr, sess->session_index);
4276 spin_unlock(&sess_idr_lock);
4278 kfree(sess->sess_ops);
4279 sess->sess_ops = NULL;
4280 spin_unlock_bh(&se_tpg->session_lock);
4282 kfree(sess);
4283 return 0;
4286 static void iscsit_logout_post_handler_closesession(
4287 struct iscsi_conn *conn)
4289 struct iscsi_session *sess = conn->sess;
4291 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4292 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4294 atomic_set(&conn->conn_logout_remove, 0);
4295 complete(&conn->conn_logout_comp);
4297 iscsit_dec_conn_usage_count(conn);
4298 iscsit_stop_session(sess, 1, 1);
4299 iscsit_dec_session_usage_count(sess);
4300 iscsit_close_session(sess);
4303 static void iscsit_logout_post_handler_samecid(
4304 struct iscsi_conn *conn)
4306 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4307 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4309 atomic_set(&conn->conn_logout_remove, 0);
4310 complete(&conn->conn_logout_comp);
4312 iscsit_cause_connection_reinstatement(conn, 1);
4313 iscsit_dec_conn_usage_count(conn);
4316 static void iscsit_logout_post_handler_diffcid(
4317 struct iscsi_conn *conn,
4318 u16 cid)
4320 struct iscsi_conn *l_conn;
4321 struct iscsi_session *sess = conn->sess;
4323 if (!sess)
4324 return;
4326 spin_lock_bh(&sess->conn_lock);
4327 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4328 if (l_conn->cid == cid) {
4329 iscsit_inc_conn_usage_count(l_conn);
4330 break;
4333 spin_unlock_bh(&sess->conn_lock);
4335 if (!l_conn)
4336 return;
4338 if (l_conn->sock)
4339 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4341 spin_lock_bh(&l_conn->state_lock);
4342 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4343 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4344 spin_unlock_bh(&l_conn->state_lock);
4346 iscsit_cause_connection_reinstatement(l_conn, 1);
4347 iscsit_dec_conn_usage_count(l_conn);
4351 * Return of 0 causes the TX thread to restart.
4353 static int iscsit_logout_post_handler(
4354 struct iscsi_cmd *cmd,
4355 struct iscsi_conn *conn)
4357 int ret = 0;
4359 switch (cmd->logout_reason) {
4360 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4361 switch (cmd->logout_response) {
4362 case ISCSI_LOGOUT_SUCCESS:
4363 case ISCSI_LOGOUT_CLEANUP_FAILED:
4364 default:
4365 iscsit_logout_post_handler_closesession(conn);
4366 break;
4368 ret = 0;
4369 break;
4370 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4371 if (conn->cid == cmd->logout_cid) {
4372 switch (cmd->logout_response) {
4373 case ISCSI_LOGOUT_SUCCESS:
4374 case ISCSI_LOGOUT_CLEANUP_FAILED:
4375 default:
4376 iscsit_logout_post_handler_samecid(conn);
4377 break;
4379 ret = 0;
4380 } else {
4381 switch (cmd->logout_response) {
4382 case ISCSI_LOGOUT_SUCCESS:
4383 iscsit_logout_post_handler_diffcid(conn,
4384 cmd->logout_cid);
4385 break;
4386 case ISCSI_LOGOUT_CID_NOT_FOUND:
4387 case ISCSI_LOGOUT_CLEANUP_FAILED:
4388 default:
4389 break;
4391 ret = 1;
4393 break;
4394 case ISCSI_LOGOUT_REASON_RECOVERY:
4395 switch (cmd->logout_response) {
4396 case ISCSI_LOGOUT_SUCCESS:
4397 case ISCSI_LOGOUT_CID_NOT_FOUND:
4398 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4399 case ISCSI_LOGOUT_CLEANUP_FAILED:
4400 default:
4401 break;
4403 ret = 1;
4404 break;
4405 default:
4406 break;
4409 return ret;
4412 void iscsit_fail_session(struct iscsi_session *sess)
4414 struct iscsi_conn *conn;
4416 spin_lock_bh(&sess->conn_lock);
4417 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4418 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4419 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4421 spin_unlock_bh(&sess->conn_lock);
4423 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4424 sess->session_state = TARG_SESS_STATE_FAILED;
4427 int iscsit_free_session(struct iscsi_session *sess)
4429 u16 conn_count = atomic_read(&sess->nconn);
4430 struct iscsi_conn *conn, *conn_tmp = NULL;
4431 int is_last;
4433 spin_lock_bh(&sess->conn_lock);
4434 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4436 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4437 conn_list) {
4438 if (conn_count == 0)
4439 break;
4441 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4442 is_last = 1;
4443 } else {
4444 iscsit_inc_conn_usage_count(conn_tmp);
4445 is_last = 0;
4447 iscsit_inc_conn_usage_count(conn);
4449 spin_unlock_bh(&sess->conn_lock);
4450 iscsit_cause_connection_reinstatement(conn, 1);
4451 spin_lock_bh(&sess->conn_lock);
4453 iscsit_dec_conn_usage_count(conn);
4454 if (is_last == 0)
4455 iscsit_dec_conn_usage_count(conn_tmp);
4457 conn_count--;
4460 if (atomic_read(&sess->nconn)) {
4461 spin_unlock_bh(&sess->conn_lock);
4462 wait_for_completion(&sess->session_wait_comp);
4463 } else
4464 spin_unlock_bh(&sess->conn_lock);
4466 iscsit_close_session(sess);
4467 return 0;
4470 void iscsit_stop_session(
4471 struct iscsi_session *sess,
4472 int session_sleep,
4473 int connection_sleep)
4475 u16 conn_count = atomic_read(&sess->nconn);
4476 struct iscsi_conn *conn, *conn_tmp = NULL;
4477 int is_last;
4479 spin_lock_bh(&sess->conn_lock);
4480 if (session_sleep)
4481 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4483 if (connection_sleep) {
4484 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4485 conn_list) {
4486 if (conn_count == 0)
4487 break;
4489 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4490 is_last = 1;
4491 } else {
4492 iscsit_inc_conn_usage_count(conn_tmp);
4493 is_last = 0;
4495 iscsit_inc_conn_usage_count(conn);
4497 spin_unlock_bh(&sess->conn_lock);
4498 iscsit_cause_connection_reinstatement(conn, 1);
4499 spin_lock_bh(&sess->conn_lock);
4501 iscsit_dec_conn_usage_count(conn);
4502 if (is_last == 0)
4503 iscsit_dec_conn_usage_count(conn_tmp);
4504 conn_count--;
4506 } else {
4507 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4508 iscsit_cause_connection_reinstatement(conn, 0);
4511 if (session_sleep && atomic_read(&sess->nconn)) {
4512 spin_unlock_bh(&sess->conn_lock);
4513 wait_for_completion(&sess->session_wait_comp);
4514 } else
4515 spin_unlock_bh(&sess->conn_lock);
4518 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4520 struct iscsi_session *sess;
4521 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4522 struct se_session *se_sess, *se_sess_tmp;
4523 int session_count = 0;
4525 spin_lock_bh(&se_tpg->session_lock);
4526 if (tpg->nsessions && !force) {
4527 spin_unlock_bh(&se_tpg->session_lock);
4528 return -1;
4531 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4532 sess_list) {
4533 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4535 spin_lock(&sess->conn_lock);
4536 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4537 atomic_read(&sess->session_logout) ||
4538 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4539 spin_unlock(&sess->conn_lock);
4540 continue;
4542 atomic_set(&sess->session_reinstatement, 1);
4543 spin_unlock(&sess->conn_lock);
4544 spin_unlock_bh(&se_tpg->session_lock);
4546 iscsit_free_session(sess);
4547 spin_lock_bh(&se_tpg->session_lock);
4549 session_count++;
4551 spin_unlock_bh(&se_tpg->session_lock);
4553 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4554 " Group: %hu\n", session_count, tpg->tpgt);
4555 return 0;
4558 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4559 MODULE_VERSION("4.1.x");
4560 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4561 MODULE_LICENSE("GPL");
4563 module_init(iscsi_target_init_module);
4564 module_exit(iscsi_target_cleanup_module);