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