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 2000 by Cisco Systems, Inc.  All rights reserved.
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * iSCSI Software Initiator
27  */
28 
29 /*
30  * Framework interface routines for iSCSI
31  */
32 
33 #include "iscsi.h"				/* main header */
34 #include <sys/iscsi_protocol.h>	/* protocol structs */
35 #include <sys/scsi/adapters/iscsi_if.h>		/* ioctl interfaces */
36 #include "iscsi_targetparam.h"
37 #include "persistent.h"
38 #include <sys/scsi/adapters/iscsi_door.h>
39 #include <sys/dlpi.h>
40 #include <sys/utsname.h>
41 #include "isns_client.h"
42 #include "isns_protocol.h"
43 #include <sys/bootprops.h>
44 #include <sys/types.h>
45 #include <sys/bootconf.h>
46 
47 #define	ISCSI_NAME_VERSION	"iSCSI Initiator v-1.55"
48 
49 #define	MAX_GET_NAME_SIZE	1024
50 #define	MAX_NAME_PROP_SIZE	256
51 #define	UNDEFINED		-1
52 #define	ISCSI_DISC_DELAY	2	/* seconds */
53 
54 /*
55  * +--------------------------------------------------------------------+
56  * | iscsi globals                                                      |
57  * +--------------------------------------------------------------------+
58  */
59 void		*iscsi_state;
60 kmutex_t	iscsi_oid_mutex;
61 uint32_t	iscsi_oid;
62 int		iscsi_nop_delay		= ISCSI_DEFAULT_NOP_DELAY;
63 int		iscsi_rx_window		= ISCSI_DEFAULT_RX_WINDOW;
64 int		iscsi_rx_max_window	= ISCSI_DEFAULT_RX_MAX_WINDOW;
65 boolean_t	iscsi_logging		= B_FALSE;
66 
67 extern ib_boot_prop_t	*iscsiboot_prop;
68 extern int		modrootloaded;
69 extern struct bootobj	rootfs;
70 
71 /*
72  * +--------------------------------------------------------------------+
73  * | iscsi.c prototypes							|
74  * +--------------------------------------------------------------------+
75  */
76 static int iscsi_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd,
77     void *arg, void **result);
78 static int iscsi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
79 static int iscsi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
80 
81 /* scsi_tran prototypes */
82 static int iscsi_tran_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
83     scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
84 static int iscsi_tran_lun_probe(struct scsi_device *sd, int (*callback) ());
85 static struct scsi_pkt *iscsi_tran_init_pkt(struct scsi_address *ap,
86     struct scsi_pkt *pkt, struct buf *bp, int cmdlen, int statuslen,
87     int tgtlen, int flags, int (*callback) (), caddr_t arg);
88 static void iscsi_tran_lun_free(dev_info_t *hba_dip, dev_info_t *lun_dip,
89     scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
90 static int iscsi_tran_start(struct scsi_address *ap, struct scsi_pkt *pkt);
91 static int iscsi_tran_abort(struct scsi_address *ap, struct scsi_pkt *pkt);
92 static int iscsi_tran_reset(struct scsi_address *ap, int level);
93 static int iscsi_tran_getcap(struct scsi_address *ap, char *cap, int whom);
94 static int iscsi_tran_setcap(struct scsi_address *ap, char *cap,
95     int value, int whom);
96 static void iscsi_tran_destroy_pkt(struct scsi_address *ap,
97     struct scsi_pkt *pkt);
98 static void iscsi_tran_dmafree(struct scsi_address *ap,
99     struct scsi_pkt *pkt);
100 static void iscsi_tran_sync_pkt(struct scsi_address *ap,
101     struct scsi_pkt *pkt);
102 static void iscsi_tran_sync_pkt(struct scsi_address *ap,
103     struct scsi_pkt *pkt);
104 static int iscsi_tran_reset_notify(struct scsi_address *ap, int flag,
105     void (*callback) (caddr_t), caddr_t arg);
106 static int iscsi_tran_bus_config(dev_info_t *parent, uint_t flags,
107     ddi_bus_config_op_t op, void *arg, dev_info_t **childp);
108 static int iscsi_tran_bus_unconfig(dev_info_t *parent, uint_t flags,
109     ddi_bus_config_op_t op, void *arg);
110 static int iscsi_tran_get_name(struct scsi_device *sd, char *name, int len);
111 static int iscsi_tran_get_bus_addr(struct scsi_device *sd, char *name, int len);
112 
113 /* bus_ops prototypes */
114 /* LINTED E_STATIC_UNUSED */
115 static ddi_intrspec_t iscsi_get_intrspec(dev_info_t *dip, dev_info_t *rdip,
116     uint_t inumber);
117 /* LINTED E_STATIC_UNUSED */
118 static int iscsi_add_intrspec(dev_info_t *dip, dev_info_t *rdip,
119     ddi_intrspec_t intrspec, ddi_iblock_cookie_t *iblock_cookiep,
120     ddi_idevice_cookie_t *idevice_cookiep, uint_t (*int_handler)(caddr_t
121     int_handler_arg), caddr_t int_handler_arg, int kind);
122 /* LINTED E_STATIC_UNUSED */
123 static void iscsi_remove_intrspec(dev_info_t *dip, dev_info_t *rdip,
124     ddi_intrspec_t intrspec, ddi_iblock_cookie_t iblock_cookie);
125 /* LINTED E_STATIC_UNUSED */
126 static int iscsi_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
127     void *arg, void *result);
128 
129 /* cb_ops prototypes */
130 static int iscsi_open(dev_t *devp, int flags, int otyp, cred_t *credp);
131 static int iscsi_close(dev_t dev, int flag, int otyp, cred_t *credp);
132 static int iscsi_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
133     cred_t *credp, int *rvalp);
134 
135 int iscsi_get_persisted_param(uchar_t *name,
136     iscsi_param_get_t *ipgp,
137     iscsi_login_params_t *params);
138 static void iscsi_override_target_default(iscsi_hba_t *ihp,
139     iscsi_param_get_t *ipg);
140 
141 /* scsi_tran helpers */
142 static int iscsi_virt_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
143     scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
144 static int iscsi_phys_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
145     scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
146 static int iscsi_i_commoncap(struct scsi_address *ap, char *cap,
147     int val, int lunonly, int doset);
148 static void iscsi_get_name_to_iqn(char *name, int name_max_len);
149 static void iscsi_get_name_from_iqn(char *name, int name_max_len);
150 static boolean_t iscsi_cmp_boot_sess_oid(iscsi_hba_t *ihp, uint32_t oid);
151 
152 /* iscsi initiator service helpers */
153 static boolean_t iscsi_enter_service_zone(iscsi_hba_t *ihp, uint32_t status);
154 static void iscsi_exit_service_zone(iscsi_hba_t *ihp, uint32_t status);
155 static void iscsi_check_miniroot(iscsi_hba_t *ihp);
156 static void iscsi_get_tunable_default(iscsi_tunable_object_t *param);
157 static int iscsi_get_persisted_tunable_param(uchar_t *name,
158     iscsi_tunable_object_t *tpsg);
159 static void iscsi_set_default_tunable_params(iscsi_tunable_params_t *params);
160 
161 /* struct helpers prototypes */
162 
163 /*
164  * At this point this driver doesn't need this structure because nothing
165  * is done during the open, close or ioctl. Code put in place because
166  * some admin related work might be done in the ioctl routine.
167  */
168 static struct cb_ops iscsi_cb_ops = {
169 	iscsi_open,			/* open */
170 	iscsi_close,			/* close */
171 	nodev,				/* strategy */
172 	nodev,				/* print */
173 	nodev,				/* dump */
174 	nodev,				/* read */
175 	nodev,				/* write */
176 	iscsi_ioctl,			/* ioctl */
177 	nodev,				/* devmap */
178 	nodev,				/* mmap */
179 	nodev,				/* segmap */
180 	nochpoll,			/* poll */
181 	ddi_prop_op,			/* prop_op */
182 	NULL,				/* streamtab */
183 	D_NEW | D_MP | D_HOTPLUG,	/* flags */
184 	CB_REV,				/* cb_rev */
185 	nodev,				/* aread */
186 	nodev,				/* awrite */
187 };
188 
189 static struct dev_ops iscsi_dev_ops = {
190 	DEVO_REV,		/* devo_rev */
191 	0,			/* refcnt */
192 	iscsi_getinfo,		/* getinfo */
193 	nulldev,		/* identify */
194 	nulldev,		/* probe */
195 	iscsi_attach,		/* attach */
196 	iscsi_detach,		/* detach */
197 	nodev,			/* reset */
198 	&iscsi_cb_ops,		/* driver operations */
199 	NULL,			/* bus ops */
200 	NULL,			/* power management */
201 	ddi_quiesce_not_needed,	/* quiesce */
202 };
203 
204 static struct modldrv modldrv = {
205 	&mod_driverops,		/* drv_modops */
206 	ISCSI_NAME_VERSION,	/* drv_linkinfo */
207 	&iscsi_dev_ops		/* drv_dev_ops */
208 };
209 
210 static struct modlinkage modlinkage = {
211 	MODREV_1,		/* ml_rev */
212 	&modldrv,		/* ml_linkage[] */
213 	NULL			/* NULL termination */
214 };
215 
216 /*
217  * This structure is bogus. scsi_hba_attach_setup() requires, as in the kernel
218  * will panic if you don't pass this in to the routine, this information.
219  * Need to determine what the actual impact to the system is by providing
220  * this information if any. Since dma allocation is done in pkt_init it may
221  * not have any impact. These values are straight from the Writing Device
222  * Driver manual.
223  */
224 static ddi_dma_attr_t iscsi_dma_attr = {
225 	DMA_ATTR_V0,	/* ddi_dma_attr version */
226 	0,		/* low address */
227 	0xffffffff,	/* high address */
228 	0x00ffffff,	/* counter upper bound */
229 	1,		/* alignment requirements */
230 	0x3f,		/* burst sizes */
231 	1,		/* minimum DMA access */
232 	0xffffffff,	/* maximum DMA access */
233 	(1 << 24) - 1,	/* segment boundary restrictions */
234 	1,		/* scater/gather list length */
235 	512,		/* device granularity */
236 	0		/* DMA flags */
237 };
238 
239 /*
240  * _init - General driver init entry
241  */
242 int
243 _init(void)
244 {
245 	int rval = 0;
246 
247 	iscsi_net_init();
248 
249 	mutex_init(&iscsi_oid_mutex, NULL, MUTEX_DRIVER, NULL);
250 	iscsi_oid = ISCSI_INITIATOR_OID;
251 
252 	/*
253 	 * Set up the soft state structures. If this driver is actually
254 	 * being attached to the system then we'll have at least one
255 	 * HBA/NIC used.
256 	 */
257 	rval = ddi_soft_state_init(&iscsi_state,
258 	    sizeof (iscsi_hba_t), 1);
259 	if (rval != 0) {
260 		iscsi_net_fini();
261 		goto init_done;
262 	}
263 
264 	rval = scsi_hba_init(&modlinkage);
265 	if (rval != 0) {
266 		ddi_soft_state_fini(&iscsi_state);
267 		iscsi_net_fini();
268 		goto init_done;
269 	}
270 
271 	rval = mod_install(&modlinkage);
272 	if (rval != 0) {
273 		ddi_soft_state_fini(&iscsi_state);
274 		scsi_hba_fini(&modlinkage);
275 		iscsi_net_fini();
276 		goto init_done;
277 	}
278 	(void) iscsi_door_ini();
279 
280 init_done:
281 	return (rval);
282 }
283 
284 /*
285  * _fini - General driver destructor entry
286  */
287 int
288 _fini(void)
289 {
290 	int rval = 0;
291 
292 	rval = mod_remove(&modlinkage);
293 	if (rval == 0) {
294 		scsi_hba_fini(&modlinkage);
295 		ddi_soft_state_fini(&iscsi_state);
296 		mutex_destroy(&iscsi_oid_mutex);
297 		(void) iscsi_door_term();
298 		iscsi_net_fini();
299 	}
300 	return (rval);
301 }
302 
303 /*
304  * _info - General driver info entry
305  */
306 int
307 _info(struct modinfo *mp)
308 {
309 	int rval = 0;
310 
311 	rval = mod_info(&modlinkage, mp);
312 
313 	return (rval);
314 }
315 
316 
317 /*
318  * +--------------------------------------------------------------------+
319  * | Start of dev_ops routines					  |
320  * +--------------------------------------------------------------------+
321  */
322 
323 /*
324  * iscsi_getinfo - returns general driver information
325  */
326 /* ARGSUSED */
327 static int
328 iscsi_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd,
329     void *arg, void **result)
330 {
331 	int		rval		= DDI_SUCCESS;
332 	int		instance	= getminor((dev_t)arg);
333 	iscsi_hba_t	*ip;
334 
335 	switch (infocmd) {
336 	case DDI_INFO_DEVT2DEVINFO:
337 		if ((ip = ddi_get_soft_state(iscsi_state, instance)) == NULL) {
338 			return (DDI_FAILURE);
339 		}
340 		*result = ip->hba_dip;
341 		if (ip->hba_dip == NULL)
342 			rval = DDI_FAILURE;
343 		else
344 			rval = DDI_SUCCESS;
345 		break;
346 
347 	case DDI_INFO_DEVT2INSTANCE:
348 		*result = (void *)(uintptr_t)instance;
349 		rval = DDI_SUCCESS;
350 		break;
351 
352 	default:
353 		rval = DDI_FAILURE;
354 		break;
355 	}
356 	return (rval);
357 }
358 
359 
360 /*
361  * iscsi_attach -- Attach instance of an iSCSI HBA.  We
362  * will attempt to create our HBA and register it with
363  * scsi_vhci.  If it's not possible to create the HBA
364  * or register with vhci we will fail the attach.
365  */
366 static int
367 iscsi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
368 {
369 	int			rval		= DDI_SUCCESS;
370 	int			instance	= ddi_get_instance(dip);
371 	iscsi_hba_t		*ihp		= NULL;
372 	scsi_hba_tran_t		*tran		= NULL;
373 	char			init_port_name[MAX_NAME_PROP_SIZE];
374 
375 	switch (cmd) {
376 	case DDI_ATTACH:
377 		/* create iSCSH HBA devctl device node */
378 		if (ddi_create_minor_node(dip, ISCSI_DEVCTL, S_IFCHR, 0,
379 		    DDI_PSEUDO, 0) == DDI_SUCCESS) {
380 
381 			/* allocate HBA soft state */
382 			if (ddi_soft_state_zalloc(iscsi_state, instance) !=
383 			    DDI_SUCCESS) {
384 				ddi_remove_minor_node(dip, NULL);
385 				rval = DDI_FAILURE;
386 				break;
387 			}
388 
389 			/* get reference to soft state */
390 			if ((ihp = (iscsi_hba_t *)ddi_get_soft_state(
391 			    iscsi_state, instance)) == NULL) {
392 				ddi_remove_minor_node(dip, NULL);
393 				ddi_soft_state_free(iscsi_state, instance);
394 				rval = DDI_FAILURE;
395 				break;
396 			}
397 
398 			/* init HBA mutex used to protect discovery events */
399 			mutex_init(&ihp->hba_discovery_events_mutex, NULL,
400 			    MUTEX_DRIVER, NULL);
401 
402 			/* Get LDI ident */
403 			rval = ldi_ident_from_dip(dip, &ihp->hba_li);
404 			ASSERT(rval == 0); /* Failure indicates invalid arg */
405 
406 			/* init HBA mutex used to protect service status */
407 			mutex_init(&ihp->hba_service_lock, NULL,
408 			    MUTEX_DRIVER, NULL);
409 			cv_init(&ihp->hba_service_cv, NULL, CV_DRIVER, NULL);
410 
411 			/*
412 			 * init SendTargets semaphore that is used to allow
413 			 * only one operation at a time
414 			 */
415 			sema_init(&ihp->hba_sendtgts_semaphore, 1, NULL,
416 			    SEMA_DRIVER, NULL);
417 
418 			ihp->hba_sess_list = NULL;
419 			rw_init(&ihp->hba_sess_list_rwlock, NULL,
420 			    RW_DRIVER, NULL);
421 
422 			/* allocate scsi_hba_tran */
423 			if ((tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP))
424 			    == NULL) {
425 				ddi_remove_minor_node(dip, NULL);
426 				goto iscsi_attach_failed2;
427 			}
428 
429 			/* soft state setup */
430 			ihp->hba_sig	= ISCSI_SIG_HBA;
431 			ihp->hba_tran	= tran;
432 			ihp->hba_dip	= dip;
433 			if (iscsiboot_prop == NULL) {
434 				ihp->hba_service_status =
435 				    ISCSI_SERVICE_DISABLED;
436 				ihp->hba_service_status_overwrite = B_FALSE;
437 			} else {
438 				ihp->hba_service_status =
439 				    ISCSI_SERVICE_ENABLED;
440 				ihp->hba_service_status_overwrite = B_TRUE;
441 			}
442 			ihp->hba_service_client_count = 0;
443 
444 			mutex_enter(&iscsi_oid_mutex);
445 			ihp->hba_oid		  = iscsi_oid++;
446 			mutex_exit(&iscsi_oid_mutex);
447 
448 			ihp->hba_name[0]	  = '\0';
449 			ihp->hba_name_length	  = 0;
450 			ihp->hba_alias_length	  = 0;
451 			ihp->hba_alias[0]	  = '\0';
452 
453 			iscsi_net->tweaks.rcvbuf = ddi_prop_get_int(
454 			    DDI_DEV_T_ANY, ihp->hba_dip, 0, "so-rcvbuf",
455 			    ISCSI_SOCKET_RCVBUF_SIZE);
456 
457 			iscsi_net->tweaks.sndbuf = ddi_prop_get_int(
458 			    DDI_DEV_T_ANY, ihp->hba_dip, 0, "so-sndbuf",
459 			    ISCSI_SOCKET_SNDBUF_SIZE);
460 
461 			iscsi_net->tweaks.nodelay = ddi_prop_get_int(
462 			    DDI_DEV_T_ANY, ihp->hba_dip, 0, "tcp-nodelay",
463 			    ISCSI_TCP_NODELAY_DEFAULT);
464 
465 			iscsi_net->tweaks.conn_notify_threshold =
466 			    ddi_prop_get_int(DDI_DEV_T_ANY,
467 			    ihp->hba_dip, 0, "tcp-conn-notify-threshold",
468 			    ISCSI_TCP_CNOTIFY_THRESHOLD_DEFAULT);
469 
470 			iscsi_net->tweaks.conn_abort_threshold =
471 			    ddi_prop_get_int(DDI_DEV_T_ANY, ihp->hba_dip,
472 			    0, "tcp-conn-abort-threshold",
473 			    ISCSI_TCP_CABORT_THRESHOLD_DEFAULT);
474 
475 			iscsi_net->tweaks.abort_threshold = ddi_prop_get_int(
476 			    DDI_DEV_T_ANY, ihp->hba_dip, 0,
477 			    "tcp-abort-threshold",
478 			    ISCSI_TCP_ABORT_THRESHOLD_DEFAULT);
479 
480 			ihp->hba_config_storm_delay = ddi_prop_get_int(
481 			    DDI_DEV_T_ANY, ihp->hba_dip, 0,
482 			    "config-storm-delay",
483 			    ISCSI_CONFIG_STORM_DELAY_DEFAULT);
484 
485 			(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
486 			    "so-rcvbuf", iscsi_net->tweaks.rcvbuf);
487 
488 			(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
489 			    "so-sndbuf", iscsi_net->tweaks.sndbuf);
490 
491 			(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
492 			    "tcp-nodelay", iscsi_net->tweaks.nodelay);
493 
494 			(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
495 			    "tcp-conn-notify-threshold",
496 			    iscsi_net->tweaks.conn_notify_threshold);
497 
498 			(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
499 			    "tcp-conn-abort-threshold",
500 			    iscsi_net->tweaks.conn_abort_threshold);
501 
502 			(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
503 			    "tcp-abort-threshold",
504 			    iscsi_net->tweaks.abort_threshold);
505 
506 			(void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip,
507 			    "config-storm-delay",
508 			    ihp->hba_config_storm_delay);
509 
510 			/* setup hba defaults */
511 			iscsi_set_default_login_params(&ihp->hba_params);
512 			iscsi_set_default_tunable_params(
513 			    &ihp->hba_tunable_params);
514 
515 			/* setup minimal initiator params */
516 			iscsid_set_default_initiator_node_settings(ihp, B_TRUE);
517 
518 			/* hba set up */
519 			tran->tran_hba_private  = ihp;
520 			tran->tran_tgt_private  = NULL;
521 			tran->tran_tgt_init	= iscsi_tran_lun_init;
522 			tran->tran_tgt_probe	= iscsi_tran_lun_probe;
523 			tran->tran_tgt_free	= iscsi_tran_lun_free;
524 			tran->tran_start	= iscsi_tran_start;
525 			tran->tran_abort	= iscsi_tran_abort;
526 			tran->tran_reset	= iscsi_tran_reset;
527 			tran->tran_getcap	= iscsi_tran_getcap;
528 			tran->tran_setcap	= iscsi_tran_setcap;
529 			tran->tran_init_pkt	= iscsi_tran_init_pkt;
530 			tran->tran_destroy_pkt	= iscsi_tran_destroy_pkt;
531 			tran->tran_dmafree	= iscsi_tran_dmafree;
532 			tran->tran_sync_pkt	= iscsi_tran_sync_pkt;
533 			tran->tran_reset_notify	= iscsi_tran_reset_notify;
534 			tran->tran_bus_config	= iscsi_tran_bus_config;
535 			tran->tran_bus_unconfig	= iscsi_tran_bus_unconfig;
536 
537 			tran->tran_get_name	= iscsi_tran_get_name;
538 			tran->tran_get_bus_addr	= iscsi_tran_get_bus_addr;
539 			tran->tran_interconnect_type = INTERCONNECT_ISCSI;
540 
541 			/* register scsi hba with scsa */
542 			if (scsi_hba_attach_setup(dip, &iscsi_dma_attr,
543 			    tran, SCSI_HBA_TRAN_CLONE) != DDI_SUCCESS) {
544 				goto iscsi_attach_failed1;
545 			}
546 
547 			/* register scsi hba with mdi (MPxIO/vhci) */
548 			if (mdi_phci_register(MDI_HCI_CLASS_SCSI, dip, 0) !=
549 			    MDI_SUCCESS) {
550 				ihp->hba_mpxio_enabled = B_FALSE;
551 			} else {
552 				ihp->hba_mpxio_enabled = B_TRUE;
553 			}
554 
555 			(void) iscsi_hba_kstat_init(ihp);
556 
557 			/* Initialize targetparam list */
558 			iscsi_targetparam_init();
559 
560 			/* Initialize ISID */
561 			ihp->hba_isid[0] = ISCSI_SUN_ISID_0;
562 			ihp->hba_isid[1] = ISCSI_SUN_ISID_1;
563 			ihp->hba_isid[2] = ISCSI_SUN_ISID_2;
564 			ihp->hba_isid[3] = ISCSI_SUN_ISID_3;
565 			ihp->hba_isid[4] = ISCSI_SUN_ISID_4;
566 			ihp->hba_isid[5] = ISCSI_SUN_ISID_5;
567 
568 			/* Setup iSNS transport services and client */
569 			isns_client_init();
570 
571 			/*
572 			 * initialize persistent store,
573 			 * or boot target info in case of iscsi boot
574 			 */
575 			ihp->hba_persistent_loaded = B_FALSE;
576 			if (iscsid_init(ihp) == B_FALSE) {
577 				goto iscsi_attach_failed0;
578 			}
579 
580 			/* Setup init_port_name for MPAPI */
581 			(void) snprintf(init_port_name, MAX_NAME_PROP_SIZE,
582 			    "%s,%02x%02x%02x%02x%02x%02x",
583 			    (char *)ihp->hba_name, ihp->hba_isid[0],
584 			    ihp->hba_isid[1], ihp->hba_isid[2],
585 			    ihp->hba_isid[3], ihp->hba_isid[4],
586 			    ihp->hba_isid[5]);
587 
588 			if (ddi_prop_update_string(DDI_DEV_T_NONE, dip,
589 			    SCSI_ADDR_PROP_INITIATOR_PORT, init_port_name) !=
590 			    DDI_PROP_SUCCESS) {
591 				cmn_err(CE_WARN, "iscsi_attach: Creating "
592 				    SCSI_ADDR_PROP_INITIATOR_PORT
593 				    " property on iSCSI "
594 				    "HBA(%s) with dip(%d) Failed",
595 				    (char *)ihp->hba_name,
596 				    ddi_get_instance(dip));
597 			}
598 
599 			ddi_report_dev(dip);
600 		} else {
601 			rval = DDI_FAILURE;
602 		}
603 		break;
604 
605 iscsi_attach_failed0:
606 		isns_client_cleanup();
607 		if (ihp->stats.ks) {
608 			(void) iscsi_hba_kstat_term(ihp);
609 		}
610 		if (ihp->hba_mpxio_enabled == B_TRUE) {
611 			(void) mdi_phci_unregister(dip, 0);
612 		}
613 		(void) scsi_hba_detach(ihp->hba_dip);
614 iscsi_attach_failed1:
615 		ddi_remove_minor_node(dip, NULL);
616 		ddi_prop_remove_all(ihp->hba_dip);
617 		scsi_hba_tran_free(tran);
618 iscsi_attach_failed2:
619 		cv_destroy(&ihp->hba_service_cv);
620 		mutex_destroy(&ihp->hba_service_lock);
621 		mutex_destroy(&ihp->hba_discovery_events_mutex);
622 		sema_destroy(&ihp->hba_sendtgts_semaphore);
623 		rw_destroy(&ihp->hba_sess_list_rwlock);
624 		ddi_soft_state_free(iscsi_state, instance);
625 		rval = DDI_FAILURE;
626 		break;
627 
628 	case DDI_RESUME:
629 		break;
630 
631 	default:
632 		rval = DDI_FAILURE;
633 	}
634 
635 	if (rval != DDI_SUCCESS) {
636 		cmn_err(CE_WARN, "iscsi driver unable to attach "
637 		    "hba instance %d", instance);
638 	}
639 
640 	return (rval);
641 }
642 
643 /*
644  * iscsi_detach - called on unload of hba instance
645  */
646 static int
647 iscsi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
648 {
649 	int			rval		= DDI_SUCCESS;
650 	scsi_hba_tran_t		*tran		= NULL;
651 	iscsi_hba_t		*ihp		= NULL;
652 	iscsi_hba_t		*ihp_check	= NULL;
653 	int			instance;
654 	char			*init_node_name;
655 
656 	instance = ddi_get_instance(dip);
657 
658 	switch (cmd) {
659 	case DDI_DETACH:
660 		if (!(tran = (scsi_hba_tran_t *)ddi_get_driver_private(dip))) {
661 			rval = DDI_SUCCESS;
662 			break;
663 		}
664 
665 		if ((ihp = (iscsi_hba_t *)tran->tran_hba_private) == NULL) {
666 			rval =  DDI_FAILURE;
667 			break;
668 		}
669 
670 		/*
671 		 * Validate that what is stored by the DDI framework is still
672 		 * the same state structure referenced by the SCSI framework
673 		 */
674 		ihp_check = ddi_get_soft_state(iscsi_state, instance);
675 		if (ihp_check != ihp) {
676 			rval = DDI_FAILURE;
677 			break;
678 		}
679 
680 		/* If a session exists we can't safely detach */
681 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
682 		if (ihp->hba_sess_list != NULL) {
683 			rw_exit(&ihp->hba_sess_list_rwlock);
684 			rval = DDI_FAILURE;
685 			break;
686 		}
687 		rw_exit(&ihp->hba_sess_list_rwlock);
688 
689 		/* Disable all discovery services */
690 		if (iscsid_disable_discovery(ihp,
691 		    ISCSI_ALL_DISCOVERY_METHODS) == B_FALSE) {
692 			/* Disable failed.  Fail detach */
693 			rval = DDI_FAILURE;
694 			break;
695 		}
696 
697 		/* Deregister from iSNS server(s). */
698 		init_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
699 		if (persistent_initiator_name_get(init_node_name,
700 		    ISCSI_MAX_NAME_LEN) == B_TRUE) {
701 			if (strlen(init_node_name) > 0) {
702 				(void) isns_dereg(ihp->hba_isid,
703 				    (uint8_t *)init_node_name);
704 			}
705 		}
706 		kmem_free(init_node_name, ISCSI_MAX_NAME_LEN);
707 		init_node_name = NULL;
708 
709 		/* Cleanup iSNS Client */
710 		isns_client_cleanup();
711 
712 		iscsi_targetparam_cleanup();
713 
714 		/* Cleanup iscsid resources */
715 		iscsid_fini();
716 
717 		if (rval != DDI_SUCCESS) {
718 			break;
719 		}
720 		/* kstat hba. destroy */
721 		KSTAT_DEC_HBA_CNTR_SESS(ihp);
722 
723 		if (ihp->hba_mpxio_enabled == B_TRUE) {
724 			(void) mdi_phci_unregister(dip, 0);
725 		}
726 		ddi_remove_minor_node(dip, NULL);
727 
728 		ddi_prop_remove_all(ihp->hba_dip);
729 
730 		ldi_ident_release(ihp->hba_li);
731 
732 		cv_destroy(&ihp->hba_service_cv);
733 		mutex_destroy(&ihp->hba_service_lock);
734 		mutex_destroy(&ihp->hba_discovery_events_mutex);
735 		rw_destroy(&ihp->hba_sess_list_rwlock);
736 		(void) iscsi_hba_kstat_term(ihp);
737 
738 		(void) scsi_hba_detach(dip);
739 		if (tran != NULL) {
740 			scsi_hba_tran_free(tran);
741 		}
742 		ddi_soft_state_free(iscsi_state, instance);
743 		break;
744 	default:
745 		break;
746 	}
747 
748 	if (rval != DDI_SUCCESS) {
749 		cmn_err(CE_WARN, "iscsi driver unable to "
750 		    "detach hba instance %d", instance);
751 	}
752 
753 	return (rval);
754 }
755 
756 /*
757  * +--------------------------------------------------------------------+
758  * | End of dev_ops routines						|
759  * +--------------------------------------------------------------------+
760  */
761 
762 /*
763  * +--------------------------------------------------------------------+
764  * | scsi_tran(9E) routines						|
765  * +--------------------------------------------------------------------+
766  */
767 
768 /*
769  * iscsi_tran_lun_init - Find target device based on SCSI device
770  * Based on the information given (SCSI device, target dev_info) find
771  * the target iSCSI device and put a pointer to that information in
772  * the scsi_hba_tran_t structure.
773  */
774 static int
775 iscsi_tran_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
776     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
777 {
778 	int		rval	= 0;
779 	int		type	= 0;
780 
781 	ASSERT(hba_tran->tran_hba_private != NULL);
782 
783 	/*
784 	 * Child node is getting initialized.  Look at the mpxio component
785 	 * type on the child device to see if this device is mpxio managed
786 	 * or not.
787 	 */
788 	type = mdi_get_component_type(lun_dip);
789 	if (type != MDI_COMPONENT_CLIENT) {
790 		rval = iscsi_phys_lun_init(hba_dip, lun_dip, hba_tran, sd);
791 	} else {
792 		rval = iscsi_virt_lun_init(hba_dip, lun_dip, hba_tran, sd);
793 	}
794 
795 	return (rval);
796 }
797 
798 /*
799  * iscsi_tran_lun_probe - This function didn't need to be implemented.
800  * We could have left NULL in the tran table.  Since this isn't a
801  * performance path this seems safe.  We are just wrappering the
802  * function so we can see the call go through if we have debugging
803  * enabled.
804  */
805 static int
806 iscsi_tran_lun_probe(struct scsi_device *sd, int (*callback) ())
807 {
808 	int rval = 0;
809 
810 	rval = scsi_hba_probe(sd, callback);
811 
812 	return (rval);
813 }
814 
815 /*
816  * iscsi_init_pkt - Allocate SCSI packet and fill in required info.
817  */
818 /* ARGSUSED */
819 static struct scsi_pkt *
820 iscsi_tran_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
821     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
822     int (*callback) (), caddr_t arg)
823 {
824 	iscsi_lun_t *ilp;
825 	iscsi_cmd_t *icmdp;
826 
827 	ASSERT(ap != NULL);
828 	ASSERT(callback == NULL_FUNC || callback == SLEEP_FUNC);
829 
830 	/*
831 	 * The software stack doesn't have DMA which means the iSCSI
832 	 * protocol layer will be doing a bcopy from bp to outgoing
833 	 * streams buffers. Make sure that the buffer is mapped in
834 	 * so that the copy won't panic the system.
835 	 */
836 	if (bp && (bp->b_bcount != 0) &&
837 	    bp_mapin_common(bp, (callback == NULL_FUNC) ?
838 	    VM_NOSLEEP : VM_SLEEP) == NULL) {
839 		return (NULL);
840 	}
841 
842 	ilp = (iscsi_lun_t *)ap->a_hba_tran->tran_tgt_private;
843 	ASSERT(ilp != NULL);
844 
845 	if (pkt == NULL) {
846 		pkt = scsi_hba_pkt_alloc(ilp->lun_sess->sess_hba->hba_dip,
847 		    ap, cmdlen, statuslen, tgtlen, sizeof (iscsi_cmd_t),
848 		    callback, arg);
849 		if (pkt == NULL) {
850 			return (NULL);
851 		}
852 		icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private;
853 		icmdp->cmd_sig			= ISCSI_SIG_CMD;
854 		icmdp->cmd_state		= ISCSI_CMD_STATE_FREE;
855 		icmdp->cmd_lun			= ilp;
856 		icmdp->cmd_type			= ISCSI_CMD_TYPE_SCSI;
857 		/* add the report lun addressing type on to the lun */
858 		icmdp->cmd_un.scsi.lun		= ilp->lun_addr_type << 14;
859 		icmdp->cmd_un.scsi.lun		= icmdp->cmd_un.scsi.lun |
860 		    ilp->lun_num;
861 		icmdp->cmd_un.scsi.pkt		= pkt;
862 		icmdp->cmd_un.scsi.bp		= bp;
863 		icmdp->cmd_un.scsi.cmdlen	= cmdlen;
864 		icmdp->cmd_un.scsi.statuslen	= statuslen;
865 		icmdp->cmd_crc_error_seen	= B_FALSE;
866 		icmdp->cmd_misc_flags		= 0;
867 		if (flags & PKT_XARQ) {
868 			icmdp->cmd_misc_flags |= ISCSI_CMD_MISCFLAG_XARQ;
869 		}
870 
871 
872 		idm_sm_audit_init(&icmdp->cmd_state_audit);
873 
874 		mutex_init(&icmdp->cmd_mutex, NULL, MUTEX_DRIVER, NULL);
875 		cv_init(&icmdp->cmd_completion, NULL, CV_DRIVER, NULL);
876 
877 		pkt->pkt_address		= *ap;
878 		pkt->pkt_comp			= (void (*)())NULL;
879 		pkt->pkt_flags			= 0;
880 		pkt->pkt_time			= 0;
881 		pkt->pkt_resid			= 0;
882 		pkt->pkt_statistics		= 0;
883 		pkt->pkt_reason			= 0;
884 	}
885 	return (pkt);
886 }
887 
888 /*
889  * iscsi_tran_lun_free - Free a SCSI LUN
890  */
891 static void
892 iscsi_tran_lun_free(dev_info_t *hba_dip, dev_info_t *lun_dip,
893     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
894 {
895 	iscsi_lun_t *ilp = NULL;
896 
897 	ASSERT(hba_dip != NULL);
898 	ASSERT(lun_dip != NULL);
899 	ASSERT(hba_tran != NULL);
900 	ASSERT(sd != NULL);
901 	ilp = (iscsi_lun_t *)hba_tran->tran_tgt_private;
902 	ASSERT(ilp != NULL);
903 
904 	(void) mdi_prop_remove(ilp->lun_pip, NULL);
905 }
906 
907 /*
908  * iscsi_start -- Start a SCSI transaction based on the packet
909  * This will attempt to add the icmdp to the pending queue
910  * for the connection and kick the queue.  If the enqueue
911  * fails that means the queue is full.
912  */
913 static int
914 iscsi_tran_start(struct scsi_address *ap, struct scsi_pkt *pkt)
915 {
916 	iscsi_lun_t	*ilp		= NULL;
917 	iscsi_sess_t	*isp		= NULL;
918 	iscsi_cmd_t	*icmdp		= NULL;
919 	uint_t		flags;
920 
921 	ASSERT(ap != NULL);
922 	ASSERT(pkt != NULL);
923 	ilp = (iscsi_lun_t *)ap->a_hba_tran->tran_tgt_private;
924 	isp = (iscsi_sess_t *)ilp->lun_sess;
925 	icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private;
926 	flags = pkt->pkt_flags;
927 	ASSERT(ilp != NULL);
928 	ASSERT(isp != NULL);
929 	ASSERT(icmdp != NULL);
930 
931 	/*
932 	 * If the session is in the FREE state then
933 	 * all connections are down and retries have
934 	 * been exhausted.  Fail command with fatal error.
935 	 */
936 	mutex_enter(&isp->sess_state_mutex);
937 	if (isp->sess_state == ISCSI_SESS_STATE_FREE) {
938 		mutex_exit(&isp->sess_state_mutex);
939 		return (TRAN_FATAL_ERROR);
940 	}
941 
942 	/*
943 	 * If the session is not in LOGGED_IN then we have
944 	 * no connections LOGGED_IN, but we haven't exhuasted
945 	 * our retries.  Fail the command with busy so the
946 	 * caller might try again later.  Once retries are
947 	 * exhausted the state machine will move us to FREE.
948 	 */
949 	if (isp->sess_state != ISCSI_SESS_STATE_LOGGED_IN) {
950 		mutex_exit(&isp->sess_state_mutex);
951 		return (TRAN_BUSY);
952 	}
953 
954 	/*
955 	 * If we haven't received data from the target in the
956 	 * max specified period something is wrong with the
957 	 * transport.  Fail IO with FATAL_ERROR.
958 	 */
959 	if (isp->sess_rx_lbolt + SEC_TO_TICK(iscsi_rx_max_window) <
960 	    ddi_get_lbolt()) {
961 		mutex_exit(&isp->sess_state_mutex);
962 		return (TRAN_FATAL_ERROR);
963 	}
964 
965 	/*
966 	 * If we haven't received data from the target in the
967 	 * specified period something is probably wrong with
968 	 * the transport.  Just return back BUSY until either
969 	 * the problem is resolved of the transport fails.
970 	 */
971 	if (isp->sess_rx_lbolt + SEC_TO_TICK(iscsi_rx_window) <
972 	    ddi_get_lbolt()) {
973 		mutex_exit(&isp->sess_state_mutex);
974 		return (TRAN_BUSY);
975 	}
976 
977 
978 	/* reset cmd values in case upper level driver is retrying cmd */
979 	icmdp->cmd_prev = icmdp->cmd_next = NULL;
980 	icmdp->cmd_crc_error_seen = B_FALSE;
981 	icmdp->cmd_lbolt_pending = icmdp->cmd_lbolt_active =
982 	    icmdp->cmd_lbolt_aborting = icmdp->cmd_lbolt_timeout =
983 	    (clock_t)NULL;
984 	icmdp->cmd_itt = icmdp->cmd_ttt = 0;
985 	icmdp->cmd_un.scsi.abort_icmdp = NULL;
986 
987 	mutex_enter(&isp->sess_queue_pending.mutex);
988 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp);
989 	mutex_exit(&isp->sess_queue_pending.mutex);
990 	mutex_exit(&isp->sess_state_mutex);
991 
992 	/*
993 	 * If this packet doesn't have FLAG_NOINTR set, it could have
994 	 * already run to completion (and the memory freed) at this
995 	 * point, so check our local copy of pkt_flags.  Otherwise we
996 	 * have to wait for completion before returning to the caller.
997 	 */
998 	if (flags & FLAG_NOINTR) {
999 		mutex_enter(&icmdp->cmd_mutex);
1000 		while ((icmdp->cmd_state != ISCSI_CMD_STATE_COMPLETED) ||
1001 		    (icmdp->cmd_un.scsi.r2t_icmdp != NULL) ||
1002 		    (icmdp->cmd_un.scsi.abort_icmdp != NULL) ||
1003 		    (icmdp->cmd_un.scsi.r2t_more == B_TRUE)) {
1004 			cv_wait(&icmdp->cmd_completion, &icmdp->cmd_mutex);
1005 		}
1006 		icmdp->cmd_state = ISCSI_CMD_STATE_FREE;
1007 		mutex_exit(&icmdp->cmd_mutex);
1008 	}
1009 
1010 	return (TRAN_ACCEPT);
1011 }
1012 
1013 /*
1014  * iscsi_tran_abort - Called when an upper level application
1015  * or driver wants to kill a scsi_pkt that was already sent to
1016  * this driver.
1017  */
1018 /* ARGSUSED */
1019 static int
1020 iscsi_tran_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
1021 {
1022 	return (0);
1023 }
1024 
1025 /*
1026  * iscsi_tran_reset - Reset target at either BUS, TARGET, or LUN
1027  * level.  This will require the issuing of a task management
1028  * command down to the target/lun.
1029  */
1030 static int
1031 iscsi_tran_reset(struct scsi_address *ap, int level)
1032 {
1033 	int		rval    = ISCSI_STATUS_INTERNAL_ERROR;
1034 	iscsi_sess_t	*isp    = NULL;
1035 	iscsi_lun_t	*ilp    = NULL;
1036 
1037 	ilp = (iscsi_lun_t *)ap->a_hba_tran->tran_tgt_private;
1038 	ASSERT(ilp != NULL);
1039 	isp = ilp->lun_sess;
1040 	ASSERT(isp != NULL);
1041 
1042 	switch (level) {
1043 	case RESET_LUN:
1044 		/* reset attempt will block until attempt is complete */
1045 		rval = iscsi_handle_reset(isp, level, ilp);
1046 		break;
1047 	case RESET_BUS:
1048 		/*
1049 		 * What are we going to realy reset the ethernet
1050 		 * network!?  Just fall through to a target reset.
1051 		 */
1052 	case RESET_TARGET:
1053 		/* reset attempt will block until attempt is complete */
1054 		rval = iscsi_handle_reset(isp, level, NULL);
1055 		break;
1056 	case RESET_ALL:
1057 	default:
1058 		break;
1059 	}
1060 
1061 	return (ISCSI_SUCCESS(rval) ? 1 : 0);
1062 }
1063 
1064 /*
1065  * iscsi_tran_getcap - Get target/lun capabilities.
1066  */
1067 static int
1068 iscsi_tran_getcap(struct scsi_address *ap, char *cap, int whom)
1069 {
1070 	return (iscsi_i_commoncap(ap, cap, 0, whom, 0));
1071 }
1072 
1073 
1074 /*
1075  * iscsi_tran_setcap - Set target/lun capabilities.
1076  */
1077 /* ARGSUSED */
1078 static int
1079 iscsi_tran_setcap(struct scsi_address *ap, char *cap, int value, int whom)
1080 {
1081 	return (iscsi_i_commoncap(ap, cap, value, whom, 1));
1082 }
1083 
1084 
1085 /*
1086  * iscsi_tran_destroy_pkt - Clean up packet
1087  */
1088 static void
1089 iscsi_tran_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
1090 {
1091 	iscsi_cmd_t	*icmdp;
1092 
1093 	icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private;
1094 
1095 	ASSERT(icmdp != NULL);
1096 	ASSERT(icmdp->cmd_sig == ISCSI_SIG_CMD);
1097 	ASSERT(icmdp->cmd_state == ISCSI_CMD_STATE_FREE);
1098 
1099 	mutex_destroy(&icmdp->cmd_mutex);
1100 	cv_destroy(&icmdp->cmd_completion);
1101 	scsi_hba_pkt_free(ap, pkt);
1102 }
1103 
1104 /*
1105  * iscsi_tran_dmafree - This is a software driver, NO DMA
1106  */
1107 /* ARGSUSED */
1108 static void
1109 iscsi_tran_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
1110 {
1111 	/*
1112 	 * The iSCSI interface doesn't deal with DMA
1113 	 */
1114 }
1115 
1116 /*
1117  * iscsi_tran_sync_pkt - This is a software driver, NO DMA
1118  */
1119 /* ARGSUSED */
1120 static void
1121 iscsi_tran_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
1122 {
1123 	/*
1124 	 * The iSCSI interface doesn't deal with DMA
1125 	 */
1126 }
1127 
1128 /*
1129  * iscsi_tran_reset_notify - We don't support BUS_RESET so there
1130  * is no point in support callback.
1131  */
1132 /* ARGSUSED */
1133 static int
1134 iscsi_tran_reset_notify(struct scsi_address *ap, int flag,
1135     void (*callback) (caddr_t), caddr_t arg)
1136 {
1137 
1138 	/*
1139 	 * We never do BUS_RESETS so allowing this call
1140 	 * back to register has no point?
1141 	 */
1142 	return (DDI_SUCCESS);
1143 }
1144 
1145 
1146 /*
1147  * iscsi_tran_bus_config - on demand device configuration
1148  *
1149  * iscsi_tran_bus_config is called by the NDI layer at the completion
1150  * of a dev_node creation.  There are two primary cases defined in this
1151  * function.  The first is BUS_CONFIG_ALL.  In this case the NDI is trying
1152  * to identify that targets/luns are available configured at that point
1153  * in time.  It is safe to just complete the process succcessfully.  The
1154  * second case is a new case that was defined in S10 for devfs.  BUS_CONFIG_ONE
1155  * this is to help driver the top down discovery instead of bottom up.  If
1156  * we receive a BUS_CONFIG_ONE we should check to see if the <addr> exists
1157  * if so complete successfull processing.  Otherwise we should call the
1158  * deamon and see if we can plumb the <addr>.  If it is possible to plumb the
1159  * <addr> block until plumbing is complete.  In both cases of being able to
1160  * plumb <addr> or not continue with successfull processing.
1161  */
1162 static int
1163 iscsi_tran_bus_config(dev_info_t *parent, uint_t flags,
1164     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
1165 {
1166 	int		rval	= NDI_SUCCESS;
1167 	iscsi_hba_t	*ihp	= NULL;
1168 	int		iflags	= flags;
1169 	char		*name	= NULL;
1170 	char		*ptr	= NULL;
1171 	boolean_t	config_root = B_FALSE;
1172 
1173 	/* get reference to soft state */
1174 	ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state,
1175 	    ddi_get_instance(parent));
1176 	if (ihp == NULL) {
1177 		return (NDI_FAILURE);
1178 	}
1179 
1180 	iscsi_check_miniroot(ihp);
1181 	if ((modrootloaded == 0) && (iscsiboot_prop != NULL)) {
1182 		config_root = B_TRUE;
1183 	}
1184 
1185 	if (config_root == B_FALSE) {
1186 		if (iscsi_client_request_service(ihp) == B_FALSE) {
1187 			return (NDI_FAILURE);
1188 		}
1189 	}
1190 
1191 	/* lock so only one config operation occrs */
1192 	sema_p(&iscsid_config_semaphore);
1193 
1194 	switch (op) {
1195 	case BUS_CONFIG_ONE:
1196 		/* parse target name out of name given */
1197 		if ((ptr = strchr((char *)arg, '@')) == NULL) {
1198 			rval = NDI_FAILURE;
1199 			break;
1200 		}
1201 		ptr++;		/* move past '@' */
1202 		name = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP);
1203 		(void) strncpy(name, ptr, MAX_GET_NAME_SIZE);
1204 		/* We need to strip the LUN */
1205 		if ((ptr = strchr(name, ',')) == NULL) {
1206 			rval = NDI_FAILURE;
1207 			kmem_free(name, MAX_GET_NAME_SIZE);
1208 			name = NULL;
1209 			break;
1210 		}
1211 		/* We also need to strip the 4 bytes of hex TPGT */
1212 		ptr -= 4;
1213 		if (ptr <= name) {
1214 			rval = NDI_FAILURE;
1215 			kmem_free(name, MAX_GET_NAME_SIZE);
1216 			name = NULL;
1217 			break;
1218 		}
1219 		*ptr = '\0';		/* NULL terminate */
1220 
1221 		/* translate name back to original iSCSI name */
1222 		iscsi_get_name_to_iqn(name, MAX_GET_NAME_SIZE);
1223 
1224 		/* configure target, skip 4 byte ISID */
1225 		iscsid_config_one(ihp, (name+4), B_TRUE);
1226 
1227 		kmem_free(name, MAX_GET_NAME_SIZE);
1228 		name = NULL;
1229 
1230 		/*
1231 		 * DDI group instructed us to use this flag.
1232 		 */
1233 		iflags |= NDI_MDI_FALLBACK;
1234 		break;
1235 	case BUS_CONFIG_DRIVER:
1236 		/* FALLTHRU */
1237 	case BUS_CONFIG_ALL:
1238 		iscsid_config_all(ihp, B_TRUE);
1239 		break;
1240 	default:
1241 		rval = NDI_FAILURE;
1242 		break;
1243 	}
1244 
1245 	if (rval == NDI_SUCCESS) {
1246 		rval = ndi_busop_bus_config(parent, iflags,
1247 		    op, arg, childp, 0);
1248 	}
1249 	sema_v(&iscsid_config_semaphore);
1250 
1251 	if (config_root == B_FALSE) {
1252 		iscsi_client_release_service(ihp);
1253 	}
1254 
1255 	return (rval);
1256 }
1257 
1258 /*
1259  * iscsi_tran_bus_unconfig - on demand device unconfiguration
1260  *
1261  * Called by the os framework under low resource situations.
1262  * It will attempt to unload our minor nodes (logical units
1263  * ndi/mdi nodes).
1264  */
1265 static int
1266 iscsi_tran_bus_unconfig(dev_info_t *parent, uint_t flag,
1267     ddi_bus_config_op_t op, void *arg)
1268 {
1269 	int		rval = NDI_SUCCESS;
1270 	iscsi_hba_t	*ihp = NULL;
1271 
1272 	/* get reference to soft state */
1273 	ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state,
1274 	    ddi_get_instance(parent));
1275 	if (ihp == NULL) {
1276 		return (NDI_FAILURE);
1277 	}
1278 
1279 	if (iscsi_client_request_service(ihp) == B_FALSE) {
1280 		return (NDI_FAILURE);
1281 	}
1282 
1283 	rval = ndi_busop_bus_unconfig(parent, flag, op, arg);
1284 
1285 	iscsi_client_release_service(ihp);
1286 
1287 	return (rval);
1288 }
1289 
1290 
1291 /*
1292  * iscsi_tran_get_name - create private /devices name for LUN
1293  *
1294  * This creates the <addr> in /devices/iscsi/<driver>@<addr>
1295  * path.  For this <addr> we return the <session/target_name>,<lun num>
1296  * Where <target_name> is an <iqn/eui/...> as defined by the iSCSI
1297  * specification.  We do modify the name slightly so that it still
1298  * complies with the IEEE <addr> naming scheme.  This means that we
1299  * will substitute out the ':', '@', ... and other reserved characters
1300  * defined in the IEEE definition with '%<hex value of special char>'
1301  * This routine is indirectly called by iscsi_lun_create_xxx.  These
1302  * calling routines must prevent the session and lun lists from changing
1303  * during this routine.
1304  */
1305 static int
1306 iscsi_tran_get_name(struct scsi_device *sd, char *name, int len)
1307 {
1308 	int		target		= 0;
1309 	int		lun		= 0;
1310 	iscsi_hba_t	*ihp		= NULL;
1311 	iscsi_sess_t	*isp		= NULL;
1312 	iscsi_lun_t	*ilp		= NULL;
1313 	dev_info_t	*lun_dip	= NULL;
1314 
1315 	ASSERT(sd != NULL);
1316 	ASSERT(name != NULL);
1317 	lun_dip = sd->sd_dev;
1318 	ASSERT(lun_dip != NULL);
1319 
1320 	/* get reference to soft state */
1321 	ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state,
1322 	    ddi_get_instance(ddi_get_parent(lun_dip)));
1323 	if (ihp == NULL) {
1324 		name[0] = '\0';
1325 		return (0);
1326 	}
1327 
1328 	/* Get the target num */
1329 	target = ddi_prop_get_int(DDI_DEV_T_ANY, sd->sd_dev,
1330 	    DDI_PROP_DONTPASS, TARGET_PROP, 0);
1331 
1332 	/* Get the target num */
1333 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, sd->sd_dev,
1334 	    DDI_PROP_DONTPASS, LUN_PROP, 0);
1335 
1336 	/*
1337 	 * Now we need to find our ilp by walking the lists
1338 	 * off the ihp and isp.
1339 	 */
1340 	/* See if we already created this session */
1341 
1342 	/* Walk the HBA's session list */
1343 	for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
1344 		/* compare target name as the unique identifier */
1345 		if (target == isp->sess_oid) {
1346 			/* found match */
1347 			break;
1348 		}
1349 	}
1350 
1351 	/* If we found matching session continue searching for tgt */
1352 	if (isp == NULL) {
1353 		/* sess not found */
1354 		name[0] = '\0';
1355 		return (0);
1356 	}
1357 
1358 	/*
1359 	 * Search for the matching iscsi lun structure.  We don't
1360 	 * need to hold the READER for the lun list at this point.
1361 	 * because the tran_get_name is being called from the online
1362 	 * function which is already holding a reader on the lun
1363 	 * list.
1364 	 */
1365 	for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) {
1366 		if (lun == ilp->lun_num) {
1367 			/* found match */
1368 			break;
1369 		}
1370 	}
1371 
1372 	if (ilp == NULL) {
1373 		/* tgt not found */
1374 		name[0] = '\0';
1375 		return (0);
1376 	}
1377 
1378 	/* Ensure enough space for lun_addr is available */
1379 	ASSERT(ilp->lun_addr != NULL);
1380 	if ((strlen(ilp->lun_addr) + 1) > len) {
1381 		return (0);
1382 	}
1383 
1384 	/* copy lun_addr name */
1385 	(void) strcpy(name, ilp->lun_addr);
1386 
1387 	/*
1388 	 * Based on IEEE-1275 we can't have any ':', ' ', '@', or '/'
1389 	 * characters in our naming.  So replace all those characters
1390 	 * with '-'
1391 	 */
1392 	iscsi_get_name_from_iqn(name, len);
1393 
1394 	return (1);
1395 }
1396 
1397 /*
1398  * iscsi_tran_get_bus_addr - This returns a human readable string
1399  * for the bus address.  Examining most other drivers fcp, etc.  They
1400  * all just return the same string as tran_get_name.  In our case
1401  * our tran get name is already some what usable so leave alone.
1402  */
1403 static int
1404 iscsi_tran_get_bus_addr(struct scsi_device *sd, char *name, int len)
1405 {
1406 	return (iscsi_tran_get_name(sd, name, len));
1407 }
1408 
1409 
1410 /*
1411  * +--------------------------------------------------------------------+
1412  * | End of scsi_tran routines					  |
1413  * +--------------------------------------------------------------------+
1414  */
1415 
1416 /*
1417  * +--------------------------------------------------------------------+
1418  * | Start of cb_ops routines					   |
1419  * +--------------------------------------------------------------------+
1420  */
1421 
1422 /*
1423  * iscsi_open - Driver should be made IOCTL MT safe.  Otherwise
1424  * this function needs updated.
1425  */
1426 /* ARGSUSED */
1427 static int
1428 iscsi_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1429 {
1430 	return (0);
1431 }
1432 
1433 /*
1434  * iscsi_close -
1435  */
1436 /* ARGSUSED */
1437 static int
1438 iscsi_close(dev_t dev, int flags, int otyp, cred_t *credp)
1439 {
1440 	return (0);
1441 }
1442 
1443 /*
1444  * iscsi_ioctl -
1445  */
1446 /* ARGSUSED */
1447 static int
1448 iscsi_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
1449     cred_t *credp, int *rvalp)
1450 {
1451 	int			rtn		= 0;
1452 	int			instance	= 0;
1453 	int			list_space	= 0;
1454 	int			lun_sz		= 0;
1455 	int			did;
1456 	int			retry;
1457 	iscsi_hba_t		*ihp		= NULL;
1458 	iscsi_sess_t		*isp		= NULL;
1459 	iscsi_conn_t		*icp		= NULL;
1460 	iscsi_login_params_t	*params		= NULL;
1461 	iscsi_login_params_t	*tmpParams	= NULL;
1462 	uchar_t			*name		= NULL;
1463 	dev_info_t		*lun_dip	= NULL;
1464 
1465 	entry_t			    e;
1466 	iscsi_oid_t		    oid;
1467 	iscsi_property_t	    *ipp;
1468 	iscsi_static_property_t	    *ispp;
1469 	iscsi_param_get_t	    *ilg;
1470 	iscsi_param_set_t	    *ils;
1471 	iscsi_target_list_t	    idl, *idlp		= NULL;
1472 	iscsi_addr_list_t	    ial, *ialp		= NULL;
1473 	iscsi_chap_props_t	    *chap		= NULL;
1474 	iscsi_radius_props_t	    *radius		= NULL;
1475 	iscsi_auth_props_t	    *auth		= NULL;
1476 	iscsi_lun_list_t	    *ll, *llp		= NULL;
1477 	iscsi_lun_props_t	    *lun		= NULL;
1478 	iscsi_lun_t		    *ilp 		= NULL;
1479 	iSCSIDiscoveryMethod_t	    method;
1480 	iSCSIDiscoveryProperties_t  discovery_props;
1481 	iscsi_uscsi_t		    iu;
1482 	iscsi_uscsi_t		    iu_caller;
1483 #ifdef _MULTI_DATAMODEL
1484 	/* For use when a 32 bit app makes a call into a 64 bit ioctl */
1485 	iscsi_uscsi32_t		    iu32_caller;
1486 	model_t			    model;
1487 #endif /* _MULTI_DATAMODEL */
1488 	void			    *void_p;
1489 	iscsi_sendtgts_list_t	*stl_hdr;
1490 	iscsi_sendtgts_list_t	*istl;
1491 	int			stl_sz;
1492 	iscsi_target_entry_t	*target;
1493 	uint32_t		old_oid;
1494 	uint32_t		target_oid;
1495 	iscsi_targetparam_entry_t *curr_entry;
1496 	char			*initiator_node_name;
1497 	char			*initiator_node_alias;
1498 	isns_portal_group_list_t    *pg_list = NULL;
1499 	isns_server_portal_group_list_t    *server_pg_list_hdr = NULL;
1500 	isns_server_portal_group_list_t    *server_pg_list = NULL;
1501 	int			pg_list_sz, pg_sz_copy_out, server_pg_list_sz;
1502 	iscsi_config_sess_t	*ics;
1503 	int			size;
1504 	boolean_t		rval;
1505 	char			init_port_name[MAX_NAME_PROP_SIZE];
1506 	iscsi_sockaddr_t	addr_dsc;
1507 	iscsi_boot_property_t	*bootProp;
1508 	boolean_t		discovered = B_TRUE;
1509 	iscsi_tunable_object_t	*tpsg;
1510 	iscsi_tunable_object_t	*tpss;
1511 
1512 	instance = getminor(dev);
1513 	ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state, instance);
1514 	if (ihp == NULL)
1515 		return (EFAULT);
1516 
1517 	iscsi_check_miniroot(ihp);
1518 	if ((cmd != ISCSI_SMF_ONLINE) && (cmd != ISCSI_SMF_OFFLINE) &&
1519 	    (cmd != ISCSI_SMF_GET)) {
1520 		/* other cmd needs to acquire the service */
1521 		if (iscsi_client_request_service(ihp) == B_FALSE) {
1522 			return (EFAULT);
1523 		}
1524 	}
1525 
1526 	switch (cmd) {
1527 	/*
1528 	 * ISCSI_CREATE_OID - Create a Object IDentifier for a TargetName
1529 	 */
1530 	case ISCSI_CREATE_OID:
1531 		if (ddi_copyin((caddr_t)arg, &oid, sizeof (oid), mode)) {
1532 			rtn = EFAULT;
1533 			break;
1534 		}
1535 		if (oid.o_vers != ISCSI_INTERFACE_VERSION) {
1536 			rtn = EINVAL;
1537 			break;
1538 		}
1539 
1540 		/* Set the target that this session is associated with */
1541 		oid.o_oid = iscsi_targetparam_get_oid(oid.o_name);
1542 
1543 		if (ddi_copyout(&oid, (caddr_t)arg, sizeof (oid), mode)) {
1544 			rtn = EFAULT;
1545 			break;
1546 		}
1547 		break;
1548 	/*
1549 	 * ISCSI_PARAM_GET - Get param for specified
1550 	 * connection/session.
1551 	 */
1552 	case ISCSI_PARAM_GET:
1553 		/* copyin user args */
1554 		ilg = (iscsi_param_get_t *)kmem_alloc(sizeof (*ilg), KM_SLEEP);
1555 		if (ddi_copyin((caddr_t)arg, ilg, sizeof (*ilg), mode)) {
1556 			rtn = EFAULT;
1557 			kmem_free(ilg, sizeof (*ilg));
1558 			break;
1559 		}
1560 
1561 		if (ilg->g_vers != ISCSI_INTERFACE_VERSION) {
1562 			rtn = EINVAL;
1563 			kmem_free(ilg, sizeof (*ilg));
1564 			break;
1565 		}
1566 
1567 		/* handle special case for Initiator name */
1568 		if (ilg->g_param == ISCSI_LOGIN_PARAM_INITIATOR_NAME) {
1569 			(void) strlcpy((char *)ilg->g_value.v_name,
1570 			    (char *)ihp->hba_name, ISCSI_MAX_NAME_LEN);
1571 		} else if (ilg->g_param == ISCSI_LOGIN_PARAM_INITIATOR_ALIAS) {
1572 			if (ihp->hba_alias_length == 0) {
1573 				rtn = EINVAL;
1574 			} else {
1575 				(void) strlcpy((char *)ilg->g_value.v_name,
1576 				    (char *)ihp->hba_alias, ISCSI_MAX_NAME_LEN);
1577 			}
1578 		} else {
1579 			/* To describe the validity of the requested param */
1580 			boolean_t valid_flag = B_TRUE;
1581 
1582 			name = NULL;
1583 
1584 			/*
1585 			 * switch login based if looking for initiator
1586 			 * params
1587 			 */
1588 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
1589 			if (ilg->g_oid == ihp->hba_oid) {
1590 				/* initiator */
1591 				params = &ihp->hba_params;
1592 				name = ihp->hba_name;
1593 				if (iscsi_get_persisted_param(name,
1594 				    ilg, params) != 0) {
1595 					valid_flag = B_FALSE;
1596 				}
1597 			} else {
1598 				/*
1599 				 * If the oid does represent a session check
1600 				 * to see if it is a target oid.  If so,
1601 				 * return the target's associated session.
1602 				 */
1603 				rtn = iscsi_sess_get(ilg->g_oid, ihp, &isp);
1604 				if (rtn != 0) {
1605 					rtn = iscsi_sess_get_by_target(
1606 					    ilg->g_oid, ihp, &isp);
1607 				}
1608 
1609 				/*
1610 				 * If rtn is zero then we have found an
1611 				 * existing session.  Use the session name to
1612 				 * do param lookup.  If rtn is non-zero then
1613 				 * create a targetparam object and use its name
1614 				 * for param lookup.
1615 				 */
1616 				if (rtn == 0) {
1617 					name = isp->sess_name;
1618 					params = &isp->sess_params;
1619 				} else {
1620 					name =
1621 					    iscsi_targetparam_get_name(
1622 					    ilg->g_oid);
1623 					if (ilg->g_param_type ==
1624 					    ISCSI_SESS_PARAM) {
1625 						tmpParams =
1626 						    (iscsi_login_params_t *)
1627 						    kmem_alloc(
1628 						    sizeof (*tmpParams),
1629 						    KM_SLEEP);
1630 						params = tmpParams;
1631 					}
1632 					rtn = 0;
1633 				}
1634 
1635 				if (name == NULL) {
1636 					rw_exit(
1637 					    &ihp->hba_sess_list_rwlock);
1638 					rtn = EFAULT;
1639 					kmem_free(ilg, sizeof (*ilg));
1640 					if (tmpParams != NULL)
1641 						kmem_free(tmpParams,
1642 						    sizeof (*tmpParams));
1643 
1644 					break;
1645 				}
1646 
1647 				if (ilg->g_param_type == ISCSI_SESS_PARAM) {
1648 					/* session */
1649 					/*
1650 					 * Update sess_params with the
1651 					 * latest params from the
1652 					 * persistent store.
1653 					 */
1654 					if (iscsi_get_persisted_param(name,
1655 					    ilg, params) != 0) {
1656 						/*
1657 						 * If the parameter in
1658 						 * question is not
1659 						 * overriden, no effect
1660 						 * on existing session
1661 						 * parameters. However,
1662 						 * the parameter is
1663 						 * marked invalid
1664 						 * (from the standpoint
1665 						 * of whether it is
1666 						 * overriden).
1667 						 */
1668 						valid_flag = B_FALSE;
1669 					}
1670 				} else if (ilg->g_param_type ==
1671 				    ISCSI_CONN_PARAM && isp != NULL) {
1672 					/* connection */
1673 					rw_enter(&isp->sess_conn_list_rwlock,
1674 					    RW_READER);
1675 					/* Assuming 1 conn per sess. */
1676 					/*
1677 					 * MC/S - Need to be modified to
1678 					 * take g_conn_cid into account when
1679 					 * we go multi-connection.
1680 					 */
1681 					if ((isp->sess_conn_act != NULL) &&
1682 					    (isp->sess_conn_act->conn_state ==
1683 					    ISCSI_CONN_STATE_LOGGED_IN)) {
1684 						params = &(isp->
1685 						    sess_conn_act->
1686 						    conn_params);
1687 					} else {
1688 						valid_flag = B_FALSE;
1689 					}
1690 					rw_exit(&isp->sess_conn_list_rwlock);
1691 				}
1692 			}
1693 
1694 			/* make sure we have params to get info from */
1695 			if (params) {
1696 				rtn = iscsi_get_param(params, valid_flag, ilg);
1697 
1698 				/*
1699 				 * for target parameters, check if any
1700 				 * parameters were overridden at the initiator
1701 				 * level. If so, then change the default value
1702 				 * to the initiator's overridden value
1703 				 */
1704 				if ((rtn == 0) &&
1705 				    (ilg->g_oid != ihp->hba_oid)) {
1706 					iscsi_override_target_default(ihp,
1707 					    ilg);
1708 				}
1709 			}
1710 			rw_exit(&ihp->hba_sess_list_rwlock);
1711 		}
1712 
1713 		if (rtn == 0) {
1714 			rtn = ddi_copyout(ilg, (caddr_t)arg,
1715 			    sizeof (iscsi_param_get_t), mode);
1716 		}
1717 		kmem_free(ilg, sizeof (*ilg));
1718 		if (tmpParams != NULL)
1719 			kmem_free(tmpParams, sizeof (*tmpParams));
1720 		break;
1721 
1722 	/*
1723 	 * ISCSI_INIT_NODE_NAME_SET - Change the initiator-node name for
1724 	 * the specified connection/session.
1725 	 */
1726 	case ISCSI_INIT_NODE_NAME_SET:
1727 		/* copyin user args */
1728 		ils = (iscsi_param_set_t *)kmem_alloc(sizeof (*ils), KM_SLEEP);
1729 		if (ddi_copyin((caddr_t)arg, ils, sizeof (*ils), mode)) {
1730 			rtn = EFAULT;
1731 			kmem_free(ils, sizeof (*ils));
1732 			break;
1733 		}
1734 
1735 		if (ils->s_vers != ISCSI_INTERFACE_VERSION) {
1736 			rtn = EINVAL;
1737 			kmem_free(ils, sizeof (*ils));
1738 			break;
1739 		}
1740 
1741 		/* saving off the old initiator-node name */
1742 		initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
1743 		rval = persistent_initiator_name_get(initiator_node_name,
1744 		    ISCSI_MAX_NAME_LEN);
1745 
1746 		rtn = iscsi_set_params(ils, ihp, B_TRUE);
1747 		kmem_free(ils, sizeof (*ils));
1748 		if (rtn != 0) {
1749 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
1750 			initiator_node_name = NULL;
1751 			break;
1752 		}
1753 
1754 		(void) snprintf(init_port_name, MAX_NAME_PROP_SIZE,
1755 		    "%s,%02x%02x%02x%02x%02x%02x",
1756 		    (char *)ihp->hba_name, ihp->hba_isid[0],
1757 		    ihp->hba_isid[1], ihp->hba_isid[2],
1758 		    ihp->hba_isid[3], ihp->hba_isid[4],
1759 		    ihp->hba_isid[5]);
1760 
1761 		if (ddi_prop_update_string(DDI_DEV_T_NONE,
1762 		    ihp->hba_dip, SCSI_ADDR_PROP_INITIATOR_PORT,
1763 		    init_port_name) != DDI_PROP_SUCCESS) {
1764 			cmn_err(CE_WARN, "iscsi_ioctl: Updating "
1765 			    SCSI_ADDR_PROP_INITIATOR_PORT " property on iSCSI "
1766 			    "HBA(%s) with dip(%d) Failed",
1767 			    (char *)ihp->hba_name,
1768 			    ddi_get_instance(ihp->hba_dip));
1769 		}
1770 
1771 		/*
1772 		 * Deregister the old initiator-node name from the iSNS
1773 		 * server
1774 		 * Register the new initiator-node name with the iSNS server
1775 		 */
1776 		method = persistent_disc_meth_get();
1777 		if (method & iSCSIDiscoveryMethodISNS) {
1778 			if (rval == B_TRUE) {
1779 				if (strlen(initiator_node_name) > 0) {
1780 				/*
1781 				 * we will attempt to offline the targets.
1782 				 * if logouts fail, we will still continue
1783 				 */
1784 #define	STRING_INNO "initiator-node name - Offline "
1785 #define	STRING_FFOMD "failed for one or more devices"
1786 					if ((iscsid_del(
1787 					    ihp, NULL, method, NULL))
1788 					    != B_TRUE) {
1789 						cmn_err(CE_NOTE,
1790 						    "Attempting to change "
1791 						    STRING_INNO
1792 						    STRING_FFOMD);
1793 					}
1794 					(void) isns_dereg(ihp->hba_isid,
1795 					    (uint8_t *)initiator_node_name);
1796 #undef STRING_INNO
1797 #undef STRING_FFOMD
1798 				}
1799 			}
1800 			if (persistent_initiator_name_get(initiator_node_name,
1801 			    ISCSI_MAX_NAME_LEN) != B_TRUE) {
1802 				kmem_free(initiator_node_name,
1803 				    ISCSI_MAX_NAME_LEN);
1804 				initiator_node_name = NULL;
1805 				rtn = EIO;
1806 				break;
1807 			}
1808 			if (strlen(initiator_node_name) == 0) {
1809 				kmem_free(initiator_node_name,
1810 				    ISCSI_MAX_NAME_LEN);
1811 				initiator_node_name = NULL;
1812 				rtn = EIO;
1813 				break;
1814 			}
1815 
1816 			initiator_node_alias = kmem_zalloc(ISCSI_MAX_NAME_LEN,
1817 			    KM_SLEEP);
1818 			if (persistent_alias_name_get(initiator_node_alias,
1819 			    ISCSI_MAX_NAME_LEN) != B_TRUE) {
1820 				initiator_node_alias[0] = '\0';
1821 			}
1822 
1823 			(void) isns_reg(ihp->hba_isid,
1824 			    (uint8_t *)initiator_node_name,
1825 			    ISCSI_MAX_NAME_LEN,
1826 			    (uint8_t *)initiator_node_alias,
1827 			    ISCSI_MAX_NAME_LEN,
1828 			    ISNS_INITIATOR_NODE_TYPE,
1829 			    isns_scn_callback);
1830 			iscsid_do_isns_query(ihp);
1831 
1832 			kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN);
1833 			initiator_node_alias = NULL;
1834 		}
1835 
1836 		kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
1837 		initiator_node_name = NULL;
1838 		break;
1839 
1840 	/*
1841 	 * ISCSI_PARAM_SET - Set param for specified connection/session.
1842 	 */
1843 	case ISCSI_PARAM_SET:
1844 		/* copyin user args */
1845 		ils = (iscsi_param_set_t *)kmem_alloc(sizeof (*ils), KM_SLEEP);
1846 		if (ddi_copyin((caddr_t)arg, ils, sizeof (*ils), mode)) {
1847 			rtn = EFAULT;
1848 			kmem_free(ils, sizeof (*ils));
1849 			break;
1850 		}
1851 
1852 		if (ils->s_vers != ISCSI_INTERFACE_VERSION) {
1853 			rtn = EINVAL;
1854 			kmem_free(ils, sizeof (*ils));
1855 			break;
1856 		}
1857 		rtn = iscsi_set_params(ils, ihp, B_TRUE);
1858 		if (iscsiboot_prop) {
1859 			if (iscsi_cmp_boot_sess_oid(ihp, ils->s_oid)) {
1860 				/*
1861 				 * found active session for this object
1862 				 * or this is initiator's object
1863 				 * with mpxio enabled
1864 				 */
1865 				if (!iscsi_reconfig_boot_sess(ihp)) {
1866 					rtn = EINVAL;
1867 					kmem_free(ils, sizeof (*ils));
1868 					break;
1869 				}
1870 			}
1871 		}
1872 		kmem_free(ils, sizeof (*ils));
1873 		break;
1874 
1875 	/*
1876 	 * ISCSI_TARGET_PARAM_CLEAR
1877 	 * - remove custom parameter settings for a target.
1878 	 */
1879 	case ISCSI_TARGET_PARAM_CLEAR:
1880 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
1881 			rtn = EFAULT;
1882 			break;
1883 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
1884 			rtn = EINVAL;
1885 			break;
1886 		}
1887 
1888 		if ((e.e_oid != ihp->hba_oid) &&
1889 		    (e.e_oid != ISCSI_OID_NOTSET)) {
1890 			uchar_t	    *t_name;
1891 			iscsi_sess_t *t_isp;
1892 
1893 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
1894 			/*
1895 			 * If the oid does represent a session check to see
1896 			 * if it is a target oid.  If so, return the target's
1897 			 * associated session.
1898 			 */
1899 			rtn = iscsi_sess_get(e.e_oid, ihp, &isp);
1900 			if (rtn != 0) {
1901 				rtn = iscsi_sess_get_by_target(e.e_oid, ihp,
1902 				    &isp);
1903 			}
1904 
1905 			/*
1906 			 * If rtn is zero then we have found an
1907 			 * existing session.  Use the session name to
1908 			 * do param lookup.  If rtn is non-zero then
1909 			 * create a targetparam object and use its name
1910 			 * for param lookup.
1911 			 */
1912 			if (rtn == 0) {
1913 				t_name = isp->sess_name;
1914 			} else {
1915 				t_name = iscsi_targetparam_get_name(e.e_oid);
1916 				rtn = 0;
1917 			}
1918 
1919 			if (t_name == NULL) {
1920 				rw_exit(&ihp->hba_sess_list_rwlock);
1921 				rtn = EFAULT;
1922 				break;
1923 			}
1924 
1925 			name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
1926 			(void) strncpy((char *)name, (char *)t_name,
1927 			    ISCSI_MAX_NAME_LEN);
1928 
1929 			if (persistent_param_clear((char *)name) == B_FALSE) {
1930 				kmem_free(name, ISCSI_MAX_NAME_LEN);
1931 				rw_exit(&ihp->hba_sess_list_rwlock);
1932 				rtn = EIO;
1933 				break;
1934 			}
1935 
1936 			ics = kmem_zalloc(sizeof (*ics), KM_SLEEP);
1937 			ics->ics_ver = ISCSI_INTERFACE_VERSION;
1938 			ics->ics_oid = ISCSI_INITIATOR_OID;
1939 			ics->ics_in  = 1;
1940 
1941 			/*
1942 			 * We may have multiple sessions with different
1943 			 * tpgt values.  So we need to loop through
1944 			 * the sessions and update all sessions.
1945 			 */
1946 			for (isp = ihp->hba_sess_list; isp;
1947 			    isp = t_isp) {
1948 				t_isp = isp->sess_next;
1949 
1950 				if (strncmp((char *)isp->sess_name,
1951 				    (char *)name, ISCSI_MAX_NAME_LEN) == 0) {
1952 					/*
1953 					 * When removing target-params we need
1954 					 * slightly different actions depending
1955 					 * on if the session should still exist.
1956 					 * Get the initiator-node value for
1957 					 * MS/T.  If there is no initiator
1958 					 * value then assume the default value
1959 					 * of 1.  If the initiator value is
1960 					 * less than this ISID then we need to
1961 					 * destroy the session.  Otherwise
1962 					 * update the session information and
1963 					 * resync (N7 event).
1964 					 */
1965 					rtn = iscsi_ioctl_get_config_sess(
1966 					    ihp, ics);
1967 					if (((rtn != 0) &&
1968 					    (isp->sess_isid[5] > 0)) ||
1969 					    ((rtn == 0) &&
1970 					    (ics->ics_out <=
1971 					    isp->sess_isid[5]))) {
1972 
1973 						/*
1974 						 * This session should no
1975 						 * longer exist.  Remove
1976 						 * session.
1977 						 */
1978 						if (!ISCSI_SUCCESS(
1979 						    iscsi_sess_destroy(isp))) {
1980 							kmem_free(ics,
1981 							    sizeof (*ics));
1982 							kmem_free(name,
1983 							    ISCSI_MAX_NAME_LEN);
1984 						rw_exit(&ihp->
1985 						    hba_sess_list_rwlock);
1986 							rtn = EBUSY;
1987 							break;
1988 						}
1989 						isp = ihp->hba_sess_list;
1990 					} else {
1991 						/*
1992 						 * Reset the session
1993 						 * parameters.
1994 						 */
1995 						bcopy(&(isp->sess_hba->
1996 						    hba_params),
1997 						    &(isp->sess_params),
1998 						    sizeof (isp->sess_params));
1999 						if (iscsiboot_prop &&
2000 						    isp->sess_boot) {
2001 							/*
2002 							 * reconfig boot
2003 							 * session later
2004 							 */
2005 							continue;
2006 						}
2007 						/*
2008 						 * Notify the session that the
2009 						 * login parameters have
2010 						 * changed.
2011 						 */
2012 						mutex_enter(&isp->
2013 						    sess_state_mutex);
2014 						iscsi_sess_state_machine(isp,
2015 						    ISCSI_SESS_EVENT_N7);
2016 						mutex_exit(&isp->
2017 						    sess_state_mutex);
2018 					}
2019 				}
2020 			}
2021 			kmem_free(ics, sizeof (*ics));
2022 			kmem_free(name, ISCSI_MAX_NAME_LEN);
2023 			rw_exit(&ihp->hba_sess_list_rwlock);
2024 			if (iscsiboot_prop) {
2025 				if (iscsi_cmp_boot_sess_oid(ihp, e.e_oid)) {
2026 					/*
2027 					 * found active session for this object
2028 					 * or this is initiator object
2029 					 * with mpxio enabled
2030 					 */
2031 					if (!iscsi_reconfig_boot_sess(ihp)) {
2032 						rtn = EINVAL;
2033 						break;
2034 					}
2035 				}
2036 			}
2037 		}
2038 		break;
2039 
2040 	/*
2041 	 * ISCSI_TARGET_OID_LIST_GET -
2042 	 */
2043 	case ISCSI_TARGET_OID_LIST_GET:
2044 		/* copyin user args */
2045 		if (ddi_copyin((caddr_t)arg, &idl,
2046 		    sizeof (idl), mode)) {
2047 			rtn = EFAULT;
2048 			break;
2049 		}
2050 
2051 		if (idl.tl_vers != ISCSI_INTERFACE_VERSION) {
2052 			rtn = EINVAL;
2053 			break;
2054 		}
2055 
2056 		list_space = sizeof (iscsi_target_list_t);
2057 		if (idl.tl_in_cnt != 0)
2058 			list_space += (sizeof (uint32_t) *
2059 			    (idl.tl_in_cnt - 1));
2060 
2061 		idlp = kmem_zalloc(list_space, KM_SLEEP);
2062 		bcopy(&idl, idlp, sizeof (idl));
2063 		idlp->tl_out_cnt = 0;
2064 
2065 		/*
2066 		 * If target list type is ISCSI_TGT_OID_LIST and discovery
2067 		 * has not been completed or in progress, poke the discovery
2068 		 * methods so target information is returned
2069 		 */
2070 		mutex_enter(&ihp->hba_discovery_events_mutex);
2071 		method = ihp->hba_discovery_events;
2072 		if ((idl.tl_tgt_list_type == ISCSI_TGT_OID_LIST) &&
2073 		    (method != ISCSI_ALL_DISCOVERY_METHODS) &&
2074 		    (ihp->hba_discovery_in_progress == B_FALSE)) {
2075 			ihp->hba_discovery_in_progress = B_TRUE;
2076 			mutex_exit(&ihp->hba_discovery_events_mutex);
2077 			iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodUnknown);
2078 			mutex_enter(&ihp->hba_discovery_events_mutex);
2079 			ihp->hba_discovery_in_progress = B_FALSE;
2080 		}
2081 		mutex_exit(&ihp->hba_discovery_events_mutex);
2082 
2083 		/*
2084 		 * Return the correct list information based on the type
2085 		 */
2086 		switch (idl.tl_tgt_list_type) {
2087 		/* ISCSI_TGT_PARAM_OID_LIST - iscsiadm list target-params */
2088 		case ISCSI_TGT_PARAM_OID_LIST:
2089 			/* get params from persistent store */
2090 			iscsi_targetparam_lock_list(RW_READER);
2091 			curr_entry = iscsi_targetparam_get_next_entry(NULL);
2092 			while (curr_entry != NULL) {
2093 				if (idlp->tl_out_cnt < idlp->tl_in_cnt) {
2094 					idlp->tl_oid_list[idlp->tl_out_cnt] =
2095 					    curr_entry->target_oid;
2096 				}
2097 				idlp->tl_out_cnt++;
2098 				curr_entry = iscsi_targetparam_get_next_entry(
2099 				    curr_entry);
2100 			}
2101 			iscsi_targetparam_unlock_list();
2102 			break;
2103 
2104 		/* ISCSI_STATIC_TGT_OID_LIST - iscsiadm list static-config */
2105 		case ISCSI_STATIC_TGT_OID_LIST:
2106 		{
2107 			char *target_name = NULL;
2108 			void *v = NULL;
2109 
2110 			/* get static-config from persistent store */
2111 			target_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
2112 			persistent_static_addr_lock();
2113 			while (persistent_static_addr_next(&v,
2114 			    (char *)target_name, &e) == B_TRUE) {
2115 
2116 				if (idlp->tl_out_cnt < idlp->tl_in_cnt) {
2117 					idlp->tl_oid_list[idlp->tl_out_cnt] =
2118 					    e.e_oid;
2119 				}
2120 				idlp->tl_out_cnt++;
2121 
2122 			}
2123 
2124 			persistent_static_addr_unlock();
2125 			kmem_free(target_name, ISCSI_MAX_NAME_LEN);
2126 			break;
2127 		}
2128 
2129 		/* ISCSI_TGT_OID_LIST - iscsiadm list target */
2130 		case ISCSI_TGT_OID_LIST:
2131 
2132 			/* get sessions from hba's session list */
2133 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
2134 			for (isp = ihp->hba_sess_list; isp;
2135 			    isp = isp->sess_next) {
2136 
2137 				if (((isp->sess_state !=
2138 				    ISCSI_SESS_STATE_FREE) ||
2139 				    (isp->sess_discovered_by !=
2140 				    iSCSIDiscoveryMethodUnknown)) &&
2141 				    (isp->sess_type ==
2142 				    ISCSI_SESS_TYPE_NORMAL)) {
2143 					if (idlp->tl_out_cnt <
2144 					    idlp->tl_in_cnt) {
2145 						idlp->tl_oid_list[
2146 						    idlp->tl_out_cnt] =
2147 						    isp->sess_oid;
2148 					}
2149 					idlp->tl_out_cnt++;
2150 				}
2151 
2152 			}
2153 			rw_exit(&ihp->hba_sess_list_rwlock);
2154 			break;
2155 
2156 		default:
2157 			ASSERT(FALSE);
2158 		}
2159 
2160 		rtn = ddi_copyout(idlp, (caddr_t)arg, list_space, mode);
2161 		kmem_free(idlp, list_space);
2162 		break;
2163 
2164 	/*
2165 	 * ISCSI_TARGET_PROPS_GET -
2166 	 */
2167 	case ISCSI_TARGET_PROPS_GET:
2168 		/* ---- fall through sense the code is almost the same ---- */
2169 
2170 	/*
2171 	 * ISCSI_TARGET_PROPS_SET -
2172 	 */
2173 	case ISCSI_TARGET_PROPS_SET:
2174 		/* copyin user args */
2175 		ipp = (iscsi_property_t *)kmem_alloc(sizeof (*ipp),
2176 		    KM_SLEEP);
2177 		if (ddi_copyin((caddr_t)arg, ipp, sizeof (*ipp), mode)) {
2178 			rtn = EFAULT;
2179 			kmem_free(ipp, sizeof (*ipp));
2180 			break;
2181 		}
2182 
2183 		if (ipp->p_vers != ISCSI_INTERFACE_VERSION) {
2184 			rtn = EINVAL;
2185 			kmem_free(ipp, sizeof (*ipp));
2186 			break;
2187 		}
2188 
2189 		rtn = iscsi_target_prop_mod(ihp, ipp, cmd);
2190 		if (rtn == 0)
2191 			rtn = ddi_copyout(ipp, (caddr_t)arg,
2192 			    sizeof (*ipp), mode);
2193 		kmem_free(ipp, sizeof (*ipp));
2194 		break;
2195 
2196 	/*
2197 	 * ISCSI_TARGET_ADDRESS_GET -
2198 	 */
2199 	case ISCSI_TARGET_ADDRESS_GET:
2200 		if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) {
2201 			rtn = EFAULT;
2202 			break;
2203 		}
2204 
2205 		if (ial.al_vers != ISCSI_INTERFACE_VERSION) {
2206 			rtn = EINVAL;
2207 			break;
2208 		}
2209 
2210 		/*
2211 		 * Find out how much space we need to malloc for the users
2212 		 * request.
2213 		 */
2214 		list_space = sizeof (iscsi_addr_list_t);
2215 		if (ial.al_in_cnt != 0) {
2216 			list_space += (sizeof (iscsi_addr_t) *
2217 			    (ial.al_in_cnt - 1));
2218 		}
2219 		ialp = (iscsi_addr_list_t *)kmem_zalloc(list_space, KM_SLEEP);
2220 
2221 		/* Copy in the header portion */
2222 		bcopy(&ial, ialp, sizeof (ial));
2223 
2224 		/* session */
2225 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
2226 		rtn = iscsi_sess_get(ialp->al_oid, ihp, &isp);
2227 		if (rtn != 0) {
2228 			rw_exit(&ihp->hba_sess_list_rwlock);
2229 			rtn = EFAULT;
2230 			break;
2231 		}
2232 
2233 		ialp->al_out_cnt	= 0;
2234 		ialp->al_tpgt		= isp->sess_tpgt_conf;
2235 		rw_enter(&isp->sess_conn_list_rwlock, RW_READER);
2236 		for (icp = isp->sess_conn_list; icp; icp = icp->conn_next) {
2237 			if (icp->conn_state != ISCSI_CONN_STATE_LOGGED_IN) {
2238 				continue;
2239 			}
2240 			if (ialp->al_out_cnt < ialp->al_in_cnt) {
2241 				iscsi_addr_t		*ap;
2242 
2243 				ap = &ialp->al_addrs[ialp->al_out_cnt];
2244 				if (icp->conn_base_addr.sin.sa_family
2245 				    == AF_INET) {
2246 
2247 					struct sockaddr_in *addr_in =
2248 					    (struct sockaddr_in *)&icp->
2249 					    conn_base_addr.sin4;
2250 					ap->a_addr.i_insize =
2251 					    sizeof (struct in_addr);
2252 					bcopy(&addr_in->sin_addr.s_addr,
2253 					    &ap->a_addr.i_addr.in4.s_addr,
2254 					    sizeof (struct in_addr));
2255 					ap->a_port = addr_in->sin_port;
2256 
2257 				} else {
2258 
2259 					struct sockaddr_in6 *addr_in6 =
2260 					    (struct sockaddr_in6 *)&icp->
2261 					    conn_base_addr.sin6;
2262 					ap->a_addr.i_insize =
2263 					    sizeof (struct in6_addr);
2264 					bcopy(&addr_in6->sin6_addr.s6_addr,
2265 					    &ap->a_addr.i_addr.in6.s6_addr,
2266 					    sizeof (struct in6_addr));
2267 					ap->a_port = addr_in6->sin6_port;
2268 
2269 				}
2270 			}
2271 			ialp->al_out_cnt++;
2272 		}
2273 		rw_exit(&isp->sess_conn_list_rwlock);
2274 		rw_exit(&ihp->hba_sess_list_rwlock);
2275 
2276 		rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode);
2277 		kmem_free(ialp, list_space);
2278 		break;
2279 
2280 	/*
2281 	 * ISCSI_CHAP_SET -
2282 	 */
2283 	case ISCSI_CHAP_SET:
2284 		chap = (iscsi_chap_props_t *)kmem_zalloc(sizeof (*chap),
2285 		    KM_SLEEP);
2286 		if (ddi_copyin((caddr_t)arg, chap, sizeof (*chap), mode)) {
2287 			rtn = EFAULT;
2288 			kmem_free(chap, sizeof (*chap));
2289 			break;
2290 		} else if (chap->c_vers != ISCSI_INTERFACE_VERSION) {
2291 			rtn = EINVAL;
2292 			kmem_free(chap, sizeof (*chap));
2293 			break;
2294 		}
2295 
2296 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
2297 		if (chap->c_oid == ihp->hba_oid)
2298 			name = ihp->hba_name;
2299 		else {
2300 			rtn = iscsi_sess_get(chap->c_oid, ihp, &isp);
2301 			if (rtn != 0) {
2302 				rtn = iscsi_sess_get_by_target(
2303 				    chap->c_oid, ihp, &isp);
2304 			}
2305 
2306 			/*
2307 			 * If rtn is zero then we have found an
2308 			 * existing session.  Use the session name to
2309 			 * do param lookup.  If rtn is non-zero then
2310 			 * create a targetparam object and use its name
2311 			 * for param lookup.
2312 			 */
2313 			if (rtn == 0) {
2314 				name = isp->sess_name;
2315 			} else {
2316 				name =
2317 				    iscsi_targetparam_get_name(chap->c_oid);
2318 				rtn = 0;
2319 			}
2320 		}
2321 
2322 		if (name == NULL) {
2323 			rw_exit(
2324 			    &ihp->hba_sess_list_rwlock);
2325 			rtn = EFAULT;
2326 			kmem_free(chap, sizeof (*chap));
2327 			break;
2328 		}
2329 
2330 		if (persistent_chap_set((char *)name, chap) ==
2331 		    B_FALSE) {
2332 			rtn = EIO;
2333 		}
2334 		rw_exit(&ihp->hba_sess_list_rwlock);
2335 		kmem_free(chap, sizeof (*chap));
2336 		break;
2337 
2338 	/*
2339 	 * ISCSI_CHAP_GET -
2340 	 */
2341 	case ISCSI_CHAP_GET:
2342 		chap = (iscsi_chap_props_t *)kmem_zalloc(sizeof (*chap),
2343 		    KM_SLEEP);
2344 		if (ddi_copyin((caddr_t)arg, chap, sizeof (*chap), mode)) {
2345 			kmem_free(chap, sizeof (*chap));
2346 			rtn = EFAULT;
2347 			break;
2348 		} else if (chap->c_vers != ISCSI_INTERFACE_VERSION) {
2349 			kmem_free(chap, sizeof (*chap));
2350 			rtn = EINVAL;
2351 			break;
2352 		}
2353 
2354 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
2355 		if (chap->c_oid == ihp->hba_oid)
2356 			name = ihp->hba_name;
2357 		else {
2358 			rtn = iscsi_sess_get(chap->c_oid, ihp, &isp);
2359 			if (rtn != 0) {
2360 				rtn = iscsi_sess_get_by_target(
2361 				    chap->c_oid, ihp, &isp);
2362 			}
2363 
2364 			/*
2365 			 * If rtn is zero then we have found an
2366 			 * existing session.  Use the session name to
2367 			 * do param lookup.  If rtn is non-zero then
2368 			 * create a targetparam object and use its name
2369 			 * for param lookup.
2370 			 */
2371 			if (rtn == 0) {
2372 				name = isp->sess_name;
2373 			} else {
2374 				rtn = 0;
2375 				name =
2376 				    iscsi_targetparam_get_name(chap->c_oid);
2377 			}
2378 
2379 			if (name == NULL) {
2380 				rw_exit(&ihp->hba_sess_list_rwlock);
2381 				rtn = EFAULT;
2382 				break;
2383 			}
2384 			/*
2385 			 * Initialize the target-side chap name to the
2386 			 * session name if no chap settings have been
2387 			 * saved for the current session.
2388 			 */
2389 			if (persistent_chap_get((char *)name,
2390 			    chap) == B_FALSE) {
2391 				int name_len = strlen((char *)name);
2392 				iscsi_chap_props_t *chap = NULL;
2393 				chap = (iscsi_chap_props_t *)kmem_zalloc
2394 				    (sizeof (iscsi_chap_props_t), KM_SLEEP);
2395 				bcopy((char *)name, chap->c_user, name_len);
2396 				chap->c_user_len = name_len;
2397 				(void) (persistent_chap_set((char *)name,
2398 				    chap));
2399 				kmem_free(chap, sizeof (*chap));
2400 			}
2401 		}
2402 
2403 		if (name == NULL) {
2404 			rw_exit(
2405 			    &ihp->hba_sess_list_rwlock);
2406 			rtn = EFAULT;
2407 			break;
2408 		}
2409 
2410 		if (persistent_chap_get((char *)name, chap) == B_FALSE) {
2411 			rw_exit(&ihp->hba_sess_list_rwlock);
2412 			rtn = EIO;
2413 			break;
2414 		}
2415 		rw_exit(&ihp->hba_sess_list_rwlock);
2416 
2417 		rtn = ddi_copyout(chap, (caddr_t)arg, sizeof (*chap), mode);
2418 		kmem_free(chap, sizeof (*chap));
2419 		break;
2420 
2421 	/*
2422 	 * ISCSI_CHAP_CLEAR -
2423 	 */
2424 	case ISCSI_CHAP_CLEAR:
2425 		chap = (iscsi_chap_props_t *)kmem_zalloc(sizeof (*chap),
2426 		    KM_SLEEP);
2427 		if (ddi_copyin((caddr_t)arg, chap, sizeof (*chap), mode)) {
2428 			rtn = EFAULT;
2429 			kmem_free(chap, sizeof (*chap));
2430 			break;
2431 		} else if (chap->c_vers != ISCSI_INTERFACE_VERSION) {
2432 			rtn = EINVAL;
2433 			kmem_free(chap, sizeof (*chap));
2434 			break;
2435 		}
2436 
2437 		if (chap->c_oid == ihp->hba_oid) {
2438 			iscsi_sess_t *sessp;
2439 
2440 			name = ihp->hba_name;
2441 
2442 			if (persistent_chap_clear(
2443 			    (char *)name) == B_FALSE) {
2444 				rtn = EIO;
2445 			}
2446 
2447 			/*
2448 			 * Loop through all sessions and memset their
2449 			 * (initiator's) passwords
2450 			 */
2451 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
2452 			for (sessp = ihp->hba_sess_list; sessp;
2453 			    sessp = sessp->sess_next) {
2454 				(void) memset(sessp->sess_auth.password,
2455 				    0, iscsiAuthStringMaxLength);
2456 				sessp->sess_auth.password_length = 0;
2457 			}
2458 			rw_exit(&ihp->hba_sess_list_rwlock);
2459 
2460 		} else {
2461 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
2462 			/*
2463 			 * If the oid does represent a session check to see
2464 			 * if it is a target oid.  If so, return the target's
2465 			 * associated session.
2466 			 */
2467 			rtn = iscsi_sess_get(chap->c_oid, ihp, &isp);
2468 			if (rtn != 0) {
2469 				rtn = iscsi_sess_get_by_target(chap->c_oid,
2470 				    ihp, &isp);
2471 			}
2472 
2473 			rw_exit(&ihp->hba_sess_list_rwlock);
2474 
2475 			/*
2476 			 * If rtn is zero then we have found an
2477 			 * existing session.  Use the session name to
2478 			 * do param lookup.  If rtn is non-zero then
2479 			 * create a targetparam object and use its name
2480 			 * for param lookup.
2481 			 */
2482 			if (rtn == 0) {
2483 				name = isp->sess_name;
2484 			} else {
2485 				name =
2486 				    iscsi_targetparam_get_name(chap->c_oid);
2487 				rtn = 0;
2488 			}
2489 
2490 			if (name == NULL) {
2491 				rw_exit(
2492 				    &ihp->hba_sess_list_rwlock);
2493 				rtn = EFAULT;
2494 				break;
2495 			}
2496 
2497 			if (persistent_chap_clear(
2498 			    (char *)name) == B_FALSE) {
2499 				rtn = EIO;
2500 			}
2501 
2502 			/*
2503 			 * Clear out session chap password if we found a
2504 			 * session above.
2505 			 */
2506 			if (isp != NULL) {
2507 				(void) memset(isp->sess_auth.password_in,
2508 				    0, iscsiAuthStringMaxLength);
2509 				isp->sess_auth.password_length_in = 0;
2510 			}
2511 
2512 		}
2513 
2514 		kmem_free(chap, sizeof (*chap));
2515 		break;
2516 
2517 	/*
2518 	 * ISCSI_STATIC_GET -
2519 	 */
2520 	case ISCSI_STATIC_GET:
2521 		ispp = (iscsi_static_property_t *)kmem_alloc(
2522 		    sizeof (*ispp), KM_SLEEP);
2523 
2524 		if (ddi_copyin((caddr_t)arg, ispp, sizeof (*ispp), mode)) {
2525 			rtn = EFAULT;
2526 			kmem_free(ispp, sizeof (*ispp));
2527 			break;
2528 		}
2529 
2530 		if (ispp->p_vers != ISCSI_INTERFACE_VERSION) {
2531 			rtn = EINVAL;
2532 			kmem_free(ispp, sizeof (*ispp));
2533 			break;
2534 		}
2535 
2536 		{
2537 			void *v = NULL;
2538 			boolean_t found = B_FALSE;
2539 
2540 			persistent_static_addr_lock();
2541 			while (persistent_static_addr_next(&v,
2542 			    (char *)ispp->p_name, &e) == B_TRUE) {
2543 
2544 				if (ispp->p_oid == e.e_oid) {
2545 					/*
2546 					 * In case there are multiple
2547 					 * addresses associated with the
2548 					 * given target OID, pick the first
2549 					 * one.
2550 					 */
2551 					iscsi_addr_t *ap;
2552 
2553 					ap = &(ispp->p_addr_list.al_addrs[0]);
2554 					ap->a_port = e.e_port;
2555 					ap->a_addr.i_insize = e.e_insize;
2556 					bcopy(e.e_u.u_in6.s6_addr,
2557 					    ap->a_addr.i_addr.in6.s6_addr,
2558 					    e.e_insize);
2559 					ispp->p_name_len =
2560 					    strlen((char *)ispp->p_name);
2561 					ispp->p_addr_list.al_tpgt = e.e_tpgt;
2562 					ispp->p_addr_list.al_out_cnt = 1;
2563 
2564 					found = B_TRUE;
2565 					break;
2566 				}
2567 			}
2568 			persistent_static_addr_unlock();
2569 
2570 			if (found == B_TRUE) {
2571 				rtn = ddi_copyout(ispp, (caddr_t)arg,
2572 				    sizeof (*ispp), mode);
2573 			} else {
2574 				rtn = ENOENT;
2575 			}
2576 		}
2577 		kmem_free(ispp, sizeof (*ispp));
2578 
2579 		break;
2580 
2581 	/*
2582 	 * ISCSI_STATIC_SET -
2583 	 */
2584 	case ISCSI_STATIC_SET:
2585 		target = iscsi_ioctl_copyin((caddr_t)arg, mode,
2586 		    sizeof (*target));
2587 		if (target == NULL) {
2588 			rtn = EFAULT;
2589 			break;
2590 		}
2591 
2592 		if (target->te_entry.e_vers != ISCSI_INTERFACE_VERSION) {
2593 			kmem_free(target, sizeof (*target));
2594 			rtn = EINVAL;
2595 			break;
2596 		}
2597 
2598 		/* Check if the target's already been added */
2599 		{
2600 			boolean_t static_target_found = B_FALSE;
2601 			void *v = NULL;
2602 
2603 			name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
2604 			persistent_static_addr_lock();
2605 			while (persistent_static_addr_next(&v, (char *)name,
2606 			    &e) == B_TRUE) {
2607 				/*
2608 				 * MC/S - Need to check IP address and port
2609 				 * number as well when we support MC/S.
2610 				 */
2611 				if ((strncmp((char *)name,
2612 				    (char *)target->te_name,
2613 				    ISCSI_MAX_NAME_LEN) == 0) &&
2614 				    (target->te_entry.e_tpgt == e.e_tpgt) &&
2615 				    (target->te_entry.e_insize == e.e_insize) &&
2616 				    (bcmp(&target->te_entry.e_u, &e.e_u,
2617 				    e.e_insize) == 0)) {
2618 					/*
2619 					 * We don't allow MC/S for now but
2620 					 * we do allow adding the same target
2621 					 * with different TPGTs (hence,
2622 					 * different sessions).
2623 					 */
2624 					static_target_found = B_TRUE;
2625 					break;
2626 				}
2627 			}
2628 			persistent_static_addr_unlock();
2629 			kmem_free(name, ISCSI_MAX_NAME_LEN);
2630 
2631 			if (static_target_found == B_TRUE) {
2632 				/* Duplicate entry */
2633 				kmem_free(target, sizeof (*target));
2634 				rtn = EEXIST;
2635 				break;
2636 			}
2637 		}
2638 
2639 		if (target->te_entry.e_oid == ISCSI_OID_NOTSET) {
2640 			mutex_enter(&iscsi_oid_mutex);
2641 			target->te_entry.e_oid = iscsi_oid++;
2642 			mutex_exit(&iscsi_oid_mutex);
2643 		}
2644 
2645 		persistent_static_addr_lock();
2646 		if (persistent_static_addr_set((char *)target->te_name,
2647 		    &target->te_entry) == B_FALSE) {
2648 			persistent_static_addr_unlock();
2649 			kmem_free(target, sizeof (*target));
2650 			rtn = EIO;
2651 			break;
2652 		}
2653 		persistent_static_addr_unlock();
2654 
2655 		/*
2656 		 * If Static Targets discovery is enabled, then add
2657 		 * target to discovery queue. Otherwise, just create
2658 		 * the session for potential future use.
2659 		 */
2660 		method = persistent_disc_meth_get();
2661 		if (method & iSCSIDiscoveryMethodStatic) {
2662 			iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodStatic);
2663 			(void) iscsid_login_tgt(ihp, (char *)target->te_name,
2664 			    iSCSIDiscoveryMethodStatic, NULL);
2665 		}
2666 
2667 		rtn = iscsi_ioctl_copyout(target, sizeof (*target),
2668 		    (caddr_t)arg, mode);
2669 		break;
2670 
2671 	/*
2672 	 * ISCSI_STATIC_CLEAR -
2673 	 */
2674 	case ISCSI_STATIC_CLEAR:
2675 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
2676 			rtn = EFAULT;
2677 			break;
2678 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
2679 			rtn = EINVAL;
2680 			break;
2681 		}
2682 
2683 		{
2684 			boolean_t	found = B_FALSE;
2685 			void		*v = NULL;
2686 			entry_t		tmp_e;
2687 			char		*name = NULL;
2688 
2689 			name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
2690 
2691 			/* Find name for matching static_tgt oid */
2692 			persistent_static_addr_lock();
2693 			while (persistent_static_addr_next(&v,
2694 			    (char *)name, &tmp_e) == B_TRUE) {
2695 				if (e.e_oid == tmp_e.e_oid) {
2696 					found = B_TRUE;
2697 					break;
2698 				}
2699 			}
2700 
2701 			/* If static_tgt found logout and remove it */
2702 			if (found == B_TRUE) {
2703 
2704 				iscsid_addr_to_sockaddr(tmp_e.e_insize,
2705 				    &tmp_e.e_u, tmp_e.e_port, &addr_dsc.sin);
2706 
2707 				persistent_static_addr_unlock();
2708 
2709 				/*
2710 				 * If discovery in progress, try few times
2711 				 * before return busy
2712 				 */
2713 				retry = 0;
2714 				mutex_enter(&ihp->hba_discovery_events_mutex);
2715 				while (ihp->hba_discovery_in_progress ==
2716 				    B_TRUE) {
2717 					if (++retry == 5) {
2718 						rtn = EBUSY;
2719 						break;
2720 					}
2721 					mutex_exit(
2722 					    &ihp->hba_discovery_events_mutex);
2723 					delay(SEC_TO_TICK(
2724 					    ISCSI_DISC_DELAY));
2725 					mutex_enter(
2726 					    &ihp->hba_discovery_events_mutex);
2727 				}
2728 				/* remove from persistent store */
2729 				if (rtn == 0 && persistent_static_addr_clear(
2730 				    e.e_oid) == B_FALSE) {
2731 					rtn = EIO;
2732 				}
2733 				mutex_exit(&ihp->hba_discovery_events_mutex);
2734 
2735 				if (rtn != 0) {
2736 					kmem_free(name, ISCSI_MAX_NAME_LEN);
2737 					break;
2738 				}
2739 
2740 				/* Attempt to logout of target */
2741 				if (iscsid_del(ihp, (char *)name,
2742 				    iSCSIDiscoveryMethodStatic, &addr_dsc.sin)
2743 				    == B_FALSE) {
2744 					persistent_static_addr_lock();
2745 
2746 					/*
2747 					 * Restore static_tgt to
2748 					 * persistent store
2749 					 */
2750 					if (persistent_static_addr_set(
2751 					    (char *)name,
2752 					    &e) == B_FALSE) {
2753 						cmn_err(CE_WARN, "Failed to "
2754 						    "restore static target "
2755 						    "address after logout "
2756 						    "target failure.");
2757 					}
2758 					persistent_static_addr_unlock();
2759 					rtn = EBUSY;
2760 				} else {
2761 					iscsid_poke_discovery(ihp,
2762 					    iSCSIDiscoveryMethodStatic);
2763 					(void) iscsid_login_tgt(ihp,
2764 					    (char *)name,
2765 					    iSCSIDiscoveryMethodStatic,
2766 					    NULL);
2767 
2768 				}
2769 			} else {
2770 				persistent_static_addr_unlock();
2771 				rtn = EIO;
2772 			}
2773 			kmem_free(name, ISCSI_MAX_NAME_LEN);
2774 		}
2775 		break;
2776 
2777 	/*
2778 	 * ISCSI_ISNS_SERVER_ADDR_SET:
2779 	 */
2780 	case ISCSI_ISNS_SERVER_ADDR_SET:
2781 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
2782 			rtn = EFAULT;
2783 			break;
2784 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
2785 			rtn = EINVAL;
2786 			break;
2787 		}
2788 
2789 		if (persistent_isns_addr_set(&e) == B_FALSE) {
2790 			rtn = EIO;
2791 			break;
2792 		}
2793 
2794 		/*
2795 		 * If iSNS server discovery is enabled, then kickoff
2796 		 * discovery of the targets advertised by the recently
2797 		 * added iSNS server address.
2798 		 */
2799 		method = persistent_disc_meth_get();
2800 		if (method & iSCSIDiscoveryMethodISNS) {
2801 			initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN,
2802 			    KM_SLEEP);
2803 			if (persistent_initiator_name_get(initiator_node_name,
2804 			    ISCSI_MAX_NAME_LEN) != B_TRUE) {
2805 				kmem_free(initiator_node_name,
2806 				    ISCSI_MAX_NAME_LEN);
2807 				initiator_node_name = NULL;
2808 				rtn = EIO;
2809 				break;
2810 			}
2811 			if (strlen(initiator_node_name) == 0) {
2812 				kmem_free(initiator_node_name,
2813 				    ISCSI_MAX_NAME_LEN);
2814 				initiator_node_name = NULL;
2815 				rtn = EIO;
2816 				break;
2817 			}
2818 
2819 			initiator_node_alias = kmem_zalloc(ISCSI_MAX_NAME_LEN,
2820 			    KM_SLEEP);
2821 			if (persistent_alias_name_get(initiator_node_alias,
2822 			    ISCSI_MAX_NAME_LEN) != B_TRUE) {
2823 				initiator_node_alias[0] = '\0';
2824 			}
2825 
2826 			/*
2827 			 * Register this initiator node against this iSNS
2828 			 * server.
2829 			 */
2830 			(void) isns_reg_one_server(&e, ihp->hba_isid,
2831 			    (uint8_t *)initiator_node_name,
2832 			    ISCSI_MAX_NAME_LEN,
2833 			    (uint8_t *)initiator_node_alias,
2834 			    ISCSI_MAX_NAME_LEN,
2835 			    ISNS_INITIATOR_NODE_TYPE,
2836 			    isns_scn_callback);
2837 
2838 			iscsid_do_isns_query_one_server(ihp, &e);
2839 
2840 			iscsid_addr_to_sockaddr(e.e_insize,
2841 			    &e.e_u, e.e_port, &addr_dsc.sin);
2842 
2843 			(void) iscsid_login_tgt(ihp, NULL,
2844 			    iSCSIDiscoveryMethodISNS,
2845 			    &addr_dsc.sin);
2846 
2847 			/* Done using the name and alias - free them. */
2848 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
2849 			initiator_node_name = NULL;
2850 			kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN);
2851 			initiator_node_alias = NULL;
2852 		}
2853 		break;
2854 
2855 	/*
2856 	 * ISCSI_DISCOVERY_ADDR_SET:
2857 	 */
2858 	case ISCSI_DISCOVERY_ADDR_SET:
2859 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
2860 			rtn = EFAULT;
2861 			break;
2862 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
2863 			rtn = EINVAL;
2864 			break;
2865 		}
2866 
2867 		if (e.e_oid == ISCSI_OID_NOTSET) {
2868 			mutex_enter(&iscsi_oid_mutex);
2869 			e.e_oid = iscsi_oid++;
2870 			mutex_exit(&iscsi_oid_mutex);
2871 		}
2872 
2873 		if (persistent_disc_addr_set(&e) == B_FALSE) {
2874 			rtn = EIO;
2875 			break;
2876 		}
2877 
2878 		/*
2879 		 * If Send Targets discovery is enabled, then kickoff
2880 		 * discovery of the targets advertised by the recently
2881 		 * added discovery address.
2882 		 */
2883 		method = persistent_disc_meth_get();
2884 		if (method & iSCSIDiscoveryMethodSendTargets) {
2885 
2886 			iscsid_addr_to_sockaddr(e.e_insize,
2887 			    &e.e_u, e.e_port, &addr_dsc.sin);
2888 			iscsid_do_sendtgts(&e);
2889 			(void) iscsid_login_tgt(ihp, NULL,
2890 			    iSCSIDiscoveryMethodSendTargets,
2891 			    &addr_dsc.sin);
2892 
2893 		}
2894 		break;
2895 
2896 	/*
2897 	 * ISCSI_DISCOVERY_ADDR_LIST_GET
2898 	 */
2899 	case ISCSI_DISCOVERY_ADDR_LIST_GET:
2900 		/* copyin user args */
2901 		if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) {
2902 			rtn = EFAULT;
2903 			break;
2904 		}
2905 
2906 		if (ial.al_vers != ISCSI_INTERFACE_VERSION) {
2907 			rtn = EINVAL;
2908 			break;
2909 		}
2910 
2911 		list_space = sizeof (iscsi_addr_list_t);
2912 		if (ial.al_in_cnt != 0) {
2913 			list_space += (sizeof (iscsi_addr_t) *
2914 			    (ial.al_in_cnt - 1));
2915 		}
2916 
2917 		ialp = kmem_zalloc(list_space, KM_SLEEP);
2918 		bcopy(&ial, ialp, sizeof (iscsi_addr_list_t));
2919 
2920 		void_p = NULL;
2921 		ialp->al_out_cnt = 0;
2922 		persistent_disc_addr_lock();
2923 		while (persistent_disc_addr_next(&void_p, &e) == B_TRUE) {
2924 			if (ialp->al_out_cnt < ialp->al_in_cnt) {
2925 				int		i = ialp->al_out_cnt;
2926 				iscsi_addr_t	*addr = &ialp->al_addrs[i];
2927 
2928 				addr->a_port = e.e_port;
2929 				addr->a_addr.i_insize = e.e_insize;
2930 				addr->a_oid = e.e_oid;
2931 
2932 				if (e.e_insize == sizeof (struct in_addr)) {
2933 					/* IPv4 */
2934 					addr->a_addr.i_addr.in4.s_addr =
2935 					    e.e_u.u_in4.s_addr;
2936 				} else if (e.e_insize ==
2937 					    sizeof (struct in6_addr)) {
2938 					/* IPv6 */
2939 					bcopy(e.e_u.u_in6.s6_addr,
2940 					    addr->a_addr.i_addr.in6.s6_addr,
2941 					    16);
2942 				}
2943 			}
2944 			ialp->al_out_cnt++;
2945 		}
2946 		persistent_disc_addr_unlock();
2947 
2948 		rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode);
2949 		kmem_free(ialp, list_space);
2950 		break;
2951 
2952 	/*
2953 	 * ISCSI_ISNS_SERVER_ADDR_LIST_GET
2954 	 */
2955 	case ISCSI_ISNS_SERVER_ADDR_LIST_GET:
2956 		/* copyin user args */
2957 		if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) {
2958 			rtn = EFAULT;
2959 			break;
2960 		}
2961 
2962 		if (ial.al_vers != ISCSI_INTERFACE_VERSION) {
2963 			rtn = EINVAL;
2964 			break;
2965 		}
2966 
2967 		list_space = sizeof (iscsi_addr_list_t);
2968 		if (ial.al_in_cnt != 0) {
2969 			list_space += (sizeof (iscsi_addr_t) *
2970 			    (ial.al_in_cnt - 1));
2971 		}
2972 
2973 		ialp = kmem_zalloc(list_space, KM_SLEEP);
2974 		bcopy(&ial, ialp, sizeof (iscsi_addr_list_t));
2975 
2976 		void_p = NULL;
2977 		ialp->al_out_cnt = 0;
2978 		persistent_isns_addr_lock();
2979 		while (persistent_isns_addr_next(&void_p, &e) == B_TRUE) {
2980 			if (ialp->al_out_cnt < ialp->al_in_cnt) {
2981 				int		i = ialp->al_out_cnt;
2982 				iscsi_addr_t	*addr = &ialp->al_addrs[i];
2983 
2984 				addr->a_port = e.e_port;
2985 				addr->a_addr.i_insize = e.e_insize;
2986 				if (e.e_insize == sizeof (struct in_addr)) {
2987 					/* IPv4 */
2988 					addr->a_addr.i_addr.in4.s_addr =
2989 					    e.e_u.u_in4.s_addr;
2990 				} else if (e.e_insize ==
2991 					    sizeof (struct in6_addr)) {
2992 					/* IPv6 */
2993 					bcopy(e.e_u.u_in6.s6_addr,
2994 					    addr->a_addr.i_addr.in6.s6_addr,
2995 					    16);
2996 				}
2997 			}
2998 			ialp->al_out_cnt++;
2999 		}
3000 		persistent_isns_addr_unlock();
3001 
3002 		rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode);
3003 		kmem_free(ialp, list_space);
3004 		break;
3005 
3006 	/*
3007 	 * ISCSI_DISCOVERY_ADDR_CLEAR:
3008 	 */
3009 	case ISCSI_DISCOVERY_ADDR_CLEAR:
3010 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
3011 			rtn = EFAULT;
3012 			break;
3013 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
3014 			rtn = EINVAL;
3015 			break;
3016 		}
3017 
3018 		iscsid_addr_to_sockaddr(e.e_insize,
3019 		    &e.e_u, e.e_port, &addr_dsc.sin);
3020 
3021 		/* If discovery in progress, try few times before return busy */
3022 		retry = 0;
3023 		mutex_enter(&ihp->hba_discovery_events_mutex);
3024 		while (ihp->hba_discovery_in_progress == B_TRUE) {
3025 			if (++retry == 5) {
3026 				rtn = EBUSY;
3027 				break;
3028 			}
3029 			mutex_exit(&ihp->hba_discovery_events_mutex);
3030 			delay(SEC_TO_TICK(ISCSI_DISC_DELAY));
3031 			mutex_enter(&ihp->hba_discovery_events_mutex);
3032 		}
3033 
3034 		/*
3035 		 * Clear discovery address first, so that any bus config
3036 		 * will ignore this discovery address
3037 		 */
3038 		if (rtn == 0 && persistent_disc_addr_clear(&e) == B_FALSE) {
3039 			rtn = EIO;
3040 		}
3041 		mutex_exit(&ihp->hba_discovery_events_mutex);
3042 
3043 		if (rtn != 0) {
3044 			break;
3045 		}
3046 		/* Attempt to logout of associated targets */
3047 		if (iscsid_del(ihp, NULL,
3048 		    iSCSIDiscoveryMethodSendTargets, &addr_dsc.sin) ==
3049 		    B_FALSE) {
3050 			/* Failure!, restore the discovery addr. */
3051 			if (persistent_disc_addr_set(&e) == B_FALSE) {
3052 				cmn_err(CE_WARN, "Failed to restore sendtgt "
3053 				    "discovery address after logout associated "
3054 				    "targets failures.");
3055 			}
3056 			rtn = EBUSY;
3057 		}
3058 		break;
3059 
3060 	/*
3061 	 * ISCSI_ISNS_SERVER_CLEAR:
3062 	 */
3063 	case ISCSI_ISNS_SERVER_ADDR_CLEAR:
3064 		if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) {
3065 			rtn = EFAULT;
3066 			break;
3067 		} else if (e.e_vers != ISCSI_INTERFACE_VERSION) {
3068 			rtn = EINVAL;
3069 			break;
3070 		}
3071 
3072 		iscsid_addr_to_sockaddr(e.e_insize,
3073 		    &e.e_u, e.e_port, &addr_dsc.sin);
3074 
3075 		/* If discovery in progress, try few times before return busy */
3076 		retry = 0;
3077 		mutex_enter(&ihp->hba_discovery_events_mutex);
3078 		while (ihp->hba_discovery_in_progress == B_TRUE) {
3079 			if (++retry == 5) {
3080 				rtn = EBUSY;
3081 				break;
3082 			}
3083 			mutex_exit(&ihp->hba_discovery_events_mutex);
3084 			delay(SEC_TO_TICK(ISCSI_DISC_DELAY));
3085 			mutex_enter(&ihp->hba_discovery_events_mutex);
3086 		}
3087 
3088 		/*
3089 		 * Clear isns server address first, so that any bus config
3090 		 * will ignore any target registerd on this isns server
3091 		 */
3092 		if (rtn == 0 && persistent_isns_addr_clear(&e) == B_FALSE) {
3093 			rtn = EIO;
3094 		}
3095 		mutex_exit(&ihp->hba_discovery_events_mutex);
3096 
3097 		if (rtn != 0) {
3098 			break;
3099 		}
3100 
3101 		/* Attempt logout of associated targets */
3102 		if (iscsid_del(ihp, NULL, iSCSIDiscoveryMethodISNS,
3103 		    &addr_dsc.sin) == B_FALSE) {
3104 			/* Failure!, restore the isns server addr. */
3105 
3106 			if (persistent_isns_addr_set(&e) == B_FALSE) {
3107 				cmn_err(CE_WARN, "Failed to restore isns server"
3108 				    " address after logout associated targets"
3109 				    " failures.");
3110 			}
3111 			rtn = EBUSY;
3112 		} else {
3113 			method = persistent_disc_meth_get();
3114 			if (method & iSCSIDiscoveryMethodISNS) {
3115 				boolean_t is_last_isns_server_b =
3116 				    B_FALSE;
3117 				int isns_server_count = 0;
3118 				void *void_p = NULL;
3119 
3120 				/*
3121 				 * Check if the last iSNS server's been
3122 				 * removed.
3123 				 */
3124 				{
3125 					entry_t tmp_e;
3126 					persistent_isns_addr_lock();
3127 					while (persistent_isns_addr_next(
3128 					    &void_p, &tmp_e) == B_TRUE) {
3129 						isns_server_count++;
3130 					}
3131 				}
3132 				persistent_isns_addr_unlock();
3133 				if (isns_server_count == 0) {
3134 					is_last_isns_server_b = B_TRUE;
3135 				}
3136 
3137 				/*
3138 				 * Deregister this node from this iSNS
3139 				 * server.
3140 				 */
3141 				initiator_node_name = kmem_zalloc(
3142 				    ISCSI_MAX_NAME_LEN, KM_SLEEP);
3143 				if (persistent_initiator_name_get(
3144 				    initiator_node_name,
3145 				    ISCSI_MAX_NAME_LEN) == B_TRUE) {
3146 
3147 					if (strlen(initiator_node_name) > 0) {
3148 						(void) isns_dereg_one_server(
3149 						    &e, (uint8_t *)
3150 						    initiator_node_name,
3151 						    is_last_isns_server_b);
3152 					}
3153 				}
3154 				kmem_free(initiator_node_name,
3155 				    ISCSI_MAX_NAME_LEN);
3156 				initiator_node_name = NULL;
3157 			}
3158 		}
3159 		break;
3160 
3161 	/*
3162 	 * ISCSI_DISCOVERY_SET -
3163 	 */
3164 	case ISCSI_DISCOVERY_SET:
3165 		if (ddi_copyin((caddr_t)arg, &method, sizeof (method), mode)) {
3166 			rtn = EFAULT;
3167 			break;
3168 		}
3169 
3170 		if (persistent_disc_meth_set(method) == B_FALSE) {
3171 			rtn = EIO;
3172 		} else {
3173 			(void) iscsid_enable_discovery(ihp, method, B_FALSE);
3174 			iscsid_poke_discovery(ihp, method);
3175 			(void) iscsid_login_tgt(ihp, NULL, method, NULL);
3176 		}
3177 		break;
3178 
3179 	/*
3180 	 * ISCSI_DISCOVERY_GET -
3181 	 */
3182 	case ISCSI_DISCOVERY_GET:
3183 		method = persistent_disc_meth_get();
3184 		rtn = ddi_copyout(&method, (caddr_t)arg,
3185 		    sizeof (method), mode);
3186 		break;
3187 
3188 	/*
3189 	 * ISCSI_DISCOVERY_CLEAR -
3190 	 */
3191 	case ISCSI_DISCOVERY_CLEAR:
3192 		if (ddi_copyin((caddr_t)arg, &method, sizeof (method), mode)) {
3193 			rtn = EFAULT;
3194 			break;
3195 		}
3196 
3197 		/* If discovery in progress, try few times before return busy */
3198 		retry = 0;
3199 		mutex_enter(&ihp->hba_discovery_events_mutex);
3200 		while (ihp->hba_discovery_in_progress == B_TRUE) {
3201 			if (++retry == 5) {
3202 				rtn = EBUSY;
3203 				break;
3204 			}
3205 			mutex_exit(&ihp->hba_discovery_events_mutex);
3206 			delay(SEC_TO_TICK(ISCSI_DISC_DELAY));
3207 			mutex_enter(&ihp->hba_discovery_events_mutex);
3208 		}
3209 
3210 		/*
3211 		 * Clear discovery first, so that any bus config or
3212 		 * discovery requests will ignore this discovery method
3213 		 */
3214 		if (rtn == 0 && persistent_disc_meth_clear(method) == B_FALSE) {
3215 			rtn = EIO;
3216 		}
3217 		mutex_exit(&ihp->hba_discovery_events_mutex);
3218 
3219 		if (rtn != 0) {
3220 			break;
3221 		}
3222 
3223 		/* Attempt to logout from all associated targets */
3224 		if (iscsid_disable_discovery(ihp, method) == B_FALSE) {
3225 			/* Failure!, reset the discovery */
3226 			if (persistent_disc_meth_set(method) == B_FALSE) {
3227 				cmn_err(CE_WARN, "Failed to reset discovery "
3228 				    "method after discovery disable failure.");
3229 			}
3230 			rtn = EBUSY;
3231 		}
3232 		break;
3233 
3234 	/*
3235 	 * ISCSI_DISCOVERY_PROPS -
3236 	 */
3237 	case ISCSI_DISCOVERY_PROPS:
3238 		iscsid_props(&discovery_props);
3239 		if (ddi_copyout(&discovery_props, (caddr_t)arg,
3240 		    sizeof (discovery_props), mode))
3241 			rtn = EFAULT;
3242 		break;
3243 
3244 	/*
3245 	 * ISCSI_LUN_OID_LIST --
3246 	 */
3247 	case ISCSI_LUN_OID_LIST_GET:
3248 		ll = (iscsi_lun_list_t *)kmem_alloc(sizeof (*ll), KM_SLEEP);
3249 		if (ddi_copyin((caddr_t)arg, ll, sizeof (*ll), mode)) {
3250 			rtn = EFAULT;
3251 			kmem_free(ll, sizeof (*ll));
3252 			break;
3253 		}
3254 
3255 		if (ll->ll_vers != ISCSI_INTERFACE_VERSION) {
3256 			rtn = EINVAL;
3257 			kmem_free(ll, sizeof (*ll));
3258 			break;
3259 		}
3260 
3261 		/*
3262 		 * Find out how much space the user has allocated in their
3263 		 * structure. Match the same space for our structure.
3264 		 */
3265 		lun_sz = sizeof (iscsi_lun_list_t);
3266 		if (ll->ll_in_cnt > 0) {
3267 			lun_sz += (ll->ll_in_cnt - 1) * sizeof (iscsi_if_lun_t);
3268 		}
3269 
3270 		llp = kmem_zalloc(lun_sz, KM_SLEEP);
3271 		bcopy(ll, llp, sizeof (*ll));
3272 		kmem_free(ll, sizeof (*ll));
3273 
3274 		/*
3275 		 * Check to see if oid references a target-param oid.  If so,
3276 		 * find the associated  session oid before getting lu list.
3277 		 */
3278 		if (iscsi_targetparam_get_name(llp->ll_tgt_oid) != NULL) {
3279 			for (isp = ihp->hba_sess_list; isp;
3280 			    isp = isp->sess_next) {
3281 				if (isp->sess_target_oid == llp->ll_tgt_oid) {
3282 					target_oid  = isp->sess_oid;
3283 					break;
3284 				}
3285 			}
3286 		} else {
3287 			target_oid = llp->ll_tgt_oid;
3288 		}
3289 
3290 
3291 		/*
3292 		 * Look at the LUNs attached to the specified target. If there
3293 		 * is space in the user structure save that information locally.
3294 		 * Always add up the count to the total. By always adding
3295 		 * the count this code can be used if ll_in_cnt == 0 and
3296 		 * the user just wishes to know the appropriate size to
3297 		 * allocate.
3298 		 */
3299 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3300 		for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
3301 			if ((llp->ll_all_tgts == B_FALSE) &&
3302 			    (isp->sess_oid != target_oid)) {
3303 				continue;
3304 			}
3305 			rw_enter(&isp->sess_lun_list_rwlock, RW_READER);
3306 			for (ilp = isp->sess_lun_list; ilp;
3307 			    ilp = ilp->lun_next) {
3308 				if ((ilp->lun_state &
3309 				    ISCSI_LUN_STATE_ONLINE) &&
3310 				    !(ilp->lun_state &
3311 				    ISCSI_LUN_STATE_INVALID)) {
3312 					if (llp->ll_out_cnt <
3313 					    llp->ll_in_cnt) {
3314 						iscsi_if_lun_t *lp;
3315 						lp = &llp->ll_luns[
3316 						    llp->ll_out_cnt];
3317 
3318 						lp->l_tgt_oid =
3319 						    isp->sess_oid;
3320 						lp->l_oid = ilp->lun_oid;
3321 						lp->l_num = ilp->lun_num;
3322 					}
3323 				llp->ll_out_cnt++;
3324 				}
3325 			}
3326 			rw_exit(&isp->sess_lun_list_rwlock);
3327 		}
3328 		rw_exit(&ihp->hba_sess_list_rwlock);
3329 
3330 		if (ddi_copyout(llp, (caddr_t)arg, lun_sz, mode)) {
3331 			rtn = EFAULT;
3332 		}
3333 
3334 		kmem_free(llp, lun_sz);
3335 		break;
3336 
3337 	/*
3338 	 * ISCSI_LUN_PROPS_GET --
3339 	 */
3340 	case ISCSI_LUN_PROPS_GET:
3341 		lun = (iscsi_lun_props_t *)kmem_zalloc(sizeof (*lun), KM_SLEEP);
3342 		if (ddi_copyin((caddr_t)arg, lun, sizeof (*lun), mode)) {
3343 			rtn = EFAULT;
3344 			kmem_free(lun, sizeof (*lun));
3345 			break;
3346 		}
3347 
3348 		if (lun->lp_vers != ISCSI_INTERFACE_VERSION) {
3349 			rtn = EINVAL;
3350 			kmem_free(lun, sizeof (*lun));
3351 			break;
3352 		}
3353 
3354 		/*
3355 		 * For the target specified, find the LUN specified and
3356 		 * return its properties
3357 		 */
3358 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3359 		rtn = iscsi_sess_get(lun->lp_tgt_oid, ihp, &isp);
3360 		if (rtn != 0) {
3361 			rw_exit(&ihp->hba_sess_list_rwlock);
3362 			rtn = EFAULT;
3363 			kmem_free(lun, sizeof (*lun));
3364 			break;
3365 		}
3366 		rtn = EINVAL;	/* Set bad rtn, correct only if found */
3367 		rw_enter(&isp->sess_lun_list_rwlock, RW_READER);
3368 		for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) {
3369 			if (ilp->lun_oid == lun->lp_oid) {
3370 				lun->lp_num	= ilp->lun_num;
3371 				lun->lp_status	= LunValid;
3372 				lun->lp_time_online = ilp->lun_time_online;
3373 
3374 				if (ilp->lun_pip != NULL) {
3375 					lun_dip = mdi_pi_get_client(
3376 					    ilp->lun_pip);
3377 				} else {
3378 					lun_dip = ilp->lun_dip;
3379 				}
3380 
3381 				if (lun_dip != NULL &&
3382 				    ((i_ddi_devi_attached(lun_dip)) ||
3383 				    (ddi_get_devstate(lun_dip) ==
3384 				    DDI_DEVSTATE_UP))) {
3385 					(void) ddi_pathname(lun_dip,
3386 					    lun->lp_pathname);
3387 				} else {
3388 					/*
3389 					 * The LUN is not exported to the
3390 					 * OS yet.  It is in the process
3391 					 * of being added.
3392 					 */
3393 					lun->lp_status	= LunDoesNotExist;
3394 				}
3395 				bcopy(ilp->lun_vid, lun->lp_vid,
3396 				    sizeof (lun->lp_vid));
3397 				bcopy(ilp->lun_pid, lun->lp_pid,
3398 				    sizeof (lun->lp_pid));
3399 				rtn = ddi_copyout(lun, (caddr_t)arg,
3400 				    sizeof (*lun), mode);
3401 				if (rtn == -1) {
3402 					rtn = EFAULT;
3403 				}
3404 				break;
3405 			}
3406 		}
3407 		rw_exit(&isp->sess_lun_list_rwlock);
3408 		rw_exit(&ihp->hba_sess_list_rwlock);
3409 
3410 		kmem_free(lun, sizeof (*lun));
3411 		break;
3412 
3413 	/*
3414 	 * ISCSI_CONN_OID_LIST_GET --
3415 	 */
3416 #define	ISCSIIOCOLGC iscsi_ioctl_conn_oid_list_get_copyout
3417 	case ISCSI_CONN_OID_LIST_GET:
3418 		{
3419 			iscsi_conn_list_t	*cl;
3420 
3421 			/* Asuume the worst */
3422 			rtn = EFAULT;
3423 
3424 			/* Copy the input argument into kernel world. */
3425 			cl = iscsi_ioctl_conn_oid_list_get_copyin(
3426 			    (caddr_t)arg,
3427 			    mode);
3428 			if (cl != NULL) {
3429 				if (iscsi_ioctl_conn_oid_list_get(ihp, cl) ==
3430 				    B_TRUE) {
3431 					rtn =
3432 					    ISCSIIOCOLGC(
3433 					    cl, (caddr_t)arg, mode);
3434 				}
3435 			}
3436 			break;
3437 		}
3438 #undef ISCSIIOCOLGC
3439 	/*
3440 	 * ISCSI_CONN_OID_LIST_GET --
3441 	 */
3442 	case ISCSI_CONN_PROPS_GET:
3443 		{
3444 			iscsi_conn_props_t	*cp;
3445 
3446 			/* Asuume the worst */
3447 			rtn = EFAULT;
3448 
3449 			/* Copy the input argument into kernel world. */
3450 			cp = iscsi_ioctl_copyin(
3451 			    (caddr_t)arg,
3452 			    mode,
3453 			    sizeof (iscsi_conn_props_t));
3454 
3455 			if (cp != NULL) {
3456 				/* Get the propereties. */
3457 				if (iscsi_ioctl_conn_props_get(ihp, cp) ==
3458 				    B_TRUE) {
3459 					rtn =
3460 					    iscsi_ioctl_copyout(
3461 					    cp,
3462 					    sizeof (*cp),
3463 					    (caddr_t)arg,
3464 					    mode);
3465 				} else {
3466 					kmem_free(cp, sizeof (*cp));
3467 					cp = NULL;
3468 				}
3469 			}
3470 			break;
3471 		}
3472 
3473 	/*
3474 	 * ISCSI_RADIUS_GET -
3475 	 */
3476 	case ISCSI_RADIUS_GET:
3477 	{
3478 		iscsi_nvfile_status_t	status;
3479 
3480 		radius = (iscsi_radius_props_t *)kmem_zalloc(sizeof (*radius),
3481 		    KM_SLEEP);
3482 		if (ddi_copyin((caddr_t)arg, radius, sizeof (*radius), mode)) {
3483 			kmem_free(radius, sizeof (*radius));
3484 			rtn = EFAULT;
3485 			break;
3486 		} else if (radius->r_vers != ISCSI_INTERFACE_VERSION) {
3487 			kmem_free(radius, sizeof (*radius));
3488 			rtn = EINVAL;
3489 			break;
3490 		}
3491 
3492 		old_oid = radius->r_oid;
3493 
3494 		if (radius->r_oid == ihp->hba_oid) {
3495 			name = ihp->hba_name;
3496 		} else {
3497 			/*
3498 			 * RADIUS configuration should be done on a per
3499 			 * initiator basis.
3500 			 */
3501 			kmem_free(radius, sizeof (*radius));
3502 			rtn = EINVAL;
3503 			break;
3504 		}
3505 
3506 		status = persistent_radius_get(radius);
3507 		if (status == ISCSI_NVFILE_SUCCESS) {
3508 			/*
3509 			 * Restore the value for overridden (and bogus) oid.
3510 			 */
3511 			radius->r_oid = old_oid;
3512 			rtn = ddi_copyout(radius, (caddr_t)arg,
3513 			    sizeof (*radius), mode);
3514 		} else if (status == ISCSI_NVFILE_NAMEVAL_NOT_FOUND) {
3515 			rtn = ENOENT;
3516 		} else {
3517 			rtn = EIO;
3518 		}
3519 		kmem_free(radius, sizeof (*radius));
3520 		break;
3521 	}
3522 
3523 	/*
3524 	 * ISCSI_RADIUS_SET -
3525 	 */
3526 	case ISCSI_RADIUS_SET:
3527 		radius = (iscsi_radius_props_t *)kmem_zalloc(sizeof (*radius),
3528 		    KM_SLEEP);
3529 		if (ddi_copyin((caddr_t)arg, radius, sizeof (*radius), mode)) {
3530 			rtn = EFAULT;
3531 			kmem_free(radius, sizeof (*radius));
3532 			break;
3533 		} else if (radius->r_vers != ISCSI_INTERFACE_VERSION) {
3534 			rtn = EINVAL;
3535 			kmem_free(radius, sizeof (*radius));
3536 			break;
3537 		}
3538 
3539 		if (radius->r_oid == ihp->hba_oid) {
3540 			name = ihp->hba_name;
3541 		} else {
3542 			/*
3543 			 * RADIUS configuration should be done on a per
3544 			 * initiator basis.
3545 			 */
3546 			kmem_free(radius, sizeof (*radius));
3547 			rtn = EINVAL;
3548 			break;
3549 		}
3550 
3551 		if (persistent_radius_set(radius) == B_FALSE) {
3552 			rtn = EIO;
3553 		}
3554 
3555 		kmem_free(radius, sizeof (*radius));
3556 		break;
3557 
3558 	/*
3559 	 *  ISCSI_AUTH_GET -
3560 	 */
3561 	case ISCSI_AUTH_GET:
3562 		auth = (iscsi_auth_props_t *)kmem_zalloc(sizeof (*auth),
3563 		    KM_SLEEP);
3564 		if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) {
3565 			kmem_free(auth, sizeof (*auth));
3566 			rtn = EFAULT;
3567 			break;
3568 		} else if (auth->a_vers != ISCSI_INTERFACE_VERSION) {
3569 			kmem_free(auth, sizeof (*auth));
3570 			rtn = EINVAL;
3571 			break;
3572 		}
3573 
3574 		old_oid = auth->a_oid;
3575 
3576 		if (auth->a_oid == ihp->hba_oid) {
3577 			name = ihp->hba_name;
3578 		} else {
3579 
3580 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3581 			/*
3582 			 * If the oid does represent a session check to see
3583 			 * if it is a target oid.  If so, return the target's
3584 			 * associated session.
3585 			 */
3586 			rtn = iscsi_sess_get(auth->a_oid, ihp, &isp);
3587 			if (rtn != 0) {
3588 				rtn = iscsi_sess_get_by_target(auth->a_oid,
3589 				    ihp, &isp);
3590 			}
3591 			rw_exit(&ihp->hba_sess_list_rwlock);
3592 
3593 			/*
3594 			 * If rtn is zero then we have found an
3595 			 * existing session.  Use the session name to
3596 			 * do param lookup.  If rtn is non-zero then
3597 			 * create a targetparam object and use its name
3598 			 * for param lookup.
3599 			 */
3600 			if (rtn == 0) {
3601 				name = isp->sess_name;
3602 			} else {
3603 				name =
3604 				    iscsi_targetparam_get_name(auth->a_oid);
3605 			}
3606 		}
3607 
3608 		if (name == NULL) {
3609 			rtn = EFAULT;
3610 			break;
3611 		}
3612 
3613 		if (persistent_auth_get((char *)name, auth) == B_TRUE) {
3614 			/*
3615 			 * Restore the value for overridden (and bogus) oid.
3616 			 */
3617 			auth->a_oid = old_oid;
3618 			rtn = ddi_copyout(auth, (caddr_t)arg,
3619 			    sizeof (*auth), mode);
3620 		} else {
3621 			rtn = EIO;
3622 		}
3623 
3624 		kmem_free(auth, sizeof (*auth));
3625 		break;
3626 
3627 	/*
3628 	 *  ISCSI_AUTH_SET -
3629 	 */
3630 	case ISCSI_AUTH_SET:
3631 		auth = (iscsi_auth_props_t *)kmem_zalloc(sizeof (*auth),
3632 		    KM_SLEEP);
3633 		if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) {
3634 			kmem_free(auth, sizeof (*auth));
3635 			rtn = EFAULT;
3636 			break;
3637 		} else if (auth->a_vers != ISCSI_INTERFACE_VERSION) {
3638 			kmem_free(auth, sizeof (*auth));
3639 			rtn = EINVAL;
3640 			break;
3641 		}
3642 
3643 		if (auth->a_oid == ihp->hba_oid) {
3644 			name = ihp->hba_name;
3645 		} else {
3646 			rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3647 			/*
3648 			 * If the oid does represent a session check to see
3649 			 * if it is a target oid.  If so, return the target's
3650 			 * associated session.
3651 			 */
3652 			rtn = iscsi_sess_get(auth->a_oid, ihp, &isp);
3653 			if (rtn != 0) {
3654 				rtn = iscsi_sess_get_by_target(auth->a_oid,
3655 				    ihp, &isp);
3656 			}
3657 			rw_exit(&ihp->hba_sess_list_rwlock);
3658 
3659 			/*
3660 			 * If rtn is zero then we have found an
3661 			 * existing session.  Use the session name to
3662 			 * do param lookup.  If rtn is non-zero then
3663 			 * create a targetparam object and use its name
3664 			 * for param lookup.
3665 			 */
3666 			if (rtn == 0) {
3667 				name = isp->sess_name;
3668 			} else {
3669 				name =
3670 				    iscsi_targetparam_get_name(auth->a_oid);
3671 				rtn = 0;
3672 			}
3673 		}
3674 
3675 		if (name == NULL) {
3676 			rtn = EFAULT;
3677 		} else if (persistent_auth_set((char *)name, auth)
3678 		    == B_FALSE) {
3679 			rtn = EIO;
3680 		}
3681 
3682 		kmem_free(auth, sizeof (*auth));
3683 		break;
3684 
3685 	/*
3686 	 *  ISCSI_AUTH_CLEAR -
3687 	 */
3688 	case ISCSI_AUTH_CLEAR:
3689 		auth = (iscsi_auth_props_t *)kmem_alloc(sizeof (*auth),
3690 		    KM_SLEEP);
3691 		if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) {
3692 			kmem_free(auth, sizeof (*auth));
3693 			rtn = EFAULT;
3694 			break;
3695 		} else if (auth->a_vers != ISCSI_INTERFACE_VERSION) {
3696 			kmem_free(auth, sizeof (*auth));
3697 			rtn = EINVAL;
3698 			break;
3699 		}
3700 
3701 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3702 		/*
3703 		 * If the oid does represent a session check to see
3704 		 * if it is a target oid.  If so, return the target's
3705 		 * associated session.
3706 		 */
3707 		rtn = iscsi_sess_get(auth->a_oid, ihp, &isp);
3708 		if (rtn != 0) {
3709 			rtn = iscsi_sess_get_by_target(auth->a_oid, ihp, &isp);
3710 		}
3711 		rw_exit(&ihp->hba_sess_list_rwlock);
3712 
3713 		/*
3714 		 * If rtn is zero then we have found an
3715 		 * existing session.  Use the session name to
3716 		 * do param lookup.  If rtn is non-zero then
3717 		 * create a targetparam object and use its name
3718 		 * for param lookup.
3719 		 */
3720 		if (rtn == 0) {
3721 			name = isp->sess_name;
3722 		} else {
3723 			name =
3724 			    iscsi_targetparam_get_name(auth->a_oid);
3725 			rtn = 0;
3726 			discovered = B_FALSE;
3727 		}
3728 
3729 		if (name == NULL) {
3730 			rw_exit(
3731 			    &ihp->hba_sess_list_rwlock);
3732 			rtn = EFAULT;
3733 			break;
3734 		}
3735 
3736 		if (persistent_auth_clear((char *)name) == B_FALSE) {
3737 			rtn = EIO;
3738 		}
3739 
3740 		/*
3741 		 * ISCSI_TARGET_PARAM_CLEAR, ISCSI_CHAP_CLEAR and
3742 		 * ISCSI_AUTH_CLEAR ioctl are called sequentially to remove
3743 		 * target parameters. Here, the target that is not discovered
3744 		 * by initiator should be removed from the iscsi_targets list
3745 		 * residing in the memory.
3746 		 */
3747 		if (discovered == B_FALSE) {
3748 			(void) iscsi_targetparam_remove_target(auth->a_oid);
3749 		}
3750 
3751 		kmem_free(auth, sizeof (*auth));
3752 		break;
3753 
3754 	/*
3755 	 * ISCSI_DB_DUMP -
3756 	 */
3757 	case ISCSI_DB_DUMP:
3758 		persistent_dump_data();
3759 		break;
3760 
3761 	case ISCSI_USCSI:
3762 
3763 #ifdef _MULTI_DATAMODEL
3764 		model = ddi_model_convert_from(mode & FMODELS);
3765 		switch (model) {
3766 		case DDI_MODEL_ILP32:
3767 
3768 			if (ddi_copyin((caddr_t)arg, &iu32_caller,
3769 			    sizeof (iscsi_uscsi32_t), mode)) {
3770 				rtn = EFAULT;
3771 				break;
3772 			}
3773 
3774 			/* perform conversion from 32 -> 64 */
3775 			iu_caller.iu_vers = iu32_caller.iu_vers;
3776 			iu_caller.iu_oid = iu32_caller.iu_oid;
3777 			iu_caller.iu_tpgt = iu32_caller.iu_tpgt;
3778 			iu_caller.iu_len = iu32_caller.iu_len;
3779 			iu_caller.iu_lun = iu32_caller.iu_lun;
3780 			uscsi_cmd32touscsi_cmd((&iu32_caller.iu_ucmd),
3781 			    (&iu_caller.iu_ucmd));
3782 
3783 			break;
3784 		case DDI_MODEL_NONE:
3785 			if (ddi_copyin((caddr_t)arg, &iu_caller,
3786 			    sizeof (iscsi_uscsi_t), mode)) {
3787 				rtn = EFAULT;
3788 				break;
3789 			}
3790 			break;
3791 		default:
3792 			ASSERT(FALSE);
3793 			rtn = EINVAL;
3794 			break;
3795 		}
3796 #endif /* _MULTI_DATAMODEL */
3797 
3798 		/* If failures earlier break */
3799 		if (rtn != 0) {
3800 			break;
3801 		}
3802 
3803 		/* copy from caller to internel cmd */
3804 		bcopy(&iu_caller, &iu, sizeof (iu));
3805 
3806 		if (iu.iu_vers != ISCSI_INTERFACE_VERSION) {
3807 			rtn = EINVAL;
3808 			break;
3809 		}
3810 		/*
3811 		 * Check to see if oid references a target-param oid.  If so,
3812 		 * find the associated  session oid before getting lu list.
3813 		 */
3814 		if (iscsi_targetparam_get_name(iu.iu_oid) != NULL) {
3815 			for (isp = ihp->hba_sess_list; isp; isp =
3816 			    isp->sess_next) {
3817 				if (isp->sess_target_oid == iu.iu_oid) {
3818 					target_oid  = isp->sess_oid;
3819 					break;
3820 				}
3821 			}
3822 		} else {
3823 			target_oid = iu.iu_oid;
3824 		}
3825 
3826 		/* make sure we have a matching session for this command */
3827 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
3828 		rtn = iscsi_sess_get(target_oid, ihp, &isp);
3829 		if (rtn != 0) {
3830 			rtn = iscsi_sess_get_by_target(target_oid, ihp,
3831 			    &isp);
3832 			if (rtn != 0) {
3833 				rw_exit(&ihp->hba_sess_list_rwlock);
3834 				rtn = EFAULT;
3835 				break;
3836 			}
3837 		}
3838 		/*
3839 		 * If a caller buffer is present allocate duplicate
3840 		 * kernel space and copyin caller memory.
3841 		 */
3842 		if (iu.iu_ucmd.uscsi_buflen > 0) {
3843 			iu.iu_ucmd.uscsi_bufaddr = (caddr_t)kmem_alloc(
3844 			    iu.iu_ucmd.uscsi_buflen, KM_SLEEP);
3845 			if (ddi_copyin(iu_caller.iu_ucmd.uscsi_bufaddr,
3846 			    iu.iu_ucmd.uscsi_bufaddr,
3847 			    iu.iu_ucmd.uscsi_buflen, mode)) {
3848 				rw_exit(&ihp->hba_sess_list_rwlock);
3849 				rtn = EFAULT;
3850 				break;
3851 			}
3852 		}
3853 
3854 		/*
3855 		 * If a caller cdb is present allocate duplicate
3856 		 * kernel space and copyin caller memory.
3857 		 */
3858 		if (iu.iu_ucmd.uscsi_cdblen > 0) {
3859 			iu.iu_ucmd.uscsi_cdb = (caddr_t)kmem_alloc(
3860 			    iu_caller.iu_ucmd.uscsi_cdblen, KM_SLEEP);
3861 			if (ddi_copyin(iu_caller.iu_ucmd.uscsi_cdb,
3862 			    iu.iu_ucmd.uscsi_cdb,
3863 			    iu.iu_ucmd.uscsi_cdblen, mode)) {
3864 				if (iu.iu_ucmd.uscsi_buflen > 0) {
3865 					kmem_free(iu.iu_ucmd.uscsi_bufaddr,
3866 					    iu_caller.iu_ucmd.uscsi_buflen);
3867 				}
3868 				rw_exit(&ihp->hba_sess_list_rwlock);
3869 				rtn = EFAULT;
3870 				break;
3871 			}
3872 		}
3873 
3874 		/*
3875 		 * If a caller request sense is present allocate
3876 		 * duplicate kernel space.  No need to copyin.
3877 		 */
3878 		if (iu.iu_ucmd.uscsi_rqlen > 0) {
3879 			iu.iu_ucmd.uscsi_rqbuf = (caddr_t)kmem_alloc(
3880 			    iu.iu_ucmd.uscsi_rqlen, KM_SLEEP);
3881 		}
3882 
3883 		/* issue passthru to io path handler */
3884 		rtn = iscsi_handle_passthru(isp, iu.iu_lun, &iu.iu_ucmd);
3885 		if (rtn != 0) {
3886 			rtn = EFAULT;
3887 		}
3888 
3889 		/*
3890 		 * If the caller had a buf we need to do a copyout
3891 		 * and free the kernel memory
3892 		 */
3893 		if (iu.iu_ucmd.uscsi_buflen > 0) {
3894 			if (ddi_copyout(iu.iu_ucmd.uscsi_bufaddr,
3895 			    iu_caller.iu_ucmd.uscsi_bufaddr,
3896 			    iu.iu_ucmd.uscsi_buflen, mode) != 0) {
3897 				rtn = EFAULT;
3898 			}
3899 			kmem_free(iu.iu_ucmd.uscsi_bufaddr,
3900 			    iu.iu_ucmd.uscsi_buflen);
3901 		}
3902 
3903 		/* We need to free kernel cdb, no need to copyout */
3904 		if (iu.iu_ucmd.uscsi_cdblen > 0) {
3905 			kmem_free(iu.iu_ucmd.uscsi_cdb,
3906 			    iu.iu_ucmd.uscsi_cdblen);
3907 		}
3908 
3909 		/*
3910 		 * If the caller had a request sense we need to
3911 		 * do a copyout and free the kernel memory
3912 		 */
3913 		if (iu.iu_ucmd.uscsi_rqlen > 0) {
3914 			if (ddi_copyout(iu.iu_ucmd.uscsi_rqbuf,
3915 			    iu_caller.iu_ucmd.uscsi_rqbuf,
3916 			    iu.iu_ucmd.uscsi_rqlen, mode) != 0) {
3917 				rtn = EFAULT;
3918 			}
3919 			kmem_free(iu.iu_ucmd.uscsi_rqbuf,
3920 			    iu.iu_ucmd.uscsi_rqlen);
3921 		}
3922 
3923 #ifdef _MULTI_DATAMODEL
3924 		if (iu.iu_ucmd.uscsi_status != 0) {
3925 			switch (model = ddi_model_convert_from(
3926 			    mode & FMODELS)) {
3927 			case DDI_MODEL_ILP32:
3928 				iu32_caller.iu_ucmd.uscsi_status =
3929 				    iu.iu_ucmd.uscsi_status;
3930 				if (ddi_copyout((void *)&iu32_caller,
3931 				    (caddr_t)arg, sizeof (iscsi_uscsi32_t),
3932 				    mode) != 0) {
3933 					rtn = EFAULT;
3934 				}
3935 				break;
3936 			case DDI_MODEL_NONE:
3937 				iu_caller.iu_ucmd.uscsi_status =
3938 				    iu.iu_ucmd.uscsi_status;
3939 				if (ddi_copyout((void *)&iu_caller,
3940 				    (caddr_t)arg, sizeof (iscsi_uscsi_t),
3941 				    mode) != 0) {
3942 					rtn = EFAULT;
3943 				}
3944 				break;
3945 			default:
3946 				ASSERT(FALSE);
3947 			}
3948 		}
3949 #endif /* _MULTI_DATAMODEL */
3950 		rw_exit(&ihp->hba_sess_list_rwlock);
3951 		break;
3952 
3953 	case ISCSI_SMF_ONLINE:
3954 		if (ddi_copyin((caddr_t)arg, &did, sizeof (int), mode) != 0) {
3955 			rtn = EFAULT;
3956 			break;
3957 		}
3958 		/* just a theoretical case */
3959 		if (ihp->hba_persistent_loaded == B_FALSE) {
3960 			rtn = EFAULT;
3961 			break;
3962 		}
3963 
3964 		/* doesn't need to overwrite the status anymore */
3965 		mutex_enter(&ihp->hba_service_lock);
3966 		if (ihp->hba_service_status_overwrite == B_TRUE) {
3967 			ihp->hba_service_status = ISCSI_SERVICE_DISABLED;
3968 			ihp->hba_service_status_overwrite = B_FALSE;
3969 		}
3970 		mutex_exit(&ihp->hba_service_lock);
3971 
3972 		if (iscsi_enter_service_zone(ihp, ISCSI_SERVICE_ENABLED) ==
3973 		    B_FALSE) {
3974 			break;
3975 		}
3976 
3977 		rval = iscsi_door_bind(did);
3978 		if (rval == B_TRUE) {
3979 			rval = iscsid_start(ihp);
3980 			if (rval == B_FALSE) {
3981 				iscsi_door_unbind();
3982 			}
3983 		}
3984 
3985 		if (rval == B_TRUE) {
3986 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_ENABLED);
3987 		} else {
3988 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_DISABLED);
3989 			rtn = EFAULT;
3990 		}
3991 
3992 		break;
3993 
3994 	case ISCSI_SMF_OFFLINE:
3995 		if (iscsi_enter_service_zone(ihp, ISCSI_SERVICE_DISABLED)
3996 		    == B_FALSE) {
3997 			break;
3998 		}
3999 
4000 		rval = iscsid_stop(ihp);
4001 
4002 		if (rval == B_TRUE) {
4003 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_DISABLED);
4004 			iscsi_door_unbind();
4005 		} else {
4006 			iscsi_exit_service_zone(ihp, ISCSI_SERVICE_ENABLED);
4007 			rtn = EFAULT;
4008 		}
4009 		break;
4010 
4011 	case ISCSI_SMF_GET:
4012 		mutex_enter(&ihp->hba_service_lock);
4013 		while (ihp->hba_service_status ==
4014 		    ISCSI_SERVICE_TRANSITION) {
4015 			cv_wait(&ihp->hba_service_cv,
4016 			    &ihp->hba_service_lock);
4017 		}
4018 		if (ddi_copyout((void *)&ihp->hba_service_status,
4019 		    (caddr_t)arg, sizeof (boolean_t), mode) != 0) {
4020 			rtn = EFAULT;
4021 		}
4022 		mutex_exit(&ihp->hba_service_lock);
4023 		break;
4024 
4025 	case ISCSI_DISCOVERY_EVENTS:
4026 		/*
4027 		 * If discovery has not been completed and not in progress,
4028 		 * poke the discovery methods
4029 		 */
4030 		mutex_enter(&ihp->hba_discovery_events_mutex);
4031 		method = ihp->hba_discovery_events;
4032 		if ((method != ISCSI_ALL_DISCOVERY_METHODS) &&
4033 		    (ihp->hba_discovery_in_progress == B_FALSE)) {
4034 			ihp->hba_discovery_in_progress = B_TRUE;
4035 			mutex_exit(&ihp->hba_discovery_events_mutex);
4036 			iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodUnknown);
4037 			mutex_enter(&ihp->hba_discovery_events_mutex);
4038 			ihp->hba_discovery_in_progress = B_FALSE;
4039 			method = ihp->hba_discovery_events;
4040 		}
4041 		mutex_exit(&ihp->hba_discovery_events_mutex);
4042 
4043 		if (ddi_copyout((void *)&method, (caddr_t)arg,
4044 		    sizeof (method), mode) != 0)
4045 			rtn = EFAULT;
4046 		break;
4047 
4048 	/*
4049 	 * ISCSI_SENDTGTS_GET --
4050 	 */
4051 	case ISCSI_SENDTGTS_GET:
4052 		stl_hdr = iscsi_ioctl_copyin((caddr_t)arg, mode,
4053 		    sizeof (*stl_hdr));
4054 		if (stl_hdr == NULL) {
4055 			rtn = EFAULT;
4056 			break;
4057 		}
4058 
4059 		if (stl_hdr->stl_entry.e_vers != ISCSI_INTERFACE_VERSION) {
4060 			rtn = EINVAL;
4061 			kmem_free(stl_hdr, sizeof (*stl_hdr));
4062 			break;
4063 		}
4064 
4065 		/* calculate how much memory user allocated for SendTgts */
4066 		stl_sz = sizeof (*stl_hdr);
4067 		if (stl_hdr->stl_in_cnt > 0) {
4068 			stl_sz += ((stl_hdr->stl_in_cnt - 1) *
4069 			    sizeof (iscsi_sendtgts_entry_t));
4070 		}
4071 
4072 		/* allocate local SendTgts list of the same size */
4073 		istl = kmem_zalloc(stl_sz, KM_SLEEP);
4074 		bcopy(stl_hdr, istl, sizeof (*stl_hdr));
4075 		kmem_free(stl_hdr, sizeof (*stl_hdr));
4076 
4077 		/* lock interface so only one SendTargets operation occurs */
4078 		sema_p(&ihp->hba_sendtgts_semaphore);
4079 
4080 		rtn = iscsi_ioctl_sendtgts_get(ihp, istl);
4081 
4082 		if (rtn == 0) {
4083 			rtn = iscsi_ioctl_copyout(istl, stl_sz,
4084 			    (caddr_t)arg, mode);
4085 		}
4086 
4087 		/* release lock to allow another SendTargets discovery */
4088 		sema_v(&ihp->hba_sendtgts_semaphore);
4089 
4090 		break;
4091 
4092 		/*
4093 		 * ISCSI_ISNS_SERVER_GET --
4094 		 */
4095 	case ISCSI_ISNS_SERVER_GET:
4096 		server_pg_list_hdr = iscsi_ioctl_copyin((caddr_t)arg, mode,
4097 		    sizeof (*server_pg_list_hdr));
4098 		if (server_pg_list_hdr == NULL) {
4099 			rtn = EFAULT;
4100 			break;
4101 		}
4102 
4103 		/* If iSNS discovery mode is not set, return with zero entry */
4104 		method = persistent_disc_meth_get();
4105 		if ((method & iSCSIDiscoveryMethodISNS) == 0) {
4106 			kmem_free(server_pg_list_hdr,
4107 			    sizeof (*server_pg_list_hdr));
4108 			server_pg_list_hdr = NULL;
4109 			rtn = EACCES;
4110 			break;
4111 		}
4112 
4113 		initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP);
4114 		if (persistent_initiator_name_get(initiator_node_name,
4115 		    ISCSI_MAX_NAME_LEN) != B_TRUE) {
4116 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
4117 			initiator_node_name = NULL;
4118 			kmem_free(server_pg_list_hdr,
4119 			    sizeof (*server_pg_list_hdr));
4120 			server_pg_list_hdr = NULL;
4121 			rtn = EIO;
4122 			break;
4123 		}
4124 		if (strlen(initiator_node_name) == 0) {
4125 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
4126 			initiator_node_name = NULL;
4127 			kmem_free(server_pg_list_hdr,
4128 			    sizeof (*server_pg_list_hdr));
4129 			server_pg_list_hdr = NULL;
4130 			rtn = EIO;
4131 			break;
4132 		}
4133 
4134 		initiator_node_alias = kmem_zalloc(
4135 		    ISCSI_MAX_NAME_LEN, KM_SLEEP);
4136 		if (persistent_alias_name_get(initiator_node_alias,
4137 		    ISCSI_MAX_NAME_LEN) != B_TRUE) {
4138 			initiator_node_alias[0] = '\0';
4139 		}
4140 		rtn = isns_query_one_server(&(server_pg_list_hdr->addr),
4141 		    ihp->hba_isid,
4142 		    (uint8_t *)initiator_node_name,
4143 		    (uint8_t *)initiator_node_alias,
4144 		    ISNS_INITIATOR_NODE_TYPE,
4145 		    &pg_list);
4146 		if (rtn != isns_ok || pg_list == NULL) {
4147 			kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
4148 			initiator_node_name = NULL;
4149 			kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN);
4150 			initiator_node_alias = NULL;
4151 			kmem_free(server_pg_list_hdr,
4152 			    sizeof (*server_pg_list_hdr));
4153 			server_pg_list_hdr = NULL;
4154 			rtn = EIO;
4155 			break;
4156 		}
4157 
4158 		/*
4159 		 * pg_list_sz is the size of the pg_list returned from the
4160 		 *	isns_query_all
4161 		 *
4162 		 * pg_sz_copy_out is the size of the pg_list we are going to
4163 		 *	return back to the caller
4164 		 *
4165 		 * server_pg_list_sz is total amount of data we are returning
4166 		 *	back to the caller
4167 		 */
4168 		pg_list->pg_in_cnt =
4169 		    server_pg_list_hdr->addr_port_list.pg_in_cnt;
4170 		pg_list_sz = sizeof (isns_portal_group_list_t);
4171 		if (pg_list->pg_out_cnt > 0) {
4172 			pg_list_sz += (pg_list->pg_out_cnt - 1) *
4173 			    sizeof (isns_portal_group_t);
4174 		}
4175 		/*
4176 		 * check if caller passed in a buffer with enough space
4177 		 * if there isn't enough space, fill the caller's buffer with
4178 		 * as much information as possible.
4179 		 *
4180 		 * if pg_out_cnt > pg_in_cnt, pg_out_cnt will be returned with
4181 		 * the total number of targets found
4182 		 *
4183 		 * if pg_out_cnt < pg_in_cnt, pg_out_cnt will be the number
4184 		 * of targets returned
4185 		 */
4186 		if (pg_list->pg_in_cnt < pg_list->pg_out_cnt) {
4187 			pg_sz_copy_out = sizeof (isns_portal_group_list_t);
4188 			if (pg_list->pg_in_cnt > 0) {
4189 				pg_sz_copy_out += (pg_list->pg_in_cnt - 1) *
4190 				    sizeof (isns_portal_group_t);
4191 			}
4192 			server_pg_list_sz =
4193 			    sizeof (isns_server_portal_group_list_t);
4194 			if (pg_list->pg_in_cnt > 0) {
4195 				server_pg_list_sz += (pg_list->pg_in_cnt - 1) *
4196 				    sizeof (isns_portal_group_t);
4197 			}
4198 		} else {
4199 			pg_sz_copy_out = pg_list_sz;
4200 			server_pg_list_sz =
4201 			    sizeof (isns_server_portal_group_list_t);
4202 			if (pg_list->pg_out_cnt > 0) {
4203 				server_pg_list_sz += (pg_list->pg_out_cnt - 1) *
4204 				    sizeof (isns_portal_group_t);
4205 			}
4206 		}
4207 
4208 		server_pg_list = (isns_server_portal_group_list_t *)kmem_zalloc(
4209 		    server_pg_list_sz, KM_SLEEP);
4210 
4211 		bcopy(&(server_pg_list_hdr->addr), &(server_pg_list->addr),
4212 		    sizeof (server_pg_list->addr));
4213 		bcopy(pg_list, &server_pg_list->addr_port_list, pg_sz_copy_out);
4214 
4215 		if (ddi_copyout(server_pg_list, (caddr_t)arg, server_pg_list_sz,
4216 		    mode) != 0) {
4217 			rtn = EFAULT;
4218 		}
4219 		DTRACE_PROBE1(iscsi_ioctl_iscsi_isns_server_get_pg_sz,
4220 		    int, pg_list_sz);
4221 		kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN);
4222 		initiator_node_name = NULL;
4223 		kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN);
4224 		initiator_node_alias = NULL;
4225 		kmem_free(pg_list, pg_list_sz);
4226 		pg_list = NULL;
4227 		kmem_free(server_pg_list, server_pg_list_sz);
4228 		server_pg_list = NULL;
4229 		kmem_free(server_pg_list_hdr, sizeof (*server_pg_list_hdr));
4230 		server_pg_list_hdr = NULL;
4231 		break;
4232 
4233 	/*
4234 	 * ISCSI_GET_CONFIG_SESSIONS --
4235 	 */
4236 	case ISCSI_GET_CONFIG_SESSIONS:
4237 		/* FALLTHRU */
4238 
4239 	case ISCSI_SET_CONFIG_SESSIONS:
4240 		size = sizeof (*ics);
4241 		ics = iscsi_ioctl_copyin((caddr_t)arg, mode, size);
4242 		if (ics == NULL) {
4243 			rtn = EFAULT;
4244 			break;
4245 		}
4246 
4247 		/* verify version infomration */
4248 		if (ics->ics_ver != ISCSI_INTERFACE_VERSION) {
4249 			rtn = EINVAL;
4250 			kmem_free(ics, size);
4251 			ics = NULL;
4252 			break;
4253 		}
4254 
4255 		/* Check to see if we need to copy in more memory */
4256 		if (ics->ics_in > 1) {
4257 			/* record correct size */
4258 			size = ISCSI_SESSION_CONFIG_SIZE(ics->ics_in);
4259 			/* free old buffer */
4260 			kmem_free(ics, sizeof (*ics));
4261 
4262 			/* copy in complete buffer size */
4263 			ics = iscsi_ioctl_copyin((caddr_t)arg, mode, size);
4264 			if (ics == NULL) {
4265 				rtn = EFAULT;
4266 				break;
4267 			}
4268 		}
4269 
4270 		/* switch action based on get or set */
4271 		if (cmd == ISCSI_GET_CONFIG_SESSIONS) {
4272 			/* get */
4273 			rtn = iscsi_ioctl_get_config_sess(ihp, ics);
4274 			if (rtn == 0) {
4275 				/* copyout data for gets */
4276 				rtn = iscsi_ioctl_copyout(ics, size,
4277 				    (caddr_t)arg, mode);
4278 			} else {
4279 				kmem_free(ics, size);
4280 				ics = NULL;
4281 			}
4282 		} else {
4283 			/* set */
4284 			rtn = iscsi_ioctl_set_config_sess(ihp, ics);
4285 			if (iscsiboot_prop) {
4286 				if (iscsi_cmp_boot_sess_oid(ihp,
4287 				    ics->ics_oid)) {
4288 					/*
4289 					 * found active session for this object
4290 					 * or this is initiator object
4291 					 * with mpxio enabled
4292 					 */
4293 					if (!iscsi_reconfig_boot_sess(ihp)) {
4294 						kmem_free(ics, size);
4295 						ics = NULL;
4296 						rtn = EINVAL;
4297 						break;
4298 					}
4299 				}
4300 			}
4301 			kmem_free(ics, size);
4302 			ics = NULL;
4303 		}
4304 		break;
4305 
4306 	case ISCSI_IS_ACTIVE:
4307 		/*
4308 		 * dhcpagent calls here to check if there are
4309 		 * active iSCSI sessions
4310 		 */
4311 		instance = 0;
4312 		if (iscsiboot_prop) {
4313 			instance = 1;
4314 		}
4315 		if (!instance) {
4316 			rw_enter(&ihp->hba_sess_list_rwlock,
4317 			    RW_READER);
4318 			for (isp = ihp->hba_sess_list; isp;
4319 			    isp = isp->sess_next) {
4320 				if ((isp->sess_state ==
4321 				    ISCSI_SESS_STATE_LOGGED_IN) &&
4322 				    (isp->sess_lun_list !=
4323 				    NULL)) {
4324 					instance = 1;
4325 					break;
4326 				}
4327 			}
4328 			rw_exit(&ihp->hba_sess_list_rwlock);
4329 		}
4330 		size = sizeof (instance);
4331 		if (ddi_copyout(&instance, (caddr_t)arg, size,
4332 		    mode) != 0) {
4333 			rtn = EFAULT;
4334 		}
4335 		break;
4336 
4337 	case ISCSI_BOOTPROP_GET:
4338 		size = sizeof (*bootProp);
4339 		bootProp = iscsi_ioctl_copyin((caddr_t)arg, mode, size);
4340 		if (bootProp == NULL) {
4341 			rtn = EFAULT;
4342 			break;
4343 		}
4344 		bootProp->hba_mpxio_enabled =
4345 		    iscsi_chk_bootlun_mpxio(ihp);
4346 		if (iscsiboot_prop == NULL) {
4347 			bootProp->iscsiboot = 0;
4348 			rtn = iscsi_ioctl_copyout(bootProp, size,
4349 			    (caddr_t)arg, mode);
4350 			break;
4351 		} else {
4352 			bootProp->iscsiboot = 1;
4353 		}
4354 
4355 		if (iscsiboot_prop->boot_init.ini_name != NULL) {
4356 			(void) strncpy((char *)bootProp->ini_name.n_name,
4357 			    (char *)iscsiboot_prop->boot_init.ini_name,
4358 			    ISCSI_MAX_NAME_LEN);
4359 		}
4360 		if (iscsiboot_prop->boot_init.ini_chap_name != NULL) {
4361 			bootProp->auth.a_auth_method = authMethodCHAP;
4362 			(void) strncpy((char *)bootProp->ini_chap.c_user,
4363 			    (char *)iscsiboot_prop->boot_init.ini_chap_name,
4364 			    ISCSI_MAX_NAME_LEN);
4365 			(void) strncpy((char *)bootProp->ini_chap.c_secret,
4366 			    (char *)iscsiboot_prop->boot_init.ini_chap_sec,
4367 			    ISCSI_CHAP_SECRET_LEN);
4368 			if (iscsiboot_prop->boot_tgt.tgt_chap_name !=
4369 			    NULL) {
4370 				bootProp->auth.a_bi_auth = B_TRUE;
4371 			} else {
4372 				bootProp->auth.a_bi_auth = B_FALSE;
4373 			}
4374 		}
4375 		if (iscsiboot_prop->boot_tgt.tgt_name != NULL) {
4376 			(void) strncpy((char *)bootProp->tgt_name.n_name,
4377 			    (char *)iscsiboot_prop->boot_tgt.tgt_name,
4378 			    ISCSI_MAX_NAME_LEN);
4379 		}
4380 		if (iscsiboot_prop->boot_tgt.tgt_chap_name != NULL) {
4381 			(void) strncpy((char *)bootProp->tgt_chap.c_user,
4382 			    (char *)iscsiboot_prop->boot_tgt.tgt_chap_name,
4383 			    ISCSI_MAX_NAME_LEN);
4384 			(void) strncpy((char *)bootProp->tgt_chap.c_secret,
4385 			    (char *)iscsiboot_prop->boot_tgt.tgt_chap_sec,
4386 			    ISCSI_CHAP_SECRET_LEN);
4387 		}
4388 
4389 		rtn = iscsi_ioctl_copyout(bootProp, size, (caddr_t)arg, mode);
4390 		break;
4391 
4392 	case ISCSI_TUNABLE_PARAM_SET:
4393 		tpss = (iscsi_tunable_object_t *)kmem_alloc(sizeof (*tpss),
4394 		    KM_SLEEP);
4395 		if (ddi_copyin((caddr_t)arg, tpss, sizeof (*tpss), mode)) {
4396 			rtn = EFAULT;
4397 			kmem_free(tpss, sizeof (*tpss));
4398 			break;
4399 		}
4400 		rtn = iscsi_ioctl_set_tunable_param(ihp, tpss);
4401 		kmem_free(tpss, sizeof (*tpss));
4402 		break;
4403 
4404 	case ISCSI_TUNABLE_PARAM_GET:
4405 		tpsg = (iscsi_tunable_object_t *)kmem_alloc(sizeof (*tpsg),
4406 		    KM_SLEEP);
4407 		if (ddi_copyin((caddr_t)arg, tpsg, sizeof (*tpsg), mode)) {
4408 			rtn = EFAULT;
4409 			kmem_free(tpsg, sizeof (*tpsg));
4410 			break;
4411 		}
4412 		if (tpsg->t_oid == ihp->hba_oid) {
4413 			/* initiator */
4414 			name = ihp->hba_name;
4415 			if (iscsi_get_persisted_tunable_param((uchar_t *)name,
4416 			    tpsg) == 1) {
4417 				/*
4418 				 * no persisted tunable parameters found
4419 				 * for iscsi initiator, use default tunable
4420 				 * params for initiator node.
4421 				 */
4422 				iscsi_get_tunable_default(tpsg);
4423 			}
4424 		} else {
4425 			/* check whether it is a target oid */
4426 			name = iscsi_targetparam_get_name(tpsg->t_oid);
4427 			if (name == NULL) {
4428 				/* invalid node name */
4429 				rtn = EINVAL;
4430 				kmem_free(tpsg, sizeof (*tpsg));
4431 				break;
4432 			}
4433 			if (iscsi_get_persisted_tunable_param((uchar_t *)name,
4434 			    tpsg) == 1) {
4435 				/*
4436 				 * no persisted tunable parameters found for
4437 				 * iscsi target, use initiator's configure.
4438 				 */
4439 				if (iscsi_get_persisted_tunable_param(
4440 				    (uchar_t *)ihp->hba_name, tpsg) == -1) {
4441 					/*
4442 					 * No initiator tunable parameters set
4443 					 * use default value for target
4444 					 */
4445 					iscsi_get_tunable_default(tpsg);
4446 				}
4447 			}
4448 		}
4449 
4450 		if (ddi_copyout(tpsg, (caddr_t)arg,
4451 		    sizeof (iscsi_tunable_object_t), mode) != 0) {
4452 			rtn = EFAULT;
4453 		}
4454 		kmem_free(tpsg, sizeof (*tpsg));
4455 		break;
4456 
4457 	default:
4458 		rtn = ENOTTY;
4459 		cmn_err(CE_NOTE, "unrecognized ioctl 0x%x", cmd);
4460 	} /* end of ioctl type switch/cases */
4461 
4462 	if ((cmd != ISCSI_SMF_ONLINE) && (cmd != ISCSI_SMF_OFFLINE) &&
4463 	    (cmd != ISCSI_SMF_GET)) {
4464 		/* other cmds need to release the service */
4465 		iscsi_client_release_service(ihp);
4466 	}
4467 
4468 	return (rtn);
4469 }
4470 
4471 /*
4472  * +--------------------------------------------------------------------+
4473  * | End of cb_ops routines					     |
4474  * +--------------------------------------------------------------------+
4475  */
4476 
4477 
4478 /*
4479  * +--------------------------------------------------------------------+
4480  * | Common scsi_tran support routines				  |
4481  * +--------------------------------------------------------------------+
4482  */
4483 
4484 /*
4485  * iscsi_i_commoncap -- SCSA host adapter get/set capability routines.
4486  *
4487  * Need to determine if any of these can be determined through the iSCSI
4488  * protocol. For now just return error on most.
4489  */
4490 /* ARGSUSED */
4491 static int
4492 iscsi_i_commoncap(struct scsi_address *ap, char *cap, int val,
4493     int tgtonly, int doset)
4494 {
4495 	int		rtn;
4496 	int		cidx;
4497 	iscsi_lun_t	*ilp;
4498 
4499 	ASSERT((ap)->a_hba_tran->tran_hba_private != NULL);
4500 	ilp	= (iscsi_lun_t *)((ap)->a_hba_tran->tran_tgt_private);
4501 	ASSERT(ilp != NULL);
4502 
4503 	if (cap == (char *)0) {
4504 		return (FALSE);
4505 	}
4506 
4507 	cidx = scsi_hba_lookup_capstr(cap);
4508 	if (cidx == -1) {
4509 		return (cidx);
4510 	}
4511 
4512 	/*
4513 	 * Process setcap request.
4514 	 */
4515 	if (doset) {
4516 		/*
4517 		 * At present, we can only set binary (0/1) values
4518 		 */
4519 		switch (cidx) {
4520 		case SCSI_CAP_LUN_RESET:
4521 			if (val) {
4522 				ilp->lun_cap |= ISCSI_LUN_CAP_RESET;
4523 			} else {
4524 				ilp->lun_cap &= ~ISCSI_LUN_CAP_RESET;
4525 			}
4526 			rtn = TRUE;
4527 			break;
4528 		default:
4529 			/*
4530 			 * None of these are settable via
4531 			 * the capability interface.
4532 			 */
4533 			rtn = FALSE;
4534 			break;
4535 		}
4536 
4537 		/*
4538 		 * Process getcap request.
4539 		 */
4540 	} else {
4541 		switch (cidx) {
4542 		case SCSI_CAP_DMA_MAX:
4543 			/* no DMA, Psuedo value */
4544 			rtn = INT32_MAX;
4545 			break;
4546 		case SCSI_CAP_INITIATOR_ID:
4547 			rtn = 7;
4548 			break;
4549 		case SCSI_CAP_ARQ:
4550 		case SCSI_CAP_RESET_NOTIFICATION:
4551 		case SCSI_CAP_TAGGED_QING:
4552 			rtn = TRUE;
4553 			break;
4554 		case SCSI_CAP_SCSI_VERSION:
4555 			rtn = SCSI_VERSION_3;
4556 			break;
4557 		case SCSI_CAP_INTERCONNECT_TYPE:
4558 			rtn = INTERCONNECT_FABRIC;
4559 			break;
4560 		case SCSI_CAP_LUN_RESET:
4561 			rtn = ((ilp->lun_cap & ISCSI_LUN_CAP_RESET) != 0) ?
4562 			    TRUE : FALSE;
4563 			break;
4564 		case SCSI_CAP_CDB_LEN:
4565 			/*
4566 			 * iSCSI RFC 3720 defines a default 16 byte
4567 			 * CDB as part of the Basic Header Segment
4568 			 * (BHS) (10.2.1) and allows for an Additional
4569 			 * Header Segment (AHS) Length of 255 * 4
4570 			 * (10.2.1.5).  The AHS length can be used
4571 			 * for different purposes two of which are
4572 			 * Extended CDB ADS (10.2.2.3) and Bidirectional
4573 			 * Expected Read-Data Length AHS (10.2.2.4).
4574 			 * The largest header of these consumes is
4575 			 * 32 bytes.  So the total Max CDB Length is
4576 			 * 16 + ((255 * 4 ) - 32) = 1004.
4577 			 */
4578 			rtn = 1004;
4579 			break;
4580 		default:
4581 			rtn = UNDEFINED;
4582 			break;
4583 		}
4584 	}
4585 	return (rtn);
4586 }
4587 
4588 /*
4589  * iscsi_virt_lun_init - attempts to complete a mdi/scsi_vhci binding
4590  *
4591  * This routine is used to associate the tran_tgt_private to our ilp
4592  * structure.  This function is indirectly called from our
4593  * iscsi_lun_create_xxx routines.  These routines must prevent
4594  * the session and lun lists from changing during this call.
4595  */
4596 /* ARGSUSED */
4597 static int
4598 iscsi_virt_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
4599     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
4600 {
4601 	iscsi_lun_t	*ilp		= NULL;
4602 	iscsi_lun_t	*ilp_check	= NULL;
4603 	iscsi_sess_t	*isp		= NULL;
4604 	char		*lun_guid	= NULL;
4605 	mdi_pathinfo_t	*pip		= NULL;
4606 	iscsi_hba_t	*ihp    = (iscsi_hba_t *)hba_tran->tran_hba_private;
4607 	char		target_port_name[MAX_NAME_PROP_SIZE];
4608 
4609 	/*
4610 	 * Here's a nice little piece of undocumented stuff.
4611 	 */
4612 	if ((pip = (mdi_pathinfo_t *)sd->sd_private) == NULL) {
4613 		/*
4614 		 * Very bad news if this occurs. Somehow SCSI_vhci has
4615 		 * lost the pathinfo node for this target.
4616 		 */
4617 		return (DDI_NOT_WELL_FORMED);
4618 	}
4619 
4620 	ilp = (iscsi_lun_t *)mdi_pi_get_phci_private(pip);
4621 
4622 	/*
4623 	 * +----------------------------------------------------+
4624 	 * | Looking to find the target device via the property |
4625 	 * | is not required since the driver can easily get    |
4626 	 * | this information from the mdi_phci_get_private()   |
4627 	 * | call above.  This is just a consistency check	|
4628 	 * | which can be removed.				|
4629 	 */
4630 	if (mdi_prop_lookup_string(pip, MDI_GUID, &lun_guid) !=
4631 	    DDI_PROP_SUCCESS) {
4632 		return (DDI_NOT_WELL_FORMED);
4633 	}
4634 
4635 	for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
4636 
4637 		/* If this isn't the matching session continue */
4638 		if (ilp->lun_sess != isp) {
4639 			continue;
4640 		}
4641 
4642 		/*
4643 		 * We are already holding the lun list rwlock
4644 		 * for this thread on the callers side of mdi_pi_online
4645 		 * or ndi_devi_online.  Which lead to this functions
4646 		 * call.
4647 		 */
4648 		for (ilp_check = isp->sess_lun_list; ilp_check;
4649 		    ilp_check = ilp_check->lun_next) {
4650 
4651 			/*
4652 			 * If this is the matching LUN and contains
4653 			 * the same LUN GUID then break we found our
4654 			 * match.
4655 			 */
4656 			if ((ilp == ilp_check) &&
4657 			    (strcmp(lun_guid, ilp_check->lun_guid) == 0)) {
4658 				break;
4659 			}
4660 		}
4661 		if (ilp_check != NULL) {
4662 			break;
4663 		}
4664 	}
4665 
4666 	/*
4667 	 * Free resource that's no longer required.
4668 	 */
4669 	if (lun_guid != NULL)
4670 		(void) mdi_prop_free(lun_guid);
4671 
4672 	if (ilp_check == NULL) {
4673 		/*
4674 		 * Failed to find iSCSI LUN in HBA chain based
4675 		 * on the GUID that was stored as a property on
4676 		 * the pathinfo node.
4677 		 */
4678 		return (DDI_NOT_WELL_FORMED);
4679 	}
4680 
4681 	if (ilp != ilp_check) {
4682 		/*
4683 		 * The iSCSI target that we found on the HBA link is
4684 		 * different than the iSCSI target that was stored as
4685 		 * private data on the pathinfo node.
4686 		 */
4687 		return (DDI_NOT_WELL_FORMED);
4688 	}
4689 	/*
4690 	 * | End of consistency check				|
4691 	 * +----------------------------------------------------+
4692 	 */
4693 
4694 	hba_tran->tran_tgt_private = ilp;
4695 
4696 	target_port_name[0] = '\0';
4697 	if (ilp->lun_sess->sess_tpgt_conf == ISCSI_DEFAULT_TPGT) {
4698 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
4699 		    "%02x%02x%02x%02x%02x%02x,%s",
4700 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
4701 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
4702 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
4703 		    ilp->lun_sess->sess_name);
4704 	} else {
4705 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
4706 		    "%02x%02x%02x%02x%02x%02x,%s,%d",
4707 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
4708 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
4709 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
4710 		    ilp->lun_sess->sess_name, ilp->lun_sess->sess_tpgt_conf);
4711 	}
4712 
4713 	if (mdi_prop_update_string(pip,
4714 	    SCSI_ADDR_PROP_TARGET_PORT, target_port_name) != DDI_PROP_SUCCESS) {
4715 		cmn_err(CE_WARN, "iscsi_virt_lun_init: Creating '"
4716 		    SCSI_ADDR_PROP_TARGET_PORT "' property on Path(%p) "
4717 		    "for Target(%s), Lun(%d) Failed",
4718 		    (void *)pip, ilp->lun_sess->sess_name, ilp->lun_num);
4719 	}
4720 
4721 	return (DDI_SUCCESS);
4722 }
4723 
4724 /*
4725  * iscsi_phys_lun_init - attempts to complete a ndi binding
4726  *
4727  * This routine is used to associate the tran_tgt_private to our
4728  * ilp structure.  This function is indirectly called from our
4729  * iscsi_lun_create_xxx routines.  These routines must prevent
4730  * the session and lun lists from changing during this call.
4731  */
4732 static int
4733 iscsi_phys_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip,
4734     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
4735 {
4736 	int		rtn	= DDI_SUCCESS;
4737 	iscsi_hba_t	*ihp	= NULL;
4738 	iscsi_sess_t	*isp	= NULL;
4739 	iscsi_lun_t	*ilp	= NULL;
4740 	char		target_port_name[MAX_NAME_PROP_SIZE];
4741 	int		*words = NULL;
4742 	uint_t		nwords = 0;
4743 
4744 	ASSERT(hba_dip);
4745 	ASSERT(lun_dip);
4746 	ASSERT(hba_tran);
4747 	ASSERT(sd);
4748 	ihp = (iscsi_hba_t *)hba_tran->tran_hba_private;
4749 	ASSERT(ihp);
4750 
4751 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, lun_dip,
4752 	    DDI_PROP_DONTPASS, LUN_PROP, &words, &nwords) != DDI_PROP_SUCCESS) {
4753 		cmn_err(CE_WARN, "iscsi_phys_lun_init: Returning DDI_FAILURE:"
4754 		    "lun for %s (instance %d)", ddi_get_name(lun_dip),
4755 		    ddi_get_instance(lun_dip));
4756 		return (DDI_FAILURE);
4757 	}
4758 
4759 	if (nwords == 0) {
4760 		ddi_prop_free(words);
4761 		return (DDI_FAILURE);
4762 	}
4763 
4764 	ASSERT(words != NULL);
4765 
4766 	/* See if we already created this session */
4767 
4768 	/* Walk the HBA's session list */
4769 	for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
4770 		/* compare target name as the unique identifier */
4771 		if (sd->sd_address.a_target == isp->sess_oid) {
4772 			/* found match */
4773 			break;
4774 		}
4775 	}
4776 
4777 	/* If we found matching session continue searching for tgt */
4778 	if (isp != NULL) {
4779 		/*
4780 		 * Search for the matching iscsi lun structure.  We don't
4781 		 * need to hold the READER for the lun list at this point.
4782 		 * because the tran_get_name is being called from the online
4783 		 * function which is already holding a reader on the lun
4784 		 * list.
4785 		 */
4786 		for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) {
4787 			if (*words == ilp->lun_num) {
4788 				/* found match */
4789 				break;
4790 			}
4791 		}
4792 
4793 		if (ilp != NULL) {
4794 			/*
4795 			 * tgt found path it to the tran_lun_private
4796 			 * this is used later for fast access on
4797 			 * init_pkt and start
4798 			 */
4799 			hba_tran->tran_tgt_private = ilp;
4800 		} else {
4801 			/* tgt not found */
4802 			ddi_prop_free(words);
4803 			return (DDI_FAILURE);
4804 		}
4805 	} else {
4806 		/* sess not found */
4807 		ddi_prop_free(words);
4808 		return (DDI_FAILURE);
4809 	}
4810 	ddi_prop_free(words);
4811 
4812 	target_port_name[0] = '\0';
4813 	if (ilp->lun_sess->sess_tpgt_conf == ISCSI_DEFAULT_TPGT) {
4814 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
4815 		    "%02x%02x%02x%02x%02x%02x,%s",
4816 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
4817 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
4818 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
4819 		    ilp->lun_sess->sess_name);
4820 	} else {
4821 		(void) snprintf(target_port_name, MAX_NAME_PROP_SIZE,
4822 		    "%02x%02x%02x%02x%02x%02x,%s,%d",
4823 		    ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1],
4824 		    ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3],
4825 		    ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5],
4826 		    ilp->lun_sess->sess_name, ilp->lun_sess->sess_tpgt_conf);
4827 	}
4828 
4829 	if (ddi_prop_update_string(DDI_DEV_T_NONE, lun_dip,
4830 	    SCSI_ADDR_PROP_TARGET_PORT, target_port_name) != DDI_PROP_SUCCESS) {
4831 		cmn_err(CE_WARN, "iscsi_phys_lun_init: Creating '"
4832 		    SCSI_ADDR_PROP_TARGET_PORT "' property on Target(%s), "
4833 		    "Lun(%d) Failed", ilp->lun_sess->sess_name, ilp->lun_num);
4834 	}
4835 
4836 	return (rtn);
4837 }
4838 
4839 /*
4840  * +--------------------------------------------------------------------+
4841  * | End of scsi_tran support routines					|
4842  * +--------------------------------------------------------------------+
4843  */
4844 
4845 /*
4846  * +--------------------------------------------------------------------+
4847  * | Begin of struct utility routines					|
4848  * +--------------------------------------------------------------------+
4849  */
4850 
4851 
4852 /*
4853  * iscsi_set_default_login_params - This function sets the
4854  * driver default login params.  This is using during the
4855  * creation of our iSCSI HBA structure initialization by
4856  * could be used at other times to reset back to the defaults.
4857  */
4858 void
4859 iscsi_set_default_login_params(iscsi_login_params_t *params)
4860 {
4861 	params->immediate_data		= ISCSI_DEFAULT_IMMEDIATE_DATA;
4862 	params->initial_r2t		= ISCSI_DEFAULT_INITIALR2T;
4863 	params->first_burst_length	= ISCSI_DEFAULT_FIRST_BURST_LENGTH;
4864 	params->max_burst_length	= ISCSI_DEFAULT_MAX_BURST_LENGTH;
4865 	params->data_pdu_in_order	= ISCSI_DEFAULT_DATA_PDU_IN_ORDER;
4866 	params->data_sequence_in_order	= ISCSI_DEFAULT_DATA_SEQUENCE_IN_ORDER;
4867 	params->default_time_to_wait	= ISCSI_DEFAULT_TIME_TO_WAIT;
4868 	params->default_time_to_retain	= ISCSI_DEFAULT_TIME_TO_RETAIN;
4869 	params->header_digest		= ISCSI_DEFAULT_HEADER_DIGEST;
4870 	params->data_digest		= ISCSI_DEFAULT_DATA_DIGEST;
4871 	params->max_recv_data_seg_len	= ISCSI_DEFAULT_MAX_RECV_SEG_LEN;
4872 	params->max_xmit_data_seg_len	= ISCSI_DEFAULT_MAX_XMIT_SEG_LEN;
4873 	params->max_connections		= ISCSI_DEFAULT_MAX_CONNECTIONS;
4874 	params->max_outstanding_r2t	= ISCSI_DEFAULT_MAX_OUT_R2T;
4875 	params->error_recovery_level	= ISCSI_DEFAULT_ERROR_RECOVERY_LEVEL;
4876 	params->ifmarker		= ISCSI_DEFAULT_IFMARKER;
4877 	params->ofmarker		= ISCSI_DEFAULT_OFMARKER;
4878 }
4879 
4880 /* Helper function to sets the driver default tunable parameters */
4881 static void
4882 iscsi_set_default_tunable_params(iscsi_tunable_params_t *params)
4883 {
4884 	params->recv_login_rsp_timeout = ISCSI_DEFAULT_RX_TIMEOUT_VALUE;
4885 	params->conn_login_max = ISCSI_DEFAULT_CONN_DEFAULT_LOGIN_MAX;
4886 	params->polling_login_delay = ISCSI_DEFAULT_LOGIN_POLLING_DELAY;
4887 }
4888 
4889 /*
4890  * +--------------------------------------------------------------------+
4891  * | End of struct utility routines				     |
4892  * +--------------------------------------------------------------------+
4893  */
4894 
4895 /*
4896  * +--------------------------------------------------------------------+
4897  * | Begin of ioctl utility routines				    |
4898  * +--------------------------------------------------------------------+
4899  */
4900 
4901 /*
4902  * iscsi_get_param - This function is a helper to ISCSI_GET_PARAM
4903  * IOCTL
4904  */
4905 int
4906 iscsi_get_param(iscsi_login_params_t *params, boolean_t valid_flag,
4907     iscsi_param_get_t *ipgp) {
4908 	int rtn = 0;
4909 
4910 	/* ---- Default to settable, possibly changed later ---- */
4911 	ipgp->g_value.v_valid    = valid_flag;
4912 	ipgp->g_value.v_settable = B_TRUE;
4913 
4914 	switch (ipgp->g_param) {
4915 	/*
4916 	 * Boolean parameters
4917 	 */
4918 	case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER:
4919 		ipgp->g_value.v_bool.b_current =
4920 		    params->data_sequence_in_order;
4921 		ipgp->g_value.v_bool.b_default =
4922 		    ISCSI_DEFAULT_DATA_SEQUENCE_IN_ORDER;
4923 		break;
4924 	case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA:
4925 		ipgp->g_value.v_bool.b_current =
4926 		    params->immediate_data;
4927 		ipgp->g_value.v_bool.b_default =
4928 		    ISCSI_DEFAULT_IMMEDIATE_DATA;
4929 		break;
4930 	case ISCSI_LOGIN_PARAM_INITIAL_R2T:
4931 		ipgp->g_value.v_bool.b_current =
4932 		    params->initial_r2t;
4933 		ipgp->g_value.v_bool.b_default =
4934 		    ISCSI_DEFAULT_IMMEDIATE_DATA;
4935 		break;
4936 	case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER:
4937 		ipgp->g_value.v_bool.b_current =
4938 		    params->data_pdu_in_order;
4939 		ipgp->g_value.v_bool.b_default =
4940 		    ISCSI_DEFAULT_DATA_PDU_IN_ORDER;
4941 		break;
4942 
4943 	/*
4944 	 * Integer parameters
4945 	 */
4946 	case ISCSI_LOGIN_PARAM_HEADER_DIGEST:
4947 		ipgp->g_value.v_integer.i_current = params->header_digest;
4948 		ipgp->g_value.v_integer.i_default = ISCSI_DEFAULT_HEADER_DIGEST;
4949 		ipgp->g_value.v_integer.i_min = 0;
4950 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_HEADER_DIGEST;
4951 		ipgp->g_value.v_integer.i_incr = 1;
4952 		break;
4953 	case ISCSI_LOGIN_PARAM_DATA_DIGEST:
4954 		ipgp->g_value.v_integer.i_current = params->data_digest;
4955 		ipgp->g_value.v_integer.i_default = ISCSI_DEFAULT_DATA_DIGEST;
4956 		ipgp->g_value.v_integer.i_min = 0;
4957 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_DATA_DIGEST;
4958 		ipgp->g_value.v_integer.i_incr = 1;
4959 		break;
4960 	case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN:
4961 		ipgp->g_value.v_integer.i_current =
4962 		    params->default_time_to_retain;
4963 		ipgp->g_value.v_integer.i_default =
4964 		    ISCSI_DEFAULT_TIME_TO_RETAIN;
4965 		ipgp->g_value.v_integer.i_min = 0;
4966 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_TIME2RETAIN;
4967 		ipgp->g_value.v_integer.i_incr = 1;
4968 		break;
4969 	case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT:
4970 		ipgp->g_value.v_integer.i_current =
4971 		    params->default_time_to_wait;
4972 		ipgp->g_value.v_integer.i_default =
4973 		    ISCSI_DEFAULT_TIME_TO_WAIT;
4974 		ipgp->g_value.v_integer.i_min = 0;
4975 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_TIME2WAIT;
4976 		ipgp->g_value.v_integer.i_incr = 1;
4977 		break;
4978 	case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL:
4979 		ipgp->g_value.v_integer.i_current =
4980 		    params->error_recovery_level;
4981 		ipgp->g_value.v_integer.i_default =
4982 		    ISCSI_DEFAULT_ERROR_RECOVERY_LEVEL;
4983 		ipgp->g_value.v_integer.i_min = 0;
4984 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_ERROR_RECOVERY_LEVEL;
4985 		ipgp->g_value.v_integer.i_incr = 1;
4986 		ipgp->g_value.v_settable = B_FALSE;
4987 		break;
4988 	case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH:
4989 		ipgp->g_value.v_integer.i_current =
4990 		    params->first_burst_length;
4991 		ipgp->g_value.v_integer.i_default =
4992 		    ISCSI_DEFAULT_FIRST_BURST_LENGTH;
4993 		ipgp->g_value.v_integer.i_min = 512;
4994 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_FIRST_BURST_LENGTH;
4995 		ipgp->g_value.v_integer.i_incr = 1;
4996 		break;
4997 	case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH:
4998 		ipgp->g_value.v_integer.i_current =
4999 		    params->max_burst_length;
5000 		ipgp->g_value.v_integer.i_default =
5001 		    ISCSI_DEFAULT_MAX_BURST_LENGTH;
5002 		ipgp->g_value.v_integer.i_min = 512;
5003 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_BURST_LENGTH;
5004 		ipgp->g_value.v_integer.i_incr = 1;
5005 		break;
5006 	case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS:
5007 		ipgp->g_value.v_integer.i_current =
5008 		    params->max_connections;
5009 		ipgp->g_value.v_settable = B_FALSE;
5010 		ipgp->g_value.v_integer.i_default =
5011 		    ISCSI_DEFAULT_MAX_CONNECTIONS;
5012 		ipgp->g_value.v_integer.i_min = 1;
5013 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_CONNECTIONS;
5014 		ipgp->g_value.v_integer.i_incr = 1;
5015 		break;
5016 	case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T:
5017 		ipgp->g_value.v_integer.i_current =
5018 		    params->max_outstanding_r2t;
5019 		ipgp->g_value.v_settable = B_FALSE;
5020 		ipgp->g_value.v_integer.i_default =
5021 		    ISCSI_DEFAULT_MAX_OUT_R2T;
5022 		ipgp->g_value.v_integer.i_min = 1;
5023 		ipgp->g_value.v_integer.i_max = ISCSI_MAX_OUTSTANDING_R2T;
5024 		ipgp->g_value.v_integer.i_incr = 1;
5025 		break;
5026 	case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH:
5027 		ipgp->g_value.v_integer.i_current =
5028 		    params->max_recv_data_seg_len;
5029 		ipgp->g_value.v_integer.i_default =
5030 		    ISCSI_DEFAULT_MAX_RECV_SEG_LEN;
5031 		ipgp->g_value.v_integer.i_min = 512;
5032 		ipgp->g_value.v_integer.i_max =
5033 		    ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
5034 		ipgp->g_value.v_integer.i_incr = 1;
5035 		break;
5036 	default:
5037 		rtn = EINVAL;
5038 	}
5039 
5040 	return (rtn);
5041 }
5042 
5043 /*
5044  * +--------------------------------------------------------------------+
5045  * | End of ioctl utility routines                                      |
5046  * +--------------------------------------------------------------------+
5047  */
5048 
5049 /*
5050  * iscsi_get_name_from_iqn - Translates a normal iqn/eui into a
5051  * IEEE safe address.  IEEE addresses have a number of characters
5052  * set aside as reserved.
5053  */
5054 static void
5055 iscsi_get_name_from_iqn(char *name, int name_max_len)
5056 {
5057 	char	*tmp		= NULL;
5058 	char	*oldch		= NULL;
5059 	char	*newch		= NULL;
5060 
5061 	tmp = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP);
5062 
5063 	for (oldch = &name[0], newch = &tmp[0]; *oldch != '\0';
5064 	    oldch++, newch++) {
5065 		switch (*oldch) {
5066 		case ':':
5067 			*newch++ = '%';
5068 			*newch++ = '3';
5069 			*newch = 'A';
5070 			break;
5071 		case ' ':
5072 			*newch++ = '%';
5073 			*newch++ = '2';
5074 			*newch = '0';
5075 			break;
5076 		case '@':
5077 			*newch++ = '%';
5078 			*newch++ = '4';
5079 			*newch = '0';
5080 			break;
5081 		case '/':
5082 			*newch++ = '%';
5083 			*newch++ = '2';
5084 			*newch = 'F';
5085 			break;
5086 		default:
5087 			*newch = *oldch;
5088 		}
5089 	}
5090 	(void) strncpy(name, tmp, name_max_len);
5091 	kmem_free(tmp, MAX_GET_NAME_SIZE);
5092 }
5093 
5094 /*
5095  * iscsi_get_name_to_iqn - Converts IEEE safe address back
5096  * into a iscsi iqn/eui.
5097  */
5098 static void
5099 iscsi_get_name_to_iqn(char *name, int name_max_len)
5100 {
5101 	char	*tmp		= NULL;
5102 	char	*oldch		= NULL;
5103 	char	*newch		= NULL;
5104 
5105 	tmp = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP);
5106 
5107 	for (oldch = &name[0], newch = &tmp[0]; *oldch != '\0';
5108 	    oldch++, newch++) {
5109 		if (*oldch == '%') {
5110 			switch (*(oldch+1)) {
5111 			case '2':
5112 				if (*(oldch+2) == '0') {
5113 					*newch = ' ';
5114 					oldch += 2;
5115 				} else if (*(oldch+2) == 'F') {
5116 					*newch = '/';
5117 					oldch += 2;
5118 				} else {
5119 					*newch = *oldch;
5120 				}
5121 				break;
5122 			case '3':
5123 				if (*(oldch+2) == 'A') {
5124 					*newch = ':';
5125 					oldch += 2;
5126 				} else {
5127 					*newch = *oldch;
5128 				}
5129 				break;
5130 			case '4':
5131 				if (*(oldch+2) == '0') {
5132 					*newch = '@';
5133 					oldch += 2;
5134 				} else {
5135 					*newch = *oldch;
5136 				}
5137 				break;
5138 			default:
5139 				*newch = *oldch;
5140 			}
5141 		} else {
5142 			*newch = *oldch;
5143 		}
5144 	}
5145 	(void) strncpy(name, tmp, name_max_len);
5146 	kmem_free(tmp, MAX_GET_NAME_SIZE);
5147 }
5148 
5149 /*
5150  * iscsi_get_persisted_param * - a helper to ISCSI_GET_PARAM ioctl
5151  *
5152  * On return 0 means persisted parameter found
5153  */
5154 int
5155 iscsi_get_persisted_param(uchar_t *name, iscsi_param_get_t *ipgp,
5156     iscsi_login_params_t *params)
5157 {
5158 	int rtn = 1;
5159 	persistent_param_t *pparam;
5160 
5161 	if (name == NULL || strlen((char *)name) == 0) {
5162 		return (rtn);
5163 	}
5164 
5165 	pparam = (persistent_param_t *)kmem_zalloc(sizeof (*pparam), KM_SLEEP);
5166 
5167 	if (persistent_param_get((char *)name, pparam) == B_TRUE) {
5168 		if (pparam->p_bitmap & (1 << ipgp->g_param)) {
5169 			/* Found configured parameter. */
5170 			bcopy(&pparam->p_params, params, sizeof (*params));
5171 			rtn = 0;
5172 		}
5173 	}
5174 
5175 	kmem_free(pparam, sizeof (*pparam));
5176 
5177 	return (rtn);
5178 }
5179 
5180 /*
5181  * iscsi_override_target_default - helper function set the target's default
5182  * login parameter if there is a configured initiator parameter.
5183  *
5184  */
5185 static void
5186 iscsi_override_target_default(iscsi_hba_t *ihp, iscsi_param_get_t *ipg)
5187 {
5188 	persistent_param_t *pp;
5189 	iscsi_login_params_t *params;
5190 
5191 	pp = (persistent_param_t *)kmem_zalloc(sizeof (*pp), KM_SLEEP);
5192 	if (persistent_param_get((char *)ihp->hba_name, pp) == B_TRUE) {
5193 		if (pp->p_bitmap & (1 << ipg->g_param)) {
5194 			params = &pp->p_params;
5195 			switch (ipg->g_param) {
5196 			case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER:
5197 				ipg->g_value.v_bool.b_default =
5198 				    params->data_sequence_in_order;
5199 				break;
5200 			case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA:
5201 				ipg->g_value.v_bool.b_default =
5202 				    params->immediate_data;
5203 				break;
5204 			case ISCSI_LOGIN_PARAM_INITIAL_R2T:
5205 				ipg->g_value.v_bool.b_default =
5206 				    params->initial_r2t;
5207 				break;
5208 			case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER:
5209 				ipg->g_value.v_bool.b_default =
5210 				    params->data_pdu_in_order;
5211 				break;
5212 			case ISCSI_LOGIN_PARAM_HEADER_DIGEST:
5213 				ipg->g_value.v_integer.i_default =
5214 				    params->header_digest;
5215 				break;
5216 			case ISCSI_LOGIN_PARAM_DATA_DIGEST:
5217 				ipg->g_value.v_integer.i_default =
5218 				    params->data_digest;
5219 				break;
5220 			case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN:
5221 				ipg->g_value.v_integer.i_default =
5222 				    params->default_time_to_retain;
5223 				break;
5224 			case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT:
5225 				ipg->g_value.v_integer.i_default =
5226 				    params->default_time_to_wait;
5227 				break;
5228 			case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL:
5229 				ipg->g_value.v_integer.i_default =
5230 				    params->error_recovery_level;
5231 				break;
5232 			case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH:
5233 				ipg->g_value.v_integer.i_default =
5234 				    params->first_burst_length;
5235 				break;
5236 			case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH:
5237 				ipg->g_value.v_integer.i_default =
5238 				    params->max_burst_length;
5239 				break;
5240 			case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS:
5241 				ipg->g_value.v_integer.i_default =
5242 				    params->max_connections;
5243 				break;
5244 			case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T:
5245 				ipg->g_value.v_integer.i_default =
5246 				    params->max_outstanding_r2t;
5247 				break;
5248 			case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH:
5249 				ipg->g_value.v_integer.i_default =
5250 				    params->max_xmit_data_seg_len;
5251 				break;
5252 			default:
5253 				break;
5254 			}
5255 		}
5256 	}
5257 	kmem_free(pp, sizeof (*pp));
5258 }
5259 
5260 static boolean_t
5261 iscsi_cmp_boot_sess_oid(iscsi_hba_t *ihp, uint32_t oid)
5262 {
5263 	iscsi_sess_t *isp = NULL;
5264 
5265 	if (iscsi_chk_bootlun_mpxio(ihp)) {
5266 		for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) {
5267 			if ((isp->sess_oid == oid) && isp->sess_boot) {
5268 				/* oid is session object */
5269 				break;
5270 			}
5271 			if ((isp->sess_target_oid == oid) && isp->sess_boot) {
5272 				/*
5273 				 * oid is target object while
5274 				 * this session is boot session
5275 				 */
5276 				break;
5277 			}
5278 		}
5279 		if (oid == ihp->hba_oid) {
5280 			/* oid is initiator object id */
5281 			return (B_TRUE);
5282 		} else if ((isp != NULL) && (isp->sess_boot)) {
5283 			/* oid is boot session object id */
5284 			return (B_TRUE);
5285 		}
5286 	}
5287 	return (B_FALSE);
5288 }
5289 
5290 /*
5291  * iscsi_client_request_service - request the iSCSI service
5292  *     returns true if the service is enabled and increases the count
5293  *     returns false if the service is disabled
5294  *     blocks until the service status is either enabled or disabled
5295  */
5296 boolean_t
5297 iscsi_client_request_service(iscsi_hba_t *ihp) {
5298 	boolean_t	rval = B_TRUE;
5299 
5300 	mutex_enter(&ihp->hba_service_lock);
5301 	while ((ihp->hba_service_status == ISCSI_SERVICE_TRANSITION) ||
5302 	    (ihp->hba_service_client_count == UINT_MAX)) {
5303 		cv_wait(&ihp->hba_service_cv, &ihp->hba_service_lock);
5304 	}
5305 	if (ihp->hba_service_status == ISCSI_SERVICE_ENABLED) {
5306 		ihp->hba_service_client_count++;
5307 	} else {
5308 		rval = B_FALSE;
5309 	}
5310 	mutex_exit(&ihp->hba_service_lock);
5311 
5312 	return (rval);
5313 }
5314 
5315 /*
5316  * iscsi_client_release_service - decrease the count and wake up
5317  *     blocking threads if the count reaches zero
5318  */
5319 void
5320 iscsi_client_release_service(iscsi_hba_t *ihp) {
5321 	mutex_enter(&ihp->hba_service_lock);
5322 	ASSERT(ihp->hba_service_client_count > 0);
5323 	ihp->hba_service_client_count--;
5324 	if (ihp->hba_service_client_count == 0) {
5325 		cv_broadcast(&ihp->hba_service_cv);
5326 	}
5327 	mutex_exit(&ihp->hba_service_lock);
5328 }
5329 
5330 /*
5331  * iscsi_enter_service_zone - enter the service zone, should be called
5332  * before doing any modifications to the service status
5333  * return TRUE if the zone is entered
5334  *	  FALSE if no need to enter the zone
5335  */
5336 static boolean_t
5337 iscsi_enter_service_zone(iscsi_hba_t *ihp, uint32_t status) {
5338 	if ((status != ISCSI_SERVICE_ENABLED) &&
5339 	    (status != ISCSI_SERVICE_DISABLED)) {
5340 		return (B_FALSE);
5341 	}
5342 
5343 	mutex_enter(&ihp->hba_service_lock);
5344 	while (ihp->hba_service_status == ISCSI_SERVICE_TRANSITION) {
5345 		cv_wait(&ihp->hba_service_cv, &ihp->hba_service_lock);
5346 	}
5347 	if (ihp->hba_service_status == status) {
5348 		mutex_exit(&ihp->hba_service_lock);
5349 		return (B_FALSE);
5350 	}
5351 	ihp->hba_service_status = ISCSI_SERVICE_TRANSITION;
5352 	while (ihp->hba_service_client_count > 0) {
5353 		cv_wait(&ihp->hba_service_cv, &ihp->hba_service_lock);
5354 	}
5355 	mutex_exit(&ihp->hba_service_lock);
5356 	return (B_TRUE);
5357 }
5358 
5359 /*
5360  * iscsi_exit_service_zone - exits the service zone and wakes up waiters
5361  */
5362 static void
5363 iscsi_exit_service_zone(iscsi_hba_t *ihp, uint32_t status) {
5364 	if ((status != ISCSI_SERVICE_ENABLED) &&
5365 	    (status != ISCSI_SERVICE_DISABLED)) {
5366 		return;
5367 	}
5368 
5369 	mutex_enter(&ihp->hba_service_lock);
5370 	ASSERT(ihp->hba_service_status == ISCSI_SERVICE_TRANSITION);
5371 	ihp->hba_service_status = status;
5372 	cv_broadcast(&ihp->hba_service_cv);
5373 	mutex_exit(&ihp->hba_service_lock);
5374 }
5375 
5376 static void
5377 iscsi_check_miniroot(iscsi_hba_t *ihp) {
5378 	if (strncmp(rootfs.bo_name, "/ramdisk", 8) == 0) {
5379 		/*
5380 		 * in miniroot we don't have the persistent store
5381 		 * so just to need to ensure an enabled status
5382 		 */
5383 		ihp->hba_service_status = ISCSI_SERVICE_ENABLED;
5384 	}
5385 }
5386 
5387 static void
5388 iscsi_get_tunable_default(iscsi_tunable_object_t *param) {
5389 	int	param_id = 0;
5390 
5391 	param_id = 1 << (param->t_param - 1);
5392 	param->t_set = B_FALSE;
5393 	switch (param_id) {
5394 	case ISCSI_TUNABLE_PARAM_RX_TIMEOUT_VALUE:
5395 		param->t_value.v_integer = ISCSI_DEFAULT_RX_TIMEOUT_VALUE;
5396 		break;
5397 	case ISCSI_TUNABLE_PARAM_LOGIN_POLLING_DELAY:
5398 		param->t_value.v_integer = ISCSI_DEFAULT_LOGIN_POLLING_DELAY;
5399 		break;
5400 	case ISCSI_TUNABLE_PARAM_CONN_LOGIN_MAX:
5401 		param->t_value.v_integer = ISCSI_DEFAULT_CONN_DEFAULT_LOGIN_MAX;
5402 		break;
5403 	default:
5404 		break;
5405 	}
5406 }
5407 
5408 /*
5409  * iscsi_get_persisted_tunable_param * - a helper to ISCSI_TUNABLE_PARAM_GET
5410  * ioctl
5411  * return:
5412  *    0 	persisted tunable parameter found
5413  *    1		persisted tunable parameter not found
5414  */
5415 static int
5416 iscsi_get_persisted_tunable_param(uchar_t *name, iscsi_tunable_object_t *tpsg)
5417 {
5418 	int rtn = 1;
5419 	int param_id = 0;
5420 	persistent_tunable_param_t *pparam;
5421 
5422 	if ((name == NULL) || strlen((char *)name) == 0) {
5423 		return (rtn);
5424 	}
5425 
5426 	tpsg->t_set = B_FALSE;
5427 	pparam = (persistent_tunable_param_t *)kmem_zalloc(sizeof (*pparam),
5428 	    KM_SLEEP);
5429 	if (persistent_get_tunable_param((char *)name, pparam) == B_TRUE) {
5430 		if (pparam->p_bitmap & (1 << (tpsg->t_param - 1))) {
5431 			tpsg->t_set = B_TRUE;
5432 			param_id = 1 << (tpsg->t_param - 1);
5433 			switch (param_id) {
5434 			case ISCSI_TUNABLE_PARAM_RX_TIMEOUT_VALUE:
5435 				tpsg->t_value.v_integer =
5436 				    pparam->p_params.recv_login_rsp_timeout;
5437 				break;
5438 			case ISCSI_TUNABLE_PARAM_LOGIN_POLLING_DELAY:
5439 				tpsg->t_value.v_integer =
5440 				    pparam->p_params.polling_login_delay;
5441 				break;
5442 			case ISCSI_TUNABLE_PARAM_CONN_LOGIN_MAX:
5443 				tpsg->t_value.v_integer =
5444 				    pparam->p_params.conn_login_max;
5445 				break;
5446 			default:
5447 				break;
5448 			}
5449 			rtn = 0;
5450 		}
5451 	}
5452 
5453 	kmem_free(pparam, sizeof (*pparam));
5454 
5455 	return (rtn);
5456 }
5457