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