summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/scsi/adapters/iscsi/iscsi_lun.c
blob: 2e707d9fcce849959b844636e9a1b782a91d9ca8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright 2019 Nexenta Systems, Inc.
 */

/*
 * iSCSI logical unit interfaces
 */

#include "iscsi.h"
#include <sys/bootprops.h>
#include <sys/sysevent/eventdefs.h>
#include <sys/sysevent/dev.h>

/* tpgt bytes in string form */
#define	TPGT_EXT_SIZE	5

/* logical unit number bytes in string form */
#define	LUN_EXT_SIZE	10

/*
 * Addition addr size of size of ',' + max str form of tpgt (2 bytes) +
 * ',' + max str form of logical unit number (4 bytes).
 */
#define	ADDR_EXT_SIZE	(1 + TPGT_EXT_SIZE + 1 + LUN_EXT_SIZE)

/* internal interfaces */
static iscsi_status_t iscsi_lun_virt_create(iscsi_sess_t *isp,
    uint16_t lun_num, iscsi_lun_t *ilp, struct scsi_inquiry *inq);
static iscsi_status_t iscsi_lun_phys_create(iscsi_sess_t *isp,
    uint16_t lun_num, iscsi_lun_t *ilp, struct scsi_inquiry *inq);

extern dev_info_t	*scsi_vhci_dip;
extern ib_boot_prop_t   *iscsiboot_prop;

/*
 * +--------------------------------------------------------------------+
 * | External Connection Interfaces					|
 * +--------------------------------------------------------------------+
 */


/*
 * iscsi_lun_create - This function will create a lun mapping.
 * logic specific to MPxIO vs. NDI node creation is switched
 * out to a helper function.
 */
iscsi_status_t
iscsi_lun_create(iscsi_sess_t *isp, uint16_t lun_num, uint8_t lun_addr_type,
    struct scsi_inquiry *inq, char *guid)
{
	iscsi_status_t		rtn		= ISCSI_STATUS_INTERNAL_ERROR;
	iscsi_hba_t		*ihp		= NULL;
	iscsi_lun_t		*ilp		= NULL;
	iscsi_lun_t		*ilp_tmp	= NULL;
	char			*addr		= NULL;
	uint16_t		boot_lun_num	= 0;
	uint64_t		*lun_num_ptr	= NULL;
	uint32_t		oid_tmp		= 0;

	ASSERT(isp != NULL);
	ihp = isp->sess_hba;
	ASSERT(ihp != NULL);

	mutex_enter(&iscsi_oid_mutex);
	oid_tmp = iscsi_oid++;
	mutex_exit(&iscsi_oid_mutex);

	rw_enter(&isp->sess_lun_list_rwlock, RW_WRITER);
	/*
	 * Check whether it has already existed in the list.
	 */
	for (ilp_tmp = isp->sess_lun_list; ilp_tmp != NULL;
	    ilp_tmp = ilp_tmp->lun_next) {
		if (ilp_tmp->lun_num == lun_num) {
			/*
			 * The logic unit has already existed in the list,
			 * return with success.
			 */
			rw_exit(&isp->sess_lun_list_rwlock);
			return (ISCSI_STATUS_SUCCESS);
		}
	}

	addr = kmem_zalloc((strlen((char *)isp->sess_name) +
	    ADDR_EXT_SIZE + 1), KM_SLEEP);
	(void) snprintf(addr,
	    (strlen((char *)isp->sess_name) +
	    ADDR_EXT_SIZE + 1),
	    "%02X%02X%s%04X,%d", isp->sess_isid[4],
	    isp->sess_isid[5], isp->sess_name,
	    isp->sess_tpgt_nego & 0xFFFF, lun_num);

	/* allocate space for lun struct */
	ilp = kmem_zalloc(sizeof (iscsi_lun_t), KM_SLEEP);
	ilp->lun_sig = ISCSI_SIG_LUN;
	ilp->lun_state &= ISCSI_LUN_STATE_CLEAR;
	ilp->lun_state |= ISCSI_LUN_STATE_OFFLINE;

