1/******************************************************************************* 2 * This file contains main functions related to the iSCSI Target Core Driver. 3 * 4 * (c) Copyright 2007-2013 Datera, Inc. 5 * 6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org> 7 * 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. 12 * 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 ******************************************************************************/ 18 19#include <linux/string.h> 20#include <linux/kthread.h> 21#include <linux/crypto.h> 22#include <linux/completion.h> 23#include <linux/module.h> 24#include <linux/idr.h> 25#include <asm/unaligned.h> 26#include <scsi/scsi_device.h> 27#include <scsi/iscsi_proto.h> 28#include <scsi/scsi_tcq.h> 29#include <target/target_core_base.h> 30#include <target/target_core_fabric.h> 31#include <target/target_core_configfs.h> 32 33#include <target/iscsi/iscsi_target_core.h> 34#include "iscsi_target_parameters.h" 35#include "iscsi_target_seq_pdu_list.h" 36#include "iscsi_target_datain_values.h" 37#include "iscsi_target_erl0.h" 38#include "iscsi_target_erl1.h" 39#include "iscsi_target_erl2.h" 40#include "iscsi_target_login.h" 41#include "iscsi_target_tmr.h" 42#include "iscsi_target_tpg.h" 43#include "iscsi_target_util.h" 44#include "iscsi_target.h" 45#include "iscsi_target_device.h" 46#include <target/iscsi/iscsi_target_stat.h> 47 48#include <target/iscsi/iscsi_transport.h> 49 50static LIST_HEAD(g_tiqn_list); 51static LIST_HEAD(g_np_list); 52static DEFINE_SPINLOCK(tiqn_lock); 53static DEFINE_MUTEX(np_lock); 54 55static struct idr tiqn_idr; 56struct idr sess_idr; 57struct mutex auth_id_lock; 58spinlock_t sess_idr_lock; 59 60struct iscsit_global *iscsit_global; 61 62struct kmem_cache *lio_qr_cache; 63struct kmem_cache *lio_dr_cache; 64struct kmem_cache *lio_ooo_cache; 65struct kmem_cache *lio_r2t_cache; 66 67static int iscsit_handle_immediate_data(struct iscsi_cmd *, 68 struct iscsi_scsi_req *, u32); 69 70struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf) 71{ 72 struct iscsi_tiqn *tiqn = NULL; 73 74 spin_lock(&tiqn_lock); 75 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) { 76 if (!strcmp(tiqn->tiqn, buf)) { 77 78 spin_lock(&tiqn->tiqn_state_lock); 79 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) { 80 tiqn->tiqn_access_count++; 81 spin_unlock(&tiqn->tiqn_state_lock); 82 spin_unlock(&tiqn_lock); 83 return tiqn; 84 } 85 spin_unlock(&tiqn->tiqn_state_lock); 86 } 87 } 88 spin_unlock(&tiqn_lock); 89 90 return NULL; 91} 92 93static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn) 94{ 95 spin_lock(&tiqn->tiqn_state_lock); 96 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) { 97 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN; 98 spin_unlock(&tiqn->tiqn_state_lock); 99 return 0; 100 } 101 spin_unlock(&tiqn->tiqn_state_lock); 102 103 return -1; 104} 105 106void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn) 107{ 108 spin_lock(&tiqn->tiqn_state_lock); 109 tiqn->tiqn_access_count--; 110 spin_unlock(&tiqn->tiqn_state_lock); 111} 112 113/* 114 * Note that IQN formatting is expected to be done in userspace, and 115 * no explict IQN format checks are done here. 116 */ 117struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf) 118{ 119 struct iscsi_tiqn *tiqn = NULL; 120 int ret; 121 122 if (strlen(buf) >= ISCSI_IQN_LEN) { 123 pr_err("Target IQN exceeds %d bytes\n", 124 ISCSI_IQN_LEN); 125 return ERR_PTR(-EINVAL); 126 } 127 128 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL); 129 if (!tiqn) { 130 pr_err("Unable to allocate struct iscsi_tiqn\n"); 131 return ERR_PTR(-ENOMEM); 132 } 133 134 sprintf(tiqn->tiqn, "%s", buf); 135 INIT_LIST_HEAD(&tiqn->tiqn_list); 136 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list); 137 spin_lock_init(&tiqn->tiqn_state_lock); 138 spin_lock_init(&tiqn->tiqn_tpg_lock); 139 spin_lock_init(&tiqn->sess_err_stats.lock); 140 spin_lock_init(&tiqn->login_stats.lock); 141 spin_lock_init(&tiqn->logout_stats.lock); 142 143 tiqn->tiqn_state = TIQN_STATE_ACTIVE; 144 145 idr_preload(GFP_KERNEL); 146 spin_lock(&tiqn_lock); 147 148 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT); 149 if (ret < 0) { 150 pr_err("idr_alloc() failed for tiqn->tiqn_index\n"); 151 spin_unlock(&tiqn_lock); 152 idr_preload_end(); 153 kfree(tiqn); 154 return ERR_PTR(ret); 155 } 156 tiqn->tiqn_index = ret; 157 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list); 158 159 spin_unlock(&tiqn_lock); 160 idr_preload_end(); 161 162 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn); 163 164 return tiqn; 165 166} 167 168static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn) 169{ 170 /* 171 * Wait for accesses to said struct iscsi_tiqn to end. 172 */ 173 spin_lock(&tiqn->tiqn_state_lock); 174 while (tiqn->tiqn_access_count != 0) { 175 spin_unlock(&tiqn->tiqn_state_lock); 176 msleep(10); 177 spin_lock(&tiqn->tiqn_state_lock); 178 } 179 spin_unlock(&tiqn->tiqn_state_lock); 180} 181 182void iscsit_del_tiqn(struct iscsi_tiqn *tiqn) 183{ 184 /* 185 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN 186 * while holding tiqn->tiqn_state_lock. This means that all subsequent 187 * attempts to access this struct iscsi_tiqn will fail from both transport 188 * fabric and control code paths. 189 */ 190 if (iscsit_set_tiqn_shutdown(tiqn) < 0) { 191 pr_err("iscsit_set_tiqn_shutdown() failed\n"); 192 return; 193 } 194 195 iscsit_wait_for_tiqn(tiqn); 196 197 spin_lock(&tiqn_lock); 198 list_del(&tiqn->tiqn_list); 199 idr_remove(&tiqn_idr, tiqn->tiqn_index); 200 spin_unlock(&tiqn_lock); 201 202 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n", 203 tiqn->tiqn); 204 kfree(tiqn); 205} 206 207int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg) 208{ 209 int ret; 210 /* 211 * Determine if the network portal is accepting storage traffic. 212 */ 213 spin_lock_bh(&np->np_thread_lock); 214 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) { 215 spin_unlock_bh(&np->np_thread_lock); 216 return -1; 217 } 218 spin_unlock_bh(&np->np_thread_lock); 219 /* 220 * Determine if the portal group is accepting storage traffic. 221 */ 222 spin_lock_bh(&tpg->tpg_state_lock); 223 if (tpg->tpg_state != TPG_STATE_ACTIVE) { 224 spin_unlock_bh(&tpg->tpg_state_lock); 225 return -1; 226 } 227 spin_unlock_bh(&tpg->tpg_state_lock); 228 229 /* 230 * Here we serialize access across the TIQN+TPG Tuple. 231 */ 232 ret = down_interruptible(&tpg->np_login_sem); 233 if (ret != 0) 234 return -1; 235 236 spin_lock_bh(&tpg->tpg_state_lock); 237 if (tpg->tpg_state != TPG_STATE_ACTIVE) { 238 spin_unlock_bh(&tpg->tpg_state_lock); 239 up(&tpg->np_login_sem); 240 return -1; 241 } 242 spin_unlock_bh(&tpg->tpg_state_lock); 243 244 return 0; 245} 246 247void iscsit_login_kref_put(struct kref *kref) 248{ 249 struct iscsi_tpg_np *tpg_np = container_of(kref, 250 struct iscsi_tpg_np, tpg_np_kref); 251 252 complete(&tpg_np->tpg_np_comp); 253} 254 255int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg, 256 struct iscsi_tpg_np *tpg_np) 257{ 258 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn; 259 260 up(&tpg->np_login_sem); 261 262 if (tpg_np) 263 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put); 264 265 if (tiqn) 266 iscsit_put_tiqn_for_login(tiqn); 267 268 return 0; 269} 270 271bool iscsit_check_np_match( 272 struct __kernel_sockaddr_storage *sockaddr, 273 struct iscsi_np *np, 274 int network_transport) 275{ 276 struct sockaddr_in *sock_in, *sock_in_e; 277 struct sockaddr_in6 *sock_in6, *sock_in6_e; 278 bool ip_match = false; 279 u16 port; 280 281 if (sockaddr->ss_family == AF_INET6) { 282 sock_in6 = (struct sockaddr_in6 *)sockaddr; 283 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr; 284 285 if (!memcmp(&sock_in6->sin6_addr.in6_u, 286 &sock_in6_e->sin6_addr.in6_u, 287 sizeof(struct in6_addr))) 288 ip_match = true; 289 290 port = ntohs(sock_in6->sin6_port); 291 } else { 292 sock_in = (struct sockaddr_in *)sockaddr; 293 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr; 294 295 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr) 296 ip_match = true; 297 298 port = ntohs(sock_in->sin_port); 299 } 300 301 if (ip_match && (np->np_port == port) && 302 (np->np_network_transport == network_transport)) 303 return true; 304 305 return false; 306} 307 308/* 309 * Called with mutex np_lock held 310 */ 311static struct iscsi_np *iscsit_get_np( 312 struct __kernel_sockaddr_storage *sockaddr, 313 int network_transport) 314{ 315 struct iscsi_np *np; 316 bool match; 317 318 list_for_each_entry(np, &g_np_list, np_list) { 319 spin_lock_bh(&np->np_thread_lock); 320 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) { 321 spin_unlock_bh(&np->np_thread_lock); 322 continue; 323 } 324 325 match = iscsit_check_np_match(sockaddr, np, network_transport); 326 if (match) { 327 /* 328 * Increment the np_exports reference count now to 329 * prevent iscsit_del_np() below from being called 330 * while iscsi_tpg_add_network_portal() is called. 331 */ 332 np->np_exports++; 333 spin_unlock_bh(&np->np_thread_lock); 334 return np; 335 } 336 spin_unlock_bh(&np->np_thread_lock); 337 } 338 339 return NULL; 340} 341 342struct iscsi_np *iscsit_add_np( 343 struct __kernel_sockaddr_storage *sockaddr, 344 int network_transport) 345{ 346 struct sockaddr_in *sock_in; 347 struct sockaddr_in6 *sock_in6; 348 struct iscsi_np *np; 349 int ret; 350 351 mutex_lock(&np_lock); 352 353 /* 354 * Locate the existing struct iscsi_np if already active.. 355 */ 356 np = iscsit_get_np(sockaddr, network_transport); 357 if (np) { 358 mutex_unlock(&np_lock); 359 return np; 360 } 361 362 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL); 363 if (!np) { 364 pr_err("Unable to allocate memory for struct iscsi_np\n"); 365 mutex_unlock(&np_lock); 366 return ERR_PTR(-ENOMEM); 367 } 368 369 np->np_flags |= NPF_IP_NETWORK; 370 if (sockaddr->ss_family == AF_INET6) { 371 sock_in6 = (struct sockaddr_in6 *)sockaddr; 372 np->np_port = ntohs(sock_in6->sin6_port); 373 } else { 374 sock_in = (struct sockaddr_in *)sockaddr; 375 np->np_port = ntohs(sock_in->sin_port); 376 } 377 378 np->np_network_transport = network_transport; 379 spin_lock_init(&np->np_thread_lock); 380 init_completion(&np->np_restart_comp); 381 INIT_LIST_HEAD(&np->np_list); 382 383 ret = iscsi_target_setup_login_socket(np, sockaddr); 384 if (ret != 0) { 385 kfree(np); 386 mutex_unlock(&np_lock); 387 return ERR_PTR(ret); 388 } 389 390 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np"); 391 if (IS_ERR(np->np_thread)) { 392 pr_err("Unable to create kthread: iscsi_np\n"); 393 ret = PTR_ERR(np->np_thread); 394 kfree(np); 395 mutex_unlock(&np_lock); 396 return ERR_PTR(ret); 397 } 398 /* 399 * Increment the np_exports reference count now to prevent 400 * iscsit_del_np() below from being run while a new call to 401 * iscsi_tpg_add_network_portal() for a matching iscsi_np is 402 * active. We don't need to hold np->np_thread_lock at this 403 * point because iscsi_np has not been added to g_np_list yet. 404 */ 405 np->np_exports = 1; 406 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE; 407 408 list_add_tail(&np->np_list, &g_np_list); 409 mutex_unlock(&np_lock); 410 411 pr_debug("CORE[0] - Added Network Portal: %pISc:%hu on %s\n", 412 &np->np_sockaddr, np->np_port, np->np_transport->name); 413 414 return np; 415} 416 417int iscsit_reset_np_thread( 418 struct iscsi_np *np, 419 struct iscsi_tpg_np *tpg_np, 420 struct iscsi_portal_group *tpg, 421 bool shutdown) 422{ 423 spin_lock_bh(&np->np_thread_lock); 424 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) { 425 spin_unlock_bh(&np->np_thread_lock); 426 return 0; 427 } 428 np->np_thread_state = ISCSI_NP_THREAD_RESET; 429 430 if (np->np_thread) { 431 spin_unlock_bh(&np->np_thread_lock); 432 send_sig(SIGINT, np->np_thread, 1); 433 wait_for_completion(&np->np_restart_comp); 434 spin_lock_bh(&np->np_thread_lock); 435 } 436 spin_unlock_bh(&np->np_thread_lock); 437 438 if (tpg_np && shutdown) { 439 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put); 440 441 wait_for_completion(&tpg_np->tpg_np_comp); 442 } 443 444 return 0; 445} 446 447static void iscsit_free_np(struct iscsi_np *np) 448{ 449 if (np->np_socket) 450 sock_release(np->np_socket); 451} 452 453int iscsit_del_np(struct iscsi_np *np) 454{ 455 spin_lock_bh(&np->np_thread_lock); 456 np->np_exports--; 457 if (np->np_exports) { 458 np->enabled = true; 459 spin_unlock_bh(&np->np_thread_lock); 460 return 0; 461 } 462 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN; 463 spin_unlock_bh(&np->np_thread_lock); 464 465 if (np->np_thread) { 466 /* 467 * We need to send the signal to wakeup Linux/Net 468 * which may be sleeping in sock_accept().. 469 */ 470 send_sig(SIGINT, np->np_thread, 1); 471 kthread_stop(np->np_thread); 472 np->np_thread = NULL; 473 } 474 475 np->np_transport->iscsit_free_np(np); 476 477 mutex_lock(&np_lock); 478 list_del(&np->np_list); 479 mutex_unlock(&np_lock); 480 481 pr_debug("CORE[0] - Removed Network Portal: %pISc:%hu on %s\n", 482 &np->np_sockaddr, np->np_port, np->np_transport->name); 483 484 iscsit_put_transport(np->np_transport); 485 kfree(np); 486 return 0; 487} 488 489static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int); 490static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int); 491 492static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd) 493{ 494 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state); 495 return 0; 496} 497 498static void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd) 499{ 500 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD); 501 502 spin_lock_bh(&conn->cmd_lock); 503 if (!list_empty(&cmd->i_conn_node)) 504 list_del_init(&cmd->i_conn_node); 505 spin_unlock_bh(&conn->cmd_lock); 506 507 __iscsit_free_cmd(cmd, scsi_cmd, true); 508} 509 510static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn) 511{ 512 return TARGET_PROT_NORMAL; 513} 514 515static struct iscsit_transport iscsi_target_transport = { 516 .name = "iSCSI/TCP", 517 .transport_type = ISCSI_TCP, 518 .owner = NULL, 519 .iscsit_setup_np = iscsit_setup_np, 520 .iscsit_accept_np = iscsit_accept_np, 521 .iscsit_free_np = iscsit_free_np, 522 .iscsit_get_login_rx = iscsit_get_login_rx, 523 .iscsit_put_login_tx = iscsit_put_login_tx, 524 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd, 525 .iscsit_immediate_queue = iscsit_immediate_queue, 526 .iscsit_response_queue = iscsit_response_queue, 527 .iscsit_queue_data_in = iscsit_queue_rsp, 528 .iscsit_queue_status = iscsit_queue_rsp, 529 .iscsit_aborted_task = iscsit_aborted_task, 530 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops, 531}; 532 533static int __init iscsi_target_init_module(void) 534{ 535 int ret = 0, size; 536 537 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n"); 538 539 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL); 540 if (!iscsit_global) { 541 pr_err("Unable to allocate memory for iscsit_global\n"); 542 return -1; 543 } 544 spin_lock_init(&iscsit_global->ts_bitmap_lock); 545 mutex_init(&auth_id_lock); 546 spin_lock_init(&sess_idr_lock); 547 idr_init(&tiqn_idr); 548 idr_init(&sess_idr); 549 550 ret = target_register_template(&iscsi_ops); 551 if (ret) 552 goto out; 553 554 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long); 555 iscsit_global->ts_bitmap = vzalloc(size); 556 if (!iscsit_global->ts_bitmap) { 557 pr_err("Unable to allocate iscsit_global->ts_bitmap\n"); 558 goto configfs_out; 559 } 560 561 lio_qr_cache = kmem_cache_create("lio_qr_cache", 562 sizeof(struct iscsi_queue_req), 563 __alignof__(struct iscsi_queue_req), 0, NULL); 564 if (!lio_qr_cache) { 565 pr_err("nable to kmem_cache_create() for" 566 " lio_qr_cache\n"); 567 goto bitmap_out; 568 } 569 570 lio_dr_cache = kmem_cache_create("lio_dr_cache", 571 sizeof(struct iscsi_datain_req), 572 __alignof__(struct iscsi_datain_req), 0, NULL); 573 if (!lio_dr_cache) { 574 pr_err("Unable to kmem_cache_create() for" 575 " lio_dr_cache\n"); 576 goto qr_out; 577 } 578 579 lio_ooo_cache = kmem_cache_create("lio_ooo_cache", 580 sizeof(struct iscsi_ooo_cmdsn), 581 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL); 582 if (!lio_ooo_cache) { 583 pr_err("Unable to kmem_cache_create() for" 584 " lio_ooo_cache\n"); 585 goto dr_out; 586 } 587 588 lio_r2t_cache = kmem_cache_create("lio_r2t_cache", 589 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t), 590 0, NULL); 591 if (!lio_r2t_cache) { 592 pr_err("Unable to kmem_cache_create() for" 593 " lio_r2t_cache\n"); 594 goto ooo_out; 595 } 596 597 iscsit_register_transport(&iscsi_target_transport); 598 599 if (iscsit_load_discovery_tpg() < 0) 600 goto r2t_out; 601 602 return ret; 603r2t_out: 604 iscsit_unregister_transport(&iscsi_target_transport); 605 kmem_cache_destroy(lio_r2t_cache); 606ooo_out: 607 kmem_cache_destroy(lio_ooo_cache); 608dr_out: 609 kmem_cache_destroy(lio_dr_cache); 610qr_out: 611 kmem_cache_destroy(lio_qr_cache); 612bitmap_out: 613 vfree(iscsit_global->ts_bitmap); 614configfs_out: 615 /* XXX: this probably wants it to be it's own unwind step.. */ 616 if (iscsit_global->discovery_tpg) 617 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1); 618 target_unregister_template(&iscsi_ops); 619out: 620 kfree(iscsit_global); 621 return -ENOMEM; 622} 623 624static void __exit iscsi_target_cleanup_module(void) 625{ 626 iscsit_release_discovery_tpg(); 627 iscsit_unregister_transport(&iscsi_target_transport); 628 kmem_cache_destroy(lio_qr_cache); 629 kmem_cache_destroy(lio_dr_cache); 630 kmem_cache_destroy(lio_ooo_cache); 631 kmem_cache_destroy(lio_r2t_cache); 632 633 /* 634 * Shutdown discovery sessions and disable discovery TPG 635 */ 636 if (iscsit_global->discovery_tpg) 637 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1); 638 639 target_unregister_template(&iscsi_ops); 640 641 vfree(iscsit_global->ts_bitmap); 642 kfree(iscsit_global); 643} 644 645static int iscsit_add_reject( 646 struct iscsi_conn *conn, 647 u8 reason, 648 unsigned char *buf) 649{ 650 struct iscsi_cmd *cmd; 651 652 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 653 if (!cmd) 654 return -1; 655 656 cmd->iscsi_opcode = ISCSI_OP_REJECT; 657 cmd->reject_reason = reason; 658 659 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL); 660 if (!cmd->buf_ptr) { 661 pr_err("Unable to allocate memory for cmd->buf_ptr\n"); 662 iscsit_free_cmd(cmd, false); 663 return -1; 664 } 665 666 spin_lock_bh(&conn->cmd_lock); 667 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 668 spin_unlock_bh(&conn->cmd_lock); 669 670 cmd->i_state = ISTATE_SEND_REJECT; 671 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 672 673 return -1; 674} 675 676static int iscsit_add_reject_from_cmd( 677 struct iscsi_cmd *cmd, 678 u8 reason, 679 bool add_to_conn, 680 unsigned char *buf) 681{ 682 struct iscsi_conn *conn; 683 684 if (!cmd->conn) { 685 pr_err("cmd->conn is NULL for ITT: 0x%08x\n", 686 cmd->init_task_tag); 687 return -1; 688 } 689 conn = cmd->conn; 690 691 cmd->iscsi_opcode = ISCSI_OP_REJECT; 692 cmd->reject_reason = reason; 693 694 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL); 695 if (!cmd->buf_ptr) { 696 pr_err("Unable to allocate memory for cmd->buf_ptr\n"); 697 iscsit_free_cmd(cmd, false); 698 return -1; 699 } 700 701 if (add_to_conn) { 702 spin_lock_bh(&conn->cmd_lock); 703 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 704 spin_unlock_bh(&conn->cmd_lock); 705 } 706 707 cmd->i_state = ISTATE_SEND_REJECT; 708 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 709 /* 710 * Perform the kref_put now if se_cmd has already been setup by 711 * scsit_setup_scsi_cmd() 712 */ 713 if (cmd->se_cmd.se_tfo != NULL) { 714 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n"); 715 target_put_sess_cmd(&cmd->se_cmd); 716 } 717 return -1; 718} 719 720static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason, 721 unsigned char *buf) 722{ 723 return iscsit_add_reject_from_cmd(cmd, reason, true, buf); 724} 725 726int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf) 727{ 728 return iscsit_add_reject_from_cmd(cmd, reason, false, buf); 729} 730 731/* 732 * Map some portion of the allocated scatterlist to an iovec, suitable for 733 * kernel sockets to copy data in/out. 734 */ 735static int iscsit_map_iovec( 736 struct iscsi_cmd *cmd, 737 struct kvec *iov, 738 u32 data_offset, 739 u32 data_length) 740{ 741 u32 i = 0; 742 struct scatterlist *sg; 743 unsigned int page_off; 744 745 /* 746 * We know each entry in t_data_sg contains a page. 747 */ 748 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE]; 749 page_off = (data_offset % PAGE_SIZE); 750 751 cmd->first_data_sg = sg; 752 cmd->first_data_sg_off = page_off; 753 754 while (data_length) { 755 u32 cur_len = min_t(u32, data_length, sg->length - page_off); 756 757 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off; 758 iov[i].iov_len = cur_len; 759 760 data_length -= cur_len; 761 page_off = 0; 762 sg = sg_next(sg); 763 i++; 764 } 765 766 cmd->kmapped_nents = i; 767 768 return i; 769} 770 771static void iscsit_unmap_iovec(struct iscsi_cmd *cmd) 772{ 773 u32 i; 774 struct scatterlist *sg; 775 776 sg = cmd->first_data_sg; 777 778 for (i = 0; i < cmd->kmapped_nents; i++) 779 kunmap(sg_page(&sg[i])); 780} 781 782static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn) 783{ 784 LIST_HEAD(ack_list); 785 struct iscsi_cmd *cmd, *cmd_p; 786 787 conn->exp_statsn = exp_statsn; 788 789 if (conn->sess->sess_ops->RDMAExtensions) 790 return; 791 792 spin_lock_bh(&conn->cmd_lock); 793 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) { 794 spin_lock(&cmd->istate_lock); 795 if ((cmd->i_state == ISTATE_SENT_STATUS) && 796 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) { 797 cmd->i_state = ISTATE_REMOVE; 798 spin_unlock(&cmd->istate_lock); 799 list_move_tail(&cmd->i_conn_node, &ack_list); 800 continue; 801 } 802 spin_unlock(&cmd->istate_lock); 803 } 804 spin_unlock_bh(&conn->cmd_lock); 805 806 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) { 807 list_del_init(&cmd->i_conn_node); 808 iscsit_free_cmd(cmd, false); 809 } 810} 811 812static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd) 813{ 814 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE)); 815 816 iov_count += ISCSI_IOV_DATA_BUFFER; 817 818 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL); 819 if (!cmd->iov_data) { 820 pr_err("Unable to allocate cmd->iov_data\n"); 821 return -ENOMEM; 822 } 823 824 cmd->orig_iov_data_count = iov_count; 825 return 0; 826} 827 828int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 829 unsigned char *buf) 830{ 831 int data_direction, payload_length; 832 struct iscsi_scsi_req *hdr; 833 int iscsi_task_attr; 834 int sam_task_attr; 835 836 atomic_long_inc(&conn->sess->cmd_pdus); 837 838 hdr = (struct iscsi_scsi_req *) buf; 839 payload_length = ntoh24(hdr->dlength); 840 841 /* FIXME; Add checks for AdditionalHeaderSegment */ 842 843 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) && 844 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) { 845 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL" 846 " not set. Bad iSCSI Initiator.\n"); 847 return iscsit_add_reject_cmd(cmd, 848 ISCSI_REASON_BOOKMARK_INVALID, buf); 849 } 850 851 if (((hdr->flags & ISCSI_FLAG_CMD_READ) || 852 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) { 853 /* 854 * From RFC-3720 Section 10.3.1: 855 * 856 * "Either or both of R and W MAY be 1 when either the 857 * Expected Data Transfer Length and/or Bidirectional Read 858 * Expected Data Transfer Length are 0" 859 * 860 * For this case, go ahead and clear the unnecssary bits 861 * to avoid any confusion with ->data_direction. 862 */ 863 hdr->flags &= ~ISCSI_FLAG_CMD_READ; 864 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE; 865 866 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE" 867 " set when Expected Data Transfer Length is 0 for" 868 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]); 869 } 870 871 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) && 872 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) { 873 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE" 874 " MUST be set if Expected Data Transfer Length is not 0." 875 " Bad iSCSI Initiator\n"); 876 return iscsit_add_reject_cmd(cmd, 877 ISCSI_REASON_BOOKMARK_INVALID, buf); 878 } 879 880 if ((hdr->flags & ISCSI_FLAG_CMD_READ) && 881 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) { 882 pr_err("Bidirectional operations not supported!\n"); 883 return iscsit_add_reject_cmd(cmd, 884 ISCSI_REASON_BOOKMARK_INVALID, buf); 885 } 886 887 if (hdr->opcode & ISCSI_OP_IMMEDIATE) { 888 pr_err("Illegally set Immediate Bit in iSCSI Initiator" 889 " Scsi Command PDU.\n"); 890 return iscsit_add_reject_cmd(cmd, 891 ISCSI_REASON_BOOKMARK_INVALID, buf); 892 } 893 894 if (payload_length && !conn->sess->sess_ops->ImmediateData) { 895 pr_err("ImmediateData=No but DataSegmentLength=%u," 896 " protocol error.\n", payload_length); 897 return iscsit_add_reject_cmd(cmd, 898 ISCSI_REASON_PROTOCOL_ERROR, buf); 899 } 900 901 if ((be32_to_cpu(hdr->data_length) == payload_length) && 902 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) { 903 pr_err("Expected Data Transfer Length and Length of" 904 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL" 905 " bit is not set protocol error\n"); 906 return iscsit_add_reject_cmd(cmd, 907 ISCSI_REASON_PROTOCOL_ERROR, buf); 908 } 909 910 if (payload_length > be32_to_cpu(hdr->data_length)) { 911 pr_err("DataSegmentLength: %u is greater than" 912 " EDTL: %u, protocol error.\n", payload_length, 913 hdr->data_length); 914 return iscsit_add_reject_cmd(cmd, 915 ISCSI_REASON_PROTOCOL_ERROR, buf); 916 } 917 918 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { 919 pr_err("DataSegmentLength: %u is greater than" 920 " MaxXmitDataSegmentLength: %u, protocol error.\n", 921 payload_length, conn->conn_ops->MaxXmitDataSegmentLength); 922 return iscsit_add_reject_cmd(cmd, 923 ISCSI_REASON_PROTOCOL_ERROR, buf); 924 } 925 926 if (payload_length > conn->sess->sess_ops->FirstBurstLength) { 927 pr_err("DataSegmentLength: %u is greater than" 928 " FirstBurstLength: %u, protocol error.\n", 929 payload_length, conn->sess->sess_ops->FirstBurstLength); 930 return iscsit_add_reject_cmd(cmd, 931 ISCSI_REASON_BOOKMARK_INVALID, buf); 932 } 933 934 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE : 935 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE : 936 DMA_NONE; 937 938 cmd->data_direction = data_direction; 939 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK; 940 /* 941 * Figure out the SAM Task Attribute for the incoming SCSI CDB 942 */ 943 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) || 944 (iscsi_task_attr == ISCSI_ATTR_SIMPLE)) 945 sam_task_attr = TCM_SIMPLE_TAG; 946 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED) 947 sam_task_attr = TCM_ORDERED_TAG; 948 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE) 949 sam_task_attr = TCM_HEAD_TAG; 950 else if (iscsi_task_attr == ISCSI_ATTR_ACA) 951 sam_task_attr = TCM_ACA_TAG; 952 else { 953 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using" 954 " TCM_SIMPLE_TAG\n", iscsi_task_attr); 955 sam_task_attr = TCM_SIMPLE_TAG; 956 } 957 958 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD; 959 cmd->i_state = ISTATE_NEW_CMD; 960 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); 961 cmd->immediate_data = (payload_length) ? 1 : 0; 962 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) && 963 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0); 964 if (cmd->unsolicited_data) 965 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA; 966 967 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; 968 if (hdr->flags & ISCSI_FLAG_CMD_READ) 969 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess); 970 else 971 cmd->targ_xfer_tag = 0xFFFFFFFF; 972 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); 973 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); 974 cmd->first_burst_len = payload_length; 975 976 if (!conn->sess->sess_ops->RDMAExtensions && 977 cmd->data_direction == DMA_FROM_DEVICE) { 978 struct iscsi_datain_req *dr; 979 980 dr = iscsit_allocate_datain_req(); 981 if (!dr) 982 return iscsit_add_reject_cmd(cmd, 983 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 984 985 iscsit_attach_datain_req(cmd, dr); 986 } 987 988 /* 989 * Initialize struct se_cmd descriptor from target_core_mod infrastructure 990 */ 991 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops, 992 conn->sess->se_sess, be32_to_cpu(hdr->data_length), 993 cmd->data_direction, sam_task_attr, 994 cmd->sense_buffer + 2); 995 996 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x," 997 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt, 998 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length, 999 conn->cid); 1000 1001 target_get_sess_cmd(&cmd->se_cmd, true); 1002 1003 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd, 1004 scsilun_to_int(&hdr->lun)); 1005 if (cmd->sense_reason) 1006 goto attach_cmd; 1007 1008 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb); 1009 if (cmd->sense_reason) { 1010 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) { 1011 return iscsit_add_reject_cmd(cmd, 1012 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 1013 } 1014 1015 goto attach_cmd; 1016 } 1017 1018 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) { 1019 return iscsit_add_reject_cmd(cmd, 1020 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 1021 } 1022 1023attach_cmd: 1024 spin_lock_bh(&conn->cmd_lock); 1025 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 1026 spin_unlock_bh(&conn->cmd_lock); 1027 /* 1028 * Check if we need to delay processing because of ALUA 1029 * Active/NonOptimized primary access state.. 1030 */ 1031 core_alua_check_nonop_delay(&cmd->se_cmd); 1032 1033 return 0; 1034} 1035EXPORT_SYMBOL(iscsit_setup_scsi_cmd); 1036 1037void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd) 1038{ 1039 iscsit_set_dataout_sequence_values(cmd); 1040 1041 spin_lock_bh(&cmd->dataout_timeout_lock); 1042 iscsit_start_dataout_timer(cmd, cmd->conn); 1043 spin_unlock_bh(&cmd->dataout_timeout_lock); 1044} 1045EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout); 1046 1047int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 1048 struct iscsi_scsi_req *hdr) 1049{ 1050 int cmdsn_ret = 0; 1051 /* 1052 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if 1053 * the Immediate Bit is not set, and no Immediate 1054 * Data is attached. 1055 * 1056 * A PDU/CmdSN carrying Immediate Data can only 1057 * be processed after the DataCRC has passed. 1058 * If the DataCRC fails, the CmdSN MUST NOT 1059 * be acknowledged. (See below) 1060 */ 1061 if (!cmd->immediate_data) { 1062 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, 1063 (unsigned char *)hdr, hdr->cmdsn); 1064 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) 1065 return -1; 1066 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) { 1067 target_put_sess_cmd(&cmd->se_cmd); 1068 return 0; 1069 } 1070 } 1071 1072 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); 1073 1074 /* 1075 * If no Immediate Data is attached, it's OK to return now. 1076 */ 1077 if (!cmd->immediate_data) { 1078 if (!cmd->sense_reason && cmd->unsolicited_data) 1079 iscsit_set_unsoliticed_dataout(cmd); 1080 if (!cmd->sense_reason) 1081 return 0; 1082 1083 target_put_sess_cmd(&cmd->se_cmd); 1084 return 0; 1085 } 1086 1087 /* 1088 * Early CHECK_CONDITIONs with ImmediateData never make it to command 1089 * execution. These exceptions are processed in CmdSN order using 1090 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below. 1091 */ 1092 if (cmd->sense_reason) { 1093 if (cmd->reject_reason) 1094 return 0; 1095 1096 return 1; 1097 } 1098 /* 1099 * Call directly into transport_generic_new_cmd() to perform 1100 * the backend memory allocation. 1101 */ 1102 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd); 1103 if (cmd->sense_reason) 1104 return 1; 1105 1106 return 0; 1107} 1108EXPORT_SYMBOL(iscsit_process_scsi_cmd); 1109 1110static int 1111iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr, 1112 bool dump_payload) 1113{ 1114 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION; 1115 /* 1116 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes. 1117 */ 1118 if (dump_payload) 1119 goto after_immediate_data; 1120 1121 immed_ret = iscsit_handle_immediate_data(cmd, hdr, 1122 cmd->first_burst_len); 1123after_immediate_data: 1124 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) { 1125 /* 1126 * A PDU/CmdSN carrying Immediate Data passed 1127 * DataCRC, check against ExpCmdSN/MaxCmdSN if 1128 * Immediate Bit is not set. 1129 */ 1130 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd, 1131 (unsigned char *)hdr, hdr->cmdsn); 1132 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) 1133 return -1; 1134 1135 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) { 1136 int rc; 1137 1138 rc = iscsit_dump_data_payload(cmd->conn, 1139 cmd->first_burst_len, 1); 1140 target_put_sess_cmd(&cmd->se_cmd); 1141 return rc; 1142 } else if (cmd->unsolicited_data) 1143 iscsit_set_unsoliticed_dataout(cmd); 1144 1145 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) { 1146 /* 1147 * Immediate Data failed DataCRC and ERL>=1, 1148 * silently drop this PDU and let the initiator 1149 * plug the CmdSN gap. 1150 * 1151 * FIXME: Send Unsolicited NOPIN with reserved 1152 * TTT here to help the initiator figure out 1153 * the missing CmdSN, although they should be 1154 * intelligent enough to determine the missing 1155 * CmdSN and issue a retry to plug the sequence. 1156 */ 1157 cmd->i_state = ISTATE_REMOVE; 1158 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state); 1159 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */ 1160 return -1; 1161 1162 return 0; 1163} 1164 1165static int 1166iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 1167 unsigned char *buf) 1168{ 1169 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf; 1170 int rc, immed_data; 1171 bool dump_payload = false; 1172 1173 rc = iscsit_setup_scsi_cmd(conn, cmd, buf); 1174 if (rc < 0) 1175 return 0; 1176 /* 1177 * Allocation iovecs needed for struct socket operations for 1178 * traditional iSCSI block I/O. 1179 */ 1180 if (iscsit_allocate_iovecs(cmd) < 0) { 1181 return iscsit_reject_cmd(cmd, 1182 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 1183 } 1184 immed_data = cmd->immediate_data; 1185 1186 rc = iscsit_process_scsi_cmd(conn, cmd, hdr); 1187 if (rc < 0) 1188 return rc; 1189 else if (rc > 0) 1190 dump_payload = true; 1191 1192 if (!immed_data) 1193 return 0; 1194 1195 return iscsit_get_immediate_data(cmd, hdr, dump_payload); 1196} 1197 1198static u32 iscsit_do_crypto_hash_sg( 1199 struct hash_desc *hash, 1200 struct iscsi_cmd *cmd, 1201 u32 data_offset, 1202 u32 data_length, 1203 u32 padding, 1204 u8 *pad_bytes) 1205{ 1206 u32 data_crc; 1207 u32 i; 1208 struct scatterlist *sg; 1209 unsigned int page_off; 1210 1211 crypto_hash_init(hash); 1212 1213 sg = cmd->first_data_sg; 1214 page_off = cmd->first_data_sg_off; 1215 1216 i = 0; 1217 while (data_length) { 1218 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off)); 1219 1220 crypto_hash_update(hash, &sg[i], cur_len); 1221 1222 data_length -= cur_len; 1223 page_off = 0; 1224 i++; 1225 } 1226 1227 if (padding) { 1228 struct scatterlist pad_sg; 1229 1230 sg_init_one(&pad_sg, pad_bytes, padding); 1231 crypto_hash_update(hash, &pad_sg, padding); 1232 } 1233 crypto_hash_final(hash, (u8 *) &data_crc); 1234 1235 return data_crc; 1236} 1237 1238static void iscsit_do_crypto_hash_buf( 1239 struct hash_desc *hash, 1240 const void *buf, 1241 u32 payload_length, 1242 u32 padding, 1243 u8 *pad_bytes, 1244 u8 *data_crc) 1245{ 1246 struct scatterlist sg; 1247 1248 crypto_hash_init(hash); 1249 1250 sg_init_one(&sg, buf, payload_length); 1251 crypto_hash_update(hash, &sg, payload_length); 1252 1253 if (padding) { 1254 sg_init_one(&sg, pad_bytes, padding); 1255 crypto_hash_update(hash, &sg, padding); 1256 } 1257 crypto_hash_final(hash, data_crc); 1258} 1259 1260int 1261iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf, 1262 struct iscsi_cmd **out_cmd) 1263{ 1264 struct iscsi_data *hdr = (struct iscsi_data *)buf; 1265 struct iscsi_cmd *cmd = NULL; 1266 struct se_cmd *se_cmd; 1267 u32 payload_length = ntoh24(hdr->dlength); 1268 int rc; 1269 1270 if (!payload_length) { 1271 pr_warn("DataOUT payload is ZERO, ignoring.\n"); 1272 return 0; 1273 } 1274 1275 /* iSCSI write */ 1276 atomic_long_add(payload_length, &conn->sess->rx_data_octets); 1277 1278 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { 1279 pr_err("DataSegmentLength: %u is greater than" 1280 " MaxXmitDataSegmentLength: %u\n", payload_length, 1281 conn->conn_ops->MaxXmitDataSegmentLength); 1282 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 1283 buf); 1284 } 1285 1286 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, 1287 payload_length); 1288 if (!cmd) 1289 return 0; 1290 1291 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x," 1292 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n", 1293 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset), 1294 payload_length, conn->cid); 1295 1296 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) { 1297 pr_err("Command ITT: 0x%08x received DataOUT after" 1298 " last DataOUT received, dumping payload\n", 1299 cmd->init_task_tag); 1300 return iscsit_dump_data_payload(conn, payload_length, 1); 1301 } 1302 1303 if (cmd->data_direction != DMA_TO_DEVICE) { 1304 pr_err("Command ITT: 0x%08x received DataOUT for a" 1305 " NON-WRITE command.\n", cmd->init_task_tag); 1306 return iscsit_dump_data_payload(conn, payload_length, 1); 1307 } 1308 se_cmd = &cmd->se_cmd; 1309 iscsit_mod_dataout_timer(cmd); 1310 1311 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) { 1312 pr_err("DataOut Offset: %u, Length %u greater than" 1313 " iSCSI Command EDTL %u, protocol error.\n", 1314 hdr->offset, payload_length, cmd->se_cmd.data_length); 1315 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf); 1316 } 1317 1318 if (cmd->unsolicited_data) { 1319 int dump_unsolicited_data = 0; 1320 1321 if (conn->sess->sess_ops->InitialR2T) { 1322 pr_err("Received unexpected unsolicited data" 1323 " while InitialR2T=Yes, protocol error.\n"); 1324 transport_send_check_condition_and_sense(&cmd->se_cmd, 1325 TCM_UNEXPECTED_UNSOLICITED_DATA, 0); 1326 return -1; 1327 } 1328 /* 1329 * Special case for dealing with Unsolicited DataOUT 1330 * and Unsupported SAM WRITE Opcodes and SE resource allocation 1331 * failures; 1332 */ 1333 1334 /* Something's amiss if we're not in WRITE_PENDING state... */ 1335 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING); 1336 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE)) 1337 dump_unsolicited_data = 1; 1338 1339 if (dump_unsolicited_data) { 1340 /* 1341 * Check if a delayed TASK_ABORTED status needs to 1342 * be sent now if the ISCSI_FLAG_CMD_FINAL has been 1343 * received with the unsolicitied data out. 1344 */ 1345 if (hdr->flags & ISCSI_FLAG_CMD_FINAL) 1346 iscsit_stop_dataout_timer(cmd); 1347 1348 transport_check_aborted_status(se_cmd, 1349 (hdr->flags & ISCSI_FLAG_CMD_FINAL)); 1350 return iscsit_dump_data_payload(conn, payload_length, 1); 1351 } 1352 } else { 1353 /* 1354 * For the normal solicited data path: 1355 * 1356 * Check for a delayed TASK_ABORTED status and dump any 1357 * incoming data out payload if one exists. Also, when the 1358 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current 1359 * data out sequence, we decrement outstanding_r2ts. Once 1360 * outstanding_r2ts reaches zero, go ahead and send the delayed 1361 * TASK_ABORTED status. 1362 */ 1363 if (se_cmd->transport_state & CMD_T_ABORTED) { 1364 if (hdr->flags & ISCSI_FLAG_CMD_FINAL) 1365 if (--cmd->outstanding_r2ts < 1) { 1366 iscsit_stop_dataout_timer(cmd); 1367 transport_check_aborted_status( 1368 se_cmd, 1); 1369 } 1370 1371 return iscsit_dump_data_payload(conn, payload_length, 1); 1372 } 1373 } 1374 /* 1375 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and 1376 * within-command recovery checks before receiving the payload. 1377 */ 1378 rc = iscsit_check_pre_dataout(cmd, buf); 1379 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY) 1380 return 0; 1381 else if (rc == DATAOUT_CANNOT_RECOVER) 1382 return -1; 1383 1384 *out_cmd = cmd; 1385 return 0; 1386} 1387EXPORT_SYMBOL(iscsit_check_dataout_hdr); 1388 1389static int 1390iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 1391 struct iscsi_data *hdr) 1392{ 1393 struct kvec *iov; 1394 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0; 1395 u32 payload_length = ntoh24(hdr->dlength); 1396 int iov_ret, data_crc_failed = 0; 1397 1398 rx_size += payload_length; 1399 iov = &cmd->iov_data[0]; 1400 1401 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset), 1402 payload_length); 1403 if (iov_ret < 0) 1404 return -1; 1405 1406 iov_count += iov_ret; 1407 1408 padding = ((-payload_length) & 3); 1409 if (padding != 0) { 1410 iov[iov_count].iov_base = cmd->pad_bytes; 1411 iov[iov_count++].iov_len = padding; 1412 rx_size += padding; 1413 pr_debug("Receiving %u padding bytes.\n", padding); 1414 } 1415 1416 if (conn->conn_ops->DataDigest) { 1417 iov[iov_count].iov_base = &checksum; 1418 iov[iov_count++].iov_len = ISCSI_CRC_LEN; 1419 rx_size += ISCSI_CRC_LEN; 1420 } 1421 1422 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size); 1423 1424 iscsit_unmap_iovec(cmd); 1425 1426 if (rx_got != rx_size) 1427 return -1; 1428 1429 if (conn->conn_ops->DataDigest) { 1430 u32 data_crc; 1431 1432 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd, 1433 be32_to_cpu(hdr->offset), 1434 payload_length, padding, 1435 cmd->pad_bytes); 1436 1437 if (checksum != data_crc) { 1438 pr_err("ITT: 0x%08x, Offset: %u, Length: %u," 1439 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x" 1440 " does not match computed 0x%08x\n", 1441 hdr->itt, hdr->offset, payload_length, 1442 hdr->datasn, checksum, data_crc); 1443 data_crc_failed = 1; 1444 } else { 1445 pr_debug("Got CRC32C DataDigest 0x%08x for" 1446 " %u bytes of Data Out\n", checksum, 1447 payload_length); 1448 } 1449 } 1450 1451 return data_crc_failed; 1452} 1453 1454int 1455iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr, 1456 bool data_crc_failed) 1457{ 1458 struct iscsi_conn *conn = cmd->conn; 1459 int rc, ooo_cmdsn; 1460 /* 1461 * Increment post receive data and CRC values or perform 1462 * within-command recovery. 1463 */ 1464 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed); 1465 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)) 1466 return 0; 1467 else if (rc == DATAOUT_SEND_R2T) { 1468 iscsit_set_dataout_sequence_values(cmd); 1469 conn->conn_transport->iscsit_get_dataout(conn, cmd, false); 1470 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) { 1471 /* 1472 * Handle extra special case for out of order 1473 * Unsolicited Data Out. 1474 */ 1475 spin_lock_bh(&cmd->istate_lock); 1476 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN); 1477 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT; 1478 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT; 1479 spin_unlock_bh(&cmd->istate_lock); 1480 1481 iscsit_stop_dataout_timer(cmd); 1482 if (ooo_cmdsn) 1483 return 0; 1484 target_execute_cmd(&cmd->se_cmd); 1485 return 0; 1486 } else /* DATAOUT_CANNOT_RECOVER */ 1487 return -1; 1488 1489 return 0; 1490} 1491EXPORT_SYMBOL(iscsit_check_dataout_payload); 1492 1493static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf) 1494{ 1495 struct iscsi_cmd *cmd = NULL; 1496 struct iscsi_data *hdr = (struct iscsi_data *)buf; 1497 int rc; 1498 bool data_crc_failed = false; 1499 1500 rc = iscsit_check_dataout_hdr(conn, buf, &cmd); 1501 if (rc < 0) 1502 return 0; 1503 else if (!cmd) 1504 return 0; 1505 1506 rc = iscsit_get_dataout(conn, cmd, hdr); 1507 if (rc < 0) 1508 return rc; 1509 else if (rc > 0) 1510 data_crc_failed = true; 1511 1512 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed); 1513} 1514 1515int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 1516 struct iscsi_nopout *hdr) 1517{ 1518 u32 payload_length = ntoh24(hdr->dlength); 1519 1520 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) { 1521 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n"); 1522 if (!cmd) 1523 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 1524 (unsigned char *)hdr); 1525 1526 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, 1527 (unsigned char *)hdr); 1528 } 1529 1530 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { 1531 pr_err("NOPOUT ITT is reserved, but Immediate Bit is" 1532 " not set, protocol error.\n"); 1533 if (!cmd) 1534 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 1535 (unsigned char *)hdr); 1536 1537 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, 1538 (unsigned char *)hdr); 1539 } 1540 1541 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { 1542 pr_err("NOPOUT Ping Data DataSegmentLength: %u is" 1543 " greater than MaxXmitDataSegmentLength: %u, protocol" 1544 " error.\n", payload_length, 1545 conn->conn_ops->MaxXmitDataSegmentLength); 1546 if (!cmd) 1547 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 1548 (unsigned char *)hdr); 1549 1550 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, 1551 (unsigned char *)hdr); 1552 } 1553 1554 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x," 1555 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n", 1556 hdr->itt == RESERVED_ITT ? "Response" : "Request", 1557 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn, 1558 payload_length); 1559 /* 1560 * This is not a response to a Unsolicited NopIN, which means 1561 * it can either be a NOPOUT ping request (with a valid ITT), 1562 * or a NOPOUT not requesting a NOPIN (with a reserved ITT). 1563 * Either way, make sure we allocate an struct iscsi_cmd, as both 1564 * can contain ping data. 1565 */ 1566 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) { 1567 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT; 1568 cmd->i_state = ISTATE_SEND_NOPIN; 1569 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1570 1 : 0); 1571 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; 1572 cmd->targ_xfer_tag = 0xFFFFFFFF; 1573 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); 1574 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); 1575 cmd->data_direction = DMA_NONE; 1576 } 1577 1578 return 0; 1579} 1580EXPORT_SYMBOL(iscsit_setup_nop_out); 1581 1582int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 1583 struct iscsi_nopout *hdr) 1584{ 1585 struct iscsi_cmd *cmd_p = NULL; 1586 int cmdsn_ret = 0; 1587 /* 1588 * Initiator is expecting a NopIN ping reply.. 1589 */ 1590 if (hdr->itt != RESERVED_ITT) { 1591 if (!cmd) 1592 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 1593 (unsigned char *)hdr); 1594 1595 spin_lock_bh(&conn->cmd_lock); 1596 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 1597 spin_unlock_bh(&conn->cmd_lock); 1598 1599 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); 1600 1601 if (hdr->opcode & ISCSI_OP_IMMEDIATE) { 1602 iscsit_add_cmd_to_response_queue(cmd, conn, 1603 cmd->i_state); 1604 return 0; 1605 } 1606 1607 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, 1608 (unsigned char *)hdr, hdr->cmdsn); 1609 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) 1610 return 0; 1611 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) 1612 return -1; 1613 1614 return 0; 1615 } 1616 /* 1617 * This was a response to a unsolicited NOPIN ping. 1618 */ 1619 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) { 1620 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt)); 1621 if (!cmd_p) 1622 return -EINVAL; 1623 1624 iscsit_stop_nopin_response_timer(conn); 1625 1626 cmd_p->i_state = ISTATE_REMOVE; 1627 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state); 1628 1629 iscsit_start_nopin_timer(conn); 1630 return 0; 1631 } 1632 /* 1633 * Otherwise, initiator is not expecting a NOPIN is response. 1634 * Just ignore for now. 1635 */ 1636 return 0; 1637} 1638EXPORT_SYMBOL(iscsit_process_nop_out); 1639 1640static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 1641 unsigned char *buf) 1642{ 1643 unsigned char *ping_data = NULL; 1644 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf; 1645 struct kvec *iov = NULL; 1646 u32 payload_length = ntoh24(hdr->dlength); 1647 int ret; 1648 1649 ret = iscsit_setup_nop_out(conn, cmd, hdr); 1650 if (ret < 0) 1651 return 0; 1652 /* 1653 * Handle NOP-OUT payload for traditional iSCSI sockets 1654 */ 1655 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) { 1656 u32 checksum, data_crc, padding = 0; 1657 int niov = 0, rx_got, rx_size = payload_length; 1658 1659 ping_data = kzalloc(payload_length + 1, GFP_KERNEL); 1660 if (!ping_data) { 1661 pr_err("Unable to allocate memory for" 1662 " NOPOUT ping data.\n"); 1663 ret = -1; 1664 goto out; 1665 } 1666 1667 iov = &cmd->iov_misc[0]; 1668 iov[niov].iov_base = ping_data; 1669 iov[niov++].iov_len = payload_length; 1670 1671 padding = ((-payload_length) & 3); 1672 if (padding != 0) { 1673 pr_debug("Receiving %u additional bytes" 1674 " for padding.\n", padding); 1675 iov[niov].iov_base = &cmd->pad_bytes; 1676 iov[niov++].iov_len = padding; 1677 rx_size += padding; 1678 } 1679 if (conn->conn_ops->DataDigest) { 1680 iov[niov].iov_base = &checksum; 1681 iov[niov++].iov_len = ISCSI_CRC_LEN; 1682 rx_size += ISCSI_CRC_LEN; 1683 } 1684 1685 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size); 1686 if (rx_got != rx_size) { 1687 ret = -1; 1688 goto out; 1689 } 1690 1691 if (conn->conn_ops->DataDigest) { 1692 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash, 1693 ping_data, payload_length, 1694 padding, cmd->pad_bytes, 1695 (u8 *)&data_crc); 1696 1697 if (checksum != data_crc) { 1698 pr_err("Ping data CRC32C DataDigest" 1699 " 0x%08x does not match computed 0x%08x\n", 1700 checksum, data_crc); 1701 if (!conn->sess->sess_ops->ErrorRecoveryLevel) { 1702 pr_err("Unable to recover from" 1703 " NOPOUT Ping DataCRC failure while in" 1704 " ERL=0.\n"); 1705 ret = -1; 1706 goto out; 1707 } else { 1708 /* 1709 * Silently drop this PDU and let the 1710 * initiator plug the CmdSN gap. 1711 */ 1712 pr_debug("Dropping NOPOUT" 1713 " Command CmdSN: 0x%08x due to" 1714 " DataCRC error.\n", hdr->cmdsn); 1715 ret = 0; 1716 goto out; 1717 } 1718 } else { 1719 pr_debug("Got CRC32C DataDigest" 1720 " 0x%08x for %u bytes of ping data.\n", 1721 checksum, payload_length); 1722 } 1723 } 1724 1725 ping_data[payload_length] = '\0'; 1726 /* 1727 * Attach ping data to struct iscsi_cmd->buf_ptr. 1728 */ 1729 cmd->buf_ptr = ping_data; 1730 cmd->buf_ptr_size = payload_length; 1731 1732 pr_debug("Got %u bytes of NOPOUT ping" 1733 " data.\n", payload_length); 1734 pr_debug("Ping Data: \"%s\"\n", ping_data); 1735 } 1736 1737 return iscsit_process_nop_out(conn, cmd, hdr); 1738out: 1739 if (cmd) 1740 iscsit_free_cmd(cmd, false); 1741 1742 kfree(ping_data); 1743 return ret; 1744} 1745 1746int 1747iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 1748 unsigned char *buf) 1749{ 1750 struct se_tmr_req *se_tmr; 1751 struct iscsi_tmr_req *tmr_req; 1752 struct iscsi_tm *hdr; 1753 int out_of_order_cmdsn = 0, ret; 1754 bool sess_ref = false; 1755 u8 function; 1756 1757 hdr = (struct iscsi_tm *) buf; 1758 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL; 1759 function = hdr->flags; 1760 1761 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:" 1762 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:" 1763 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function, 1764 hdr->rtt, hdr->refcmdsn, conn->cid); 1765 1766 if ((function != ISCSI_TM_FUNC_ABORT_TASK) && 1767 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) && 1768 hdr->rtt != RESERVED_ITT)) { 1769 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n"); 1770 hdr->rtt = RESERVED_ITT; 1771 } 1772 1773 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) && 1774 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { 1775 pr_err("Task Management Request TASK_REASSIGN not" 1776 " issued as immediate command, bad iSCSI Initiator" 1777 "implementation\n"); 1778 return iscsit_add_reject_cmd(cmd, 1779 ISCSI_REASON_PROTOCOL_ERROR, buf); 1780 } 1781 if ((function != ISCSI_TM_FUNC_ABORT_TASK) && 1782 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG) 1783 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG); 1784 1785 cmd->data_direction = DMA_NONE; 1786 1787 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL); 1788 if (!cmd->tmr_req) { 1789 pr_err("Unable to allocate memory for" 1790 " Task Management command!\n"); 1791 return iscsit_add_reject_cmd(cmd, 1792 ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1793 buf); 1794 } 1795 1796 /* 1797 * TASK_REASSIGN for ERL=2 / connection stays inside of 1798 * LIO-Target $FABRIC_MOD 1799 */ 1800 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) { 1801 1802 u8 tcm_function; 1803 int ret; 1804 1805 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops, 1806 conn->sess->se_sess, 0, DMA_NONE, 1807 TCM_SIMPLE_TAG, cmd->sense_buffer + 2); 1808 1809 target_get_sess_cmd(&cmd->se_cmd, true); 1810 sess_ref = true; 1811 1812 switch (function) { 1813 case ISCSI_TM_FUNC_ABORT_TASK: 1814 tcm_function = TMR_ABORT_TASK; 1815 break; 1816 case ISCSI_TM_FUNC_ABORT_TASK_SET: 1817 tcm_function = TMR_ABORT_TASK_SET; 1818 break; 1819 case ISCSI_TM_FUNC_CLEAR_ACA: 1820 tcm_function = TMR_CLEAR_ACA; 1821 break; 1822 case ISCSI_TM_FUNC_CLEAR_TASK_SET: 1823 tcm_function = TMR_CLEAR_TASK_SET; 1824 break; 1825 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET: 1826 tcm_function = TMR_LUN_RESET; 1827 break; 1828 case ISCSI_TM_FUNC_TARGET_WARM_RESET: 1829 tcm_function = TMR_TARGET_WARM_RESET; 1830 break; 1831 case ISCSI_TM_FUNC_TARGET_COLD_RESET: 1832 tcm_function = TMR_TARGET_COLD_RESET; 1833 break; 1834 default: 1835 pr_err("Unknown iSCSI TMR Function:" 1836 " 0x%02x\n", function); 1837 return iscsit_add_reject_cmd(cmd, 1838 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 1839 } 1840 1841 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, 1842 tcm_function, GFP_KERNEL); 1843 if (ret < 0) 1844 return iscsit_add_reject_cmd(cmd, 1845 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 1846 1847 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req; 1848 } 1849 1850 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC; 1851 cmd->i_state = ISTATE_SEND_TASKMGTRSP; 1852 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); 1853 cmd->init_task_tag = hdr->itt; 1854 cmd->targ_xfer_tag = 0xFFFFFFFF; 1855 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); 1856 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); 1857 se_tmr = cmd->se_cmd.se_tmr_req; 1858 tmr_req = cmd->tmr_req; 1859 /* 1860 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN 1861 */ 1862 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) { 1863 ret = transport_lookup_tmr_lun(&cmd->se_cmd, 1864 scsilun_to_int(&hdr->lun)); 1865 if (ret < 0) { 1866 se_tmr->response = ISCSI_TMF_RSP_NO_LUN; 1867 goto attach; 1868 } 1869 } 1870 1871 switch (function) { 1872 case ISCSI_TM_FUNC_ABORT_TASK: 1873 se_tmr->response = iscsit_tmr_abort_task(cmd, buf); 1874 if (se_tmr->response) 1875 goto attach; 1876 break; 1877 case ISCSI_TM_FUNC_ABORT_TASK_SET: 1878 case ISCSI_TM_FUNC_CLEAR_ACA: 1879 case ISCSI_TM_FUNC_CLEAR_TASK_SET: 1880 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET: 1881 break; 1882 case ISCSI_TM_FUNC_TARGET_WARM_RESET: 1883 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) { 1884 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED; 1885 goto attach; 1886 } 1887 break; 1888 case ISCSI_TM_FUNC_TARGET_COLD_RESET: 1889 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) { 1890 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED; 1891 goto attach; 1892 } 1893 break; 1894 case ISCSI_TM_FUNC_TASK_REASSIGN: 1895 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf); 1896 /* 1897 * Perform sanity checks on the ExpDataSN only if the 1898 * TASK_REASSIGN was successful. 1899 */ 1900 if (se_tmr->response) 1901 break; 1902 1903 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0) 1904 return iscsit_add_reject_cmd(cmd, 1905 ISCSI_REASON_BOOKMARK_INVALID, buf); 1906 break; 1907 default: 1908 pr_err("Unknown TMR function: 0x%02x, protocol" 1909 " error.\n", function); 1910 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED; 1911 goto attach; 1912 } 1913 1914 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) && 1915 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE)) 1916 se_tmr->call_transport = 1; 1917attach: 1918 spin_lock_bh(&conn->cmd_lock); 1919 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 1920 spin_unlock_bh(&conn->cmd_lock); 1921 1922 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) { 1923 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn); 1924 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP) 1925 out_of_order_cmdsn = 1; 1926 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) 1927 return 0; 1928 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) 1929 return -1; 1930 } 1931 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); 1932 1933 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE)) 1934 return 0; 1935 /* 1936 * Found the referenced task, send to transport for processing. 1937 */ 1938 if (se_tmr->call_transport) 1939 return transport_generic_handle_tmr(&cmd->se_cmd); 1940 1941 /* 1942 * Could not find the referenced LUN, task, or Task Management 1943 * command not authorized or supported. Change state and 1944 * let the tx_thread send the response. 1945 * 1946 * For connection recovery, this is also the default action for 1947 * TMR TASK_REASSIGN. 1948 */ 1949 if (sess_ref) { 1950 pr_debug("Handle TMR, using sess_ref=true check\n"); 1951 target_put_sess_cmd(&cmd->se_cmd); 1952 } 1953 1954 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 1955 return 0; 1956} 1957EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd); 1958 1959/* #warning FIXME: Support Text Command parameters besides SendTargets */ 1960int 1961iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 1962 struct iscsi_text *hdr) 1963{ 1964 u32 payload_length = ntoh24(hdr->dlength); 1965 1966 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { 1967 pr_err("Unable to accept text parameter length: %u" 1968 "greater than MaxXmitDataSegmentLength %u.\n", 1969 payload_length, conn->conn_ops->MaxXmitDataSegmentLength); 1970 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, 1971 (unsigned char *)hdr); 1972 } 1973 1974 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) || 1975 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) { 1976 pr_err("Multi sequence text commands currently not supported\n"); 1977 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED, 1978 (unsigned char *)hdr); 1979 } 1980 1981 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x," 1982 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn, 1983 hdr->exp_statsn, payload_length); 1984 1985 cmd->iscsi_opcode = ISCSI_OP_TEXT; 1986 cmd->i_state = ISTATE_SEND_TEXTRSP; 1987 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); 1988 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; 1989 cmd->targ_xfer_tag = 0xFFFFFFFF; 1990 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); 1991 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); 1992 cmd->data_direction = DMA_NONE; 1993 cmd->text_in_ptr = NULL; 1994 1995 return 0; 1996} 1997EXPORT_SYMBOL(iscsit_setup_text_cmd); 1998 1999int 2000iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 2001 struct iscsi_text *hdr) 2002{ 2003 unsigned char *text_in = cmd->text_in_ptr, *text_ptr; 2004 int cmdsn_ret; 2005 2006 if (!text_in) { 2007 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt); 2008 if (cmd->targ_xfer_tag == 0xFFFFFFFF) { 2009 pr_err("Unable to locate text_in buffer for sendtargets" 2010 " discovery\n"); 2011 goto reject; 2012 } 2013 goto empty_sendtargets; 2014 } 2015 if (strncmp("SendTargets", text_in, 11) != 0) { 2016 pr_err("Received Text Data that is not" 2017 " SendTargets, cannot continue.\n"); 2018 goto reject; 2019 } 2020 text_ptr = strchr(text_in, '='); 2021 if (!text_ptr) { 2022 pr_err("No \"=\" separator found in Text Data," 2023 " cannot continue.\n"); 2024 goto reject; 2025 } 2026 if (!strncmp("=All", text_ptr, 4)) { 2027 cmd->cmd_flags |= ICF_SENDTARGETS_ALL; 2028 } else if (!strncmp("=iqn.", text_ptr, 5) || 2029 !strncmp("=eui.", text_ptr, 5)) { 2030 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE; 2031 } else { 2032 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr); 2033 goto reject; 2034 } 2035 2036 spin_lock_bh(&conn->cmd_lock); 2037 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 2038 spin_unlock_bh(&conn->cmd_lock); 2039 2040empty_sendtargets: 2041 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); 2042 2043 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) { 2044 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, 2045 (unsigned char *)hdr, hdr->cmdsn); 2046 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) 2047 return -1; 2048 2049 return 0; 2050 } 2051 2052 return iscsit_execute_cmd(cmd, 0); 2053 2054reject: 2055 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, 2056 (unsigned char *)hdr); 2057} 2058EXPORT_SYMBOL(iscsit_process_text_cmd); 2059 2060static int 2061iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 2062 unsigned char *buf) 2063{ 2064 struct iscsi_text *hdr = (struct iscsi_text *)buf; 2065 char *text_in = NULL; 2066 u32 payload_length = ntoh24(hdr->dlength); 2067 int rx_size, rc; 2068 2069 rc = iscsit_setup_text_cmd(conn, cmd, hdr); 2070 if (rc < 0) 2071 return 0; 2072 2073 rx_size = payload_length; 2074 if (payload_length) { 2075 u32 checksum = 0, data_crc = 0; 2076 u32 padding = 0, pad_bytes = 0; 2077 int niov = 0, rx_got; 2078 struct kvec iov[3]; 2079 2080 text_in = kzalloc(payload_length, GFP_KERNEL); 2081 if (!text_in) { 2082 pr_err("Unable to allocate memory for" 2083 " incoming text parameters\n"); 2084 goto reject; 2085 } 2086 cmd->text_in_ptr = text_in; 2087 2088 memset(iov, 0, 3 * sizeof(struct kvec)); 2089 iov[niov].iov_base = text_in; 2090 iov[niov++].iov_len = payload_length; 2091 2092 padding = ((-payload_length) & 3); 2093 if (padding != 0) { 2094 iov[niov].iov_base = &pad_bytes; 2095 iov[niov++].iov_len = padding; 2096 rx_size += padding; 2097 pr_debug("Receiving %u additional bytes" 2098 " for padding.\n", padding); 2099 } 2100 if (conn->conn_ops->DataDigest) { 2101 iov[niov].iov_base = &checksum; 2102 iov[niov++].iov_len = ISCSI_CRC_LEN; 2103 rx_size += ISCSI_CRC_LEN; 2104 } 2105 2106 rx_got = rx_data(conn, &iov[0], niov, rx_size); 2107 if (rx_got != rx_size) 2108 goto reject; 2109 2110 if (conn->conn_ops->DataDigest) { 2111 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash, 2112 text_in, payload_length, 2113 padding, (u8 *)&pad_bytes, 2114 (u8 *)&data_crc); 2115 2116 if (checksum != data_crc) { 2117 pr_err("Text data CRC32C DataDigest" 2118 " 0x%08x does not match computed" 2119 " 0x%08x\n", checksum, data_crc); 2120 if (!conn->sess->sess_ops->ErrorRecoveryLevel) { 2121 pr_err("Unable to recover from" 2122 " Text Data digest failure while in" 2123 " ERL=0.\n"); 2124 goto reject; 2125 } else { 2126 /* 2127 * Silently drop this PDU and let the 2128 * initiator plug the CmdSN gap. 2129 */ 2130 pr_debug("Dropping Text" 2131 " Command CmdSN: 0x%08x due to" 2132 " DataCRC error.\n", hdr->cmdsn); 2133 kfree(text_in); 2134 return 0; 2135 } 2136 } else { 2137 pr_debug("Got CRC32C DataDigest" 2138 " 0x%08x for %u bytes of text data.\n", 2139 checksum, payload_length); 2140 } 2141 } 2142 text_in[payload_length - 1] = '\0'; 2143 pr_debug("Successfully read %d bytes of text" 2144 " data.\n", payload_length); 2145 } 2146 2147 return iscsit_process_text_cmd(conn, cmd, hdr); 2148 2149reject: 2150 kfree(cmd->text_in_ptr); 2151 cmd->text_in_ptr = NULL; 2152 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf); 2153} 2154 2155int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 2156{ 2157 struct iscsi_conn *conn_p; 2158 struct iscsi_session *sess = conn->sess; 2159 2160 pr_debug("Received logout request CLOSESESSION on CID: %hu" 2161 " for SID: %u.\n", conn->cid, conn->sess->sid); 2162 2163 atomic_set(&sess->session_logout, 1); 2164 atomic_set(&conn->conn_logout_remove, 1); 2165 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION; 2166 2167 iscsit_inc_conn_usage_count(conn); 2168 iscsit_inc_session_usage_count(sess); 2169 2170 spin_lock_bh(&sess->conn_lock); 2171 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) { 2172 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN) 2173 continue; 2174 2175 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n"); 2176 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT; 2177 } 2178 spin_unlock_bh(&sess->conn_lock); 2179 2180 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 2181 2182 return 0; 2183} 2184 2185int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 2186{ 2187 struct iscsi_conn *l_conn; 2188 struct iscsi_session *sess = conn->sess; 2189 2190 pr_debug("Received logout request CLOSECONNECTION for CID:" 2191 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid); 2192 2193 /* 2194 * A Logout Request with a CLOSECONNECTION reason code for a CID 2195 * can arrive on a connection with a differing CID. 2196 */ 2197 if (conn->cid == cmd->logout_cid) { 2198 spin_lock_bh(&conn->state_lock); 2199 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n"); 2200 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT; 2201 2202 atomic_set(&conn->conn_logout_remove, 1); 2203 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION; 2204 iscsit_inc_conn_usage_count(conn); 2205 2206 spin_unlock_bh(&conn->state_lock); 2207 } else { 2208 /* 2209 * Handle all different cid CLOSECONNECTION requests in 2210 * iscsit_logout_post_handler_diffcid() as to give enough 2211 * time for any non immediate command's CmdSN to be 2212 * acknowledged on the connection in question. 2213 * 2214 * Here we simply make sure the CID is still around. 2215 */ 2216 l_conn = iscsit_get_conn_from_cid(sess, 2217 cmd->logout_cid); 2218 if (!l_conn) { 2219 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND; 2220 iscsit_add_cmd_to_response_queue(cmd, conn, 2221 cmd->i_state); 2222 return 0; 2223 } 2224 2225 iscsit_dec_conn_usage_count(l_conn); 2226 } 2227 2228 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 2229 2230 return 0; 2231} 2232 2233int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 2234{ 2235 struct iscsi_session *sess = conn->sess; 2236 2237 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for" 2238 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid); 2239 2240 if (sess->sess_ops->ErrorRecoveryLevel != 2) { 2241 pr_err("Received Logout Request REMOVECONNFORRECOVERY" 2242 " while ERL!=2.\n"); 2243 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED; 2244 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 2245 return 0; 2246 } 2247 2248 if (conn->cid == cmd->logout_cid) { 2249 pr_err("Received Logout Request REMOVECONNFORRECOVERY" 2250 " with CID: %hu on CID: %hu, implementation error.\n", 2251 cmd->logout_cid, conn->cid); 2252 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED; 2253 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 2254 return 0; 2255 } 2256 2257 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); 2258 2259 return 0; 2260} 2261 2262int 2263iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, 2264 unsigned char *buf) 2265{ 2266 int cmdsn_ret, logout_remove = 0; 2267 u8 reason_code = 0; 2268 struct iscsi_logout *hdr; 2269 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn); 2270 2271 hdr = (struct iscsi_logout *) buf; 2272 reason_code = (hdr->flags & 0x7f); 2273 2274 if (tiqn) { 2275 spin_lock(&tiqn->logout_stats.lock); 2276 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) 2277 tiqn->logout_stats.normal_logouts++; 2278 else 2279 tiqn->logout_stats.abnormal_logouts++; 2280 spin_unlock(&tiqn->logout_stats.lock); 2281 } 2282 2283 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x" 2284 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n", 2285 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code, 2286 hdr->cid, conn->cid); 2287 2288 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) { 2289 pr_err("Received logout request on connection that" 2290 " is not in logged in state, ignoring request.\n"); 2291 iscsit_free_cmd(cmd, false); 2292 return 0; 2293 } 2294 2295 cmd->iscsi_opcode = ISCSI_OP_LOGOUT; 2296 cmd->i_state = ISTATE_SEND_LOGOUTRSP; 2297 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); 2298 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; 2299 cmd->targ_xfer_tag = 0xFFFFFFFF; 2300 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); 2301 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); 2302 cmd->logout_cid = be16_to_cpu(hdr->cid); 2303 cmd->logout_reason = reason_code; 2304 cmd->data_direction = DMA_NONE; 2305 2306 /* 2307 * We need to sleep in these cases (by returning 1) until the Logout 2308 * Response gets sent in the tx thread. 2309 */ 2310 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) || 2311 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) && 2312 be16_to_cpu(hdr->cid) == conn->cid)) 2313 logout_remove = 1; 2314 2315 spin_lock_bh(&conn->cmd_lock); 2316 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); 2317 spin_unlock_bh(&conn->cmd_lock); 2318 2319 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY) 2320 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); 2321 2322 /* 2323 * Immediate commands are executed, well, immediately. 2324 * Non-Immediate Logout Commands are executed in CmdSN order. 2325 */ 2326 if (cmd->immediate_cmd) { 2327 int ret = iscsit_execute_cmd(cmd, 0); 2328 2329 if (ret < 0) 2330 return ret; 2331 } else { 2332 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn); 2333 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) 2334 logout_remove = 0; 2335 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) 2336 return -1; 2337 } 2338 2339 return logout_remove; 2340} 2341EXPORT_SYMBOL(iscsit_handle_logout_cmd); 2342 2343static int iscsit_handle_snack( 2344 struct iscsi_conn *conn, 2345 unsigned char *buf) 2346{ 2347 struct iscsi_snack *hdr; 2348 2349 hdr = (struct iscsi_snack *) buf; 2350 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL; 2351 2352 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:" 2353 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x," 2354 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags, 2355 hdr->begrun, hdr->runlength, conn->cid); 2356 2357 if (!conn->sess->sess_ops->ErrorRecoveryLevel) { 2358 pr_err("Initiator sent SNACK request while in" 2359 " ErrorRecoveryLevel=0.\n"); 2360 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 2361 buf); 2362 } 2363 /* 2364 * SNACK_DATA and SNACK_R2T are both 0, so check which function to 2365 * call from inside iscsi_send_recovery_datain_or_r2t(). 2366 */ 2367 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) { 2368 case 0: 2369 return iscsit_handle_recovery_datain_or_r2t(conn, buf, 2370 hdr->itt, 2371 be32_to_cpu(hdr->ttt), 2372 be32_to_cpu(hdr->begrun), 2373 be32_to_cpu(hdr->runlength)); 2374 case ISCSI_FLAG_SNACK_TYPE_STATUS: 2375 return iscsit_handle_status_snack(conn, hdr->itt, 2376 be32_to_cpu(hdr->ttt), 2377 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength)); 2378 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK: 2379 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt), 2380 be32_to_cpu(hdr->begrun), 2381 be32_to_cpu(hdr->runlength)); 2382 case ISCSI_FLAG_SNACK_TYPE_RDATA: 2383 /* FIXME: Support R-Data SNACK */ 2384 pr_err("R-Data SNACK Not Supported.\n"); 2385 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 2386 buf); 2387 default: 2388 pr_err("Unknown SNACK type 0x%02x, protocol" 2389 " error.\n", hdr->flags & 0x0f); 2390 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 2391 buf); 2392 } 2393 2394 return 0; 2395} 2396 2397static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn) 2398{ 2399 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) || 2400 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) { 2401 wait_for_completion_interruptible_timeout( 2402 &conn->rx_half_close_comp, 2403 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ); 2404 } 2405} 2406 2407static int iscsit_handle_immediate_data( 2408 struct iscsi_cmd *cmd, 2409 struct iscsi_scsi_req *hdr, 2410 u32 length) 2411{ 2412 int iov_ret, rx_got = 0, rx_size = 0; 2413 u32 checksum, iov_count = 0, padding = 0; 2414 struct iscsi_conn *conn = cmd->conn; 2415 struct kvec *iov; 2416 2417 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length); 2418 if (iov_ret < 0) 2419 return IMMEDIATE_DATA_CANNOT_RECOVER; 2420 2421 rx_size = length; 2422 iov_count = iov_ret; 2423 iov = &cmd->iov_data[0]; 2424 2425 padding = ((-length) & 3); 2426 if (padding != 0) { 2427 iov[iov_count].iov_base = cmd->pad_bytes; 2428 iov[iov_count++].iov_len = padding; 2429 rx_size += padding; 2430 } 2431 2432 if (conn->conn_ops->DataDigest) { 2433 iov[iov_count].iov_base = &checksum; 2434 iov[iov_count++].iov_len = ISCSI_CRC_LEN; 2435 rx_size += ISCSI_CRC_LEN; 2436 } 2437 2438 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size); 2439 2440 iscsit_unmap_iovec(cmd); 2441 2442 if (rx_got != rx_size) { 2443 iscsit_rx_thread_wait_for_tcp(conn); 2444 return IMMEDIATE_DATA_CANNOT_RECOVER; 2445 } 2446 2447 if (conn->conn_ops->DataDigest) { 2448 u32 data_crc; 2449 2450 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd, 2451 cmd->write_data_done, length, padding, 2452 cmd->pad_bytes); 2453 2454 if (checksum != data_crc) { 2455 pr_err("ImmediateData CRC32C DataDigest 0x%08x" 2456 " does not match computed 0x%08x\n", checksum, 2457 data_crc); 2458 2459 if (!conn->sess->sess_ops->ErrorRecoveryLevel) { 2460 pr_err("Unable to recover from" 2461 " Immediate Data digest failure while" 2462 " in ERL=0.\n"); 2463 iscsit_reject_cmd(cmd, 2464 ISCSI_REASON_DATA_DIGEST_ERROR, 2465 (unsigned char *)hdr); 2466 return IMMEDIATE_DATA_CANNOT_RECOVER; 2467 } else { 2468 iscsit_reject_cmd(cmd, 2469 ISCSI_REASON_DATA_DIGEST_ERROR, 2470 (unsigned char *)hdr); 2471 return IMMEDIATE_DATA_ERL1_CRC_FAILURE; 2472 } 2473 } else { 2474 pr_debug("Got CRC32C DataDigest 0x%08x for" 2475 " %u bytes of Immediate Data\n", checksum, 2476 length); 2477 } 2478 } 2479 2480 cmd->write_data_done += length; 2481 2482 if (cmd->write_data_done == cmd->se_cmd.data_length) { 2483 spin_lock_bh(&cmd->istate_lock); 2484 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT; 2485 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT; 2486 spin_unlock_bh(&cmd->istate_lock); 2487 } 2488 2489 return IMMEDIATE_DATA_NORMAL_OPERATION; 2490} 2491 2492/* 2493 * Called with sess->conn_lock held. 2494 */ 2495/* #warning iscsi_build_conn_drop_async_message() only sends out on connections 2496 with active network interface */ 2497static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn) 2498{ 2499 struct iscsi_cmd *cmd; 2500 struct iscsi_conn *conn_p; 2501 bool found = false; 2502 2503 /* 2504 * Only send a Asynchronous Message on connections whos network 2505 * interface is still functional. 2506 */ 2507 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) { 2508 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) { 2509 iscsit_inc_conn_usage_count(conn_p); 2510 found = true; 2511 break; 2512 } 2513 } 2514 2515 if (!found) 2516 return; 2517 2518 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING); 2519 if (!cmd) { 2520 iscsit_dec_conn_usage_count(conn_p); 2521 return; 2522 } 2523 2524 cmd->logout_cid = conn->cid; 2525 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT; 2526 cmd->i_state = ISTATE_SEND_ASYNCMSG; 2527 2528 spin_lock_bh(&conn_p->cmd_lock); 2529 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list); 2530 spin_unlock_bh(&conn_p->cmd_lock); 2531 2532 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state); 2533 iscsit_dec_conn_usage_count(conn_p); 2534} 2535 2536static int iscsit_send_conn_drop_async_message( 2537 struct iscsi_cmd *cmd, 2538 struct iscsi_conn *conn) 2539{ 2540 struct iscsi_async *hdr; 2541 2542 cmd->tx_size = ISCSI_HDR_LEN; 2543 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT; 2544 2545 hdr = (struct iscsi_async *) cmd->pdu; 2546 hdr->opcode = ISCSI_OP_ASYNC_EVENT; 2547 hdr->flags = ISCSI_FLAG_CMD_FINAL; 2548 cmd->init_task_tag = RESERVED_ITT; 2549 cmd->targ_xfer_tag = 0xFFFFFFFF; 2550 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]); 2551 cmd->stat_sn = conn->stat_sn++; 2552 hdr->statsn = cpu_to_be32(cmd->stat_sn); 2553 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 2554 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); 2555 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION; 2556 hdr->param1 = cpu_to_be16(cmd->logout_cid); 2557 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait); 2558 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain); 2559 2560 if (conn->conn_ops->HeaderDigest) { 2561 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; 2562 2563 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, 2564 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); 2565 2566 cmd->tx_size += ISCSI_CRC_LEN; 2567 pr_debug("Attaching CRC32C HeaderDigest to" 2568 " Async Message 0x%08x\n", *header_digest); 2569 } 2570 2571 cmd->iov_misc[0].iov_base = cmd->pdu; 2572 cmd->iov_misc[0].iov_len = cmd->tx_size; 2573 cmd->iov_misc_count = 1; 2574 2575 pr_debug("Sending Connection Dropped Async Message StatSN:" 2576 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn, 2577 cmd->logout_cid, conn->cid); 2578 return 0; 2579} 2580 2581static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn) 2582{ 2583 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) || 2584 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) { 2585 wait_for_completion_interruptible_timeout( 2586 &conn->tx_half_close_comp, 2587 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ); 2588 } 2589} 2590 2591static void 2592iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 2593 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr, 2594 bool set_statsn) 2595{ 2596 hdr->opcode = ISCSI_OP_SCSI_DATA_IN; 2597 hdr->flags = datain->flags; 2598 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) { 2599 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) { 2600 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW; 2601 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); 2602 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) { 2603 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW; 2604 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); 2605 } 2606 } 2607 hton24(hdr->dlength, datain->length); 2608 if (hdr->flags & ISCSI_FLAG_DATA_ACK) 2609 int_to_scsilun(cmd->se_cmd.orig_fe_lun, 2610 (struct scsi_lun *)&hdr->lun); 2611 else 2612 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun); 2613 2614 hdr->itt = cmd->init_task_tag; 2615 2616 if (hdr->flags & ISCSI_FLAG_DATA_ACK) 2617 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag); 2618 else 2619 hdr->ttt = cpu_to_be32(0xFFFFFFFF); 2620 if (set_statsn) 2621 hdr->statsn = cpu_to_be32(cmd->stat_sn); 2622 else 2623 hdr->statsn = cpu_to_be32(0xFFFFFFFF); 2624 2625 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 2626 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); 2627 hdr->datasn = cpu_to_be32(datain->data_sn); 2628 hdr->offset = cpu_to_be32(datain->offset); 2629 2630 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x," 2631 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n", 2632 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn), 2633 ntohl(hdr->offset), datain->length, conn->cid); 2634} 2635 2636static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 2637{ 2638 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0]; 2639 struct iscsi_datain datain; 2640 struct iscsi_datain_req *dr; 2641 struct kvec *iov; 2642 u32 iov_count = 0, tx_size = 0; 2643 int eodr = 0, ret, iov_ret; 2644 bool set_statsn = false; 2645 2646 memset(&datain, 0, sizeof(struct iscsi_datain)); 2647 dr = iscsit_get_datain_values(cmd, &datain); 2648 if (!dr) { 2649 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n", 2650 cmd->init_task_tag); 2651 return -1; 2652 } 2653 /* 2654 * Be paranoid and double check the logic for now. 2655 */ 2656 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) { 2657 pr_err("Command ITT: 0x%08x, datain.offset: %u and" 2658 " datain.length: %u exceeds cmd->data_length: %u\n", 2659 cmd->init_task_tag, datain.offset, datain.length, 2660 cmd->se_cmd.data_length); 2661 return -1; 2662 } 2663 2664 atomic_long_add(datain.length, &conn->sess->tx_data_octets); 2665 /* 2666 * Special case for successfully execution w/ both DATAIN 2667 * and Sense Data. 2668 */ 2669 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) && 2670 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)) 2671 datain.flags &= ~ISCSI_FLAG_DATA_STATUS; 2672 else { 2673 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) || 2674 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) { 2675 iscsit_increment_maxcmdsn(cmd, conn->sess); 2676 cmd->stat_sn = conn->stat_sn++; 2677 set_statsn = true; 2678 } else if (dr->dr_complete == 2679 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY) 2680 set_statsn = true; 2681 } 2682 2683 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn); 2684 2685 iov = &cmd->iov_data[0]; 2686 iov[iov_count].iov_base = cmd->pdu; 2687 iov[iov_count++].iov_len = ISCSI_HDR_LEN; 2688 tx_size += ISCSI_HDR_LEN; 2689 2690 if (conn->conn_ops->HeaderDigest) { 2691 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; 2692 2693 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu, 2694 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); 2695 2696 iov[0].iov_len += ISCSI_CRC_LEN; 2697 tx_size += ISCSI_CRC_LEN; 2698 2699 pr_debug("Attaching CRC32 HeaderDigest" 2700 " for DataIN PDU 0x%08x\n", *header_digest); 2701 } 2702 2703 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1], 2704 datain.offset, datain.length); 2705 if (iov_ret < 0) 2706 return -1; 2707 2708 iov_count += iov_ret; 2709 tx_size += datain.length; 2710 2711 cmd->padding = ((-datain.length) & 3); 2712 if (cmd->padding) { 2713 iov[iov_count].iov_base = cmd->pad_bytes; 2714 iov[iov_count++].iov_len = cmd->padding; 2715 tx_size += cmd->padding; 2716 2717 pr_debug("Attaching %u padding bytes\n", 2718 cmd->padding); 2719 } 2720 if (conn->conn_ops->DataDigest) { 2721 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd, 2722 datain.offset, datain.length, cmd->padding, cmd->pad_bytes); 2723 2724 iov[iov_count].iov_base = &cmd->data_crc; 2725 iov[iov_count++].iov_len = ISCSI_CRC_LEN; 2726 tx_size += ISCSI_CRC_LEN; 2727 2728 pr_debug("Attached CRC32C DataDigest %d bytes, crc" 2729 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc); 2730 } 2731 2732 cmd->iov_data_count = iov_count; 2733 cmd->tx_size = tx_size; 2734 2735 /* sendpage is preferred but can't insert markers */ 2736 if (!conn->conn_ops->IFMarker) 2737 ret = iscsit_fe_sendpage_sg(cmd, conn); 2738 else 2739 ret = iscsit_send_tx_data(cmd, conn, 0); 2740 2741 iscsit_unmap_iovec(cmd); 2742 2743 if (ret < 0) { 2744 iscsit_tx_thread_wait_for_tcp(conn); 2745 return ret; 2746 } 2747 2748 if (dr->dr_complete) { 2749 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ? 2750 2 : 1; 2751 iscsit_free_datain_req(cmd, dr); 2752 } 2753 2754 return eodr; 2755} 2756 2757int 2758iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 2759 struct iscsi_logout_rsp *hdr) 2760{ 2761 struct iscsi_conn *logout_conn = NULL; 2762 struct iscsi_conn_recovery *cr = NULL; 2763 struct iscsi_session *sess = conn->sess; 2764 /* 2765 * The actual shutting down of Sessions and/or Connections 2766 * for CLOSESESSION and CLOSECONNECTION Logout Requests 2767 * is done in scsi_logout_post_handler(). 2768 */ 2769 switch (cmd->logout_reason) { 2770 case ISCSI_LOGOUT_REASON_CLOSE_SESSION: 2771 pr_debug("iSCSI session logout successful, setting" 2772 " logout response to ISCSI_LOGOUT_SUCCESS.\n"); 2773 cmd->logout_response = ISCSI_LOGOUT_SUCCESS; 2774 break; 2775 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION: 2776 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND) 2777 break; 2778 /* 2779 * For CLOSECONNECTION logout requests carrying 2780 * a matching logout CID -> local CID, the reference 2781 * for the local CID will have been incremented in 2782 * iscsi_logout_closeconnection(). 2783 * 2784 * For CLOSECONNECTION logout requests carrying 2785 * a different CID than the connection it arrived 2786 * on, the connection responding to cmd->logout_cid 2787 * is stopped in iscsit_logout_post_handler_diffcid(). 2788 */ 2789 2790 pr_debug("iSCSI CID: %hu logout on CID: %hu" 2791 " successful.\n", cmd->logout_cid, conn->cid); 2792 cmd->logout_response = ISCSI_LOGOUT_SUCCESS; 2793 break; 2794 case ISCSI_LOGOUT_REASON_RECOVERY: 2795 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) || 2796 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED)) 2797 break; 2798 /* 2799 * If the connection is still active from our point of view 2800 * force connection recovery to occur. 2801 */ 2802 logout_conn = iscsit_get_conn_from_cid_rcfr(sess, 2803 cmd->logout_cid); 2804 if (logout_conn) { 2805 iscsit_connection_reinstatement_rcfr(logout_conn); 2806 iscsit_dec_conn_usage_count(logout_conn); 2807 } 2808 2809 cr = iscsit_get_inactive_connection_recovery_entry( 2810 conn->sess, cmd->logout_cid); 2811 if (!cr) { 2812 pr_err("Unable to locate CID: %hu for" 2813 " REMOVECONNFORRECOVERY Logout Request.\n", 2814 cmd->logout_cid); 2815 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND; 2816 break; 2817 } 2818 2819 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn); 2820 2821 pr_debug("iSCSI REMOVECONNFORRECOVERY logout" 2822 " for recovery for CID: %hu on CID: %hu successful.\n", 2823 cmd->logout_cid, conn->cid); 2824 cmd->logout_response = ISCSI_LOGOUT_SUCCESS; 2825 break; 2826 default: 2827 pr_err("Unknown cmd->logout_reason: 0x%02x\n", 2828 cmd->logout_reason); 2829 return -1; 2830 } 2831 2832 hdr->opcode = ISCSI_OP_LOGOUT_RSP; 2833 hdr->flags |= ISCSI_FLAG_CMD_FINAL; 2834 hdr->response = cmd->logout_response; 2835 hdr->itt = cmd->init_task_tag; 2836 cmd->stat_sn = conn->stat_sn++; 2837 hdr->statsn = cpu_to_be32(cmd->stat_sn); 2838 2839 iscsit_increment_maxcmdsn(cmd, conn->sess); 2840 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 2841 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); 2842 2843 pr_debug("Built Logout Response ITT: 0x%08x StatSN:" 2844 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n", 2845 cmd->init_task_tag, cmd->stat_sn, hdr->response, 2846 cmd->logout_cid, conn->cid); 2847 2848 return 0; 2849} 2850EXPORT_SYMBOL(iscsit_build_logout_rsp); 2851 2852static int 2853iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 2854{ 2855 struct kvec *iov; 2856 int niov = 0, tx_size, rc; 2857 2858 rc = iscsit_build_logout_rsp(cmd, conn, 2859 (struct iscsi_logout_rsp *)&cmd->pdu[0]); 2860 if (rc < 0) 2861 return rc; 2862 2863 tx_size = ISCSI_HDR_LEN; 2864 iov = &cmd->iov_misc[0]; 2865 iov[niov].iov_base = cmd->pdu; 2866 iov[niov++].iov_len = ISCSI_HDR_LEN; 2867 2868 if (conn->conn_ops->HeaderDigest) { 2869 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; 2870 2871 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0], 2872 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); 2873 2874 iov[0].iov_len += ISCSI_CRC_LEN; 2875 tx_size += ISCSI_CRC_LEN; 2876 pr_debug("Attaching CRC32C HeaderDigest to" 2877 " Logout Response 0x%08x\n", *header_digest); 2878 } 2879 cmd->iov_misc_count = niov; 2880 cmd->tx_size = tx_size; 2881 2882 return 0; 2883} 2884 2885void 2886iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 2887 struct iscsi_nopin *hdr, bool nopout_response) 2888{ 2889 hdr->opcode = ISCSI_OP_NOOP_IN; 2890 hdr->flags |= ISCSI_FLAG_CMD_FINAL; 2891 hton24(hdr->dlength, cmd->buf_ptr_size); 2892 if (nopout_response) 2893 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun); 2894 hdr->itt = cmd->init_task_tag; 2895 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag); 2896 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ : 2897 conn->stat_sn; 2898 hdr->statsn = cpu_to_be32(cmd->stat_sn); 2899 2900 if (nopout_response) 2901 iscsit_increment_maxcmdsn(cmd, conn->sess); 2902 2903 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 2904 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); 2905 2906 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x," 2907 " StatSN: 0x%08x, Length %u\n", (nopout_response) ? 2908 "Solicitied" : "Unsolicitied", cmd->init_task_tag, 2909 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size); 2910} 2911EXPORT_SYMBOL(iscsit_build_nopin_rsp); 2912 2913/* 2914 * Unsolicited NOPIN, either requesting a response or not. 2915 */ 2916static int iscsit_send_unsolicited_nopin( 2917 struct iscsi_cmd *cmd, 2918 struct iscsi_conn *conn, 2919 int want_response) 2920{ 2921 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0]; 2922 int tx_size = ISCSI_HDR_LEN, ret; 2923 2924 iscsit_build_nopin_rsp(cmd, conn, hdr, false); 2925 2926 if (conn->conn_ops->HeaderDigest) { 2927 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; 2928 2929 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, 2930 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); 2931 2932 tx_size += ISCSI_CRC_LEN; 2933 pr_debug("Attaching CRC32C HeaderDigest to" 2934 " NopIN 0x%08x\n", *header_digest); 2935 } 2936 2937 cmd->iov_misc[0].iov_base = cmd->pdu; 2938 cmd->iov_misc[0].iov_len = tx_size; 2939 cmd->iov_misc_count = 1; 2940 cmd->tx_size = tx_size; 2941 2942 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:" 2943 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid); 2944 2945 ret = iscsit_send_tx_data(cmd, conn, 1); 2946 if (ret < 0) { 2947 iscsit_tx_thread_wait_for_tcp(conn); 2948 return ret; 2949 } 2950 2951 spin_lock_bh(&cmd->istate_lock); 2952 cmd->i_state = want_response ? 2953 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS; 2954 spin_unlock_bh(&cmd->istate_lock); 2955 2956 return 0; 2957} 2958 2959static int 2960iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 2961{ 2962 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0]; 2963 struct kvec *iov; 2964 u32 padding = 0; 2965 int niov = 0, tx_size; 2966 2967 iscsit_build_nopin_rsp(cmd, conn, hdr, true); 2968 2969 tx_size = ISCSI_HDR_LEN; 2970 iov = &cmd->iov_misc[0]; 2971 iov[niov].iov_base = cmd->pdu; 2972 iov[niov++].iov_len = ISCSI_HDR_LEN; 2973 2974 if (conn->conn_ops->HeaderDigest) { 2975 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; 2976 2977 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, 2978 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); 2979 2980 iov[0].iov_len += ISCSI_CRC_LEN; 2981 tx_size += ISCSI_CRC_LEN; 2982 pr_debug("Attaching CRC32C HeaderDigest" 2983 " to NopIn 0x%08x\n", *header_digest); 2984 } 2985 2986 /* 2987 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr. 2988 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size. 2989 */ 2990 if (cmd->buf_ptr_size) { 2991 iov[niov].iov_base = cmd->buf_ptr; 2992 iov[niov++].iov_len = cmd->buf_ptr_size; 2993 tx_size += cmd->buf_ptr_size; 2994 2995 pr_debug("Echoing back %u bytes of ping" 2996 " data.\n", cmd->buf_ptr_size); 2997 2998 padding = ((-cmd->buf_ptr_size) & 3); 2999 if (padding != 0) { 3000 iov[niov].iov_base = &cmd->pad_bytes; 3001 iov[niov++].iov_len = padding; 3002 tx_size += padding; 3003 pr_debug("Attaching %u additional" 3004 " padding bytes.\n", padding); 3005 } 3006 if (conn->conn_ops->DataDigest) { 3007 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, 3008 cmd->buf_ptr, cmd->buf_ptr_size, 3009 padding, (u8 *)&cmd->pad_bytes, 3010 (u8 *)&cmd->data_crc); 3011 3012 iov[niov].iov_base = &cmd->data_crc; 3013 iov[niov++].iov_len = ISCSI_CRC_LEN; 3014 tx_size += ISCSI_CRC_LEN; 3015 pr_debug("Attached DataDigest for %u" 3016 " bytes of ping data, CRC 0x%08x\n", 3017 cmd->buf_ptr_size, cmd->data_crc); 3018 } 3019 } 3020 3021 cmd->iov_misc_count = niov; 3022 cmd->tx_size = tx_size; 3023 3024 return 0; 3025} 3026 3027static int iscsit_send_r2t( 3028 struct iscsi_cmd *cmd, 3029 struct iscsi_conn *conn) 3030{ 3031 int tx_size = 0; 3032 struct iscsi_r2t *r2t; 3033 struct iscsi_r2t_rsp *hdr; 3034 int ret; 3035 3036 r2t = iscsit_get_r2t_from_list(cmd); 3037 if (!r2t) 3038 return -1; 3039 3040 hdr = (struct iscsi_r2t_rsp *) cmd->pdu; 3041 memset(hdr, 0, ISCSI_HDR_LEN); 3042 hdr->opcode = ISCSI_OP_R2T; 3043 hdr->flags |= ISCSI_FLAG_CMD_FINAL; 3044 int_to_scsilun(cmd->se_cmd.orig_fe_lun, 3045 (struct scsi_lun *)&hdr->lun); 3046 hdr->itt = cmd->init_task_tag; 3047 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess); 3048 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag); 3049 hdr->statsn = cpu_to_be32(conn->stat_sn); 3050 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 3051 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); 3052 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn); 3053 hdr->data_offset = cpu_to_be32(r2t->offset); 3054 hdr->data_length = cpu_to_be32(r2t->xfer_len); 3055 3056 cmd->iov_misc[0].iov_base = cmd->pdu; 3057 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN; 3058 tx_size += ISCSI_HDR_LEN; 3059 3060 if (conn->conn_ops->HeaderDigest) { 3061 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; 3062 3063 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, 3064 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); 3065 3066 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN; 3067 tx_size += ISCSI_CRC_LEN; 3068 pr_debug("Attaching CRC32 HeaderDigest for R2T" 3069 " PDU 0x%08x\n", *header_digest); 3070 } 3071 3072 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:" 3073 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n", 3074 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag, 3075 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn, 3076 r2t->offset, r2t->xfer_len, conn->cid); 3077 3078 cmd->iov_misc_count = 1; 3079 cmd->tx_size = tx_size; 3080 3081 spin_lock_bh(&cmd->r2t_lock); 3082 r2t->sent_r2t = 1; 3083 spin_unlock_bh(&cmd->r2t_lock); 3084 3085 ret = iscsit_send_tx_data(cmd, conn, 1); 3086 if (ret < 0) { 3087 iscsit_tx_thread_wait_for_tcp(conn); 3088 return ret; 3089 } 3090 3091 spin_lock_bh(&cmd->dataout_timeout_lock); 3092 iscsit_start_dataout_timer(cmd, conn); 3093 spin_unlock_bh(&cmd->dataout_timeout_lock); 3094 3095 return 0; 3096} 3097 3098/* 3099 * @recovery: If called from iscsi_task_reassign_complete_write() for 3100 * connection recovery. 3101 */ 3102int iscsit_build_r2ts_for_cmd( 3103 struct iscsi_conn *conn, 3104 struct iscsi_cmd *cmd, 3105 bool recovery) 3106{ 3107 int first_r2t = 1; 3108 u32 offset = 0, xfer_len = 0; 3109 3110 spin_lock_bh(&cmd->r2t_lock); 3111 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) { 3112 spin_unlock_bh(&cmd->r2t_lock); 3113 return 0; 3114 } 3115 3116 if (conn->sess->sess_ops->DataSequenceInOrder && 3117 !recovery) 3118 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done); 3119 3120 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) { 3121 if (conn->sess->sess_ops->DataSequenceInOrder) { 3122 offset = cmd->r2t_offset; 3123 3124 if (first_r2t && recovery) { 3125 int new_data_end = offset + 3126 conn->sess->sess_ops->MaxBurstLength - 3127 cmd->next_burst_len; 3128 3129 if (new_data_end > cmd->se_cmd.data_length) 3130 xfer_len = cmd->se_cmd.data_length - offset; 3131 else 3132 xfer_len = 3133 conn->sess->sess_ops->MaxBurstLength - 3134 cmd->next_burst_len; 3135 } else { 3136 int new_data_end = offset + 3137 conn->sess->sess_ops->MaxBurstLength; 3138 3139 if (new_data_end > cmd->se_cmd.data_length) 3140 xfer_len = cmd->se_cmd.data_length - offset; 3141 else 3142 xfer_len = conn->sess->sess_ops->MaxBurstLength; 3143 } 3144 cmd->r2t_offset += xfer_len; 3145 3146 if (cmd->r2t_offset == cmd->se_cmd.data_length) 3147 cmd->cmd_flags |= ICF_SENT_LAST_R2T; 3148 } else { 3149 struct iscsi_seq *seq; 3150 3151 seq = iscsit_get_seq_holder_for_r2t(cmd); 3152 if (!seq) { 3153 spin_unlock_bh(&cmd->r2t_lock); 3154 return -1; 3155 } 3156 3157 offset = seq->offset; 3158 xfer_len = seq->xfer_len; 3159 3160 if (cmd->seq_send_order == cmd->seq_count) 3161 cmd->cmd_flags |= ICF_SENT_LAST_R2T; 3162 } 3163 cmd->outstanding_r2ts++; 3164 first_r2t = 0; 3165 3166 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) { 3167 spin_unlock_bh(&cmd->r2t_lock); 3168 return -1; 3169 } 3170 3171 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) 3172 break; 3173 } 3174 spin_unlock_bh(&cmd->r2t_lock); 3175 3176 return 0; 3177} 3178 3179void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 3180 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr) 3181{ 3182 if (inc_stat_sn) 3183 cmd->stat_sn = conn->stat_sn++; 3184 3185 atomic_long_inc(&conn->sess->rsp_pdus); 3186 3187 memset(hdr, 0, ISCSI_HDR_LEN); 3188 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP; 3189 hdr->flags |= ISCSI_FLAG_CMD_FINAL; 3190 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) { 3191 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW; 3192 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); 3193 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) { 3194 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW; 3195 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); 3196 } 3197 hdr->response = cmd->iscsi_response; 3198 hdr->cmd_status = cmd->se_cmd.scsi_status; 3199 hdr->itt = cmd->init_task_tag; 3200 hdr->statsn = cpu_to_be32(cmd->stat_sn); 3201 3202 iscsit_increment_maxcmdsn(cmd, conn->sess); 3203 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 3204 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); 3205 3206 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x," 3207 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n", 3208 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status, 3209 cmd->se_cmd.scsi_status, conn->cid); 3210} 3211EXPORT_SYMBOL(iscsit_build_rsp_pdu); 3212 3213static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 3214{ 3215 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0]; 3216 struct kvec *iov; 3217 u32 padding = 0, tx_size = 0; 3218 int iov_count = 0; 3219 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS); 3220 3221 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr); 3222 3223 iov = &cmd->iov_misc[0]; 3224 iov[iov_count].iov_base = cmd->pdu; 3225 iov[iov_count++].iov_len = ISCSI_HDR_LEN; 3226 tx_size += ISCSI_HDR_LEN; 3227 3228 /* 3229 * Attach SENSE DATA payload to iSCSI Response PDU 3230 */ 3231 if (cmd->se_cmd.sense_buffer && 3232 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) || 3233 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) { 3234 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer); 3235 cmd->se_cmd.scsi_sense_length += sizeof (__be16); 3236 3237 padding = -(cmd->se_cmd.scsi_sense_length) & 3; 3238 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length); 3239 iov[iov_count].iov_base = cmd->sense_buffer; 3240 iov[iov_count++].iov_len = 3241 (cmd->se_cmd.scsi_sense_length + padding); 3242 tx_size += cmd->se_cmd.scsi_sense_length; 3243 3244 if (padding) { 3245 memset(cmd->sense_buffer + 3246 cmd->se_cmd.scsi_sense_length, 0, padding); 3247 tx_size += padding; 3248 pr_debug("Adding %u bytes of padding to" 3249 " SENSE.\n", padding); 3250 } 3251 3252 if (conn->conn_ops->DataDigest) { 3253 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, 3254 cmd->sense_buffer, 3255 (cmd->se_cmd.scsi_sense_length + padding), 3256 0, NULL, (u8 *)&cmd->data_crc); 3257 3258 iov[iov_count].iov_base = &cmd->data_crc; 3259 iov[iov_count++].iov_len = ISCSI_CRC_LEN; 3260 tx_size += ISCSI_CRC_LEN; 3261 3262 pr_debug("Attaching CRC32 DataDigest for" 3263 " SENSE, %u bytes CRC 0x%08x\n", 3264 (cmd->se_cmd.scsi_sense_length + padding), 3265 cmd->data_crc); 3266 } 3267 3268 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI" 3269 " Response PDU\n", 3270 cmd->se_cmd.scsi_sense_length); 3271 } 3272 3273 if (conn->conn_ops->HeaderDigest) { 3274 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; 3275 3276 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu, 3277 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); 3278 3279 iov[0].iov_len += ISCSI_CRC_LEN; 3280 tx_size += ISCSI_CRC_LEN; 3281 pr_debug("Attaching CRC32 HeaderDigest for Response" 3282 " PDU 0x%08x\n", *header_digest); 3283 } 3284 3285 cmd->iov_misc_count = iov_count; 3286 cmd->tx_size = tx_size; 3287 3288 return 0; 3289} 3290 3291static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr) 3292{ 3293 switch (se_tmr->response) { 3294 case TMR_FUNCTION_COMPLETE: 3295 return ISCSI_TMF_RSP_COMPLETE; 3296 case TMR_TASK_DOES_NOT_EXIST: 3297 return ISCSI_TMF_RSP_NO_TASK; 3298 case TMR_LUN_DOES_NOT_EXIST: 3299 return ISCSI_TMF_RSP_NO_LUN; 3300 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED: 3301 return ISCSI_TMF_RSP_NOT_SUPPORTED; 3302 case TMR_FUNCTION_REJECTED: 3303 default: 3304 return ISCSI_TMF_RSP_REJECTED; 3305 } 3306} 3307 3308void 3309iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 3310 struct iscsi_tm_rsp *hdr) 3311{ 3312 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req; 3313 3314 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP; 3315 hdr->flags = ISCSI_FLAG_CMD_FINAL; 3316 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr); 3317 hdr->itt = cmd->init_task_tag; 3318 cmd->stat_sn = conn->stat_sn++; 3319 hdr->statsn = cpu_to_be32(cmd->stat_sn); 3320 3321 iscsit_increment_maxcmdsn(cmd, conn->sess); 3322 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 3323 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); 3324 3325 pr_debug("Built Task Management Response ITT: 0x%08x," 3326 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n", 3327 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid); 3328} 3329EXPORT_SYMBOL(iscsit_build_task_mgt_rsp); 3330 3331static int 3332iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn) 3333{ 3334 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0]; 3335 u32 tx_size = 0; 3336 3337 iscsit_build_task_mgt_rsp(cmd, conn, hdr); 3338 3339 cmd->iov_misc[0].iov_base = cmd->pdu; 3340 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN; 3341 tx_size += ISCSI_HDR_LEN; 3342 3343 if (conn->conn_ops->HeaderDigest) { 3344 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; 3345 3346 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, 3347 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); 3348 3349 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN; 3350 tx_size += ISCSI_CRC_LEN; 3351 pr_debug("Attaching CRC32 HeaderDigest for Task" 3352 " Mgmt Response PDU 0x%08x\n", *header_digest); 3353 } 3354 3355 cmd->iov_misc_count = 1; 3356 cmd->tx_size = tx_size; 3357 3358 return 0; 3359} 3360 3361static bool iscsit_check_inaddr_any(struct iscsi_np *np) 3362{ 3363 bool ret = false; 3364 3365 if (np->np_sockaddr.ss_family == AF_INET6) { 3366 const struct sockaddr_in6 sin6 = { 3367 .sin6_addr = IN6ADDR_ANY_INIT }; 3368 struct sockaddr_in6 *sock_in6 = 3369 (struct sockaddr_in6 *)&np->np_sockaddr; 3370 3371 if (!memcmp(sock_in6->sin6_addr.s6_addr, 3372 sin6.sin6_addr.s6_addr, 16)) 3373 ret = true; 3374 } else { 3375 struct sockaddr_in * sock_in = 3376 (struct sockaddr_in *)&np->np_sockaddr; 3377 3378 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY)) 3379 ret = true; 3380 } 3381 3382 return ret; 3383} 3384 3385#define SENDTARGETS_BUF_LIMIT 32768U 3386 3387static int 3388iscsit_build_sendtargets_response(struct iscsi_cmd *cmd, 3389 enum iscsit_transport_type network_transport, 3390 int skip_bytes, bool *completed) 3391{ 3392 char *payload = NULL; 3393 struct iscsi_conn *conn = cmd->conn; 3394 struct iscsi_portal_group *tpg; 3395 struct iscsi_tiqn *tiqn; 3396 struct iscsi_tpg_np *tpg_np; 3397 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0; 3398 int target_name_printed; 3399 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */ 3400 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL; 3401 3402 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength, 3403 SENDTARGETS_BUF_LIMIT); 3404 3405 payload = kzalloc(buffer_len, GFP_KERNEL); 3406 if (!payload) { 3407 pr_err("Unable to allocate memory for sendtargets" 3408 " response.\n"); 3409 return -ENOMEM; 3410 } 3411 /* 3412 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE 3413 * explicit case.. 3414 */ 3415 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) { 3416 text_ptr = strchr(text_in, '='); 3417 if (!text_ptr) { 3418 pr_err("Unable to locate '=' string in text_in:" 3419 " %s\n", text_in); 3420 kfree(payload); 3421 return -EINVAL; 3422 } 3423 /* 3424 * Skip over '=' character.. 3425 */ 3426 text_ptr += 1; 3427 } 3428 3429 spin_lock(&tiqn_lock); 3430 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) { 3431 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) && 3432 strcmp(tiqn->tiqn, text_ptr)) { 3433 continue; 3434 } 3435 3436 target_name_printed = 0; 3437 3438 spin_lock(&tiqn->tiqn_tpg_lock); 3439 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) { 3440 3441 /* If demo_mode_discovery=0 and generate_node_acls=0 3442 * (demo mode dislabed) do not return 3443 * TargetName+TargetAddress unless a NodeACL exists. 3444 */ 3445 3446 if ((tpg->tpg_attrib.generate_node_acls == 0) && 3447 (tpg->tpg_attrib.demo_mode_discovery == 0) && 3448 (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg, 3449 cmd->conn->sess->sess_ops->InitiatorName))) { 3450 continue; 3451 } 3452 3453 spin_lock(&tpg->tpg_state_lock); 3454 if ((tpg->tpg_state == TPG_STATE_FREE) || 3455 (tpg->tpg_state == TPG_STATE_INACTIVE)) { 3456 spin_unlock(&tpg->tpg_state_lock); 3457 continue; 3458 } 3459 spin_unlock(&tpg->tpg_state_lock); 3460 3461 spin_lock(&tpg->tpg_np_lock); 3462 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, 3463 tpg_np_list) { 3464 struct iscsi_np *np = tpg_np->tpg_np; 3465 bool inaddr_any = iscsit_check_inaddr_any(np); 3466 3467 if (np->np_network_transport != network_transport) 3468 continue; 3469 3470 if (!target_name_printed) { 3471 len = sprintf(buf, "TargetName=%s", 3472 tiqn->tiqn); 3473 len += 1; 3474 3475 if ((len + payload_len) > buffer_len) { 3476 spin_unlock(&tpg->tpg_np_lock); 3477 spin_unlock(&tiqn->tiqn_tpg_lock); 3478 end_of_buf = 1; 3479 goto eob; 3480 } 3481 3482 if (skip_bytes && len <= skip_bytes) { 3483 skip_bytes -= len; 3484 } else { 3485 memcpy(payload + payload_len, buf, len); 3486 payload_len += len; 3487 target_name_printed = 1; 3488 if (len > skip_bytes) 3489 skip_bytes = 0; 3490 } 3491 } 3492 3493 if (inaddr_any) { 3494 len = sprintf(buf, "TargetAddress=" 3495 "%s:%hu,%hu", 3496 conn->local_ip, 3497 np->np_port, 3498 tpg->tpgt); 3499 } else { 3500 len = sprintf(buf, "TargetAddress=" 3501 "%pISpc,%hu", 3502 &np->np_sockaddr, 3503 tpg->tpgt); 3504 } 3505 len += 1; 3506 3507 if ((len + payload_len) > buffer_len) { 3508 spin_unlock(&tpg->tpg_np_lock); 3509 spin_unlock(&tiqn->tiqn_tpg_lock); 3510 end_of_buf = 1; 3511 goto eob; 3512 } 3513 3514 if (skip_bytes && len <= skip_bytes) { 3515 skip_bytes -= len; 3516 } else { 3517 memcpy(payload + payload_len, buf, len); 3518 payload_len += len; 3519 if (len > skip_bytes) 3520 skip_bytes = 0; 3521 } 3522 } 3523 spin_unlock(&tpg->tpg_np_lock); 3524 } 3525 spin_unlock(&tiqn->tiqn_tpg_lock); 3526eob: 3527 if (end_of_buf) { 3528 *completed = false; 3529 break; 3530 } 3531 3532 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) 3533 break; 3534 } 3535 spin_unlock(&tiqn_lock); 3536 3537 cmd->buf_ptr = payload; 3538 3539 return payload_len; 3540} 3541 3542int 3543iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 3544 struct iscsi_text_rsp *hdr, 3545 enum iscsit_transport_type network_transport) 3546{ 3547 int text_length, padding; 3548 bool completed = true; 3549 3550 text_length = iscsit_build_sendtargets_response(cmd, network_transport, 3551 cmd->read_data_done, 3552 &completed); 3553 if (text_length < 0) 3554 return text_length; 3555 3556 if (completed) { 3557 hdr->flags |= ISCSI_FLAG_CMD_FINAL; 3558 } else { 3559 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE; 3560 cmd->read_data_done += text_length; 3561 if (cmd->targ_xfer_tag == 0xFFFFFFFF) 3562 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess); 3563 } 3564 hdr->opcode = ISCSI_OP_TEXT_RSP; 3565 padding = ((-text_length) & 3); 3566 hton24(hdr->dlength, text_length); 3567 hdr->itt = cmd->init_task_tag; 3568 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag); 3569 cmd->stat_sn = conn->stat_sn++; 3570 hdr->statsn = cpu_to_be32(cmd->stat_sn); 3571 3572 iscsit_increment_maxcmdsn(cmd, conn->sess); 3573 /* 3574 * Reset maxcmdsn_inc in multi-part text payload exchanges to 3575 * correctly increment MaxCmdSN for each response answering a 3576 * non immediate text request with a valid CmdSN. 3577 */ 3578 cmd->maxcmdsn_inc = 0; 3579 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 3580 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); 3581 3582 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x," 3583 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag, 3584 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid, 3585 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL), 3586 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)); 3587 3588 return text_length + padding; 3589} 3590EXPORT_SYMBOL(iscsit_build_text_rsp); 3591 3592static int iscsit_send_text_rsp( 3593 struct iscsi_cmd *cmd, 3594 struct iscsi_conn *conn) 3595{ 3596 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu; 3597 struct kvec *iov; 3598 u32 tx_size = 0; 3599 int text_length, iov_count = 0, rc; 3600 3601 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_TCP); 3602 if (rc < 0) 3603 return rc; 3604 3605 text_length = rc; 3606 iov = &cmd->iov_misc[0]; 3607 iov[iov_count].iov_base = cmd->pdu; 3608 iov[iov_count++].iov_len = ISCSI_HDR_LEN; 3609 iov[iov_count].iov_base = cmd->buf_ptr; 3610 iov[iov_count++].iov_len = text_length; 3611 3612 tx_size += (ISCSI_HDR_LEN + text_length); 3613 3614 if (conn->conn_ops->HeaderDigest) { 3615 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; 3616 3617 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, 3618 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); 3619 3620 iov[0].iov_len += ISCSI_CRC_LEN; 3621 tx_size += ISCSI_CRC_LEN; 3622 pr_debug("Attaching CRC32 HeaderDigest for" 3623 " Text Response PDU 0x%08x\n", *header_digest); 3624 } 3625 3626 if (conn->conn_ops->DataDigest) { 3627 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, 3628 cmd->buf_ptr, text_length, 3629 0, NULL, (u8 *)&cmd->data_crc); 3630 3631 iov[iov_count].iov_base = &cmd->data_crc; 3632 iov[iov_count++].iov_len = ISCSI_CRC_LEN; 3633 tx_size += ISCSI_CRC_LEN; 3634 3635 pr_debug("Attaching DataDigest for %u bytes of text" 3636 " data, CRC 0x%08x\n", text_length, 3637 cmd->data_crc); 3638 } 3639 3640 cmd->iov_misc_count = iov_count; 3641 cmd->tx_size = tx_size; 3642 3643 return 0; 3644} 3645 3646void 3647iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn, 3648 struct iscsi_reject *hdr) 3649{ 3650 hdr->opcode = ISCSI_OP_REJECT; 3651 hdr->reason = cmd->reject_reason; 3652 hdr->flags |= ISCSI_FLAG_CMD_FINAL; 3653 hton24(hdr->dlength, ISCSI_HDR_LEN); 3654 hdr->ffffffff = cpu_to_be32(0xffffffff); 3655 cmd->stat_sn = conn->stat_sn++; 3656 hdr->statsn = cpu_to_be32(cmd->stat_sn); 3657 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 3658 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); 3659 3660} 3661EXPORT_SYMBOL(iscsit_build_reject); 3662 3663static int iscsit_send_reject( 3664 struct iscsi_cmd *cmd, 3665 struct iscsi_conn *conn) 3666{ 3667 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0]; 3668 struct kvec *iov; 3669 u32 iov_count = 0, tx_size; 3670 3671 iscsit_build_reject(cmd, conn, hdr); 3672 3673 iov = &cmd->iov_misc[0]; 3674 iov[iov_count].iov_base = cmd->pdu; 3675 iov[iov_count++].iov_len = ISCSI_HDR_LEN; 3676 iov[iov_count].iov_base = cmd->buf_ptr; 3677 iov[iov_count++].iov_len = ISCSI_HDR_LEN; 3678 3679 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN); 3680 3681 if (conn->conn_ops->HeaderDigest) { 3682 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; 3683 3684 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, 3685 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); 3686 3687 iov[0].iov_len += ISCSI_CRC_LEN; 3688 tx_size += ISCSI_CRC_LEN; 3689 pr_debug("Attaching CRC32 HeaderDigest for" 3690 " REJECT PDU 0x%08x\n", *header_digest); 3691 } 3692 3693 if (conn->conn_ops->DataDigest) { 3694 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr, 3695 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc); 3696 3697 iov[iov_count].iov_base = &cmd->data_crc; 3698 iov[iov_count++].iov_len = ISCSI_CRC_LEN; 3699 tx_size += ISCSI_CRC_LEN; 3700 pr_debug("Attaching CRC32 DataDigest for REJECT" 3701 " PDU 0x%08x\n", cmd->data_crc); 3702 } 3703 3704 cmd->iov_misc_count = iov_count; 3705 cmd->tx_size = tx_size; 3706 3707 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x," 3708 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid); 3709 3710 return 0; 3711} 3712 3713void iscsit_thread_get_cpumask(struct iscsi_conn *conn) 3714{ 3715 int ord, cpu; 3716 /* 3717 * bitmap_id is assigned from iscsit_global->ts_bitmap from 3718 * within iscsit_start_kthreads() 3719 * 3720 * Here we use bitmap_id to determine which CPU that this 3721 * iSCSI connection's RX/TX threads will be scheduled to 3722 * execute upon. 3723 */ 3724 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask); 3725 for_each_online_cpu(cpu) { 3726 if (ord-- == 0) { 3727 cpumask_set_cpu(cpu, conn->conn_cpumask); 3728 return; 3729 } 3730 } 3731 /* 3732 * This should never be reached.. 3733 */ 3734 dump_stack(); 3735 cpumask_setall(conn->conn_cpumask); 3736} 3737 3738static inline void iscsit_thread_check_cpumask( 3739 struct iscsi_conn *conn, 3740 struct task_struct *p, 3741 int mode) 3742{ 3743 /* 3744 * mode == 1 signals iscsi_target_tx_thread() usage. 3745 * mode == 0 signals iscsi_target_rx_thread() usage. 3746 */ 3747 if (mode == 1) { 3748 if (!conn->conn_tx_reset_cpumask) 3749 return; 3750 conn->conn_tx_reset_cpumask = 0; 3751 } else { 3752 if (!conn->conn_rx_reset_cpumask) 3753 return; 3754 conn->conn_rx_reset_cpumask = 0; 3755 } 3756 /* 3757 * Update the CPU mask for this single kthread so that 3758 * both TX and RX kthreads are scheduled to run on the 3759 * same CPU. 3760 */ 3761 set_cpus_allowed_ptr(p, conn->conn_cpumask); 3762} 3763 3764static int 3765iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state) 3766{ 3767 int ret; 3768 3769 switch (state) { 3770 case ISTATE_SEND_R2T: 3771 ret = iscsit_send_r2t(cmd, conn); 3772 if (ret < 0) 3773 goto err; 3774 break; 3775 case ISTATE_REMOVE: 3776 spin_lock_bh(&conn->cmd_lock); 3777 list_del_init(&cmd->i_conn_node); 3778 spin_unlock_bh(&conn->cmd_lock); 3779 3780 iscsit_free_cmd(cmd, false); 3781 break; 3782 case ISTATE_SEND_NOPIN_WANT_RESPONSE: 3783 iscsit_mod_nopin_response_timer(conn); 3784 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1); 3785 if (ret < 0) 3786 goto err; 3787 break; 3788 case ISTATE_SEND_NOPIN_NO_RESPONSE: 3789 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0); 3790 if (ret < 0) 3791 goto err; 3792 break; 3793 default: 3794 pr_err("Unknown Opcode: 0x%02x ITT:" 3795 " 0x%08x, i_state: %d on CID: %hu\n", 3796 cmd->iscsi_opcode, cmd->init_task_tag, state, 3797 conn->cid); 3798 goto err; 3799 } 3800 3801 return 0; 3802 3803err: 3804 return -1; 3805} 3806 3807static int 3808iscsit_handle_immediate_queue(struct iscsi_conn *conn) 3809{ 3810 struct iscsit_transport *t = conn->conn_transport; 3811 struct iscsi_queue_req *qr; 3812 struct iscsi_cmd *cmd; 3813 u8 state; 3814 int ret; 3815 3816 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) { 3817 atomic_set(&conn->check_immediate_queue, 0); 3818 cmd = qr->cmd; 3819 state = qr->state; 3820 kmem_cache_free(lio_qr_cache, qr); 3821 3822 ret = t->iscsit_immediate_queue(conn, cmd, state); 3823 if (ret < 0) 3824 return ret; 3825 } 3826 3827 return 0; 3828} 3829 3830static int 3831iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state) 3832{ 3833 int ret; 3834 3835check_rsp_state: 3836 switch (state) { 3837 case ISTATE_SEND_DATAIN: 3838 ret = iscsit_send_datain(cmd, conn); 3839 if (ret < 0) 3840 goto err; 3841 else if (!ret) 3842 /* more drs */ 3843 goto check_rsp_state; 3844 else if (ret == 1) { 3845 /* all done */ 3846 spin_lock_bh(&cmd->istate_lock); 3847 cmd->i_state = ISTATE_SENT_STATUS; 3848 spin_unlock_bh(&cmd->istate_lock); 3849 3850 if (atomic_read(&conn->check_immediate_queue)) 3851 return 1; 3852 3853 return 0; 3854 } else if (ret == 2) { 3855 /* Still must send status, 3856 SCF_TRANSPORT_TASK_SENSE was set */ 3857 spin_lock_bh(&cmd->istate_lock); 3858 cmd->i_state = ISTATE_SEND_STATUS; 3859 spin_unlock_bh(&cmd->istate_lock); 3860 state = ISTATE_SEND_STATUS; 3861 goto check_rsp_state; 3862 } 3863 3864 break; 3865 case ISTATE_SEND_STATUS: 3866 case ISTATE_SEND_STATUS_RECOVERY: 3867 ret = iscsit_send_response(cmd, conn); 3868 break; 3869 case ISTATE_SEND_LOGOUTRSP: 3870 ret = iscsit_send_logout(cmd, conn); 3871 break; 3872 case ISTATE_SEND_ASYNCMSG: 3873 ret = iscsit_send_conn_drop_async_message( 3874 cmd, conn); 3875 break; 3876 case ISTATE_SEND_NOPIN: 3877 ret = iscsit_send_nopin(cmd, conn); 3878 break; 3879 case ISTATE_SEND_REJECT: 3880 ret = iscsit_send_reject(cmd, conn); 3881 break; 3882 case ISTATE_SEND_TASKMGTRSP: 3883 ret = iscsit_send_task_mgt_rsp(cmd, conn); 3884 if (ret != 0) 3885 break; 3886 ret = iscsit_tmr_post_handler(cmd, conn); 3887 if (ret != 0) 3888 iscsit_fall_back_to_erl0(conn->sess); 3889 break; 3890 case ISTATE_SEND_TEXTRSP: 3891 ret = iscsit_send_text_rsp(cmd, conn); 3892 break; 3893 default: 3894 pr_err("Unknown Opcode: 0x%02x ITT:" 3895 " 0x%08x, i_state: %d on CID: %hu\n", 3896 cmd->iscsi_opcode, cmd->init_task_tag, 3897 state, conn->cid); 3898 goto err; 3899 } 3900 if (ret < 0) 3901 goto err; 3902 3903 if (iscsit_send_tx_data(cmd, conn, 1) < 0) { 3904 iscsit_tx_thread_wait_for_tcp(conn); 3905 iscsit_unmap_iovec(cmd); 3906 goto err; 3907 } 3908 iscsit_unmap_iovec(cmd); 3909 3910 switch (state) { 3911 case ISTATE_SEND_LOGOUTRSP: 3912 if (!iscsit_logout_post_handler(cmd, conn)) 3913 return -ECONNRESET; 3914 /* fall through */ 3915 case ISTATE_SEND_STATUS: 3916 case ISTATE_SEND_ASYNCMSG: 3917 case ISTATE_SEND_NOPIN: 3918 case ISTATE_SEND_STATUS_RECOVERY: 3919 case ISTATE_SEND_TEXTRSP: 3920 case ISTATE_SEND_TASKMGTRSP: 3921 case ISTATE_SEND_REJECT: 3922 spin_lock_bh(&cmd->istate_lock); 3923 cmd->i_state = ISTATE_SENT_STATUS; 3924 spin_unlock_bh(&cmd->istate_lock); 3925 break; 3926 default: 3927 pr_err("Unknown Opcode: 0x%02x ITT:" 3928 " 0x%08x, i_state: %d on CID: %hu\n", 3929 cmd->iscsi_opcode, cmd->init_task_tag, 3930 cmd->i_state, conn->cid); 3931 goto err; 3932 } 3933 3934 if (atomic_read(&conn->check_immediate_queue)) 3935 return 1; 3936 3937 return 0; 3938 3939err: 3940 return -1; 3941} 3942 3943static int iscsit_handle_response_queue(struct iscsi_conn *conn) 3944{ 3945 struct iscsit_transport *t = conn->conn_transport; 3946 struct iscsi_queue_req *qr; 3947 struct iscsi_cmd *cmd; 3948 u8 state; 3949 int ret; 3950 3951 while ((qr = iscsit_get_cmd_from_response_queue(conn))) { 3952 cmd = qr->cmd; 3953 state = qr->state; 3954 kmem_cache_free(lio_qr_cache, qr); 3955 3956 ret = t->iscsit_response_queue(conn, cmd, state); 3957 if (ret == 1 || ret < 0) 3958 return ret; 3959 } 3960 3961 return 0; 3962} 3963 3964int iscsi_target_tx_thread(void *arg) 3965{ 3966 int ret = 0; 3967 struct iscsi_conn *conn = arg; 3968 /* 3969 * Allow ourselves to be interrupted by SIGINT so that a 3970 * connection recovery / failure event can be triggered externally. 3971 */ 3972 allow_signal(SIGINT); 3973 3974 while (!kthread_should_stop()) { 3975 /* 3976 * Ensure that both TX and RX per connection kthreads 3977 * are scheduled to run on the same CPU. 3978 */ 3979 iscsit_thread_check_cpumask(conn, current, 1); 3980 3981 wait_event_interruptible(conn->queues_wq, 3982 !iscsit_conn_all_queues_empty(conn)); 3983 3984 if (signal_pending(current)) 3985 goto transport_err; 3986 3987get_immediate: 3988 ret = iscsit_handle_immediate_queue(conn); 3989 if (ret < 0) 3990 goto transport_err; 3991 3992 ret = iscsit_handle_response_queue(conn); 3993 if (ret == 1) 3994 goto get_immediate; 3995 else if (ret == -ECONNRESET) 3996 goto out; 3997 else if (ret < 0) 3998 goto transport_err; 3999 } 4000 4001transport_err: 4002 /* 4003 * Avoid the normal connection failure code-path if this connection 4004 * is still within LOGIN mode, and iscsi_np process context is 4005 * responsible for cleaning up the early connection failure. 4006 */ 4007 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN) 4008 iscsit_take_action_for_connection_exit(conn); 4009out: 4010 return 0; 4011} 4012 4013static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf) 4014{ 4015 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf; 4016 struct iscsi_cmd *cmd; 4017 int ret = 0; 4018 4019 switch (hdr->opcode & ISCSI_OPCODE_MASK) { 4020 case ISCSI_OP_SCSI_CMD: 4021 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 4022 if (!cmd) 4023 goto reject; 4024 4025 ret = iscsit_handle_scsi_cmd(conn, cmd, buf); 4026 break; 4027 case ISCSI_OP_SCSI_DATA_OUT: 4028 ret = iscsit_handle_data_out(conn, buf); 4029 break; 4030 case ISCSI_OP_NOOP_OUT: 4031 cmd = NULL; 4032 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) { 4033 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 4034 if (!cmd) 4035 goto reject; 4036 } 4037 ret = iscsit_handle_nop_out(conn, cmd, buf); 4038 break; 4039 case ISCSI_OP_SCSI_TMFUNC: 4040 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 4041 if (!cmd) 4042 goto reject; 4043 4044 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf); 4045 break; 4046 case ISCSI_OP_TEXT: 4047 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) { 4048 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt); 4049 if (!cmd) 4050 goto reject; 4051 } else { 4052 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 4053 if (!cmd) 4054 goto reject; 4055 } 4056 4057 ret = iscsit_handle_text_cmd(conn, cmd, buf); 4058 break; 4059 case ISCSI_OP_LOGOUT: 4060 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); 4061 if (!cmd) 4062 goto reject; 4063 4064 ret = iscsit_handle_logout_cmd(conn, cmd, buf); 4065 if (ret > 0) 4066 wait_for_completion_timeout(&conn->conn_logout_comp, 4067 SECONDS_FOR_LOGOUT_COMP * HZ); 4068 break; 4069 case ISCSI_OP_SNACK: 4070 ret = iscsit_handle_snack(conn, buf); 4071 break; 4072 default: 4073 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode); 4074 if (!conn->sess->sess_ops->ErrorRecoveryLevel) { 4075 pr_err("Cannot recover from unknown" 4076 " opcode while ERL=0, closing iSCSI connection.\n"); 4077 return -1; 4078 } 4079 if (!conn->conn_ops->OFMarker) { 4080 pr_err("Unable to recover from unknown" 4081 " opcode while OFMarker=No, closing iSCSI" 4082 " connection.\n"); 4083 return -1; 4084 } 4085 if (iscsit_recover_from_unknown_opcode(conn) < 0) { 4086 pr_err("Unable to recover from unknown" 4087 " opcode, closing iSCSI connection.\n"); 4088 return -1; 4089 } 4090 break; 4091 } 4092 4093 return ret; 4094reject: 4095 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); 4096} 4097 4098static bool iscsi_target_check_conn_state(struct iscsi_conn *conn) 4099{ 4100 bool ret; 4101 4102 spin_lock_bh(&conn->state_lock); 4103 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN); 4104 spin_unlock_bh(&conn->state_lock); 4105 4106 return ret; 4107} 4108 4109int iscsi_target_rx_thread(void *arg) 4110{ 4111 int ret, rc; 4112 u8 buffer[ISCSI_HDR_LEN], opcode; 4113 u32 checksum = 0, digest = 0; 4114 struct iscsi_conn *conn = arg; 4115 struct kvec iov; 4116 /* 4117 * Allow ourselves to be interrupted by SIGINT so that a 4118 * connection recovery / failure event can be triggered externally. 4119 */ 4120 allow_signal(SIGINT); 4121 /* 4122 * Wait for iscsi_post_login_handler() to complete before allowing 4123 * incoming iscsi/tcp socket I/O, and/or failing the connection. 4124 */ 4125 rc = wait_for_completion_interruptible(&conn->rx_login_comp); 4126 if (rc < 0 || iscsi_target_check_conn_state(conn)) 4127 return 0; 4128 4129 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) { 4130 struct completion comp; 4131 4132 init_completion(&comp); 4133 rc = wait_for_completion_interruptible(&comp); 4134 if (rc < 0) 4135 goto transport_err; 4136 4137 goto transport_err; 4138 } 4139 4140 while (!kthread_should_stop()) { 4141 /* 4142 * Ensure that both TX and RX per connection kthreads 4143 * are scheduled to run on the same CPU. 4144 */ 4145 iscsit_thread_check_cpumask(conn, current, 0); 4146 4147 memset(buffer, 0, ISCSI_HDR_LEN); 4148 memset(&iov, 0, sizeof(struct kvec)); 4149 4150 iov.iov_base = buffer; 4151 iov.iov_len = ISCSI_HDR_LEN; 4152 4153 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN); 4154 if (ret != ISCSI_HDR_LEN) { 4155 iscsit_rx_thread_wait_for_tcp(conn); 4156 goto transport_err; 4157 } 4158 4159 if (conn->conn_ops->HeaderDigest) { 4160 iov.iov_base = &digest; 4161 iov.iov_len = ISCSI_CRC_LEN; 4162 4163 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN); 4164 if (ret != ISCSI_CRC_LEN) { 4165 iscsit_rx_thread_wait_for_tcp(conn); 4166 goto transport_err; 4167 } 4168 4169 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash, 4170 buffer, ISCSI_HDR_LEN, 4171 0, NULL, (u8 *)&checksum); 4172 4173 if (digest != checksum) { 4174 pr_err("HeaderDigest CRC32C failed," 4175 " received 0x%08x, computed 0x%08x\n", 4176 digest, checksum); 4177 /* 4178 * Set the PDU to 0xff so it will intentionally 4179 * hit default in the switch below. 4180 */ 4181 memset(buffer, 0xff, ISCSI_HDR_LEN); 4182 atomic_long_inc(&conn->sess->conn_digest_errors); 4183 } else { 4184 pr_debug("Got HeaderDigest CRC32C" 4185 " 0x%08x\n", checksum); 4186 } 4187 } 4188 4189 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT) 4190 goto transport_err; 4191 4192 opcode = buffer[0] & ISCSI_OPCODE_MASK; 4193 4194 if (conn->sess->sess_ops->SessionType && 4195 ((!(opcode & ISCSI_OP_TEXT)) || 4196 (!(opcode & ISCSI_OP_LOGOUT)))) { 4197 pr_err("Received illegal iSCSI Opcode: 0x%02x" 4198 " while in Discovery Session, rejecting.\n", opcode); 4199 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, 4200 buffer); 4201 goto transport_err; 4202 } 4203 4204 ret = iscsi_target_rx_opcode(conn, buffer); 4205 if (ret < 0) 4206 goto transport_err; 4207 } 4208 4209transport_err: 4210 if (!signal_pending(current)) 4211 atomic_set(&conn->transport_failed, 1); 4212 iscsit_take_action_for_connection_exit(conn); 4213 return 0; 4214} 4215 4216static void iscsit_release_commands_from_conn(struct iscsi_conn *conn) 4217{ 4218 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL; 4219 struct iscsi_session *sess = conn->sess; 4220 /* 4221 * We expect this function to only ever be called from either RX or TX 4222 * thread context via iscsit_close_connection() once the other context 4223 * has been reset -> returned sleeping pre-handler state. 4224 */ 4225 spin_lock_bh(&conn->cmd_lock); 4226 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) { 4227 4228 list_del_init(&cmd->i_conn_node); 4229 spin_unlock_bh(&conn->cmd_lock); 4230 4231 iscsit_increment_maxcmdsn(cmd, sess); 4232 4233 iscsit_free_cmd(cmd, true); 4234 4235 spin_lock_bh(&conn->cmd_lock); 4236 } 4237 spin_unlock_bh(&conn->cmd_lock); 4238} 4239 4240static void iscsit_stop_timers_for_cmds( 4241 struct iscsi_conn *conn) 4242{ 4243 struct iscsi_cmd *cmd; 4244 4245 spin_lock_bh(&conn->cmd_lock); 4246 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) { 4247 if (cmd->data_direction == DMA_TO_DEVICE) 4248 iscsit_stop_dataout_timer(cmd); 4249 } 4250 spin_unlock_bh(&conn->cmd_lock); 4251} 4252 4253int iscsit_close_connection( 4254 struct iscsi_conn *conn) 4255{ 4256 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT); 4257 struct iscsi_session *sess = conn->sess; 4258 4259 pr_debug("Closing iSCSI connection CID %hu on SID:" 4260 " %u\n", conn->cid, sess->sid); 4261 /* 4262 * Always up conn_logout_comp for the traditional TCP case just in case 4263 * the RX Thread in iscsi_target_rx_opcode() is sleeping and the logout 4264 * response never got sent because the connection failed. 4265 * 4266 * However for iser-target, isert_wait4logout() is using conn_logout_comp 4267 * to signal logout response TX interrupt completion. Go ahead and skip 4268 * this for iser since isert_rx_opcode() does not wait on logout failure, 4269 * and to avoid iscsi_conn pointer dereference in iser-target code. 4270 */ 4271 if (conn->conn_transport->transport_type == ISCSI_TCP) 4272 complete(&conn->conn_logout_comp); 4273 4274 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) { 4275 if (conn->tx_thread && 4276 cmpxchg(&conn->tx_thread_active, true, false)) { 4277 send_sig(SIGINT, conn->tx_thread, 1); 4278 kthread_stop(conn->tx_thread); 4279 } 4280 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) { 4281 if (conn->rx_thread && 4282 cmpxchg(&conn->rx_thread_active, true, false)) { 4283 send_sig(SIGINT, conn->rx_thread, 1); 4284 kthread_stop(conn->rx_thread); 4285 } 4286 } 4287 4288 spin_lock(&iscsit_global->ts_bitmap_lock); 4289 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id, 4290 get_order(1)); 4291 spin_unlock(&iscsit_global->ts_bitmap_lock); 4292 4293 iscsit_stop_timers_for_cmds(conn); 4294 iscsit_stop_nopin_response_timer(conn); 4295 iscsit_stop_nopin_timer(conn); 4296 4297 if (conn->conn_transport->iscsit_wait_conn) 4298 conn->conn_transport->iscsit_wait_conn(conn); 4299 4300 /* 4301 * During Connection recovery drop unacknowledged out of order 4302 * commands for this connection, and prepare the other commands 4303 * for realligence. 4304 * 4305 * During normal operation clear the out of order commands (but 4306 * do not free the struct iscsi_ooo_cmdsn's) and release all 4307 * struct iscsi_cmds. 4308 */ 4309 if (atomic_read(&conn->connection_recovery)) { 4310 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn); 4311 iscsit_prepare_cmds_for_realligance(conn); 4312 } else { 4313 iscsit_clear_ooo_cmdsns_for_conn(conn); 4314 iscsit_release_commands_from_conn(conn); 4315 } 4316 iscsit_free_queue_reqs_for_conn(conn); 4317 4318 /* 4319 * Handle decrementing session or connection usage count if 4320 * a logout response was not able to be sent because the 4321 * connection failed. Fall back to Session Recovery here. 4322 */ 4323 if (atomic_read(&conn->conn_logout_remove)) { 4324 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) { 4325 iscsit_dec_conn_usage_count(conn); 4326 iscsit_dec_session_usage_count(sess); 4327 } 4328 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) 4329 iscsit_dec_conn_usage_count(conn); 4330 4331 atomic_set(&conn->conn_logout_remove, 0); 4332 atomic_set(&sess->session_reinstatement, 0); 4333 atomic_set(&sess->session_fall_back_to_erl0, 1); 4334 } 4335 4336 spin_lock_bh(&sess->conn_lock); 4337 list_del(&conn->conn_list); 4338 4339 /* 4340 * Attempt to let the Initiator know this connection failed by 4341 * sending an Connection Dropped Async Message on another 4342 * active connection. 4343 */ 4344 if (atomic_read(&conn->connection_recovery)) 4345 iscsit_build_conn_drop_async_message(conn); 4346 4347 spin_unlock_bh(&sess->conn_lock); 4348 4349 /* 4350 * If connection reinstatement is being performed on this connection, 4351 * up the connection reinstatement semaphore that is being blocked on 4352 * in iscsit_cause_connection_reinstatement(). 4353 */ 4354 spin_lock_bh(&conn->state_lock); 4355 if (atomic_read(&conn->sleep_on_conn_wait_comp)) { 4356 spin_unlock_bh(&conn->state_lock); 4357 complete(&conn->conn_wait_comp); 4358 wait_for_completion(&conn->conn_post_wait_comp); 4359 spin_lock_bh(&conn->state_lock); 4360 } 4361 4362 /* 4363 * If connection reinstatement is being performed on this connection 4364 * by receiving a REMOVECONNFORRECOVERY logout request, up the 4365 * connection wait rcfr semaphore that is being blocked on 4366 * an iscsit_connection_reinstatement_rcfr(). 4367 */ 4368 if (atomic_read(&conn->connection_wait_rcfr)) { 4369 spin_unlock_bh(&conn->state_lock); 4370 complete(&conn->conn_wait_rcfr_comp); 4371 wait_for_completion(&conn->conn_post_wait_comp); 4372 spin_lock_bh(&conn->state_lock); 4373 } 4374 atomic_set(&conn->connection_reinstatement, 1); 4375 spin_unlock_bh(&conn->state_lock); 4376 4377 /* 4378 * If any other processes are accessing this connection pointer we 4379 * must wait until they have completed. 4380 */ 4381 iscsit_check_conn_usage_count(conn); 4382 4383 if (conn->conn_rx_hash.tfm) 4384 crypto_free_hash(conn->conn_rx_hash.tfm); 4385 if (conn->conn_tx_hash.tfm) 4386 crypto_free_hash(conn->conn_tx_hash.tfm); 4387 4388 free_cpumask_var(conn->conn_cpumask); 4389 4390 kfree(conn->conn_ops); 4391 conn->conn_ops = NULL; 4392 4393 if (conn->sock) 4394 sock_release(conn->sock); 4395 4396 if (conn->conn_transport->iscsit_free_conn) 4397 conn->conn_transport->iscsit_free_conn(conn); 4398 4399 iscsit_put_transport(conn->conn_transport); 4400 4401 pr_debug("Moving to TARG_CONN_STATE_FREE.\n"); 4402 conn->conn_state = TARG_CONN_STATE_FREE; 4403 kfree(conn); 4404 4405 spin_lock_bh(&sess->conn_lock); 4406 atomic_dec(&sess->nconn); 4407 pr_debug("Decremented iSCSI connection count to %hu from node:" 4408 " %s\n", atomic_read(&sess->nconn), 4409 sess->sess_ops->InitiatorName); 4410 /* 4411 * Make sure that if one connection fails in an non ERL=2 iSCSI 4412 * Session that they all fail. 4413 */ 4414 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout && 4415 !atomic_read(&sess->session_logout)) 4416 atomic_set(&sess->session_fall_back_to_erl0, 1); 4417 4418 /* 4419 * If this was not the last connection in the session, and we are 4420 * performing session reinstatement or falling back to ERL=0, call 4421 * iscsit_stop_session() without sleeping to shutdown the other 4422 * active connections. 4423 */ 4424 if (atomic_read(&sess->nconn)) { 4425 if (!atomic_read(&sess->session_reinstatement) && 4426 !atomic_read(&sess->session_fall_back_to_erl0)) { 4427 spin_unlock_bh(&sess->conn_lock); 4428 return 0; 4429 } 4430 if (!atomic_read(&sess->session_stop_active)) { 4431 atomic_set(&sess->session_stop_active, 1); 4432 spin_unlock_bh(&sess->conn_lock); 4433 iscsit_stop_session(sess, 0, 0); 4434 return 0; 4435 } 4436 spin_unlock_bh(&sess->conn_lock); 4437 return 0; 4438 } 4439 4440 /* 4441 * If this was the last connection in the session and one of the 4442 * following is occurring: 4443 * 4444 * Session Reinstatement is not being performed, and are falling back 4445 * to ERL=0 call iscsit_close_session(). 4446 * 4447 * Session Logout was requested. iscsit_close_session() will be called 4448 * elsewhere. 4449 * 4450 * Session Continuation is not being performed, start the Time2Retain 4451 * handler and check if sleep_on_sess_wait_sem is active. 4452 */ 4453 if (!atomic_read(&sess->session_reinstatement) && 4454 atomic_read(&sess->session_fall_back_to_erl0)) { 4455 spin_unlock_bh(&sess->conn_lock); 4456 target_put_session(sess->se_sess); 4457 4458 return 0; 4459 } else if (atomic_read(&sess->session_logout)) { 4460 pr_debug("Moving to TARG_SESS_STATE_FREE.\n"); 4461 sess->session_state = TARG_SESS_STATE_FREE; 4462 spin_unlock_bh(&sess->conn_lock); 4463 4464 if (atomic_read(&sess->sleep_on_sess_wait_comp)) 4465 complete(&sess->session_wait_comp); 4466 4467 return 0; 4468 } else { 4469 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n"); 4470 sess->session_state = TARG_SESS_STATE_FAILED; 4471 4472 if (!atomic_read(&sess->session_continuation)) { 4473 spin_unlock_bh(&sess->conn_lock); 4474 iscsit_start_time2retain_handler(sess); 4475 } else 4476 spin_unlock_bh(&sess->conn_lock); 4477 4478 if (atomic_read(&sess->sleep_on_sess_wait_comp)) 4479 complete(&sess->session_wait_comp); 4480 4481 return 0; 4482 } 4483 spin_unlock_bh(&sess->conn_lock); 4484 4485 return 0; 4486} 4487 4488int iscsit_close_session(struct iscsi_session *sess) 4489{ 4490 struct iscsi_portal_group *tpg = sess->tpg; 4491 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; 4492 4493 if (atomic_read(&sess->nconn)) { 4494 pr_err("%d connection(s) still exist for iSCSI session" 4495 " to %s\n", atomic_read(&sess->nconn), 4496 sess->sess_ops->InitiatorName); 4497 BUG(); 4498 } 4499 4500 spin_lock_bh(&se_tpg->session_lock); 4501 atomic_set(&sess->session_logout, 1); 4502 atomic_set(&sess->session_reinstatement, 1); 4503 iscsit_stop_time2retain_timer(sess); 4504 spin_unlock_bh(&se_tpg->session_lock); 4505 4506 /* 4507 * transport_deregister_session_configfs() will clear the 4508 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context 4509 * can be setting it again with __transport_register_session() in 4510 * iscsi_post_login_handler() again after the iscsit_stop_session() 4511 * completes in iscsi_np context. 4512 */ 4513 transport_deregister_session_configfs(sess->se_sess); 4514 4515 /* 4516 * If any other processes are accessing this session pointer we must 4517 * wait until they have completed. If we are in an interrupt (the 4518 * time2retain handler) and contain and active session usage count we 4519 * restart the timer and exit. 4520 */ 4521 if (!in_interrupt()) { 4522 if (iscsit_check_session_usage_count(sess) == 1) 4523 iscsit_stop_session(sess, 1, 1); 4524 } else { 4525 if (iscsit_check_session_usage_count(sess) == 2) { 4526 atomic_set(&sess->session_logout, 0); 4527 iscsit_start_time2retain_handler(sess); 4528 return 0; 4529 } 4530 } 4531 4532 transport_deregister_session(sess->se_sess); 4533 4534 if (sess->sess_ops->ErrorRecoveryLevel == 2) 4535 iscsit_free_connection_recovery_entires(sess); 4536 4537 iscsit_free_all_ooo_cmdsns(sess); 4538 4539 spin_lock_bh(&se_tpg->session_lock); 4540 pr_debug("Moving to TARG_SESS_STATE_FREE.\n"); 4541 sess->session_state = TARG_SESS_STATE_FREE; 4542 pr_debug("Released iSCSI session from node: %s\n", 4543 sess->sess_ops->InitiatorName); 4544 tpg->nsessions--; 4545 if (tpg->tpg_tiqn) 4546 tpg->tpg_tiqn->tiqn_nsessions--; 4547 4548 pr_debug("Decremented number of active iSCSI Sessions on" 4549 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions); 4550 4551 spin_lock(&sess_idr_lock); 4552 idr_remove(&sess_idr, sess->session_index); 4553 spin_unlock(&sess_idr_lock); 4554 4555 kfree(sess->sess_ops); 4556 sess->sess_ops = NULL; 4557 spin_unlock_bh(&se_tpg->session_lock); 4558 4559 kfree(sess); 4560 return 0; 4561} 4562 4563static void iscsit_logout_post_handler_closesession( 4564 struct iscsi_conn *conn) 4565{ 4566 struct iscsi_session *sess = conn->sess; 4567 int sleep = 1; 4568 /* 4569 * Traditional iscsi/tcp will invoke this logic from TX thread 4570 * context during session logout, so clear tx_thread_active and 4571 * sleep if iscsit_close_connection() has not already occured. 4572 * 4573 * Since iser-target invokes this logic from it's own workqueue, 4574 * always sleep waiting for RX/TX thread shutdown to complete 4575 * within iscsit_close_connection(). 4576 */ 4577 if (conn->conn_transport->transport_type == ISCSI_TCP) 4578 sleep = cmpxchg(&conn->tx_thread_active, true, false); 4579 4580 atomic_set(&conn->conn_logout_remove, 0); 4581 complete(&conn->conn_logout_comp); 4582 4583 iscsit_dec_conn_usage_count(conn); 4584 iscsit_stop_session(sess, sleep, sleep); 4585 iscsit_dec_session_usage_count(sess); 4586 target_put_session(sess->se_sess); 4587} 4588 4589static void iscsit_logout_post_handler_samecid( 4590 struct iscsi_conn *conn) 4591{ 4592 int sleep = 1; 4593 4594 if (conn->conn_transport->transport_type == ISCSI_TCP) 4595 sleep = cmpxchg(&conn->tx_thread_active, true, false); 4596 4597 atomic_set(&conn->conn_logout_remove, 0); 4598 complete(&conn->conn_logout_comp); 4599 4600 iscsit_cause_connection_reinstatement(conn, sleep); 4601 iscsit_dec_conn_usage_count(conn); 4602} 4603 4604static void iscsit_logout_post_handler_diffcid( 4605 struct iscsi_conn *conn, 4606 u16 cid) 4607{ 4608 struct iscsi_conn *l_conn; 4609 struct iscsi_session *sess = conn->sess; 4610 bool conn_found = false; 4611 4612 if (!sess) 4613 return; 4614 4615 spin_lock_bh(&sess->conn_lock); 4616 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) { 4617 if (l_conn->cid == cid) { 4618 iscsit_inc_conn_usage_count(l_conn); 4619 conn_found = true; 4620 break; 4621 } 4622 } 4623 spin_unlock_bh(&sess->conn_lock); 4624 4625 if (!conn_found) 4626 return; 4627 4628 if (l_conn->sock) 4629 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN); 4630 4631 spin_lock_bh(&l_conn->state_lock); 4632 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n"); 4633 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT; 4634 spin_unlock_bh(&l_conn->state_lock); 4635 4636 iscsit_cause_connection_reinstatement(l_conn, 1); 4637 iscsit_dec_conn_usage_count(l_conn); 4638} 4639 4640/* 4641 * Return of 0 causes the TX thread to restart. 4642 */ 4643int iscsit_logout_post_handler( 4644 struct iscsi_cmd *cmd, 4645 struct iscsi_conn *conn) 4646{ 4647 int ret = 0; 4648 4649 switch (cmd->logout_reason) { 4650 case ISCSI_LOGOUT_REASON_CLOSE_SESSION: 4651 switch (cmd->logout_response) { 4652 case ISCSI_LOGOUT_SUCCESS: 4653 case ISCSI_LOGOUT_CLEANUP_FAILED: 4654 default: 4655 iscsit_logout_post_handler_closesession(conn); 4656 break; 4657 } 4658 ret = 0; 4659 break; 4660 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION: 4661 if (conn->cid == cmd->logout_cid) { 4662 switch (cmd->logout_response) { 4663 case ISCSI_LOGOUT_SUCCESS: 4664 case ISCSI_LOGOUT_CLEANUP_FAILED: 4665 default: 4666 iscsit_logout_post_handler_samecid(conn); 4667 break; 4668 } 4669 ret = 0; 4670 } else { 4671 switch (cmd->logout_response) { 4672 case ISCSI_LOGOUT_SUCCESS: 4673 iscsit_logout_post_handler_diffcid(conn, 4674 cmd->logout_cid); 4675 break; 4676 case ISCSI_LOGOUT_CID_NOT_FOUND: 4677 case ISCSI_LOGOUT_CLEANUP_FAILED: 4678 default: 4679 break; 4680 } 4681 ret = 1; 4682 } 4683 break; 4684 case ISCSI_LOGOUT_REASON_RECOVERY: 4685 switch (cmd->logout_response) { 4686 case ISCSI_LOGOUT_SUCCESS: 4687 case ISCSI_LOGOUT_CID_NOT_FOUND: 4688 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED: 4689 case ISCSI_LOGOUT_CLEANUP_FAILED: 4690 default: 4691 break; 4692 } 4693 ret = 1; 4694 break; 4695 default: 4696 break; 4697 4698 } 4699 return ret; 4700} 4701EXPORT_SYMBOL(iscsit_logout_post_handler); 4702 4703void iscsit_fail_session(struct iscsi_session *sess) 4704{ 4705 struct iscsi_conn *conn; 4706 4707 spin_lock_bh(&sess->conn_lock); 4708 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) { 4709 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n"); 4710 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT; 4711 } 4712 spin_unlock_bh(&sess->conn_lock); 4713 4714 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n"); 4715 sess->session_state = TARG_SESS_STATE_FAILED; 4716} 4717 4718int iscsit_free_session(struct iscsi_session *sess) 4719{ 4720 u16 conn_count = atomic_read(&sess->nconn); 4721 struct iscsi_conn *conn, *conn_tmp = NULL; 4722 int is_last; 4723 4724 spin_lock_bh(&sess->conn_lock); 4725 atomic_set(&sess->sleep_on_sess_wait_comp, 1); 4726 4727 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list, 4728 conn_list) { 4729 if (conn_count == 0) 4730 break; 4731 4732 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) { 4733 is_last = 1; 4734 } else { 4735 iscsit_inc_conn_usage_count(conn_tmp); 4736 is_last = 0; 4737 } 4738 iscsit_inc_conn_usage_count(conn); 4739 4740 spin_unlock_bh(&sess->conn_lock); 4741 iscsit_cause_connection_reinstatement(conn, 1); 4742 spin_lock_bh(&sess->conn_lock); 4743 4744 iscsit_dec_conn_usage_count(conn); 4745 if (is_last == 0) 4746 iscsit_dec_conn_usage_count(conn_tmp); 4747 4748 conn_count--; 4749 } 4750 4751 if (atomic_read(&sess->nconn)) { 4752 spin_unlock_bh(&sess->conn_lock); 4753 wait_for_completion(&sess->session_wait_comp); 4754 } else 4755 spin_unlock_bh(&sess->conn_lock); 4756 4757 target_put_session(sess->se_sess); 4758 return 0; 4759} 4760 4761void iscsit_stop_session( 4762 struct iscsi_session *sess, 4763 int session_sleep, 4764 int connection_sleep) 4765{ 4766 u16 conn_count = atomic_read(&sess->nconn); 4767 struct iscsi_conn *conn, *conn_tmp = NULL; 4768 int is_last; 4769 4770 spin_lock_bh(&sess->conn_lock); 4771 if (session_sleep) 4772 atomic_set(&sess->sleep_on_sess_wait_comp, 1); 4773 4774 if (connection_sleep) { 4775 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list, 4776 conn_list) { 4777 if (conn_count == 0) 4778 break; 4779 4780 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) { 4781 is_last = 1; 4782 } else { 4783 iscsit_inc_conn_usage_count(conn_tmp); 4784 is_last = 0; 4785 } 4786 iscsit_inc_conn_usage_count(conn); 4787 4788 spin_unlock_bh(&sess->conn_lock); 4789 iscsit_cause_connection_reinstatement(conn, 1); 4790 spin_lock_bh(&sess->conn_lock); 4791 4792 iscsit_dec_conn_usage_count(conn); 4793 if (is_last == 0) 4794 iscsit_dec_conn_usage_count(conn_tmp); 4795 conn_count--; 4796 } 4797 } else { 4798 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) 4799 iscsit_cause_connection_reinstatement(conn, 0); 4800 } 4801 4802 if (session_sleep && atomic_read(&sess->nconn)) { 4803 spin_unlock_bh(&sess->conn_lock); 4804 wait_for_completion(&sess->session_wait_comp); 4805 } else 4806 spin_unlock_bh(&sess->conn_lock); 4807} 4808 4809int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force) 4810{ 4811 struct iscsi_session *sess; 4812 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; 4813 struct se_session *se_sess, *se_sess_tmp; 4814 LIST_HEAD(free_list); 4815 int session_count = 0; 4816 4817 spin_lock_bh(&se_tpg->session_lock); 4818 if (tpg->nsessions && !force) { 4819 spin_unlock_bh(&se_tpg->session_lock); 4820 return -1; 4821 } 4822 4823 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list, 4824 sess_list) { 4825 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr; 4826 4827 spin_lock(&sess->conn_lock); 4828 if (atomic_read(&sess->session_fall_back_to_erl0) || 4829 atomic_read(&sess->session_logout) || 4830 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) { 4831 spin_unlock(&sess->conn_lock); 4832 continue; 4833 } 4834 atomic_set(&sess->session_reinstatement, 1); 4835 spin_unlock(&sess->conn_lock); 4836 4837 list_move_tail(&se_sess->sess_list, &free_list); 4838 } 4839 spin_unlock_bh(&se_tpg->session_lock); 4840 4841 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) { 4842 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr; 4843 4844 iscsit_free_session(sess); 4845 session_count++; 4846 } 4847 4848 pr_debug("Released %d iSCSI Session(s) from Target Portal" 4849 " Group: %hu\n", session_count, tpg->tpgt); 4850 return 0; 4851} 4852 4853MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure"); 4854MODULE_VERSION("4.1.x"); 4855MODULE_AUTHOR("nab@Linux-iSCSI.org"); 4856MODULE_LICENSE("GPL"); 4857 4858module_init(iscsi_target_init_module); 4859module_exit(iscsi_target_cleanup_module); 4860