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