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