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 on whether 482 * the connection is in FFP. The reason we do this 483 * is that we don't want to process CN_FFP_DISABLED until 484 * CN_FFP_ENABLED has been full handled. 485 */ 486 mutex_enter(&icp->conn_login_mutex); 487 while (icp->conn_login_state != LOGIN_FFP) { 488 cv_wait(&icp->conn_login_cv, &icp->conn_login_mutex); 489 } 490 mutex_exit(&icp->conn_login_mutex); 491 break; 492 case CN_FFP_DISABLED: 493 disable_type = (idm_ffp_disable_t)data; 494 495 mutex_enter(&icp->conn_state_mutex); 496 switch (disable_type) { 497 case FD_SESS_LOGOUT: 498 case FD_CONN_LOGOUT: 499 if (icp->conn_async_logout) { 500 /* 501 * Our logout was in response to an 502 * async logout request so treat this 503 * like a connection failure (we will 504 * try to re-establish the connection) 505 */ 506 iscsi_conn_update_state_locked(icp, 507 ISCSI_CONN_STATE_FAILED); 508 } else { 509 /* 510 * Logout due to to user config change, 511 * we will not try to re-establish 512 * the connection. 513 */ 514 iscsi_conn_update_state_locked(icp, 515 ISCSI_CONN_STATE_IN_LOGOUT); 516 /* 517 * Hold off generating the ISCSI_SESS_EVENT_N3 518 * event until we get the CN_CONNECT_LOST 519 * notification. This matches the pre-IDM 520 * implementation better. 521 */ 522 } 523 break; 524 525 case FD_CONN_FAIL: 526 default: 527 iscsi_conn_update_state_locked(icp, 528 ISCSI_CONN_STATE_FAILED); 529 break; 530 } 531 532 icp->conn_state_ffp = B_FALSE; 533 cv_broadcast(&icp->conn_state_change); 534 mutex_exit(&icp->conn_state_mutex); 535 536 break; 537 case CN_CONNECT_LOST: 538 /* 539 * We only care about CN_CONNECT_LOST if we've logged in. IDM 540 * sends a flag as the data payload to indicate whether we 541 * were trying to login. The CN_LOGIN_FAIL notification 542 * gives us what we need to know for login failures and 543 * otherwise we will need to keep a bunch of state to know 544 * what CN_CONNECT_LOST means to us. 545 */ 546 in_login = (boolean_t)data; 547 if (in_login) { 548 mutex_enter(&icp->conn_state_mutex); 549 550 icp->conn_state_idm_connected = B_FALSE; 551 cv_broadcast(&icp->conn_state_change); 552 mutex_exit(&icp->conn_state_mutex); 553 554 /* Release connect hold from CN_READY_FOR_LOGIN */ 555 idm_conn_rele(ic); 556 break; 557 } 558 559 /* Any remaining commands are never going to finish */ 560 iscsi_conn_flush_active_cmds(icp); 561 562 /* 563 * The connection is no longer active so cleanup any 564 * references to the connection and release any holds so 565 * that IDM can finish cleanup. 566 */ 567 mutex_enter(&icp->conn_state_mutex); 568 if (icp->conn_state != ISCSI_CONN_STATE_FAILED) { 569 570 mutex_enter(&isp->sess_state_mutex); 571 iscsi_sess_state_machine(isp, ISCSI_SESS_EVENT_N3); 572 mutex_exit(&isp->sess_state_mutex); 573 574 iscsi_conn_update_state_locked(icp, 575 ISCSI_CONN_STATE_FREE); 576 } else { 577 578 mutex_enter(&isp->sess_state_mutex); 579 iscsi_sess_state_machine(isp, 580 ISCSI_SESS_EVENT_N5); 581 mutex_exit(&isp->sess_state_mutex); 582 583 /* 584 * If session type is NORMAL, try to reestablish the 585 * connection. 586 */ 587 if (isp->sess_type == ISCSI_SESS_TYPE_NORMAL) { 588 iscsi_conn_retry(isp, icp); 589 } else { 590 591 mutex_enter(&isp->sess_state_mutex); 592 iscsi_sess_state_machine(isp, 593 ISCSI_SESS_EVENT_N6); 594 mutex_exit(&isp->sess_state_mutex); 595 596 iscsi_conn_update_state_locked(icp, 597 ISCSI_CONN_STATE_FREE); 598 } 599 } 600 601 (void) iscsi_thread_stop(icp->conn_tx_thread); 602 603 icp->conn_state_idm_connected = B_FALSE; 604 cv_broadcast(&icp->conn_state_change); 605 mutex_exit(&icp->conn_state_mutex); 606 607 /* Release connect hold from CN_READY_FOR_LOGIN */ 608 idm_conn_rele(ic); 609 break; 610 default: 611 ISCSI_CONN_LOG(CE_WARN, 612 "iscsi_client_notify: unknown notification: " 613 "%x: NOT IMPLEMENTED YET: icp: %p ic: %p ", 614 icn, (void *)icp, (void *)ic); 615 break; 616 } 617 /* free the task notify structure we allocated in iscsi_client_notify */ 618 kmem_free(cn_task, sizeof (*cn_task)); 619 620 /* Release the hold we acquired in iscsi_client_notify */ 621 idm_conn_rele(ic); 622 } 623 624 /* 625 * iscsi_conn_sync_params - used to update connection parameters 626 * 627 * Used to update connection parameters with current configured 628 * parameters in the persistent store. This should be called 629 * before starting to make a new iscsi connection in iscsi_login. 630 */ 631 iscsi_status_t 632 iscsi_conn_sync_params(iscsi_conn_t *icp) 633 { 634 iscsi_sess_t *isp; 635 iscsi_hba_t *ihp; 636 int param_id; 637 persistent_param_t pp; 638 persistent_tunable_param_t ptp; 639 iscsi_config_sess_t *ics; 640 int idx, size; 641 char *name; 642 643 ASSERT(icp != NULL); 644 ASSERT((icp->conn_state == ISCSI_CONN_STATE_IN_LOGIN) || 645 (icp->conn_state == ISCSI_CONN_STATE_FAILED) || 646 (icp->conn_state == ISCSI_CONN_STATE_POLLING)); 647 isp = icp->conn_sess; 648 ASSERT(isp != NULL); 649 ihp = isp->sess_hba; 650 ASSERT(ihp != NULL); 651 652 /* 653 * Check if someone is trying to destroy this 654 * connection. If so fail the sync request, 655 * as a method of fast fail. 656 */ 657 if (icp->conn_state_destroy == B_TRUE) { 658 return (ISCSI_STATUS_SHUTDOWN); 659 } 660 661 bzero(&pp, sizeof (pp)); 662 663 /* First get a copy of the HBA params */ 664 bcopy(&ihp->hba_params, &icp->conn_params, 665 sizeof (iscsi_login_params_t)); 666 bcopy(&ihp->hba_tunable_params, &icp->conn_tunable_params, 667 sizeof (iscsi_tunable_params_t)); 668 669 /* 670 * Now we need to get the session configured 671 * values from the persistent store and apply 672 * them to our connection. 673 */ 674 (void) persistent_param_get((char *)isp->sess_name, &pp); 675 for (param_id = 0; param_id < ISCSI_NUM_LOGIN_PARAM; 676 param_id++) { 677 if (iscsiboot_prop && modrootloaded && 678 !iscsi_chk_bootlun_mpxio(ihp) && isp->sess_boot) { 679 /* 680 * iscsi boot with mpxio disabled 681 * while iscsi booting target's parameter overriden 682 * do no update target's parameters. 683 */ 684 if (pp.p_bitmap) { 685 cmn_err(CE_NOTE, "Adopting " 686 " default login parameters in" 687 " boot session as MPxIO is disabled"); 688 } 689 break; 690 } 691 if (pp.p_bitmap & (1 << param_id)) { 692 693 switch (param_id) { 694 /* 695 * Boolean parameters 696 */ 697 case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER: 698 icp->conn_params.data_pdu_in_order = 699 pp.p_params.data_pdu_in_order; 700 break; 701 case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA: 702 icp->conn_params.immediate_data = 703 pp.p_params.immediate_data; 704 break; 705 case ISCSI_LOGIN_PARAM_INITIAL_R2T: 706 icp->conn_params.initial_r2t = 707 pp.p_params.initial_r2t; 708 break; 709 case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER: 710 icp->conn_params.data_pdu_in_order = 711 pp.p_params.data_pdu_in_order; 712 break; 713 /* 714 * Integer parameters 715 */ 716 case ISCSI_LOGIN_PARAM_HEADER_DIGEST: 717 icp->conn_params.header_digest = 718 pp.p_params.header_digest; 719 break; 720 case ISCSI_LOGIN_PARAM_DATA_DIGEST: 721 icp->conn_params.data_digest = 722 pp.p_params.data_digest; 723 break; 724 case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN: 725 icp->conn_params.default_time_to_retain = 726 pp.p_params.default_time_to_retain; 727 break; 728 case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT: 729 icp->conn_params.default_time_to_wait = 730 pp.p_params.default_time_to_wait; 731 break; 732 case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH: 733 icp->conn_params.max_recv_data_seg_len = 734 pp.p_params.max_recv_data_seg_len; 735 break; 736 case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH: 737 icp->conn_params.first_burst_length = 738 pp.p_params.first_burst_length; 739 break; 740 case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH: 741 icp->conn_params.max_burst_length = 742 pp.p_params.max_burst_length; 743 break; 744 745 /* 746 * Integer parameters which currently are unsettable 747 */ 748 case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS: 749 /* FALLTHRU */ 750 case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T: 751 /* FALLTHRU */ 752 case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL: 753 /* FALLTHRU */ 754 default: 755 break; 756 } 757 } 758 } 759 760 if (persistent_get_tunable_param((char *)isp->sess_name, &ptp) == 761 B_TRUE) { 762 if (ptp.p_bitmap & ISCSI_TUNABLE_PARAM_RX_TIMEOUT_VALUE) { 763 icp->conn_tunable_params.recv_login_rsp_timeout = 764 ptp.p_params.recv_login_rsp_timeout; 765 } 766 if (ptp.p_bitmap & ISCSI_TUNABLE_PARAM_CONN_LOGIN_MAX) { 767 icp->conn_tunable_params.conn_login_max = 768 ptp.p_params.conn_login_max; 769 } 770 if (ptp.p_bitmap & ISCSI_TUNABLE_PARAM_LOGIN_POLLING_DELAY) { 771 icp->conn_tunable_params.polling_login_delay = 772 ptp.p_params.polling_login_delay; 773 } 774 } 775 776 /* Skip binding checks on discovery sessions */ 777 if (isp->sess_type == ISCSI_SESS_TYPE_DISCOVERY) { 778 return (ISCSI_STATUS_SUCCESS); 779 } 780 781 /* 782 * Now we need to get the current optional connection 783 * binding information. 784 */ 785 /* setup initial buffer for configured session information */ 786 size = sizeof (*ics); 787 ics = kmem_zalloc(size, KM_SLEEP); 788 ics->ics_in = 1; 789 790 /* get configured sessions information */ 791 name = (char *)isp->sess_name; 792 if (persistent_get_config_session(name, ics) == B_FALSE) { 793 /* 794 * If we were unable to get target level information 795 * then check the initiator level information. 796 */ 797 name = (char *)isp->sess_hba->hba_name; 798 if (persistent_get_config_session(name, ics) == B_FALSE) { 799 /* 800 * No hba information is found. So assume default 801 * one session unbound behavior. 802 */ 803 ics->ics_out = 1; 804 ics->ics_bound = B_FALSE; 805 } 806 } 807 808 if (iscsiboot_prop && (ics->ics_out > 1) && isp->sess_boot && 809 !iscsi_chk_bootlun_mpxio(ihp)) { 810 /* 811 * iscsi booting session with mpxio disabled, 812 * no need set multiple sessions for booting session 813 */ 814 ics->ics_out = 1; 815 ics->ics_bound = B_FALSE; 816 cmn_err(CE_NOTE, "MPxIO is disabled," 817 " no need to configure multiple boot sessions"); 818 } 819 820 /* 821 * Check to make sure this session is still a configured 822 * session. The user might have decreased the session 823 * count. (NOTE: byte 5 of the sess_isid is the session 824 * count (via MS/T). This counter starts at 0.) 825 */ 826 827 828 idx = isp->sess_isid[5]; 829 830 if (iscsiboot_prop && (idx == ISCSI_MAX_CONFIG_SESSIONS)) { 831 /* 832 * This is temporary session for boot session propose 833 * no need to bound IP for this session 834 */ 835 icp->conn_bound = B_FALSE; 836 kmem_free(ics, sizeof (iscsi_config_sess_t)); 837 return (ISCSI_STATUS_SUCCESS); 838 } 839 840 if (ics->ics_out <= idx) { 841 /* 842 * No longer a configured session. Return a 843 * failure so we don't attempt to relogin. 844 */ 845 return (ISCSI_STATUS_SHUTDOWN); 846 } 847 848 /* 849 * If sessions are unbound set this information on 850 * the connection and return success. 851 */ 852 if (ics->ics_bound == B_FALSE) { 853 icp->conn_bound = B_FALSE; 854 kmem_free(ics, sizeof (iscsi_config_sess_t)); 855 return (ISCSI_STATUS_SUCCESS); 856 } 857 858 /* 859 * Since the sessions are bound we need to find the matching 860 * binding information for the session's isid. If this 861 * session's isid is > 0 then we need to get more configured 862 * session information to find the binding info. 863 */ 864 if (idx > 0) { 865 int ics_out; 866 867 ics_out = ics->ics_out; 868 /* record new size and free last buffer */ 869 size = ISCSI_SESSION_CONFIG_SIZE(ics_out); 870 kmem_free(ics, sizeof (*ics)); 871 872 /* allocate new buffer */ 873 ics = kmem_zalloc(size, KM_SLEEP); 874 ics->ics_in = ics_out; 875 876 /* get configured sessions information */ 877 if (persistent_get_config_session(name, ics) != B_TRUE) { 878 cmn_err(CE_NOTE, "iscsi session(%d) - " 879 "unable to get configured session information\n", 880 isp->sess_oid); 881 kmem_free(ics, size); 882 return (ISCSI_STATUS_SHUTDOWN); 883 } 884 } 885 886 /* Copy correct binding information to the connection */ 887 icp->conn_bound = B_TRUE; 888 if (ics->ics_bindings[idx].i_insize == sizeof (struct in_addr)) { 889 bcopy(&ics->ics_bindings[idx].i_addr.in4, 890 &icp->conn_bound_addr.sin4.sin_addr.s_addr, 891 sizeof (struct in_addr)); 892 icp->conn_bound_addr.sin4.sin_family = AF_INET; 893 } else { 894 bcopy(&ics->ics_bindings[idx].i_addr.in6, 895 &icp->conn_bound_addr.sin6.sin6_addr.s6_addr, 896 sizeof (struct in6_addr)); 897 icp->conn_bound_addr.sin6.sin6_family = AF_INET6; 898 } 899 900 kmem_free(ics, size); 901 902 return (ISCSI_STATUS_SUCCESS); 903 } 904 905 /* 906 * +--------------------------------------------------------------------+ 907 * | Internal Connection Interfaces | 908 * +--------------------------------------------------------------------+ 909 */ 910 911 /* 912 * iscsi_conn_flush_active_cmds - flush all active icmdps 913 * for a connection. 914 */ 915 static void 916 iscsi_conn_flush_active_cmds(iscsi_conn_t *icp) 917 { 918 iscsi_cmd_t *icmdp; 919 iscsi_sess_t *isp; 920 boolean_t lock_held = B_FALSE; 921 922 ASSERT(icp != NULL); 923 isp = icp->conn_sess; 924 ASSERT(isp != NULL); 925 926 if (mutex_owned(&icp->conn_queue_active.mutex)) { 927 lock_held = B_TRUE; 928 } else { 929 mutex_enter(&icp->conn_queue_active.mutex); 930 } 931 932 /* Flush active queue */ 933 icmdp = icp->conn_queue_active.head; 934 while (icmdp != NULL) { 935 936 mutex_enter(&icmdp->cmd_mutex); 937 if (icmdp->cmd_type == ISCSI_CMD_TYPE_SCSI) { 938 icmdp->cmd_un.scsi.pkt_stat |= STAT_ABORTED; 939 } 940 mutex_exit(&icmdp->cmd_mutex); 941 942 iscsi_cmd_state_machine(icmdp, 943 ISCSI_CMD_EVENT_E7, isp); 944 icmdp = icp->conn_queue_active.head; 945 } 946 947 /* Wait for active queue to drain */ 948 while (icp->conn_queue_active.count) { 949 mutex_exit(&icp->conn_queue_active.mutex); 950 delay(drv_usectohz(100000)); 951 mutex_enter(&icp->conn_queue_active.mutex); 952 } 953 954 if (lock_held == B_FALSE) { 955 mutex_exit(&icp->conn_queue_active.mutex); 956 } 957 958 /* Wait for IDM abort queue to drain (if necessary) */ 959 mutex_enter(&icp->conn_queue_idm_aborting.mutex); 960 while (icp->conn_queue_idm_aborting.count) { 961 mutex_exit(&icp->conn_queue_idm_aborting.mutex); 962 delay(drv_usectohz(100000)); 963 mutex_enter(&icp->conn_queue_idm_aborting.mutex); 964 } 965 mutex_exit(&icp->conn_queue_idm_aborting.mutex); 966 } 967 968 /* 969 * iscsi_conn_retry - retry connect/login 970 */ 971 void 972 iscsi_conn_retry(iscsi_sess_t *isp, iscsi_conn_t *icp) 973 { 974 iscsi_task_t *itp; 975 976 ASSERT(isp != NULL); 977 ASSERT(icp != NULL); 978 979 /* set login min/max time values */ 980 iscsi_conn_set_login_min_max(icp, 981 ISCSI_CONN_DEFAULT_LOGIN_MIN, 982 icp->conn_tunable_params.conn_login_max); 983 984 ISCSI_CONN_LOG(CE_NOTE, "DEBUG: iscsi_conn_retry: icp: %p icp: %p ", 985 (void *)icp, 986 (void *)icp->conn_ic); 987 988 /* 989 * Sync base connection information before login. 990 * A login redirection might have shifted the 991 * current information from the base. 992 */ 993 bcopy(&icp->conn_base_addr, &icp->conn_curr_addr, 994 sizeof (icp->conn_curr_addr)); 995 996 /* schedule login task */ 997 itp = kmem_zalloc(sizeof (iscsi_task_t), KM_SLEEP); 998 itp->t_arg = icp; 999 itp->t_blocking = B_FALSE; 1000 if (ddi_taskq_dispatch(isp->sess_taskq, 1001 (void(*)())iscsi_login_start, itp, DDI_SLEEP) != 1002 DDI_SUCCESS) { 1003 kmem_free(itp, sizeof (iscsi_task_t)); 1004 cmn_err(CE_WARN, "iscsi connection(%u) failure - " 1005 "unable to schedule login task", icp->conn_oid); 1006 1007 iscsi_conn_update_state(icp, ISCSI_CONN_STATE_FREE); 1008 mutex_enter(&isp->sess_state_mutex); 1009 iscsi_sess_state_machine(isp, 1010 ISCSI_SESS_EVENT_N6); 1011 mutex_exit(&isp->sess_state_mutex); 1012 } 1013 } 1014 1015 void 1016 iscsi_conn_update_state(iscsi_conn_t *icp, iscsi_conn_state_t 1017 next_state) 1018 { 1019 mutex_enter(&icp->conn_state_mutex); 1020 (void) iscsi_conn_update_state_locked(icp, next_state); 1021 mutex_exit(&icp->conn_state_mutex); 1022 } 1023 1024 void 1025 iscsi_conn_update_state_locked(iscsi_conn_t *icp, 1026 iscsi_conn_state_t next_state) 1027 { 1028 ASSERT(mutex_owned(&icp->conn_state_mutex)); 1029 next_state = (next_state > ISCSI_CONN_STATE_MAX) ? 1030 ISCSI_CONN_STATE_MAX : next_state; 1031 idm_sm_audit_state_change(&icp->conn_state_audit, 1032 SAS_ISCSI_CONN, icp->conn_state, next_state); 1033 switch (next_state) { 1034 case ISCSI_CONN_STATE_FREE: 1035 case ISCSI_CONN_STATE_IN_LOGIN: 1036 case ISCSI_CONN_STATE_LOGGED_IN: 1037 case ISCSI_CONN_STATE_IN_LOGOUT: 1038 case ISCSI_CONN_STATE_FAILED: 1039 case ISCSI_CONN_STATE_POLLING: 1040 ISCSI_CONN_LOG(CE_NOTE, 1041 "iscsi_conn_update_state conn %p %s(%d) -> %s(%d)", 1042 (void *)icp, 1043 iscsi_ics_name[icp->conn_state], icp->conn_state, 1044 iscsi_ics_name[next_state], next_state); 1045 icp->conn_prev_state = icp->conn_state; 1046 icp->conn_state = next_state; 1047 cv_broadcast(&icp->conn_state_change); 1048 break; 1049 default: 1050 cmn_err(CE_WARN, "Update state found illegal state: %x " 1051 "prev_state: %x", next_state, icp->conn_prev_state); 1052 ASSERT(0); 1053 } 1054 } 1055