	/* initialize common LU information */
	ilp->lun_num	    = lun_num;
	ilp->lun_addr_type  = lun_addr_type;
	ilp->lun_sess	    = isp;
	ilp->lun_addr	    = addr;
	ilp->lun_type	    = inq->inq_dtype & DTYPE_MASK;
	ilp->lun_oid	    = oid_tmp;
	/*
	 * Setting refcnt to 1 is the first hold for the LUN structure.
	 */
	ilp->lun_refcnt	    = 1;
	mutex_init(&ilp->lun_mutex, NULL, MUTEX_DRIVER, NULL);

	bcopy(inq->inq_vid, ilp->lun_vid, sizeof (inq->inq_vid));
	bcopy(inq->inq_pid, ilp->lun_pid, sizeof (inq->inq_pid));

	/* store GUID if valid one exists */
	if (guid != NULL) {
		ilp->lun_guid_size = strlen(guid) + 1;
		ilp->lun_guid = kmem_zalloc(ilp->lun_guid_size, KM_SLEEP);
		(void) strcpy(ilp->lun_guid, guid);
	} else {
		ilp->lun_guid_size = 0;
		ilp->lun_guid = NULL;
	}

	/*
	 * We need to add the lun to our lists now because during the
	 * lun creation we will get called back into multiple times
	 * depending on the createion type.  These callbacks will
	 * occur via our tran_init_lun, tran_get_name, tran_get_bus_addr,
	 * tran_init_pkt, tran_start.
	 */
	if (isp->sess_lun_list == NULL) {
		isp->sess_lun_list = ilp;
	} else {
		ilp->lun_next = isp->sess_lun_list;
		isp->sess_lun_list = ilp;
	}

	/* Attempt to create a scsi_vhci binding if GUID is available */
	if ((ihp->hba_mpxio_enabled == B_TRUE) &&
	    (guid != NULL)) {
		rtn = iscsi_lun_virt_create(isp, lun_num, ilp, inq);
	}
	if (!ISCSI_SUCCESS(rtn)) {
		/* unable to bind under scsi_vhci, failback to ndi */
		rtn = iscsi_lun_phys_create(isp, lun_num, ilp, inq);
	}

	/*
	 * If NOT successful we need to remove the lun from the
	 * session and free any related resources.
	 */
	if (!ISCSI_SUCCESS(rtn)) {
		if (ilp == isp->sess_lun_list) {
			/* if head, set head to our next */
			isp->sess_lun_list = ilp->lun_next;
		} else {
			/* if not head, set prev lun's next to our next */
			for (ilp_tmp = isp->sess_lun_list; ilp_tmp;
			    ilp_tmp = ilp_tmp->lun_next) {
				if (ilp_tmp->lun_next == ilp) {
					ilp_tmp->lun_next = ilp->lun_next;
					break;
				}
			}
		}

		kmem_free(ilp->lun_addr,
		    (strlen((char *)isp->sess_name) +
		    ADDR_EXT_SIZE + 1));
		ilp->lun_addr = NULL;

		if (ilp->lun_guid != NULL) {
			kmem_free(ilp->lun_guid, ilp->lun_guid_size);
			ilp->lun_guid = NULL;
		}
		mutex_destroy(&ilp->lun_mutex);
		kmem_free(ilp, sizeof (iscsi_lun_t));
	} else {
		ilp->lun_state &= ISCSI_LUN_STATE_CLEAR;
		ilp->lun_state |= ISCSI_LUN_STATE_ONLINE;
		ilp->lun_time_online = ddi_get_time();

		/* Check whether this is the required LUN for iscsi boot */
		if (iscsiboot_prop != NULL && isp->sess_boot == B_TRUE &&
		    iscsiboot_prop->boot_tgt.lun_online == 0) {
			lun_num_ptr =
			    (uint64_t *)iscsiboot_prop->boot_tgt.tgt_boot_lun;
			boot_lun_num = (uint16_t)(*lun_num_ptr);
			if (boot_lun_num == ilp->lun_num) {
				/*
				 * During iscsi boot, the boot lun has been
				 * online, we should set the "online flag".
				 */
				iscsiboot_prop->boot_tgt.lun_online = 1;
			}
		}
	}
	rw_exit(&isp->sess_lun_list_rwlock);

	return (rtn);
}

