Home | History | Annotate | Download | only in iscsit
      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 	 * We need a global hold until the STMF-ONLINE state machine
    908 	 * completes.  Acquire that hold now, in case we need to call
    909 	 * iscsit_tgt_destroy, which will also release the hold.
    910 	 */
    911 	iscsit_global_hold();
    912 
    913 	/*
    914 	 * Additional target modifications from config
    915 	 */
    916 	if (iscsit_tgt_modify(result, cfg_tgt) != IDM_STATUS_SUCCESS) {
    917 		iscsit_tgt_destroy(result);
    918 		return (NULL);
    919 	}
    920 
    921 	/*
    922 	 * Register the target with STMF but not until we have all the
    923 	 * TPGT bindings and any other additional config setup.  STMF
    924 	 * may immediately ask us to go online.
    925 	 */
    926 	if (stmf_register_local_port(lport) != STMF_SUCCESS) {
    927 		iscsit_tgt_destroy(result);
    928 		return (NULL);
    929 	}
    930 	result->target_stmf_lport_registered = 1;
    931 
    932 	return (result);
    933 }
    934 
    935 static idm_status_t
    936 iscsit_tgt_modify(iscsit_tgt_t *tgt, it_tgt_t *cfg_tgt)
    937 {
    938 	idm_status_t	idmrc = IDM_STATUS_SUCCESS;
    939 	list_t		tpgt_del_list;
    940 
    941 	/* Merge TPGT */
    942 	list_create(&tpgt_del_list, sizeof (iscsit_tpgt_t),
    943 	    offsetof(iscsit_tpgt_t, tpgt_delete_ln));
    944 
    945 	mutex_enter(&tgt->target_mutex);
    946 	if (tgt->target_props) {
    947 		nvlist_free(tgt->target_props);
    948 		tgt->target_props = NULL;
    949 	}
    950 	(void) nvlist_dup(cfg_tgt->tgt_properties, &tgt->target_props,
    951 	    KM_SLEEP);
    952 
    953 	if ((idmrc = iscsit_tgt_merge_tpgt(tgt, cfg_tgt, &tpgt_del_list)) !=
    954 	    IDM_STATUS_SUCCESS) {
    955 		/* This should never happen */
    956 		cmn_err(CE_WARN, "Fail to configure TPGTs for "
    957 		    "target %s, the target modification could not be "
    958 		    "completed.", tgt->target_name);
    959 	}
    960 
    961 	mutex_exit(&tgt->target_mutex);
    962 
    963 	iscsit_config_destroy_tpgts(&tpgt_del_list);
    964 
    965 	/*
    966 	 * If the target is truly modified (not newly created),
    967 	 * inform iSNS to update the target registration.
    968 	 */
    969 	if ((tgt->target_generation > 0) &&
    970 	    (cfg_tgt->tgt_generation > tgt->target_generation)) {
    971 		iscsit_isns_target_update(tgt);
    972 	}
    973 
    974 	tgt->target_generation = cfg_tgt->tgt_generation;
    975 
    976 	return (idmrc);
    977 }
    978 
    979 void
    980 iscsit_config_destroy_tpgts(list_t *tpgt_del_list)
    981 {
    982 	iscsit_tpgt_t	*tpgt, *next_tpgt;
    983 
    984 	for (tpgt = list_head(tpgt_del_list);
    985 	    tpgt != NULL;
    986 	    tpgt = next_tpgt) {
    987 		next_tpgt = list_next(tpgt_del_list, tpgt);
    988 
    989 		list_remove(tpgt_del_list, tpgt);
    990 		idm_refcnt_wait_ref(&tpgt->tpgt_refcnt);
    991 		iscsit_tpgt_destroy(tpgt);
    992 	}
    993 }
    994 
    995 void
    996 iscsit_tgt_unref(void *tgt_void)
    997 {
    998 	iscsit_tgt_t	*tgt = tgt_void;
    999 
   1000 	ISCSIT_GLOBAL_LOCK(RW_WRITER);
   1001 	list_remove(&iscsit_global.global_deleted_target_list, tgt);
   1002 	ISCSIT_GLOBAL_UNLOCK();
   1003 	iscsit_tgt_destroy(tgt);
   1004 }
   1005 
   1006 void
   1007 iscsit_tgt_async_wait_ref(iscsit_tgt_t *tgt, idm_refcnt_cb_t *cb_func)
   1008 {
   1009 	idm_refcnt_async_wait_ref(&tgt->target_refcnt, cb_func);
   1010 }
   1011 
   1012 static void
   1013 iscsit_tgt_destroy(iscsit_tgt_t *tgt)
   1014 {
   1015 	iscsit_tpgt_t *tpgt, *next_tpgt;
   1016 
   1017 	ASSERT(tgt->target_state == TS_DELETING ||
   1018 	    (tgt->target_state == TS_CREATED &&
   1019 	    tgt->target_stmf_lport_registered == 0));
   1020 
   1021 	/*
   1022 	 * Destroy all target portal group tags
   1023 	 */
   1024 	mutex_enter(&tgt->target_mutex);
   1025 	for (tpgt = avl_first(&tgt->target_tpgt_list);
   1026 	    tpgt != NULL;
   1027 	    tpgt = next_tpgt) {
   1028 		next_tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt);
   1029 		avl_remove(&tgt->target_tpgt_list, tpgt);
   1030 		iscsit_tpgt_destroy(tpgt);
   1031 	}
   1032 
   1033 	if (tgt->target_props) {
   1034 		nvlist_free(tgt->target_props);
   1035 	}
   1036 	mutex_exit(&tgt->target_mutex);
   1037 
   1038 	/*
   1039 	 * Destroy target
   1040 	 */
   1041 	idm_refcnt_destroy(&tgt->target_sess_refcnt);
   1042 	idm_refcnt_destroy(&tgt->target_refcnt);
   1043 	list_destroy(&tgt->target_events);
   1044 	avl_destroy(&tgt->target_tpgt_list);
   1045 	avl_destroy(&tgt->target_sess_list);
   1046 	mutex_destroy(&tgt->target_mutex);
   1047 	stmf_free(tgt->target_stmf_lport); /* Also frees "tgt' */
   1048 	iscsit_global_rele();
   1049 }
   1050 
   1051 void
   1052 iscsit_tgt_hold(iscsit_tgt_t *tgt)
   1053 {
   1054 	idm_refcnt_hold(&tgt->target_refcnt);
   1055 }
   1056 
   1057 void
   1058 iscsit_tgt_rele(iscsit_tgt_t *tgt)
   1059 {
   1060 	idm_refcnt_rele(&tgt->target_refcnt);
   1061 }
   1062 
   1063 int
   1064 iscsit_tgt_avl_compare(const void *void_tgt1, const void *void_tgt2)
   1065 {
   1066 	const iscsit_tgt_t	*tgt1 = void_tgt1;
   1067 	const iscsit_tgt_t	*tgt2 = void_tgt2;
   1068 	int 			result;
   1069 
   1070 	/*
   1071 	 * Sort by ISID first then TSIH
   1072 	 */
   1073 	result = strcmp(tgt1->target_name, tgt2->target_name);
   1074 	if (result < 0) {
   1075 		return (-1);
   1076 	} else if (result > 0) {
   1077 		return (1);
   1078 	}
   1079 
   1080 	return (0);
   1081 }
   1082 
   1083 
   1084 iscsit_tpgt_t *
   1085 iscsit_tgt_lookup_tpgt(iscsit_tgt_t *tgt, uint16_t tag)
   1086 {
   1087 	iscsit_tpgt_t *result;
   1088 
   1089 	mutex_enter(&tgt->target_mutex);
   1090 	result = iscsit_tgt_lookup_tpgt_locked(tgt, tag);
   1091 	mutex_exit(&tgt->target_mutex);
   1092 
   1093 	return (result);
   1094 }
   1095 
   1096 static iscsit_tpgt_t *
   1097 iscsit_tgt_lookup_tpgt_locked(iscsit_tgt_t *tgt, uint16_t tag)
   1098 {
   1099 	iscsit_tpgt_t	tmp_tpgt;
   1100 	iscsit_tpgt_t	*result;
   1101 
   1102 	/* Caller holds tgt->target_mutex */
   1103 	tmp_tpgt.tpgt_tag = tag;
   1104 	if ((result = avl_find(&tgt->target_tpgt_list, &tmp_tpgt, NULL)) !=
   1105 	    NULL) {
   1106 		iscsit_tpgt_hold(result);
   1107 	}
   1108 
   1109 	return (result);
   1110 }
   1111 
   1112 iscsit_portal_t *
   1113 iscsit_tgt_lookup_portal(iscsit_tgt_t *tgt, struct sockaddr_storage *sa,
   1114     iscsit_tpgt_t **output_tpgt)
   1115 {
   1116 	iscsit_tpgt_t 	*tpgt;
   1117 	iscsit_portal_t	*portal;
   1118 
   1119 	/* Caller holds tgt->target_mutex */
   1120 	ASSERT(mutex_owned(&tgt->target_mutex));
   1121 	for (tpgt = avl_first(&tgt->target_tpgt_list);
   1122 	    tpgt != NULL;
   1123 	    tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt)) {
   1124 		portal = iscsit_tpg_portal_lookup(tpgt->tpgt_tpg, sa);
   1125 		if (portal) {
   1126 			iscsit_tpgt_hold(tpgt);
   1127 			*output_tpgt = tpgt;
   1128 			return (portal);
   1129 		}
   1130 	}
   1131 
   1132 	return (NULL);
   1133 }
   1134 
   1135 
   1136 void
   1137 iscsit_tgt_bind_sess(iscsit_tgt_t *tgt, iscsit_sess_t *sess)
   1138 {
   1139 	if (tgt) {
   1140 		sess->ist_lport = tgt->target_stmf_lport;
   1141 		iscsit_tgt_hold(tgt);
   1142 		idm_refcnt_hold(&tgt->target_sess_refcnt);
   1143 		mutex_enter(&tgt->target_mutex);
   1144 		avl_add(&tgt->target_sess_list, sess);
   1145 		mutex_exit(&tgt->target_mutex);
   1146 	} else {
   1147 		/* Discovery session */
   1148 		sess->ist_lport = NULL;
   1149 		ISCSIT_GLOBAL_LOCK(RW_WRITER);
   1150 		avl_add(&iscsit_global.global_discovery_sessions, sess);
   1151 		ISCSIT_GLOBAL_UNLOCK();
   1152 	}
   1153 }
   1154 
   1155 void
   1156 iscsit_tgt_unbind_sess(iscsit_tgt_t *tgt, iscsit_sess_t *sess)
   1157 {
   1158 	if (tgt) {
   1159 		mutex_enter(&tgt->target_mutex);
   1160 		avl_remove(&tgt->target_sess_list, sess);
   1161 		mutex_exit(&tgt->target_mutex);
   1162 		sess->ist_tgt = (iscsit_tgt_t *)SESS_UNBOUND_FROM_TGT;
   1163 		idm_refcnt_rele(&tgt->target_sess_refcnt);
   1164 		iscsit_tgt_rele(tgt);
   1165 	} else {
   1166 		/* Discovery session */
   1167 		ISCSIT_GLOBAL_LOCK(RW_WRITER);
   1168 		avl_remove(&iscsit_global.global_discovery_sessions, sess);
   1169 		ISCSIT_GLOBAL_UNLOCK();
   1170 	}
   1171 }
   1172 
   1173 #define	LOCK_FOR_SESS_LOOKUP(lookup_tgt) { 			\
   1174 	if ((lookup_tgt) == NULL) {				\
   1175 		ISCSIT_GLOBAL_LOCK(RW_READER);			\
   1176 	} else {						\
   1177 		mutex_enter(&(lookup_tgt)->target_mutex);	\
   1178 	}							\
   1179 }
   1180 
   1181 #define	UNLOCK_FOR_SESS_LOOKUP(lookup_tgt) { 			\
   1182 	if ((lookup_tgt) == NULL) {				\
   1183 		ISCSIT_GLOBAL_UNLOCK();				\
   1184 	} else {					 	\
   1185 		mutex_exit(&(lookup_tgt)->target_mutex); 	\
   1186 	}							\
   1187 }
   1188 
   1189 iscsit_sess_t *
   1190 iscsit_tgt_lookup_sess(iscsit_tgt_t *tgt, char *initiator_name,
   1191     uint8_t *isid, uint16_t tsih, uint16_t tag)
   1192 {
   1193 	iscsit_sess_t	tmp_sess;
   1194 	avl_tree_t	*sess_avl;
   1195 	avl_index_t	where;
   1196 	iscsit_sess_t	*result;
   1197 
   1198 	/*
   1199 	 * If tgt is NULL then we are looking for a discovery session
   1200 	 */
   1201 	if (tgt == NULL) {
   1202 		sess_avl = &iscsit_global.global_discovery_sessions;
   1203 	} else {
   1204 		sess_avl = &tgt->target_sess_list;
   1205 	}
   1206 
   1207 	LOCK_FOR_SESS_LOOKUP(tgt);
   1208 	if (avl_numnodes(sess_avl) == NULL) {
   1209 		UNLOCK_FOR_SESS_LOOKUP(tgt);
   1210 		return (NULL);
   1211 	}
   1212 
   1213 	/*
   1214 	 * We'll try to find a session matching ISID + TSIH first.  If we
   1215 	 * can't find one then we will return the closest match.  If the
   1216 	 * caller needs an exact match it must compare the TSIH after
   1217 	 * the session is returned.
   1218 	 *
   1219 	 * The reason we do this "fuzzy matching" is to allow matching
   1220 	 * sessions with different TSIH values on the same AVL list.  This
   1221 	 * makes session reinstatement much easier since the new session can
   1222 	 * live on the list at the same time as the old session is cleaning up.
   1223 	 */
   1224 	bcopy(isid, tmp_sess.ist_isid, ISCSI_ISID_LEN);
   1225 	tmp_sess.ist_initiator_name = initiator_name;
   1226 	tmp_sess.ist_tsih = tsih;
   1227 	tmp_sess.ist_tpgt_tag = tag;
   1228 
   1229 	result = avl_find(sess_avl, &tmp_sess, &where);
   1230 	if (result != NULL) {
   1231 		iscsit_sess_hold(result);
   1232 		UNLOCK_FOR_SESS_LOOKUP(tgt);
   1233 		return (result);
   1234 	}
   1235 
   1236 	/*
   1237 	 * avl_find_nearest() may return a result with a different ISID so
   1238 	 * we should only return a result if the name and ISID match
   1239 	 */
   1240 	result = avl_nearest(sess_avl, where, AVL_BEFORE);
   1241 	if ((result != NULL) &&
   1242 	    (strcmp(result->ist_initiator_name, initiator_name) == 0) &&
   1243 	    (memcmp(result->ist_isid, isid, ISCSI_ISID_LEN) == 0) &&
   1244 	    (result->ist_tpgt_tag == tag)) {
   1245 		iscsit_sess_hold(result);
   1246 		UNLOCK_FOR_SESS_LOOKUP(tgt);
   1247 		return (result);
   1248 	}
   1249 
   1250 	result = avl_nearest(sess_avl, where, AVL_AFTER);
   1251 	if ((result != NULL) &&
   1252 	    (strcmp(result->ist_initiator_name, initiator_name) == 0) &&
   1253 	    (memcmp(result->ist_isid, isid, ISCSI_ISID_LEN) == 0) &&
   1254 	    (result->ist_tpgt_tag == tag)) {
   1255 		iscsit_sess_hold(result);
   1256 		UNLOCK_FOR_SESS_LOOKUP(tgt);
   1257 		return (result);
   1258 	}
   1259 
   1260 	UNLOCK_FOR_SESS_LOOKUP(tgt);
   1261 
   1262 	return (NULL);
   1263 }
   1264 
   1265 static idm_status_t
   1266 iscsit_tgt_merge_tpgt(iscsit_tgt_t *tgt, it_tgt_t *cfg_tgt,
   1267     list_t *tpgt_del_list)
   1268 {
   1269 	iscsit_tpgt_t	*tpgt, *next_tpgt;
   1270 	it_tpgt_t	*cfg_tpgt;
   1271 
   1272 	/*
   1273 	 * 1. >> Lock <<
   1274 	 * 2. Removing all objects and place on a temp list
   1275 	 * 3. Add new objects
   1276 	 * 4. >> Unlock <<
   1277 	 * 5. tpgt_del_list contains deleted objects
   1278 	 */
   1279 	ASSERT(avl_is_empty(&tgt->target_tpgt_list) ||
   1280 	    (tpgt_del_list != NULL));
   1281 
   1282 	if (tpgt_del_list) {
   1283 		for (tpgt = avl_first(&tgt->target_tpgt_list);
   1284 		    tpgt != NULL; tpgt = next_tpgt) {
   1285 			next_tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt);
   1286 			avl_remove(&tgt->target_tpgt_list, tpgt);
   1287 			if (tgt->target_state == TS_STMF_ONLINE) {
   1288 				tpgt->tpgt_needs_tpg_offline = B_TRUE;
   1289 			}
   1290 			list_insert_tail(tpgt_del_list, tpgt);
   1291 		}
   1292 	}
   1293 
   1294 	if (cfg_tgt->tgt_tpgt_list == NULL) {
   1295 		tpgt = iscsit_tpgt_create_default();
   1296 		if (tgt->target_state == TS_STMF_ONLINE) {
   1297 			(void) iscsit_tpg_online(tpgt->tpgt_tpg);
   1298 		}
   1299 		avl_add(&tgt->target_tpgt_list, tpgt);
   1300 	} else {
   1301 		/* Add currently defined TPGTs */
   1302 		for (cfg_tpgt = cfg_tgt->tgt_tpgt_list;
   1303 		    cfg_tpgt != NULL;
   1304 		    cfg_tpgt = cfg_tpgt->tpgt_next) {
   1305 			tpgt = iscsit_tpgt_create(cfg_tpgt);
   1306 			if (tpgt == NULL)
   1307 				goto tpgt_merge_error;
   1308 			if (tgt->target_state == TS_STMF_ONLINE) {
   1309 				(void) iscsit_tpg_online(tpgt->tpgt_tpg);
   1310 			}
   1311 			avl_add(&tgt->target_tpgt_list, tpgt);
   1312 		}
   1313 	}
   1314 
   1315 	return (IDM_STATUS_SUCCESS);
   1316 
   1317 tpgt_merge_error:
   1318 	/*
   1319 	 * This is a problem in configuration we received so it should never
   1320 	 * happen.  That's good because we're probably no longer in a state
   1321 	 * requested by the user -- all the unbind operations have already
   1322 	 * taken place.
   1323 	 */
   1324 	return (IDM_STATUS_FAIL);
   1325 }
   1326 
   1327 static iscsit_tpgt_t *
   1328 iscsit_tpgt_create(it_tpgt_t *cfg_tpgt)
   1329 {
   1330 	iscsit_tpg_t	*tpg;
   1331 	iscsit_tpgt_t	*result;
   1332 
   1333 	/* This takes a reference on the TPG */
   1334 	tpg = iscsit_tpg_lookup_locked(cfg_tpgt->tpgt_tpg_name);
   1335 	if (tpg == NULL)
   1336 		return (NULL);
   1337 
   1338 	result = kmem_zalloc(sizeof (*result), KM_SLEEP);
   1339 
   1340 	result->tpgt_tpg = tpg;
   1341 	result->tpgt_tag = cfg_tpgt->tpgt_tag;
   1342 
   1343 	return (result);
   1344 }
   1345 
   1346 iscsit_tpgt_t *
   1347 iscsit_tpgt_create_default()
   1348 {
   1349 	iscsit_tpgt_t	*result;
   1350 
   1351 	result = kmem_zalloc(sizeof (*result), KM_SLEEP);
   1352 
   1353 	result->tpgt_tpg = iscsit_global.global_default_tpg;
   1354 	iscsit_tpg_hold(result->tpgt_tpg);
   1355 	result->tpgt_tag = ISCSIT_DEFAULT_TPGT;
   1356 
   1357 	return (result);
   1358 }
   1359 
   1360 void
   1361 iscsit_tpgt_destroy(iscsit_tpgt_t *tpgt)
   1362 {
   1363 	if (tpgt->tpgt_needs_tpg_offline) {
   1364 		iscsit_tpg_offline(tpgt->tpgt_tpg);
   1365 	}
   1366 	iscsit_tpg_rele(tpgt->tpgt_tpg);
   1367 	kmem_free(tpgt, sizeof (*tpgt));
   1368 }
   1369 
   1370 void
   1371 iscsit_tpgt_hold(iscsit_tpgt_t *tpgt)
   1372 {
   1373 	idm_refcnt_hold(&tpgt->tpgt_refcnt);
   1374 }
   1375 
   1376 void
   1377 iscsit_tpgt_rele(iscsit_tpgt_t *tpgt)
   1378 {
   1379 	idm_refcnt_rele(&tpgt->tpgt_refcnt);
   1380 }
   1381 
   1382 int
   1383 iscsit_tpgt_avl_compare(const void *void_tpgt1, const void *void_tpgt2)
   1384 {
   1385 	const iscsit_tpgt_t	*tpgt1 = void_tpgt1;
   1386 	const iscsit_tpgt_t	*tpgt2 = void_tpgt2;
   1387 
   1388 	if (tpgt1->tpgt_tag < tpgt2->tpgt_tag)
   1389 		return (-1);
   1390 	else if (tpgt1->tpgt_tag > tpgt2->tpgt_tag)
   1391 		return (1);
   1392 
   1393 	return (0);
   1394 }
   1395 
   1396 static idm_status_t
   1397 iscsit_tgt_online(iscsit_tgt_t *tgt)
   1398 {
   1399 	iscsit_tpgt_t 		*tpgt, *tpgt_fail;
   1400 	idm_status_t		rc;
   1401 
   1402 	mutex_enter(&tgt->target_mutex);
   1403 
   1404 	ASSERT(tgt->target_sess_list.avl_numnodes == 0);
   1405 	idm_refcnt_reset(&tgt->target_sess_refcnt);
   1406 	for (tpgt = avl_first(&tgt->target_tpgt_list);
   1407 	    tpgt != NULL;
   1408 	    tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt)) {
   1409 		rc = iscsit_tpg_online(tpgt->tpgt_tpg);
   1410 		if (rc != IDM_STATUS_SUCCESS) {
   1411 			tpgt_fail = tpgt;
   1412 			goto tgt_online_fail;
   1413 		}
   1414 	}
   1415 
   1416 	mutex_exit(&tgt->target_mutex);
   1417 
   1418 	return (IDM_STATUS_SUCCESS);
   1419 
   1420 tgt_online_fail:
   1421 	/* Offline all the tpgs we successfully onlined up to the failure */
   1422 	for (tpgt = avl_first(&tgt->target_tpgt_list);
   1423 	    tpgt != tpgt_fail;
   1424 	    tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt)) {
   1425 		iscsit_tpg_offline(tpgt->tpgt_tpg);
   1426 	}
   1427 	mutex_exit(&tgt->target_mutex);
   1428 	return (rc);
   1429 }
   1430 
   1431 static void
   1432 iscsit_tgt_offline_cb(void *tgt_void)
   1433 {
   1434 	iscsit_tgt_t *tgt = tgt_void;
   1435 	stmf_change_status_t	scs;
   1436 
   1437 	iscsit_tgt_sm_event(tgt, TE_OFFLINE_COMPLETE);
   1438 
   1439 	scs.st_completion_status = STMF_SUCCESS;
   1440 	scs.st_additional_info = NULL;
   1441 	(void) stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE,
   1442 	    tgt->target_stmf_lport, &scs);
   1443 }
   1444 
   1445 static void
   1446 iscsit_tgt_offline(iscsit_tgt_t *tgt)
   1447 {
   1448 	iscsit_tpgt_t 		*tpgt;
   1449 	iscsit_sess_t		*ist;
   1450 
   1451 	mutex_enter(&tgt->target_mutex);
   1452 
   1453 	/* Offline target portal groups */
   1454 	for (tpgt = avl_first(&tgt->target_tpgt_list);
   1455 	    tpgt != NULL;
   1456 	    tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt)) {
   1457 		iscsit_tpg_offline(tpgt->tpgt_tpg);
   1458 	}
   1459 
   1460 	/* Close any active sessions */
   1461 	for (ist = avl_first(&tgt->target_sess_list);
   1462 	    ist != NULL;
   1463 	    ist = AVL_NEXT(&tgt->target_sess_list, ist)) {
   1464 		/*
   1465 		 * This is not a synchronous operation but after all
   1466 		 * sessions have been cleaned up there will be no
   1467 		 * more session-related holds on the target.
   1468 		 */
   1469 		iscsit_sess_close(ist);
   1470 	}
   1471 
   1472 	mutex_exit(&tgt->target_mutex);
   1473 
   1474 	/*
   1475 	 * Wait for all the sessions to quiesce.
   1476 	 */
   1477 	idm_refcnt_async_wait_ref(&tgt->target_sess_refcnt,
   1478 	    &iscsit_tgt_offline_cb);
   1479 }
   1480 
   1481 it_cfg_status_t
   1482 iscsit_config_merge_tpg(it_config_t *cfg, list_t *tpg_del_list)
   1483 {
   1484 	it_tpg_t	*cfg_tpg;
   1485 	iscsit_tpg_t	*tpg, *next_tpg;
   1486 
   1487 	/*
   1488 	 * 1. >> Lock <<
   1489 	 * 2. Removing deleted objects and place on a temp list
   1490 	 * 3. Add new objects
   1491 	 * 4. >> Unlock <<
   1492 	 * 5. tpg_del_list contains objects to destroy
   1493 	 */
   1494 	for (tpg = avl_first(&iscsit_global.global_tpg_list);
   1495 	    tpg != NULL;
   1496 	    tpg = next_tpg) {
   1497 		next_tpg = AVL_NEXT(&iscsit_global.global_tpg_list, tpg);
   1498 
   1499 		if (it_tpg_lookup(cfg, tpg->tpg_name) == NULL) {
   1500 			/*
   1501 			 * The policy around when to allow a target portal
   1502 			 * group to be deleted is implemented in libiscsit.
   1503 			 * By the time the request gets to the kernel module
   1504 			 * we expect that it conforms to policy so we will
   1505 			 * cleanup all references to TPG and destroy it if it
   1506 			 * is possible to do so.
   1507 			 *
   1508 			 */
   1509 			avl_remove(&iscsit_global.global_tpg_list, tpg);
   1510 			list_insert_tail(tpg_del_list, tpg);
   1511 		}
   1512 	}
   1513 
   1514 	/* Now walk through the list of configured target portal groups */
   1515 	for (cfg_tpg = cfg->config_tpg_list;
   1516 	    cfg_tpg != NULL;
   1517 	    cfg_tpg = cfg_tpg->tpg_next) {
   1518 		/* See if we have an existing target portal group */
   1519 		tpg = iscsit_tpg_lookup_locked(cfg_tpg->tpg_name);
   1520 
   1521 		if (tpg == NULL) {
   1522 			tpg = iscsit_tpg_create(cfg_tpg);
   1523 			ASSERT(tpg != NULL);
   1524 			avl_add(&iscsit_global.global_tpg_list, tpg);
   1525 		} else {
   1526 			mutex_enter(&tpg->tpg_mutex);
   1527 			iscsit_tpg_modify(tpg, cfg_tpg);
   1528 			mutex_exit(&tpg->tpg_mutex);
   1529 			iscsit_tpg_rele(tpg);
   1530 		}
   1531 	}
   1532 
   1533 	return (ITCFG_SUCCESS);
   1534 }
   1535 
   1536 
   1537 void
   1538 iscsit_config_destroy_tpgs(list_t *tpg_del_list)
   1539 {
   1540 	iscsit_tpg_t	*tpg, *next_tpg;
   1541 
   1542 	/* Now finish destroying the target portal groups */
   1543 	for (tpg = list_head(tpg_del_list);
   1544 	    tpg != NULL;
   1545 	    tpg = next_tpg) {
   1546 		next_tpg = list_next(tpg_del_list, tpg);
   1547 		list_remove(tpg_del_list, tpg);
   1548 		idm_refcnt_wait_ref(&tpg->tpg_refcnt);
   1549 
   1550 		/* Kill it */
   1551 		iscsit_tpg_destroy(tpg);
   1552 	}
   1553 }
   1554 
   1555 iscsit_tpg_t *
   1556 iscsit_tpg_lookup(char *tpg_name)
   1557 {
   1558 	iscsit_tpg_t *result;
   1559 
   1560 	ISCSIT_GLOBAL_LOCK(RW_READER);
   1561 	result = iscsit_tpg_lookup_locked(tpg_name);
   1562 	ISCSIT_GLOBAL_UNLOCK();
   1563 
   1564 	return (result);
   1565 }
   1566 
   1567 static iscsit_tpg_t *
   1568 iscsit_tpg_lookup_locked(char *tpg_name)
   1569 {
   1570 	iscsit_tpg_t	tmp_tpg;
   1571 	iscsit_tpg_t	*result;
   1572 
   1573 	(void) strlcpy(tmp_tpg.tpg_name, tpg_name, MAX_ISCSI_NODENAMELEN);
   1574 	if ((result = avl_find(&iscsit_global.global_tpg_list,
   1575 	    &tmp_tpg, NULL)) != NULL) {
   1576 		iscsit_tpg_hold(result);
   1577 	}
   1578 
   1579 	return (result);
   1580 }
   1581 
   1582 iscsit_tpg_t *
   1583 iscsit_tpg_create(it_tpg_t *cfg_tpg)
   1584 {
   1585 	iscsit_tpg_t *tpg;
   1586 
   1587 	tpg = kmem_zalloc(sizeof (*tpg), KM_SLEEP);
   1588 
   1589 	mutex_init(&tpg->tpg_mutex, NULL, MUTEX_DEFAULT, NULL);
   1590 	(void) strlcpy(tpg->tpg_name, cfg_tpg->tpg_name, MAX_TPG_NAMELEN);
   1591 	avl_create(&tpg->tpg_portal_list, iscsit_portal_avl_compare,
   1592 	    sizeof (iscsit_portal_t), offsetof(iscsit_portal_t, portal_tpg_ln));
   1593 	idm_refcnt_init(&tpg->tpg_refcnt, tpg);
   1594 
   1595 	mutex_enter(&tpg->tpg_mutex);
   1596 	iscsit_tpg_modify(tpg, cfg_tpg);
   1597 	mutex_exit(&tpg->tpg_mutex);
   1598 	iscsit_global_hold();
   1599 
   1600 	return (tpg);
   1601 }
   1602 
   1603 static void
   1604 iscsit_tpg_modify(iscsit_tpg_t *tpg, it_tpg_t *cfg_tpg)
   1605 {
   1606 	iscsit_portal_t		*portal, *next_portal;
   1607 	it_portal_t		*cfg_portal;
   1608 
   1609 	/* Update portals */
   1610 	for (portal = avl_first(&tpg->tpg_portal_list);
   1611 	    portal != NULL;
   1612 	    portal = next_portal) {
   1613 		next_portal = AVL_NEXT(&tpg->tpg_portal_list, portal);
   1614 		if (it_portal_lookup(cfg_tpg, &portal->portal_addr) == NULL) {
   1615 			avl_remove(&tpg->tpg_portal_list, portal);
   1616 			iscsit_portal_destroy(portal);
   1617 		}
   1618 	}
   1619 
   1620 	for (cfg_portal = cfg_tpg->tpg_portal_list;
   1621 	    cfg_portal != NULL;
   1622 	    cfg_portal = cfg_portal->next) {
   1623 		if ((portal = iscsit_tpg_portal_lookup_locked(tpg,
   1624 		    &cfg_portal->portal_addr)) == NULL) {
   1625 			(void) iscsit_portal_create(tpg,
   1626 			    &cfg_portal->portal_addr);
   1627 		} else {
   1628 			iscsit_portal_rele(portal);
   1629 		}
   1630 	}
   1631 }
   1632 
   1633 void
   1634 iscsit_tpg_destroy(iscsit_tpg_t *tpg)
   1635 {
   1636 	iscsit_portal_t *portal, *next_portal;
   1637 
   1638 	for (portal = avl_first(&tpg->tpg_portal_list);
   1639 	    portal != NULL;
   1640 	    portal = next_portal) {
   1641 		next_portal = AVL_NEXT(&tpg->tpg_portal_list, portal);
   1642 		avl_remove(&tpg->tpg_portal_list, portal);
   1643 		iscsit_portal_destroy(portal);
   1644 	}
   1645 
   1646 	idm_refcnt_wait_ref(&tpg->tpg_refcnt);
   1647 	idm_refcnt_destroy(&tpg->tpg_refcnt);
   1648 	avl_destroy(&tpg->tpg_portal_list);
   1649 	mutex_destroy(&tpg->tpg_mutex);
   1650 	kmem_free(tpg, sizeof (*tpg));
   1651 	iscsit_global_rele();
   1652 }
   1653 
   1654 void
   1655 iscsit_tpg_hold(iscsit_tpg_t *tpg)
   1656 {
   1657 	idm_refcnt_hold(&tpg->tpg_refcnt);
   1658 }
   1659 
   1660 void
   1661 iscsit_tpg_rele(iscsit_tpg_t *tpg)
   1662 {
   1663 	idm_refcnt_rele(&tpg->tpg_refcnt);
   1664 }
   1665 
   1666 iscsit_tpg_t *
   1667 iscsit_tpg_createdefault()
   1668 {
   1669 	iscsit_tpg_t *tpg;
   1670 
   1671 	tpg = kmem_zalloc(sizeof (*tpg), KM_SLEEP);
   1672 
   1673 	mutex_init(&tpg->tpg_mutex, NULL, MUTEX_DEFAULT, NULL);
   1674 	(void) strlcpy(tpg->tpg_name, ISCSIT_DEFAULT_TPG, MAX_TPG_NAMELEN);
   1675 	avl_create(&tpg->tpg_portal_list, iscsit_portal_avl_compare,
   1676 	    sizeof (iscsit_portal_t), offsetof(iscsit_portal_t, portal_tpg_ln));
   1677 	idm_refcnt_init(&tpg->tpg_refcnt, tpg);
   1678 
   1679 	/* Now create default portal */
   1680 	if (iscsit_portal_create(tpg, NULL) == NULL) {
   1681 		iscsit_tpg_destroy(tpg);
   1682 		return (NULL);
   1683 	}
   1684 
   1685 	return (tpg);
   1686 }
   1687 
   1688 void
   1689 iscsit_tpg_destroydefault(iscsit_tpg_t *tpg)
   1690 {
   1691 	iscsit_portal_t *portal;
   1692 
   1693 	portal = avl_first(&tpg->tpg_portal_list);
   1694 	ASSERT(portal != NULL);
   1695 	avl_remove(&tpg->tpg_portal_list, portal);
   1696 	iscsit_portal_destroy(portal);
   1697 
   1698 	idm_refcnt_wait_ref(&tpg->tpg_refcnt);
   1699 	idm_refcnt_destroy(&tpg->tpg_refcnt);
   1700 	avl_destroy(&tpg->tpg_portal_list);
   1701 	mutex_destroy(&tpg->tpg_mutex);
   1702 	kmem_free(tpg, sizeof (*tpg));
   1703 }
   1704 
   1705 int
   1706 iscsit_tpg_avl_compare(const void *void_tpg1, const void *void_tpg2)
   1707 {
   1708 	const iscsit_tpg_t	*tpg1 = void_tpg1;
   1709 	const iscsit_tpg_t	*tpg2 = void_tpg2;
   1710 	int 			result;
   1711 
   1712 	/*
   1713 	 * Sort by ISID first then TSIH
   1714 	 */
   1715 	result = strcmp(tpg1->tpg_name, tpg2->tpg_name);
   1716 	if (result < 0) {
   1717 		return (-1);
   1718 	} else if (result > 0) {
   1719 		return (1);
   1720 	}
   1721 
   1722 	return (0);
   1723 }
   1724 
   1725 idm_status_t
   1726 iscsit_tpg_online(iscsit_tpg_t *tpg)
   1727 {
   1728 	iscsit_portal_t *portal, *portal_fail;
   1729 	idm_status_t	rc;
   1730 
   1731 	mutex_enter(&tpg->tpg_mutex);
   1732 	if (tpg->tpg_online == 0) {
   1733 		for (portal = avl_first(&tpg->tpg_portal_list);
   1734 		    portal != NULL;
   1735 		    portal = AVL_NEXT(&tpg->tpg_portal_list, portal)) {
   1736 			rc = iscsit_portal_online(portal);
   1737 			if (rc != IDM_STATUS_SUCCESS) {
   1738 				portal_fail = portal;
   1739 				goto tpg_online_fail;
   1740 			}
   1741 		}
   1742 	}
   1743 	tpg->tpg_online++;
   1744 
   1745 	mutex_exit(&tpg->tpg_mutex);
   1746 	return (IDM_STATUS_SUCCESS);
   1747 
   1748 tpg_online_fail:
   1749 	/* Offline all the portals we successfully onlined up to the failure */
   1750 	for (portal = avl_first(&tpg->tpg_portal_list);
   1751 	    portal != portal_fail;
   1752 	    portal = AVL_NEXT(&tpg->tpg_portal_list, portal)) {
   1753 		iscsit_portal_offline(portal);
   1754 	}
   1755 	mutex_exit(&tpg->tpg_mutex);
   1756 	return (rc);
   1757 }
   1758 
   1759 void
   1760 iscsit_tpg_offline(iscsit_tpg_t *tpg)
   1761 {
   1762 	iscsit_portal_t *portal;
   1763 
   1764 	mutex_enter(&tpg->tpg_mutex);
   1765 	tpg->tpg_online--;
   1766 	if (tpg->tpg_online == 0) {
   1767 		for (portal = avl_first(&tpg->tpg_portal_list);
   1768 		    portal != NULL;
   1769 		    portal = AVL_NEXT(&tpg->tpg_portal_list, portal)) {
   1770 			iscsit_portal_offline(portal);
   1771 		}
   1772 	}
   1773 	mutex_exit(&tpg->tpg_mutex);
   1774 }
   1775 
   1776 iscsit_portal_t *
   1777 iscsit_tpg_portal_lookup(iscsit_tpg_t *tpg, struct sockaddr_storage *sa)
   1778 {
   1779 	iscsit_portal_t	*result;
   1780 
   1781 	mutex_enter(&tpg->tpg_mutex);
   1782 	result = iscsit_tpg_portal_lookup_locked(tpg, sa);
   1783 	mutex_exit(&tpg->tpg_mutex);
   1784 
   1785 	return (result);
   1786 }
   1787 
   1788 static iscsit_portal_t *
   1789 iscsit_tpg_portal_lookup_locked(iscsit_tpg_t *tpg,
   1790     struct sockaddr_storage *sa)
   1791 {
   1792 	iscsit_portal_t	tmp_portal;
   1793 	iscsit_portal_t	*result;
   1794 
   1795 	/* Caller holds tpg->tpg_mutex */
   1796 	bcopy(sa, &tmp_portal.portal_addr, sizeof (*sa));
   1797 	if ((result = avl_find(&tpg->tpg_portal_list, &tmp_portal, NULL)) !=
   1798 	    NULL) {
   1799 		iscsit_portal_hold(result);
   1800 	}
   1801 
   1802 	return (result);
   1803 }
   1804 
   1805 iscsit_portal_t *
   1806 iscsit_portal_create(iscsit_tpg_t *tpg, struct sockaddr_storage *sa)
   1807 {
   1808 	iscsit_portal_t *portal;
   1809 
   1810 	portal = kmem_zalloc(sizeof (*portal), KM_SLEEP);
   1811 	/*
   1812 	 * If (sa == NULL) then we are being asked to create the default
   1813 	 * portal -- targets will use this portal when no portals are
   1814 	 * explicitly configured.
   1815 	 */
   1816 	if (sa == NULL) {
   1817 		portal->portal_default = B_TRUE;
   1818 	} else {
   1819 		portal->portal_default = B_FALSE;
   1820 		bcopy(sa, &portal->portal_addr, sizeof (*sa));
   1821 	}
   1822 
   1823 	idm_refcnt_init(&portal->portal_refcnt, portal);
   1824 
   1825 	/*
   1826 	 * Add this portal to the list
   1827 	 */
   1828 	avl_add(&tpg->tpg_portal_list, portal);
   1829 
   1830 	return (portal);
   1831 }
   1832 
   1833 void
   1834 iscsit_portal_destroy(iscsit_portal_t *portal)
   1835 {
   1836 	ASSERT(portal->portal_svc == NULL);
   1837 	idm_refcnt_destroy(&portal->portal_refcnt);
   1838 	kmem_free(portal, sizeof (*portal));
   1839 }
   1840 
   1841 void
   1842 iscsit_portal_hold(iscsit_portal_t *portal)
   1843 {
   1844 	idm_refcnt_hold(&portal->portal_refcnt);
   1845 }
   1846 
   1847 void
   1848 iscsit_portal_rele(iscsit_portal_t *portal)
   1849 {
   1850 	idm_refcnt_rele(&portal->portal_refcnt);
   1851 }
   1852 
   1853 int
   1854 iscsit_portal_avl_compare(const void *void_portal1, const void *void_portal2)
   1855 {
   1856 	const iscsit_portal_t			*portal1 = void_portal1;
   1857 	const iscsit_portal_t			*portal2 = void_portal2;
   1858 	const struct sockaddr_storage		*ss1, *ss2;
   1859 	const struct in_addr			*in1, *in2;
   1860 	const struct in6_addr			*in61, *in62;
   1861 	int i;
   1862 
   1863 	/*
   1864 	 * Compare ports, then address family, then ip address
   1865 	 */
   1866 	ss1 = &portal1->portal_addr;
   1867 	ss2 = &portal2->portal_addr;
   1868 	if (((struct sockaddr_in *)ss1)->sin_port !=
   1869 	    ((struct sockaddr_in *)ss2)->sin_port) {
   1870 		if (((struct sockaddr_in *)ss1)->sin_port >
   1871 		    ((struct sockaddr_in *)ss2)->sin_port)
   1872 			return (1);
   1873 		else
   1874 			return (-1);
   1875 	}
   1876 
   1877 	/*
   1878 	 * ports are the same
   1879 	 */
   1880 	if (ss1->ss_family != ss2->ss_family) {
   1881 		if (ss1->ss_family == AF_INET)
   1882 			return (1);
   1883 		else
   1884 			return (-1);
   1885 	}
   1886 	/*
   1887 	 * address families are the same
   1888 	 */
   1889 	if (ss1->ss_family == AF_INET) {
   1890 		in1 = &((struct sockaddr_in *)ss1)->sin_addr;
   1891 		in2 = &((struct sockaddr_in *)ss2)->sin_addr;
   1892 
   1893 		if (in1->s_addr > in2->s_addr)
   1894 			return (1);
   1895 		else if (in1->s_addr < in2->s_addr)
   1896 			return (-1);
   1897 		else
   1898 			return (0);
   1899 	} else if (ss1->ss_family == AF_INET6) {
   1900 		in61 = &((struct sockaddr_in6 *)ss1)->sin6_addr;
   1901 		in62 = &((struct sockaddr_in6 *)ss2)->sin6_addr;
   1902 
   1903 		for (i = 0; i < 4; i++) {
   1904 			if (in61->s6_addr32[i] > in62->s6_addr32[i])
   1905 				return (1);
   1906 			else if (in61->s6_addr32[i] < in62->s6_addr32[i])
   1907 				return (-1);
   1908 		}
   1909 		return (0);
   1910 	} else
   1911 		cmn_err(CE_WARN,
   1912 		    "iscsit_portal_avl_compare: unknown ss_family %d",
   1913 		    ss1->ss_family);
   1914 
   1915 	return (1);
   1916 }
   1917 
   1918 
   1919 idm_status_t
   1920 iscsit_portal_online(iscsit_portal_t *portal)
   1921 {
   1922 	idm_status_t rc = 0;
   1923 	idm_svc_t	*svc;
   1924 	idm_svc_req_t	sr;
   1925 	uint16_t	port;
   1926 	struct sockaddr_in *sin;
   1927 
   1928 	/* Caller holds parent TPG mutex */
   1929 	if (portal->portal_online == 0) {
   1930 		/*
   1931 		 * If there is no existing IDM service instance for this port,
   1932 		 * create one.  If the service exists, then the lookup,
   1933 		 * creates a reference on the existing service.
   1934 		 */
   1935 		sin = (struct sockaddr_in *)&portal->portal_addr;
   1936 		port = ntohs(sin->sin_port);
   1937 		if (port == 0)
   1938 			port = ISCSI_LISTEN_PORT;
   1939 		ASSERT(portal->portal_svc == NULL);
   1940 		if ((svc = idm_tgt_svc_lookup(port)) == NULL) {
   1941 			sr.sr_port = port;
   1942 			sr.sr_li = iscsit_global.global_li;
   1943 			sr.sr_conn_ops.icb_rx_scsi_cmd = &iscsit_op_scsi_cmd;
   1944 			sr.sr_conn_ops.icb_rx_scsi_rsp = NULL;
   1945 			sr.sr_conn_ops.icb_rx_misc = &iscsit_rx_pdu;
   1946 			sr.sr_conn_ops.icb_rx_error = &iscsit_rx_pdu_error;
   1947 			sr.sr_conn_ops.icb_task_aborted = &iscsit_task_aborted;
   1948 			sr.sr_conn_ops.icb_client_notify =
   1949 			    &iscsit_client_notify;
   1950 			sr.sr_conn_ops.icb_build_hdr = &iscsit_build_hdr;
   1951 			sr.sr_conn_ops.icb_update_statsn =
   1952 			    &iscsit_update_statsn;
   1953 			sr.sr_conn_ops.icb_keepalive = &iscsit_keepalive;
   1954 
   1955 			if (idm_tgt_svc_create(&sr, &svc) !=
   1956 			    IDM_STATUS_SUCCESS) {
   1957 				return (IDM_STATUS_FAIL);
   1958 			}
   1959 
   1960 			/* Get reference on the service we just created */
   1961 			idm_tgt_svc_hold(svc);
   1962 		}
   1963 		if ((rc = idm_tgt_svc_online(svc)) != IDM_STATUS_SUCCESS) {
   1964 			idm_tgt_svc_rele_and_destroy(svc);
   1965 			return (IDM_STATUS_FAIL);
   1966 		}
   1967 		portal->portal_svc = svc;
   1968 
   1969 		/*
   1970 		 * Only call iSNS for first online
   1971 		 */
   1972 		iscsit_isns_portal_online(portal);
   1973 	}
   1974 
   1975 	portal->portal_online++;
   1976 
   1977 	return (rc);
   1978 }
   1979 
   1980 void
   1981 iscsit_portal_offline(iscsit_portal_t *portal)
   1982 {
   1983 	portal->portal_online--;
   1984 
   1985 	if (portal->portal_online == 0) {
   1986 		/*
   1987 		 * Only call iSNS for last offline
   1988 		 */
   1989 		iscsit_isns_portal_offline(portal);
   1990 		idm_tgt_svc_offline(portal->portal_svc);
   1991 		/* If service is unreferenced, destroy it too */
   1992 		idm_tgt_svc_rele_and_destroy(portal->portal_svc);
   1993 		portal->portal_svc = NULL;
   1994 	}
   1995 
   1996 }
   1997 
   1998 it_cfg_status_t
   1999 iscsit_config_merge_ini(it_config_t *cfg)
   2000 {
   2001 	iscsit_ini_t	*ini, *next_ini;
   2002 	it_ini_t	*cfg_ini;
   2003 
   2004 	/*
   2005 	 * Initiator objects are so simple we will just destroy all the current
   2006 	 * objects and build new ones.  Nothing should ever reference an
   2007 	 * initator object.. instead just lookup the initiator object and
   2008 	 * grab the properties while holding the global config lock.
   2009 	 */
   2010 	for (ini = avl_first(&iscsit_global.global_ini_list);
   2011 	    ini != NULL;
   2012 	    ini = next_ini) {
   2013 		next_ini = AVL_NEXT(&iscsit_global.global_ini_list, ini);
   2014 		avl_remove(&iscsit_global.global_ini_list, ini);
   2015 		nvlist_free(ini->ini_props);
   2016 		kmem_free(ini, sizeof (*ini));
   2017 		iscsit_global_rele();
   2018 	}
   2019 
   2020 	for (cfg_ini = cfg->config_ini_list;
   2021 	    cfg_ini != NULL;
   2022 	    cfg_ini = cfg_ini->ini_next) {
   2023 		ini = kmem_zalloc(sizeof (iscsit_ini_t), KM_SLEEP);
   2024 		(void) strlcpy(ini->ini_name, cfg_ini->ini_name,
   2025 		    MAX_ISCSI_NODENAMELEN);
   2026 		(void) nvlist_dup(cfg_ini->ini_properties, &ini->ini_props,
   2027 		    KM_SLEEP);
   2028 		avl_add(&iscsit_global.global_ini_list, ini);
   2029 		iscsit_global_hold();
   2030 	}
   2031 
   2032 	return (ITCFG_SUCCESS);
   2033 }
   2034 
   2035 int
   2036 iscsit_ini_avl_compare(const void *void_ini1, const void *void_ini2)
   2037 {
   2038 	const iscsit_ini_t	*ini1 = void_ini1;
   2039 	const iscsit_ini_t	*ini2 = void_ini2;
   2040 	int 			result;
   2041 
   2042 	/*
   2043 	 * Sort by ISID first then TSIH
   2044 	 */
   2045 	result = strcmp(ini1->ini_name, ini2->ini_name);
   2046 	if (result < 0) {
   2047 		return (-1);
   2048 	} else if (result > 0) {
   2049 		return (1);
   2050 	}
   2051 
   2052 	return (0);
   2053 }
   2054 
   2055 iscsit_ini_t *
   2056 iscsit_ini_lookup_locked(char *ini_name)
   2057 {
   2058 	iscsit_ini_t	tmp_ini;
   2059 	iscsit_ini_t	*result;
   2060 
   2061 	/*
   2062 	 * Use a dummy target for lookup, filling in all fields used in AVL
   2063 	 * comparison.
   2064 	 */
   2065 	(void) strlcpy(tmp_ini.ini_name, ini_name, MAX_ISCSI_NODENAMELEN);
   2066 	result = avl_find(&iscsit_global.global_ini_list, &tmp_ini, NULL);
   2067 
   2068 	return (result);
   2069 }
   2070