1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 * 25 * iSCSI connection interfaces 26 */ 27 28 #define ISCSI_ICS_NAMES 29 #include "iscsi.h" 30 #include "persistent.h" 31 #include <sys/bootprops.h> 32 33 extern ib_boot_prop_t *iscsiboot_prop; 34 35 static void iscsi_client_notify_task(void *cn_task_void); 36 37 static void iscsi_conn_flush_active_cmds(iscsi_conn_t *icp); 38 39 #define SHUTDOWN_TIMEOUT 180 /* seconds */ 40 41 extern int modrootloaded; 42 43 boolean_t iscsi_conn_logging = B_FALSE; 44 45 /* 46 * +--------------------------------------------------------------------+ 47 * | External Connection Interfaces | 48 * +--------------------------------------------------------------------+ 49 */ 50 51 /* 52 * iscsi_conn_create - This creates an iscsi connection structure and 53 * associates it with a session structure. The session's sess_conn_list_rwlock 54 * should be held as a writer before calling this function. 55 */ 56 iscsi_status_t 57 iscsi_conn_create(struct sockaddr *addr, iscsi_sess_t *isp, iscsi_conn_t **icpp) 58 { 59 iscsi_conn_t *icp = NULL; 60 char th_name[ISCSI_TH_MAX_NAME_LEN]; 61 62 /* See if this connection already exists */ 63 for (icp = isp->sess_conn_list; icp; icp = icp->conn_next) { 64 65 /* 66 * Compare the ioctl information to see if 67 * its a match for this connection. (This 68 * is done by making sure the IPs are of 69 * the same size and then they are the 70 * same value. 71 */ 72 if (bcmp(&icp->conn_base_addr, addr, 73 SIZEOF_SOCKADDR(addr)) == 0) { 74 /* It's a match, record this connection */ 75 break; 76 } 77 } 78 79 /* If icp is found return it */ 80 if (icp != NULL) { 81 *icpp = icp; 82 return (ISCSI_STATUS_SUCCESS); 83 } 84 85 /* We are creating the connection, allocate, and setup */ 86 icp = (iscsi_conn_t *)kmem_zalloc(sizeof (iscsi_conn_t), KM_SLEEP); 87 88 /* 89 * Setup connection 90 */ 91 icp->conn_sig = ISCSI_SIG_CONN; 92 icp->conn_state = ISCSI_CONN_STATE_FREE; 93 mutex_init(&icp->conn_state_mutex, NULL, MUTEX_DRIVER, NULL); 94 cv_init(&icp->conn_state_change, NULL, CV_DRIVER, NULL); 95 mutex_init(&icp->conn_login_mutex, NULL, MUTEX_DRIVER, NULL); 96 cv_init(&icp->conn_login_cv, NULL, CV_DRIVER, NULL); 97 icp->conn_state_destroy = B_FALSE; 98 idm_sm_audit_init(&icp->conn_state_audit); 99 icp->conn_sess = isp; 100 101 mutex_enter(&iscsi_oid_mutex); 102 icp->conn_oid = iscsi_oid++; 103 mutex_exit(&iscsi_oid_mutex); 104 105 /* 106 * IDM CN taskq 107 */ 108 109 if (snprintf(th_name, sizeof (th_name) - 1, 110 ISCSI_CONN_CN_TASKQ_NAME_FORMAT, 111 icp->conn_sess->sess_hba->hba_oid, icp->conn_sess->sess_oid, 112 icp->conn_oid) >= sizeof (th_name)) { 113 cv_destroy(&icp->conn_state_change); 114 mutex_destroy(&icp->conn_state_mutex); 115 kmem_free(icp, sizeof (iscsi_conn_t)); 116 *icpp = NULL; 117 return (ISCSI_STATUS_INTERNAL_ERROR); 118 } 119 120 icp->conn_cn_taskq = 121 ddi_taskq_create(icp->conn_sess->sess_hba->hba_dip, th_name, 1, 122 TASKQ_DEFAULTPRI, 0); 123 if (icp->conn_cn_taskq == NULL) { 124 cv_destroy(&icp->conn_state_change); 125 mutex_destroy(&icp->conn_state_mutex); 126 kmem_free(icp, sizeof (iscsi_conn_t)); 127 *icpp = NULL; 128 return (ISCSI_STATUS_INTERNAL_ERROR); 129 } 130 131 /* Creation of the transfer thread */ 132 if (snprintf(th_name, sizeof (th_name) - 1, ISCSI_CONN_TXTH_NAME_FORMAT, 133 icp->conn_sess->sess_hba->hba_oid, icp->conn_sess->sess_oid, 134 icp->conn_oid) >= sizeof (th_name)) { 135 cv_destroy(&icp->conn_state_change); 136 mutex_destroy(&icp->conn_state_mutex); 137 kmem_free(icp, sizeof (iscsi_conn_t)); 138 ddi_taskq_destroy(icp->conn_cn_taskq); 139 *icpp = NULL; 140 return (ISCSI_STATUS_INTERNAL_ERROR); 141 } 142 143 icp->conn_tx_thread = iscsi_thread_create(isp->sess_hba->hba_dip, 144 th_name, iscsi_tx_thread, icp); 145 146 /* setup connection queues */ 147 iscsi_init_queue(&icp->conn_queue_active); 148 iscsi_init_queue(&icp->conn_queue_idm_aborting); 149 150 bcopy(addr, &icp->conn_base_addr, sizeof (icp->conn_base_addr)); 151 152 /* Add new connection to the session connection list */ 153 icp->conn_cid = isp->sess_conn_next_cid++; 154 if (isp->sess_conn_list == NULL) { 155 isp->sess_conn_list = isp->sess_conn_list_last_ptr = icp; 156 } else { 157 isp->sess_conn_list_last_ptr->conn_next = icp; 158 isp->sess_conn_list_last_ptr = icp; 159 } 160 161 KSTAT_INC_SESS_CNTR_CONN(isp); 162 (void) iscsi_conn_kstat_init(icp); 163 164 *icpp = icp; 165 166 return (ISCSI_STATUS_SUCCESS); 167 } 168 169 /* 170 * iscsi_conn_online - This attempts to take a connection from 171 * ISCSI_CONN_STATE_FREE to ISCSI_CONN_STATE_LOGGED_IN. 172 */ 173 iscsi_status_t 174 iscsi_conn_online(iscsi_conn_t *icp) 175 { 176 iscsi_task_t *itp; 177 iscsi_status_t rval; 178 179 ASSERT(icp != NULL); 180 ASSERT(mutex_owned(&icp->conn_state_mutex)); 181 ASSERT(icp->conn_state == ISCSI_CONN_STATE_FREE); 182 183 /* 184 * If we are attempting to connect then for the purposes of the 185 * other initiator code we are effectively in ISCSI_CONN_STATE_IN_LOGIN. 186 */ 187 iscsi_conn_update_state_locked(icp, ISCSI_CONN_STATE_IN_LOGIN); 188 mutex_exit(&icp->conn_state_mutex); 189 190 /* 191 * Sync base connection information before login 192 * A login redirection might have shifted the 193 * current information from the base. 194 */ 195 bcopy(&icp->conn_base_addr, &icp->conn_curr_addr, 196 sizeof (icp->conn_curr_addr)); 197 198 itp = kmem_zalloc(sizeof (iscsi_task_t), KM_SLEEP); 199 ASSERT(itp != NULL); 200 201 itp->t_arg = icp; 202 itp->t_blocking = B_TRUE; 203 rval = iscsi_login_start(itp); 204 kmem_free(itp, sizeof (iscsi_task_t)); 205 206 mutex_enter(&icp->conn_state_mutex); 207 208 return (rval); 209 } 210 211 /* 212 * iscsi_conn_offline - This attempts to take a connection from 213 * any state to ISCSI_CONN_STATE_FREE. 214 */ 215 iscsi_status_t 216 iscsi_conn_offline(iscsi_conn_t *icp) 217 { 218 clock_t delay; 219 220 ASSERT(icp != NULL); 221 222 /* 223 * We can only destroy a connection if its either in 224 * a state of FREE or LOGGED. The other states are 225 * transitionary and its unsafe to perform actions 226 * on the connection in those states. Set a flag 227 * on the connection to influence the transitions 228 * to quickly complete. Then wait for a state 229 * transition. 230 * 231 * ISCSI_CONN_STATE_LOGGED_IN is set immediately at the 232 * start of CN_NOTIFY_FFP processing. icp->conn_state_ffp 233 * is set to true at the end of ffp processing, at which 234 * point any session updates are complete. We don't 235 * want to start offlining the connection before we're 236 * done completing the FFP processing since this might 237 * interrupt the discovery process. 238 */ 239 delay = ddi_get_lbolt() + SEC_TO_TICK(SHUTDOWN_TIMEOUT); 240 mutex_enter(&icp->conn_state_mutex); 241 icp->conn_state_destroy = B_TRUE; 242 while ((((icp->conn_state != ISCSI_CONN_STATE_FREE) && 243 (icp->conn_state != ISCSI_CONN_STATE_LOGGED_IN)) || 244 ((icp->conn_state == ISCSI_CONN_STATE_LOGGED_IN) && 245 !icp->conn_state_ffp)) && 246 (ddi_get_lbolt() < delay)) { 247 /* wait for transition */ 248 (void) cv_timedwait(&icp->conn_state_change, 249 &icp->conn_state_mutex, delay); 250 } 251 252 switch (icp->conn_state) { 253 case ISCSI_CONN_STATE_FREE: 254 break; 255 case ISCSI_CONN_STATE_LOGGED_IN: 256 if (icp->conn_state_ffp) 257 (void) iscsi_handle_logout(icp); 258 else { 259 icp->conn_state_destroy = B_FALSE; 260 mutex_exit(&icp->conn_state_mutex); 261 return (ISCSI_STATUS_INTERNAL_ERROR); 262 } 263 break; 264 case ISCSI_CONN_STATE_IN_LOGIN: 265 case ISCSI_CONN_STATE_IN_LOGOUT: 266 case ISCSI_CONN_STATE_FAILED: 267 case ISCSI_CONN_STATE_POLLING: 268 default: 269 icp->conn_state_destroy = B_FALSE; 270 mutex_exit(&icp->conn_state_mutex); 271 return (ISCSI_STATUS_INTERNAL_ERROR); 272 } 273 mutex_exit(&icp->conn_state_mutex); 274 275 return (ISCSI_STATUS_SUCCESS); 276 } 277 278 /* 279 * iscsi_conn_destroy - This destroys an iscsi connection structure 280 * and de-associates it with the session. The connection should 281 * already been in the ISCSI_CONN_STATE_FREE when attempting this 282 * operation. 283 */ 284 iscsi_status_t 285 iscsi_conn_destroy(iscsi_conn_t *icp) 286 { 287 iscsi_sess_t *isp; 288 iscsi_conn_t *t_icp; 289 290 ASSERT(icp != NULL); 291 isp = icp->conn_sess; 292 ASSERT(isp != NULL); 293 294 if (icp->conn_state != ISCSI_CONN_STATE_FREE) { 295 return (ISCSI_STATUS_INTERNAL_ERROR); 296 } 297 298 /* Destroy transfer thread */ 299 iscsi_thread_destroy(icp->conn_tx_thread); 300 ddi_taskq_destroy(icp->conn_cn_taskq); 301 302 /* Terminate connection queues */ 303 iscsi_destroy_queue(&icp->conn_queue_idm_aborting); 304 iscsi_destroy_queue(&icp->conn_queue_active); 305 306 cv_destroy(&icp->conn_login_cv); 307 mutex_destroy(&icp->conn_login_mutex); 308 cv_destroy(&icp->conn_state_change); 309 mutex_destroy(&icp->conn_state_mutex); 310 311 /* 312 * Remove connection from sessions linked list. 313 */ 314 if (isp->sess_conn_list == icp) { 315 /* connection first item in list */ 316 isp->sess_conn_list = icp->conn_next; 317 /* 318 * check if this is also the last item in the list 319 */ 320 if (isp->sess_conn_list_last_ptr == icp) { 321 isp->sess_conn_list_last_ptr = NULL; 322 } 323 } else { 324 /* 325 * search session list for icp pointing 326 * to connection being removed. Then 327 * update that connections next pointer. 328 */ 329 t_icp = isp->sess_conn_list; 330 while (t_icp->conn_next != NULL) { 331 if (t_icp->conn_next == icp) { 332 break; 333 } 334 t_icp = t_icp->conn_next; 335 } 336 if (t_icp->conn_next == icp) { 337 t_icp->conn_next = icp->conn_next; 338 /* 339 * if this is the last connection in the list 340 * update the last_ptr to point to t_icp 341 */ 342 if (isp->sess_conn_list_last_ptr == icp) { 343 isp->sess_conn_list_last_ptr = t_icp; 344 } 345 } else { 346 /* couldn't find session */ 347 ASSERT(FALSE); 348 } 349 } 350 351 /* Free this Connections Data */ 352 iscsi_conn_kstat_term(icp); 353 kmem_free(icp, sizeof (iscsi_conn_t)); 354 355 return (ISCSI_STATUS_SUCCESS); 356 } 357 358 359 /* 360 * iscsi_conn_set_login_min_max - set min/max login window 361 * 362 * Used to set the min and max login window. Input values 363 * are in seconds. 364 */ 365 void 366 iscsi_conn_set_login_min_max(iscsi_conn_t *icp, int min, int max) 367 { 368 ASSERT(icp != NULL); 369 370 icp->conn_login_min = ddi_get_lbolt() + SEC_TO_TICK(min); 371 icp->conn_login_max = ddi_get_lbolt() + SEC_TO_TICK(max); 372 } 373 374 375 /* 376 * Process the idm notifications 377 */ 378 idm_status_t 379 iscsi_client_notify(idm_conn_t *ic, idm_client_notify_t icn, uintptr_t data) 380 { 381 iscsi_cn_task_t *cn; 382 iscsi_conn_t *icp = ic->ic_handle; 383 iscsi_sess_t *isp; 384 385 /* 386 * Don't access icp if the notification is CN_CONNECT_DESTROY 387 * since icp may have already been freed. 388 * 389 * Handle CN_FFP_ENABLED and CN_CONNECT_DESTROY immediately 390 */ 391 switch (icn) { 392 case CN_CONNECT_FAIL: 393 case CN_LOGIN_FAIL: 394 /* 395 * Wakeup any thread waiting for login stuff to happen. 396 */ 397 ASSERT(icp != NULL); 398 iscsi_login_update_state(icp, LOGIN_ERROR); 399 return (IDM_STATUS_SUCCESS); 400 case CN_READY_FOR_LOGIN: 401 idm_conn_hold(ic); /* Released in CN_CONNECT_LOST */ 402 mutex_enter(&icp->conn_state_mutex); 403 icp->conn_state_idm_connected = B_TRUE; 404 cv_broadcast(&icp->conn_state_change); 405 mutex_exit(&icp->conn_state_mutex); 406 407 iscsi_login_update_state(icp, LOGIN_READY); 408 return (IDM_STATUS_SUCCESS); 409 case CN_CONNECT_DESTROY: 410 /* 411 * We released any dependecies we had on this object in 412 * either CN_LOGIN_FAIL or CN_CONNECT_LOST so we just need 413 * to destroy the IDM connection now. 414 */ 415 idm_ini_conn_destroy(ic); 416 return (IDM_STATUS_SUCCESS); 417 } 418 419 ASSERT(icp != NULL); 420 isp = icp->conn_sess; 421 422 /* 423 * Dispatch notifications to the taskq since they often require 424 * long blocking operations. In the case of CN_CONNECT_DESTROY 425 * we actually just want to destroy the connection which we 426 * can't do in the IDM taskq context. 427 */ 428 cn = kmem_alloc(sizeof (*cn), KM_SLEEP); 429 430 cn->ct_ic = ic; 431 cn->ct_icn = icn; 432 cn->ct_data = data; 433 434 idm_conn_hold(ic); 435 436 if (ddi_taskq_dispatch(icp->conn_cn_taskq, 437 iscsi_client_notify_task, cn, DDI_SLEEP) != DDI_SUCCESS) { 438 idm_conn_rele(ic); 439 cmn_err(CE_WARN, "iscsi connection(%u) failure - " 440 "unable to schedule notify task", icp->conn_oid); 441 iscsi_conn_update_state(icp, ISCSI_CONN_STATE_FREE); 442 mutex_enter(&isp->sess_state_mutex); 443 iscsi_sess_state_machine(isp, 444 ISCSI_SESS_EVENT_N6); 445 mutex_exit(&isp->sess_state_mutex); 446 } 447 448 return (IDM_STATUS_SUCCESS); 449 } 450 451 static void 452 iscsi_client_notify_task(void *cn_task_void) 453 { 454 iscsi_cn_task_t *cn_task = cn_task_void; 455 iscsi_conn_t *icp; 456 iscsi_sess_t *isp; 457 idm_conn_t *ic; 458 idm_client_notify_t icn; 459 uintptr_t data; 460 idm_ffp_disable_t disable_type; 461 boolean_t in_login; 462 463 ic = cn_task->ct_ic; 464 icn = cn_task->ct_icn; 465 data = cn_task->ct_data; 466 467 icp = ic->ic_handle; 468 ASSERT(icp != NULL); 469 isp = icp->conn_sess; 470 471 switch (icn) { 472 case CN_FFP_ENABLED: 473 mutex_enter(&icp->conn_state_mutex); 474 icp->conn_async_logout = B_FALSE; 475 icp->conn_state_ffp = B_TRUE; 476 cv_broadcast(&icp->conn_state_change); 477 mutex_exit(&icp->conn_state_mutex); 478 479 /* 480 * This logic assumes that the IDM login-snooping code 481 * and the initiator login code will agree to go when 482 * the connection is in FFP or final error received. 483 * The reason we do this is that we don't want to process 484 * CN_FFP_DISABLED until CN_FFP_ENABLED has been full handled. 485 */ 486 mutex_enter(&icp->conn_login_mutex); 487 while ((icp->conn_login_state != LOGIN_FFP) && 488 (icp->conn_login_state != LOGIN_ERROR)) { 489 cv_wait(&icp->conn_login_cv, &icp->conn_login_mutex); 490 } 491 mutex_exit(&icp->conn_login_mutex); 492 break; 493 case CN_FFP_DISABLED: 494 disable_type = (idm_ffp_disable_t)data; 495 496 mutex_enter(&icp->conn_state_mutex); 497 switch (disable_type) { 498 case FD_SESS_LOGOUT: 499 case FD_CONN_LOGOUT: 500 if (icp->conn_async_logout) { 501 /* 502 * Our logout was in response to an 503 * async logout request so treat this 504 * like a connection failure (we will 505 * try to re-establish the connection) 506 */ 507 iscsi_conn_update_state_locked(icp, 508 ISCSI_CONN_STATE_FAILED); 509 } else { 510 /* 511 * Logout due to to user config change, 512 * we will not try to re-establish 513 * the connection. 514 */ 515 iscsi_conn_update_state_locked(icp, 516 ISCSI_CONN_STATE_IN_LOGOUT); 517 /* 518 * Hold off generating the ISCSI_SESS_EVENT_N3 519 * event until we get the CN_CONNECT_LOST 520 * notification. This matches the pre-IDM 521 * implementation better. 522 */ 523 } 524 break; 525 526 case FD_CONN_FAIL: 527 default: 528 if (icp->conn_state == ISCSI_CONN_STATE_IN_LOGIN) { 529 iscsi_conn_update_state_locked(icp, 530 ISCSI_CONN_STATE_FREE); 531 } else { 532 iscsi_conn_update_state_locked(icp, 533 ISCSI_CONN_STATE_FAILED); 534 } 535 break; 536 } 537 538 icp->conn_state_ffp = B_FALSE; 539 cv_broadcast(&icp->conn_state_change); 540 mutex_exit(&icp->conn_state_mutex); 541 542 break; 543 case CN_CONNECT_LOST: 544 /* 545 * We only care about CN_CONNECT_LOST if we've logged in. IDM 546 * sends a flag as the data payload to indicate whether we 547 * were trying to login. The CN_LOGIN_FAIL notification 548 * gives us what we need to know for login failures and 549 * otherwise we will need to keep a bunch of state to know 550 * what CN_CONNECT_LOST means to us. 551 */ 552 in_login = (boolean_t)data; 553 if (in_login || 554 (icp->conn_prev_state == ISCSI_CONN_STATE_IN_LOGIN)) { 555 mutex_enter(&icp->conn_state_mutex); 556 557 icp->conn_state_idm_connected = B_FALSE; 558 cv_broadcast(&icp->conn_state_change); 559 mutex_exit(&icp->conn_state_mutex); 560 561 /* Release connect hold from CN_READY_FOR_LOGIN */ 562 idm_conn_rele(ic); 563 break; 564 } 565 566 /* Any remaining commands are never going to finish */ 567 iscsi_conn_flush_active_cmds(icp); 568 569 /* 570 * The connection is no longer active so cleanup any 571 * references to the connection and release any holds so 572 * that IDM can finish cleanup. 573 */ 574 mutex_enter(&icp->conn_state_mutex); 575 if (icp->conn_state != ISCSI_CONN_STATE_FAILED) { 576 577 mutex_enter(&isp->sess_state_mutex); 578 iscsi_sess_state_machine(isp, ISCSI_SESS_EVENT_N3); 579 mutex_exit(&isp->sess_state_mutex); 580 581 iscsi_conn_update_state_locked(icp, 582 ISCSI_CONN_STATE_FREE); 583 } else { 584 585 mutex_enter(&isp->sess_state_mutex); 586 iscsi_sess_state_machine(isp, 587 ISCSI_SESS_EVENT_N5); 588 mutex_exit(&isp->sess_state_mutex); 589 590 /* 591 * If session type is NORMAL, try to reestablish the 592 * connection. 593 */ 594 if (isp->sess_type == ISCSI_SESS_TYPE_NORMAL) { 595 iscsi_conn_retry(isp, icp); 596 } else { 597 598 mutex_enter(&isp->sess_state_mutex); 599 iscsi_sess_state_machine(isp, 600 ISCSI_SESS_EVENT_N6); 601 mutex_exit(&isp->sess_state_mutex); 602 603 iscsi_conn_update_state_locked(icp, 604 ISCSI_CONN_STATE_FREE); 605 } 606 } 607 608 (void) iscsi_thread_stop(icp->conn_tx_thread); 609 610 icp->conn_state_idm_connected = B_FALSE; 611 cv_broadcast(&icp->conn_state_change); 612 mutex_exit(&icp->conn_state_mutex); 613 614 /* Release connect hold from CN_READY_FOR_LOGIN */ 615 idm_conn_rele(ic); 616 break; 617 default: 618 ISCSI_CONN_LOG(CE_WARN, 619 "iscsi_client_notify: unknown notification: " 620 "%x: NOT IMPLEMENTED YET: icp: %p ic: %p ", 621 icn, (void *)icp, (void *)ic); 622 break; 623 } 624 /* free the task notify structure we allocated in iscsi_client_notify */ 625 kmem_free(cn_task, sizeof (*cn_task)); 626 627 /* Release the hold we acquired in iscsi_client_notify */ 628 idm_conn_rele(ic); 629 } 630 631 /* 632 * iscsi_conn_sync_params - used to update connection parameters 633 * 634 * Used to update connection parameters with current configured 635 * parameters in the persistent store. This should be called 636 * before starting to make a new iscsi connection in iscsi_login. 637 */ 638 iscsi_status_t 639 iscsi_conn_sync_params(iscsi_conn_t *icp) 640 { 641 iscsi_sess_t *isp; 642 iscsi_hba_t *ihp; 643 int param_id; 644 persistent_param_t pp; 645 persistent_tunable_param_t ptp; 646 iscsi_config_sess_t *ics; 647 int idx, size; 648 char *name; 649 650 ASSERT(icp != NULL); 651 ASSERT((icp->conn_state == ISCSI_CONN_STATE_IN_LOGIN) || 652 (icp->conn_state == ISCSI_CONN_STATE_FAILED) || 653 (icp->conn_state == ISCSI_CONN_STATE_POLLING)); 654 isp = icp->conn_sess; 655 ASSERT(isp != NULL); 656 ihp = isp->sess_hba; 657 ASSERT(ihp != NULL); 658 659 /* 660 * Check if someone is trying to destroy this 661 * connection. If so fail the sync request, 662 * as a method of fast fail. 663 */ 664 if (icp->conn_state_destroy == B_TRUE) { 665 return (ISCSI_STATUS_SHUTDOWN); 666 } 667 668 bzero(&pp, sizeof (pp)); 669 670 /* First get a copy of the HBA params */ 671 bcopy(&ihp->hba_params, &icp->conn_params, 672 sizeof (iscsi_login_params_t)); 673 bcopy(&ihp->hba_tunable_params, &icp->conn_tunable_params, 674 sizeof (iscsi_tunable_params_t)); 675 676 /* 677 * Now we need to get the session configured 678 * values from the persistent store and apply 679 * them to our connection. 680 */ 681 (void) persistent_param_get((char *)isp->sess_name, &pp); 682 for (param_id = 0; param_id < ISCSI_NUM_LOGIN_PARAM; 683 param_id++) { 684 if (iscsiboot_prop && modrootloaded && 685 !iscsi_chk_bootlun_mpxio(ihp) && isp->sess_boot) { 686 /* 687 * iscsi boot with mpxio disabled 688 * while iscsi booting target's parameter overriden 689 * do no update target's parameters. 690 */ 691 if (pp.p_bitmap) { 692 cmn_err(CE_NOTE, "Adopting " 693 " default login parameters in" 694 " boot session as MPxIO is disabled"); 695 } 696 break; 697 } 698 if (pp.p_bitmap & (1 << param_id)) { 699 700 switch (param_id) { 701 /* 702 * Boolean parameters 703 */ 704 case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER: 705 icp->conn_params.data_pdu_in_order = 706 pp.p_params.data_pdu_in_order; 707 break; 708 case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA: 709 icp->conn_params.immediate_data = 710 pp.p_params.immediate_data; 711 break; 712 case ISCSI_LOGIN_PARAM_INITIAL_R2T: 713 icp->conn_params.initial_r2t = 714 pp.p_params.initial_r2t; 715 break; 716 case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER: 717 icp->conn_params.data_pdu_in_order = 718 pp.p_params.data_pdu_in_order; 719 break; 720 /* 721 * Integer parameters 722 */ 723 case ISCSI_LOGIN_PARAM_HEADER_DIGEST: 724 icp->conn_params.header_digest = 725 pp.p_params.header_digest; 726 break; 727 case ISCSI_LOGIN_PARAM_DATA_DIGEST: 728 icp->conn_params.data_digest = 729 pp.p_params.data_digest; 730 break; 731 case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN: 732 icp->conn_params.default_time_to_retain = 733 pp.p_params.default_time_to_retain; 734 break; 735 case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT: 736 icp->conn_params.default_time_to_wait = 737 pp.p_params.default_time_to_wait; 738 break; 739 case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH: 740 icp->conn_params.max_recv_data_seg_len = 741 pp.p_params.max_recv_data_seg_len; 742 break; 743 case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH: 744 icp->conn_params.first_burst_length = 745 pp.p_params.first_burst_length; 746 break; 747 case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH: 748 icp->conn_params.max_burst_length = 749 pp.p_params.max_burst_length; 750 break; 751 752 /* 753 * Integer parameters which currently are unsettable 754 */ 755 case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS: 756 /* FALLTHRU */ 757 case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T: 758 /* FALLTHRU */ 759 case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL: 760 /* FALLTHRU */ 761 default: 762 break; 763 } 764 } 765 } 766 767 if (persistent_get_tunable_param((char *)isp->sess_name, &ptp) == 768 B_TRUE) { 769 if (ptp.p_bitmap & ISCSI_TUNABLE_PARAM_RX_TIMEOUT_VALUE) { 770 icp->conn_tunable_params.recv_login_rsp_timeout = 771 ptp.p_params.recv_login_rsp_timeout; 772 } 773 if (ptp.p_bitmap & ISCSI_TUNABLE_PARAM_CONN_LOGIN_MAX) { 774 icp->conn_tunable_params.conn_login_max = 775 ptp.p_params.conn_login_max; 776 } 777 if (ptp.p_bitmap & ISCSI_TUNABLE_PARAM_LOGIN_POLLING_DELAY) { 778 icp->conn_tunable_params.polling_login_delay = 779 ptp.p_params.polling_login_delay; 780 } 781 } 782 783 /* Skip binding checks on discovery sessions */ 784 if (isp->sess_type == ISCSI_SESS_TYPE_DISCOVERY) { 785 return (ISCSI_STATUS_SUCCESS); 786 } 787 788 /* 789 * Now we need to get the current optional connection 790 * binding information. 791 */ 792 /* setup initial buffer for configured session information */ 793 size = sizeof (*ics); 794 ics = kmem_zalloc(size, KM_SLEEP); 795 ics->ics_in = 1; 796 797 /* get configured sessions information */ 798 name = (char *)isp->sess_name; 799 if (persistent_get_config_session(name, ics) == B_FALSE) { 800 /* 801 * If we were unable to get target level information 802 * then check the initiator level information. 803 */ 804 name = (char *)isp->sess_hba->hba_name; 805 if (persistent_get_config_session(name, ics) == B_FALSE) { 806 /* 807 * No hba information is found. So assume default 808 * one session unbound behavior. 809 */ 810 ics->ics_out = 1; 811 ics->ics_bound = B_FALSE; 812 } 813 } 814 815 if (iscsiboot_prop && (ics->ics_out > 1) && isp->sess_boot && 816 !iscsi_chk_bootlun_mpxio(ihp)) { 817 /* 818 * iscsi booting session with mpxio disabled, 819 * no need set multiple sessions for booting session 820 */ 821 ics->ics_out = 1; 822 ics->ics_bound = B_FALSE; 823 cmn_err(CE_NOTE, "MPxIO is disabled," 824 " no need to configure multiple boot sessions"); 825 } 826 827 /* 828 * Check to make sure this session is still a configured 829 * session. The user might have decreased the session 830 * count. (NOTE: byte 5 of the sess_isid is the session 831 * count (via MS/T). This counter starts at 0.) 832 */ 833 834 835 idx = isp->sess_isid[5]; 836 837 if (iscsiboot_prop && (idx == ISCSI_MAX_CONFIG_SESSIONS)) { 838 /* 839 * This is temporary session for boot session propose 840 * no need to bound IP for this session 841 */ 842 icp->conn_bound = B_FALSE; 843 kmem_free(ics, sizeof (iscsi_config_sess_t)); 844 return (ISCSI_STATUS_SUCCESS); 845 } 846 847 if (ics->ics_out <= idx) { 848 /* 849 * No longer a configured session. Return a 850 * failure so we don't attempt to relogin. 851 */ 852 return (ISCSI_STATUS_SHUTDOWN); 853 } 854 855 /* 856 * If sessions are unbound set this information on 857 * the connection and return success. 858 */ 859 if (ics->ics_bound == B_FALSE) { 860 icp->conn_bound = B_FALSE; 861 kmem_free(ics, sizeof (iscsi_config_sess_t)); 862 return (ISCSI_STATUS_SUCCESS); 863 } 864 865 /* 866 * Since the sessions are bound we need to find the matching 867 * binding information for the session's isid. If this 868 * session's isid is > 0 then we need to get more configured 869 * session information to find the binding info. 870 */ 871 if (idx > 0) { 872 int ics_out; 873 874 ics_out = ics->ics_out; 875 /* record new size and free last buffer */ 876 size = ISCSI_SESSION_CONFIG_SIZE(ics_out); 877 kmem_free(ics, sizeof (*ics)); 878 879 /* allocate new buffer */ 880 ics = kmem_zalloc(size, KM_SLEEP); 881 ics->ics_in = ics_out; 882 883 /* get configured sessions information */ 884 if (persistent_get_config_session(name, ics) != B_TRUE) { 885 cmn_err(CE_NOTE, "iscsi session(%d) - " 886 "unable to get configured session information\n", 887 isp->sess_oid); 888 kmem_free(ics, size); 889 return (ISCSI_STATUS_SHUTDOWN); 890 } 891 } 892 893 /* Copy correct binding information to the connection */ 894 icp->conn_bound = B_TRUE; 895 if (ics->ics_bindings[idx].i_insize == sizeof (struct in_addr)) { 896 bcopy(&ics->ics_bindings[idx].i_addr.in4, 897 &icp->conn_bound_addr.sin4.sin_addr.s_addr, 898 sizeof (struct in_addr)); 899 icp->conn_bound_addr.sin4.sin_family = AF_INET; 900 } else { 901 bcopy(&ics->ics_bindings[idx].i_addr.in6, 902 &icp->conn_bound_addr.sin6.sin6_addr.s6_addr, 903 sizeof (struct in6_addr)); 904 icp->conn_bound_addr.sin6.sin6_family = AF_INET6; 905 } 906 907 kmem_free(ics, size); 908 909 return (ISCSI_STATUS_SUCCESS); 910 } 911 912 /* 913 * +--------------------------------------------------------------------+ 914 * | Internal Connection Interfaces | 915 * +--------------------------------------------------------------------+ 916 */ 917 918 /* 919 * iscsi_conn_flush_active_cmds - flush all active icmdps 920 * for a connection. 921 */ 922 static void 923 iscsi_conn_flush_active_cmds(iscsi_conn_t *icp) 924 { 925 iscsi_cmd_t *icmdp; 926 iscsi_sess_t *isp; 927 boolean_t lock_held = B_FALSE; 928 929 ASSERT(icp != NULL); 930 isp = icp->conn_sess; 931 ASSERT(isp != NULL); 932 933 if (mutex_owned(&icp->conn_queue_active.mutex)) { 934 lock_held = B_TRUE; 935 } else { 936 mutex_enter(&icp->conn_queue_active.mutex); 937 } 938 939 /* Flush active queue */ 940 icmdp = icp->conn_queue_active.head; 941 while (icmdp != NULL) { 942 943 mutex_enter(&icmdp->cmd_mutex); 944 if (icmdp->cmd_type == ISCSI_CMD_TYPE_SCSI) { 945 icmdp->cmd_un.scsi.pkt_stat |= STAT_ABORTED; 946 } 947 mutex_exit(&icmdp->cmd_mutex); 948 949 iscsi_cmd_state_machine(icmdp, 950 ISCSI_CMD_EVENT_E7, isp); 951 icmdp = icp->conn_queue_active.head; 952 } 953 954 /* Wait for active queue to drain */ 955 while (icp->conn_queue_active.count) { 956 mutex_exit(&icp->conn_queue_active.mutex); 957 delay(drv_usectohz(100000)); 958 mutex_enter(&icp->conn_queue_active.mutex); 959 } 960 961 if (lock_held == B_FALSE) { 962 mutex_exit(&icp->conn_queue_active.mutex); 963 } 964 965 /* Wait for IDM abort queue to drain (if necessary) */ 966 mutex_enter(&icp->conn_queue_idm_aborting.mutex); 967 while (icp->conn_queue_idm_aborting.count) { 968 mutex_exit(&icp->conn_queue_idm_aborting.mutex); 969 delay(drv_usectohz(100000)); 970 mutex_enter(&icp->conn_queue_idm_aborting.mutex); 971 } 972 mutex_exit(&icp->conn_queue_idm_aborting.mutex); 973 } 974 975 /* 976 * iscsi_conn_retry - retry connect/login 977 */ 978 void 979 iscsi_conn_retry(iscsi_sess_t *isp, iscsi_conn_t *icp) 980 { 981 iscsi_task_t *itp; 982 983 ASSERT(isp != NULL); 984 ASSERT(icp != NULL); 985 986 /* set login min/max time values */ 987 iscsi_conn_set_login_min_max(icp, 988 ISCSI_CONN_DEFAULT_LOGIN_MIN, 989 icp->conn_tunable_params.conn_login_max); 990 991 ISCSI_CONN_LOG(CE_NOTE, "DEBUG: iscsi_conn_retry: icp: %p icp: %p ", 992 (void *)icp, 993 (void *)icp->conn_ic); 994 995 /* 996 * Sync base connection information before login. 997 * A login redirection might have shifted the 998 * current information from the base. 999 */ 1000 bcopy(&icp->conn_base_addr, &icp->conn_curr_addr, 1001 sizeof (icp->conn_curr_addr)); 1002 1003 /* schedule login task */ 1004 itp = kmem_zalloc(sizeof (iscsi_task_t), KM_SLEEP); 1005 itp->t_arg = icp; 1006 itp->t_blocking = B_FALSE; 1007 if (ddi_taskq_dispatch(isp->sess_taskq, 1008 (void(*)())iscsi_login_start, itp, DDI_SLEEP) != 1009 DDI_SUCCESS) { 1010 kmem_free(itp, sizeof (iscsi_task_t)); 1011 cmn_err(CE_WARN, "iscsi connection(%u) failure - " 1012 "unable to schedule login task", icp->conn_oid); 1013 1014 iscsi_conn_update_state(icp, ISCSI_CONN_STATE_FREE); 1015 mutex_enter(&isp->sess_state_mutex); 1016 iscsi_sess_state_machine(isp, 1017 ISCSI_SESS_EVENT_N6); 1018 mutex_exit(&isp->sess_state_mutex); 1019 } 1020 } 1021 1022 void 1023 iscsi_conn_update_state(iscsi_conn_t *icp, iscsi_conn_state_t 1024 next_state) 1025 { 1026 mutex_enter(&icp->conn_state_mutex); 1027 (void) iscsi_conn_update_state_locked(icp, next_state); 1028 mutex_exit(&icp->conn_state_mutex); 1029 } 1030 1031 void 1032 iscsi_conn_update_state_locked(iscsi_conn_t *icp, 1033 iscsi_conn_state_t next_state) 1034 { 1035 ASSERT(mutex_owned(&icp->conn_state_mutex)); 1036 next_state = (next_state > ISCSI_CONN_STATE_MAX) ? 1037 ISCSI_CONN_STATE_MAX : next_state; 1038 idm_sm_audit_state_change(&icp->conn_state_audit, 1039 SAS_ISCSI_CONN, icp->conn_state, next_state); 1040 switch (next_state) { 1041 case ISCSI_CONN_STATE_FREE: 1042 case ISCSI_CONN_STATE_IN_LOGIN: 1043 case ISCSI_CONN_STATE_LOGGED_IN: 1044 case ISCSI_CONN_STATE_IN_LOGOUT: 1045 case ISCSI_CONN_STATE_FAILED: 1046 case ISCSI_CONN_STATE_POLLING: 1047 ISCSI_CONN_LOG(CE_NOTE, 1048 "iscsi_conn_update_state conn %p %s(%d) -> %s(%d)", 1049 (void *)icp, 1050 iscsi_ics_name[icp->conn_state], icp->conn_state, 1051 iscsi_ics_name[next_state], next_state); 1052 icp->conn_prev_state = icp->conn_state; 1053 icp->conn_state = next_state; 1054 cv_broadcast(&icp->conn_state_change); 1055 break; 1056 default: 1057 cmn_err(CE_WARN, "Update state found illegal state: %x " 1058 "prev_state: %x", next_state, icp->conn_prev_state); 1059 ASSERT(0); 1060 } 1061 } 1062