void
iscsi_lun_hold(iscsi_lun_t *ilp)
{
	mutex_enter(&ilp->lun_mutex);
	/*
	 * By design lun_refcnt should never be zero when this routine
	 * is called. When the LUN is created the refcnt is set to 1.
	 * If iscsi_lun_rele is called and the refcnt goes to zero the
	 * structure will be freed so this method shouldn't be called
	 * afterwards.
	 */
	ASSERT(ilp->lun_refcnt > 0);
	ilp->lun_refcnt++;
	mutex_exit(&ilp->lun_mutex);
}

void
iscsi_lun_rele(iscsi_lun_t *ilp)
{
	ASSERT(ilp != NULL);

	mutex_enter(&ilp->lun_mutex);
	ASSERT(ilp->lun_refcnt > 0);
	if (--ilp->lun_refcnt == 0) {
		iscsi_sess_t		*isp;

		isp = ilp->lun_sess;
		ASSERT(isp != NULL);

		/* ---- release its memory ---- */
		kmem_free(ilp->lun_addr, (strlen((char *)isp->sess_name) +
		    ADDR_EXT_SIZE + 1));

		if (ilp->lun_guid != NULL) {
			kmem_free(ilp->lun_guid, ilp->lun_guid_size);
		}
		mutex_destroy(&ilp->lun_mutex);
		kmem_free(ilp, sizeof (iscsi_lun_t));
	} else {
		mutex_exit(&ilp->lun_mutex);
	}
}

/*
 * iscsi_lun_cmd_cancel -- as the name implies, cancel all commands for the lun
 *
 * This code is similar to the timeout function with a lot less checking of
 * state before sending the ABORT event for commands on the pending queue.
 *
 * This function is only used by iscsi_lun_destroy().
 */
static void
iscsi_lun_cmd_cancel(iscsi_lun_t *ilp)
{
	iscsi_sess_t	*isp;
	iscsi_cmd_t	*icmdp, *nicmdp;

	isp = ilp->lun_sess;
	rw_enter(&isp->sess_state_rwlock, RW_READER);
	mutex_enter(&isp->sess_queue_pending.mutex);
	for (icmdp = isp->sess_queue_pending.head;
	    icmdp; icmdp = nicmdp) {
		nicmdp = icmdp->cmd_next;

		/*
		 * For commands on the pending queue we can go straight
		 * to and abort request which will free the command
		 * and call back to the complete function.
		 */
		iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E4, isp);
	}
	mutex_exit(&isp->sess_queue_pending.mutex);
	rw_exit(&isp->sess_state_rwlock);
}

/*
 * iscsi_lun_destroy - offline and remove lun
 *
 * This interface is called when a name service change has
 * occured and the storage is no longer available to this
 * initiator.  This function will offline and free the
 * solaris node resources.  Then it will free all iscsi lun
 * resources.
 *
 * This function can fail with ISCSI_STATUS_BUSY if the
 * logical unit is in use.  The user should unmount or
 * close the device and perform the nameservice operation
 * again if this occurs.
 */
iscsi_status_t
iscsi_lun_destroy(iscsi_hba_t *ihp, iscsi_lun_t *ilp)
{
	iscsi_status_t		status		= ISCSI_STATUS_SUCCESS;
	iscsi_sess_t		*isp		= NULL;
	iscsi_lun_t		*t_ilp		= NULL;

	ASSERT(ilp != NULL);
	isp = ilp->lun_sess;
	ASSERT(isp != NULL);

	/* flush all outstanding commands first */
	iscsi_lun_cmd_cancel(ilp);

	/* attempt to offline and free solaris node */
	status = iscsi_lun_offline(ihp, ilp, B_TRUE);

	/* If we successfully unplumbed the lun remove it from our lists */
	if (ISCSI_SUCCESS(status)) {
		if (isp->sess_lun_list == ilp) {
			/* target first item in list */
			isp->sess_lun_list = ilp->lun_next;
		} else {
			/*
			 * search session list for ilp pointing
			 * to lun being removed.  Then
			 * update that luns next pointer.
			 */
			t_ilp = isp->sess_lun_list;
			while (t_ilp->lun_next != NULL) {
				if (t_ilp->lun_next == ilp) {
					break;
				}
				t_ilp = t_ilp->lun_next;
			}
			if (t_ilp->lun_next == ilp) {
				t_ilp->lun_next = ilp->lun_next;
			} else {
				/* couldn't find session */
				ASSERT(FALSE);
			}
		}

		iscsi_lun_rele(ilp);
	}

	return (status);
}

