Linux 4.19.133
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target.c
blob1633e26662687e6f41ff076f0150227fa4b06278
1 /*******************************************************************************
2 * This file contains main functions related to the iSCSI Target Core Driver.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
19 #include <crypto/hash.h>
20 #include <linux/string.h>
21 #include <linux/kthread.h>
22 #include <linux/completion.h>
23 #include <linux/module.h>
24 #include <linux/vmalloc.h>
25 #include <linux/idr.h>
26 #include <linux/delay.h>
27 #include <linux/sched/signal.h>
28 #include <asm/unaligned.h>
29 #include <linux/inet.h>
30 #include <net/ipv6.h>
31 #include <scsi/scsi_proto.h>
32 #include <scsi/iscsi_proto.h>
33 #include <scsi/scsi_tcq.h>
34 #include <target/target_core_base.h>
35 #include <target/target_core_fabric.h>
37 #include <target/iscsi/iscsi_target_core.h>
38 #include "iscsi_target_parameters.h"
39 #include "iscsi_target_seq_pdu_list.h"
40 #include "iscsi_target_datain_values.h"
41 #include "iscsi_target_erl0.h"
42 #include "iscsi_target_erl1.h"
43 #include "iscsi_target_erl2.h"
44 #include "iscsi_target_login.h"
45 #include "iscsi_target_tmr.h"
46 #include "iscsi_target_tpg.h"
47 #include "iscsi_target_util.h"
48 #include "iscsi_target.h"
49 #include "iscsi_target_device.h"
50 #include <target/iscsi/iscsi_target_stat.h>
52 #include <target/iscsi/iscsi_transport.h>
54 static LIST_HEAD(g_tiqn_list);
55 static LIST_HEAD(g_np_list);
56 static DEFINE_SPINLOCK(tiqn_lock);
57 static DEFINE_MUTEX(np_lock);
59 static struct idr tiqn_idr;
60 DEFINE_IDA(sess_ida);
61 struct mutex auth_id_lock;
63 struct iscsit_global *iscsit_global;
65 struct kmem_cache *lio_qr_cache;
66 struct kmem_cache *lio_dr_cache;
67 struct kmem_cache *lio_ooo_cache;
68 struct kmem_cache *lio_r2t_cache;
70 static int iscsit_handle_immediate_data(struct iscsi_cmd *,
71 struct iscsi_scsi_req *, u32);
73 struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
75 struct iscsi_tiqn *tiqn = NULL;
77 spin_lock(&tiqn_lock);
78 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
79 if (!strcmp(tiqn->tiqn, buf)) {
81 spin_lock(&tiqn->tiqn_state_lock);
82 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
83 tiqn->tiqn_access_count++;
84 spin_unlock(&tiqn->tiqn_state_lock);
85 spin_unlock(&tiqn_lock);
86 return tiqn;
88 spin_unlock(&tiqn->tiqn_state_lock);
91 spin_unlock(&tiqn_lock);
93 return NULL;
96 static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
98 spin_lock(&tiqn->tiqn_state_lock);
99 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
100 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
101 spin_unlock(&tiqn->tiqn_state_lock);
102 return 0;
104 spin_unlock(&tiqn->tiqn_state_lock);
106 return -1;
109 void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
111 spin_lock(&tiqn->tiqn_state_lock);
112 tiqn->tiqn_access_count--;
113 spin_unlock(&tiqn->tiqn_state_lock);
117 * Note that IQN formatting is expected to be done in userspace, and
118 * no explict IQN format checks are done here.
120 struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
122 struct iscsi_tiqn *tiqn = NULL;
123 int ret;
125 if (strlen(buf) >= ISCSI_IQN_LEN) {
126 pr_err("Target IQN exceeds %d bytes\n",
127 ISCSI_IQN_LEN);
128 return ERR_PTR(-EINVAL);
131 tiqn = kzalloc(sizeof(*tiqn), GFP_KERNEL);
132 if (!tiqn)
133 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 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
146 idr_preload(GFP_KERNEL);
147 spin_lock(&tiqn_lock);
149 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
150 if (ret < 0) {
151 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
152 spin_unlock(&tiqn_lock);
153 idr_preload_end();
154 kfree(tiqn);
155 return ERR_PTR(ret);
157 tiqn->tiqn_index = ret;
158 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
160 spin_unlock(&tiqn_lock);
161 idr_preload_end();
163 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
165 return tiqn;
169 static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
172 * Wait for accesses to said struct iscsi_tiqn to end.
174 spin_lock(&tiqn->tiqn_state_lock);
175 while (tiqn->tiqn_access_count != 0) {
176 spin_unlock(&tiqn->tiqn_state_lock);
177 msleep(10);
178 spin_lock(&tiqn->tiqn_state_lock);
180 spin_unlock(&tiqn->tiqn_state_lock);
183 void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
186 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
187 * while holding tiqn->tiqn_state_lock. This means that all subsequent
188 * attempts to access this struct iscsi_tiqn will fail from both transport
189 * fabric and control code paths.
191 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
192 pr_err("iscsit_set_tiqn_shutdown() failed\n");
193 return;
196 iscsit_wait_for_tiqn(tiqn);
198 spin_lock(&tiqn_lock);
199 list_del(&tiqn->tiqn_list);
200 idr_remove(&tiqn_idr, tiqn->tiqn_index);
201 spin_unlock(&tiqn_lock);
203 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
204 tiqn->tiqn);
205 kfree(tiqn);
208 int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
210 int ret;
212 * Determine if the network portal is accepting storage traffic.
214 spin_lock_bh(&np->np_thread_lock);
215 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
216 spin_unlock_bh(&np->np_thread_lock);
217 return -1;
219 spin_unlock_bh(&np->np_thread_lock);
221 * Determine if the portal group is accepting storage traffic.
223 spin_lock_bh(&tpg->tpg_state_lock);
224 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
225 spin_unlock_bh(&tpg->tpg_state_lock);
226 return -1;
228 spin_unlock_bh(&tpg->tpg_state_lock);
231 * Here we serialize access across the TIQN+TPG Tuple.
233 ret = down_interruptible(&tpg->np_login_sem);
234 if (ret != 0)
235 return -1;
237 spin_lock_bh(&tpg->tpg_state_lock);
238 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
239 spin_unlock_bh(&tpg->tpg_state_lock);
240 up(&tpg->np_login_sem);
241 return -1;
243 spin_unlock_bh(&tpg->tpg_state_lock);
245 return 0;
248 void iscsit_login_kref_put(struct kref *kref)
250 struct iscsi_tpg_np *tpg_np = container_of(kref,
251 struct iscsi_tpg_np, tpg_np_kref);
253 complete(&tpg_np->tpg_np_comp);
256 int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
257 struct iscsi_tpg_np *tpg_np)
259 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
261 up(&tpg->np_login_sem);
263 if (tpg_np)
264 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
266 if (tiqn)
267 iscsit_put_tiqn_for_login(tiqn);
269 return 0;
272 bool iscsit_check_np_match(
273 struct sockaddr_storage *sockaddr,
274 struct iscsi_np *np,
275 int network_transport)
277 struct sockaddr_in *sock_in, *sock_in_e;
278 struct sockaddr_in6 *sock_in6, *sock_in6_e;
279 bool ip_match = false;
280 u16 port, port_e;
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(&sock_in6->sin6_addr.in6_u,
287 &sock_in6_e->sin6_addr.in6_u,
288 sizeof(struct in6_addr)))
289 ip_match = true;
291 port = ntohs(sock_in6->sin6_port);
292 port_e = ntohs(sock_in6_e->sin6_port);
293 } else {
294 sock_in = (struct sockaddr_in *)sockaddr;
295 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
297 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
298 ip_match = true;
300 port = ntohs(sock_in->sin_port);
301 port_e = ntohs(sock_in_e->sin_port);
304 if (ip_match && (port_e == port) &&
305 (np->np_network_transport == network_transport))
306 return true;
308 return false;
312 * Called with mutex np_lock held
314 static struct iscsi_np *iscsit_get_np(
315 struct sockaddr_storage *sockaddr,
316 int network_transport)
318 struct iscsi_np *np;
319 bool match;
321 list_for_each_entry(np, &g_np_list, np_list) {
322 spin_lock_bh(&np->np_thread_lock);
323 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
324 spin_unlock_bh(&np->np_thread_lock);
325 continue;
328 match = iscsit_check_np_match(sockaddr, np, network_transport);
329 if (match) {
331 * Increment the np_exports reference count now to
332 * prevent iscsit_del_np() below from being called
333 * while iscsi_tpg_add_network_portal() is called.
335 np->np_exports++;
336 spin_unlock_bh(&np->np_thread_lock);
337 return np;
339 spin_unlock_bh(&np->np_thread_lock);
342 return NULL;
345 struct iscsi_np *iscsit_add_np(
346 struct sockaddr_storage *sockaddr,
347 int network_transport)
349 struct iscsi_np *np;
350 int ret;
352 mutex_lock(&np_lock);
355 * Locate the existing struct iscsi_np if already active..
357 np = iscsit_get_np(sockaddr, network_transport);
358 if (np) {
359 mutex_unlock(&np_lock);
360 return np;
363 np = kzalloc(sizeof(*np), GFP_KERNEL);
364 if (!np) {
365 mutex_unlock(&np_lock);
366 return ERR_PTR(-ENOMEM);
369 np->np_flags |= NPF_IP_NETWORK;
370 np->np_network_transport = network_transport;
371 spin_lock_init(&np->np_thread_lock);
372 init_completion(&np->np_restart_comp);
373 INIT_LIST_HEAD(&np->np_list);
375 timer_setup(&np->np_login_timer, iscsi_handle_login_thread_timeout, 0);
377 ret = iscsi_target_setup_login_socket(np, sockaddr);
378 if (ret != 0) {
379 kfree(np);
380 mutex_unlock(&np_lock);
381 return ERR_PTR(ret);
384 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
385 if (IS_ERR(np->np_thread)) {
386 pr_err("Unable to create kthread: iscsi_np\n");
387 ret = PTR_ERR(np->np_thread);
388 kfree(np);
389 mutex_unlock(&np_lock);
390 return ERR_PTR(ret);
393 * Increment the np_exports reference count now to prevent
394 * iscsit_del_np() below from being run while a new call to
395 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
396 * active. We don't need to hold np->np_thread_lock at this
397 * point because iscsi_np has not been added to g_np_list yet.
399 np->np_exports = 1;
400 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
402 list_add_tail(&np->np_list, &g_np_list);
403 mutex_unlock(&np_lock);
405 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
406 &np->np_sockaddr, np->np_transport->name);
408 return np;
411 int iscsit_reset_np_thread(
412 struct iscsi_np *np,
413 struct iscsi_tpg_np *tpg_np,
414 struct iscsi_portal_group *tpg,
415 bool shutdown)
417 spin_lock_bh(&np->np_thread_lock);
418 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
419 spin_unlock_bh(&np->np_thread_lock);
420 return 0;
422 np->np_thread_state = ISCSI_NP_THREAD_RESET;
423 atomic_inc(&np->np_reset_count);
425 if (np->np_thread) {
426 spin_unlock_bh(&np->np_thread_lock);
427 send_sig(SIGINT, np->np_thread, 1);
428 wait_for_completion(&np->np_restart_comp);
429 spin_lock_bh(&np->np_thread_lock);
431 spin_unlock_bh(&np->np_thread_lock);
433 if (tpg_np && shutdown) {
434 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
436 wait_for_completion(&tpg_np->tpg_np_comp);
439 return 0;
442 static void iscsit_free_np(struct iscsi_np *np)
444 if (np->np_socket)
445 sock_release(np->np_socket);
448 int iscsit_del_np(struct iscsi_np *np)
450 spin_lock_bh(&np->np_thread_lock);
451 np->np_exports--;
452 if (np->np_exports) {
453 np->enabled = true;
454 spin_unlock_bh(&np->np_thread_lock);
455 return 0;
457 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
458 spin_unlock_bh(&np->np_thread_lock);
460 if (np->np_thread) {
462 * We need to send the signal to wakeup Linux/Net
463 * which may be sleeping in sock_accept()..
465 send_sig(SIGINT, np->np_thread, 1);
466 kthread_stop(np->np_thread);
467 np->np_thread = NULL;
470 np->np_transport->iscsit_free_np(np);
472 mutex_lock(&np_lock);
473 list_del(&np->np_list);
474 mutex_unlock(&np_lock);
476 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
477 &np->np_sockaddr, np->np_transport->name);
479 iscsit_put_transport(np->np_transport);
480 kfree(np);
481 return 0;
484 static void iscsit_get_rx_pdu(struct iscsi_conn *);
486 int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
488 return iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
490 EXPORT_SYMBOL(iscsit_queue_rsp);
492 void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
494 spin_lock_bh(&conn->cmd_lock);
495 if (!list_empty(&cmd->i_conn_node) &&
496 !(cmd->se_cmd.transport_state & CMD_T_FABRIC_STOP))
497 list_del_init(&cmd->i_conn_node);
498 spin_unlock_bh(&conn->cmd_lock);
500 __iscsit_free_cmd(cmd, true);
502 EXPORT_SYMBOL(iscsit_aborted_task);
504 static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
505 u32, u32, const void *, void *);
506 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *);
508 static int
509 iscsit_xmit_nondatain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
510 const void *data_buf, u32 data_buf_len)
512 struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu;
513 struct kvec *iov;
514 u32 niov = 0, tx_size = ISCSI_HDR_LEN;
515 int ret;
517 iov = &cmd->iov_misc[0];
518 iov[niov].iov_base = cmd->pdu;
519 iov[niov++].iov_len = ISCSI_HDR_LEN;
521 if (conn->conn_ops->HeaderDigest) {
522 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
524 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
525 ISCSI_HDR_LEN, 0, NULL,
526 header_digest);
528 iov[0].iov_len += ISCSI_CRC_LEN;
529 tx_size += ISCSI_CRC_LEN;
530 pr_debug("Attaching CRC32C HeaderDigest"
531 " to opcode 0x%x 0x%08x\n",
532 hdr->opcode, *header_digest);
535 if (data_buf_len) {
536 u32 padding = ((-data_buf_len) & 3);
538 iov[niov].iov_base = (void *)data_buf;
539 iov[niov++].iov_len = data_buf_len;
540 tx_size += data_buf_len;
542 if (padding != 0) {
543 iov[niov].iov_base = &cmd->pad_bytes;
544 iov[niov++].iov_len = padding;
545 tx_size += padding;
546 pr_debug("Attaching %u additional"
547 " padding bytes.\n", padding);
550 if (conn->conn_ops->DataDigest) {
551 iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
552 data_buf, data_buf_len,
553 padding, &cmd->pad_bytes,
554 &cmd->data_crc);
556 iov[niov].iov_base = &cmd->data_crc;
557 iov[niov++].iov_len = ISCSI_CRC_LEN;
558 tx_size += ISCSI_CRC_LEN;
559 pr_debug("Attached DataDigest for %u"
560 " bytes opcode 0x%x, CRC 0x%08x\n",
561 data_buf_len, hdr->opcode, cmd->data_crc);
565 cmd->iov_misc_count = niov;
566 cmd->tx_size = tx_size;
568 ret = iscsit_send_tx_data(cmd, conn, 1);
569 if (ret < 0) {
570 iscsit_tx_thread_wait_for_tcp(conn);
571 return ret;
574 return 0;
577 static int iscsit_map_iovec(struct iscsi_cmd *, struct kvec *, u32, u32);
578 static void iscsit_unmap_iovec(struct iscsi_cmd *);
579 static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsi_cmd *,
580 u32, u32, u32, u8 *);
581 static int
582 iscsit_xmit_datain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
583 const struct iscsi_datain *datain)
585 struct kvec *iov;
586 u32 iov_count = 0, tx_size = 0;
587 int ret, iov_ret;
589 iov = &cmd->iov_data[0];
590 iov[iov_count].iov_base = cmd->pdu;
591 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
592 tx_size += ISCSI_HDR_LEN;
594 if (conn->conn_ops->HeaderDigest) {
595 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
597 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
598 ISCSI_HDR_LEN, 0, NULL,
599 header_digest);
601 iov[0].iov_len += ISCSI_CRC_LEN;
602 tx_size += ISCSI_CRC_LEN;
604 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
605 *header_digest);
608 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
609 datain->offset, datain->length);
610 if (iov_ret < 0)
611 return -1;
613 iov_count += iov_ret;
614 tx_size += datain->length;
616 cmd->padding = ((-datain->length) & 3);
617 if (cmd->padding) {
618 iov[iov_count].iov_base = cmd->pad_bytes;
619 iov[iov_count++].iov_len = cmd->padding;
620 tx_size += cmd->padding;
622 pr_debug("Attaching %u padding bytes\n", cmd->padding);
625 if (conn->conn_ops->DataDigest) {
626 cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
627 cmd, datain->offset,
628 datain->length,
629 cmd->padding,
630 cmd->pad_bytes);
632 iov[iov_count].iov_base = &cmd->data_crc;
633 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
634 tx_size += ISCSI_CRC_LEN;
636 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
637 datain->length + cmd->padding, cmd->data_crc);
640 cmd->iov_data_count = iov_count;
641 cmd->tx_size = tx_size;
643 ret = iscsit_fe_sendpage_sg(cmd, conn);
645 iscsit_unmap_iovec(cmd);
647 if (ret < 0) {
648 iscsit_tx_thread_wait_for_tcp(conn);
649 return ret;
652 return 0;
655 static int iscsit_xmit_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
656 struct iscsi_datain_req *dr, const void *buf,
657 u32 buf_len)
659 if (dr)
660 return iscsit_xmit_datain_pdu(conn, cmd, buf);
661 else
662 return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len);
665 static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
667 return TARGET_PROT_NORMAL;
670 static struct iscsit_transport iscsi_target_transport = {
671 .name = "iSCSI/TCP",
672 .transport_type = ISCSI_TCP,
673 .rdma_shutdown = false,
674 .owner = NULL,
675 .iscsit_setup_np = iscsit_setup_np,
676 .iscsit_accept_np = iscsit_accept_np,
677 .iscsit_free_np = iscsit_free_np,
678 .iscsit_get_login_rx = iscsit_get_login_rx,
679 .iscsit_put_login_tx = iscsit_put_login_tx,
680 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
681 .iscsit_immediate_queue = iscsit_immediate_queue,
682 .iscsit_response_queue = iscsit_response_queue,
683 .iscsit_queue_data_in = iscsit_queue_rsp,
684 .iscsit_queue_status = iscsit_queue_rsp,
685 .iscsit_aborted_task = iscsit_aborted_task,
686 .iscsit_xmit_pdu = iscsit_xmit_pdu,
687 .iscsit_get_rx_pdu = iscsit_get_rx_pdu,
688 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
691 static int __init iscsi_target_init_module(void)
693 int ret = 0, size;
695 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
696 iscsit_global = kzalloc(sizeof(*iscsit_global), GFP_KERNEL);
697 if (!iscsit_global)
698 return -1;
700 spin_lock_init(&iscsit_global->ts_bitmap_lock);
701 mutex_init(&auth_id_lock);
702 idr_init(&tiqn_idr);
704 ret = target_register_template(&iscsi_ops);
705 if (ret)
706 goto out;
708 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
709 iscsit_global->ts_bitmap = vzalloc(size);
710 if (!iscsit_global->ts_bitmap)
711 goto configfs_out;
713 lio_qr_cache = kmem_cache_create("lio_qr_cache",
714 sizeof(struct iscsi_queue_req),
715 __alignof__(struct iscsi_queue_req), 0, NULL);
716 if (!lio_qr_cache) {
717 pr_err("nable to kmem_cache_create() for"
718 " lio_qr_cache\n");
719 goto bitmap_out;
722 lio_dr_cache = kmem_cache_create("lio_dr_cache",
723 sizeof(struct iscsi_datain_req),
724 __alignof__(struct iscsi_datain_req), 0, NULL);
725 if (!lio_dr_cache) {
726 pr_err("Unable to kmem_cache_create() for"
727 " lio_dr_cache\n");
728 goto qr_out;
731 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
732 sizeof(struct iscsi_ooo_cmdsn),
733 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
734 if (!lio_ooo_cache) {
735 pr_err("Unable to kmem_cache_create() for"
736 " lio_ooo_cache\n");
737 goto dr_out;
740 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
741 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
742 0, NULL);
743 if (!lio_r2t_cache) {
744 pr_err("Unable to kmem_cache_create() for"
745 " lio_r2t_cache\n");
746 goto ooo_out;
749 iscsit_register_transport(&iscsi_target_transport);
751 if (iscsit_load_discovery_tpg() < 0)
752 goto r2t_out;
754 return ret;
755 r2t_out:
756 iscsit_unregister_transport(&iscsi_target_transport);
757 kmem_cache_destroy(lio_r2t_cache);
758 ooo_out:
759 kmem_cache_destroy(lio_ooo_cache);
760 dr_out:
761 kmem_cache_destroy(lio_dr_cache);
762 qr_out:
763 kmem_cache_destroy(lio_qr_cache);
764 bitmap_out:
765 vfree(iscsit_global->ts_bitmap);
766 configfs_out:
767 /* XXX: this probably wants it to be it's own unwind step.. */
768 if (iscsit_global->discovery_tpg)
769 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
770 target_unregister_template(&iscsi_ops);
771 out:
772 kfree(iscsit_global);
773 return -ENOMEM;
776 static void __exit iscsi_target_cleanup_module(void)
778 iscsit_release_discovery_tpg();
779 iscsit_unregister_transport(&iscsi_target_transport);
780 kmem_cache_destroy(lio_qr_cache);
781 kmem_cache_destroy(lio_dr_cache);
782 kmem_cache_destroy(lio_ooo_cache);
783 kmem_cache_destroy(lio_r2t_cache);
786 * Shutdown discovery sessions and disable discovery TPG
788 if (iscsit_global->discovery_tpg)
789 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
791 target_unregister_template(&iscsi_ops);
793 vfree(iscsit_global->ts_bitmap);
794 kfree(iscsit_global);
797 int iscsit_add_reject(
798 struct iscsi_conn *conn,
799 u8 reason,
800 unsigned char *buf)
802 struct iscsi_cmd *cmd;
804 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
805 if (!cmd)
806 return -1;
808 cmd->iscsi_opcode = ISCSI_OP_REJECT;
809 cmd->reject_reason = reason;
811 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
812 if (!cmd->buf_ptr) {
813 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
814 iscsit_free_cmd(cmd, false);
815 return -1;
818 spin_lock_bh(&conn->cmd_lock);
819 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
820 spin_unlock_bh(&conn->cmd_lock);
822 cmd->i_state = ISTATE_SEND_REJECT;
823 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
825 return -1;
827 EXPORT_SYMBOL(iscsit_add_reject);
829 static int iscsit_add_reject_from_cmd(
830 struct iscsi_cmd *cmd,
831 u8 reason,
832 bool add_to_conn,
833 unsigned char *buf)
835 struct iscsi_conn *conn;
836 const bool do_put = cmd->se_cmd.se_tfo != NULL;
838 if (!cmd->conn) {
839 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
840 cmd->init_task_tag);
841 return -1;
843 conn = cmd->conn;
845 cmd->iscsi_opcode = ISCSI_OP_REJECT;
846 cmd->reject_reason = reason;
848 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
849 if (!cmd->buf_ptr) {
850 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
851 iscsit_free_cmd(cmd, false);
852 return -1;
855 if (add_to_conn) {
856 spin_lock_bh(&conn->cmd_lock);
857 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
858 spin_unlock_bh(&conn->cmd_lock);
861 cmd->i_state = ISTATE_SEND_REJECT;
862 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
864 * Perform the kref_put now if se_cmd has already been setup by
865 * scsit_setup_scsi_cmd()
867 if (do_put) {
868 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
869 target_put_sess_cmd(&cmd->se_cmd);
871 return -1;
874 static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
875 unsigned char *buf)
877 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
880 int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
882 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
884 EXPORT_SYMBOL(iscsit_reject_cmd);
887 * Map some portion of the allocated scatterlist to an iovec, suitable for
888 * kernel sockets to copy data in/out.
890 static int iscsit_map_iovec(
891 struct iscsi_cmd *cmd,
892 struct kvec *iov,
893 u32 data_offset,
894 u32 data_length)
896 u32 i = 0;
897 struct scatterlist *sg;
898 unsigned int page_off;
901 * We know each entry in t_data_sg contains a page.
903 u32 ent = data_offset / PAGE_SIZE;
905 if (ent >= cmd->se_cmd.t_data_nents) {
906 pr_err("Initial page entry out-of-bounds\n");
907 return -1;
910 sg = &cmd->se_cmd.t_data_sg[ent];
911 page_off = (data_offset % PAGE_SIZE);
913 cmd->first_data_sg = sg;
914 cmd->first_data_sg_off = page_off;
916 while (data_length) {
917 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
919 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
920 iov[i].iov_len = cur_len;
922 data_length -= cur_len;
923 page_off = 0;
924 sg = sg_next(sg);
925 i++;
928 cmd->kmapped_nents = i;
930 return i;
933 static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
935 u32 i;
936 struct scatterlist *sg;
938 sg = cmd->first_data_sg;
940 for (i = 0; i < cmd->kmapped_nents; i++)
941 kunmap(sg_page(&sg[i]));
944 static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
946 LIST_HEAD(ack_list);
947 struct iscsi_cmd *cmd, *cmd_p;
949 conn->exp_statsn = exp_statsn;
951 if (conn->sess->sess_ops->RDMAExtensions)
952 return;
954 spin_lock_bh(&conn->cmd_lock);
955 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
956 spin_lock(&cmd->istate_lock);
957 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
958 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
959 cmd->i_state = ISTATE_REMOVE;
960 spin_unlock(&cmd->istate_lock);
961 list_move_tail(&cmd->i_conn_node, &ack_list);
962 continue;
964 spin_unlock(&cmd->istate_lock);
966 spin_unlock_bh(&conn->cmd_lock);
968 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
969 list_del_init(&cmd->i_conn_node);
970 iscsit_free_cmd(cmd, false);
974 static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
976 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
978 iov_count += ISCSI_IOV_DATA_BUFFER;
979 cmd->iov_data = kcalloc(iov_count, sizeof(*cmd->iov_data), GFP_KERNEL);
980 if (!cmd->iov_data)
981 return -ENOMEM;
983 cmd->orig_iov_data_count = iov_count;
984 return 0;
987 int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
988 unsigned char *buf)
990 int data_direction, payload_length;
991 struct iscsi_scsi_req *hdr;
992 int iscsi_task_attr;
993 int sam_task_attr;
995 atomic_long_inc(&conn->sess->cmd_pdus);
997 hdr = (struct iscsi_scsi_req *) buf;
998 payload_length = ntoh24(hdr->dlength);
1000 /* FIXME; Add checks for AdditionalHeaderSegment */
1002 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
1003 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1004 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1005 " not set. Bad iSCSI Initiator.\n");
1006 return iscsit_add_reject_cmd(cmd,
1007 ISCSI_REASON_BOOKMARK_INVALID, buf);
1010 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
1011 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
1013 * From RFC-3720 Section 10.3.1:
1015 * "Either or both of R and W MAY be 1 when either the
1016 * Expected Data Transfer Length and/or Bidirectional Read
1017 * Expected Data Transfer Length are 0"
1019 * For this case, go ahead and clear the unnecssary bits
1020 * to avoid any confusion with ->data_direction.
1022 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
1023 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
1025 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
1026 " set when Expected Data Transfer Length is 0 for"
1027 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
1030 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
1031 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
1032 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1033 " MUST be set if Expected Data Transfer Length is not 0."
1034 " Bad iSCSI Initiator\n");
1035 return iscsit_add_reject_cmd(cmd,
1036 ISCSI_REASON_BOOKMARK_INVALID, buf);
1039 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
1040 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
1041 pr_err("Bidirectional operations not supported!\n");
1042 return iscsit_add_reject_cmd(cmd,
1043 ISCSI_REASON_BOOKMARK_INVALID, buf);
1046 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1047 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1048 " Scsi Command PDU.\n");
1049 return iscsit_add_reject_cmd(cmd,
1050 ISCSI_REASON_BOOKMARK_INVALID, buf);
1053 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
1054 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1055 " protocol error.\n", payload_length);
1056 return iscsit_add_reject_cmd(cmd,
1057 ISCSI_REASON_PROTOCOL_ERROR, buf);
1060 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
1061 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
1062 pr_err("Expected Data Transfer Length and Length of"
1063 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1064 " bit is not set protocol error\n");
1065 return iscsit_add_reject_cmd(cmd,
1066 ISCSI_REASON_PROTOCOL_ERROR, buf);
1069 if (payload_length > be32_to_cpu(hdr->data_length)) {
1070 pr_err("DataSegmentLength: %u is greater than"
1071 " EDTL: %u, protocol error.\n", payload_length,
1072 hdr->data_length);
1073 return iscsit_add_reject_cmd(cmd,
1074 ISCSI_REASON_PROTOCOL_ERROR, buf);
1077 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1078 pr_err("DataSegmentLength: %u is greater than"
1079 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1080 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
1081 return iscsit_add_reject_cmd(cmd,
1082 ISCSI_REASON_PROTOCOL_ERROR, buf);
1085 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
1086 pr_err("DataSegmentLength: %u is greater than"
1087 " FirstBurstLength: %u, protocol error.\n",
1088 payload_length, conn->sess->sess_ops->FirstBurstLength);
1089 return iscsit_add_reject_cmd(cmd,
1090 ISCSI_REASON_BOOKMARK_INVALID, buf);
1093 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
1094 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
1095 DMA_NONE;
1097 cmd->data_direction = data_direction;
1098 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
1100 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1102 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
1103 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
1104 sam_task_attr = TCM_SIMPLE_TAG;
1105 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
1106 sam_task_attr = TCM_ORDERED_TAG;
1107 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
1108 sam_task_attr = TCM_HEAD_TAG;
1109 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
1110 sam_task_attr = TCM_ACA_TAG;
1111 else {
1112 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
1113 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
1114 sam_task_attr = TCM_SIMPLE_TAG;
1117 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
1118 cmd->i_state = ISTATE_NEW_CMD;
1119 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1120 cmd->immediate_data = (payload_length) ? 1 : 0;
1121 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
1122 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
1123 if (cmd->unsolicited_data)
1124 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
1126 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1127 if (hdr->flags & ISCSI_FLAG_CMD_READ)
1128 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
1129 else
1130 cmd->targ_xfer_tag = 0xFFFFFFFF;
1131 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1132 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1133 cmd->first_burst_len = payload_length;
1135 if (!conn->sess->sess_ops->RDMAExtensions &&
1136 cmd->data_direction == DMA_FROM_DEVICE) {
1137 struct iscsi_datain_req *dr;
1139 dr = iscsit_allocate_datain_req();
1140 if (!dr)
1141 return iscsit_add_reject_cmd(cmd,
1142 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1144 iscsit_attach_datain_req(cmd, dr);
1148 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1150 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
1151 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
1152 cmd->data_direction, sam_task_attr,
1153 cmd->sense_buffer + 2);
1155 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1156 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
1157 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1158 conn->cid);
1160 target_get_sess_cmd(&cmd->se_cmd, true);
1162 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1163 scsilun_to_int(&hdr->lun));
1164 if (cmd->sense_reason)
1165 goto attach_cmd;
1167 /* only used for printks or comparing with ->ref_task_tag */
1168 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
1169 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1170 if (cmd->sense_reason) {
1171 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
1172 return iscsit_add_reject_cmd(cmd,
1173 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1176 goto attach_cmd;
1179 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
1180 return iscsit_add_reject_cmd(cmd,
1181 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1184 attach_cmd:
1185 spin_lock_bh(&conn->cmd_lock);
1186 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1187 spin_unlock_bh(&conn->cmd_lock);
1189 * Check if we need to delay processing because of ALUA
1190 * Active/NonOptimized primary access state..
1192 core_alua_check_nonop_delay(&cmd->se_cmd);
1194 return 0;
1196 EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
1198 void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1200 iscsit_set_dataout_sequence_values(cmd);
1202 spin_lock_bh(&cmd->dataout_timeout_lock);
1203 iscsit_start_dataout_timer(cmd, cmd->conn);
1204 spin_unlock_bh(&cmd->dataout_timeout_lock);
1206 EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1208 int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1209 struct iscsi_scsi_req *hdr)
1211 int cmdsn_ret = 0;
1213 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1214 * the Immediate Bit is not set, and no Immediate
1215 * Data is attached.
1217 * A PDU/CmdSN carrying Immediate Data can only
1218 * be processed after the DataCRC has passed.
1219 * If the DataCRC fails, the CmdSN MUST NOT
1220 * be acknowledged. (See below)
1222 if (!cmd->immediate_data) {
1223 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1224 (unsigned char *)hdr, hdr->cmdsn);
1225 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1226 return -1;
1227 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1228 target_put_sess_cmd(&cmd->se_cmd);
1229 return 0;
1233 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1236 * If no Immediate Data is attached, it's OK to return now.
1238 if (!cmd->immediate_data) {
1239 if (!cmd->sense_reason && cmd->unsolicited_data)
1240 iscsit_set_unsoliticed_dataout(cmd);
1241 if (!cmd->sense_reason)
1242 return 0;
1244 target_put_sess_cmd(&cmd->se_cmd);
1245 return 0;
1249 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1250 * execution. These exceptions are processed in CmdSN order using
1251 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1253 if (cmd->sense_reason)
1254 return 1;
1256 * Call directly into transport_generic_new_cmd() to perform
1257 * the backend memory allocation.
1259 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
1260 if (cmd->sense_reason)
1261 return 1;
1263 return 0;
1265 EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1267 static int
1268 iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1269 bool dump_payload)
1271 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1273 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1275 if (dump_payload)
1276 goto after_immediate_data;
1278 * Check for underflow case where both EDTL and immediate data payload
1279 * exceeds what is presented by CDB's TRANSFER LENGTH, and what has
1280 * already been set in target_cmd_size_check() as se_cmd->data_length.
1282 * For this special case, fail the command and dump the immediate data
1283 * payload.
1285 if (cmd->first_burst_len > cmd->se_cmd.data_length) {
1286 cmd->sense_reason = TCM_INVALID_CDB_FIELD;
1287 goto after_immediate_data;
1290 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1291 cmd->first_burst_len);
1292 after_immediate_data:
1293 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1295 * A PDU/CmdSN carrying Immediate Data passed
1296 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1297 * Immediate Bit is not set.
1299 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1300 (unsigned char *)hdr, hdr->cmdsn);
1301 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1302 return -1;
1304 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1305 int rc;
1307 rc = iscsit_dump_data_payload(cmd->conn,
1308 cmd->first_burst_len, 1);
1309 target_put_sess_cmd(&cmd->se_cmd);
1310 return rc;
1311 } else if (cmd->unsolicited_data)
1312 iscsit_set_unsoliticed_dataout(cmd);
1314 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1316 * Immediate Data failed DataCRC and ERL>=1,
1317 * silently drop this PDU and let the initiator
1318 * plug the CmdSN gap.
1320 * FIXME: Send Unsolicited NOPIN with reserved
1321 * TTT here to help the initiator figure out
1322 * the missing CmdSN, although they should be
1323 * intelligent enough to determine the missing
1324 * CmdSN and issue a retry to plug the sequence.
1326 cmd->i_state = ISTATE_REMOVE;
1327 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
1328 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1329 return -1;
1331 return 0;
1334 static int
1335 iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1336 unsigned char *buf)
1338 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1339 int rc, immed_data;
1340 bool dump_payload = false;
1342 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1343 if (rc < 0)
1344 return 0;
1346 * Allocation iovecs needed for struct socket operations for
1347 * traditional iSCSI block I/O.
1349 if (iscsit_allocate_iovecs(cmd) < 0) {
1350 return iscsit_reject_cmd(cmd,
1351 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1353 immed_data = cmd->immediate_data;
1355 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1356 if (rc < 0)
1357 return rc;
1358 else if (rc > 0)
1359 dump_payload = true;
1361 if (!immed_data)
1362 return 0;
1364 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1367 static u32 iscsit_do_crypto_hash_sg(
1368 struct ahash_request *hash,
1369 struct iscsi_cmd *cmd,
1370 u32 data_offset,
1371 u32 data_length,
1372 u32 padding,
1373 u8 *pad_bytes)
1375 u32 data_crc;
1376 struct scatterlist *sg;
1377 unsigned int page_off;
1379 crypto_ahash_init(hash);
1381 sg = cmd->first_data_sg;
1382 page_off = cmd->first_data_sg_off;
1384 while (data_length) {
1385 u32 cur_len = min_t(u32, data_length, (sg->length - page_off));
1387 ahash_request_set_crypt(hash, sg, NULL, cur_len);
1388 crypto_ahash_update(hash);
1390 data_length -= cur_len;
1391 page_off = 0;
1392 /* iscsit_map_iovec has already checked for invalid sg pointers */
1393 sg = sg_next(sg);
1396 if (padding) {
1397 struct scatterlist pad_sg;
1399 sg_init_one(&pad_sg, pad_bytes, padding);
1400 ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
1401 padding);
1402 crypto_ahash_finup(hash);
1403 } else {
1404 ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
1405 crypto_ahash_final(hash);
1408 return data_crc;
1411 static void iscsit_do_crypto_hash_buf(struct ahash_request *hash,
1412 const void *buf, u32 payload_length, u32 padding,
1413 const void *pad_bytes, void *data_crc)
1415 struct scatterlist sg[2];
1417 sg_init_table(sg, ARRAY_SIZE(sg));
1418 sg_set_buf(sg, buf, payload_length);
1419 if (padding)
1420 sg_set_buf(sg + 1, pad_bytes, padding);
1422 ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
1424 crypto_ahash_digest(hash);
1428 __iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1429 struct iscsi_cmd *cmd, u32 payload_length,
1430 bool *success)
1432 struct iscsi_data *hdr = buf;
1433 struct se_cmd *se_cmd;
1434 int rc;
1436 /* iSCSI write */
1437 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
1439 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1440 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1441 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
1442 payload_length, conn->cid);
1444 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1445 pr_err("Command ITT: 0x%08x received DataOUT after"
1446 " last DataOUT received, dumping payload\n",
1447 cmd->init_task_tag);
1448 return iscsit_dump_data_payload(conn, payload_length, 1);
1451 if (cmd->data_direction != DMA_TO_DEVICE) {
1452 pr_err("Command ITT: 0x%08x received DataOUT for a"
1453 " NON-WRITE command.\n", cmd->init_task_tag);
1454 return iscsit_dump_data_payload(conn, payload_length, 1);
1456 se_cmd = &cmd->se_cmd;
1457 iscsit_mod_dataout_timer(cmd);
1459 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
1460 pr_err("DataOut Offset: %u, Length %u greater than iSCSI Command EDTL %u, protocol error.\n",
1461 be32_to_cpu(hdr->offset), payload_length,
1462 cmd->se_cmd.data_length);
1463 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
1466 if (cmd->unsolicited_data) {
1467 int dump_unsolicited_data = 0;
1469 if (conn->sess->sess_ops->InitialR2T) {
1470 pr_err("Received unexpected unsolicited data"
1471 " while InitialR2T=Yes, protocol error.\n");
1472 transport_send_check_condition_and_sense(&cmd->se_cmd,
1473 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1474 return -1;
1477 * Special case for dealing with Unsolicited DataOUT
1478 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1479 * failures;
1482 /* Something's amiss if we're not in WRITE_PENDING state... */
1483 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1484 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
1485 dump_unsolicited_data = 1;
1487 if (dump_unsolicited_data) {
1489 * Check if a delayed TASK_ABORTED status needs to
1490 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1491 * received with the unsolicited data out.
1493 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1494 iscsit_stop_dataout_timer(cmd);
1496 transport_check_aborted_status(se_cmd,
1497 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1498 return iscsit_dump_data_payload(conn, payload_length, 1);
1500 } else {
1502 * For the normal solicited data path:
1504 * Check for a delayed TASK_ABORTED status and dump any
1505 * incoming data out payload if one exists. Also, when the
1506 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1507 * data out sequence, we decrement outstanding_r2ts. Once
1508 * outstanding_r2ts reaches zero, go ahead and send the delayed
1509 * TASK_ABORTED status.
1511 if (se_cmd->transport_state & CMD_T_ABORTED) {
1512 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1513 if (--cmd->outstanding_r2ts < 1) {
1514 iscsit_stop_dataout_timer(cmd);
1515 transport_check_aborted_status(
1516 se_cmd, 1);
1519 return iscsit_dump_data_payload(conn, payload_length, 1);
1523 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1524 * within-command recovery checks before receiving the payload.
1526 rc = iscsit_check_pre_dataout(cmd, buf);
1527 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
1528 return 0;
1529 else if (rc == DATAOUT_CANNOT_RECOVER)
1530 return -1;
1531 *success = true;
1532 return 0;
1534 EXPORT_SYMBOL(__iscsit_check_dataout_hdr);
1537 iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1538 struct iscsi_cmd **out_cmd)
1540 struct iscsi_data *hdr = buf;
1541 struct iscsi_cmd *cmd;
1542 u32 payload_length = ntoh24(hdr->dlength);
1543 int rc;
1544 bool success = false;
1546 if (!payload_length) {
1547 pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
1548 return 0;
1551 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1552 pr_err_ratelimited("DataSegmentLength: %u is greater than"
1553 " MaxXmitDataSegmentLength: %u\n", payload_length,
1554 conn->conn_ops->MaxXmitDataSegmentLength);
1555 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buf);
1558 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, payload_length);
1559 if (!cmd)
1560 return 0;
1562 rc = __iscsit_check_dataout_hdr(conn, buf, cmd, payload_length, &success);
1564 if (success)
1565 *out_cmd = cmd;
1567 return rc;
1569 EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1571 static int
1572 iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1573 struct iscsi_data *hdr)
1575 struct kvec *iov;
1576 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1577 u32 payload_length = ntoh24(hdr->dlength);
1578 int iov_ret, data_crc_failed = 0;
1580 rx_size += payload_length;
1581 iov = &cmd->iov_data[0];
1583 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1584 payload_length);
1585 if (iov_ret < 0)
1586 return -1;
1588 iov_count += iov_ret;
1590 padding = ((-payload_length) & 3);
1591 if (padding != 0) {
1592 iov[iov_count].iov_base = cmd->pad_bytes;
1593 iov[iov_count++].iov_len = padding;
1594 rx_size += padding;
1595 pr_debug("Receiving %u padding bytes.\n", padding);
1598 if (conn->conn_ops->DataDigest) {
1599 iov[iov_count].iov_base = &checksum;
1600 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1601 rx_size += ISCSI_CRC_LEN;
1604 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1606 iscsit_unmap_iovec(cmd);
1608 if (rx_got != rx_size)
1609 return -1;
1611 if (conn->conn_ops->DataDigest) {
1612 u32 data_crc;
1614 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
1615 be32_to_cpu(hdr->offset),
1616 payload_length, padding,
1617 cmd->pad_bytes);
1619 if (checksum != data_crc) {
1620 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1621 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1622 " does not match computed 0x%08x\n",
1623 hdr->itt, hdr->offset, payload_length,
1624 hdr->datasn, checksum, data_crc);
1625 data_crc_failed = 1;
1626 } else {
1627 pr_debug("Got CRC32C DataDigest 0x%08x for"
1628 " %u bytes of Data Out\n", checksum,
1629 payload_length);
1633 return data_crc_failed;
1637 iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1638 bool data_crc_failed)
1640 struct iscsi_conn *conn = cmd->conn;
1641 int rc, ooo_cmdsn;
1643 * Increment post receive data and CRC values or perform
1644 * within-command recovery.
1646 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1647 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
1648 return 0;
1649 else if (rc == DATAOUT_SEND_R2T) {
1650 iscsit_set_dataout_sequence_values(cmd);
1651 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1652 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
1654 * Handle extra special case for out of order
1655 * Unsolicited Data Out.
1657 spin_lock_bh(&cmd->istate_lock);
1658 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1659 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1660 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1661 spin_unlock_bh(&cmd->istate_lock);
1663 iscsit_stop_dataout_timer(cmd);
1664 if (ooo_cmdsn)
1665 return 0;
1666 target_execute_cmd(&cmd->se_cmd);
1667 return 0;
1668 } else /* DATAOUT_CANNOT_RECOVER */
1669 return -1;
1671 return 0;
1673 EXPORT_SYMBOL(iscsit_check_dataout_payload);
1675 static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1677 struct iscsi_cmd *cmd = NULL;
1678 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1679 int rc;
1680 bool data_crc_failed = false;
1682 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1683 if (rc < 0)
1684 return 0;
1685 else if (!cmd)
1686 return 0;
1688 rc = iscsit_get_dataout(conn, cmd, hdr);
1689 if (rc < 0)
1690 return rc;
1691 else if (rc > 0)
1692 data_crc_failed = true;
1694 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1697 int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1698 struct iscsi_nopout *hdr)
1700 u32 payload_length = ntoh24(hdr->dlength);
1702 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1703 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1704 if (!cmd)
1705 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1706 (unsigned char *)hdr);
1708 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1709 (unsigned char *)hdr);
1712 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1713 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1714 " not set, protocol error.\n");
1715 if (!cmd)
1716 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1717 (unsigned char *)hdr);
1719 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1720 (unsigned char *)hdr);
1723 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1724 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1725 " greater than MaxXmitDataSegmentLength: %u, protocol"
1726 " error.\n", payload_length,
1727 conn->conn_ops->MaxXmitDataSegmentLength);
1728 if (!cmd)
1729 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1730 (unsigned char *)hdr);
1732 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1733 (unsigned char *)hdr);
1736 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1737 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1738 hdr->itt == RESERVED_ITT ? "Response" : "Request",
1739 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1740 payload_length);
1742 * This is not a response to a Unsolicited NopIN, which means
1743 * it can either be a NOPOUT ping request (with a valid ITT),
1744 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1745 * Either way, make sure we allocate an struct iscsi_cmd, as both
1746 * can contain ping data.
1748 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1749 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1750 cmd->i_state = ISTATE_SEND_NOPIN;
1751 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1752 1 : 0);
1753 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1754 cmd->targ_xfer_tag = 0xFFFFFFFF;
1755 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1756 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1757 cmd->data_direction = DMA_NONE;
1760 return 0;
1762 EXPORT_SYMBOL(iscsit_setup_nop_out);
1764 int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1765 struct iscsi_nopout *hdr)
1767 struct iscsi_cmd *cmd_p = NULL;
1768 int cmdsn_ret = 0;
1770 * Initiator is expecting a NopIN ping reply..
1772 if (hdr->itt != RESERVED_ITT) {
1773 if (!cmd)
1774 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1775 (unsigned char *)hdr);
1777 spin_lock_bh(&conn->cmd_lock);
1778 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1779 spin_unlock_bh(&conn->cmd_lock);
1781 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1783 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1784 iscsit_add_cmd_to_response_queue(cmd, conn,
1785 cmd->i_state);
1786 return 0;
1789 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1790 (unsigned char *)hdr, hdr->cmdsn);
1791 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1792 return 0;
1793 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1794 return -1;
1796 return 0;
1799 * This was a response to a unsolicited NOPIN ping.
1801 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1802 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1803 if (!cmd_p)
1804 return -EINVAL;
1806 iscsit_stop_nopin_response_timer(conn);
1808 cmd_p->i_state = ISTATE_REMOVE;
1809 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1811 iscsit_start_nopin_timer(conn);
1812 return 0;
1815 * Otherwise, initiator is not expecting a NOPIN is response.
1816 * Just ignore for now.
1819 if (cmd)
1820 iscsit_free_cmd(cmd, false);
1822 return 0;
1824 EXPORT_SYMBOL(iscsit_process_nop_out);
1826 static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1827 unsigned char *buf)
1829 unsigned char *ping_data = NULL;
1830 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1831 struct kvec *iov = NULL;
1832 u32 payload_length = ntoh24(hdr->dlength);
1833 int ret;
1835 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1836 if (ret < 0)
1837 return 0;
1839 * Handle NOP-OUT payload for traditional iSCSI sockets
1841 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1842 u32 checksum, data_crc, padding = 0;
1843 int niov = 0, rx_got, rx_size = payload_length;
1845 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1846 if (!ping_data) {
1847 ret = -1;
1848 goto out;
1851 iov = &cmd->iov_misc[0];
1852 iov[niov].iov_base = ping_data;
1853 iov[niov++].iov_len = payload_length;
1855 padding = ((-payload_length) & 3);
1856 if (padding != 0) {
1857 pr_debug("Receiving %u additional bytes"
1858 " for padding.\n", padding);
1859 iov[niov].iov_base = &cmd->pad_bytes;
1860 iov[niov++].iov_len = padding;
1861 rx_size += padding;
1863 if (conn->conn_ops->DataDigest) {
1864 iov[niov].iov_base = &checksum;
1865 iov[niov++].iov_len = ISCSI_CRC_LEN;
1866 rx_size += ISCSI_CRC_LEN;
1869 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1870 if (rx_got != rx_size) {
1871 ret = -1;
1872 goto out;
1875 if (conn->conn_ops->DataDigest) {
1876 iscsit_do_crypto_hash_buf(conn->conn_rx_hash, ping_data,
1877 payload_length, padding,
1878 cmd->pad_bytes, &data_crc);
1880 if (checksum != data_crc) {
1881 pr_err("Ping data CRC32C DataDigest"
1882 " 0x%08x does not match computed 0x%08x\n",
1883 checksum, data_crc);
1884 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1885 pr_err("Unable to recover from"
1886 " NOPOUT Ping DataCRC failure while in"
1887 " ERL=0.\n");
1888 ret = -1;
1889 goto out;
1890 } else {
1892 * Silently drop this PDU and let the
1893 * initiator plug the CmdSN gap.
1895 pr_debug("Dropping NOPOUT"
1896 " Command CmdSN: 0x%08x due to"
1897 " DataCRC error.\n", hdr->cmdsn);
1898 ret = 0;
1899 goto out;
1901 } else {
1902 pr_debug("Got CRC32C DataDigest"
1903 " 0x%08x for %u bytes of ping data.\n",
1904 checksum, payload_length);
1908 ping_data[payload_length] = '\0';
1910 * Attach ping data to struct iscsi_cmd->buf_ptr.
1912 cmd->buf_ptr = ping_data;
1913 cmd->buf_ptr_size = payload_length;
1915 pr_debug("Got %u bytes of NOPOUT ping"
1916 " data.\n", payload_length);
1917 pr_debug("Ping Data: \"%s\"\n", ping_data);
1920 return iscsit_process_nop_out(conn, cmd, hdr);
1921 out:
1922 if (cmd)
1923 iscsit_free_cmd(cmd, false);
1925 kfree(ping_data);
1926 return ret;
1929 static enum tcm_tmreq_table iscsit_convert_tmf(u8 iscsi_tmf)
1931 switch (iscsi_tmf) {
1932 case ISCSI_TM_FUNC_ABORT_TASK:
1933 return TMR_ABORT_TASK;
1934 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1935 return TMR_ABORT_TASK_SET;
1936 case ISCSI_TM_FUNC_CLEAR_ACA:
1937 return TMR_CLEAR_ACA;
1938 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1939 return TMR_CLEAR_TASK_SET;
1940 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1941 return TMR_LUN_RESET;
1942 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1943 return TMR_TARGET_WARM_RESET;
1944 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1945 return TMR_TARGET_COLD_RESET;
1946 default:
1947 return TMR_UNKNOWN;
1952 iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1953 unsigned char *buf)
1955 struct se_tmr_req *se_tmr;
1956 struct iscsi_tmr_req *tmr_req;
1957 struct iscsi_tm *hdr;
1958 int out_of_order_cmdsn = 0, ret;
1959 u8 function, tcm_function = TMR_UNKNOWN;
1961 hdr = (struct iscsi_tm *) buf;
1962 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1963 function = hdr->flags;
1965 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1966 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1967 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1968 hdr->rtt, hdr->refcmdsn, conn->cid);
1970 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1971 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1972 hdr->rtt != RESERVED_ITT)) {
1973 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1974 hdr->rtt = RESERVED_ITT;
1977 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1978 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1979 pr_err("Task Management Request TASK_REASSIGN not"
1980 " issued as immediate command, bad iSCSI Initiator"
1981 "implementation\n");
1982 return iscsit_add_reject_cmd(cmd,
1983 ISCSI_REASON_PROTOCOL_ERROR, buf);
1985 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1986 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1987 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
1989 cmd->data_direction = DMA_NONE;
1990 cmd->tmr_req = kzalloc(sizeof(*cmd->tmr_req), GFP_KERNEL);
1991 if (!cmd->tmr_req) {
1992 return iscsit_add_reject_cmd(cmd,
1993 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1994 buf);
1997 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
1998 conn->sess->se_sess, 0, DMA_NONE,
1999 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
2001 target_get_sess_cmd(&cmd->se_cmd, true);
2004 * TASK_REASSIGN for ERL=2 / connection stays inside of
2005 * LIO-Target $FABRIC_MOD
2007 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
2008 tcm_function = iscsit_convert_tmf(function);
2009 if (tcm_function == TMR_UNKNOWN) {
2010 pr_err("Unknown iSCSI TMR Function:"
2011 " 0x%02x\n", function);
2012 return iscsit_add_reject_cmd(cmd,
2013 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
2016 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, tcm_function,
2017 GFP_KERNEL);
2018 if (ret < 0)
2019 return iscsit_add_reject_cmd(cmd,
2020 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
2022 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
2024 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
2025 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
2026 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2027 cmd->init_task_tag = hdr->itt;
2028 cmd->targ_xfer_tag = 0xFFFFFFFF;
2029 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2030 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2031 se_tmr = cmd->se_cmd.se_tmr_req;
2032 tmr_req = cmd->tmr_req;
2034 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2036 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
2037 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
2038 scsilun_to_int(&hdr->lun));
2039 if (ret < 0) {
2040 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2041 goto attach;
2045 switch (function) {
2046 case ISCSI_TM_FUNC_ABORT_TASK:
2047 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
2048 if (se_tmr->response)
2049 goto attach;
2050 break;
2051 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2052 case ISCSI_TM_FUNC_CLEAR_ACA:
2053 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2054 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2055 break;
2056 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2057 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
2058 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2059 goto attach;
2061 break;
2062 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2063 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
2064 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2065 goto attach;
2067 break;
2068 case ISCSI_TM_FUNC_TASK_REASSIGN:
2069 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2071 * Perform sanity checks on the ExpDataSN only if the
2072 * TASK_REASSIGN was successful.
2074 if (se_tmr->response)
2075 break;
2077 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
2078 return iscsit_add_reject_cmd(cmd,
2079 ISCSI_REASON_BOOKMARK_INVALID, buf);
2080 break;
2081 default:
2082 pr_err("Unknown TMR function: 0x%02x, protocol"
2083 " error.\n", function);
2084 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2085 goto attach;
2088 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2089 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2090 se_tmr->call_transport = 1;
2091 attach:
2092 spin_lock_bh(&conn->cmd_lock);
2093 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2094 spin_unlock_bh(&conn->cmd_lock);
2096 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2097 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
2098 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP) {
2099 out_of_order_cmdsn = 1;
2100 } else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2101 target_put_sess_cmd(&cmd->se_cmd);
2102 return 0;
2103 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
2104 return -1;
2107 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2109 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
2110 return 0;
2112 * Found the referenced task, send to transport for processing.
2114 if (se_tmr->call_transport)
2115 return transport_generic_handle_tmr(&cmd->se_cmd);
2118 * Could not find the referenced LUN, task, or Task Management
2119 * command not authorized or supported. Change state and
2120 * let the tx_thread send the response.
2122 * For connection recovery, this is also the default action for
2123 * TMR TASK_REASSIGN.
2125 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2126 target_put_sess_cmd(&cmd->se_cmd);
2127 return 0;
2129 EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
2131 /* #warning FIXME: Support Text Command parameters besides SendTargets */
2133 iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2134 struct iscsi_text *hdr)
2136 u32 payload_length = ntoh24(hdr->dlength);
2138 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
2139 pr_err("Unable to accept text parameter length: %u"
2140 "greater than MaxXmitDataSegmentLength %u.\n",
2141 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
2142 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2143 (unsigned char *)hdr);
2146 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2147 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2148 pr_err("Multi sequence text commands currently not supported\n");
2149 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2150 (unsigned char *)hdr);
2153 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2154 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2155 hdr->exp_statsn, payload_length);
2157 cmd->iscsi_opcode = ISCSI_OP_TEXT;
2158 cmd->i_state = ISTATE_SEND_TEXTRSP;
2159 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2160 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2161 cmd->targ_xfer_tag = 0xFFFFFFFF;
2162 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2163 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2164 cmd->data_direction = DMA_NONE;
2165 kfree(cmd->text_in_ptr);
2166 cmd->text_in_ptr = NULL;
2168 return 0;
2170 EXPORT_SYMBOL(iscsit_setup_text_cmd);
2173 iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2174 struct iscsi_text *hdr)
2176 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
2177 int cmdsn_ret;
2179 if (!text_in) {
2180 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2181 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2182 pr_err("Unable to locate text_in buffer for sendtargets"
2183 " discovery\n");
2184 goto reject;
2186 goto empty_sendtargets;
2188 if (strncmp("SendTargets", text_in, 11) != 0) {
2189 pr_err("Received Text Data that is not"
2190 " SendTargets, cannot continue.\n");
2191 goto reject;
2193 text_ptr = strchr(text_in, '=');
2194 if (!text_ptr) {
2195 pr_err("No \"=\" separator found in Text Data,"
2196 " cannot continue.\n");
2197 goto reject;
2199 if (!strncmp("=All", text_ptr, 4)) {
2200 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
2201 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2202 !strncmp("=eui.", text_ptr, 5)) {
2203 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
2204 } else {
2205 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2206 goto reject;
2209 spin_lock_bh(&conn->cmd_lock);
2210 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2211 spin_unlock_bh(&conn->cmd_lock);
2213 empty_sendtargets:
2214 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2216 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2217 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2218 (unsigned char *)hdr, hdr->cmdsn);
2219 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2220 return -1;
2222 return 0;
2225 return iscsit_execute_cmd(cmd, 0);
2227 reject:
2228 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2229 (unsigned char *)hdr);
2231 EXPORT_SYMBOL(iscsit_process_text_cmd);
2233 static int
2234 iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2235 unsigned char *buf)
2237 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2238 char *text_in = NULL;
2239 u32 payload_length = ntoh24(hdr->dlength);
2240 int rx_size, rc;
2242 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2243 if (rc < 0)
2244 return 0;
2246 rx_size = payload_length;
2247 if (payload_length) {
2248 u32 checksum = 0, data_crc = 0;
2249 u32 padding = 0, pad_bytes = 0;
2250 int niov = 0, rx_got;
2251 struct kvec iov[3];
2253 text_in = kzalloc(payload_length, GFP_KERNEL);
2254 if (!text_in)
2255 goto reject;
2257 cmd->text_in_ptr = text_in;
2259 memset(iov, 0, 3 * sizeof(struct kvec));
2260 iov[niov].iov_base = text_in;
2261 iov[niov++].iov_len = payload_length;
2263 padding = ((-payload_length) & 3);
2264 if (padding != 0) {
2265 iov[niov].iov_base = &pad_bytes;
2266 iov[niov++].iov_len = padding;
2267 rx_size += padding;
2268 pr_debug("Receiving %u additional bytes"
2269 " for padding.\n", padding);
2271 if (conn->conn_ops->DataDigest) {
2272 iov[niov].iov_base = &checksum;
2273 iov[niov++].iov_len = ISCSI_CRC_LEN;
2274 rx_size += ISCSI_CRC_LEN;
2277 rx_got = rx_data(conn, &iov[0], niov, rx_size);
2278 if (rx_got != rx_size)
2279 goto reject;
2281 if (conn->conn_ops->DataDigest) {
2282 iscsit_do_crypto_hash_buf(conn->conn_rx_hash, text_in,
2283 payload_length, padding,
2284 &pad_bytes, &data_crc);
2286 if (checksum != data_crc) {
2287 pr_err("Text data CRC32C DataDigest"
2288 " 0x%08x does not match computed"
2289 " 0x%08x\n", checksum, data_crc);
2290 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2291 pr_err("Unable to recover from"
2292 " Text Data digest failure while in"
2293 " ERL=0.\n");
2294 goto reject;
2295 } else {
2297 * Silently drop this PDU and let the
2298 * initiator plug the CmdSN gap.
2300 pr_debug("Dropping Text"
2301 " Command CmdSN: 0x%08x due to"
2302 " DataCRC error.\n", hdr->cmdsn);
2303 kfree(text_in);
2304 return 0;
2306 } else {
2307 pr_debug("Got CRC32C DataDigest"
2308 " 0x%08x for %u bytes of text data.\n",
2309 checksum, payload_length);
2312 text_in[payload_length - 1] = '\0';
2313 pr_debug("Successfully read %d bytes of text"
2314 " data.\n", payload_length);
2317 return iscsit_process_text_cmd(conn, cmd, hdr);
2319 reject:
2320 kfree(cmd->text_in_ptr);
2321 cmd->text_in_ptr = NULL;
2322 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
2325 int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2327 struct iscsi_conn *conn_p;
2328 struct iscsi_session *sess = conn->sess;
2330 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2331 " for SID: %u.\n", conn->cid, conn->sess->sid);
2333 atomic_set(&sess->session_logout, 1);
2334 atomic_set(&conn->conn_logout_remove, 1);
2335 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2337 iscsit_inc_conn_usage_count(conn);
2338 iscsit_inc_session_usage_count(sess);
2340 spin_lock_bh(&sess->conn_lock);
2341 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2342 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2343 continue;
2345 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2346 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2348 spin_unlock_bh(&sess->conn_lock);
2350 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2352 return 0;
2355 int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2357 struct iscsi_conn *l_conn;
2358 struct iscsi_session *sess = conn->sess;
2360 pr_debug("Received logout request CLOSECONNECTION for CID:"
2361 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2364 * A Logout Request with a CLOSECONNECTION reason code for a CID
2365 * can arrive on a connection with a differing CID.
2367 if (conn->cid == cmd->logout_cid) {
2368 spin_lock_bh(&conn->state_lock);
2369 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2370 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2372 atomic_set(&conn->conn_logout_remove, 1);
2373 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2374 iscsit_inc_conn_usage_count(conn);
2376 spin_unlock_bh(&conn->state_lock);
2377 } else {
2379 * Handle all different cid CLOSECONNECTION requests in
2380 * iscsit_logout_post_handler_diffcid() as to give enough
2381 * time for any non immediate command's CmdSN to be
2382 * acknowledged on the connection in question.
2384 * Here we simply make sure the CID is still around.
2386 l_conn = iscsit_get_conn_from_cid(sess,
2387 cmd->logout_cid);
2388 if (!l_conn) {
2389 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2390 iscsit_add_cmd_to_response_queue(cmd, conn,
2391 cmd->i_state);
2392 return 0;
2395 iscsit_dec_conn_usage_count(l_conn);
2398 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2400 return 0;
2403 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2405 struct iscsi_session *sess = conn->sess;
2407 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2408 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2410 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2411 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2412 " while ERL!=2.\n");
2413 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2414 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2415 return 0;
2418 if (conn->cid == cmd->logout_cid) {
2419 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2420 " with CID: %hu on CID: %hu, implementation error.\n",
2421 cmd->logout_cid, conn->cid);
2422 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2423 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2424 return 0;
2427 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2429 return 0;
2433 iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2434 unsigned char *buf)
2436 int cmdsn_ret, logout_remove = 0;
2437 u8 reason_code = 0;
2438 struct iscsi_logout *hdr;
2439 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2441 hdr = (struct iscsi_logout *) buf;
2442 reason_code = (hdr->flags & 0x7f);
2444 if (tiqn) {
2445 spin_lock(&tiqn->logout_stats.lock);
2446 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2447 tiqn->logout_stats.normal_logouts++;
2448 else
2449 tiqn->logout_stats.abnormal_logouts++;
2450 spin_unlock(&tiqn->logout_stats.lock);
2453 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2454 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2455 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2456 hdr->cid, conn->cid);
2458 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2459 pr_err("Received logout request on connection that"
2460 " is not in logged in state, ignoring request.\n");
2461 iscsit_free_cmd(cmd, false);
2462 return 0;
2465 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2466 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2467 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2468 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2469 cmd->targ_xfer_tag = 0xFFFFFFFF;
2470 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2471 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2472 cmd->logout_cid = be16_to_cpu(hdr->cid);
2473 cmd->logout_reason = reason_code;
2474 cmd->data_direction = DMA_NONE;
2477 * We need to sleep in these cases (by returning 1) until the Logout
2478 * Response gets sent in the tx thread.
2480 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2481 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
2482 be16_to_cpu(hdr->cid) == conn->cid))
2483 logout_remove = 1;
2485 spin_lock_bh(&conn->cmd_lock);
2486 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2487 spin_unlock_bh(&conn->cmd_lock);
2489 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
2490 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2493 * Immediate commands are executed, well, immediately.
2494 * Non-Immediate Logout Commands are executed in CmdSN order.
2496 if (cmd->immediate_cmd) {
2497 int ret = iscsit_execute_cmd(cmd, 0);
2499 if (ret < 0)
2500 return ret;
2501 } else {
2502 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
2503 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
2504 logout_remove = 0;
2505 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2506 return -1;
2509 return logout_remove;
2511 EXPORT_SYMBOL(iscsit_handle_logout_cmd);
2513 int iscsit_handle_snack(
2514 struct iscsi_conn *conn,
2515 unsigned char *buf)
2517 struct iscsi_snack *hdr;
2519 hdr = (struct iscsi_snack *) buf;
2520 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
2522 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2523 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2524 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2525 hdr->begrun, hdr->runlength, conn->cid);
2527 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2528 pr_err("Initiator sent SNACK request while in"
2529 " ErrorRecoveryLevel=0.\n");
2530 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2531 buf);
2534 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2535 * call from inside iscsi_send_recovery_datain_or_r2t().
2537 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2538 case 0:
2539 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
2540 hdr->itt,
2541 be32_to_cpu(hdr->ttt),
2542 be32_to_cpu(hdr->begrun),
2543 be32_to_cpu(hdr->runlength));
2544 case ISCSI_FLAG_SNACK_TYPE_STATUS:
2545 return iscsit_handle_status_snack(conn, hdr->itt,
2546 be32_to_cpu(hdr->ttt),
2547 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
2548 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
2549 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2550 be32_to_cpu(hdr->begrun),
2551 be32_to_cpu(hdr->runlength));
2552 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2553 /* FIXME: Support R-Data SNACK */
2554 pr_err("R-Data SNACK Not Supported.\n");
2555 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2556 buf);
2557 default:
2558 pr_err("Unknown SNACK type 0x%02x, protocol"
2559 " error.\n", hdr->flags & 0x0f);
2560 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2561 buf);
2564 return 0;
2566 EXPORT_SYMBOL(iscsit_handle_snack);
2568 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2570 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2571 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2572 wait_for_completion_interruptible_timeout(
2573 &conn->rx_half_close_comp,
2574 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2578 static int iscsit_handle_immediate_data(
2579 struct iscsi_cmd *cmd,
2580 struct iscsi_scsi_req *hdr,
2581 u32 length)
2583 int iov_ret, rx_got = 0, rx_size = 0;
2584 u32 checksum, iov_count = 0, padding = 0;
2585 struct iscsi_conn *conn = cmd->conn;
2586 struct kvec *iov;
2588 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2589 if (iov_ret < 0)
2590 return IMMEDIATE_DATA_CANNOT_RECOVER;
2592 rx_size = length;
2593 iov_count = iov_ret;
2594 iov = &cmd->iov_data[0];
2596 padding = ((-length) & 3);
2597 if (padding != 0) {
2598 iov[iov_count].iov_base = cmd->pad_bytes;
2599 iov[iov_count++].iov_len = padding;
2600 rx_size += padding;
2603 if (conn->conn_ops->DataDigest) {
2604 iov[iov_count].iov_base = &checksum;
2605 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2606 rx_size += ISCSI_CRC_LEN;
2609 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2611 iscsit_unmap_iovec(cmd);
2613 if (rx_got != rx_size) {
2614 iscsit_rx_thread_wait_for_tcp(conn);
2615 return IMMEDIATE_DATA_CANNOT_RECOVER;
2618 if (conn->conn_ops->DataDigest) {
2619 u32 data_crc;
2621 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
2622 cmd->write_data_done, length, padding,
2623 cmd->pad_bytes);
2625 if (checksum != data_crc) {
2626 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2627 " does not match computed 0x%08x\n", checksum,
2628 data_crc);
2630 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2631 pr_err("Unable to recover from"
2632 " Immediate Data digest failure while"
2633 " in ERL=0.\n");
2634 iscsit_reject_cmd(cmd,
2635 ISCSI_REASON_DATA_DIGEST_ERROR,
2636 (unsigned char *)hdr);
2637 return IMMEDIATE_DATA_CANNOT_RECOVER;
2638 } else {
2639 iscsit_reject_cmd(cmd,
2640 ISCSI_REASON_DATA_DIGEST_ERROR,
2641 (unsigned char *)hdr);
2642 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2644 } else {
2645 pr_debug("Got CRC32C DataDigest 0x%08x for"
2646 " %u bytes of Immediate Data\n", checksum,
2647 length);
2651 cmd->write_data_done += length;
2653 if (cmd->write_data_done == cmd->se_cmd.data_length) {
2654 spin_lock_bh(&cmd->istate_lock);
2655 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2656 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2657 spin_unlock_bh(&cmd->istate_lock);
2660 return IMMEDIATE_DATA_NORMAL_OPERATION;
2664 * Called with sess->conn_lock held.
2666 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2667 with active network interface */
2668 static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2670 struct iscsi_cmd *cmd;
2671 struct iscsi_conn *conn_p;
2672 bool found = false;
2675 * Only send a Asynchronous Message on connections whos network
2676 * interface is still functional.
2678 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2679 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2680 iscsit_inc_conn_usage_count(conn_p);
2681 found = true;
2682 break;
2686 if (!found)
2687 return;
2689 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
2690 if (!cmd) {
2691 iscsit_dec_conn_usage_count(conn_p);
2692 return;
2695 cmd->logout_cid = conn->cid;
2696 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2697 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2699 spin_lock_bh(&conn_p->cmd_lock);
2700 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
2701 spin_unlock_bh(&conn_p->cmd_lock);
2703 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2704 iscsit_dec_conn_usage_count(conn_p);
2707 static int iscsit_send_conn_drop_async_message(
2708 struct iscsi_cmd *cmd,
2709 struct iscsi_conn *conn)
2711 struct iscsi_async *hdr;
2713 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2715 hdr = (struct iscsi_async *) cmd->pdu;
2716 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2717 hdr->flags = ISCSI_FLAG_CMD_FINAL;
2718 cmd->init_task_tag = RESERVED_ITT;
2719 cmd->targ_xfer_tag = 0xFFFFFFFF;
2720 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2721 cmd->stat_sn = conn->stat_sn++;
2722 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2723 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2724 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2725 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2726 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2727 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2728 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2730 pr_debug("Sending Connection Dropped Async Message StatSN:"
2731 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2732 cmd->logout_cid, conn->cid);
2734 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
2737 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2739 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2740 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2741 wait_for_completion_interruptible_timeout(
2742 &conn->tx_half_close_comp,
2743 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2747 void
2748 iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2749 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2750 bool set_statsn)
2752 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2753 hdr->flags = datain->flags;
2754 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2755 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2756 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2757 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2758 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2759 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2760 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2763 hton24(hdr->dlength, datain->length);
2764 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2765 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2766 (struct scsi_lun *)&hdr->lun);
2767 else
2768 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2770 hdr->itt = cmd->init_task_tag;
2772 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2773 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2774 else
2775 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2776 if (set_statsn)
2777 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2778 else
2779 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2781 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2782 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2783 hdr->datasn = cpu_to_be32(datain->data_sn);
2784 hdr->offset = cpu_to_be32(datain->offset);
2786 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2787 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2788 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2789 ntohl(hdr->offset), datain->length, conn->cid);
2791 EXPORT_SYMBOL(iscsit_build_datain_pdu);
2793 static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2795 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
2796 struct iscsi_datain datain;
2797 struct iscsi_datain_req *dr;
2798 int eodr = 0, ret;
2799 bool set_statsn = false;
2801 memset(&datain, 0, sizeof(struct iscsi_datain));
2802 dr = iscsit_get_datain_values(cmd, &datain);
2803 if (!dr) {
2804 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2805 cmd->init_task_tag);
2806 return -1;
2809 * Be paranoid and double check the logic for now.
2811 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
2812 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2813 " datain.length: %u exceeds cmd->data_length: %u\n",
2814 cmd->init_task_tag, datain.offset, datain.length,
2815 cmd->se_cmd.data_length);
2816 return -1;
2819 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
2821 * Special case for successfully execution w/ both DATAIN
2822 * and Sense Data.
2824 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2825 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2826 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2827 else {
2828 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2829 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2830 iscsit_increment_maxcmdsn(cmd, conn->sess);
2831 cmd->stat_sn = conn->stat_sn++;
2832 set_statsn = true;
2833 } else if (dr->dr_complete ==
2834 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2835 set_statsn = true;
2838 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
2840 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
2841 if (ret < 0)
2842 return ret;
2844 if (dr->dr_complete) {
2845 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
2846 2 : 1;
2847 iscsit_free_datain_req(cmd, dr);
2850 return eodr;
2854 iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2855 struct iscsi_logout_rsp *hdr)
2857 struct iscsi_conn *logout_conn = NULL;
2858 struct iscsi_conn_recovery *cr = NULL;
2859 struct iscsi_session *sess = conn->sess;
2861 * The actual shutting down of Sessions and/or Connections
2862 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2863 * is done in scsi_logout_post_handler().
2865 switch (cmd->logout_reason) {
2866 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2867 pr_debug("iSCSI session logout successful, setting"
2868 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2869 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2870 break;
2871 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2872 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2873 break;
2875 * For CLOSECONNECTION logout requests carrying
2876 * a matching logout CID -> local CID, the reference
2877 * for the local CID will have been incremented in
2878 * iscsi_logout_closeconnection().
2880 * For CLOSECONNECTION logout requests carrying
2881 * a different CID than the connection it arrived
2882 * on, the connection responding to cmd->logout_cid
2883 * is stopped in iscsit_logout_post_handler_diffcid().
2886 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2887 " successful.\n", cmd->logout_cid, conn->cid);
2888 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2889 break;
2890 case ISCSI_LOGOUT_REASON_RECOVERY:
2891 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2892 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2893 break;
2895 * If the connection is still active from our point of view
2896 * force connection recovery to occur.
2898 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2899 cmd->logout_cid);
2900 if (logout_conn) {
2901 iscsit_connection_reinstatement_rcfr(logout_conn);
2902 iscsit_dec_conn_usage_count(logout_conn);
2905 cr = iscsit_get_inactive_connection_recovery_entry(
2906 conn->sess, cmd->logout_cid);
2907 if (!cr) {
2908 pr_err("Unable to locate CID: %hu for"
2909 " REMOVECONNFORRECOVERY Logout Request.\n",
2910 cmd->logout_cid);
2911 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2912 break;
2915 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2917 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2918 " for recovery for CID: %hu on CID: %hu successful.\n",
2919 cmd->logout_cid, conn->cid);
2920 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2921 break;
2922 default:
2923 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2924 cmd->logout_reason);
2925 return -1;
2928 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2929 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2930 hdr->response = cmd->logout_response;
2931 hdr->itt = cmd->init_task_tag;
2932 cmd->stat_sn = conn->stat_sn++;
2933 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2935 iscsit_increment_maxcmdsn(cmd, conn->sess);
2936 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2937 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2939 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2940 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2941 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2942 cmd->logout_cid, conn->cid);
2944 return 0;
2946 EXPORT_SYMBOL(iscsit_build_logout_rsp);
2948 static int
2949 iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2951 int rc;
2953 rc = iscsit_build_logout_rsp(cmd, conn,
2954 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2955 if (rc < 0)
2956 return rc;
2958 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
2961 void
2962 iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2963 struct iscsi_nopin *hdr, bool nopout_response)
2965 hdr->opcode = ISCSI_OP_NOOP_IN;
2966 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2967 hton24(hdr->dlength, cmd->buf_ptr_size);
2968 if (nopout_response)
2969 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2970 hdr->itt = cmd->init_task_tag;
2971 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2972 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2973 conn->stat_sn;
2974 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2976 if (nopout_response)
2977 iscsit_increment_maxcmdsn(cmd, conn->sess);
2979 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2980 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2982 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2983 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2984 "Solicited" : "Unsolicited", cmd->init_task_tag,
2985 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2987 EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2990 * Unsolicited NOPIN, either requesting a response or not.
2992 static int iscsit_send_unsolicited_nopin(
2993 struct iscsi_cmd *cmd,
2994 struct iscsi_conn *conn,
2995 int want_response)
2997 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2998 int ret;
3000 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
3002 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
3003 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
3005 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3006 if (ret < 0)
3007 return ret;
3009 spin_lock_bh(&cmd->istate_lock);
3010 cmd->i_state = want_response ?
3011 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
3012 spin_unlock_bh(&cmd->istate_lock);
3014 return 0;
3017 static int
3018 iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3020 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
3022 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
3025 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3026 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3028 pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
3030 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3031 cmd->buf_ptr,
3032 cmd->buf_ptr_size);
3035 static int iscsit_send_r2t(
3036 struct iscsi_cmd *cmd,
3037 struct iscsi_conn *conn)
3039 struct iscsi_r2t *r2t;
3040 struct iscsi_r2t_rsp *hdr;
3041 int ret;
3043 r2t = iscsit_get_r2t_from_list(cmd);
3044 if (!r2t)
3045 return -1;
3047 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3048 memset(hdr, 0, ISCSI_HDR_LEN);
3049 hdr->opcode = ISCSI_OP_R2T;
3050 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3051 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3052 (struct scsi_lun *)&hdr->lun);
3053 hdr->itt = cmd->init_task_tag;
3054 if (conn->conn_transport->iscsit_get_r2t_ttt)
3055 conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
3056 else
3057 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
3058 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3059 hdr->statsn = cpu_to_be32(conn->stat_sn);
3060 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3061 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3062 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3063 hdr->data_offset = cpu_to_be32(r2t->offset);
3064 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3066 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3067 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3068 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3069 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3070 r2t->offset, r2t->xfer_len, conn->cid);
3072 spin_lock_bh(&cmd->r2t_lock);
3073 r2t->sent_r2t = 1;
3074 spin_unlock_bh(&cmd->r2t_lock);
3076 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3077 if (ret < 0) {
3078 return ret;
3081 spin_lock_bh(&cmd->dataout_timeout_lock);
3082 iscsit_start_dataout_timer(cmd, conn);
3083 spin_unlock_bh(&cmd->dataout_timeout_lock);
3085 return 0;
3089 * @recovery: If called from iscsi_task_reassign_complete_write() for
3090 * connection recovery.
3092 int iscsit_build_r2ts_for_cmd(
3093 struct iscsi_conn *conn,
3094 struct iscsi_cmd *cmd,
3095 bool recovery)
3097 int first_r2t = 1;
3098 u32 offset = 0, xfer_len = 0;
3100 spin_lock_bh(&cmd->r2t_lock);
3101 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3102 spin_unlock_bh(&cmd->r2t_lock);
3103 return 0;
3106 if (conn->sess->sess_ops->DataSequenceInOrder &&
3107 !recovery)
3108 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
3110 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3111 if (conn->sess->sess_ops->DataSequenceInOrder) {
3112 offset = cmd->r2t_offset;
3114 if (first_r2t && recovery) {
3115 int new_data_end = offset +
3116 conn->sess->sess_ops->MaxBurstLength -
3117 cmd->next_burst_len;
3119 if (new_data_end > cmd->se_cmd.data_length)
3120 xfer_len = cmd->se_cmd.data_length - offset;
3121 else
3122 xfer_len =
3123 conn->sess->sess_ops->MaxBurstLength -
3124 cmd->next_burst_len;
3125 } else {
3126 int new_data_end = offset +
3127 conn->sess->sess_ops->MaxBurstLength;
3129 if (new_data_end > cmd->se_cmd.data_length)
3130 xfer_len = cmd->se_cmd.data_length - offset;
3131 else
3132 xfer_len = conn->sess->sess_ops->MaxBurstLength;
3134 cmd->r2t_offset += xfer_len;
3136 if (cmd->r2t_offset == cmd->se_cmd.data_length)
3137 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3138 } else {
3139 struct iscsi_seq *seq;
3141 seq = iscsit_get_seq_holder_for_r2t(cmd);
3142 if (!seq) {
3143 spin_unlock_bh(&cmd->r2t_lock);
3144 return -1;
3147 offset = seq->offset;
3148 xfer_len = seq->xfer_len;
3150 if (cmd->seq_send_order == cmd->seq_count)
3151 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3153 cmd->outstanding_r2ts++;
3154 first_r2t = 0;
3156 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3157 spin_unlock_bh(&cmd->r2t_lock);
3158 return -1;
3161 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3162 break;
3164 spin_unlock_bh(&cmd->r2t_lock);
3166 return 0;
3168 EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
3170 void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3171 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
3173 if (inc_stat_sn)
3174 cmd->stat_sn = conn->stat_sn++;
3176 atomic_long_inc(&conn->sess->rsp_pdus);
3178 memset(hdr, 0, ISCSI_HDR_LEN);
3179 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3180 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3181 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3182 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
3183 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3184 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3185 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
3186 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3188 hdr->response = cmd->iscsi_response;
3189 hdr->cmd_status = cmd->se_cmd.scsi_status;
3190 hdr->itt = cmd->init_task_tag;
3191 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3193 iscsit_increment_maxcmdsn(cmd, conn->sess);
3194 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3195 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3197 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3198 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3199 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3200 cmd->se_cmd.scsi_status, conn->cid);
3202 EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3204 static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3206 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3207 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3208 void *data_buf = NULL;
3209 u32 padding = 0, data_buf_len = 0;
3211 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3214 * Attach SENSE DATA payload to iSCSI Response PDU
3216 if (cmd->se_cmd.sense_buffer &&
3217 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3218 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
3219 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3220 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3222 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
3223 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
3224 data_buf = cmd->sense_buffer;
3225 data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
3227 if (padding) {
3228 memset(cmd->sense_buffer +
3229 cmd->se_cmd.scsi_sense_length, 0, padding);
3230 pr_debug("Adding %u bytes of padding to"
3231 " SENSE.\n", padding);
3234 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3235 " Response PDU\n",
3236 cmd->se_cmd.scsi_sense_length);
3239 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
3240 data_buf_len);
3243 static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3245 switch (se_tmr->response) {
3246 case TMR_FUNCTION_COMPLETE:
3247 return ISCSI_TMF_RSP_COMPLETE;
3248 case TMR_TASK_DOES_NOT_EXIST:
3249 return ISCSI_TMF_RSP_NO_TASK;
3250 case TMR_LUN_DOES_NOT_EXIST:
3251 return ISCSI_TMF_RSP_NO_LUN;
3252 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3253 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3254 case TMR_FUNCTION_REJECTED:
3255 default:
3256 return ISCSI_TMF_RSP_REJECTED;
3260 void
3261 iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3262 struct iscsi_tm_rsp *hdr)
3264 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3266 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
3267 hdr->flags = ISCSI_FLAG_CMD_FINAL;
3268 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
3269 hdr->itt = cmd->init_task_tag;
3270 cmd->stat_sn = conn->stat_sn++;
3271 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3273 iscsit_increment_maxcmdsn(cmd, conn->sess);
3274 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3275 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3277 pr_debug("Built Task Management Response ITT: 0x%08x,"
3278 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3279 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3281 EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3283 static int
3284 iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3286 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3288 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3290 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3293 #define SENDTARGETS_BUF_LIMIT 32768U
3295 static int
3296 iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
3297 enum iscsit_transport_type network_transport,
3298 int skip_bytes, bool *completed)
3300 char *payload = NULL;
3301 struct iscsi_conn *conn = cmd->conn;
3302 struct iscsi_portal_group *tpg;
3303 struct iscsi_tiqn *tiqn;
3304 struct iscsi_tpg_np *tpg_np;
3305 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
3306 int target_name_printed;
3307 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
3308 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
3309 bool active;
3311 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
3312 SENDTARGETS_BUF_LIMIT);
3314 payload = kzalloc(buffer_len, GFP_KERNEL);
3315 if (!payload)
3316 return -ENOMEM;
3319 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
3320 * explicit case..
3322 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
3323 text_ptr = strchr(text_in, '=');
3324 if (!text_ptr) {
3325 pr_err("Unable to locate '=' string in text_in:"
3326 " %s\n", text_in);
3327 kfree(payload);
3328 return -EINVAL;
3331 * Skip over '=' character..
3333 text_ptr += 1;
3336 spin_lock(&tiqn_lock);
3337 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3338 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
3339 strcmp(tiqn->tiqn, text_ptr)) {
3340 continue;
3343 target_name_printed = 0;
3345 spin_lock(&tiqn->tiqn_tpg_lock);
3346 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3348 /* If demo_mode_discovery=0 and generate_node_acls=0
3349 * (demo mode dislabed) do not return
3350 * TargetName+TargetAddress unless a NodeACL exists.
3353 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3354 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3355 (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
3356 cmd->conn->sess->sess_ops->InitiatorName))) {
3357 continue;
3360 spin_lock(&tpg->tpg_state_lock);
3361 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
3362 spin_unlock(&tpg->tpg_state_lock);
3364 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3365 continue;
3367 spin_lock(&tpg->tpg_np_lock);
3368 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3369 tpg_np_list) {
3370 struct iscsi_np *np = tpg_np->tpg_np;
3371 struct sockaddr_storage *sockaddr;
3373 if (np->np_network_transport != network_transport)
3374 continue;
3376 if (!target_name_printed) {
3377 len = sprintf(buf, "TargetName=%s",
3378 tiqn->tiqn);
3379 len += 1;
3381 if ((len + payload_len) > buffer_len) {
3382 spin_unlock(&tpg->tpg_np_lock);
3383 spin_unlock(&tiqn->tiqn_tpg_lock);
3384 end_of_buf = 1;
3385 goto eob;
3388 if (skip_bytes && len <= skip_bytes) {
3389 skip_bytes -= len;
3390 } else {
3391 memcpy(payload + payload_len, buf, len);
3392 payload_len += len;
3393 target_name_printed = 1;
3394 if (len > skip_bytes)
3395 skip_bytes = 0;
3399 if (inet_addr_is_any((struct sockaddr *)&np->np_sockaddr))
3400 sockaddr = &conn->local_sockaddr;
3401 else
3402 sockaddr = &np->np_sockaddr;
3404 len = sprintf(buf, "TargetAddress="
3405 "%pISpc,%hu",
3406 sockaddr,
3407 tpg->tpgt);
3408 len += 1;
3410 if ((len + payload_len) > buffer_len) {
3411 spin_unlock(&tpg->tpg_np_lock);
3412 spin_unlock(&tiqn->tiqn_tpg_lock);
3413 end_of_buf = 1;
3414 goto eob;
3417 if (skip_bytes && len <= skip_bytes) {
3418 skip_bytes -= len;
3419 } else {
3420 memcpy(payload + payload_len, buf, len);
3421 payload_len += len;
3422 if (len > skip_bytes)
3423 skip_bytes = 0;
3426 spin_unlock(&tpg->tpg_np_lock);
3428 spin_unlock(&tiqn->tiqn_tpg_lock);
3429 eob:
3430 if (end_of_buf) {
3431 *completed = false;
3432 break;
3435 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
3436 break;
3438 spin_unlock(&tiqn_lock);
3440 cmd->buf_ptr = payload;
3442 return payload_len;
3446 iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3447 struct iscsi_text_rsp *hdr,
3448 enum iscsit_transport_type network_transport)
3450 int text_length, padding;
3451 bool completed = true;
3453 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3454 cmd->read_data_done,
3455 &completed);
3456 if (text_length < 0)
3457 return text_length;
3459 if (completed) {
3460 hdr->flags = ISCSI_FLAG_CMD_FINAL;
3461 } else {
3462 hdr->flags = ISCSI_FLAG_TEXT_CONTINUE;
3463 cmd->read_data_done += text_length;
3464 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3465 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3467 hdr->opcode = ISCSI_OP_TEXT_RSP;
3468 padding = ((-text_length) & 3);
3469 hton24(hdr->dlength, text_length);
3470 hdr->itt = cmd->init_task_tag;
3471 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3472 cmd->stat_sn = conn->stat_sn++;
3473 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3475 iscsit_increment_maxcmdsn(cmd, conn->sess);
3477 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3478 * correctly increment MaxCmdSN for each response answering a
3479 * non immediate text request with a valid CmdSN.
3481 cmd->maxcmdsn_inc = 0;
3482 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3483 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3485 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3486 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3487 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3488 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3489 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
3491 return text_length + padding;
3493 EXPORT_SYMBOL(iscsit_build_text_rsp);
3495 static int iscsit_send_text_rsp(
3496 struct iscsi_cmd *cmd,
3497 struct iscsi_conn *conn)
3499 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
3500 int text_length;
3502 text_length = iscsit_build_text_rsp(cmd, conn, hdr,
3503 conn->conn_transport->transport_type);
3504 if (text_length < 0)
3505 return text_length;
3507 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3508 cmd->buf_ptr,
3509 text_length);
3512 void
3513 iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3514 struct iscsi_reject *hdr)
3516 hdr->opcode = ISCSI_OP_REJECT;
3517 hdr->reason = cmd->reject_reason;
3518 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3519 hton24(hdr->dlength, ISCSI_HDR_LEN);
3520 hdr->ffffffff = cpu_to_be32(0xffffffff);
3521 cmd->stat_sn = conn->stat_sn++;
3522 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3523 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3524 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3527 EXPORT_SYMBOL(iscsit_build_reject);
3529 static int iscsit_send_reject(
3530 struct iscsi_cmd *cmd,
3531 struct iscsi_conn *conn)
3533 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
3535 iscsit_build_reject(cmd, conn, hdr);
3537 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3538 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3540 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3541 cmd->buf_ptr,
3542 ISCSI_HDR_LEN);
3545 void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3547 int ord, cpu;
3549 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3550 * within iscsit_start_kthreads()
3552 * Here we use bitmap_id to determine which CPU that this
3553 * iSCSI connection's RX/TX threads will be scheduled to
3554 * execute upon.
3556 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
3557 for_each_online_cpu(cpu) {
3558 if (ord-- == 0) {
3559 cpumask_set_cpu(cpu, conn->conn_cpumask);
3560 return;
3564 * This should never be reached..
3566 dump_stack();
3567 cpumask_setall(conn->conn_cpumask);
3571 iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3573 int ret;
3575 switch (state) {
3576 case ISTATE_SEND_R2T:
3577 ret = iscsit_send_r2t(cmd, conn);
3578 if (ret < 0)
3579 goto err;
3580 break;
3581 case ISTATE_REMOVE:
3582 spin_lock_bh(&conn->cmd_lock);
3583 list_del_init(&cmd->i_conn_node);
3584 spin_unlock_bh(&conn->cmd_lock);
3586 iscsit_free_cmd(cmd, false);
3587 break;
3588 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3589 iscsit_mod_nopin_response_timer(conn);
3590 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3591 if (ret < 0)
3592 goto err;
3593 break;
3594 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3595 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3596 if (ret < 0)
3597 goto err;
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, state,
3603 conn->cid);
3604 goto err;
3607 return 0;
3609 err:
3610 return -1;
3612 EXPORT_SYMBOL(iscsit_immediate_queue);
3614 static int
3615 iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3617 struct iscsit_transport *t = conn->conn_transport;
3618 struct iscsi_queue_req *qr;
3619 struct iscsi_cmd *cmd;
3620 u8 state;
3621 int ret;
3623 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3624 atomic_set(&conn->check_immediate_queue, 0);
3625 cmd = qr->cmd;
3626 state = qr->state;
3627 kmem_cache_free(lio_qr_cache, qr);
3629 ret = t->iscsit_immediate_queue(conn, cmd, state);
3630 if (ret < 0)
3631 return ret;
3634 return 0;
3638 iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3640 int ret;
3642 check_rsp_state:
3643 switch (state) {
3644 case ISTATE_SEND_DATAIN:
3645 ret = iscsit_send_datain(cmd, conn);
3646 if (ret < 0)
3647 goto err;
3648 else if (!ret)
3649 /* more drs */
3650 goto check_rsp_state;
3651 else if (ret == 1) {
3652 /* all done */
3653 spin_lock_bh(&cmd->istate_lock);
3654 cmd->i_state = ISTATE_SENT_STATUS;
3655 spin_unlock_bh(&cmd->istate_lock);
3657 if (atomic_read(&conn->check_immediate_queue))
3658 return 1;
3660 return 0;
3661 } else if (ret == 2) {
3662 /* Still must send status,
3663 SCF_TRANSPORT_TASK_SENSE was set */
3664 spin_lock_bh(&cmd->istate_lock);
3665 cmd->i_state = ISTATE_SEND_STATUS;
3666 spin_unlock_bh(&cmd->istate_lock);
3667 state = ISTATE_SEND_STATUS;
3668 goto check_rsp_state;
3671 break;
3672 case ISTATE_SEND_STATUS:
3673 case ISTATE_SEND_STATUS_RECOVERY:
3674 ret = iscsit_send_response(cmd, conn);
3675 break;
3676 case ISTATE_SEND_LOGOUTRSP:
3677 ret = iscsit_send_logout(cmd, conn);
3678 break;
3679 case ISTATE_SEND_ASYNCMSG:
3680 ret = iscsit_send_conn_drop_async_message(
3681 cmd, conn);
3682 break;
3683 case ISTATE_SEND_NOPIN:
3684 ret = iscsit_send_nopin(cmd, conn);
3685 break;
3686 case ISTATE_SEND_REJECT:
3687 ret = iscsit_send_reject(cmd, conn);
3688 break;
3689 case ISTATE_SEND_TASKMGTRSP:
3690 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3691 if (ret != 0)
3692 break;
3693 ret = iscsit_tmr_post_handler(cmd, conn);
3694 if (ret != 0)
3695 iscsit_fall_back_to_erl0(conn->sess);
3696 break;
3697 case ISTATE_SEND_TEXTRSP:
3698 ret = iscsit_send_text_rsp(cmd, conn);
3699 break;
3700 default:
3701 pr_err("Unknown Opcode: 0x%02x ITT:"
3702 " 0x%08x, i_state: %d on CID: %hu\n",
3703 cmd->iscsi_opcode, cmd->init_task_tag,
3704 state, conn->cid);
3705 goto err;
3707 if (ret < 0)
3708 goto err;
3710 switch (state) {
3711 case ISTATE_SEND_LOGOUTRSP:
3712 if (!iscsit_logout_post_handler(cmd, conn))
3713 return -ECONNRESET;
3714 /* fall through */
3715 case ISTATE_SEND_STATUS:
3716 case ISTATE_SEND_ASYNCMSG:
3717 case ISTATE_SEND_NOPIN:
3718 case ISTATE_SEND_STATUS_RECOVERY:
3719 case ISTATE_SEND_TEXTRSP:
3720 case ISTATE_SEND_TASKMGTRSP:
3721 case ISTATE_SEND_REJECT:
3722 spin_lock_bh(&cmd->istate_lock);
3723 cmd->i_state = ISTATE_SENT_STATUS;
3724 spin_unlock_bh(&cmd->istate_lock);
3725 break;
3726 default:
3727 pr_err("Unknown Opcode: 0x%02x ITT:"
3728 " 0x%08x, i_state: %d on CID: %hu\n",
3729 cmd->iscsi_opcode, cmd->init_task_tag,
3730 cmd->i_state, conn->cid);
3731 goto err;
3734 if (atomic_read(&conn->check_immediate_queue))
3735 return 1;
3737 return 0;
3739 err:
3740 return -1;
3742 EXPORT_SYMBOL(iscsit_response_queue);
3744 static int iscsit_handle_response_queue(struct iscsi_conn *conn)
3746 struct iscsit_transport *t = conn->conn_transport;
3747 struct iscsi_queue_req *qr;
3748 struct iscsi_cmd *cmd;
3749 u8 state;
3750 int ret;
3752 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3753 cmd = qr->cmd;
3754 state = qr->state;
3755 kmem_cache_free(lio_qr_cache, qr);
3757 ret = t->iscsit_response_queue(conn, cmd, state);
3758 if (ret == 1 || ret < 0)
3759 return ret;
3762 return 0;
3765 int iscsi_target_tx_thread(void *arg)
3767 int ret = 0;
3768 struct iscsi_conn *conn = arg;
3769 bool conn_freed = false;
3772 * Allow ourselves to be interrupted by SIGINT so that a
3773 * connection recovery / failure event can be triggered externally.
3775 allow_signal(SIGINT);
3777 while (!kthread_should_stop()) {
3779 * Ensure that both TX and RX per connection kthreads
3780 * are scheduled to run on the same CPU.
3782 iscsit_thread_check_cpumask(conn, current, 1);
3784 wait_event_interruptible(conn->queues_wq,
3785 !iscsit_conn_all_queues_empty(conn));
3787 if (signal_pending(current))
3788 goto transport_err;
3790 get_immediate:
3791 ret = iscsit_handle_immediate_queue(conn);
3792 if (ret < 0)
3793 goto transport_err;
3795 ret = iscsit_handle_response_queue(conn);
3796 if (ret == 1) {
3797 goto get_immediate;
3798 } else if (ret == -ECONNRESET) {
3799 conn_freed = true;
3800 goto out;
3801 } else if (ret < 0) {
3802 goto transport_err;
3806 transport_err:
3808 * Avoid the normal connection failure code-path if this connection
3809 * is still within LOGIN mode, and iscsi_np process context is
3810 * responsible for cleaning up the early connection failure.
3812 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
3813 iscsit_take_action_for_connection_exit(conn, &conn_freed);
3814 out:
3815 if (!conn_freed) {
3816 while (!kthread_should_stop()) {
3817 msleep(100);
3820 return 0;
3823 static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3825 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3826 struct iscsi_cmd *cmd;
3827 int ret = 0;
3829 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3830 case ISCSI_OP_SCSI_CMD:
3831 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3832 if (!cmd)
3833 goto reject;
3835 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3836 break;
3837 case ISCSI_OP_SCSI_DATA_OUT:
3838 ret = iscsit_handle_data_out(conn, buf);
3839 break;
3840 case ISCSI_OP_NOOP_OUT:
3841 cmd = NULL;
3842 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
3843 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3844 if (!cmd)
3845 goto reject;
3847 ret = iscsit_handle_nop_out(conn, cmd, buf);
3848 break;
3849 case ISCSI_OP_SCSI_TMFUNC:
3850 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3851 if (!cmd)
3852 goto reject;
3854 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3855 break;
3856 case ISCSI_OP_TEXT:
3857 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
3858 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
3859 if (!cmd)
3860 goto reject;
3861 } else {
3862 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3863 if (!cmd)
3864 goto reject;
3867 ret = iscsit_handle_text_cmd(conn, cmd, buf);
3868 break;
3869 case ISCSI_OP_LOGOUT:
3870 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3871 if (!cmd)
3872 goto reject;
3874 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3875 if (ret > 0)
3876 wait_for_completion_timeout(&conn->conn_logout_comp,
3877 SECONDS_FOR_LOGOUT_COMP * HZ);
3878 break;
3879 case ISCSI_OP_SNACK:
3880 ret = iscsit_handle_snack(conn, buf);
3881 break;
3882 default:
3883 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3884 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3885 pr_err("Cannot recover from unknown"
3886 " opcode while ERL=0, closing iSCSI connection.\n");
3887 return -1;
3889 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3890 " closing iSCSI connection.\n");
3891 ret = -1;
3892 break;
3895 return ret;
3896 reject:
3897 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
3900 static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
3902 bool ret;
3904 spin_lock_bh(&conn->state_lock);
3905 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
3906 spin_unlock_bh(&conn->state_lock);
3908 return ret;
3911 static void iscsit_get_rx_pdu(struct iscsi_conn *conn)
3913 int ret;
3914 u8 *buffer, opcode;
3915 u32 checksum = 0, digest = 0;
3916 struct kvec iov;
3918 buffer = kcalloc(ISCSI_HDR_LEN, sizeof(*buffer), GFP_KERNEL);
3919 if (!buffer)
3920 return;
3922 while (!kthread_should_stop()) {
3924 * Ensure that both TX and RX per connection kthreads
3925 * are scheduled to run on the same CPU.
3927 iscsit_thread_check_cpumask(conn, current, 0);
3929 memset(&iov, 0, sizeof(struct kvec));
3931 iov.iov_base = buffer;
3932 iov.iov_len = ISCSI_HDR_LEN;
3934 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3935 if (ret != ISCSI_HDR_LEN) {
3936 iscsit_rx_thread_wait_for_tcp(conn);
3937 break;
3940 if (conn->conn_ops->HeaderDigest) {
3941 iov.iov_base = &digest;
3942 iov.iov_len = ISCSI_CRC_LEN;
3944 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3945 if (ret != ISCSI_CRC_LEN) {
3946 iscsit_rx_thread_wait_for_tcp(conn);
3947 break;
3950 iscsit_do_crypto_hash_buf(conn->conn_rx_hash, buffer,
3951 ISCSI_HDR_LEN, 0, NULL,
3952 &checksum);
3954 if (digest != checksum) {
3955 pr_err("HeaderDigest CRC32C failed,"
3956 " received 0x%08x, computed 0x%08x\n",
3957 digest, checksum);
3959 * Set the PDU to 0xff so it will intentionally
3960 * hit default in the switch below.
3962 memset(buffer, 0xff, ISCSI_HDR_LEN);
3963 atomic_long_inc(&conn->sess->conn_digest_errors);
3964 } else {
3965 pr_debug("Got HeaderDigest CRC32C"
3966 " 0x%08x\n", checksum);
3970 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
3971 break;
3973 opcode = buffer[0] & ISCSI_OPCODE_MASK;
3975 if (conn->sess->sess_ops->SessionType &&
3976 ((!(opcode & ISCSI_OP_TEXT)) ||
3977 (!(opcode & ISCSI_OP_LOGOUT)))) {
3978 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3979 " while in Discovery Session, rejecting.\n", opcode);
3980 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
3981 buffer);
3982 break;
3985 ret = iscsi_target_rx_opcode(conn, buffer);
3986 if (ret < 0)
3987 break;
3990 kfree(buffer);
3993 int iscsi_target_rx_thread(void *arg)
3995 int rc;
3996 struct iscsi_conn *conn = arg;
3997 bool conn_freed = false;
4000 * Allow ourselves to be interrupted by SIGINT so that a
4001 * connection recovery / failure event can be triggered externally.
4003 allow_signal(SIGINT);
4005 * Wait for iscsi_post_login_handler() to complete before allowing
4006 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4008 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4009 if (rc < 0 || iscsi_target_check_conn_state(conn))
4010 goto out;
4012 if (!conn->conn_transport->iscsit_get_rx_pdu)
4013 return 0;
4015 conn->conn_transport->iscsit_get_rx_pdu(conn);
4017 if (!signal_pending(current))
4018 atomic_set(&conn->transport_failed, 1);
4019 iscsit_take_action_for_connection_exit(conn, &conn_freed);
4021 out:
4022 if (!conn_freed) {
4023 while (!kthread_should_stop()) {
4024 msleep(100);
4028 return 0;
4031 static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4033 LIST_HEAD(tmp_list);
4034 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4035 struct iscsi_session *sess = conn->sess;
4037 * We expect this function to only ever be called from either RX or TX
4038 * thread context via iscsit_close_connection() once the other context
4039 * has been reset -> returned sleeping pre-handler state.
4041 spin_lock_bh(&conn->cmd_lock);
4042 list_splice_init(&conn->conn_cmd_list, &tmp_list);
4044 list_for_each_entry(cmd, &tmp_list, i_conn_node) {
4045 struct se_cmd *se_cmd = &cmd->se_cmd;
4047 if (se_cmd->se_tfo != NULL) {
4048 spin_lock_irq(&se_cmd->t_state_lock);
4049 se_cmd->transport_state |= CMD_T_FABRIC_STOP;
4050 spin_unlock_irq(&se_cmd->t_state_lock);
4053 spin_unlock_bh(&conn->cmd_lock);
4055 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4056 list_del_init(&cmd->i_conn_node);
4058 iscsit_increment_maxcmdsn(cmd, sess);
4059 iscsit_free_cmd(cmd, true);
4064 static void iscsit_stop_timers_for_cmds(
4065 struct iscsi_conn *conn)
4067 struct iscsi_cmd *cmd;
4069 spin_lock_bh(&conn->cmd_lock);
4070 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
4071 if (cmd->data_direction == DMA_TO_DEVICE)
4072 iscsit_stop_dataout_timer(cmd);
4074 spin_unlock_bh(&conn->cmd_lock);
4077 int iscsit_close_connection(
4078 struct iscsi_conn *conn)
4080 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4081 struct iscsi_session *sess = conn->sess;
4083 pr_debug("Closing iSCSI connection CID %hu on SID:"
4084 " %u\n", conn->cid, sess->sid);
4086 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4087 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4088 * sleeping and the logout response never got sent because the
4089 * connection failed.
4091 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4092 * to signal logout response TX interrupt completion. Go ahead and skip
4093 * this for iser since isert_rx_opcode() does not wait on logout failure,
4094 * and to avoid iscsi_conn pointer dereference in iser-target code.
4096 if (!conn->conn_transport->rdma_shutdown)
4097 complete(&conn->conn_logout_comp);
4099 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4100 if (conn->tx_thread &&
4101 cmpxchg(&conn->tx_thread_active, true, false)) {
4102 send_sig(SIGINT, conn->tx_thread, 1);
4103 kthread_stop(conn->tx_thread);
4105 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4106 if (conn->rx_thread &&
4107 cmpxchg(&conn->rx_thread_active, true, false)) {
4108 send_sig(SIGINT, conn->rx_thread, 1);
4109 kthread_stop(conn->rx_thread);
4113 spin_lock(&iscsit_global->ts_bitmap_lock);
4114 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4115 get_order(1));
4116 spin_unlock(&iscsit_global->ts_bitmap_lock);
4118 iscsit_stop_timers_for_cmds(conn);
4119 iscsit_stop_nopin_response_timer(conn);
4120 iscsit_stop_nopin_timer(conn);
4122 if (conn->conn_transport->iscsit_wait_conn)
4123 conn->conn_transport->iscsit_wait_conn(conn);
4126 * During Connection recovery drop unacknowledged out of order
4127 * commands for this connection, and prepare the other commands
4128 * for reallegiance.
4130 * During normal operation clear the out of order commands (but
4131 * do not free the struct iscsi_ooo_cmdsn's) and release all
4132 * struct iscsi_cmds.
4134 if (atomic_read(&conn->connection_recovery)) {
4135 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4136 iscsit_prepare_cmds_for_reallegiance(conn);
4137 } else {
4138 iscsit_clear_ooo_cmdsns_for_conn(conn);
4139 iscsit_release_commands_from_conn(conn);
4141 iscsit_free_queue_reqs_for_conn(conn);
4144 * Handle decrementing session or connection usage count if
4145 * a logout response was not able to be sent because the
4146 * connection failed. Fall back to Session Recovery here.
4148 if (atomic_read(&conn->conn_logout_remove)) {
4149 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4150 iscsit_dec_conn_usage_count(conn);
4151 iscsit_dec_session_usage_count(sess);
4153 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4154 iscsit_dec_conn_usage_count(conn);
4156 atomic_set(&conn->conn_logout_remove, 0);
4157 atomic_set(&sess->session_reinstatement, 0);
4158 atomic_set(&sess->session_fall_back_to_erl0, 1);
4161 spin_lock_bh(&sess->conn_lock);
4162 list_del(&conn->conn_list);
4165 * Attempt to let the Initiator know this connection failed by
4166 * sending an Connection Dropped Async Message on another
4167 * active connection.
4169 if (atomic_read(&conn->connection_recovery))
4170 iscsit_build_conn_drop_async_message(conn);
4172 spin_unlock_bh(&sess->conn_lock);
4175 * If connection reinstatement is being performed on this connection,
4176 * up the connection reinstatement semaphore that is being blocked on
4177 * in iscsit_cause_connection_reinstatement().
4179 spin_lock_bh(&conn->state_lock);
4180 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4181 spin_unlock_bh(&conn->state_lock);
4182 complete(&conn->conn_wait_comp);
4183 wait_for_completion(&conn->conn_post_wait_comp);
4184 spin_lock_bh(&conn->state_lock);
4188 * If connection reinstatement is being performed on this connection
4189 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4190 * connection wait rcfr semaphore that is being blocked on
4191 * an iscsit_connection_reinstatement_rcfr().
4193 if (atomic_read(&conn->connection_wait_rcfr)) {
4194 spin_unlock_bh(&conn->state_lock);
4195 complete(&conn->conn_wait_rcfr_comp);
4196 wait_for_completion(&conn->conn_post_wait_comp);
4197 spin_lock_bh(&conn->state_lock);
4199 atomic_set(&conn->connection_reinstatement, 1);
4200 spin_unlock_bh(&conn->state_lock);
4203 * If any other processes are accessing this connection pointer we
4204 * must wait until they have completed.
4206 iscsit_check_conn_usage_count(conn);
4208 ahash_request_free(conn->conn_tx_hash);
4209 if (conn->conn_rx_hash) {
4210 struct crypto_ahash *tfm;
4212 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
4213 ahash_request_free(conn->conn_rx_hash);
4214 crypto_free_ahash(tfm);
4217 if (conn->sock)
4218 sock_release(conn->sock);
4220 if (conn->conn_transport->iscsit_free_conn)
4221 conn->conn_transport->iscsit_free_conn(conn);
4223 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4224 conn->conn_state = TARG_CONN_STATE_FREE;
4225 iscsit_free_conn(conn);
4227 spin_lock_bh(&sess->conn_lock);
4228 atomic_dec(&sess->nconn);
4229 pr_debug("Decremented iSCSI connection count to %hu from node:"
4230 " %s\n", atomic_read(&sess->nconn),
4231 sess->sess_ops->InitiatorName);
4233 * Make sure that if one connection fails in an non ERL=2 iSCSI
4234 * Session that they all fail.
4236 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4237 !atomic_read(&sess->session_logout))
4238 atomic_set(&sess->session_fall_back_to_erl0, 1);
4241 * If this was not the last connection in the session, and we are
4242 * performing session reinstatement or falling back to ERL=0, call
4243 * iscsit_stop_session() without sleeping to shutdown the other
4244 * active connections.
4246 if (atomic_read(&sess->nconn)) {
4247 if (!atomic_read(&sess->session_reinstatement) &&
4248 !atomic_read(&sess->session_fall_back_to_erl0)) {
4249 spin_unlock_bh(&sess->conn_lock);
4250 return 0;
4252 if (!atomic_read(&sess->session_stop_active)) {
4253 atomic_set(&sess->session_stop_active, 1);
4254 spin_unlock_bh(&sess->conn_lock);
4255 iscsit_stop_session(sess, 0, 0);
4256 return 0;
4258 spin_unlock_bh(&sess->conn_lock);
4259 return 0;
4263 * If this was the last connection in the session and one of the
4264 * following is occurring:
4266 * Session Reinstatement is not being performed, and are falling back
4267 * to ERL=0 call iscsit_close_session().
4269 * Session Logout was requested. iscsit_close_session() will be called
4270 * elsewhere.
4272 * Session Continuation is not being performed, start the Time2Retain
4273 * handler and check if sleep_on_sess_wait_sem is active.
4275 if (!atomic_read(&sess->session_reinstatement) &&
4276 atomic_read(&sess->session_fall_back_to_erl0)) {
4277 spin_unlock_bh(&sess->conn_lock);
4278 complete_all(&sess->session_wait_comp);
4279 iscsit_close_session(sess);
4281 return 0;
4282 } else if (atomic_read(&sess->session_logout)) {
4283 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4284 sess->session_state = TARG_SESS_STATE_FREE;
4286 if (atomic_read(&sess->session_close)) {
4287 spin_unlock_bh(&sess->conn_lock);
4288 complete_all(&sess->session_wait_comp);
4289 iscsit_close_session(sess);
4290 } else {
4291 spin_unlock_bh(&sess->conn_lock);
4294 return 0;
4295 } else {
4296 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4297 sess->session_state = TARG_SESS_STATE_FAILED;
4299 if (!atomic_read(&sess->session_continuation))
4300 iscsit_start_time2retain_handler(sess);
4302 if (atomic_read(&sess->session_close)) {
4303 spin_unlock_bh(&sess->conn_lock);
4304 complete_all(&sess->session_wait_comp);
4305 iscsit_close_session(sess);
4306 } else {
4307 spin_unlock_bh(&sess->conn_lock);
4310 return 0;
4315 * If the iSCSI Session for the iSCSI Initiator Node exists,
4316 * forcefully shutdown the iSCSI NEXUS.
4318 int iscsit_close_session(struct iscsi_session *sess)
4320 struct iscsi_portal_group *tpg = sess->tpg;
4321 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4323 if (atomic_read(&sess->nconn)) {
4324 pr_err("%d connection(s) still exist for iSCSI session"
4325 " to %s\n", atomic_read(&sess->nconn),
4326 sess->sess_ops->InitiatorName);
4327 BUG();
4330 spin_lock_bh(&se_tpg->session_lock);
4331 atomic_set(&sess->session_logout, 1);
4332 atomic_set(&sess->session_reinstatement, 1);
4333 iscsit_stop_time2retain_timer(sess);
4334 spin_unlock_bh(&se_tpg->session_lock);
4337 * transport_deregister_session_configfs() will clear the
4338 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4339 * can be setting it again with __transport_register_session() in
4340 * iscsi_post_login_handler() again after the iscsit_stop_session()
4341 * completes in iscsi_np context.
4343 transport_deregister_session_configfs(sess->se_sess);
4346 * If any other processes are accessing this session pointer we must
4347 * wait until they have completed. If we are in an interrupt (the
4348 * time2retain handler) and contain and active session usage count we
4349 * restart the timer and exit.
4351 if (!in_interrupt()) {
4352 if (iscsit_check_session_usage_count(sess) == 1)
4353 iscsit_stop_session(sess, 1, 1);
4354 } else {
4355 if (iscsit_check_session_usage_count(sess) == 2) {
4356 atomic_set(&sess->session_logout, 0);
4357 iscsit_start_time2retain_handler(sess);
4358 return 0;
4362 transport_deregister_session(sess->se_sess);
4364 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4365 iscsit_free_connection_recovery_entires(sess);
4367 iscsit_free_all_ooo_cmdsns(sess);
4369 spin_lock_bh(&se_tpg->session_lock);
4370 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4371 sess->session_state = TARG_SESS_STATE_FREE;
4372 pr_debug("Released iSCSI session from node: %s\n",
4373 sess->sess_ops->InitiatorName);
4374 tpg->nsessions--;
4375 if (tpg->tpg_tiqn)
4376 tpg->tpg_tiqn->tiqn_nsessions--;
4378 pr_debug("Decremented number of active iSCSI Sessions on"
4379 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4381 ida_free(&sess_ida, sess->session_index);
4382 kfree(sess->sess_ops);
4383 sess->sess_ops = NULL;
4384 spin_unlock_bh(&se_tpg->session_lock);
4386 kfree(sess);
4387 return 0;
4390 static void iscsit_logout_post_handler_closesession(
4391 struct iscsi_conn *conn)
4393 struct iscsi_session *sess = conn->sess;
4394 int sleep = 1;
4396 * Traditional iscsi/tcp will invoke this logic from TX thread
4397 * context during session logout, so clear tx_thread_active and
4398 * sleep if iscsit_close_connection() has not already occured.
4400 * Since iser-target invokes this logic from it's own workqueue,
4401 * always sleep waiting for RX/TX thread shutdown to complete
4402 * within iscsit_close_connection().
4404 if (!conn->conn_transport->rdma_shutdown) {
4405 sleep = cmpxchg(&conn->tx_thread_active, true, false);
4406 if (!sleep)
4407 return;
4410 atomic_set(&conn->conn_logout_remove, 0);
4411 complete(&conn->conn_logout_comp);
4413 iscsit_dec_conn_usage_count(conn);
4414 atomic_set(&sess->session_close, 1);
4415 iscsit_stop_session(sess, sleep, sleep);
4416 iscsit_dec_session_usage_count(sess);
4419 static void iscsit_logout_post_handler_samecid(
4420 struct iscsi_conn *conn)
4422 int sleep = 1;
4424 if (!conn->conn_transport->rdma_shutdown) {
4425 sleep = cmpxchg(&conn->tx_thread_active, true, false);
4426 if (!sleep)
4427 return;
4430 atomic_set(&conn->conn_logout_remove, 0);
4431 complete(&conn->conn_logout_comp);
4433 iscsit_cause_connection_reinstatement(conn, sleep);
4434 iscsit_dec_conn_usage_count(conn);
4437 static void iscsit_logout_post_handler_diffcid(
4438 struct iscsi_conn *conn,
4439 u16 cid)
4441 struct iscsi_conn *l_conn;
4442 struct iscsi_session *sess = conn->sess;
4443 bool conn_found = false;
4445 if (!sess)
4446 return;
4448 spin_lock_bh(&sess->conn_lock);
4449 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4450 if (l_conn->cid == cid) {
4451 iscsit_inc_conn_usage_count(l_conn);
4452 conn_found = true;
4453 break;
4456 spin_unlock_bh(&sess->conn_lock);
4458 if (!conn_found)
4459 return;
4461 if (l_conn->sock)
4462 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4464 spin_lock_bh(&l_conn->state_lock);
4465 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4466 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4467 spin_unlock_bh(&l_conn->state_lock);
4469 iscsit_cause_connection_reinstatement(l_conn, 1);
4470 iscsit_dec_conn_usage_count(l_conn);
4474 * Return of 0 causes the TX thread to restart.
4476 int iscsit_logout_post_handler(
4477 struct iscsi_cmd *cmd,
4478 struct iscsi_conn *conn)
4480 int ret = 0;
4482 switch (cmd->logout_reason) {
4483 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4484 switch (cmd->logout_response) {
4485 case ISCSI_LOGOUT_SUCCESS:
4486 case ISCSI_LOGOUT_CLEANUP_FAILED:
4487 default:
4488 iscsit_logout_post_handler_closesession(conn);
4489 break;
4491 ret = 0;
4492 break;
4493 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4494 if (conn->cid == cmd->logout_cid) {
4495 switch (cmd->logout_response) {
4496 case ISCSI_LOGOUT_SUCCESS:
4497 case ISCSI_LOGOUT_CLEANUP_FAILED:
4498 default:
4499 iscsit_logout_post_handler_samecid(conn);
4500 break;
4502 ret = 0;
4503 } else {
4504 switch (cmd->logout_response) {
4505 case ISCSI_LOGOUT_SUCCESS:
4506 iscsit_logout_post_handler_diffcid(conn,
4507 cmd->logout_cid);
4508 break;
4509 case ISCSI_LOGOUT_CID_NOT_FOUND:
4510 case ISCSI_LOGOUT_CLEANUP_FAILED:
4511 default:
4512 break;
4514 ret = 1;
4516 break;
4517 case ISCSI_LOGOUT_REASON_RECOVERY:
4518 switch (cmd->logout_response) {
4519 case ISCSI_LOGOUT_SUCCESS:
4520 case ISCSI_LOGOUT_CID_NOT_FOUND:
4521 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4522 case ISCSI_LOGOUT_CLEANUP_FAILED:
4523 default:
4524 break;
4526 ret = 1;
4527 break;
4528 default:
4529 break;
4532 return ret;
4534 EXPORT_SYMBOL(iscsit_logout_post_handler);
4536 void iscsit_fail_session(struct iscsi_session *sess)
4538 struct iscsi_conn *conn;
4540 spin_lock_bh(&sess->conn_lock);
4541 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4542 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4543 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4545 spin_unlock_bh(&sess->conn_lock);
4547 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4548 sess->session_state = TARG_SESS_STATE_FAILED;
4551 void iscsit_stop_session(
4552 struct iscsi_session *sess,
4553 int session_sleep,
4554 int connection_sleep)
4556 u16 conn_count = atomic_read(&sess->nconn);
4557 struct iscsi_conn *conn, *conn_tmp = NULL;
4558 int is_last;
4560 spin_lock_bh(&sess->conn_lock);
4562 if (connection_sleep) {
4563 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4564 conn_list) {
4565 if (conn_count == 0)
4566 break;
4568 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4569 is_last = 1;
4570 } else {
4571 iscsit_inc_conn_usage_count(conn_tmp);
4572 is_last = 0;
4574 iscsit_inc_conn_usage_count(conn);
4576 spin_unlock_bh(&sess->conn_lock);
4577 iscsit_cause_connection_reinstatement(conn, 1);
4578 spin_lock_bh(&sess->conn_lock);
4580 iscsit_dec_conn_usage_count(conn);
4581 if (is_last == 0)
4582 iscsit_dec_conn_usage_count(conn_tmp);
4583 conn_count--;
4585 } else {
4586 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4587 iscsit_cause_connection_reinstatement(conn, 0);
4590 if (session_sleep && atomic_read(&sess->nconn)) {
4591 spin_unlock_bh(&sess->conn_lock);
4592 wait_for_completion(&sess->session_wait_comp);
4593 } else
4594 spin_unlock_bh(&sess->conn_lock);
4597 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4599 struct iscsi_session *sess;
4600 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4601 struct se_session *se_sess, *se_sess_tmp;
4602 LIST_HEAD(free_list);
4603 int session_count = 0;
4605 spin_lock_bh(&se_tpg->session_lock);
4606 if (tpg->nsessions && !force) {
4607 spin_unlock_bh(&se_tpg->session_lock);
4608 return -1;
4611 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4612 sess_list) {
4613 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4615 spin_lock(&sess->conn_lock);
4616 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4617 atomic_read(&sess->session_logout) ||
4618 atomic_read(&sess->session_close) ||
4619 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4620 spin_unlock(&sess->conn_lock);
4621 continue;
4623 iscsit_inc_session_usage_count(sess);
4624 atomic_set(&sess->session_reinstatement, 1);
4625 atomic_set(&sess->session_fall_back_to_erl0, 1);
4626 atomic_set(&sess->session_close, 1);
4627 spin_unlock(&sess->conn_lock);
4629 list_move_tail(&se_sess->sess_list, &free_list);
4631 spin_unlock_bh(&se_tpg->session_lock);
4633 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4634 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4636 list_del_init(&se_sess->sess_list);
4637 iscsit_stop_session(sess, 1, 1);
4638 iscsit_dec_session_usage_count(sess);
4639 session_count++;
4642 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4643 " Group: %hu\n", session_count, tpg->tpgt);
4644 return 0;
4647 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4648 MODULE_VERSION("4.1.x");
4649 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4650 MODULE_LICENSE("GPL");
4652 module_init(iscsi_target_init_module);
4653 module_exit(iscsi_target_cleanup_module);