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