/*
 * +--------------------------------------------------------------------+
 * | External Logical Unit Interfaces					|
 * +--------------------------------------------------------------------+
 */

/*
 * iscsi_lun_virt_create - Creates solaris logical unit via MDI
 */
static iscsi_status_t
iscsi_lun_virt_create(iscsi_sess_t *isp, uint16_t lun_num, iscsi_lun_t *ilp,
    struct scsi_inquiry *inq)
{
	iscsi_status_t		rtn		= ISCSI_STATUS_INTERNAL_ERROR;
	int			mdi_rtn		= MDI_FAILURE;
	iscsi_hba_t		*ihp		= NULL;
	mdi_pathinfo_t		*pip		= NULL;
	char			*nodename	= NULL;
	char			**compatible	= NULL;
	int			ncompatible	= 0;
	int			circ = 0;

	ASSERT(isp != NULL);
	ASSERT(ilp != NULL);
	ihp = isp->sess_hba;
	ASSERT(ihp != NULL);

	/*
	 * Generate compatible property
	 */
	scsi_hba_nodename_compatible_get(inq, "vhci",
	    inq->inq_dtype, NULL, &nodename, &compatible, &ncompatible);

	/* if nodename can't be determined then print a message and skip it */
	if (nodename == NULL) {
		cmn_err(CE_WARN, "iscsi driver found no compatible driver "
		    "for %s lun %d dtype:0x%02x", isp->sess_name, lun_num,
		    inq->inq_dtype);
		return (ISCSI_STATUS_INTERNAL_ERROR);
	}

	/*
	 *
	 */
	ndi_devi_enter(scsi_vhci_dip, &circ);
	mdi_rtn = mdi_pi_alloc_compatible(ihp->hba_dip, nodename,
	    ilp->lun_guid, ilp->lun_addr, compatible, ncompatible,
	    0, &pip);

	if (mdi_rtn == MDI_SUCCESS) {
		mdi_pi_set_phci_private(pip, (caddr_t)ilp);

		if (mdi_prop_update_string(pip, MDI_GUID,
		    ilp->lun_guid) != DDI_SUCCESS) {
			cmn_err(CE_WARN, "iscsi driver unable to create "
			    "property for %s lun %d (MDI_GUID)",
			    isp->sess_name, lun_num);
			mdi_rtn = MDI_FAILURE;
			goto virt_create_done;
		}

		if (mdi_prop_update_int(pip, TARGET_PROP,
		    isp->sess_oid) != DDI_SUCCESS) {
			cmn_err(CE_WARN, "iscsi driver unable to create "
			    "property for %s lun %d (TARGET_PROP)",
			    isp->sess_name, lun_num);
			mdi_rtn = MDI_FAILURE;
			goto virt_create_done;
		}

		if (mdi_prop_update_int(pip, LUN_PROP,
		    ilp->lun_num) != DDI_SUCCESS) {
			cmn_err(CE_WARN, "iscsi driver unable to create "
			    "property for %s lun %d (LUN_PROP)",
			    isp->sess_name, lun_num);
			mdi_rtn = MDI_FAILURE;
			goto virt_create_done;
		}

		if (mdi_prop_update_string_array(pip, "compatible",
		    compatible, ncompatible) !=
		    DDI_PROP_SUCCESS) {
			cmn_err(CE_WARN, "iscsi driver unable to create "
			    "property for %s lun %d (COMPATIBLE)",
			    isp->sess_name, lun_num);
			mdi_rtn = MDI_FAILURE;
			goto virt_create_done;
		}

		mdi_rtn = mdi_pi_online(pip, 0);
		if (mdi_rtn == MDI_NOT_SUPPORTED) {
			mdi_rtn = MDI_FAILURE;
			goto virt_create_done;
		}

		ilp->lun_pip = pip;
		ilp->lun_dip = NULL;

virt_create_done:

		if (pip && mdi_rtn != MDI_SUCCESS) {
			ilp->lun_pip = NULL;
			ilp->lun_dip = NULL;
			(void) mdi_prop_remove(pip, NULL);
			(void) mdi_pi_free(pip, 0);
		} else {
			rtn = ISCSI_STATUS_SUCCESS;
		}
	}
	ndi_devi_exit(scsi_vhci_dip, circ);

	scsi_hba_nodename_compatible_free(nodename, compatible);

