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