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