	return (rtn);
}


/*
 * iscsi_lun_phys_create - creates solaris logical unit via NDI
 */
static iscsi_status_t
iscsi_lun_phys_create(iscsi_sess_t *isp, uint16_t lun_num,
    iscsi_lun_t *ilp, struct scsi_inquiry *inq)
{
	iscsi_status_t		rtn		= ISCSI_STATUS_INTERNAL_ERROR;
	int			ndi_rtn		= NDI_FAILURE;
	iscsi_hba_t		*ihp		= NULL;
	dev_info_t		*lun_dip	= NULL;
	char			*nodename	= NULL;
	char			**compatible	= NULL;
	int			ncompatible	= 0;
	char			*scsi_binding_set = NULL;
	char			instance[32];
	int			circ		= 0;

	ASSERT(isp != NULL);
	ASSERT(ilp != NULL);
	ihp = isp->sess_hba;
	ASSERT(ihp != NULL);
	ASSERT(inq != NULL);

	/* get the 'scsi-binding-set' property */
	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, isp->sess_hba->hba_dip,
	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, "scsi-binding-set",
	    &scsi_binding_set) != DDI_PROP_SUCCESS) {
		scsi_binding_set = NULL;
	}

	/* generate compatible property */
	scsi_hba_nodename_compatible_get(inq, scsi_binding_set,
	    inq->inq_dtype, NULL, &nodename, &compatible, &ncompatible);
	if (scsi_binding_set)
		ddi_prop_free(scsi_binding_set);

	/* if nodename can't be determined then print a message and skip it */
	if (nodename == NULL) {
		cmn_err(CE_WARN, "iscsi driver found no compatible driver "
		    "for %s lun %d", isp->sess_name, lun_num);
		return (ISCSI_STATUS_INTERNAL_ERROR);
	}

	ndi_devi_enter(ihp->hba_dip, &circ);

	ndi_rtn = ndi_devi_alloc(ihp->hba_dip, nodename,
	    DEVI_SID_NODEID, &lun_dip);

	/* if lun alloc success, set props */
	if (ndi_rtn == NDI_SUCCESS) {

		if (ndi_prop_update_int(DDI_DEV_T_NONE,
		    lun_dip, TARGET_PROP, (int)isp->sess_oid) !=
		    DDI_PROP_SUCCESS) {
			cmn_err(CE_WARN, "iscsi driver unable to create "
			    "property for %s lun %d (TARGET_PROP)",
			    isp->sess_name, lun_num);
			ndi_rtn = NDI_FAILURE;
			goto phys_create_done;
		}

		if (ndi_prop_update_int(DDI_DEV_T_NONE,
		    lun_dip, LUN_PROP, (int)ilp->lun_num) !=
		    DDI_PROP_SUCCESS) {
			cmn_err(CE_WARN, "iscsi driver unable to create "
			    "property for %s lun %d (LUN_PROP)",
			    isp->sess_name, lun_num);
			ndi_rtn = NDI_FAILURE;
			goto phys_create_done;
		}

		if (ndi_prop_update_string_array(DDI_DEV_T_NONE,
		    lun_dip, "compatible", compatible, ncompatible)
		    != DDI_PROP_SUCCESS) {
			cmn_err(CE_WARN, "iscsi driver unable to create "
			    "property for %s lun %d (COMPATIBLE)",
			    isp->sess_name, lun_num);
			ndi_rtn = NDI_FAILURE;
			goto phys_create_done;
		}

phys_create_done:
		/* If props were setup ok, online the lun */
		if (ndi_rtn == NDI_SUCCESS) {
			/* Try to online the new node */
			ndi_rtn = ndi_devi_online(lun_dip, 0);
		}

		/* If success set rtn flag, else unwire alloc'd lun */
		if (ndi_rtn == NDI_SUCCESS) {
			rtn = ISCSI_STATUS_SUCCESS;
			/*
			 * Assign the instance number for the dev_link
			 * generator.  This will ensure the link name is
			 * unique and persistent across reboots.
			 */
			(void) snprintf(instance, 32, "%d",
			    ddi_get_instance(lun_dip));
			(void) ndi_prop_update_string(DDI_DEV_T_NONE,
			    lun_dip, NDI_GUID, instance);
		} else {
			cmn_err(CE_WARN, "iscsi driver unable to online "
			    "%s lun %d", isp->sess_name, lun_num);
			ndi_prop_remove_all(lun_dip);
			(void) ndi_devi_free(lun_dip);
		}

	}
	ndi_devi_exit(ihp->hba_dip, circ);

	ilp->lun_dip = lun_dip;
	ilp->lun_pip = NULL;

	scsi_hba_nodename_compatible_free(nodename, compatible);

	return (rtn);
}


