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 26 #include <sys/cpuvar.h> 27 #include <sys/types.h> 28 #include <sys/conf.h> 29 #include <sys/file.h> 30 #include <sys/ddi.h> 31 #include <sys/sunddi.h> 32 #include <sys/modctl.h> 33 #include <sys/sysmacros.h> 34 #include <sys/sdt.h> 35 36 #include <sys/socket.h> 37 #include <sys/strsubr.h> 38 39 #include <sys/stmf.h> 40 #include <sys/stmf_ioctl.h> 41 #include <sys/portif.h> 42 #include <sys/idm/idm.h> 43 44 #define ISCSIT_TGT_SM_STRINGS 45 #include <iscsit.h> 46 #include <iscsit_isns.h> 47 48 typedef struct { 49 list_node_t te_ctx_node; 50 iscsit_tgt_event_t te_ctx_event; 51 } tgt_event_ctx_t; 52 53 static void 54 tgt_sm_event_dispatch(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 55 56 static void 57 tgt_sm_created(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 58 59 static void 60 tgt_sm_onlining(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 61 62 static void 63 tgt_sm_online(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 64 65 static void 66 tgt_sm_stmf_online(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 67 68 static void 69 tgt_sm_deleting_need_offline(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 70 71 static void 72 tgt_sm_offlining(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 73 74 static void 75 tgt_sm_offline(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 76 77 static void 78 tgt_sm_stmf_offline(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 79 80 static void 81 tgt_sm_deleting_stmf_dereg(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 82 83 static void 84 tgt_sm_deleting_stmf_dereg_fail(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 85 86 static void 87 tgt_sm_deleting(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 88 89 static void 90 iscsit_tgt_dereg_retry(void *arg); 91 92 static void 93 iscsit_tgt_dereg_task(void *arg); 94 95 static void 96 tgt_sm_new_state(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx, 97 iscsit_tgt_state_t new_state); 98 99 100 static iscsit_tgt_t * 101 iscsit_tgt_create(it_tgt_t *cfg_tgt); 102 103 static void 104 iscsit_tgt_unref(void *tgt); 105 106 static void 107 iscsit_tgt_async_wait_ref(iscsit_tgt_t *tgt, idm_refcnt_cb_t *cb_func); 108 109 static void 110 iscsit_tgt_destroy(iscsit_tgt_t *tgt); 111 112 static iscsit_tpgt_t * 113 iscsit_tgt_lookup_tpgt_locked(iscsit_tgt_t *tgt, uint16_t tag); 114 115 static iscsit_tpg_t * 116 iscsit_tpg_lookup_locked(char *tpg_name); 117 118 static iscsit_portal_t * 119 iscsit_tpg_portal_lookup_locked(iscsit_tpg_t *tpg, 120 struct sockaddr_storage *sa); 121 122 static idm_status_t 123 iscsit_tgt_online(iscsit_tgt_t *tgt); 124 125 static void 126 iscsit_tgt_offline(iscsit_tgt_t *tgt); 127 128 static idm_status_t 129 iscsit_tgt_modify(iscsit_tgt_t *tgt, it_tgt_t *cfg_tgt); 130 131 static idm_status_t 132 iscsit_tgt_merge_tpgt(iscsit_tgt_t *tgt, it_tgt_t *cfg_tgt, 133 list_t *tpgt_del_list); 134 135 static iscsit_tpgt_t * 136 iscsit_tpgt_create(it_tpgt_t *cfg_tpgt); 137 138 static iscsit_tpgt_t * 139 iscsit_tpgt_create_default(); 140 141 static void 142 iscsit_tpgt_destroy(iscsit_tpgt_t *tpgt); 143 144 static iscsit_tpg_t * 145 iscsit_tpg_create(it_tpg_t *tpg); 146 147 static void 148 iscsit_tpg_modify(iscsit_tpg_t *tpg, it_tpg_t *cfg_tpg); 149 150 static void 151 iscsit_tpg_destroy(iscsit_tpg_t *tpg); 152 153 static iscsit_portal_t * 154 iscsit_portal_create(iscsit_tpg_t *tpg, struct sockaddr_storage *sa); 155 156 static void 157 iscsit_portal_destroy(iscsit_portal_t *portal); 158 159 static idm_status_t 160 iscsit_portal_online(iscsit_portal_t *portal); 161 162 static void 163 iscsit_portal_offline(iscsit_portal_t *portal); 164 165 166 167 /* 168 * Target state machine 169 */ 170 171 void 172 iscsit_tgt_sm_event(iscsit_tgt_t *tgt, iscsit_tgt_event_t event) 173 { 174 mutex_enter(&tgt->target_mutex); 175 tgt_sm_event_locked(tgt, event); 176 mutex_exit(&tgt->target_mutex); 177 } 178 179 void 180 tgt_sm_event_locked(iscsit_tgt_t *tgt, iscsit_tgt_event_t event) 181 { 182 tgt_event_ctx_t *ctx; 183 184 iscsit_tgt_hold(tgt); 185 186 ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP); 187 188 ctx->te_ctx_event = event; 189 190 list_insert_tail(&tgt->target_events, ctx); 191 /* 192 * Use the target_sm_busy flag to keep the state machine single 193 * threaded. This also serves as recursion avoidance since this 194 * flag will always be set if we call iscsit_tgt_sm_event from 195 * within the state machine code. 196 */ 197 if (!tgt->target_sm_busy) { 198 tgt->target_sm_busy = B_TRUE; 199 while (!list_is_empty(&tgt->target_events)) { 200 ctx = list_head(&tgt->target_events); 201 list_remove(&tgt->target_events, ctx); 202 idm_sm_audit_event(&tgt->target_state_audit, 203 SAS_ISCSIT_TGT, (int)tgt->target_state, 204 (int)ctx->te_ctx_event, 0); 205 mutex_exit(&tgt->target_mutex); 206 tgt_sm_event_dispatch(tgt, ctx); 207 mutex_enter(&tgt->target_mutex); 208 } 209 tgt->target_sm_busy = B_FALSE; 210 211 } 212 213 iscsit_tgt_rele(tgt); 214 } 215 216 static void 217 tgt_sm_event_dispatch(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 218 { 219 DTRACE_PROBE2(tgt__event, iscsit_tgt_t *, tgt, 220 tgt_event_ctx_t *, ctx); 221 222 IDM_SM_LOG(CE_NOTE, "tgt_sm_event_dispatch: tgt %p event %s(%d)", 223 (void *)tgt, iscsit_te_name[ctx->te_ctx_event], ctx->te_ctx_event); 224 225 /* State independent actions */ 226 switch (ctx->te_ctx_event) { 227 case TE_DELETE: 228 tgt->target_deleting = B_TRUE; 229 break; 230 } 231 232 /* State dependent actions */ 233 switch (tgt->target_state) { 234 case TS_CREATED: 235 tgt_sm_created(tgt, ctx); 236 break; 237 case TS_ONLINING: 238 tgt_sm_onlining(tgt, ctx); 239 break; 240 case TS_ONLINE: 241 tgt_sm_online(tgt, ctx); 242 break; 243 case TS_STMF_ONLINE: 244 tgt_sm_stmf_online(tgt, ctx); 245 break; 246 case TS_DELETING_NEED_OFFLINE: 247 tgt_sm_deleting_need_offline(tgt, ctx); 248 break; 249 case TS_OFFLINING: 250 tgt_sm_offlining(tgt, ctx); 251 break; 252 case TS_OFFLINE: 253 tgt_sm_offline(tgt, ctx); 254 break; 255 case TS_STMF_OFFLINE: 256 tgt_sm_stmf_offline(tgt, ctx); 257 break; 258 case TS_DELETING_STMF_DEREG: 259 tgt_sm_deleting_stmf_dereg(tgt, ctx); 260 break; 261 case TS_DELETING_STMF_DEREG_FAIL: 262 tgt_sm_deleting_stmf_dereg_fail(tgt, ctx); 263 break; 264 case TS_DELETING: 265 tgt_sm_deleting(tgt, ctx); 266 break; 267 default: 268 ASSERT(0); 269 } 270 271 kmem_free(ctx, sizeof (*ctx)); 272 } 273 274 static void 275 tgt_sm_created(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 276 { 277 stmf_change_status_t scs; 278 279 switch (ctx->te_ctx_event) { 280 case TE_STMF_ONLINE_REQ: 281 tgt_sm_new_state(tgt, ctx, TS_ONLINING); 282 break; 283 case TE_DELETE: 284 tgt_sm_new_state(tgt, ctx, TS_DELETING_STMF_DEREG); 285 break; 286 case TE_STMF_OFFLINE_REQ: 287 /* 288 * We're already offline but update to an equivelant 289 * state just to note that STMF talked to us. 290 */ 291 scs.st_completion_status = STMF_SUCCESS; 292 scs.st_additional_info = NULL; 293 tgt_sm_new_state(tgt, ctx, TS_OFFLINE); 294 (void) stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE, 295 tgt->target_stmf_lport, &scs); 296 break; 297 case TE_STMF_ONLINE_COMPLETE_ACK: 298 case TE_STMF_OFFLINE_COMPLETE_ACK: 299 /* Ignore */ 300 break; 301 default: 302 ASSERT(0); 303 } 304 } 305 306 static void 307 tgt_sm_onlining(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 308 { 309 stmf_change_status_t scs; 310 311 switch (ctx->te_ctx_event) { 312 case TE_ONLINE_SUCCESS: 313 tgt_sm_new_state(tgt, ctx, TS_ONLINE); 314 break; 315 case TE_ONLINE_FAIL: 316 tgt_sm_new_state(tgt, ctx, TS_STMF_OFFLINE); 317 break; 318 case TE_DELETE: 319 /* TE_DELETE is handled in tgt_sm_event_dispatch() */ 320 break; 321 case TE_STMF_ONLINE_REQ: 322 case TE_STMF_OFFLINE_REQ: 323 /* 324 * We can't complete STMF's request since we are busy going 325 * online. 326 */ 327 scs.st_completion_status = STMF_INVALID_ARG; 328 scs.st_additional_info = NULL; 329 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 330 STMF_CMD_LPORT_ONLINE_COMPLETE : 331 STMF_CMD_LPORT_OFFLINE_COMPLETE, 332 tgt->target_stmf_lport, &scs); 333 break; 334 case TE_STMF_ONLINE_COMPLETE_ACK: 335 case TE_STMF_OFFLINE_COMPLETE_ACK: 336 /* Ignore */ 337 break; 338 default: 339 ASSERT(0); 340 } 341 } 342 343 static void 344 tgt_sm_online(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 345 { 346 stmf_change_status_t scs; 347 348 switch (ctx->te_ctx_event) { 349 case TE_STMF_ONLINE_COMPLETE_ACK: 350 if (tgt->target_deleting) { 351 tgt_sm_new_state(tgt, ctx, TS_DELETING_NEED_OFFLINE); 352 } else { 353 tgt_sm_new_state(tgt, ctx, TS_STMF_ONLINE); 354 } 355 break; 356 case TE_DELETE: 357 /* TE_DELETE is handled in tgt_sm_event_dispatch() */ 358 break; 359 case TE_STMF_ONLINE_REQ: 360 case TE_STMF_OFFLINE_REQ: 361 /* 362 * We can't complete STMF's request since we are busy going 363 * online (waiting for acknowlegement from STMF) 364 */ 365 scs.st_completion_status = STMF_INVALID_ARG; 366 scs.st_additional_info = NULL; 367 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 368 STMF_CMD_LPORT_ONLINE_COMPLETE : 369 STMF_CMD_LPORT_OFFLINE_COMPLETE, 370 tgt->target_stmf_lport, &scs); 371 break; 372 case TE_STMF_OFFLINE_COMPLETE_ACK: 373 /* Ignore */ 374 break; 375 default: 376 ASSERT(0); 377 } 378 } 379 380 381 static void 382 tgt_sm_stmf_online(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 383 { 384 stmf_change_status_t scs; 385 386 /* Deregister target with iSNS whenever we leave this state */ 387 388 switch (ctx->te_ctx_event) { 389 case TE_DELETE: 390 (void) iscsit_isns_deregister(tgt); 391 tgt_sm_new_state(tgt, ctx, TS_DELETING_NEED_OFFLINE); 392 break; 393 case TE_STMF_OFFLINE_REQ: 394 (void) iscsit_isns_deregister(tgt); 395 tgt_sm_new_state(tgt, ctx, TS_OFFLINING); 396 break; 397 case TE_STMF_ONLINE_REQ: 398 /* Already online */ 399 scs.st_completion_status = STMF_ALREADY; 400 scs.st_additional_info = NULL; 401 (void) stmf_ctl(STMF_CMD_LPORT_ONLINE_COMPLETE, 402 tgt->target_stmf_lport, &scs); 403 break; 404 case TE_STMF_ONLINE_COMPLETE_ACK: 405 case TE_STMF_OFFLINE_COMPLETE_ACK: 406 /* Ignore */ 407 break; 408 default: 409 ASSERT(0); 410 } 411 } 412 413 414 static void 415 tgt_sm_deleting_need_offline(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 416 { 417 stmf_change_status_t scs; 418 419 switch (ctx->te_ctx_event) { 420 case TE_STMF_OFFLINE_REQ: 421 tgt_sm_new_state(tgt, ctx, TS_OFFLINING); 422 break; 423 case TE_DELETE: 424 /* TE_DELETE is handled in tgt_sm_event_dispatch() */ 425 break; 426 case TE_STMF_ONLINE_REQ: 427 /* 428 * We can't complete STMF's request since we need to be offlined 429 */ 430 scs.st_completion_status = STMF_INVALID_ARG; 431 scs.st_additional_info = NULL; 432 (void) stmf_ctl(STMF_CMD_LPORT_ONLINE_COMPLETE, 433 tgt->target_stmf_lport, &scs); 434 break; 435 case TE_STMF_ONLINE_COMPLETE_ACK: 436 case TE_STMF_OFFLINE_COMPLETE_ACK: 437 /* Ignore */ 438 break; 439 default: 440 ASSERT(0); 441 } 442 } 443 444 445 static void 446 tgt_sm_offlining(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 447 { 448 stmf_change_status_t scs; 449 450 switch (ctx->te_ctx_event) { 451 case TE_OFFLINE_COMPLETE: 452 tgt_sm_new_state(tgt, ctx, TS_OFFLINE); 453 break; 454 case TE_DELETE: 455 /* TE_DELETE is handled in tgt_sm_event_dispatch() */ 456 break; 457 case TE_STMF_ONLINE_REQ: 458 case TE_STMF_OFFLINE_REQ: 459 /* 460 * We can't complete STMF's request since we are busy going 461 * offline. 462 */ 463 scs.st_completion_status = STMF_INVALID_ARG; 464 scs.st_additional_info = NULL; 465 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 466 STMF_CMD_LPORT_ONLINE_COMPLETE : 467 STMF_CMD_LPORT_OFFLINE_COMPLETE, 468 tgt->target_stmf_lport, &scs); 469 break; 470 case TE_STMF_ONLINE_COMPLETE_ACK: 471 case TE_STMF_OFFLINE_COMPLETE_ACK: 472 /* Ignore */ 473 break; 474 default: 475 ASSERT(0); 476 } 477 } 478 479 480 static void 481 tgt_sm_offline(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 482 { 483 stmf_change_status_t scs; 484 485 switch (ctx->te_ctx_event) { 486 case TE_STMF_OFFLINE_COMPLETE_ACK: 487 if (tgt->target_deleting) { 488 tgt_sm_new_state(tgt, ctx, TS_DELETING_STMF_DEREG); 489 } else { 490 tgt_sm_new_state(tgt, ctx, TS_STMF_OFFLINE); 491 } 492 break; 493 case TE_DELETE: 494 /* TE_DELETE is handled in tgt_sm_event_dispatch() */ 495 break; 496 case TE_STMF_ONLINE_REQ: 497 case TE_STMF_OFFLINE_REQ: 498 /* 499 * We can't complete STMF's request since we are busy going 500 * offline. 501 */ 502 scs.st_completion_status = STMF_INVALID_ARG; 503 scs.st_additional_info = NULL; 504 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 505 STMF_CMD_LPORT_ONLINE_COMPLETE : 506 STMF_CMD_LPORT_OFFLINE_COMPLETE, 507 tgt->target_stmf_lport, &scs); 508 break; 509 case TE_STMF_ONLINE_COMPLETE_ACK: 510 /* Ignore */ 511 break; 512 default: 513 ASSERT(0); 514 } 515 } 516 517 518 static void 519 tgt_sm_stmf_offline(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 520 { 521 stmf_change_status_t scs; 522 523 switch (ctx->te_ctx_event) { 524 case TE_STMF_ONLINE_REQ: 525 tgt_sm_new_state(tgt, ctx, TS_ONLINING); 526 break; 527 case TE_DELETE: 528 tgt_sm_new_state(tgt, ctx, TS_DELETING_STMF_DEREG); 529 break; 530 case TE_STMF_OFFLINE_REQ: 531 /* Already offline */ 532 scs.st_completion_status = STMF_ALREADY; 533 scs.st_additional_info = NULL; 534 (void) stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE, 535 tgt->target_stmf_lport, &scs); 536 break; 537 case TE_STMF_ONLINE_COMPLETE_ACK: 538 case TE_STMF_OFFLINE_COMPLETE_ACK: 539 /* Ignore */ 540 break; 541 default: 542 ASSERT(0); 543 } 544 } 545 546 547 static void 548 tgt_sm_deleting_stmf_dereg(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 549 { 550 stmf_change_status_t scs; 551 552 /* Terminal state, no events */ 553 switch (ctx->te_ctx_event) { 554 case TE_STMF_ONLINE_REQ: 555 case TE_STMF_OFFLINE_REQ: 556 /* 557 * We can't complete STMF's request since we are being deleted 558 */ 559 scs.st_completion_status = STMF_INVALID_ARG; 560 scs.st_additional_info = NULL; 561 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 562 STMF_CMD_LPORT_ONLINE_COMPLETE : 563 STMF_CMD_LPORT_OFFLINE_COMPLETE, 564 tgt->target_stmf_lport, &scs); 565 break; 566 case TE_STMF_ONLINE_COMPLETE_ACK: 567 case TE_STMF_OFFLINE_COMPLETE_ACK: 568 /* Ignore */ 569 break; 570 case TE_STMF_DEREG_SUCCESS: 571 tgt_sm_new_state(tgt, ctx, TS_DELETING); 572 break; 573 case TE_STMF_DEREG_FAIL: 574 tgt_sm_new_state(tgt, ctx, TS_DELETING_STMF_DEREG_FAIL); 575 break; 576 default: 577 ASSERT(0); 578 } 579 } 580 581 static void 582 tgt_sm_deleting_stmf_dereg_fail(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 583 { 584 stmf_change_status_t scs; 585 586 /* Terminal state, no events */ 587 switch (ctx->te_ctx_event) { 588 case TE_STMF_ONLINE_REQ: 589 case TE_STMF_OFFLINE_REQ: 590 /* 591 * We can't complete STMF's request since we are being deleted 592 */ 593 scs.st_completion_status = STMF_INVALID_ARG; 594 scs.st_additional_info = NULL; 595 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 596 STMF_CMD_LPORT_ONLINE_COMPLETE : 597 STMF_CMD_LPORT_OFFLINE_COMPLETE, 598 tgt->target_stmf_lport, &scs); 599 break; 600 case TE_STMF_ONLINE_COMPLETE_ACK: 601 case TE_STMF_OFFLINE_COMPLETE_ACK: 602 /* Ignore */ 603 break; 604 case TE_STMF_DEREG_RETRY: 605 tgt_sm_new_state(tgt, ctx, TS_DELETING_STMF_DEREG); 606 break; 607 default: 608 ASSERT(0); 609 } 610 } 611 612 static void 613 tgt_sm_deleting(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 614 { 615 stmf_change_status_t scs; 616 617 /* Terminal state, no events */ 618 switch (ctx->te_ctx_event) { 619 case TE_STMF_ONLINE_REQ: 620 case TE_STMF_OFFLINE_REQ: 621 /* 622 * We can't complete STMF's request since we are being deleted 623 */ 624 scs.st_completion_status = STMF_INVALID_ARG; 625 scs.st_additional_info = NULL; 626 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 627 STMF_CMD_LPORT_ONLINE_COMPLETE : 628 STMF_CMD_LPORT_OFFLINE_COMPLETE, 629 tgt->target_stmf_lport, &scs); 630 break; 631 case TE_STMF_ONLINE_COMPLETE_ACK: 632 case TE_STMF_OFFLINE_COMPLETE_ACK: 633 /* Ignore */ 634 break; 635 default: 636 ASSERT(0); 637 } 638 } 639 640 641 static void 642 iscsit_tgt_dereg_retry(void *arg) 643 { 644 iscsit_tgt_t *tgt = arg; 645 646 /* 647 * Rather than guaranteeing the target state machine code will not 648 * block for long periods of time (tying up this callout thread) 649 * we will queue a task on the taskq to send the retry event. 650 * If it fails we'll setup another timeout and try again later. 651 */ 652 if (taskq_dispatch(iscsit_global.global_dispatch_taskq, 653 iscsit_tgt_dereg_task, tgt, DDI_NOSLEEP) == NULL) { 654 /* Dispatch failed, try again later */ 655 (void) timeout(iscsit_tgt_dereg_retry, tgt, 656 drv_usectohz(TGT_DEREG_RETRY_SECONDS * 1000000)); 657 } 658 } 659 660 static void 661 iscsit_tgt_dereg_task(void *arg) 662 { 663 iscsit_tgt_t *tgt = arg; 664 665 iscsit_tgt_sm_event(tgt, TE_STMF_DEREG_RETRY); 666 } 667 668 static void 669 tgt_sm_new_state(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx, 670 iscsit_tgt_state_t new_state) 671 { 672 stmf_local_port_t *lport = tgt->target_stmf_lport; 673 stmf_change_status_t scs; 674 stmf_state_change_info_t sci; 675 idm_status_t idmrc; 676 stmf_status_t stmfrc; 677 678 scs.st_completion_status = STMF_SUCCESS; 679 scs.st_additional_info = NULL; 680 681 /* 682 * Validate new state 683 */ 684 ASSERT(new_state != TS_UNDEFINED); 685 ASSERT3U(new_state, <, TS_MAX_STATE); 686 687 new_state = (new_state < TS_MAX_STATE) ? 688 new_state : TS_UNDEFINED; 689 690 IDM_SM_LOG(CE_NOTE, "tgt_sm_new_state: tgt %p, %s(%d) --> %s(%d)\n", 691 (void *) tgt, iscsit_ts_name[tgt->target_state], tgt->target_state, 692 iscsit_ts_name[new_state], new_state); 693 DTRACE_PROBE3(target__state__change, 694 iscsit_tgt_t *, tgt, tgt_event_ctx_t *, ctx, 695 iscsit_tgt_state_t, new_state); 696 697 mutex_enter(&tgt->target_mutex); 698 idm_sm_audit_state_change(&tgt->target_state_audit, SAS_ISCSIT_TGT, 699 (int)tgt->target_state, (int)new_state); 700 tgt->target_last_state = tgt->target_state; 701 tgt->target_state = new_state; 702 mutex_exit(&tgt->target_mutex); 703 704 switch (tgt->target_state) { 705 case TS_ONLINING: 706 idmrc = iscsit_tgt_online(tgt); 707 if (idmrc != IDM_STATUS_SUCCESS) { 708 scs.st_completion_status = STMF_TARGET_FAILURE; 709 iscsit_tgt_sm_event(tgt, TE_ONLINE_FAIL); 710 } else { 711 iscsit_tgt_sm_event(tgt, TE_ONLINE_SUCCESS); 712 } 713 /* 714 * Let STMF know the how the online operation completed. 715 * STMF will respond with an acknowlege later 716 */ 717 (void) stmf_ctl(STMF_CMD_LPORT_ONLINE_COMPLETE, lport, &scs); 718 break; 719 case TS_ONLINE: 720 break; 721 case TS_STMF_ONLINE: 722 (void) iscsit_isns_register(tgt); 723 break; 724 case TS_DELETING_NEED_OFFLINE: 725 sci.st_rflags = STMF_RFLAG_STAY_OFFLINED; 726 sci.st_additional_info = "Offline for delete"; 727 (void) stmf_ctl(STMF_CMD_LPORT_OFFLINE, lport, &sci); 728 break; 729 case TS_OFFLINING: 730 /* Async callback generates completion event */ 731 iscsit_tgt_offline(tgt); 732 break; 733 case TS_OFFLINE: 734 break; 735 case TS_STMF_OFFLINE: 736 break; 737 case TS_DELETING_STMF_DEREG: 738 stmfrc = stmf_deregister_local_port(tgt->target_stmf_lport); 739 if (stmfrc == STMF_SUCCESS) { 740 iscsit_tgt_sm_event(tgt, TE_STMF_DEREG_SUCCESS); 741 } else { 742 iscsit_tgt_sm_event(tgt, TE_STMF_DEREG_FAIL); 743 } 744 break; 745 case TS_DELETING_STMF_DEREG_FAIL: 746 /* Retry dereg in 1 second */ 747 (void) timeout(iscsit_tgt_dereg_retry, tgt, 748 drv_usectohz(TGT_DEREG_RETRY_SECONDS * 1000000)); 749 break; 750 case TS_DELETING: 751 iscsit_tgt_async_wait_ref(tgt, iscsit_tgt_unref); 752 break; 753 default: 754 ASSERT(0); 755 } 756 } 757 758 759 /* 760 * Target, TPGT, TPG utility functions 761 */ 762 763 it_cfg_status_t 764 iscsit_config_merge_tgt(it_config_t *cfg) 765 { 766 it_tgt_t *cfg_tgt; 767 iscsit_tgt_t *tgt, *next_tgt; 768 769 770 /* 771 * 1. >> Lock << 772 * 2. Removing deleted objects 773 * 3. Add deleted targets to global delete list 774 * 4. "delete" event to target state machine 775 * 5. >> Unlock << 776 * 6. Create new targets, update modified targets 777 */ 778 for (tgt = avl_first(&iscsit_global.global_target_list); 779 tgt != NULL; 780 tgt = next_tgt) { 781 next_tgt = AVL_NEXT(&iscsit_global.global_target_list, tgt); 782 783 if (it_tgt_lookup(cfg, tgt->target_name) == NULL) { 784 avl_remove(&iscsit_global.global_target_list, tgt); 785 list_insert_tail( 786 &iscsit_global.global_deleted_target_list, tgt); 787 iscsit_tgt_sm_event(tgt, TE_DELETE); 788 } 789 } 790 791 /* Now walk through the list of configured targets */ 792 for (cfg_tgt = cfg->config_tgt_list; 793 cfg_tgt != NULL; 794 cfg_tgt = cfg_tgt->tgt_next) { 795 /* See if we have an existing target */ 796 tgt = iscsit_tgt_lookup_locked(cfg_tgt->tgt_name); 797 798 if (tgt == NULL) { 799 tgt = iscsit_tgt_create(cfg_tgt); 800 if (tgt == NULL) 801 return (ITCFG_TGT_CREATE_ERR); 802 avl_add(&iscsit_global.global_target_list, tgt); 803 } else { 804 (void) iscsit_tgt_modify(tgt, cfg_tgt); 805 iscsit_tgt_rele(tgt); 806 } 807 } 808 809 /* 810 * Targets on the iscsit_global.global_deleted_target_list will remove 811 * and destroy themselves when their associated state machines reach 812 * the TS_DELETED state and all references are released. 813 */ 814 return (ITCFG_SUCCESS); 815 } 816 817 iscsit_tgt_t * 818 iscsit_tgt_lookup(char *target_name) 819 { 820 iscsit_tgt_t *result; 821 822 ISCSIT_GLOBAL_LOCK(RW_READER); 823 result = iscsit_tgt_lookup_locked(target_name); 824 ISCSIT_GLOBAL_UNLOCK(); 825 826 return (result); 827 } 828 829 iscsit_tgt_t * 830 iscsit_tgt_lookup_locked(char *target_name) 831 { 832 iscsit_tgt_t tmp_tgt; 833 iscsit_tgt_t *result; 834 835 /* 836 * Use a dummy target for lookup, filling in all fields used in AVL 837 * comparison. 838 */ 839 tmp_tgt.target_name = target_name; 840 if ((result = avl_find(&iscsit_global.global_target_list, 841 &tmp_tgt, NULL)) != NULL) { 842 iscsit_tgt_hold(result); 843 } 844 845 return (result); 846 } 847 848 iscsit_tgt_t * 849 iscsit_tgt_create(it_tgt_t *cfg_tgt) 850 { 851 iscsit_tgt_t *result; 852 stmf_local_port_t *lport; 853 854 /* 855 * Each target is an STMF local port. 856 */ 857 lport = stmf_alloc(STMF_STRUCT_STMF_LOCAL_PORT, 858 sizeof (iscsit_tgt_t) + sizeof (scsi_devid_desc_t) + 859 strnlen(cfg_tgt->tgt_name, MAX_ISCSI_NODENAMELEN) + 1, 0); 860 if (lport == NULL) { 861 return (NULL); 862 } 863 864 result = lport->lport_port_private; 865 result->target_state = TS_CREATED; 866 result->target_stmf_lport_registered = 0; 867 /* Use pointer arithmetic to find scsi_devid_desc_t */ 868 result->target_devid = (scsi_devid_desc_t *)(result + 1); 869 (void) strcpy((char *)result->target_devid->ident, cfg_tgt->tgt_name); 870 result->target_devid->ident_length = 871 strnlen(cfg_tgt->tgt_name, MAX_ISCSI_NODENAMELEN); 872 result->target_devid->protocol_id = PROTOCOL_iSCSI; 873 result->target_devid->piv = 1; 874 result->target_devid->code_set = CODE_SET_ASCII; 875 result->target_devid->association = ID_IS_TARGET_PORT; 876 877 /* Store a shortcut to the target name */ 878 result->target_name = (char *)result->target_devid->ident; 879 idm_sm_audit_init(&result->target_state_audit); 880 mutex_init(&result->target_mutex, NULL, MUTEX_DEFAULT, NULL); 881 avl_create(&result->target_sess_list, iscsit_sess_avl_compare, 882 sizeof (iscsit_sess_t), offsetof(iscsit_sess_t, ist_tgt_ln)); 883 avl_create(&result->target_tpgt_list, iscsit_tpgt_avl_compare, 884 sizeof (iscsit_tpgt_t), offsetof(iscsit_tpgt_t, tpgt_tgt_ln)); 885 list_create(&result->target_events, sizeof (tgt_event_ctx_t), 886 offsetof(tgt_event_ctx_t, te_ctx_node)); 887 idm_refcnt_init(&result->target_refcnt, result); 888 idm_refcnt_init(&result->target_sess_refcnt, result); 889 890 /* Finish initializing local port */ 891 /* 892 * Would like infinite timeout, but this is about as long as can 893 * be specified to stmf on a 32 bit kernel. 894 */ 895 lport->lport_abort_timeout = 2000; /* seconds */ 896 lport->lport_id = result->target_devid; 897 lport->lport_pp = iscsit_global.global_pp; 898 lport->lport_ds = iscsit_global.global_dbuf_store; 899 lport->lport_xfer_data = &iscsit_xfer_scsi_data; 900 lport->lport_send_status = &iscsit_send_scsi_status; 901 lport->lport_task_free = &iscsit_lport_task_free; 902 lport->lport_abort = &iscsit_abort; 903 lport->lport_ctl = &iscsit_ctl; 904 result->target_stmf_lport = lport; 905 906 /* 907 * Additional target modifications from config 908 */ 909 if (iscsit_tgt_modify(result, cfg_tgt) != IDM_STATUS_SUCCESS) { 910 iscsit_tgt_destroy(result); 911 return (NULL); 912 } 913 914 /* 915 * Register the target with STMF but not until we have all the 916 * TPGT bindings and any other additional config setup. STMF 917 * may immediately ask us to go online. 918 */ 919 if (stmf_register_local_port(lport) != STMF_SUCCESS) { 920 iscsit_tgt_destroy(result); 921 return (NULL); 922 } 923 result->target_stmf_lport_registered = 1; 924 925 iscsit_global_hold(); 926 927 return (result); 928 } 929 930 static idm_status_t 931 iscsit_tgt_modify(iscsit_tgt_t *tgt, it_tgt_t *cfg_tgt) 932 { 933 idm_status_t idmrc = IDM_STATUS_SUCCESS; 934 list_t tpgt_del_list; 935 936 /* Merge TPGT */ 937 list_create(&tpgt_del_list, sizeof (iscsit_tpgt_t), 938 offsetof(iscsit_tpgt_t, tpgt_delete_ln)); 939 940 mutex_enter(&tgt->target_mutex); 941 if (tgt->target_props) { 942 nvlist_free(tgt->target_props); 943 tgt->target_props = NULL; 944 } 945 (void) nvlist_dup(cfg_tgt->tgt_properties, &tgt->target_props, 946 KM_SLEEP); 947 948 if ((idmrc = iscsit_tgt_merge_tpgt(tgt, cfg_tgt, &tpgt_del_list)) != 949 IDM_STATUS_SUCCESS) { 950 /* This should never happen */ 951 cmn_err(CE_WARN, "Fail to configure TPGTs for " 952 "target %s, the target modification could not be " 953 "completed.", tgt->target_name); 954 } 955 956 mutex_exit(&tgt->target_mutex); 957 958 iscsit_config_destroy_tpgts(&tpgt_del_list); 959 960 /* 961 * If the target is truly modified (not newly created), 962 * inform iSNS to update the target registration. 963 */ 964 if ((tgt->target_generation > 0) && 965 (cfg_tgt->tgt_generation > tgt->target_generation)) { 966 iscsit_isns_target_update(tgt); 967 } 968 969 tgt->target_generation = cfg_tgt->tgt_generation; 970 971 return (idmrc); 972 } 973 974 void 975 iscsit_config_destroy_tpgts(list_t *tpgt_del_list) 976 { 977 iscsit_tpgt_t *tpgt, *next_tpgt; 978 979 for (tpgt = list_head(tpgt_del_list); 980 tpgt != NULL; 981 tpgt = next_tpgt) { 982 next_tpgt = list_next(tpgt_del_list, tpgt); 983 984 list_remove(tpgt_del_list, tpgt); 985 idm_refcnt_wait_ref(&tpgt->tpgt_refcnt); 986 iscsit_tpgt_destroy(tpgt); 987 } 988 } 989 990 void 991 iscsit_tgt_unref(void *tgt_void) 992 { 993 iscsit_tgt_t *tgt = tgt_void; 994 995 ISCSIT_GLOBAL_LOCK(RW_WRITER); 996 list_remove(&iscsit_global.global_deleted_target_list, tgt); 997 ISCSIT_GLOBAL_UNLOCK(); 998 iscsit_tgt_destroy(tgt); 999 } 1000 1001 void 1002 iscsit_tgt_async_wait_ref(iscsit_tgt_t *tgt, idm_refcnt_cb_t *cb_func) 1003 { 1004 idm_refcnt_async_wait_ref(&tgt->target_refcnt, cb_func); 1005 } 1006 1007 static void 1008 iscsit_tgt_destroy(iscsit_tgt_t *tgt) 1009 { 1010 iscsit_tpgt_t *tpgt, *next_tpgt; 1011 1012 ASSERT(tgt->target_state == TS_DELETING); 1013 1014 /* 1015 * Destroy all target portal group tags 1016 */ 1017 mutex_enter(&tgt->target_mutex); 1018 for (tpgt = avl_first(&tgt->target_tpgt_list); 1019 tpgt != NULL; 1020 tpgt = next_tpgt) { 1021 next_tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt); 1022 avl_remove(&tgt->target_tpgt_list, tpgt); 1023 iscsit_tpgt_destroy(tpgt); 1024 } 1025 1026 if (tgt->target_props) { 1027 nvlist_free(tgt->target_props); 1028 } 1029 mutex_exit(&tgt->target_mutex); 1030 1031 /* 1032 * Destroy target 1033 */ 1034 idm_refcnt_destroy(&tgt->target_sess_refcnt); 1035 idm_refcnt_destroy(&tgt->target_refcnt); 1036 list_destroy(&tgt->target_events); 1037 avl_destroy(&tgt->target_tpgt_list); 1038 avl_destroy(&tgt->target_sess_list); 1039 mutex_destroy(&tgt->target_mutex); 1040 stmf_free(tgt->target_stmf_lport); /* Also frees "tgt' */ 1041 iscsit_global_rele(); 1042 } 1043 1044 void 1045 iscsit_tgt_hold(iscsit_tgt_t *tgt) 1046 { 1047 idm_refcnt_hold(&tgt->target_refcnt); 1048 } 1049 1050 void 1051 iscsit_tgt_rele(iscsit_tgt_t *tgt) 1052 { 1053 idm_refcnt_rele(&tgt->target_refcnt); 1054 } 1055 1056 int 1057 iscsit_tgt_avl_compare(const void *void_tgt1, const void *void_tgt2) 1058 { 1059 const iscsit_tgt_t *tgt1 = void_tgt1; 1060 const iscsit_tgt_t *tgt2 = void_tgt2; 1061 int result; 1062 1063 /* 1064 * Sort by ISID first then TSIH 1065 */ 1066 result = strcmp(tgt1->target_name, tgt2->target_name); 1067 if (result < 0) { 1068 return (-1); 1069 } else if (result > 0) { 1070 return (1); 1071 } 1072 1073 return (0); 1074 } 1075 1076 1077 iscsit_tpgt_t * 1078 iscsit_tgt_lookup_tpgt(iscsit_tgt_t *tgt, uint16_t tag) 1079 { 1080 iscsit_tpgt_t *result; 1081 1082 mutex_enter(&tgt->target_mutex); 1083 result = iscsit_tgt_lookup_tpgt_locked(tgt, tag); 1084 mutex_exit(&tgt->target_mutex); 1085 1086 return (result); 1087 } 1088 1089 static iscsit_tpgt_t * 1090 iscsit_tgt_lookup_tpgt_locked(iscsit_tgt_t *tgt, uint16_t tag) 1091 { 1092 iscsit_tpgt_t tmp_tpgt; 1093 iscsit_tpgt_t *result; 1094 1095 /* Caller holds tgt->target_mutex */ 1096 tmp_tpgt.tpgt_tag = tag; 1097 if ((result = avl_find(&tgt->target_tpgt_list, &tmp_tpgt, NULL)) != 1098 NULL) { 1099 iscsit_tpgt_hold(result); 1100 } 1101 1102 return (result); 1103 } 1104 1105 iscsit_portal_t * 1106 iscsit_tgt_lookup_portal(iscsit_tgt_t *tgt, struct sockaddr_storage *sa, 1107 iscsit_tpgt_t **output_tpgt) 1108 { 1109 iscsit_tpgt_t *tpgt; 1110 iscsit_portal_t *portal; 1111 1112 /* Caller holds tgt->target_mutex */ 1113 ASSERT(mutex_owned(&tgt->target_mutex)); 1114 for (tpgt = avl_first(&tgt->target_tpgt_list); 1115 tpgt != NULL; 1116 tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt)) { 1117 portal = iscsit_tpg_portal_lookup(tpgt->tpgt_tpg, sa); 1118 if (portal) { 1119 iscsit_tpgt_hold(tpgt); 1120 *output_tpgt = tpgt; 1121 return (portal); 1122 } 1123 } 1124 1125 return (NULL); 1126 } 1127 1128 1129 void 1130 iscsit_tgt_bind_sess(iscsit_tgt_t *tgt, iscsit_sess_t *sess) 1131 { 1132 if (tgt) { 1133 sess->ist_lport = tgt->target_stmf_lport; 1134 iscsit_tgt_hold(tgt); 1135 idm_refcnt_hold(&tgt->target_sess_refcnt); 1136 mutex_enter(&tgt->target_mutex); 1137 avl_add(&tgt->target_sess_list, sess); 1138 mutex_exit(&tgt->target_mutex); 1139 } else { 1140 /* Discovery session */ 1141 sess->ist_lport = NULL; 1142 iscsit_global_hold(); 1143 ISCSIT_GLOBAL_LOCK(RW_WRITER); 1144 avl_add(&iscsit_global.global_discovery_sessions, sess); 1145 ISCSIT_GLOBAL_UNLOCK(); 1146 } 1147 } 1148 1149 void 1150 iscsit_tgt_unbind_sess(iscsit_tgt_t *tgt, iscsit_sess_t *sess) 1151 { 1152 if (tgt) { 1153 mutex_enter(&tgt->target_mutex); 1154 avl_remove(&tgt->target_sess_list, sess); 1155 mutex_exit(&tgt->target_mutex); 1156 sess->ist_tgt = (iscsit_tgt_t *)SESS_UNBOUND_FROM_TGT; 1157 idm_refcnt_rele(&tgt->target_sess_refcnt); 1158 iscsit_tgt_rele(tgt); 1159 } else { 1160 /* Discovery session */ 1161 ISCSIT_GLOBAL_LOCK(RW_WRITER); 1162 avl_remove(&iscsit_global.global_discovery_sessions, sess); 1163 ISCSIT_GLOBAL_UNLOCK(); 1164 iscsit_global_rele(); 1165 } 1166 } 1167 1168 #define LOCK_FOR_SESS_LOOKUP(lookup_tgt) { \ 1169 if ((lookup_tgt) == NULL) { \ 1170 ISCSIT_GLOBAL_LOCK(RW_READER); \ 1171 } else { \ 1172 mutex_enter(&(lookup_tgt)->target_mutex); \ 1173 } \ 1174 } 1175 1176 #define UNLOCK_FOR_SESS_LOOKUP(lookup_tgt) { \ 1177 if ((lookup_tgt) == NULL) { \ 1178 ISCSIT_GLOBAL_UNLOCK(); \ 1179 } else { \ 1180 mutex_exit(&(lookup_tgt)->target_mutex); \ 1181 } \ 1182 } 1183 1184 iscsit_sess_t * 1185 iscsit_tgt_lookup_sess(iscsit_tgt_t *tgt, char *initiator_name, 1186 uint8_t *isid, uint16_t tsih, uint16_t tag) 1187 { 1188 iscsit_sess_t tmp_sess; 1189 avl_tree_t *sess_avl; 1190 avl_index_t where; 1191 iscsit_sess_t *result; 1192 1193 /* 1194 * If tgt is NULL then we are looking for a discovery session 1195 */ 1196 if (tgt == NULL) { 1197 sess_avl = &iscsit_global.global_discovery_sessions; 1198 } else { 1199 sess_avl = &tgt->target_sess_list; 1200 } 1201 1202 LOCK_FOR_SESS_LOOKUP(tgt); 1203 if (avl_numnodes(sess_avl) == NULL) { 1204 UNLOCK_FOR_SESS_LOOKUP(tgt); 1205 return (NULL); 1206 } 1207 1208 /* 1209 * We'll try to find a session matching ISID + TSIH first. If we 1210 * can't find one then we will return the closest match. If the 1211 * caller needs an exact match it must compare the TSIH after 1212 * the session is returned. 1213 * 1214 * The reason we do this "fuzzy matching" is to allow matching 1215 * sessions with different TSIH values on the same AVL list. This 1216 * makes session reinstatement much easier since the new session can 1217 * live on the list at the same time as the old session is cleaning up. 1218 */ 1219 bcopy(isid, tmp_sess.ist_isid, ISCSI_ISID_LEN); 1220 tmp_sess.ist_initiator_name = initiator_name; 1221 tmp_sess.ist_tsih = tsih; 1222 tmp_sess.ist_tpgt_tag = tag; 1223 1224 result = avl_find(sess_avl, &tmp_sess, &where); 1225 if (result != NULL) { 1226 iscsit_sess_hold(result); 1227 UNLOCK_FOR_SESS_LOOKUP(tgt); 1228 return (result); 1229 } 1230 1231 /* 1232 * avl_find_nearest() may return a result with a different ISID so 1233 * we should only return a result if the name and ISID match 1234 */ 1235 result = avl_nearest(sess_avl, where, AVL_BEFORE); 1236 if ((result != NULL) && 1237 (strcmp(result->ist_initiator_name, initiator_name) == 0) && 1238 (memcmp(result->ist_isid, isid, ISCSI_ISID_LEN) == 0) && 1239 (result->ist_tpgt_tag == tag)) { 1240 iscsit_sess_hold(result); 1241 UNLOCK_FOR_SESS_LOOKUP(tgt); 1242 return (result); 1243 } 1244 1245 result = avl_nearest(sess_avl, where, AVL_AFTER); 1246 if ((result != NULL) && 1247 (strcmp(result->ist_initiator_name, initiator_name) == 0) && 1248 (memcmp(result->ist_isid, isid, ISCSI_ISID_LEN) == 0) && 1249 (result->ist_tpgt_tag == tag)) { 1250 iscsit_sess_hold(result); 1251 UNLOCK_FOR_SESS_LOOKUP(tgt); 1252 return (result); 1253 } 1254 1255 UNLOCK_FOR_SESS_LOOKUP(tgt); 1256 1257 return (NULL); 1258 } 1259 1260 static idm_status_t 1261 iscsit_tgt_merge_tpgt(iscsit_tgt_t *tgt, it_tgt_t *cfg_tgt, 1262 list_t *tpgt_del_list) 1263 { 1264 iscsit_tpgt_t *tpgt, *next_tpgt; 1265 it_tpgt_t *cfg_tpgt; 1266 1267 /* 1268 * 1. >> Lock << 1269 * 2. Removing all objects and place on a temp list 1270 * 3. Add new objects 1271 * 4. >> Unlock << 1272 * 5. tpgt_del_list contains deleted objects 1273 */ 1274 ASSERT(avl_is_empty(&tgt->target_tpgt_list) || 1275 (tpgt_del_list != NULL)); 1276 1277 if (tpgt_del_list) { 1278 for (tpgt = avl_first(&tgt->target_tpgt_list); 1279 tpgt != NULL; tpgt = next_tpgt) { 1280 next_tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt); 1281 avl_remove(&tgt->target_tpgt_list, tpgt); 1282 if (tgt->target_state == TS_STMF_ONLINE) { 1283 tpgt->tpgt_needs_tpg_offline = B_TRUE; 1284 } 1285 list_insert_tail(tpgt_del_list, tpgt); 1286 } 1287 } 1288 1289 if (cfg_tgt->tgt_tpgt_list == NULL) { 1290 tpgt = iscsit_tpgt_create_default(); 1291 if (tgt->target_state == TS_STMF_ONLINE) { 1292 (void) iscsit_tpg_online(tpgt->tpgt_tpg); 1293 } 1294 avl_add(&tgt->target_tpgt_list, tpgt); 1295 } else { 1296 /* Add currently defined TPGTs */ 1297 for (cfg_tpgt = cfg_tgt->tgt_tpgt_list; 1298 cfg_tpgt != NULL; 1299 cfg_tpgt = cfg_tpgt->tpgt_next) { 1300 tpgt = iscsit_tpgt_create(cfg_tpgt); 1301 if (tpgt == NULL) 1302 goto tpgt_merge_error; 1303 if (tgt->target_state == TS_STMF_ONLINE) { 1304 (void) iscsit_tpg_online(tpgt->tpgt_tpg); 1305 } 1306 avl_add(&tgt->target_tpgt_list, tpgt); 1307 } 1308 } 1309 1310 return (IDM_STATUS_SUCCESS); 1311 1312 tpgt_merge_error: 1313 /* 1314 * This is a problem in configuration we received so it should never 1315 * happen. That's good because we're probably no longer in a state 1316 * requested by the user -- all the unbind operations have already 1317 * taken place. 1318 */ 1319 return (IDM_STATUS_FAIL); 1320 } 1321 1322 static iscsit_tpgt_t * 1323 iscsit_tpgt_create(it_tpgt_t *cfg_tpgt) 1324 { 1325 iscsit_tpg_t *tpg; 1326 iscsit_tpgt_t *result; 1327 1328 /* This takes a reference on the TPG */ 1329 tpg = iscsit_tpg_lookup_locked(cfg_tpgt->tpgt_tpg_name); 1330 if (tpg == NULL) 1331 return (NULL); 1332 1333 result = kmem_zalloc(sizeof (*result), KM_SLEEP); 1334 1335 result->tpgt_tpg = tpg; 1336 result->tpgt_tag = cfg_tpgt->tpgt_tag; 1337 1338 return (result); 1339 } 1340 1341 iscsit_tpgt_t * 1342 iscsit_tpgt_create_default() 1343 { 1344 iscsit_tpgt_t *result; 1345 1346 result = kmem_zalloc(sizeof (*result), KM_SLEEP); 1347 1348 result->tpgt_tpg = iscsit_global.global_default_tpg; 1349 iscsit_tpg_hold(result->tpgt_tpg); 1350 result->tpgt_tag = ISCSIT_DEFAULT_TPGT; 1351 1352 return (result); 1353 } 1354 1355 void 1356 iscsit_tpgt_destroy(iscsit_tpgt_t *tpgt) 1357 { 1358 if (tpgt->tpgt_needs_tpg_offline) { 1359 iscsit_tpg_offline(tpgt->tpgt_tpg); 1360 } 1361 iscsit_tpg_rele(tpgt->tpgt_tpg); 1362 kmem_free(tpgt, sizeof (*tpgt)); 1363 } 1364 1365 void 1366 iscsit_tpgt_hold(iscsit_tpgt_t *tpgt) 1367 { 1368 idm_refcnt_hold(&tpgt->tpgt_refcnt); 1369 } 1370 1371 void 1372 iscsit_tpgt_rele(iscsit_tpgt_t *tpgt) 1373 { 1374 idm_refcnt_rele(&tpgt->tpgt_refcnt); 1375 } 1376 1377 int 1378 iscsit_tpgt_avl_compare(const void *void_tpgt1, const void *void_tpgt2) 1379 { 1380 const iscsit_tpgt_t *tpgt1 = void_tpgt1; 1381 const iscsit_tpgt_t *tpgt2 = void_tpgt2; 1382 1383 if (tpgt1->tpgt_tag < tpgt2->tpgt_tag) 1384 return (-1); 1385 else if (tpgt1->tpgt_tag > tpgt2->tpgt_tag) 1386 return (1); 1387 1388 return (0); 1389 } 1390 1391 static idm_status_t 1392 iscsit_tgt_online(iscsit_tgt_t *tgt) 1393 { 1394 iscsit_tpgt_t *tpgt, *tpgt_fail; 1395 idm_status_t rc; 1396 1397 mutex_enter(&tgt->target_mutex); 1398 1399 ASSERT(tgt->target_sess_list.avl_numnodes == 0); 1400 idm_refcnt_reset(&tgt->target_sess_refcnt); 1401 for (tpgt = avl_first(&tgt->target_tpgt_list); 1402 tpgt != NULL; 1403 tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt)) { 1404 rc = iscsit_tpg_online(tpgt->tpgt_tpg); 1405 if (rc != IDM_STATUS_SUCCESS) { 1406 tpgt_fail = tpgt; 1407 goto tgt_online_fail; 1408 } 1409 } 1410 1411 mutex_exit(&tgt->target_mutex); 1412 1413 return (IDM_STATUS_SUCCESS); 1414 1415 tgt_online_fail: 1416 /* Offline all the tpgs we successfully onlined up to the failure */ 1417 for (tpgt = avl_first(&tgt->target_tpgt_list); 1418 tpgt != tpgt_fail; 1419 tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt)) { 1420 iscsit_tpg_offline(tpgt->tpgt_tpg); 1421 } 1422 mutex_exit(&tgt->target_mutex); 1423 return (rc); 1424 } 1425 1426 static void 1427 iscsit_tgt_offline_cb(void *tgt_void) 1428 { 1429 iscsit_tgt_t *tgt = tgt_void; 1430 stmf_change_status_t scs; 1431 1432 iscsit_tgt_sm_event(tgt, TE_OFFLINE_COMPLETE); 1433 1434 scs.st_completion_status = STMF_SUCCESS; 1435 scs.st_additional_info = NULL; 1436 (void) stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE, 1437 tgt->target_stmf_lport, &scs); 1438 } 1439 1440 static void 1441 iscsit_tgt_offline(iscsit_tgt_t *tgt) 1442 { 1443 iscsit_tpgt_t *tpgt; 1444 iscsit_sess_t *ist; 1445 1446 mutex_enter(&tgt->target_mutex); 1447 1448 /* Offline target portal groups */ 1449 for (tpgt = avl_first(&tgt->target_tpgt_list); 1450 tpgt != NULL; 1451 tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt)) { 1452 iscsit_tpg_offline(tpgt->tpgt_tpg); 1453 } 1454 1455 /* Close any active sessions */ 1456 for (ist = avl_first(&tgt->target_sess_list); 1457 ist != NULL; 1458 ist = AVL_NEXT(&tgt->target_sess_list, ist)) { 1459 /* 1460 * This is not a synchronous operation but after all 1461 * sessions have been cleaned up there will be no 1462 * more session-related holds on the target. 1463 */ 1464 iscsit_sess_close(ist); 1465 } 1466 1467 mutex_exit(&tgt->target_mutex); 1468 1469 /* 1470 * Wait for all the sessions to quiesce. 1471 */ 1472 idm_refcnt_async_wait_ref(&tgt->target_sess_refcnt, 1473 &iscsit_tgt_offline_cb); 1474 } 1475 1476 it_cfg_status_t 1477 iscsit_config_merge_tpg(it_config_t *cfg, list_t *tpg_del_list) 1478 { 1479 it_tpg_t *cfg_tpg; 1480 iscsit_tpg_t *tpg, *next_tpg; 1481 1482 /* 1483 * 1. >> Lock << 1484 * 2. Removing deleted objects and place on a temp list 1485 * 3. Add new objects 1486 * 4. >> Unlock << 1487 * 5. tpg_del_list contains objects to destroy 1488 */ 1489 for (tpg = avl_first(&iscsit_global.global_tpg_list); 1490 tpg != NULL; 1491 tpg = next_tpg) { 1492 next_tpg = AVL_NEXT(&iscsit_global.global_tpg_list, tpg); 1493 1494 if (it_tpg_lookup(cfg, tpg->tpg_name) == NULL) { 1495 /* 1496 * The policy around when to allow a target portal 1497 * group to be deleted is implemented in libiscsit. 1498 * By the time the request gets to the kernel module 1499 * we expect that it conforms to policy so we will 1500 * cleanup all references to TPG and destroy it if it 1501 * is possible to do so. 1502 * 1503 */ 1504 avl_remove(&iscsit_global.global_tpg_list, tpg); 1505 list_insert_tail(tpg_del_list, tpg); 1506 } 1507 } 1508 1509 /* Now walk through the list of configured target portal groups */ 1510 for (cfg_tpg = cfg->config_tpg_list; 1511 cfg_tpg != NULL; 1512 cfg_tpg = cfg_tpg->tpg_next) { 1513 /* See if we have an existing target portal group */ 1514 tpg = iscsit_tpg_lookup_locked(cfg_tpg->tpg_name); 1515 1516 if (tpg == NULL) { 1517 tpg = iscsit_tpg_create(cfg_tpg); 1518 ASSERT(tpg != NULL); 1519 avl_add(&iscsit_global.global_tpg_list, tpg); 1520 } else { 1521 mutex_enter(&tpg->tpg_mutex); 1522 iscsit_tpg_modify(tpg, cfg_tpg); 1523 mutex_exit(&tpg->tpg_mutex); 1524 iscsit_tpg_rele(tpg); 1525 } 1526 } 1527 1528 return (ITCFG_SUCCESS); 1529 } 1530 1531 1532 void 1533 iscsit_config_destroy_tpgs(list_t *tpg_del_list) 1534 { 1535 iscsit_tpg_t *tpg, *next_tpg; 1536 1537 /* Now finish destroying the target portal groups */ 1538 for (tpg = list_head(tpg_del_list); 1539 tpg != NULL; 1540 tpg = next_tpg) { 1541 next_tpg = list_next(tpg_del_list, tpg); 1542 list_remove(tpg_del_list, tpg); 1543 idm_refcnt_wait_ref(&tpg->tpg_refcnt); 1544 1545 /* Kill it */ 1546 iscsit_tpg_destroy(tpg); 1547 } 1548 } 1549 1550 iscsit_tpg_t * 1551 iscsit_tpg_lookup(char *tpg_name) 1552 { 1553 iscsit_tpg_t *result; 1554 1555 ISCSIT_GLOBAL_LOCK(RW_READER); 1556 result = iscsit_tpg_lookup_locked(tpg_name); 1557 ISCSIT_GLOBAL_UNLOCK(); 1558 1559 return (result); 1560 } 1561 1562 static iscsit_tpg_t * 1563 iscsit_tpg_lookup_locked(char *tpg_name) 1564 { 1565 iscsit_tpg_t tmp_tpg; 1566 iscsit_tpg_t *result; 1567 1568 (void) strlcpy(tmp_tpg.tpg_name, tpg_name, MAX_ISCSI_NODENAMELEN); 1569 if ((result = avl_find(&iscsit_global.global_tpg_list, 1570 &tmp_tpg, NULL)) != NULL) { 1571 iscsit_tpg_hold(result); 1572 } 1573 1574 return (result); 1575 } 1576 1577 iscsit_tpg_t * 1578 iscsit_tpg_create(it_tpg_t *cfg_tpg) 1579 { 1580 iscsit_tpg_t *tpg; 1581 1582 tpg = kmem_zalloc(sizeof (*tpg), KM_SLEEP); 1583 1584 mutex_init(&tpg->tpg_mutex, NULL, MUTEX_DEFAULT, NULL); 1585 (void) strlcpy(tpg->tpg_name, cfg_tpg->tpg_name, MAX_TPG_NAMELEN); 1586 avl_create(&tpg->tpg_portal_list, iscsit_portal_avl_compare, 1587 sizeof (iscsit_portal_t), offsetof(iscsit_portal_t, portal_tpg_ln)); 1588 idm_refcnt_init(&tpg->tpg_refcnt, tpg); 1589 1590 mutex_enter(&tpg->tpg_mutex); 1591 iscsit_tpg_modify(tpg, cfg_tpg); 1592 mutex_exit(&tpg->tpg_mutex); 1593 iscsit_global_hold(); 1594 1595 return (tpg); 1596 } 1597 1598 static void 1599 iscsit_tpg_modify(iscsit_tpg_t *tpg, it_tpg_t *cfg_tpg) 1600 { 1601 iscsit_portal_t *portal, *next_portal; 1602 it_portal_t *cfg_portal; 1603 1604 /* Update portals */ 1605 for (portal = avl_first(&tpg->tpg_portal_list); 1606 portal != NULL; 1607 portal = next_portal) { 1608 next_portal = AVL_NEXT(&tpg->tpg_portal_list, portal); 1609 if (it_portal_lookup(cfg_tpg, &portal->portal_addr) == NULL) { 1610 avl_remove(&tpg->tpg_portal_list, portal); 1611 iscsit_portal_destroy(portal); 1612 } 1613 } 1614 1615 for (cfg_portal = cfg_tpg->tpg_portal_list; 1616 cfg_portal != NULL; 1617 cfg_portal = cfg_portal->next) { 1618 if ((portal = iscsit_tpg_portal_lookup_locked(tpg, 1619 &cfg_portal->portal_addr)) == NULL) { 1620 (void) iscsit_portal_create(tpg, 1621 &cfg_portal->portal_addr); 1622 } else { 1623 iscsit_portal_rele(portal); 1624 } 1625 } 1626 } 1627 1628 void 1629 iscsit_tpg_destroy(iscsit_tpg_t *tpg) 1630 { 1631 iscsit_portal_t *portal, *next_portal; 1632 1633 for (portal = avl_first(&tpg->tpg_portal_list); 1634 portal != NULL; 1635 portal = next_portal) { 1636 next_portal = AVL_NEXT(&tpg->tpg_portal_list, portal); 1637 avl_remove(&tpg->tpg_portal_list, portal); 1638 iscsit_portal_destroy(portal); 1639 } 1640 1641 idm_refcnt_wait_ref(&tpg->tpg_refcnt); 1642 idm_refcnt_destroy(&tpg->tpg_refcnt); 1643 avl_destroy(&tpg->tpg_portal_list); 1644 mutex_destroy(&tpg->tpg_mutex); 1645 kmem_free(tpg, sizeof (*tpg)); 1646 iscsit_global_rele(); 1647 } 1648 1649 void 1650 iscsit_tpg_hold(iscsit_tpg_t *tpg) 1651 { 1652 idm_refcnt_hold(&tpg->tpg_refcnt); 1653 } 1654 1655 void 1656 iscsit_tpg_rele(iscsit_tpg_t *tpg) 1657 { 1658 idm_refcnt_rele(&tpg->tpg_refcnt); 1659 } 1660 1661 iscsit_tpg_t * 1662 iscsit_tpg_createdefault() 1663 { 1664 iscsit_tpg_t *tpg; 1665 1666 tpg = kmem_zalloc(sizeof (*tpg), KM_SLEEP); 1667 1668 mutex_init(&tpg->tpg_mutex, NULL, MUTEX_DEFAULT, NULL); 1669 (void) strlcpy(tpg->tpg_name, ISCSIT_DEFAULT_TPG, MAX_TPG_NAMELEN); 1670 avl_create(&tpg->tpg_portal_list, iscsit_portal_avl_compare, 1671 sizeof (iscsit_portal_t), offsetof(iscsit_portal_t, portal_tpg_ln)); 1672 idm_refcnt_init(&tpg->tpg_refcnt, tpg); 1673 1674 /* Now create default portal */ 1675 if (iscsit_portal_create(tpg, NULL) == NULL) { 1676 iscsit_tpg_destroy(tpg); 1677 return (NULL); 1678 } 1679 1680 return (tpg); 1681 } 1682 1683 void 1684 iscsit_tpg_destroydefault(iscsit_tpg_t *tpg) 1685 { 1686 iscsit_portal_t *portal; 1687 1688 portal = avl_first(&tpg->tpg_portal_list); 1689 ASSERT(portal != NULL); 1690 avl_remove(&tpg->tpg_portal_list, portal); 1691 iscsit_portal_destroy(portal); 1692 1693 idm_refcnt_wait_ref(&tpg->tpg_refcnt); 1694 idm_refcnt_destroy(&tpg->tpg_refcnt); 1695 avl_destroy(&tpg->tpg_portal_list); 1696 mutex_destroy(&tpg->tpg_mutex); 1697 kmem_free(tpg, sizeof (*tpg)); 1698 } 1699 1700 int 1701 iscsit_tpg_avl_compare(const void *void_tpg1, const void *void_tpg2) 1702 { 1703 const iscsit_tpg_t *tpg1 = void_tpg1; 1704 const iscsit_tpg_t *tpg2 = void_tpg2; 1705 int result; 1706 1707 /* 1708 * Sort by ISID first then TSIH 1709 */ 1710 result = strcmp(tpg1->tpg_name, tpg2->tpg_name); 1711 if (result < 0) { 1712 return (-1); 1713 } else if (result > 0) { 1714 return (1); 1715 } 1716 1717 return (0); 1718 } 1719 1720 idm_status_t 1721 iscsit_tpg_online(iscsit_tpg_t *tpg) 1722 { 1723 iscsit_portal_t *portal, *portal_fail; 1724 idm_status_t rc; 1725 1726 mutex_enter(&tpg->tpg_mutex); 1727 if (tpg->tpg_online == 0) { 1728 for (portal = avl_first(&tpg->tpg_portal_list); 1729 portal != NULL; 1730 portal = AVL_NEXT(&tpg->tpg_portal_list, portal)) { 1731 rc = iscsit_portal_online(portal); 1732 if (rc != IDM_STATUS_SUCCESS) { 1733 portal_fail = portal; 1734 goto tpg_online_fail; 1735 } 1736 } 1737 } 1738 tpg->tpg_online++; 1739 1740 mutex_exit(&tpg->tpg_mutex); 1741 return (IDM_STATUS_SUCCESS); 1742 1743 tpg_online_fail: 1744 /* Offline all the portals we successfully onlined up to the failure */ 1745 for (portal = avl_first(&tpg->tpg_portal_list); 1746 portal != portal_fail; 1747 portal = AVL_NEXT(&tpg->tpg_portal_list, portal)) { 1748 iscsit_portal_offline(portal); 1749 } 1750 mutex_exit(&tpg->tpg_mutex); 1751 return (rc); 1752 } 1753 1754 void 1755 iscsit_tpg_offline(iscsit_tpg_t *tpg) 1756 { 1757 iscsit_portal_t *portal; 1758 1759 mutex_enter(&tpg->tpg_mutex); 1760 tpg->tpg_online--; 1761 if (tpg->tpg_online == 0) { 1762 for (portal = avl_first(&tpg->tpg_portal_list); 1763 portal != NULL; 1764 portal = AVL_NEXT(&tpg->tpg_portal_list, portal)) { 1765 iscsit_portal_offline(portal); 1766 } 1767 } 1768 mutex_exit(&tpg->tpg_mutex); 1769 } 1770 1771 iscsit_portal_t * 1772 iscsit_tpg_portal_lookup(iscsit_tpg_t *tpg, struct sockaddr_storage *sa) 1773 { 1774 iscsit_portal_t *result; 1775 1776 mutex_enter(&tpg->tpg_mutex); 1777 result = iscsit_tpg_portal_lookup_locked(tpg, sa); 1778 mutex_exit(&tpg->tpg_mutex); 1779 1780 return (result); 1781 } 1782 1783 static iscsit_portal_t * 1784 iscsit_tpg_portal_lookup_locked(iscsit_tpg_t *tpg, 1785 struct sockaddr_storage *sa) 1786 { 1787 iscsit_portal_t tmp_portal; 1788 iscsit_portal_t *result; 1789 1790 /* Caller holds tpg->tpg_mutex */ 1791 bcopy(sa, &tmp_portal.portal_addr, sizeof (*sa)); 1792 if ((result = avl_find(&tpg->tpg_portal_list, &tmp_portal, NULL)) != 1793 NULL) { 1794 iscsit_portal_hold(result); 1795 } 1796 1797 return (result); 1798 } 1799 1800 iscsit_portal_t * 1801 iscsit_portal_create(iscsit_tpg_t *tpg, struct sockaddr_storage *sa) 1802 { 1803 iscsit_portal_t *portal; 1804 1805 portal = kmem_zalloc(sizeof (*portal), KM_SLEEP); 1806 /* 1807 * If (sa == NULL) then we are being asked to create the default 1808 * portal -- targets will use this portal when no portals are 1809 * explicitly configured. 1810 */ 1811 if (sa != NULL) { 1812 bcopy(sa, &portal->portal_addr, sizeof (*sa)); 1813 } 1814 1815 idm_refcnt_init(&portal->portal_refcnt, portal); 1816 1817 /* 1818 * Add this portal to the list 1819 */ 1820 avl_add(&tpg->tpg_portal_list, portal); 1821 1822 return (portal); 1823 } 1824 1825 void 1826 iscsit_portal_destroy(iscsit_portal_t *portal) 1827 { 1828 ASSERT(portal->portal_svc == NULL); 1829 idm_refcnt_destroy(&portal->portal_refcnt); 1830 kmem_free(portal, sizeof (*portal)); 1831 } 1832 1833 void 1834 iscsit_portal_hold(iscsit_portal_t *portal) 1835 { 1836 idm_refcnt_hold(&portal->portal_refcnt); 1837 } 1838 1839 void 1840 iscsit_portal_rele(iscsit_portal_t *portal) 1841 { 1842 idm_refcnt_rele(&portal->portal_refcnt); 1843 } 1844 1845 int 1846 iscsit_portal_avl_compare(const void *void_portal1, const void *void_portal2) 1847 { 1848 const iscsit_portal_t *portal1 = void_portal1; 1849 const iscsit_portal_t *portal2 = void_portal2; 1850 const struct sockaddr_storage *ss1, *ss2; 1851 const struct in_addr *in1, *in2; 1852 const struct in6_addr *in61, *in62; 1853 int i; 1854 1855 /* 1856 * Compare ports, then address family, then ip address 1857 */ 1858 ss1 = &portal1->portal_addr; 1859 ss2 = &portal2->portal_addr; 1860 if (((struct sockaddr_in *)ss1)->sin_port != 1861 ((struct sockaddr_in *)ss2)->sin_port) { 1862 if (((struct sockaddr_in *)ss1)->sin_port > 1863 ((struct sockaddr_in *)ss2)->sin_port) 1864 return (1); 1865 else 1866 return (-1); 1867 } 1868 1869 /* 1870 * ports are the same 1871 */ 1872 if (ss1->ss_family != ss2->ss_family) { 1873 if (ss1->ss_family == AF_INET) 1874 return (1); 1875 else 1876 return (-1); 1877 } 1878 /* 1879 * address families are the same 1880 */ 1881 if (ss1->ss_family == AF_INET) { 1882 in1 = &((struct sockaddr_in *)ss1)->sin_addr; 1883 in2 = &((struct sockaddr_in *)ss2)->sin_addr; 1884 1885 if (in1->s_addr > in2->s_addr) 1886 return (1); 1887 else if (in1->s_addr < in2->s_addr) 1888 return (-1); 1889 else 1890 return (0); 1891 } else if (ss1->ss_family == AF_INET6) { 1892 in61 = &((struct sockaddr_in6 *)ss1)->sin6_addr; 1893 in62 = &((struct sockaddr_in6 *)ss2)->sin6_addr; 1894 1895 for (i = 0; i < 4; i++) { 1896 if (in61->s6_addr32[i] > in62->s6_addr32[i]) 1897 return (1); 1898 else if (in61->s6_addr32[i] < in62->s6_addr32[i]) 1899 return (-1); 1900 } 1901 return (0); 1902 } else 1903 cmn_err(CE_WARN, 1904 "iscsit_portal_avl_compare: unknown ss_family %d", 1905 ss1->ss_family); 1906 1907 return (1); 1908 } 1909 1910 1911 idm_status_t 1912 iscsit_portal_online(iscsit_portal_t *portal) 1913 { 1914 idm_status_t rc; 1915 idm_svc_t *svc; 1916 idm_svc_req_t sr; 1917 uint16_t port; 1918 struct sockaddr_in *sin; 1919 1920 /* Caller holds parent TPG mutex */ 1921 if (portal->portal_online == 0) { 1922 /* 1923 * If there is no existing IDM service instance for this port, 1924 * create one. If the service exists, then the lookup, 1925 * creates a reference on the existing service. 1926 */ 1927 sin = (struct sockaddr_in *)&portal->portal_addr; 1928 port = ntohs(sin->sin_port); 1929 if (port == 0) 1930 port = ISCSI_LISTEN_PORT; 1931 ASSERT(portal->portal_svc == NULL); 1932 if ((svc = idm_tgt_svc_lookup(port)) == NULL) { 1933 sr.sr_port = port; 1934 sr.sr_li = iscsit_global.global_li; 1935 sr.sr_conn_ops.icb_rx_scsi_cmd = &iscsit_op_scsi_cmd; 1936 sr.sr_conn_ops.icb_rx_scsi_rsp = NULL; 1937 sr.sr_conn_ops.icb_rx_misc = &iscsit_rx_pdu; 1938 sr.sr_conn_ops.icb_rx_error = &iscsit_rx_pdu_error; 1939 sr.sr_conn_ops.icb_task_aborted = &iscsit_task_aborted; 1940 sr.sr_conn_ops.icb_client_notify = 1941 &iscsit_client_notify; 1942 sr.sr_conn_ops.icb_build_hdr = &iscsit_build_hdr; 1943 1944 if (idm_tgt_svc_create(&sr, &svc) != 1945 IDM_STATUS_SUCCESS) { 1946 return (IDM_STATUS_FAIL); 1947 } 1948 1949 /* Get reference on the service we just created */ 1950 idm_tgt_svc_hold(svc); 1951 } 1952 if ((rc = idm_tgt_svc_online(svc)) != IDM_STATUS_SUCCESS) { 1953 idm_tgt_svc_rele_and_destroy(svc); 1954 return (IDM_STATUS_FAIL); 1955 } 1956 portal->portal_svc = svc; 1957 1958 /* 1959 * Only call iSNS for first online 1960 */ 1961 iscsit_isns_portal_online(portal); 1962 } 1963 1964 portal->portal_online++; 1965 1966 return (rc); 1967 } 1968 1969 void 1970 iscsit_portal_offline(iscsit_portal_t *portal) 1971 { 1972 portal->portal_online--; 1973 1974 if (portal->portal_online == 0) { 1975 /* 1976 * Only call iSNS for last offline 1977 */ 1978 iscsit_isns_portal_offline(portal); 1979 idm_tgt_svc_offline(portal->portal_svc); 1980 /* If service is unreferenced, destroy it too */ 1981 idm_tgt_svc_rele_and_destroy(portal->portal_svc); 1982 portal->portal_svc = NULL; 1983 } 1984 1985 } 1986 1987 it_cfg_status_t 1988 iscsit_config_merge_ini(it_config_t *cfg) 1989 { 1990 iscsit_ini_t *ini, *next_ini; 1991 it_ini_t *cfg_ini; 1992 1993 /* 1994 * Initiator objects are so simple we will just destroy all the current 1995 * objects and build new ones. Nothing should ever reference an 1996 * initator object.. instead just lookup the initiator object and 1997 * grab the properties while holding the global config lock. 1998 */ 1999 for (ini = avl_first(&iscsit_global.global_ini_list); 2000 ini != NULL; 2001 ini = next_ini) { 2002 next_ini = AVL_NEXT(&iscsit_global.global_ini_list, ini); 2003 avl_remove(&iscsit_global.global_ini_list, ini); 2004 nvlist_free(ini->ini_props); 2005 kmem_free(ini, sizeof (*ini)); 2006 iscsit_global_rele(); 2007 } 2008 2009 for (cfg_ini = cfg->config_ini_list; 2010 cfg_ini != NULL; 2011 cfg_ini = cfg_ini->ini_next) { 2012 ini = kmem_zalloc(sizeof (iscsit_ini_t), KM_SLEEP); 2013 (void) strlcpy(ini->ini_name, cfg_ini->ini_name, 2014 MAX_ISCSI_NODENAMELEN); 2015 (void) nvlist_dup(cfg_ini->ini_properties, &ini->ini_props, 2016 KM_SLEEP); 2017 avl_add(&iscsit_global.global_ini_list, ini); 2018 iscsit_global_hold(); 2019 } 2020 2021 return (ITCFG_SUCCESS); 2022 } 2023 2024 int 2025 iscsit_ini_avl_compare(const void *void_ini1, const void *void_ini2) 2026 { 2027 const iscsit_ini_t *ini1 = void_ini1; 2028 const iscsit_ini_t *ini2 = void_ini2; 2029 int result; 2030 2031 /* 2032 * Sort by ISID first then TSIH 2033 */ 2034 result = strcmp(ini1->ini_name, ini2->ini_name); 2035 if (result < 0) { 2036 return (-1); 2037 } else if (result > 0) { 2038 return (1); 2039 } 2040 2041 return (0); 2042 } 2043 2044 iscsit_ini_t * 2045 iscsit_ini_lookup_locked(char *ini_name) 2046 { 2047 iscsit_ini_t tmp_ini; 2048 iscsit_ini_t *result; 2049 2050 /* 2051 * Use a dummy target for lookup, filling in all fields used in AVL 2052 * comparison. 2053 */ 2054 (void) strlcpy(tmp_ini.ini_name, ini_name, MAX_ISCSI_NODENAMELEN); 2055 result = avl_find(&iscsit_global.global_ini_list, &tmp_ini, NULL); 2056 2057 return (result); 2058 } 2059