/*
 * iscsi_lun_online - _di_online logical unit
 *
 * This is called after a path has recovered it will cause
 * an offline path to become online/active again.
 */
void
iscsi_lun_online(iscsi_hba_t *ihp, iscsi_lun_t *ilp)
{
	int			circ		= 0;
	int			rval		= 0;
	uint64_t		*lun_num_ptr	= NULL;
	uint16_t		boot_lun_num	= 0;
	iscsi_sess_t		*isp		= NULL;
	boolean_t		online		= B_FALSE;
	nvlist_t		*attr_list	= NULL;
	char			*pathname	= NULL;
	dev_info_t		*lun_dip	= NULL;

	ASSERT(ilp != NULL);
	ASSERT((ilp->lun_pip != NULL) || (ilp->lun_dip != NULL));

	if (ilp->lun_pip != NULL) {
		ndi_devi_enter(scsi_vhci_dip, &circ);
		rval =  mdi_pi_online(ilp->lun_pip, 0);
		ndi_devi_exit(scsi_vhci_dip, circ);
		if (rval == MDI_SUCCESS) {
			ilp->lun_state &= ISCSI_LUN_STATE_CLEAR;
			ilp->lun_state |= ISCSI_LUN_STATE_ONLINE;
			ilp->lun_time_online = ddi_get_time();
			online = B_TRUE;
		}

	} else if (ilp->lun_dip != NULL) {
		ndi_devi_enter(ihp->hba_dip, &circ);
		rval =  ndi_devi_online(ilp->lun_dip, 0);
		ndi_devi_exit(ihp->hba_dip, circ);
		if (rval == NDI_SUCCESS) {
			ilp->lun_state &= ISCSI_LUN_STATE_CLEAR;
			ilp->lun_state |= ISCSI_LUN_STATE_ONLINE;
			ilp->lun_time_online = ddi_get_time();
			online = B_TRUE;
		}
	}

	/* Check whether this is the required LUN for iscsi boot */
	if (iscsiboot_prop != NULL &&
	    iscsiboot_prop->boot_tgt.lun_online == 0) {
		isp = ilp->lun_sess;
		if (isp->sess_boot == B_TRUE) {
			lun_num_ptr =
			    (uint64_t *)iscsiboot_prop->boot_tgt.tgt_boot_lun;
			boot_lun_num = (uint16_t)(*lun_num_ptr);
			if (boot_lun_num == ilp->lun_num) {
				/*
				 * During iscsi boot, the boot lun has been
				 * online, we should set the "online flag".
				 */
				iscsiboot_prop->boot_tgt.lun_online = 1;
			}
		}
	}

	/*
	 * If the LUN has been online and it is a disk,
	 * send out a system event.
	 */
	if (online == B_TRUE && ilp->lun_type == DTYPE_DIRECT) {
		if (nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, KM_SLEEP) !=
		    DDI_SUCCESS) {
			return;
		}

		if (ilp->lun_pip != NULL) {
			lun_dip = mdi_pi_get_client(ilp->lun_pip);
		} else {
			lun_dip = ilp->lun_dip;
		}

		pathname = kmem_zalloc(MAXNAMELEN + 1, KM_SLEEP);
		(void) ddi_pathname(lun_dip, pathname);

		if (nvlist_add_string(attr_list, DEV_PHYS_PATH, pathname) !=
		    DDI_SUCCESS) {
			nvlist_free(attr_list);
			kmem_free(pathname, MAXNAMELEN + 1);
			return;
		}
		iscsi_send_sysevent(ihp, EC_DEV_ADD, ESC_DISK, attr_list);
		kmem_free(pathname, MAXNAMELEN + 1);
		nvlist_free(attr_list);
	}
}

/*
 * iscsi_lun_offline - attempt _di_offline [and optional _di_free]
 *
 * This function is called via two paths.  When a transport
 * path has failed it will be called to offline the logical
 * unit.  When nameservice access has been removed it will
 * be called to both offline and free the logical unit.
 * (This operates soley on the solaris node states.
 * iscsi_lun_destroy() should be called when attempting
 * to free all iscsi lun resources.)
 *
 * This function can fail with ISCSI_STATUS_BUSY if the
 * logical unit is in use.  The user should unmount or
 * close the device and perform the nameservice operation
 * again if this occurs.
 *
 * If we fail to offline a LUN that we don't want to destroy,
 * we will mark it with invalid state. If this LUN still
 * exists on the target, we can have another chance to online
 * it again when we do the LUN enumeration.
 */
iscsi_status_t
iscsi_lun_offline(iscsi_hba_t *ihp, iscsi_lun_t *ilp, boolean_t lun_free)
{
	iscsi_status_t		status		= ISCSI_STATUS_SUCCESS;
	int			circ		= 0;
	dev_info_t		*cdip;
	char			*pathname	= NULL;
	boolean_t		offline		= B_FALSE;
	nvlist_t		*attr_list	= NULL;

	ASSERT(ilp != NULL);
	ASSERT((ilp->lun_pip != NULL) || (ilp->lun_dip != NULL));

	if (ilp->lun_pip == NULL)
		cdip = ilp->lun_dip;
	else
		cdip = mdi_pi_get_client(ilp->lun_pip);

	if (cdip != NULL && ilp->lun_type == DTYPE_DIRECT) {
		pathname = kmem_zalloc(MAXNAMELEN + 1, KM_SLEEP);
		(void) ddi_pathname(cdip, pathname);
	}

	/* Attempt to offline the logical units */
	if (ilp->lun_pip != NULL) {
		/* virt/mdi */
		ndi_devi_enter(scsi_vhci_dip, &circ);
		if (mdi_pi_offline(ilp->lun_pip, 0) == MDI_SUCCESS) {
			ilp->lun_state &= ISCSI_LUN_STATE_CLEAR;
			ilp->lun_state |= ISCSI_LUN_STATE_OFFLINE;
			if (lun_free == B_TRUE) {
				(void) mdi_prop_remove(ilp->lun_pip, NULL);
				(void) mdi_pi_free(ilp->lun_pip, 0);
			}
			offline = B_TRUE;
		} else {
			status = ISCSI_STATUS_BUSY;
			if (lun_free == B_FALSE) {
				ilp->lun_state |= ISCSI_LUN_STATE_INVALID;
				offline = B_TRUE;
			}
		}
		ndi_devi_exit(scsi_vhci_dip, circ);

	} else  {
		/* phys/ndi */
		int flags = NDI_DEVFS_CLEAN;

		ndi_devi_enter(ihp->hba_dip, &circ);
		if (lun_free == B_TRUE &&
		    (ilp->lun_state & ISCSI_LUN_STATE_ONLINE))
			flags |= NDI_DEVI_REMOVE;
		if (ndi_devi_offline(ilp->lun_dip, flags) != NDI_SUCCESS) {
			status = ISCSI_STATUS_BUSY;
			if (lun_free == B_FALSE) {
				ilp->lun_state |= ISCSI_LUN_STATE_INVALID;
				offline = B_TRUE;
			}
		} else {
			ilp->lun_state &= ISCSI_LUN_STATE_CLEAR;
			ilp->lun_state |= ISCSI_LUN_STATE_OFFLINE;
			offline = B_TRUE;
		}
		ndi_devi_exit(ihp->hba_dip, circ);
	}

	if (offline == B_TRUE && pathname != NULL &&
	    ilp->lun_type == DTYPE_DIRECT) {
		if (nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, KM_SLEEP) !=
		    DDI_SUCCESS) {
			kmem_free(pathname, MAXNAMELEN + 1);
			return (status);
		}

		if (nvlist_add_string(attr_list, DEV_PHYS_PATH, pathname) !=
		    DDI_SUCCESS) {
			nvlist_free(attr_list);
			kmem_free(pathname, MAXNAMELEN + 1);
			return (status);
		}

		iscsi_send_sysevent(ihp, EC_DEV_REMOVE, ESC_DISK, attr_list);
		nvlist_free(attr_list);
	}

	if (pathname != NULL) {
		kmem_free(pathname, MAXNAMELEN + 1);
	}

	return (status);
}